/src/FreeImage/Source/OpenEXR/IlmImf/ImfChannelList.cpp

https://bitbucket.org/cabalistic/ogredeps/ · C++ · 321 lines · 204 code · 77 blank · 40 comment · 34 complexity · d622f01b7505209ee27bd85d2c70ddef MD5 · raw file

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