]> git.datanom.net - lensdb.git/blob - lib/main.cpp
Fix collecting lens data
[lensdb.git] / lib / main.cpp
1 #include "main.hpp"
2 #include <cstring>
3 #include <string.h>
4
5 using namespace std;
6
7 static void print_all(gpointer key, gpointer value, gpointer user_data) {
8 cout << (gchar*)key << " = " << (gchar*)value << endl;
9 }
10
11 ExifScanner::ExifScanner(const char* file): file(file) {
12 exif_tags = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
13 get_data_from_image();
14 }
15
16 ExifScanner::~ExifScanner() {
17 g_hash_table_destroy(exif_tags);
18 }
19
20 std::string ExifScanner::get_error() {
21 return error;
22 }
23
24 void ExifScanner::get_data_from_image() {
25 if (file != NULL) {
26 // create our ExifTool object
27 ExifTool *et = new ExifTool();
28 // read metadata from the image
29 TagInfo *info = et->ImageInfo(file);
30 if (info) {
31 // print returned information
32 for (TagInfo *i=info; i; i=i->next) {
33 g_hash_table_insert(exif_tags, g_strdup(i->name), g_strdup(i->value));
34 }
35 // we are responsible for deleting the information when done
36 delete info;
37 } else if (et->LastComplete() <= 0) {
38 error = "Error executing exiftool!";
39 }
40 // print exiftool stderr messages
41 char *err = et->GetError();
42 if (err) {
43 error.append("\n");
44 error.append(err);
45 }
46 delete et; // delete our ExifTool object
47 }
48 }
49
50 string ExifScanner::get_maker() {
51 string maker;
52
53 //g_hash_table_foreach(exif_tags, print_all, NULL);
54 const gchar* lensId = (gchar *)g_hash_table_lookup(exif_tags, "LensMake");
55 gulong len = g_utf8_strlen(lensId, -1);
56 if (!lensId || g_strcmp0("Unknown (2822)", lensId) == 0 || len < 1) {
57 const gchar* s = (gchar *)g_hash_table_lookup(exif_tags, "LensID");
58 if (s && g_strcmp0("Unknown (2822)", s) != 0) {
59 gchar* pos = g_strstr_len(s, -1, " ");
60 //cout << pos << endl;
61 if (pos) {
62 //cout << "S length: " << strlen(s) << "POS length: " << strlen(pos) << endl;
63 lensId = g_strndup(s, strlen(s) - strlen(pos));
64 }
65 len = g_utf8_strlen(lensId, -1);
66 }
67 }
68
69 if (!lensId || g_strcmp0("Unknown (2822)", lensId) == 0 || len < 1) {
70 const gchar* s = (gchar *)g_hash_table_lookup(exif_tags, "LensModel");
71 if (s) {
72 gchar* pos = g_strstr_len(s, -1, " ");
73 //cout << pos << endl;
74 if (pos) {
75 //cout << "S length: " << strlen(s) << "POS length: " << strlen(pos) << endl;
76 lensId = g_strndup(s, strlen(s) - strlen(pos));
77 len = g_utf8_strlen(lensId, -1);
78 }
79 }
80 }
81 if (!lensId || len < 1)
82 maker = get_cscring("Unknown");
83 else
84 maker = get_cscring(lensId);
85
86 return maker;
87 }
88
89 string ExifScanner::get_serial_number() {
90 string sn;
91
92 const gchar* id = (gchar *)g_hash_table_lookup(exif_tags, "LensSerialNumber");
93 gulong len = g_utf8_strlen(id, -1);
94 if (!id || len < 1) {
95 id = (gchar *)g_hash_table_lookup(exif_tags, "SerialNumber");
96 len = g_utf8_strlen(id, -1);
97 }
98
99 if (!id || len < 1)
100 sn = get_cscring("Unknown");
101 else
102 sn = get_cscring(id);
103
104 return sn;
105 }
106
107 string ExifScanner::get_lens() {
108 string lens;
109
110 const gchar* lensId = (gchar *)g_hash_table_lookup(exif_tags, "LensID");
111 gulong len = g_utf8_strlen(lensId, -1);
112 if (!lensId || g_strcmp0("Unknown (2822)", lensId) == 0 || len < 1) {
113 lensId = (gchar *)g_hash_table_lookup(exif_tags, "LensModel");
114 len = g_utf8_strlen(lensId, -1);
115 if (!lensId || g_strcmp0("Unknown (2822)", lensId) == 0 || len < 1) {
116 lensId = (gchar *)g_hash_table_lookup(exif_tags, "LensSpec");
117 len = g_utf8_strlen(lensId, -1);
118 }
119 }
120
121 if (!lensId || g_strcmp0("Unknown (2822)", lensId) == 0 || len < 1)
122 lens = get_cscring("Unknown");
123 else
124 lens = get_cscring(lensId);
125
126 return lens;
127 }
128
129 gchar* ExifScanner::get_cscring(string s) {
130 gchar* cstring = g_strdup(s.c_str());
131
132 return cstring;
133 }
This page took 0.922705 seconds and 6 git commands to generate.