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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/* exif.hxx
 *
 * Copyright  2002,2003 Hans Meine <[email protected]>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA.
 */

#ifndef EXIF_HXX
#define EXIF_HXX

#include <libexif/exif-entry.h>
#include <libexif/exif-content.h>
#include <libexif/exif-ifd.h>
#include <libexif/exif-data.h>
#include <libexif/exif-format.h>
#include <libexif/exif-utils.h>
#include <stdexcept>
#include <string>

namespace Exif {

#ifndef EXIF_NO_EXCEPTIONS
struct InvalidIndex : std::runtime_error
{
    InvalidIndex(const std::string& s)
    : std::runtime_error(s) {}
};

struct InvalidFormat : std::runtime_error
{
    InvalidFormat(const std::string& s)
    : std::runtime_error(s) {}
};

struct IOError : std::runtime_error
{
    IOError(const std::string& s)
    : std::runtime_error(s) {}
};
#endif // EXIF_NO_EXCEPTIONS

struct Entry
{
    ExifEntry *entry_;

        // construct an empty entry, FIXME: is this needed in the public API?
    Entry()
    : entry_(exif_entry_new())
    {}

        // construct an entry for the given tag
    Entry(ExifTag tag)
    : entry_(exif_entry_new())
    {
        exif_entry_initialize(entry_, tag);
    }

        // copy constructor
    Entry(Entry const &other)
    : entry_(other.entry_)
    {
        exif_entry_ref(entry_);
    }

        // internal, do not use directly
    Entry(ExifEntry *entry)
    : entry_(entry)
    {
        exif_entry_ref(entry_);
    }

    ~Entry()
    {
        exif_entry_unref(entry_);
    }

    Entry &operator=(Entry const &other)
    {
        exif_entry_unref(entry_);
        entry_ = other.entry_;
        exif_entry_ref(entry_);
        return *this;
    }

    ExifTag tag() const
    {
        return entry_->tag;
    }

/*
    void setTag(ExifTag tag)
    {
        entry_->tag = tag;
    }
*/

    ExifFormat format() const
    {
        return entry_->format;
    }

/*
    void setFormat(ExifFormat format)
    {
        entry_->format = format;
    }
*/

    unsigned long components() const
    {
        return entry_->components;
    }

/*
    void setComponents(unsigned long components)
    {
        entry_->components = components;
    }

    void initialize(ExifTag tag)
    {
        exif_entry_initialize(entry_, tag);
    }
*/

    ExifByte getByte(unsigned int index) const
    {
#ifndef EXIF_NO_EXCEPTIONS
        if(entry_->format != EXIF_FORMAT_BYTE)
            throw InvalidFormat(
                "Exif::Entry::getByte(): Format is not EXIF_FORMAT_BYTE");
        if(index >= components())
            throw InvalidIndex(
                "Exif::getByte: component index out of range");
#endif
        return *(entry_->data
                 + index * exif_format_get_size(entry_->format));
    }

    const ExifAscii getAscii() const
    {
#ifndef EXIF_NO_EXCEPTIONS
        if(entry_->format != EXIF_FORMAT_ASCII)
            throw InvalidFormat(
                "Exif::Entry::getAscii(): Format is not EXIF_FORMAT_ASCII");
#endif
        return (ExifAscii)entry_->data;
    }

    ExifShort getShort(unsigned int index) const
    {
#ifndef EXIF_NO_EXCEPTIONS
        if(entry_->format != EXIF_FORMAT_SHORT)
            throw InvalidFormat(
                "Exif::Entry::getShort(): Format is not EXIF_FORMAT_SHORT");
        if(index >= components())
            throw InvalidIndex(
                "Exif::getShort: component index out of range");
#endif
        return exif_get_short(entry_->data
                              + index * exif_format_get_size(entry_->format),
                              exif_data_get_byte_order(entry_->parent->parent));
    }

    ExifLong getLong(unsigned int index) const
    {
#ifndef EXIF_NO_EXCEPTIONS
        if(entry_->format != EXIF_FORMAT_LONG)
            throw InvalidFormat(
                "Exif::Entry::getLong(): Format is not EXIF_FORMAT_LONG");
        if(index >= components())
            throw InvalidIndex(
                "Exif::getLong: component index out of range");
#endif
        return exif_get_long(entry_->data
                             + index * exif_format_get_size(entry_->format),
                             exif_data_get_byte_order(entry_->parent->parent));
    }

