#include "image.hpp"
#include "exif.hpp"
#include <iostream>
#include <iomanip>
#include <cassert>
int main(int argc, char* const argv[])
try {
if (argc != 2) {
std::cout << "Usage: " << argv[0] << " file\n";
return 1;
}
std::string file(argv[1]);
Exiv2::ExifData exifData;
exifData["Exif.Image.Model"] = "Test 1";
exifData["Exif.Image.SamplesPerPixel"] = uint16_t(162);
exifData["Exif.Image.XResolution"] = int32_t(-2);
exifData["Exif.Image.YResolution"] = Exiv2::Rational(-2, 3);
std::cout << "Added a few tags the quick way.\n";
Exiv2::Value::AutoPtr v = Exiv2::Value::create(Exiv2::asciiString);
v->read("1999:12:31 23:59:59");
Exiv2::ExifKey key("Exif.Photo.DateTimeOriginal");
exifData.add(key, v.get());
std::cout << "Added key \"" << key << "\", value \"" << *v << "\"\n";
Exiv2::URationalValue::AutoPtr rv(new Exiv2::URationalValue);
rv->read("1/2 1/3");
rv->value_.push_back(std::make_pair(2,3));
rv->value_.push_back(std::make_pair(3,4));
key = Exiv2::ExifKey("Exif.Image.PrimaryChromaticities");
exifData.add(key, rv.get());
std::cout << "Added key \"" << key << "\", value \"" << *rv << "\"\n";
Exiv2::Exifdatum& tag = exifData["Exif.Photo.DateTimeOriginal"];
std::string date = tag.toString();
date.replace(0, 4, "2000");
tag.setValue(date);
std::cout << "Modified key \"" << key
<< "\", new value \"" << tag.value() << "\"\n";
key = Exiv2::ExifKey("Exif.Image.PrimaryChromaticities");
Exiv2::ExifData::iterator pos = exifData.findKey(key);
if (pos == exifData.end()) throw Exiv2::Error(1, "Key not found");
v = pos->getValue();
Exiv2::URationalValue* prv = dynamic_cast<Exiv2::URationalValue*>(v.release());
if (prv == 0) throw Exiv2::Error(1, "Downcast failed");
rv = Exiv2::URationalValue::AutoPtr(prv);
rv->value_[2] = std::make_pair(88,77);
pos->setValue(rv.get());
std::cout << "Modified key \"" << key
<< "\", new value \"" << pos->value() << "\"\n";
key = Exiv2::ExifKey("Exif.Image.PrimaryChromaticities");
pos = exifData.findKey(key);
if (pos == exifData.end()) throw Exiv2::Error(1, "Key not found");
exifData.erase(pos);
std::cout << "Deleted key \"" << key << "\"\n";
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(file);
assert(image.get() != 0);
image->setExifData(exifData);
image->writeMetadata();
return 0;
}
catch (Exiv2::AnyError& e) {
std::cout << "Caught Exiv2 exception '" << e << "'\n";
return -1;
}