00001
00002
00003
00004
00005 #include "image.hpp"
00006 #include "exif.hpp"
00007 #include <iostream>
00008 #include <iomanip>
00009 #include <cassert>
00010
00011 int main(int argc, char* const argv[])
00012 try {
00013 if (argc != 2) {
00014 std::cout << "Usage: " << argv[0] << " file\n";
00015 return 1;
00016 }
00017 std::string file(argv[1]);
00018
00019
00020
00021
00022 Exiv2::ExifData exifData;
00023
00024
00025
00026
00027
00028
00029
00030 exifData["Exif.Image.Model"] = "Test 1";
00031 exifData["Exif.Image.SamplesPerPixel"] = uint16_t(162);
00032 exifData["Exif.Image.XResolution"] = int32_t(-2);
00033 exifData["Exif.Image.YResolution"] = Exiv2::Rational(-2, 3);
00034 std::cout << "Added a few tags the quick way.\n";
00035
00036
00037 Exiv2::Value::AutoPtr v = Exiv2::Value::create(Exiv2::asciiString);
00038
00039 v->read("1999:12:31 23:59:59");
00040
00041 Exiv2::ExifKey key("Exif.Photo.DateTimeOriginal");
00042 exifData.add(key, v.get());
00043 std::cout << "Added key \"" << key << "\", value \"" << *v << "\"\n";
00044
00045
00046 Exiv2::URationalValue::AutoPtr rv(new Exiv2::URationalValue);
00047
00048 rv->read("1/2 1/3");
00049
00050 rv->value_.push_back(std::make_pair(2,3));
00051 rv->value_.push_back(std::make_pair(3,4));
00052
00053 key = Exiv2::ExifKey("Exif.Image.PrimaryChromaticities");
00054 exifData.add(key, rv.get());
00055 std::cout << "Added key \"" << key << "\", value \"" << *rv << "\"\n";
00056
00057
00058
00059
00060
00061
00062 Exiv2::Exifdatum& tag = exifData["Exif.Photo.DateTimeOriginal"];
00063 std::string date = tag.toString();
00064 date.replace(0, 4, "2000");
00065 tag.setValue(date);
00066 std::cout << "Modified key \"" << key
00067 << "\", new value \"" << tag.value() << "\"\n";
00068
00069
00070 key = Exiv2::ExifKey("Exif.Image.PrimaryChromaticities");
00071 Exiv2::ExifData::iterator pos = exifData.findKey(key);
00072 if (pos == exifData.end()) throw Exiv2::Error(1, "Key not found");
00073
00074 v = pos->getValue();
00075
00076 Exiv2::URationalValue* prv = dynamic_cast<Exiv2::URationalValue*>(v.release());
00077 if (prv == 0) throw Exiv2::Error(1, "Downcast failed");
00078 rv = Exiv2::URationalValue::AutoPtr(prv);
00079
00080 rv->value_[2] = std::make_pair(88,77);
00081
00082 pos->setValue(rv.get());
00083 std::cout << "Modified key \"" << key
00084 << "\", new value \"" << pos->value() << "\"\n";
00085
00086
00087
00088
00089
00090 key = Exiv2::ExifKey("Exif.Image.PrimaryChromaticities");
00091 pos = exifData.findKey(key);
00092 if (pos == exifData.end()) throw Exiv2::Error(1, "Key not found");
00093 exifData.erase(pos);
00094 std::cout << "Deleted key \"" << key << "\"\n";
00095
00096
00097
00098 Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(file);
00099 assert(image.get() != 0);
00100
00101 image->setExifData(exifData);
00102 image->writeMetadata();
00103
00104 return 0;
00105 }
00106 catch (Exiv2::AnyError& e) {
00107 std::cout << "Caught Exiv2 exception '" << e << "'\n";
00108 return -1;
00109 }