File: dataarea-test.cpp

package info (click to toggle)
exiv2 0.17.1-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 8,668 kB
  • ctags: 5,254
  • sloc: cpp: 43,979; sh: 8,353; ansic: 1,641; makefile: 484; awk: 92; python: 36; sed: 16
file content (177 lines) | stat: -rw-r--r-- 5,197 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// ***************************************************************** -*- C++ -*-
/*
  Abstract : Tests for dataArea related methods

  File     : dataarea-test.cpp
  Version  : $Rev$
  Author(s): Andreas Huggel (ahu) <ahuggel@gmx.net>
  History  : 12-Nov-04, ahu: created

 */
// *****************************************************************************
// included header files
#include <exiv2/image.hpp>
#include <exiv2/jpgimage.hpp>
#include <exiv2/exif.hpp>
#include <iostream>
#include <iomanip>
#include <string>
#include <cassert>

void write(const std::string& file, Exiv2::ExifData& ed);
void print(const std::string& file);
int read(const std::string& path);

using namespace Exiv2;

// *****************************************************************************
// Main
int main(int /*argc*/, char* const /*argv*/[])
{
try {
    byte da1[]
        = { 0xaa,0xbb,0xaa,0xbb,0xaa,0xbb,0xaa,0xbb,
            0xaa,0xbb,0xaa,0xbb,0xaa,0xbb,0xaa,0xbb,
            0xaa,0xbb,0xaa,0xbb,0xaa,0xbb,0xaa,0xbb,
            0xaa,0xbb,0xaa,0xbb,0xaa,0xbb,0xaa,0xbb
        };

    long len1 = 32;

    byte da2[]
        = { 0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,
            0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc
        };
    long len2 = 16;

    Value::AutoPtr v1 = Value::create(unsignedLong);
    v1->setDataArea(da1, len1);
    v1->read("0");

    Value::AutoPtr v2 = Value::create(undefined);
    v2->read("238 238 238 238 238 238 238 238");

    Value::AutoPtr v3 = Value::create(unsignedShort);
    v3->setDataArea(da2, len2);
    v3->read("0 16");

    ExifData ed;
    ed.add(ExifKey("Exif.Image.Copyright"), v1.get());
    ed.add(ExifKey("Exif.Image.Software"), v2.get());
    ed.add(ExifKey("Exif.Image.Artist"), v3.get());

    std::string file("dataarea.exv");
    std::cout << "Writing file " << file << "\n";
    write(file, ed);

    std::cout << "\nReading IFD from file\n";
    read(file);

    std::cout << "\nReading metadata from file\n";
    print(file);

    return 0;
}
catch (Exiv2::AnyError& e) {
    std::cout << "Caught Exiv2 exception '" << e << "'\n";
    return -1;
}
}

void write(const std::string& file, Exiv2::ExifData& ed)
{
    Image::AutoPtr image = ImageFactory::create(Exiv2::ImageType::exv, file);
    assert(image.get() != 0);

    image->setExifData(ed);
    image->writeMetadata();
}

void print(const std::string& file)
{
    Image::AutoPtr image = ImageFactory::open(file);
    assert(image.get() != 0);
    image->readMetadata();

    Exiv2::ExifData &ed = image->exifData();
    Exiv2::ExifData::const_iterator end = ed.end();
    for (Exiv2::ExifData::const_iterator i = ed.begin(); i != end; ++i) {
        std::cout << std::setw(35) << std::setfill(' ') << std::left
                  << i->key() << " "
                  << "0x" << std::setw(4) << std::setfill('0') << std::right
                  << std::hex << i->tag() << " "
                  << std::setw(12) << std::setfill(' ') << std::left
                  << i->ifdName() << " "
                  << std::setw(9) << std::setfill(' ') << std::left
                  << i->typeName() << " "
                  << std::dec << std::setw(3)
                  << std::setfill(' ') << std::right
                  << i->count() << " "
                  << std::dec << i->value()
                  << "\n";
    }
}

int read(const std::string& path)
{
    Image::AutoPtr image = ImageFactory::open(path);
    assert(image.get() != 0);
    image->readMetadata();

    if (!image->exifData().empty()) {
        DataBuf exifData = image->exifData().copy();
        long size = exifData.size_;

        // Read the TIFF header
        TiffHeader tiffHeader;
        int rc = tiffHeader.read(exifData.pData_);
        if (rc) return rc;

        // Read IFD0
        Ifd ifd0(ifd0Id);
        rc = ifd0.read(exifData.pData_,
                       size,
                       tiffHeader.offset(),
                       tiffHeader.byteOrder());
        if (rc) return rc;
        ifd0.print(std::cout);

        Ifd::const_iterator i = ifd0.findTag(0x8298);
        assert(i != ifd0.end());

        Value::AutoPtr v = Value::create(TypeId(i->type()));
        v->read(i->data(), i->count() * i->typeSize(), tiffHeader.byteOrder());
        v->setDataArea(exifData.pData_ + v->toLong(), 32);

        std::cout << "Value of tag 0x8298: " << std::hex;
        v->write(std::cout);
        std::cout << std::endl;

        DataBuf buf = v->dataArea();
        for (int i = 0; i< buf.size_; ++i) {
            std::cout << std::hex << (int)buf.pData_[i] << " ";
        }
        std::cout << std::endl;

        // --------

        i = ifd0.findTag(0x013b);
        assert(i != ifd0.end());

        v = Value::create(TypeId(i->type()));
        v->read(i->data(), i->count() * i->typeSize(), tiffHeader.byteOrder());
        v->setDataArea(exifData.pData_ + v->toLong(), 16);

        std::cout << "Value of tag 0x013b: ";
        v->write(std::cout);
        std::cout << std::endl;

        buf = v->dataArea();
        for (int i = 0; i< buf.size_; ++i) {
            std::cout << std::hex << (int)buf.pData_[i] << " ";
        }
        std::cout << std::endl;

    }
    return 0;
}