ImfFlatImageLevel.h 8.9 KB

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