~noskcaj/ubuntu/trusty/libextractor/merge

« back to all changes in this revision

Viewing changes to src/plugins/exiv2/makernote.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    makernote.hpp
23
 
  @brief   Contains the Exif %MakerNote interface, IFD %MakerNote and a
24
 
           MakerNote factory
25
 
  @version $Rev: 598 $
26
 
  @author  Andreas Huggel (ahu)
27
 
           <a href="mailto:ahuggel@gmx.net">ahuggel@gmx.net</a>
28
 
  @date    18-Feb-04, ahu: created
29
 
 */
30
 
#ifndef MAKERNOTE_HPP_
31
 
#define MAKERNOTE_HPP_
32
 
 
33
 
// *****************************************************************************
34
 
// included header files
35
 
#include "types.hpp"
36
 
#include "ifd.hpp"
37
 
 
38
 
// + standard includes
39
 
#include <string>
40
 
#include <iosfwd>
41
 
#include <utility>
42
 
#include <vector>
43
 
#include <map>
44
 
#include <memory>
45
 
#include <string.h>
46
 
 
47
 
// *****************************************************************************
48
 
// namespace extensions
49
 
namespace Exiv2 {
50
 
 
51
 
// *****************************************************************************
52
 
// class declarations
53
 
    class Value;
54
 
 
55
 
// *****************************************************************************
56
 
// class definitions
57
 
 
58
 
    /*!
59
 
      @brief Exif makernote interface
60
 
 
61
 
      %MakerNote is a low-level container for makernote entries. The ExifData
62
 
      container uses makernote entries just like the other Exif metadata. Thus,
63
 
      clients can access Exif and makernote tags and their values uniformly
64
 
      through the ExifData interface. The role of %MakerNote is very similar to
65
 
      that of class Ifd (but makernotes do not need to be in IFD format, see
66
 
      below). In addition, it provides %MakerNote specific tag descriptions and
67
 
      print functions to interpret the makernote values.
68
 
 
69
 
      MakerNote holds methods and functionality to
70
 
      - read the makernote from a character buffer
71
 
      - copy the makernote to a character buffer
72
 
      - maintain a list of makernote entries (similar to IFD entries)
73
 
      - interpret (print) the values of makernote tags
74
 
 
75
 
      Makernotes can be added to the system by subclassing %MakerNote and
76
 
      registering a create function for the new subclass together with the
77
 
      camera make and model (which may contain wildcards) in the
78
 
      MakerNoteFactory. Since the majority of makernotes are in IFD format,
79
 
      subclass IfdMakerNote is provided. It contains an IFD container and
80
 
      implements all interface methods related to the makernote entries. <BR>
81
 
 
82
 
      To implement a new IFD makernote, all that you need to do is
83
 
      - subclass %IfdMakerNote,
84
 
      - implement methods to read and check the header (if any) as well as
85
 
        clone and create functions,
86
 
      - add a list of tag descriptions and appropriate print functions and
87
 
      - register the camera make/model and create function in the makernote factory.
88
 
      .
89
 
      See existing makernote implementations for examples, e.g., CanonMakerNote
90
 
      or FujiMakerNote.
91
 
 
92
 
      Finally, the header file which defines the static variable
93
 
      \em register*MakerNote needs to be included from mn.hpp, to ensure that
94
 
      the makernote is automatically registered in the factory.
95
 
     */
96
 
    class MakerNote {
97
 
        //! @name Not implemented
98
 
        //@{
99
 
        //! Assignment not allowed (memory management mode alloc_ is const)
100
 
        MakerNote& operator=(const MakerNote& rhs);
101
 
        //@}
102
 
 
103
 
    public:
104
 
        //! Shortcut for a %MakerNote auto pointer.
105
 
        typedef std::auto_ptr<MakerNote> AutoPtr;
106
 
 
107
 
        //! @name Creators
108
 
        //@{
109
 
        /*!
110
 
          @brief Constructor. Allows to choose whether or not memory management
111
 
                 is required for the Entries.
112
 
         */
113
 
        explicit MakerNote(bool alloc =true);
114
 
        //! Virtual destructor.
115
 
        virtual ~MakerNote() {}
116
 
        //@}
117
 
 
118
 
        //! @name Manipulators
119
 
        //@{
120
 
