ImfTiledMisc.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2004, 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. // Miscellaneous stuff related to tiled files
  37. //
  38. //-----------------------------------------------------------------------------
  39. #include <ImfTiledMisc.h>
  40. #include "Iex.h"
  41. #include <ImfMisc.h>
  42. #include <ImfChannelList.h>
  43. #include <ImfTileDescription.h>
  44. #include <algorithm>
  45. #include "ImfNamespace.h"
  46. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
  47. using IMATH_NAMESPACE::Box2i;
  48. using IMATH_NAMESPACE::V2i;
  49. int
  50. levelSize (int min, int max, int l, LevelRoundingMode rmode)
  51. {
  52. if (l < 0)
  53. throw IEX_NAMESPACE::ArgExc ("Argument not in valid range.");
  54. int a = max - min + 1;
  55. int b = (1 << l);
  56. int size = a / b;
  57. if (rmode == ROUND_UP && size * b < a)
  58. size += 1;
  59. return std::max (size, 1);
  60. }
  61. Box2i
  62. dataWindowForLevel (const TileDescription &tileDesc,
  63. int minX, int maxX,
  64. int minY, int maxY,
  65. int lx, int ly)
  66. {
  67. V2i levelMin = V2i (minX, minY);
  68. V2i levelMax = levelMin +
  69. V2i (levelSize (minX, maxX, lx, tileDesc.roundingMode) - 1,
  70. levelSize (minY, maxY, ly, tileDesc.roundingMode) - 1);
  71. return Box2i(levelMin, levelMax);
  72. }
  73. Box2i
  74. dataWindowForTile (const TileDescription &tileDesc,
  75. int minX, int maxX,
  76. int minY, int maxY,
  77. int dx, int dy,
  78. int lx, int ly)
  79. {
  80. V2i tileMin = V2i (minX + dx * tileDesc.xSize,
  81. minY + dy * tileDesc.ySize);
  82. V2i tileMax = tileMin + V2i (tileDesc.xSize - 1, tileDesc.ySize - 1);
  83. V2i levelMax = dataWindowForLevel
  84. (tileDesc, minX, maxX, minY, maxY, lx, ly).max;
  85. tileMax = V2i (std::min (tileMax[0], levelMax[0]),
  86. std::min (tileMax[1], levelMax[1]));
  87. return Box2i (tileMin, tileMax);
  88. }
  89. size_t
  90. calculateBytesPerPixel (const Header &header)
  91. {
  92. const ChannelList &channels = header.channels();
  93. size_t bytesPerPixel = 0;
  94. for (ChannelList::ConstIterator c = channels.begin();
  95. c != channels.end();
  96. ++c)
  97. {
  98. bytesPerPixel += pixelTypeSize (c.channel().type);
  99. }
  100. return bytesPerPixel;
  101. }
  102. void
  103. calculateBytesPerLine (const Header &header,
  104. char* sampleCountBase,
  105. int sampleCountXStride,
  106. int sampleCountYStride,
  107. int minX, int maxX,
  108. int minY, int maxY,
  109. std::vector<int>& xOffsets,
  110. std::vector<int>& yOffsets,
  111. std::vector<Int64>& bytesPerLine)
  112. {
  113. const ChannelList &channels = header.channels();
  114. int pos = 0;
  115. for (ChannelList::ConstIterator c = channels.begin();
  116. c != channels.end();
  117. ++c, ++pos)
  118. {
  119. int xOffset = xOffsets[pos];
  120. int yOffset = yOffsets[pos];
  121. int i = 0;
  122. for (int y = minY - yOffset; y <= maxY - yOffset; y++, i++)
  123. for (int x = minX - xOffset; x <= maxX - xOffset; x++)
  124. {
  125. bytesPerLine[i] += sampleCount(sampleCountBase,
  126. sampleCountXStride,
  127. sampleCountYStride,
  128. x, y)
  129. * pixelTypeSize (c.channel().type);
  130. }
  131. }
  132. }
  133. namespace {
  134. int
  135. floorLog2 (int x)
  136. {
  137. //
  138. // For x > 0, floorLog2(y) returns floor(log(x)/log(2)).
  139. //
  140. int y = 0;
  141. while (x > 1)
  142. {
  143. y += 1;
  144. x >>= 1;
  145. }
  146. return y;
  147. }
  148. int
  149. ceilLog2 (int x)
  150. {
  151. //
  152. // For x > 0, ceilLog2(y) returns ceil(log(x)/log(2)).
  153. //
  154. int y = 0;
  155. int r = 0;
  156. while (x > 1)
  157. {
  158. if (x & 1)
  159. r = 1;
  160. y += 1;
  161. x >>= 1;
  162. }
  163. return y + r;
  164. }
  165. int
  166. roundLog2 (int x, LevelRoundingMode rmode)
  167. {
  168. return (rmode == ROUND_DOWN)? floorLog2 (x): ceilLog2 (x);
  169. }
  170. int
  171. calculateNumXLevels (const TileDescription& tileDesc,
  172. int minX, int maxX,
  173. int minY, int maxY)
  174. {
  175. int num = 0;
  176. switch (tileDesc.mode)
  177. {
  178. case ONE_LEVEL:
  179. num = 1;
  180. break;
  181. case MIPMAP_LEVELS:
  182. {
  183. int w = maxX - minX + 1;
  184. int h = maxY - minY + 1;
  185. num = roundLog2 (std::max (w, h), tileDesc.roundingMode) + 1;
  186. }
  187. break;
  188. case RIPMAP_LEVELS:
  189. {
  190. int w = maxX - minX + 1;
  191. num = roundLog2 (w, tileDesc.roundingMode) + 1;
  192. }
  193. break;
  194. default:
  195. throw IEX_NAMESPACE::ArgExc ("Unknown LevelMode format.");
  196. }
  197. return num;
  198. }
  199. int
  200. calculateNumYLevels (const TileDescription& tileDesc,
  201. int minX, int maxX,
  202. int minY, int maxY)
  203. {
  204. int num = 0;
  205. switch (tileDesc.mode)
  206. {
  207. case ONE_LEVEL:
  208. num = 1;
  209. break;
  210. case MIPMAP_LEVELS:
  211. {
  212. int w = maxX - minX + 1;
  213. int h = maxY - minY + 1;
  214. num = roundLog2 (std::max (w, h), tileDesc.roundingMode) + 1;
  215. }
  216. break;
  217. case RIPMAP_LEVELS:
  218. {
  219. int h = maxY - minY + 1;
  220. num = roundLog2 (h, tileDesc.roundingMode) + 1;
  221. }
  222. break;
  223. default:
  224. throw IEX_NAMESPACE::ArgExc ("Unknown LevelMode format.");
  225. }
  226. return num;
  227. }
  228. void
  229. calculateNumTiles (int *numTiles,
  230. int numLevels,
  231. int min, int max,
  232. int size,
  233. LevelRoundingMode rmode)
  234. {
  235. for (int i = 0; i < numLevels; i++)
  236. {
  237. numTiles[i] = (levelSize (min, max, i, rmode) + size - 1) / size;
  238. }
  239. }
  240. } // namespace
  241. void
  242. precalculateTileInfo (const TileDescription& tileDesc,
  243. int minX, int maxX,
  244. int minY, int maxY,
  245. int *&numXTiles, int *&numYTiles,
  246. int &numXLevels, int &numYLevels)
  247. {
  248. numXLevels = calculateNumXLevels(tileDesc, minX, maxX, minY, maxY);
  249. numYLevels = calculateNumYLevels(tileDesc, minX, maxX, minY, maxY);
  250. numXTiles = new int[numXLevels];
  251. numYTiles = new int[numYLevels];
  252. calculateNumTiles (numXTiles,
  253. numXLevels,
  254. minX, maxX,
  255. tileDesc.xSize,
  256. tileDesc.roundingMode);
  257. calculateNumTiles (numYTiles,
  258. numYLevels,
  259. minY, maxY,
  260. tileDesc.ySize,
  261. tileDesc.roundingMode);
  262. }
  263. int
  264. getTiledChunkOffsetTableSize(const Header& header)
  265. {
  266. //
  267. // Save the dataWindow information
  268. //
  269. const Box2i &dataWindow = header.dataWindow();
  270. //
  271. // Precompute level and tile information.
  272. //
  273. int* numXTiles;
  274. int* numYTiles;
  275. int numXLevels;
  276. int numYLevels;
  277. precalculateTileInfo (header.tileDescription(),
  278. dataWindow.min.x, dataWindow.max.x,
  279. dataWindow.min.y, dataWindow.max.y,
  280. numXTiles, numYTiles,
  281. numXLevels, numYLevels);
  282. //
  283. // Calculate lineOffsetSize.
  284. //
  285. int lineOffsetSize = 0;
  286. const TileDescription &desc = header.tileDescription();
  287. switch (desc.mode)
  288. {
  289. case ONE_LEVEL:
  290. case MIPMAP_LEVELS:
  291. for (int i = 0; i < numXLevels; i++)
  292. lineOffsetSize += numXTiles[i] * numYTiles[i];
  293. break;
  294. case RIPMAP_LEVELS:
  295. for (int i = 0; i < numXLevels; i++)
  296. for (int j = 0; j < numYLevels; j++)
  297. lineOffsetSize += numXTiles[i] * numYTiles[j];
  298. break;
  299. case NUM_LEVELMODES :
  300. throw IEX_NAMESPACE::LogicExc("Bad level mode getting chunk offset table size");
  301. }
  302. delete[] numXTiles;
  303. delete[] numYTiles;
  304. return lineOffsetSize;
  305. }
  306. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT