123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554 |
- #include <ImfDeepScanLineOutputFile.h>
- #include <ImfDeepScanLineInputFile.h>
- #include <ImfDeepScanLineInputPart.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 "ImfDeepFrameBuffer.h"
- #include "ImfOutputStreamMutex.h"
- #include "ImfOutputPartData.h"
- #include "IlmThreadPool.h"
- #include "IlmThreadSemaphore.h"
- #include "IlmThreadMutex.h"
- #include "Iex.h"
- #include <string>
- #include <vector>
- #include <fstream>
- #include <assert.h>
- #include <algorithm>
- #include "ImfNamespace.h"
- 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;
- ptrdiff_t sampleStride;
- ptrdiff_t xStride;
- ptrdiff_t yStride;
- int xSampling;
- int ySampling;
- bool zero;
- OutSliceInfo (PixelType type = HALF,
- const char * base =NULL,
- ptrdiff_t sampleStride = 0,
- ptrdiff_t xStride = 0,
- ptrdiff_t yStride =0,
- int xSampling = 1,
- int ySampling = 1,
- bool zero = false);
- };
- OutSliceInfo::OutSliceInfo (PixelType t,
- const char * base,
- ptrdiff_t spstride,
- ptrdiff_t xst,
- ptrdiff_t yst,
- int xsm, int ysm,
- bool z)
- :
- type (t),
- base (base),
- sampleStride (spstride),
- xStride(xst),
- yStride(yst),
- xSampling (xsm),
- ySampling (ysm),
- zero (z)
- {
-
- }
- struct LineBuffer
- {
- Array< Array<char> > buffer;
- Array<char> consecutiveBuffer;
- const char * dataPtr;
- Int64 uncompressedDataSize;
- Int64 dataSize;
- Array<char> sampleCountTableBuffer;
- const char * sampleCountTablePtr;
- Int64 sampleCountTableSize;
- Compressor* sampleCountTableCompressor;
- int minY;
- int maxY;
- int scanLineMin;
- int scanLineMax;
- Compressor * compressor;
- bool partiallyFull;
- bool hasException;
- string exception;
- LineBuffer (int linesInBuffer);
- ~LineBuffer ();
- void wait () {_sem.wait();}
- void post () {_sem.post();}
- private:
- Semaphore _sem;
- };
- LineBuffer::LineBuffer (int linesInBuffer) :
- dataPtr (0),
- dataSize (0),
- sampleCountTablePtr (0),
- sampleCountTableCompressor (0),
- compressor (0),
- partiallyFull (false),
- hasException (false),
- exception (),
- _sem (1)
- {
- buffer.resizeErase(linesInBuffer);
- }
- LineBuffer::~LineBuffer ()
- {
- if (compressor != 0)
- delete compressor;
- if (sampleCountTableCompressor != 0)
- delete sampleCountTableCompressor;
- }
- }
- struct DeepScanLineOutputFile::Data
- {
- Header header;
- int version;
- bool multipart;
- Int64 previewPosition;
- DeepFrameBuffer frameBuffer;
- int currentScanLine;
- int missingScanLines;
- LineOrder lineOrder;
- int minX;
- int maxX;
- int minY;
- int maxY;
- vector<Int64> lineOffsets;
-
- vector<size_t> bytesPerLine;
-
- Compressor::Format format;
- vector<OutSliceInfo*> slices;
- Int64 lineOffsetsPosition;
-
- vector<LineBuffer*> lineBuffers;
- int linesInBuffer;
-
- int partNumber;
- char* sampleCountSliceBase;
-
- int sampleCountXStride;
- int sampleCountYStride;
- Array<unsigned int> lineSampleCount;
-
- Int64 maxSampleCountTableSize;
-
-
- OutputStreamMutex* _streamData;
- bool _deleteStream;
- Data (int numThreads);
- ~Data ();
- inline LineBuffer * getLineBuffer (int number);
-
-
- inline int& getSampleCount(int x, int y);
-
- };
- DeepScanLineOutputFile::Data::Data (int numThreads):
- lineOffsetsPosition (0),
- partNumber (-1) ,
- _streamData(NULL),
- _deleteStream(false)
- {
-
-
-
-
- lineBuffers.resize (max (1, 2 * numThreads));
- for (size_t i = 0; i < lineBuffers.size(); i++)
- lineBuffers[i] = 0;
- }
- DeepScanLineOutputFile::Data::~Data ()
- {
- for (size_t i = 0; i < lineBuffers.size(); i++)
- if (lineBuffers[i] != 0)
- delete lineBuffers[i];
- for (size_t i = 0; i < slices.size(); i++)
- delete slices[i];
- }
- int&
- DeepScanLineOutputFile::Data::getSampleCount(int x, int y)
- {
- return sampleCount(sampleCountSliceBase,
- sampleCountXStride,
- sampleCountYStride,
- x, y);
- }
- LineBuffer*
- DeepScanLineOutputFile::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++)
- OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (os, lineOffsets[i]);
- return pos;
- }
- void
- writePixelData (OutputStreamMutex *filedata,
- DeepScanLineOutputFile::Data *partdata,
- int lineBufferMinY,
- const char pixelData[],
- Int64 packedDataSize,
- Int64 unpackedDataSize,
- const char sampleCountTableData[],
- Int64 sampleCountTableSize)
- {
-
-
-
-
-
- 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, sampleCountTableSize);
-
-
-
- OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (*filedata->os, packedDataSize);
-
-
-
- OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (*filedata->os, unpackedDataSize);
-
-
-
- filedata->os->write (sampleCountTableData, sampleCountTableSize);
-
-
-
- filedata->os->write (pixelData, packedDataSize);
-
-
-
- filedata->currentPosition = currentPosition +
- Xdr::size<int>() +
- Xdr::size<Int64>() +
- Xdr::size<Int64>() +
- Xdr::size<Int64>() +
- sampleCountTableSize +
- packedDataSize;
- if (partdata->multipart)
- {
- filedata->currentPosition += Xdr::size<int>();
- }
- }
- inline void
- writePixelData (OutputStreamMutex* filedata,
- DeepScanLineOutputFile::Data *partdata,
- const LineBuffer *lineBuffer)
- {
- writePixelData (filedata, partdata,
- lineBuffer->minY,
- lineBuffer->dataPtr,
- lineBuffer->dataSize,
- lineBuffer->uncompressedDataSize,
- lineBuffer->sampleCountTablePtr,
- lineBuffer->sampleCountTableSize);
- }
- void
- convertToXdr (DeepScanLineOutputFile::Data *ofd,
- Array<char> &lineBuffer,
- int lineBufferMinY,
- int lineBufferMaxY,
- int inSize)
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- char* writePtr = &lineBuffer[0];
- for (int y = lineBufferMinY; y <= lineBufferMaxY; y++)
- {
-
-
-
-
- const char *readPtr = writePtr;
-
-
-
- for (unsigned int i = 0; i < ofd->slices.size(); ++i)
- {
-
-
-
-
-
- const OutSliceInfo &slice = *ofd->slices[i];
- if (modp (y, slice.ySampling) != 0)
- continue;
-
-
-
-
-
- int xSampleCount = ofd->lineSampleCount[y - ofd->minY];
-
-
-
- convertInPlace (writePtr, readPtr, slice.type, xSampleCount);
- }
- }
- }
- class LineBufferTask: public Task
- {
- public:
- LineBufferTask (TaskGroup *group,
- DeepScanLineOutputFile::Data *ofd,
- int number,
- int scanLineMin,
- int scanLineMax);
- virtual ~LineBufferTask ();
- virtual void execute ();
- private:
- DeepScanLineOutputFile::Data * _ofd;
- LineBuffer * _lineBuffer;
- };
- LineBufferTask::LineBufferTask
- (TaskGroup *group,
- DeepScanLineOutputFile::Data *ofd,
- int number,
- int scanLineMin,
- int scanLineMax)
- :
- Task (group),
- _ofd (ofd),
- _lineBuffer (_ofd->getLineBuffer(number))
- {
-
-
-
- _lineBuffer->wait ();
-
-
-
- if (!_lineBuffer->partiallyFull)
- {
- _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 ()
- {
-
-
-
- _lineBuffer->post ();
- }
- void
- LineBufferTask::execute ()
- {
- try
- {
-
-
-
-
- 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;
- }
-
-
-
-
- bytesPerDeepLineTable (_ofd->header,
- _lineBuffer->scanLineMin,
- _lineBuffer->scanLineMax,
- _ofd->sampleCountSliceBase,
- _ofd->sampleCountXStride,
- _ofd->sampleCountYStride,
- _ofd->bytesPerLine);
- for (int i = _lineBuffer->scanLineMin; i <= _lineBuffer->scanLineMax; i++)
- {
-
- _lineBuffer->buffer[i - _lineBuffer->minY].resizeErase(
- _ofd->bytesPerLine[i - _ofd->minY]);
- for (int j = _ofd->minX; j <= _ofd->maxX; j++)
- _ofd->lineSampleCount[i - _ofd->minY] += _ofd->getSampleCount(j, i);
- }
-
-
-
- int y;
- for (y = yStart; y != yStop; y += dy)
- {
-
-
-
-
- char *writePtr = &_lineBuffer->buffer[y - _lineBuffer->minY][0];
-
-
-
- for (unsigned int i = 0; i < _ofd->slices.size(); ++i)
- {
-
-
-
-
- const OutSliceInfo &slice = *_ofd->slices[i];
- if (modp (y, slice.ySampling) != 0)
- continue;
-
-
-
- if (slice.zero)
- {
-
-
-
-
- fillChannelWithZeroes (writePtr, _ofd->format, slice.type,
- _ofd->lineSampleCount[y - _ofd->minY]);
- }
- else
- {
- copyFromDeepFrameBuffer (writePtr, slice.base,
- _ofd->sampleCountSliceBase,
- _ofd->sampleCountXStride,
- _ofd->sampleCountYStride,
- y, _ofd->minX, _ofd->maxX,
- 0, 0,
- 0, 0,
- slice.sampleStride,
- slice.xStride,
- slice.yStride,
- _ofd->format,
- slice.type);
- }
- }
- }
-
-
-
-
-
-
- if (y >= _lineBuffer->minY && y <= _lineBuffer->maxY)
- return;
-
-
-
- Int64 totalBytes = 0;
- Int64 maxBytesPerLine = 0;
- for (int i = 0; i < _lineBuffer->maxY - _lineBuffer->minY + 1; i++)
- {
- totalBytes += _lineBuffer->buffer[i].size();
- if (Int64(_lineBuffer->buffer[i].size()) > maxBytesPerLine)
- maxBytesPerLine = _lineBuffer->buffer[i].size();
- }
- _lineBuffer->consecutiveBuffer.resizeErase(totalBytes);
- int pos = 0;
- for (int i = 0; i < _lineBuffer->maxY - _lineBuffer->minY + 1; i++)
- {
- memcpy(_lineBuffer->consecutiveBuffer + pos,
- &_lineBuffer->buffer[i][0],
- _lineBuffer->buffer[i].size());
- pos += _lineBuffer->buffer[i].size();
- }
- _lineBuffer->dataPtr = _lineBuffer->consecutiveBuffer;
- _lineBuffer->dataSize = totalBytes;
- _lineBuffer->uncompressedDataSize = _lineBuffer->dataSize;
-
-
-
- char* ptr = _lineBuffer->sampleCountTableBuffer;
- Int64 tableDataSize = 0;
- for (int i = _lineBuffer->minY; i <= _lineBuffer->maxY; i++)
- {
- int count = 0;
- for (int j = _ofd->minX; j <= _ofd->maxX; j++)
- {
- count += _ofd->getSampleCount(j, i);
- Xdr::write <CharPtrIO> (ptr, count);
- tableDataSize += sizeof (int);
- }
- }
- if(_lineBuffer->sampleCountTableCompressor)
- {
- _lineBuffer->sampleCountTableSize =
- _lineBuffer->sampleCountTableCompressor->compress (
- _lineBuffer->sampleCountTableBuffer,
- tableDataSize,
- _lineBuffer->minY,
- _lineBuffer->sampleCountTablePtr);
- }
-
-
-
- if (!_lineBuffer->sampleCountTableCompressor ||
- _lineBuffer->sampleCountTableSize >= tableDataSize)
- {
- _lineBuffer->sampleCountTableSize = tableDataSize;
- _lineBuffer->sampleCountTablePtr = _lineBuffer->sampleCountTableBuffer;
- }
-
-
-
-
- if (_lineBuffer->compressor != 0)
- delete _lineBuffer->compressor;
- _lineBuffer->compressor = newCompressor (_ofd->header.compression(),
- maxBytesPerLine,
- _ofd->header);
- Compressor *compressor = _lineBuffer->compressor;
- if (compressor)
- {
- const char *compPtr;
- Int64 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)
- {
-
-
-
-
-
-
- convertToXdr (_ofd, _lineBuffer->consecutiveBuffer, _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;
- }
- }
- }
- }
- DeepScanLineOutputFile::DeepScanLineOutputFile
- (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);
- initialize (header);
- _data->_streamData->currentPosition = _data->_streamData->os->tellp();
-
- writeMagicNumberAndVersionField(*_data->_streamData->os, _data->header);
- _data->previewPosition =
- _data->header.writeTo (*_data->_streamData->os);
- _data->lineOffsetsPosition =
- writeLineOffsets (*_data->_streamData->os, _data->lineOffsets);
- _data->multipart=false;
- }
- catch (IEX_NAMESPACE::BaseExc &e)
- {
- delete _data->_streamData->os;
- delete _data->_streamData;
- delete _data;
- REPLACE_EXC (e, "Cannot open image file "
- "\"" << fileName << "\". " << e.what());
- throw;
- }
- catch (...)
- {
- delete _data->_streamData->os;
- delete _data->_streamData;
- delete _data;
- throw;
- }
- }
- DeepScanLineOutputFile::DeepScanLineOutputFile
- (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;
- initialize (header);
- _data->_streamData->currentPosition = _data->_streamData->os->tellp();
-
- writeMagicNumberAndVersionField(*_data->_streamData->os, _data->header);
- _data->previewPosition =
- _data->header.writeTo (*_data->_streamData->os);
- _data->lineOffsetsPosition =
- writeLineOffsets (*_data->_streamData->os, _data->lineOffsets);
- _data->multipart=false;
- }
- catch (IEX_NAMESPACE::BaseExc &e)
- {
- delete _data->_streamData;
- delete _data;
- REPLACE_EXC (e, "Cannot open image file "
- "\"" << os.fileName() << "\". " << e.what());
- throw;
- }
- catch (...)
- {
- delete _data->_streamData;
- delete _data;
- throw;
- }
- }
- DeepScanLineOutputFile::DeepScanLineOutputFile(const OutputPartData* part)
- {
- try
- {
- if (part->header.type() != DEEPSCANLINE)
- throw IEX_NAMESPACE::ArgExc("Can't build a DeepScanLineOutputFile from a type-mismatched part.");
- _data = new Data (part->numThreads);
- _data->_streamData = part->mutex;
- _data->_deleteStream=false;
- initialize (part->header);
- _data->partNumber = part->partNumber;
- _data->lineOffsetsPosition = part->chunkOffsetTablePosition;
- _data->previewPosition = part->previewPosition;
- _data->multipart=part->multipart;
- }
- catch (IEX_NAMESPACE::BaseExc &e)
- {
- delete _data;
- REPLACE_EXC (e, "Cannot initialize output part "
- "\"" << part->partNumber << "\". " << e.what());
- throw;
- }
- catch (...)
- {
- delete _data;
- throw;
- }
- }
- void
- DeepScanLineOutputFile::initialize (const Header &header)
- {
- _data->header = header;
- _data->header.setType(DEEPSCANLINE);
-
- 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;
- _data->lineSampleCount.resizeErase(_data->maxY - _data->minY + 1);
- Compressor* compressor = newCompressor (_data->header.compression(),
- 0,
- _data->header);
- _data->format = defaultFormat (compressor);
- _data->linesInBuffer = numLinesInBuffer (compressor);
- if (compressor != 0)
- delete compressor;
- int lineOffsetSize = (_data->maxY - _data->minY +
- _data->linesInBuffer) / _data->linesInBuffer;
- _data->header.setChunkCount(lineOffsetSize);
- _data->lineOffsets.resize (lineOffsetSize);
- _data->bytesPerLine.resize (_data->maxY - _data->minY + 1);
- _data->maxSampleCountTableSize = min(_data->linesInBuffer, _data->maxY - _data->minY + 1) *
- (_data->maxX - _data->minX + 1) *
- sizeof(unsigned int);
- for (size_t i = 0; i < _data->lineBuffers.size(); ++i)
- {
- _data->lineBuffers[i] = new LineBuffer (_data->linesInBuffer);
- _data->lineBuffers[i]->sampleCountTableBuffer.resizeErase(_data->maxSampleCountTableSize);
- _data->lineBuffers[i]->sampleCountTableCompressor =
- newCompressor (_data->header.compression(),
- _data->maxSampleCountTableSize,
- _data->header);
- }
- }
- DeepScanLineOutputFile::~DeepScanLineOutputFile ()
- {
- {
- 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);
-
-
-
- _data->_streamData->os->seekp (originalPosition);
- }
- catch (...)
- {
-
-
-
-
-
-
- }
- }
- }
- if (_data->_deleteStream)
- delete _data->_streamData->os;
-
-
-
-
- if (_data->partNumber == -1)
- delete _data->_streamData;
- delete _data;
- }
- const char *
- DeepScanLineOutputFile::fileName () const
- {
- return _data->_streamData->os->fileName();
- }
- const Header &
- DeepScanLineOutputFile::header () const
- {
- return _data->header;
- }
- void
- DeepScanLineOutputFile::setFrameBuffer (const DeepFrameBuffer &frameBuffer)
- {
- Lock lock (*_data->_streamData);
-
-
-
-
- const ChannelList &channels = _data->header.channels();
- for (ChannelList::ConstIterator i = channels.begin();
- i != channels.end();
- ++i)
- {
- DeepFrameBuffer::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.");
- }
- }
-
-
-
-
- const Slice& sampleCountSlice = frameBuffer.getSampleCountSlice();
- if (sampleCountSlice.base == 0)
- {
- throw IEX_NAMESPACE::ArgExc ("Invalid base pointer, please set a proper sample count slice.");
- }
- else
- {
- _data->sampleCountSliceBase = sampleCountSlice.base;
- _data->sampleCountXStride = sampleCountSlice.xStride;
- _data->sampleCountYStride = sampleCountSlice.yStride;
- }
-
-
-
-
-
-
-
- vector<OutSliceInfo*> slices;
- for (ChannelList::ConstIterator i = channels.begin();
- i != channels.end();
- ++i)
- {
- DeepFrameBuffer::ConstIterator j = frameBuffer.find (i.name());
- if (j == frameBuffer.end())
- {
-
-
-
-
- slices.push_back (new OutSliceInfo (i.channel().type,
- NULL,
- 0,
- 0,
- 0,
- i.channel().xSampling,
- i.channel().ySampling,
- true));
- }
- else
- {
-
-
-
- slices.push_back (new OutSliceInfo (j.slice().type,
- j.slice().base,
- j.slice().sampleStride,
- j.slice().xStride,
- j.slice().yStride,
- j.slice().xSampling,
- j.slice().ySampling,
- false));
- }
- }
-
-
-
- _data->frameBuffer = frameBuffer;
- for (size_t i = 0; i < _data->slices.size(); i++)
- delete _data->slices[i];
- _data->slices = slices;
- }
- const DeepFrameBuffer &
- DeepScanLineOutputFile::frameBuffer () const
- {
- Lock lock (*_data->_streamData);
- return _data->frameBuffer;
- }
- void
- DeepScanLineOutputFile::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.");
-
-
-
-
-
- int first = (_data->currentScanLine - _data->minY) /
- _data->linesInBuffer;
- int nextWriteBuffer = first;
- int nextCompressBuffer;
- int stop;
- int step;
- int scanLineMin;
- int scanLineMax;
- {
-
-
-
-
-
- TaskGroup taskGroup;
-
-
-
-
-
-
- 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.");
- }
-
-
-
- LineBuffer *writeBuffer =
- _data->getLineBuffer (nextWriteBuffer);
- writeBuffer->wait();
- int numLines = writeBuffer->scanLineMax -
- writeBuffer->scanLineMin + 1;
- _data->missingScanLines -= numLines;
-
-
-
-
- if (writeBuffer->partiallyFull)
- {
- _data->currentScanLine = _data->currentScanLine +
- step * numLines;
- writeBuffer->post();
- return;
- }
-
-
-
- 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
-
-
-
- writeBuffer->post();
-
-
-
- if (nextWriteBuffer == stop)
- break;
-
-
-
-
- if (nextCompressBuffer == stop)
- continue;
-
-
-
- ThreadPool::addGlobalTask
- (new LineBufferTask (&taskGroup, _data, nextCompressBuffer,
- scanLineMin, scanLineMax));
-
-
-
- nextCompressBuffer += step;
- }
-
-
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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
- DeepScanLineOutputFile::currentScanLine () const
- {
- Lock lock (*_data->_streamData);
- return _data->currentScanLine;
- }
- void
- DeepScanLineOutputFile::copyPixels (DeepScanLineInputPart &in)
- {
- copyPixels(*in.file);
- }
- void
- DeepScanLineOutputFile::copyPixels (DeepScanLineInputFile &in)
- {
- Lock lock (*_data->_streamData);
-
-
-
-
- const Header &hdr = _data->header;
- const Header &inHdr = in.header();
- if(!inHdr.hasType() || inHdr.type()!=DEEPSCANLINE)
- {
- THROW (IEX_NAMESPACE::ArgExc, "Cannot copy pixels from image "
- "file \"" << in.fileName() << "\" to image "
- "file \"" << fileName() << "\": the input needs to be a deep scanline image");
- }
- 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.");
-
-
-
- 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.");
-
-
-
- vector<char> data(4096);
-
- while (_data->missingScanLines > 0)
- {
- Int64 dataSize = (Int64) data.size();
- in.rawPixelData(_data->currentScanLine, &data[0], dataSize);
- if(dataSize > data.size())
- {
-
- data.resize(dataSize);
- in.rawPixelData(_data->currentScanLine, &data[0], dataSize);
- }
-
-
- Int64 packedSampleCountSize = *(Int64 *) (&data[4]);
- Int64 packedDataSize = *(Int64 *) (&data[12]);
- Int64 unpackedDataSize = *(Int64 *) (&data[20]);
- const char * sampleCountTable = &data[0]+28;
- const char * pixelData = sampleCountTable + packedSampleCountSize;
-
-
- writePixelData (_data->_streamData, _data, lineBufferMinY (_data->currentScanLine,
- _data->minY,
- _data->linesInBuffer),
- pixelData, packedDataSize, unpackedDataSize,sampleCountTable,packedSampleCountSize);
- _data->currentScanLine += (_data->lineOrder == INCREASING_Y)?
- _data->linesInBuffer: -_data->linesInBuffer;
- _data->missingScanLines -= _data->linesInBuffer;
- }
- }
- void
- DeepScanLineOutputFile::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.");
-
-
-
- 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];
-
-
-
-
-
- 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;
- }
- }
- OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT
|