2012-08-25 04:31:49 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
|
|
|
|
// Digital Ltd. LLC
|
2012-10-17 15:12:04 +08:00
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
// All rights reserved.
|
2012-10-17 15:12:04 +08:00
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
// 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
|
2012-10-17 15:12:04 +08:00
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
// 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 "IlmThreadPool.h"
|
|
|
|
#include "IlmThreadSemaphore.h"
|
|
|
|
#include "IlmThreadMutex.h"
|
|
|
|
#include "Iex.h"
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <fstream>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
2012-10-17 15:12:04 +08:00
|
|
|
const char *base = 0,
|
|
|
|
size_t xStride = 0,
|
|
|
|
size_t yStride = 0,
|
|
|
|
int xSampling = 1,
|
|
|
|
int ySampling = 1,
|
|
|
|
bool zero = false);
|
2012-08-25 04:31:49 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
OutSliceInfo::OutSliceInfo (PixelType t,
|
2012-10-17 15:12:04 +08:00
|
|
|
const char *b,
|
|
|
|
size_t xs, size_t ys,
|
|
|
|
int xsm, int ysm,
|
|
|
|
bool z)
|
2012-08-25 04:31:49 +08:00
|
|
|
:
|
|
|
|
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: public Mutex
|
|
|
|
{
|
|
|
|
Header header; // the image header
|
|
|
|
int version; // file format version
|
|
|
|
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
|
2012-10-17 15:12:04 +08:00
|
|
|
// each scanline
|
2012-08-25 04:31:49 +08:00
|
|
|
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
|
|
|
|
OStream * os; // file stream to write to
|
|
|
|
bool deleteStream;
|
|
|
|
Int64 lineOffsetsPosition; // file position for line
|
|
|
|
// offset table
|
|
|
|
Int64 currentPosition; // current file position
|
|
|
|
|
|
|
|
vector<LineBuffer*> lineBuffers; // each holds one line buffer
|
|
|
|
int linesInBuffer; // number of scanlines each
|
|
|
|
// buffer holds
|
|
|
|
size_t lineBufferSize; // size of the line buffer
|
|
|
|
|
|
|
|
Data (bool deleteStream, int numThreads);
|
|
|
|
~Data ();
|
|
|
|
|
|
|
|
|
|
|
|
inline LineBuffer * getLineBuffer (int number); // hash function from line
|
2012-10-17 15:12:04 +08:00
|
|
|
// buffer indices into our
|
|
|
|
// vector of line buffers
|
2012-08-25 04:31:49 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
OutputFile::Data::Data (bool deleteStream, int numThreads):
|
|
|
|
os (0),
|
|
|
|
deleteStream (deleteStream),
|
|
|
|
lineOffsetsPosition (0)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// 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 ()
|
|
|
|
{
|
|
|
|
if (deleteStream)
|
2012-10-17 15:12:04 +08:00
|
|
|
delete os;
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
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)
|
2012-10-17 15:12:04 +08:00
|
|
|
Iex::throwErrnoExc ("Cannot determine current file position (%T).");
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < lineOffsets.size(); i++)
|
2012-10-17 15:12:04 +08:00
|
|
|
Xdr::write <StreamIO> (os, lineOffsets[i]);
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
writePixelData (OutputFile::Data *ofd,
|
|
|
|
int lineBufferMinY,
|
2012-10-17 15:12:04 +08:00
|
|
|
const char pixelData[],
|
|
|
|
int pixelDataSize)
|
2012-08-25 04:31:49 +08:00
|
|
|
{
|
|
|
|
//
|
|
|
|
// 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 = ofd->currentPosition;
|
|
|
|
ofd->currentPosition = 0;
|
|
|
|
|
|
|
|
if (currentPosition == 0)
|
2012-10-17 15:12:04 +08:00
|
|
|
currentPosition = ofd->os->tellp();
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
ofd->lineOffsets[(ofd->currentScanLine - ofd->minY) / ofd->linesInBuffer] =
|
2012-10-17 15:12:04 +08:00
|
|
|
currentPosition;
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
assert (ofd->os->tellp() == currentPosition);
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Xdr::write <StreamIO> (*ofd->os, lineBufferMinY);
|
|
|
|
Xdr::write <StreamIO> (*ofd->os, pixelDataSize);
|
|
|
|
ofd->os->write (pixelData, pixelDataSize);
|
|
|
|
|
|
|
|
ofd->currentPosition = currentPosition +
|
2012-10-17 15:12:04 +08:00
|
|
|
Xdr::size<int>() +
|
|
|
|
Xdr::size<int>() +
|
|
|
|
pixelDataSize;
|
2012-08-25 04:31:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void
|
|
|
|
writePixelData (OutputFile::Data *ofd, const LineBuffer *lineBuffer)
|
|
|
|
{
|
|
|
|
writePixelData (ofd,
|
2012-10-17 15:12:04 +08:00
|
|
|
lineBuffer->minY,
|
2012-08-25 04:31:49 +08:00
|
|
|
lineBuffer->dataPtr,
|
2012-10-17 15:12:04 +08:00
|
|
|
lineBuffer->dataSize);
|
2012-08-25 04:31:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
convertToXdr (OutputFile::Data *ofd,
|
|
|
|
Array<char> &lineBuffer,
|
|
|
|
int lineBufferMinY,
|
|
|
|
int lineBufferMaxY,
|
2012-09-04 18:15:00 +08:00
|
|
|
int /*inSize*/)
|
2012-08-25 04:31:49 +08:00
|
|
|
{
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
int startY, endY; // The first and last scanlines in
|
2012-10-17 15:12:04 +08:00
|
|
|
// the file that are in the lineBuffer.
|
2012-08-25 04:31:49 +08:00
|
|
|
int step;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
if (ofd->lineOrder == INCREASING_Y)
|
|
|
|
{
|
2012-10-17 15:12:04 +08:00
|
|
|
startY = max (lineBufferMinY, ofd->minY);
|
|
|
|
endY = min (lineBufferMaxY, ofd->maxY) + 1;
|
2012-08-25 04:31:49 +08:00
|
|
|
step = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-17 15:12:04 +08:00
|
|
|
startY = min (lineBufferMaxY, ofd->maxY);
|
|
|
|
endY = max (lineBufferMinY, ofd->minY) - 1;
|
2012-08-25 04:31:49 +08:00
|
|
|
step = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Iterate over all scanlines in the lineBuffer to convert.
|
|
|
|
//
|
|
|
|
|
|
|
|
for (int y = startY; y != endY; y += step)
|
|
|
|
{
|
2012-10-17 15:12:04 +08:00
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
// Set these to point to the start of line y.
|
|
|
|
// We will write to writePtr from readPtr.
|
2012-10-17 15:12:04 +08:00
|
|
|
//
|
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
char *writePtr = lineBuffer + ofd->offsetInLineBuffer[y - ofd->minY];
|
|
|
|
const char *readPtr = writePtr;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
// Iterate over all slices in the file.
|
2012-10-17 15:12:04 +08:00
|
|
|
//
|
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
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
|
2012-10-17 15:12:04 +08:00
|
|
|
// slice i in scan line y (i.e. pixels within the data window
|
2012-08-25 04:31:49 +08:00
|
|
|
// for which x % xSampling == 0).
|
|
|
|
//
|
|
|
|
|
|
|
|
int dMinX = divp (ofd->minX, slice.xSampling);
|
|
|
|
int dMaxX = divp (ofd->maxX, slice.xSampling);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
// Convert the samples in place.
|
2012-10-17 15:12:04 +08:00
|
|
|
//
|
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
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,
|
2012-10-17 15:12:04 +08:00
|
|
|
int number,
|
2012-08-25 04:31:49 +08:00
|
|
|
int scanLineMin,
|
2012-10-17 15:12:04 +08:00
|
|
|
int scanLineMax);
|
2012-08-25 04:31:49 +08:00
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
virtual ~LineBufferTask ();
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
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 ();
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
//
|
|
|
|
// 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,
|
2012-10-17 15:12:04 +08:00
|
|
|
_ofd->maxY);
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
_lineBuffer->partiallyFull = true;
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
_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
|
2012-10-17 15:12:04 +08:00
|
|
|
// frame buffer into the line buffer
|
2012-08-25 04:31:49 +08:00
|
|
|
//
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
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;
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
int y;
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
for (y = yStart; y != yStop; y += dy)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Gather one scan line's worth of pixel data and store
|
|
|
|
// them in _ofd->lineBuffer.
|
|
|
|
//
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
char *writePtr = _lineBuffer->buffer +
|
|
|
|
_ofd->offsetInLineBuffer[y - _ofd->minY];
|
|
|
|
//
|
|
|
|
// Iterate over all image channels.
|
|
|
|
//
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
for (unsigned int i = 0; i < _ofd->slices.size(); ++i)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Test if scan line y of this channel contains any data
|
2012-10-17 15:12:04 +08:00
|
|
|
// (the scan line contains data only if y % ySampling == 0).
|
2012-08-25 04:31:49 +08:00
|
|
|
//
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
const OutSliceInfo &slice = _ofd->slices[i];
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
if (modp (y, slice.ySampling) != 0)
|
|
|
|
continue;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
//
|
|
|
|
// Find the x coordinates of the leftmost and rightmost
|
|
|
|
// sampled pixels (i.e. pixels within the data window
|
|
|
|
// for which x % xSampling == 0).
|
|
|
|
//
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
int dMinX = divp (_ofd->minX, slice.xSampling);
|
|
|
|
int dMaxX = divp (_ofd->maxX, slice.xSampling);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
//
|
2012-10-17 15:12:04 +08:00
|
|
|
// Fill the line buffer with with pixel data.
|
2012-08-25 04:31:49 +08:00
|
|
|
//
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
if (slice.zero)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// The frame buffer contains no data for this channel.
|
|
|
|
// Store zeroes in _lineBuffer->buffer.
|
|
|
|
//
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
fillChannelWithZeroes (writePtr, _ofd->format, slice.type,
|
|
|
|
dMaxX - dMinX + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// If necessary, convert the pixel data to Xdr format.
|
2012-10-17 15:12:04 +08:00
|
|
|
// Then store the pixel data in _ofd->lineBuffer.
|
2012-08-25 04:31:49 +08:00
|
|
|
//
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
const char *linePtr = slice.base +
|
|
|
|
divp (y, slice.ySampling) *
|
|
|
|
slice.yStride;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
const char *readPtr = linePtr + dMinX * slice.xStride;
|
|
|
|
const char *endPtr = linePtr + dMaxX * slice.xStride;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
copyFromFrameBuffer (writePtr, readPtr, endPtr,
|
|
|
|
slice.xStride, _ofd->format,
|
|
|
|
slice.type);
|
|
|
|
}
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
if (_lineBuffer->endOfLineBufferData < writePtr)
|
|
|
|
_lineBuffer->endOfLineBufferData = writePtr;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
#ifdef DEBUG
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
assert (writePtr - (_lineBuffer->buffer +
|
|
|
|
_ofd->offsetInLineBuffer[y - _ofd->minY]) ==
|
|
|
|
(int) _ofd->bytesPerLine[y - _ofd->minY]);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
#endif
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
//
|
|
|
|
// If the next scanline isn't past the bounds of the lineBuffer
|
|
|
|
// then we are done, otherwise compress the linebuffer
|
|
|
|
//
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
if (y >= _lineBuffer->minY && y <= _lineBuffer->maxY)
|
|
|
|
return;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
_lineBuffer->dataPtr = _lineBuffer->buffer;
|
|
|
|
|
|
|
|
_lineBuffer->dataSize = _lineBuffer->endOfLineBufferData -
|
|
|
|
_lineBuffer->buffer;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
// Compress the data
|
2012-10-17 15:12:04 +08:00
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
Compressor *compressor = _lineBuffer->compressor;
|
|
|
|
|
|
|
|
if (compressor)
|
|
|
|
{
|
|
|
|
const char *compPtr;
|
|
|
|
|
|
|
|
int compSize = compressor->compress (_lineBuffer->dataPtr,
|
|
|
|
_lineBuffer->dataSize,
|
|
|
|
_lineBuffer->minY, compPtr);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
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.
|
|
|
|
//
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
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 (true, numThreads))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2012-10-17 15:12:04 +08:00
|
|
|
header.sanityCheck();
|
|
|
|
_data->os = new StdOFStream (fileName);
|
|
|
|
initialize (header);
|
2012-08-25 04:31:49 +08:00
|
|
|
}
|
|
|
|
catch (Iex::BaseExc &e)
|
|
|
|
{
|
2012-10-17 15:12:04 +08:00
|
|
|
delete _data;
|
2012-08-25 04:31:49 +08:00
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
REPLACE_EXC (e, "Cannot open image file "
|
|
|
|
"\"" << fileName << "\". " << e);
|
|
|
|
throw;
|
2012-08-25 04:31:49 +08:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2012-10-17 15:12:04 +08:00
|
|
|
delete _data;
|
2012-08-25 04:31:49 +08:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OutputFile::OutputFile
|
|
|
|
(OStream &os,
|
|
|
|
const Header &header,
|
|
|
|
int numThreads)
|
|
|
|
:
|
|
|
|
_data (new Data (false, numThreads))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2012-10-17 15:12:04 +08:00
|
|
|
header.sanityCheck();
|
|
|
|
_data->os = &os;
|
|
|
|
initialize (header);
|
2012-08-25 04:31:49 +08:00
|
|
|
}
|
|
|
|
catch (Iex::BaseExc &e)
|
|
|
|
{
|
2012-10-17 15:12:04 +08:00
|
|
|
delete _data;
|
2012-08-25 04:31:49 +08:00
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
REPLACE_EXC (e, "Cannot open image file "
|
|
|
|
"\"" << os.fileName() << "\". " << e);
|
|
|
|
throw;
|
2012-08-25 04:31:49 +08:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2012-10-17 15:12:04 +08:00
|
|
|
delete _data;
|
2012-08-25 04:31:49 +08:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
OutputFile::initialize (const Header &header)
|
|
|
|
{
|
|
|
|
_data->header = header;
|
|
|
|
|
|
|
|
const Box2i &dataWindow = header.dataWindow();
|
|
|
|
|
|
|
|
_data->currentScanLine = (header.lineOrder() == INCREASING_Y)?
|
2012-10-17 15:12:04 +08:00
|
|
|
dataWindow.min.y: dataWindow.max.y;
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
_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,
|
2012-10-17 15:12:04 +08:00
|
|
|
_data->bytesPerLine);
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
for (size_t i = 0; i < _data->lineBuffers.size(); ++i)
|
|
|
|
{
|
|
|
|
_data->lineBuffers[i] =
|
2012-10-17 15:12:04 +08:00
|
|
|
new LineBuffer (newCompressor (_data->header.compression(),
|
|
|
|
maxBytesPerLine,
|
|
|
|
_data->header));
|
2012-08-25 04:31:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 +
|
2012-10-17 15:12:04 +08:00
|
|
|
_data->linesInBuffer) / _data->linesInBuffer;
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
_data->lineOffsets.resize (lineOffsetSize);
|
|
|
|
|
|
|
|
offsetInLineBufferTable (_data->bytesPerLine,
|
2012-10-17 15:12:04 +08:00
|
|
|
_data->linesInBuffer,
|
|
|
|
_data->offsetInLineBuffer);
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
_data->previewPosition =
|
2012-10-17 15:12:04 +08:00
|
|
|
_data->header.writeTo (*_data->os);
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
_data->lineOffsetsPosition =
|
2012-10-17 15:12:04 +08:00
|
|
|
writeLineOffsets (*_data->os, _data->lineOffsets);
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
_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 (...)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// We cannot safely throw any exceptions from here.
|
|
|
|
// This destructor may have been called because the
|
|
|
|
// stack is currently being unwound for another
|
|
|
|
// exception.
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
delete _data;
|
2012-08-25 04:31:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *
|
|
|
|
OutputFile::fileName () const
|
|
|
|
{
|
|
|
|
return _data->os->fileName();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const Header &
|
|
|
|
OutputFile::header () const
|
|
|
|
{
|
|
|
|
return _data->header;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
void
|
2012-08-25 04:31:49 +08:00
|
|
|
OutputFile::setFrameBuffer (const FrameBuffer &frameBuffer)
|
|
|
|
{
|
|
|
|
Lock lock (*_data);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
//
|
|
|
|
// 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();
|
2012-10-17 15:12:04 +08:00
|
|
|
i != channels.end();
|
|
|
|
++i)
|
2012-08-25 04:31:49 +08:00
|
|
|
{
|
2012-10-17 15:12:04 +08:00
|
|
|
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.");
|
2012-08-25 04:31:49 +08:00
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
//
|
|
|
|
// Initialize slice table for writePixels().
|
|
|
|
//
|
|
|
|
|
|
|
|
vector<OutSliceInfo> slices;
|
|
|
|
|
|
|
|
for (ChannelList::ConstIterator i = channels.begin();
|
2012-10-17 15:12:04 +08:00
|
|
|
i != channels.end();
|
|
|
|
++i)
|
2012-08-25 04:31:49 +08:00
|
|
|
{
|
2012-10-17 15:12:04 +08:00
|
|
|
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
|
|
|
|
}
|
2012-08-25 04:31:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Store the new frame buffer.
|
|
|
|
//
|
|
|
|
|
|
|
|
_data->frameBuffer = frameBuffer;
|
|
|
|
_data->slices = slices;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const FrameBuffer &
|
|
|
|
OutputFile::frameBuffer () const
|
|
|
|
{
|
|
|
|
Lock lock (*_data);
|
|
|
|
return _data->frameBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
void
|
2012-08-25 04:31:49 +08:00
|
|
|
OutputFile::writePixels (int numScanLines)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Lock lock (*_data);
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
if (_data->slices.size() == 0)
|
|
|
|
throw Iex::ArgExc ("No frame buffer specified "
|
|
|
|
"as pixel data source.");
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// 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
|
2012-10-17 15:12:04 +08:00
|
|
|
// all tasks are complete.
|
2012-08-25 04:31:49 +08:00
|
|
|
//
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
TaskGroup taskGroup;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
//
|
|
|
|
// Determine the range of lineBuffers that intersect the scan
|
2012-10-17 15:12:04 +08:00
|
|
|
// 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.
|
2012-08-25 04:31:49 +08:00
|
|
|
//
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
if (_data->lineOrder == INCREASING_Y)
|
|
|
|
{
|
|
|
|
int last = (_data->currentScanLine + (numScanLines - 1) -
|
|
|
|
_data->minY) / _data->linesInBuffer;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
scanLineMin = _data->currentScanLine;
|
|
|
|
scanLineMax = _data->currentScanLine + numScanLines - 1;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
int numTasks = max (min ((int)_data->lineBuffers.size(),
|
|
|
|
last - first + 1),
|
2012-10-17 15:12:04 +08:00
|
|
|
1);
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < numTasks; i++)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2012-08-25 04:31:49 +08:00
|
|
|
ThreadPool::addGlobalTask
|
|
|
|
(new LineBufferTask (&taskGroup, _data, first + i,
|
|
|
|
scanLineMin, scanLineMax));
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
nextCompressBuffer = first + numTasks;
|
|
|
|
stop = last + 1;
|
|
|
|
step = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int last = (_data->currentScanLine - (numScanLines - 1) -
|
|
|
|
_data->minY) / _data->linesInBuffer;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
scanLineMax = _data->currentScanLine;
|
|
|
|
scanLineMin = _data->currentScanLine - numScanLines + 1;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
int numTasks = max (min ((int)_data->lineBuffers.size(),
|
|
|
|
first - last + 1),
|
2012-10-17 15:12:04 +08:00
|
|
|
1);
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < numTasks; i++)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2012-08-25 04:31:49 +08:00
|
|
|
ThreadPool::addGlobalTask
|
|
|
|
(new LineBufferTask (&taskGroup, _data, first - i,
|
|
|
|
scanLineMin, scanLineMax));
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
nextCompressBuffer = first - numTasks;
|
|
|
|
stop = last - 1;
|
|
|
|
step = -1;
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (_data->missingScanLines <= 0)
|
|
|
|
{
|
|
|
|
throw Iex::ArgExc ("Tried to write more scan lines "
|
|
|
|
"than specified by the data window.");
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
// Wait until the next line buffer is ready to be written
|
2012-10-17 15:12:04 +08:00
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
LineBuffer *writeBuffer =
|
2012-10-17 15:12:04 +08:00
|
|
|
_data->getLineBuffer (nextWriteBuffer);
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
writeBuffer->wait();
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
int numLines = writeBuffer->scanLineMax -
|
2012-08-25 04:31:49 +08:00
|
|
|
writeBuffer->scanLineMin + 1;
|
|
|
|
|
|
|
|
_data->missingScanLines -= numLines;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
// If the line buffer is only partially full, then it is
|
2012-10-17 15:12:04 +08:00
|
|
|
// not complete and we cannot write it to disk yet.
|
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
if (writeBuffer->partiallyFull)
|
|
|
|
{
|
|
|
|
_data->currentScanLine = _data->currentScanLine +
|
|
|
|
step * numLines;
|
|
|
|
writeBuffer->post();
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
// Write the line buffer
|
2012-10-17 15:12:04 +08:00
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
writePixelData (_data, writeBuffer);
|
|
|
|
nextWriteBuffer += step;
|
|
|
|
|
|
|
|
_data->currentScanLine = _data->currentScanLine +
|
|
|
|
step * numLines;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
#ifdef DEBUG
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
assert (_data->currentScanLine ==
|
|
|
|
((_data->lineOrder == INCREASING_Y) ?
|
|
|
|
writeBuffer->scanLineMax + 1:
|
|
|
|
writeBuffer->scanLineMin - 1));
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-08-25 04:31:49 +08:00
|
|
|
#endif
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
// Release the lock on the line buffer
|
2012-10-17 15:12:04 +08:00
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
writeBuffer->post();
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
// If this was the last line buffer in the scanline range
|
2012-10-17 15:12:04 +08:00
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
if (nextWriteBuffer == stop)
|
|
|
|
break;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
// If there are no more line buffers to compress,
|
|
|
|
// then only continue to write out remaining lineBuffers
|
2012-10-17 15:12:04 +08:00
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
if (nextCompressBuffer == stop)
|
|
|
|
continue;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
// Add nextCompressBuffer as a compression task
|
2012-10-17 15:12:04 +08:00
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
ThreadPool::addGlobalTask
|
|
|
|
(new LineBufferTask (&taskGroup, _data, nextCompressBuffer,
|
|
|
|
scanLineMin, scanLineMax));
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
// Update the next line buffer we need to compress
|
2012-10-17 15:12:04 +08:00
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
nextCompressBuffer += step;
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
// Finish all tasks
|
2012-10-17 15:12:04 +08:00
|
|
|
//
|
2012-08-25 04:31:49 +08:00
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// 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;
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < _data->lineBuffers.size(); ++i)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2012-08-25 04:31:49 +08:00
|
|
|
LineBuffer *lineBuffer = _data->lineBuffers[i];
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
if (lineBuffer->hasException && !exception)
|
|
|
|
exception = &lineBuffer->exception;
|
2012-08-25 04:31:49 +08:00
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
lineBuffer->hasException = false;
|
|
|
|
}
|
2012-08-25 04:31:49 +08:00
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
if (exception)
|
|
|
|
throw Iex::IoExc (*exception);
|
2012-08-25 04:31:49 +08:00
|
|
|
}
|
|
|
|
catch (Iex::BaseExc &e)
|
|
|
|
{
|
2012-10-17 15:12:04 +08:00
|
|
|
REPLACE_EXC (e, "Failed to write pixel data to image "
|
|
|
|
"file \"" << fileName() << "\". " << e);
|
|
|
|
throw;
|
2012-08-25 04:31:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
int
|
2012-08-25 04:31:49 +08:00
|
|
|
OutputFile::currentScanLine () const
|
|
|
|
{
|
|
|
|
Lock lock (*_data);
|
|
|
|
return _data->currentScanLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
void
|
2012-08-25 04:31:49 +08:00
|
|
|
OutputFile::copyPixels (InputFile &in)
|
|
|
|
{
|
|
|
|
Lock lock (*_data);
|
|
|
|
|
|
|
|
//
|
|
|
|
// 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())
|
2012-10-17 15:12:04 +08:00
|
|
|
THROW (Iex::ArgExc, "Cannot copy pixels from image "
|
|
|
|
"file \"" << in.fileName() << "\" to image "
|
|
|
|
"file \"" << fileName() << "\". "
|
2012-08-25 04:31:49 +08:00
|
|
|
"The input file is tiled, but the output file is "
|
|
|
|
"not. Try using TiledOutputFile::copyPixels "
|
|
|
|
"instead.");
|
|
|
|
|
|
|
|
if (!(hdr.dataWindow() == inHdr.dataWindow()))
|
2012-10-17 15:12:04 +08:00
|
|
|
THROW (Iex::ArgExc, "Cannot copy pixels from image "
|
|
|
|
"file \"" << in.fileName() << "\" to image "
|
|
|
|
"file \"" << fileName() << "\". "
|
2012-08-25 04:31:49 +08:00
|
|
|
"The files have different data windows.");
|
|
|
|
|
|
|
|
if (!(hdr.lineOrder() == inHdr.lineOrder()))
|
2012-10-17 15:12:04 +08:00
|
|
|
THROW (Iex::ArgExc, "Quick pixel copy from image "
|
|
|
|
"file \"" << in.fileName() << "\" to image "
|
|
|
|
"file \"" << fileName() << "\" failed. "
|
|
|
|
"The files have different line orders.");
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
if (!(hdr.compression() == inHdr.compression()))
|
2012-10-17 15:12:04 +08:00
|
|
|
THROW (Iex::ArgExc, "Quick pixel copy from image "
|
|
|
|
"file \"" << in.fileName() << "\" to image "
|
|
|
|
"file \"" << fileName() << "\" failed. "
|
|
|
|
"The files use different compression methods.");
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
if (!(hdr.channels() == inHdr.channels()))
|
2012-10-17 15:12:04 +08:00
|
|
|
THROW (Iex::ArgExc, "Quick pixel copy from image "
|
|
|
|
"file \"" << in.fileName() << "\" to image "
|
|
|
|
"file \"" << fileName() << "\" failed. "
|
|
|
|
"The files have different channel lists.");
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// 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)
|
2012-10-17 15:12:04 +08:00
|
|
|
THROW (Iex::LogicExc, "Quick pixel copy from image "
|
|
|
|
"file \"" << in.fileName() << "\" to image "
|
|
|
|
"file \"" << fileName() << "\" failed. "
|
|
|
|
"\"" << fileName() << "\" already contains "
|
|
|
|
"pixel data.");
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// Copy the pixel data.
|
|
|
|
//
|
|
|
|
|
|
|
|
while (_data->missingScanLines > 0)
|
|
|
|
{
|
2012-10-17 15:12:04 +08:00
|
|
|
const char *pixelData;
|
|
|
|
int pixelDataSize;
|
2012-08-25 04:31:49 +08:00
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
in.rawPixelData (_data->currentScanLine, pixelData, pixelDataSize);
|
2012-08-25 04:31:49 +08:00
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
writePixelData (_data, lineBufferMinY (_data->currentScanLine,
|
|
|
|
_data->minY,
|
|
|
|
_data->linesInBuffer),
|
2012-08-25 04:31:49 +08:00
|
|
|
pixelData, pixelDataSize);
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
_data->currentScanLine += (_data->lineOrder == INCREASING_Y)?
|
|
|
|
_data->linesInBuffer: -_data->linesInBuffer;
|
2012-08-25 04:31:49 +08:00
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
_data->missingScanLines -= _data->linesInBuffer;
|
2012-08-25 04:31:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
OutputFile::updatePreviewImage (const PreviewRgba newPixels[])
|
|
|
|
{
|
|
|
|
Lock lock (*_data);
|
|
|
|
|
|
|
|
if (_data->previewPosition <= 0)
|
2012-10-17 15:12:04 +08:00
|
|
|
THROW (Iex::LogicExc, "Cannot update preview image pixels. "
|
|
|
|
"File \"" << fileName() << "\" does not "
|
|
|
|
"contain a preview image.");
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// Store the new pixels in the header's preview image attribute.
|
|
|
|
//
|
|
|
|
|
|
|
|
PreviewImageAttribute &pia =
|
2012-10-17 15:12:04 +08:00
|
|
|
_data->header.typedAttribute <PreviewImageAttribute> ("preview");
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
PreviewImage &pi = pia.value();
|
|
|
|
PreviewRgba *pixels = pi.pixels();
|
|
|
|
int numPixels = pi.width() * pi.height();
|
|
|
|
|
|
|
|
for (int i = 0; i < numPixels; ++i)
|
2012-10-17 15:12:04 +08:00
|
|
|
pixels[i] = newPixels[i];
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// 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->os->tellp();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2012-10-17 15:12:04 +08:00
|
|
|
_data->os->seekp (_data->previewPosition);
|
|
|
|
pia.writeValueTo (*_data->os, _data->version);
|
|
|
|
_data->os->seekp (savedPosition);
|
2012-08-25 04:31:49 +08:00
|
|
|
}
|
|
|
|
catch (Iex::BaseExc &e)
|
|
|
|
{
|
2012-10-17 15:12:04 +08:00
|
|
|
REPLACE_EXC (e, "Cannot update preview image pixels for "
|
|
|
|
"file \"" << fileName() << "\". " << e);
|
|
|
|
throw;
|
2012-08-25 04:31:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
void
|
2012-08-25 04:31:49 +08:00
|
|
|
OutputFile::breakScanLine (int y, int offset, int length, char c)
|
|
|
|
{
|
|
|
|
Lock lock (*_data);
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
Int64 position =
|
|
|
|
_data->lineOffsets[(y - _data->minY) / _data->linesInBuffer];
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
if (!position)
|
2012-10-17 15:12:04 +08:00
|
|
|
THROW (Iex::ArgExc, "Cannot overwrite scan line " << y << ". "
|
|
|
|
"The scan line has not yet been stored in "
|
|
|
|
"file \"" << fileName() << "\".");
|
2012-08-25 04:31:49 +08:00
|
|
|
|
|
|
|
_data->currentPosition = 0;
|
|
|
|
_data->os->seekp (position + offset);
|
|
|
|
|
|
|
|
for (int i = 0; i < length; ++i)
|
2012-10-17 15:12:04 +08:00
|
|
|
_data->os->write (&c, 1);
|
2012-08-25 04:31:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Imf
|