Browse Source

Add option to create HdrImageStack from CSV file

Johannes Hofmann 7 years ago
parent
commit
37207a5f4f
4 changed files with 68 additions and 8 deletions
  1. 11
    2
      include/haader.h
  2. 1
    1
      include/image.h
  3. 50
    2
      src/haader.cpp
  4. 6
    3
      src/image.cpp

+ 11
- 2
include/haader.h View File

120
             // Read images from the given array of file paths
120
             // Read images from the given array of file paths
121
             bool read_from_files(char * const* file_paths, int number_of_paths);
121
             bool read_from_files(char * const* file_paths, int number_of_paths);
122
 
122
 
123
-            // Add an image from the given file path
124
-            bool add_from_file_path(const char* file_path);
123
+            // Add an image from the given file path.
124
+            // Tries to retrieve the exposure time from the file if
125
+            // exposure_time is omitted or the given value is <= 0.0
126
+            bool add_from_file_path(const char* file_path, double exposure_time=0.0);
127
+
128
+            // Read images from a CSV file in this format:
129
+            // <path_image_1>,<exposure_time_1>
130
+            // <path_image_2>,<exposure_time_2>
131
+            // <path_image_3>,<exposure_time_3>
132
+            // ...
133
+            bool read_from_csv_file(const char* csv_path);
125
 
134
 
126
             bool get_average_image(Image &output);
135
             bool get_average_image(Image &output);
127
             bool get_average_image_slow(Image &output);
136
             bool get_average_image_slow(Image &output);

+ 1
- 1
include/image.h View File

22
 
22
 
23
             void to_string(std::string &s) const;
23
             void to_string(std::string &s) const;
24
 
24
 
25
-            bool read_from_file(const char *file_path);
25
+            bool read_from_file(const char *file_path, double exposure_time=0.0);
26
             bool save_as_ppm_file(const char *file_path, bool ascii=false) const;
26
             bool save_as_ppm_file(const char *file_path, bool ascii=false) const;
27
 
27
 
28
             LdrHistogram histogram() const;
28
             LdrHistogram histogram() const;

+ 50
- 2
src/haader.cpp View File

7
 #include <math.h>
7
 #include <math.h>
8
 
8
 
9
 #include "haader.h"
9
 #include "haader.h"
10
+#include "csv_reader.h"
10
 
11
 
11
 #define max_number(a, b) (a) > (b) ? (a) : (b)
12
 #define max_number(a, b) (a) > (b) ? (a) : (b)
12
 #define min_number(a, b) (a) < (b) ? (a) : (b)
13
 #define min_number(a, b) (a) < (b) ? (a) : (b)
343
     return true;
344
     return true;
344
 }
345
 }
345
 
346
 
346
-bool HdrImageStack::add_from_file_path(const char* file_path) {
347
+bool HdrImageStack::add_from_file_path(const char* file_path, double exposure_time) {
347
     unsigned int i = m_images.size();
348
     unsigned int i = m_images.size();
348
     m_images.push_back(Image());
349
     m_images.push_back(Image());
349
 
350
 
350
-    if (m_images[i].read_from_file(file_path)) {
351
+    if (m_images[i].read_from_file(file_path, exposure_time)) {
351
         cout << "Read \"" << file_path << "\"" << endl;
352
         cout << "Read \"" << file_path << "\"" << endl;
352
         if (!m_images[0].has_equal_dimensions(m_images[i])) {
353
         if (!m_images[0].has_equal_dimensions(m_images[i])) {
353
             cerr << "HdrImageStack::add_from_file_path: Dimensions do not match (\""
354
             cerr << "HdrImageStack::add_from_file_path: Dimensions do not match (\""
372
     return true;
373
     return true;
373
 }
374
 }
374
 
375
 
376
+struct CSVUserData {
377
+    bool ok;
378
+    HdrImageStack *stack;
379
+};
380
+
381
+static bool csv_row_callback(const CSVRow &row, void *userData) {
382
+    CSVUserData *cud = (CSVUserData*)userData;
383
+
384
+    string path;
385
+    double exposure_time = 0.0;
386
+
387
+    bool ok = row.getFieldAsString(0, &path);
388
+    ok = ok && row.getFieldAsDouble(1, &exposure_time);
389
+
390
+    if (ok) {
391
+        ok = ok && cud->stack->add_from_file_path(path.c_str(), exposure_time);
392
+    } else {
393
+        cout << "Failed to parse line " << row.getLineNumber() << endl;
394
+    }
395
+
396
+    if (!ok) {
397
+        cud->ok = false;
398
+        return false;
399
+    } else {
400
+        return true;
401
+    }
402
+}
403
+
404
+bool HdrImageStack::read_from_csv_file(const char* csv_path) {
405
+    string path(csv_path);
406
+
407
+    CSVUserData cud;
408
+    cud.ok = true;
409
+    cud.stack = this;
410
+
411
+    bool csv_ok = CSVReader::readFromFile(path, &csv_row_callback, &cud);
412
+
413
+    if (csv_ok && cud.ok) {
414
+        cout << "Successful read from " << csv_path << endl;
415
+        return true;
416
+    } else {
417
+        cout << "Failed to read from CSV " << csv_path << endl;
418
+        m_images.clear();
419
+        return false;
420
+    }
421
+}
422
+
375
 bool HdrImageStack::get_average_image_slow(Image &output) {
423
 bool HdrImageStack::get_average_image_slow(Image &output) {
376
     if (m_images.size() == 0) {
424
     if (m_images.size() == 0) {
377
         return false;
425
         return false;

+ 6
- 3
src/image.cpp View File

33
     s = ss.str();
33
     s = ss.str();
34
 }
34
 }
35
 
35
 
36
-bool Image::read_from_file(const char *file_path) {
36
+bool Image::read_from_file(const char *file_path, double exposure_time) {
37
     int x = 0;
37
     int x = 0;
38
     int y = 0;
38
     int y = 0;
39
     int components = 0;
39
     int components = 0;
47
         size_t size = (size_t)x * (size_t)y * (size_t)components;
47
         size_t size = (size_t)x * (size_t)y * (size_t)components;
48
         m_image_data.assign(data, data + size);
48
         m_image_data.assign(data, data + size);
49
 
49
 
50
-        // try to read EXIF data
51
-        {
50
+        if (exposure_time <= 0.0) {
51
+            // try to read EXIF data
52
+
52
             ExifTags tags;
53
             ExifTags tags;
53
             ifstream stream;
54
             ifstream stream;
54
             stream.open(file_path);
55
             stream.open(file_path);
58
                     cout << "Exposure time: " << m_exposure_time << " sec" << endl;
59
                     cout << "Exposure time: " << m_exposure_time << " sec" << endl;
59
                 }
60
                 }
60
             }
61
             }
62
+        } else {
63
+            m_exposure_time = exposure_time;
61
         }
64
         }
62
 
65
 
63
         return true;
66
         return true;