opencv/3rdparty/openexr/IlmImf/ImfTiledOutputFile.cpp

1693 lines
42 KiB
C++
Raw Normal View History

2012-08-24 22:31:49 +02:00
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
// Digital Ltd. LLC
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
// All rights reserved.
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02: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 09:12:04 +02:00
// from this software without specific prior written permission.
//
2012-08-24 22:31:49 +02: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 TiledOutputFile
//
//-----------------------------------------------------------------------------
#include <ImfTiledOutputFile.h>
#include <ImfTiledInputFile.h>
#include <ImfInputFile.h>
#include <ImfTileDescriptionAttribute.h>
#include <ImfPreviewImageAttribute.h>
#include <ImfChannelList.h>
#include <ImfMisc.h>
#include <ImfTiledMisc.h>
#include <ImfStdIO.h>
#include <ImfCompressor.h>
#include "ImathBox.h"
#include <ImfArray.h>
#include <ImfXdr.h>
#include <ImfVersion.h>
#include <ImfTileOffsets.h>
#include <ImfThreading.h>
#include "IlmThreadPool.h"
#include "IlmThreadSemaphore.h"
#include "IlmThreadMutex.h"
#include "Iex.h"
#include <string>
#include <vector>
#include <fstream>
#include <assert.h>
#include <map>
namespace Imf {
using Imath::Box2i;
using Imath::V2i;
using std::string;
using std::vector;
using std::ofstream;
using std::map;
using std::min;
using std::max;
using std::swap;
using IlmThread::Mutex;
using IlmThread::Lock;
using IlmThread::Semaphore;
using IlmThread::Task;
using IlmThread::TaskGroup;
using IlmThread::ThreadPool;
namespace {
struct TOutSliceInfo
{
PixelType type;
const char * base;
size_t xStride;
size_t yStride;
bool zero;
int xTileCoords;
int yTileCoords;
TOutSliceInfo (PixelType type = HALF,
2012-10-17 09:12:04 +02:00
const char *base = 0,
size_t xStride = 0,
size_t yStride = 0,
bool zero = false,
2012-08-24 22:31:49 +02:00
int xTileCoords = 0,
int yTileCoords = 0);
};
TOutSliceInfo::TOutSliceInfo (PixelType t,
2012-10-17 09:12:04 +02:00
const char *b,
size_t xs, size_t ys,
bool z,
2012-08-24 22:31:49 +02:00
int xtc,
int ytc)
:
type (t),
base (b),
xStride (xs),
yStride (ys),
zero (z),
xTileCoords (xtc),
yTileCoords (ytc)
{
// empty
}
struct TileCoord
{
int dx;
int dy;
int lx;
int ly;
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
TileCoord (int xTile = 0, int yTile = 0,
2012-10-17 09:12:04 +02:00
int xLevel = 0, int yLevel = 0)
2012-08-24 22:31:49 +02:00
:
dx (xTile), dy (yTile),
2012-10-17 09:12:04 +02:00
lx (xLevel), ly (yLevel)
2012-08-24 22:31:49 +02:00
{
// empty
}
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
bool
operator < (const TileCoord &other) const
{
return (ly < other.ly) ||
2012-10-17 09:12:04 +02:00
(ly == other.ly && lx < other.lx) ||
((ly == other.ly && lx == other.lx) &&
((dy < other.dy) || (dy == other.dy && dx < other.dx)));
2012-08-24 22:31:49 +02:00
}
bool
operator == (const TileCoord &other) const
{
return lx == other.lx &&
2012-10-17 09:12:04 +02:00
ly == other.ly &&
dx == other.dx &&
dy == other.dy;
2012-08-24 22:31:49 +02:00
}
};
struct BufferedTile
{
char * pixelData;
int pixelDataSize;
BufferedTile (const char *data, int size):
2012-10-17 09:12:04 +02:00
pixelData (0),
pixelDataSize(size)
2012-08-24 22:31:49 +02:00
{
2012-10-17 09:12:04 +02:00
pixelData = new char[pixelDataSize];
memcpy (pixelData, data, pixelDataSize);
2012-08-24 22:31:49 +02:00
}
~BufferedTile()
{
2012-10-17 09:12:04 +02:00
delete [] pixelData;
2012-08-24 22:31:49 +02:00
}
};
typedef map <TileCoord, BufferedTile *> TileMap;
struct TileBuffer
{
Array<char> buffer;
const char * dataPtr;
int dataSize;
Compressor * compressor;
TileCoord tileCoord;
bool hasException;
string exception;
TileBuffer (Compressor *comp);
~TileBuffer ();
inline void wait () {_sem.wait();}
inline void post () {_sem.post();}
protected:
Semaphore _sem;
};
TileBuffer::TileBuffer (Compressor *comp):
dataPtr (0),
dataSize (0),
compressor (comp),
hasException (false),
exception (),
_sem (1)
{
// empty
}
TileBuffer::~TileBuffer ()
{
delete compressor;
}
} // namespace
struct TiledOutputFile::Data: public Mutex
{
Header header; // the image header
int version; // file format version
TileDescription tileDesc; // describes the tile layout
FrameBuffer frameBuffer; // framebuffer to write into
Int64 previewPosition;
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
int numXLevels; // number of x levels
int numYLevels; // number of y levels
int * numXTiles; // number of x tiles at a level
int * numYTiles; // number of y tiles at a level
TileOffsets tileOffsets; // stores offsets in file for
2012-10-17 09:12:04 +02:00
// each tile
2012-08-24 22:31:49 +02:00
Compressor::Format format; // compressor's data format
vector<TOutSliceInfo> slices; // info about channels in file
OStream * os; // file stream to write to
bool deleteStream;
size_t maxBytesPerTileLine; // combined size of a tile line
2012-10-17 09:12:04 +02:00
// over all channels
2012-08-24 22:31:49 +02:00
vector<TileBuffer*> tileBuffers;
size_t tileBufferSize; // size of a tile buffer
Int64 tileOffsetsPosition; // position of the tile index
Int64 currentPosition; // current position in the file
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
TileMap tileMap;
TileCoord nextTileToWrite;
Data (bool del, int numThreads);
~Data ();
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
inline TileBuffer * getTileBuffer (int number);
2012-10-17 09:12:04 +02:00
// hash function from tile
// buffer coords into our
// vector of tile buffers
2012-08-24 22:31:49 +02:00
TileCoord nextTileCoord (const TileCoord &a);
};
TiledOutputFile::Data::Data (bool del, int numThreads):
numXTiles(0),
numYTiles(0),
os (0),
deleteStream (del),
tileOffsetsPosition (0)
{
//
// We need at least one tileBuffer, but if threading is used,
// to keep n threads busy we need 2*n tileBuffers
//
tileBuffers.resize (max (1, 2 * numThreads));
}
TiledOutputFile::Data::~Data ()
{
delete [] numXTiles;
delete [] numYTiles;
if (deleteStream)
2012-10-17 09:12:04 +02:00
delete os;
2012-08-24 22:31:49 +02:00
//
// Delete all the tile buffers, if any still happen to exist
//
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
for (TileMap::iterator i = tileMap.begin(); i != tileMap.end(); ++i)
2012-10-17 09:12:04 +02:00
delete i->second;
2012-08-24 22:31:49 +02:00
for (size_t i = 0; i < tileBuffers.size(); i++)
delete tileBuffers[i];
}
TileBuffer*
TiledOutputFile::Data::getTileBuffer (int number)
{
return tileBuffers[number % tileBuffers.size()];
}
TileCoord
TiledOutputFile::Data::nextTileCoord (const TileCoord &a)
{
TileCoord b = a;
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
if (lineOrder == INCREASING_Y)
{
b.dx++;
if (b.dx >= numXTiles[b.lx])
{
b.dx = 0;
b.dy++;
if (b.dy >= numYTiles[b.ly])
{
2012-10-17 09:12:04 +02:00
//
// the next tile is in the next level
//
2012-08-24 22:31:49 +02:00
b.dy = 0;
switch (tileDesc.mode)
{
case ONE_LEVEL:
case MIPMAP_LEVELS:
b.lx++;
b.ly++;
break;
case RIPMAP_LEVELS:
b.lx++;
if (b.lx >= numXLevels)
{
b.lx = 0;
b.ly++;
2012-10-17 09:12:04 +02:00
#ifdef DEBUG
assert (b.ly <= numYLevels);
#endif
2012-08-24 22:31:49 +02:00
}
break;
}
}
}
}
else if (lineOrder == DECREASING_Y)
{
b.dx++;
if (b.dx >= numXTiles[b.lx])
{
b.dx = 0;
b.dy--;
if (b.dy < 0)
{
2012-10-17 09:12:04 +02:00
//
// the next tile is in the next level
//
2012-08-24 22:31:49 +02:00
switch (tileDesc.mode)
{
case ONE_LEVEL:
case MIPMAP_LEVELS:
b.lx++;
b.ly++;
break;
case RIPMAP_LEVELS:
b.lx++;
if (b.lx >= numXLevels)
{
b.lx = 0;
b.ly++;
2012-10-17 09:12:04 +02:00
#ifdef DEBUG
assert (b.ly <= numYLevels);
#endif
2012-08-24 22:31:49 +02:00
}
break;
}
2012-10-17 09:12:04 +02:00
if (b.ly < numYLevels)
b.dy = numYTiles[b.ly] - 1;
2012-08-24 22:31:49 +02:00
}
}
}
2012-10-17 09:12:04 +02:00
return b;
2012-08-24 22:31:49 +02:00
}
namespace {
void
writeTileData (TiledOutputFile::Data *ofd,
int dx, int dy,
2012-10-17 09:12:04 +02:00
int lx, int ly,
2012-08-24 22:31:49 +02:00
const char pixelData[],
int pixelDataSize)
{
//
// Store a block of pixel data in the output file, and try
// to keep track of the current writing position the file,
// without calling tellp() (tellp() can be fairly expensive).
//
Int64 currentPosition = ofd->currentPosition;
ofd->currentPosition = 0;
if (currentPosition == 0)
currentPosition = ofd->os->tellp();
ofd->tileOffsets (dx, dy, lx, ly) = currentPosition;
#ifdef DEBUG
2012-10-17 09:12:04 +02:00
assert (ofd->os->tellp() == currentPosition);
2012-08-24 22:31:49 +02:00
#endif
//
// Write the tile header.
//
Xdr::write <StreamIO> (*ofd->os, dx);
Xdr::write <StreamIO> (*ofd->os, dy);
Xdr::write <StreamIO> (*ofd->os, lx);
Xdr::write <StreamIO> (*ofd->os, ly);
Xdr::write <StreamIO> (*ofd->os, pixelDataSize);
2012-10-17 09:12:04 +02:00
ofd->os->write (pixelData, pixelDataSize);
2012-08-24 22:31:49 +02:00
//
2012-10-17 09:12:04 +02:00
// Keep current position in the file so that we can avoid
2012-08-24 22:31:49 +02:00
// redundant seekg() operations (seekg() can be fairly expensive).
//
ofd->currentPosition = currentPosition +
5 * Xdr::size<int>() +
pixelDataSize;
}
void
bufferedTileWrite (TiledOutputFile::Data *ofd,
int dx, int dy,
2012-10-17 09:12:04 +02:00
int lx, int ly,
2012-08-24 22:31:49 +02:00
const char pixelData[],
int pixelDataSize)
{
//
// Check if a tile with coordinates (dx,dy,lx,ly) has already been written.
//
if (ofd->tileOffsets (dx, dy, lx, ly))
{
2012-10-17 09:12:04 +02:00
THROW (Iex::ArgExc,
"Attempt to write tile "
"(" << dx << ", " << dy << ", " << lx << "," << ly << ") "
"more than once.");
2012-08-24 22:31:49 +02:00
}
//
// If tiles can be written in random order, then don't buffer anything.
//
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
if (ofd->lineOrder == RANDOM_Y)
{
writeTileData (ofd, dx, dy, lx, ly, pixelData, pixelDataSize);
return;
}
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
//
// If the tiles cannot be written in random order, then check if a
// tile with coordinates (dx,dy,lx,ly) has already been buffered.
//
TileCoord currentTile = TileCoord(dx, dy, lx, ly);
if (ofd->tileMap.find (currentTile) != ofd->tileMap.end())
{
2012-10-17 09:12:04 +02:00
THROW (Iex::ArgExc,
"Attempt to write tile "
"(" << dx << ", " << dy << ", " << lx << "," << ly << ") "
"more than once.");
2012-08-24 22:31:49 +02:00
}
//
// If all the tiles before this one have already been written to the file,
// then write this tile immediately and check if we have buffered tiles
// that can be written after this tile.
//
// Otherwise, buffer the tile so it can be written to file later.
//
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
if (ofd->nextTileToWrite == currentTile)
{
2012-10-17 09:12:04 +02:00
writeTileData (ofd, dx, dy, lx, ly, pixelData, pixelDataSize);
2012-08-24 22:31:49 +02:00
ofd->nextTileToWrite = ofd->nextTileCoord (ofd->nextTileToWrite);
TileMap::iterator i = ofd->tileMap.find (ofd->nextTileToWrite);
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
//
// Step through the tiles and write all successive buffered tiles after
// the current one.
//
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
while(i != ofd->tileMap.end())
{
//
// Write the tile, and then delete the tile's buffered data
//
writeTileData (ofd,
2012-10-17 09:12:04 +02:00
i->first.dx, i->first.dy,
i->first.lx, i->first.ly,
i->second->pixelData,
i->second->pixelDataSize);
2012-08-24 22:31:49 +02:00
delete i->second;
ofd->tileMap.erase (i);
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
//
// Proceed to the next tile
//
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
ofd->nextTileToWrite = ofd->nextTileCoord (ofd->nextTileToWrite);
i = ofd->tileMap.find (ofd->nextTileToWrite);
}
}
else
{
//
// Create a new BufferedTile, copy the pixelData into it, and
// insert it into the tileMap.
//
2012-10-17 09:12:04 +02:00
ofd->tileMap[currentTile] =
new BufferedTile ((const char *)pixelData, pixelDataSize);
2012-08-24 22:31:49 +02:00
}
}
void
convertToXdr (TiledOutputFile::Data *ofd,
Array<char>& tileBuffer,
2012-10-17 09:12:04 +02:00
int numScanLines,
int numPixelsPerScanLine)
2012-08-24 22:31:49 +02:00
{
//
2012-10-17 09:12:04 +02:00
// Convert the contents of a TiledOutputFile's tileBuffer from the
2012-08-24 22:31:49 +02:00
// machine's native representation to Xdr format. This function is called
// by writeTile(), 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.
//
//
// Set these to point to the start of the tile.
// We will write to toPtr, and read from fromPtr.
//
char *writePtr = tileBuffer;
const char *readPtr = writePtr;
//
// Iterate over all scan lines in the tile.
//
for (int y = 0; y < numScanLines; ++y)
{
2012-10-17 09:12:04 +02:00
//
// Iterate over all slices in the file.
//
for (unsigned int i = 0; i < ofd->slices.size(); ++i)
{
const TOutSliceInfo &slice = ofd->slices[i];
//
// Convert the samples in place.
//
2012-08-24 22:31:49 +02:00
convertInPlace (writePtr, readPtr, slice.type,
numPixelsPerScanLine);
2012-10-17 09:12:04 +02:00
}
2012-08-24 22:31:49 +02:00
}
#ifdef DEBUG
2012-10-17 09:12:04 +02:00
assert (writePtr == readPtr);
2012-08-24 22:31:49 +02:00
#endif
}
//
// A TileBufferTask encapsulates the task of copying a tile from
// the user's framebuffer into a LineBuffer and compressing the data
// if necessary.
//
class TileBufferTask: public Task
{
public:
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
TileBufferTask (TaskGroup *group,
TiledOutputFile::Data *ofd,
int number,
2012-10-17 09:12:04 +02:00
int dx, int dy,
int lx, int ly);
2012-08-24 22:31:49 +02:00
virtual ~TileBufferTask ();
virtual void execute ();
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
private:
TiledOutputFile::Data * _ofd;
TileBuffer * _tileBuffer;
};
TileBufferTask::TileBufferTask
(TaskGroup *group,
TiledOutputFile::Data *ofd,
int number,
int dx, int dy,
int lx, int ly)
:
Task (group),
_ofd (ofd),
_tileBuffer (_ofd->getTileBuffer (number))
{
//
// Wait for the tileBuffer to become available
//
_tileBuffer->wait ();
_tileBuffer->tileCoord = TileCoord (dx, dy, lx, ly);
}
TileBufferTask::~TileBufferTask ()
{
//
// Signal that the tile buffer is now free
//
_tileBuffer->post ();
}
void
TileBufferTask::execute ()
{
try
{
//
// First copy the pixel data from the frame buffer
2012-10-17 09:12:04 +02:00
// into the tile buffer
2012-08-24 22:31:49 +02:00
//
// Convert one tile's worth of pixel data to
// a machine-independent representation, and store
// the result in _tileBuffer->buffer.
//
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
char *writePtr = _tileBuffer->buffer;
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
Box2i tileRange = Imf::dataWindowForTile (_ofd->tileDesc,
_ofd->minX, _ofd->maxX,
_ofd->minY, _ofd->maxY,
_tileBuffer->tileCoord.dx,
_tileBuffer->tileCoord.dy,
_tileBuffer->tileCoord.lx,
_tileBuffer->tileCoord.ly);
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
int numScanLines = tileRange.max.y - tileRange.min.y + 1;
int numPixelsPerScanLine = tileRange.max.x - tileRange.min.x + 1;
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
//
// Iterate over the scan lines in the tile.
//
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
for (int y = tileRange.min.y; y <= tileRange.max.y; ++y)
{
//
// Iterate over all image channels.
//
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
for (unsigned int i = 0; i < _ofd->slices.size(); ++i)
{
const TOutSliceInfo &slice = _ofd->slices[i];
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
//
// These offsets are used to facilitate both absolute
// and tile-relative pixel coordinates.
//
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
int xOffset = slice.xTileCoords * tileRange.min.x;
int yOffset = slice.yTileCoords * tileRange.min.y;
2012-10-17 09:12:04 +02:00
//
// Fill the tile buffer with pixel data.
//
2012-08-24 22:31:49 +02:00
if (slice.zero)
{
//
// The frame buffer contains no data for this channel.
// Store zeroes in _data->tileBuffer.
//
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
fillChannelWithZeroes (writePtr, _ofd->format, slice.type,
numPixelsPerScanLine);
}
else
{
//
// The frame buffer contains data for this channel.
//
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
const char *readPtr = slice.base +
(y - yOffset) * slice.yStride +
(tileRange.min.x - xOffset) *
slice.xStride;
const char *endPtr = readPtr +
(numPixelsPerScanLine - 1) *
slice.xStride;
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
copyFromFrameBuffer (writePtr, readPtr, endPtr,
slice.xStride, _ofd->format,
slice.type);
}
}
}
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
//
2012-10-17 09:12:04 +02:00
// Compress the contents of the tileBuffer,
2012-08-24 22:31:49 +02:00
// and store the compressed data in the output file.
//
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
_tileBuffer->dataSize = writePtr - _tileBuffer->buffer;
_tileBuffer->dataPtr = _tileBuffer->buffer;
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
if (_tileBuffer->compressor)
{
const char *compPtr;
int compSize = _tileBuffer->compressor->compressTile
(_tileBuffer->dataPtr,
_tileBuffer->dataSize,
tileRange, compPtr);
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
if (compSize < _tileBuffer->dataSize)
{
_tileBuffer->dataSize = compSize;
_tileBuffer->dataPtr = compPtr;
}
else if (_ofd->format == Compressor::NATIVE)
{
//
// The data did not shrink during compression, but
// we cannot write to the file using native format,
// so we need to convert the lineBuffer to Xdr.
//
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
convertToXdr (_ofd, _tileBuffer->buffer, numScanLines,
numPixelsPerScanLine);
}
}
}
catch (std::exception &e)
{
if (!_tileBuffer->hasException)
{
_tileBuffer->exception = e.what ();
_tileBuffer->hasException = true;
}
}
catch (...)
{
if (!_tileBuffer->hasException)
{
_tileBuffer->exception = "unrecognized exception";
_tileBuffer->hasException = true;
}
}
}
} // namespace
TiledOutputFile::TiledOutputFile
(const char fileName[],
const Header &header,
int numThreads)
:
_data (new Data (true, numThreads))
{
try
{
2012-10-17 09:12:04 +02:00
header.sanityCheck (true);
_data->os = new StdOFStream (fileName);
initialize (header);
2012-08-24 22:31:49 +02:00
}
catch (Iex::BaseExc &e)
{
2012-10-17 09:12:04 +02:00
delete _data;
2012-08-24 22:31:49 +02:00
2012-10-17 09:12:04 +02:00
REPLACE_EXC (e, "Cannot open image file "
"\"" << fileName << "\". " << e);
throw;
2012-08-24 22:31:49 +02:00
}
catch (...)
{
2012-10-17 09:12:04 +02:00
delete _data;
2012-08-24 22:31:49 +02:00
throw;
}
}
TiledOutputFile::TiledOutputFile
(OStream &os,
const Header &header,
int numThreads)
:
_data (new Data (false, numThreads))
{
try
{
2012-10-17 09:12:04 +02:00
header.sanityCheck(true);
_data->os = &os;
initialize (header);
2012-08-24 22:31:49 +02:00
}
catch (Iex::BaseExc &e)
{
2012-10-17 09:12:04 +02:00
delete _data;
2012-08-24 22:31:49 +02:00
2012-10-17 09:12:04 +02:00
REPLACE_EXC (e, "Cannot open image file "
"\"" << os.fileName() << "\". " << e);
throw;
2012-08-24 22:31:49 +02:00
}
catch (...)
{
2012-10-17 09:12:04 +02:00
delete _data;
2012-08-24 22:31:49 +02:00
throw;
}
}
void
TiledOutputFile::initialize (const Header &header)
{
_data->header = header;
_data->lineOrder = _data->header.lineOrder();
//
// Check that the file is indeed tiled
//
_data->tileDesc = _data->header.tileDescription();
//
// Save the dataWindow information
//
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;
//
// Precompute level and tile information to speed up utility functions
//
precalculateTileInfo (_data->tileDesc,
2012-10-17 09:12:04 +02:00
_data->minX, _data->maxX,
_data->minY, _data->maxY,
_data->numXTiles, _data->numYTiles,
_data->numXLevels, _data->numYLevels);
2012-08-24 22:31:49 +02:00
//
// Determine the first tile coordinate that we will be writing
// if the file is not RANDOM_Y.
//
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
_data->nextTileToWrite = (_data->lineOrder == INCREASING_Y)?
2012-10-17 09:12:04 +02:00
TileCoord (0, 0, 0, 0):
TileCoord (0, _data->numYTiles[0] - 1, 0, 0);
2012-08-24 22:31:49 +02:00
_data->maxBytesPerTileLine =
2012-10-17 09:12:04 +02:00
calculateBytesPerPixel (_data->header) * _data->tileDesc.xSize;
2012-08-24 22:31:49 +02:00
_data->tileBufferSize = _data->maxBytesPerTileLine * _data->tileDesc.ySize;
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
//
// Create all the TileBuffers and allocate their internal buffers
//
for (size_t i = 0; i < _data->tileBuffers.size(); i++)
{
_data->tileBuffers[i] = new TileBuffer (newTileCompressor
2012-10-17 09:12:04 +02:00
(_data->header.compression(),
_data->maxBytesPerTileLine,
_data->tileDesc.ySize,
_data->header));
2012-08-24 22:31:49 +02:00
_data->tileBuffers[i]->buffer.resizeErase(_data->tileBufferSize);
}
_data->format = defaultFormat (_data->tileBuffers[0]->compressor);
_data->tileOffsets = TileOffsets (_data->tileDesc.mode,
2012-10-17 09:12:04 +02:00
_data->numXLevels,
_data->numYLevels,
_data->numXTiles,
_data->numYTiles);
2012-08-24 22:31:49 +02:00
_data->previewPosition = _data->header.writeTo (*_data->os, true);
_data->tileOffsetsPosition = _data->tileOffsets.writeTo (*_data->os);
_data->currentPosition = _data->os->tellp();
}
TiledOutputFile::~TiledOutputFile ()
{
if (_data)
{
{
if (_data->tileOffsetsPosition > 0)
{
try
{
_data->os->seekp (_data->tileOffsetsPosition);
_data->tileOffsets.writeTo (*_data->os);
}
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 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
delete _data;
}
}
const char *
TiledOutputFile::fileName () const
{
return _data->os->fileName();
}
const Header &
TiledOutputFile::header () const
{
return _data->header;
}
2012-10-17 09:12:04 +02:00
void
2012-08-24 22:31:49 +02:00
TiledOutputFile::setFrameBuffer (const FrameBuffer &frameBuffer)
{
Lock lock (*_data);
//
// 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 09:12:04 +02:00
i != channels.end();
++i)
2012-08-24 22:31:49 +02:00
{
2012-10-17 09:12:04 +02:00
FrameBuffer::ConstIterator j = frameBuffer.find (i.name());
2012-08-24 22:31:49 +02:00
2012-10-17 09:12:04 +02:00
if (j == frameBuffer.end())
continue;
2012-08-24 22:31:49 +02:00
2012-10-17 09:12:04 +02:00
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.");
2012-08-24 22:31:49 +02:00
2012-10-17 09:12:04 +02:00
if (j.slice().xSampling != 1 || j.slice().ySampling != 1)
THROW (Iex::ArgExc, "All channels in a tiled file must have"
"sampling (1,1).");
2012-08-24 22:31:49 +02:00
}
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
//
// Initialize slice table for writePixels().
//
vector<TOutSliceInfo> slices;
for (ChannelList::ConstIterator i = channels.begin();
2012-10-17 09:12:04 +02:00
i != channels.end();
++i)
{
FrameBuffer::ConstIterator j = frameBuffer.find (i.name());
if (j == frameBuffer.end())
2012-08-24 22:31:49 +02:00
{
2012-10-17 09:12:04 +02:00
//
// Channel i is not present in the frame buffer.
// In the file, channel i will contain only zeroes.
//
slices.push_back (TOutSliceInfo (i.channel().type,
0, // base
0, // xStride,
0, // yStride,
true)); // zero
}
else
{
//
// Channel i is present in the frame buffer.
//
slices.push_back (TOutSliceInfo (j.slice().type,
j.slice().base,
j.slice().xStride,
j.slice().yStride,
false, // zero
2012-08-24 22:31:49 +02:00
(j.slice().xTileCoords)? 1: 0,
(j.slice().yTileCoords)? 1: 0));
2012-10-17 09:12:04 +02:00
}
2012-08-24 22:31:49 +02:00
}
//
// Store the new frame buffer.
//
_data->frameBuffer = frameBuffer;
_data->slices = slices;
}
const FrameBuffer &
TiledOutputFile::frameBuffer () const
{
Lock lock (*_data);
return _data->frameBuffer;
}
2012-10-17 09:12:04 +02:00
void
2012-08-24 22:31:49 +02:00
TiledOutputFile::writeTiles (int dx1, int dx2, int dy1, int dy2,
int lx, int ly)
{
try
{
Lock lock (*_data);
if (_data->slices.size() == 0)
2012-10-17 09:12:04 +02:00
throw Iex::ArgExc ("No frame buffer specified "
"as pixel data source.");
2012-08-24 22:31:49 +02:00
2012-10-17 09:12:04 +02:00
if (!isValidTile (dx1, dy1, lx, ly) || !isValidTile (dx2, dy2, lx, ly))
throw Iex::ArgExc ("Tile coordinates are invalid.");
2012-08-24 22:31:49 +02:00
//
// Determine the first and last tile coordinates in both dimensions
// based on the file's lineOrder
//
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
if (dx1 > dx2)
swap (dx1, dx2);
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
if (dy1 > dy2)
swap (dy1, dy2);
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
int dyStart = dy1;
2012-10-17 09:12:04 +02:00
int dyStop = dy2 + 1;
int dY = 1;
2012-08-24 22:31:49 +02:00
if (_data->lineOrder == DECREASING_Y)
{
dyStart = dy2;
dyStop = dy1 - 1;
dY = -1;
}
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
int numTiles = (dx2 - dx1 + 1) * (dy2 - dy1 + 1);
int numTasks = min ((int)_data->tileBuffers.size(), numTiles);
//
// Create a task group for all tile buffer tasks. When the
2012-10-17 09:12:04 +02:00
// task group goes out of scope, the destructor waits until
// all tasks are complete.
2012-08-24 22:31:49 +02:00
//
{
TaskGroup taskGroup;
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
//
// Add in the initial compression tasks to the thread pool
//
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
int nextCompBuffer = 0;
2012-10-17 09:12:04 +02:00
int dxComp = dx1;
int dyComp = dyStart;
2012-08-24 22:31:49 +02:00
while (nextCompBuffer < numTasks)
{
ThreadPool::addGlobalTask (new TileBufferTask (&taskGroup,
_data,
nextCompBuffer++,
dxComp, dyComp,
lx, ly));
dxComp++;
if (dxComp > dx2)
{
dxComp = dx1;
dyComp += dY;
}
}
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
//
// Write the compressed buffers and add in more compression
2012-10-17 09:12:04 +02:00
// tasks until done
2012-08-24 22:31:49 +02:00
//
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
int nextWriteBuffer = 0;
2012-10-17 09:12:04 +02:00
int dxWrite = dx1;
int dyWrite = dyStart;
2012-08-24 22:31:49 +02:00
while (nextWriteBuffer < numTiles)
{
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
// Wait until the nextWriteBuffer is ready to be written
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
TileBuffer* writeBuffer =
_data->getTileBuffer (nextWriteBuffer);
writeBuffer->wait();
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
// Write the tilebuffer
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
bufferedTileWrite (_data, dxWrite, dyWrite, lx, ly,
writeBuffer->dataPtr,
writeBuffer->dataSize);
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
// Release the lock on nextWriteBuffer
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
writeBuffer->post();
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
// If there are no more tileBuffers to compress, then
2012-10-17 09:12:04 +02:00
// only continue to write out remaining tileBuffers,
// otherwise keep adding compression tasks.
//
2012-08-24 22:31:49 +02:00
if (nextCompBuffer < numTiles)
{
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
// add nextCompBuffer as a compression Task
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
ThreadPool::addGlobalTask
2012-10-17 09:12:04 +02:00
(new TileBufferTask (&taskGroup,
_data,
nextCompBuffer,
2012-08-24 22:31:49 +02:00
dxComp, dyComp,
2012-10-17 09:12:04 +02:00
lx, ly));
2012-08-24 22:31:49 +02:00
}
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
nextWriteBuffer++;
dxWrite++;
if (dxWrite > dx2)
{
dxWrite = dx1;
dyWrite += dY;
}
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
nextCompBuffer++;
dxComp++;
if (dxComp > dx2)
{
dxComp = dx1;
dyComp += dY;
}
}
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
// finish all tasks
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
}
2012-10-17 09:12:04 +02:00
//
// Exeption handling:
//
// TileBufferTask::execute() may have encountered exceptions, but
// those exceptions occurred in another thread, not in the thread
// that is executing this call to TiledOutputFile::writeTiles().
// TileBufferTask::execute() has caught all exceptions and stored
// the exceptions' what() strings in the tile buffers.
// Now we check if any tile buffer contains a stored exception; if
// this is the case then we re-throw the exception in this thread.
// (It is possible that multiple tile buffers contain stored
// exceptions. We re-throw the first exception we find and
// ignore all others.)
//
const string *exception = 0;
2012-08-24 22:31:49 +02:00
for (int i = 0; i < _data->tileBuffers.size(); ++i)
2012-10-17 09:12:04 +02:00
{
2012-08-24 22:31:49 +02:00
TileBuffer *tileBuffer = _data->tileBuffers[i];
2012-10-17 09:12:04 +02:00
if (tileBuffer->hasException && !exception)
exception = &tileBuffer->exception;
2012-08-24 22:31:49 +02:00
2012-10-17 09:12:04 +02:00
tileBuffer->hasException = false;
}
2012-08-24 22:31:49 +02:00
2012-10-17 09:12:04 +02:00
if (exception)
throw Iex::IoExc (*exception);
2012-08-24 22:31:49 +02:00
}
catch (Iex::BaseExc &e)
{
REPLACE_EXC (e, "Failed to write pixel data to image "
"file \"" << fileName() << "\". " << e);
throw;
}
}
2012-10-17 09:12:04 +02:00
void
2012-08-24 22:31:49 +02:00
TiledOutputFile::writeTiles (int dx1, int dxMax, int dyMin, int dyMax, int l)
{
writeTiles (dx1, dxMax, dyMin, dyMax, l, l);
}
2012-10-17 09:12:04 +02:00
void
2012-08-24 22:31:49 +02:00
TiledOutputFile::writeTile (int dx, int dy, int lx, int ly)
{
writeTiles (dx, dx, dy, dy, lx, ly);
}
void
TiledOutputFile::writeTile (int dx, int dy, int l)
{
writeTile(dx, dy, l, l);
}
2012-10-17 09:12:04 +02:00
void
2012-08-24 22:31:49 +02:00
TiledOutputFile::copyPixels (TiledInputFile &in)
{
Lock lock (*_data);
//
// Check if this file's and and the InputFile's
// headers are compatible.
//
const Header &hdr = _data->header;
2012-10-17 09:12:04 +02:00
const Header &inHdr = in.header();
2012-08-24 22:31:49 +02:00
if (!hdr.hasTileDescription() || !inHdr.hasTileDescription())
THROW (Iex::ArgExc, "Cannot perform a quick pixel copy from image "
2012-10-17 09:12:04 +02:00
"file \"" << in.fileName() << "\" to image "
"file \"" << fileName() << "\". The "
2012-08-24 22:31:49 +02:00
"output file is tiled, but the input file is not. "
"Try using OutputFile::copyPixels() instead.");
if (!(hdr.tileDescription() == inHdr.tileDescription()))
THROW (Iex::ArgExc, "Quick pixel copy from image "
2012-10-17 09:12:04 +02:00
"file \"" << in.fileName() << "\" to image "
"file \"" << fileName() << "\" failed. "
"The files have different tile descriptions.");
2012-08-24 22:31:49 +02:00
if (!(hdr.dataWindow() == inHdr.dataWindow()))
THROW (Iex::ArgExc, "Cannot copy pixels from image "
2012-10-17 09:12:04 +02:00
"file \"" << in.fileName() << "\" to image "
"file \"" << fileName() << "\". The "
2012-08-24 22:31:49 +02:00
"files have different data windows.");
if (!(hdr.lineOrder() == inHdr.lineOrder()))
THROW (Iex::ArgExc, "Quick pixel copy from image "
2012-10-17 09:12:04 +02:00
"file \"" << in.fileName() << "\" to image "
"file \"" << fileName() << "\" failed. "
"The files have different line orders.");
2012-08-24 22:31:49 +02:00
if (!(hdr.compression() == inHdr.compression()))
THROW (Iex::ArgExc, "Quick pixel copy from image "
2012-10-17 09:12:04 +02:00
"file \"" << in.fileName() << "\" to image "
"file \"" << fileName() << "\" failed. "
"The files use different compression methods.");
2012-08-24 22:31:49 +02:00
if (!(hdr.channels() == inHdr.channels()))
THROW (Iex::ArgExc, "Quick pixel copy from image "
2012-10-17 09:12:04 +02:00
"file \"" << in.fileName() << "\" to image "
"file \"" << fileName() << "\" "
2012-08-24 22:31:49 +02:00
"failed. The files have different channel "
"lists.");
//
// Verify that no pixel data have been written to this file yet.
//
if (!_data->tileOffsets.isEmpty())
THROW (Iex::LogicExc, "Quick pixel copy from image "
2012-10-17 09:12:04 +02:00
"file \"" << in.fileName() << "\" to image "
"file \"" << _data->os->fileName() << "\" "
2012-08-24 22:31:49 +02:00
"failed. \"" << fileName() << "\" "
"already contains pixel data.");
//
// Calculate the total number of tiles in the file
//
int numAllTiles = 0;
switch (levelMode ())
{
case ONE_LEVEL:
case MIPMAP_LEVELS:
for (size_t i_l = 0; i_l < numLevels (); ++i_l)
numAllTiles += numXTiles (i_l) * numYTiles (i_l);
break;
case RIPMAP_LEVELS:
for (size_t i_ly = 0; i_ly < numYLevels (); ++i_ly)
for (size_t i_lx = 0; i_lx < numXLevels (); ++i_lx)
numAllTiles += numXTiles (i_lx) * numYTiles (i_ly);
break;
default:
throw Iex::ArgExc ("Unknown LevelMode format.");
}
for (int i = 0; i < numAllTiles; ++i)
{
const char *pixelData;
int pixelDataSize;
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
int dx = _data->nextTileToWrite.dx;
int dy = _data->nextTileToWrite.dy;
int lx = _data->nextTileToWrite.lx;
int ly = _data->nextTileToWrite.ly;
in.rawTileData (dx, dy, lx, ly, pixelData, pixelDataSize);
writeTileData (_data, dx, dy, lx, ly, pixelData, pixelDataSize);
}
}
2012-10-17 09:12:04 +02:00
void
2012-08-24 22:31:49 +02:00
TiledOutputFile::copyPixels (InputFile &in)
{
copyPixels (*in.tFile());
}
unsigned int
TiledOutputFile::tileXSize () const
{
return _data->tileDesc.xSize;
}
unsigned int
TiledOutputFile::tileYSize () const
{
return _data->tileDesc.ySize;
}
LevelMode
TiledOutputFile::levelMode () const
{
return _data->tileDesc.mode;
}
LevelRoundingMode
TiledOutputFile::levelRoundingMode () const
{
return _data->tileDesc.roundingMode;
}
int
TiledOutputFile::numLevels () const
{
if (levelMode() == RIPMAP_LEVELS)
2012-10-17 09:12:04 +02:00
THROW (Iex::LogicExc, "Error calling numLevels() on image "
"file \"" << fileName() << "\" "
"(numLevels() is not defined for RIPMAPs).");
2012-08-24 22:31:49 +02:00
return _data->numXLevels;
}
int
TiledOutputFile::numXLevels () const
{
return _data->numXLevels;
}
int
TiledOutputFile::numYLevels () const
{
return _data->numYLevels;
}
2012-10-17 09:12:04 +02:00
bool
2012-08-24 22:31:49 +02:00
TiledOutputFile::isValidLevel (int lx, int ly) const
{
if (lx < 0 || ly < 0)
2012-10-17 09:12:04 +02:00
return false;
2012-08-24 22:31:49 +02:00
if (levelMode() == MIPMAP_LEVELS && lx != ly)
2012-10-17 09:12:04 +02:00
return false;
2012-08-24 22:31:49 +02:00
if (lx >= numXLevels() || ly >= numYLevels())
2012-10-17 09:12:04 +02:00
return false;
2012-08-24 22:31:49 +02:00
return true;
}
int
TiledOutputFile::levelWidth (int lx) const
{
try
{
2012-10-17 09:12:04 +02:00
int retVal = levelSize (_data->minX, _data->maxX, lx,
_data->tileDesc.roundingMode);
2012-08-24 22:31:49 +02:00
return retVal;
}
catch (Iex::BaseExc &e)
{
2012-10-17 09:12:04 +02:00
REPLACE_EXC (e, "Error calling levelWidth() on image "
"file \"" << fileName() << "\". " << e);
throw;
2012-08-24 22:31:49 +02:00
}
}
int
TiledOutputFile::levelHeight (int ly) const
{
try
{
2012-10-17 09:12:04 +02:00
return levelSize (_data->minY, _data->maxY, ly,
_data->tileDesc.roundingMode);
2012-08-24 22:31:49 +02:00
}
catch (Iex::BaseExc &e)
{
2012-10-17 09:12:04 +02:00
REPLACE_EXC (e, "Error calling levelHeight() on image "
"file \"" << fileName() << "\". " << e);
throw;
2012-08-24 22:31:49 +02:00
}
}
int
TiledOutputFile::numXTiles (int lx) const
{
if (lx < 0 || lx >= _data->numXLevels)
2012-10-17 09:12:04 +02:00
THROW (Iex::LogicExc, "Error calling numXTiles() on image "
"file \"" << _data->os->fileName() << "\" "
"(Argument is not in valid range).");
2012-08-24 22:31:49 +02:00
return _data->numXTiles[lx];
}
int
TiledOutputFile::numYTiles (int ly) const
{
if (ly < 0 || ly >= _data->numYLevels)
2012-10-17 09:12:04 +02:00
THROW (Iex::LogicExc, "Error calling numXTiles() on image "
"file \"" << _data->os->fileName() << "\" "
"(Argument is not in valid range).");
2012-08-24 22:31:49 +02:00
return _data->numYTiles[ly];
}
Box2i
TiledOutputFile::dataWindowForLevel (int l) const
{
return dataWindowForLevel (l, l);
}
Box2i
TiledOutputFile::dataWindowForLevel (int lx, int ly) const
{
try
{
2012-10-17 09:12:04 +02:00
return Imf::dataWindowForLevel (_data->tileDesc,
_data->minX, _data->maxX,
_data->minY, _data->maxY,
lx, ly);
2012-08-24 22:31:49 +02:00
}
catch (Iex::BaseExc &e)
{
2012-10-17 09:12:04 +02:00
REPLACE_EXC (e, "Error calling dataWindowForLevel() on image "
"file \"" << fileName() << "\". " << e);
throw;
2012-08-24 22:31:49 +02:00
}
}
Box2i
TiledOutputFile::dataWindowForTile (int dx, int dy, int l) const
{
return dataWindowForTile (dx, dy, l, l);
}
Box2i
TiledOutputFile::dataWindowForTile (int dx, int dy, int lx, int ly) const
{
try
{
2012-10-17 09:12:04 +02:00
if (!isValidTile (dx, dy, lx, ly))
throw Iex::ArgExc ("Arguments not in valid range.");
return Imf::dataWindowForTile (_data->tileDesc,
_data->minX, _data->maxX,
_data->minY, _data->maxY,
dx, dy,
lx, ly);
2012-08-24 22:31:49 +02:00
}
catch (Iex::BaseExc &e)
{
2012-10-17 09:12:04 +02:00
REPLACE_EXC (e, "Error calling dataWindowForTile() on image "
"file \"" << fileName() << "\". " << e);
throw;
2012-08-24 22:31:49 +02:00
}
}
bool
TiledOutputFile::isValidTile (int dx, int dy, int lx, int ly) const
{
return ((lx < _data->numXLevels && lx >= 0) &&
2012-10-17 09:12:04 +02:00
(ly < _data->numYLevels && ly >= 0) &&
(dx < _data->numXTiles[lx] && dx >= 0) &&
(dy < _data->numYTiles[ly] && dy >= 0));
2012-08-24 22:31:49 +02:00
}
void
TiledOutputFile::updatePreviewImage (const PreviewRgba newPixels[])
{
Lock lock (*_data);
if (_data->previewPosition <= 0)
2012-10-17 09:12:04 +02:00
THROW (Iex::LogicExc, "Cannot update preview image pixels. "
"File \"" << fileName() << "\" does not "
"contain a preview image.");
2012-08-24 22:31:49 +02:00
//
// Store the new pixels in the header's preview image attribute.
//
PreviewImageAttribute &pia =
2012-10-17 09:12:04 +02:00
_data->header.typedAttribute <PreviewImageAttribute> ("preview");
2012-08-24 22:31:49 +02: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 09:12:04 +02:00
pixels[i] = newPixels[i];
2012-08-24 22:31:49 +02: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 09:12:04 +02:00
_data->os->seekp (_data->previewPosition);
pia.writeValueTo (*_data->os, _data->version);
_data->os->seekp (savedPosition);
2012-08-24 22:31:49 +02:00
}
catch (Iex::BaseExc &e)
{
2012-10-17 09:12:04 +02:00
REPLACE_EXC (e, "Cannot update preview image pixels for "
"file \"" << fileName() << "\". " << e);
throw;
2012-08-24 22:31:49 +02:00
}
}
void
2012-10-17 09:12:04 +02:00
TiledOutputFile::breakTile
2012-08-24 22:31:49 +02:00
(int dx, int dy,
int lx, int ly,
int offset,
int length,
char c)
{
Lock lock (*_data);
Int64 position = _data->tileOffsets (dx, dy, lx, ly);
if (!position)
2012-10-17 09:12:04 +02:00
THROW (Iex::ArgExc,
"Cannot overwrite tile "
"(" << dx << ", " << dy << ", " << lx << "," << ly << "). "
"The tile has not yet been stored in "
"file \"" << fileName() << "\".");
2012-08-24 22:31:49 +02:00
_data->currentPosition = 0;
_data->os->seekp (position + offset);
for (int i = 0; i < length; ++i)
2012-10-17 09:12:04 +02:00
_data->os->write (&c, 1);
2012-08-24 22:31:49 +02:00
}
} // namespace Imf