        /*!
121
 
          @brief Read the makernote, including the makernote header, from
122
 
                 character buffer buf of length len at position offset (from the
123
 
                 start of the TIFF header) and encoded in byte order byteOrder.
124
 
                 Return 0 if successful.
125
 
         */
126
 
        virtual int read(const byte* buf,
127
 
                         long len,
128
 
                         ByteOrder byteOrder,
129
 
                         long offset) =0;
130
 
        /*!
131
 
          @brief Copy (write) the makerNote to the character buffer buf at
132
 
                 position offset (from the start of the TIFF header), encoded
133
 
                 in byte order byteOrder. Update internal offsets if necessary.
134
 
                 Return the number of bytes written.
135
 
         */
136
 
        virtual long copy(byte* buf, ByteOrder byteOrder, long offset) =0;
137
 
        /*!
138
 
          @brief Add the entry to the makernote. No duplicate-check is performed,
139
 
                 i.e., it is possible to add multiple entries with the same tag.
140
 
                 The memory allocation mode of the entry to be added must be the
141
 
                 same as that of the makernote and the IFD id of the entry must
142
 
                 be set to 'makerIfd'.
143
 
         */
144
 
        virtual void add(const Entry& entry) =0;
145
 
        //! The first makernote entry
146
 
        virtual Entries::iterator begin() =0;
147
 
        //! End of the makernote entries
148
 
        virtual Entries::iterator end() =0;
149
 
        /*!
150
 
          @brief Update the base pointer of the %MakerNote and all its entries
151
 
                 to \em pNewBase.
152
 
 
153
 
          Allows to re-locate the underlying data buffer to a new location
154
 
          \em pNewBase. This method only has an effect in non-alloc mode.
155
 
         */
156
 
        virtual void updateBase(byte* pNewBase) =0;
157
 
        //@}
158
 
 
159
 
        //! @name Accessors
160
 
        //@{
161
 
        //! Return the byte order (little or big endian).
162
 
        ByteOrder byteOrder() const { return byteOrder_; }
163
 
        //! Return the offset of the makernote from the start of the TIFF header
164
 
        long offset() const  { return offset_; }
165
 
        /*!
166
 
          @brief Return an auto-pointer to an newly created, empty instance of
167
 
                 the same type as this. The makernote entries are <B>not</B>
168
 
                 copied.  The caller owns the new object and the auto-pointer
169
 
                 ensures that it will be deleted.
170
 
 
171
 
          @param alloc Memory management model for the newly created object.
172
 
                 Indicates if memory required to store data should be allocated
173
 
                 and deallocated (true) or not (false). If false, only pointers
174
 
                 to the buffer provided to read() will be kept. See Ifd for more
175
 
                 background on this concept.
176
 
         */
177
 
        AutoPtr create(bool alloc =true) const;
178
 
        /*!
179
 
          @brief Return an auto-pointer to a clone of this object. The caller
180
 
                 owns the new object and the auto-pointer ensures that it will
181
 
                 be deleted.
182
 
 
183
 
          @note  In non-alloc mode the clone potentially contains pointers to
184
 
                 the same data buffer as the original.
185
 
                 Use updateBase(byte* pNewBase) to adjust them.
186
 
         */
187
 
        AutoPtr clone() const;
188
 
        //! The first makernote entry
189
 
        virtual Entries::const_iterator begin() const =0;
190
 
        //! End of the makernote entries
191
 
        virtual Entries::const_iterator end() const =0;
192
 
        //! Find an entry by idx, return a const iterator to the record
193
 
        virtual Entries::const_iterator findIdx(int idx) const =0;
194
 
        //! Return the size of the makernote in bytes
195
 
        virtual long size() const =0;
196
 
        //@}
197
 
 
198
 
    protected:
199
 
        // DATA
200
 
        /*!
201
 
          @brief Flag to control the memory management: <BR>
202
 
                 True:  requires memory allocation and deallocation, <BR>
203
 
                 False: no memory management needed.
204
 
         */
205
 
        const bool alloc_;
206
 
        /*!
207
 
          @brief Offset of the makernote from the start of the TIFF header
208
 
                 (for offset()).
209
 
         */
210
 
        long offset_;
211
 
        /*!
212
 
          @brief Alternative byte order to use, invalid if the byte order of the
213
 
                 Exif block can be used
214
 
         */
215
 
        ByteOrder byteOrder_;
216
 
 
217
 
    private:
218
 
        //! Internal virtual create function.
219
 
