rawls_merge_mean_incr.cpp 6.6 KB

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