~noskcaj/ubuntu/trusty/libextractor/merge

« back to all changes in this revision

Viewing changes to src/plugins/exiv2/exif.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2009-11-17 20:27:32 UTC
  • mfrom: (1.10.4 upstream) (5.2.5 sid)
  • Revision ID: james.westby@ubuntu.com-20091117202732-ipm2h3gks5bdw2vx
Tags: 0.5.23+dfsg-3
* Building against libltdl7.
* Updating to standards version 3.8.3.
* Adding maintainer homepage field to control.
* Marking maintainer homepage field to be also included in binary
  packages and changelog.
* Adding README.source.
* Simplifying autotools handling in rules.
* Updating README.source.
* Moving maintainer homepage field from control to copyright.
* Dropping la files.
* Simplyfing debhelper install files.
* Bumping versioned build-depends on debhelper.
* Adding depends to dpkg install info.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// ***************************************************************** -*- C++ -*-
2
 
/*
3
 
 * Copyright (C) 2004, 2005 Andreas Huggel <ahuggel@gmx.net>
4
 
 *
5
 
 * This program is part of the Exiv2 distribution.
6
 
 *
7
 
 * This program is free software; you can redistribute it and/or
8
 
 * modify it under the terms of the GNU General Public License
9
 
 * as published by the Free Software Foundation; either version 2
10
 
 * of the License, or (at your option) any later version.
11
 
 *
12
 
 * This program is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 * GNU General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU General Public License
18
 
 * along with this program; if not, write to the Free Software
19
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
 
 */
21
 
/*!
22
 
  @file    exif.hpp
23
 
  @brief   Encoding and decoding of Exif data
24
 
  @version $Rev: 599 $
25
 
  @author  Andreas Huggel (ahu)
26
 
           <a href="mailto:ahuggel@gmx.net">ahuggel@gmx.net</a>
27
 
  @date    09-Jan-04, ahu: created
28
 
 */
29
 
#ifndef EXIF_HPP_
30
 
#define EXIF_HPP_
31
 
 
32
 
// *****************************************************************************
33
 
// included header files
34
 
#include "metadatum.hpp"
35
 
#include "types.hpp"
36
 
#include "error.hpp"
37
 
#include "value.hpp"
38
 
#include "ifd.hpp"
39
 
#include "tags.hpp"
40
 
 
41
 
// + standard includes
42
 
#include <string>
43
 
#include <vector>
44
 
#include <memory>
45
 
 
46
 
// *****************************************************************************
47
 
// namespace extensions
48
 
/*!
49
 
  @brief Provides classes and functions to encode and decode Exif and Iptc data.
50
 
         This namespace corresponds to the <b>libexiv2</b> library.
51
 
 
52
 
 */
53
 
namespace Exiv2 {
54
 
 
55
 
// *****************************************************************************
56
 
// class declarations
57
 
    class ExifData;
58
 
    class MakerNote;
59
 
    class TiffHeader;
60
 
 
61
 
// *****************************************************************************
62
 
// class definitions
63
 
 
64
 
    /*!
65
 
      @brief Information related to one Exif tag. An Exif metadatum consists of
66
 
             an ExifKey and a Value and provides methods to manipulate these.
67
 
     */
68
 
    class Exifdatum : public Metadatum {
69
 
        friend std::ostream& operator<<(std::ostream&, const Exifdatum&);
70
 
        template<typename T> friend Exifdatum& setValue(Exifdatum&, const T&);
71
 
    public:
72
 
        //! @name Creators
73
 
        //@{
74
 
        /*!
75
 
          @brief Constructor for new tags created by an application. The
76
 
                 %Exifdatum is created from a \em key / value pair. %Exifdatum copies
77
 
                 (clones) the \em key and value if one is provided. Alternatively,
78
 
                 a program can create an 'empty' %Exifdatum with only a key
79
 
                 and set the value using setValue().
80
 
 
81
 
          @param key %ExifKey.
82
 
          @param pValue Pointer to an %Exifdatum value.
83
 
          @throw Error if the key cannot be parsed and converted.
84
 
         */
85
 
        explicit Exifdatum(const ExifKey& key, const Value* pValue =0);
86
 
        //! Constructor to build an %Exifdatum from an IFD entry.
87
 
        Exifdatum(const Entry& e, ByteOrder byteOrder);
88
 
        //! Copy constructor
89
 
        Exifdatum(const Exifdatum& rhs);
90
 
        //! Destructor
91
 
        virtual ~Exifdatum();
92
 
        //@}
93
 
 
94
 
        //! @name Manipulators
95
 
        //@{
96
 
        //! Assignment operator
97
 
        Exifdatum& operator=(const Exifdatum& rhs);
98
 
        /*!
99
 
          @brief Assign \em value to the %Exifdatum. The type of the new Value
100
 
                 is set to UShortValue.
101
 
         */
102
 
        Exifdatum& operator=(const uint16_t& value);
103
 
        /*!
104
 
          @brief Assign \em value to the %Exifdatum. The type of the new Value
105
 
                 is set to ULongValue.
106
 
         */
107
 
        Exifdatum& operator=(const uint32_t& value);
108
 
        /*!
109
 
          @brief Assign \em value to the %Exifdatum. The type of the new Value
110
 
                 is set to URationalValue.
111
 
         */
112
 
