Exiv2
crwimage_int.hpp
1 // ***************************************************************** -*- C++ -*-
2 /*
3  * Copyright (C) 2004-2021 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  */
20 #ifndef CRWIMAGE_INT_HPP_
21 #define CRWIMAGE_INT_HPP_
22 
23 // *****************************************************************************
24 // included header files
25 #include "tags_int.hpp"
26 #include "image.hpp"
27 
28 // + standard includes
29 #include <iosfwd>
30 #include <string>
31 #include <vector>
32 #include <stack>
33 
34 // *****************************************************************************
35 // namespace extensions
36 namespace Exiv2 {
37 
38 // *****************************************************************************
39 // class declarations
40  class ExifData;
41 
42  namespace Internal {
43 
44 // *****************************************************************************
45 // class declarations
46  class CiffHeader;
47  class CiffComponent;
48  struct CrwMapping;
49  struct CrwSubDir;
50 
51 // *****************************************************************************
52 // type definitions
53 
55  typedef void (*CrwDecodeFct)(const CiffComponent&,
56  const CrwMapping*,
57  Image&,
58  ByteOrder);
59 
61  typedef void (*CrwEncodeFct)(const Image&,
62  const CrwMapping*,
63  CiffHeader*);
64 
66  typedef std::stack<CrwSubDir> CrwDirs;
67 
69  enum DataLocId {
70  valueData,
71  directoryData,
72  lastDataLocId
73  };
74 
75 // *****************************************************************************
76 // class definitions
77 
84  class CiffComponent {
85  public:
87  typedef std::auto_ptr<CiffComponent> AutoPtr;
89  typedef std::vector<CiffComponent*> Components;
90 
92 
95  : dir_(0), tag_(0), size_(0), offset_(0), pData_(0),
96  isAllocated_(false) {}
98  CiffComponent(uint16_t tag, uint16_t dir)
99  : dir_(dir), tag_(tag), size_(0), offset_(0), pData_(0),
100  isAllocated_(false) {}
102  virtual ~CiffComponent();
104 
106 
107  // Default assignment operator is fine
108 
110  void add(AutoPtr component);
124  CiffComponent* add(CrwDirs& crwDirs, uint16_t crwTagId);
134  void remove(CrwDirs& crwDirs, uint16_t crwTagId);
145  void read(const byte* pData,
146  uint32_t size,
147  uint32_t start,
148  ByteOrder byteOrder);
159  uint32_t write(Blob& blob, ByteOrder byteOrder, uint32_t offset);
170  uint32_t writeValueData(Blob& blob, uint32_t offset);
172  void setDir(uint16_t dir) { dir_ = dir; }
174  void setValue(DataBuf buf);
176 
178  static TypeId typeId(uint16_t tag);
180  static DataLocId dataLocation(uint16_t tag);
181 
183 
184 
191  void decode(Image& image, ByteOrder byteOrder) const;
199  void print(std::ostream& os,
200  ByteOrder byteOrder,
201  const std::string& prefix ="") const;
207  void writeDirEntry(Blob& blob, ByteOrder byteOrder) const;
209  uint16_t dir() const { return dir_; }
210 
212  uint16_t tag() const { return tag_; }
213 
215  bool empty() const;
216 
225  uint32_t size() const { return size_; }
226 
228  uint32_t offset() const { return offset_; }
229 
231  const byte* pData() const { return pData_; }
232 
234  uint16_t tagId() const { return tag_ & 0x3fff; }
235 
237  TypeId typeId() const { return typeId(tag_); }
238 
240  DataLocId dataLocation() const { return dataLocation(tag_); }
241 
246  CiffComponent* findComponent(uint16_t crwTagId, uint16_t crwDir) const;
248 
249  protected:
251 
252  virtual void doAdd(AutoPtr component) =0;
255  virtual CiffComponent* doAdd(CrwDirs& crwDirs, uint16_t crwTagId);
257  virtual void doRemove(CrwDirs& crwDirs, uint16_t crwTagId);
259  virtual void doRead(const byte* pData,
260  uint32_t size,
261  uint32_t start,
262  ByteOrder byteOrder);
264  virtual uint32_t doWrite(Blob& blob,
265  ByteOrder byteOrder,
266  uint32_t offset) =0;
268  void setSize(uint32_t size) { size_ = size; }
270  void setOffset(uint32_t offset) { offset_ = offset; }
272 
274 
275  virtual void doDecode(Image& image,
277  ByteOrder byteOrder) const =0;
279  virtual void doPrint(std::ostream& os,
280  ByteOrder byteOrder,
281  const std::string& prefix) const;
283  virtual bool doEmpty() const;
285  virtual CiffComponent* doFindComponent(uint16_t crwTagId,
286  uint16_t crwDir) const;
288 
289  private:
290  // DATA
291  uint16_t dir_;
292  uint16_t tag_;
293  uint32_t size_;
294  uint32_t offset_;
295  const byte* pData_;
296  bool isAllocated_;
297 
298  }; // class CiffComponent
299 
304  class CiffEntry : public CiffComponent {
305  public:
307 
308  CiffEntry() {}
311  CiffEntry(uint16_t tag, uint16_t dir) : CiffComponent(tag, dir) {}
312 
314  virtual ~CiffEntry();
316 
317  // Default assignment operator is fine
318 
319  private:
321 
322  using CiffComponent::doAdd;
323  // See base class comment
324  virtual void doAdd(AutoPtr component);
329  virtual uint32_t doWrite(Blob& blob,
330  ByteOrder byteOrder,
331  uint32_t offset);
333 
335 
336  // See base class comment
337  virtual void doDecode(Image& image, ByteOrder byteOrder) const;
339 
340  }; // class CiffEntry
341 
343  class CiffDirectory : public CiffComponent {
344  public:
346 
347  CiffDirectory() : cc_(NULL) {}
350  CiffDirectory(uint16_t tag, uint16_t dir) : CiffComponent(tag, dir), cc_(NULL) {}
351 
353  virtual ~CiffDirectory();
355 
357 
358  // Default assignment operator is fine
359 
367  void readDirectory(const byte* pData,
368  uint32_t size,
369  ByteOrder byteOrder);
371 
372  private:
374 
375  // See base class comment
376  virtual void doAdd(AutoPtr component);
377  // See base class comment
378  virtual CiffComponent* doAdd(CrwDirs& crwDirs, uint16_t crwTagId);
379  // See base class comment
380  virtual void doRemove(CrwDirs& crwDirs, uint16_t crwTagId);
385  virtual uint32_t doWrite(Blob& blob,
386  ByteOrder byteOrder,
387  uint32_t offset);
388  // See base class comment
389  virtual void doRead(const byte* pData,
390  uint32_t size,
391  uint32_t start,
392  ByteOrder byteOrder);
394 
396 
397  // See base class comment
398  virtual void doDecode(Image& image,
399  ByteOrder byteOrder) const;
400 
401  // See base class comment
402  virtual void doPrint(std::ostream& os,
403  ByteOrder byteOrder,
404  const std::string& prefix) const;
405 
407  virtual bool doEmpty() const;
408 
409  // See base class comment
410  virtual CiffComponent* doFindComponent(uint16_t crwTagId,
411  uint16_t crwDir) const;
413 
414  private:
415  // DATA
416  Components components_;
417  AutoPtr m_; // used by recursive doAdd
418  CiffComponent* cc_;
419 
420  }; // class CiffDirectory
421 
428  class CiffHeader {
429  public:
431  typedef std::auto_ptr<CiffHeader> AutoPtr;
432 
434 
435  CiffHeader()
437  : pRootDir_ (0),
438  byteOrder_ (littleEndian),
439  offset_ (0x0000001a),
440  pPadding_ (0),
441  padded_ (0)
442  {}
444  virtual ~CiffHeader();
446 
448 
449 
458  void read(const byte* pData, uint32_t size);
468  void add(uint16_t crwTagId, uint16_t crwDir, DataBuf buf);
477  void remove(uint16_t crwTagId, uint16_t crwDir);
479 
481  static const char* signature() { return signature_; }
482 
484 
485 
493  void write(Blob& blob) const;
502  void decode(Image& image) const;
509  void print(std::ostream& os, const std::string& prefix ="") const;
511  ByteOrder byteOrder() const { return byteOrder_; }
516  CiffComponent* findComponent(uint16_t crwTagId, uint16_t crwDir) const;
518 
519  private:
520  // DATA
521  static const char signature_[];
522 
523  CiffDirectory* pRootDir_;
524  ByteOrder byteOrder_;
525  uint32_t offset_;
526  byte* pPadding_;
527  uint32_t padded_;
528 
529  }; // class CiffHeader
530 
532  struct CrwSubDir {
533  uint16_t crwDir_;
534  uint16_t parent_;
535  }; // struct CrwSubDir
536 
541  struct CrwMapping {
543 
544  CrwMapping(
546  uint16_t crwTagId,
547  uint16_t crwDir,
548  uint32_t size,
549  uint16_t tag,
550  Internal::IfdId ifdId,
551  CrwDecodeFct toExif,
552  CrwEncodeFct fromExif)
553  : crwTagId_ (crwTagId),
554  crwDir_ (crwDir),
555  size_ (size),
556  tag_ (tag),
557  ifdId_ (ifdId),
558  toExif_ (toExif),
559  fromExif_ (fromExif)
560  {}
562 
563  // DATA
564  uint16_t crwTagId_;
565  uint16_t crwDir_;
566  uint32_t size_;
567  uint16_t tag_;
571 
572  }; // struct CrwMapping
573 
578  class CrwMap {
580 
581  CrwMap();
584 
585  public:
596  static void decode(const CiffComponent& ciffComponent,
597  Image& image,
598  ByteOrder byteOrder);
608  static void encode(CiffHeader* pHead, const Image& image);
609 
616  static void loadStack(CrwDirs& crwDirs, uint16_t crwDir);
617 
618  private:
620  static const CrwMapping* crwMapping(uint16_t crwDir, uint16_t crwTagId);
621 
631  static void decodeBasic(const CiffComponent& ciffComponent,
632  const CrwMapping* pCrwMapping,
633  Image& image,
634  ByteOrder byteOrder);
635 
637  static void decode0x0805(const CiffComponent& ciffComponent,
638  const CrwMapping* pCrwMapping,
639  Image& image,
640  ByteOrder byteOrder);
641 
643  static void decode0x080a(const CiffComponent& ciffComponent,
644  const CrwMapping* pCrwMapping,
645  Image& image,
646  ByteOrder byteOrder);
647 
649  static void decodeArray(const CiffComponent& ciffComponent,
650  const CrwMapping* pCrwMapping,
651  Image& image,
652  ByteOrder byteOrder);
653 
655  static void decode0x180e(const CiffComponent& ciffComponent,
656  const CrwMapping* pCrwMapping,
657  Image& image,
658  ByteOrder byteOrder);
659 
661  static void decode0x1810(const CiffComponent& ciffComponent,
662  const CrwMapping* pCrwMapping,
663  Image& image,
664  ByteOrder byteOrder);
665 
667  static void decode0x2008(const CiffComponent& ciffComponent,
668  const CrwMapping* pCrwMapping,
669  Image& image,
670  ByteOrder byteOrder);
671 
686  static void encodeBasic(const Image& image,
687  const CrwMapping* pCrwMapping,
688  CiffHeader* pHead);
689 
691  static void encode0x0805(const Image& image,
692  const CrwMapping* pCrwMapping,
693  CiffHeader* pHead);
694 
696  static void encode0x080a(const Image& image,
697  const CrwMapping* pCrwMapping,
698  CiffHeader* pHead);
699 
701  static void encodeArray(const Image& image,
702  const CrwMapping* pCrwMapping,
703  CiffHeader* pHead);
704 
706  static void encode0x180e(const Image& image,
707  const CrwMapping* pCrwMapping,
708  CiffHeader* pHead);
709 
711  static void encode0x1810(const Image& image,
712  const CrwMapping* pCrwMapping,
713  CiffHeader* pHead);
714 
716  static void encode0x2008(const Image& image,
717  const CrwMapping* pCrwMapping,
718  CiffHeader* pHead);
719  private:
720  // DATA
721  static const CrwMapping crwMapping_[];
722  static const CrwSubDir crwSubDir_[];
723 
724  }; // class CrwMap
725 
726 // *****************************************************************************
727 // template, inline and free functions
728 
734  DataBuf packIfdId(const ExifData& exifData,
735  IfdId ifdId,
736  ByteOrder byteOrder);
737 
738 }} // namespace Internal, Exiv2
739 
740 #endif // #ifndef CRWIMAGE_INT_HPP_
Utility class containing a character array. All it does is to take care of memory allocation and dele...
Definition: types.hpp:193
A container for Exif data. This is a top-level class of the Exiv2 library. The container holds Exifda...
Definition: exif.hpp:434
Abstract base class defining the interface for an image. This is the top-level interface to the Exiv2...
Definition: image.hpp:78
Interface class for components of the CIFF directory hierarchy of a CRW (Canon Raw data) image....
Definition: crwimage_int.hpp:84
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:488
std::auto_ptr< CiffComponent > AutoPtr
CiffComponent auto_ptr type.
Definition: crwimage_int.hpp:87
std::vector< CiffComponent * > Components
Container type to hold all metadata.
Definition: crwimage_int.hpp:89
uint32_t writeValueData(Blob &blob, uint32_t offset)
Writes the entry's value if size is larger than eight bytes. If needed, the value is padded with one ...
Definition: crwimage_int.cpp:422
virtual CiffComponent * doFindComponent(uint16_t crwTagId, uint16_t crwDir) const
Implements findComponent(). The default implementation checks the entry.
Definition: crwimage_int.cpp:637
CiffComponent(uint16_t tag, uint16_t dir)
Constructor taking a tag and directory.
Definition: crwimage_int.hpp:98
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:544
TypeId typeId() const
Return the type id of thi component.
Definition: crwimage_int.hpp:237
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:408
uint32_t size() const
Return the data size of this component.
Definition: crwimage_int.hpp:225
bool empty() const
Return true if the component is empty, else false.
Definition: crwimage_int.cpp:793
void setSize(uint32_t size)
Set the size of the data area.
Definition: crwimage_int.hpp:268
virtual uint32_t doWrite(Blob &blob, ByteOrder byteOrder, uint32_t offset)=0
Implements write()
virtual void doRemove(CrwDirs &crwDirs, uint16_t crwTagId)
Implements remove(). The default implementation does nothing.
Definition: crwimage_int.cpp:756
DataLocId dataLocation() const
Return the data location for this component.
Definition: crwimage_int.hpp:240
virtual ~CiffComponent()
Virtual destructor.
Definition: crwimage_int.cpp:184
void read(const byte *pData, uint32_t size, uint32_t start, ByteOrder byteOrder)
Read a component from a data buffer.
Definition: crwimage_int.cpp:245
virtual bool doEmpty() const
Implements empty(). Default implementation returns true if size is 0.
Definition: crwimage_int.cpp:798
void setOffset(uint32_t offset)
Set the offset for this component.
Definition: crwimage_int.hpp:270
virtual void doDecode(Image &image, ByteOrder byteOrder) const =0
Implements decode()
void remove(CrwDirs &crwDirs, uint16_t crwTagId)
Remove crwTagId from the parse tree, if it exists yet. crwDirs contains the path of subdirectories,...
Definition: crwimage_int.cpp:751
void setDir(uint16_t dir)
Set the directory tag for this component.
Definition: crwimage_int.hpp:172
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:631
void decode(Image &image, ByteOrder byteOrder) const
Decode metadata from the component and add it to image.
Definition: crwimage_int.cpp:354
uint16_t tagId() const
Return the tag id of this component.
Definition: crwimage_int.hpp:234
void setValue(DataBuf buf)
Set the data value of the entry.
Definition: crwimage_int.cpp:579
uint16_t tag() const
Return the tag of this component.
Definition: crwimage_int.hpp:212
void print(std::ostream &os, ByteOrder byteOrder, const std::string &prefix="") const
Print debug info about a component to os.
Definition: crwimage_int.cpp:537
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:253
uint16_t dir() const
Return the tag of the directory containing this component.
Definition: crwimage_int.hpp:209
CiffComponent()
Default constructor.
Definition: crwimage_int.hpp:94
virtual void doAdd(AutoPtr component)=0
Implements add()
uint32_t offset() const
Return the offset to the data from the start of the directory.
Definition: crwimage_int.hpp:228
void add(AutoPtr component)
Add a component to the composition.
Definition: crwimage_int.cpp:202
const byte * pData() const
Return a pointer to the data area of this component.
Definition: crwimage_int.hpp:231
This class models a CIFF directory of a CRW (Canon Raw data) image.
Definition: crwimage_int.hpp:343
CiffDirectory()
Default constructor.
Definition: crwimage_int.hpp:348
void readDirectory(const byte *pData, uint32_t size, ByteOrder byteOrder)
Parse a CIFF directory from a memory buffer.
Definition: crwimage_int.cpp:316
virtual ~CiffDirectory()
Virtual destructor.
Definition: crwimage_int.cpp:193
CiffDirectory(uint16_t tag, uint16_t dir)
Constructor taking a tag and directory.
Definition: crwimage_int.hpp:350
This class models one directory entry of a CIFF directory of a CRW (Canon Raw data) image.
Definition: crwimage_int.hpp:304
virtual ~CiffEntry()
Virtual destructor.
Definition: crwimage_int.cpp:189
CiffEntry(uint16_t tag, uint16_t dir)
Constructor taking a tag and directory.
Definition: crwimage_int.hpp:311
CiffEntry()
Default constructor.
Definition: crwimage_int.hpp:309
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:428
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't exist,...
Definition: crwimage_int.cpp:659
std::auto_ptr< CiffHeader > AutoPtr
CiffHeader auto_ptr type.
Definition: crwimage_int.hpp:431
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:217
ByteOrder byteOrder() const
Return the byte order (little or big endian).
Definition: crwimage_int.hpp:511
void remove(uint16_t crwTagId, uint16_t crwDir)
Remove entry crwTagId in directory crwDir from the parse tree. If it's the last entry in the director...
Definition: crwimage_int.cpp:738
void print(std::ostream &os, const std::string &prefix="") const
Print debug info for the CRW image to os.
Definition: crwimage_int.cpp:527
void decode(Image &image) const
Decode the CRW image and add it to image.
Definition: crwimage_int.cpp:348
static const char * signature()
Return a pointer to the Canon CRW signature.
Definition: crwimage_int.hpp:481
virtual ~CiffHeader()
Virtual destructor.
Definition: crwimage_int.cpp:178
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:373
CiffHeader()
Default constructor.
Definition: crwimage_int.hpp:436
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:624
Static class providing mapping functionality from CRW entries to image metadata and vice versa.
Definition: crwimage_int.hpp:578
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:808
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:1026
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:1016
void(* CrwDecodeFct)(const CiffComponent &, const CrwMapping *, Image &, ByteOrder)
Function pointer for functions to decode Exif tags from a CRW entry.
Definition: crwimage_int.hpp:55
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:1244
DataLocId
Type to identify where the data is stored in a directory.
Definition: crwimage_int.hpp:69
std::stack< CrwSubDir > CrwDirs
Stack to hold a path of CRW directories.
Definition: crwimage_int.hpp:66
IfdId
Type to specify the IFD to which a metadata belongs.
Definition: tags_int.hpp:46
void(* CrwEncodeFct)(const Image &, const CrwMapping *, CiffHeader *)
Function pointer for functions to encode CRW entries from Exif tags.
Definition: crwimage_int.hpp:61
Provides classes and functions to encode and decode Exif and Iptc data. The libexiv2 API consists of ...
Definition: asfvideo.hpp:36
TypeId
Exiv2 value type identifiers.
Definition: types.hpp:119
std::vector< byte > Blob
Container for binary data.
Definition: types.hpp:151
ByteOrder
Type to express the byte order (little or big endian)
Definition: types.hpp:102
Structure for a mapping table for conversion of CIFF entries to image metadata and vice versa.
Definition: crwimage_int.hpp:541
CrwDecodeFct toExif_
Conversion function.
Definition: crwimage_int.hpp:569
IfdId ifdId_
Exif Ifd id to map to.
Definition: crwimage_int.hpp:568
uint16_t tag_
Exif tag to map to.
Definition: crwimage_int.hpp:567
uint32_t size_
Data size (overwrites the size from the entry)
Definition: crwimage_int.hpp:566
uint16_t crwDir_
CRW directory tag.
Definition: crwimage_int.hpp:565
CrwEncodeFct fromExif_
Reverse conversion function.
Definition: crwimage_int.hpp:570
uint16_t crwTagId_
CRW tag id.
Definition: crwimage_int.hpp:564
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:545
Structure for the CIFF directory hierarchy.
Definition: crwimage_int.hpp:532
uint16_t parent_
Parent directory tag.
Definition: crwimage_int.hpp:534
uint16_t crwDir_
Directory tag.
Definition: crwimage_int.hpp:533