This source file includes following definitions.
- zero
- wait
- post
- _sem
- lineOffsetsPosition
- getLineBuffer
- writeLineOffsets
- writePixelData
- writePixelData
- convertToXdr
- _lineBuffer
- execute
- initialize
- fileName
- header
- setFrameBuffer
- frameBuffer
- writePixels
- currentScanLine
- copyPixels
- updatePreviewImage
- breakScanLine
#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 "IlmThreadPool.h"
#include "IlmThreadSemaphore.h"
#include "IlmThreadMutex.h"
#include "Iex.h"
#include <string>
#include <vector>
#include <fstream>
#include <assert.h>
#include <algorithm>
namespace Imf {
using Imath::Box2i;
using Imath::divp;
using Imath::modp;
using std::string;
using std::vector;
using std::ofstream;
using std::min;
using std::max;
using IlmThread::Mutex;
using IlmThread::Lock;
using IlmThread::Semaphore;
using IlmThread::Task;
using IlmThread::TaskGroup;
using IlmThread::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)
{
}
struct LineBuffer
{
Array<char> buffer;
const char * dataPtr;
int dataSize;
char * endOfLineBufferData;
int minY;
int maxY;
int scanLineMin;
int scanLineMax;
Compressor * compressor;
bool partiallyFull;
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)
{
}
LineBuffer::~LineBuffer ()
{
delete compressor;
}
}
struct OutputFile::Data: public Mutex
{
Header header;
int version;
Int64 previewPosition;
FrameBuffer frameBuffer;
int currentScanLine;
int missingScanLines;
LineOrder lineOrder;
int minX;
int maxX;
int minY;
int maxY;
vector<Int64> lineOffsets;
vector<size_t> bytesPerLine;
vector<size_t> offsetInLineBuffer;
Compressor::Format format;
vector<OutSliceInfo> slices;
OStream * os;
bool deleteStream;
Int64 lineOffsetsPosition;
Int64 currentPosition;
vector<LineBuffer*> lineBuffers;
int linesInBuffer;
size_t lineBufferSize;
Data (bool deleteStream, int numThreads);
~Data ();
inline LineBuffer * getLineBuffer (int number);
};
OutputFile::Data::Data (bool deleteStream, int numThreads):
os (0),
deleteStream (deleteStream),
lineOffsetsPosition (0)
{
lineBuffers.resize (max (1, 2 * numThreads));
}
OutputFile::Data::~Data ()
{
if (deleteStream)
delete os;
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 (OStream &os, const vector<Int64> &lineOffsets)
{
Int64 pos = os.tellp();
if (pos == -1)
Iex::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 (OutputFile::Data *ofd,
int lineBufferMinY,
const char pixelData[],
int pixelDataSize)
{
Int64 currentPosition = ofd->currentPosition;
ofd->currentPosition = 0;
if (currentPosition == 0)
currentPosition = ofd->os->tellp();
ofd->lineOffsets[(ofd->currentScanLine - ofd->minY) / ofd->linesInBuffer] =
currentPosition;
#ifdef DEBUG
assert (ofd->os->tellp() == currentPosition);
#endif
Xdr::write <StreamIO> (*ofd->os, lineBufferMinY);
Xdr::write <StreamIO> (*ofd->os, pixelDataSize);
ofd->os->write (pixelData, pixelDataSize);
ofd->currentPosition = currentPosition +
Xdr::size<int>() +
Xdr::size<int>() +
pixelDataSize;
}
inline void
writePixelData (OutputFile::Data *ofd, const LineBuffer *lineBuffer)
{
writePixelData (ofd,
lineBuffer->minY,
lineBuffer->dataPtr,
lineBuffer->dataSize);
}
void
convertToXdr (OutputFile::Data *ofd,
Array<char> &lineBuffer,
int lineBufferMinY,
int lineBufferMaxY,
int )
{
int startY, endY;
int step;
if (ofd->lineOrder == INCREASING_Y)
{
startY = max (lineBufferMinY, ofd->minY);
endY = min (lineBufferMaxY, ofd->maxY) + 1;
step = 1;
}
else
{
startY = min (lineBufferMaxY, ofd->maxY);
endY = max (lineBufferMinY, ofd->minY) - 1;
step = -1;
}
for (int y = startY; y != endY; y += step)
{
char *writePtr = lineBuffer + ofd->offsetInLineBuffer[y - ofd->minY];
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 dMinX = divp (ofd->minX, slice.xSampling);
int dMaxX = divp (ofd->maxX, slice.xSampling);
convertInPlace (writePtr, readPtr, slice.type, dMaxX - dMinX + 1);
}
}
}
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))
{
_lineBuffer->wait ();
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 ()
{
_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;
}
int y;
for (y = yStart; y != yStop; y += dy)
{
char *writePtr = _lineBuffer->buffer +
_ofd->offsetInLineBuffer[y - _ofd->minY];
for (unsigned int i = 0; i < _ofd->slices.size(); ++i)
{
const OutSliceInfo &slice = _ofd->slices[i];
if (modp (y, slice.ySampling) != 0)
continue;
int dMinX = divp (_ofd->minX, slice.xSampling);
int dMaxX = divp (_ofd->maxX, slice.xSampling);
if (slice.zero)
{
fillChannelWithZeroes (writePtr, _ofd->format, slice.type,
dMaxX - dMinX + 1);
}
else
{
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 (y >= _lineBuffer->minY && y <= _lineBuffer->maxY)
return;
_lineBuffer->dataPtr = _lineBuffer->buffer;
_lineBuffer->dataSize = _lineBuffer->endOfLineBufferData -
_lineBuffer->buffer;
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)
{
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;
}
}
}
}
OutputFile::OutputFile
(const char fileName[],
const Header &header,
int numThreads)
:
_data (new Data (true, numThreads))
{
try
{
header.sanityCheck();
_data->os = new StdOFStream (fileName);
initialize (header);
}
catch (Iex::BaseExc &e)
{
delete _data;
REPLACE_EXC (e, "Cannot open image file "
"\"" << fileName << "\". " << e);
throw;
}
catch (...)
{
delete _data;
throw;
}
}
OutputFile::OutputFile
(OStream &os,
const Header &header,
int numThreads)
:
_data (new Data (false, numThreads))
{
try
{
header.sanityCheck();
_data->os = &os;
initialize (header);
}
catch (Iex::BaseExc &e)
{
delete _data;
REPLACE_EXC (e, "Cannot open image file "
"\"" << os.fileName() << "\". " << e);
throw;
}
catch (...)
{
delete _data;
throw;
}
}
void
OutputFile::initialize (const Header &header)
{
_data->header = header;
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);
_data->previewPosition =
_data->header.writeTo (*_data->os);
_data->lineOffsetsPosition =
writeLineOffsets (*_data->os, _data->lineOffsets);
_data->currentPosition = _data->os->tellp();
}
OutputFile::~OutputFile ()
{
if (_data)
{
{
if (_data->lineOffsetsPosition > 0)
{
try
{
_data->os->seekp (_data->lineOffsetsPosition);
writeLineOffsets (*_data->os, _data->lineOffsets);
}
catch (...)
{
}
}
}
delete _data;
}
}
const char *
OutputFile::fileName () const
{
return _data->os->fileName();
}
const Header &
OutputFile::header () const
{
return _data->header;
}
void
OutputFile::setFrameBuffer (const FrameBuffer &frameBuffer)
{
Lock lock (*_data);
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::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::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.");
}
}
vector<OutSliceInfo> slices;
for (ChannelList::ConstIterator i = channels.begin();
i != channels.end();
++i)
{
FrameBuffer::ConstIterator j = frameBuffer.find (i.name());
if (j == frameBuffer.end())
{
slices.push_back (OutSliceInfo (i.channel().type,
0,
0,
0,
i.channel().xSampling,
i.channel().ySampling,
true));
}
else
{
slices.push_back (OutSliceInfo (j.slice().type,
j.slice().base,
j.slice().xStride,
j.slice().yStride,
j.slice().xSampling,
j.slice().ySampling,
false));
}
}
_data->frameBuffer = frameBuffer;
_data->slices = slices;
}
const FrameBuffer &
OutputFile::frameBuffer () const
{
Lock lock (*_data);
return _data->frameBuffer;
}
void
OutputFile::writePixels (int numScanLines)
{
try
{
Lock lock (*_data);
if (_data->slices.size() == 0)
throw Iex::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::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, 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 (int 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::IoExc (*exception);
}
catch (Iex::BaseExc &e)
{
REPLACE_EXC (e, "Failed to write pixel data to image "
"file \"" << fileName() << "\". " << e);
throw;
}
}
int
OutputFile::currentScanLine () const
{
Lock lock (*_data);
return _data->currentScanLine;
}
void
OutputFile::copyPixels (InputFile &in)
{
Lock lock (*_data);
const Header &hdr = _data->header;
const Header &inHdr = in.header();
if (inHdr.find("tiles") != inHdr.end())
THROW (Iex::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::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::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::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::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::LogicExc, "Quick pixel copy from image "
"file \"" << in.fileName() << "\" to image "
"file \"" << fileName() << "\" failed. "
"\"" << fileName() << "\" already contains "
"pixel data.");
while (_data->missingScanLines > 0)
{
const char *pixelData;
int pixelDataSize;
in.rawPixelData (_data->currentScanLine, pixelData, pixelDataSize);
writePixelData (_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::updatePreviewImage (const PreviewRgba newPixels[])
{
Lock lock (*_data);
if (_data->previewPosition <= 0)
THROW (Iex::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->os->tellp();
try
{
_data->os->seekp (_data->previewPosition);
pia.writeValueTo (*_data->os, _data->version);
_data->os->seekp (savedPosition);
}
catch (Iex::BaseExc &e)
{
REPLACE_EXC (e, "Cannot update preview image pixels for "
"file \"" << fileName() << "\". " << e);
throw;
}
}
void
OutputFile::breakScanLine (int y, int offset, int length, char c)
{
Lock lock (*_data);
Int64 position =
_data->lineOffsets[(y - _data->minY) / _data->linesInBuffer];
if (!position)
THROW (Iex::ArgExc, "Cannot overwrite scan line " << y << ". "
"The scan line has not yet been stored in "
"file \"" << fileName() << "\".");
_data->currentPosition = 0;
_data->os->seekp (position + offset);
for (int i = 0; i < length; ++i)
_data->os->write (&c, 1);
}
}