        Exifdatum& operator=(const URational& value);
113
 
        /*!
114
 
          @brief Assign \em value to the %Exifdatum. The type of the new Value
115
 
                 is set to ShortValue.
116
 
         */
117
 
        Exifdatum& operator=(const int16_t& value);
118
 
        /*!
119
 
          @brief Assign \em value to the %Exifdatum. The type of the new Value
120
 
                 is set to LongValue.
121
 
         */
122
 
        Exifdatum& operator=(const int32_t& value);
123
 
        /*!
124
 
          @brief Assign \em value to the %Exifdatum. The type of the new Value
125
 
                 is set to RationalValue.
126
 
         */
127
 
        Exifdatum& operator=(const Rational& value);
128
 
        /*!
129
 
          @brief Assign \em value to the %Exifdatum.
130
 
                 Calls setValue(const std::string&).
131
 
         */
132
 
        Exifdatum& operator=(const std::string& value);
133
 
        /*!
134
 
          @brief Assign \em value to the %Exifdatum.
135
 
                 Calls setValue(const Value*).
136
 
         */
137
 
        Exifdatum& operator=(const Value& value);
138
 
        /*!
139
 
          @brief Set the value. This method copies (clones) the value pointed
140
 
                 to by \em pValue.
141
 
         */
142
 
        void setValue(const Value* pValue);
143
 
        /*!
144
 
          @brief Set the value to the string \em value.  Uses Value::read(const
145
 
                 std::string&).  If the %Exifdatum does not have a Value yet,
146
 
                 then a %Value of the correct type for this %Exifdatum is
147
 
                 created. An AsciiValue is created for unknown tags.
148
 
         */
149
 
        void setValue(const std::string& value);
150
 
        /*!
151
 
          @brief Set the value from an IFD entry.
152
 
         */
153
 
        void setValue(const Entry& e, ByteOrder byteOrder);
154
 
        /*!
155
 
          @brief Set the data area by copying (cloning) the buffer pointed to
156
 
                 by \em buf.
157
 
 
158
 
          Values may have a data area, which can contain additional
159
 
          information besides the actual value. This method is used to set such
160
 
          a data area.
161
 
 
162
 
          @param buf Pointer to the source data area
163
 
          @param len Size of the data area
164
 
          @return Return -1 if the %Exifdatum does not have a value yet or the
165
 
                  value has no data area, else 0.
166
 
         */
167
 
        int setDataArea(const byte* buf, long len)
168
 
            { return value_.get() == 0 ? -1 : value_->setDataArea(buf, len); }
169
 
        //@}
170
 
 
171
 
        //! @name Accessors
172
 
        //@{
173
 
        //! Return the key of the %Exifdatum.
174
 
        std::string key() const
175
 
            { return key_.get() == 0 ? "" : key_->key(); }
176
 
        //! Return the name of the group (the second part of the key)
177
 
        std::string groupName() const
178
 
            { return key_.get() == 0 ? "" : key_->groupName(); }
179
 
        //! Return the name of the tag (which is also the third part of the key)
180
 
        std::string tagName() const
181
 
            { return key_.get() == 0 ? "" : key_->tagName(); }
182
 
        //! Return the tag
183
 
        uint16_t tag() const
184
 
            { return key_.get() == 0 ? 0xffff : key_->tag(); }
185
 
        //! Return the IFD id
186
 
        IfdId ifdId() const
187
 
            { return key_.get() == 0 ? ifdIdNotSet : key_->ifdId(); }
188
 
        //! Return the name of the IFD
189
 
        const char* ifdName() const
190
 
            { return key_.get() == 0 ? "" : key_->ifdName(); }
191
 
        //! Return the related image item (deprecated)
192
 
        std::string ifdItem() const
193
 
            { return key_.get() == 0 ? "" : key_->ifdItem(); }
194
 
        //! Return the index (unique id of this key within the original IFD)
195
 
        int idx() const
196
 
            { return key_.get() == 0 ? 0 : key_->idx(); }
197
 
        /*!
198
 
          @brief Write value to a data buffer and return the number
199
 
                 of bytes written.
200
 
 
201
 
          The user must ensure that the buffer has enough memory. Otherwise
202
 
          the call results in undefined behaviour.
203
 
 
204
 
          @param buf Data buffer to write to.
205
 
          @param byteOrder Applicable byte order (little or big endian).
206
 
          @return Number of characters written.
207
 
        */
208
 
        long copy(byte* buf, ByteOrder byteOrder) const
209
 
            { return value_.get() == 0 ? 0 : value_->copy(buf, byteOrder); }
210
 
        //! Return the type id of the value
211
 
        TypeId typeId() const
212
 
            { return value_.get() == 0 ? invalidTypeId : value_->typeId(); }
213
 
        //! Return the name of the type
214
 
        const char* typeName() const
215
 
            { return TypeInfo::typeName(typeId()); }
216
 
        //! Return the size in bytes of one component of this type
217
 
        long typeSize() const
218
 
            { return TypeInfo::typeSize(typeId()); }
219
 
        //! Return the number of components in the value
220
 
        long count() const
221
 
            { return value_.get() == 0 ? 0 : value_->count(); }
222
 
        //! Return the size of the value in bytes
223
 
        long size() const
224
 
            { return value_.get() == 0 ? 0 : value_->size(); }
225
 
