ImfDeepImageLevel.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. //
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Copyright (c) Contributors to the OpenEXR Project.
  4. //
  5. #ifndef INCLUDED_IMF_DEEP_IMAGE_LEVEL_H
  6. #define INCLUDED_IMF_DEEP_IMAGE_LEVEL_H
  7. //----------------------------------------------------------------------------
  8. //
  9. // class DeepImageLevel
  10. // class DeepImageLevel::Iterator
  11. // class DeepImageLevel::ConstIterator
  12. //
  13. // For an explanation of images, levels and channels,
  14. // see the comments in header file Image.h.
  15. //
  16. //----------------------------------------------------------------------------
  17. #include "ImfUtilExport.h"
  18. #include "ImfNamespace.h"
  19. #include "ImfDeepImageChannel.h"
  20. #include "ImfSampleCountChannel.h"
  21. #include "ImfImageLevel.h"
  22. #include <string>
  23. #include <map>
  24. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  25. class DeepImage;
  26. class IMFUTIL_EXPORT_TYPE DeepImageLevel : public ImageLevel
  27. {
  28. public:
  29. //
  30. // Access to the image to which the level belongs.
  31. //
  32. IMFUTIL_EXPORT
  33. DeepImage & deepImage ();
  34. IMFUTIL_EXPORT
  35. const DeepImage & deepImage () const;
  36. //
  37. // Access to deep channels by name:
  38. //
  39. // findChannel(n) returns a pointer to the image channel with
  40. // name n, or 0 if no such channel exists.
  41. //
  42. // channel(n) returns a reference to the image channel with
  43. // name n, or throws an Iex::ArgExc exception if
  44. // no such channel exists.
  45. //
  46. // findTypedChannel<T>(n) returns a pointer to the image channel with
  47. // name n and type T, or 0 if no such channel
  48. // exists.
  49. //
  50. // typedChannel(n) returns a reference to the image channel with
  51. // name n and type T, or throws an Iex::ArgExc
  52. // exception if no such channel exists.
  53. //
  54. IMFUTIL_EXPORT
  55. DeepImageChannel * findChannel (const std::string& name);
  56. IMFUTIL_EXPORT
  57. const DeepImageChannel * findChannel (const std::string& name) const;
  58. IMFUTIL_EXPORT
  59. DeepImageChannel & channel (const std::string& name);
  60. IMFUTIL_EXPORT
  61. const DeepImageChannel & channel (const std::string& name) const;
  62. template <class T>
  63. TypedDeepImageChannel<T> * findTypedChannel
  64. (const std::string& name);
  65. template <class T>
  66. const TypedDeepImageChannel<T> * findTypedChannel
  67. (const std::string& name) const;
  68. template <class T>
  69. TypedDeepImageChannel<T> & typedChannel
  70. (const std::string& name);
  71. template <class T>
  72. const TypedDeepImageChannel<T> & typedChannel
  73. (const std::string& name) const;
  74. //
  75. // Iterator-style access to deep channels
  76. //
  77. typedef std::map <std::string, DeepImageChannel *> ChannelMap;
  78. class Iterator;
  79. class ConstIterator;
  80. IMFUTIL_EXPORT
  81. Iterator begin();
  82. IMFUTIL_EXPORT
  83. ConstIterator begin() const;
  84. IMFUTIL_EXPORT
  85. Iterator end();
  86. IMFUTIL_EXPORT
  87. ConstIterator end() const;
  88. //
  89. // Access to the sample count channel
  90. //
  91. IMFUTIL_EXPORT
  92. SampleCountChannel & sampleCounts();
  93. IMFUTIL_EXPORT
  94. const SampleCountChannel & sampleCounts() const;
  95. private:
  96. friend class DeepImage;
  97. friend class SampleCountChannel;
  98. //
  99. // The constructor and destructor are private.
  100. // Deep image levels exist only as part of a deep image.
  101. //
  102. IMF_HIDDEN
  103. DeepImageLevel (DeepImage& image,
  104. int xLevelNumber,
  105. int yLevelNumber,
  106. const IMATH_NAMESPACE::Box2i& dataWindow);
  107. IMF_HIDDEN
  108. ~DeepImageLevel ();
  109. IMF_HIDDEN
  110. void setSamplesToZero (size_t i,
  111. unsigned int oldNumSamples,
  112. unsigned int newNumSamples);
  113. IMF_HIDDEN
  114. void moveSampleList (size_t i,
  115. unsigned int oldNumSamples,
  116. unsigned int newNumSamples,
  117. size_t newSampleListPosition);
  118. IMF_HIDDEN
  119. void moveSamplesToNewBuffer (const unsigned int * oldNumSamples,
  120. const unsigned int * newNumSamples,
  121. const size_t * newSampleListPositions);
  122. IMF_HIDDEN
  123. void initializeSampleLists ();
  124. IMF_HIDDEN
  125. virtual void resize (const IMATH_NAMESPACE::Box2i& dataWindow);
  126. IMF_HIDDEN
  127. virtual void shiftPixels (int dx, int dy);
  128. IMF_HIDDEN
  129. virtual void insertChannel (const std::string& name,
  130. PixelType type,
  131. int xSampling,
  132. int ySampling,
  133. bool pLinear);
  134. IMF_HIDDEN
  135. virtual void eraseChannel (const std::string& name);
  136. IMF_HIDDEN
  137. virtual void clearChannels ();
  138. IMF_HIDDEN
  139. virtual void renameChannel (const std::string &oldName,
  140. const std::string &newName);
  141. IMF_HIDDEN
  142. virtual void renameChannels (const RenamingMap &oldToNewNames);
  143. ChannelMap _channels;
  144. SampleCountChannel _sampleCounts;
  145. };
  146. class IMFUTIL_EXPORT_TYPE DeepImageLevel::Iterator
  147. {
  148. public:
  149. IMFUTIL_EXPORT
  150. Iterator ();
  151. IMFUTIL_EXPORT
  152. Iterator (const DeepImageLevel::ChannelMap::iterator& i);
  153. //
  154. // Advance the iterator
  155. //
  156. IMFUTIL_EXPORT
  157. Iterator & operator ++ ();
  158. IMFUTIL_EXPORT
  159. Iterator operator ++ (int);
  160. //
  161. // Access to the channel to which the iterator points,
  162. // and to the name of that channel.
  163. //
  164. IMFUTIL_EXPORT
  165. const std::string & name () const;
  166. IMFUTIL_EXPORT
  167. DeepImageChannel & channel () const;
  168. private:
  169. friend class DeepImageLevel::ConstIterator;
  170. DeepImageLevel::ChannelMap::iterator _i;
  171. };
  172. class IMFUTIL_EXPORT_TYPE DeepImageLevel::ConstIterator
  173. {
  174. public:
  175. IMFUTIL_EXPORT
  176. ConstIterator ();
  177. IMFUTIL_EXPORT
  178. ConstIterator (const DeepImageLevel::ChannelMap::const_iterator& i);
  179. IMFUTIL_EXPORT
  180. ConstIterator (const DeepImageLevel::Iterator& other);
  181. //
  182. // Advance the iterator
  183. //
  184. IMFUTIL_EXPORT
  185. ConstIterator & operator ++ ();
  186. IMFUTIL_EXPORT
  187. ConstIterator operator ++ (int);
  188. //
  189. // Access to the channel to which the iterator points,
  190. // and to the name of that channel.
  191. //
  192. IMFUTIL_EXPORT
  193. const std::string & name () const;
  194. IMFUTIL_EXPORT
  195. const DeepImageChannel & channel () const;
  196. private:
  197. friend bool operator ==
  198. (const ConstIterator &, const ConstIterator &);
  199. friend bool operator !=
  200. (const ConstIterator &, const ConstIterator &);
  201. DeepImageLevel::ChannelMap::const_iterator _i;
  202. };
  203. //-----------------------------------------------------------------------------
  204. // Implementation of inline functions
  205. //-----------------------------------------------------------------------------
  206. template <class T>
  207. TypedDeepImageChannel<T> *
  208. DeepImageLevel::findTypedChannel (const std::string& name)
  209. {
  210. return dynamic_cast <TypedDeepImageChannel<T> *> (findChannel (name));
  211. }
  212. template <class T>
  213. const TypedDeepImageChannel<T> *
  214. DeepImageLevel::findTypedChannel (const std::string& name) const
  215. {
  216. return dynamic_cast <const TypedDeepImageChannel<T> *> (findChannel (name));
  217. }
  218. template <class T>
  219. TypedDeepImageChannel<T> &
  220. DeepImageLevel::typedChannel (const std::string& name)
  221. {
  222. TypedDeepImageChannel<T> * ptr = findTypedChannel<T> (name);
  223. if (ptr == 0)
  224. throwBadChannelNameOrType (name);
  225. return *ptr;
  226. }
  227. template <class T>
  228. const TypedDeepImageChannel<T> &
  229. DeepImageLevel::typedChannel (const std::string& name) const
  230. {
  231. const TypedDeepImageChannel<T> * ptr = findTypedChannel<T> (name);
  232. if (ptr == 0)
  233. throwBadChannelNameOrType (name);
  234. return *ptr;
  235. }
  236. inline SampleCountChannel &
  237. DeepImageLevel::sampleCounts ()
  238. {
  239. return _sampleCounts;
  240. }
  241. inline const SampleCountChannel &
  242. DeepImageLevel::sampleCounts () const
  243. {
  244. return _sampleCounts;
  245. }
  246. inline
  247. DeepImageLevel::Iterator::Iterator (): _i()
  248. {
  249. // empty
  250. }
  251. inline
  252. DeepImageLevel::Iterator::Iterator
  253. (const DeepImageLevel::ChannelMap::iterator& i)
  254. :
  255. _i (i)
  256. {
  257. // empty
  258. }
  259. inline DeepImageLevel::Iterator &
  260. DeepImageLevel::Iterator::operator ++ ()
  261. {
  262. ++_i;
  263. return *this;
  264. }
  265. inline DeepImageLevel::Iterator
  266. DeepImageLevel::Iterator::operator ++ (int)
  267. {
  268. Iterator tmp = *this;
  269. ++_i;
  270. return tmp;
  271. }
  272. inline const std::string &
  273. DeepImageLevel::Iterator::name () const
  274. {
  275. return _i->first;
  276. }
  277. inline DeepImageChannel &
  278. DeepImageLevel::Iterator::channel () const
  279. {
  280. return *_i->second;
  281. }
  282. inline
  283. DeepImageLevel::ConstIterator::ConstIterator (): _i()
  284. {
  285. // empty
  286. }
  287. inline
  288. DeepImageLevel::ConstIterator::ConstIterator
  289. (const DeepImageLevel::ChannelMap::const_iterator& i): _i (i)
  290. {
  291. // empty
  292. }
  293. inline
  294. DeepImageLevel::ConstIterator::ConstIterator
  295. (const DeepImageLevel::Iterator& other): _i (other._i)
  296. {
  297. // empty
  298. }
  299. inline DeepImageLevel::ConstIterator &
  300. DeepImageLevel::ConstIterator::operator ++ ()
  301. {
  302. ++_i;
  303. return *this;
  304. }
  305. inline DeepImageLevel::ConstIterator
  306. DeepImageLevel::ConstIterator::operator ++ (int)
  307. {
  308. ConstIterator tmp = *this;
  309. ++_i;
  310. return tmp;
  311. }
  312. inline const std::string &
  313. DeepImageLevel::ConstIterator::name () const
  314. {
  315. return _i->first;
  316. }
  317. inline const DeepImageChannel &
  318. DeepImageLevel::ConstIterator::channel () const
  319. {
  320. return *_i->second;
  321. }
  322. inline bool
  323. operator == (const DeepImageLevel::ConstIterator& x,
  324. const DeepImageLevel::ConstIterator& y)
  325. {
  326. return x._i == y._i;
  327. }
  328. inline bool
  329. operator != (const DeepImageLevel::ConstIterator& x,
  330. const DeepImageLevel::ConstIterator& y)
  331. {
  332. return !(x == y);
  333. }
  334. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  335. #endif