ImfDeepTiledOutputFile.cpp 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2011, 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 DeepTiledOutputFile
  37. //
  38. //-----------------------------------------------------------------------------
  39. #include "ImfDeepTiledOutputFile.h"
  40. #include "ImfDeepTiledInputFile.h"
  41. #include "ImfDeepTiledInputPart.h"
  42. #include "ImfInputFile.h"
  43. #include "ImfTileDescriptionAttribute.h"
  44. #include "ImfPreviewImageAttribute.h"
  45. #include "ImfChannelList.h"
  46. #include "ImfMisc.h"
  47. #include "ImfTiledMisc.h"
  48. #include "ImfStdIO.h"
  49. #include "ImfCompressor.h"
  50. #include "ImfOutputStreamMutex.h"
  51. #include "ImfOutputPartData.h"
  52. #include "ImfArray.h"
  53. #include "ImfXdr.h"
  54. #include "ImfVersion.h"
  55. #include "ImfTileOffsets.h"
  56. #include "ImfThreading.h"
  57. #include "ImfPartType.h"
  58. #include "ImathBox.h"
  59. #include "IlmThreadPool.h"
  60. #include "IlmThreadSemaphore.h"
  61. #include "IlmThreadMutex.h"
  62. #include "Iex.h"
  63. #include <string>
  64. #include <vector>
  65. #include <fstream>
  66. #include <assert.h>
  67. #include <map>
  68. #include <algorithm>
  69. #include "ImfNamespace.h"
  70. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
  71. using IMATH_NAMESPACE::Box2i;
  72. using IMATH_NAMESPACE::V2i;
  73. using std::string;
  74. using std::vector;
  75. using std::ofstream;
  76. using std::map;
  77. using std::min;
  78. using std::max;
  79. using std::swap;
  80. using ILMTHREAD_NAMESPACE::Mutex;
  81. using ILMTHREAD_NAMESPACE::Lock;
  82. using ILMTHREAD_NAMESPACE::Semaphore;
  83. using ILMTHREAD_NAMESPACE::Task;
  84. using ILMTHREAD_NAMESPACE::TaskGroup;
  85. using ILMTHREAD_NAMESPACE::ThreadPool;
  86. namespace {
  87. struct TOutSliceInfo
  88. {
  89. PixelType type;
  90. const char * base;
  91. size_t sampleStride;
  92. size_t xStride;
  93. size_t yStride;
  94. bool zero;
  95. int xTileCoords;
  96. int yTileCoords;
  97. TOutSliceInfo (PixelType type = HALF,
  98. size_t sampleStride = 0,
  99. size_t xStride = 0,
  100. size_t yStride = 0,
  101. bool zero = false,
  102. int xTileCoords = 0,
  103. int yTileCoords = 0);
  104. };
  105. TOutSliceInfo::TOutSliceInfo (PixelType t,
  106. size_t spst,
  107. size_t xStride,
  108. size_t yStride,
  109. bool z,
  110. int xtc,
  111. int ytc)
  112. :
  113. type (t),
  114. sampleStride (spst),
  115. xStride(xStride),
  116. yStride(yStride),
  117. zero (z),
  118. xTileCoords (xtc),
  119. yTileCoords (ytc)
  120. {
  121. // empty
  122. }
  123. struct TileCoord
  124. {
  125. int dx;
  126. int dy;
  127. int lx;
  128. int ly;
  129. TileCoord (int xTile = 0, int yTile = 0,
  130. int xLevel = 0, int yLevel = 0)
  131. :
  132. dx (xTile), dy (yTile),
  133. lx (xLevel), ly (yLevel)
  134. {
  135. // empty
  136. }
  137. bool
  138. operator < (const TileCoord &other) const
  139. {
  140. return (ly < other.ly) ||
  141. (ly == other.ly && lx < other.lx) ||
  142. ((ly == other.ly && lx == other.lx) &&
  143. ((dy < other.dy) || (dy == other.dy && dx < other.dx)));
  144. }
  145. bool
  146. operator == (const TileCoord &other) const
  147. {
  148. return lx == other.lx &&
  149. ly == other.ly &&
  150. dx == other.dx &&
  151. dy == other.dy;
  152. }
  153. };
  154. struct BufferedTile
  155. {
  156. char * pixelData;
  157. Int64 pixelDataSize;
  158. Int64 unpackedDataSize;
  159. char * sampleCountTableData;
  160. Int64 sampleCountTableSize;
  161. BufferedTile (const char *data, int size, int unpackedSize,
  162. const char *tableData, int tableSize):
  163. pixelData (0),
  164. pixelDataSize(size),
  165. unpackedDataSize(unpackedSize),
  166. sampleCountTableData(0),
  167. sampleCountTableSize(tableSize)
  168. {
  169. pixelData = new char[pixelDataSize];
  170. memcpy (pixelData, data, pixelDataSize);
  171. sampleCountTableData = new char[tableSize];
  172. memcpy (sampleCountTableData, tableData, tableSize);
  173. }
  174. ~BufferedTile()
  175. {
  176. delete [] pixelData;
  177. delete [] sampleCountTableData;
  178. }
  179. };
  180. typedef map <TileCoord, BufferedTile *> TileMap;
  181. struct TileBuffer
  182. {
  183. Array<char> buffer;
  184. const char * dataPtr;
  185. Int64 dataSize;
  186. Int64 uncompressedSize;
  187. Compressor * compressor;
  188. Array<char> sampleCountTableBuffer;
  189. const char * sampleCountTablePtr;
  190. Int64 sampleCountTableSize;
  191. Compressor* sampleCountTableCompressor;
  192. TileCoord tileCoord;
  193. bool hasException;
  194. string exception;
  195. TileBuffer ();
  196. ~TileBuffer ();
  197. inline void wait () {_sem.wait();}
  198. inline void post () {_sem.post();}
  199. protected:
  200. Semaphore _sem;
  201. };
  202. TileBuffer::TileBuffer ():
  203. dataPtr (0),
  204. dataSize (0),
  205. compressor (0),
  206. sampleCountTablePtr (0),
  207. sampleCountTableCompressor (0),
  208. hasException (false),
  209. exception (),
  210. _sem (1)
  211. {
  212. // empty
  213. }
  214. TileBuffer::~TileBuffer ()
  215. {
  216. if (compressor != 0)
  217. delete compressor;
  218. if (sampleCountTableCompressor != 0)
  219. delete sampleCountTableCompressor;
  220. }
  221. } // namespace
  222. struct DeepTiledOutputFile::Data
  223. {
  224. Header header; // the image header
  225. int version; // file format version
  226. bool multipart; // file is multipart
  227. TileDescription tileDesc; // describes the tile layout
  228. DeepFrameBuffer frameBuffer; // framebuffer to write into
  229. Int64 previewPosition;
  230. LineOrder lineOrder; // the file's lineorder
  231. int minX; // data window's min x coord
  232. int maxX; // data window's max x coord
  233. int minY; // data window's min y coord
  234. int maxY; // data window's max x coord
  235. int numXLevels; // number of x levels
  236. int numYLevels; // number of y levels
  237. int * numXTiles; // number of x tiles at a level
  238. int * numYTiles; // number of y tiles at a level
  239. TileOffsets tileOffsets; // stores offsets in file for
  240. // each tile
  241. Compressor::Format format; // compressor's data format
  242. vector<TOutSliceInfo*> slices; // info about channels in file
  243. vector<TileBuffer*> tileBuffers;
  244. Int64 tileOffsetsPosition; // position of the tile index
  245. TileMap tileMap; // the map of buffered tiles
  246. TileCoord nextTileToWrite;
  247. int partNumber; // the output part number
  248. char* sampleCountSliceBase; // the pointer to the number
  249. // of samples in each pixel
  250. int sampleCountXStride; // the x stride for sampleCountSliceBase
  251. int sampleCountYStride; // the y stride for sampleCountSliceBase
  252. int sampleCountXTileCoords; // using x coordinates relative to current tile
  253. int sampleCountYTileCoords; // using y coordinates relative to current tile
  254. Int64 maxSampleCountTableSize;// the max size in bytes for a pixel
  255. // sample count table
  256. OutputStreamMutex* _streamData;
  257. bool _deleteStream;
  258. Data (int numThreads);
  259. ~Data ();
  260. inline TileBuffer * getTileBuffer (int number);
  261. // hash function from tile
  262. // buffer coords into our
  263. // vector of tile buffers
  264. int& getSampleCount(int x, int y);
  265. // get the number of samples
  266. // in each pixel
  267. TileCoord nextTileCoord (const TileCoord &a);
  268. };
  269. DeepTiledOutputFile::Data::Data (int numThreads):
  270. numXTiles(0),
  271. numYTiles(0),
  272. tileOffsetsPosition (0),
  273. partNumber(-1),
  274. _streamData(NULL),
  275. _deleteStream(true)
  276. {
  277. //
  278. // We need at least one tileBuffer, but if threading is used,
  279. // to keep n threads busy we need 2*n tileBuffers
  280. //
  281. tileBuffers.resize (max (1, 2 * numThreads));
  282. for (size_t i = 0; i < tileBuffers.size(); i++)
  283. tileBuffers[i] = 0;
  284. }
  285. DeepTiledOutputFile::Data::~Data ()
  286. {
  287. delete [] numXTiles;
  288. delete [] numYTiles;
  289. //
  290. // Delete all the tile buffers, if any still happen to exist
  291. //
  292. for (TileMap::iterator i = tileMap.begin(); i != tileMap.end(); ++i)
  293. delete i->second;
  294. for (size_t i = 0; i < tileBuffers.size(); i++)
  295. if (tileBuffers[i] != 0)
  296. delete tileBuffers[i];
  297. for (size_t i = 0; i < slices.size(); i++)
  298. delete slices[i];
  299. }
  300. int&
  301. DeepTiledOutputFile::Data::getSampleCount(int x, int y)
  302. {
  303. return sampleCount(sampleCountSliceBase,
  304. sampleCountXStride,
  305. sampleCountYStride,
  306. x, y);
  307. }
  308. TileBuffer*
  309. DeepTiledOutputFile::Data::getTileBuffer (int number)
  310. {
  311. return tileBuffers[number % tileBuffers.size()];
  312. }
  313. TileCoord
  314. DeepTiledOutputFile::Data::nextTileCoord (const TileCoord &a)
  315. {
  316. TileCoord b = a;
  317. if (lineOrder == INCREASING_Y)
  318. {
  319. b.dx++;
  320. if (b.dx >= numXTiles[b.lx])
  321. {
  322. b.dx = 0;
  323. b.dy++;
  324. if (b.dy >= numYTiles[b.ly])
  325. {
  326. //
  327. // the next tile is in the next level
  328. //
  329. b.dy = 0;
  330. switch (tileDesc.mode)
  331. {
  332. case ONE_LEVEL:
  333. case MIPMAP_LEVELS:
  334. b.lx++;
  335. b.ly++;
  336. break;
  337. case RIPMAP_LEVELS:
  338. b.lx++;
  339. if (b.lx >= numXLevels)
  340. {
  341. b.lx = 0;
  342. b.ly++;
  343. #ifdef DEBUG
  344. assert (b.ly <= numYLevels);
  345. #endif
  346. }
  347. break;
  348. case NUM_LEVELMODES :
  349. throw IEX_NAMESPACE::LogicExc("unknown level mode computing nextTileCoord");
  350. }
  351. }
  352. }
  353. }
  354. else if (lineOrder == DECREASING_Y)
  355. {
  356. b.dx++;
  357. if (b.dx >= numXTiles[b.lx])
  358. {
  359. b.dx = 0;
  360. b.dy--;
  361. if (b.dy < 0)
  362. {
  363. //
  364. // the next tile is in the next level
  365. //
  366. switch (tileDesc.mode)
  367. {
  368. case ONE_LEVEL:
  369. case MIPMAP_LEVELS:
  370. b.lx++;
  371. b.ly++;
  372. break;
  373. case RIPMAP_LEVELS:
  374. b.lx++;
  375. if (b.lx >= numXLevels)
  376. {
  377. b.lx = 0;
  378. b.ly++;
  379. #ifdef DEBUG
  380. assert (b.ly <= numYLevels);
  381. #endif
  382. }
  383. break;
  384. case NUM_LEVELMODES :
  385. throw IEX_NAMESPACE::LogicExc("unknown level mode computing nextTileCoord");
  386. }
  387. if (b.ly < numYLevels)
  388. b.dy = numYTiles[b.ly] - 1;
  389. }
  390. }
  391. }else if(lineOrder==RANDOM_Y)
  392. {
  393. THROW (IEX_NAMESPACE::ArgExc,
  394. "can't compute next tile from randomly ordered image: use getTilesInOrder instead");
  395. }
  396. return b;
  397. }
  398. namespace {
  399. void
  400. writeTileData (DeepTiledOutputFile::Data *ofd,
  401. int dx, int dy,
  402. int lx, int ly,
  403. const char pixelData[],
  404. Int64 pixelDataSize,
  405. Int64 unpackedDataSize,
  406. const char sampleCountTableData[],
  407. Int64 sampleCountTableSize)
  408. {
  409. //
  410. // Store a block of pixel data in the output file, and try
  411. // to keep track of the current writing position the file,
  412. // without calling tellp() (tellp() can be fairly expensive).
  413. //
  414. Int64 currentPosition = ofd->_streamData->currentPosition;
  415. ofd->_streamData->currentPosition = 0;
  416. if (currentPosition == 0)
  417. currentPosition = ofd->_streamData->os->tellp();
  418. ofd->tileOffsets (dx, dy, lx, ly) = currentPosition;
  419. #ifdef DEBUG
  420. assert (ofd->_streamData->os->tellp() == currentPosition);
  421. #endif
  422. //
  423. // Write the tile header.
  424. //
  425. if (ofd->multipart)
  426. {
  427. Xdr::write <StreamIO> (*ofd->_streamData->os, ofd->partNumber);
  428. }
  429. Xdr::write <StreamIO> (*ofd->_streamData->os, dx);
  430. Xdr::write <StreamIO> (*ofd->_streamData->os, dy);
  431. Xdr::write <StreamIO> (*ofd->_streamData->os, lx);
  432. Xdr::write <StreamIO> (*ofd->_streamData->os, ly);
  433. //
  434. // Write the packed size of the pixel sample count table (64 bits)
  435. //
  436. Xdr::write <StreamIO> (*ofd->_streamData->os, sampleCountTableSize);
  437. //
  438. // Write the packed and unpacked data size (64 bits each)
  439. //
  440. Xdr::write <StreamIO> (*ofd->_streamData->os, pixelDataSize);
  441. Xdr::write <StreamIO> (*ofd->_streamData->os, unpackedDataSize);
  442. //
  443. // Write the compressed pixel sample count table.
  444. //
  445. ofd->_streamData->os->write (sampleCountTableData, sampleCountTableSize);
  446. //
  447. // Write the compressed data.
  448. //
  449. ofd->_streamData->os->write (pixelData, pixelDataSize);
  450. //
  451. // Keep current position in the file so that we can avoid
  452. // redundant seekg() operations (seekg() can be fairly expensive).
  453. //
  454. ofd->_streamData->currentPosition = currentPosition +
  455. 4 * Xdr::size<int>() + // dx, dy, lx, ly,
  456. 3 * Xdr::size<Int64>() + // sampleCountTableSize,
  457. // pixelDataSize,
  458. // unpackedDataSize
  459. sampleCountTableSize +
  460. pixelDataSize;
  461. if (ofd->multipart)
  462. {
  463. ofd->_streamData->currentPosition += Xdr::size<int>();
  464. }
  465. }
  466. void
  467. bufferedTileWrite (
  468. DeepTiledOutputFile::Data *ofd,
  469. int dx, int dy,
  470. int lx, int ly,
  471. const char pixelData[],
  472. Int64 pixelDataSize,
  473. Int64 unpackedDataSize,
  474. const char sampleCountTableData[],
  475. Int64 sampleCountTableSize)
  476. {
  477. //
  478. // Check if a tile with coordinates (dx,dy,lx,ly) has already been written.
  479. //
  480. if (ofd->tileOffsets (dx, dy, lx, ly))
  481. {
  482. THROW (IEX_NAMESPACE::ArgExc,
  483. "Attempt to write tile "
  484. "(" << dx << ", " << dy << ", " << lx << ", " << ly << ") "
  485. "more than once.");
  486. }
  487. //
  488. // If tiles can be written in random order, then don't buffer anything.
  489. //
  490. if (ofd->lineOrder == RANDOM_Y)
  491. {
  492. writeTileData (ofd, dx, dy, lx, ly,
  493. pixelData, pixelDataSize, unpackedDataSize,
  494. sampleCountTableData, sampleCountTableSize);
  495. return;
  496. }
  497. //
  498. // If the tiles cannot be written in random order, then check if a
  499. // tile with coordinates (dx,dy,lx,ly) has already been buffered.
  500. //
  501. TileCoord currentTile = TileCoord(dx, dy, lx, ly);
  502. if (ofd->tileMap.find (currentTile) != ofd->tileMap.end())
  503. {
  504. THROW (IEX_NAMESPACE::ArgExc,
  505. "Attempt to write tile "
  506. "(" << dx << ", " << dy << ", " << lx << ", " << ly << ") "
  507. "more than once.");
  508. }
  509. //
  510. // If all the tiles before this one have already been written to the file,
  511. // then write this tile immediately and check if we have buffered tiles
  512. // that can be written after this tile.
  513. //
  514. // Otherwise, buffer the tile so it can be written to file later.
  515. //
  516. if (ofd->nextTileToWrite == currentTile)
  517. {
  518. writeTileData (ofd, dx, dy, lx, ly,
  519. pixelData, pixelDataSize, unpackedDataSize,
  520. sampleCountTableData, sampleCountTableSize);
  521. ofd->nextTileToWrite = ofd->nextTileCoord (ofd->nextTileToWrite);
  522. TileMap::iterator i = ofd->tileMap.find (ofd->nextTileToWrite);
  523. //
  524. // Step through the tiles and write all successive buffered tiles after
  525. // the current one.
  526. //
  527. while(i != ofd->tileMap.end())
  528. {
  529. //
  530. // Write the tile, and then delete the tile's buffered data
  531. //
  532. writeTileData (ofd,
  533. i->first.dx, i->first.dy,
  534. i->first.lx, i->first.ly,
  535. i->second->pixelData,
  536. i->second->pixelDataSize,
  537. i->second->unpackedDataSize,
  538. i->second->sampleCountTableData,
  539. i->second->sampleCountTableSize);
  540. delete i->second;
  541. ofd->tileMap.erase (i);
  542. //
  543. // Proceed to the next tile
  544. //
  545. ofd->nextTileToWrite = ofd->nextTileCoord (ofd->nextTileToWrite);
  546. i = ofd->tileMap.find (ofd->nextTileToWrite);
  547. }
  548. }
  549. else
  550. {
  551. //
  552. // Create a new BufferedTile, copy the pixelData into it, and
  553. // insert it into the tileMap.
  554. //
  555. ofd->tileMap[currentTile] =
  556. new BufferedTile ((const char *)pixelData, pixelDataSize, unpackedDataSize,
  557. sampleCountTableData, sampleCountTableSize);
  558. }
  559. }
  560. void
  561. convertToXdr (DeepTiledOutputFile::Data *ofd,
  562. Array<char>& tileBuffer,
  563. int numScanLines,
  564. vector<Int64>& bytesPerLine)
  565. {
  566. //
  567. // Convert the contents of a TiledOutputFile's tileBuffer from the
  568. // machine's native representation to Xdr format. This function is called
  569. // by writeTile(), below, if the compressor wanted its input pixel data
  570. // in the machine's native format, but then failed to compress the data
  571. // (most compressors will expand rather than compress random input data).
  572. //
  573. // Note that this routine assumes that the machine's native representation
  574. // of the pixel data has the same size as the Xdr representation. This
  575. // makes it possible to convert the pixel data in place, without an
  576. // intermediate temporary buffer.
  577. //
  578. //
  579. // Set these to point to the start of the tile.
  580. // We will write to toPtr, and read from fromPtr.
  581. //
  582. char *writePtr = tileBuffer;
  583. const char *readPtr = writePtr;
  584. //
  585. // Iterate over all scan lines in the tile.
  586. //
  587. for (int y = 0; y < numScanLines; ++y)
  588. {
  589. //
  590. // Iterate over all slices in the file.
  591. //
  592. for (unsigned int i = 0; i < ofd->slices.size(); ++i)
  593. {
  594. const TOutSliceInfo &slice = *ofd->slices[i];
  595. //
  596. // Convert the samples in place.
  597. //
  598. Int64 numPixelsPerScanLine = bytesPerLine[y];
  599. convertInPlace (writePtr, readPtr, slice.type,
  600. numPixelsPerScanLine);
  601. }
  602. }
  603. #ifdef DEBUG
  604. assert (writePtr == readPtr);
  605. #endif
  606. }
  607. //
  608. // A TileBufferTask encapsulates the task of copying a tile from
  609. // the user's framebuffer into a LineBuffer and compressing the data
  610. // if necessary.
  611. //
  612. class TileBufferTask: public Task
  613. {
  614. public:
  615. TileBufferTask (TaskGroup *group,
  616. DeepTiledOutputFile::Data *ofd,
  617. int number,
  618. int dx, int dy,
  619. int lx, int ly);
  620. virtual ~TileBufferTask ();
  621. virtual void execute ();
  622. private:
  623. DeepTiledOutputFile::Data * _ofd;
  624. TileBuffer * _tileBuffer;
  625. };
  626. TileBufferTask::TileBufferTask
  627. (TaskGroup *group,
  628. DeepTiledOutputFile::Data *ofd,
  629. int number,
  630. int dx, int dy,
  631. int lx, int ly)
  632. :
  633. Task (group),
  634. _ofd (ofd),
  635. _tileBuffer (_ofd->getTileBuffer (number))
  636. {
  637. //
  638. // Wait for the tileBuffer to become available
  639. //
  640. _tileBuffer->wait ();
  641. _tileBuffer->tileCoord = TileCoord (dx, dy, lx, ly);
  642. }
  643. TileBufferTask::~TileBufferTask ()
  644. {
  645. //
  646. // Signal that the tile buffer is now free
  647. //
  648. _tileBuffer->post ();
  649. }
  650. void
  651. TileBufferTask::execute ()
  652. {
  653. try
  654. {
  655. //
  656. // First copy the pixel data from the frame buffer
  657. // into the tile buffer
  658. //
  659. // Convert one tile's worth of pixel data to
  660. // a machine-independent representation, and store
  661. // the result in _tileBuffer->buffer.
  662. //
  663. Box2i tileRange = OPENEXR_IMF_INTERNAL_NAMESPACE::dataWindowForTile (
  664. _ofd->tileDesc,
  665. _ofd->minX, _ofd->maxX,
  666. _ofd->minY, _ofd->maxY,
  667. _tileBuffer->tileCoord.dx,
  668. _tileBuffer->tileCoord.dy,
  669. _tileBuffer->tileCoord.lx,
  670. _tileBuffer->tileCoord.ly);
  671. int numScanLines = tileRange.max.y - tileRange.min.y + 1;
  672. // int numPixelsPerScanLine = tileRange.max.x - tileRange.min.x + 1;
  673. //
  674. // Get the bytes for each line.
  675. //
  676. vector<Int64> bytesPerLine(_ofd->tileDesc.ySize);
  677. vector<int> xOffsets(_ofd->slices.size());
  678. vector<int> yOffsets(_ofd->slices.size());
  679. for (size_t i = 0; i < _ofd->slices.size(); i++)
  680. {
  681. const TOutSliceInfo &slice = *_ofd->slices[i];
  682. xOffsets[i] = slice.xTileCoords * tileRange.min.x;
  683. yOffsets[i] = slice.yTileCoords * tileRange.min.y;
  684. }
  685. calculateBytesPerLine(_ofd->header,
  686. _ofd->sampleCountSliceBase,
  687. _ofd->sampleCountXStride,
  688. _ofd->sampleCountYStride,
  689. tileRange.min.x, tileRange.max.x,
  690. tileRange.min.y, tileRange.max.y,
  691. xOffsets, yOffsets,
  692. bytesPerLine);
  693. //
  694. // Allocate the memory for internal buffer.
  695. // (TODO) more efficient memory management?
  696. //
  697. Int64 totalBytes = 0;
  698. Int64 maxBytesPerTileLine = 0;
  699. for (size_t i = 0; i < bytesPerLine.size(); i++)
  700. {
  701. totalBytes += bytesPerLine[i];
  702. if (bytesPerLine[i] > maxBytesPerTileLine)
  703. maxBytesPerTileLine = bytesPerLine[i];
  704. }
  705. _tileBuffer->buffer.resizeErase(totalBytes);
  706. char *writePtr = _tileBuffer->buffer;
  707. //
  708. // Iterate over the scan lines in the tile.
  709. //
  710. int xOffsetForSampleCount =
  711. (_ofd->sampleCountXTileCoords == 0) ? 0 : tileRange.min.x;
  712. int yOffsetForSampleCount =
  713. (_ofd->sampleCountYTileCoords == 0) ? 0 : tileRange.min.y;
  714. for (int y = tileRange.min.y; y <= tileRange.max.y; ++y)
  715. {
  716. //
  717. // Iterate over all image channels.
  718. //
  719. for (unsigned int i = 0; i < _ofd->slices.size(); ++i)
  720. {
  721. const TOutSliceInfo &slice = *_ofd->slices[i];
  722. //
  723. // Fill the tile buffer with pixel data.
  724. //
  725. if (slice.zero)
  726. {
  727. //
  728. // The frame buffer contains no data for this channel.
  729. // Store zeroes in _data->tileBuffer.
  730. //
  731. fillChannelWithZeroes (writePtr, _ofd->format, slice.type,
  732. bytesPerLine[y - tileRange.min.y]);
  733. }
  734. else
  735. {
  736. //
  737. // The frame buffer contains data for this channel.
  738. //
  739. int xOffsetForData = slice.xTileCoords ? tileRange.min.x : 0;
  740. int yOffsetForData = slice.yTileCoords ? tileRange.min.y : 0;
  741. // (TOOD) treat sample count offsets differently.
  742. copyFromDeepFrameBuffer (writePtr,
  743. slice.base,
  744. _ofd->sampleCountSliceBase,
  745. _ofd->sampleCountXStride,
  746. _ofd->sampleCountYStride,
  747. y,
  748. tileRange.min.x,
  749. tileRange.max.x,
  750. xOffsetForSampleCount,
  751. yOffsetForSampleCount,
  752. xOffsetForData,
  753. yOffsetForData,
  754. slice.sampleStride,
  755. slice.xStride,
  756. slice.yStride,
  757. _ofd->format,
  758. slice.type);
  759. #if defined(DEBUG)
  760. assert(writePtr-_tileBuffer->buffer<=totalBytes);
  761. #endif
  762. }
  763. }
  764. }
  765. //
  766. // Compress the pixel sample count table.
  767. //
  768. char* ptr = _tileBuffer->sampleCountTableBuffer;
  769. Int64 tableDataSize = 0;
  770. for (int i = tileRange.min.y; i <= tileRange.max.y; i++)
  771. {
  772. int count = 0;
  773. for (int j = tileRange.min.x; j <= tileRange.max.x; j++)
  774. {
  775. count += _ofd->getSampleCount(j - xOffsetForSampleCount,
  776. i - yOffsetForSampleCount);
  777. Xdr::write <CharPtrIO> (ptr, count);
  778. tableDataSize += sizeof (int);
  779. }
  780. }
  781. if(_tileBuffer->sampleCountTableCompressor)
  782. {
  783. _tileBuffer->sampleCountTableSize =
  784. _tileBuffer->sampleCountTableCompressor->compress (
  785. _tileBuffer->sampleCountTableBuffer,
  786. tableDataSize,
  787. tileRange.min.y,
  788. _tileBuffer->sampleCountTablePtr);
  789. }
  790. //
  791. // If we can't make data shrink (or compression was disabled), then just use the raw data.
  792. //
  793. if ( ! _tileBuffer->sampleCountTableCompressor ||
  794. _tileBuffer->sampleCountTableSize >= _ofd->maxSampleCountTableSize)
  795. {
  796. _tileBuffer->sampleCountTableSize = _ofd->maxSampleCountTableSize;
  797. _tileBuffer->sampleCountTablePtr = _tileBuffer->sampleCountTableBuffer;
  798. }
  799. //
  800. // Compress the contents of the tileBuffer,
  801. // and store the compressed data in the output file.
  802. //
  803. _tileBuffer->dataSize = writePtr - _tileBuffer->buffer;
  804. _tileBuffer->uncompressedSize = _tileBuffer->dataSize;
  805. _tileBuffer->dataPtr = _tileBuffer->buffer;
  806. // (TODO) don't do this all the time.
  807. if (_tileBuffer->compressor != 0)
  808. delete _tileBuffer->compressor;
  809. _tileBuffer->compressor = newTileCompressor
  810. (_ofd->header.compression(),
  811. maxBytesPerTileLine,
  812. _ofd->tileDesc.ySize,
  813. _ofd->header);
  814. if (_tileBuffer->compressor)
  815. {
  816. const char *compPtr;
  817. Int64 compSize = _tileBuffer->compressor->compressTile
  818. (_tileBuffer->dataPtr,
  819. _tileBuffer->dataSize,
  820. tileRange, compPtr);
  821. if (compSize < _tileBuffer->dataSize)
  822. {
  823. _tileBuffer->dataSize = compSize;
  824. _tileBuffer->dataPtr = compPtr;
  825. }
  826. else if (_ofd->format == Compressor::NATIVE)
  827. {
  828. //
  829. // The data did not shrink during compression, but
  830. // we cannot write to the file using native format,
  831. // so we need to convert the lineBuffer to Xdr.
  832. //
  833. convertToXdr (_ofd, _tileBuffer->buffer, numScanLines,
  834. bytesPerLine);
  835. }
  836. }
  837. }
  838. catch (std::exception &e)
  839. {
  840. if (!_tileBuffer->hasException)
  841. {
  842. _tileBuffer->exception = e.what ();
  843. _tileBuffer->hasException = true;
  844. }
  845. }
  846. catch (...)
  847. {
  848. if (!_tileBuffer->hasException)
  849. {
  850. _tileBuffer->exception = "unrecognized exception";
  851. _tileBuffer->hasException = true;
  852. }
  853. }
  854. }
  855. } // namespace
  856. DeepTiledOutputFile::DeepTiledOutputFile
  857. (const char fileName[],
  858. const Header &header,
  859. int numThreads)
  860. :
  861. _data (new Data (numThreads))
  862. {
  863. _data->_streamData=new OutputStreamMutex();
  864. _data->_deleteStream =true;
  865. try
  866. {
  867. header.sanityCheck (true);
  868. _data->_streamData->os = new StdOFStream (fileName);
  869. initialize (header);
  870. _data->_streamData->currentPosition = _data->_streamData->os->tellp();
  871. // Write header and empty offset table to the file.
  872. writeMagicNumberAndVersionField(*_data->_streamData->os, _data->header);
  873. _data->previewPosition = _data->header.writeTo (*_data->_streamData->os, true);
  874. _data->tileOffsetsPosition = _data->tileOffsets.writeTo (*_data->_streamData->os);
  875. _data->multipart = false;
  876. }
  877. catch (IEX_NAMESPACE::BaseExc &e)
  878. {
  879. if (_data && _data->_streamData && _data->_streamData->os) delete _data->_streamData->os;
  880. if (_data && _data->_streamData) delete _data->_streamData;
  881. if (_data) delete _data;
  882. REPLACE_EXC (e, "Cannot open image file "
  883. "\"" << fileName << "\". " << e.what());
  884. throw;
  885. }
  886. catch (...)
  887. {
  888. if (_data && _data->_streamData && _data->_streamData->os) delete _data->_streamData->os;
  889. if (_data->_streamData) delete _data->_streamData;
  890. if (_data) delete _data;
  891. throw;
  892. }
  893. }
  894. DeepTiledOutputFile::DeepTiledOutputFile
  895. (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os,
  896. const Header &header,
  897. int numThreads)
  898. :
  899. _data (new Data (numThreads))
  900. {
  901. _data->_streamData=new OutputStreamMutex();
  902. _data->_deleteStream=false;
  903. try
  904. {
  905. header.sanityCheck(true);
  906. _data->_streamData->os = &os;
  907. initialize (header);
  908. _data->_streamData->currentPosition = _data->_streamData->os->tellp();
  909. // Write header and empty offset table to the file.
  910. writeMagicNumberAndVersionField(*_data->_streamData->os, _data->header);
  911. _data->previewPosition = _data->header.writeTo (*_data->_streamData->os, true);
  912. _data->tileOffsetsPosition = _data->tileOffsets.writeTo (*_data->_streamData->os);
  913. _data->multipart = false;
  914. }
  915. catch (IEX_NAMESPACE::BaseExc &e)
  916. {
  917. if (_data && _data->_streamData) delete _data->_streamData;
  918. if (_data) delete _data;
  919. REPLACE_EXC (e, "Cannot open image file "
  920. "\"" << os.fileName() << "\". " << e.what());
  921. throw;
  922. }
  923. catch (...)
  924. {
  925. if (_data && _data->_streamData) delete _data->_streamData;
  926. if (_data) delete _data;
  927. throw;
  928. }
  929. }
  930. DeepTiledOutputFile::DeepTiledOutputFile(const OutputPartData* part)
  931. {
  932. try
  933. {
  934. if (part->header.type() != DEEPTILE)
  935. throw IEX_NAMESPACE::ArgExc("Can't build a DeepTiledOutputFile from "
  936. "a type-mismatched part.");
  937. _data = new Data (part->numThreads);
  938. _data->_streamData=part->mutex;
  939. _data->_deleteStream=false;
  940. initialize(part->header);
  941. _data->partNumber = part->partNumber;
  942. _data->tileOffsetsPosition = part->chunkOffsetTablePosition;
  943. _data->previewPosition = part->previewPosition;
  944. _data->multipart = part->multipart;
  945. }
  946. catch (IEX_NAMESPACE::BaseExc &e)
  947. {
  948. if (_data) delete _data;
  949. REPLACE_EXC (e, "Cannot initialize output part "
  950. "\"" << part->partNumber << "\". " << e.what());
  951. throw;
  952. }
  953. catch (...)
  954. {
  955. if (_data) delete _data;
  956. throw;
  957. }
  958. }
  959. void
  960. DeepTiledOutputFile::initialize (const Header &header)
  961. {
  962. _data->header = header;
  963. _data->header.setType(DEEPTILE);
  964. _data->lineOrder = _data->header.lineOrder();
  965. //
  966. // Check that the file is indeed tiled
  967. //
  968. _data->tileDesc = _data->header.tileDescription();
  969. //
  970. // Save the dataWindow information
  971. //
  972. const Box2i &dataWindow = _data->header.dataWindow();
  973. _data->minX = dataWindow.min.x;
  974. _data->maxX = dataWindow.max.x;
  975. _data->minY = dataWindow.min.y;
  976. _data->maxY = dataWindow.max.y;
  977. //
  978. // Precompute level and tile information to speed up utility functions
  979. //
  980. precalculateTileInfo (_data->tileDesc,
  981. _data->minX, _data->maxX,
  982. _data->minY, _data->maxY,
  983. _data->numXTiles, _data->numYTiles,
  984. _data->numXLevels, _data->numYLevels);
  985. //
  986. // Determine the first tile coordinate that we will be writing
  987. // if the file is not RANDOM_Y.
  988. //
  989. _data->nextTileToWrite = (_data->lineOrder == INCREASING_Y)?
  990. TileCoord (0, 0, 0, 0):
  991. TileCoord (0, _data->numYTiles[0] - 1, 0, 0);
  992. Compressor* compressor = newTileCompressor
  993. (_data->header.compression(),
  994. 0,
  995. _data->tileDesc.ySize,
  996. _data->header);
  997. _data->format = defaultFormat (compressor);
  998. if (compressor != 0)
  999. delete compressor;
  1000. _data->tileOffsets = TileOffsets (_data->tileDesc.mode,
  1001. _data->numXLevels,
  1002. _data->numYLevels,
  1003. _data->numXTiles,
  1004. _data->numYTiles);
  1005. //ignore the existing value of chunkCount - correct it if it's wrong
  1006. _data->header.setChunkCount(getChunkOffsetTableSize(_data->header,true));
  1007. _data->maxSampleCountTableSize = _data->tileDesc.ySize *
  1008. _data->tileDesc.xSize *
  1009. sizeof(int);
  1010. for (size_t i = 0; i < _data->tileBuffers.size(); i++)
  1011. {
  1012. _data->tileBuffers[i] = new TileBuffer ();
  1013. _data->tileBuffers[i]->sampleCountTableBuffer.
  1014. resizeErase(_data->maxSampleCountTableSize);
  1015. char * p = &(_data->tileBuffers[i]->sampleCountTableBuffer[0]);
  1016. memset (p, 0, _data->maxSampleCountTableSize);
  1017. _data->tileBuffers[i]->sampleCountTableCompressor =
  1018. newCompressor (_data->header.compression(),
  1019. _data->maxSampleCountTableSize,
  1020. _data->header);
  1021. }
  1022. }
  1023. DeepTiledOutputFile::~DeepTiledOutputFile ()
  1024. {
  1025. if (_data)
  1026. {
  1027. {
  1028. Lock lock(*_data->_streamData);
  1029. Int64 originalPosition = _data->_streamData->os->tellp();
  1030. if (_data->tileOffsetsPosition > 0)
  1031. {
  1032. try
  1033. {
  1034. _data->_streamData->os->seekp (_data->tileOffsetsPosition);
  1035. _data->tileOffsets.writeTo (*_data->_streamData->os);
  1036. //
  1037. // Restore the original position.
  1038. //
  1039. _data->_streamData->os->seekp (originalPosition);
  1040. }
  1041. catch (...)
  1042. {
  1043. //
  1044. // We cannot safely throw any exceptions from here.
  1045. // This destructor may have been called because the
  1046. // stack is currently being unwound for another
  1047. // exception.
  1048. //
  1049. }
  1050. }
  1051. }
  1052. if (_data->_deleteStream && _data->_streamData)
  1053. delete _data->_streamData->os;
  1054. //
  1055. // (TODO) we should have a way to tell if the stream data is owned by
  1056. // this file or by a parent multipart file.
  1057. //
  1058. if (_data->partNumber == -1 && _data->_streamData)
  1059. delete _data->_streamData;
  1060. delete _data;
  1061. }
  1062. }
  1063. const char *
  1064. DeepTiledOutputFile::fileName () const
  1065. {
  1066. return _data->_streamData->os->fileName();
  1067. }
  1068. const Header &
  1069. DeepTiledOutputFile::header () const
  1070. {
  1071. return _data->header;
  1072. }
  1073. void
  1074. DeepTiledOutputFile::setFrameBuffer (const DeepFrameBuffer &frameBuffer)
  1075. {
  1076. Lock lock (*_data->_streamData);
  1077. //
  1078. // Check if the new frame buffer descriptor
  1079. // is compatible with the image file header.
  1080. //
  1081. const ChannelList &channels = _data->header.channels();
  1082. for (ChannelList::ConstIterator i = channels.begin();
  1083. i != channels.end();
  1084. ++i)
  1085. {
  1086. DeepFrameBuffer::ConstIterator j = frameBuffer.find (i.name());
  1087. if (j == frameBuffer.end())
  1088. continue;
  1089. if (i.channel().type != j.slice().type)
  1090. THROW (IEX_NAMESPACE::ArgExc, "Pixel type of \"" << i.name() << "\" channel "
  1091. "of output file \"" << fileName() << "\" is "
  1092. "not compatible with the frame buffer's "
  1093. "pixel type.");
  1094. if (j.slice().xSampling != 1 || j.slice().ySampling != 1)
  1095. THROW (IEX_NAMESPACE::ArgExc, "All channels in a tiled file must have"
  1096. "sampling (1,1).");
  1097. }
  1098. //
  1099. // Store the pixel sample count table.
  1100. //
  1101. const Slice& sampleCountSlice = frameBuffer.getSampleCountSlice();
  1102. if (sampleCountSlice.base == 0)
  1103. {
  1104. throw IEX_NAMESPACE::ArgExc ("Invalid base pointer, please set a proper sample count slice.");
  1105. }
  1106. else
  1107. {
  1108. _data->sampleCountSliceBase = sampleCountSlice.base;
  1109. _data->sampleCountXStride = sampleCountSlice.xStride;
  1110. _data->sampleCountYStride = sampleCountSlice.yStride;
  1111. _data->sampleCountXTileCoords = sampleCountSlice.xTileCoords;
  1112. _data->sampleCountYTileCoords = sampleCountSlice.yTileCoords;
  1113. }
  1114. //
  1115. // Initialize slice table for writePixels().
  1116. // Pixel sample count slice is not presented in the header,
  1117. // so it wouldn't be added here.
  1118. // Store the pixel base pointer table.
  1119. //
  1120. vector<TOutSliceInfo*> slices;
  1121. for (ChannelList::ConstIterator i = channels.begin();
  1122. i != channels.end();
  1123. ++i)
  1124. {
  1125. DeepFrameBuffer::ConstIterator j = frameBuffer.find (i.name());
  1126. if (j == frameBuffer.end())
  1127. {
  1128. //
  1129. // Channel i is not present in the frame buffer.
  1130. // In the file, channel i will contain only zeroes.
  1131. //
  1132. slices.push_back (new TOutSliceInfo (i.channel().type,
  1133. 0, // sampleStride,
  1134. 0, // xStride
  1135. 0, // yStride
  1136. true)); // zero
  1137. }
  1138. else
  1139. {
  1140. //
  1141. // Channel i is present in the frame buffer.
  1142. //
  1143. slices.push_back (new TOutSliceInfo (j.slice().type,
  1144. j.slice().sampleStride,
  1145. j.slice().xStride,
  1146. j.slice().yStride,
  1147. false, // zero
  1148. (j.slice().xTileCoords)? 1: 0,
  1149. (j.slice().yTileCoords)? 1: 0));
  1150. TOutSliceInfo* slice = slices.back();
  1151. slice->base = j.slice().base;
  1152. }
  1153. }
  1154. //
  1155. // Store the new frame buffer.
  1156. //
  1157. _data->frameBuffer = frameBuffer;
  1158. for (size_t i = 0; i < _data->slices.size(); i++)
  1159. delete _data->slices[i];
  1160. _data->slices = slices;
  1161. }
  1162. const DeepFrameBuffer &
  1163. DeepTiledOutputFile::frameBuffer () const
  1164. {
  1165. Lock lock (*_data->_streamData);
  1166. return _data->frameBuffer;
  1167. }
  1168. void
  1169. DeepTiledOutputFile::writeTiles (int dx1, int dx2, int dy1, int dy2,
  1170. int lx, int ly)
  1171. {
  1172. try
  1173. {
  1174. Lock lock (*_data->_streamData);
  1175. if (_data->slices.size() == 0)
  1176. throw IEX_NAMESPACE::ArgExc ("No frame buffer specified "
  1177. "as pixel data source.");
  1178. if (!isValidTile (dx1, dy1, lx, ly) || !isValidTile (dx2, dy2, lx, ly))
  1179. throw IEX_NAMESPACE::ArgExc ("Tile coordinates are invalid.");
  1180. if (!isValidLevel (lx, ly))
  1181. THROW (IEX_NAMESPACE::ArgExc,
  1182. "Level coordinate "
  1183. "(" << lx << ", " << ly << ") "
  1184. "is invalid.");
  1185. //
  1186. // Determine the first and last tile coordinates in both dimensions
  1187. // based on the file's lineOrder
  1188. //
  1189. if (dx1 > dx2)
  1190. swap (dx1, dx2);
  1191. if (dy1 > dy2)
  1192. swap (dy1, dy2);
  1193. int dyStart = dy1;
  1194. int dyStop = dy2 + 1;
  1195. int dY = 1;
  1196. if (_data->lineOrder == DECREASING_Y)
  1197. {
  1198. dyStart = dy2;
  1199. dyStop = dy1 - 1;
  1200. dY = -1;
  1201. }
  1202. int numTiles = (dx2 - dx1 + 1) * (dy2 - dy1 + 1);
  1203. int numTasks = min ((int)_data->tileBuffers.size(), numTiles);
  1204. //
  1205. // Create a task group for all tile buffer tasks. When the
  1206. // task group goes out of scope, the destructor waits until
  1207. // all tasks are complete.
  1208. //
  1209. {
  1210. TaskGroup taskGroup;
  1211. //
  1212. // Add in the initial compression tasks to the thread pool
  1213. //
  1214. int nextCompBuffer = 0;
  1215. int dxComp = dx1;
  1216. int dyComp = dyStart;
  1217. while (nextCompBuffer < numTasks)
  1218. {
  1219. ThreadPool::addGlobalTask (new TileBufferTask (&taskGroup,
  1220. _data,
  1221. nextCompBuffer++,
  1222. dxComp, dyComp,
  1223. lx, ly));
  1224. dxComp++;
  1225. if (dxComp > dx2)
  1226. {
  1227. dxComp = dx1;
  1228. dyComp += dY;
  1229. }
  1230. }
  1231. //
  1232. // Write the compressed buffers and add in more compression
  1233. // tasks until done
  1234. //
  1235. int nextWriteBuffer = 0;
  1236. int dxWrite = dx1;
  1237. int dyWrite = dyStart;
  1238. while (nextWriteBuffer < numTiles)
  1239. {
  1240. //
  1241. // Wait until the nextWriteBuffer is ready to be written
  1242. //
  1243. TileBuffer* writeBuffer =
  1244. _data->getTileBuffer (nextWriteBuffer);
  1245. writeBuffer->wait();
  1246. //
  1247. // Write the tilebuffer
  1248. //
  1249. bufferedTileWrite ( _data, dxWrite, dyWrite, lx, ly,
  1250. writeBuffer->dataPtr,
  1251. writeBuffer->dataSize,
  1252. writeBuffer->uncompressedSize,
  1253. writeBuffer->sampleCountTablePtr,
  1254. writeBuffer->sampleCountTableSize);
  1255. //
  1256. // Release the lock on nextWriteBuffer
  1257. //
  1258. writeBuffer->post();
  1259. //
  1260. // If there are no more tileBuffers to compress, then
  1261. // only continue to write out remaining tileBuffers,
  1262. // otherwise keep adding compression tasks.
  1263. //
  1264. if (nextCompBuffer < numTiles)
  1265. {
  1266. //
  1267. // add nextCompBuffer as a compression Task
  1268. //
  1269. ThreadPool::addGlobalTask
  1270. (new TileBufferTask (&taskGroup,
  1271. _data,
  1272. nextCompBuffer,
  1273. dxComp, dyComp,
  1274. lx, ly));
  1275. }
  1276. nextWriteBuffer++;
  1277. dxWrite++;
  1278. if (dxWrite > dx2)
  1279. {
  1280. dxWrite = dx1;
  1281. dyWrite += dY;
  1282. }
  1283. nextCompBuffer++;
  1284. dxComp++;
  1285. if (dxComp > dx2)
  1286. {
  1287. dxComp = dx1;
  1288. dyComp += dY;
  1289. }
  1290. }
  1291. //
  1292. // finish all tasks
  1293. //
  1294. }
  1295. //
  1296. // Exeption handling:
  1297. //
  1298. // TileBufferTask::execute() may have encountered exceptions, but
  1299. // those exceptions occurred in another thread, not in the thread
  1300. // that is executing this call to TiledOutputFile::writeTiles().
  1301. // TileBufferTask::execute() has caught all exceptions and stored
  1302. // the exceptions' what() strings in the tile buffers.
  1303. // Now we check if any tile buffer contains a stored exception; if
  1304. // this is the case then we re-throw the exception in this thread.
  1305. // (It is possible that multiple tile buffers contain stored
  1306. // exceptions. We re-throw the first exception we find and
  1307. // ignore all others.)
  1308. //
  1309. const string *exception = 0;
  1310. for (size_t i = 0; i < _data->tileBuffers.size(); ++i)
  1311. {
  1312. TileBuffer *tileBuffer = _data->tileBuffers[i];
  1313. if (tileBuffer->hasException && !exception)
  1314. exception = &tileBuffer->exception;
  1315. tileBuffer->hasException = false;
  1316. }
  1317. if (exception)
  1318. throw IEX_NAMESPACE::IoExc (*exception);
  1319. }
  1320. catch (IEX_NAMESPACE::BaseExc &e)
  1321. {
  1322. REPLACE_EXC (e, "Failed to write pixel data to image "
  1323. "file \"" << fileName() << "\". " << e.what());
  1324. throw;
  1325. }
  1326. }
  1327. void
  1328. DeepTiledOutputFile::writeTiles (int dx1, int dxMax, int dyMin, int dyMax, int l)
  1329. {
  1330. writeTiles (dx1, dxMax, dyMin, dyMax, l, l);
  1331. }
  1332. void
  1333. DeepTiledOutputFile::writeTile (int dx, int dy, int lx, int ly)
  1334. {
  1335. writeTiles (dx, dx, dy, dy, lx, ly);
  1336. }
  1337. void
  1338. DeepTiledOutputFile::writeTile (int dx, int dy, int l)
  1339. {
  1340. writeTile(dx, dy, l, l);
  1341. }
  1342. void
  1343. DeepTiledOutputFile::copyPixels (DeepTiledInputFile &in)
  1344. {
  1345. //
  1346. // Check if this file's and and the InputFile's
  1347. // headers are compatible.
  1348. //
  1349. const Header &hdr = _data->header;
  1350. const Header &inHdr = in.header();
  1351. if (!(hdr.tileDescription() == inHdr.tileDescription()))
  1352. THROW (IEX_NAMESPACE::ArgExc, "Quick pixel copy from image "
  1353. "file \"" << in.fileName() << "\" to image "
  1354. "file \"" << fileName() << "\" failed. "
  1355. "The files have different tile descriptions.");
  1356. if (!(hdr.dataWindow() == inHdr.dataWindow()))
  1357. THROW (IEX_NAMESPACE::ArgExc, "Cannot copy pixels from image "
  1358. "file \"" << in.fileName() << "\" to image "
  1359. "file \"" << fileName() << "\". The "
  1360. "files have different data windows.");
  1361. if (!(hdr.lineOrder() == inHdr.lineOrder()))
  1362. THROW (IEX_NAMESPACE::ArgExc, "Quick pixel copy from image "
  1363. "file \"" << in.fileName() << "\" to image "
  1364. "file \"" << fileName() << "\" failed. "
  1365. "The files have different line orders.");
  1366. if (!(hdr.compression() == inHdr.compression()))
  1367. THROW (IEX_NAMESPACE::ArgExc, "Quick pixel copy from image "
  1368. "file \"" << in.fileName() << "\" to image "
  1369. "file \"" << fileName() << "\" failed. "
  1370. "The files use different compression methods.");
  1371. if (!(hdr.channels() == inHdr.channels()))
  1372. THROW (IEX_NAMESPACE::ArgExc, "Quick pixel copy from image "
  1373. "file \"" << in.fileName() << "\" to image "
  1374. "file \"" << fileName() << "\" "
  1375. "failed. The files have different channel "
  1376. "lists.");
  1377. // Verify that no pixel data have been written to this file yet.
  1378. //
  1379. if (!_data->tileOffsets.isEmpty())
  1380. THROW (IEX_NAMESPACE::LogicExc, "Quick pixel copy from image "
  1381. "file \"" << in.fileName() << "\" to image "
  1382. "file \"" << _data->_streamData->os->fileName() << "\" "
  1383. "failed. \"" << fileName() << "\" "
  1384. "already contains pixel data.");
  1385. int numAllTiles = in.totalTiles();
  1386. Lock lock (*_data->_streamData);
  1387. //
  1388. // special handling for random tiles
  1389. //
  1390. vector<int> dx_list(_data->lineOrder==RANDOM_Y ? numAllTiles : 1);
  1391. vector<int> dy_list(_data->lineOrder==RANDOM_Y ? numAllTiles : 1);
  1392. vector<int> lx_list(_data->lineOrder==RANDOM_Y ? numAllTiles : 1);
  1393. vector<int> ly_list(_data->lineOrder==RANDOM_Y ? numAllTiles : 1);
  1394. if(_data->lineOrder==RANDOM_Y)
  1395. {
  1396. in.getTileOrder(&dx_list[0],&dy_list[0],&lx_list[0],&ly_list[0]);
  1397. _data->nextTileToWrite.dx=dx_list[0];
  1398. _data->nextTileToWrite.dy=dy_list[0];
  1399. _data->nextTileToWrite.lx=lx_list[0];
  1400. _data->nextTileToWrite.ly=ly_list[0];
  1401. }
  1402. vector<char> data(4096);
  1403. for (int i = 0; i < numAllTiles; ++i)
  1404. {
  1405. int dx = _data->nextTileToWrite.dx;
  1406. int dy = _data->nextTileToWrite.dy;
  1407. int lx = _data->nextTileToWrite.lx;
  1408. int ly = _data->nextTileToWrite.ly;
  1409. Int64 dataSize = data.size();
  1410. in.rawTileData (dx, dy, lx, ly, &data[0], dataSize);
  1411. if(dataSize>data.size())
  1412. {
  1413. data.resize(dataSize);
  1414. in.rawTileData (dx, dy, lx, ly, &data[0], dataSize);
  1415. }
  1416. Int64 sampleCountTableSize = *(Int64 *)(&data[0] + 16);
  1417. Int64 pixelDataSize = *(Int64 *)(&data[0] + 24);
  1418. Int64 unpackedPixelDataSize = *(Int64 *)(&data[0] + 32);
  1419. char * sampleCountTable = &data[0]+40;
  1420. char * pixelData = sampleCountTable + sampleCountTableSize;
  1421. writeTileData (_data, dx, dy, lx, ly, pixelData, pixelDataSize,unpackedPixelDataSize,sampleCountTable,sampleCountTableSize);
  1422. if(_data->lineOrder==RANDOM_Y)
  1423. {
  1424. if(i<numAllTiles-1)
  1425. {
  1426. _data->nextTileToWrite.dx=dx_list[i+1];
  1427. _data->nextTileToWrite.dy=dy_list[i+1];
  1428. _data->nextTileToWrite.lx=lx_list[i+1];
  1429. _data->nextTileToWrite.ly=ly_list[i+1];
  1430. }
  1431. }else{
  1432. _data->nextTileToWrite = _data->nextTileCoord (_data->nextTileToWrite);
  1433. }
  1434. }
  1435. }
  1436. void
  1437. DeepTiledOutputFile::copyPixels (DeepTiledInputPart &in)
  1438. {
  1439. copyPixels(*in.file);
  1440. }
  1441. unsigned int
  1442. DeepTiledOutputFile::tileXSize () const
  1443. {
  1444. return _data->tileDesc.xSize;
  1445. }
  1446. unsigned int
  1447. DeepTiledOutputFile::tileYSize () const
  1448. {
  1449. return _data->tileDesc.ySize;
  1450. }
  1451. LevelMode
  1452. DeepTiledOutputFile::levelMode () const
  1453. {
  1454. return _data->tileDesc.mode;
  1455. }
  1456. LevelRoundingMode
  1457. DeepTiledOutputFile::levelRoundingMode () const
  1458. {
  1459. return _data->tileDesc.roundingMode;
  1460. }
  1461. int
  1462. DeepTiledOutputFile::numLevels () const
  1463. {
  1464. if (levelMode() == RIPMAP_LEVELS)
  1465. THROW (IEX_NAMESPACE::LogicExc, "Error calling numLevels() on image "
  1466. "file \"" << fileName() << "\" "
  1467. "(numLevels() is not defined for RIPMAPs).");
  1468. return _data->numXLevels;
  1469. }
  1470. int
  1471. DeepTiledOutputFile::numXLevels () const
  1472. {
  1473. return _data->numXLevels;
  1474. }
  1475. int
  1476. DeepTiledOutputFile::numYLevels () const
  1477. {
  1478. return _data->numYLevels;
  1479. }
  1480. bool
  1481. DeepTiledOutputFile::isValidLevel (int lx, int ly) const
  1482. {
  1483. if (lx < 0 || ly < 0)
  1484. return false;
  1485. if (levelMode() == MIPMAP_LEVELS && lx != ly)
  1486. return false;
  1487. if (lx >= numXLevels() || ly >= numYLevels())
  1488. return false;
  1489. return true;
  1490. }
  1491. int
  1492. DeepTiledOutputFile::levelWidth (int lx) const
  1493. {
  1494. try
  1495. {
  1496. int retVal = levelSize (_data->minX, _data->maxX, lx,
  1497. _data->tileDesc.roundingMode);
  1498. return retVal;
  1499. }
  1500. catch (IEX_NAMESPACE::BaseExc &e)
  1501. {
  1502. REPLACE_EXC (e, "Error calling levelWidth() on image "
  1503. "file \"" << fileName() << "\". " << e.what());
  1504. throw;
  1505. }
  1506. }
  1507. int
  1508. DeepTiledOutputFile::levelHeight (int ly) const
  1509. {
  1510. try
  1511. {
  1512. return levelSize (_data->minY, _data->maxY, ly,
  1513. _data->tileDesc.roundingMode);
  1514. }
  1515. catch (IEX_NAMESPACE::BaseExc &e)
  1516. {
  1517. REPLACE_EXC (e, "Error calling levelHeight() on image "
  1518. "file \"" << fileName() << "\". " << e.what());
  1519. throw;
  1520. }
  1521. }
  1522. int
  1523. DeepTiledOutputFile::numXTiles (int lx) const
  1524. {
  1525. if (lx < 0 || lx >= _data->numXLevels)
  1526. THROW (IEX_NAMESPACE::LogicExc, "Error calling numXTiles() on image "
  1527. "file \"" << _data->_streamData->os->fileName() << "\" "
  1528. "(Argument is not in valid range).");
  1529. return _data->numXTiles[lx];
  1530. }
  1531. int
  1532. DeepTiledOutputFile::numYTiles (int ly) const
  1533. {
  1534. if (ly < 0 || ly >= _data->numYLevels)
  1535. THROW (IEX_NAMESPACE::LogicExc, "Error calling numXTiles() on image "
  1536. "file \"" << _data->_streamData->os->fileName() << "\" "
  1537. "(Argument is not in valid range).");
  1538. return _data->numYTiles[ly];
  1539. }
  1540. Box2i
  1541. DeepTiledOutputFile::dataWindowForLevel (int l) const
  1542. {
  1543. return dataWindowForLevel (l, l);
  1544. }
  1545. Box2i
  1546. DeepTiledOutputFile::dataWindowForLevel (int lx, int ly) const
  1547. {
  1548. try
  1549. {
  1550. return OPENEXR_IMF_INTERNAL_NAMESPACE::dataWindowForLevel (
  1551. _data->tileDesc,
  1552. _data->minX, _data->maxX,
  1553. _data->minY, _data->maxY,
  1554. lx, ly);
  1555. }
  1556. catch (IEX_NAMESPACE::BaseExc &e)
  1557. {
  1558. REPLACE_EXC (e, "Error calling dataWindowForLevel() on image "
  1559. "file \"" << fileName() << "\". " << e.what());
  1560. throw;
  1561. }
  1562. }
  1563. Box2i
  1564. DeepTiledOutputFile::dataWindowForTile (int dx, int dy, int l) const
  1565. {
  1566. return dataWindowForTile (dx, dy, l, l);
  1567. }
  1568. Box2i
  1569. DeepTiledOutputFile::dataWindowForTile (int dx, int dy, int lx, int ly) const
  1570. {
  1571. try
  1572. {
  1573. if (!isValidTile (dx, dy, lx, ly))
  1574. throw IEX_NAMESPACE::ArgExc ("Arguments not in valid range.");
  1575. return OPENEXR_IMF_INTERNAL_NAMESPACE::dataWindowForTile (
  1576. _data->tileDesc,
  1577. _data->minX, _data->maxX,
  1578. _data->minY, _data->maxY,
  1579. dx, dy,
  1580. lx, ly);
  1581. }
  1582. catch (IEX_NAMESPACE::BaseExc &e)
  1583. {
  1584. REPLACE_EXC (e, "Error calling dataWindowForTile() on image "
  1585. "file \"" << fileName() << "\". " << e.what());
  1586. throw;
  1587. }
  1588. }
  1589. bool
  1590. DeepTiledOutputFile::isValidTile (int dx, int dy, int lx, int ly) const
  1591. {
  1592. return ((lx < _data->numXLevels && lx >= 0) &&
  1593. (ly < _data->numYLevels && ly >= 0) &&
  1594. (dx < _data->numXTiles[lx] && dx >= 0) &&
  1595. (dy < _data->numYTiles[ly] && dy >= 0));
  1596. }
  1597. void
  1598. DeepTiledOutputFile::updatePreviewImage (const PreviewRgba newPixels[])
  1599. {
  1600. Lock lock (*_data->_streamData);
  1601. if (_data->previewPosition <= 0)
  1602. THROW (IEX_NAMESPACE::LogicExc, "Cannot update preview image pixels. "
  1603. "File \"" << fileName() << "\" does not "
  1604. "contain a preview image.");
  1605. //
  1606. // Store the new pixels in the header's preview image attribute.
  1607. //
  1608. PreviewImageAttribute &pia =
  1609. _data->header.typedAttribute <PreviewImageAttribute> ("preview");
  1610. PreviewImage &pi = pia.value();
  1611. PreviewRgba *pixels = pi.pixels();
  1612. int numPixels = pi.width() * pi.height();
  1613. for (int i = 0; i < numPixels; ++i)
  1614. pixels[i] = newPixels[i];
  1615. //
  1616. // Save the current file position, jump to the position in
  1617. // the file where the preview image starts, store the new
  1618. // preview image, and jump back to the saved file position.
  1619. //
  1620. Int64 savedPosition = _data->_streamData->os->tellp();
  1621. try
  1622. {
  1623. _data->_streamData->os->seekp (_data->previewPosition);
  1624. pia.writeValueTo (*_data->_streamData->os, _data->version);
  1625. _data->_streamData->os->seekp (savedPosition);
  1626. }
  1627. catch (IEX_NAMESPACE::BaseExc &e)
  1628. {
  1629. REPLACE_EXC (e, "Cannot update preview image pixels for "
  1630. "file \"" << fileName() << "\". " << e.what());
  1631. throw;
  1632. }
  1633. }
  1634. void
  1635. DeepTiledOutputFile::breakTile
  1636. (int dx, int dy,
  1637. int lx, int ly,
  1638. int offset,
  1639. int length,
  1640. char c)
  1641. {
  1642. Lock lock (*_data->_streamData);
  1643. Int64 position = _data->tileOffsets (dx, dy, lx, ly);
  1644. if (!position)
  1645. THROW (IEX_NAMESPACE::ArgExc,
  1646. "Cannot overwrite tile "
  1647. "(" << dx << ", " << dy << ", " << lx << "," << ly << "). "
  1648. "The tile has not yet been stored in "
  1649. "file \"" << fileName() << "\".");
  1650. _data->_streamData->currentPosition = 0;
  1651. _data->_streamData->os->seekp (position + offset);
  1652. for (int i = 0; i < length; ++i)
  1653. _data->_streamData->os->write (&c, 1);
  1654. }
  1655. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT