Browse Source

Add of bash script and update flip helper program

Jérôme BUISINE 4 months ago
parent
commit
e5b83e12c8
5 changed files with 94 additions and 99 deletions
  1. 5 5
      CMakeLists.txt
  2. 50 0
      convert_folder.sh
  3. 3 1
      main/CMakeLists.txt
  4. 35 92
      main/rawls_h_flip.cpp
  5. 1 1
      main/rawls_merge.cpp

+ 5 - 5
CMakeLists.txt

@@ -1,9 +1,9 @@
 # CMakeLists files in this project can
-# refer to the root source directory of the project as ${rawls-reader_SOURCE_DIR} and
-# to the root binary directory of the project as ${rawls-reader_BINARY_DIR}.
-cmake_minimum_required (VERSION 3.16.2)
-project(rawls-reader)
+# refer to the root source directory of the project as ${rawls-tools_SOURCE_DIR} and
+# to the root binary directory of the project as ${rawls-tools_BINARY_DIR}.
+cmake_minimum_required (VERSION 3.10.2)
+project(rawls-tools)
 
 add_subdirectory(lodepng)
 add_subdirectory(rawls)
-add_subdirectory(main)
+add_subdirectory(main)

+ 50 - 0
convert_folder.sh

@@ -0,0 +1,50 @@
+#! /bin/bash
+if [ -z "$1" ]
+  then
+    echo "No argument supplied"
+    echo "Need folder argument"
+    exit 1
+fi
+
+if [ -z "$2" ]
+  then
+    echo "No argument supplied"
+    echo "Need output extension"
+    exit 1
+fi
+
+if [ -z "$3" ]
+  then
+    echo "No argument supplied"
+    echo "Need output folder"
+    exit 1
+fi
+
+
+main_folder=$1
+prefix="p3d_"
+
+ext=$2
+output_folder=$3
+
+for folder in $(ls -d -- ${main_folder}*/)
+do
+  for scene in $(ls -d $folder)
+  do
+    for file in $(ls $folder)
+    do 
+        filename=$folder$file
+        filename_fixed=${filename//\/\//\/}
+
+        IFS='.' read -ra ADDR <<< "${file}"
+
+        filename_without_ext=${ADDR[0]}
+        outfile="${filename_without_ext}.${ext}"
+
+        # check if filename contains 
+        if [[ "$file" == ${prefix}* ]]; then
+            ./main/rawls_convert --image ${filename_fixed} --outfile ${output_folder}/${outfile}
+        fi
+    done 
+  done
+done

+ 3 - 1
main/CMakeLists.txt

@@ -4,6 +4,8 @@ add_executable(rawls_h_flip rawls_h_flip.cpp)
 
 target_link_libraries(rawls_convert LINK_PUBLIC rawls)
 target_link_libraries(rawls_merge LINK_PUBLIC rawls)
+target_link_libraries(rawls_h_flip LINK_PUBLIC rawls)
 
 set_property(TARGET rawls_merge PROPERTY CXX_STANDARD 17)
-set_property(TARGET rawls_convert PROPERTY CXX_STANDARD 17)
+set_property(TARGET rawls_convert PROPERTY CXX_STANDARD 17)
+set_property(TARGET rawls_h_flip PROPERTY CXX_STANDARD 17)

+ 35 - 92
main/rawls_h_flip.cpp

@@ -1,11 +1,6 @@
 #include <stdio.h>
-#include <string.h>
-#include <iostream>
-#include <fstream>
-#include <vector>
-#include <math.h>
 
-#include <bitset>
+#include "rawls.h"
 
 int main(int argc, char *argv[]){
 
@@ -21,102 +16,50 @@ int main(int argc, char *argv[]){
     }
     
 
-    std::cout << "Read image `" << imageName << "` and save it into " << outfileName << std::endl;
+    // extract data from image
+    unsigned width, height, nbChanels;
+    float* buffer;
+    float* outputBuffer;
 
-    std::ifstream rf(imageName, std::ios::out | std::ios::binary);
+    std::tuple<unsigned, unsigned, unsigned, float*> data = rawls::getDataRAWLS(imageName);
 
-    if(!rf) {
-      std::cout << "Cannot open file!" << std::endl;
-      return 1;
-    }
-
-
-    std::string line; 
-    int nbChanels, width, height;
-    char c; // to get space of new line char
-
-    // READ IHDR info
-    bool ihdrBegin = false;
-
-    while (!ihdrBegin && std::getline(rf, line)) { 
-
-        if (line.find(std::string("IHDR")) != std::string::npos){
-            ihdrBegin = true;
-            std::getline(rf, line); // avoid data size line
-
-            rf.read((char *) &width, sizeof(int));
-            rf.get(c);
-            rf.read((char *) &height, sizeof(int));
-            rf.get(c);
-            rf.read((char *) &nbChanels, sizeof(int));
-            rf.get(c);
-        }
-    }
-
-  
-    bool dataBegin = false;
-
-
-    // READ DATA info
-    // case of data chunck begin
-    while (!dataBegin && std::getline(rf, line)) { 
-        
-        if (line.find(std::string("DATA")) != std::string::npos){
-            dataBegin = true;
-        }
-    }
-
-    std::getline(rf, line);
-
-    int size = std::stoi(line);
+    width = std::get<0>(data);
+    height = std::get<1>(data);
+    nbChanels = std::get<2>(data);
+    buffer = std::get<3>(data);
 
+    outputBuffer = new float[width * height * nbChanels];
 
-    // compute number of pixels into image
-    int nbPixels = (size / (nbChanels * 4));
-
-
-    // create outfile 
-    // now write image as .ppm
-    std::ofstream outfile(outfileName);
-
-    outfile << "P" << nbChanels << std::endl;
-    outfile << width << " " << height << std::endl;
-    outfile << "255" << std::endl;
-
-
-    for (int i = 0; i < height; i++){
-
-        float widthValues[width][nbChanels];
-
-        for (int j = 0; j < width; j++){
+    
+    for(unsigned y = 0; y < height; y++){
 
-             // for each chanel read and keep save float value into new file
-            for(int k = 0; k < nbChanels; k++){
-                rf.read((char *) &widthValues[j][k], sizeof(float));  
-            } 
+        for(unsigned x = 0; x < width; x++) {
 
-            // go to next line
-            rf.get(c);
-        }
+            unsigned reversedWidth = width - x;
 
-        for (int j = width; j > 0; j--){
-            
-            for(int k = 0; k < nbChanels; k++){
-                outfile << int(widthValues[j][k]) << " "; 
+            for(unsigned j = 0; j < nbChanels; j++){
+                
+                float value = buffer[nbChanels * width * y + nbChanels * reversedWidth + j];
+                outputBuffer[nbChanels * width * y + nbChanels * x + j] = value;
             }
         }
-
-        outfile << std::endl;
     }
 
-    rf.close();
-
-    if(!rf.good()) {
-        std::cout << "Error occurred at reading time!" << std::endl;
-        return 1;
+    // create outfile 
+    if (rawls::HasExtension(outfileName, ".ppm")){
+        rawls::saveAsPPM(width, height, nbChanels, outputBuffer, outfileName);
+    } 
+    else if (rawls::HasExtension(outfileName, ".png")){
+        rawls::saveAsPNG(width, height, nbChanels, outputBuffer, outfileName);
+    } 
+    else if (rawls::HasExtension(outfileName, ".rawls") || rawls::HasExtension(outfileName, ".rawls_20")){
+        // need to get comments from an image
+        std::string comments = rawls::getCommentsRAWLS(imageName);
+
+        // Here no gamma conversion is done, only mean of samples
+        rawls::saveAsRAWLS(width, height, nbChanels, comments, outputBuffer, outfileName);
+    } 
+    else{
+        std::cout << "Unexpected output extension image" << std::endl;
     }
-
-    std::cout << "Image is now flipped and saved as .ppm into " << outfileName << std::endl;
-
-    outfile.close();
 }

+ 1 - 1
main/rawls_merge.cpp

@@ -100,7 +100,7 @@ int main(int argc, char *argv[]){
 
             for(unsigned x = 0; x < width; x++) {
 
-                for(int j = 0; j < nbChanels; j++){
+                for(unsigned j = 0; j < nbChanels; j++){
                     
                     float value = buffer[nbChanels * width * y + nbChanels * x + j];
                     outputBuffer[nbChanels * width * y + nbChanels * x + j] = outputBuffer[nbChanels * width * y + nbChanels * x + j] + value;