        virtual MakerNote* create_(bool alloc =true) const =0;
220
 
        //! Internal virtual copy constructor.
221
 
        virtual MakerNote* clone_() const =0;
222
 
 
223
 
    }; // class MakerNote
224
 
 
225
 
    //! Type for a pointer to a function creating a makernote
226
 
    typedef MakerNote::AutoPtr (*CreateFct)(bool, const byte*, long, ByteOrder, long);
227
 
 
228
 
    /*!
229
 
      @brief Interface for MakerNotes in IFD format. See MakerNote.
230
 
     */
231
 
    class IfdMakerNote : public MakerNote {
232
 
        //! @name Not implemented
233
 
        //@{
234
 
        //! Assignment not allowed (Ifd does not have an assignment operator)
235
 
        IfdMakerNote& operator=(const IfdMakerNote& rhs);
236
 
        //@}
237
 
 
238
 
    public:
239
 
        //! Shortcut for an %IfdMakerNote auto pointer.
240
 
        typedef std::auto_ptr<IfdMakerNote> AutoPtr;
241
 
 
242
 
        //! @name Creators
243
 
        //@{
244
 
        /*!
245
 
          @brief Constructor. Requires an %Ifd id and allows to choose whether
246
 
                 or not memory management is needed for the Entries and whether
247
 
                 the IFD has a next pointer.
248
 
        */
249
 
        explicit IfdMakerNote(IfdId ifdId, bool alloc =true, bool hasNext =true);
250
 
        //! Copy constructor
251
 
        IfdMakerNote(const IfdMakerNote& rhs);
252
 
        //! Virtual destructor
253
 
        virtual ~IfdMakerNote() {}
254
 
        //@}
255
 
 
256
 
        //! @name Manipulators
257
 
        //@{
258
 
        virtual int read(const byte* buf,
259
 
                         long len,
260
 
                         ByteOrder byteOrder,
261
 
                         long offset);
262
 
        /*!
263
 
          @brief Read the makernote header from the makernote databuffer.  This
264
 
                 method must set the offset adjustment (adjOffset_), if needed
265
 
                 (assuming that the required information is in the header).
266
 
                 Return 0 if successful.
267
 
          @note  The default implementation does nothing, assuming there is no
268
 
                 header
269
 
         */
270
 
        virtual int readHeader(const byte* buf,
271
 
                               long len,
272
 
                               ByteOrder byteOrder);
273
 
        virtual long copy(byte* buf, ByteOrder byteOrder, long offset);
274
 
        virtual void add(const Entry& entry) { ifd_.add(entry); }
275
 
        virtual Entries::iterator begin() { return ifd_.begin(); }
276
 
        virtual Entries::iterator end() { return ifd_.end(); }
277
 
        virtual void updateBase(byte* pNewBase);
278
 
        //@}
279
 
 
280
 
        //! @name Accessors
281
 
        //@{
282
 
        virtual Entries::const_iterator begin() const { return ifd_.begin(); }
283
 
        virtual Entries::const_iterator end() const { return ifd_.end(); }
284
 
        virtual Entries::const_iterator findIdx(int idx) const;
285
 
        virtual long size() const;
286
 
        AutoPtr create(bool alloc =true) const;
287
 
        AutoPtr clone() const;
288
 
        /*!
289
 
          @brief Check the makernote header. This will typically check if a
290
 
                 required prefix string is present in the header. Return 0 if
291
 
                 successful.
292
 
          @note  The default implementation does nothing, assuming there is no
293
 
                 header
294
 
         */
295
 
        virtual int checkHeader() const;
296
 
        /*!
297
 
          @brief Write the makernote header to a character buffer, return the
298
 
                 number of characters written.
299
 
          @note  The default implementation copies the header_ buffer.
300
 
         */
301
 
        virtual long copyHeader(byte* buf) const;
302
 
        /*!
303
 
          @brief Return the size of the makernote header in bytes.
304
 
          @note  The default implementation returns the size of the header_
305
 
                 buffer.
306
 
         */
307
 
        virtual long headerSize() const;
308
 
        //@}
309
 
 
310
 
    protected:
311
 
        // DATA
312
 
        /*!
313
 
          @brief True:  Adjustment of the IFD offsets is to be added to the
314
 
                        offset from the start of the TIFF header (i.e., the
315
 
                        start of the Exif data section),
316
 
                 False: Adjustment of the IFD offsets is a suitable absolute
317
 
                        value. Ignore the offset from the start of the TIFF
318
 
                        header.
319
 
         */
320
 
