Blame samples/exifdata-test.cpp

Packit Service 21b5d1
// ***************************************************************** -*- C++ -*-
Packit Service 21b5d1
/*
Packit Service 21b5d1
  Abstract : ExifData assignment and copy construction unit tests
Packit Service 21b5d1
Packit Service 21b5d1
  File     : exifdata-test.cpp
Packit Service 21b5d1
  Author(s): Andreas Huggel (ahu) <ahuggel@gmx.net>
Packit Service 21b5d1
  History  : 20-Feb-05, ahu: created
Packit Service 21b5d1
Packit Service 21b5d1
 */
Packit Service 21b5d1
// *****************************************************************************
Packit Service 21b5d1
// included header files
Packit Service 21b5d1
#include <exiv2/exiv2.hpp>
Packit Service 21b5d1
Packit Service 21b5d1
#include <iostream>
Packit Service 21b5d1
#include <iomanip>
Packit Service 21b5d1
#include <string>
Packit Service 21b5d1
#include <cassert>
Packit Service 21b5d1
Packit Service 21b5d1
void write(const std::string& file, Exiv2::ExifData& ed);
Packit Service 21b5d1
void print(const std::string& file);
Packit Service 21b5d1
Packit Service 21b5d1
// *****************************************************************************
Packit Service 21b5d1
// Main
Packit Service 21b5d1
int main(int argc, char* const argv[])
Packit Service 21b5d1
{
Packit Service 21b5d1
try {
Packit Service 21b5d1
    if (argc != 2) {
Packit Service 21b5d1
        std::cout << "Usage: " << argv[0] << " file\n";
Packit Service 21b5d1
        return 1;
Packit Service 21b5d1
    }
Packit Service 21b5d1
    std::string file(argv[1]);
Packit Service 21b5d1
Packit Service 21b5d1
    Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(file);
Packit Service 21b5d1
    assert (image.get() != 0);
Packit Service 21b5d1
    image->readMetadata();
Packit Service 21b5d1
Packit Service 21b5d1
    Exiv2::ExifData &ed = image->exifData();
Packit Service 21b5d1
    if (ed.empty()) {
Packit Service 21b5d1
        std::string error = file + ": No Exif data found in the file";
Packit Service 21b5d1
        throw Exiv2::Error(Exiv2::kerErrorMessage, error);
Packit Service 21b5d1
    }
Packit Service 21b5d1
Packit Service 21b5d1
    std::cout << "Copy construction, non-intrusive changes\n";
Packit Service 21b5d1
    Exiv2::ExifData ed1(ed);
Packit Service 21b5d1
    ed1["Exif.Image.DateTime"] = "Sunday, 11am";
Packit Service 21b5d1
    ed1["Exif.Image.Orientation"] = uint16_t(2);
Packit Service 21b5d1
    ed1["Exif.Photo.DateTimeOriginal"] = "Sunday, 11am";
Packit Service 21b5d1
    ed1["Exif.Photo.MeteringMode"] = uint16_t(1);
Packit Service 21b5d1
    ed1["Exif.Iop.InteroperabilityIndex"] = "123";
Packit Service 21b5d1
//    ed1["Exif.Thumbnail.Orientation"] = uint16_t(2);
Packit Service 21b5d1
    write(file, ed1);
Packit Service 21b5d1
    print(file);
Packit Service 21b5d1
    std::cout << "----------------------------------------------\n";
Packit Service 21b5d1
Packit Service 21b5d1
    std::cout << "Copy construction, intrusive changes\n";
Packit Service 21b5d1
    Exiv2::ExifData ed2(ed);
Packit Service 21b5d1
    ed2["Exif.Image.DateTime"] = "Sunday, 11am and ten minutes";
Packit Service 21b5d1
    ed2["Exif.Image.Orientation"] = "2 3 4 5";
Packit Service 21b5d1
    ed2["Exif.Photo.DateTimeOriginal"] = "Sunday, 11am and ten minutes";
Packit Service 21b5d1
    ed2["Exif.Photo.MeteringMode"] = "1 2 3 4 5 6";
Packit Service 21b5d1
    ed2["Exif.Iop.InteroperabilityIndex"] = "1234";
Packit Service 21b5d1
    ed2["Exif.Thumbnail.Orientation"] = "2 3 4 5 6";
Packit Service 21b5d1
    write(file, ed2);
Packit Service 21b5d1
    print(file);
Packit Service 21b5d1
    std::cout << "----------------------------------------------\n";
Packit Service 21b5d1
Packit Service 21b5d1
    std::cout << "Assignment, non-intrusive changes\n";
Packit Service 21b5d1
    Exiv2::ExifData ed3;
Packit Service 21b5d1
    ed3["Exif.Iop.InteroperabilityVersion"] = "Test 6 Iop tag";
Packit Service 21b5d1
    ed3["Exif.Thumbnail.Artist"] = "Test 6 Ifd1 tag";
Packit Service 21b5d1
    ed3 = ed;
Packit Service 21b5d1
    ed3["Exif.Image.DateTime"] = "Sunday, 11am";
Packit Service 21b5d1
    ed3["Exif.Image.Orientation"] = uint16_t(2);
Packit Service 21b5d1
    ed3["Exif.Photo.DateTimeOriginal"] = "Sunday, 11am";
Packit Service 21b5d1
    ed3["Exif.Photo.MeteringMode"] = uint16_t(1);
Packit Service 21b5d1
    ed3["Exif.Iop.InteroperabilityIndex"] = "123";
Packit Service 21b5d1
    ed3["Exif.Thumbnail.Orientation"] = uint16_t(2);
Packit Service 21b5d1
    write(file, ed3);
Packit Service 21b5d1
    print(file);
Packit Service 21b5d1
    std::cout << "----------------------------------------------\n";
Packit Service 21b5d1
Packit Service 21b5d1
    std::cout << "Assignment, intrusive changes\n";
Packit Service 21b5d1
    Exiv2::ExifData ed4;
Packit Service 21b5d1
    ed4["Exif.Iop.InteroperabilityVersion"] = "Test 6 Iop tag";
Packit Service 21b5d1
    ed4["Exif.Thumbnail.Artist"] = "Test 6 Ifd1 tag";
Packit Service 21b5d1
    ed4 = ed;
Packit Service 21b5d1
    ed4["Exif.Image.DateTime"] = "Sunday, 11am and ten minutes";
Packit Service 21b5d1
    ed4["Exif.Image.Orientation"] = "2 3 4 5";
Packit Service 21b5d1
    ed4["Exif.Photo.DateTimeOriginal"] = "Sunday, 11am and ten minutes";
Packit Service 21b5d1
    ed4["Exif.Photo.MeteringMode"] = uint16_t(1);
Packit Service 21b5d1
    ed4["Exif.Iop.InteroperabilityIndex"] = "123";
Packit Service 21b5d1
    ed4["Exif.Thumbnail.Orientation"] = uint16_t(2);
Packit Service 21b5d1
    write(file, ed4);
Packit Service 21b5d1
    print(file);
Packit Service 21b5d1
Packit Service 21b5d1
    return 0;
Packit Service 21b5d1
}
Packit Service 21b5d1
catch (Exiv2::AnyError& e) {
Packit Service 21b5d1
    std::cout << "Caught Exiv2 exception '" << e << "'\n";
Packit Service 21b5d1
    return -1;
Packit Service 21b5d1
}
Packit Service 21b5d1
}
Packit Service 21b5d1
Packit Service 21b5d1
void write(const std::string& file, Exiv2::ExifData& ed)
Packit Service 21b5d1
{
Packit Service 21b5d1
    Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(file);
Packit Service 21b5d1
    assert (image.get() != 0);
Packit Service 21b5d1
    image->setExifData(ed);
Packit Service 21b5d1
    image->writeMetadata();
Packit Service 21b5d1
}
Packit Service 21b5d1
Packit Service 21b5d1
void print(const std::string& file)
Packit Service 21b5d1
{
Packit Service 21b5d1
    Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(file);
Packit Service 21b5d1
    assert (image.get() != 0);
Packit Service 21b5d1
    image->readMetadata();
Packit Service 21b5d1
Packit Service 21b5d1
    Exiv2::ExifData &ed = image->exifData();
Packit Service 21b5d1
    Exiv2::ExifData::const_iterator end = ed.end();
Packit Service 21b5d1
    for (Exiv2::ExifData::const_iterator i = ed.begin(); i != end; ++i) {
Packit Service 21b5d1
        std::cout << std::setw(45) << std::setfill(' ') << std::left
Packit Service 21b5d1
                  << i->key() << " "
Packit Service 21b5d1
                  << "0x" << std::setw(4) << std::setfill('0') << std::right
Packit Service 21b5d1
                  << std::hex << i->tag() << " "
Packit Service 21b5d1
                  << std::setw(12) << std::setfill(' ') << std::left
Packit Service 21b5d1
                  << i->ifdName() << " "
Packit Service 21b5d1
                  << std::setw(9) << std::setfill(' ') << std::left
Packit Service 21b5d1
                  << i->typeName() << " "
Packit Service 21b5d1
                  << std::dec << std::setw(3)
Packit Service 21b5d1
                  << std::setfill(' ') << std::right
Packit Service 21b5d1
                  << i->count() << " "
Packit Service 21b5d1
                  << std::dec << i->value()
Packit Service 21b5d1
                  << "\n";
Packit Service 21b5d1
    }
Packit Service 21b5d1
}