ImfChannelList.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. //
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Copyright (c) Contributors to the OpenEXR Project.
  4. //
  5. #ifndef INCLUDED_IMF_CHANNEL_LIST_H
  6. #define INCLUDED_IMF_CHANNEL_LIST_H
  7. //-----------------------------------------------------------------------------
  8. //
  9. // class Channel
  10. // class ChannelList
  11. //
  12. //-----------------------------------------------------------------------------
  13. #include "ImfForward.h"
  14. #include "ImfName.h"
  15. #include "ImfPixelType.h"
  16. #include <map>
  17. #include <set>
  18. #include <string>
  19. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  20. struct IMF_EXPORT_TYPE Channel
  21. {
  22. //------------------------------
  23. // Data type; see ImfPixelType.h
  24. //------------------------------
  25. PixelType type;
  26. //--------------------------------------------
  27. // Subsampling: pixel (x, y) is present in the
  28. // channel only if
  29. //
  30. // x % xSampling == 0 && y % ySampling == 0
  31. //
  32. //--------------------------------------------
  33. int xSampling;
  34. int ySampling;
  35. //--------------------------------------------------------------
  36. // Hint to lossy compression methods that indicates whether
  37. // human perception of the quantity represented by this channel
  38. // is closer to linear or closer to logarithmic. Compression
  39. // methods may optimize image quality by adjusting pixel data
  40. // quantization according to this hint.
  41. // For example, perception of red, green, blue and luminance is
  42. // approximately logarithmic; the difference between 0.1 and 0.2
  43. // is perceived to be roughly the same as the difference between
  44. // 1.0 and 2.0. Perception of chroma coordinates tends to be
  45. // closer to linear than logarithmic; the difference between 0.1
  46. // and 0.2 is perceived to be roughly the same as the difference
  47. // between 1.0 and 1.1.
  48. //--------------------------------------------------------------
  49. bool pLinear;
  50. //------------
  51. // Constructor
  52. //------------
  53. IMF_EXPORT
  54. Channel (PixelType type = HALF,
  55. int xSampling = 1,
  56. int ySampling = 1,
  57. bool pLinear = false);
  58. //------------
  59. // Operator ==
  60. //------------
  61. IMF_EXPORT
  62. bool operator == (const Channel &other) const;
  63. };
  64. class IMF_EXPORT_TYPE ChannelList
  65. {
  66. public:
  67. //--------------
  68. // Add a channel
  69. //--------------
  70. IMF_EXPORT
  71. void insert (const char name[],
  72. const Channel &channel);
  73. IMF_EXPORT
  74. void insert (const std::string &name,
  75. const Channel &channel);
  76. //------------------------------------------------------------------
  77. // Access to existing channels:
  78. //
  79. // [n] Returns a reference to the channel with name n.
  80. // If no channel with name n exists, an IEX_NAMESPACE::ArgExc
  81. // is thrown.
  82. //
  83. // findChannel(n) Returns a pointer to the channel with name n,
  84. // or 0 if no channel with name n exists.
  85. //
  86. //------------------------------------------------------------------
  87. IMF_EXPORT
  88. Channel & operator [] (const char name[]);
  89. IMF_EXPORT
  90. const Channel & operator [] (const char name[]) const;
  91. IMF_EXPORT
  92. Channel & operator [] (const std::string &name);
  93. IMF_EXPORT
  94. const Channel & operator [] (const std::string &name) const;
  95. IMF_EXPORT
  96. Channel * findChannel (const char name[]);
  97. IMF_EXPORT
  98. const Channel * findChannel (const char name[]) const;
  99. IMF_EXPORT
  100. Channel * findChannel (const std::string &name);
  101. IMF_EXPORT
  102. const Channel * findChannel (const std::string &name) const;
  103. //-------------------------------------------
  104. // Iterator-style access to existing channels
  105. //-------------------------------------------
  106. typedef std::map <Name, Channel> ChannelMap;
  107. class Iterator;
  108. class ConstIterator;
  109. IMF_EXPORT
  110. Iterator begin ();
  111. IMF_EXPORT
  112. ConstIterator begin () const;
  113. IMF_EXPORT
  114. Iterator end ();
  115. IMF_EXPORT
  116. ConstIterator end () const;
  117. IMF_EXPORT
  118. Iterator find (const char name[]);
  119. IMF_EXPORT
  120. ConstIterator find (const char name[]) const;
  121. IMF_EXPORT
  122. Iterator find (const std::string &name);
  123. IMF_EXPORT
  124. ConstIterator find (const std::string &name) const;
  125. //-----------------------------------------------------------------
  126. // Support for image layers:
  127. //
  128. // In an image file with many channels it is sometimes useful to
  129. // group the channels into "layers", that is, into sets of channels
  130. // that logically belong together. Grouping channels into layers
  131. // is done using a naming convention: channel C in layer L is
  132. // called "L.C".
  133. //
  134. // For example, a computer graphic image may contain separate
  135. // R, G and B channels for light that originated at each of
  136. // several different virtual light sources. The channels in
  137. // this image might be called "light1.R", "light1.G", "light1.B",
  138. // "light2.R", "light2.G", "light2.B", etc.
  139. //
  140. // Note that this naming convention allows layers to be nested;
  141. // for example, "light1.specular.R" identifies the "R" channel
  142. // in the "specular" sub-layer of layer "light1".
  143. //
  144. // Channel names that don't contain a "." or that contain a
  145. // "." only at the beginning or at the end are not considered
  146. // to be part of any layer.
  147. //
  148. // layers(lns) sorts the channels in this ChannelList
  149. // into layers and stores the names of
  150. // all layers, sorted alphabetically,
  151. // into string set lns.
  152. //
  153. // channelsInLayer(ln,f,l) stores a pair of iterators in f and l
  154. // such that the loop
  155. //
  156. // for (ConstIterator i = f; i != l; ++i)
  157. // ...
  158. //
  159. // iterates over all channels in layer ln.
  160. // channelsInLayer (ln, l, p) calls
  161. // channelsWithPrefix (ln + ".", l, p).
  162. //
  163. //-----------------------------------------------------------------
  164. IMF_EXPORT
  165. void layers (std::set <std::string> &layerNames) const;
  166. IMF_EXPORT
  167. void channelsInLayer (const std::string &layerName,
  168. Iterator &first,
  169. Iterator &last);
  170. IMF_EXPORT
  171. void channelsInLayer (const std::string &layerName,
  172. ConstIterator &first,
  173. ConstIterator &last) const;
  174. //-------------------------------------------------------------------
  175. // Find all channels whose name begins with a given prefix:
  176. //
  177. // channelsWithPrefix(p,f,l) stores a pair of iterators in f and l
  178. // such that the following loop iterates over all channels whose name
  179. // begins with string p:
  180. //
  181. // for (ConstIterator i = f; i != l; ++i)
  182. // ...
  183. //
  184. //-------------------------------------------------------------------
  185. IMF_EXPORT
  186. void channelsWithPrefix (const char prefix[],
  187. Iterator &first,
  188. Iterator &last);
  189. IMF_EXPORT
  190. void channelsWithPrefix (const char prefix[],
  191. ConstIterator &first,
  192. ConstIterator &last) const;
  193. IMF_EXPORT
  194. void channelsWithPrefix (const std::string &prefix,
  195. Iterator &first,
  196. Iterator &last);
  197. IMF_EXPORT
  198. void channelsWithPrefix (const std::string &prefix,
  199. ConstIterator &first,
  200. ConstIterator &last) const;
  201. //------------
  202. // Operator ==
  203. //------------
  204. IMF_EXPORT
  205. bool operator == (const ChannelList &other) const;
  206. private:
  207. ChannelMap _map;
  208. };
  209. //----------
  210. // Iterators
  211. //----------
  212. class IMF_EXPORT_TYPE ChannelList::Iterator
  213. {
  214. public:
  215. IMF_EXPORT
  216. Iterator ();
  217. IMF_EXPORT
  218. Iterator (const ChannelList::ChannelMap::iterator &i);
  219. IMF_EXPORT
  220. Iterator & operator ++ ();
  221. IMF_EXPORT
  222. Iterator operator ++ (int);
  223. IMF_EXPORT
  224. const char * name () const;
  225. IMF_EXPORT
  226. Channel & channel () const;
  227. private:
  228. friend class ChannelList::ConstIterator;
  229. ChannelList::ChannelMap::iterator _i;
  230. };
  231. class IMF_EXPORT_TYPE ChannelList::ConstIterator
  232. {
  233. public:
  234. IMF_EXPORT
  235. ConstIterator ();
  236. IMF_EXPORT
  237. ConstIterator (const ChannelList::ChannelMap::const_iterator &i);
  238. IMF_EXPORT
  239. ConstIterator (const ChannelList::Iterator &other);
  240. IMF_EXPORT
  241. ConstIterator & operator ++ ();
  242. IMF_EXPORT
  243. ConstIterator operator ++ (int);
  244. IMF_EXPORT
  245. const char * name () const;
  246. IMF_EXPORT
  247. const Channel & channel () const;
  248. private:
  249. friend bool operator == (const ConstIterator &, const ConstIterator &);
  250. friend bool operator != (const ConstIterator &, const ConstIterator &);
  251. ChannelList::ChannelMap::const_iterator _i;
  252. };
  253. //-----------------
  254. // Inline Functions
  255. //-----------------
  256. inline
  257. ChannelList::Iterator::Iterator (): _i()
  258. {
  259. // empty
  260. }
  261. inline
  262. ChannelList::Iterator::Iterator (const ChannelList::ChannelMap::iterator &i):
  263. _i (i)
  264. {
  265. // empty
  266. }
  267. inline ChannelList::Iterator &
  268. ChannelList::Iterator::operator ++ ()
  269. {
  270. ++_i;
  271. return *this;
  272. }
  273. inline ChannelList::Iterator
  274. ChannelList::Iterator::operator ++ (int)
  275. {
  276. Iterator tmp = *this;
  277. ++_i;
  278. return tmp;
  279. }
  280. inline const char *
  281. ChannelList::Iterator::name () const
  282. {
  283. return *_i->first;
  284. }
  285. inline Channel &
  286. ChannelList::Iterator::channel () const
  287. {
  288. return _i->second;
  289. }
  290. inline
  291. ChannelList::ConstIterator::ConstIterator (): _i()
  292. {
  293. // empty
  294. }
  295. inline
  296. ChannelList::ConstIterator::ConstIterator
  297. (const ChannelList::ChannelMap::const_iterator &i): _i (i)
  298. {
  299. // empty
  300. }
  301. inline
  302. ChannelList::ConstIterator::ConstIterator (const ChannelList::Iterator &other):
  303. _i (other._i)
  304. {
  305. // empty
  306. }
  307. inline ChannelList::ConstIterator &
  308. ChannelList::ConstIterator::operator ++ ()
  309. {
  310. ++_i;
  311. return *this;
  312. }
  313. inline ChannelList::ConstIterator
  314. ChannelList::ConstIterator::operator ++ (int)
  315. {
  316. ConstIterator tmp = *this;
  317. ++_i;
  318. return tmp;
  319. }
  320. inline const char *
  321. ChannelList::ConstIterator::name () const
  322. {
  323. return *_i->first;
  324. }
  325. inline const Channel &
  326. ChannelList::ConstIterator::channel () const
  327. {
  328. return _i->second;
  329. }
  330. inline bool
  331. operator == (const ChannelList::ConstIterator &x,
  332. const ChannelList::ConstIterator &y)
  333. {
  334. return x._i == y._i;
  335. }
  336. inline bool
  337. operator != (const ChannelList::ConstIterator &x,
  338. const ChannelList::ConstIterator &y)
  339. {
  340. return !(x == y);
  341. }
  342. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  343. #endif