rawls_merge_mean_incr_v2.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include <iostream>
  2. #include <string.h>
  3. #include <memory>
  4. #include "lodepng.h"
  5. #include "rawls.h"
  6. #include <algorithm>
  7. #include <filesystem>
  8. #include <regex>
  9. void writeProgress(float progress, bool moveUp = false){
  10. int barWidth = 200;
  11. if (moveUp){
  12. // move up line
  13. std::cout << "\e[A";
  14. std::cout.flush();
  15. }
  16. std::cout << "[";
  17. int pos = barWidth * progress;
  18. for (int i = 0; i < barWidth; ++i) {
  19. if (i < pos) std::cout << "=";
  20. else if (i == pos) std::cout << ">";
  21. else std::cout << " ";
  22. }
  23. std::cout << "] " << int(progress * 100.0) << " %\r";
  24. std::cout.flush();
  25. }
  26. /*
  27. * Save current step images from current buffer
  28. */
  29. bool saveCurrentImage(int width, int height, int nbChanels, float* buffer, std::string outfileName, std::string comments){
  30. // create outfile
  31. if (rawls::HasExtension(outfileName, ".ppm")){
  32. rawls::saveAsPPM(width, height, nbChanels, buffer, outfileName);
  33. }
  34. else if (rawls::HasExtension(outfileName, ".png")){
  35. rawls::saveAsPNG(width, height, nbChanels, buffer, outfileName);
  36. }
  37. else if (rawls::HasExtension(outfileName, ".rawls") || rawls::HasExtension(outfileName, ".rawls_20")){
  38. // Here no gamma conversion is done, only mean of samples
  39. rawls::saveAsRAWLS(width, height, nbChanels, comments, buffer, outfileName);
  40. }
  41. else{
  42. std::cout << "Unexpected output extension image" << std::endl;
  43. return false;
  44. }
  45. return true;
  46. }
  47. /*
  48. * Incremental merge of `rawls` images
  49. */
  50. int main(int argc, char *argv[]){
  51. std::string folderName;
  52. std::string outputFolder;
  53. std::string prefixImageName;
  54. std::string imageExtension;
  55. unsigned step = 10;
  56. unsigned maxSamples = 0;
  57. bool random;
  58. for (int i = 1; i < argc; ++i) {
  59. if (!strcmp(argv[i], "--folder") || !strcmp(argv[i], "-folder")) {
  60. folderName = argv[++i];
  61. } else if (!strcmp(argv[i], "--step") || !strcmp(argv[i], "-step")) {
  62. step = atoi(argv[++i]);
  63. }else if (!strcmp(argv[i], "--random") || !strcmp(argv[i], "-random")) {
  64. random = bool(atoi(argv[++i]));
  65. }else if (!strcmp(argv[i], "--output") || !strcmp(argv[i], "-output")) {
  66. outputFolder = argv[++i];
  67. }else if (!strcmp(argv[i], "--prefix") || !strcmp(argv[i], "-prefix")) {
  68. prefixImageName = argv[++i];
  69. }else if (!strcmp(argv[i], "--max") || !strcmp(argv[i], "-max")) {
  70. maxSamples = atoi(argv[++i]);
  71. }else if (!strcmp(argv[i], "--extension") || !strcmp(argv[i], "-extension")) {
  72. imageExtension = argv[++i];
  73. }
  74. }
  75. std::vector<std::string> imagesPath;
  76. for (const auto & entry : std::filesystem::directory_iterator(folderName)){
  77. std::string imageName = entry.path().string();
  78. if (rawls::HasExtension(imageName, ".rawls") || rawls::HasExtension(imageName, ".rawls_20")){
  79. imagesPath.push_back(imageName);
  80. }
  81. }
  82. // sort or shuffle the images path
  83. if (!random){
  84. std::sort(imagesPath.begin(), imagesPath.end(), std::less<std::string>());
  85. }else{
  86. std::random_shuffle(imagesPath.begin(), imagesPath.end());
  87. }
  88. unsigned width, height, nbChanels;
  89. float* outputStepBuffer;
  90. float* outputBuffer;
  91. if (imagesPath.size() > 0){
  92. std::tuple<unsigned, unsigned, unsigned> dimensions = rawls::getDimensionsRAWLS(imagesPath.at(0));
  93. width = std::get<0>(dimensions);
  94. height = std::get<1>(dimensions);
  95. nbChanels = std::get<2>(dimensions);
  96. outputBuffer = new float[width * height * nbChanels];
  97. outputStepBuffer = new float[width * height * nbChanels];
  98. // init values of buffer
  99. for (int i = 0; i < height * width * nbChanels; i++){
  100. outputBuffer[i] = 0;
  101. outputStepBuffer[i] = 0;
  102. }
  103. }
  104. else
  105. {
  106. std::cout << "Folder is empty..." << std::endl;
  107. return 1;
  108. }
  109. // just for indication
  110. float progress = 0.0;
  111. unsigned bufferSize = width * height * nbChanels;
  112. std::string comments;
  113. if (rawls::HasExtension(imageExtension, "rawls")){
  114. comments = rawls::getCommentsRAWLS(imagesPath.at(0));
  115. }
  116. for (int i = 0; i < maxSamples; i++){
  117. unsigned currentSample = i + 1;
  118. // read into folder all `.rawls` file and merge pixels values
  119. float* buffer = rawls::getPixelsRAWLS(imagesPath.at(i));
  120. double w1 = (double)i / (double)(i + 1.);
  121. double w2 = 1. / ((double)i + 1.);
  122. std::cout << w1 << " - " << w2 << std::endl;
  123. for(unsigned y = 0; y < height; y++){
  124. for(unsigned x = 0; x < width; x++) {
  125. for(unsigned j = 0; j < nbChanels; j++){
  126. float value = buffer[nbChanels * width * y + nbChanels * x + j];
  127. //outputBuffer[nbChanels * width * y + nbChanels * x + j] += value;
  128. outputBuffer[nbChanels * width * y + nbChanels * x + j] = outputBuffer[nbChanels * width * y + nbChanels * x + j] * w1 + value * w2;
  129. }
  130. }
  131. }
  132. // save a new
  133. if (currentSample % step == 0){
  134. // mean all samples values by number of samples used
  135. for (int j = 0; j < height * width * nbChanels; j++){
  136. outputStepBuffer[j] = outputBuffer[j];// / currentSample;
  137. }
  138. // add suffix with `5` digits
  139. std::string suffix = std::to_string(currentSample);
  140. while(suffix.length() < 5){
  141. suffix = "0" + suffix;
  142. }
  143. // build output path of image
  144. std::string outfileName = outputFolder + "/" + prefixImageName + "_" + suffix + "." + imageExtension;
  145. outfileName = std::regex_replace(outfileName, std::regex("\\//"), "/"); // fix path
  146. std::cout << "Saved image into " << outfileName << std::endl;
  147. // save the expected `step` image using built outpath
  148. saveCurrentImage(width, height, nbChanels, outputStepBuffer, outfileName, comments);
  149. writeProgress(progress, true);
  150. }
  151. // update and write progress information
  152. progress += (1 / (float)maxSamples);
  153. writeProgress(progress);
  154. delete buffer;
  155. }
  156. writeProgress(1.);
  157. std::cout << std::endl;
  158. // delete the outputbuffer used
  159. delete outputBuffer;
  160. delete outputStepBuffer;
  161. }