        //! Return the value as a string.
226
 
        std::string toString() const
227
 
            { return value_.get() == 0 ? "" : value_->toString(); }
228
 
        /*!
229
 
          @brief Return the <EM>n</EM>-th component of the value converted to
230
 
                 long. The return value is -1 if the value of the Exifdatum is
231
 
                 not set and the behaviour of the method is undefined if there
232
 
                 is no n-th component.
233
 
         */
234
 
        long toLong(long n =0) const
235
 
            { return value_.get() == 0 ? -1 : value_->toLong(n); }
236
 
        /*!
237
 
          @brief Return the <EM>n</EM>-th component of the value converted to
238
 
                 float.  The return value is -1 if the value of the Exifdatum is
239
 
                 not set and the behaviour of the method is undefined if there
240
 
                 is no n-th component.
241
 
         */
242
 
        float toFloat(long n =0) const
243
 
            { return value_.get() == 0 ? -1 : value_->toFloat(n); }
244
 
        /*!
245
 
          @brief Return the <EM>n</EM>-th component of the value converted to
246
 
                 Rational. The return value is -1/1 if the value of the
247
 
                 Exifdatum is not set and the behaviour of the method is
248
 
                 undefined if there is no n-th component.
249
 
         */
250
 
        Rational toRational(long n =0) const
251
 
            { return value_.get() == 0 ? Rational(-1, 1) : value_->toRational(n); }
252
 
        /*!
253
 
          @brief Return an auto-pointer to a copy (clone) of the value. The
254
 
                 caller owns this copy and the auto-pointer ensures that it will
255
 
                 be deleted.
256
 
 
257
 
          This method is provided for users who need full control over the
258
 
          value. A caller may, e.g., downcast the pointer to the appropriate
259
 
          subclass of Value to make use of the interface of the subclass to set
260
 
          or modify its contents.
261
 
 
262
 
          @return An auto-pointer to a copy (clone) of the value, 0 if the value
263
 
                  is not set.
264
 
         */
265
 
        Value::AutoPtr getValue() const
266
 
            { return value_.get() == 0 ? Value::AutoPtr(0) : value_->clone(); }
267
 
        /*!
268
 
          @brief Return a constant reference to the value.
269
 
 
270
 
          This method is provided mostly for convenient and versatile output of
271
 
          the value which can (to some extent) be formatted through standard
272
 
          stream manipulators.  Do not attempt to write to the value through
273
 
          this reference.
274
 
 
275
 
          <b>Example:</b> <br>
276
 
          @code
277
 
          ExifData::const_iterator i = exifData.findKey(key);
278
 
          if (i != exifData.end()) {
279
 
              std::cout << i->key() << " " << std::hex << i->value() << "\n";
280
 
          }
281
 
          @endcode
282
 
 
283
 
          @return A constant reference to the value.
284
 
          @throw Error if the value is not set.
285
 
         */
286
 
        const Value& value() const;
287
 
        //! Return the size of the data area.
288
 
        long sizeDataArea() const
289
 
            { return value_.get() == 0 ? 0 : value_->sizeDataArea(); }
290
 
        /*!
291
 
          @brief Return a copy of the data area of the value. The caller owns
292
 
                 this copy and %DataBuf ensures that it will be deleted.
293
 
 
294
 
          Values may have a data area, which can contain additional
295
 
          information besides the actual value. This method is used to access
296
 
          such a data area.
297
 
 
298
 
          @return A %DataBuf containing a copy of the data area or an empty
299
 
                  %DataBuf if the value does not have a data area assigned or the
300
 
                  value is not set.
301
 
         */
302
 
        DataBuf dataArea() const
303
 
            { return value_.get() == 0 ? DataBuf(0, 0) : value_->dataArea(); }
304
 
 
305
 
        //@}
306
 
 
307
 
    private:
308
 
        // DATA
309
 
        ExifKey::AutoPtr key_;                  //!< Key
310
 
        Value::AutoPtr   value_;                //!< Value
311
 
 
312
 
    }; // class Exifdatum
313
 
 
314
 
    /*!
315
 
      @brief Output operator for Exifdatum types, prints the interpreted
316
 
             tag value.
317
 
     */
318
 
    std::ostream& operator<<(std::ostream& os, const Exifdatum& md);
319
 
 
320
 
    /*!
321
 
      @brief Set the value of \em exifDatum to \em value. If the object already
322
 
             has a value, it is replaced. Otherwise a new ValueType\<T\> value
323
 
             is created and set to \em value.
324
 
 
325
 
      This is a helper function, called from Exifdatum members. It is meant to
326
 
      be used with T = (u)int16_t, (u)int32_t or (U)Rational. Do not use directly.
327
 
    */
328
 
    template<typename T>
329
 
    Exifdatum& setValue(Exifdatum& exifDatum, const T& value);
330
 
 
331
 
    /*!
332
 
      @brief Exif %Thumbnail image. This abstract base class provides the
333
 
             interface for the thumbnail image that is optionally embedded in
334
 
             the Exif data. This class is used internally by ExifData, it is
335
 
             probably not useful for a client as a standalone class.  Instead,
336
 
             use an instance of ExifData to access the Exif thumbnail image.
337
 
     */
338
 
    class Thumbnail {
339
 
    public:
340
 
