more exiv2 fixes
[geeqie.git] / src / exiv2.cc
1
2 #ifdef HAVE_CONFIG_H
3 #  include "config.h"
4 #endif
5
6 #ifdef HAVE_EXIV2
7
8 #include <exiv2/image.hpp>
9 #include <exiv2/exif.hpp>
10 #include <iostream>
11
12 extern "C" {
13
14 #include <glib.h> 
15 #include "exif.h"
16
17 }
18
19 struct _ExifData
20 {
21         Exiv2::ExifData exifData;
22         Exiv2::ExifData::const_iterator iter;
23
24         _ExifData(gchar *path, gint parse_color_profile)
25         {
26                 Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(path);
27                 g_assert (image.get() != 0);
28                 image->readMetadata();
29                 exifData = image->exifData();
30         }
31
32 };
33
34 extern "C" {
35
36 ExifData *exif_read(gchar *path, gint parse_color_profile)
37 {
38         printf("exif %s\n", path);
39         try {
40                 return new ExifData(path, parse_color_profile);
41         }
42         catch (Exiv2::AnyError& e) {
43                 std::cout << "Caught Exiv2 exception '" << e << "'\n";
44                 return 0;
45         }
46         
47 }
48
49 void exif_free(ExifData *exif)
50 {
51         
52         delete exif;
53 }
54
55 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
56 {
57         try {
58                 Exiv2::ExifKey ekey(key);
59                 Exiv2::ExifData::iterator pos = exif->exifData.findKey(ekey);
60                 if (pos == exif->exifData.end()) return NULL;
61                 Exiv2::Exifdatum *item = &*pos;
62                 return (ExifItem *)item;
63         }
64         catch (Exiv2::AnyError& e) {
65                 std::cout << "Caught Exiv2 exception '" << e << "'\n";
66                 return NULL;
67         }
68 }
69
70
71 ExifItem *exif_get_first_item(ExifData *exif)
72 {
73         try {
74                 exif->iter = exif->exifData.begin();
75                 if (exif->iter == exif->exifData.end()) return NULL;
76                 const Exiv2::Exifdatum *item = &*exif->iter;
77                 return (ExifItem *)item;
78         }
79         catch (Exiv2::AnyError& e) {
80                 std::cout << "Caught Exiv2 exception '" << e << "'\n";
81                 return NULL;
82         }
83 }
84
85 ExifItem *exif_get_next_item(ExifData *exif)
86 {
87         try {
88                 exif->iter++;
89                 if (exif->iter == exif->exifData.end()) return NULL;
90                 const Exiv2::Exifdatum *item = &*exif->iter;
91                 return (ExifItem *)item;
92         }
93         catch (Exiv2::AnyError& e) {
94                 std::cout << "Caught Exiv2 exception '" << e << "'\n";
95                 return NULL;
96         }
97 }
98
99 const char *exif_item_get_tag_name(ExifItem *item)
100 {
101         try {
102                 if (!item) return NULL;
103                 return ((Exiv2::Exifdatum *)item)->key().c_str();
104         }
105         catch (Exiv2::AnyError& e) {
106                 std::cout << "Caught Exiv2 exception '" << e << "'\n";
107                 return NULL;
108         }
109 }
110
111 guint exif_item_get_tag_id(ExifItem *item)
112 {
113         try {
114                 if (!item) return 0;
115                 return ((Exiv2::Exifdatum *)item)->tag();
116         }
117         catch (Exiv2::AnyError& e) {
118                 std::cout << "Caught Exiv2 exception '" << e << "'\n";
119                 return 0;
120         }
121 }
122
123 guint exif_item_get_elements(ExifItem *item)
124 {
125         try {
126                 if (!item) return 0;
127                 return ((Exiv2::Exifdatum *)item)->count();
128         }
129         catch (Exiv2::AnyError& e) {
130                 std::cout << "Caught Exiv2 exception '" << e << "'\n";
131                 return NULL;
132         }
133 }
134
135 char *exif_item_get_data(ExifItem *item, guint *data_len)
136 {
137 }
138
139 char *exif_item_get_description(ExifItem *item)
140 {
141         try {
142                 if (!item) return NULL;
143                 return g_strdup(((Exiv2::Exifdatum *)item)->tagLabel().c_str());
144         }
145         catch (Exiv2::AnyError& e) {
146                 std::cout << "Caught Exiv2 exception '" << e << "'\n";
147                 return NULL;
148         }
149 }
150
151 /*
152 invalidTypeId, unsignedByte, asciiString, unsignedShort,
153   unsignedLong, unsignedRational, signedByte, undefined,
154   signedShort, signedLong, signedRational, string,
155   date, time, comment, directory,
156   xmpText, xmpAlt, xmpBag, xmpSeq,
157   langAlt, lastTypeId 
158 */
159
160 static guint format_id_trans_tbl [] = {
161         EXIF_FORMAT_UNKNOWN,
162         EXIF_FORMAT_BYTE_UNSIGNED,
163         EXIF_FORMAT_STRING,
164         EXIF_FORMAT_SHORT_UNSIGNED,
165         EXIF_FORMAT_LONG_UNSIGNED,
166         EXIF_FORMAT_RATIONAL_UNSIGNED,
167         EXIF_FORMAT_BYTE,
168         EXIF_FORMAT_UNDEFINED,
169         EXIF_FORMAT_SHORT,
170         EXIF_FORMAT_LONG,
171         EXIF_FORMAT_RATIONAL,
172         EXIF_FORMAT_STRING,
173         EXIF_FORMAT_STRING,
174         EXIF_FORMAT_STRING,
175         EXIF_FORMAT_UNDEFINED,
176         EXIF_FORMAT_STRING,
177         EXIF_FORMAT_STRING,
178         EXIF_FORMAT_STRING,
179         EXIF_FORMAT_STRING
180         };
181         
182         
183
184 guint exif_item_get_format_id(ExifItem *item)
185 {
186         try {
187                 if (!item) return EXIF_FORMAT_UNKNOWN;
188                 guint id = ((Exiv2::Exifdatum *)item)->typeId();
189                 if (id >= (sizeof(format_id_trans_tbl) / sizeof(format_id_trans_tbl[0])) ) return EXIF_FORMAT_UNKNOWN;
190                 return format_id_trans_tbl[id];
191         }
192         catch (Exiv2::AnyError& e) {
193                 std::cout << "Caught Exiv2 exception '" << e << "'\n";
194                 return EXIF_FORMAT_UNKNOWN;
195         }
196 }
197
198 const char *exif_item_get_format_name(ExifItem *item, gint brief)
199 {
200         try {
201                 if (!item) return NULL;
202                 return ((Exiv2::Exifdatum *)item)->typeName();
203         }
204         catch (Exiv2::AnyError& e) {
205                 std::cout << "Caught Exiv2 exception '" << e << "'\n";
206                 return NULL;
207         }
208 }
209
210
211 gchar *exif_item_get_data_as_text(ExifItem *item)
212 {
213         try {
214                 if (!item) return NULL;
215                 std::stringstream str;
216                 str << *((Exiv2::Exifdatum *)item);
217                 return g_strdup(str.str().c_str());
218         }
219         catch (Exiv2::AnyError& e) {
220                 return NULL;
221         }
222 }
223
224
225 gint exif_item_get_integer(ExifItem *item, gint *value)
226 {
227         try {
228                 if (!item) return 0;
229                 return ((Exiv2::Exifdatum *)item)->toLong();
230         }
231         catch (Exiv2::AnyError& e) {
232                 std::cout << "Caught Exiv2 exception '" << e << "'\n";
233                 return 0;
234         }
235 }
236
237 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign)
238 {
239         try {
240                 if (!item) return NULL;
241                 Exiv2::Rational v = ((Exiv2::Exifdatum *)item)->toRational();
242                 static ExifRational ret;
243                 ret.num = v.first;
244                 ret.den = v.second;
245                 return &ret;
246         }
247         catch (Exiv2::AnyError& e) {
248                 std::cout << "Caught Exiv2 exception '" << e << "'\n";
249                 return NULL;
250         }
251 }
252
253 const gchar *exif_get_tag_description_by_key(const gchar *key)
254 {
255         try {
256                 Exiv2::ExifKey ekey(key);
257                 return Exiv2::ExifTags::tagLabel(ekey.tag(), ekey.ifdId ());
258         }
259         catch (Exiv2::AnyError& e) {
260                 std::cout << "Caught Exiv2 exception '" << e << "'\n";
261                 return NULL;
262         }
263 }
264
265 gint format_raw_img_exif_offsets_fd(int fd, const gchar *path,
266                                     unsigned char *header_data, const guint header_len,
267                                     guint *image_offset, guint *exif_offset)
268 {
269         return 0;
270 }
271
272 }
273
274 #endif 
275 /* HAVE_EXIV2 */