ImfOutputFile.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  1. //
  2. ///\todo: version needs fixing!
  3. //
  4. ///////////////////////////////////////////////////////////////////////////
  5. //
  6. // Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
  7. // Digital Ltd. LLC
  8. //
  9. // All rights reserved.
  10. //
  11. // Redistribution and use in source and binary forms, with or without
  12. // modification, are permitted provided that the following conditions are
  13. // met:
  14. // * Redistributions of source code must retain the above copyright
  15. // notice, this list of conditions and the following disclaimer.
  16. // * Redistributions in binary form must reproduce the above
  17. // copyright notice, this list of conditions and the following disclaimer
  18. // in the documentation and/or other materials provided with the
  19. // distribution.
  20. // * Neither the name of Industrial Light & Magic nor the names of
  21. // its contributors may be used to endorse or promote products derived
  22. // from this software without specific prior written permission.
  23. //
  24. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. ///////////////////////////////////////////////////////////////////////////
  37. //-----------------------------------------------------------------------------
  38. //
  39. // class OutputFile
  40. //
  41. //-----------------------------------------------------------------------------
  42. #include <ImfOutputFile.h>
  43. #include <ImfInputFile.h>
  44. #include <ImfChannelList.h>
  45. #include <ImfMisc.h>
  46. #include <ImfStdIO.h>
  47. #include <ImfCompressor.h>
  48. #include "ImathBox.h"
  49. #include "ImathFun.h"
  50. #include <ImfArray.h>
  51. #include "ImfXdr.h"
  52. #include <ImfPreviewImageAttribute.h>
  53. #include <ImfPartType.h>
  54. #include "IlmThreadPool.h"
  55. #include "ImfOutputStreamMutex.h"
  56. #include "IlmThreadSemaphore.h"
  57. #include "IlmThreadMutex.h"
  58. #include "Iex.h"
  59. #include "ImfInputPart.h"
  60. #include "ImfNamespace.h"
  61. #include "ImfOutputPartData.h"
  62. #include <string>
  63. #include <vector>
  64. #include <fstream>
  65. #include <assert.h>
  66. #include <algorithm>
  67. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
  68. using IMATH_NAMESPACE::Box2i;
  69. using IMATH_NAMESPACE::divp;
  70. using IMATH_NAMESPACE::modp;
  71. using std::string;
  72. using std::vector;
  73. using std::ofstream;
  74. using std::min;
  75. using std::max;
  76. using ILMTHREAD_NAMESPACE::Mutex;
  77. using ILMTHREAD_NAMESPACE::Lock;
  78. using ILMTHREAD_NAMESPACE::Semaphore;
  79. using ILMTHREAD_NAMESPACE::Task;
  80. using ILMTHREAD_NAMESPACE::TaskGroup;
  81. using ILMTHREAD_NAMESPACE::ThreadPool;
  82. namespace {
  83. struct OutSliceInfo
  84. {
  85. PixelType type;
  86. const char * base;
  87. size_t xStride;
  88. size_t yStride;
  89. int xSampling;
  90. int ySampling;
  91. bool zero;
  92. OutSliceInfo (PixelType type = HALF,
  93. const char *base = 0,
  94. size_t xStride = 0,
  95. size_t yStride = 0,
  96. int xSampling = 1,
  97. int ySampling = 1,
  98. bool zero = false);
  99. };
  100. OutSliceInfo::OutSliceInfo (PixelType t,
  101. const char *b,
  102. size_t xs, size_t ys,
  103. int xsm, int ysm,
  104. bool z)
  105. :
  106. type (t),
  107. base (b),
  108. xStride (xs),
  109. yStride (ys),
  110. xSampling (xsm),
  111. ySampling (ysm),
  112. zero (z)
  113. {
  114. // empty
  115. }
  116. struct LineBuffer
  117. {
  118. Array<char> buffer;
  119. const char * dataPtr;
  120. int dataSize;
  121. char * endOfLineBufferData;
  122. int minY;
  123. int maxY;
  124. int scanLineMin;
  125. int scanLineMax;
  126. Compressor * compressor;
  127. bool partiallyFull; // has incomplete data
  128. bool hasException;
  129. string exception;
  130. LineBuffer (Compressor *comp);
  131. ~LineBuffer ();
  132. void wait () {_sem.wait();}
  133. void post () {_sem.post();}
  134. private:
  135. Semaphore _sem;
  136. };
  137. LineBuffer::LineBuffer (Compressor *comp) :
  138. dataPtr (0),
  139. dataSize (0),
  140. compressor (comp),
  141. partiallyFull (false),
  142. hasException (false),
  143. exception (),
  144. _sem (1)
  145. {
  146. // empty
  147. }
  148. LineBuffer::~LineBuffer ()
  149. {
  150. delete compressor;
  151. }
  152. } // namespace
  153. struct OutputFile::Data
  154. {
  155. Header header; // the image header
  156. bool multiPart; // is the file multipart?
  157. int version; // version attribute \todo NOT BEING WRITTEN PROPERLY
  158. Int64 previewPosition; // file position for preview
  159. FrameBuffer frameBuffer; // framebuffer to write into
  160. int currentScanLine; // next scanline to be written
  161. int missingScanLines; // number of lines to write
  162. LineOrder lineOrder; // the file's lineorder
  163. int minX; // data window's min x coord
  164. int maxX; // data window's max x coord
  165. int minY; // data window's min y coord
  166. int maxY; // data window's max x coord
  167. vector<Int64> lineOffsets; // stores offsets in file for
  168. // each scanline
  169. vector<size_t> bytesPerLine; // combined size of a line over
  170. // all channels
  171. vector<size_t> offsetInLineBuffer; // offset for each scanline in
  172. // its linebuffer
  173. Compressor::Format format; // compressor's data format
  174. vector<OutSliceInfo> slices; // info about channels in file
  175. Int64 lineOffsetsPosition; // file position for line
  176. // offset table
  177. vector<LineBuffer*> lineBuffers; // each holds one line buffer
  178. int linesInBuffer; // number of scanlines each
  179. // buffer holds
  180. size_t lineBufferSize; // size of the line buffer
  181. int partNumber; // the output part number
  182. OutputStreamMutex * _streamData;
  183. bool _deleteStream;
  184. Data (int numThreads);
  185. ~Data ();
  186. inline LineBuffer * getLineBuffer (int number); // hash function from line
  187. // buffer indices into our
  188. // vector of line buffers
  189. };
  190. OutputFile::Data::Data (int numThreads):
  191. lineOffsetsPosition (0),
  192. partNumber (-1),
  193. _streamData(0),
  194. _deleteStream(false)
  195. {
  196. //
  197. // We need at least one lineBuffer, but if threading is used,
  198. // to keep n threads busy we need 2*n lineBuffers.
  199. //
  200. lineBuffers.resize (max (1, 2 * numThreads));
  201. }
  202. OutputFile::Data::~Data ()
  203. {
  204. for (size_t i = 0; i < lineBuffers.size(); i++)
  205. delete lineBuffers[i];
  206. }
  207. LineBuffer*
  208. OutputFile::Data::getLineBuffer (int number)
  209. {
  210. return lineBuffers[number % lineBuffers.size()];
  211. }
  212. namespace {
  213. Int64
  214. writeLineOffsets (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os, const vector<Int64> &lineOffsets)
  215. {
  216. Int64 pos = os.tellp();
  217. if (pos == -1)
  218. IEX_NAMESPACE::throwErrnoExc ("Cannot determine current file position (%T).");
  219. for (unsigned int i = 0; i < lineOffsets.size(); i++)
  220. Xdr::write<StreamIO> (os, lineOffsets[i]);
  221. return pos;
  222. }
  223. void
  224. writePixelData (OutputStreamMutex *filedata,
  225. OutputFile::Data *partdata,
  226. int lineBufferMinY,
  227. const char pixelData[],
  228. int pixelDataSize)
  229. {
  230. //
  231. // Store a block of pixel data in the output file, and try
  232. // to keep track of the current writing position the file
  233. // without calling tellp() (tellp() can be fairly expensive).
  234. //
  235. Int64 currentPosition = filedata->currentPosition;
  236. filedata->currentPosition = 0;
  237. if (currentPosition == 0)
  238. currentPosition = filedata->os->tellp();
  239. partdata->lineOffsets[(partdata->currentScanLine - partdata->minY) / partdata->linesInBuffer] =
  240. currentPosition;
  241. #ifdef DEBUG
  242. assert (filedata->os->tellp() == currentPosition);
  243. #endif
  244. if (partdata->multiPart)
  245. {
  246. OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (*filedata->os, partdata->partNumber);
  247. }
  248. OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (*filedata->os, lineBufferMinY);
  249. OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (*filedata->os, pixelDataSize);
  250. filedata->os->write (pixelData, pixelDataSize);
  251. filedata->currentPosition = currentPosition +
  252. Xdr::size<int>() +
  253. Xdr::size<int>() +
  254. pixelDataSize;
  255. if (partdata->multiPart)
  256. {
  257. filedata->currentPosition += Xdr::size<int>();
  258. }
  259. }
  260. inline void
  261. writePixelData (OutputStreamMutex* filedata,
  262. OutputFile::Data *partdata,
  263. const LineBuffer *lineBuffer)
  264. {
  265. writePixelData (filedata, partdata,
  266. lineBuffer->minY,
  267. lineBuffer->dataPtr,
  268. lineBuffer->dataSize);
  269. }
  270. void
  271. convertToXdr (OutputFile::Data *ofd,
  272. Array<char> &lineBuffer,
  273. int lineBufferMinY,
  274. int lineBufferMaxY,
  275. int inSize)
  276. {
  277. //
  278. // Convert the contents of a lineBuffer from the machine's native
  279. // representation to Xdr format. This function is called by
  280. // CompressLineBuffer::execute(), below, if the compressor wanted
  281. // its input pixel data in the machine's native format, but then
  282. // failed to compress the data (most compressors will expand rather
  283. // than compress random input data).
  284. //
  285. // Note that this routine assumes that the machine's native
  286. // representation of the pixel data has the same size as the
  287. // Xdr representation. This makes it possible to convert the
  288. // pixel data in place, without an intermediate temporary buffer.
  289. //
  290. //
  291. // Iterate over all scanlines in the lineBuffer to convert.
  292. //
  293. char *writePtr = &lineBuffer[0];
  294. for (int y = lineBufferMinY; y <= lineBufferMaxY; y++)
  295. {
  296. //
  297. // Set these to point to the start of line y.
  298. // We will write to writePtr from readPtr.
  299. //
  300. const char *readPtr = writePtr;
  301. //
  302. // Iterate over all slices in the file.
  303. //
  304. for (unsigned int i = 0; i < ofd->slices.size(); ++i)
  305. {
  306. //
  307. // Test if scan line y of this channel is
  308. // contains any data (the scan line contains
  309. // data only if y % ySampling == 0).
  310. //
  311. const OutSliceInfo &slice = ofd->slices[i];
  312. if (modp (y, slice.ySampling) != 0)
  313. continue;
  314. //
  315. // Find the number of sampled pixels, dMaxX-dMinX+1, for
  316. // slice i in scan line y (i.e. pixels within the data window
  317. // for which x % xSampling == 0).
  318. //
  319. int dMinX = divp (ofd->minX, slice.xSampling);
  320. int dMaxX = divp (ofd->maxX, slice.xSampling);
  321. //
  322. // Convert the samples in place.
  323. //
  324. convertInPlace (writePtr, readPtr, slice.type, dMaxX - dMinX + 1);
  325. }
  326. }
  327. }
  328. //
  329. // A LineBufferTask encapsulates the task of copying a set of scanlines
  330. // from the user's frame buffer into a LineBuffer object, compressing
  331. // the data if necessary.
  332. //
  333. class LineBufferTask: public Task
  334. {
  335. public:
  336. LineBufferTask (TaskGroup *group,
  337. OutputFile::Data *ofd,
  338. int number,
  339. int scanLineMin,
  340. int scanLineMax);
  341. virtual ~LineBufferTask ();
  342. virtual void execute ();
  343. private:
  344. OutputFile::Data * _ofd;
  345. LineBuffer * _lineBuffer;
  346. };
  347. LineBufferTask::LineBufferTask
  348. (TaskGroup *group,
  349. OutputFile::Data *ofd,
  350. int number,
  351. int scanLineMin,
  352. int scanLineMax)
  353. :
  354. Task (group),
  355. _ofd (ofd),
  356. _lineBuffer (_ofd->getLineBuffer(number))
  357. {
  358. //
  359. // Wait for the lineBuffer to become available
  360. //
  361. _lineBuffer->wait ();
  362. //
  363. // Initialize the lineBuffer data if necessary
  364. //
  365. if (!_lineBuffer->partiallyFull)
  366. {
  367. _lineBuffer->endOfLineBufferData = _lineBuffer->buffer;
  368. _lineBuffer->minY = _ofd->minY + number * _ofd->linesInBuffer;
  369. _lineBuffer->maxY = min (_lineBuffer->minY + _ofd->linesInBuffer - 1,
  370. _ofd->maxY);
  371. _lineBuffer->partiallyFull = true;
  372. }
  373. _lineBuffer->scanLineMin = max (_lineBuffer->minY, scanLineMin);
  374. _lineBuffer->scanLineMax = min (_lineBuffer->maxY, scanLineMax);
  375. }
  376. LineBufferTask::~LineBufferTask ()
  377. {
  378. //
  379. // Signal that the line buffer is now free
  380. //
  381. _lineBuffer->post ();
  382. }
  383. void
  384. LineBufferTask::execute ()
  385. {
  386. try
  387. {
  388. //
  389. // First copy the pixel data from the
  390. // frame buffer into the line buffer
  391. //
  392. int yStart, yStop, dy;
  393. if (_ofd->lineOrder == INCREASING_Y)
  394. {
  395. yStart = _lineBuffer->scanLineMin;
  396. yStop = _lineBuffer->scanLineMax + 1;
  397. dy = 1;
  398. }
  399. else
  400. {
  401. yStart = _lineBuffer->scanLineMax;
  402. yStop = _lineBuffer->scanLineMin - 1;
  403. dy = -1;
  404. }
  405. int y;
  406. for (y = yStart; y != yStop; y += dy)
  407. {
  408. //
  409. // Gather one scan line's worth of pixel data and store
  410. // them in _ofd->lineBuffer.
  411. //
  412. char *writePtr = _lineBuffer->buffer +
  413. _ofd->offsetInLineBuffer[y - _ofd->minY];
  414. //
  415. // Iterate over all image channels.
  416. //
  417. for (unsigned int i = 0; i < _ofd->slices.size(); ++i)
  418. {
  419. //
  420. // Test if scan line y of this channel contains any data
  421. // (the scan line contains data only if y % ySampling == 0).
  422. //
  423. const OutSliceInfo &slice = _ofd->slices[i];
  424. if (modp (y, slice.ySampling) != 0)
  425. continue;
  426. //
  427. // Find the x coordinates of the leftmost and rightmost
  428. // sampled pixels (i.e. pixels within the data window
  429. // for which x % xSampling == 0).
  430. //
  431. int dMinX = divp (_ofd->minX, slice.xSampling);
  432. int dMaxX = divp (_ofd->maxX, slice.xSampling);
  433. //
  434. // Fill the line buffer with with pixel data.
  435. //
  436. if (slice.zero)
  437. {
  438. //
  439. // The frame buffer contains no data for this channel.
  440. // Store zeroes in _lineBuffer->buffer.
  441. //
  442. fillChannelWithZeroes (writePtr, _ofd->format, slice.type,
  443. dMaxX - dMinX + 1);
  444. }
  445. else
  446. {
  447. //
  448. // If necessary, convert the pixel data to Xdr format.
  449. // Then store the pixel data in _ofd->lineBuffer.
  450. //
  451. const char *linePtr = slice.base +
  452. divp (y, slice.ySampling) *
  453. slice.yStride;
  454. const char *readPtr = linePtr + dMinX * slice.xStride;
  455. const char *endPtr = linePtr + dMaxX * slice.xStride;
  456. copyFromFrameBuffer (writePtr, readPtr, endPtr,
  457. slice.xStride, _ofd->format,
  458. slice.type);
  459. }
  460. }
  461. if (_lineBuffer->endOfLineBufferData < writePtr)
  462. _lineBuffer->endOfLineBufferData = writePtr;
  463. #ifdef DEBUG
  464. assert (writePtr - (_lineBuffer->buffer +
  465. _ofd->offsetInLineBuffer[y - _ofd->minY]) ==
  466. (int) _ofd->bytesPerLine[y - _ofd->minY]);
  467. #endif
  468. }
  469. //
  470. // If the next scanline isn't past the bounds of the lineBuffer
  471. // then we are done, otherwise compress the linebuffer
  472. //
  473. if (y >= _lineBuffer->minY && y <= _lineBuffer->maxY)
  474. return;
  475. _lineBuffer->dataPtr = _lineBuffer->buffer;
  476. _lineBuffer->dataSize = _lineBuffer->endOfLineBufferData -
  477. _lineBuffer->buffer;
  478. //
  479. // Compress the data
  480. //
  481. Compressor *compressor = _lineBuffer->compressor;
  482. if (compressor)
  483. {
  484. const char *compPtr;
  485. int compSize = compressor->compress (_lineBuffer->dataPtr,
  486. _lineBuffer->dataSize,
  487. _lineBuffer->minY, compPtr);
  488. if (compSize < _lineBuffer->dataSize)
  489. {
  490. _lineBuffer->dataSize = compSize;
  491. _lineBuffer->dataPtr = compPtr;
  492. }
  493. else if (_ofd->format == Compressor::NATIVE)
  494. {
  495. //
  496. // The data did not shrink during compression, but
  497. // we cannot write to the file using the machine's
  498. // native format, so we need to convert the lineBuffer
  499. // to Xdr.
  500. //
  501. convertToXdr (_ofd, _lineBuffer->buffer, _lineBuffer->minY,
  502. _lineBuffer->maxY, _lineBuffer->dataSize);
  503. }
  504. }
  505. _lineBuffer->partiallyFull = false;
  506. }
  507. catch (std::exception &e)
  508. {
  509. if (!_lineBuffer->hasException)
  510. {
  511. _lineBuffer->exception = e.what ();
  512. _lineBuffer->hasException = true;
  513. }
  514. }
  515. catch (...)
  516. {
  517. if (!_lineBuffer->hasException)
  518. {
  519. _lineBuffer->exception = "unrecognized exception";
  520. _lineBuffer->hasException = true;
  521. }
  522. }
  523. }
  524. } // namespace
  525. OutputFile::OutputFile
  526. (const char fileName[],
  527. const Header &header,
  528. int numThreads)
  529. :
  530. _data (new Data (numThreads))
  531. {
  532. _data->_streamData=new OutputStreamMutex ();
  533. _data->_deleteStream=true;
  534. try
  535. {
  536. header.sanityCheck();
  537. _data->_streamData->os = new StdOFStream (fileName);
  538. _data->multiPart=false; // only one header, not multipart
  539. initialize (header);
  540. _data->_streamData->currentPosition = _data->_streamData->os->tellp();
  541. // Write header and empty offset table to the file.
  542. writeMagicNumberAndVersionField(*_data->_streamData->os, _data->header);
  543. _data->previewPosition =
  544. _data->header.writeTo (*_data->_streamData->os);
  545. _data->lineOffsetsPosition =
  546. writeLineOffsets (*_data->_streamData->os,_data->lineOffsets);
  547. }
  548. catch (IEX_NAMESPACE::BaseExc &e)
  549. {
  550. // ~OutputFile will not run, so free memory here
  551. if (_data)
  552. {
  553. if (_data->_streamData)
  554. {
  555. delete _data->_streamData->os;
  556. delete _data->_streamData;
  557. }
  558. delete _data;
  559. }
  560. REPLACE_EXC (e, "Cannot open image file "
  561. "\"" << fileName << "\". " << e.what());
  562. throw;
  563. }
  564. catch (...)
  565. {
  566. // ~OutputFile will not run, so free memory here
  567. if (_data)
  568. {
  569. if (_data->_streamData)
  570. {
  571. delete _data->_streamData->os;
  572. delete _data->_streamData;
  573. }
  574. delete _data;
  575. }
  576. throw;
  577. }
  578. }
  579. OutputFile::OutputFile
  580. (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os,
  581. const Header &header,
  582. int numThreads)
  583. :
  584. _data (new Data (numThreads))
  585. {
  586. _data->_streamData=new OutputStreamMutex ();
  587. _data->_deleteStream=false;
  588. try
  589. {
  590. header.sanityCheck();
  591. _data->_streamData->os = &os;
  592. _data->multiPart=false;
  593. initialize (header);
  594. _data->_streamData->currentPosition = _data->_streamData->os->tellp();
  595. // Write header and empty offset table to the file.
  596. writeMagicNumberAndVersionField(*_data->_streamData->os, _data->header);
  597. _data->previewPosition =
  598. _data->header.writeTo (*_data->_streamData->os);
  599. _data->lineOffsetsPosition =
  600. writeLineOffsets (*_data->_streamData->os, _data->lineOffsets);
  601. }
  602. catch (IEX_NAMESPACE::BaseExc &e)
  603. {
  604. // ~OutputFile will not run, so free memory here
  605. if (_data)
  606. {
  607. if (_data->_streamData)
  608. delete _data->_streamData;
  609. delete _data;
  610. }
  611. REPLACE_EXC (e, "Cannot open image file "
  612. "\"" << os.fileName() << "\". " << e.what());
  613. throw;
  614. }
  615. catch (...)
  616. {
  617. // ~OutputFile will not run, so free memory here
  618. if (_data)
  619. {
  620. if (_data->_streamData)
  621. delete _data->_streamData;
  622. delete _data;
  623. }
  624. throw;
  625. }
  626. }
  627. OutputFile::OutputFile(const OutputPartData* part) : _data(NULL)
  628. {
  629. try
  630. {
  631. if (part->header.type() != SCANLINEIMAGE)
  632. throw IEX_NAMESPACE::ArgExc("Can't build a OutputFile from a type-mismatched part.");
  633. _data = new Data (part->numThreads);
  634. _data->_streamData = part->mutex;
  635. _data->_deleteStream=false;
  636. _data->multiPart=part->multipart;
  637. initialize (part->header);
  638. _data->partNumber = part->partNumber;
  639. _data->lineOffsetsPosition = part->chunkOffsetTablePosition;
  640. _data->previewPosition = part->previewPosition;
  641. }
  642. catch (IEX_NAMESPACE::BaseExc &e)
  643. {
  644. if (_data) delete _data;
  645. REPLACE_EXC (e, "Cannot initialize output part "
  646. "\"" << part->partNumber << "\". " << e.what());
  647. throw;
  648. }
  649. catch (...)
  650. {
  651. if (_data) delete _data;
  652. throw;
  653. }
  654. }
  655. void
  656. OutputFile::initialize (const Header &header)
  657. {
  658. _data->header = header;
  659. // "fix" the type if it happens to be set incorrectly
  660. // (attribute is optional, but ensure it is correct if it exists)
  661. if(_data->header.hasType())
  662. {
  663. _data->header.setType(SCANLINEIMAGE);
  664. }
  665. const Box2i &dataWindow = header.dataWindow();
  666. _data->currentScanLine = (header.lineOrder() == INCREASING_Y)?
  667. dataWindow.min.y: dataWindow.max.y;
  668. _data->missingScanLines = dataWindow.max.y - dataWindow.min.y + 1;
  669. _data->lineOrder = header.lineOrder();
  670. _data->minX = dataWindow.min.x;
  671. _data->maxX = dataWindow.max.x;
  672. _data->minY = dataWindow.min.y;
  673. _data->maxY = dataWindow.max.y;
  674. size_t maxBytesPerLine = bytesPerLineTable (_data->header,
  675. _data->bytesPerLine);
  676. for (size_t i = 0; i < _data->lineBuffers.size(); ++i)
  677. {
  678. _data->lineBuffers[i] =
  679. new LineBuffer (newCompressor (_data->header.compression(),
  680. maxBytesPerLine,
  681. _data->header));
  682. }
  683. LineBuffer *lineBuffer = _data->lineBuffers[0];
  684. _data->format = defaultFormat (lineBuffer->compressor);
  685. _data->linesInBuffer = numLinesInBuffer (lineBuffer->compressor);
  686. _data->lineBufferSize = maxBytesPerLine * _data->linesInBuffer;
  687. for (size_t i = 0; i < _data->lineBuffers.size(); i++)
  688. _data->lineBuffers[i]->buffer.resizeErase(_data->lineBufferSize);
  689. int lineOffsetSize = (dataWindow.max.y - dataWindow.min.y +
  690. _data->linesInBuffer) / _data->linesInBuffer;
  691. _data->lineOffsets.resize (lineOffsetSize);
  692. offsetInLineBufferTable (_data->bytesPerLine,
  693. _data->linesInBuffer,
  694. _data->offsetInLineBuffer);
  695. }
  696. OutputFile::~OutputFile ()
  697. {
  698. if (_data)
  699. {
  700. {
  701. Lock lock(*_data->_streamData);
  702. Int64 originalPosition = _data->_streamData->os->tellp();
  703. if (_data->lineOffsetsPosition > 0)
  704. {
  705. try
  706. {
  707. _data->_streamData->os->seekp (_data->lineOffsetsPosition);
  708. writeLineOffsets (*_data->_streamData->os, _data->lineOffsets);
  709. //
  710. // Restore the original position.
  711. //
  712. _data->_streamData->os->seekp (originalPosition);
  713. }
  714. catch (...)
  715. {
  716. //
  717. // We cannot safely throw any exceptions from here.
  718. // This destructor may have been called because the
  719. // stack is currently being unwound for another
  720. // exception.
  721. //
  722. }
  723. }
  724. }
  725. if (_data->_deleteStream && _data->_streamData)
  726. delete _data->_streamData->os;
  727. if (_data->partNumber == -1 && _data->_streamData)
  728. delete _data->_streamData;
  729. delete _data;
  730. }
  731. }
  732. const char *
  733. OutputFile::fileName () const
  734. {
  735. return _data->_streamData->os->fileName();
  736. }
  737. const Header &
  738. OutputFile::header () const
  739. {
  740. return _data->header;
  741. }
  742. void
  743. OutputFile::setFrameBuffer (const FrameBuffer &frameBuffer)
  744. {
  745. Lock lock (*_data->_streamData);
  746. //
  747. // Check if the new frame buffer descriptor
  748. // is compatible with the image file header.
  749. //
  750. const ChannelList &channels = _data->header.channels();
  751. for (ChannelList::ConstIterator i = channels.begin();
  752. i != channels.end();
  753. ++i)
  754. {
  755. FrameBuffer::ConstIterator j = frameBuffer.find (i.name());
  756. if (j == frameBuffer.end())
  757. continue;
  758. if (i.channel().type != j.slice().type)
  759. {
  760. THROW (IEX_NAMESPACE::ArgExc, "Pixel type of \"" << i.name() << "\" channel "
  761. "of output file \"" << fileName() << "\" is "
  762. "not compatible with the frame buffer's "
  763. "pixel type.");
  764. }
  765. if (i.channel().xSampling != j.slice().xSampling ||
  766. i.channel().ySampling != j.slice().ySampling)
  767. {
  768. THROW (IEX_NAMESPACE::ArgExc, "X and/or y subsampling factors "
  769. "of \"" << i.name() << "\" channel "
  770. "of output file \"" << fileName() << "\" are "
  771. "not compatible with the frame buffer's "
  772. "subsampling factors.");
  773. }
  774. }
  775. //
  776. // Initialize slice table for writePixels().
  777. //
  778. vector<OutSliceInfo> slices;
  779. for (ChannelList::ConstIterator i = channels.begin();
  780. i != channels.end();
  781. ++i)
  782. {
  783. FrameBuffer::ConstIterator j = frameBuffer.find (i.name());
  784. if (j == frameBuffer.end())
  785. {
  786. //
  787. // Channel i is not present in the frame buffer.
  788. // In the file, channel i will contain only zeroes.
  789. //
  790. slices.push_back (OutSliceInfo (i.channel().type,
  791. 0, // base
  792. 0, // xStride,
  793. 0, // yStride,
  794. i.channel().xSampling,
  795. i.channel().ySampling,
  796. true)); // zero
  797. }
  798. else
  799. {
  800. //
  801. // Channel i is present in the frame buffer.
  802. //
  803. slices.push_back (OutSliceInfo (j.slice().type,
  804. j.slice().base,
  805. j.slice().xStride,
  806. j.slice().yStride,
  807. j.slice().xSampling,
  808. j.slice().ySampling,
  809. false)); // zero
  810. }
  811. }
  812. //
  813. // Store the new frame buffer.
  814. //
  815. _data->frameBuffer = frameBuffer;
  816. _data->slices = slices;
  817. }
  818. const FrameBuffer &
  819. OutputFile::frameBuffer () const
  820. {
  821. Lock lock (*_data->_streamData);
  822. return _data->frameBuffer;
  823. }
  824. void
  825. OutputFile::writePixels (int numScanLines)
  826. {
  827. try
  828. {
  829. Lock lock (*_data->_streamData);
  830. if (_data->slices.size() == 0)
  831. throw IEX_NAMESPACE::ArgExc ("No frame buffer specified "
  832. "as pixel data source.");
  833. //
  834. // Maintain two iterators:
  835. // nextWriteBuffer: next linebuffer to be written to the file
  836. // nextCompressBuffer: next linebuffer to compress
  837. //
  838. int first = (_data->currentScanLine - _data->minY) /
  839. _data->linesInBuffer;
  840. int nextWriteBuffer = first;
  841. int nextCompressBuffer;
  842. int stop;
  843. int step;
  844. int scanLineMin;
  845. int scanLineMax;
  846. {
  847. //
  848. // Create a task group for all line buffer tasks. When the
  849. // taskgroup goes out of scope, the destructor waits until
  850. // all tasks are complete.
  851. //
  852. TaskGroup taskGroup;
  853. //
  854. // Determine the range of lineBuffers that intersect the scan
  855. // line range. Then add the initial compression tasks to the
  856. // thread pool. We always add in at least one task but the
  857. // individual task might not do anything if numScanLines == 0.
  858. //
  859. if (_data->lineOrder == INCREASING_Y)
  860. {
  861. int last = (_data->currentScanLine + (numScanLines - 1) -
  862. _data->minY) / _data->linesInBuffer;
  863. scanLineMin = _data->currentScanLine;
  864. scanLineMax = _data->currentScanLine + numScanLines - 1;
  865. int numTasks = max (min ((int)_data->lineBuffers.size(),
  866. last - first + 1),
  867. 1);
  868. for (int i = 0; i < numTasks; i++)
  869. {
  870. ThreadPool::addGlobalTask
  871. (new LineBufferTask (&taskGroup, _data, first + i,
  872. scanLineMin, scanLineMax));
  873. }
  874. nextCompressBuffer = first + numTasks;
  875. stop = last + 1;
  876. step = 1;
  877. }
  878. else
  879. {
  880. int last = (_data->currentScanLine - (numScanLines - 1) -
  881. _data->minY) / _data->linesInBuffer;
  882. scanLineMax = _data->currentScanLine;
  883. scanLineMin = _data->currentScanLine - numScanLines + 1;
  884. int numTasks = max (min ((int)_data->lineBuffers.size(),
  885. first - last + 1),
  886. 1);
  887. for (int i = 0; i < numTasks; i++)
  888. {
  889. ThreadPool::addGlobalTask
  890. (new LineBufferTask (&taskGroup, _data, first - i,
  891. scanLineMin, scanLineMax));
  892. }
  893. nextCompressBuffer = first - numTasks;
  894. stop = last - 1;
  895. step = -1;
  896. }
  897. while (true)
  898. {
  899. if (_data->missingScanLines <= 0)
  900. {
  901. throw IEX_NAMESPACE::ArgExc ("Tried to write more scan lines "
  902. "than specified by the data window.");
  903. }
  904. //
  905. // Wait until the next line buffer is ready to be written
  906. //
  907. LineBuffer *writeBuffer =
  908. _data->getLineBuffer (nextWriteBuffer);
  909. writeBuffer->wait();
  910. int numLines = writeBuffer->scanLineMax -
  911. writeBuffer->scanLineMin + 1;
  912. _data->missingScanLines -= numLines;
  913. //
  914. // If the line buffer is only partially full, then it is
  915. // not complete and we cannot write it to disk yet.
  916. //
  917. if (writeBuffer->partiallyFull)
  918. {
  919. _data->currentScanLine = _data->currentScanLine +
  920. step * numLines;
  921. writeBuffer->post();
  922. return;
  923. }
  924. //
  925. // Write the line buffer
  926. //
  927. writePixelData (_data->_streamData, _data, writeBuffer);
  928. nextWriteBuffer += step;
  929. _data->currentScanLine = _data->currentScanLine +
  930. step * numLines;
  931. #ifdef DEBUG
  932. assert (_data->currentScanLine ==
  933. ((_data->lineOrder == INCREASING_Y) ?
  934. writeBuffer->scanLineMax + 1:
  935. writeBuffer->scanLineMin - 1));
  936. #endif
  937. //
  938. // Release the lock on the line buffer
  939. //
  940. writeBuffer->post();
  941. //
  942. // If this was the last line buffer in the scanline range
  943. //
  944. if (nextWriteBuffer == stop)
  945. break;
  946. //
  947. // If there are no more line buffers to compress,
  948. // then only continue to write out remaining lineBuffers
  949. //
  950. if (nextCompressBuffer == stop)
  951. continue;
  952. //
  953. // Add nextCompressBuffer as a compression task
  954. //
  955. ThreadPool::addGlobalTask
  956. (new LineBufferTask (&taskGroup, _data, nextCompressBuffer,
  957. scanLineMin, scanLineMax));
  958. //
  959. // Update the next line buffer we need to compress
  960. //
  961. nextCompressBuffer += step;
  962. }
  963. //
  964. // Finish all tasks
  965. //
  966. }
  967. //
  968. // Exeption handling:
  969. //
  970. // LineBufferTask::execute() may have encountered exceptions, but
  971. // those exceptions occurred in another thread, not in the thread
  972. // that is executing this call to OutputFile::writePixels().
  973. // LineBufferTask::execute() has caught all exceptions and stored
  974. // the exceptions' what() strings in the line buffers.
  975. // Now we check if any line buffer contains a stored exception; if
  976. // this is the case then we re-throw the exception in this thread.
  977. // (It is possible that multiple line buffers contain stored
  978. // exceptions. We re-throw the first exception we find and
  979. // ignore all others.)
  980. //
  981. const string *exception = 0;
  982. for (size_t i = 0; i < _data->lineBuffers.size(); ++i)
  983. {
  984. LineBuffer *lineBuffer = _data->lineBuffers[i];
  985. if (lineBuffer->hasException && !exception)
  986. exception = &lineBuffer->exception;
  987. lineBuffer->hasException = false;
  988. }
  989. if (exception)
  990. throw IEX_NAMESPACE::IoExc (*exception);
  991. }
  992. catch (IEX_NAMESPACE::BaseExc &e)
  993. {
  994. REPLACE_EXC (e, "Failed to write pixel data to image "
  995. "file \"" << fileName() << "\". " << e.what());
  996. throw;
  997. }
  998. }
  999. int
  1000. OutputFile::currentScanLine () const
  1001. {
  1002. Lock lock (*_data->_streamData);
  1003. return _data->currentScanLine;
  1004. }
  1005. void
  1006. OutputFile::copyPixels (InputFile &in)
  1007. {
  1008. Lock lock (*_data->_streamData);
  1009. //
  1010. // Check if this file's and and the InputFile's
  1011. // headers are compatible.
  1012. //
  1013. const Header &hdr = _data->header;
  1014. const Header &inHdr = in.header();
  1015. if (inHdr.find("tiles") != inHdr.end())
  1016. THROW (IEX_NAMESPACE::ArgExc, "Cannot copy pixels from image "
  1017. "file \"" << in.fileName() << "\" to image "
  1018. "file \"" << fileName() << "\". "
  1019. "The input file is tiled, but the output file is "
  1020. "not. Try using TiledOutputFile::copyPixels "
  1021. "instead.");
  1022. if (!(hdr.dataWindow() == inHdr.dataWindow()))
  1023. THROW (IEX_NAMESPACE::ArgExc, "Cannot copy pixels from image "
  1024. "file \"" << in.fileName() << "\" to image "
  1025. "file \"" << fileName() << "\". "
  1026. "The files have different data windows.");
  1027. if (!(hdr.lineOrder() == inHdr.lineOrder()))
  1028. THROW (IEX_NAMESPACE::ArgExc, "Quick pixel copy from image "
  1029. "file \"" << in.fileName() << "\" to image "
  1030. "file \"" << fileName() << "\" failed. "
  1031. "The files have different line orders.");
  1032. if (!(hdr.compression() == inHdr.compression()))
  1033. THROW (IEX_NAMESPACE::ArgExc, "Quick pixel copy from image "
  1034. "file \"" << in.fileName() << "\" to image "
  1035. "file \"" << fileName() << "\" failed. "
  1036. "The files use different compression methods.");
  1037. if (!(hdr.channels() == inHdr.channels()))
  1038. THROW (IEX_NAMESPACE::ArgExc, "Quick pixel copy from image "
  1039. "file \"" << in.fileName() << "\" to image "
  1040. "file \"" << fileName() << "\" failed. "
  1041. "The files have different channel lists.");
  1042. //
  1043. // Verify that no pixel data have been written to this file yet.
  1044. //
  1045. const Box2i &dataWindow = hdr.dataWindow();
  1046. if (_data->missingScanLines != dataWindow.max.y - dataWindow.min.y + 1)
  1047. THROW (IEX_NAMESPACE::LogicExc, "Quick pixel copy from image "
  1048. "file \"" << in.fileName() << "\" to image "
  1049. "file \"" << fileName() << "\" failed. "
  1050. "\"" << fileName() << "\" already contains "
  1051. "pixel data.");
  1052. //
  1053. // Copy the pixel data.
  1054. //
  1055. while (_data->missingScanLines > 0)
  1056. {
  1057. const char *pixelData;
  1058. int pixelDataSize;
  1059. in.rawPixelData (_data->currentScanLine, pixelData, pixelDataSize);
  1060. writePixelData (_data->_streamData, _data, lineBufferMinY (_data->currentScanLine,
  1061. _data->minY,
  1062. _data->linesInBuffer),
  1063. pixelData, pixelDataSize);
  1064. _data->currentScanLine += (_data->lineOrder == INCREASING_Y)?
  1065. _data->linesInBuffer: -_data->linesInBuffer;
  1066. _data->missingScanLines -= _data->linesInBuffer;
  1067. }
  1068. }
  1069. void
  1070. OutputFile::copyPixels( InputPart & in)
  1071. {
  1072. copyPixels(*in.file);
  1073. }
  1074. void
  1075. OutputFile::updatePreviewImage (const PreviewRgba newPixels[])
  1076. {
  1077. Lock lock (*_data->_streamData);
  1078. if (_data->previewPosition <= 0)
  1079. THROW (IEX_NAMESPACE::LogicExc, "Cannot update preview image pixels. "
  1080. "File \"" << fileName() << "\" does not "
  1081. "contain a preview image.");
  1082. //
  1083. // Store the new pixels in the header's preview image attribute.
  1084. //
  1085. PreviewImageAttribute &pia =
  1086. _data->header.typedAttribute <PreviewImageAttribute> ("preview");
  1087. PreviewImage &pi = pia.value();
  1088. PreviewRgba *pixels = pi.pixels();
  1089. int numPixels = pi.width() * pi.height();
  1090. for (int i = 0; i < numPixels; ++i)
  1091. pixels[i] = newPixels[i];
  1092. //
  1093. // Save the current file position, jump to the position in
  1094. // the file where the preview image starts, store the new
  1095. // preview image, and jump back to the saved file position.
  1096. //
  1097. Int64 savedPosition = _data->_streamData->os->tellp();
  1098. try
  1099. {
  1100. _data->_streamData->os->seekp (_data->previewPosition);
  1101. pia.writeValueTo (*_data->_streamData->os, _data->version);
  1102. _data->_streamData->os->seekp (savedPosition);
  1103. }
  1104. catch (IEX_NAMESPACE::BaseExc &e)
  1105. {
  1106. REPLACE_EXC (e, "Cannot update preview image pixels for "
  1107. "file \"" << fileName() << "\". " << e.what());
  1108. throw;
  1109. }
  1110. }
  1111. void
  1112. OutputFile::breakScanLine (int y, int offset, int length, char c)
  1113. {
  1114. Lock lock (*_data->_streamData);
  1115. Int64 position =
  1116. _data->lineOffsets[(y - _data->minY) / _data->linesInBuffer];
  1117. if (!position)
  1118. THROW (IEX_NAMESPACE::ArgExc, "Cannot overwrite scan line " << y << ". "
  1119. "The scan line has not yet been stored in "
  1120. "file \"" << fileName() << "\".");
  1121. _data->_streamData->currentPosition = 0;
  1122. _data->_streamData->os->seekp (position + offset);
  1123. for (int i = 0; i < length; ++i)
  1124. _data->_streamData->os->write (&c, 1);
  1125. }
  1126. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT