]> git.datanom.net - gtk_lensdatabase.git/blob - lib/main.cpp
On the way
[gtk_lensdatabase.git] / lib / main.cpp
1 #include "main.hpp"
2 #include <cstring>
3 #include <string.h>
4
5 using namespace std;
6
7 static const char* LensMakers[] = {
8 "7Artisans",
9 "Canon",
10 "Carl Zeiss",
11 "Chinon",
12 "Cosina",
13 "Essilor",
14 "EssilorLuxottica",
15 "Fujifilm",
16 "Goerz",
17 "Hoya",
18 "Kenko",
19 "Konica",
20 "Leica",
21 "Leidolf",
22 "Lumix",
23 "Meopta",
24 "Meyer Optik Görlitz",
25 "Minolta",
26 "Nikon",
27 "Nisi",
28 "Olympus",
29 "OM Digital Solutions",
30 "Pentacon",
31 "Pentax",
32 "Pergear",
33 "Rodenstock",
34 "Samyang",
35 "Schneider Kreuznach",
36 "Seagull",
37 "Seiko",
38 "Sigma",
39 "Silor",
40 "Schneider Kreuznach",
41 "Soligor",
42 "Sony",
43 "Tamron",
44 "Tokina",
45 "Topcon",
46 "TTArtisan",
47 "Yongnuo",
48 "Viltrox",
49 "Voigtländer",
50 NULL
51 };
52
53 static void print_all(gpointer key, gpointer value, gpointer user_data) {
54 cout << (gchar*)key << " = " << (gchar*)value << endl;
55 }
56
57 ExifScanner::ExifScanner(const char* file): file(file) {
58 exif_tags = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
59 get_data_from_image();
60 }
61
62 ExifScanner::~ExifScanner() {
63 g_hash_table_destroy(exif_tags);
64 }
65
66 std::string ExifScanner::get_error() {
67 return error;
68 }
69
70 void ExifScanner::get_data_from_image() {
71 if (file != NULL) {
72 // create our ExifTool object
73 ExifTool *et = new ExifTool();
74 // read metadata from the image
75 TagInfo *info = et->ImageInfo(file);
76 if (info) {
77 // print returned information
78 for (TagInfo *i=info; i; i=i->next) {
79 g_hash_table_insert(exif_tags, g_strdup(i->name), g_strdup(i->value));
80 }
81 // we are responsible for deleting the information when done
82 delete info;
83 } else if (et->LastComplete() <= 0) {
84 error = "Error executing exiftool!";
85 }
86 // print exiftool stderr messages
87 char *err = et->GetError();
88 if (err) {
89 error.append("\n");
90 error.append(err);
91 }
92 delete et; // delete our ExifTool object
93 }
94 }
95
96 string ExifScanner::get_maker() {
97 string maker;
98
99 //g_hash_table_foreach(exif_tags, print_all, NULL);
100 gchar* lensId = g_strdup((gchar *)g_hash_table_lookup(exif_tags, "LensMake"));
101 gulong len = char_len(lensId);
102 if (!lensId || g_strcmp0("Unknown (2822)", lensId) == 0 || len < 1) {
103 const gchar* s = (gchar *)g_hash_table_lookup(exif_tags, "LensID");
104 if (s && g_strcmp0("Unknown (2822)", s) != 0) {
105 gchar* pos = g_strstr_len(s, -1, " ");
106 //cout << pos << endl;
107 if (pos) {
108 //cout << "S length: " << strlen(s) << "POS length: " << strlen(pos) << endl;
109 g_free(lensId);
110 lensId = g_strndup(s, strlen(s) - strlen(pos));
111 }
112 len = g_utf8_strlen(lensId, -1);
113 }
114 }
115
116 if (match_maker(lensId) == false) {
117 g_free(lensId);
118 lensId = NULL;
119 }
120
121 if (!lensId || g_strcmp0("Unknown (2822)", lensId) == 0 || len < 1) {
122 const gchar* s = (gchar *)g_hash_table_lookup(exif_tags, "LensModel");
123 if (s) {
124 gchar* pos = g_strstr_len(s, -1, " ");
125 //cout << pos << endl;
126 if (pos) {
127 //cout << "S length: " << strlen(s) << "POS length: " << strlen(pos) << endl;
128 g_free(lensId);
129 lensId = g_strndup(s, strlen(s) - strlen(pos));
130 len = char_len(lensId);
131 }
132 }
133 }
134
135 if (match_maker(lensId) == false) {
136 g_free(lensId);
137 lensId = NULL;
138 }
139
140 if (!lensId || len < 1)
141 maker = get_cstring("Unknown");
142 else
143 maker = get_cstring(lensId);
144 g_free(lensId);
145
146 return maker;
147 }
148
149 string ExifScanner::get_serial_number() {
150 string sn;
151
152 const gchar* id = (gchar *)g_hash_table_lookup(exif_tags, "LensSerialNumber");
153 gulong len = char_len(id);
154 if (!id || len < 1) {
155 id = (gchar *)g_hash_table_lookup(exif_tags, "SerialNumber");
156 len = char_len(id);
157 }
158
159 if (!id || len < 1)
160 sn = get_cstring("Unknown");
161 else
162 sn = get_cstring(id);
163
164 return sn;
165 }
166
167 string ExifScanner::get_lens() {
168 string lens;
169
170 const gchar* lensId = (gchar *)g_hash_table_lookup(exif_tags, "LensID");
171 gulong len = char_len(lensId);
172 if (!lensId || g_strcmp0("Unknown (2822)", lensId) == 0 || len < 1) {
173 lensId = (gchar *)g_hash_table_lookup(exif_tags, "LensModel");
174 len = char_len(lensId);
175 if (!lensId || g_strcmp0("Unknown (2822)", lensId) == 0 || len < 1) {
176 lensId = (gchar *)g_hash_table_lookup(exif_tags, "LensSpec");
177 len = char_len(lensId);
178 }
179 }
180
181 if (!lensId || g_strcmp0("Unknown (2822)", lensId) == 0 || len < 1)
182 lens = get_cstring("Unknown");
183 else
184 lens = get_cstring(lensId);
185
186 return lens;
187 }
188
189 gchar* ExifScanner::get_cstring(string s) {
190 gchar* cstring = g_strdup(s.c_str());
191
192 return cstring;
193 }
194
195 bool ExifScanner::match_maker(const gchar* maker) {
196 int i = 0;
197 const char* lensmaker = NULL;
198
199 if (maker == NULL) return false;
200
201 lensmaker = LensMakers[i];
202 while (lensmaker) {
203 if (g_ascii_strcasecmp(lensmaker, maker) == 0) {
204 return true;
205 }
206 i++;
207 lensmaker = LensMakers[i];
208 }
209
210 return false;
211 }
212
213 gulong ExifScanner::char_len(const gchar* elem) {
214 if (!elem) return 0;
215
216 return g_utf8_strlen(elem, -1);
217 }
This page took 0.915508 seconds and 6 git commands to generate.