ImfChannelList.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. //-----------------------------------------------------------------------------
  35. //
  36. // class Channel
  37. // class ChannelList
  38. //
  39. //-----------------------------------------------------------------------------
  40. #include <ImfChannelList.h>
  41. #include <Iex.h>
  42. using std::string;
  43. using std::set;
  44. #include "ImfNamespace.h"
  45. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
  46. Channel::Channel (PixelType t, int xs, int ys, bool pl):
  47. type (t),
  48. xSampling (xs),
  49. ySampling (ys),
  50. pLinear (pl)
  51. {
  52. // empty
  53. }
  54. bool
  55. Channel::operator == (const Channel &other) const
  56. {
  57. return type == other.type &&
  58. xSampling == other.xSampling &&
  59. ySampling == other.ySampling &&
  60. pLinear == other.pLinear;
  61. }
  62. void
  63. ChannelList::insert (const char name[], const Channel &channel)
  64. {
  65. if (name[0] == 0)
  66. THROW (IEX_NAMESPACE::ArgExc, "Image channel name cannot be an empty string.");
  67. _map[name] = channel;
  68. }
  69. void
  70. ChannelList::insert (const string &name, const Channel &channel)
  71. {
  72. insert (name.c_str(), channel);
  73. }
  74. Channel &
  75. ChannelList::operator [] (const char name[])
  76. {
  77. ChannelMap::iterator i = _map.find (name);
  78. if (i == _map.end())
  79. THROW (IEX_NAMESPACE::ArgExc, "Cannot find image channel \"" << name << "\".");
  80. return i->second;
  81. }
  82. const Channel &
  83. ChannelList::operator [] (const char name[]) const
  84. {
  85. ChannelMap::const_iterator i = _map.find (name);
  86. if (i == _map.end())
  87. THROW (IEX_NAMESPACE::ArgExc, "Cannot find image channel \"" << name << "\".");
  88. return i->second;
  89. }
  90. Channel &
  91. ChannelList::operator [] (const string &name)
  92. {
  93. return this->operator[] (name.c_str());
  94. }
  95. const Channel &
  96. ChannelList::operator [] (const string &name) const
  97. {
  98. return this->operator[] (name.c_str());
  99. }
  100. Channel *
  101. ChannelList::findChannel (const char name[])
  102. {
  103. ChannelMap::iterator i = _map.find (name);
  104. return (i == _map.end())? 0: &i->second;
  105. }
  106. const Channel *
  107. ChannelList::findChannel (const char name[]) const
  108. {
  109. ChannelMap::const_iterator i = _map.find (name);
  110. return (i == _map.end())? 0: &i->second;
  111. }
  112. Channel *
  113. ChannelList::findChannel (const string &name)
  114. {
  115. return findChannel (name.c_str());
  116. }
  117. const Channel *
  118. ChannelList::findChannel (const string &name) const
  119. {
  120. return findChannel (name.c_str());
  121. }
  122. ChannelList::Iterator
  123. ChannelList::begin ()
  124. {
  125. return _map.begin();
  126. }
  127. ChannelList::ConstIterator
  128. ChannelList::begin () const
  129. {
  130. return _map.begin();
  131. }
  132. ChannelList::Iterator
  133. ChannelList::end ()
  134. {
  135. return _map.end();
  136. }
  137. ChannelList::ConstIterator
  138. ChannelList::end () const
  139. {
  140. return _map.end();
  141. }
  142. ChannelList::Iterator
  143. ChannelList::find (const char name[])
  144. {
  145. return _map.find (name);
  146. }
  147. ChannelList::ConstIterator
  148. ChannelList::find (const char name[]) const
  149. {
  150. return _map.find (name);
  151. }
  152. ChannelList::Iterator
  153. ChannelList::find (const string &name)
  154. {
  155. return find (name.c_str());
  156. }
  157. ChannelList::ConstIterator
  158. ChannelList::find (const string &name) const
  159. {
  160. return find (name.c_str());
  161. }
  162. void
  163. ChannelList::layers (set <string> &layerNames) const
  164. {
  165. layerNames.clear();
  166. for (ConstIterator i = begin(); i != end(); ++i)
  167. {
  168. string layerName = i.name();
  169. size_t pos = layerName.rfind ('.');
  170. if (pos != string::npos && pos != 0 && pos + 1 < layerName.size())
  171. {
  172. layerName.erase (pos);
  173. layerNames.insert (layerName);
  174. }
  175. }
  176. }
  177. void
  178. ChannelList::channelsInLayer (const string &layerName,
  179. Iterator &first,
  180. Iterator &last)
  181. {
  182. channelsWithPrefix (layerName + '.', first, last);
  183. }
  184. void
  185. ChannelList::channelsInLayer (const string &layerName,
  186. ConstIterator &first,
  187. ConstIterator &last) const
  188. {
  189. channelsWithPrefix (layerName + '.', first, last);
  190. }
  191. void
  192. ChannelList::channelsWithPrefix (const char prefix[],
  193. Iterator &first,
  194. Iterator &last)
  195. {
  196. first = last = _map.lower_bound (prefix);
  197. size_t n = int(strlen (prefix));
  198. while (last != Iterator (_map.end()) &&
  199. strncmp (last.name(), prefix, n) <= 0)
  200. {
  201. ++last;
  202. }
  203. }
  204. void
  205. ChannelList::channelsWithPrefix (const char prefix[],
  206. ConstIterator &first,
  207. ConstIterator &last) const
  208. {
  209. first = last = _map.lower_bound (prefix);
  210. size_t n = strlen (prefix);
  211. while (last != ConstIterator (_map.end()) &&
  212. strncmp (last.name(), prefix, n) <= 0)
  213. {
  214. ++last;
  215. }
  216. }
  217. void
  218. ChannelList::channelsWithPrefix (const string &prefix,
  219. Iterator &first,
  220. Iterator &last)
  221. {
  222. return channelsWithPrefix (prefix.c_str(), first, last);
  223. }
  224. void
  225. ChannelList::channelsWithPrefix (const string &prefix,
  226. ConstIterator &first,
  227. ConstIterator &last) const
  228. {
  229. return channelsWithPrefix (prefix.c_str(), first, last);
  230. }
  231. bool
  232. ChannelList::operator == (const ChannelList &other) const
  233. {
  234. ConstIterator i = begin();
  235. ConstIterator j = other.begin();
  236. while (i != end() && j != other.end())
  237. {
  238. if (!(i.channel() == j.channel()))
  239. return false;
  240. ++i;
  241. ++j;
  242. }
  243. return i == end() && j == other.end();
  244. }
  245. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT