rawls_merge.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 = 70;
  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. imagesPath.push_back(entry.path().string());
  39. // sort or shuffle the images path
  40. if (!random){
  41. std::sort(imagesPath.begin(), imagesPath.end(), std::less<std::string>());
  42. }else{
  43. std::random_shuffle(imagesPath.begin(), imagesPath.end());
  44. }
  45. // check nSamples
  46. if (nbSamples > imagesPath.size()){
  47. nbSamples = imagesPath.size();
  48. }
  49. unsigned width, height, nbChanels;
  50. float* outputBuffer;
  51. if (imagesPath.size() > 0){
  52. std::tuple<unsigned, unsigned, unsigned> dimensions = rawls::getDimensionsRAWLS(imagesPath.at(0));
  53. width = std::get<0>(dimensions);
  54. height = std::get<1>(dimensions);
  55. nbChanels = std::get<2>(dimensions);
  56. outputBuffer = new float[width * height * nbChanels];
  57. // init values of buffer
  58. for (int i = 0; i < height * width * nbChanels; i++){
  59. outputBuffer[i] = 0;
  60. }
  61. }
  62. else
  63. {
  64. std::cout << "Folder is empty..." << std::endl;
  65. return 1;
  66. }
  67. // just for indication
  68. float progress = 0.0;
  69. unsigned bufferSize = width * height * nbChanels;
  70. for (unsigned i = 0; i < nbSamples; i++){
  71. // read into folder all `.rawls` file and merge pixels values
  72. float* buffer = rawls::getPixelsRAWLS(imagesPath.at(i));
  73. for(unsigned y = 0; y < height; y++){
  74. for(unsigned x = 0; x < width; x++) {
  75. for(int j = 0; j < nbChanels; j++){
  76. float value = buffer[nbChanels * width * y + nbChanels * x + j];
  77. outputBuffer[nbChanels * width * y + nbChanels * x + j] = outputBuffer[nbChanels * width * y + nbChanels * x + j] + value;
  78. }
  79. }
  80. }
  81. // update and write progress information
  82. progress += (1 / (float)nbSamples);
  83. writeProgress(progress);
  84. delete buffer;
  85. }
  86. std::cout << std::endl;
  87. // mean all samples values by number of samples used
  88. for (int i = 0; i < height * width * nbChanels; i++){
  89. outputBuffer[i] = outputBuffer[i] / nbSamples;
  90. }
  91. // create outfile
  92. if (rawls::HasExtension(outfileName, ".ppm")){
  93. rawls::saveAsPPM(width, height, nbChanels, outputBuffer, outfileName);
  94. }
  95. else if (rawls::HasExtension(outfileName, ".png")){
  96. rawls::saveAsPNG(width, height, nbChanels, outputBuffer, outfileName);
  97. }
  98. else if (rawls::HasExtension(outfileName, ".rawls")){
  99. // TODO
  100. //rawls::saveAsPNG(width, height, nbChanels, outputBuffer, outfileName);
  101. std::cout << "Saved as rawls is not yet implemented" << std::endl;
  102. }
  103. else{
  104. std::cout << "Unexpected output extension image" << std::endl;
  105. }
  106. // delete the outputbuffer used
  107. delete outputBuffer;
  108. }