8 #include <exiv2/image.hpp>
9 #include <exiv2/exif.hpp>
21 Exiv2::ExifData exifData;
22 Exiv2::ExifData::const_iterator exifIter; /* for exif_get_next_item */
23 Exiv2::IptcData iptcData;
24 Exiv2::IptcData::const_iterator iptcIter; /* for exif_get_next_item */
25 Exiv2::XmpData xmpData;
26 Exiv2::XmpData::const_iterator xmpIter; /* for exif_get_next_item */
28 _ExifData(gchar *path, gint parse_color_profile)
30 Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(path);
31 g_assert (image.get() != 0);
32 image->readMetadata();
33 exifData = image->exifData();
34 iptcData = image->iptcData();
35 xmpData = image->xmpData();
42 ExifData *exif_read(gchar *path, gint parse_color_profile)
44 printf("exif %s\n", path);
46 return new ExifData(path, parse_color_profile);
48 catch (Exiv2::AnyError& e) {
49 std::cout << "Caught Exiv2 exception '" << e << "'\n";
55 void exif_free(ExifData *exif)
61 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
64 Exiv2::Metadatum *item;
66 Exiv2::ExifKey ekey(key);
67 Exiv2::ExifData::iterator pos = exif->exifData.findKey(ekey);
68 if (pos == exif->exifData.end()) return NULL;
71 catch (Exiv2::AnyError& e) {
73 Exiv2::IptcKey ekey(key);
74 Exiv2::IptcData::iterator pos = exif->iptcData.findKey(ekey);
75 if (pos == exif->iptcData.end()) return NULL;
78 catch (Exiv2::AnyError& e) {
79 Exiv2::XmpKey ekey(key);
80 Exiv2::XmpData::iterator pos = exif->xmpData.findKey(ekey);
81 if (pos == exif->xmpData.end()) return NULL;
85 return (ExifItem *)item;
87 catch (Exiv2::AnyError& e) {
88 std::cout << "Caught Exiv2 exception '" << e << "'\n";
94 ExifItem *exif_get_first_item(ExifData *exif)
97 exif->exifIter = exif->exifData.begin();
98 exif->iptcIter = exif->iptcData.begin();
99 exif->xmpIter = exif->xmpData.begin();
100 if (exif->exifIter != exif->exifData.end())
102 const Exiv2::Metadatum *item = &*exif->exifIter;
104 return (ExifItem *)item;
106 if (exif->iptcIter != exif->iptcData.end())
108 const Exiv2::Metadatum *item = &*exif->iptcIter;
110 return (ExifItem *)item;
112 if (exif->xmpIter != exif->xmpData.end())
114 const Exiv2::Metadatum *item = &*exif->xmpIter;
116 return (ExifItem *)item;
121 catch (Exiv2::AnyError& e) {
122 std::cout << "Caught Exiv2 exception '" << e << "'\n";
127 ExifItem *exif_get_next_item(ExifData *exif)
130 if (exif->exifIter != exif->exifData.end())
132 const Exiv2::Metadatum *item = &*exif->exifIter;
134 return (ExifItem *)item;
136 if (exif->iptcIter != exif->iptcData.end())
138 const Exiv2::Metadatum *item = &*exif->iptcIter;
140 return (ExifItem *)item;
142 if (exif->xmpIter != exif->xmpData.end())
144 const Exiv2::Metadatum *item = &*exif->xmpIter;
146 return (ExifItem *)item;
150 catch (Exiv2::AnyError& e) {
151 std::cout << "Caught Exiv2 exception '" << e << "'\n";
156 char *exif_item_get_tag_name(ExifItem *item)
159 if (!item) return NULL;
160 return g_strdup(((Exiv2::Metadatum *)item)->key().c_str());
162 catch (Exiv2::AnyError& e) {
163 std::cout << "Caught Exiv2 exception '" << e << "'\n";
168 guint exif_item_get_tag_id(ExifItem *item)
172 return ((Exiv2::Metadatum *)item)->tag();
174 catch (Exiv2::AnyError& e) {
175 std::cout << "Caught Exiv2 exception '" << e << "'\n";
180 guint exif_item_get_elements(ExifItem *item)
184 return ((Exiv2::Metadatum *)item)->count();
186 catch (Exiv2::AnyError& e) {
187 std::cout << "Caught Exiv2 exception '" << e << "'\n";
192 char *exif_item_get_data(ExifItem *item, guint *data_len)
196 char *exif_item_get_description(ExifItem *item)
199 if (!item) return NULL;
200 return g_strdup(((Exiv2::Metadatum *)item)->tagLabel().c_str());
202 catch (std::exception& e) {
203 // std::cout << "Caught Exiv2 exception '" << e << "'\n";
209 invalidTypeId, unsignedByte, asciiString, unsignedShort,
210 unsignedLong, unsignedRational, signedByte, undefined,
211 signedShort, signedLong, signedRational, string,
212 date, time, comment, directory,
213 xmpText, xmpAlt, xmpBag, xmpSeq,
217 static guint format_id_trans_tbl [] = {
219 EXIF_FORMAT_BYTE_UNSIGNED,
221 EXIF_FORMAT_SHORT_UNSIGNED,
222 EXIF_FORMAT_LONG_UNSIGNED,
223 EXIF_FORMAT_RATIONAL_UNSIGNED,
225 EXIF_FORMAT_UNDEFINED,
228 EXIF_FORMAT_RATIONAL,
232 EXIF_FORMAT_UNDEFINED,
241 guint exif_item_get_format_id(ExifItem *item)
244 if (!item) return EXIF_FORMAT_UNKNOWN;
245 guint id = ((Exiv2::Metadatum *)item)->typeId();
246 if (id >= (sizeof(format_id_trans_tbl) / sizeof(format_id_trans_tbl[0])) ) return EXIF_FORMAT_UNKNOWN;
247 return format_id_trans_tbl[id];
249 catch (Exiv2::AnyError& e) {
250 std::cout << "Caught Exiv2 exception '" << e << "'\n";
251 return EXIF_FORMAT_UNKNOWN;
255 const char *exif_item_get_format_name(ExifItem *item, gint brief)
258 if (!item) return NULL;
259 return ((Exiv2::Metadatum *)item)->typeName();
261 catch (Exiv2::AnyError& e) {
262 std::cout << "Caught Exiv2 exception '" << e << "'\n";
268 gchar *exif_item_get_data_as_text(ExifItem *item)
271 if (!item) return NULL;
272 // std::stringstream str; // does not work with Exiv2::Metadatum because operator<< is not virtual
273 // str << *((Exiv2::Metadatum *)item);
274 // return g_strdup(str.str().c_str());
275 return g_strdup(((Exiv2::Metadatum *)item)->toString().c_str());
277 catch (Exiv2::AnyError& e) {
283 gint exif_item_get_integer(ExifItem *item, gint *value)
287 return ((Exiv2::Metadatum *)item)->toLong();
289 catch (Exiv2::AnyError& e) {
290 std::cout << "Caught Exiv2 exception '" << e << "'\n";
295 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign)
298 if (!item) return NULL;
299 Exiv2::Rational v = ((Exiv2::Metadatum *)item)->toRational();
300 static ExifRational ret;
305 catch (Exiv2::AnyError& e) {
306 std::cout << "Caught Exiv2 exception '" << e << "'\n";
311 const gchar *exif_get_tag_description_by_key(const gchar *key)
314 Exiv2::ExifKey ekey(key);
315 return Exiv2::ExifTags::tagLabel(ekey.tag(), ekey.ifdId ());
317 catch (Exiv2::AnyError& e) {
318 std::cout << "Caught Exiv2 exception '" << e << "'\n";
323 gint format_raw_img_exif_offsets_fd(int fd, const gchar *path,
324 unsigned char *header_data, const guint header_len,
325 guint *image_offset, guint *exif_offset)