Skip to content

Commit

Permalink
read also iptc and xmp
Browse files Browse the repository at this point in the history
  • Loading branch information
nadvornik committed Feb 15, 2008
1 parent 8c7b5e4 commit 406e542
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 35 deletions.
6 changes: 3 additions & 3 deletions src/bar_exif.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ static void bar_exif_update(ExifBar *eb)
{
GtkListStore *store;
GtkTreeIter iter;
GList *work;
ExifItem *item;

store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(eb->listview)));
Expand All @@ -250,11 +249,11 @@ static void bar_exif_update(ExifBar *eb)
while (item)
{
gchar *tag;
const gchar *tag_name;
gchar *tag_name;
gchar *text;
const gchar *format;
gchar *elements;
const gchar *description;
gchar *description;

tag = g_strdup_printf("0x%04x", exif_item_get_tag_id(item));
tag_name = exif_item_get_tag_name(item);
Expand All @@ -277,6 +276,7 @@ static void bar_exif_update(ExifBar *eb)
g_free(text);
g_free(elements);
g_free(description);
g_free(tag_name);
item = exif_get_next_item(exif);
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/exif.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ static void exif_item_free(ExifItem *item)
const char *exif_item_get_tag_name(ExifItem *item)
{
if (!item || !item->marker) return NULL;
return item->marker->key;
return g_strdup(item->marker->key);
}

guint exif_item_get_tag_id(ExifItem *item)
Expand Down Expand Up @@ -694,7 +694,9 @@ void exif_item_copy_data(ExifItem *item, void *src, guint len,
if (!dest ||
ExifFormatList[src_format].size * ne > len)
{
printf("exif tag %s data size mismatch\n", exif_item_get_tag_name(item));
gchar *tag = exif_item_get_tag_name(item);
printf("exif tag %s data size mismatch\n", tag);
g_free(tag);
return;
}

Expand Down Expand Up @@ -1556,8 +1558,10 @@ static void exif_write_item(FILE *f, ExifItem *item)
text = exif_item_get_data_as_text(item);
if (text)
{
gchar *tag = exif_item_get_tag_name(item);
fprintf(f, "%4x %9s %30s %s\n", item->tag, ExifFormatList[item->format].short_name,
exif_item_get_tag_name(item), text);
tag, text);
g_free(tag);
}
g_free(text);
}
Expand Down
2 changes: 1 addition & 1 deletion src/exif.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ ExifItem *exif_get_item(ExifData *exif, const gchar *key);
ExifItem *exif_get_first_item(ExifData *exif);
ExifItem *exif_get_next_item(ExifData *exif);

const char *exif_item_get_tag_name(ExifItem *item);
char *exif_item_get_tag_name(ExifItem *item);
guint exif_item_get_tag_id(ExifItem *item);
guint exif_item_get_elements(ExifItem *item);
char *exif_item_get_data(ExifItem *item, guint *data_len);
Expand Down
114 changes: 86 additions & 28 deletions src/exiv2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ extern "C" {
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)
{
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(path);
g_assert (image.get() != 0);
image->readMetadata();
exifData = image->exifData();
iptcData = image->iptcData();
xmpData = image->xmpData();
}

};
Expand Down Expand Up @@ -55,10 +61,27 @@ void exif_free(ExifData *exif)
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) {
Expand All @@ -71,10 +94,29 @@ ExifItem *exif_get_item(ExifData *exif, const gchar *key)
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";
Expand All @@ -85,22 +127,37 @@ ExifItem *exif_get_first_item(ExifData *exif)
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";
return NULL;
}
}

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";
Expand All @@ -112,7 +169,7 @@ guint exif_item_get_tag_id(ExifItem *item)
{
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";
Expand All @@ -124,11 +181,11 @@ guint exif_item_get_elements(ExifItem *item)
{
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;
}
}

Expand All @@ -140,10 +197,10 @@ char *exif_item_get_description(ExifItem *item)
{
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;
}
}
Expand Down Expand Up @@ -185,7 +242,7 @@ guint exif_item_get_format_id(ExifItem *item)
{
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];
}
Expand All @@ -199,7 +256,7 @@ const char *exif_item_get_format_name(ExifItem *item, gint brief)
{
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";
Expand All @@ -212,9 +269,10 @@ gchar *exif_item_get_data_as_text(ExifItem *item)
{
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;
Expand All @@ -226,7 +284,7 @@ gint exif_item_get_integer(ExifItem *item, gint *value)
{
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";
Expand All @@ -238,7 +296,7 @@ ExifRational *exif_item_get_rational(ExifItem *item, gint *sign)
{
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;
Expand Down

0 comments on commit 406e542

Please sign in to comment.