diff src/exiv2.cc @ 185:354da67a7ca2

read also iptc and xmp
author nadvornik
date Fri, 15 Feb 2008 10:48:09 +0000
parents 2507418ab7a2
children bd3fc1aa7fe9
line wrap: on
line diff
--- a/src/exiv2.cc	Thu Feb 14 14:04:43 2008 +0000
+++ b/src/exiv2.cc	Fri Feb 15 10:48:09 2008 +0000
@@ -19,7 +19,11 @@
 struct _ExifData
 {
 	Exiv2::ExifData exifData;
-	Exiv2::ExifData::const_iterator iter;
+	Exiv2::ExifData::const_iterator exifIter; /* for exif_get_next_item */
+	Exiv2::IptcData iptcData;
+	Exiv2::IptcData::const_iterator iptcIter; /* for exif_get_next_item */
+	Exiv2::XmpData xmpData;
+	Exiv2::XmpData::const_iterator xmpIter; /* for exif_get_next_item */
 
 	_ExifData(gchar *path, gint parse_color_profile)
 	{
@@ -27,6 +31,8 @@
 		g_assert (image.get() != 0);
 		image->readMetadata();
 		exifData = image->exifData();
+		iptcData = image->iptcData();
+		xmpData = image->xmpData();
 	}
 
 };
