Loading...
Searching...
No Matches
exifprint.cpp

Sample program to print Exif data from an image.

// SPDX-License-Identifier: GPL-2.0-or-later
#include <exiv2/exiv2.hpp>
#include <iostream>
#include <regex>
// copied from src/tiffvisitor_int.cpp
static const Exiv2::TagInfo* findTag(const Exiv2::TagInfo* pList, uint16_t tag) {
while (pList->tag_ != 0xffff && pList->tag_ != tag)
pList++;
return pList->tag_ != 0xffff ? pList : nullptr;
}
int main(int argc, char* const argv[]) {
try {
setlocale(LC_CTYPE, ".utf8");
Exiv2::XmpParser::initialize();
::atexit(Exiv2::XmpParser::terminate);
const char* prog = argv[0];
if (argc == 1) {
std::cout << "Usage: " << prog << " [ [--lint] path | --version | --version-test ]" << std::endl;
return EXIT_FAILURE;
}
int rc = EXIT_SUCCESS;
const char* file = argv[1];
bool bLint = strcmp(file, "--lint") == 0 && argc == 3;
if (bLint)
file = argv[2];
if (strcmp(file, "--version") == 0) {
std::vector<std::regex> keys;
Exiv2::dumpLibraryInfo(std::cout, keys);
return rc;
}
if (strcmp(file, "--version-test") == 0) {
// verifies/test macro EXIV2_TEST_VERSION
// described in include/exiv2/version.hpp
std::cout << "EXV_PACKAGE_VERSION " << EXV_PACKAGE_VERSION << std::endl
<< "Exiv2::version() " << Exiv2::version() << std::endl
<< "strlen(Exiv2::version()) " << ::strlen(Exiv2::version()) << std::endl
<< "Exiv2::versionNumber() " << Exiv2::versionNumber() << std::endl
<< "Exiv2::versionString() " << Exiv2::versionString() << std::endl
<< "Exiv2::versionNumberHexString() " << Exiv2::versionNumberHexString() << std::endl;
// Test the Exiv2 version available at runtime but compile the if-clause only if
// the compile-time version is at least 0.15. Earlier versions didn't have a
// testVersion() function:
#if EXIV2_TEST_VERSION(0, 15, 0)
if (Exiv2::testVersion(0, 13, 0)) {
std::cout << "Available Exiv2 version is equal to or greater than 0.13\n";
} else {
std::cout << "Installed Exiv2 version is less than 0.13\n";
}
#else
std::cout << "Compile-time Exiv2 version doesn't have Exiv2::testVersion()\n";
#endif
return rc;
}
auto image = Exiv2::ImageFactory::open(file);
image->readMetadata();
Exiv2::ExifData& exifData = image->exifData();
if (exifData.empty()) {
std::string error("No Exif data found in file");
throw Exiv2::Error(Exiv2::ErrorCode::kerErrorMessage, error);
}
std::set<std::string> shortLong;
shortLong.insert("Exif.Photo.PixelXDimension");
shortLong.insert("Exif.Photo.PixelYDimension");
shortLong.insert("Exif.Photo.ImageLength");
shortLong.insert("Exif.Photo.ImageWidth");
shortLong.insert("Exif.Photo.RowsPerStrip");
shortLong.insert("Exif.Photo.StripOffsets");
shortLong.insert("Exif.Photo.StripByteCounts");
auto end = exifData.end();
for (auto i = exifData.begin(); i != end; ++i) {
if (!bLint) {
const char* tn = i->typeName();
std::cout << std::setw(44) << std::setfill(' ') << std::left << i->key() << " "
<< "0x" << std::setw(4) << std::setfill('0') << std::right << std::hex << i->tag() << " "
<< std::setw(9) << std::setfill(' ') << std::left << (tn ? tn : "Unknown") << " " << std::dec
<< std::setw(3) << std::setfill(' ') << std::right << i->count() << " " << std::dec << i->toString()
<< "\n";
} else {
const Exiv2::TagInfo* tagInfo = findTag(Exiv2::ExifTags::tagList(i->groupName()), i->tag());
if (tagInfo) {
Exiv2::TypeId type = i->typeId();
if (type != tagInfo->typeId_ &&
(tagInfo->typeId_ != Exiv2::comment || type != Exiv2::undefined) // comment is stored as undefined
&& (shortLong.find(i->key()) == shortLong.end() ||
(type != Exiv2::unsignedShort && type != Exiv2::unsignedLong)) // can be short or long!
) {
std::cerr << i->key() << " type " << i->typeName() << " (" << type << ")"
<< " expected " << Exiv2::TypeInfo::typeName(tagInfo->typeId_) << " (" << tagInfo->typeId_ << ")"
<< std::endl;
rc = 2;
}
}
}
}
return rc;
} catch (Exiv2::Error& e) {
std::cout << "Caught Exiv2 exception '" << e.what() << "'\n";
return EXIT_FAILURE;
}
}
Exiv2::Error
Simple error class used for exceptions. An output operator is provided to print errors to a stream.
Definition error.hpp:236
Exiv2::Error::what
const char * what() const noexcept override
Return the error message as a C-string. The pointer returned by what() is valid only as long as the B...
Definition error.cpp:162
Exiv2::ExifData
A container for Exif data. This is a top-level class of the Exiv2 library. The container holds Exifda...
Definition exif.hpp:379
Exiv2::ExifData::empty
bool empty() const
Return true if there is no Exif metadata.
Definition exif.hpp:465
Exiv2::ExifData::begin
iterator begin()
Begin of the metadata.
Definition exif.hpp:435
Exiv2::ExifData::end
iterator end()
End of the metadata.
Definition exif.hpp:439
Exiv2::ExifTags::tagList
static const TagInfo * tagList(const std::string &groupName)
Return read-only list of built-in groupName tags.
Definition tags.cpp:110
Exiv2::ImageFactory::open
static Image::UniquePtr open(const std::string &path, bool useCurl=true)
Create an Image subclass of the appropriate type by reading the specified file. Image type is derived...
Definition image.cpp:820
Exiv2::TypeInfo::typeName
static const char * typeName(TypeId typeId)
Return the name of the type, 0 if unknown.
Definition types.cpp:75
Exiv2::XmpParser::initialize
static bool initialize(XmpParser::XmpLockFct xmpLockFct=nullptr, void *pLockData=nullptr)
Initialize the XMP Toolkit.
Definition xmp.cpp:568
Exiv2::XmpParser::terminate
static void terminate()
Terminate the XMP Toolkit and unregister custom namespaces.
Definition xmp.cpp:623
Exiv2::Internal::tagInfo
const TagInfo * tagInfo(uint16_t tag, IfdId ifdId)
Return the tag info for tag and ifdId.
Definition tags_int.cpp:2441
Exiv2::versionNumber
EXIV2API uint32_t versionNumber()
Return the version of Exiv2 available at runtime as a uint32_t.
Definition version.cpp:66
Exiv2::version
EXIV2API const char * version()
Return the version of Exiv2 as "C" string eg "0.27.0.2".
Definition version.cpp:82
Exiv2::dumpLibraryInfo
EXIV2API void dumpLibraryInfo(std::ostream &os, const std::vector< std::regex > &keys)
dumpLibraryInfo implements the exiv2 option –version –verbose used by exiv2 test suite to inspect lib...
Definition version.cpp:199
Exiv2::versionNumberHexString
EXIV2API std::string versionNumberHexString()
Return the version of Exiv2 as hex string of fixed length 6.
Definition version.cpp:76
Exiv2::TypeId
TypeId
Exiv2 value type identifiers.
Definition types.hpp:70
Exiv2::unsignedShort
@ unsignedShort
Exif SHORT type, 16-bit (2-byte) unsigned integer.
Definition types.hpp:73
Exiv2::unsignedLong
@ unsignedLong
Exif LONG type, 32-bit (4-byte) unsigned integer.
Definition types.hpp:74
Exiv2::comment
@ comment
Exiv2 type for the Exif user comment.
Definition types.hpp:90
Exiv2::undefined
@ undefined
Exif UNDEFINED type, an 8-bit byte that may contain anything.
Definition types.hpp:77
Exiv2::versionString
EXIV2API std::string versionString()
Return the version string Example: "0.25.0" (major.minor.patch).
Definition version.cpp:70
Exiv2::testVersion
EXIV2API bool testVersion(uint32_t major, uint32_t minor, uint32_t patch)
Test the version of the available Exiv2 library at runtime. Return true if it is the same as or newer...
Definition version.cpp:86
Exiv2::TagInfo
Tag information.
Definition tags.hpp:224
Exiv2::TagInfo::tag_
uint16_t tag_
Tag.
Definition tags.hpp:225
Exiv2::TagInfo::typeId_
TypeId typeId_
Type id.
Definition tags.hpp:231