        //! Shortcut for a %Thumbnail auto pointer.
341
 
        typedef std::auto_ptr<Thumbnail> AutoPtr;
342
 
 
343
 
        //! @name Creators
344
 
        //@{
345
 
        //! Virtual destructor
346
 
        virtual ~Thumbnail() {}
347
 
        //@}
348
 
 
349
 
        //! @name Accessors
350
 
        //@{
351
 
        /*!
352
 
          @brief Set the image data as data area of the appropriate Exif
353
 
                 metadatum. Read the thumbnail image data from data buffer
354
 
                 \em buf. Return 0 if successful.
355
 
 
356
 
          @param exifData Exif data corresponding to the data buffer.
357
 
          @param pIfd1 Corresponding raw IFD1.
358
 
          @param buf Data buffer containing the thumbnail data. The buffer must
359
 
                 start with the TIFF header.
360
 
          @param len Number of bytes in the data buffer.
361
 
          @return 0 if successful;<BR>
362
 
                  1 in case of inconsistent thumbnail Exif data; or<BR>
363
 
                  2 if the data area is outside of the data buffer
364
 
         */
365
 
        virtual int setDataArea(ExifData& exifData,
366
 
                                Ifd* pIfd1,
367
 
                                const byte* buf,
368
 
                                long len) const =0;
369
 
        /*!
370
 
          @brief Return the thumbnail image in a %DataBuf. The caller owns the
371
 
                 data buffer and %DataBuf ensures that it will be deleted.
372
 
         */
373
 
        virtual DataBuf copy(const ExifData& exifData) const =0;
374
 
        /*!
375
 
          @brief Return a short string for the format of the thumbnail
376
 
                 ("TIFF", "JPEG").
377
 
         */
378
 
        virtual const char* format() const =0;
379
 
        /*!
380
 
          @brief Return the file extension for the format of the thumbnail
381
 
                 (".tif", ".jpg").
382
 
         */
383
 
        virtual const char* extension() const =0;
384
 
        //@}
385
 
 
386
 
    protected:
387
 
        //! @name Manipulators
388
 
        //@{
389
 
        /*!
390
 
          @brief Assignment operator. Protected so that it can only be used
391
 
                 by subclasses but not directly.
392
 
         */
393
 
        Thumbnail& operator=(const Thumbnail& rhs);
394
 
        //@}
395
 
 
396
 
    }; // class Thumbnail
397
 
 
398
 
    //! Exif thumbnail image in TIFF format
399
 
    class TiffThumbnail : public Thumbnail {
400
 
    public:
401
 
        //! Shortcut for a %TiffThumbnail auto pointer.
402
 
        typedef std::auto_ptr<TiffThumbnail> AutoPtr;
403
 
 
404
 
        //! @name Manipulators
405
 
        //@{
406
 
        //! Assignment operator.
407
 
        TiffThumbnail& operator=(const TiffThumbnail& rhs) { return *this; }
408
 
        //@}
409
 
 
410
 
        //! @name Accessors
411
 
        //@{
412
 
        int setDataArea(ExifData& exifData,
413
 
                        Ifd* pIfd1,
414
 
                        const byte* buf,
415
 
                        long len) const;
416
 
        DataBuf copy(const ExifData& exifData) const;
417
 
        const char* format() const;
418
 
        const char* extension() const;
419
 
        //@}
420
 
 
421
 
    }; // class TiffThumbnail
422
 
 
423
 
    //! Exif thumbnail image in JPEG format
424
 
    class JpegThumbnail : public Thumbnail {
425
 
    public:
426
 
        //! Shortcut for a %JpegThumbnail auto pointer.
427
 
        typedef std::auto_ptr<JpegThumbnail> AutoPtr;
428
 
 
429
 
        //! @name Manipulators
430
 
        //@{
431
 
        //! Assignment operator.
432
 
        JpegThumbnail& operator=(const JpegThumbnail& rhs) { return *this; }
433
 
        //@}
434
 
 
435
 
        //! @name Accessors
436
 
        //@{
437
 
        int setDataArea(ExifData& exifData,
438
 
                        Ifd* pIfd1,
439
 
                        const byte* buf,
440
 
                        long len) const;
441
 
        DataBuf copy(const ExifData& exifData) const;
442
 
        const char* format() const;
443
 
        const char* extension() const;
444
 
        //@}
445
 
 
446
 
    }; // class JpegThumbnail
447
 
 
448
 
    //! Container type to hold all metadata
449
 
    typedef std::vector<Exifdatum> ExifMetadata;
450
 
 
451
 
    //! Unary predicate that matches a Exifdatum with a given ifd id and idx
452
 
    class FindMetadatumByIfdIdIdx {
453
 
    public:
454
 
        //! Constructor, initializes the object with the ifd id and idx to look for
455
 
        FindMetadatumByIfdIdIdx(IfdId ifdId, int idx)
456
 
            : ifdId_(ifdId), idx_(idx) {}
457
 
        /*!
458
 
          @brief Returns true if the ifd id and idx of the argument
459
 
                 \em exifdatum is equal to that of the object.
460
 
        */
461
 
        bool operator()(const Exifdatum& exifdatum) const
462
 
            { return ifdId_ == exifdatum.ifdId() && idx_ == exifdatum.idx(); }
463
 
 
464
 
    private:
465
 
