~noskcaj/ubuntu/trusty/libextractor/merge

« back to all changes in this revision

Viewing changes to src/plugins/exiv2/jpgimage.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    jpgimage.hpp
23
 
  @brief   Class JpegImage to access JPEG images
24
 
  @version $Rev: 563 $
25
 
  @author  Andreas Huggel (ahu)
26
 
           <a href="mailto:ahuggel@gmx.net">ahuggel@gmx.net</a>
27
 
  @author  Brad Schick (brad)
28
 
           <a href="mailto:brad@robotbattle.com">brad@robotbattle.com</a>
29
 
  @date    15-Jan-05, brad: split out from image.cpp
30
 
 */
31
 
#ifndef JPGIMAGE_HPP_
32
 
#define JPGIMAGE_HPP_
33
 
 
34
 
// *****************************************************************************
35
 
// included header files
36
 
#include "types.hpp"
37
 
#include "image.hpp"
38
 
#include "basicio.hpp"
39
 
#include "exif.hpp"
40
 
#include "iptc.hpp"
41
 
 
42
 
// + standard includes
43
 
#include <string>
44
 
 
45
 
// *****************************************************************************
46
 
// namespace extensions
47
 
namespace Exiv2 {
48
 
 
49
 
 
50
 
// *****************************************************************************
51
 
// class definitions
52
 
 
53
 
    /*!
54
 
      @brief Abstract helper base class to access JPEG images.
55
 
     */
56
 
    class JpegBase : public Image {
57
 
    public:
58
 
        //! @name Creators
59
 
        //@{
60
 
        //! Virtual destructor.
61
 
        virtual ~JpegBase() {}
62
 
        //@}
63
 
        //! @name Manipulators
64
 
        //@{
65
 
        /*!
66
 
          @brief Read all metadata from the image. Before this method
67
 
              is called, the various metadata types (Iptc, Exif) will be empty.
68
 
 
69
 
          This method returns success even when no metadata is found in
70
 
          the image. Callers must therefore check the size of individual
71
 
          metadata types before accessing the data.
72
 
 
73
 
          @throw Error if opening or reading of the file fails or the image
74
 
              data is not valid (does not look like JPEG data).
75
 
         */
76
 
        void readMetadata();
77
 
        /*!
78
 
          @brief Write metadata back to the image.
79
 
 
80
 
          All existing metadata sections in the image are either created,
81
 
          replaced, or erased. If values for a given metadata type have been
82
 
          assigned, a section for that metadata type will either be created or
83
 
          replaced. If no values have been assigned to a given metadata type,
84
 
          any exists section for that metadata type will be removed from the
85
 
          image.
86
 
 
87
 
          @throw Error if the operation fails
88
 
         */
89
 
        void writeMetadata();
90
 
        /*!
91
 
          @brief Assign new exif data. The new exif data is not written
92
 
             to the image until the writeMetadata() method is called.
93
 
          @param exifData An ExifData instance holding exif data to be copied
94
 
         */
95
 
        void setExifData(const ExifData& exifData);
96
 
        void clearExifData();
97
 
        void setIptcData(const IptcData& iptcData);
98
 
        void clearIptcData();
99
 
        void setComment(const std::string& comment);
100
 
        void clearComment();
101
 
        void setMetadata(const Image& image);
102
 
        void clearMetadata();
103
 
        //@}
104
 
 
105
 
        //! @name Accessors
106
 
        //@{
107
 
        bool good() const;
108
 
        const ExifData& exifData() const { return exifData_; }
109
 
        ExifData& exifData() { return exifData_; }
110
 
        const IptcData& iptcData() const { return iptcData_; }
111
 
        IptcData& iptcData() { return iptcData_; }
112
 
        std::string comment() const { return comment_; }
113
 
        BasicIo& io() const { return *io_; }
114
 
        //@}
115
 
    protected:
116
 
        //! @name Creators
117
 
        //@{
118
 
