Exiv2
xmp_exiv2.hpp
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #ifndef XMP_HPP_
4 #define XMP_HPP_
5 
6 // *****************************************************************************
7 #include "exiv2lib_export.h"
8 
9 // included header files
10 #include "metadatum.hpp"
11 #include "properties.hpp"
12 
13 // *****************************************************************************
14 // namespace extensions
15 namespace Exiv2 {
16 // *****************************************************************************
17 // class declarations
18 class ExifData;
19 
20 // *****************************************************************************
21 // class definitions
22 
27 class EXIV2API Xmpdatum : public Metadatum {
28  public:
30 
31 
43  explicit Xmpdatum(const XmpKey& key, const Value* pValue = nullptr);
45  Xmpdatum(const Xmpdatum& rhs);
47  ~Xmpdatum() override;
49 
51 
52  Xmpdatum& operator=(const Xmpdatum& rhs);
58  Xmpdatum& operator=(const std::string& value);
63  template <typename T>
64  Xmpdatum& operator=(const T& value);
69  Xmpdatum& operator=(const Value& value);
70  void setValue(const Value* pValue) override;
78  int setValue(const std::string& value) override;
80 
82 
83  size_t copy(byte* buf, ByteOrder byteOrder) const override;
85  std::ostream& write(std::ostream& os, const ExifData* pMetadata = nullptr) const override;
92  [[nodiscard]] std::string key() const override;
93  [[nodiscard]] const char* familyName() const override;
95  [[nodiscard]] std::string groupName() const override;
97  [[nodiscard]] std::string tagName() const override;
98  [[nodiscard]] std::string tagLabel() const override;
99  [[nodiscard]] std::string tagDesc() const override;
101  [[nodiscard]] uint16_t tag() const override;
102  [[nodiscard]] TypeId typeId() const override;
103  [[nodiscard]] const char* typeName() const override;
104  // Todo: Remove this method from the baseclass
106  [[nodiscard]] size_t typeSize() const override;
107  [[nodiscard]] size_t count() const override;
108  [[nodiscard]] size_t size() const override;
109  [[nodiscard]] std::string toString() const override;
110  [[nodiscard]] std::string toString(size_t n) const override;
111  [[nodiscard]] int64_t toInt64(size_t n = 0) const override;
112  [[nodiscard]] float toFloat(size_t n = 0) const override;
113  [[nodiscard]] Rational toRational(size_t n = 0) const override;
114  [[nodiscard]] Value::UniquePtr getValue() const override;
115  [[nodiscard]] const Value& value() const override;
117 
118  private:
119  // Pimpl idiom
120  struct Impl;
121  std::unique_ptr<Impl> p_;
122 
123 }; // class Xmpdatum
124 
126 using XmpMetadata = std::vector<Xmpdatum>;
127 
138 class EXIV2API XmpData {
139  public:
141  XmpData() = default;
142 
144  using iterator = XmpMetadata::iterator;
146  using const_iterator = XmpMetadata::const_iterator;
147 
149 
150 
158  Xmpdatum& operator[](const std::string& key);
164  int add(const XmpKey& key, const Value* value);
169  int add(const Xmpdatum& xmpdatum);
170  /*
171  @brief Delete the Xmpdatum at iterator position pos, return the
172  position of the next Xmpdatum.
173 
174  @note Iterators into the metadata, including pos, are potentially
175  invalidated by this call.
176  @brief Delete the Xmpdatum at iterator position pos and update pos
177  */
178  iterator erase(XmpData::iterator pos);
184  void eraseFamily(XmpData::iterator& pos);
186  void clear();
188  void sortByKey();
190  iterator begin();
192  iterator end();
197  iterator findKey(const XmpKey& key);
199 
201 
202  [[nodiscard]] const_iterator begin() const;
205  [[nodiscard]] const_iterator end() const;
210  [[nodiscard]] const_iterator findKey(const XmpKey& key) const;
212  [[nodiscard]] bool empty() const;
214  [[nodiscard]] long count() const;
215 
217  [[nodiscard]] bool usePacket() const {
218  return usePacket_;
219  }
220 
222  bool usePacket(bool b) {
223  bool r = usePacket_;
224  usePacket_ = b;
225  return r;
226  }
228  void setPacket(std::string xmpPacket) {
229  xmpPacket_ = std::move(xmpPacket);
230  usePacket(false);
231  }
232  // ! getPacket
233  [[nodiscard]] const std::string& xmpPacket() const {
234  return xmpPacket_;
235  }
236 
238 
239  private:
240  // DATA
241  XmpMetadata xmpMetadata_;
242  std::string xmpPacket_;
243  bool usePacket_{};
244 }; // class XmpData
245 
251 class EXIV2API XmpParser {
252  public:
255  omitPacketWrapper = 0x0010UL,
256  readOnlyPacket = 0x0020UL,
257  useCompactFormat = 0x0040UL,
258  includeThumbnailPad = 0x0100UL,
259  exactPacketLength = 0x0200UL,
260  writeAliasComments = 0x0400UL,
261  omitAllFormatting = 0x0800UL
262  };
276  static int decode(XmpData& xmpData, const std::string& xmpPacket);
294  static int encode(std::string& xmpPacket, const XmpData& xmpData, uint16_t formatFlags = useCompactFormat,
295  uint32_t padding = 0);
306  using XmpLockFct = void (*)(void* pLockData, bool lockUnlock);
307 
358  static bool initialize(XmpParser::XmpLockFct xmpLockFct = nullptr, void* pLockData = nullptr);
365  static void terminate();
366 
367  private:
371  static void registerNs(const std::string& ns, const std::string& prefix);
377  static void unregisterNs(const std::string& ns);
378 
382  static void registeredNamespaces(Exiv2::Dictionary&);
383 
384  // DATA
385  static bool initialized_;
386  static XmpLockFct xmpLockFct_;
387  static void* pLockData_;
388 
389  friend class XmpProperties; // permit XmpProperties -> registerNs() and registeredNamespaces()
390 
391 }; // class XmpParser
392 
393 // *****************************************************************************
394 // free functions, template and inline definitions
395 
396 template <typename T>
397 Xmpdatum& Xmpdatum::operator=(const T& value) {
398 #ifdef __cpp_if_constexpr
399  if constexpr (std::is_same_v<T, bool>) {
400 #else
401  if (std::is_same<T, bool>::value) {
402 #endif
403  setValue(Exiv2::toString(value ? "True" : "False"));
404  return *this;
405  } else {
406  setValue(Exiv2::toString(value));
407  return *this;
408  }
409 }
410 
411 } // namespace Exiv2
412 
413 #endif // #ifndef XMP_HPP_
XmpMetadata::iterator iterator
XmpMetadata iterator type.
Definition: xmp_exiv2.hpp:144
void setPacket(std::string xmpPacket)
setPacket
Definition: xmp_exiv2.hpp:228
XmpMetadata::const_iterator const_iterator
XmpMetadata const iterator type.
Definition: xmp_exiv2.hpp:146
A container for XMP data. This is a top-level class of the Exiv2 library.
Definition: xmp_exiv2.hpp:138
A container for Exif data. This is a top-level class of the Exiv2 library. The container holds Exifda...
Definition: exif.hpp:373
ByteOrder
Type to express the byte order (little or big endian)
Definition: types.hpp:34
T getValue(const byte *buf, ByteOrder byteOrder)
Read a value of type T from the data buffer.
Definition: value.hpp:1332
TypeId
Exiv2 value type identifiers.
Definition: types.hpp:70
bool usePacket(bool b)
set usePacket_
Definition: xmp_exiv2.hpp:222
std::map< std::string, std::string > Dictionary
typedef for string:string map
Definition: datasets.hpp:312
XmpFormatFlags
Options to control the format of the serialized XMP packet.
Definition: xmp_exiv2.hpp:254
uint8_t byte
1 byte unsigned integer type.
Definition: types.hpp:26
std::string toString(const T &arg)
Utility function to convert the argument of any type to a string.
Definition: types.hpp:456
Information related to an XMP property. An XMP metadatum consists of an XmpKey and a Value and provid...
Definition: xmp_exiv2.hpp:27
XMP property reference, implemented as a static class.
Definition: properties.hpp:64
std::pair< int32_t, int32_t > Rational
8 byte signed rational type.
Definition: types.hpp:31
void(*)(void *pLockData, bool lockUnlock) XmpLockFct
Lock/unlock function type.
Definition: xmp_exiv2.hpp:306
Internal Pimpl structure of class Xmpdatum.
Definition: xmp.cpp:257
Stateless parser class for XMP packets. Images use this class to parse and serialize XMP packets...
Definition: xmp_exiv2.hpp:251
std::vector< Xmpdatum > XmpMetadata
Container type to hold all metadata.
Definition: xmp_exiv2.hpp:126
Concrete keys for XMP metadata.
Definition: properties.hpp:207
Exiv2::Exifdatum & setValue(Exiv2::Exifdatum &exifDatum, const T &value)
Set the value of exifDatum to value. If the object already has a value, it is replaced. Otherwise a new ValueType value is created and set to value.
Definition: exif.cpp:147
const char * groupName(IfdId ifdId)
Return the group name for a group id.
Definition: tags_int.cpp:2406
Common interface for all types of values used with metadata.
Definition: value.hpp:33
Class CrwImage to access Canon CRW images. References: The Canon RAW (CRW) File Format by Phil Harv...
Definition: asfvideo.hpp:15
void setValue(const Value *pValue) override
Set the value. This method copies (clones) the value pointed to by pValue.
Definition: xmp.cpp:404
Abstract base class defining the interface to access information related to one metadata tag...
Definition: metadatum.hpp:103
bool usePacket() const
are we to use the packet?
Definition: xmp_exiv2.hpp:217
std::unique_ptr< Value > UniquePtr
Shortcut for a Value auto pointer.
Definition: value.hpp:36
Xmpdatum & operator=(const Xmpdatum &rhs)
Assignment operator.
Definition: xmp.cpp:298