HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImfHeader.h
Go to the documentation of this file.
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright (c) Contributors to the OpenEXR Project.
4 //
5 
6 #ifndef INCLUDED_IMF_HEADER_H
7 #define INCLUDED_IMF_HEADER_H
8 
9 //-----------------------------------------------------------------------------
10 //
11 // class Header
12 //
13 //-----------------------------------------------------------------------------
14 
15 #include "ImfForward.h"
16 
17 #include "IexBaseExc.h"
18 #include "ImathBox.h"
19 #include "ImathVec.h"
20 #include "ImfCompression.h"
21 #include "ImfLineOrder.h"
22 #include "ImfName.h"
23 #include "ImfTileDescription.h"
24 
25 #include "ImfAttribute.h"
26 
27 #include <cstdint>
28 #include <iosfwd>
29 #include <map>
30 #include <string>
31 
33 
34 using std::string;
35 
37 {
38 public:
39  //----------------------------------------------------------------
40  // Default constructor -- the display window and the data window
41  // are both set to Box2i (V2i (0, 0), V2i (width-1, height-1).
42  //----------------------------------------------------------------
43 
45  Header (
46  int width = 64,
47  int height = 64,
48  float pixelAspectRatio = 1,
49  const IMATH_NAMESPACE::V2f& screenWindowCenter =
50  IMATH_NAMESPACE::V2f (0, 0),
51  float screenWindowWidth = 1,
52  LineOrder lineOrder = INCREASING_Y,
54 
55  //--------------------------------------------------------------------
56  // Constructor -- the data window is specified explicitly; the display
57  // window is set to Box2i (V2i (0, 0), V2i (width-1, height-1).
58  //--------------------------------------------------------------------
59 
61  Header (
62  int width,
63  int height,
64  const IMATH_NAMESPACE::Box2i& dataWindow,
65  float pixelAspectRatio = 1,
66  const IMATH_NAMESPACE::V2f& screenWindowCenter =
67  IMATH_NAMESPACE::V2f (0, 0),
68  float screenWindowWidth = 1,
69  LineOrder lineOrder = INCREASING_Y,
71 
72  //----------------------------------------------------------
73  // Constructor -- the display window and the data window are
74  // both specified explicitly.
75  //----------------------------------------------------------
76 
78  Header (
79  const IMATH_NAMESPACE::Box2i& displayWindow,
80  const IMATH_NAMESPACE::Box2i& dataWindow,
81  float pixelAspectRatio = 1,
82  const IMATH_NAMESPACE::V2f& screenWindowCenter =
83  IMATH_NAMESPACE::V2f (0, 0),
84  float screenWindowWidth = 1,
85  LineOrder lineOrder = INCREASING_Y,
87 
88  //-----------------
89  // Copy constructor
90  //-----------------
91 
93  Header (const Header& other);
95  Header (Header&& other);
96 
97  //-----------
98  // Destructor
99  //-----------
100 
101  IMF_EXPORT
102  ~Header ();
103 
104  //-----------
105  // Assignment
106  //-----------
107 
108  IMF_EXPORT
109  Header& operator= (const Header& other);
110  IMF_EXPORT
111  Header& operator= (Header&& other);
112 
113  //---------------------------------------------------------------
114  // Add an attribute:
115  //
116  // insert(n,attr) If no attribute with name n exists, a new
117  // attribute with name n, and the same type as
118  // attr, is added, and the value of attr is
119  // copied into the new attribute.
120  //
121  // If an attribute with name n exists, and its
122  // type is the same as attr, the value of attr
123  // is copied into this attribute.
124  //
125  // If an attribute with name n exists, and its
126  // type is different from attr, an IEX_NAMESPACE::TypeExc
127  // is thrown.
128  //
129  //---------------------------------------------------------------
130 
131  IMF_EXPORT
132  void insert (const char name[], const Attribute& attribute);
133 
134  IMF_EXPORT
135  void insert (const std::string& name, const Attribute& attribute);
136 
137  //---------------------------------------------------------------
138  // Remove an attribute:
139  //
140  // remove(n) If an attribute with name n exists, then it
141  // is removed from the map of present attributes.
142  //
143  // If no attribute with name n exists, then this
144  // functions becomes a 'no-op'
145  //
146  //---------------------------------------------------------------
147 
148  IMF_EXPORT
149  void erase (const char name[]);
150  IMF_EXPORT
151  void erase (const std::string& name);
152 
153  //------------------------------------------------------------------
154  // Access to existing attributes:
155  //
156  // [n] Returns a reference to the attribute
157  // with name n. If no attribute with
158  // name n exists, an IEX_NAMESPACE::ArgExc is thrown.
159  //
160  // typedAttribute<T>(n) Returns a reference to the attribute
161  // with name n and type T. If no attribute
162  // with name n exists, an IEX_NAMESPACE::ArgExc is
163  // thrown. If an attribute with name n
164  // exists, but its type is not T, an
165  // IEX_NAMESPACE::TypeExc is thrown.
166  //
167  // findTypedAttribute<T>(n) Returns a pointer to the attribute with
168  // name n and type T, or 0 if no attribute
169  // with name n and type T exists.
170  //
171  //------------------------------------------------------------------
172 
173  IMF_EXPORT
174  Attribute& operator[] (const char name[]);
175  IMF_EXPORT
176  const Attribute& operator[] (const char name[]) const;
177 
178  IMF_EXPORT
179  Attribute& operator[] (const std::string& name);
180  IMF_EXPORT
181  const Attribute& operator[] (const std::string& name) const;
182 
183  template <class T> T& typedAttribute (const char name[]);
184  template <class T> const T& typedAttribute (const char name[]) const;
185 
186  template <class T> T& typedAttribute (const std::string& name);
187  template <class T> const T& typedAttribute (const std::string& name) const;
188 
189  template <class T> T* findTypedAttribute (const char name[]);
190  template <class T> const T* findTypedAttribute (const char name[]) const;
191 
192  template <class T> T* findTypedAttribute (const std::string& name);
193  template <class T>
194  const T* findTypedAttribute (const std::string& name) const;
195 
196  //---------------------------------------------
197  // Iterator-style access to existing attributes
198  //---------------------------------------------
199 
200  typedef std::map<Name, Attribute*> AttributeMap;
201 
202  class Iterator;
203  class ConstIterator;
204 
205  IMF_EXPORT
206  Iterator begin ();
207  IMF_EXPORT
208  ConstIterator begin () const;
209 
210  IMF_EXPORT
211  Iterator end ();
212  IMF_EXPORT
213  ConstIterator end () const;
214 
215  IMF_EXPORT
216  Iterator find (const char name[]);
217  IMF_EXPORT
218  ConstIterator find (const char name[]) const;
219 
220  IMF_EXPORT
221  Iterator find (const std::string& name);
222  IMF_EXPORT
223  ConstIterator find (const std::string& name) const;
224 
225  //--------------------------------
226  // Access to predefined attributes
227  //--------------------------------
228 
229  IMF_EXPORT
230  IMATH_NAMESPACE::Box2i& displayWindow ();
231  IMF_EXPORT
232  const IMATH_NAMESPACE::Box2i& displayWindow () const;
233 
234  IMF_EXPORT
235  IMATH_NAMESPACE::Box2i& dataWindow ();
236  IMF_EXPORT
237  const IMATH_NAMESPACE::Box2i& dataWindow () const;
238 
239  IMF_EXPORT
240  float& pixelAspectRatio ();
241  IMF_EXPORT
242  const float& pixelAspectRatio () const;
243 
244  IMF_EXPORT
245  IMATH_NAMESPACE::V2f& screenWindowCenter ();
246  IMF_EXPORT
247  const IMATH_NAMESPACE::V2f& screenWindowCenter () const;
248 
249  IMF_EXPORT
250  float& screenWindowWidth ();
251  IMF_EXPORT
252  const float& screenWindowWidth () const;
253 
254  IMF_EXPORT
255  ChannelList& channels ();
256  IMF_EXPORT
257  const ChannelList& channels () const;
258 
259  IMF_EXPORT
260  LineOrder& lineOrder ();
261  IMF_EXPORT
262  const LineOrder& lineOrder () const;
263 
264  IMF_EXPORT
265  Compression& compression ();
266  IMF_EXPORT
267  const Compression& compression () const;
268 
269  //-----------------------------------------------------
270  // The header object allows one to store a compression level to be
271  // used when writing a file.
272  //
273  // NB: These are NOT attributes, and will not be written to the
274  // file, but are instead ephemeral settings to be used for this
275  // instance of the header object.
276  //
277  // -----------------------------------------------------
278  IMF_EXPORT
279  void resetDefaultCompressionLevels ();
280  IMF_EXPORT
281  int& zipCompressionLevel ();
282  IMF_EXPORT
283  int zipCompressionLevel () const;
284  IMF_EXPORT
285  float& dwaCompressionLevel ();
286  IMF_EXPORT
287  float dwaCompressionLevel () const;
288 
289  //-----------------------------------------------------
290  // Access to required attributes for multipart files
291  // They are optional to non-multipart files and mandatory
292  // for multipart files.
293  //-----------------------------------------------------
294  IMF_EXPORT
295  void setName (const string& name);
296 
297  IMF_EXPORT
298  string& name ();
299  IMF_EXPORT
300  const string& name () const;
301 
302  IMF_EXPORT
303  bool hasName () const;
304 
305  IMF_EXPORT
306  void setType (const string& Type);
307 
308  IMF_EXPORT
309  string& type ();
310  IMF_EXPORT
311  const string& type () const;
312 
313  IMF_EXPORT
314  bool hasType () const;
315 
316  IMF_EXPORT
317  void setVersion (const int version);
318 
319  IMF_EXPORT
320  int& version ();
321  IMF_EXPORT
322  const int& version () const;
323 
324  IMF_EXPORT
325  bool hasVersion () const;
326 
327  //
328  // the chunkCount attribute is set automatically when a file is written.
329  // There is no need to set it manually
330  //
331  IMF_EXPORT
332  void setChunkCount (int chunks);
333  IMF_EXPORT
334  bool hasChunkCount () const;
335  IMF_EXPORT
336  const int& chunkCount () const;
337  IMF_EXPORT
338  int& chunkCount ();
339 
340  //
341  // for multipart files, return whether the file has a view string attribute
342  // (for the deprecated single part multiview format EXR, see ImfMultiView.h)
343  //
344  IMF_EXPORT
345  void setView (const string& view);
346  IMF_EXPORT
347  bool hasView () const;
348  IMF_EXPORT
349  string& view ();
350  IMF_EXPORT
351  const string& view () const;
352 
353  //----------------------------------------------------------------------
354  // Tile Description:
355  //
356  // The tile description is a TileDescriptionAttribute whose name
357  // is "tiles". The "tiles" attribute must be present in any tiled
358  // image file. When present, it describes various properties of the
359  // tiles that make up the file.
360  //
361  // Convenience functions:
362  //
363  // setTileDescription(td)
364  // calls insert ("tiles", TileDescriptionAttribute (td))
365  //
366  // tileDescription()
367  // returns typedAttribute<TileDescriptionAttribute>("tiles").value()
368  //
369  // hasTileDescription()
370  // return findTypedAttribute<TileDescriptionAttribute>("tiles") != 0
371  //
372  //----------------------------------------------------------------------
373 
374  IMF_EXPORT
375  void setTileDescription (const TileDescription& td);
376 
377  IMF_EXPORT
378  TileDescription& tileDescription ();
379  IMF_EXPORT
380  const TileDescription& tileDescription () const;
381 
382  IMF_EXPORT
383  bool hasTileDescription () const;
384 
385  //----------------------------------------------------------------------
386  // Preview image:
387  //
388  // The preview image is a PreviewImageAttribute whose name is "preview".
389  // This attribute is special -- while an image file is being written,
390  // the pixels of the preview image can be changed repeatedly by calling
391  // OutputFile::updatePreviewImage().
392  //
393  // Convenience functions:
394  //
395  // setPreviewImage(p)
396  // calls insert ("preview", PreviewImageAttribute (p))
397  //
398  // previewImage()
399  // returns typedAttribute<PreviewImageAttribute>("preview").value()
400  //
401  // hasPreviewImage()
402  // return findTypedAttribute<PreviewImageAttribute>("preview") != 0
403  //
404  //----------------------------------------------------------------------
405 
406  IMF_EXPORT
407  void setPreviewImage (const PreviewImage& p);
408 
409  IMF_EXPORT
410  PreviewImage& previewImage ();
411  IMF_EXPORT
412  const PreviewImage& previewImage () const;
413 
414  IMF_EXPORT
415  bool hasPreviewImage () const;
416 
417  //-------------------------------------------------------------
418  // Sanity check -- examines the header, and throws an exception
419  // if it finds something wrong (empty display window, negative
420  // pixel aspect ratio, unknown compression scheme etc...)
421  //
422  // set isTiled to true if you are checking a tiled/multi-res
423  // header
424  //-------------------------------------------------------------
425 
426  IMF_EXPORT
427  void sanityCheck (bool isTiled = false, bool isMultipartFile = false) const;
428 
429  //----------------------------------------------------------------
430  // Maximum image size and maximum tile size:
431  //
432  // sanityCheck() will throw an exception if the width or height of
433  // the data window exceeds the maximum image width or height, or
434  // if the size of a tile exceeds the maximum tile width or height.
435  //
436  // At program startup the maximum image and tile width and height
437  // are set to zero, meaning that width and height are unlimited.
438  //
439  // Limiting image and tile width and height limits how much memory
440  // will be allocated when a file is opened. This can help protect
441  // applications from running out of memory while trying to read
442  // a damaged image file.
443  //----------------------------------------------------------------
444 
445  IMF_EXPORT
446  static void setMaxImageSize (int maxWidth, int maxHeight);
447  IMF_EXPORT
448  static void setMaxTileSize (int maxWidth, int maxHeight);
449  IMF_EXPORT
450  static void getMaxImageSize (int& maxWidth, int& maxHeight);
451  IMF_EXPORT
452  static void getMaxTileSize (int& maxWidth, int& maxHeight);
453 
454 
455  //
456  // Check if the header reads nothing.
457  //
458  IMF_EXPORT
459  bool readsNothing ();
460 
461  //------------------------------------------------------------------
462  // Input and output:
463  //
464  // If the header contains a preview image attribute, then writeTo()
465  // returns the position of that attribute in the output stream; this
466  // information is used by OutputFile::updatePreviewImage().
467  // If the header contains no preview image attribute, then writeTo()
468  // returns 0.
469  //------------------------------------------------------------------
470 
471  IMF_EXPORT
472  uint64_t writeTo (
474  bool isTiled = false) const;
475 
476  IMF_EXPORT
477  void readFrom (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream& is, int& version);
478 
479 private:
480  AttributeMap _map;
481 
482  bool _readsNothing;
483 };
484 
485 //----------
486 // Iterators
487 //----------
488 
489 class IMF_EXPORT_TYPE Header::Iterator
490 {
491 public:
492  IMF_EXPORT
493  Iterator ();
494  IMF_EXPORT
495  Iterator (const Header::AttributeMap::iterator& i);
496 
497  IMF_EXPORT
498  Iterator& operator++ ();
499  IMF_EXPORT
500  Iterator operator++ (int);
501 
502  IMF_EXPORT
503  const char* name () const;
504  IMF_EXPORT
505  Attribute& attribute () const;
506 
507 private:
508  friend class Header::ConstIterator;
509 
510  Header::AttributeMap::iterator _i;
511 };
512 
513 class IMF_EXPORT_TYPE Header::ConstIterator
514 {
515 public:
516  IMF_EXPORT
517  ConstIterator ();
518  IMF_EXPORT
519  ConstIterator (const Header::AttributeMap::const_iterator& i);
520  IMF_EXPORT
521  ConstIterator (const Header::Iterator& other);
522 
523  IMF_EXPORT
524  ConstIterator& operator++ ();
525  IMF_EXPORT
526  ConstIterator operator++ (int);
527 
528  IMF_EXPORT
529  const char* name () const;
530  IMF_EXPORT
531  const Attribute& attribute () const;
532 
533 private:
534  friend bool operator== (const ConstIterator&, const ConstIterator&);
535  friend bool operator!= (const ConstIterator&, const ConstIterator&);
536 
537  Header::AttributeMap::const_iterator _i;
538 };
539 
540 //------------------------------------------------------------------------
541 // Library initialization:
542 //
543 // In a multithreaded program, staticInitialize() must be called once
544 // during startup, before the program accesses any other functions or
545 // classes in the OpenEXR library. Calling staticInitialize() in this
546 // way avoids races during initialization of the library's global
547 // variables.
548 //
549 // Single-threaded programs are not required to call staticInitialize();
550 // initialization of the library's global variables happens automatically.
551 //
552 //------------------------------------------------------------------------
553 
555 
556 //-----------------
557 // Inline Functions
558 //-----------------
559 
560 inline Header::Iterator::Iterator () : _i ()
561 {
562  // empty
563 }
564 
565 inline Header::Iterator::Iterator (const Header::AttributeMap::iterator& i)
566  : _i (i)
567 {
568  // empty
569 }
570 
571 inline Header::Iterator&
572 Header::Iterator::operator++ ()
573 {
574  ++_i;
575  return *this;
576 }
577 
578 inline Header::Iterator
579 Header::Iterator::operator++ (int)
580 {
581  Iterator tmp = *this;
582  ++_i;
583  return tmp;
584 }
585 
586 inline const char*
587 Header::Iterator::name () const
588 {
589  return *_i->first;
590 }
591 
592 inline Attribute&
594 {
595  return *_i->second;
596 }
597 
598 inline Header::ConstIterator::ConstIterator () : _i ()
599 {
600  // empty
601 }
602 
603 inline Header::ConstIterator::ConstIterator (
604  const Header::AttributeMap::const_iterator& i)
605  : _i (i)
606 {
607  // empty
608 }
609 
610 inline Header::ConstIterator::ConstIterator (const Header::Iterator& other)
611  : _i (other._i)
612 {
613  // empty
614 }
615 
616 inline Header::ConstIterator&
617 Header::ConstIterator::operator++ ()
618 {
619  ++_i;
620  return *this;
621 }
622 
624 Header::ConstIterator::operator++ (int)
625 {
626  ConstIterator tmp = *this;
627  ++_i;
628  return tmp;
629 }
630 
631 inline const char*
633 {
634  return *_i->first;
635 }
636 
637 inline const Attribute&
639 {
640  return *_i->second;
641 }
642 
643 inline bool
645 {
646  return x._i == y._i;
647 }
648 
649 inline bool
651 {
652  return !(x == y);
653 }
654 
655 //---------------------
656 // Template definitions
657 //---------------------
658 
659 template <class T>
660 T&
662 {
663  Attribute* attr = &(*this)[name];
664  T* tattr = dynamic_cast<T*> (attr);
665 
666  if (tattr == 0) throw IEX_NAMESPACE::TypeExc ("Unexpected attribute type.");
667 
668  return *tattr;
669 }
670 
671 template <class T>
672 const T&
673 Header::typedAttribute (const char name[]) const
674 {
675  const Attribute* attr = &(*this)[name];
676  const T* tattr = dynamic_cast<const T*> (attr);
677 
678  if (tattr == 0) throw IEX_NAMESPACE::TypeExc ("Unexpected attribute type.");
679 
680  return *tattr;
681 }
682 
683 template <class T>
684 T&
686 {
687  return typedAttribute<T> (name.c_str ());
688 }
689 
690 template <class T>
691 const T&
693 {
694  return typedAttribute<T> (name.c_str ());
695 }
696 
697 template <class T>
698 T*
700 {
701  AttributeMap::iterator i = _map.find (name);
702  return (i == _map.end ()) ? 0 : dynamic_cast<T*> (i->second);
703 }
704 
705 template <class T>
706 const T*
707 Header::findTypedAttribute (const char name[]) const
708 {
709  AttributeMap::const_iterator i = _map.find (name);
710  return (i == _map.end ()) ? 0 : dynamic_cast<const T*> (i->second);
711 }
712 
713 template <class T>
714 T*
716 {
717  return findTypedAttribute<T> (name.c_str ());
718 }
719 
720 template <class T>
721 const T*
723 {
724  return findTypedAttribute<T> (name.c_str ());
725 }
726 
728 
729 #endif
IMF_EXPORT Iterator()
ZIP_COMPRESSION
#define OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
Definition: ImfNamespace.h:83
T & typedAttribute(const char name[])
Definition: ImfHeader.h:661
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER enum IMF_EXPORT_ENUM Compression
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER enum IMF_EXPORT_ENUM LineOrder
Definition: ImfLineOrder.h:19
GLint y
Definition: glcorearb.h:103
OIIO_FORCEINLINE vbool4 insert(const vbool4 &a, bool val)
Helper: substitute val for a[i].
Definition: simd.h:3436
IMF_EXPORT string & name()
T * findTypedAttribute(const char name[])
Definition: ImfHeader.h:699
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
class IMF_EXPORT_TYPE Header
Definition: ImfForward.h:30
IMF_EXPORT bool isTiled(const std::string &name)
Box< V2i > Box2i
2D box of base type int.
Definition: ImathBox.h:143
IMF_EXPORT ConstIterator()
GLuint GLuint end
Definition: glcorearb.h:475
OPENVDB_API void setVersion(std::ios_base &, const VersionId &libraryVersion, uint32_t fileVersion)
Associate specific file format and library version numbers with the given stream. ...
bool operator!=(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Inequality operator, does exact floating point comparisons.
Definition: Mat3.h:556
#define IMF_EXPORT
Definition: ImfExport.h:54
GLuint const GLchar * name
Definition: glcorearb.h:786
bool operator!=(const Header::ConstIterator &x, const Header::ConstIterator &y)
Definition: ImfHeader.h:650
HUSD_API const char * pixelAspectRatio()
class IMF_EXPORT_TYPE OStream
Definition: ImfForward.h:86
GLint GLenum GLint x
Definition: glcorearb.h:409
std::map< Name, Attribute * > AttributeMap
Definition: ImfHeader.h:200
bool operator==(const Header::ConstIterator &x, const Header::ConstIterator &y)
Definition: ImfHeader.h:644
GT_API const UT_StringHolder version
Vec2< float > V2f
Vec2 of float.
Definition: ImathVec.h:834
LeafData & operator=(const LeafData &)=delete
IMF_EXPORT void staticInitialize()
OIIO_API bool attribute(string_view name, TypeDesc type, const void *val)
#define OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
Definition: ImfNamespace.h:80
GLint GLsizei width
Definition: glcorearb.h:103
Definition: core.h:982
INCREASING_Y
Definition: ImfLineOrder.h:21
type
Definition: core.h:1059
#define IMF_EXPORT_TYPE
Definition: ImfExport.h:57
bool operator==(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Equality operator, does exact floating point comparisons.
Definition: Mat3.h:542
ImageBuf OIIO_API channels(const ImageBuf &src, int nchannels, cspan< int > channelorder, cspan< float > channelvalues={}, cspan< std::string > newchannelnames={}, bool shuffle_channel_names=false, int nthreads=0)
class IMF_EXPORT_TYPE IStream
Definition: ImfForward.h:87
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2089
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:558