ImfChannelList.h 12 KB

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