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
36namespace 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 using CrwDecodeFct = void (*)(const CiffComponent&, const CrwMapping*, Image&, ByteOrder);
56
58 using CrwEncodeFct = void (*)(const Image&, const CrwMapping*, CiffHeader*);
59
61 using CrwDirs = std::stack<CrwSubDir>;
62
64 enum DataLocId {
65 valueData,
66 directoryData,
67 lastDataLocId
68 };
69
70// *****************************************************************************
71// class definitions
72
80 public:
82 using UniquePtr = std::unique_ptr<CiffComponent>;
84 using Components = std::vector<CiffComponent*>;
85
87
88
89 CiffComponent() = default;
91 CiffComponent(uint16_t tag, uint16_t dir)
92 : dir_(dir), tag_(tag)
93 {}
95 virtual ~CiffComponent() = default;
97
99
100 // Default assignment operator is fine
101
103 void add(UniquePtr component);
117 CiffComponent* add(CrwDirs& crwDirs, uint16_t crwTagId);
127 void remove(CrwDirs& crwDirs, uint16_t crwTagId);
138 void read(const byte* pData,
139 uint32_t size,
140 uint32_t start,
141 ByteOrder byteOrder);
152 uint32_t write(Blob& blob, ByteOrder byteOrder, uint32_t offset);
163 uint32_t writeValueData(Blob& blob, uint32_t offset);
165 void setDir(uint16_t dir) { dir_ = dir; }
167 void setValue(DataBuf&& buf);
169
171 static TypeId typeId(uint16_t tag);
173 static DataLocId dataLocation(uint16_t tag);
174
176
177
184 void decode(Image& image, ByteOrder byteOrder) const;
192 void print(std::ostream& os,
193 ByteOrder byteOrder,
194 const std::string& prefix ="") const;
200 void writeDirEntry(Blob& blob, ByteOrder byteOrder) const;
202 uint16_t dir() const { return dir_; }
203
205 uint16_t tag() const { return tag_; }
206
208 bool empty() const;
209
218 uint32_t size() const { return size_; }
219
221 uint32_t offset() const { return offset_; }
222
224 const byte* pData() const { return pData_; }
225
227 uint16_t tagId() const { return tag_ & 0x3fff; }
228
230 TypeId typeId() const { return typeId(tag_); }
231
233 DataLocId dataLocation() const { return dataLocation(tag_); }
234
239 CiffComponent* findComponent(uint16_t crwTagId, uint16_t crwDir) const;
241
242 protected:
244
245
246 virtual void doAdd(UniquePtr component) =0;
248 virtual CiffComponent* doAdd(CrwDirs& crwDirs, uint16_t crwTagId);
250 virtual void doRemove(CrwDirs& crwDirs, uint16_t crwTagId);
252 virtual void doRead(const byte* pData,
253 uint32_t size,
254 uint32_t start,
255 ByteOrder byteOrder);
257 virtual uint32_t doWrite(Blob& blob,
258 ByteOrder byteOrder,
259 uint32_t offset) =0;
261 void setSize(uint32_t size) { size_ = size; }
263 void setOffset(uint32_t offset) { offset_ = offset; }
265
267
268
269 virtual void doDecode(Image& image,
270 ByteOrder byteOrder) const =0;
272 virtual void doPrint(std::ostream& os,
273 ByteOrder byteOrder,
274 const std::string& prefix) const;
276 virtual bool doEmpty() const;
278 virtual CiffComponent* doFindComponent(uint16_t crwTagId,
279 uint16_t crwDir) const;
281
282 private:
283 // DATA
284 uint16_t dir_ = 0;
285 uint16_t tag_ = 0;
286 uint32_t size_ = 0;
287 uint32_t offset_ = 0;
288
289 // Notes on the ownership model of pData_: pData_ is a always a read-only
290 // pointer to a buffer owned by somebody else. Usually it is a pointer
291 // into a copy of the image that was memory-mapped in CrwImage::readMetadata().
292 // However, if CiffComponent::setValue() is used, then it is a pointer into
293 // the storage_ DataBuf below.
294 const byte* pData_ = nullptr;
295
296 // This DataBuf is only used when CiffComponent::setValue is called.
297 // Otherwise, it remains empty.
298 DataBuf storage_;
299 }; // class CiffComponent
300
305 class CiffEntry : public CiffComponent {
306 public:
308
309
310 CiffEntry() = default;
312 CiffEntry(uint16_t tag, uint16_t dir) : CiffComponent(tag, dir) {}
314
315 // Default assignment operator is fine
316
317 private:
319
321 // See base class comment
322 void doAdd(UniquePtr component) override;
327 uint32_t doWrite(Blob& blob, ByteOrder byteOrder, uint32_t offset) override;
329
331
332 // See base class comment
333 void doDecode(Image& image, ByteOrder byteOrder) const override;
335
336 }; // class CiffEntry
337
340 public:
342
343
344 CiffDirectory() = default;
346 CiffDirectory(uint16_t tag, uint16_t dir) : CiffComponent(tag, dir) {}
347
349 ~CiffDirectory() override;
351
353
354 // Default assignment operator is fine
355
363 void readDirectory(const byte* pData,
364 uint32_t size,
365 ByteOrder byteOrder);
367
368 private:
370
371 // See base class comment
372 void doAdd(UniquePtr component) override;
373 // See base class comment
374 CiffComponent* doAdd(CrwDirs& crwDirs, uint16_t crwTagId) override;
375 // See base class comment
376 void doRemove(CrwDirs& crwDirs, uint16_t crwTagId) override;
381 uint32_t doWrite(Blob& blob, ByteOrder byteOrder, uint32_t offset) override;
382 // See base class comment
383 void doRead(const byte* pData, uint32_t size, uint32_t start, ByteOrder byteOrder) override;
385
387
388 // See base class comment
389 void doDecode(Image& image, ByteOrder byteOrder) const override;
390
391 // See base class comment
392 void doPrint(std::ostream& os, ByteOrder byteOrder, const std::string& prefix) const override;
393
395 bool doEmpty() const override;
396
397 // See base class comment
398 CiffComponent* doFindComponent(uint16_t crwTagId, uint16_t crwDir) const override;
400
401 private:
402 // DATA
403 Components components_;
404 UniquePtr m_; // used by recursive doAdd
405 CiffComponent* cc_ = nullptr;
406
407 }; // class CiffDirectory
408
416 public:
418 using UniquePtr = std::unique_ptr<CiffHeader>;
419
421
422
423 CiffHeader() = default;
425 virtual ~CiffHeader();
427
429
430
439 void read(const byte* pData, uint32_t size);
449 void add(uint16_t crwTagId, uint16_t crwDir, DataBuf&& buf);
458 void remove(uint16_t crwTagId, uint16_t crwDir);
460
462 static const char* signature() { return signature_; }
463
465
466
474 void write(Blob& blob) const;
483 void decode(Image& image) const;
490 void print(std::ostream& os, const std::string& prefix ="") const;
492 ByteOrder byteOrder() const { return byteOrder_; }
497 CiffComponent* findComponent(uint16_t crwTagId, uint16_t crwDir) const;
499
500 private:
501 // DATA
502 static const char signature_[];
503
504 CiffDirectory* pRootDir_ = nullptr;
505 ByteOrder byteOrder_ = littleEndian;
506 uint32_t offset_ = 0;
507 byte* pPadding_ = nullptr;
508 uint32_t padded_ = 0;
509
510 }; // class CiffHeader
511
513 struct CrwSubDir {
514 uint16_t crwDir_;
515 uint16_t parent_;
516 }; // struct CrwSubDir
517
522 struct CrwMapping {
524
525
527 uint16_t crwTagId,
528 uint16_t crwDir,
529 uint32_t size,
530 uint16_t tag,
531 Internal::IfdId ifdId,
532 CrwDecodeFct toExif,
533 CrwEncodeFct fromExif)
534 : crwTagId_ (crwTagId),
535 crwDir_ (crwDir),
536 size_ (size),
537 tag_ (tag),
538 ifdId_ (ifdId),
539 toExif_ (toExif),
540 fromExif_ (fromExif)
541 {}
543
544 // DATA
545 uint16_t crwTagId_;
546 uint16_t crwDir_;
547 uint32_t size_;
548 uint16_t tag_;
552
553 }; // struct CrwMapping
554
559 class CrwMap {
561
562
563 CrwMap();
565
566 public:
577 static void decode(const CiffComponent& ciffComponent,
578 Image& image,
579 ByteOrder byteOrder);
589 static void encode(CiffHeader* pHead, const Image& image);
590
597 static void loadStack(CrwDirs& crwDirs, uint16_t crwDir);
598
599 private:
601 static const CrwMapping* crwMapping(uint16_t crwDir, uint16_t crwTagId);
602
612 static void decodeBasic(const CiffComponent& ciffComponent,
613 const CrwMapping* pCrwMapping,
614 Image& image,
615 ByteOrder byteOrder);
616
618 static void decode0x0805(const CiffComponent& ciffComponent,
619 const CrwMapping* pCrwMapping,
620 Image& image,
621 ByteOrder byteOrder);
622
624 static void decode0x080a(const CiffComponent& ciffComponent,
625 const CrwMapping* pCrwMapping,
626 Image& image,
627 ByteOrder byteOrder);
628
630 static void decodeArray(const CiffComponent& ciffComponent,
631 const CrwMapping* pCrwMapping,
632 Image& image,
633 ByteOrder byteOrder);
634
636 static void decode0x180e(const CiffComponent& ciffComponent,
637 const CrwMapping* pCrwMapping,
638 Image& image,
639 ByteOrder byteOrder);
640
642 static void decode0x1810(const CiffComponent& ciffComponent,
643 const CrwMapping* pCrwMapping,
644 Image& image,
645 ByteOrder byteOrder);
646
648 static void decode0x2008(const CiffComponent& ciffComponent,
649 const CrwMapping* pCrwMapping,
650 Image& image,
651 ByteOrder byteOrder);
652
667 static void encodeBasic(const Image& image,
668 const CrwMapping* pCrwMapping,
669 CiffHeader* pHead);
670
672 static void encode0x0805(const Image& image,
673 const CrwMapping* pCrwMapping,
674 CiffHeader* pHead);
675
677 static void encode0x080a(const Image& image,
678 const CrwMapping* pCrwMapping,
679 CiffHeader* pHead);
680
682 static void encodeArray(const Image& image,
683 const CrwMapping* pCrwMapping,
684 CiffHeader* pHead);
685
687 static void encode0x180e(const Image& image,
688 const CrwMapping* pCrwMapping,
689 CiffHeader* pHead);
690
692 static void encode0x1810(const Image& image,
693 const CrwMapping* pCrwMapping,
694 CiffHeader* pHead);
695
697 static void encode0x2008(const Image& image,
698 const CrwMapping* pCrwMapping,
699 CiffHeader* pHead);
700 private:
701 // DATA
702 static const CrwMapping crwMapping_[];
703 static const CrwSubDir crwSubDir_[];
704
705 }; // class CrwMap
706
707// *****************************************************************************
708// template, inline and free functions
709
715 DataBuf packIfdId(const ExifData& exifData,
716 IfdId ifdId,
717 ByteOrder byteOrder);
718
719}} // namespace Internal, Exiv2
720
721#endif // #ifndef CRWIMAGE_INT_HPP_
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:79
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:467
const byte * pData() const
Return a pointer to the data area of this component.
Definition: crwimage_int.hpp:224
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:403
virtual CiffComponent * doFindComponent(uint16_t crwTagId, uint16_t crwDir) const
Implements findComponent(). The default implementation checks the entry.
Definition: crwimage_int.cpp:609
CiffComponent(uint16_t tag, uint16_t dir)
Constructor taking a tag and directory.
Definition: crwimage_int.hpp:91
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:523
TypeId typeId() const
Return the type id of thi component.
Definition: crwimage_int.hpp:230
void add(UniquePtr component)
Add a component to the composition.
Definition: crwimage_int.cpp:185
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:389
uint32_t size() const
Return the data size of this component.
Definition: crwimage_int.hpp:218
bool empty() const
Return true if the component is empty, else false.
Definition: crwimage_int.cpp:762
void setSize(uint32_t size)
Set the size of the data area.
Definition: crwimage_int.hpp:261
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:725
CiffComponent()=default
Default constructor.
virtual void doAdd(UniquePtr component)=0
Implements add()
DataLocId dataLocation() const
Return the data location for this component.
Definition: crwimage_int.hpp:233
std::unique_ptr< CiffComponent > UniquePtr
CiffComponent auto_ptr type.
Definition: crwimage_int.hpp:82
std::vector< CiffComponent * > Components
Container type to hold all metadata.
Definition: crwimage_int.hpp:84
void read(const byte *pData, uint32_t size, uint32_t start, ByteOrder byteOrder)
Read a component from a data buffer.
Definition: crwimage_int.cpp:228
virtual bool doEmpty() const
Implements empty(). Default implementation returns true if size is 0.
Definition: crwimage_int.cpp:767
void setOffset(uint32_t offset)
Set the offset for this component.
Definition: crwimage_int.hpp:263
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:720
void setDir(uint16_t dir)
Set the directory tag for this component.
Definition: crwimage_int.hpp:165
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:603
void decode(Image &image, ByteOrder byteOrder) const
Decode metadata from the component and add it to image.
Definition: crwimage_int.cpp:337
uint16_t tagId() const
Return the tag id of this component.
Definition: crwimage_int.hpp:227
uint16_t tag() const
Return the tag of this component.
Definition: crwimage_int.hpp:205
void print(std::ostream &os, ByteOrder byteOrder, const std::string &prefix="") const
Print debug info about a component to os.
Definition: crwimage_int.cpp:516
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:236
virtual ~CiffComponent()=default
Virtual destructor.
uint16_t dir() const
Return the tag of the directory containing this component.
Definition: crwimage_int.hpp:202
uint32_t offset() const
Return the offset to the data from the start of the directory.
Definition: crwimage_int.hpp:221
void setValue(DataBuf &&buf)
Set the data value of the entry.
Definition: crwimage_int.cpp:556
This class models a CIFF directory of a CRW (Canon Raw data) image.
Definition: crwimage_int.hpp:339
CiffDirectory()=default
Default constructor.
void readDirectory(const byte *pData, uint32_t size, ByteOrder byteOrder)
Parse a CIFF directory from a memory buffer.
Definition: crwimage_int.cpp:299
~CiffDirectory() override
Virtual destructor.
Definition: crwimage_int.cpp:178
CiffDirectory(uint16_t tag, uint16_t dir)
Constructor taking a tag and directory.
Definition: crwimage_int.hpp:346
This class models one directory entry of a CIFF directory of a CRW (Canon Raw data) image.
Definition: crwimage_int.hpp:305
CiffEntry(uint16_t tag, uint16_t dir)
Constructor taking a tag and directory.
Definition: crwimage_int.hpp:312
CiffEntry()=default
Default constructor.
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:415
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:200
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:629
ByteOrder byteOrder() const
Return the byte order (little or big endian).
Definition: crwimage_int.hpp:492
CiffHeader()=default
Default constructor.
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:707
void print(std::ostream &os, const std::string &prefix="") const
Print debug info for the CRW image to os.
Definition: crwimage_int.cpp:506
void decode(Image &image) const
Decode the CRW image and add it to image.
Definition: crwimage_int.cpp:331
virtual ~CiffHeader()
Virtual destructor.
Definition: crwimage_int.cpp:172
std::unique_ptr< CiffHeader > UniquePtr
CiffHeader auto_ptr type.
Definition: crwimage_int.hpp:418
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:354
static const char * signature()
Return a pointer to the Canon CRW signature.
Definition: crwimage_int.hpp:462
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:595
Static class providing mapping functionality from CRW entries to image metadata and vice versa.
Definition: crwimage_int.hpp:559
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:777
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:994
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:984
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:1212
void(*)(const Image &, const CrwMapping *, CiffHeader *) CrwEncodeFct
Function pointer for functions to encode CRW entries from Exif tags.
Definition: crwimage_int.hpp:58
void(*)(const CiffComponent &, const CrwMapping *, Image &, ByteOrder) CrwDecodeFct
Function pointer for functions to decode Exif tags from a CRW entry.
Definition: crwimage_int.hpp:55
DataLocId
Type to identify where the data is stored in a directory.
Definition: crwimage_int.hpp:64
IfdId
Type to specify the IFD to which a metadata belongs.
Definition: tags_int.hpp:46
std::stack< CrwSubDir > CrwDirs
Stack to hold a path of CRW directories.
Definition: crwimage_int.hpp:61
Provides classes and functions to encode and decode Exif and Iptc data. The libexiv2 API consists of ...
Definition: basicio.hpp:43
TypeId
Exiv2 value type identifiers.
Definition: types.hpp:100
std::vector< byte > Blob
Container for binary data.
Definition: types.hpp:132
ByteOrder
Type to express the byte order (little or big endian)
Definition: types.hpp:83
Utility class containing a character array. All it does is to take care of memory allocation and dele...
Definition: types.hpp:162
Structure for a mapping table for conversion of CIFF entries to image metadata and vice versa.
Definition: crwimage_int.hpp:522
CrwDecodeFct toExif_
Conversion function.
Definition: crwimage_int.hpp:550
IfdId ifdId_
Exif Ifd id to map to.
Definition: crwimage_int.hpp:549
uint16_t tag_
Exif tag to map to.
Definition: crwimage_int.hpp:548
uint32_t size_
Data size (overwrites the size from the entry)
Definition: crwimage_int.hpp:547
uint16_t crwDir_
CRW directory tag.
Definition: crwimage_int.hpp:546
CrwEncodeFct fromExif_
Reverse conversion function.
Definition: crwimage_int.hpp:551
uint16_t crwTagId_
CRW tag id.
Definition: crwimage_int.hpp:545
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:526
Structure for the CIFF directory hierarchy.
Definition: crwimage_int.hpp:513
uint16_t parent_
Parent directory tag.
Definition: crwimage_int.hpp:515
uint16_t crwDir_
Directory tag.
Definition: crwimage_int.hpp:514