@@ -55,10 +61,27 @@
 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
 {
 	try {
-		Exiv2::ExifKey ekey(key);
-		Exiv2::ExifData::iterator pos = exif->exifData.findKey(ekey);
-		if (pos == exif->exifData.end()) return NULL;
-		Exiv2::Exifdatum *item = &*pos;
+		Exiv2::Metadatum *item;
+		try {
+			Exiv2::ExifKey ekey(key);
+			Exiv2::ExifData::iterator pos = exif->exifData.findKey(ekey);
+			if (pos == exif->exifData.end()) return NULL;
+			item = &*pos;
+		}
+		catch (Exiv2::AnyError& e) {
+			try {
+				Exiv2::IptcKey ekey(key);
+				Exiv2::IptcData::iterator pos = exif->iptcData.findKey(ekey);
+				if (pos == exif->iptcData.end()) return NULL;
+				item = &*pos;
+			}
+			catch (Exiv2::AnyError& e) {
+				Exiv2::XmpKey ekey(key);
+				Exiv2::XmpData::iterator pos = exif->xmpData.findKey(ekey);
+				if (pos == exif->xmpData.end()) return NULL;
+				item = &*pos;
+			}
+		}
 		return (ExifItem *)item;
 	}
 	catch (Exiv2::AnyError& e) {
@@ -71,10 +94,29 @@
 ExifItem *exif_get_first_item(ExifData *exif)
 {
 	try {
-		exif->iter = exif->exifData.begin();
-		if (exif->iter == exif->exifData.end()) return NULL;
-		const Exiv2::Exifdatum *item = &*exif->iter;
-		return (ExifItem *)item;
+		exif->exifIter = exif->exifData.begin();
+		exif->iptcIter = exif->iptcData.begin();
+		exif->xmpIter = exif->xmpData.begin();
+		if (exif->exifIter != exif->exifData.end()) 
+			{
+			const Exiv2::Metadatum *item = &*exif->exifIter;
+			exif->exifIter++;
+			return (ExifItem *)item;
+			}
+		if (exif->iptcIter != exif->iptcData.end()) 
+			{
+			const Exiv2::Metadatum *item = &*exif->iptcIter;
+			exif->iptcIter++;
+			return (ExifItem *)item;
+			}
+		if (exif->xmpIter != exif->xmpData.end()) 
+			{
+			const Exiv2::Metadatum *item = &*exif->xmpIter;
+			exif->xmpIter++;
+			return (ExifItem *)item;
+			}
+		return NULL;
+			
 	}
 	catch (Exiv2::AnyError& e) {
 		std::cout << "Caught Exiv2 exception '" << e << "'\n";
@@ -85,10 +127,25 @@
 ExifItem *exif_get_next_item(ExifData *exif)
 {
 	try {
-		exif->iter++;
-		if (exif->iter == exif->exifData.end()) return NULL;
-		const Exiv2::Exifdatum *item = &*exif->iter;
-		return (ExifItem *)item;
+		if (exif->exifIter != exif->exifData.end())
+			{
+			const Exiv2::Metadatum *item = &*exif->exifIter;
+			exif->exifIter++;
+			return (ExifItem *)item;
+		}
+		if (exif->iptcIter != exif->iptcData.end())
+			{
+			const Exiv2::Metadatum *item = &*exif->iptcIter;
+			exif->iptcIter++;
+			return (ExifItem *)item;
+		}
+		if (exif->xmpIter != exif->xmpData.end())
+			{
+			const Exiv2::Metadatum *item = &*exif->xmpIter;
+			exif->xmpIter++;
+			return (ExifItem *)item;
+		}
+		return NULL;
 	}
 	catch (Exiv2::AnyError& e) {
 		std::cout << "Caught Exiv2 exception '" << e << "'\n";
@@ -96,11 +153,11 @@
 	}
 }
 
-const char *exif_item_get_tag_name(ExifItem *item)
+char *exif_item_get_tag_name(ExifItem *item)
 {
 	try {
 		if (!item) return NULL;
-		return ((Exiv2::Exifdatum *)item)->key().c_str();
+		return g_strdup(((Exiv2::Metadatum *)item)->key().c_str());
 	}
 	catch (Exiv2::AnyError& e) {
 		std::cout << "Caught Exiv2 exception '" << e << "'\n";
@@ -112,7 +169,7 @@
 {
 	try {
 		if (!item) return 0;
-		return ((Exiv2::Exifdatum *)item)->tag();
+		return ((Exiv2::Metadatum *)item)->tag();
 	}
 	catch (Exiv2::AnyError& e) {
 		std::cout << "Caught Exiv2 exception '" << e << "'\n";
@@ -124,11 +181,11 @@
 {
 	try {
 		if (!item) return 0;
-		return ((Exiv2::Exifdatum *)item)->count();
+		return ((Exiv2::Metadatum *)item)->count();
 	}
 	catch (Exiv2::AnyError& e) {
 		std::cout << "Caught Exiv2 exception '" << e << "'\n";
-		return NULL;
+		return 0;
 	}
 }
 
@@ -140,10 +197,10 @@
 {
 	try {
 		if (!item) return NULL;
-		return g_strdup(((Exiv2::Exifdatum *)item)->tagLabel().c_str());
+		return g_strdup(((Exiv2::Metadatum *)item)->tagLabel().c_str());
 	}
-	catch (Exiv2::AnyError& e) {
-		std::cout << "Caught Exiv2 exception '" << e << "'\n";
+	catch (std::exception& e) {
+//		std::cout << "Caught Exiv2 exception '" << e << "'\n";
 		return NULL;
 	}
 }
@@ -185,7 +242,7 @@
 {
 	try {
 		if (!item) return EXIF_FORMAT_UNKNOWN;
-		guint id = ((Exiv2::Exifdatum *)item)->typeId();
+		guint id = ((Exiv2::Metadatum *)item)->typeId();
 		if (id >= (sizeof(format_id_trans_tbl) / sizeof(format_id_trans_tbl[0])) ) return EXIF_FORMAT_UNKNOWN;
 		return format_id_trans_tbl[id];
 	}
@@ -199,7 +256,7 @@
 {
 	try {
 		if (!item) return NULL;
-		return ((Exiv2::Exifdatum *)item)->typeName();
+		return ((Exiv2::Metadatum *)item)->typeName();
 	}
 	catch (Exiv2::AnyError& e) {
 		std::cout << "Caught Exiv2 exception '" << e << "'\n";
@@ -212,9 +269,10 @@
 {
 	try {
 		if (!item) return NULL;
-		std::stringstream str;
-		str << *((Exiv2::Exifdatum *)item);
-		return g_strdup(str.str().c_str());
+//		std::stringstream str;  // does not work with Exiv2::Metadatum because operator<< is not virtual
+//		str << *((Exiv2::Metadatum *)item);
+//		return g_strdup(str.str().c_str());
+		return g_strdup(((Exiv2::Metadatum *)item)->toString().c_str());
 	}
 	catch (Exiv2::AnyError& e) {
 		return NULL;
@@ -226,7 +284,7 @@
 {
 	try {
 		if (!item) return 0;
-		return ((Exiv2::Exifdatum *)item)->toLong();
+		return ((Exiv2::Metadatum *)item)->toLong();
 	}
 	catch (Exiv2::AnyError& e) {
 		std::cout << "Caught Exiv2 exception '" << e << "'\n";
@@ -238,7 +296,7 @@
 {
 	try {
 		if (!item) return NULL;
-		Exiv2::Rational v = ((Exiv2::Exifdatum *)item)->toRational();
+		Exiv2::Rational v = ((Exiv2::Metadatum *)item)->toRational();
 		static ExifRational ret;
 		ret.num = v.first;
 		ret.den = v.second;