rawls_merge_median_incr.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 = 150;
  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. void insertSorted(unsigned size, float* values, float value){
  27. // avoid use of:
  28. //std::sort(values, values + size, std::greater<float>());
  29. unsigned position = 0;
  30. // find expected position value
  31. for (int i = 0; i < size; i++){
  32. if(value <= values[i]){
  33. break; // position found go out of the loop
  34. }
  35. position = i + 1;
  36. }
  37. // shift all values to right by one
  38. for (int i = size; i > position; i--){
  39. values[i] = values[i - 1];
  40. }
  41. // insert new value into found position (now free)
  42. values[position] = value;
  43. }
  44. float getMedianValue(unsigned size, float* values){
  45. if (size % 2 == 0)
  46. {
  47. return (values[size / 2 - 1] + values[size / 2]) / 2;
  48. }
  49. else
  50. {
  51. return values[size / 2];
  52. }
  53. }
  54. /*
  55. * Save current step images from current buffer
  56. */
  57. bool saveCurrentImage(int width, int height, int nbChanels, float* buffer, std::string outfileName, std::string comments){
  58. // create outfile
  59. if (rawls::HasExtension(outfileName, ".ppm")){
  60. rawls::saveAsPPM(width, height, nbChanels, buffer, outfileName);
  61. }
  62. else if (rawls::HasExtension(outfileName, ".png")){
  63. rawls::saveAsPNG(width, height, nbChanels, buffer, outfileName);
  64. }
  65. else if (rawls::HasExtension(outfileName, ".rawls") || rawls::HasExtension(outfileName, ".rawls_20")){
  66. // Here no gamma conversion is done, only mean of samples
  67. rawls::saveAsRAWLS(width, height, nbChanels, comments, buffer, outfileName);
  68. }
  69. else{
  70. std::cout << "Unexpected output extension image" << std::endl;
  71. return false;
  72. }
  73. return true;
  74. }
  75. /*
  76. * Incremental merge of `rawls` images
  77. */
  78. int main(int argc, char *argv[]){
  79. std::string folderName;
  80. std::string outputFolder;
  81. std::string prefixImageName;
  82. std::string imageExtension;
  83. unsigned step = 10;
  84. unsigned maxSamples = 0;
  85. bool random;
  86. for (int i = 1; i < argc; ++i) {
  87. if (!strcmp(argv[i], "--folder") || !strcmp(argv[i], "-folder")) {
  88. folderName = argv[++i];
  89. } else if (!strcmp(argv[i], "--step") || !strcmp(argv[i], "-step")) {
  90. step = atoi(argv[++i]);
  91. }else if (!strcmp(argv[i], "--random") || !strcmp(argv[i], "-random")) {
  92. random = bool(atoi(argv[++i]));
  93. }else if (!strcmp(argv[i], "--output") || !strcmp(argv[i], "-output")) {
  94. outputFolder = argv[++i];
  95. }else if (!strcmp(argv[i], "--prefix") || !strcmp(argv[i], "-prefix")) {
  96. prefixImageName = argv[++i];
  97. }else if (!strcmp(argv[i], "--max") || !strcmp(argv[i], "-max")) {
  98. maxSamples = atoi(argv[++i]);
  99. }else if (!strcmp(argv[i], "--extension") || !strcmp(argv[i], "-extension")) {
  100. imageExtension = argv[++i];
  101. }
  102. }
  103. std::vector<std::string> imagesPath;
  104. for (const auto & entry : std::filesystem::directory_iterator(folderName)){
  105. std::string imageName = entry.path().string();
  106. if (rawls::HasExtension(imageName, ".rawls") || rawls::HasExtension(imageName, ".rawls_20")){
  107. imagesPath.push_back(imageName);
  108. }
  109. }
  110. // sort or shuffle the images path
  111. if (!random){
  112. std::sort(imagesPath.begin(), imagesPath.end(), std::less<std::string>());
  113. }else{
  114. std::random_shuffle(imagesPath.begin(), imagesPath.end());
  115. }
  116. unsigned width, height, nbChanels;
  117. float* outputStepBuffer;
  118. float** outputBuffer;
  119. if (imagesPath.size() > 0){
  120. std::tuple<unsigned, unsigned, unsigned> dimensions = rawls::getDimensionsRAWLS(imagesPath.at(0));
  121. width = std::get<0>(dimensions);
  122. height = std::get<1>(dimensions);
  123. nbChanels = std::get<2>(dimensions);
  124. outputBuffer = new float*[width * height * nbChanels];
  125. outputStepBuffer = new float[width * height * nbChanels];
  126. // init values of buffer
  127. for (int i = 0; i < height * width * nbChanels; i++){
  128. outputBuffer[i] = new float[maxSamples];
  129. outputStepBuffer[i] = 0;
  130. }
  131. }
  132. else
  133. {
  134. std::cout << "Folder is empty..." << std::endl;
  135. return 1;
  136. }
  137. // just for indication
  138. float progress = 0.0;
  139. unsigned bufferSize = width * height * nbChanels;
  140. std::string comments;
  141. // get comments if output is also `rawls` file
  142. if (rawls::HasExtension(imageExtension, "rawls")){
  143. comments = rawls::getCommentsRAWLS(imagesPath.at(0));
  144. }
  145. for (unsigned i = 0; i < maxSamples; i++){
  146. unsigned currentSample = i + 1;
  147. // read into folder all `.rawls` file and merge pixels values
  148. float* buffer = rawls::getPixelsRAWLS(imagesPath.at(i));
  149. for(unsigned y = 0; y < height; y++){
  150. for(unsigned x = 0; x < width; x++) {
  151. for(unsigned j = 0; j < nbChanels; j++){
  152. float value = buffer[nbChanels * width * y + nbChanels * x + j];
  153. insertSorted(i, outputBuffer[nbChanels * width * y + nbChanels * x + j], value);
  154. }
  155. }
  156. }
  157. // save a new
  158. if (currentSample % step == 0){
  159. // get median all samples values by number of samples used
  160. for (int j = 0; j < height * width * nbChanels; j++){
  161. outputStepBuffer[j] = getMedianValue(currentSample, outputBuffer[j]);
  162. }
  163. // add suffix with `5` digits
  164. std::string suffix = std::to_string(currentSample);
  165. while(suffix.length() < 5){
  166. suffix = "0" + suffix;
  167. }
  168. // build output path of image
  169. std::string outfileName = outputFolder + "/" + prefixImageName + "_" + suffix + "." + imageExtension;
  170. outfileName = std::regex_replace(outfileName, std::regex("\\//"), "/"); // fix path
  171. // save the expected `step` image using built outpath
  172. saveCurrentImage(width, height, nbChanels, outputStepBuffer, outfileName, comments);
  173. writeProgress(progress, true);
  174. }
  175. // update and write progress information
  176. progress += (1 / (float)maxSamples);
  177. writeProgress(progress);
  178. delete buffer;
  179. }
  180. writeProgress(1.);
  181. std::cout << std::endl;
  182. // delete the outputbuffer used
  183. for (int j = 0; j < height * width * nbChanels; j++){
  184. delete[] outputBuffer[j];
  185. }
  186. delete[] outputBuffer;
  187. delete outputStepBuffer;
  188. }