        /*!
119
 
          @brief Constructor that can either open an existing image or create
120
 
              a new image from scratch. If a new image is to be created, any
121
 
              existing data is overwritten.
122
 
          @param io An auto-pointer that owns a BasicIo instance used for
123
 
              reading and writing image metadata. \b Important: The constructor
124
 
              takes ownership of the passed in BasicIo instance through the
125
 
              auto-pointer. Callers should not continue to use the BasicIo
126
 
              instance after it is passed to this method.  Use the Image::io()
127
 
              method to get a temporary reference.
128
 
          @param create Specifies if an existing image should be read (false)
129
 
              or if a new image should be created (true).
130
 
          @param initData Data to initialize newly created images. Only used
131
 
              when \em create is true. Should contain data for the smallest
132
 
              valid image of the calling subclass.
133
 
          @param dataSize Size of initData in bytes.
134
 
         */
135
 
        JpegBase(BasicIo::AutoPtr io, bool create,
136
 
                 const byte initData[], long dataSize);
137
 
        //@}
138
 
        //! @name Manipulators
139
 
        //@{
140
 
        /*!
141
 
          @brief Writes the image header (aka signature) to the BasicIo instance.
142
 
          @param oIo BasicIo instance that the header is written to.
143
 
          @return 0 if successful;<BR>
144
 
                 4 if the output file can not be written to;<BR>
145
 
         */
146
 
        virtual int writeHeader(BasicIo& oIo) const =0;
147
 
        //@}
148
 
        //! @name Accessors
149
 
        //@{
150
 
        /*!
151
 
          @brief Determine if the content of the BasicIo instance is of the
152
 
              type supported by this class.
153
 
 
154
 
          The advance flag determines if the read position in the stream is
155
 
          moved (see below). This applies only if the type matches and the
156
 
          function returns true. If the type does not match, the stream
157
 
          position is not changed. However, if reading from the stream fails,
158
 
          the stream position is undefined. Consult the stream state to obtain
159
 
          more information in this case.
160
 
 
161
 
          @param iIo BasicIo instance to read from.
162
 
          @param advance Flag indicating whether the position of the io
163
 
              should be advanced by the number of characters read to
164
 
              analyse the data (true) or left at its original
165
 
              position (false). This applies only if the type matches.
166
 
          @return  true  if the data matches the type of this class;<BR>
167
 
                   false if the data does not match;<BR>
168
 
         */
169
 
        virtual bool isThisType(BasicIo& iIo, bool advance) const =0;
170
 
        //@}
171
 
 
172
 
        // Constant Data
173
 
        static const byte sos_;                 //!< JPEG SOS marker
174
 
        static const byte eoi_;                 //!< JPEG EOI marker
175
 
        static const byte app0_;                //!< JPEG APP0 marker
176
 
        static const byte app1_;                //!< JPEG APP1 marker
177
 
        static const byte app13_;               //!< JPEG APP13 marker
178
 
        static const byte com_;                 //!< JPEG Comment marker
179
 
        static const char exifId_[];            //!< Exif identifier
180
 
        static const char jfifId_[];            //!< JFIF identifier
181
 
        static const char ps3Id_[];             //!< Photoshop marker
182
 
        static const char bimId_[];             //!< Photoshop marker
183
 
        static const uint16_t iptc_;              //!< Photoshop Iptc marker
184
 
 
185
 
    private:
186
 
        // DATA
187
 
        BasicIo::AutoPtr io_;                   //!< Image data io pointer
188
 
        ExifData exifData_;                     //!< Exif data container
189
 
        IptcData iptcData_;                     //!< Iptc data container
190
 
        std::string comment_;                   //!< JPEG comment
191
 
 
192
 
        // METHODS
193
 
        /*!
194
 
          @brief Advances associated io instance to one byte past the next
195
 
              Jpeg marker and returns the marker. This method should be called
196
 
              when the BasicIo instance is positioned one byte past the end of a
197
 
              Jpeg segment.
198
 
          @return the next Jpeg segment marker if successful;<BR>
199
 
                 -1 if a maker was not found before EOF;<BR>
200
 
         */
201
 
        int advanceToMarker() const;
202
 
