rawls_merge_mean.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. int main(int argc, char *argv[]){
  21. std::string folderName;
  22. std::string outfileName;
  23. unsigned nbSamples = 10;
  24. unsigned startIndex = 0;
  25. bool random;
  26. for (int i = 1; i < argc; ++i) {
  27. if (!strcmp(argv[i], "--folder") || !strcmp(argv[i], "-folder")) {
  28. folderName = argv[++i];
  29. } else if (!strcmp(argv[i], "--samples") || !strcmp(argv[i], "-samples")) {
  30. nbSamples = atoi(argv[++i]);
  31. }else if (!strcmp(argv[i], "--random") || !strcmp(argv[i], "-random")) {
  32. random = bool(atoi(argv[++i]));
  33. }else if (!strcmp(argv[i], "--outfile") || !strcmp(argv[i], "-ouftile")) {
  34. outfileName = argv[++i];
  35. }else if (!strcmp(argv[i], "--startindex") || !strcmp(argv[i], "-startindex")) {
  36. startIndex = atoi(argv[++i]);
  37. }
  38. }
  39. std::vector<std::string> imagesPath;
  40. for (const auto & entry : std::filesystem::directory_iterator(folderName)){
  41. std::string imageName = entry.path().string();
  42. if (rawls::HasExtension(imageName, ".rawls") || rawls::HasExtension(imageName, ".rawls_20")){
  43. imagesPath.push_back(imageName);
  44. }
  45. }
  46. // sort or shuffle the images path
  47. if (!random){
  48. std::sort(imagesPath.begin(), imagesPath.end(), std::less<std::string>());
  49. }else{
  50. std::random_shuffle(imagesPath.begin(), imagesPath.end());
  51. }
  52. // check nSamples
  53. if (nbSamples > imagesPath.size()){
  54. nbSamples = imagesPath.size();
  55. }
  56. unsigned width, height, nbChanels;
  57. float* outputBuffer;
  58. if (imagesPath.size() > 0){
  59. std::tuple<unsigned, unsigned, unsigned> dimensions = rawls::getDimensionsRAWLS(imagesPath.at(0));
  60. width = std::get<0>(dimensions);
  61. height = std::get<1>(dimensions);
  62. nbChanels = std::get<2>(dimensions);
  63. outputBuffer = new float[width * height * nbChanels];
  64. // init values of buffer
  65. for (int i = 0; i < height * width * nbChanels; i++){
  66. outputBuffer[i] = 0;
  67. }
  68. }
  69. else
  70. {
  71. std::cout << "Folder is empty..." << std::endl;
  72. return 1;
  73. }
  74. // just for indication
  75. float progress = 0.0;
  76. unsigned bufferSize = width * height * nbChanels;
  77. for (unsigned i = startIndex; i < (nbSamples + startIndex); i++){
  78. // read into folder all `.rawls` file and merge pixels values
  79. float* buffer = rawls::getPixelsRAWLS(imagesPath.at(i));
  80. for(unsigned y = 0; y < height; y++){
  81. for(unsigned x = 0; x < width; x++) {
  82. for(unsigned j = 0; j < nbChanels; j++){
  83. float value = buffer[nbChanels * width * y + nbChanels * x + j];
  84. outputBuffer[nbChanels * width * y + nbChanels * x + j] = outputBuffer[nbChanels * width * y + nbChanels * x + j] + value;
  85. }
  86. }
  87. }
  88. // update and write progress information
  89. progress += (1 / (float)nbSamples);
  90. writeProgress(progress);
  91. delete buffer;
  92. }
  93. writeProgress(1.);
  94. std::cout << std::endl;
  95. // mean all samples values by number of samples used
  96. for (int i = 0; i < height * width * nbChanels; i++){
  97. outputBuffer[i] = outputBuffer[i] / nbSamples;
  98. }
  99. // create outfile
  100. if (rawls::HasExtension(outfileName, ".ppm")){
  101. rawls::saveAsPPM(width, height, nbChanels, outputBuffer, outfileName);
  102. }
  103. else if (rawls::HasExtension(outfileName, ".png")){
  104. rawls::saveAsPNG(width, height, nbChanels, outputBuffer, outfileName);
  105. }
  106. else if (rawls::HasExtension(outfileName, ".rawls") || rawls::HasExtension(outfileName, ".rawls_20")){
  107. // need to get comments from an image
  108. std::string comments = rawls::getCommentsRAWLS(imagesPath.at(0));
  109. // Here no gamma conversion is done, only mean of samples
  110. rawls::saveAsRAWLS(width, height, nbChanels, comments, outputBuffer, outfileName);
  111. }
  112. else{
  113. std::cout << "Unexpected output extension image" << std::endl;
  114. }
  115. // delete the outputbuffer used
  116. delete outputBuffer;
  117. }