rawls_merge_MON_incr.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. // number of means expected
  10. const unsigned numberOfMeans = 20;
  11. void writeProgress(float progress, bool moveUp = false){
  12. int barWidth = 150;
  13. if (moveUp){
  14. // move up line
  15. std::cout << "\e[A";
  16. std::cout.flush();
  17. }
  18. std::cout << "[";
  19. int pos = barWidth * progress;
  20. for (int i = 0; i < barWidth; ++i) {
  21. if (i < pos) std::cout << "=";
  22. else if (i == pos) std::cout << ">";
  23. else std::cout << " ";
  24. }
  25. std::cout << "] " << int(progress * 100.0) << " %\r";
  26. std::cout.flush();
  27. }
  28. void insertSample(unsigned* occurences, float* values, float value){
  29. /* generate secret number: */
  30. unsigned foundIndex = rand() % numberOfMeans;
  31. values[foundIndex] += value;
  32. occurences[foundIndex] += 1;
  33. }
  34. /*
  35. * Compute array of means and sort values
  36. */
  37. float* prepareMeans(unsigned* occurences, float* values){
  38. float* means = new float[numberOfMeans];
  39. for (int i = 0; i < numberOfMeans; i++){
  40. means[i] = values[i] / occurences[i];
  41. }
  42. std::sort(means, means + numberOfMeans, std::less<float>());
  43. return means;
  44. }
  45. /*
  46. * Returns median value from array of values
  47. */
  48. float getMedianValue(float values[]){
  49. if (numberOfMeans % 2 == 0)
  50. {
  51. return (values[numberOfMeans / 2 - 1] + values[numberOfMeans / 2]) / 2;
  52. }
  53. else
  54. {
  55. return values[numberOfMeans / 2];
  56. }
  57. }
  58. /*
  59. * Save current step images from current buffer
  60. */
  61. bool saveCurrentImage(int width, int height, int nbChanels, float* buffer, std::string outfileName, std::string comments){
  62. // create outfile
  63. if (rawls::HasExtension(outfileName, ".ppm")){
  64. rawls::saveAsPPM(width, height, nbChanels, buffer, outfileName);
  65. }
  66. else if (rawls::HasExtension(outfileName, ".png")){
  67. rawls::saveAsPNG(width, height, nbChanels, buffer, outfileName);
  68. }
  69. else if (rawls::HasExtension(outfileName, ".rawls") || rawls::HasExtension(outfileName, ".rawls_20")){
  70. // Here no gamma conversion is done, only mean of samples
  71. rawls::saveAsRAWLS(width, height, nbChanels, comments, buffer, outfileName);
  72. }
  73. else{
  74. std::cout << "Unexpected output extension image" << std::endl;
  75. return false;
  76. }
  77. return true;
  78. }
  79. /*
  80. * Incremental merge of `rawls` images using `median-of-means`
  81. */
  82. int main(int argc, char *argv[]){
  83. /* initialize random seed: */
  84. srand ( time(NULL) );
  85. std::string folderName;
  86. std::string outputFolder;
  87. std::string prefixImageName;
  88. std::string imageExtension;
  89. unsigned step = 10;
  90. unsigned maxSamples = 0;
  91. bool random;
  92. for (int i = 1; i < argc; ++i) {
  93. if (!strcmp(argv[i], "--folder") || !strcmp(argv[i], "-folder")) {
  94. folderName = argv[++i];
  95. } else if (!strcmp(argv[i], "--step") || !strcmp(argv[i], "-step")) {
  96. step = atoi(argv[++i]);
  97. }else if (!strcmp(argv[i], "--random") || !strcmp(argv[i], "-random")) {
  98. random = bool(atoi(argv[++i]));
  99. }else if (!strcmp(argv[i], "--output") || !strcmp(argv[i], "-output")) {
  100. outputFolder = argv[++i];
  101. }else if (!strcmp(argv[i], "--prefix") || !strcmp(argv[i], "-prefix")) {
  102. prefixImageName = argv[++i];
  103. }else if (!strcmp(argv[i], "--max") || !strcmp(argv[i], "-max")) {
  104. maxSamples = atoi(argv[++i]);
  105. }else if (!strcmp(argv[i], "--extension") || !strcmp(argv[i], "-extension")) {
  106. imageExtension = argv[++i];
  107. }
  108. }
  109. std::vector<std::string> imagesPath;
  110. for (const auto & entry : std::filesystem::directory_iterator(folderName)){
  111. std::string imageName = entry.path().string();
  112. if (rawls::HasExtension(imageName, ".rawls") || rawls::HasExtension(imageName, ".rawls_20")){
  113. imagesPath.push_back(imageName);
  114. }
  115. }
  116. // sort or shuffle the images path
  117. if (!random){
  118. std::sort(imagesPath.begin(), imagesPath.end(), std::less<std::string>());
  119. }else{
  120. std::random_shuffle(imagesPath.begin(), imagesPath.end());
  121. }
  122. unsigned width, height, nbChanels;
  123. float** sumBuffer; // stores sum array for each sample
  124. unsigned** occurencesMeanBuffer;
  125. float* outputStepBuffer; // buffer which stores kept median for each generated image (median is found using `outputMeanBuffer`)
  126. if (imagesPath.size() > 0){
  127. std::tuple<unsigned, unsigned, unsigned> dimensions = rawls::getDimensionsRAWLS(imagesPath.at(0));
  128. width = std::get<0>(dimensions);
  129. height = std::get<1>(dimensions);
  130. nbChanels = std::get<2>(dimensions);
  131. // init all pointers size
  132. sumBuffer = new float*[width * height * nbChanels];
  133. occurencesMeanBuffer = new unsigned*[width * height * nbChanels];
  134. outputStepBuffer = new float[width * height * nbChanels];
  135. // init values of buffer
  136. for (int i = 0; i < height * width * nbChanels; i++){
  137. // define array size and initialization
  138. sumBuffer[i] = new float[numberOfMeans];
  139. occurencesMeanBuffer[i] = new unsigned[numberOfMeans];
  140. for (int j = 0; j < numberOfMeans; j++){
  141. sumBuffer[i][j] = 0;
  142. occurencesMeanBuffer[i][j] = 0;
  143. }
  144. outputStepBuffer[i] = 0;
  145. }
  146. }
  147. else
  148. {
  149. std::cout << "Folder is empty..." << std::endl;
  150. return 1;
  151. }
  152. // just for indication
  153. float progress = 0.0;
  154. unsigned bufferSize = width * height * nbChanels;
  155. std::string comments;
  156. // get comments if output is also `rawls` file
  157. if (rawls::HasExtension(imageExtension, "rawls")){
  158. comments = rawls::getCommentsRAWLS(imagesPath.at(0));
  159. }
  160. for (unsigned i = 0; i < maxSamples; i++){
  161. unsigned currentSample = i + 1;
  162. // read into folder all `.rawls` file and merge pixels values
  163. float* buffer = rawls::getPixelsRAWLS(imagesPath.at(i));
  164. for(unsigned y = 0; y < height; y++){
  165. for(unsigned x = 0; x < width; x++) {
  166. for(unsigned j = 0; j < nbChanels; j++){
  167. unsigned currentIndex = nbChanels * width * y + nbChanels * x + j;
  168. float value = buffer[currentIndex];
  169. // add new `luminance` of chanel[j] found randomly (uniformly) into means array
  170. insertSample(occurencesMeanBuffer[currentIndex], sumBuffer[currentIndex], value);
  171. }
  172. }
  173. }
  174. // save a new
  175. if (currentSample % step == 0){
  176. float currentMean;
  177. // get median all samples values by number of samples used (using MON method)
  178. for (int j = 0; j < height * width * nbChanels; j++){
  179. float* means = prepareMeans(occurencesMeanBuffer[j], sumBuffer[j]);
  180. // get meadian of these means as expected output luminance
  181. outputStepBuffer[j] = getMedianValue(means);
  182. // remove pointer values
  183. delete means;
  184. }
  185. // add suffix with `5` digits
  186. std::string suffix = std::to_string(currentSample);
  187. while(suffix.length() < 5){
  188. suffix = "0" + suffix;
  189. }
  190. // build output path of image
  191. std::string outfileName = outputFolder + "/" + prefixImageName + "_" + suffix + "." + imageExtension;
  192. outfileName = std::regex_replace(outfileName, std::regex("\\//"), "/"); // fix path
  193. // save the expected `step` image using built outpath
  194. saveCurrentImage(width, height, nbChanels, outputStepBuffer, outfileName, comments);
  195. // just for progress information with erasing previous info
  196. writeProgress(progress, true);
  197. }
  198. // update and write progress information
  199. progress += (1 / (float)maxSamples);
  200. writeProgress(progress);
  201. delete buffer;
  202. }
  203. writeProgress(1.);
  204. std::cout << std::endl;
  205. // clear all pointers memory
  206. for (int j = 0; j < height * width * nbChanels; j++){
  207. delete[] sumBuffer[j];
  208. delete[] occurencesMeanBuffer[j];
  209. }
  210. delete sumBuffer;
  211. delete occurencesMeanBuffer;
  212. delete outputStepBuffer;
  213. }