        /*!
203
 
          @brief Locates Photoshop formated Iptc data in a memory buffer.
204
 
              Operates on raw data to simplify reuse.
205
 
          @param pPsData Pointer to buffer containing entire payload of
206
 
              Photoshop formated APP13 Jpeg segment.
207
 
          @param sizePsData Size in bytes of pPsData.
208
 
          @param record Output value that is set to the start of the Iptc
209
 
              data block within pPsData (may not be null).
210
 
          @param sizeHdr Output value that is set to the size of the header
211
 
              within the Iptc data block pointed to by record (may not
212
 
              be null).
213
 
          @param sizeIptc Output value that is set to the size of the actual
214
 
              Iptc data within the Iptc data block pointed to by record
215
 
              (may not be null).
216
 
          @return 0 if successful;<BR>
217
 
                  3 if no Iptc data was found in pPsData;<BR>
218
 
                 -2 if the pPsData buffer does not contain valid data.
219
 
         */
220
 
        int locateIptcData(const byte *pPsData,
221
 
                           long sizePsData,
222
 
                           const byte **record,
223
 
                           uint16_t *const sizeHdr,
224
 
                           uint16_t *const sizeIptc) const;
225
 
        /*!
226
 
          @brief Initialize the image with the provided data.
227
 
          @param initData Data to be written to the associated BasicIo
228
 
          @param dataSize Size in bytes of data to be written
229
 
          @return 0 if successful;<BR>
230
 
                  4 if the image can not be written to.
231
 
         */
232
 
        int initImage(const byte initData[], long dataSize);
233
 
        /*!
234
 
          @brief Provides the main implementation of writeMetadata() by
235
 
                writing all buffered metadata to the provided BasicIo.
236
 
          @param oIo BasicIo instance to write to (a temporary location).
237
 
 
238
 
          @return 4 if opening or writing to the associated BasicIo fails
239
 
         */
240
 
        void doWriteMetadata(BasicIo& oIo);
241
 
 
242
 
        // NOT Implemented
243
 
        //! Default constructor.
244
 
        JpegBase();
245
 
        //! Copy constructor
246
 
        JpegBase(const JpegBase& rhs);
247
 
        //! Assignment operator
248
 
        JpegBase& operator=(const JpegBase& rhs);
249
 
    }; // class JpegBase
250
 
 
251
 
    /*!
252
 
      @brief Class to access JPEG images
253
 
     */
254
 
    class JpegImage : public JpegBase {
255
 
        friend bool isJpegType(BasicIo& iIo, bool advance);
256
 
    public:
257
 
        //! @name Creators
258
 
        //@{
259
 
        /*!
260
 
          @brief Constructor that can either open an existing Jpeg image or create
261
 
              a new image from scratch. If a new image is to be created, any
262
 
              existing data is overwritten. Since the constructor can not return
263
 
              a result, callers should check the good() method after object
264
 
              construction to determine success or failure.
265
 
          @param io An auto-pointer that owns a BasicIo instance used for
266
 
              reading and writing image metadata. \b Important: The constructor
267
 
              takes ownership of the passed in BasicIo instance through the
268
 
              auto-pointer. Callers should not continue to use the BasicIo
269
 
              instance after it is passed to this method.  Use the Image::io()
270
 
              method to get a temporary reference.
271
 
          @param create Specifies if an existing image should be read (false)
272
 
              or if a new file should be created (true).
273
 
         */
274
 
        JpegImage(BasicIo::AutoPtr io, bool create);
275
 
        //! Destructor
276
 
        ~JpegImage() {}
277
 
        //@}
278
 
 
279
 
        //! @cond IGNORE
280
 
        // Public only so that we can create a static instance
281
 
        struct JpegRegister{
282
 
            JpegRegister();
283
 
        };
284
 
        //! @endcond
285
 
    protected:
286
 
        //! @name Accessors
287
 
        //@{
288
 
        /*!
289
 
          @brief Determine if the content of the BasicIo instance is a Jpeg image.
290
 
              See base class for more details.
291
 
          @param iIo BasicIo instance to read from.
292
 
          @param advance Flag indicating whether the position of the io
293
 
              should be advanced by the number of characters read to
294
 
              analyse the data (true) or left at its original
295
 
              position (false). This applies only if the type matches.
296
 
          @return  true  if the data matches a Jpeg image;<BR>
297
 
                   false if the data does not match;<BR>
298
 
         */
299
 
        bool isThisType(BasicIo& iIo, bool advance) const;
300
 
        //@}
301
 
        //! @name Manipulators
302
 
        //@{
303
 
