rawls_merge.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include <iostream>
  2. #include <string.h>
  3. #include <memory>
  4. #include "lodepng.h"
  5. #include "rawls.h"
  6. #include <algorithm>
  7. // We haven't checked which filesystem to include yet
  8. #ifndef INCLUDE_STD_FILESYSTEM_EXPERIMENTAL
  9. // Check for feature test macro for <filesystem>
  10. # if defined(__cpp_lib_filesystem)
  11. # define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 0
  12. // Check for feature test macro for <experimental/filesystem>
  13. # elif defined(__cpp_lib_experimental_filesystem)
  14. # define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 1
  15. // We can't check if headers exist...
  16. // Let's assume experimental to be safe
  17. # elif !defined(__has_include)
  18. # define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 1
  19. // Check if the header "<filesystem>" exists
  20. # elif __has_include(<filesystem>)
  21. // If we're compiling on Visual Studio and are not compiling with C++17, we need to use experimental
  22. # ifdef _MSC_VER
  23. // Check and include header that defines "_HAS_CXX17"
  24. # if __has_include(<yvals_core.h>)
  25. # include <yvals_core.h>
  26. // Check for enabled C++17 support
  27. # if defined(_HAS_CXX17) && _HAS_CXX17
  28. // We're using C++17, so let's use the normal version
  29. # define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 0
  30. # endif
  31. # endif
  32. // If the marco isn't defined yet, that means any of the other VS specific checks failed, so we need to use experimental
  33. # ifndef INCLUDE_STD_FILESYSTEM_EXPERIMENTAL
  34. # define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 1
  35. # endif
  36. // Not on Visual Studio. Let's use the normal version
  37. # else // #ifdef _MSC_VER
  38. # define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 0
  39. # endif
  40. // Check if the header "<filesystem>" exists
  41. # elif __has_include(<experimental/filesystem>)
  42. # define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 1
  43. // Fail if neither header is available with a nice error message
  44. # else
  45. # error Could not find system header "<filesystem>" or "<experimental/filesystem>"
  46. # endif
  47. // We priously determined that we need the exprimental version
  48. # if INCLUDE_STD_FILESYSTEM_EXPERIMENTAL
  49. // Include it
  50. # include <experimental/filesystem>
  51. // We need the alias from std::experimental::filesystem to std::filesystem
  52. namespace std {
  53. namespace filesystem = experimental::filesystem;
  54. }
  55. // We have a decent compiler and can use the normal version
  56. # else
  57. // Include it
  58. # include <filesystem>
  59. # endif
  60. #endif // #ifndef INCLUDE_STD_FILESYSTEM_EXPERIMENTAL
  61. void writeProgress(float progress){
  62. int barWidth = 200;
  63. std::cout << "[";
  64. int pos = barWidth * progress;
  65. for (int i = 0; i < barWidth; ++i) {
  66. if (i < pos) std::cout << "=";
  67. else if (i == pos) std::cout << ">";
  68. else std::cout << " ";
  69. }
  70. std::cout << "] " << int(progress * 100.0) << " %\r";
  71. std::cout.flush();
  72. }
  73. int main(int argc, char *argv[]){
  74. std::string folderName;
  75. std::string outfileName;
  76. unsigned nbSamples = 10;
  77. bool random;
  78. for (int i = 1; i < argc; ++i) {
  79. if (!strcmp(argv[i], "--folder") || !strcmp(argv[i], "-folder")) {
  80. folderName = argv[++i];
  81. } else if (!strcmp(argv[i], "--samples") || !strcmp(argv[i], "-samples")) {
  82. nbSamples = atoi(argv[++i]);
  83. }else if (!strcmp(argv[i], "--random") || !strcmp(argv[i], "-random")) {
  84. random = bool(atoi(argv[++i]));
  85. }else if (!strcmp(argv[i], "--outfile") || !strcmp(argv[i], "-ouftile")) {
  86. outfileName = argv[++i];
  87. }
  88. }
  89. std::vector<std::string> imagesPath;
  90. for (const auto & entry : std::filesystem::directory_iterator(folderName)){
  91. std::string imageName = entry.path().string();
  92. if (rawls::HasExtension(imageName, ".rawls") || rawls::HasExtension(imageName, ".rawls_20")){
  93. imagesPath.push_back(imageName);
  94. }
  95. }
  96. // sort or shuffle the images path
  97. if (!random){
  98. std::sort(imagesPath.begin(), imagesPath.end(), std::less<std::string>());
  99. }else{
  100. std::random_shuffle(imagesPath.begin(), imagesPath.end());
  101. }
  102. // check nSamples
  103. if (nbSamples > imagesPath.size()){
  104. nbSamples = imagesPath.size();
  105. }
  106. unsigned width, height, nbChanels;
  107. float* outputBuffer;
  108. if (imagesPath.size() > 0){
  109. std::tuple<unsigned, unsigned, unsigned> dimensions = rawls::getDimensionsRAWLS(imagesPath.at(0));
  110. width = std::get<0>(dimensions);
  111. height = std::get<1>(dimensions);
  112. nbChanels = std::get<2>(dimensions);
  113. outputBuffer = new float[width * height * nbChanels];
  114. // init values of buffer
  115. for (int i = 0; i < height * width * nbChanels; i++){
  116. outputBuffer[i] = 0;
  117. }
  118. }
  119. else
  120. {
  121. std::cout << "Folder is empty..." << std::endl;
  122. return 1;
  123. }
  124. // just for indication
  125. float progress = 0.0;
  126. unsigned bufferSize = width * height * nbChanels;
  127. for (unsigned i = 0; i < nbSamples; i++){
  128. // read into folder all `.rawls` file and merge pixels values
  129. float* buffer = rawls::getPixelsRAWLS(imagesPath.at(i));
  130. for(unsigned y = 0; y < height; y++){
  131. for(unsigned x = 0; x < width; x++) {
  132. for(unsigned j = 0; j < nbChanels; j++){
  133. float value = buffer[nbChanels * width * y + nbChanels * x + j];
  134. outputBuffer[nbChanels * width * y + nbChanels * x + j] = outputBuffer[nbChanels * width * y + nbChanels * x + j] + value;
  135. }
  136. }
  137. }
  138. // update and write progress information
  139. progress += (1 / (float)nbSamples);
  140. writeProgress(progress);
  141. delete buffer;
  142. }
  143. writeProgress(1.);
  144. std::cout << std::endl;
  145. // mean all samples values by number of samples used
  146. for (int i = 0; i < height * width * nbChanels; i++){
  147. outputBuffer[i] = outputBuffer[i] / nbSamples;
  148. }
  149. // create outfile
  150. if (rawls::HasExtension(outfileName, ".ppm")){
  151. rawls::saveAsPPM(width, height, nbChanels, outputBuffer, outfileName);
  152. }
  153. else if (rawls::HasExtension(outfileName, ".png")){
  154. rawls::saveAsPNG(width, height, nbChanels, outputBuffer, outfileName);
  155. }
  156. else if (rawls::HasExtension(outfileName, ".rawls") || rawls::HasExtension(outfileName, ".rawls_20")){
  157. // need to get comments from an image
  158. std::string comments = rawls::getCommentsRAWLS(imagesPath.at(0));
  159. // Here no gamma conversion is done, only mean of samples
  160. rawls::saveAsRAWLS(width, height, nbChanels, comments, outputBuffer, outfileName);
  161. }
  162. else{
  163. std::cout << "Unexpected output extension image" << std::endl;
  164. }
  165. // delete the outputbuffer used
  166. delete outputBuffer;
  167. }