        IfdId ifdId_;
466
 
        int idx_;
467
 
 
468
 
    }; // class FindMetadatumByIfdIdIdx
469
 
 
470
 
    /*!
471
 
      @brief A container for Exif data.  This is a top-level class of the %Exiv2
472
 
             library. The container holds Exifdatum objects.
473
 
 
474
 
      Provide high-level access to the Exif data of an image:
475
 
      - read Exif information from JPEG files
476
 
      - access metadata through keys and standard C++ iterators
477
 
      - add, modify and delete metadata
478
 
      - write Exif data to JPEG files
479
 
      - extract Exif metadata to files, insert from these files
480
 
      - extract and delete Exif thumbnail (JPEG and TIFF thumbnails)
481
 
    */
482
 
    class ExifData {
483
 
    public:
484
 
        //! ExifMetadata iterator type
485
 
        typedef ExifMetadata::iterator iterator;
486
 
        //! ExifMetadata const iterator type
487
 
        typedef ExifMetadata::const_iterator const_iterator;
488
 
 
489
 
        //! @name Creators
490
 
        //@{
491
 
        //! Default constructor
492
 
        ExifData();
493
 
        //! Copy constructor (Todo: copy image data also)
494
 
        ExifData(const ExifData& rhs);
495
 
        //! Destructor
496
 
        ~ExifData();
497
 
        //@}
498
 
 
499
 
        //! @name Manipulators
500
 
        //@{
501
 
        //! Assignment operator (Todo: assign image data also)
502
 
        ExifData& operator=(const ExifData& rhs);
503
 
        /*!
504
 
          @brief Load the Exif data from a byte buffer. The data buffer
505
 
                 must start with the TIFF header.
506
 
          @param buf Pointer to the data buffer to read from
507
 
          @param len Number of bytes in the data buffer
508
 
          @return 0 if successful.
509
 
         */
510
 
        int load(const byte* buf, long len);
511
 
        /*!
512
 
          @brief Write the Exif data to a data buffer, which is returned.  The
513
 
                 caller owns this copy and %DataBuf ensures that it will be
514
 
                 deleted.  The copied data starts with the TIFF header.
515
 
 
516
 
          Tries to update the original data buffer and write it back with
517
 
          minimal changes, in a 'non-intrusive' fashion, if possible. In this
518
 
          case, tag data that ExifData does not understand stand a good chance
519
 
          to remain valid. (In particular, if the Exif data contains a
520
 
          Makernote in IFD format, the offsets in its IFD will remain valid.)
521
 
          <BR>
522
 
          If 'non-intrusive' writing is not possible, the Exif data will be
523
 
          re-built from scratch, in which case the absolute position of the
524
 
          metadata entries within the data buffer may (and in most cases will)
525
 
          be different from their original position. Furthermore, in this case,
526
 
          the Exif data is updated with the metadata from the actual thumbnail
527
 
          image (overriding existing metadata).
528
 
 
529
 
          @return A %DataBuf containing the Exif data.
530
 
         */
531
 
        DataBuf copy();
532
 
        /*!
533
 
          @brief Returns a reference to the %Exifdatum that is associated with a
534
 
                 particular \em key. If %ExifData does not already contain such
535
 
                 an %Exifdatum, operator[] adds object \em Exifdatum(key).
536
 
 
537
 
          @note  Since operator[] might insert a new element, it can't be a const
538
 
                 member function.
539
 
         */
540
 
        Exifdatum& operator[](const std::string& key);
541
 
        /*!
542
 
          @brief Add all (IFD) entries in the range from iterator position begin
543
 
                 to iterator position end to the Exif metadata. No duplicate
544
 
                 checks are performed, i.e., it is possible to add multiple
545
 
                 metadata with the same key.
546
 
         */
547
 
        void add(Entries::const_iterator begin,
548
 
                 Entries::const_iterator end,
549
 
                 ByteOrder byteOrder);
550
 
        /*!
551
 
          @brief Add an Exifdatum from the supplied key and value pair.  This
552
 
                 method copies (clones) key and value. No duplicate checks are
553
 
                 performed, i.e., it is possible to add multiple metadata with
554
 
                 the same key.
555
 
         */
556
 
        void add(const ExifKey& key, const Value* pValue);
557
 
        /*!
558
 
          @brief Add a copy of the \em exifdatum to the Exif metadata.  No
559
 
                 duplicate checks are performed, i.e., it is possible to add
560
 
                 multiple metadata with the same key.
561
 
 
562
 
          @throw Error if the makernote cannot be created
563
 
         */
564
 
        void add(const Exifdatum& exifdatum);
565
 
        /*!
566
 
          @brief Delete the Exifdatum at iterator position \em pos, return the
567
 
                 position of the next exifdatum. Note that iterators into
568
 
                 the metadata, including \em pos, are potentially invalidated
569
 
                 by this call.
570
 
         */
571
 
        iterator erase(iterator pos);
572
 
        /*!
573
 
          @brief Delete all Exifdatum instances resulting in an empty container.
574
 
                 Note that this also removes thumbnails.
575
 
         */
576
 
        void clear() { eraseThumbnail(); exifMetadata_.clear(); }
577
 
        //! Sort metadata by key
578
 
        void sortByKey();
579
 
        //! Sort metadata by tag
580
 
        void sortByTag();
581
 