        /*!
304
 
          @brief Writes a Jpeg header (aka signature) to the BasicIo instance.
305
 
          @param oIo BasicIo instance that the header is written to.
306
 
          @return 0 if successful;<BR>
307
 
                 2 if the input image is invalid or can not be read;<BR>
308
 
                 4 if the temporary image can not be written to;<BR>
309
 
                -3 other temporary errors;<BR>
310
 
         */
311
 
        int writeHeader(BasicIo& oIo) const;
312
 
        //@}
313
 
    private:
314
 
        // Constant data
315
 
        static const byte soi_;          // SOI marker
316
 
        static const byte blank_[];      // Minimal Jpeg image
317
 
 
318
 
        // NOT Implemented
319
 
        //! Default constructor
320
 
        JpegImage();
321
 
        //! Copy constructor
322
 
        JpegImage(const JpegImage& rhs);
323
 
        //! Assignment operator
324
 
        JpegImage& operator=(const JpegImage& rhs);
325
 
    }; // class JpegImage
326
 
 
327
 
    static JpegImage::JpegRegister jpegReg;
328
 
 
329
 
    //! Helper class to access %Exiv2 files
330
 
    class ExvImage : public JpegBase {
331
 
        friend bool isExvType(BasicIo& iIo, bool advance);
332
 
    public:
333
 
        //! @name Creators
334
 
        //@{
335
 
        /*!
336
 
          @brief Constructor that can either open an existing Exv image or create
337
 
              a new image from scratch. If a new image is to be created, any
338
 
              existing data is overwritten. Since the constructor can not return
339
 
              a result, callers should check the good() method after object
340
 
              construction to determine success or failure.
341
 
          @param io An auto-pointer that owns a BasicIo instance used for
342
 
              reading and writing image metadata. \b Important: The constructor
343
 
              takes ownership of the passed in BasicIo instance through the
344
 
              auto-pointer. Callers should not continue to use the BasicIo
345
 
              instance after it is passed to this method.  Use the Image::io()
346
 
              method to get a temporary reference.
347
 
          @param create Specifies if an existing image should be read (false)
348
 
                 or if a new file should be created (true).
349
 
         */
350
 
        ExvImage(BasicIo::AutoPtr io, bool create);
351
 
        //! Destructor
352
 
        ~ExvImage() {}
353
 
        //@}
354
 
 
355
 
        //! @cond IGNORE
356
 
        // Public only so that we can create a static instance
357
 
        struct ExvRegister{
358
 
            ExvRegister();
359
 
        };
360
 
        //! @endcond
361
 
    protected:
362
 
        //! @name Accessors
363
 
        //@{
364
 
        /*!
365
 
          @brief Determine if the content of the BasicIo instance is an Exv
366
 
              image. See base class for more details.
367
 
          @param iIo BasicIo instance to read from.
368
 
          @param advance Flag indicating whether the position of the io
369
 
              should be advanced by the number of characters read to
370
 
              analyse the data (true) or left at its original
371
 
              position (false). This applies only if the type matches.
372
 
          @return  true  if the data matches a Jpeg image;<BR>
373
 
                   false if the data does not match;<BR>
374
 
         */
375
 
        virtual bool isThisType(BasicIo& iIo, bool advance) const;
376
 
        //@}
377
 
        //! @name Manipulators
378
 
        //@{
379
 
        /*!
380
 
          @brief Writes an Exv header (aka signature) to the BasicIo instance.
381
 
          @param oIo BasicIo instance that the header is written to.
382
 
          @return 0 if successful;<BR>
383
 
                  4 if the output file can not be written to;<BR>
384
 
         */
385
 
        int writeHeader(BasicIo& oIo) const;
386
 
        //@}
387
 
    private:
388
 
        // Constant data
389
 
        static const char exiv2Id_[];    // Exv identifier
390
 
        static const byte blank_[];      // Minimal exiv file
391
 
 
392
 
        // NOT Implemented
393
 
        //! Default constructor
394
 
        ExvImage();
395
 
        //! Copy constructor
396
 
        ExvImage(const ExvImage& rhs);
397
 
        //! Assignment operator
398
 
        ExvImage& operator=(const ExvImage& rhs);
399
 
    }; // class ExvImage
400
 
 
401
 
    static ExvImage::ExvRegister exvReg;
402
 
}                                       // namespace Exiv2
403
 
 
404
 
 
405
 
#endif                                  // #ifndef JPGIMAGE_HPP_