1
///////////////////////////////////////////////////////////////////////////
3
// Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
6
// All rights reserved.
8
// Redistribution and use in source and binary forms, with or without
9
// modification, are permitted provided that the following conditions are
11
// * Redistributions of source code must retain the above copyright
12
// notice, this list of conditions and the following disclaimer.
13
// * Redistributions in binary form must reproduce the above
14
// copyright notice, this list of conditions and the following disclaimer
15
// in the documentation and/or other materials provided with the
17
// * Neither the name of Industrial Light & Magic nor the names of
18
// its contributors may be used to endorse or promote products derived
19
// from this software without specific prior written permission.
21
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
///////////////////////////////////////////////////////////////////////////
36
//-----------------------------------------------------------------------------
40
//-----------------------------------------------------------------------------
42
#include <ImfOutputFile.h>
43
#include <ImfInputFile.h>
44
#include <ImfChannelList.h>
47
#include <ImfCompressor.h>
52
#include <ImfPreviewImageAttribute.h>
53
#include "IlmThreadPool.h"
54
#include "IlmThreadSemaphore.h"
55
#include "IlmThreadMutex.h"
61
#include <algorithm> // for std::max()
74
using IlmThread::Mutex;
75
using IlmThread::Lock;
76
using IlmThread::Semaphore;
77
using IlmThread::Task;
78
using IlmThread::TaskGroup;
79
using IlmThread::ThreadPool;
94
OutSliceInfo (PixelType type = HALF,
104
OutSliceInfo::OutSliceInfo (PixelType t,
106
size_t xs, size_t ys,
125
const char * dataPtr;
127
char * endOfLineBufferData;
132
Compressor * compressor;
133
bool partiallyFull; // has incomplete data
137
LineBuffer (Compressor *comp);
140
void wait () {_sem.wait();}
141
void post () {_sem.post();}
149
LineBuffer::LineBuffer (Compressor *comp) :
153
partiallyFull (false),
154
hasException (false),
162
LineBuffer::~LineBuffer ()
170
struct OutputFile::Data: public Mutex
172
Header header; // the image header
173
int version; // file format version
174
Int64 previewPosition; // file position for preview
175
FrameBuffer frameBuffer; // framebuffer to write into
176
int currentScanLine; // next scanline to be written
177
int missingScanLines; // number of lines to write
178
LineOrder lineOrder; // the file's lineorder
179
int minX; // data window's min x coord
180
int maxX; // data window's max x coord
181
int minY; // data window's min y coord
182
int maxY; // data window's max x coord
183
vector<Int64> lineOffsets; // stores offsets in file for
185
vector<size_t> bytesPerLine; // combined size of a line over
187
vector<size_t> offsetInLineBuffer; // offset for each scanline in
189
Compressor::Format format; // compressor's data format
190
vector<OutSliceInfo> slices; // info about channels in file
191
OStream * os; // file stream to write to
193
Int64 lineOffsetsPosition; // file position for line
195
Int64 currentPosition; // current file position
197
vector<LineBuffer*> lineBuffers; // each holds one line buffer
198
int linesInBuffer; // number of scanlines each
200
size_t lineBufferSize; // size of the line buffer
202
Data (bool deleteStream, int numThreads);
206
inline LineBuffer * getLineBuffer (int number); // hash function from line
207
// buffer indices into our
208
// vector of line buffers
212
OutputFile::Data::Data (bool deleteStream, int numThreads):
214
deleteStream (deleteStream),
215
lineOffsetsPosition (0)
218
// We need at least one lineBuffer, but if threading is used,
219
// to keep n threads busy we need 2*n lineBuffers.
222
lineBuffers.resize (max (1, 2 * numThreads));
226
OutputFile::Data::~Data ()
231
for (size_t i = 0; i < lineBuffers.size(); i++)
232
delete lineBuffers[i];
237
OutputFile::Data::getLineBuffer (int number)
239
return lineBuffers[number % lineBuffers.size()];
247
writeLineOffsets (OStream &os, const vector<Int64> &lineOffsets)
249
Int64 pos = os.tellp();
252
Iex::throwErrnoExc ("Cannot determine current file position (%T).");
254
for (unsigned int i = 0; i < lineOffsets.size(); i++)
255
Xdr::write <StreamIO> (os, lineOffsets[i]);
262
writePixelData (OutputFile::Data *ofd,
264
const char pixelData[],
268
// Store a block of pixel data in the output file, and try
269
// to keep track of the current writing position the file
270
// without calling tellp() (tellp() can be fairly expensive).
273
Int64 currentPosition = ofd->currentPosition;
274
ofd->currentPosition = 0;
276
if (currentPosition == 0)
277
currentPosition = ofd->os->tellp();
279
ofd->lineOffsets[(ofd->currentScanLine - ofd->minY) / ofd->linesInBuffer] =
284
assert (ofd->os->tellp() == currentPosition);
288
Xdr::write <StreamIO> (*ofd->os, lineBufferMinY);
289
Xdr::write <StreamIO> (*ofd->os, pixelDataSize);
290
ofd->os->write (pixelData, pixelDataSize);
292
ofd->currentPosition = currentPosition +
300
writePixelData (OutputFile::Data *ofd, const LineBuffer *lineBuffer)
305
lineBuffer->dataSize);
310
convertToXdr (OutputFile::Data *ofd,
311
Array<char> &lineBuffer,
317
// Convert the contents of a lineBuffer from the machine's native
318
// representation to Xdr format. This function is called by
319
// CompressLineBuffer::execute(), below, if the compressor wanted
320
// its input pixel data in the machine's native format, but then
321
// failed to compress the data (most compressors will expand rather
322
// than compress random input data).
324
// Note that this routine assumes that the machine's native
325
// representation of the pixel data has the same size as the
326
// Xdr representation. This makes it possible to convert the
327
// pixel data in place, without an intermediate temporary buffer.
330
int startY, endY; // The first and last scanlines in
331
// the file that are in the lineBuffer.
334
if (ofd->lineOrder == INCREASING_Y)
336
startY = max (lineBufferMinY, ofd->minY);
337
endY = min (lineBufferMaxY, ofd->maxY) + 1;
342
startY = min (lineBufferMaxY, ofd->maxY);
343
endY = max (lineBufferMinY, ofd->minY) - 1;
348
// Iterate over all scanlines in the lineBuffer to convert.
351
for (int y = startY; y != endY; y += step)
354
// Set these to point to the start of line y.
355
// We will write to writePtr from readPtr.
358
char *writePtr = lineBuffer + ofd->offsetInLineBuffer[y - ofd->minY];
359
const char *readPtr = writePtr;
362
// Iterate over all slices in the file.
365
for (unsigned int i = 0; i < ofd->slices.size(); ++i)
368
// Test if scan line y of this channel is
369
// contains any data (the scan line contains
370
// data only if y % ySampling == 0).
373
const OutSliceInfo &slice = ofd->slices[i];
375
if (modp (y, slice.ySampling) != 0)
379
// Find the number of sampled pixels, dMaxX-dMinX+1, for
380
// slice i in scan line y (i.e. pixels within the data window
381
// for which x % xSampling == 0).
384
int dMinX = divp (ofd->minX, slice.xSampling);
385
int dMaxX = divp (ofd->maxX, slice.xSampling);
388
// Convert the samples in place.
391
convertInPlace (writePtr, readPtr, slice.type, dMaxX - dMinX + 1);
398
// A LineBufferTask encapsulates the task of copying a set of scanlines
399
// from the user's frame buffer into a LineBuffer object, compressing
400
// the data if necessary.
403
class LineBufferTask: public Task
407
LineBufferTask (TaskGroup *group,
408
OutputFile::Data *ofd,
413
virtual ~LineBufferTask ();
415
virtual void execute ();
419
OutputFile::Data * _ofd;
420
LineBuffer * _lineBuffer;
424
LineBufferTask::LineBufferTask
426
OutputFile::Data *ofd,
433
_lineBuffer (_ofd->getLineBuffer(number))
436
// Wait for the lineBuffer to become available
439
_lineBuffer->wait ();
442
// Initialize the lineBuffer data if necessary
445
if (!_lineBuffer->partiallyFull)
447
_lineBuffer->endOfLineBufferData = _lineBuffer->buffer;
449
_lineBuffer->minY = _ofd->minY + number * _ofd->linesInBuffer;
451
_lineBuffer->maxY = min (_lineBuffer->minY + _ofd->linesInBuffer - 1,
454
_lineBuffer->partiallyFull = true;
457
_lineBuffer->scanLineMin = max (_lineBuffer->minY, scanLineMin);
458
_lineBuffer->scanLineMax = min (_lineBuffer->maxY, scanLineMax);
462
LineBufferTask::~LineBufferTask ()
465
// Signal that the line buffer is now free
468
_lineBuffer->post ();
473
LineBufferTask::execute ()
478
// First copy the pixel data from the
479
// frame buffer into the line buffer
482
int yStart, yStop, dy;
484
if (_ofd->lineOrder == INCREASING_Y)
486
yStart = _lineBuffer->scanLineMin;
487
yStop = _lineBuffer->scanLineMax + 1;
492
yStart = _lineBuffer->scanLineMax;
493
yStop = _lineBuffer->scanLineMin - 1;
499
for (y = yStart; y != yStop; y += dy)
502
// Gather one scan line's worth of pixel data and store
503
// them in _ofd->lineBuffer.
506
char *writePtr = _lineBuffer->buffer +
507
_ofd->offsetInLineBuffer[y - _ofd->minY];
509
// Iterate over all image channels.
512
for (unsigned int i = 0; i < _ofd->slices.size(); ++i)
515
// Test if scan line y of this channel contains any data
516
// (the scan line contains data only if y % ySampling == 0).
519
const OutSliceInfo &slice = _ofd->slices[i];
521
if (modp (y, slice.ySampling) != 0)
525
// Find the x coordinates of the leftmost and rightmost
526
// sampled pixels (i.e. pixels within the data window
527
// for which x % xSampling == 0).
530
int dMinX = divp (_ofd->minX, slice.xSampling);
531
int dMaxX = divp (_ofd->maxX, slice.xSampling);
534
// Fill the line buffer with with pixel data.
540
// The frame buffer contains no data for this channel.
541
// Store zeroes in _lineBuffer->buffer.
544
fillChannelWithZeroes (writePtr, _ofd->format, slice.type,
550
// If necessary, convert the pixel data to Xdr format.
551
// Then store the pixel data in _ofd->lineBuffer.
554
const char *linePtr = slice.base +
555
divp (y, slice.ySampling) *
558
const char *readPtr = linePtr + dMinX * slice.xStride;
559
const char *endPtr = linePtr + dMaxX * slice.xStride;
561
copyFromFrameBuffer (writePtr, readPtr, endPtr,
562
slice.xStride, _ofd->format,
567
if (_lineBuffer->endOfLineBufferData < writePtr)
568
_lineBuffer->endOfLineBufferData = writePtr;
572
assert (writePtr - (_lineBuffer->buffer +
573
_ofd->offsetInLineBuffer[y - _ofd->minY]) ==
574
(int) _ofd->bytesPerLine[y - _ofd->minY]);
581
// If the next scanline isn't past the bounds of the lineBuffer
582
// then we are done, otherwise compress the linebuffer
585
if (y >= _lineBuffer->minY && y <= _lineBuffer->maxY)
588
_lineBuffer->dataPtr = _lineBuffer->buffer;
590
_lineBuffer->dataSize = _lineBuffer->endOfLineBufferData -
597
Compressor *compressor = _lineBuffer->compressor;
603
int compSize = compressor->compress (_lineBuffer->dataPtr,
604
_lineBuffer->dataSize,
605
_lineBuffer->minY, compPtr);
607
if (compSize < _lineBuffer->dataSize)
609
_lineBuffer->dataSize = compSize;
610
_lineBuffer->dataPtr = compPtr;
612
else if (_ofd->format == Compressor::NATIVE)
615
// The data did not shrink during compression, but
616
// we cannot write to the file using the machine's
617
// native format, so we need to convert the lineBuffer
621
convertToXdr (_ofd, _lineBuffer->buffer, _lineBuffer->minY,
622
_lineBuffer->maxY, _lineBuffer->dataSize);
626
_lineBuffer->partiallyFull = false;
628
catch (std::exception &e)
630
if (!_lineBuffer->hasException)
632
_lineBuffer->exception = e.what ();
633
_lineBuffer->hasException = true;
638
if (!_lineBuffer->hasException)
640
_lineBuffer->exception = "unrecognized exception";
641
_lineBuffer->hasException = true;
649
OutputFile::OutputFile
650
(const char fileName[],
651
const Header &header,
654
_data (new Data (true, numThreads))
658
header.sanityCheck();
659
_data->os = new StdOFStream (fileName);
662
catch (Iex::BaseExc &e)
666
REPLACE_EXC (e, "Cannot open image file "
667
"\"" << fileName << "\". " << e);
678
OutputFile::OutputFile
680
const Header &header,
683
_data (new Data (false, numThreads))
687
header.sanityCheck();
691
catch (Iex::BaseExc &e)
695
REPLACE_EXC (e, "Cannot open image file "
696
"\"" << os.fileName() << "\". " << e);
708
OutputFile::initialize (const Header &header)
710
_data->header = header;
712
const Box2i &dataWindow = header.dataWindow();
714
_data->currentScanLine = (header.lineOrder() == INCREASING_Y)?
715
dataWindow.min.y: dataWindow.max.y;
717
_data->missingScanLines = dataWindow.max.y - dataWindow.min.y + 1;
718
_data->lineOrder = header.lineOrder();
719
_data->minX = dataWindow.min.x;
720
_data->maxX = dataWindow.max.x;
721
_data->minY = dataWindow.min.y;
722
_data->maxY = dataWindow.max.y;
724
size_t maxBytesPerLine = bytesPerLineTable (_data->header,
725
_data->bytesPerLine);
727
for (size_t i = 0; i < _data->lineBuffers.size(); ++i)
729
_data->lineBuffers[i] =
730
new LineBuffer (newCompressor (_data->header.compression(),
735
LineBuffer *lineBuffer = _data->lineBuffers[0];
736
_data->format = defaultFormat (lineBuffer->compressor);
737
_data->linesInBuffer = numLinesInBuffer (lineBuffer->compressor);
738
_data->lineBufferSize = maxBytesPerLine * _data->linesInBuffer;
740
for (size_t i = 0; i < _data->lineBuffers.size(); i++)
741
_data->lineBuffers[i]->buffer.resizeErase(_data->lineBufferSize);
743
int lineOffsetSize = (dataWindow.max.y - dataWindow.min.y +
744
_data->linesInBuffer) / _data->linesInBuffer;
746
_data->lineOffsets.resize (lineOffsetSize);
748
offsetInLineBufferTable (_data->bytesPerLine,
749
_data->linesInBuffer,
750
_data->offsetInLineBuffer);
752
_data->previewPosition =
753
_data->header.writeTo (*_data->os);
755
_data->lineOffsetsPosition =
756
writeLineOffsets (*_data->os, _data->lineOffsets);
758
_data->currentPosition = _data->os->tellp();
762
OutputFile::~OutputFile ()
767
if (_data->lineOffsetsPosition > 0)
771
_data->os->seekp (_data->lineOffsetsPosition);
772
writeLineOffsets (*_data->os, _data->lineOffsets);
777
// We cannot safely throw any exceptions from here.
778
// This destructor may have been called because the
779
// stack is currently being unwound for another
792
OutputFile::fileName () const
794
return _data->os->fileName();
799
OutputFile::header () const
801
return _data->header;
806
OutputFile::setFrameBuffer (const FrameBuffer &frameBuffer)
811
// Check if the new frame buffer descriptor
812
// is compatible with the image file header.
815
const ChannelList &channels = _data->header.channels();
817
for (ChannelList::ConstIterator i = channels.begin();
821
FrameBuffer::ConstIterator j = frameBuffer.find (i.name());
823
if (j == frameBuffer.end())
826
if (i.channel().type != j.slice().type)
828
THROW (Iex::ArgExc, "Pixel type of \"" << i.name() << "\" channel "
829
"of output file \"" << fileName() << "\" is "
830
"not compatible with the frame buffer's "
834
if (i.channel().xSampling != j.slice().xSampling ||
835
i.channel().ySampling != j.slice().ySampling)
837
THROW (Iex::ArgExc, "X and/or y subsampling factors "
838
"of \"" << i.name() << "\" channel "
839
"of output file \"" << fileName() << "\" are "
840
"not compatible with the frame buffer's "
841
"subsampling factors.");
846
// Initialize slice table for writePixels().
849
vector<OutSliceInfo> slices;
851
for (ChannelList::ConstIterator i = channels.begin();
855
FrameBuffer::ConstIterator j = frameBuffer.find (i.name());
857
if (j == frameBuffer.end())
860
// Channel i is not present in the frame buffer.
861
// In the file, channel i will contain only zeroes.
864
slices.push_back (OutSliceInfo (i.channel().type,
868
i.channel().xSampling,
869
i.channel().ySampling,
875
// Channel i is present in the frame buffer.
878
slices.push_back (OutSliceInfo (j.slice().type,
889
// Store the new frame buffer.
892
_data->frameBuffer = frameBuffer;
893
_data->slices = slices;
898
OutputFile::frameBuffer () const
901
return _data->frameBuffer;
906
OutputFile::writePixels (int numScanLines)
912
if (_data->slices.size() == 0)
913
throw Iex::ArgExc ("No frame buffer specified "
914
"as pixel data source.");
917
// Maintain two iterators:
918
// nextWriteBuffer: next linebuffer to be written to the file
919
// nextCompressBuffer: next linebuffer to compress
922
int first = (_data->currentScanLine - _data->minY) /
923
_data->linesInBuffer;
925
int nextWriteBuffer = first;
926
int nextCompressBuffer;
934
// Create a task group for all line buffer tasks. When the
935
// taskgroup goes out of scope, the destructor waits until
936
// all tasks are complete.
942
// Determine the range of lineBuffers that intersect the scan
943
// line range. Then add the initial compression tasks to the
944
// thread pool. We always add in at least one task but the
945
// individual task might not do anything if numScanLines == 0.
948
if (_data->lineOrder == INCREASING_Y)
950
int last = (_data->currentScanLine + (numScanLines - 1) -
951
_data->minY) / _data->linesInBuffer;
953
scanLineMin = _data->currentScanLine;
954
scanLineMax = _data->currentScanLine + numScanLines - 1;
956
int numTasks = max (min ((int)_data->lineBuffers.size(),
960
for (int i = 0; i < numTasks; i++)
962
ThreadPool::addGlobalTask
963
(new LineBufferTask (&taskGroup, _data, first + i,
964
scanLineMin, scanLineMax));
967
nextCompressBuffer = first + numTasks;
973
int last = (_data->currentScanLine - (numScanLines - 1) -
974
_data->minY) / _data->linesInBuffer;
976
scanLineMax = _data->currentScanLine;
977
scanLineMin = _data->currentScanLine - numScanLines + 1;
979
int numTasks = max (min ((int)_data->lineBuffers.size(),
983
for (int i = 0; i < numTasks; i++)
985
ThreadPool::addGlobalTask
986
(new LineBufferTask (&taskGroup, _data, first - i,
987
scanLineMin, scanLineMax));
990
nextCompressBuffer = first - numTasks;
997
if (_data->missingScanLines <= 0)
999
throw Iex::ArgExc ("Tried to write more scan lines "
1000
"than specified by the data window.");
1004
// Wait until the next line buffer is ready to be written
1007
LineBuffer *writeBuffer =
1008
_data->getLineBuffer (nextWriteBuffer);
1010
writeBuffer->wait();
1012
int numLines = writeBuffer->scanLineMax -
1013
writeBuffer->scanLineMin + 1;
1015
_data->missingScanLines -= numLines;
1018
// If the line buffer is only partially full, then it is
1019
// not complete and we cannot write it to disk yet.
1022
if (writeBuffer->partiallyFull)
1024
_data->currentScanLine = _data->currentScanLine +
1026
writeBuffer->post();
1032
// Write the line buffer
1035
writePixelData (_data, writeBuffer);
1036
nextWriteBuffer += step;
1038
_data->currentScanLine = _data->currentScanLine +
1043
assert (_data->currentScanLine ==
1044
((_data->lineOrder == INCREASING_Y) ?
1045
writeBuffer->scanLineMax + 1:
1046
writeBuffer->scanLineMin - 1));
1051
// Release the lock on the line buffer
1054
writeBuffer->post();
1057
// If this was the last line buffer in the scanline range
1060
if (nextWriteBuffer == stop)
1064
// If there are no more line buffers to compress,
1065
// then only continue to write out remaining lineBuffers
1068
if (nextCompressBuffer == stop)
1072
// Add nextCompressBuffer as a compression task
1075
ThreadPool::addGlobalTask
1076
(new LineBufferTask (&taskGroup, _data, nextCompressBuffer,
1077
scanLineMin, scanLineMax));
1080
// Update the next line buffer we need to compress
1083
nextCompressBuffer += step;
1092
// Exeption handling:
1094
// LineBufferTask::execute() may have encountered exceptions, but
1095
// those exceptions occurred in another thread, not in the thread
1096
// that is executing this call to OutputFile::writePixels().
1097
// LineBufferTask::execute() has caught all exceptions and stored
1098
// the exceptions' what() strings in the line buffers.
1099
// Now we check if any line buffer contains a stored exception; if
1100
// this is the case then we re-throw the exception in this thread.
1101
// (It is possible that multiple line buffers contain stored
1102
// exceptions. We re-throw the first exception we find and
1103
// ignore all others.)
1106
const string *exception = 0;
1108
for (int i = 0; i < _data->lineBuffers.size(); ++i)
1110
LineBuffer *lineBuffer = _data->lineBuffers[i];
1112
if (lineBuffer->hasException && !exception)
1113
exception = &lineBuffer->exception;
1115
lineBuffer->hasException = false;
1119
throw Iex::IoExc (*exception);
1121
catch (Iex::BaseExc &e)
1123
REPLACE_EXC (e, "Failed to write pixel data to image "
1124
"file \"" << fileName() << "\". " << e);
1131
OutputFile::currentScanLine () const
1134
return _data->currentScanLine;
1139
OutputFile::copyPixels (InputFile &in)
1144
// Check if this file's and and the InputFile's
1145
// headers are compatible.
1148
const Header &hdr = _data->header;
1149
const Header &inHdr = in.header();
1151
if (inHdr.find("tiles") != inHdr.end())
1152
THROW (Iex::ArgExc, "Cannot copy pixels from image "
1153
"file \"" << in.fileName() << "\" to image "
1154
"file \"" << fileName() << "\". "
1155
"The input file is tiled, but the output file is "
1156
"not. Try using TiledOutputFile::copyPixels "
1159
if (!(hdr.dataWindow() == inHdr.dataWindow()))
1160
THROW (Iex::ArgExc, "Cannot copy pixels from image "
1161
"file \"" << in.fileName() << "\" to image "
1162
"file \"" << fileName() << "\". "
1163
"The files have different data windows.");
1165
if (!(hdr.lineOrder() == inHdr.lineOrder()))
1166
THROW (Iex::ArgExc, "Quick pixel copy from image "
1167
"file \"" << in.fileName() << "\" to image "
1168
"file \"" << fileName() << "\" failed. "
1169
"The files have different line orders.");
1171
if (!(hdr.compression() == inHdr.compression()))
1172
THROW (Iex::ArgExc, "Quick pixel copy from image "
1173
"file \"" << in.fileName() << "\" to image "
1174
"file \"" << fileName() << "\" failed. "
1175
"The files use different compression methods.");
1177
if (!(hdr.channels() == inHdr.channels()))
1178
THROW (Iex::ArgExc, "Quick pixel copy from image "
1179
"file \"" << in.fileName() << "\" to image "
1180
"file \"" << fileName() << "\" failed. "
1181
"The files have different channel lists.");
1184
// Verify that no pixel data have been written to this file yet.
1187
const Box2i &dataWindow = hdr.dataWindow();
1189
if (_data->missingScanLines != dataWindow.max.y - dataWindow.min.y + 1)
1190
THROW (Iex::LogicExc, "Quick pixel copy from image "
1191
"file \"" << in.fileName() << "\" to image "
1192
"file \"" << fileName() << "\" failed. "
1193
"\"" << fileName() << "\" already contains "
1197
// Copy the pixel data.
1200
while (_data->missingScanLines > 0)
1202
const char *pixelData;
1205
in.rawPixelData (_data->currentScanLine, pixelData, pixelDataSize);
1207
writePixelData (_data, lineBufferMinY (_data->currentScanLine,
1209
_data->linesInBuffer),
1210
pixelData, pixelDataSize);
1212
_data->currentScanLine += (_data->lineOrder == INCREASING_Y)?
1213
_data->linesInBuffer: -_data->linesInBuffer;
1215
_data->missingScanLines -= _data->linesInBuffer;
1221
OutputFile::updatePreviewImage (const PreviewRgba newPixels[])
1225
if (_data->previewPosition <= 0)
1226
THROW (Iex::LogicExc, "Cannot update preview image pixels. "
1227
"File \"" << fileName() << "\" does not "
1228
"contain a preview image.");
1231
// Store the new pixels in the header's preview image attribute.
1234
PreviewImageAttribute &pia =
1235
_data->header.typedAttribute <PreviewImageAttribute> ("preview");
1237
PreviewImage &pi = pia.value();
1238
PreviewRgba *pixels = pi.pixels();
1239
int numPixels = pi.width() * pi.height();
1241
for (int i = 0; i < numPixels; ++i)
1242
pixels[i] = newPixels[i];
1245
// Save the current file position, jump to the position in
1246
// the file where the preview image starts, store the new
1247
// preview image, and jump back to the saved file position.
1250
Int64 savedPosition = _data->os->tellp();
1254
_data->os->seekp (_data->previewPosition);
1255
pia.writeValueTo (*_data->os, _data->version);
1256
_data->os->seekp (savedPosition);
1258
catch (Iex::BaseExc &e)
1260
REPLACE_EXC (e, "Cannot update preview image pixels for "
1261
"file \"" << fileName() << "\". " << e);
1268
OutputFile::breakScanLine (int y, int offset, int length, char c)
1273
_data->lineOffsets[(y - _data->minY) / _data->linesInBuffer];
1276
THROW (Iex::ArgExc, "Cannot overwrite scan line " << y << ". "
1277
"The scan line has not yet been stored in "
1278
"file \"" << fileName() << "\".");
1280
_data->currentPosition = 0;
1281
_data->os->seekp (position + offset);
1283
for (int i = 0; i < length; ++i)
1284
_data->os->write (&c, 1);