Exiv2
crwimage_int.hpp
Go to the documentation of this file.
1 // ***************************************************************** -*- C++ -*-
2 /*
3  * Copyright (C) 2004-2018 Exiv2 authors
4  * This program is part of the Exiv2 distribution.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
19  */
27 #ifndef CRWIMAGE_INT_HPP_
28 #define CRWIMAGE_INT_HPP_
29 
30 // *****************************************************************************
31 // included header files
32 #include "tags_int.hpp"
33 #include "image.hpp"
34 
35 // + standard includes
36 #include <iosfwd>
37 #include <string>
38 #include <vector>
39 #include <stack>
40 
41 // *****************************************************************************
42 // namespace extensions
43 namespace Exiv2 {
44 
45 // *****************************************************************************
46 // class declarations
47  class ExifData;
48 
49  namespace Internal {
50 
51 // *****************************************************************************
52 // class declarations
53  class CiffHeader;
54  class CiffComponent;
55  struct CrwMapping;
56  struct CrwSubDir;
57 
58 // *****************************************************************************
59 // type definitions
60 
62  typedef void (*CrwDecodeFct)(const CiffComponent&,
63  const CrwMapping*,
64  Image&,
65  ByteOrder);
66 
68  typedef void (*CrwEncodeFct)(const Image&,
69  const CrwMapping*,
70  CiffHeader*);
71 
73  typedef std::stack<CrwSubDir> CrwDirs;
74 
76  enum DataLocId {
77  invalidDataLocId,
78  valueData,
79  directoryData,
80  lastDataLocId
81  };
82 
83 // *****************************************************************************
84 // class definitions
85 
92  class CiffComponent {
93  public:
95  typedef std::auto_ptr<CiffComponent> AutoPtr;
97  typedef std::vector<CiffComponent*> Components;
98 
100 
101  CiffComponent()
103  : dir_(0), tag_(0), size_(0), offset_(0), pData_(0),
104  isAllocated_(false) {}
106  CiffComponent(uint16_t tag, uint16_t dir)
107  : dir_(dir), tag_(tag), size_(0), offset_(0), pData_(0),
108  isAllocated_(false) {}
110  virtual ~CiffComponent();
112 
114 
115  // Default assignment operator is fine
116 
118  void add(AutoPtr component);
132  CiffComponent* add(CrwDirs& crwDirs, uint16_t crwTagId);
142  void remove(CrwDirs& crwDirs, uint16_t crwTagId);
153  void read(const byte* pData,
154  uint32_t size,
155  uint32_t start,
156  ByteOrder byteOrder);
167  uint32_t write(Blob& blob, ByteOrder byteOrder, uint32_t offset);
178  uint32_t writeValueData(Blob& blob, uint32_t offset);
180  void setDir(uint16_t dir) { dir_ = dir; }
182  void setValue(DataBuf buf);
184 
186  static TypeId typeId(uint16_t tag);
188  static DataLocId dataLocation(uint16_t tag);
189 
191 
192 
199  void decode(Image& image, ByteOrder byteOrder) const;
207  void print(std::ostream& os,
208  ByteOrder byteOrder,
209  const std::string& prefix ="") const;
215  void writeDirEntry(Blob& blob, ByteOrder byteOrder) const;
217  uint16_t dir() const { return dir_; }
218 
220  uint16_t tag() const { return tag_; }
221 
223  bool empty() const;
224 
233  uint32_t size() const { return size_; }
234 
236  uint32_t offset() const { return offset_; }
237 
239  const byte* pData() const { return pData_; }
240 
242  uint16_t tagId() const { return tag_ & 0x3fff; }
243 
245  TypeId typeId() const { return typeId(tag_); }
246 
248  DataLocId dataLocation() const { return dataLocation(tag_); }
249 
254  CiffComponent* findComponent(uint16_t crwTagId, uint16_t crwDir) const;
256 
257  protected:
259 
260  virtual void doAdd(AutoPtr component) =0;
263  virtual CiffComponent* doAdd(CrwDirs& crwDirs, uint16_t crwTagId);
265  virtual void doRemove(CrwDirs& crwDirs, uint16_t crwTagId);
267  virtual void doRead(const byte* pData,
268  uint32_t size,
269  uint32_t start,
270  ByteOrder byteOrder);
272  virtual uint32_t doWrite(Blob& blob,
273  ByteOrder byteOrder,
274  uint32_t offset) =0;
276  void setSize(uint32_t size) { size_ = size; }
278  void setOffset(uint32_t offset) { offset_ = offset; }
280 
282 
283  virtual void doDecode(Image& image,
285  ByteOrder byteOrder) const =0;
287  virtual void doPrint(std::ostream& os,
288  ByteOrder byteOrder,
289  const std::string& prefix) const;
291  virtual bool doEmpty() const;
293  virtual CiffComponent* doFindComponent(uint16_t crwTagId,
294  uint16_t crwDir) const;
296 
297  private:
298  // DATA
299  uint16_t dir_;
300  uint16_t tag_;
301  uint32_t size_;
302  uint32_t offset_;
303  const byte* pData_;
304  bool isAllocated_;
305 
306  }; // class CiffComponent
307 
312  class CiffEntry : public CiffComponent {
313  public:
315 
316  CiffEntry() {}
319  CiffEntry(uint16_t tag, uint16_t dir) : CiffComponent(tag, dir) {}
320 
322  virtual ~CiffEntry();
324 
325  // Default assignment operator is fine
326 
327  private:
329 
330  using CiffComponent::doAdd;
331  // See base class comment
332  virtual void doAdd(AutoPtr component);
337  virtual uint32_t doWrite(Blob& blob,
338  ByteOrder byteOrder,
339  uint32_t offset);
341 
343 
344  // See base class comment
345  virtual void doDecode(Image& image, ByteOrder byteOrder) const;
347 
348  }; // class CiffEntry
349 
351  class CiffDirectory : public CiffComponent {
352  public:
354 
355  CiffDirectory() : cc_(NULL) {}
358  CiffDirectory(uint16_t tag, uint16_t dir) : CiffComponent(tag, dir), cc_(NULL) {}
359 
361  virtual ~CiffDirectory();
363 
365 
366  // Default assignment operator is fine
367 
375  void readDirectory(const byte* pData,
376  uint32_t size,
377  ByteOrder byteOrder);
379 
380  private:
382 
383  // See base class comment
384  virtual void doAdd(AutoPtr component);
385  // See base class comment
386  virtual CiffComponent* doAdd(CrwDirs& crwDirs, uint16_t crwTagId);
387  // See base class comment
388  virtual void doRemove(CrwDirs& crwDirs, uint16_t crwTagId);
393  virtual uint32_t doWrite(Blob& blob,
394  ByteOrder byteOrder,
395  uint32_t offset);
396  // See base class comment
397  virtual void doRead(const byte* pData,
398  uint32_t size,
399  uint32_t start,
400  ByteOrder byteOrder);
402 
404 
405  // See base class comment
406  virtual void doDecode(Image& image,
407  ByteOrder byteOrder) const;
408 
409  // See base class comment
410  virtual void doPrint(std::ostream& os,
411  ByteOrder byteOrder,
412  const std::string& prefix) const;
413 
415  virtual bool doEmpty() const;
416 
417  // See base class comment
418  virtual CiffComponent* doFindComponent(uint16_t crwTagId,
419  uint16_t crwDir) const;
421 
422  private:
423  // DATA
424  Components components_;
425  AutoPtr m_; // used by recursive doAdd
426  CiffComponent* cc_;
427 
428  }; // class CiffDirectory
429 
436  class CiffHeader {
437  public:
439  typedef std::auto_ptr<CiffHeader> AutoPtr;
440 
442 
443  CiffHeader()
445  : pRootDir_ (0),
446  byteOrder_ (littleEndian),
447  offset_ (0x0000001a),
448  pPadding_ (0),
449  padded_ (0)
450  {}
452  virtual ~CiffHeader();
454 
456 
457 
466  void read(const byte* pData, uint32_t size);
476  void add(uint16_t crwTagId, uint16_t crwDir, DataBuf buf);
485  void remove(uint16_t crwTagId, uint16_t crwDir);
487 
489  static const char* signature() { return signature_; }
490 
492 
493 
501  void write(Blob& blob) const;
510  void decode(Image& image) const;
517  void print(std::ostream& os, const std::string& prefix ="") const;
519  ByteOrder byteOrder() const { return byteOrder_; }
524  CiffComponent* findComponent(uint16_t crwTagId, uint16_t crwDir) const;
526 
527  private:
528  // DATA
529  static const char signature_[];
530 
531  CiffDirectory* pRootDir_;
532  ByteOrder byteOrder_;
533  uint32_t offset_;
534  byte* pPadding_;
535  uint32_t padded_;
536 
537  }; // class CiffHeader
538 
540  struct CrwSubDir {
541  uint16_t crwDir_;
542  uint16_t parent_;
543  }; // struct CrwSubDir
544 
549  struct CrwMapping {
551 
552  CrwMapping(
554  uint16_t crwTagId,
555  uint16_t crwDir,
556  uint32_t size,
557  uint16_t tag,
558  Internal::IfdId ifdId,
559  CrwDecodeFct toExif,
560  CrwEncodeFct fromExif)
561  : crwTagId_ (crwTagId),
562  crwDir_ (crwDir),
563  size_ (size),
564  tag_ (tag),
565  ifdId_ (ifdId),
566  toExif_ (toExif),
567  fromExif_ (fromExif)
568  {}
570 
571  // DATA
572  uint16_t crwTagId_;
573  uint16_t crwDir_;
574  uint32_t size_;
575  uint16_t tag_;
579 
580  }; // struct CrwMapping
581 
586  class CrwMap {
588 
589  CrwMap();
592 
593  public:
604  static void decode(const CiffComponent& ciffComponent,
605  Image& image,
606  ByteOrder byteOrder);
616  static void encode(CiffHeader* pHead, const Image& image);
617 
624  static void loadStack(CrwDirs& crwDirs, uint16_t crwDir);
625 
626  private:
628  static const CrwMapping* crwMapping(uint16_t crwDir, uint16_t crwTagId);
629 
639  static void decodeBasic(const CiffComponent& ciffComponent,
640  const CrwMapping* pCrwMapping,
641  Image& image,
642  ByteOrder byteOrder);
643 
645  static void decode0x0805(const CiffComponent& ciffComponent,
646  const CrwMapping* pCrwMapping,
647  Image& image,
648  ByteOrder byteOrder);
649 
651  static void decode0x080a(const CiffComponent& ciffComponent,
652  const CrwMapping* pCrwMapping,
653  Image& image,
654  ByteOrder byteOrder);
655 
657  static void decodeArray(const CiffComponent& ciffComponent,
658  const CrwMapping* pCrwMapping,
659  Image& image,
660  ByteOrder byteOrder);
661 
663  static void decode0x180e(const CiffComponent& ciffComponent,
664  const CrwMapping* pCrwMapping,
665  Image& image,
666  ByteOrder byteOrder);
667 
669  static void decode0x1810(const CiffComponent& ciffComponent,
670  const CrwMapping* pCrwMapping,
671  Image& image,
672  ByteOrder byteOrder);
673 
675  static void decode0x2008(const CiffComponent& ciffComponent,
676  const CrwMapping* pCrwMapping,
677  Image& image,
678  ByteOrder byteOrder);
679 
694  static void encodeBasic(const Image& image,
695  const CrwMapping* pCrwMapping,
696  CiffHeader* pHead);
697 
699  static void encode0x0805(const Image& image,
700  const CrwMapping* pCrwMapping,
701  CiffHeader* pHead);
702 
704  static void encode0x080a(const Image& image,
705  const CrwMapping* pCrwMapping,
706  CiffHeader* pHead);
707 
709  static void encodeArray(const Image& image,
710  const CrwMapping* pCrwMapping,
711  CiffHeader* pHead);
712 
714  static void encode0x180e(const Image& image,
715  const CrwMapping* pCrwMapping,
716  CiffHeader* pHead);
717 
719  static void encode0x1810(const Image& image,
720  const CrwMapping* pCrwMapping,
721  CiffHeader* pHead);
722 
724  static void encode0x2008(const Image& image,
725  const CrwMapping* pCrwMapping,
726  CiffHeader* pHead);
727  private:
728  // DATA
729  static const CrwMapping crwMapping_[];
730  static const CrwSubDir crwSubDir_[];
731 
732  }; // class CrwMap
733 
734 // *****************************************************************************
735 // template, inline and free functions
736 
742  DataBuf packIfdId(const ExifData& exifData,
743  IfdId ifdId,
744  ByteOrder byteOrder);
745 
746 }} // namespace Internal, Exiv2
747 
748 #endif // #ifndef CRWIMAGE_INT_HPP_
virtual void doRead(const byte *pData, uint32_t size, uint32_t start, ByteOrder byteOrder)
Implements read(). The default implementation reads a directory entry.
Definition: crwimage_int.cpp:232
uint16_t parent_
Parent directory tag.
Definition: crwimage_int.hpp:542
uint32_t write(Blob &blob, ByteOrder byteOrder, uint32_t offset)
Write the metadata from the raw metadata component to the binary image blob. This method may append t...
Definition: crwimage_int.cpp:369
void readDirectory(const byte *pData, uint32_t size, ByteOrder byteOrder)
Parse a CIFF directory from a memory buffer.
Definition: crwimage_int.cpp:277
void write(Blob &blob) const
Write the CRW image to the binary image blob, starting with the Ciff header. This method appends to t...
Definition: crwimage_int.cpp:334
CiffDirectory()
Default constructor.
Definition: crwimage_int.hpp:356
void writeDirEntry(Blob &blob, ByteOrder byteOrder) const
Write a directory entry for the component to the blob. If the size of the data is not larger than 8 b...
Definition: crwimage_int.cpp:449
A container for Exif data. This is a top-level class of the Exiv2 library. The container holds Exifda...
Definition: exif.hpp:434
Structure for the CIFF directory hierarchy.
Definition: crwimage_int.hpp:540
uint16_t crwTagId_
CRW tag id.
Definition: crwimage_int.hpp:572
ByteOrder
Type to express the byte order (little or big endian)
Definition: types.hpp:113
Utility class containing a character array. All it does is to take care of memory allocation and dele...
Definition: types.hpp:204
virtual void doPrint(std::ostream &os, ByteOrder byteOrder, const std::string &prefix) const
Implements print(). The default implementation prints the entry.
Definition: crwimage_int.cpp:505
DataBuf packIfdId(const ExifData &exifData, IfdId ifdId, ByteOrder byteOrder)
Pack the tag values of all ifdId tags in exifData into a data buffer. This function is used to pack C...
Definition: crwimage_int.cpp:1198
CiffDirectory(uint16_t tag, uint16_t dir)
Constructor taking a tag and directory.
Definition: crwimage_int.hpp:358
TypeId
Exiv2 value type identifiers.
Definition: types.hpp:130
void decode(Image &image) const
Decode the CRW image and add it to image.
Definition: crwimage_int.cpp:309
void print(std::ostream &os, const std::string &prefix="") const
Print debug info for the CRW image to os.
Definition: crwimage_int.cpp:488
This class models a CIFF directory of a CRW (Canon Raw data) image.
Definition: crwimage_int.hpp:351
CiffEntry()
Default constructor.
Definition: crwimage_int.hpp:317
CiffComponent()
Default constructor.
Definition: crwimage_int.hpp:102
virtual ~CiffDirectory()
Virtual destructor.
Definition: crwimage_int.cpp:172
uint32_t offset() const
Return the offset to the data from the start of the directory.
Definition: crwimage_int.hpp:236
uint8_t byte
1 byte unsigned integer type.
Definition: types.hpp:105
bool empty() const
Return true if the component is empty, else false.
Definition: crwimage_int.cpp:755
Abstract base class defining the interface for an image. This is the top-level interface to the Exiv2...
Definition: image.hpp:81
CrwDecodeFct toExif_
Conversion function.
Definition: crwimage_int.hpp:577
This class models the header of a CRW (Canon Raw data) image. It is the head of a CIFF parse tree...
Definition: crwimage_int.hpp:436
virtual void doDecode(Image &image, ByteOrder byteOrder) const =0
Implements decode()
void add(AutoPtr component)
Add a component to the composition.
Definition: crwimage_int.cpp:181
TypeId typeId() const
Return the type id of thi component.
Definition: crwimage_int.hpp:245
static const char * signature()
Return a pointer to the Canon CRW signature.
Definition: crwimage_int.hpp:489
virtual ~CiffComponent()
Virtual destructor.
Definition: crwimage_int.cpp:163
void setOffset(uint32_t offset)
Set the offset for this component.
Definition: crwimage_int.hpp:278
ByteOrder byteOrder() const
Return the byte order (little or big endian).
Definition: crwimage_int.hpp:519
std::stack< CrwSubDir > CrwDirs
Stack to hold a path of CRW directories.
Definition: crwimage_int.hpp:73
This class models one directory entry of a CIFF directory of a CRW (Canon Raw data) image...
Definition: crwimage_int.hpp:312
virtual ~CiffEntry()
Virtual destructor.
Definition: crwimage_int.cpp:168
void setValue(DataBuf buf)
Set the data value of the entry.
Definition: crwimage_int.cpp:540
uint16_t dir() const
Return the tag of the directory containing this component.
Definition: crwimage_int.hpp:217
std::auto_ptr< CiffComponent > AutoPtr
CiffComponent auto_ptr type.
Definition: crwimage_int.hpp:95
IfdId ifdId_
Exif Ifd id to map to.
Definition: crwimage_int.hpp:576
uint16_t tag() const
Return the tag of this component.
Definition: crwimage_int.hpp:220
CiffEntry(uint16_t tag, uint16_t dir)
Constructor taking a tag and directory.
Definition: crwimage_int.hpp:319
CrwEncodeFct fromExif_
Reverse conversion function.
Definition: crwimage_int.hpp:578
std::vector< byte > Blob
Container for binary data.
Definition: types.hpp:162
virtual ~CiffHeader()
Virtual destructor.
Definition: crwimage_int.cpp:157
void read(const byte *pData, uint32_t size, uint32_t start, ByteOrder byteOrder)
Read a component from a data buffer.
Definition: crwimage_int.cpp:224
static void loadStack(CrwDirs &crwDirs, uint16_t crwDir)
Load the stack: loop through the CRW subdirs hierarchy and push all directories on the path from crwD...
Definition: crwimage_int.cpp:983
virtual CiffComponent * doFindComponent(uint16_t crwTagId, uint16_t crwDir) const
Implements findComponent(). The default implementation checks the entry.
Definition: crwimage_int.cpp:599
CiffComponent(uint16_t tag, uint16_t dir)
Constructor taking a tag and directory.
Definition: crwimage_int.hpp:106
uint16_t tagId() const
Return the tag id of this component.
Definition: crwimage_int.hpp:242
Static class providing mapping functionality from CRW entries to image metadata and vice versa...
Definition: crwimage_int.hpp:586
CrwMapping(uint16_t crwTagId, uint16_t crwDir, uint32_t size, uint16_t tag, Internal::IfdId ifdId, CrwDecodeFct toExif, CrwEncodeFct fromExif)
Default constructor.
Definition: crwimage_int.hpp:553
void read(const byte *pData, uint32_t size)
Read the CRW image from a data buffer, starting with the Ciff header.
Definition: crwimage_int.cpp:196
virtual uint32_t doWrite(Blob &blob, ByteOrder byteOrder, uint32_t offset)=0
Implements write()
CiffComponent * findComponent(uint16_t crwTagId, uint16_t crwDir) const
Finds crwTagId in directory crwDir in the parse tree, returning a pointer to the component or 0 if no...
Definition: crwimage_int.cpp:586
IPTC string type.
Definition: types.hpp:147
Structure for a mapping table for conversion of CIFF entries to image metadata and vice versa...
Definition: crwimage_int.hpp:549
Internal Exif tag and type information.
CiffHeader()
Default constructor.
Definition: crwimage_int.hpp:444
void decode(Image &image, ByteOrder byteOrder) const
Decode metadata from the component and add it to image.
Definition: crwimage_int.cpp:315
virtual void doAdd(AutoPtr component)=0
Implements add()
std::vector< CiffComponent * > Components
Container type to hold all metadata.
Definition: crwimage_int.hpp:97
DataLocId
Type to identify where the data is stored in a directory.
Definition: crwimage_int.hpp:76
uint32_t size_
Data size (overwrites the size from the entry)
Definition: crwimage_int.hpp:574
void(* CrwDecodeFct)(const CiffComponent &, const CrwMapping *, Image &, ByteOrder)
Function pointer for functions to decode Exif tags from a CRW entry.
Definition: crwimage_int.hpp:62
CiffComponent * findComponent(uint16_t crwTagId, uint16_t crwDir) const
Finds crwTagId in directory crwDir, returning a pointer to the component or 0 if not found...
Definition: crwimage_int.cpp:593
Provides classes and functions to encode and decode Exif and Iptc data. The libexiv2 API consists of ...
Definition: asfvideo.hpp:36
uint32_t size() const
Return the data size of this component.
Definition: crwimage_int.hpp:233
std::auto_ptr< CiffHeader > AutoPtr
CiffHeader auto_ptr type.
Definition: crwimage_int.hpp:439
static void decode(const CiffComponent &ciffComponent, Image &image, ByteOrder byteOrder)
Decode image metadata from a CRW entry, convert and add it to the image metadata. This function conve...
Definition: crwimage_int.cpp:770
uint16_t crwDir_
Directory tag.
Definition: crwimage_int.hpp:541
void setDir(uint16_t dir)
Set the directory tag for this component.
Definition: crwimage_int.hpp:180
virtual void doRemove(CrwDirs &crwDirs, uint16_t crwTagId)
Implements remove(). The default implementation does nothing.
Definition: crwimage_int.cpp:718
uint16_t tag_
Exif tag to map to.
Definition: crwimage_int.hpp:575
void(* CrwEncodeFct)(const Image &, const CrwMapping *, CiffHeader *)
Function pointer for functions to encode CRW entries from Exif tags.
Definition: crwimage_int.hpp:68
DataLocId dataLocation() const
Return the data location for this component.
Definition: crwimage_int.hpp:248
const byte * pData() const
Return a pointer to the data area of this component.
Definition: crwimage_int.hpp:239
static void encode(CiffHeader *pHead, const Image &image)
Encode image metadata from image into the CRW parse tree. This function converts all Exif metadata th...
Definition: crwimage_int.cpp:993
uint16_t crwDir_
CRW directory tag.
Definition: crwimage_int.hpp:573
IfdId
Type to specify the IFD to which a metadata belongs.
Definition: tags_int.hpp:54
void setSize(uint32_t size)
Set the size of the data area.
Definition: crwimage_int.hpp:276
Interface class for components of the CIFF directory hierarchy of a CRW (Canon Raw data) image...
Definition: crwimage_int.hpp:92
void add(uint16_t crwTagId, uint16_t crwDir, DataBuf buf)
Set the value of entry crwTagId in directory crwDir to buf. If this tag doesn&#39;t exist, it is added along with all directories needed.
Definition: crwimage_int.cpp:621
virtual bool doEmpty() const
Implements empty(). Default implementation returns true if size is 0.
Definition: crwimage_int.cpp:760
void print(std::ostream &os, ByteOrder byteOrder, const std::string &prefix="") const
Print debug info about a component to os.
Definition: crwimage_int.cpp:498
uint32_t writeValueData(Blob &blob, uint32_t offset)
Writes the entry&#39;s value if size is larger than eight bytes. If needed, the value is padded with one ...
Definition: crwimage_int.cpp:383