A tool to construct HDR-images from a series of exposures.

haader.cpp 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include "haader.h"
  6. using namespace std;
  7. using namespace haader;
  8. HdrImageStack::HdrImageStack() {
  9. }
  10. bool HdrImageStack::read_from_files(char * const* file_paths, int number_of_paths) {
  11. m_images.clear();
  12. if (number_of_paths < 0) {
  13. cerr << "HdrImageStack::read_from_files: number_of_paths is negative" << endl;
  14. return false;
  15. }
  16. for (int i = 0; i < number_of_paths; i++) {
  17. m_images.push_back(Image());
  18. if (m_images[i].read_from_file(file_paths[i])) {
  19. cout << "Read \"" << file_paths[i] << "\"" << endl;
  20. if (!m_images[0].has_equal_dimensions(m_images[i])) {
  21. cerr << "HdrImageStack::read_from_files: Dimensions do not match (\""
  22. << file_paths[i]
  23. << "\")"
  24. << endl;
  25. return false;
  26. }
  27. } else {
  28. return false;
  29. }
  30. }
  31. return true;
  32. }
  33. bool HdrImageStack::get_average_image_slow(Image &output) {
  34. if (m_images.size() == 0) {
  35. return false;
  36. }
  37. output.m_image_data.resize(m_images[0].m_image_data.size());
  38. output.m_width = m_images[0].m_width;
  39. output.m_height = m_images[0].m_height;
  40. output.m_components = m_images[0].m_components;
  41. unsigned int bytes_size = m_images[0].m_image_data.size();
  42. unsigned int images_size = m_images.size();
  43. for (unsigned int i = 0; i < bytes_size; i++) {
  44. int v = 0;
  45. for (unsigned int k = 0; k < images_size; k++) {
  46. v += m_images[k].m_image_data[i];
  47. }
  48. output.m_image_data[i] = v / (int)images_size;
  49. }
  50. return true;
  51. }
  52. bool HdrImageStack::get_average_image(Image &output) {
  53. if (m_images.size() == 0) {
  54. return false;
  55. }
  56. output.m_image_data.resize(m_images[0].m_image_data.size());
  57. output.m_width = m_images[0].m_width;
  58. output.m_height = m_images[0].m_height;
  59. output.m_components = m_images[0].m_components;
  60. unsigned int bytes_size = m_images[0].m_image_data.size();
  61. unsigned int images_size = m_images.size();
  62. const unsigned int buf_size = 128;
  63. int buf[buf_size];
  64. for (unsigned int i = 0; i + buf_size <= bytes_size; i += buf_size) {
  65. for (unsigned int m = 0; m < buf_size; m++) {
  66. buf[m] = m_images[0].m_image_data[i + m];
  67. }
  68. for (unsigned int k = 1; k < images_size; k++) {
  69. for (unsigned int m = 0; m < buf_size; m++) {
  70. buf[m] += m_images[k].m_image_data[i + m];
  71. }
  72. }
  73. for (unsigned int m = 0; m < buf_size; m++) {
  74. output.m_image_data[i + m] = buf[m] / images_size;
  75. }
  76. }
  77. // TODO test this with an image that is prime numbered dimensions
  78. unsigned int rest = bytes_size % buf_size;
  79. for (unsigned int i = bytes_size - rest; i < bytes_size; i++) {
  80. int v = 0;
  81. for (unsigned int k = 0; k < images_size; k++) {
  82. v += m_images[k].m_image_data[i];
  83. }
  84. output.m_image_data[i] = v / (int)images_size;
  85. }
  86. return true;
  87. }
  88. bool HdrImageStack::samples_to_csv(const char *file_path) {
  89. if (m_images.size() == 0) {
  90. return false;
  91. }
  92. srand(time(0));
  93. ofstream output(file_path);
  94. if (output.is_open()) {
  95. unsigned int images_size = m_images.size();
  96. unsigned int c = m_images[0].m_components;
  97. unsigned int width = m_images[0].m_width;
  98. unsigned int height = m_images[0].m_height;
  99. for (unsigned int i = 0; i < 100; i++) {
  100. unsigned int x = rand() % width;
  101. unsigned int y = rand() % height;
  102. unsigned int index = (y * width + x) * c;
  103. for (unsigned int k = 0; k < images_size; k++) {
  104. int val = m_images[k].m_image_data[index];
  105. output << val;
  106. if (k + 1 < images_size) {
  107. output << ',';
  108. } else {
  109. output << '\n';
  110. }
  111. }
  112. }
  113. return true;
  114. } else {
  115. return false;
  116. }
  117. }
  118. bool HdrImageStack::recover_response_function(ResponseFunction &respFunc) {
  119. //TODO implement
  120. return false;
  121. }