        //! Begin of the metadata
582
 
        iterator begin() { return exifMetadata_.begin(); }
583
 
        //! End of the metadata
584
 
        iterator end() { return exifMetadata_.end(); }
585
 
        /*!
586
 
          @brief Find a Exifdatum with the given \em key, return an iterator to
587
 
                 it.  If multiple metadata with the same key exist, it is
588
 
                 undefined which of the matching metadata is found.
589
 
         */
590
 
        iterator findKey(const ExifKey& key);
591
 
        /*!
592
 
          @brief Find the Exifdatum with the given \em ifdId and \em idx,
593
 
                 return an iterator to it.
594
 
 
595
 
          This method can be used to uniquely identify an exifdatum that was
596
 
          created from an IFD or from the makernote (with idx greater than
597
 
          0). Metadata created by an application (not read from an IFD or a
598
 
          makernote) all have their idx field set to 0, i.e., they cannot be
599
 
          uniquely identified with this method.  If multiple metadata with the
600
 
          same key exist, it is undefined which of the matching metadata is
601
 
          found.
602
 
         */
603
 
        iterator findIfdIdIdx(IfdId ifdId, int idx);
604
 
        /*!
605
 
          @brief Set the Exif thumbnail to the Jpeg image \em path. Set
606
 
                 XResolution, YResolution and ResolutionUnit to \em xres,
607
 
                 \em yres and \em unit, respectively.
608
 
 
609
 
          This results in the minimal thumbnail tags being set for a Jpeg
610
 
          thumbnail, as mandated by the Exif standard.
611
 
 
612
 
          @throw Error if reading the file fails.
613
 
 
614
 
          @note  No checks on the file format or size are performed.
615
 
          @note  Additional existing Exif thumbnail tags are not modified.
616
 
          @note  The Jpeg image inserted as thumbnail image should not
617
 
                 itself contain Exif data (or other metadata), as existing
618
 
                 applications may have problems with that. (The preview
619
 
                 application that comes with OS X for one.) - David Harvey.
620
 
         */
621
 
        void setJpegThumbnail(const std::string& path,
622
 
                              URational xres, URational yres, uint16_t unit);
623
 
        /*!
624
 
          @brief Set the Exif thumbnail to the Jpeg image pointed to by \em buf,
625
 
                 and size \em size. Set XResolution, YResolution and
626
 
                 ResolutionUnit to \em xres, \em yres and \em unit, respectively.
627
 
 
628
 
          This results in the minimal thumbnail tags being set for a Jpeg
629
 
          thumbnail, as mandated by the Exif standard.
630
 
 
631
 
          @throw Error if reading the file fails.
632
 
 
633
 
          @note  No checks on the image format or size are performed.
634
 
          @note  Additional existing Exif thumbnail tags are not modified.
635
 
          @note  The Jpeg image inserted as thumbnail image should not
636
 
                 itself contain Exif data (or other metadata), as existing
637
 
                 applications may have problems with that. (The preview
638
 
                 application that comes with OS X for one.) - David Harvey.
639
 
         */
640
 
        void setJpegThumbnail(const byte* buf, long size,
641
 
                              URational xres, URational yres, uint16_t unit);
642
 
        /*!
643
 
          @brief Set the Exif thumbnail to the Jpeg image \em path.
644
 
 
645
 
          This sets only the Compression, JPEGInterchangeFormat and
646
 
          JPEGInterchangeFormatLength tags, which is not all the thumbnail
647
 
          Exif information mandatory according to the Exif standard. (But it's
648
 
          enough to work with the thumbnail.)
649
 
 
650
 
          @throw Error if reading the file fails.
651
 
 
652
 
          @note  No checks on the file format or size are performed.
653
 
          @note  Additional existing Exif thumbnail tags are not modified.
654
 
         */
655
 
        void setJpegThumbnail(const std::string& path);
656
 
        /*!
657
 
          @brief Set the Exif thumbnail to the Jpeg image pointed to by \em buf,
658
 
                 and size \em size.
659
 
 
660
 
          This sets only the Compression, JPEGInterchangeFormat and
661
 
          JPEGInterchangeFormatLength tags, which is not all the thumbnail
662
 
          Exif information mandatory according to the Exif standard. (But it's
663
 
          enough to work with the thumbnail.)
664
 
 
665
 
          @note  No checks on the image format or size are performed.
666
 
          @note  Additional existing Exif thumbnail tags are not modified.
667
 
         */
668
 
        void setJpegThumbnail(const byte* buf, long size);
669
 
        /*!
670
 
          @brief Delete the thumbnail from the Exif data. Removes all
671
 
                 Exif.%Thumbnail.*, i.e., IFD1 metadata.
672
 
 
673
 
          @return The number of bytes of thumbnail data erased from the original
674
 
                  Exif data. Note that the original image size may differ from
675
 
                  the size of the image after deleting the thumbnail by more
676
 
                  than this number. This is the case if the Exif data contains
677
 
                  extra bytes (often at the end of the Exif block) or gaps and
678
 
                  the thumbnail is not located at the end of the Exif block so
679
 
                  that non-intrusive writing of a truncated Exif block is not
680
 
                  possible. Instead it is in this case necessary to write the
681
 
                  Exif data, without the thumbnail, from the metadata and all
682
 
                  extra bytes and gaps are lost, resulting in a smaller image.
683
 
         */
684
 
