~ubuntu-branches/debian/lenny/exiv2/lenny

« back to all changes in this revision

Viewing changes to samples/addmoddel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mark Purcell
  • Date: 2008-06-21 08:23:53 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20080621082353-b1n4w08trwfwbfl4
Tags: 0.17.1-1
* New upstream release
  - Library transition cleared on debian-release/ d-d-a
* Version 0.17 also fixes:
  - CVE-2008-2696: DoS via metadata in images (Closes: #486328)
  - crashes when fed with wrong file (Closes: #485670)
* Urgency medium for CVE fix
* debian/patches/gcc4.3.diff unecessary for gcc-4.3
* Add /usr/share/bug/exiv2/presubj message for reportbug(1)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ***************************************************************** -*- C++ -*-
 
2
// addmoddel.cpp, $Rev: 1271 $
 
3
// Sample program showing how to add, modify and delete Exif metadata.
 
4
 
 
5
#include <exiv2/image.hpp>
 
6
#include <exiv2/exif.hpp>
 
7
#include <iostream>
 
8
#include <iomanip>
 
9
#include <cassert>
 
10
 
 
11
int main(int argc, char* const argv[])
 
12
try {
 
13
    if (argc != 2) {
 
14
        std::cout << "Usage: " << argv[0] << " file\n";
 
15
        return 1;
 
16
    }
 
17
    std::string file(argv[1]);
 
18
 
 
19
    // Container for exif metadata. This is an example of creating
 
20
    // exif metadata from scratch. If you want to add, modify, delete
 
21
    // metadata that exists in an image, start with ImageFactory::open
 
22
    Exiv2::ExifData exifData;
 
23
 
 
24
    // *************************************************************************
 
25
    // Add to the Exif data
 
26
 
 
27
    // This is the quickest way to add (simple) Exif data. If a metadatum for
 
28
    // a given key already exists, its value is overwritten. Otherwise a new
 
29
    // tag is added.
 
30
    exifData["Exif.Image.Model"] = "Test 1";                     // AsciiValue
 
31
    exifData["Exif.Image.SamplesPerPixel"] = uint16_t(162);      // UShortValue
 
32
    exifData["Exif.Image.XResolution"] = int32_t(-2);            // LongValue
 
33
    exifData["Exif.Image.YResolution"] = Exiv2::Rational(-2, 3); // RationalValue
 
34
    std::cout << "Added a few tags the quick way.\n";
 
35
 
 
36
    // Create a ASCII string value (note the use of create)
 
37
    Exiv2::Value::AutoPtr v = Exiv2::Value::create(Exiv2::asciiString);
 
38
    // Set the value to a string
 
39
    v->read("1999:12:31 23:59:59");
 
40
    // Add the value together with its key to the Exif data container
 
41
    Exiv2::ExifKey key("Exif.Photo.DateTimeOriginal");
 
42
    exifData.add(key, v.get());
 
43
    std::cout << "Added key \"" << key << "\", value \"" << *v << "\"\n";
 
44
 
 
45
    // Now create a more interesting value (without using the create method)
 
46
    Exiv2::URationalValue::AutoPtr rv(new Exiv2::URationalValue);
 
47
    // Set two rational components from a string
 
48
    rv->read("1/2 1/3");
 
49
    // Add more elements through the extended interface of rational value
 
50
    rv->value_.push_back(std::make_pair(2,3));
 
51
    rv->value_.push_back(std::make_pair(3,4));
 
52
    // Add the key and value pair to the Exif data
 
53
    key = Exiv2::ExifKey("Exif.Image.PrimaryChromaticities");
 
54
    exifData.add(key, rv.get());
 
55
    std::cout << "Added key \"" << key << "\", value \"" << *rv << "\"\n";
 
56
 
 
57
    // *************************************************************************
 
58
    // Modify Exif data
 
59
 
 
60
    // Since we know that the metadatum exists (or we don't mind creating a new
 
61
    // tag if it doesn't), we can simply do this:
 
62
    Exiv2::Exifdatum& tag = exifData["Exif.Photo.DateTimeOriginal"];
 
63
    std::string date = tag.toString();
 
64
    date.replace(0, 4, "2000");
 
65
    tag.setValue(date);
 
66
    std::cout << "Modified key \"" << key
 
67
              << "\", new value \"" << tag.value() << "\"\n";
 
68
 
 
69
    // Alternatively, we can use findKey()
 
70
    key = Exiv2::ExifKey("Exif.Image.PrimaryChromaticities");
 
71
    Exiv2::ExifData::iterator pos = exifData.findKey(key);
 
72
    if (pos == exifData.end()) throw Exiv2::Error(1, "Key not found");
 
73
    // Get a pointer to a copy of the value
 
74
    v = pos->getValue();
 
75
    // Downcast the Value pointer to its actual type
 
76
    Exiv2::URationalValue* prv = dynamic_cast<Exiv2::URationalValue*>(v.release());
 
77
    if (prv == 0) throw Exiv2::Error(1, "Downcast failed");
 
78
    rv = Exiv2::URationalValue::AutoPtr(prv);
 
79
    // Modify the value directly through the interface of URationalValue
 
80
    rv->value_[2] = std::make_pair(88,77);
 
81
    // Copy the modified value back to the metadatum
 
82
    pos->setValue(rv.get());
 
83
    std::cout << "Modified key \"" << key
 
84
              << "\", new value \"" << pos->value() << "\"\n";
 
85
 
 
86
    // *************************************************************************
 
87
    // Delete metadata from the Exif data container
 
88
 
 
89
    // Delete the metadatum at iterator position pos
 
90
    key = Exiv2::ExifKey("Exif.Image.PrimaryChromaticities");
 
91
    pos = exifData.findKey(key);
 
92
    if (pos == exifData.end()) throw Exiv2::Error(1, "Key not found");
 
93
    exifData.erase(pos);
 
94
    std::cout << "Deleted key \"" << key << "\"\n";
 
95
 
 
96
    // *************************************************************************
 
97
    // Finally, write the remaining Exif data to the image file
 
98
    Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(file);
 
99
    assert(image.get() != 0);
 
100
 
 
101
    image->setExifData(exifData);
 
102
    image->writeMetadata();
 
103
 
 
104
    return 0;
 
105
}
 
106
catch (Exiv2::AnyError& e) {
 
107
    std::cout << "Caught Exiv2 exception '" << e << "'\n";
 
108
    return -1;
 
109
}