    ExifSLong getSLong(unsigned int index) const
    {
#ifndef EXIF_NO_EXCEPTIONS
        if(entry_->format != EXIF_FORMAT_SLONG)
            throw InvalidFormat(
                "Exif::Entry::getSLong(): Format is not EXIF_FORMAT_SLONG");
        if(index >= components())
            throw InvalidIndex(
                "Exif::getSLong: component index out of range");
#endif
        return exif_get_slong(entry_->data
                              + index * exif_format_get_size(entry_->format),
                              exif_data_get_byte_order(entry_->parent->parent));
    }

    ExifRational getRational(unsigned int index) const
    {
#ifndef EXIF_NO_EXCEPTIONS
        if(entry_->format != EXIF_FORMAT_RATIONAL)
            throw InvalidFormat(
                "Exif::Entry::getRational(): Format is not EXIF_FORMAT_RATIONAL");
        if(index >= components())
            throw InvalidIndex(
                "Exif::getRational: component index out of range");
#endif
        return exif_get_rational(entry_->data
                                 + index * exif_format_get_size(entry_->format),
                                 exif_data_get_byte_order(entry_->parent->parent));
    }

    ExifSRational getSRational(unsigned int index) const
    {
#ifndef EXIF_NO_EXCEPTIONS
        if(entry_->format != EXIF_FORMAT_SRATIONAL)
            throw InvalidFormat(
                "Exif::Entry::getSRational(): Format is not EXIF_FORMAT_SRATIONAL");
        if(index >= components())
            throw InvalidIndex(
                "Exif::getSRational: component index out of range");
#endif
        return exif_get_srational(entry_->data
                                  + index * exif_format_get_size(entry_->format),
                                  exif_data_get_byte_order(entry_->parent->parent));
    }

    void setByte(unsigned int index, ExifByte value) const
    {
#ifndef EXIF_NO_EXCEPTIONS
        if(entry_->format != EXIF_FORMAT_BYTE)
            throw InvalidFormat(
                "Exif::Entry::setByte(): Format is not EXIF_FORMAT_BYTE");
        if(index >= components())
            throw InvalidIndex(
                "Exif::setByte: component index out of range");
#endif
        *(entry_->data
          + index * exif_format_get_size(entry_->format)) = value;
    }

/*
    const ExifAscii setAscii() const
    {
#ifndef EXIF_NO_EXCEPTIONS
        if(entry_->format != EXIF_FORMAT_ASCII)
            throw InvalidFormat(
                "Exif::Entry::setAscii(): Format is not EXIF_FORMAT_ASCII");
#endif
        return (ExifAscii)entry_->data;
    }
*/

    void setShort(unsigned int index, ExifShort value) const
    {
#ifndef EXIF_NO_EXCEPTIONS
        if(entry_->format != EXIF_FORMAT_SHORT)
            throw InvalidFormat(
                "Exif::Entry::setShort(): Format is not EXIF_FORMAT_SHORT");
        if(index >= components())
            throw InvalidIndex(
                "Exif::setShort: component index out of range");
#endif
        return exif_set_short(entry_->data
                              + index * exif_format_get_size(entry_->format),
                              exif_data_get_byte_order(entry_->parent->parent),
                              value);
    }

    void setLong(unsigned int index, ExifLong value) const
    {
#ifndef EXIF_NO_EXCEPTIONS
        if(entry_->format != EXIF_FORMAT_LONG)
            throw InvalidFormat(
                "Exif::Entry::setLong(): Format is not EXIF_FORMAT_LONG");
        if(index >= components())
            throw InvalidIndex(
                "Exif::setLong: component index out of range");
#endif
        return exif_set_long(entry_->data
                             + index * exif_format_get_size(entry_->format),
                             exif_data_get_byte_order(entry_->parent->parent),
                             value);
    }

    void setSLong(unsigned int index, ExifSLong value) const
    {
#ifndef EXIF_NO_EXCEPTIONS
        if(entry_->format != EXIF_FORMAT_SLONG)
            throw InvalidFormat(
                "Exif::Entry::setSLong(): Format is not EXIF_FORMAT_SLONG");
        if(index >= components())
            throw InvalidIndex(
                "Exif::setSLong: component index out of range");
#endif
        return exif_set_slong(entry_->data
                              + index * exif_format_get_size(entry_->format),
                              exif_data_get_byte_order(entry_->parent->parent),
                              value);
    }

    void setRational(unsigned int index, ExifRational value) const
    {
#ifndef EXIF_NO_EXCEPTIONS
        if(entry_->format != EXIF_FORMAT_RATIONAL)
            throw InvalidFormat(
                "Exif::Entry::setRational(): Format is not EXIF_FORMAT_RATIONAL");
        if(index >= components())
            throw InvalidIndex(
                "Exif::setRational: component index out of range");
#endif
        return exif_set_rational(entry_->data
                                 + index * exif_format_get_size(entry_->format),
                                 exif_data_get_byte_order(entry_->parent->parent),
                                 value);
    }

