12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403 |
- //
- ///\todo: version needs fixing!
- //
- ///////////////////////////////////////////////////////////////////////////
- //
- // Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
- // Digital Ltd. LLC
- //
- // All rights reserved.
- //
- // Redistribution and use in source and binary forms, with or without
- // modification, are permitted provided that the following conditions are
- // met:
- // * Redistributions of source code must retain the above copyright
- // notice, this list of conditions and the following disclaimer.
- // * Redistributions in binary form must reproduce the above
- // copyright notice, this list of conditions and the following disclaimer
- // in the documentation and/or other materials provided with the
- // distribution.
- // * Neither the name of Industrial Light & Magic nor the names of
- // its contributors may be used to endorse or promote products derived
- // from this software without specific prior written permission.
- //
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- //
- ///////////////////////////////////////////////////////////////////////////
- //-----------------------------------------------------------------------------
- //
- // class OutputFile
- //
- //-----------------------------------------------------------------------------
- #include <ImfOutputFile.h>
- #include <ImfInputFile.h>
- #include <ImfChannelList.h>
- #include <ImfMisc.h>
- #include <ImfStdIO.h>
- #include <ImfCompressor.h>
- #include "ImathBox.h"
- #include "ImathFun.h"
- #include <ImfArray.h>
- #include "ImfXdr.h"
- #include <ImfPreviewImageAttribute.h>
- #include <ImfPartType.h>
- #include "IlmThreadPool.h"
- #include "ImfOutputStreamMutex.h"
- #include "IlmThreadSemaphore.h"
- #include "IlmThreadMutex.h"
- #include "Iex.h"
- #include "ImfInputPart.h"
- #include "ImfNamespace.h"
- #include "ImfOutputPartData.h"
- #include <string>
- #include <vector>
- #include <fstream>
- #include <assert.h>
- #include <algorithm>
- OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
- using IMATH_NAMESPACE::Box2i;
- using IMATH_NAMESPACE::divp;
- using IMATH_NAMESPACE::modp;
- using std::string;
- using std::vector;
- using std::ofstream;
- using std::min;
- using std::max;
- using ILMTHREAD_NAMESPACE::Mutex;
- using ILMTHREAD_NAMESPACE::Lock;
- using ILMTHREAD_NAMESPACE::Semaphore;
- using ILMTHREAD_NAMESPACE::Task;
- using ILMTHREAD_NAMESPACE::TaskGroup;
- using ILMTHREAD_NAMESPACE::ThreadPool;
- namespace {
- struct OutSliceInfo
- {
- PixelType type;
- const char * base;
- size_t xStride;
- size_t yStride;
- int xSampling;
- int ySampling;
- bool zero;
- OutSliceInfo (PixelType type = HALF,
- const char *base = 0,
- size_t xStride = 0,
- size_t yStride = 0,
- int xSampling = 1,
- int ySampling = 1,
- bool zero = false);
-
- };
- OutSliceInfo::OutSliceInfo (PixelType t,
- const char *b,
- size_t xs, size_t ys,
- int xsm, int ysm,
- bool z)
- :
- type (t),
- base (b),
- xStride (xs),
- yStride (ys),
- xSampling (xsm),
- ySampling (ysm),
- zero (z)
- {
- // empty
- }
- struct LineBuffer
- {
- Array<char> buffer;
- const char * dataPtr;
- int dataSize;
- char * endOfLineBufferData;
- int minY;
- int maxY;
- int scanLineMin;
- int scanLineMax;
- Compressor * compressor;
- bool partiallyFull; // has incomplete data
- bool hasException;
- string exception;
- LineBuffer (Compressor *comp);
- ~LineBuffer ();
- void wait () {_sem.wait();}
- void post () {_sem.post();}
- private:
- Semaphore _sem;
- };
- LineBuffer::LineBuffer (Compressor *comp) :
- dataPtr (0),
- dataSize (0),
- compressor (comp),
- partiallyFull (false),
- hasException (false),
- exception (),
- _sem (1)
- {
- // empty
- }
- LineBuffer::~LineBuffer ()
- {
- delete compressor;
- }
- } // namespace
- struct OutputFile::Data
- {
- Header header; // the image header
- bool multiPart; // is the file multipart?
- int version; // version attribute \todo NOT BEING WRITTEN PROPERLY
- Int64 previewPosition; // file position for preview
- FrameBuffer frameBuffer; // framebuffer to write into
- int currentScanLine; // next scanline to be written
- int missingScanLines; // number of lines to write
- LineOrder lineOrder; // the file's lineorder
- int minX; // data window's min x coord
- int maxX; // data window's max x coord
- int minY; // data window's min y coord
- int maxY; // data window's max x coord
- vector<Int64> lineOffsets; // stores offsets in file for
- // each scanline
- vector<size_t> bytesPerLine; // combined size of a line over
- // all channels
- vector<size_t> offsetInLineBuffer; // offset for each scanline in
- // its linebuffer
- Compressor::Format format; // compressor's data format
- vector<OutSliceInfo> slices; // info about channels in file
- Int64 lineOffsetsPosition; // file position for line
- // offset table
- vector<LineBuffer*> lineBuffers; // each holds one line buffer
- int linesInBuffer; // number of scanlines each
- // buffer holds
- size_t lineBufferSize; // size of the line buffer
- int partNumber; // the output part number
- OutputStreamMutex * _streamData;
- bool _deleteStream;
- Data (int numThreads);
- ~Data ();
- inline LineBuffer * getLineBuffer (int number); // hash function from line
- // buffer indices into our
- // vector of line buffers
- };
- OutputFile::Data::Data (int numThreads):
- lineOffsetsPosition (0),
- partNumber (-1),
- _streamData(0),
- _deleteStream(false)
- {
- //
- // We need at least one lineBuffer, but if threading is used,
- // to keep n threads busy we need 2*n lineBuffers.
- //
- lineBuffers.resize (max (1, 2 * numThreads));
- }
- OutputFile::Data::~Data ()
- {
- for (size_t i = 0; i < lineBuffers.size(); i++)
- delete lineBuffers[i];
- }
- LineBuffer*
- OutputFile::Data::getLineBuffer (int number)
- {
- return lineBuffers[number % lineBuffers.size()];
- }
- namespace {
- Int64
- writeLineOffsets (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os, const vector<Int64> &lineOffsets)
- {
- Int64 pos = os.tellp();
- if (pos == -1)
- IEX_NAMESPACE::throwErrnoExc ("Cannot determine current file position (%T).");
-
- for (unsigned int i = 0; i < lineOffsets.size(); i++)
- Xdr::write<StreamIO> (os, lineOffsets[i]);
- return pos;
- }
- void
- writePixelData (OutputStreamMutex *filedata,
- OutputFile::Data *partdata,
- int lineBufferMinY,
- const char pixelData[],
- int pixelDataSize)
- {
- //
- // Store a block of pixel data in the output file, and try
- // to keep track of the current writing position the file
- // without calling tellp() (tellp() can be fairly expensive).
- //
- Int64 currentPosition = filedata->currentPosition;
- filedata->currentPosition = 0;
- if (currentPosition == 0)
- currentPosition = filedata->os->tellp();
- partdata->lineOffsets[(partdata->currentScanLine - partdata->minY) / partdata->linesInBuffer] =
- currentPosition;
- #ifdef DEBUG
- assert (filedata->os->tellp() == currentPosition);
- #endif
-
-
-
- if (partdata->multiPart)
- {
- OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (*filedata->os, partdata->partNumber);
- }
-
- OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (*filedata->os, lineBufferMinY);
- OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (*filedata->os, pixelDataSize);
- filedata->os->write (pixelData, pixelDataSize);
- filedata->currentPosition = currentPosition +
- Xdr::size<int>() +
- Xdr::size<int>() +
- pixelDataSize;
- if (partdata->multiPart)
- {
- filedata->currentPosition += Xdr::size<int>();
- }
- }
- inline void
- writePixelData (OutputStreamMutex* filedata,
- OutputFile::Data *partdata,
- const LineBuffer *lineBuffer)
- {
- writePixelData (filedata, partdata,
- lineBuffer->minY,
- lineBuffer->dataPtr,
- lineBuffer->dataSize);
- }
- void
- convertToXdr (OutputFile::Data *ofd,
- Array<char> &lineBuffer,
- int lineBufferMinY,
- int lineBufferMaxY,
- int inSize)
- {
- //
- // Convert the contents of a lineBuffer from the machine's native
- // representation to Xdr format. This function is called by
- // CompressLineBuffer::execute(), below, if the compressor wanted
- // its input pixel data in the machine's native format, but then
- // failed to compress the data (most compressors will expand rather
- // than compress random input data).
- //
- // Note that this routine assumes that the machine's native
- // representation of the pixel data has the same size as the
- // Xdr representation. This makes it possible to convert the
- // pixel data in place, without an intermediate temporary buffer.
- //
-
- //
- // Iterate over all scanlines in the lineBuffer to convert.
- //
- char *writePtr = &lineBuffer[0];
- for (int y = lineBufferMinY; y <= lineBufferMaxY; y++)
- {
- //
- // Set these to point to the start of line y.
- // We will write to writePtr from readPtr.
- //
- const char *readPtr = writePtr;
-
- //
- // Iterate over all slices in the file.
- //
-
- for (unsigned int i = 0; i < ofd->slices.size(); ++i)
- {
- //
- // Test if scan line y of this channel is
- // contains any data (the scan line contains
- // data only if y % ySampling == 0).
- //
- const OutSliceInfo &slice = ofd->slices[i];
- if (modp (y, slice.ySampling) != 0)
- continue;
- //
- // Find the number of sampled pixels, dMaxX-dMinX+1, for
- // slice i in scan line y (i.e. pixels within the data window
- // for which x % xSampling == 0).
- //
- int dMinX = divp (ofd->minX, slice.xSampling);
- int dMaxX = divp (ofd->maxX, slice.xSampling);
-
- //
- // Convert the samples in place.
- //
-
- convertInPlace (writePtr, readPtr, slice.type, dMaxX - dMinX + 1);
- }
- }
- }
- //
- // A LineBufferTask encapsulates the task of copying a set of scanlines
- // from the user's frame buffer into a LineBuffer object, compressing
- // the data if necessary.
- //
- class LineBufferTask: public Task
- {
- public:
- LineBufferTask (TaskGroup *group,
- OutputFile::Data *ofd,
- int number,
- int scanLineMin,
- int scanLineMax);
- virtual ~LineBufferTask ();
- virtual void execute ();
- private:
- OutputFile::Data * _ofd;
- LineBuffer * _lineBuffer;
- };
- LineBufferTask::LineBufferTask
- (TaskGroup *group,
- OutputFile::Data *ofd,
- int number,
- int scanLineMin,
- int scanLineMax)
- :
- Task (group),
- _ofd (ofd),
- _lineBuffer (_ofd->getLineBuffer(number))
- {
- //
- // Wait for the lineBuffer to become available
- //
- _lineBuffer->wait ();
-
- //
- // Initialize the lineBuffer data if necessary
- //
- if (!_lineBuffer->partiallyFull)
- {
- _lineBuffer->endOfLineBufferData = _lineBuffer->buffer;
- _lineBuffer->minY = _ofd->minY + number * _ofd->linesInBuffer;
- _lineBuffer->maxY = min (_lineBuffer->minY + _ofd->linesInBuffer - 1,
- _ofd->maxY);
- _lineBuffer->partiallyFull = true;
- }
-
- _lineBuffer->scanLineMin = max (_lineBuffer->minY, scanLineMin);
- _lineBuffer->scanLineMax = min (_lineBuffer->maxY, scanLineMax);
- }
- LineBufferTask::~LineBufferTask ()
- {
- //
- // Signal that the line buffer is now free
- //
- _lineBuffer->post ();
- }
- void
- LineBufferTask::execute ()
- {
- try
- {
- //
- // First copy the pixel data from the
- // frame buffer into the line buffer
- //
-
- int yStart, yStop, dy;
- if (_ofd->lineOrder == INCREASING_Y)
- {
- yStart = _lineBuffer->scanLineMin;
- yStop = _lineBuffer->scanLineMax + 1;
- dy = 1;
- }
- else
- {
- yStart = _lineBuffer->scanLineMax;
- yStop = _lineBuffer->scanLineMin - 1;
- dy = -1;
- }
-
- int y;
- for (y = yStart; y != yStop; y += dy)
- {
- //
- // Gather one scan line's worth of pixel data and store
- // them in _ofd->lineBuffer.
- //
-
- char *writePtr = _lineBuffer->buffer +
- _ofd->offsetInLineBuffer[y - _ofd->minY];
- //
- // Iterate over all image channels.
- //
- for (unsigned int i = 0; i < _ofd->slices.size(); ++i)
- {
- //
- // Test if scan line y of this channel contains any data
- // (the scan line contains data only if y % ySampling == 0).
- //
-
- const OutSliceInfo &slice = _ofd->slices[i];
-
- if (modp (y, slice.ySampling) != 0)
- continue;
-
- //
- // Find the x coordinates of the leftmost and rightmost
- // sampled pixels (i.e. pixels within the data window
- // for which x % xSampling == 0).
- //
-
- int dMinX = divp (_ofd->minX, slice.xSampling);
- int dMaxX = divp (_ofd->maxX, slice.xSampling);
-
- //
- // Fill the line buffer with with pixel data.
- //
-
- if (slice.zero)
- {
- //
- // The frame buffer contains no data for this channel.
- // Store zeroes in _lineBuffer->buffer.
- //
-
- fillChannelWithZeroes (writePtr, _ofd->format, slice.type,
- dMaxX - dMinX + 1);
- }
- else
- {
- //
- // If necessary, convert the pixel data to Xdr format.
- // Then store the pixel data in _ofd->lineBuffer.
- //
-
- const char *linePtr = slice.base +
- divp (y, slice.ySampling) *
- slice.yStride;
-
- const char *readPtr = linePtr + dMinX * slice.xStride;
- const char *endPtr = linePtr + dMaxX * slice.xStride;
-
- copyFromFrameBuffer (writePtr, readPtr, endPtr,
- slice.xStride, _ofd->format,
- slice.type);
- }
- }
-
- if (_lineBuffer->endOfLineBufferData < writePtr)
- _lineBuffer->endOfLineBufferData = writePtr;
-
- #ifdef DEBUG
-
- assert (writePtr - (_lineBuffer->buffer +
- _ofd->offsetInLineBuffer[y - _ofd->minY]) ==
- (int) _ofd->bytesPerLine[y - _ofd->minY]);
-
- #endif
-
- }
-
- //
- // If the next scanline isn't past the bounds of the lineBuffer
- // then we are done, otherwise compress the linebuffer
- //
-
- if (y >= _lineBuffer->minY && y <= _lineBuffer->maxY)
- return;
-
- _lineBuffer->dataPtr = _lineBuffer->buffer;
- _lineBuffer->dataSize = _lineBuffer->endOfLineBufferData -
- _lineBuffer->buffer;
-
- //
- // Compress the data
- //
- Compressor *compressor = _lineBuffer->compressor;
- if (compressor)
- {
- const char *compPtr;
- int compSize = compressor->compress (_lineBuffer->dataPtr,
- _lineBuffer->dataSize,
- _lineBuffer->minY, compPtr);
-
- if (compSize < _lineBuffer->dataSize)
- {
- _lineBuffer->dataSize = compSize;
- _lineBuffer->dataPtr = compPtr;
- }
- else if (_ofd->format == Compressor::NATIVE)
- {
- //
- // The data did not shrink during compression, but
- // we cannot write to the file using the machine's
- // native format, so we need to convert the lineBuffer
- // to Xdr.
- //
-
- convertToXdr (_ofd, _lineBuffer->buffer, _lineBuffer->minY,
- _lineBuffer->maxY, _lineBuffer->dataSize);
- }
- }
- _lineBuffer->partiallyFull = false;
- }
- catch (std::exception &e)
- {
- if (!_lineBuffer->hasException)
- {
- _lineBuffer->exception = e.what ();
- _lineBuffer->hasException = true;
- }
- }
- catch (...)
- {
- if (!_lineBuffer->hasException)
- {
- _lineBuffer->exception = "unrecognized exception";
- _lineBuffer->hasException = true;
- }
- }
- }
- } // namespace
- OutputFile::OutputFile
- (const char fileName[],
- const Header &header,
- int numThreads)
- :
- _data (new Data (numThreads))
- {
- _data->_streamData=new OutputStreamMutex ();
- _data->_deleteStream=true;
- try
- {
- header.sanityCheck();
- _data->_streamData->os = new StdOFStream (fileName);
- _data->multiPart=false; // only one header, not multipart
- initialize (header);
- _data->_streamData->currentPosition = _data->_streamData->os->tellp();
-
- // Write header and empty offset table to the file.
- writeMagicNumberAndVersionField(*_data->_streamData->os, _data->header);
- _data->previewPosition =
- _data->header.writeTo (*_data->_streamData->os);
- _data->lineOffsetsPosition =
- writeLineOffsets (*_data->_streamData->os,_data->lineOffsets);
- }
- catch (IEX_NAMESPACE::BaseExc &e)
- {
- // ~OutputFile will not run, so free memory here
- if (_data)
- {
- if (_data->_streamData)
- {
- delete _data->_streamData->os;
- delete _data->_streamData;
- }
- delete _data;
- }
- REPLACE_EXC (e, "Cannot open image file "
- "\"" << fileName << "\". " << e.what());
- throw;
- }
- catch (...)
- {
- // ~OutputFile will not run, so free memory here
- if (_data)
- {
- if (_data->_streamData)
- {
- delete _data->_streamData->os;
- delete _data->_streamData;
- }
- delete _data;
- }
- throw;
- }
- }
- OutputFile::OutputFile
- (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os,
- const Header &header,
- int numThreads)
- :
- _data (new Data (numThreads))
- {
- _data->_streamData=new OutputStreamMutex ();
- _data->_deleteStream=false;
- try
- {
- header.sanityCheck();
- _data->_streamData->os = &os;
- _data->multiPart=false;
- initialize (header);
- _data->_streamData->currentPosition = _data->_streamData->os->tellp();
- // Write header and empty offset table to the file.
- writeMagicNumberAndVersionField(*_data->_streamData->os, _data->header);
- _data->previewPosition =
- _data->header.writeTo (*_data->_streamData->os);
- _data->lineOffsetsPosition =
- writeLineOffsets (*_data->_streamData->os, _data->lineOffsets);
- }
- catch (IEX_NAMESPACE::BaseExc &e)
- {
- // ~OutputFile will not run, so free memory here
- if (_data)
- {
- if (_data->_streamData)
- delete _data->_streamData;
- delete _data;
- }
- REPLACE_EXC (e, "Cannot open image file "
- "\"" << os.fileName() << "\". " << e.what());
- throw;
- }
- catch (...)
- {
- // ~OutputFile will not run, so free memory here
- if (_data)
- {
- if (_data->_streamData)
- delete _data->_streamData;
- delete _data;
- }
- throw;
- }
- }
- OutputFile::OutputFile(const OutputPartData* part) : _data(NULL)
- {
- try
- {
- if (part->header.type() != SCANLINEIMAGE)
- throw IEX_NAMESPACE::ArgExc("Can't build a OutputFile from a type-mismatched part.");
- _data = new Data (part->numThreads);
- _data->_streamData = part->mutex;
- _data->_deleteStream=false;
- _data->multiPart=part->multipart;
- initialize (part->header);
- _data->partNumber = part->partNumber;
- _data->lineOffsetsPosition = part->chunkOffsetTablePosition;
- _data->previewPosition = part->previewPosition;
- }
- catch (IEX_NAMESPACE::BaseExc &e)
- {
- if (_data) delete _data;
- REPLACE_EXC (e, "Cannot initialize output part "
- "\"" << part->partNumber << "\". " << e.what());
- throw;
- }
- catch (...)
- {
- if (_data) delete _data;
- throw;
- }
- }
- void
- OutputFile::initialize (const Header &header)
- {
- _data->header = header;
- // "fix" the type if it happens to be set incorrectly
- // (attribute is optional, but ensure it is correct if it exists)
- if(_data->header.hasType())
- {
- _data->header.setType(SCANLINEIMAGE);
- }
-
- const Box2i &dataWindow = header.dataWindow();
- _data->currentScanLine = (header.lineOrder() == INCREASING_Y)?
- dataWindow.min.y: dataWindow.max.y;
- _data->missingScanLines = dataWindow.max.y - dataWindow.min.y + 1;
- _data->lineOrder = header.lineOrder();
- _data->minX = dataWindow.min.x;
- _data->maxX = dataWindow.max.x;
- _data->minY = dataWindow.min.y;
- _data->maxY = dataWindow.max.y;
- size_t maxBytesPerLine = bytesPerLineTable (_data->header,
- _data->bytesPerLine);
- for (size_t i = 0; i < _data->lineBuffers.size(); ++i)
- {
- _data->lineBuffers[i] =
- new LineBuffer (newCompressor (_data->header.compression(),
- maxBytesPerLine,
- _data->header));
- }
- LineBuffer *lineBuffer = _data->lineBuffers[0];
- _data->format = defaultFormat (lineBuffer->compressor);
- _data->linesInBuffer = numLinesInBuffer (lineBuffer->compressor);
- _data->lineBufferSize = maxBytesPerLine * _data->linesInBuffer;
- for (size_t i = 0; i < _data->lineBuffers.size(); i++)
- _data->lineBuffers[i]->buffer.resizeErase(_data->lineBufferSize);
- int lineOffsetSize = (dataWindow.max.y - dataWindow.min.y +
- _data->linesInBuffer) / _data->linesInBuffer;
- _data->lineOffsets.resize (lineOffsetSize);
-
-
- offsetInLineBufferTable (_data->bytesPerLine,
- _data->linesInBuffer,
- _data->offsetInLineBuffer);
- }
- OutputFile::~OutputFile ()
- {
- if (_data)
- {
- {
- Lock lock(*_data->_streamData);
- Int64 originalPosition = _data->_streamData->os->tellp();
- if (_data->lineOffsetsPosition > 0)
- {
- try
- {
- _data->_streamData->os->seekp (_data->lineOffsetsPosition);
- writeLineOffsets (*_data->_streamData->os, _data->lineOffsets);
- //
- // Restore the original position.
- //
- _data->_streamData->os->seekp (originalPosition);
- }
- catch (...)
- {
- //
- // We cannot safely throw any exceptions from here.
- // This destructor may have been called because the
- // stack is currently being unwound for another
- // exception.
- //
- }
- }
- }
- if (_data->_deleteStream && _data->_streamData)
- delete _data->_streamData->os;
- if (_data->partNumber == -1 && _data->_streamData)
- delete _data->_streamData;
- delete _data;
- }
- }
- const char *
- OutputFile::fileName () const
- {
- return _data->_streamData->os->fileName();
- }
- const Header &
- OutputFile::header () const
- {
- return _data->header;
- }
- void
- OutputFile::setFrameBuffer (const FrameBuffer &frameBuffer)
- {
- Lock lock (*_data->_streamData);
-
- //
- // Check if the new frame buffer descriptor
- // is compatible with the image file header.
- //
- const ChannelList &channels = _data->header.channels();
- for (ChannelList::ConstIterator i = channels.begin();
- i != channels.end();
- ++i)
- {
- FrameBuffer::ConstIterator j = frameBuffer.find (i.name());
- if (j == frameBuffer.end())
- continue;
- if (i.channel().type != j.slice().type)
- {
- THROW (IEX_NAMESPACE::ArgExc, "Pixel type of \"" << i.name() << "\" channel "
- "of output file \"" << fileName() << "\" is "
- "not compatible with the frame buffer's "
- "pixel type.");
- }
- if (i.channel().xSampling != j.slice().xSampling ||
- i.channel().ySampling != j.slice().ySampling)
- {
- THROW (IEX_NAMESPACE::ArgExc, "X and/or y subsampling factors "
- "of \"" << i.name() << "\" channel "
- "of output file \"" << fileName() << "\" are "
- "not compatible with the frame buffer's "
- "subsampling factors.");
- }
- }
-
- //
- // Initialize slice table for writePixels().
- //
- vector<OutSliceInfo> slices;
- for (ChannelList::ConstIterator i = channels.begin();
- i != channels.end();
- ++i)
- {
- FrameBuffer::ConstIterator j = frameBuffer.find (i.name());
- if (j == frameBuffer.end())
- {
- //
- // Channel i is not present in the frame buffer.
- // In the file, channel i will contain only zeroes.
- //
- slices.push_back (OutSliceInfo (i.channel().type,
- 0, // base
- 0, // xStride,
- 0, // yStride,
- i.channel().xSampling,
- i.channel().ySampling,
- true)); // zero
- }
- else
- {
- //
- // Channel i is present in the frame buffer.
- //
- slices.push_back (OutSliceInfo (j.slice().type,
- j.slice().base,
- j.slice().xStride,
- j.slice().yStride,
- j.slice().xSampling,
- j.slice().ySampling,
- false)); // zero
- }
- }
- //
- // Store the new frame buffer.
- //
- _data->frameBuffer = frameBuffer;
- _data->slices = slices;
- }
- const FrameBuffer &
- OutputFile::frameBuffer () const
- {
- Lock lock (*_data->_streamData);
- return _data->frameBuffer;
- }
- void
- OutputFile::writePixels (int numScanLines)
- {
- try
- {
- Lock lock (*_data->_streamData);
- if (_data->slices.size() == 0)
- throw IEX_NAMESPACE::ArgExc ("No frame buffer specified "
- "as pixel data source.");
- //
- // Maintain two iterators:
- // nextWriteBuffer: next linebuffer to be written to the file
- // nextCompressBuffer: next linebuffer to compress
- //
- int first = (_data->currentScanLine - _data->minY) /
- _data->linesInBuffer;
- int nextWriteBuffer = first;
- int nextCompressBuffer;
- int stop;
- int step;
- int scanLineMin;
- int scanLineMax;
- {
- //
- // Create a task group for all line buffer tasks. When the
- // taskgroup goes out of scope, the destructor waits until
- // all tasks are complete.
- //
-
- TaskGroup taskGroup;
-
- //
- // Determine the range of lineBuffers that intersect the scan
- // line range. Then add the initial compression tasks to the
- // thread pool. We always add in at least one task but the
- // individual task might not do anything if numScanLines == 0.
- //
-
- if (_data->lineOrder == INCREASING_Y)
- {
- int last = (_data->currentScanLine + (numScanLines - 1) -
- _data->minY) / _data->linesInBuffer;
-
- scanLineMin = _data->currentScanLine;
- scanLineMax = _data->currentScanLine + numScanLines - 1;
-
- int numTasks = max (min ((int)_data->lineBuffers.size(),
- last - first + 1),
- 1);
- for (int i = 0; i < numTasks; i++)
- {
- ThreadPool::addGlobalTask
- (new LineBufferTask (&taskGroup, _data, first + i,
- scanLineMin, scanLineMax));
- }
-
- nextCompressBuffer = first + numTasks;
- stop = last + 1;
- step = 1;
- }
- else
- {
- int last = (_data->currentScanLine - (numScanLines - 1) -
- _data->minY) / _data->linesInBuffer;
-
- scanLineMax = _data->currentScanLine;
- scanLineMin = _data->currentScanLine - numScanLines + 1;
-
- int numTasks = max (min ((int)_data->lineBuffers.size(),
- first - last + 1),
- 1);
- for (int i = 0; i < numTasks; i++)
- {
- ThreadPool::addGlobalTask
- (new LineBufferTask (&taskGroup, _data, first - i,
- scanLineMin, scanLineMax));
- }
-
- nextCompressBuffer = first - numTasks;
- stop = last - 1;
- step = -1;
- }
-
- while (true)
- {
- if (_data->missingScanLines <= 0)
- {
- throw IEX_NAMESPACE::ArgExc ("Tried to write more scan lines "
- "than specified by the data window.");
- }
-
- //
- // Wait until the next line buffer is ready to be written
- //
- LineBuffer *writeBuffer =
- _data->getLineBuffer (nextWriteBuffer);
- writeBuffer->wait();
-
- int numLines = writeBuffer->scanLineMax -
- writeBuffer->scanLineMin + 1;
- _data->missingScanLines -= numLines;
-
- //
- // If the line buffer is only partially full, then it is
- // not complete and we cannot write it to disk yet.
- //
- if (writeBuffer->partiallyFull)
- {
- _data->currentScanLine = _data->currentScanLine +
- step * numLines;
- writeBuffer->post();
-
- return;
- }
-
- //
- // Write the line buffer
- //
- writePixelData (_data->_streamData, _data, writeBuffer);
- nextWriteBuffer += step;
- _data->currentScanLine = _data->currentScanLine +
- step * numLines;
-
- #ifdef DEBUG
-
- assert (_data->currentScanLine ==
- ((_data->lineOrder == INCREASING_Y) ?
- writeBuffer->scanLineMax + 1:
- writeBuffer->scanLineMin - 1));
-
- #endif
-
- //
- // Release the lock on the line buffer
- //
- writeBuffer->post();
-
- //
- // If this was the last line buffer in the scanline range
- //
- if (nextWriteBuffer == stop)
- break;
-
- //
- // If there are no more line buffers to compress,
- // then only continue to write out remaining lineBuffers
- //
- if (nextCompressBuffer == stop)
- continue;
-
- //
- // Add nextCompressBuffer as a compression task
- //
- ThreadPool::addGlobalTask
- (new LineBufferTask (&taskGroup, _data, nextCompressBuffer,
- scanLineMin, scanLineMax));
-
- //
- // Update the next line buffer we need to compress
- //
- nextCompressBuffer += step;
- }
-
- //
- // Finish all tasks
- //
- }
-
- //
- // Exeption handling:
- //
- // LineBufferTask::execute() may have encountered exceptions, but
- // those exceptions occurred in another thread, not in the thread
- // that is executing this call to OutputFile::writePixels().
- // LineBufferTask::execute() has caught all exceptions and stored
- // the exceptions' what() strings in the line buffers.
- // Now we check if any line buffer contains a stored exception; if
- // this is the case then we re-throw the exception in this thread.
- // (It is possible that multiple line buffers contain stored
- // exceptions. We re-throw the first exception we find and
- // ignore all others.)
- //
- const string *exception = 0;
- for (size_t i = 0; i < _data->lineBuffers.size(); ++i)
- {
- LineBuffer *lineBuffer = _data->lineBuffers[i];
- if (lineBuffer->hasException && !exception)
- exception = &lineBuffer->exception;
- lineBuffer->hasException = false;
- }
- if (exception)
- throw IEX_NAMESPACE::IoExc (*exception);
- }
- catch (IEX_NAMESPACE::BaseExc &e)
- {
- REPLACE_EXC (e, "Failed to write pixel data to image "
- "file \"" << fileName() << "\". " << e.what());
- throw;
- }
- }
- int
- OutputFile::currentScanLine () const
- {
- Lock lock (*_data->_streamData);
- return _data->currentScanLine;
- }
- void
- OutputFile::copyPixels (InputFile &in)
- {
- Lock lock (*_data->_streamData);
- //
- // Check if this file's and and the InputFile's
- // headers are compatible.
- //
- const Header &hdr = _data->header;
- const Header &inHdr = in.header();
- if (inHdr.find("tiles") != inHdr.end())
- THROW (IEX_NAMESPACE::ArgExc, "Cannot copy pixels from image "
- "file \"" << in.fileName() << "\" to image "
- "file \"" << fileName() << "\". "
- "The input file is tiled, but the output file is "
- "not. Try using TiledOutputFile::copyPixels "
- "instead.");
- if (!(hdr.dataWindow() == inHdr.dataWindow()))
- THROW (IEX_NAMESPACE::ArgExc, "Cannot copy pixels from image "
- "file \"" << in.fileName() << "\" to image "
- "file \"" << fileName() << "\". "
- "The files have different data windows.");
- if (!(hdr.lineOrder() == inHdr.lineOrder()))
- THROW (IEX_NAMESPACE::ArgExc, "Quick pixel copy from image "
- "file \"" << in.fileName() << "\" to image "
- "file \"" << fileName() << "\" failed. "
- "The files have different line orders.");
- if (!(hdr.compression() == inHdr.compression()))
- THROW (IEX_NAMESPACE::ArgExc, "Quick pixel copy from image "
- "file \"" << in.fileName() << "\" to image "
- "file \"" << fileName() << "\" failed. "
- "The files use different compression methods.");
- if (!(hdr.channels() == inHdr.channels()))
- THROW (IEX_NAMESPACE::ArgExc, "Quick pixel copy from image "
- "file \"" << in.fileName() << "\" to image "
- "file \"" << fileName() << "\" failed. "
- "The files have different channel lists.");
- //
- // Verify that no pixel data have been written to this file yet.
- //
- const Box2i &dataWindow = hdr.dataWindow();
- if (_data->missingScanLines != dataWindow.max.y - dataWindow.min.y + 1)
- THROW (IEX_NAMESPACE::LogicExc, "Quick pixel copy from image "
- "file \"" << in.fileName() << "\" to image "
- "file \"" << fileName() << "\" failed. "
- "\"" << fileName() << "\" already contains "
- "pixel data.");
- //
- // Copy the pixel data.
- //
- while (_data->missingScanLines > 0)
- {
- const char *pixelData;
- int pixelDataSize;
- in.rawPixelData (_data->currentScanLine, pixelData, pixelDataSize);
- writePixelData (_data->_streamData, _data, lineBufferMinY (_data->currentScanLine,
- _data->minY,
- _data->linesInBuffer),
- pixelData, pixelDataSize);
- _data->currentScanLine += (_data->lineOrder == INCREASING_Y)?
- _data->linesInBuffer: -_data->linesInBuffer;
- _data->missingScanLines -= _data->linesInBuffer;
- }
- }
- void
- OutputFile::copyPixels( InputPart & in)
- {
- copyPixels(*in.file);
- }
- void
- OutputFile::updatePreviewImage (const PreviewRgba newPixels[])
- {
- Lock lock (*_data->_streamData);
- if (_data->previewPosition <= 0)
- THROW (IEX_NAMESPACE::LogicExc, "Cannot update preview image pixels. "
- "File \"" << fileName() << "\" does not "
- "contain a preview image.");
- //
- // Store the new pixels in the header's preview image attribute.
- //
- PreviewImageAttribute &pia =
- _data->header.typedAttribute <PreviewImageAttribute> ("preview");
- PreviewImage &pi = pia.value();
- PreviewRgba *pixels = pi.pixels();
- int numPixels = pi.width() * pi.height();
- for (int i = 0; i < numPixels; ++i)
- pixels[i] = newPixels[i];
- //
- // Save the current file position, jump to the position in
- // the file where the preview image starts, store the new
- // preview image, and jump back to the saved file position.
- //
- Int64 savedPosition = _data->_streamData->os->tellp();
- try
- {
- _data->_streamData->os->seekp (_data->previewPosition);
- pia.writeValueTo (*_data->_streamData->os, _data->version);
- _data->_streamData->os->seekp (savedPosition);
- }
- catch (IEX_NAMESPACE::BaseExc &e)
- {
- REPLACE_EXC (e, "Cannot update preview image pixels for "
- "file \"" << fileName() << "\". " << e.what());
- throw;
- }
- }
- void
- OutputFile::breakScanLine (int y, int offset, int length, char c)
- {
- Lock lock (*_data->_streamData);
- Int64 position =
- _data->lineOffsets[(y - _data->minY) / _data->linesInBuffer];
- if (!position)
- THROW (IEX_NAMESPACE::ArgExc, "Cannot overwrite scan line " << y << ". "
- "The scan line has not yet been stored in "
- "file \"" << fileName() << "\".");
- _data->_streamData->currentPosition = 0;
- _data->_streamData->os->seekp (position + offset);
- for (int i = 0; i < length; ++i)
- _data->_streamData->os->write (&c, 1);
- }
- OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT
|