8 #include <exiv2/image.hpp>
9 #include <exiv2/exif.hpp>
12 #include <sys/types.h>
18 #include <exiv2/tiffparser.hpp>
19 #include <exiv2/tiffcomposite.hpp>
20 #include <exiv2/tiffvisitor.hpp>
21 #include <exiv2/tiffimage.hpp>
22 #include <exiv2/cr2image.hpp>
23 #include <exiv2/orfimage.hpp>
24 #include <exiv2/rafimage.hpp>
25 #include <exiv2/futils.hpp>
37 Exiv2::Image::AutoPtr image;
38 Exiv2::ExifData::const_iterator exifIter; /* for exif_get_next_item */
39 Exiv2::IptcData::const_iterator iptcIter; /* for exif_get_next_item */
40 Exiv2::XmpData::const_iterator xmpIter; /* for exif_get_next_item */
42 _ExifData(gchar *path, gint parse_color_profile)
44 image = Exiv2::ImageFactory::open(path);
45 // g_assert (image.get() != 0);
46 image->readMetadata();
51 image->writeMetadata();
59 ExifData *exif_read(gchar *path, gint parse_color_profile)
61 printf("exif %s\n", path);
63 return new ExifData(path, parse_color_profile);
65 catch (Exiv2::AnyError& e) {
66 std::cout << "Caught Exiv2 exception '" << e << "'\n";
72 int exif_write(ExifData *exif)
75 exif->writeMetadata();
78 catch (Exiv2::AnyError& e) {
79 std::cout << "Caught Exiv2 exception '" << e << "'\n";
86 void exif_free(ExifData *exif)
92 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
95 Exiv2::Metadatum *item;
97 Exiv2::ExifKey ekey(key);
98 Exiv2::ExifData::iterator pos = exif->image->exifData().findKey(ekey);
99 if (pos == exif->image->exifData().end()) return NULL;
102 catch (Exiv2::AnyError& e) {
104 Exiv2::IptcKey ekey(key);
105 Exiv2::IptcData::iterator pos = exif->image->iptcData().findKey(ekey);
106 if (pos == exif->image->iptcData().end()) return NULL;
109 catch (Exiv2::AnyError& e) {
110 Exiv2::XmpKey ekey(key);
111 Exiv2::XmpData::iterator pos = exif->image->xmpData().findKey(ekey);
112 if (pos == exif->image->xmpData().end()) return NULL;
116 return (ExifItem *)item;
118 catch (Exiv2::AnyError& e) {
119 std::cout << "Caught Exiv2 exception '" << e << "'\n";
124 ExifItem *exif_add_item(ExifData *exif, const gchar *key)
127 Exiv2::Metadatum *item;
129 Exiv2::ExifKey ekey(key);
130 exif->image->exifData().add(ekey, NULL);
131 Exiv2::ExifData::iterator pos = exif->image->exifData().end(); // a hack, there should be a better way to get the currently added item
135 catch (Exiv2::AnyError& e) {
137 Exiv2::IptcKey ekey(key);
138 exif->image->iptcData().add(ekey, NULL);
139 Exiv2::IptcData::iterator pos = exif->image->iptcData().end();
143 catch (Exiv2::AnyError& e) {
144 Exiv2::XmpKey ekey(key);
145 exif->image->xmpData().add(ekey, NULL);
146 Exiv2::XmpData::iterator pos = exif->image->xmpData().end();
151 return (ExifItem *)item;
153 catch (Exiv2::AnyError& e) {
154 std::cout << "Caught Exiv2 exception '" << e << "'\n";
160 ExifItem *exif_get_first_item(ExifData *exif)
163 exif->exifIter = exif->image->exifData().begin();
164 exif->iptcIter = exif->image->iptcData().begin();
165 exif->xmpIter = exif->image->xmpData().begin();
166 if (exif->exifIter != exif->image->exifData().end())
168 const Exiv2::Metadatum *item = &*exif->exifIter;
170 return (ExifItem *)item;
172 if (exif->iptcIter != exif->image->iptcData().end())
174 const Exiv2::Metadatum *item = &*exif->iptcIter;
176 return (ExifItem *)item;
178 if (exif->xmpIter != exif->image->xmpData().end())
180 const Exiv2::Metadatum *item = &*exif->xmpIter;
182 return (ExifItem *)item;
187 catch (Exiv2::AnyError& e) {
188 std::cout << "Caught Exiv2 exception '" << e << "'\n";
193 ExifItem *exif_get_next_item(ExifData *exif)
196 if (exif->exifIter != exif->image->exifData().end())
198 const Exiv2::Metadatum *item = &*exif->exifIter;
200 return (ExifItem *)item;
202 if (exif->iptcIter != exif->image->iptcData().end())
204 const Exiv2::Metadatum *item = &*exif->iptcIter;
206 return (ExifItem *)item;
208 if (exif->xmpIter != exif->image->xmpData().end())
210 const Exiv2::Metadatum *item = &*exif->xmpIter;
212 return (ExifItem *)item;
216 catch (Exiv2::AnyError& e) {
217 std::cout << "Caught Exiv2 exception '" << e << "'\n";
222 char *exif_item_get_tag_name(ExifItem *item)
225 if (!item) return NULL;
226 return g_strdup(((Exiv2::Metadatum *)item)->key().c_str());
228 catch (Exiv2::AnyError& e) {
229 std::cout << "Caught Exiv2 exception '" << e << "'\n";
234 guint exif_item_get_tag_id(ExifItem *item)
238 return ((Exiv2::Metadatum *)item)->tag();
240 catch (Exiv2::AnyError& e) {
241 std::cout << "Caught Exiv2 exception '" << e << "'\n";
246 guint exif_item_get_elements(ExifItem *item)
250 return ((Exiv2::Metadatum *)item)->count();
252 catch (Exiv2::AnyError& e) {
253 std::cout << "Caught Exiv2 exception '" << e << "'\n";
258 char *exif_item_get_data(ExifItem *item, guint *data_len)
262 char *exif_item_get_description(ExifItem *item)
265 if (!item) return NULL;
266 return g_strdup(((Exiv2::Metadatum *)item)->tagLabel().c_str());
268 catch (std::exception& e) {
269 // std::cout << "Caught Exiv2 exception '" << e << "'\n";
275 invalidTypeId, unsignedByte, asciiString, unsignedShort,
276 unsignedLong, unsignedRational, signedByte, undefined,
277 signedShort, signedLong, signedRational, string,
278 date, time, comment, directory,
279 xmpText, xmpAlt, xmpBag, xmpSeq,
283 static guint format_id_trans_tbl [] = {
285 EXIF_FORMAT_BYTE_UNSIGNED,
287 EXIF_FORMAT_SHORT_UNSIGNED,
288 EXIF_FORMAT_LONG_UNSIGNED,
289 EXIF_FORMAT_RATIONAL_UNSIGNED,
291 EXIF_FORMAT_UNDEFINED,
294 EXIF_FORMAT_RATIONAL,
298 EXIF_FORMAT_UNDEFINED,
307 guint exif_item_get_format_id(ExifItem *item)
310 if (!item) return EXIF_FORMAT_UNKNOWN;
311 guint id = ((Exiv2::Metadatum *)item)->typeId();
312 if (id >= (sizeof(format_id_trans_tbl) / sizeof(format_id_trans_tbl[0])) ) return EXIF_FORMAT_UNKNOWN;
313 return format_id_trans_tbl[id];
315 catch (Exiv2::AnyError& e) {
316 std::cout << "Caught Exiv2 exception '" << e << "'\n";
317 return EXIF_FORMAT_UNKNOWN;
321 const char *exif_item_get_format_name(ExifItem *item, gint brief)
324 if (!item) return NULL;
325 return ((Exiv2::Metadatum *)item)->typeName();
327 catch (Exiv2::AnyError& e) {
328 std::cout << "Caught Exiv2 exception '" << e << "'\n";
334 gchar *exif_item_get_data_as_text(ExifItem *item)
337 if (!item) return NULL;
338 // std::stringstream str; // does not work with Exiv2::Metadatum because operator<< is not virtual
339 // str << *((Exiv2::Metadatum *)item);
340 // return g_strdup(str.str().c_str());
341 return g_strdup(((Exiv2::Metadatum *)item)->toString().c_str());
343 catch (Exiv2::AnyError& e) {
349 gint exif_item_get_integer(ExifItem *item, gint *value)
353 *value = ((Exiv2::Metadatum *)item)->toLong();
356 catch (Exiv2::AnyError& e) {
357 std::cout << "Caught Exiv2 exception '" << e << "'\n";
362 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign)
365 if (!item) return NULL;
366 Exiv2::Rational v = ((Exiv2::Metadatum *)item)->toRational();
367 static ExifRational ret;
372 catch (Exiv2::AnyError& e) {
373 std::cout << "Caught Exiv2 exception '" << e << "'\n";
378 const gchar *exif_get_tag_description_by_key(const gchar *key)
381 Exiv2::ExifKey ekey(key);
382 return Exiv2::ExifTags::tagLabel(ekey.tag(), ekey.ifdId ());
384 catch (Exiv2::AnyError& e) {
385 std::cout << "Caught Exiv2 exception '" << e << "'\n";
390 int exif_item_set_string(ExifItem *item, const char *str)
394 ((Exiv2::Metadatum *)item)->setValue(std::string(str));
397 catch (Exiv2::AnyError& e) {
402 int exif_item_delete(ExifData *exif, ExifItem *item)
406 for (Exiv2::ExifData::iterator i = exif->image->exifData().begin(); i != exif->image->exifData().end(); ++i) {
407 if (((Exiv2::Metadatum *)item) == &*i) {
408 i = exif->image->exifData().erase(i);
412 for (Exiv2::IptcData::iterator i = exif->image->iptcData().begin(); i != exif->image->iptcData().end(); ++i) {
413 if (((Exiv2::Metadatum *)item) == &*i) {
414 i = exif->image->iptcData().erase(i);
418 for (Exiv2::XmpData::iterator i = exif->image->xmpData().begin(); i != exif->image->xmpData().end(); ++i) {
419 if (((Exiv2::Metadatum *)item) == &*i) {
420 i = exif->image->xmpData().erase(i);
427 catch (Exiv2::AnyError& e) {
436 /* This is a dirty hack to support raw file preview, bassed on
437 tiffparse.cpp from Exiv2 examples */
445 const Exiv2::Value *find(uint16_t tag, uint16_t group);
447 unsigned long preview_offset();
451 Exiv2::TiffComponent::AutoPtr rootDir;
452 Exiv2::byte *map_data;
454 unsigned long offset;
457 using namespace Exiv2;
459 RawFile::RawFile(int fd) : map_data(NULL), map_len(0), offset(0)
462 if (fstat(fd, &st) == -1)
466 map_len = st.st_size;
467 map_data = (Exiv2::byte *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
468 if (map_data == MAP_FAILED)
472 type = Exiv2::ImageFactory::getType(map_data, map_len);
474 TiffHeaderBase *tiffHeader;
476 case Exiv2::ImageType::tiff:
477 tiffHeader = new TiffHeade2();
479 case Exiv2::ImageType::cr2:
480 tiffHeader = new Cr2Header();
482 case Exiv2::ImageType::orf:
483 tiffHeader = new OrfHeader();
485 case Exiv2::ImageType::raf:
486 if (map_len < 84 + 4) throw Error(14);
487 offset = getULong(map_data + 84, bigEndian);
491 throw Error(3, "RAW");
494 // process tiff-like formats
495 if (!tiffHeader->read(map_data, map_len)) throw Error(3, "TIFF");
497 TiffCompFactoryFct createFct = TiffCreator::create;
499 rootDir = createFct(Tag::root, Group::none);
500 if (0 == rootDir.get()) {
501 throw Error(1, "No root element defined in TIFF structure");
503 rootDir->setStart(map_data + tiffHeader->offset());
505 TiffRwState::AutoPtr state(
506 new TiffRwState(tiffHeader->byteOrder(), 0, createFct));
508 TiffReader reader(map_data,
513 rootDir->accept(reader);
520 if (map_data && munmap(map_data, map_len) == -1)
522 printf("Failed to unmap file \n");
526 const Value * RawFile::find(uint16_t tag, uint16_t group)
528 printf("%04x %04x\n", tag, group);
529 TiffFinder finder(tag, group);
530 rootDir->accept(finder);
531 TiffEntryBase* te = dynamic_cast<TiffEntryBase*>(finder.result());
538 unsigned long RawFile::preview_offset()
541 if (offset) return offset;
543 if (type == Exiv2::ImageType::cr2)
545 val = find(0x111, Group::ifd0);
546 if (val) return val->toLong();
551 val = find(0x201, Group::sub0_0);
552 if (val) return val->toLong();
554 val = find(0x201, Group::ifd0);
555 if (val) return val->toLong();
557 val = find(0x201, Group::ignr); // for PEF files, originally it was probably ifd2
558 if (val) return val->toLong();
560 val = find(0x111, Group::sub0_1); // dng
561 if (val) return val->toLong();
567 const static char *raw_ext_list[] = { ".cr2", ".nef", ".pef", ".arw", NULL };
569 extern "C" gint format_raw_img_exif_offsets_fd(int fd, const gchar *path,
570 unsigned char *header_data, const guint header_len,
571 guint *image_offset, guint *exif_offset)
574 unsigned long offset;
576 /* given image pathname, first do simple (and fast) file extension test */
583 ext = strrchr(path, '.');
584 if (!ext) return FALSE;
586 for (i = 0; raw_ext_list[i]; i++)
588 if (strcasecmp(raw_ext_list[i], ext) == 0)
595 if (!match) return FALSE;
601 offset = rf.preview_offset();
603 catch (Exiv2::AnyError& e) {
604 std::cout << "Caught Exiv2 exception '" << e << "'\n";
610 *image_offset = offset;
611 if (lseek(fd, *image_offset, SEEK_SET) != *image_offset)
613 printf("Failed to seek to embedded image\n");
616 if (*exif_offset) *exif_offset = 0;