Преглед на файлове

Add option to create HdrImageStack from CSV file

Johannes Hofmann преди 7 години
родител
ревизия
37207a5f4f
променени са 4 файла, в които са добавени 68 реда и са изтрити 8 реда
  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 Целия файл

@@ -120,8 +120,17 @@ namespace haader {
120 120
             // Read images from the given array of file paths
121 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 135
             bool get_average_image(Image &output);
127 136
             bool get_average_image_slow(Image &output);

+ 1
- 1
include/image.h Целия файл

@@ -22,7 +22,7 @@ namespace haader {
22 22
 
23 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 26
             bool save_as_ppm_file(const char *file_path, bool ascii=false) const;
27 27
 
28 28
             LdrHistogram histogram() const;

+ 50
- 2
src/haader.cpp Целия файл

@@ -7,6 +7,7 @@
7 7
 #include <math.h>
8 8
 
9 9
 #include "haader.h"
10
+#include "csv_reader.h"
10 11
 
11 12
 #define max_number(a, b) (a) > (b) ? (a) : (b)
12 13
 #define min_number(a, b) (a) < (b) ? (a) : (b)
@@ -343,11 +344,11 @@ bool HdrImageStack::read_from_files(char * const* file_paths, int number_of_path
343 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 348
     unsigned int i = m_images.size();
348 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 352
         cout << "Read \"" << file_path << "\"" << endl;
352 353
         if (!m_images[0].has_equal_dimensions(m_images[i])) {
353 354
             cerr << "HdrImageStack::add_from_file_path: Dimensions do not match (\""
@@ -372,6 +373,53 @@ bool HdrImageStack::add_from_file_path(const char* file_path) {
372 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 423
 bool HdrImageStack::get_average_image_slow(Image &output) {
376 424
     if (m_images.size() == 0) {
377 425
         return false;

+ 6
- 3
src/image.cpp Целия файл

@@ -33,7 +33,7 @@ void Image::to_string(string &s) const {
33 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 37
     int x = 0;
38 38
     int y = 0;
39 39
     int components = 0;
@@ -47,8 +47,9 @@ bool Image::read_from_file(const char *file_path) {
47 47
         size_t size = (size_t)x * (size_t)y * (size_t)components;
48 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 53
             ExifTags tags;
53 54
             ifstream stream;
54 55
             stream.open(file_path);
@@ -58,6 +59,8 @@ bool Image::read_from_file(const char *file_path) {
58 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 66
         return true;