        long eraseThumbnail();
685
 
        //@}
686
 
 
687
 
        //! @name Accessors
688
 
        //@{
689
 
        //! Begin of the metadata
690
 
        const_iterator begin() const { return exifMetadata_.begin(); }
691
 
        //! End of the metadata
692
 
        const_iterator end() const { return exifMetadata_.end(); }
693
 
        /*!
694
 
          @brief Find an exifdatum with the given \em key, return a const
695
 
                 iterator to it.  If multiple metadata with the same key exist,
696
 
                 it is undefined which of the matching metadata is found.
697
 
         */
698
 
        const_iterator findKey(const ExifKey& key) const;
699
 
        /*!
700
 
          @brief Find the exifdatum with the given \em ifdId and \em idx,
701
 
                 return an iterator to it.
702
 
 
703
 
          This method can be used to uniquely identify a Exifdatum that was
704
 
          created from an IFD or from the makernote (with idx greater than
705
 
          0). Metadata created by an application (not read from an IFD or a
706
 
          makernote) all have their idx field set to 0, i.e., they cannot be
707
 
          uniquely identified with this method.  If multiple metadata with the
708
 
          same key exist, it is undefined which of the matching metadata is
709
 
          found.
710
 
         */
711
 
        const_iterator findIfdIdIdx(IfdId ifdId, int idx) const;
712
 
        //! Return true if there is no Exif metadata
713
 
        bool empty() const { return count() == 0; }
714
 
        //! Get the number of metadata entries
715
 
        long count() const { return static_cast<long>(exifMetadata_.size()); }
716
 
        /*!
717
 
          @brief Returns the byte order. Default is little endian.
718
 
         */
719
 
        ByteOrder byteOrder() const;
720
 
        /*!
721
 
          @brief Write the thumbnail image to a file. A filename extension
722
 
                 is appended to \em path according to the image type of the
723
 
                 thumbnail, so \em path should not include an extension.
724
 
                 This will overwrite an existing file of the same name.
725
 
 
726
 
          @param  path Path of the filename without image type extension
727
 
 
728
 
          @throw Error if writing to the file fails.
729
 
 
730
 
          @return 0 if successful;<BR>
731
 
                  8 if the Exif data does not contain a thumbnail.
732
 
         */
733
 
        int writeThumbnail(const std::string& path) const;
734
 
        /*!
735
 
          @brief Return the thumbnail image in a %DataBuf. The caller owns the
736
 
                 data buffer and %DataBuf ensures that it will be deleted.
737
 
         */
738
 
        DataBuf copyThumbnail() const;
739
 
        /*!
740
 
          @brief Return a short string describing the format of the Exif
741
 
                 thumbnail ("TIFF", "JPEG").
742
 
         */
743
 
        const char* thumbnailFormat() const;
744
 
        /*!
745
 
          @brief Return the file extension for the Exif thumbnail depending
746
 
                 on the format (".tif", ".jpg").
747
 
         */
748
 
        const char* thumbnailExtension() const;
749
 
        /*!
750
 
          @brief Return a thumbnail object of the correct type, corresponding to
751
 
                 the current Exif data. Caller owns this object and the auto
752
 
                 pointer ensures that it will be deleted.
753
 
         */
754
 
        Thumbnail::AutoPtr getThumbnail() const;
755
 
        //@}
756
 
 
757
 
    private:
758
 
        //! @name Manipulators
759
 
        //@{
760
 
        /*!
761
 
          @brief Read the thumbnail from the data buffer. Assigns the thumbnail
762
 
                 data area with the appropriate Exif tags. Return 0 if successful,
763
 
                 i.e., if there is a thumbnail.
764
 
         */
765
 
        int readThumbnail();
766
 
        /*!
767
 
          @brief Check if the metadata changed and update the internal IFDs and
768
 
                 the MakerNote if the changes are compatible with the existing
769
 
                 data (non-intrusive write support).
770
 
 
771
 
          @return True if only compatible changes were detected in the metadata
772
 
                  and the internal IFDs and MakerNote (and thus the data buffer)
773
 
                  were updated successfully. Return false, if non-intrusive
774
 
                  writing is not possible. The internal IFDs and the MakerNote
775
 
                  (and thus the data buffer) may or may not be modified in this
776
 
                  case.
777
 
         */
778
 
        bool updateEntries();
779
 
        /*!
780
 
          @brief Update the metadata for a range of entries. Called by
781
 
                 updateEntries() for each of the internal IFDs and the MakerNote
782
 
                 (if any).
783
 
         */
784
 
        bool updateRange(const Entries::iterator& begin,
785
 
                         const Entries::iterator& end,
786
 
                         ByteOrder byteOrder);
787
 
        /*!
788
 
          @brief Write the Exif data to a data buffer the hard way, return the
789
 
                 data buffer. The caller owns this data buffer and %DataBuf
790
 
                 ensures that it will be deleted.
791
 
 
792
 
          Rebuilds the Exif data from scratch, using the TIFF header, metadata
793
 
          container and thumbnail. In particular, the internal IFDs and the
794
 
          original data buffer are not used. Furthermore, this method updates
795
 
          the Exif data with the metadata from the actual thumbnail image
796
 
          (overriding existing metadata).
797
 
 
798
 
          @return A %DataBuf containing the Exif data.
799
 
         */
800
 
