rawls_merge_incr.cpp 5.6 KB

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