    void setSRational(unsigned int index, ExifSRational value) const
    {
#ifndef EXIF_NO_EXCEPTIONS
        if(entry_->format != EXIF_FORMAT_SRATIONAL)
            throw InvalidFormat(
                "Exif::Entry::setSRational(): Format is not EXIF_FORMAT_SRATIONAL");
        if(index >= components())
            throw InvalidIndex(
                "Exif::setSRational: component index out of range");
#endif
        return exif_set_srational(entry_->data
                                  + index * exif_format_get_size(entry_->format),
                                  exif_data_get_byte_order(entry_->parent->parent),
                                  value);
    }

    const char *value()
    {
        return exif_entry_get_value(entry_);
    }

    const char *briefValue()
    {
        return exif_entry_get_value_brief(entry_);
    }

    void dump(unsigned int indent = 0) const
    {
        exif_entry_dump(entry_, indent);
    }
};

struct Content
{
    ExifContent *content_;

    Content()
    : content_(exif_content_new())
    {}

    Content(Content const &other)
    : content_(other.content_)
    {
        exif_content_ref(content_);
    }

        // internal, do not use directly
    Content(ExifContent *content)
    : content_(content)
    {
        exif_content_ref(content_);
    }

    ~Content()
    {
        exif_content_unref(content_);
    }

    Content &operator=(Content const &other)
    {
        exif_content_unref(content_);
        content_ = other.content_;
        exif_content_ref(content_);
        return *this;
    }

    Entry operator[](ExifTag tag)
    {
        ExifEntry *result = exif_content_get_entry(content_, tag);
        if(result)
            return Entry(result);
#ifndef EXIF_NO_EXCEPTIONS
        throw InvalidIndex(
            "Exif::Content: IFD does not contain given tag");
#endif
        return Entry();
    }

    Entry operator[](unsigned int index)
    {
        if(index < size())
            return Entry(content_->entries[index]);
#ifndef EXIF_NO_EXCEPTIONS
        throw InvalidIndex(
            "Exif::Content: numeric entry index out of range");
#endif // EXIF_NO_EXCEPTIONS
        return Entry();
    }

    unsigned int size() const
    {
        // FIXME: content_ should never be NULL, so this is unneeded!?
        return content_ ? content_->count : 0;
    }

    void add(Entry &entry)
    {
        exif_content_add_entry(content_, entry.entry_);
    }

    void remove(Entry &entry)
    {
        exif_content_remove_entry(content_, entry.entry_);
    }

        // for your convenience
    const char *value(ExifTag tag)
    {
        return exif_content_get_value(content_, tag);
    }

        // for your convenience
    const char *briefValue(ExifTag tag)
    {
        return exif_content_get_value_brief(content_, tag);
    }

    void dump(unsigned int indent = 0) const
    {
        exif_content_dump(content_, indent);
    }
};

struct Data
{
    ExifData *data_;

    Data()
    : data_(exif_data_new())
    {}

    Data(const char *path, bool *success = 0)
    : data_(exif_data_new_from_file(path))
    {
        if(success)
            *success = data_;
#ifndef EXIF_NO_EXCEPTIONS
        else
            if(!data_)
                throw IOError("Exif::Data: Could not load file");
#endif // EXIF_NO_EXCEPTIONS
        if(!data_)
            exif_data_new();
    }

    Data(const unsigned char *data,
         unsigned int size)
    : data_(exif_data_new_from_data(data, size))
    {}

    Data(Data const &other)
    : data_(other.data_)
    {
        exif_data_ref(data_);
    }

    ~Data()
    {
        exif_data_unref(data_);
    }

    Data &operator=(Data const &other)
    {
        exif_data_unref(data_);
        data_ = other.data_;
        exif_data_ref(data_);
        return *this;
    }

    void save(unsigned char **d, unsigned int *size)
    {
        exif_data_save_data(data_, d, size);
    }

    unsigned int size() const
    {
        return EXIF_IFD_COUNT;
    }

    Content operator[](unsigned int index)
    {
        if(index < size())
            return Content(data_->ifd[index]);
#ifndef EXIF_NO_EXCEPTIONS
        throw InvalidIndex(
            "Exif::Data: IFD index out of range");
#endif // EXIF_NO_EXCEPTIONS
        return Content();
    }

    ExifByteOrder byteOrder() const
    {
        return exif_data_get_byte_order(data_);
    }

    void setByteOrder(ExifByteOrder bo) const
    {
        exif_data_set_byte_order(data_, bo);
    }

    void dump()
    {
        exif_data_dump(data_);
    }
};

} // namespace Exif

#endif // EXIF_HXX