        DataBuf copyFromMetadata();
801
 
        //@}
802
 
 
803
 
        //! @name Accessors
804
 
        //@{
805
 
        /*!
806
 
          @brief Check if the metadata is compatible with the internal IFDs for
807
 
                 non-intrusive writing. Return true if compatible, false if not.
808
 
 
809
 
          @note This function does not detect deleted metadata as incompatible,
810
 
                although the deletion of metadata is not (yet) a supported
811
 
                non-intrusive write operation.
812
 
         */
813
 
        bool compatible() const;
814
 
        /*!
815
 
          @brief Find the IFD or makernote entry corresponding to ifd id and idx.
816
 
 
817
 
          @return A pair of which the first part determines if a match was found
818
 
                  and, if true, the second contains an iterator to the entry.
819
 
         */
820
 
        std::pair<bool, Entries::const_iterator>
821
 
        findEntry(IfdId ifdId, int idx) const;
822
 
        //! Return a pointer to the internal IFD identified by its IFD id
823
 
        const Ifd* getIfd(IfdId ifdId) const;
824
 
        /*!
825
 
          @brief Check if IFD1, the IFD1 data and thumbnail data are located at
826
 
                 the end of the Exif data. Return true, if they are or if there
827
 
                 is no thumbnail at all, else return false.
828
 
         */
829
 
        bool stdThumbPosition() const;
830
 
        //@}
831
 
 
832
 
        // DATA
833
 
        ExifMetadata exifMetadata_;
834
 
 
835
 
        // The pointers below are used only if Exif data is read from a
836
 
        // raw data buffer
837
 
        TiffHeader* pTiffHeader_;      //! Pointer to the TIFF header
838
 
        Ifd* pIfd0_;                   //! Pointer to Ifd0
839
 
        Ifd* pExifIfd_;                //! Pointer to ExifIfd
840
 
        Ifd* pIopIfd_;                 //! Pointer to IopIfd
841
 
        Ifd* pGpsIfd_;                 //! Pointer to GpsIfd
842
 
        Ifd* pIfd1_;                   //! Pointer to Ifd1
843
 
        MakerNote* pMakerNote_;        //! Pointer to the MakerNote, if any
844
 
 
845
 
        long size_;                    //!< Size of the Exif raw data in bytes
846
 
        byte* pData_;                  //!< Exif raw data buffer
847
 
 
848
 
        /*!
849
 
          Can be set to false to indicate that non-intrusive writing is not
850
 
          possible. If it is true (the default), then the compatibility checks
851
 
          will be performed to determine which writing method to use.
852
 
         */
853
 
        bool compatible_;
854
 
 
855
 
    }; // class ExifData
856
 
 
857
 
// *****************************************************************************
858
 
// template, inline and free functions
859
 
 
860
 
    template<typename T>
861
 
    Exifdatum& setValue(Exifdatum& exifDatum, const T& value)
862
 
    {
863
 
        std::auto_ptr<ValueType<T> > v
864
 
            = std::auto_ptr<ValueType<T> >(new ValueType<T>);
865
 
        v->value_.push_back(value);
866
 
        exifDatum.value_ = v;
867
 
        return exifDatum;
868
 
    }
869
 
    /*!
870
 
      @brief Add all metadata in the range from iterator position begin to
871
 
             iterator position end, which have an IFD id matching that of the
872
 
             IFD to the list of directory entries of ifd.  No duplicate checks
873
 
             are performed, i.e., it is possible to add multiple metadata with
874
 
             the same key to an IFD.
875
 
     */
876
 
    void addToIfd(Ifd& ifd,
877
 
                  ExifMetadata::const_iterator begin,
878
 
                  ExifMetadata::const_iterator end,
879
 
                  ByteOrder byteOrder);
880
 
    /*!
881
 
      @brief Add the Exifdatum to the IFD.  No duplicate checks are performed,
882
 
             i.e., it is possible to add multiple metadata with the same key to
883
 
             an IFD.
884
 
     */
885
 
    void addToIfd(Ifd& ifd, const Exifdatum& exifdatum, ByteOrder byteOrder);
886
 
    /*!
887
 
      @brief Add all metadata in the range from iterator position begin to
888
 
             iterator position end with IFD id 'makerIfd' to the list of
889
 
             makernote entries of the object pointed to be makerNote.  No
890
 
             duplicate checks are performed, i.e., it is possible to add
891
 
             multiple metadata with the same key to a makernote.
892
 
     */
893
 
    void addToMakerNote(MakerNote* makerNote,
894
 
                        ExifMetadata::const_iterator begin,
895
 
                        ExifMetadata::const_iterator end,
896
 
                        ByteOrder byteOrder);
897
 
    /*!
898
 
      @brief Add the Exifdatum to makerNote, encoded in byte order byteOrder.
899
 
             No duplicate checks are performed, i.e., it is possible to add
900
 
             multiple metadata with the same key to a makernote.
901
 
     */
902
 
    void addToMakerNote(MakerNote* makerNote,
903
 
                        const Exifdatum& exifdatum,
904
 
                        ByteOrder byteOrder);
905
 
 
906
 
}                                       // namespace Exiv2
907
 
 
908
 
#endif                                  // #ifndef EXIF_HPP_