rawls_merge.cpp 4.4 KB

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