ImfMultiView.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2007, Weta Digital Ltd
  4. //
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are
  9. // met:
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Weta Digital nor the names of
  17. // its contributors may be used to endorse or promote products derived
  18. // from this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. //
  32. ///////////////////////////////////////////////////////////////////////////
  33. //-----------------------------------------------------------------------------
  34. //
  35. // Functions related to accessing channels
  36. // and views in multi-view OpenEXR files.
  37. //
  38. //-----------------------------------------------------------------------------
  39. #include <ImfMultiView.h>
  40. using namespace std;
  41. #include "ImfNamespace.h"
  42. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
  43. namespace {
  44. StringVector
  45. parseString (string name, char c = '.')
  46. {
  47. //
  48. // Turn name into a list of strings, separating
  49. // on char 'c' with whitespace stripped.
  50. //
  51. StringVector r;
  52. while (name.size() > 0)
  53. {
  54. size_t s = name.find (c);
  55. string sec = name.substr (0, s);
  56. //
  57. // Strip spaces from beginning
  58. //
  59. while (sec.size() > 0 && sec[0] == ' ')
  60. sec.erase (0, 1);
  61. //
  62. // Strip spaces from end
  63. //
  64. while (sec.size() > 0 && sec[sec.size() - 1] == ' ')
  65. sec.erase (sec.size() - 1);
  66. r.push_back (sec);
  67. //
  68. // Strip off name including ending 'c'
  69. //
  70. if (s == name.npos)
  71. name = "";
  72. else
  73. name = name.substr (s + 1);
  74. }
  75. return r;
  76. }
  77. int
  78. viewNum (const string &view, const StringVector &multiView)
  79. {
  80. //
  81. // returns which view number is called 'view'
  82. // returns -1 if no member of multiView is 'view'
  83. // (i.e. if viewNum() returns -1, 'view' isn't a view name
  84. // if viewNum() returns 0, 'view' is the default view
  85. // otherwise, it's some other (valid) view
  86. //
  87. for (size_t i = 0; i < multiView.size(); ++i)
  88. {
  89. if (multiView[i] == view)
  90. return i;
  91. }
  92. return -1;
  93. }
  94. } // namespace
  95. string
  96. defaultViewName (const StringVector &multiView)
  97. {
  98. if (multiView.size() > 0)
  99. return multiView[0];
  100. else
  101. return "";
  102. }
  103. string
  104. viewFromChannelName (const string &channel,
  105. const StringVector &multiView)
  106. {
  107. //
  108. // Given the name of a channel, return the name of the view to
  109. // which it belongs.
  110. //
  111. //
  112. // View name is penultimate section of name separated by periods ('.'s)
  113. //
  114. StringVector s = parseString (channel, '.');
  115. if (s.size() == 0)
  116. return ""; // nothing in, nothing out
  117. if (s.size() == 1)
  118. {
  119. //
  120. // Return default view name.
  121. // The rules say ALL channels with no periods
  122. // in the name belong to the default view.
  123. //
  124. return multiView[0];
  125. }
  126. else
  127. {
  128. //
  129. // size >= 2 - the last part is the channel name,
  130. // the next-to-last part is the view name.
  131. // Check if that part of the name really is
  132. // a valid view and, if it is, return it.
  133. //
  134. const string &viewName = s[s.size() - 2];
  135. if (viewNum (viewName, multiView) >= 0)
  136. return viewName;
  137. else
  138. return ""; // not associated with any particular view
  139. }
  140. }
  141. ChannelList
  142. channelsInView (const string & viewName,
  143. const ChannelList & channelList,
  144. const StringVector & multiView)
  145. {
  146. //
  147. // Return a list of all channels belonging to view viewName.
  148. //
  149. ChannelList q;
  150. for (ChannelList::ConstIterator i = channelList.begin();
  151. i != channelList.end();
  152. ++i)
  153. {
  154. //
  155. // Get view name for this channel
  156. //
  157. string view = viewFromChannelName (i.name(), multiView);
  158. //
  159. // Insert channel into q if it's a member of view viewName
  160. //
  161. if (view == viewName)
  162. q.insert (i.name(), i.channel());
  163. }
  164. return q;
  165. }
  166. ChannelList
  167. channelsInNoView (const ChannelList &channelList,
  168. const StringVector &multiView)
  169. {
  170. //
  171. // Return a list of channels not associated with any named view.
  172. //
  173. return channelsInView ("", channelList, multiView);
  174. }
  175. bool
  176. areCounterparts (const string &channel1,
  177. const string &channel2,
  178. const StringVector &multiView)
  179. {
  180. //
  181. // Given two channels, return true if they are the same
  182. // channel in two different views.
  183. //
  184. StringVector chan1 = parseString (channel1);
  185. size_t size1 = chan1.size(); // number of SECTIONS in string
  186. // name (not string length)
  187. StringVector chan2 = parseString (channel2);
  188. size_t size2 = chan2.size();
  189. if (size1 == 0 || size2 == 0)
  190. return false;
  191. //
  192. // channel1 and channel2 can't be counterparts
  193. // if either channel is in no view.
  194. //
  195. if (size1 > 1 && viewNum (chan1[size1 - 2], multiView) == -1)
  196. return false;
  197. if (size2 > 1 && viewNum (chan2[size2 - 2], multiView) == -1)
  198. return false;
  199. if (viewFromChannelName (channel1, multiView) ==
  200. viewFromChannelName (channel2, multiView))
  201. {
  202. //
  203. // channel1 and channel2 are not counterparts
  204. // if they are in the same view.
  205. //
  206. return false;
  207. }
  208. if (size1 == 1)
  209. {
  210. //
  211. // channel1 is a default channel - the channels will only be
  212. // counterparts if channel2 is of the form <view>.<channel1>
  213. //
  214. return size2 == 2 && chan1[0] == chan2[1];
  215. }
  216. if (size2 == 1)
  217. {
  218. //
  219. // channel2 is a default channel - the channels will only be
  220. // counterparts if channel1 is of the form <view>.<channel2>
  221. //
  222. return size1 == 2 && chan2[0] == chan1[1];
  223. }
  224. //
  225. // Neither channel is a default channel. To be counterparts both
  226. // channel names must have the same number of components, and
  227. // all components except the penultimate one must be the same.
  228. //
  229. if (size1 != size2)
  230. return false;
  231. for(size_t i = 0; i < size1; ++i)
  232. {
  233. if (i != size1 - 2 && chan1[i] != chan2[i])
  234. return false;
  235. }
  236. return true;
  237. }
  238. ChannelList
  239. channelInAllViews (const string &channelName,
  240. const ChannelList &channelList,
  241. const StringVector &multiView)
  242. {
  243. //
  244. // Given the name of a channel, return a
  245. // list of the same channel in all views.
  246. //
  247. ChannelList q;
  248. for (ChannelList::ConstIterator i=channelList.begin();
  249. i != channelList.end();
  250. ++i)
  251. {
  252. if (i.name() == channelName ||
  253. areCounterparts (i.name(), channelName, multiView))
  254. {
  255. q.insert (i.name(), i.channel());
  256. }
  257. }
  258. return q;
  259. }
  260. string
  261. channelInOtherView (const string &channelName,
  262. const ChannelList &channelList,
  263. const StringVector &multiView,
  264. const string &otherViewName)
  265. {
  266. //
  267. // Given the name of a channel in one view, return the
  268. // corresponding channel name for view otherViewName.
  269. //
  270. for (ChannelList::ConstIterator i=channelList.begin();
  271. i != channelList.end();
  272. ++i)
  273. {
  274. if (viewFromChannelName (i.name(), multiView) == otherViewName &&
  275. areCounterparts (i.name(), channelName, multiView))
  276. {
  277. return i.name();
  278. }
  279. }
  280. return "";
  281. }
  282. string
  283. insertViewName (const string &channel,
  284. const StringVector &multiView,
  285. int i)
  286. {
  287. //
  288. // Insert multiView[i] into the channel name if appropriate.
  289. //
  290. StringVector s = parseString (channel, '.');
  291. if (s.size() == 0)
  292. return ""; // nothing in, nothing out
  293. if (s.size() == 1 && i == 0)
  294. {
  295. //
  296. // Channel in the default view, with no periods in its name.
  297. // Do not insert view name.
  298. //
  299. return channel;
  300. }
  301. //
  302. // View name becomes penultimate section of new channel name.
  303. //
  304. string newName;
  305. for (size_t j = 0; j < s.size(); ++j)
  306. {
  307. if (j < s.size() - 1)
  308. newName += s[j] + ".";
  309. else
  310. newName += multiView[i] + "." + s[j];
  311. }
  312. return newName;
  313. }
  314. string
  315. removeViewName(const string & channel,const string & view)
  316. {
  317. StringVector s = parseString (channel, '.');
  318. if (s.size() == 0)
  319. return ""; // nothing in, nothing out
  320. if (s.size() == 1)
  321. {
  322. //
  323. // Channel in the default view, since no periods in its name.
  324. // No viewname to remove
  325. //
  326. return channel;
  327. }
  328. string newName;
  329. for( size_t j = 0 ; j < s.size() ; ++j)
  330. {
  331. // only add the penultimate string part
  332. // if it doesn't match the view name
  333. if(j+2!=s.size() || s[j]!=view)
  334. {
  335. newName += s[j];
  336. if(j+1!=s.size())
  337. {
  338. newName += ".";
  339. }
  340. }
  341. }
  342. return newName;
  343. }
  344. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT