ImfFrameBuffer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. #ifndef INCLUDED_IMF_FRAME_BUFFER_H
  35. #define INCLUDED_IMF_FRAME_BUFFER_H
  36. //-----------------------------------------------------------------------------
  37. //
  38. // class Slice
  39. // class FrameBuffer
  40. //
  41. //-----------------------------------------------------------------------------
  42. #include "ImfName.h"
  43. #include "ImfPixelType.h"
  44. #include "ImfExport.h"
  45. #include "ImfNamespace.h"
  46. #include <map>
  47. #include <string>
  48. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  49. //-------------------------------------------------------
  50. // Description of a single slice of the frame buffer:
  51. //
  52. // Note -- terminology: as part of a file, a component of
  53. // an image (e.g. red, green, blue, depth etc.) is called
  54. // a "channel". As part of a frame buffer, an image
  55. // component is called a "slice".
  56. //-------------------------------------------------------
  57. struct Slice
  58. {
  59. //------------------------------
  60. // Data type; see ImfPixelType.h
  61. //------------------------------
  62. PixelType type;
  63. //---------------------------------------------------------------------
  64. // Memory layout: The address of pixel (x, y) is
  65. //
  66. // base + (xp / xSampling) * xStride + (yp / ySampling) * yStride
  67. //
  68. // where xp and yp are computed as follows:
  69. //
  70. // * If we are reading or writing a scanline-based file:
  71. //
  72. // xp = x
  73. // yp = y
  74. //
  75. // * If we are reading a tile whose upper left coorner is at (xt, yt):
  76. //
  77. // if xTileCoords is true then xp = x - xt, else xp = x
  78. // if yTileCoords is true then yp = y - yt, else yp = y
  79. //
  80. //---------------------------------------------------------------------
  81. char * base;
  82. size_t xStride;
  83. size_t yStride;
  84. //--------------------------------------------
  85. // Subsampling: pixel (x, y) is present in the
  86. // slice only if
  87. //
  88. // x % xSampling == 0 && y % ySampling == 0
  89. //
  90. //--------------------------------------------
  91. int xSampling;
  92. int ySampling;
  93. //----------------------------------------------------------
  94. // Default value, used to fill the slice when a file without
  95. // a channel that corresponds to this slice is read.
  96. //----------------------------------------------------------
  97. double fillValue;
  98. //-------------------------------------------------------
  99. // For tiled files, the xTileCoords and yTileCoords flags
  100. // determine whether pixel addressing is performed using
  101. // absolute coordinates or coordinates relative to a
  102. // tile's upper left corner. (See the comment on base,
  103. // xStride and yStride, above.)
  104. //
  105. // For scanline-based files these flags have no effect;
  106. // pixel addressing is always done using absolute
  107. // coordinates.
  108. //-------------------------------------------------------
  109. bool xTileCoords;
  110. bool yTileCoords;
  111. //------------
  112. // Constructor
  113. //------------
  114. IMF_EXPORT
  115. Slice (PixelType type = HALF,
  116. char * base = 0,
  117. size_t xStride = 0,
  118. size_t yStride = 0,
  119. int xSampling = 1,
  120. int ySampling = 1,
  121. double fillValue = 0.0,
  122. bool xTileCoords = false,
  123. bool yTileCoords = false);
  124. };
  125. class FrameBuffer
  126. {
  127. public:
  128. //------------
  129. // Add a slice
  130. //------------
  131. IMF_EXPORT
  132. void insert (const char name[],
  133. const Slice &slice);
  134. IMF_EXPORT
  135. void insert (const std::string &name,
  136. const Slice &slice);
  137. //----------------------------------------------------------------
  138. // Access to existing slices:
  139. //
  140. // [n] Returns a reference to the slice with name n.
  141. // If no slice with name n exists, an IEX_NAMESPACE::ArgExc
  142. // is thrown.
  143. //
  144. // findSlice(n) Returns a pointer to the slice with name n,
  145. // or 0 if no slice with name n exists.
  146. //
  147. //----------------------------------------------------------------
  148. IMF_EXPORT
  149. Slice & operator [] (const char name[]);
  150. IMF_EXPORT
  151. const Slice & operator [] (const char name[]) const;
  152. IMF_EXPORT
  153. Slice & operator [] (const std::string &name);
  154. IMF_EXPORT
  155. const Slice & operator [] (const std::string &name) const;
  156. IMF_EXPORT
  157. Slice * findSlice (const char name[]);
  158. IMF_EXPORT
  159. const Slice * findSlice (const char name[]) const;
  160. IMF_EXPORT
  161. Slice * findSlice (const std::string &name);
  162. IMF_EXPORT
  163. const Slice * findSlice (const std::string &name) const;
  164. //-----------------------------------------
  165. // Iterator-style access to existing slices
  166. //-----------------------------------------
  167. typedef std::map <Name, Slice> SliceMap;
  168. class Iterator;
  169. class ConstIterator;
  170. IMF_EXPORT
  171. Iterator begin ();
  172. IMF_EXPORT
  173. ConstIterator begin () const;
  174. IMF_EXPORT
  175. Iterator end ();
  176. IMF_EXPORT
  177. ConstIterator end () const;
  178. IMF_EXPORT
  179. Iterator find (const char name[]);
  180. IMF_EXPORT
  181. ConstIterator find (const char name[]) const;
  182. IMF_EXPORT
  183. Iterator find (const std::string &name);
  184. IMF_EXPORT
  185. ConstIterator find (const std::string &name) const;
  186. private:
  187. SliceMap _map;
  188. };
  189. //----------
  190. // Iterators
  191. //----------
  192. class FrameBuffer::Iterator
  193. {
  194. public:
  195. IMF_EXPORT
  196. Iterator ();
  197. IMF_EXPORT
  198. Iterator (const FrameBuffer::SliceMap::iterator &i);
  199. IMF_EXPORT
  200. Iterator & operator ++ ();
  201. IMF_EXPORT
  202. Iterator operator ++ (int);
  203. IMF_EXPORT
  204. const char * name () const;
  205. IMF_EXPORT
  206. Slice & slice () const;
  207. private:
  208. friend class FrameBuffer::ConstIterator;
  209. FrameBuffer::SliceMap::iterator _i;
  210. };
  211. class FrameBuffer::ConstIterator
  212. {
  213. public:
  214. IMF_EXPORT
  215. ConstIterator ();
  216. IMF_EXPORT
  217. ConstIterator (const FrameBuffer::SliceMap::const_iterator &i);
  218. IMF_EXPORT
  219. ConstIterator (const FrameBuffer::Iterator &other);
  220. IMF_EXPORT
  221. ConstIterator & operator ++ ();
  222. IMF_EXPORT
  223. ConstIterator operator ++ (int);
  224. IMF_EXPORT
  225. const char * name () const;
  226. IMF_EXPORT
  227. const Slice & slice () const;
  228. private:
  229. friend bool operator == (const ConstIterator &, const ConstIterator &);
  230. friend bool operator != (const ConstIterator &, const ConstIterator &);
  231. FrameBuffer::SliceMap::const_iterator _i;
  232. };
  233. //-----------------
  234. // Inline Functions
  235. //-----------------
  236. inline
  237. FrameBuffer::Iterator::Iterator (): _i()
  238. {
  239. // empty
  240. }
  241. inline
  242. FrameBuffer::Iterator::Iterator (const FrameBuffer::SliceMap::iterator &i):
  243. _i (i)
  244. {
  245. // empty
  246. }
  247. inline FrameBuffer::Iterator &
  248. FrameBuffer::Iterator::operator ++ ()
  249. {
  250. ++_i;
  251. return *this;
  252. }
  253. inline FrameBuffer::Iterator
  254. FrameBuffer::Iterator::operator ++ (int)
  255. {
  256. Iterator tmp = *this;
  257. ++_i;
  258. return tmp;
  259. }
  260. inline const char *
  261. FrameBuffer::Iterator::name () const
  262. {
  263. return *_i->first;
  264. }
  265. inline Slice &
  266. FrameBuffer::Iterator::slice () const
  267. {
  268. return _i->second;
  269. }
  270. inline
  271. FrameBuffer::ConstIterator::ConstIterator (): _i()
  272. {
  273. // empty
  274. }
  275. inline
  276. FrameBuffer::ConstIterator::ConstIterator
  277. (const FrameBuffer::SliceMap::const_iterator &i): _i (i)
  278. {
  279. // empty
  280. }
  281. inline
  282. FrameBuffer::ConstIterator::ConstIterator (const FrameBuffer::Iterator &other):
  283. _i (other._i)
  284. {
  285. // empty
  286. }
  287. inline FrameBuffer::ConstIterator &
  288. FrameBuffer::ConstIterator::operator ++ ()
  289. {
  290. ++_i;
  291. return *this;
  292. }
  293. inline FrameBuffer::ConstIterator
  294. FrameBuffer::ConstIterator::operator ++ (int)
  295. {
  296. ConstIterator tmp = *this;
  297. ++_i;
  298. return tmp;
  299. }
  300. inline const char *
  301. FrameBuffer::ConstIterator::name () const
  302. {
  303. return *_i->first;
  304. }
  305. inline const Slice &
  306. FrameBuffer::ConstIterator::slice () const
  307. {
  308. return _i->second;
  309. }
  310. inline bool
  311. operator == (const FrameBuffer::ConstIterator &x,
  312. const FrameBuffer::ConstIterator &y)
  313. {
  314. return x._i == y._i;
  315. }
  316. inline bool
  317. operator != (const FrameBuffer::ConstIterator &x,
  318. const FrameBuffer::ConstIterator &y)
  319. {
  320. return !(x == y);
  321. }
  322. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  323. #endif