// ***************************************************************************** // Main int main(int argc, char* const argv[]) try { if (argc != 2) { std::cout << "Usage: " << argv[0] << " file\n"; return 1; } Exiv2::IptcData iptcData; int rc = iptcData.read(argv[1]); if (rc) { std::string error = Exiv2::IptcData::strError(rc, argv[1]); throw Exiv2::Error(error); } Exiv2::IptcData::iterator end = iptcData.end(); for (Exiv2::IptcData::iterator i = iptcData.begin(); i != end; ++i) { std::cout << std::setw(40) << std::setfill(' ') << std::left << i->key() << " " << "0x" << std::setw(4) << std::setfill('0') << std::right << std::hex << i->tag() << " " << std::dec << i->value() << "\n"; } return rc; } catch (Exiv2::Error& e) { std::cout << "Caught Exiv2 exception '" << e << "'\n"; return -1; }
QStringList KExiv2::getIptcSubCategories() const { try { if (!d->iptcMetadata().empty()) { QStringList subCategories; Exiv2::IptcData iptcData(d->iptcMetadata()); for (Exiv2::IptcData::iterator it = iptcData.begin(); it != iptcData.end(); ++it) { QString key = QString::fromLocal8Bit(it->key().c_str()); if (key == QString("Iptc.Application2.SuppCategory")) { QString val(it->toString().c_str()); subCategories.append(val); } } return subCategories; } } catch( Exiv2::Error& e ) { d->printExiv2ExceptionError("Cannot get Iptc Sub Categories from image using Exiv2 ", e); } return QStringList(); }
QStringList KExiv2::getIptcTagsStringList(const char* iptcTagName, bool escapeCR) const { try { if (!d->iptcMetadata().empty()) { QStringList values; Exiv2::IptcData iptcData(d->iptcMetadata()); for (Exiv2::IptcData::iterator it = iptcData.begin(); it != iptcData.end(); ++it) { QString key = QString::fromLocal8Bit(it->key().c_str()); if (key == QString(iptcTagName)) { QString tagValue(it->toString().c_str()); if (escapeCR) tagValue.replace('\n', ' '); values.append(tagValue); } } return values; } } catch( Exiv2::Error& e ) { d->printExiv2ExceptionError(QString("Cannot find Iptc key '%1' into image using Exiv2 ") .arg(iptcTagName), e); } return QStringList(); }
bool KExiv2::removeIptcTag(const char* iptcTagName, bool setProgramName) const { if (!setProgramId(setProgramName)) return false; try { Exiv2::IptcData::iterator it = d->iptcMetadata().begin(); int i = 0; while(it != d->iptcMetadata().end()) { QString key = QString::fromLocal8Bit(it->key().c_str()); if (key == QString(iptcTagName)) { it = d->iptcMetadata().erase(it); ++i; } else { ++it; } }; if (i > 0) return true; } catch( Exiv2::Error& e ) { d->printExiv2ExceptionError("Cannot remove Iptc tag using Exiv2 ", e); } return false; }
bool KExiv2::setIptcTagsStringList(const char* iptcTagName, int maxSize, const QStringList& oldValues, const QStringList& newValues, bool setProgramName) const { if (!setProgramId(setProgramName)) return false; try { QStringList oldvals = oldValues; QStringList newvals = newValues; kDebug() << d->filePath.toAscii().constData() << " : " << iptcTagName << " => " << newvals.join(",").toAscii().constData(); // Remove all old values. Exiv2::IptcData iptcData(d->iptcMetadata()); Exiv2::IptcData::iterator it = iptcData.begin(); while(it != iptcData.end()) { QString key = QString::fromLocal8Bit(it->key().c_str()); QString val(it->toString().c_str()); // Also remove new values to avoid duplicates. They will be added again below. if ( key == QString(iptcTagName) && (oldvals.contains(val) || newvals.contains(val)) ) it = iptcData.erase(it); else ++it; }; // Add new values. Exiv2::IptcKey iptcTag(iptcTagName); for (QStringList::iterator it = newvals.begin(); it != newvals.end(); ++it) { QString key = *it; key.truncate(maxSize); Exiv2::Value::AutoPtr val = Exiv2::Value::create(Exiv2::string); val->read(key.toLatin1().constData()); iptcData.add(iptcTag, val.get()); } d->iptcMetadata() = iptcData; return true; } catch( Exiv2::Error& e ) { d->printExiv2ExceptionError(QString("Cannot set Iptc key '%1' into image using Exiv2 ") .arg(iptcTagName), e); } return false; }
bool KExiv2::setIptcKeywords(const QStringList& oldKeywords, const QStringList& newKeywords, bool setProgramName) const { if (!setProgramId(setProgramName)) return false; try { QStringList oldkeys = oldKeywords; QStringList newkeys = newKeywords; kDebug() << d->filePath.toAscii().constData() << " ==> Iptc Keywords: " << newkeys.join(",").toAscii().constData(); // Remove all old keywords. Exiv2::IptcData iptcData(d->iptcMetadata()); Exiv2::IptcData::iterator it = iptcData.begin(); while(it != iptcData.end()) { QString key = QString::fromLocal8Bit(it->key().c_str()); QString val(it->toString().c_str()); // Also remove new keywords to avoid duplicates. They will be added again below. if ( key == QString("Iptc.Application2.Keywords") && (oldKeywords.contains(val) || newKeywords.contains(val)) ) it = iptcData.erase(it); else ++it; }; // Add new keywords. Note that Keywords Iptc tag is limited to 64 char but can be redondant. Exiv2::IptcKey iptcTag("Iptc.Application2.Keywords"); for (QStringList::iterator it = newkeys.begin(); it != newkeys.end(); ++it) { QString key = *it; key.truncate(64); Exiv2::Value::AutoPtr val = Exiv2::Value::create(Exiv2::string); val->read(key.toLatin1().constData()); iptcData.add(iptcTag, val.get()); } d->iptcMetadata() = iptcData; return true; } catch( Exiv2::Error& e ) { d->printExiv2ExceptionError("Cannot set Iptc Keywords into image using Exiv2 ", e); } return false; }
bool KExiv2::setIptcSubCategories(const QStringList& oldSubCategories, const QStringList& newSubCategories, bool setProgramName) const { if (!setProgramId(setProgramName)) return false; try { QStringList oldkeys = oldSubCategories; QStringList newkeys = newSubCategories; // Remove all old Sub Categories. Exiv2::IptcData iptcData(d->iptcMetadata()); Exiv2::IptcData::iterator it = iptcData.begin(); while(it != iptcData.end()) { QString key = QString::fromLocal8Bit(it->key().c_str()); QString val(it->toString().c_str()); if (key == QString("Iptc.Application2.SuppCategory") && oldSubCategories.contains(val)) it = iptcData.erase(it); else ++it; }; // Add new Sub Categories. Note that SubCategories Iptc tag is limited to 32 // characters but can be redondant. Exiv2::IptcKey iptcTag("Iptc.Application2.SuppCategory"); for (QStringList::iterator it = newkeys.begin(); it != newkeys.end(); ++it) { QString key = *it; key.truncate(32); Exiv2::Value::AutoPtr val = Exiv2::Value::create(Exiv2::string); val->read(key.toLatin1().constData()); iptcData.add(iptcTag, val.get()); } d->iptcMetadata() = iptcData; return true; } catch( Exiv2::Error& e ) { d->printExiv2ExceptionError("Cannot set Iptc Sub Categories into image using Exiv2 ", e); } return false; }
gboolean gexiv2_metadata_has_iptc_tag(GExiv2Metadata *self, const gchar* tag) { g_return_val_if_fail(GEXIV2_IS_METADATA(self), FALSE); g_return_val_if_fail(tag != NULL, FALSE); g_return_val_if_fail(self->priv->image.get() != NULL, FALSE); Exiv2::IptcData &iptc_data = self->priv->image->iptcData(); for (Exiv2::IptcData::iterator it = iptc_data.begin(); it != iptc_data.end(); ++it) { if (it->count() > 0 && g_ascii_strcasecmp(tag, it->key().c_str()) == 0) return TRUE; } return FALSE; }
bool getIptcKeywords(Exiv2::IptcData &iptcData, bool isIptcUtf8, QStringList &keywords) { bool anyAdded = false; try { QString keywordsTagName = QString::fromLatin1(IPTC_KEYWORDS); for (Exiv2::IptcData::iterator it = iptcData.begin(); it != iptcData.end(); ++it) { QString key = QString::fromLocal8Bit(it->key().c_str()); if (key == keywordsTagName) { QString tag; if (isIptcUtf8) { tag = QString::fromUtf8(it->toString().c_str()); } else { tag = QString::fromLocal8Bit(it->toString().c_str()); } keywords.append(tag); anyAdded = true; } } if (keywords.length() == 1 && keywords[0].contains(',')) { LOG_DEBUG << "processing legacy saved keywords"; QString composite = keywords[0]; keywords.clear(); keywords += decomposeKeyword(composite); } } catch (Exiv2::Error &e) { LOG_WARNING << "Exiv2 error:" << e.what(); anyAdded = false; } catch (...) { LOG_WARNING << "Exception"; anyAdded = false; #ifdef QT_DEBUG throw; #endif } return anyAdded; }
gboolean gexiv2_metadata_clear_iptc_tag(GExiv2Metadata *self, const gchar* tag) { g_return_val_if_fail(GEXIV2_IS_METADATA(self), FALSE); g_return_val_if_fail(tag != NULL, FALSE); g_return_val_if_fail(self->priv->image.get() != NULL, FALSE); Exiv2::IptcData &iptc_data = self->priv->image->iptcData(); gboolean erased = FALSE; Exiv2::IptcData::iterator it = iptc_data.begin(); while (it != iptc_data.end()) { if (it->count() > 0 && g_ascii_strcasecmp(tag, it->key().c_str()) == 0) { it = iptc_data.erase(it); erased = TRUE; } else { it++; } } return erased; }
int main(int argc, char* const argv[]) try { if (argc != 2) { std::cout << "Usage: " << argv[0] << " file\n"; return 1; } Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(argv[1]); assert (image.get() != 0); image->readMetadata(); Exiv2::IptcData &iptcData = image->iptcData(); if (iptcData.empty()) { std::string error(argv[1]); error += ": No IPTC data found in the file"; throw Exiv2::Error(1, error); } Exiv2::IptcData::iterator end = iptcData.end(); for (Exiv2::IptcData::iterator md = iptcData.begin(); md != end; ++md) { std::cout << std::setw(44) << std::setfill(' ') << std::left << md->key() << " " << "0x" << std::setw(4) << std::setfill('0') << std::right << std::hex << md->tag() << " " << std::setw(9) << std::setfill(' ') << std::left << md->typeName() << " " << std::dec << std::setw(3) << std::setfill(' ') << std::right << md->count() << " " << std::dec << md->value() << std::endl; } return 0; } catch (Exiv2::AnyError& e) { std::cout << "Caught Exiv2 exception '" << e << "'\n"; return -1; }
KExiv2::MetaDataMap KExiv2::getIptcTagsDataList(const QStringList& iptcKeysFilter, bool invertSelection) const { if (d->iptcMetadata().empty()) return MetaDataMap(); try { Exiv2::IptcData iptcData = d->iptcMetadata(); iptcData.sortByKey(); QString ifDItemName; MetaDataMap metaDataMap; for (Exiv2::IptcData::iterator md = iptcData.begin(); md != iptcData.end(); ++md) { QString key = QString::fromLocal8Bit(md->key().c_str()); // Decode the tag value with a user friendly output. std::ostringstream os; os << *md; QString value = QString(os.str().c_str()); // To make a string just on one line. value.replace('\n', ' '); // Some Iptc key are redondancy. check if already one exist... MetaDataMap::iterator it = metaDataMap.find(key); // We apply a filter to get only the Iptc tags that we need. if (!invertSelection) { if (iptcKeysFilter.contains(key.section('.', 1, 1))) { if (it == metaDataMap.end()) metaDataMap.insert(key, value); else { QString v = *it; v.append(", "); v.append(value); metaDataMap.insert(key, v); } } } else { if (!iptcKeysFilter.contains(key.section('.', 1, 1))) { if (it == metaDataMap.end()) metaDataMap.insert(key, value); else { QString v = *it; v.append(", "); v.append(value); metaDataMap.insert(key, v); } } } } return metaDataMap; } catch (Exiv2::Error& e) { d->printExiv2ExceptionError("Cannot parse Iptc metadata using Exiv2 ", e); } return MetaDataMap(); }