ImfTileOffsets.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. // class TileOffsets
  37. //
  38. //-----------------------------------------------------------------------------
  39. #include <ImfTileOffsets.h>
  40. #include <ImfXdr.h>
  41. #include <ImfIO.h>
  42. #include "Iex.h"
  43. #include "ImfNamespace.h"
  44. #include <algorithm>
  45. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
  46. TileOffsets::TileOffsets (LevelMode mode,
  47. int numXLevels, int numYLevels,
  48. const int *numXTiles, const int *numYTiles)
  49. :
  50. _mode (mode),
  51. _numXLevels (numXLevels),
  52. _numYLevels (numYLevels)
  53. {
  54. switch (_mode)
  55. {
  56. case ONE_LEVEL:
  57. case MIPMAP_LEVELS:
  58. _offsets.resize (_numXLevels);
  59. for (unsigned int l = 0; l < _offsets.size(); ++l)
  60. {
  61. _offsets[l].resize (numYTiles[l]);
  62. for (unsigned int dy = 0; dy < _offsets[l].size(); ++dy)
  63. {
  64. _offsets[l][dy].resize (numXTiles[l]);
  65. }
  66. }
  67. break;
  68. case RIPMAP_LEVELS:
  69. _offsets.resize (_numXLevels * _numYLevels);
  70. for (int ly = 0; ly < _numYLevels; ++ly)
  71. {
  72. for (int lx = 0; lx < _numXLevels; ++lx)
  73. {
  74. int l = ly * _numXLevels + lx;
  75. _offsets[l].resize (numYTiles[ly]);
  76. for (size_t dy = 0; dy < _offsets[l].size(); ++dy)
  77. {
  78. _offsets[l][dy].resize (numXTiles[lx]);
  79. }
  80. }
  81. }
  82. break;
  83. case NUM_LEVELMODES :
  84. throw IEX_NAMESPACE::ArgExc("Bad initialisation of TileOffsets object");
  85. }
  86. }
  87. bool
  88. TileOffsets::anyOffsetsAreInvalid () const
  89. {
  90. for (unsigned int l = 0; l < _offsets.size(); ++l)
  91. for (unsigned int dy = 0; dy < _offsets[l].size(); ++dy)
  92. for (unsigned int dx = 0; dx < _offsets[l][dy].size(); ++dx)
  93. if (_offsets[l][dy][dx] <= 0)
  94. return true;
  95. return false;
  96. }
  97. void
  98. TileOffsets::findTiles (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is, bool isMultiPartFile, bool isDeep, bool skipOnly)
  99. {
  100. for (unsigned int l = 0; l < _offsets.size(); ++l)
  101. {
  102. for (unsigned int dy = 0; dy < _offsets[l].size(); ++dy)
  103. {
  104. for (unsigned int dx = 0; dx < _offsets[l][dy].size(); ++dx)
  105. {
  106. Int64 tileOffset = is.tellg();
  107. if (isMultiPartFile)
  108. {
  109. int partNumber;
  110. OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (is, partNumber);
  111. }
  112. int tileX;
  113. OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (is, tileX);
  114. int tileY;
  115. OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (is, tileY);
  116. int levelX;
  117. OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (is, levelX);
  118. int levelY;
  119. OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (is, levelY);
  120. if(isDeep)
  121. {
  122. Int64 packed_offset_table_size;
  123. Int64 packed_sample_size;
  124. OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (is, packed_offset_table_size);
  125. OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (is, packed_sample_size);
  126. // next Int64 is unpacked sample size - skip that too
  127. Xdr::skip <StreamIO> (is, packed_offset_table_size+packed_sample_size+8);
  128. }else{
  129. int dataSize;
  130. OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (is, dataSize);
  131. Xdr::skip <StreamIO> (is, dataSize);
  132. }
  133. if (skipOnly) continue;
  134. if (!isValidTile(tileX, tileY, levelX, levelY))
  135. return;
  136. operator () (tileX, tileY, levelX, levelY) = tileOffset;
  137. }
  138. }
  139. }
  140. }
  141. void
  142. TileOffsets::reconstructFromFile (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is,bool isMultiPart,bool isDeep)
  143. {
  144. //
  145. // Try to reconstruct a missing tile offset table by sequentially
  146. // scanning through the file, and recording the offsets in the file
  147. // of the tiles we find.
  148. //
  149. Int64 position = is.tellg();
  150. try
  151. {
  152. findTiles (is,isMultiPart,isDeep,false);
  153. }
  154. catch (...)
  155. {
  156. //
  157. // Suppress all exceptions. This function is called only to
  158. // reconstruct the tile offset table for incomplete files,
  159. // and exceptions are likely.
  160. //
  161. }
  162. is.clear();
  163. is.seekg (position);
  164. }
  165. void
  166. TileOffsets::readFrom (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is, bool &complete,bool isMultiPartFile, bool isDeep)
  167. {
  168. //
  169. // Read in the tile offsets from the file's tile offset table
  170. //
  171. for (unsigned int l = 0; l < _offsets.size(); ++l)
  172. for (unsigned int dy = 0; dy < _offsets[l].size(); ++dy)
  173. for (unsigned int dx = 0; dx < _offsets[l][dy].size(); ++dx)
  174. OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (is, _offsets[l][dy][dx]);
  175. //
  176. // Check if any tile offsets are invalid.
  177. //
  178. // Invalid offsets mean that the file is probably incomplete
  179. // (the offset table is the last thing written to the file).
  180. // Either some process is still busy writing the file, or
  181. // writing the file was aborted.
  182. //
  183. // We should still be able to read the existing parts of the
  184. // file. In order to do this, we have to make a sequential
  185. // scan over the scan tile to reconstruct the tile offset
  186. // table.
  187. //
  188. if (anyOffsetsAreInvalid())
  189. {
  190. complete = false;
  191. reconstructFromFile (is,isMultiPartFile,isDeep);
  192. }
  193. else
  194. {
  195. complete = true;
  196. }
  197. }
  198. void
  199. TileOffsets::readFrom (std::vector<Int64> chunkOffsets,bool &complete)
  200. {
  201. size_t totalSize = 0;
  202. for (unsigned int l = 0; l < _offsets.size(); ++l)
  203. for (unsigned int dy = 0; dy < _offsets[l].size(); ++dy)
  204. totalSize += _offsets[l][dy].size();
  205. if (chunkOffsets.size() != totalSize)
  206. throw IEX_NAMESPACE::ArgExc ("Wrong offset count, not able to read from this array");
  207. int pos = 0;
  208. for (size_t l = 0; l < _offsets.size(); ++l)
  209. for (size_t dy = 0; dy < _offsets[l].size(); ++dy)
  210. for (size_t dx = 0; dx < _offsets[l][dy].size(); ++dx)
  211. {
  212. _offsets[l][dy][dx] = chunkOffsets[pos];
  213. pos++;
  214. }
  215. complete = !anyOffsetsAreInvalid();
  216. }
  217. Int64
  218. TileOffsets::writeTo (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os) const
  219. {
  220. //
  221. // Write the tile offset table to the file, and
  222. // return the position of the start of the table
  223. // in the file.
  224. //
  225. Int64 pos = os.tellp();
  226. if (pos == -1)
  227. IEX_NAMESPACE::throwErrnoExc ("Cannot determine current file position (%T).");
  228. for (unsigned int l = 0; l < _offsets.size(); ++l)
  229. for (unsigned int dy = 0; dy < _offsets[l].size(); ++dy)
  230. for (unsigned int dx = 0; dx < _offsets[l][dy].size(); ++dx)
  231. OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (os, _offsets[l][dy][dx]);
  232. return pos;
  233. }
  234. namespace {
  235. struct tilepos{
  236. Int64 filePos;
  237. int dx;
  238. int dy;
  239. int l;
  240. bool operator <(const tilepos & other) const
  241. {
  242. return filePos < other.filePos;
  243. }
  244. };
  245. }
  246. //-------------------------------------
  247. // fill array with tile coordinates in the order they appear in the file
  248. //
  249. // each input array must be of size (totalTiles)
  250. //
  251. //
  252. // if the tile order is not RANDOM_Y, it is more efficient to compute the
  253. // tile ordering rather than using this function
  254. //
  255. //-------------------------------------
  256. void TileOffsets::getTileOrder(int dx_table[],int dy_table[],int lx_table[],int ly_table[]) const
  257. {
  258. //
  259. // helper class
  260. //
  261. // how many entries?
  262. size_t entries=0;
  263. for (unsigned int l = 0; l < _offsets.size(); ++l)
  264. for (unsigned int dy = 0; dy < _offsets[l].size(); ++dy)
  265. entries+=_offsets[l][dy].size();
  266. std::vector<struct tilepos> table(entries);
  267. size_t i = 0;
  268. for (unsigned int l = 0; l < _offsets.size(); ++l)
  269. for (unsigned int dy = 0; dy < _offsets[l].size(); ++dy)
  270. for (unsigned int dx = 0; dx < _offsets[l][dy].size(); ++dx)
  271. {
  272. table[i].filePos = _offsets[l][dy][dx];
  273. table[i].dx = dx;
  274. table[i].dy = dy;
  275. table[i].l = l;
  276. ++i;
  277. }
  278. std::sort(table.begin(),table.end());
  279. //
  280. // write out the values
  281. //
  282. // pass 1: write out dx and dy, since these are independent of level mode
  283. for(size_t i=0;i<entries;i++)
  284. {
  285. dx_table[i] = table[i].dx;
  286. dy_table[i] = table[i].dy;
  287. }
  288. // now write out the levels, which depend on the level mode
  289. switch (_mode)
  290. {
  291. case ONE_LEVEL:
  292. {
  293. for(size_t i=0;i<entries;i++)
  294. {
  295. lx_table[i] = 0;
  296. ly_table[i] = 0;
  297. }
  298. break;
  299. }
  300. case MIPMAP_LEVELS:
  301. {
  302. for(size_t i=0;i<entries;i++)
  303. {
  304. lx_table[i]= table[i].l;
  305. ly_table[i] =table[i].l;
  306. }
  307. break;
  308. }
  309. case RIPMAP_LEVELS:
  310. {
  311. for(size_t i=0;i<entries;i++)
  312. {
  313. lx_table[i]= table[i].l % _numXLevels;
  314. ly_table[i] = table[i].l / _numXLevels;
  315. }
  316. break;
  317. }
  318. case NUM_LEVELMODES :
  319. throw IEX_NAMESPACE::LogicExc("Bad level mode getting tile order");
  320. }
  321. }
  322. bool
  323. TileOffsets::isEmpty () const
  324. {
  325. for (unsigned int l = 0; l < _offsets.size(); ++l)
  326. for (unsigned int dy = 0; dy < _offsets[l].size(); ++dy)
  327. for (unsigned int dx = 0; dx < _offsets[l][dy].size(); ++dx)
  328. if (_offsets[l][dy][dx] != 0)
  329. return false;
  330. return true;
  331. }
  332. bool
  333. TileOffsets::isValidTile (int dx, int dy, int lx, int ly) const
  334. {
  335. if(lx<0 || ly < 0 || dx<0 || dy < 0) return false;
  336. switch (_mode)
  337. {
  338. case ONE_LEVEL:
  339. if (lx == 0 &&
  340. ly == 0 &&
  341. _offsets.size() > 0 &&
  342. int(_offsets[0].size()) > dy &&
  343. int(_offsets[0][dy].size()) > dx)
  344. {
  345. return true;
  346. }
  347. break;
  348. case MIPMAP_LEVELS:
  349. if (lx < _numXLevels &&
  350. ly < _numYLevels &&
  351. int(_offsets.size()) > lx &&
  352. int(_offsets[lx].size()) > dy &&
  353. int(_offsets[lx][dy].size()) > dx)
  354. {
  355. return true;
  356. }
  357. break;
  358. case RIPMAP_LEVELS:
  359. if (lx < _numXLevels &&
  360. ly < _numYLevels &&
  361. (_offsets.size() > (size_t) lx+ ly * (size_t) _numXLevels) &&
  362. int(_offsets[lx + ly * _numXLevels].size()) > dy &&
  363. int(_offsets[lx + ly * _numXLevels][dy].size()) > dx)
  364. {
  365. return true;
  366. }
  367. break;
  368. default:
  369. return false;
  370. }
  371. return false;
  372. }
  373. Int64 &
  374. TileOffsets::operator () (int dx, int dy, int lx, int ly)
  375. {
  376. //
  377. // Looks up the value of the tile with tile coordinate (dx, dy)
  378. // and level number (lx, ly) in the _offsets array, and returns
  379. // the cooresponding offset.
  380. //
  381. switch (_mode)
  382. {
  383. case ONE_LEVEL:
  384. return _offsets[0][dy][dx];
  385. break;
  386. case MIPMAP_LEVELS:
  387. return _offsets[lx][dy][dx];
  388. break;
  389. case RIPMAP_LEVELS:
  390. return _offsets[lx + ly * _numXLevels][dy][dx];
  391. break;
  392. default:
  393. throw IEX_NAMESPACE::ArgExc ("Unknown LevelMode format.");
  394. }
  395. }
  396. Int64 &
  397. TileOffsets::operator () (int dx, int dy, int l)
  398. {
  399. return operator () (dx, dy, l, l);
  400. }
  401. const Int64 &
  402. TileOffsets::operator () (int dx, int dy, int lx, int ly) const
  403. {
  404. //
  405. // Looks up the value of the tile with tile coordinate (dx, dy)
  406. // and level number (lx, ly) in the _offsets array, and returns
  407. // the cooresponding offset.
  408. //
  409. switch (_mode)
  410. {
  411. case ONE_LEVEL:
  412. return _offsets[0][dy][dx];
  413. break;
  414. case MIPMAP_LEVELS:
  415. return _offsets[lx][dy][dx];
  416. break;
  417. case RIPMAP_LEVELS:
  418. return _offsets[lx + ly * _numXLevels][dy][dx];
  419. break;
  420. default:
  421. throw IEX_NAMESPACE::ArgExc ("Unknown LevelMode format.");
  422. }
  423. }
  424. const Int64 &
  425. TileOffsets::operator () (int dx, int dy, int l) const
  426. {
  427. return operator () (dx, dy, l, l);
  428. }
  429. const std::vector<std::vector<std::vector <Int64> > >&
  430. TileOffsets::getOffsets() const
  431. {
  432. return _offsets;
  433. }
  434. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT