Exiv2
image.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 IMAGE_HPP_
21 #define IMAGE_HPP_
22 
23 // *****************************************************************************
24 #include "exiv2lib_export.h"
25 
26 // included header files
27 #include "basicio.hpp"
28 #include "exif.hpp"
29 #include "iptc.hpp"
30 #include "xmp_exiv2.hpp"
31 
32 // + standard includes
33 #include <string>
34 #include <vector>
35 
36 // *****************************************************************************
37 // namespace extensions
38 namespace Exiv2 {
39 
40 // *****************************************************************************
41 // class definitions
42 
44  namespace ImageType {
45  const int none = 0;
46  }
47 
49  struct NativePreview {
50  long position_;
51  uint32_t size_;
52  uint32_t width_;
53  uint32_t height_;
54  std::string filter_;
55  std::string mimeType_;
56  };
57 
59  typedef std::vector<NativePreview> NativePreviewList;
60 
64  typedef enum { kpsNone, kpsBasic, kpsXMP, kpsRecursive
65  , kpsIccProfile , kpsIptcErase
67 
78  class EXIV2API Image {
79  public:
81  typedef std::auto_ptr<Image> AutoPtr;
82 
84 
85 
90  Image(int imageType,
91  uint16_t supportedMetadata,
92  BasicIo::AutoPtr io);
94  virtual ~Image();
96 
98 
99 
106  virtual void printStructure(std::ostream& out, PrintStructureOption option =kpsNone, int depth=0);
120  virtual void readMetadata() =0;
133  virtual void writeMetadata() =0;
139  virtual void setExifData(const ExifData& exifData);
144  virtual void clearExifData();
150  virtual void setIptcData(const IptcData& iptcData);
155  virtual void clearIptcData();
167  virtual void setXmpPacket(const std::string& xmpPacket);
180  virtual void clearXmpPacket();
193  virtual void setXmpData(const XmpData& xmpData);
207  virtual void clearXmpData();
213  virtual void setComment(const std::string& comment);
218  virtual void clearComment();
225  virtual void setIccProfile(DataBuf& iccProfile,bool bTestValid=true);
232  virtual void appendIccProfile(const uint8_t* bytes, size_t size, bool bTestValid);
236  virtual void checkIccProfile();
241  virtual void clearIccProfile();
246  virtual bool iccProfileDefined() { return iccProfile_.size_?true:false;}
247 
251  virtual DataBuf* iccProfile() { return &iccProfile_; }
258  virtual void setMetadata(const Image& image);
263  virtual void clearMetadata();
275  virtual ExifData& exifData();
287  virtual IptcData& iptcData();
299  virtual XmpData& xmpData();
303  virtual std::string& xmpPacket();
318  void writeXmpFromPacket(bool flag);
328  void setByteOrder(ByteOrder byteOrder);
329 
335  void printTiffStructure(BasicIo& io,std::ostream& out, PrintStructureOption option,int depth,size_t offset=0);
336 
340  void printIFDStructure(BasicIo& io, std::ostream& out, Exiv2::PrintStructureOption option,uint32_t start,bool bSwap,char c,int depth);
341 
345  bool isBigEndianPlatform();
346 
350  bool isLittleEndianPlatform();
351 
352  bool isStringType(uint16_t type);
353  bool isShortType(uint16_t type);
354  bool isLongType(uint16_t type);
355  bool isLongLongType(uint16_t type);
356  bool isRationalType(uint16_t type);
357  bool is2ByteType(uint16_t type);
358  bool is4ByteType(uint16_t type);
359  bool is8ByteType(uint16_t type);
360  bool isPrintXMP(uint16_t type, Exiv2::PrintStructureOption option);
361  bool isPrintICC(uint16_t type, Exiv2::PrintStructureOption option);
362 
363  uint64_t byteSwap(uint64_t value,bool bSwap) const;
364  uint32_t byteSwap(uint32_t value,bool bSwap) const;
365  uint16_t byteSwap(uint16_t value,bool bSwap) const;
366  uint16_t byteSwap2(const DataBuf& buf,size_t offset,bool bSwap) const;
367  uint32_t byteSwap4(const DataBuf& buf,size_t offset,bool bSwap) const;
368  uint64_t byteSwap8(const DataBuf& buf,size_t offset,bool bSwap) const;
369 
371 
373 
374 
378  ByteOrder byteOrder() const;
384  bool good() const;
395  virtual std::string mimeType() const =0;
399  virtual int pixelWidth() const;
403  virtual int pixelHeight() const;
415  virtual const ExifData& exifData() const;
427  virtual const IptcData& iptcData() const;
439  virtual const XmpData& xmpData() const;
443  virtual std::string comment() const;
447  virtual const std::string& xmpPacket() const;
462  virtual BasicIo& io() const;
469  AccessMode checkMode(MetadataId metadataId) const;
474  bool supportsMetadata(MetadataId metadataId) const;
476  bool writeXmpFromPacket() const;
478  const NativePreviewList& nativePreviews() const;
480 
483  int imageType,
484  uint16_t supportedMetadata
485  ) {
486  imageType_ = imageType;
487  supportedMetadata_ = supportedMetadata;
488  }
489 
491  int imageType() const { return imageType_; }
492 
493  protected:
494  // DATA
500  std::string comment_;
501  std::string xmpPacket_;
505 
507  const std::string& tagName(uint16_t tag);
508 
510  const char* typeName(uint16_t tag) const;
511 
512  private:
514 
515  Image(const Image& rhs);
518  Image& operator=(const Image& rhs);
520 
521  // DATA
522  int imageType_;
523  uint16_t supportedMetadata_;
524  bool writeXmpFromPacket_;
525  ByteOrder byteOrder_;
526 
527  std::map<int,std::string> tags_;
528  bool init_;
529 
530  }; // class Image
531 
533  typedef Image::AutoPtr (*NewInstanceFct)(BasicIo::AutoPtr io, bool create);
535  typedef bool (*IsThisTypeFct)(BasicIo& iIo, bool advance);
536 
542  class EXIV2API ImageFactory {
543  friend bool Image::good() const;
544  public:
559  static BasicIo::AutoPtr createIo(const std::string& path, bool useCurl = true);
560 #ifdef EXV_UNICODE_PATH
565  static BasicIo::AutoPtr createIo(const std::wstring& wpath, bool useCurl = true);
566 #endif
580  static Image::AutoPtr open(const std::string& path, bool useCurl = true);
581 #ifdef EXV_UNICODE_PATH
586  static Image::AutoPtr open(const std::wstring& wpath, bool useCurl = true);
587 #endif
599  static Image::AutoPtr open(const byte* data, long size);
617  static Image::AutoPtr open(BasicIo::AutoPtr io);
627  static Image::AutoPtr create(int type, const std::string& path);
628 #ifdef EXV_UNICODE_PATH
633  static Image::AutoPtr create(int type, const std::wstring& wpath);
634 #endif
643  static Image::AutoPtr create(int type);
658  static Image::AutoPtr create(int type, BasicIo::AutoPtr io);
665  static int getType(const std::string& path);
666 #ifdef EXV_UNICODE_PATH
671  static int getType(const std::wstring& wpath);
672 #endif
680  static int getType(const byte* data, long size);
688  static int getType(BasicIo& io);
697  static AccessMode checkMode(int type, MetadataId metadataId);
718  static bool checkType(int type, BasicIo& io, bool advance);
719 
720  private:
722 
723  ImageFactory();
726  ImageFactory(const ImageFactory& rhs);
728 
729  }; // class ImageFactory
730 
731 // *****************************************************************************
732 // template, inline and free functions
733 
735  EXIV2API void append(Exiv2::Blob& blob, const byte* buf, uint32_t len);
736 
737 } // namespace Exiv2
738 
739 #endif // #ifndef IMAGE_HPP_
An interface for simple binary IO.
Definition: basicio.hpp:55
std::auto_ptr< BasicIo > AutoPtr
BasicIo auto_ptr type.
Definition: basicio.hpp:58
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
Returns an Image instance of the specified type.
Definition: image.hpp:542
Abstract base class defining the interface for an image. This is the top-level interface to the Exiv2...
Definition: image.hpp:78
int pixelHeight_
image pixel height
Definition: image.hpp:503
int pixelWidth_
image pixel width
Definition: image.hpp:502
virtual void readMetadata()=0
Read all metadata supported by a specific image format from the image. Before this method is called,...
virtual DataBuf * iccProfile()
return iccProfile
Definition: image.hpp:251
BasicIo::AutoPtr io_
Image data IO pointer.
Definition: image.hpp:495
void setTypeSupported(int imageType, uint16_t supportedMetadata)
set type support for this image format
Definition: image.hpp:482
bool good() const
Check if the Image instance is valid. Use after object construction.
Definition: image.cpp:791
virtual std::string mimeType() const =0
Return the MIME type of the image.
NativePreviewList nativePreviews_
list of native previews
Definition: image.hpp:504
std::auto_ptr< Image > AutoPtr
Image auto_ptr type.
Definition: image.hpp:81
int imageType() const
set type support for this image format
Definition: image.hpp:491
ExifData exifData_
Exif data container.
Definition: image.hpp:496
std::string xmpPacket_
XMP packet.
Definition: image.hpp:501
virtual bool iccProfileDefined()
Erase iccProfile. the profile is not removed from the actual image until the writeMetadata() method i...
Definition: image.hpp:246
IptcData iptcData_
IPTC data container.
Definition: image.hpp:497
std::string comment_
User comment.
Definition: image.hpp:500
virtual void writeMetadata()=0
Write metadata back to the image.
DataBuf iccProfile_
ICC buffer (binary data)
Definition: image.hpp:499
XmpData xmpData_
XMP data container.
Definition: image.hpp:498
A container for IPTC data. This is a top-level class of the Exiv2 library.
Definition: iptc.hpp:170
A container for XMP data. This is a top-level class of the Exiv2 library.
Definition: xmp_exiv2.hpp:166
Encoding and decoding of Exif data.
Encoding and decoding of IPTC data.
const int none
Not an image.
Definition: image.hpp:45
Provides classes and functions to encode and decode Exif and Iptc data. The libexiv2 API consists of ...
Definition: asfvideo.hpp:36
PrintStructureOption
Options for printStructure.
Definition: image.hpp:64
@ comment
Exiv2 type for the Exif user comment.
Definition: types.hpp:139
std::vector< byte > Blob
Container for binary data.
Definition: types.hpp:151
EXIV2API void append(Exiv2::Blob &blob, const byte *buf, uint32_t len)
Append len bytes pointed to by buf to blob.
Definition: image.cpp:1037
ByteOrder
Type to express the byte order (little or big endian)
Definition: types.hpp:102
bool(* IsThisTypeFct)(BasicIo &iIo, bool advance)
Type for function pointer that checks image types.
Definition: image.hpp:535
MetadataId
An identifier for each type of metadata.
Definition: types.hpp:108
TypeId getType()
Template to determine the TypeId for a type T.
AccessMode
An identifier for each mode of metadata support.
Definition: types.hpp:111
std::vector< NativePreview > NativePreviewList
List of native previews. This is meant to be used only by the PreviewManager.
Definition: image.hpp:59
Image::AutoPtr(* NewInstanceFct)(BasicIo::AutoPtr io, bool create)
Type for function pointer that creates new Image instances.
Definition: image.hpp:533
Native preview information. This is meant to be used only by the PreviewManager.
Definition: image.hpp:49
std::string filter_
Filter.
Definition: image.hpp:54
uint32_t width_
Width.
Definition: image.hpp:52
uint32_t size_
Size.
Definition: image.hpp:51
std::string mimeType_
MIME type.
Definition: image.hpp:55
uint32_t height_
Height.
Definition: image.hpp:53
long position_
Position.
Definition: image.hpp:50