        bool absOffset_;
321
 
        /*!
322
 
          @brief Adjustment of the IFD offsets relative to the start of the
323
 
                 TIFF header or to the start of the makernote, depending on
324
 
                 the setting of absOffset_.
325
 
         */
326
 
        long adjOffset_;
327
 
        //! Data buffer for the makernote header
328
 
        DataBuf header_;
329
 
        //! The makernote IFD
330
 
        Ifd ifd_;
331
 
 
332
 
    private:
333
 
        virtual IfdMakerNote* create_(bool alloc =true) const =0;
334
 
        virtual IfdMakerNote* clone_() const =0;
335
 
 
336
 
    }; // class IfdMakerNote
337
 
 
338
 
    /*!
339
 
      @brief Factory for MakerNote objects.
340
 
 
341
 
      Maintains an associative list (tree) of camera makes/models and
342
 
      corresponding %MakerNote create functions. Creates an instance of the
343
 
      %MakerNote for one camera make/model. The factory is implemented as a
344
 
      static class.
345
 
    */
346
 
    class MakerNoteFactory {
347
 
    public:
348
 
        //! Destructor.
349
 
        static void cleanup();
350
 
        /*!
351
 
          @brief Register a %MakerNote create function for a camera make and
352
 
                 model.
353
 
 
354
 
          Registers a create function for a %MakerNote for a given make and
355
 
          model combination with the factory. Both the make and model strings
356
 
          may contain wildcards ('*', e.g., "Canon*").  If the make already
357
 
          exists in the registry, then a new branch for the model is added. If
358
 
          the model also already exists, then the new create function replaces
359
 
          the old one.
360
 
 
361
 
          @param make Camera manufacturer. (Typically the string from the Exif
362
 
                 make tag.)
363
 
          @param model Camera model. (Typically the string from the Exif
364
 
                 model tag.)
365
 
          @param createMakerNote Pointer to a function to create a new
366
 
                 %MakerNote of a particular type.
367
 
        */
368
 
        static void registerMakerNote(const std::string& make,
369
 
                                      const std::string& model,
370
 
                                      CreateFct createMakerNote);
371
 
 
372
 
        //! Register a %MakerNote prototype in the IFD id registry.
373
 
        static void registerMakerNote(IfdId ifdId, MakerNote::AutoPtr makerNote);
374
 
 
375
 
        /*!
376
 
          @brief Create the appropriate %MakerNote based on camera make and
377
 
                 model and possibly the contents of the makernote itself, return
378
 
                 an auto-pointer to the newly created MakerNote instance. Return
379
 
                 0 if no %MakerNote is defined for the camera model.
380
 
 
381
 
          The method searches the make-model tree for a make and model
382
 
          combination in the registry that matches the search key. The search is
383
 
          case insensitive (Todo: implement case-insensitive comparisons) and
384
 
          wildcards in the registry entries are supported. First the best
385
 
          matching make is searched, then the best matching model for this make
386
 
          is searched. If there is no matching make or no matching model within
387
 
          the models registered for the best matching make, then no makernote
388
 
          is created and the function returns 0. If a match is found, the
389
 
          function invokes the registered create function and returns an
390
 
          auto-pointer to the newly created MakerNote. The makernote pointed to
391
 
          is owned by the caller of the function and the auto-pointer ensures
392
 
          that it is deleted.  The best match is an exact match, then a match is
393
 
          rated according to the number of matching characters. The makernote
394
 
          buffer is passed on to the create function, which can based on its
395
 
          content, automatically determine the correct version or flavour of the
396
 
          makernote required. This is used, e.g., to determine which of the
397
 
          three Nikon makernotes to create.
398
 
 
399
 
          @param make Camera manufacturer. (Typically the string from the Exif
400
 
                 make tag.)
401
 
          @param model Camera model. (Typically the string from the Exif
402
 
                 model tag.)
403
 
          @param alloc Memory management model for the new MakerNote. Determines
404
 
                 if memory required to store data should be allocated and
405
 
                 deallocated (true) or not (false). If false, only pointers to
406
 
                 the buffer provided to read() will be kept. See Ifd for more
407
 
                 background on this concept.
408
 
          @param buf Pointer to the makernote character buffer.
409
 
          @param len Length of the makernote character buffer.
410
 
          @param byteOrder Byte order in which the Exif data (and possibly the
411
 
                 makernote) is encoded.
412
 
          @param offset Offset from the start of the TIFF header of the makernote
413
 
                 buffer.
414
 
 
415
 
          @return An auto-pointer that owns a %MakerNote for the camera model.
416
 
                 If the camera is not supported, the pointer is 0.
417
 
         */
418
 
