ImfFrameBuffer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. //
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Copyright (c) Contributors to the OpenEXR Project.
  4. //
  5. #ifndef INCLUDED_IMF_FRAME_BUFFER_H
  6. #define INCLUDED_IMF_FRAME_BUFFER_H
  7. //-----------------------------------------------------------------------------
  8. //
  9. // class Slice
  10. // class FrameBuffer
  11. //
  12. //-----------------------------------------------------------------------------
  13. #include "ImfForward.h"
  14. #include "ImfName.h"
  15. #include "ImfPixelType.h"
  16. #include <ImathBox.h>
  17. #include <map>
  18. #include <string>
  19. #include <cstdint>
  20. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  21. //-------------------------------------------------------
  22. // Description of a single slice of the frame buffer:
  23. //
  24. // Note -- terminology: as part of a file, a component of
  25. // an image (e.g. red, green, blue, depth etc.) is called
  26. // a "channel". As part of a frame buffer, an image
  27. // component is called a "slice".
  28. //-------------------------------------------------------
  29. struct IMF_EXPORT_TYPE Slice
  30. {
  31. //------------------------------
  32. // Data type; see ImfPixelType.h
  33. //------------------------------
  34. PixelType type;
  35. //---------------------------------------------------------------------
  36. // Memory layout: The address of pixel (x, y) is
  37. //
  38. // base + (xp / xSampling) * xStride + (yp / ySampling) * yStride
  39. //
  40. // where xp and yp are computed as follows:
  41. //
  42. // * If we are reading or writing a scanline-based file:
  43. //
  44. // xp = x
  45. // yp = y
  46. //
  47. // * If we are reading a tile whose upper left coorner is at (xt, yt):
  48. //
  49. // if xTileCoords is true then xp = x - xt, else xp = x
  50. // if yTileCoords is true then yp = y - yt, else yp = y
  51. //
  52. //---------------------------------------------------------------------
  53. char * base;
  54. size_t xStride;
  55. size_t yStride;
  56. //--------------------------------------------
  57. // Subsampling: pixel (x, y) is present in the
  58. // slice only if
  59. //
  60. // x % xSampling == 0 && y % ySampling == 0
  61. //
  62. //--------------------------------------------
  63. int xSampling;
  64. int ySampling;
  65. //----------------------------------------------------------
  66. // Default value, used to fill the slice when a file without
  67. // a channel that corresponds to this slice is read.
  68. //----------------------------------------------------------
  69. double fillValue;
  70. //-------------------------------------------------------
  71. // For tiled files, the xTileCoords and yTileCoords flags
  72. // determine whether pixel addressing is performed using
  73. // absolute coordinates or coordinates relative to a
  74. // tile's upper left corner. (See the comment on base,
  75. // xStride and yStride, above.)
  76. //
  77. // For scanline-based files these flags have no effect;
  78. // pixel addressing is always done using absolute
  79. // coordinates.
  80. //-------------------------------------------------------
  81. bool xTileCoords;
  82. bool yTileCoords;
  83. //------------
  84. // Constructor
  85. //------------
  86. IMF_EXPORT
  87. Slice (PixelType type = HALF,
  88. char * base = 0,
  89. size_t xStride = 0,
  90. size_t yStride = 0,
  91. int xSampling = 1,
  92. int ySampling = 1,
  93. double fillValue = 0.0,
  94. bool xTileCoords = false,
  95. bool yTileCoords = false);
  96. // Does the heavy lifting of computing the base pointer for a slice,
  97. // avoiding overflow issues with large origin offsets
  98. //
  99. // if xStride == 0, assumes sizeof(pixeltype)
  100. // if yStride == 0, assumes xStride * ( w / xSampling )
  101. IMF_EXPORT
  102. static Slice Make(PixelType type,
  103. const void *ptr,
  104. const IMATH_NAMESPACE::V2i &origin,
  105. int64_t w,
  106. int64_t h,
  107. size_t xStride = 0,
  108. size_t yStride = 0,
  109. int xSampling = 1,
  110. int ySampling = 1,
  111. double fillValue = 0.0,
  112. bool xTileCoords = false,
  113. bool yTileCoords = false);
  114. // same as above, just computes w and h for you
  115. // from a data window
  116. IMF_EXPORT
  117. static Slice Make(PixelType type,
  118. const void *ptr,
  119. const IMATH_NAMESPACE::Box2i &dataWindow,
  120. size_t xStride = 0,
  121. size_t yStride = 0,
  122. int xSampling = 1,
  123. int ySampling = 1,
  124. double fillValue = 0.0,
  125. bool xTileCoords = false,
  126. bool yTileCoords = false);
  127. };
  128. class IMF_EXPORT_TYPE FrameBuffer
  129. {
  130. public:
  131. //------------
  132. // Add a slice
  133. //------------
  134. IMF_EXPORT
  135. void insert (const char name[],
  136. const Slice &slice);
  137. IMF_EXPORT
  138. void insert (const std::string &name,
  139. const Slice &slice);
  140. //----------------------------------------------------------------
  141. // Access to existing slices:
  142. //
  143. // [n] Returns a reference to the slice with name n.
  144. // If no slice with name n exists, an IEX_NAMESPACE::ArgExc
  145. // is thrown.
  146. //
  147. // findSlice(n) Returns a pointer to the slice with name n,
  148. // or 0 if no slice with name n exists.
  149. //
  150. //----------------------------------------------------------------
  151. IMF_EXPORT
  152. Slice & operator [] (const char name[]);
  153. IMF_EXPORT
  154. const Slice & operator [] (const char name[]) const;
  155. IMF_EXPORT
  156. Slice & operator [] (const std::string &name);
  157. IMF_EXPORT
  158. const Slice & operator [] (const std::string &name) const;
  159. IMF_EXPORT
  160. Slice * findSlice (const char name[]);
  161. IMF_EXPORT
  162. const Slice * findSlice (const char name[]) const;
  163. IMF_EXPORT
  164. Slice * findSlice (const std::string &name);
  165. IMF_EXPORT
  166. const Slice * findSlice (const std::string &name) const;
  167. //-----------------------------------------
  168. // Iterator-style access to existing slices
  169. //-----------------------------------------
  170. typedef std::map <Name, Slice> SliceMap;
  171. class Iterator;
  172. class ConstIterator;
  173. IMF_EXPORT
  174. Iterator begin ();
  175. IMF_EXPORT
  176. ConstIterator begin () const;
  177. IMF_EXPORT
  178. Iterator end ();
  179. IMF_EXPORT
  180. ConstIterator end () const;
  181. IMF_EXPORT
  182. Iterator find (const char name[]);
  183. IMF_EXPORT
  184. ConstIterator find (const char name[]) const;
  185. IMF_EXPORT
  186. Iterator find (const std::string &name);
  187. IMF_EXPORT
  188. ConstIterator find (const std::string &name) const;
  189. private:
  190. SliceMap _map;
  191. };
  192. //----------
  193. // Iterators
  194. //----------
  195. class IMF_EXPORT_TYPE FrameBuffer::Iterator
  196. {
  197. public:
  198. IMF_EXPORT
  199. Iterator ();
  200. IMF_EXPORT
  201. Iterator (const FrameBuffer::SliceMap::iterator &i);
  202. IMF_EXPORT
  203. Iterator & operator ++ ();
  204. IMF_EXPORT
  205. Iterator operator ++ (int);
  206. IMF_EXPORT
  207. const char * name () const;
  208. IMF_EXPORT
  209. Slice & slice () const;
  210. private:
  211. friend class FrameBuffer::ConstIterator;
  212. FrameBuffer::SliceMap::iterator _i;
  213. };
  214. class IMF_EXPORT_TYPE FrameBuffer::ConstIterator
  215. {
  216. public:
  217. IMF_EXPORT
  218. ConstIterator ();
  219. IMF_EXPORT
  220. ConstIterator (const FrameBuffer::SliceMap::const_iterator &i);
  221. IMF_EXPORT
  222. ConstIterator (const FrameBuffer::Iterator &other);
  223. IMF_EXPORT
  224. ConstIterator & operator ++ ();
  225. IMF_EXPORT
  226. ConstIterator operator ++ (int);
  227. IMF_EXPORT
  228. const char * name () const;
  229. IMF_EXPORT
  230. const Slice & slice () const;
  231. private:
  232. friend bool operator == (const ConstIterator &, const ConstIterator &);
  233. friend bool operator != (const ConstIterator &, const ConstIterator &);
  234. FrameBuffer::SliceMap::const_iterator _i;
  235. };
  236. //-----------------
  237. // Inline Functions
  238. //-----------------
  239. inline
  240. FrameBuffer::Iterator::Iterator (): _i()
  241. {
  242. // empty
  243. }
  244. inline
  245. FrameBuffer::Iterator::Iterator (const FrameBuffer::SliceMap::iterator &i):
  246. _i (i)
  247. {
  248. // empty
  249. }
  250. inline FrameBuffer::Iterator &
  251. FrameBuffer::Iterator::operator ++ ()
  252. {
  253. ++_i;
  254. return *this;
  255. }
  256. inline FrameBuffer::Iterator
  257. FrameBuffer::Iterator::operator ++ (int)
  258. {
  259. Iterator tmp = *this;
  260. ++_i;
  261. return tmp;
  262. }
  263. inline const char *
  264. FrameBuffer::Iterator::name () const
  265. {
  266. return *_i->first;
  267. }
  268. inline Slice &
  269. FrameBuffer::Iterator::slice () const
  270. {
  271. return _i->second;
  272. }
  273. inline
  274. FrameBuffer::ConstIterator::ConstIterator (): _i()
  275. {
  276. // empty
  277. }
  278. inline
  279. FrameBuffer::ConstIterator::ConstIterator
  280. (const FrameBuffer::SliceMap::const_iterator &i): _i (i)
  281. {
  282. // empty
  283. }
  284. inline
  285. FrameBuffer::ConstIterator::ConstIterator (const FrameBuffer::Iterator &other):
  286. _i (other._i)
  287. {
  288. // empty
  289. }
  290. inline FrameBuffer::ConstIterator &
  291. FrameBuffer::ConstIterator::operator ++ ()
  292. {
  293. ++_i;
  294. return *this;
  295. }
  296. inline FrameBuffer::ConstIterator
  297. FrameBuffer::ConstIterator::operator ++ (int)
  298. {
  299. ConstIterator tmp = *this;
  300. ++_i;
  301. return tmp;
  302. }
  303. inline const char *
  304. FrameBuffer::ConstIterator::name () const
  305. {
  306. return *_i->first;
  307. }
  308. inline const Slice &
  309. FrameBuffer::ConstIterator::slice () const
  310. {
  311. return _i->second;
  312. }
  313. inline bool
  314. operator == (const FrameBuffer::ConstIterator &x,
  315. const FrameBuffer::ConstIterator &y)
  316. {
  317. return x._i == y._i;
  318. }
  319. inline bool
  320. operator != (const FrameBuffer::ConstIterator &x,
  321. const FrameBuffer::ConstIterator &y)
  322. {
  323. return !(x == y);
  324. }
  325. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  326. #endif