This source file includes following definitions.
- fillValue
- wait
- post
- _sem
- getLineBuffer
- reconstructLineOffsets
- readLineOffsets
- readPixelData
- _scanLineMax
- execute
- fileName
- header
- version
- setFrameBuffer
- frameBuffer
- isComplete
- readPixels
- readPixels
- rawPixelData
#include <ImfScanLineInputFile.h>
#include <ImfChannelList.h>
#include <ImfMisc.h>
#include <ImfStdIO.h>
#include <ImfCompressor.h>
#include "ImathBox.h"
#include "ImathFun.h"
#include <ImfXdr.h>
#include <ImfConvert.h>
#include <ImfThreading.h>
#include "IlmThreadPool.h"
#include "IlmThreadSemaphore.h"
#include "IlmThreadMutex.h"
#include "Iex.h"
#include <string>
#include <vector>
#include <assert.h>
#include <algorithm>
namespace Imf {
using Imath::Box2i;
using Imath::divp;
using Imath::modp;
using std::string;
using std::vector;
using std::ifstream;
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 InSliceInfo
{
PixelType typeInFrameBuffer;
PixelType typeInFile;
char * base;
size_t xStride;
size_t yStride;
int xSampling;
int ySampling;
bool fill;
bool skip;
double fillValue;
InSliceInfo (PixelType typeInFrameBuffer = HALF,
PixelType typeInFile = HALF,
char *base = 0,
size_t xStride = 0,
size_t yStride = 0,
int xSampling = 1,
int ySampling = 1,
bool fill = false,
bool skip = false,
double fillValue = 0.0);
};
InSliceInfo::InSliceInfo (PixelType tifb,
PixelType tifl,
char *b,
size_t xs, size_t ys,
int xsm, int ysm,
bool f, bool s,
double fv)
:
typeInFrameBuffer (tifb),
typeInFile (tifl),
base (b),
xStride (xs),
yStride (ys),
xSampling (xsm),
ySampling (ysm),
fill (f),
skip (s),
fillValue (fv)
{
}
struct LineBuffer
{
const char * uncompressedData;
char * buffer;
int dataSize;
int minY;
int maxY;
Compressor * compressor;
Compressor::Format format;
int number;
bool hasException;
string exception;
LineBuffer (Compressor * const comp);
~LineBuffer ();
inline void wait () {_sem.wait();}
inline void post () {_sem.post();}
private:
Semaphore _sem;
};
LineBuffer::LineBuffer (Compressor *comp):
uncompressedData (0),
buffer (0),
dataSize (0),
compressor (comp),
format (defaultFormat(compressor)),
number (-1),
hasException (false),
exception (),
_sem (1)
{
}
LineBuffer::~LineBuffer ()
{
delete compressor;
}
}
struct ScanLineInputFile::Data: public Mutex
{
Header header;
int version;
FrameBuffer frameBuffer;
LineOrder lineOrder;
int minX;
int maxX;
int minY;
int maxY;
vector<Int64> lineOffsets;
bool fileIsComplete;
int nextLineBufferMinY;
vector<size_t> bytesPerLine;
vector<size_t> offsetInLineBuffer;
vector<InSliceInfo> slices;
IStream * is;
vector<LineBuffer*> lineBuffers;
int linesInBuffer;
size_t lineBufferSize;
Data (IStream *is, int numThreads);
~Data ();
inline LineBuffer * getLineBuffer (int number);
};
ScanLineInputFile::Data::Data (IStream *is, int numThreads):
is (is)
{
lineBuffers.resize (max (1, 2 * numThreads));
}
ScanLineInputFile::Data::~Data ()
{
for (size_t i = 0; i < lineBuffers.size(); i++)
delete lineBuffers[i];
}
inline LineBuffer *
ScanLineInputFile::Data::getLineBuffer (int lineBufferNumber)
{
return lineBuffers[lineBufferNumber % lineBuffers.size()];
}
namespace {
void
reconstructLineOffsets (IStream &is,
LineOrder lineOrder,
vector<Int64> &lineOffsets)
{
Int64 position = is.tellg();
try
{
for (unsigned int i = 0; i < lineOffsets.size(); i++)
{
Int64 lineOffset = is.tellg();
int y;
Xdr::read <StreamIO> (is, y);
int dataSize;
Xdr::read <StreamIO> (is, dataSize);
Xdr::skip <StreamIO> (is, dataSize);
if (lineOrder == INCREASING_Y)
lineOffsets[i] = lineOffset;
else
lineOffsets[lineOffsets.size() - i - 1] = lineOffset;
}
}
catch (...)
{
}
is.clear();
is.seekg (position);
}
void
readLineOffsets (IStream &is,
LineOrder lineOrder,
vector<Int64> &lineOffsets,
bool &complete)
{
for (unsigned int i = 0; i < lineOffsets.size(); i++)
{
Xdr::read <StreamIO> (is, lineOffsets[i]);
}
complete = true;
for (unsigned int i = 0; i < lineOffsets.size(); i++)
{
if (lineOffsets[i] <= 0)
{
complete = false;
reconstructLineOffsets (is, lineOrder, lineOffsets);
break;
}
}
}
void
readPixelData (ScanLineInputFile::Data *ifd,
int minY,
char *&buffer,
int &dataSize)
{
Int64 lineOffset =
ifd->lineOffsets[(minY - ifd->minY) / ifd->linesInBuffer];
if (lineOffset == 0)
THROW (Iex::InputExc, "Scan line " << minY << " is missing.");
if (ifd->nextLineBufferMinY != minY)
ifd->is->seekg (lineOffset);
int yInFile;
Xdr::read <StreamIO> (*ifd->is, yInFile);
Xdr::read <StreamIO> (*ifd->is, dataSize);
if (yInFile != minY)
throw Iex::InputExc ("Unexpected data block y coordinate.");
if (dataSize > (int) ifd->lineBufferSize)
throw Iex::InputExc ("Unexpected data block length.");
if (ifd->is->isMemoryMapped ())
buffer = ifd->is->readMemoryMapped (dataSize);
else
ifd->is->read (buffer, dataSize);
if (ifd->lineOrder == INCREASING_Y)
ifd->nextLineBufferMinY = minY + ifd->linesInBuffer;
else
ifd->nextLineBufferMinY = minY - ifd->linesInBuffer;
}
class LineBufferTask : public Task
{
public:
LineBufferTask (TaskGroup *group,
ScanLineInputFile::Data *ifd,
LineBuffer *lineBuffer,
int scanLineMin,
int scanLineMax);
virtual ~LineBufferTask ();
virtual void execute ();
private:
ScanLineInputFile::Data * _ifd;
LineBuffer * _lineBuffer;
int _scanLineMin;
int _scanLineMax;
};
LineBufferTask::LineBufferTask
(TaskGroup *group,
ScanLineInputFile::Data *ifd,
LineBuffer *lineBuffer,
int scanLineMin,
int scanLineMax)
:
Task (group),
_ifd (ifd),
_lineBuffer (lineBuffer),
_scanLineMin (scanLineMin),
_scanLineMax (scanLineMax)
{
}
LineBufferTask::~LineBufferTask ()
{
_lineBuffer->post ();
}
void
LineBufferTask::execute ()
{
try
{
if (_lineBuffer->uncompressedData == 0)
{
int uncompressedSize = 0;
int maxY = min (_lineBuffer->maxY, _ifd->maxY);
for (int i = _lineBuffer->minY - _ifd->minY;
i <= maxY - _ifd->minY;
++i)
{
uncompressedSize += (int) _ifd->bytesPerLine[i];
}
if (_lineBuffer->compressor &&
_lineBuffer->dataSize < uncompressedSize)
{
_lineBuffer->format = _lineBuffer->compressor->format();
_lineBuffer->dataSize = _lineBuffer->compressor->uncompress
(_lineBuffer->buffer, _lineBuffer->dataSize,
_lineBuffer->minY, _lineBuffer->uncompressedData);
}
else
{
_lineBuffer->format = Compressor::XDR;
_lineBuffer->uncompressedData = _lineBuffer->buffer;
}
}
int yStart, yStop, dy;
if (_ifd->lineOrder == INCREASING_Y)
{
yStart = _scanLineMin;
yStop = _scanLineMax + 1;
dy = 1;
}
else
{
yStart = _scanLineMax;
yStop = _scanLineMin - 1;
dy = -1;
}
for (int y = yStart; y != yStop; y += dy)
{
const char *readPtr = _lineBuffer->uncompressedData +
_ifd->offsetInLineBuffer[y - _ifd->minY];
for (unsigned int i = 0; i < _ifd->slices.size(); ++i)
{
const InSliceInfo &slice = _ifd->slices[i];
if (modp (y, slice.ySampling) != 0)
continue;
int dMinX = divp (_ifd->minX, slice.xSampling);
int dMaxX = divp (_ifd->maxX, slice.xSampling);
if (slice.skip)
{
skipChannel (readPtr, slice.typeInFile, dMaxX - dMinX + 1);
}
else
{
char *linePtr = slice.base +
divp (y, slice.ySampling) *
slice.yStride;
char *writePtr = linePtr + dMinX * slice.xStride;
char *endPtr = linePtr + dMaxX * slice.xStride;
copyIntoFrameBuffer (readPtr, writePtr, endPtr,
slice.xStride, slice.fill,
slice.fillValue, _lineBuffer->format,
slice.typeInFrameBuffer,
slice.typeInFile);
}
}
}
}
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;
}
}
}
LineBufferTask *
newLineBufferTask
(TaskGroup *group,
ScanLineInputFile::Data *ifd,
int number,
int scanLineMin,
int scanLineMax)
{
LineBuffer *lineBuffer = ifd->getLineBuffer (number);
try
{
lineBuffer->wait ();
if (lineBuffer->number != number)
{
lineBuffer->minY = ifd->minY + number * ifd->linesInBuffer;
lineBuffer->maxY = lineBuffer->minY + ifd->linesInBuffer - 1;
lineBuffer->number = number;
lineBuffer->uncompressedData = 0;
readPixelData (ifd, lineBuffer->minY,
lineBuffer->buffer,
lineBuffer->dataSize);
}
}
catch (std::exception &e)
{
if (!lineBuffer->hasException)
{
lineBuffer->exception = e.what();
lineBuffer->hasException = true;
}
lineBuffer->number = -1;
lineBuffer->post();\
throw;
}
catch (...)
{
lineBuffer->exception = "unrecognized exception";
lineBuffer->hasException = true;
lineBuffer->number = -1;
lineBuffer->post();
throw;
}
scanLineMin = max (lineBuffer->minY, scanLineMin);
scanLineMax = min (lineBuffer->maxY, scanLineMax);
return new LineBufferTask (group, ifd, lineBuffer,
scanLineMin, scanLineMax);
}
}
ScanLineInputFile::ScanLineInputFile
(const Header &header,
IStream *is,
int numThreads)
:
_data (new Data (is, numThreads))
{
try
{
_data->header = header;
_data->lineOrder = _data->header.lineOrder();
const Box2i &dataWindow = _data->header.dataWindow();
_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));
}
_data->linesInBuffer =
numLinesInBuffer (_data->lineBuffers[0]->compressor);
_data->lineBufferSize = maxBytesPerLine * _data->linesInBuffer;
if (!_data->is->isMemoryMapped())
for (size_t i = 0; i < _data->lineBuffers.size(); i++)
_data->lineBuffers[i]->buffer = new char[_data->lineBufferSize];
_data->nextLineBufferMinY = _data->minY - 1;
offsetInLineBufferTable (_data->bytesPerLine,
_data->linesInBuffer,
_data->offsetInLineBuffer);
int lineOffsetSize = (dataWindow.max.y - dataWindow.min.y +
_data->linesInBuffer) / _data->linesInBuffer;
_data->lineOffsets.resize (lineOffsetSize);
readLineOffsets (*_data->is,
_data->lineOrder,
_data->lineOffsets,
_data->fileIsComplete);
}
catch (...)
{
delete _data;
throw;
}
}
ScanLineInputFile::~ScanLineInputFile ()
{
if (!_data->is->isMemoryMapped())
for (size_t i = 0; i < _data->lineBuffers.size(); i++)
delete [] _data->lineBuffers[i]->buffer;
delete _data;
}
const char *
ScanLineInputFile::fileName () const
{
return _data->is->fileName();
}
const Header &
ScanLineInputFile::header () const
{
return _data->header;
}
int
ScanLineInputFile::version () const
{
return _data->version;
}
void
ScanLineInputFile::setFrameBuffer (const FrameBuffer &frameBuffer)
{
Lock lock (*_data);
const ChannelList &channels = _data->header.channels();
for (FrameBuffer::ConstIterator j = frameBuffer.begin();
j != frameBuffer.end();
++j)
{
ChannelList::ConstIterator i = channels.find (j.name());
if (i == channels.end())
continue;
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 input file \"" << fileName() << "\" are "
"not compatible with the frame buffer's "
"subsampling factors.");
}
vector<InSliceInfo> slices;
ChannelList::ConstIterator i = channels.begin();
for (FrameBuffer::ConstIterator j = frameBuffer.begin();
j != frameBuffer.end();
++j)
{
while (i != channels.end() && strcmp (i.name(), j.name()) < 0)
{
slices.push_back (InSliceInfo (i.channel().type,
i.channel().type,
0,
0,
0,
i.channel().xSampling,
i.channel().ySampling,
false,
true,
0.0));
++i;
}
bool fill = false;
if (i == channels.end() || strcmp (i.name(), j.name()) > 0)
{
fill = true;
}
slices.push_back (InSliceInfo (j.slice().type,
fill? j.slice().type:
i.channel().type,
j.slice().base,
j.slice().xStride,
j.slice().yStride,
j.slice().xSampling,
j.slice().ySampling,
fill,
false,
j.slice().fillValue));
if (i != channels.end() && !fill)
++i;
}
_data->frameBuffer = frameBuffer;
_data->slices = slices;
}
const FrameBuffer &
ScanLineInputFile::frameBuffer () const
{
Lock lock (*_data);
return _data->frameBuffer;
}
bool
ScanLineInputFile::isComplete () const
{
return _data->fileIsComplete;
}
void
ScanLineInputFile::readPixels (int scanLine1, int scanLine2)
{
try
{
Lock lock (*_data);
if (_data->slices.size() == 0)
throw Iex::ArgExc ("No frame buffer specified "
"as pixel data destination.");
int scanLineMin = min (scanLine1, scanLine2);
int scanLineMax = max (scanLine1, scanLine2);
if (scanLineMin < _data->minY || scanLineMax > _data->maxY)
throw Iex::ArgExc ("Tried to read scan line outside "
"the image file's data window.");
int start, stop, dl;
if (_data->lineOrder == INCREASING_Y)
{
start = (scanLineMin - _data->minY) / _data->linesInBuffer;
stop = (scanLineMax - _data->minY) / _data->linesInBuffer + 1;
dl = 1;
}
else
{
start = (scanLineMax - _data->minY) / _data->linesInBuffer;
stop = (scanLineMin - _data->minY) / _data->linesInBuffer - 1;
dl = -1;
}
{
TaskGroup taskGroup;
for (int l = start; l != stop; l += dl)
{
ThreadPool::addGlobalTask (newLineBufferTask (&taskGroup,
_data, l,
scanLineMin,
scanLineMax));
}
}
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, "Error reading pixel data from image "
"file \"" << fileName() << "\". " << e);
throw;
}
}
void
ScanLineInputFile::readPixels (int scanLine)
{
readPixels (scanLine, scanLine);
}
void
ScanLineInputFile::rawPixelData (int firstScanLine,
const char *&pixelData,
int &pixelDataSize)
{
try
{
Lock lock (*_data);
if (firstScanLine < _data->minY || firstScanLine > _data->maxY)
{
throw Iex::ArgExc ("Tried to read scan line outside "
"the image file's data window.");
}
int minY = lineBufferMinY
(firstScanLine, _data->minY, _data->linesInBuffer);
readPixelData
(_data, minY, _data->lineBuffers[0]->buffer, pixelDataSize);
pixelData = _data->lineBuffers[0]->buffer;
}
catch (Iex::BaseExc &e)
{
REPLACE_EXC (e, "Error reading pixel data from image "
"file \"" << fileName() << "\". " << e);
throw;
}
}
}