        static MakerNote::AutoPtr create(const std::string& make,
419
 
                                         const std::string& model,
420
 
                                         bool alloc,
421
 
                                         const byte* buf,
422
 
                                         long len,
423
 
                                         ByteOrder byteOrder,
424
 
                                         long offset);
425
 
 
426
 
        //! Create a %MakerNote for an IFD id.
427
 
        static MakerNote::AutoPtr create(IfdId ifdId, bool alloc =true);
428
 
 
429
 
        /*!
430
 
          @brief Match a registry entry with a key (used for make and model).
431
 
 
432
 
          The matching algorithm is case insensitive and wildcards ('*') in the
433
 
          registry entry are supported. The best match is an exact match, then
434
 
          a match is rated according to the number of matching characters.
435
 
 
436
 
          @return A score value indicating how good the key and registry entry
437
 
                  match. 0 means no match, values greater than 0 indicate a
438
 
                  match, larger values are better matches:<BR>
439
 
                  0: key and registry entry do not match<BR>
440
 
                  1: a pure wildcard match, i.e., the registry entry is just
441
 
                     a wildcard.<BR>
442
 
                  Score values greater than 1 are computed by adding 1 to the
443
 
                  number of matching characters, except for an exact match,
444
 
                  which scores 2 plus the number of matching characters.
445
 
         */
446
 
        static int match(const std::string& regEntry, const std::string& key);
447
 
 
448
 
        /*!
449
 
          @brief Class Init is used to execute initialisation and termination
450
 
                 code exactly once, at the begin and end of the program.
451
 
 
452
 
          See Bjarne Stroustrup, 'The C++ Programming Language 3rd
453
 
          Edition', section 21.5.2 for details about this pattern.
454
 
        */
455
 
        class Init {
456
 
            static int count;           //!< Counts calls to constructor
457
 
        public:
458
 
            //! @name Creators
459
 
            //@{
460
 
            //! Perform one-time initialisations.
461
 
            Init();
462
 
            //! Perform one-time cleanup operations.
463
 
            ~Init();
464
 
            //@}
465
 
        };
466
 
 
467
 
    private:
468
 
        //! @name Creators
469
 
        //@{
470
 
        //! Prevent construction: not implemented.
471
 
        MakerNoteFactory() {}
472
 
        //! Prevent copy construction: not implemented.
473
 
        MakerNoteFactory(const MakerNoteFactory& rhs);
474
 
        //@}
475
 
 
476
 
        //! Creates the private static instance
477
 
        static void init();
478
 
 
479
 
        //! Type used to store model labels and %MakerNote create functions
480
 
        typedef std::vector<std::pair<std::string, CreateFct> > ModelRegistry;
481
 
        //! Type used to store a list of make labels and model registries
482
 
        typedef std::vector<std::pair<std::string, ModelRegistry*> > Registry;
483
 
        //! Type used to store a list of IFD ids and %MakerNote prototypes
484
 
        typedef std::map<IfdId, MakerNote*> IfdIdRegistry;
485
 
 
486
 
        // DATA
487
 
        //! List of makernote types and corresponding makernote create functions.
488
 
        static Registry* pRegistry_;
489
 
        //! List of makernote IFD ids and corresponding create functions.
490
 
        static IfdIdRegistry* pIfdIdRegistry_;
491
 
 
492
 
    }; // class MakerNoteFactory
493
 
 
494
 
}                                       // namespace Exiv2
495
 
 
496
 
namespace {
497
 
    /*!
498
 
      Each translation unit that includes makernote.hpp declares its own
499
 
      Init object. The destructor ensures that the factory is properly
500
 
      freed exactly once.
501
 
 
502
 
      See Bjarne Stroustrup, 'The C++ Programming Language 3rd
503
 
      Edition', section 21.5.2 for details about this pattern.
504
 
    */
505
 
    Exiv2::MakerNoteFactory::Init makerNoteFactoryInit;
506
 
}
507
 
 
508
 
#endif                                  // #ifndef MAKERNOTE_HPP_