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"
73
using IlmThread::Mutex;
74
using IlmThread::Lock;
75
using IlmThread::Semaphore;
76
using IlmThread::Task;
77
using IlmThread::TaskGroup;
78
using IlmThread::ThreadPool;
93
OutSliceInfo (PixelType type = HALF,
103
OutSliceInfo::OutSliceInfo (PixelType t,
105
size_t xs, size_t ys,
124
const char * dataPtr;
126
char * endOfLineBufferData;
131
Compressor * compressor;
132
bool partiallyFull; // has incomplete data
136
LineBuffer (Compressor *comp);
139
void wait () {_sem.wait();}
140
void post () {_sem.post();}
148
LineBuffer::LineBuffer (Compressor *comp) :
152
partiallyFull (false),
153
hasException (false),
161
LineBuffer::~LineBuffer ()
169
struct OutputFile::Data: public Mutex
171
Header header; // the image header
172
int version; // file format version
173
Int64 previewPosition; // file position for preview
174
FrameBuffer frameBuffer; // framebuffer to write into
175
int currentScanLine; // next scanline to be written
176
int missingScanLines; // number of lines to write
177
LineOrder lineOrder; // the file's lineorder
178
int minX; // data window's min x coord
179
int maxX; // data window's max x coord
180
int minY; // data window's min y coord
181
int maxY; // data window's max x coord
182
vector<Int64> lineOffsets; // stores offsets in file for
184
vector<size_t> bytesPerLine; // combined size of a line over
186
vector<size_t> offsetInLineBuffer; // offset for each scanline in
188
Compressor::Format format; // compressor's data format
189
vector<OutSliceInfo> slices; // info about channels in file
190
OStream * os; // file stream to write to
192
Int64 lineOffsetsPosition; // file position for line
194
Int64 currentPosition; // current file position
196
vector<LineBuffer*> lineBuffers; // each holds one line buffer
197
int linesInBuffer; // number of scanlines each
199
size_t lineBufferSize; // size of the line buffer
201
Data (bool deleteStream, int numThreads);
205
inline LineBuffer * getLineBuffer (int number); // hash function from line
206
// buffer indices into our
207
// vector of line buffers
211
OutputFile::Data::Data (bool deleteStream, int numThreads):
213
deleteStream (deleteStream),
214
lineOffsetsPosition (0)
217
// We need at least one lineBuffer, but if threading is used,
218
// to keep n threads busy we need 2*n lineBuffers.
221
lineBuffers.resize (max (1, 2 * numThreads));
225
OutputFile::Data::~Data ()
230
for (size_t i = 0; i < lineBuffers.size(); i++)
231
delete lineBuffers[i];
236
OutputFile::Data::getLineBuffer (int number)
238
return lineBuffers[number % lineBuffers.size()];
246
writeLineOffsets (OStream &os, const vector<Int64> &lineOffsets)
248
Int64 pos = os.tellp();
251
Iex::throwErrnoExc ("Cannot determine current file position (%T).");
253
for (unsigned int i = 0; i < lineOffsets.size(); i++)
254
Xdr::write <StreamIO> (os, lineOffsets[i]);
261
writePixelData (OutputFile::Data *ofd,
263
const char pixelData[],
267
// Store a block of pixel data in the output file, and try
268
// to keep track of the current writing position the file
269
// without calling tellp() (tellp() can be fairly expensive).
272
Int64 currentPosition = ofd->currentPosition;
273
ofd->currentPosition = 0;
275
if (currentPosition == 0)
276
currentPosition = ofd->os->tellp();
278
ofd->lineOffsets[(ofd->currentScanLine - ofd->minY) / ofd->linesInBuffer] =
283
assert (ofd->os->tellp() == currentPosition);
287
Xdr::write <StreamIO> (*ofd->os, lineBufferMinY);
288
Xdr::write <StreamIO> (*ofd->os, pixelDataSize);
289
ofd->os->write (pixelData, pixelDataSize);
291
ofd->currentPosition = currentPosition +
299
writePixelData (OutputFile::Data *ofd, const LineBuffer *lineBuffer)
304
lineBuffer->dataSize);
309
convertToXdr (OutputFile::Data *ofd,
310
Array<char> &lineBuffer,
316
// Convert the contents of a lineBuffer from the machine's native
317
// representation to Xdr format. This function is called by
318
// CompressLineBuffer::execute(), below, if the compressor wanted
319
// its input pixel data in the machine's native format, but then
320
// failed to compress the data (most compressors will expand rather
321
// than compress random input data).
323
// Note that this routine assumes that the machine's native
324
// representation of the pixel data has the same size as the
325
// Xdr representation. This makes it possible to convert the
326
// pixel data in place, without an intermediate temporary buffer.
329
int startY, endY; // The first and last scanlines in
330
// the file that are in the lineBuffer.
333
if (ofd->lineOrder == INCREASING_Y)
335
startY = max (lineBufferMinY, ofd->minY);
336
endY = min (lineBufferMaxY, ofd->maxY) + 1;
341
startY = min (lineBufferMaxY, ofd->maxY);
342
endY = max (lineBufferMinY, ofd->minY) - 1;
347
// Iterate over all scanlines in the lineBuffer to convert.
350
for (int y = startY; y != endY; y += step)
353
// Set these to point to the start of line y.
354
// We will write to writePtr from readPtr.
357
char *writePtr = lineBuffer + ofd->offsetInLineBuffer[y - ofd->minY];
358
const char *readPtr = writePtr;
361
// Iterate over all slices in the file.
364
for (unsigned int i = 0; i < ofd->slices.size(); ++i)
367
// Test if scan line y of this channel is
368
// contains any data (the scan line contains
369
// data only if y % ySampling == 0).
372
const OutSliceInfo &slice = ofd->slices[i];
374
if (modp (y, slice.ySampling) != 0)
378
// Find the number of sampled pixels, dMaxX-dMinX+1, for
379
// slice i in scan line y (i.e. pixels within the data window
380
// for which x % xSampling == 0).
383
int dMinX = divp (ofd->minX, slice.xSampling);
384
int dMaxX = divp (ofd->maxX, slice.xSampling);
387
// Convert the samples in place.
390
convertInPlace (writePtr, readPtr, slice.type, dMaxX - dMinX + 1);
397
// A LineBufferTask encapsulates the task of copying a set of scanlines
398
// from the user's frame buffer into a LineBuffer object, compressing
399
// the data if necessary.
402
class LineBufferTask: public Task
406
LineBufferTask (TaskGroup *group,
407
OutputFile::Data *ofd,
412
virtual ~LineBufferTask ();
414
virtual void execute ();
418
OutputFile::Data * _ofd;
419
LineBuffer * _lineBuffer;
423
LineBufferTask::LineBufferTask
425
OutputFile::Data *ofd,
432
_lineBuffer (_ofd->getLineBuffer(number))
435
// Wait for the lineBuffer to become available
438
_lineBuffer->wait ();
441
// Initialize the lineBuffer data if necessary
444
if (!_lineBuffer->partiallyFull)
446
_lineBuffer->endOfLineBufferData = _lineBuffer->buffer;
448
_lineBuffer->minY = _ofd->minY + number * _ofd->linesInBuffer;
450
_lineBuffer->maxY = min (_lineBuffer->minY + _ofd->linesInBuffer - 1,
453
_lineBuffer->partiallyFull = true;
456
_lineBuffer->scanLineMin = max (_lineBuffer->minY, scanLineMin);
457
_lineBuffer->scanLineMax = min (_lineBuffer->maxY, scanLineMax);
461
LineBufferTask::~LineBufferTask ()
464
// Signal that the line buffer is now free
467
_lineBuffer->post ();
472
LineBufferTask::execute ()
477
// First copy the pixel data from the
478
// frame buffer into the line buffer
481
int yStart, yStop, dy;
483
if (_ofd->lineOrder == INCREASING_Y)
485
yStart = _lineBuffer->scanLineMin;
486
yStop = _lineBuffer->scanLineMax + 1;
491
yStart = _lineBuffer->scanLineMax;
492
yStop = _lineBuffer->scanLineMin - 1;
498
for (y = yStart; y != yStop; y += dy)
501
// Gather one scan line's worth of pixel data and store
502
// them in _ofd->lineBuffer.
505
char *writePtr = _lineBuffer->buffer +
506
_ofd->offsetInLineBuffer[y - _ofd->minY];
508
// Iterate over all image channels.
511
for (unsigned int i = 0; i < _ofd->slices.size(); ++i)
514
// Test if scan line y of this channel contains any data
515
// (the scan line contains data only if y % ySampling == 0).
518
const OutSliceInfo &slice = _ofd->slices[i];
520
if (modp (y, slice.ySampling) != 0)
524
// Find the x coordinates of the leftmost and rightmost
525
// sampled pixels (i.e. pixels within the data window
526
// for which x % xSampling == 0).
529
int dMinX = divp (_ofd->minX, slice.xSampling);
530
int dMaxX = divp (_ofd->maxX, slice.xSampling);
533
// Fill the line buffer with with pixel data.
539
// The frame buffer contains no data for this channel.
540
// Store zeroes in _lineBuffer->buffer.
543
fillChannelWithZeroes (writePtr, _ofd->format, slice.type,
549
// If necessary, convert the pixel data to Xdr format.
550
// Then store the pixel data in _ofd->lineBuffer.
553
const char *linePtr = slice.base +
554
divp (y, slice.ySampling) *
557
const char *readPtr = linePtr + dMinX * slice.xStride;
558
const char *endPtr = linePtr + dMaxX * slice.xStride;
560
copyFromFrameBuffer (writePtr, readPtr, endPtr,
561
slice.xStride, _ofd->format,
566
if (_lineBuffer->endOfLineBufferData < writePtr)
567
_lineBuffer->endOfLineBufferData = writePtr;
571
assert (writePtr - (_lineBuffer->buffer +
572
_ofd->offsetInLineBuffer[y - _ofd->minY]) ==
573
(int) _ofd->bytesPerLine[y - _ofd->minY]);
580
// If the next scanline isn't past the bounds of the lineBuffer
581
// then we are done, otherwise compress the linebuffer
584
if (y >= _lineBuffer->minY && y <= _lineBuffer->maxY)
587
_lineBuffer->dataPtr = _lineBuffer->buffer;
589
_lineBuffer->dataSize = _lineBuffer->endOfLineBufferData -
596
Compressor *compressor = _lineBuffer->compressor;
602
int compSize = compressor->compress (_lineBuffer->dataPtr,
603
_lineBuffer->dataSize,
604
_lineBuffer->minY, compPtr);
606
if (compSize < _lineBuffer->dataSize)
608
_lineBuffer->dataSize = compSize;
609
_lineBuffer->dataPtr = compPtr;
611
else if (_ofd->format == Compressor::NATIVE)
614
// The data did not shrink during compression, but
615
// we cannot write to the file using the machine's
616
// native format, so we need to convert the lineBuffer
620
convertToXdr (_ofd, _lineBuffer->buffer, _lineBuffer->minY,
621
_lineBuffer->maxY, _lineBuffer->dataSize);
625
_lineBuffer->partiallyFull = false;
627
catch (std::exception &e)
629
if (!_lineBuffer->hasException)
631
_lineBuffer->exception = e.what ();
632
_lineBuffer->hasException = true;
637
if (!_lineBuffer->hasException)
639
_lineBuffer->exception = "unrecognized exception";
640
_lineBuffer->hasException = true;
648
OutputFile::OutputFile
649
(const char fileName[],
650
const Header &header,
653
_data (new Data (true, numThreads))
657
header.sanityCheck();
658
_data->os = new StdOFStream (fileName);
661
catch (Iex::BaseExc &e)
665
REPLACE_EXC (e, "Cannot open image file "
666
"\"" << fileName << "\". " << e);
677
OutputFile::OutputFile
679
const Header &header,
682
_data (new Data (false, numThreads))
686
header.sanityCheck();
690
catch (Iex::BaseExc &e)
694
REPLACE_EXC (e, "Cannot open image file "
695
"\"" << os.fileName() << "\". " << e);
707
OutputFile::initialize (const Header &header)
709
_data->header = header;
711
const Box2i &dataWindow = header.dataWindow();
713
_data->currentScanLine = (header.lineOrder() == INCREASING_Y)?
714
dataWindow.min.y: dataWindow.max.y;
716
_data->missingScanLines = dataWindow.max.y - dataWindow.min.y + 1;
717
_data->lineOrder = header.lineOrder();
718
_data->minX = dataWindow.min.x;
719
_data->maxX = dataWindow.max.x;
720
_data->minY = dataWindow.min.y;
721
_data->maxY = dataWindow.max.y;
723
size_t maxBytesPerLine = bytesPerLineTable (_data->header,
724
_data->bytesPerLine);
726
for (size_t i = 0; i < _data->lineBuffers.size(); ++i)
728
_data->lineBuffers[i] =
729
new LineBuffer (newCompressor (_data->header.compression(),
734
LineBuffer *lineBuffer = _data->lineBuffers[0];
735
_data->format = defaultFormat (lineBuffer->compressor);
736
_data->linesInBuffer = numLinesInBuffer (lineBuffer->compressor);
737
_data->lineBufferSize = maxBytesPerLine * _data->linesInBuffer;
739
for (size_t i = 0; i < _data->lineBuffers.size(); i++)
740
_data->lineBuffers[i]->buffer.resizeErase(_data->lineBufferSize);
742
int lineOffsetSize = (dataWindow.max.y - dataWindow.min.y +
743
_data->linesInBuffer) / _data->linesInBuffer;
745
_data->lineOffsets.resize (lineOffsetSize);
747
offsetInLineBufferTable (_data->bytesPerLine,
748
_data->linesInBuffer,
749
_data->offsetInLineBuffer);
751
_data->previewPosition =
752
_data->header.writeTo (*_data->os);
754
_data->lineOffsetsPosition =
755
writeLineOffsets (*_data->os, _data->lineOffsets);
757
_data->currentPosition = _data->os->tellp();
761
OutputFile::~OutputFile ()
766
if (_data->lineOffsetsPosition > 0)
770
_data->os->seekp (_data->lineOffsetsPosition);
771
writeLineOffsets (*_data->os, _data->lineOffsets);
776
// We cannot safely throw any exceptions from here.
777
// This destructor may have been called because the
778
// stack is currently being unwound for another
791
OutputFile::fileName () const
793
return _data->os->fileName();
798
OutputFile::header () const
800
return _data->header;
805
OutputFile::setFrameBuffer (const FrameBuffer &frameBuffer)
810
// Check if the new frame buffer descriptor
811
// is compatible with the image file header.
814
const ChannelList &channels = _data->header.channels();
816
for (ChannelList::ConstIterator i = channels.begin();
820
FrameBuffer::ConstIterator j = frameBuffer.find (i.name());
822
if (j == frameBuffer.end())
825
if (i.channel().type != j.slice().type)
827
THROW (Iex::ArgExc, "Pixel type of \"" << i.name() << "\" channel "
828
"of output file \"" << fileName() << "\" is "
829
"not compatible with the frame buffer's "
833
if (i.channel().xSampling != j.slice().xSampling ||
834
i.channel().ySampling != j.slice().ySampling)
836
THROW (Iex::ArgExc, "X and/or y subsampling factors "
837
"of \"" << i.name() << "\" channel "
838
"of output file \"" << fileName() << "\" are "
839
"not compatible with the frame buffer's "
840
"subsampling factors.");
845
// Initialize slice table for writePixels().
848
vector<OutSliceInfo> slices;
850
for (ChannelList::ConstIterator i = channels.begin();
854
FrameBuffer::ConstIterator j = frameBuffer.find (i.name());
856
if (j == frameBuffer.end())
859
// Channel i is not present in the frame buffer.
860
// In the file, channel i will contain only zeroes.
863
slices.push_back (OutSliceInfo (i.channel().type,
867
i.channel().xSampling,
868
i.channel().ySampling,
874
// Channel i is present in the frame buffer.
877
slices.push_back (OutSliceInfo (j.slice().type,
888
// Store the new frame buffer.
891
_data->frameBuffer = frameBuffer;
892
_data->slices = slices;
897
OutputFile::frameBuffer () const
900
return _data->frameBuffer;
905
OutputFile::writePixels (int numScanLines)
911
if (_data->slices.size() == 0)
912
throw Iex::ArgExc ("No frame buffer specified "
913
"as pixel data source.");
916
// Maintain two iterators:
917
// nextWriteBuffer: next linebuffer to be written to the file
918
// nextCompressBuffer: next linebuffer to compress
921
int first = (_data->currentScanLine - _data->minY) /
922
_data->linesInBuffer;
924
int nextWriteBuffer = first;
925
int nextCompressBuffer;
933
// Create a task group for all line buffer tasks. When the
934
// taskgroup goes out of scope, the destructor waits until
935
// all tasks are complete.
941
// Determine the range of lineBuffers that intersect the scan
942
// line range. Then add the initial compression tasks to the
943
// thread pool. We always add in at least one task but the
944
// individual task might not do anything if numScanLines == 0.
947
if (_data->lineOrder == INCREASING_Y)
949
int last = (_data->currentScanLine + (numScanLines - 1) -
950
_data->minY) / _data->linesInBuffer;
952
scanLineMin = _data->currentScanLine;
953
scanLineMax = _data->currentScanLine + numScanLines - 1;
955
int numTasks = max (min ((int)_data->lineBuffers.size(),
959
for (int i = 0; i < numTasks; i++)
961
ThreadPool::addGlobalTask
962
(new LineBufferTask (&taskGroup, _data, first + i,
963
scanLineMin, scanLineMax));
966
nextCompressBuffer = first + numTasks;
972
int last = (_data->currentScanLine - (numScanLines - 1) -
973
_data->minY) / _data->linesInBuffer;
975
scanLineMax = _data->currentScanLine;
976
scanLineMin = _data->currentScanLine - numScanLines + 1;
978
int numTasks = max (min ((int)_data->lineBuffers.size(),
982
for (int i = 0; i < numTasks; i++)
984
ThreadPool::addGlobalTask
985
(new LineBufferTask (&taskGroup, _data, first - i,
986
scanLineMin, scanLineMax));
989
nextCompressBuffer = first - numTasks;
996
if (_data->missingScanLines <= 0)
998
throw Iex::ArgExc ("Tried to write more scan lines "
999
"than specified by the data window.");
1003
// Wait until the next line buffer is ready to be written
1006
LineBuffer *writeBuffer =
1007
_data->getLineBuffer (nextWriteBuffer);
1009
writeBuffer->wait();
1011
int numLines = writeBuffer->scanLineMax -
1012
writeBuffer->scanLineMin + 1;
1014
_data->missingScanLines -= numLines;
1017
// If the line buffer is only partially full, then it is
1018
// not complete and we cannot write it to disk yet.
1021
if (writeBuffer->partiallyFull)
1023
_data->currentScanLine = _data->currentScanLine +
1025
writeBuffer->post();
1031
// Write the line buffer
1034
writePixelData (_data, writeBuffer);
1035
nextWriteBuffer += step;
1037
_data->currentScanLine = _data->currentScanLine +
1042
assert (_data->currentScanLine ==
1043
((_data->lineOrder == INCREASING_Y) ?
1044
writeBuffer->scanLineMax + 1:
1045
writeBuffer->scanLineMin - 1));
1050
// Release the lock on the line buffer
1053
writeBuffer->post();
1056
// If this was the last line buffer in the scanline range
1059
if (nextWriteBuffer == stop)
1063
// If there are no more line buffers to compress,
1064
// then only continue to write out remaining lineBuffers
1067
if (nextCompressBuffer == stop)
1071
// Add nextCompressBuffer as a compression task
1074
ThreadPool::addGlobalTask
1075
(new LineBufferTask (&taskGroup, _data, nextCompressBuffer,
1076
scanLineMin, scanLineMax));
1079
// Update the next line buffer we need to compress
1082
nextCompressBuffer += step;
1091
// Exeption handling:
1093
// LineBufferTask::execute() may have encountered exceptions, but
1094
// those exceptions occurred in another thread, not in the thread
1095
// that is executing this call to OutputFile::writePixels().
1096
// LineBufferTask::execute() has caught all exceptions and stored
1097
// the exceptions' what() strings in the line buffers.
1098
// Now we check if any line buffer contains a stored exception; if
1099
// this is the case then we re-throw the exception in this thread.
1100
// (It is possible that multiple line buffers contain stored
1101
// exceptions. We re-throw the first exception we find and
1102
// ignore all others.)
1105
const string *exception = 0;
1107
for (int i = 0; i < _data->lineBuffers.size(); ++i)
1109
LineBuffer *lineBuffer = _data->lineBuffers[i];
1111
if (lineBuffer->hasException && !exception)
1112
exception = &lineBuffer->exception;
1114
lineBuffer->hasException = false;
1118
throw Iex::IoExc (*exception);
1120
catch (Iex::BaseExc &e)
1122
REPLACE_EXC (e, "Failed to write pixel data to image "
1123
"file \"" << fileName() << "\". " << e);
1130
OutputFile::currentScanLine () const
1133
return _data->currentScanLine;
1138
OutputFile::copyPixels (InputFile &in)
1143
// Check if this file's and and the InputFile's
1144
// headers are compatible.
1147
const Header &hdr = _data->header;
1148
const Header &inHdr = in.header();
1150
if (inHdr.find("tiles") != inHdr.end())
1151
THROW (Iex::ArgExc, "Cannot copy pixels from image "
1152
"file \"" << in.fileName() << "\" to image "
1153
"file \"" << fileName() << "\". "
1154
"The input file is tiled, but the output file is "
1155
"not. Try using TiledOutputFile::copyPixels "
1158
if (!(hdr.dataWindow() == inHdr.dataWindow()))
1159
THROW (Iex::ArgExc, "Cannot copy pixels from image "
1160
"file \"" << in.fileName() << "\" to image "
1161
"file \"" << fileName() << "\". "
1162
"The files have different data windows.");
1164
if (!(hdr.lineOrder() == inHdr.lineOrder()))
1165
THROW (Iex::ArgExc, "Quick pixel copy from image "
1166
"file \"" << in.fileName() << "\" to image "
1167
"file \"" << fileName() << "\" failed. "
1168
"The files have different line orders.");
1170
if (!(hdr.compression() == inHdr.compression()))
1171
THROW (Iex::ArgExc, "Quick pixel copy from image "
1172
"file \"" << in.fileName() << "\" to image "
1173
"file \"" << fileName() << "\" failed. "
1174
"The files use different compression methods.");
1176
if (!(hdr.channels() == inHdr.channels()))
1177
THROW (Iex::ArgExc, "Quick pixel copy from image "
1178
"file \"" << in.fileName() << "\" to image "
1179
"file \"" << fileName() << "\" failed. "
1180
"The files have different channel lists.");
1183
// Verify that no pixel data have been written to this file yet.
1186
const Box2i &dataWindow = hdr.dataWindow();
1188
if (_data->missingScanLines != dataWindow.max.y - dataWindow.min.y + 1)
1189
THROW (Iex::LogicExc, "Quick pixel copy from image "
1190
"file \"" << in.fileName() << "\" to image "
1191
"file \"" << fileName() << "\" failed. "
1192
"\"" << fileName() << "\" already contains "
1196
// Copy the pixel data.
1199
while (_data->missingScanLines > 0)
1201
const char *pixelData;
1204
in.rawPixelData (_data->currentScanLine, pixelData, pixelDataSize);
1206
writePixelData (_data, lineBufferMinY (_data->currentScanLine,
1208
_data->linesInBuffer),
1209
pixelData, pixelDataSize);
1211
_data->currentScanLine += (_data->lineOrder == INCREASING_Y)?
1212
_data->linesInBuffer: -_data->linesInBuffer;
1214
_data->missingScanLines -= _data->linesInBuffer;
1220
OutputFile::updatePreviewImage (const PreviewRgba newPixels[])
1224
if (_data->previewPosition <= 0)
1225
THROW (Iex::LogicExc, "Cannot update preview image pixels. "
1226
"File \"" << fileName() << "\" does not "
1227
"contain a preview image.");
1230
// Store the new pixels in the header's preview image attribute.
1233
PreviewImageAttribute &pia =
1234
_data->header.typedAttribute <PreviewImageAttribute> ("preview");
1236
PreviewImage &pi = pia.value();
1237
PreviewRgba *pixels = pi.pixels();
1238
int numPixels = pi.width() * pi.height();
1240
for (int i = 0; i < numPixels; ++i)
1241
pixels[i] = newPixels[i];
1244
// Save the current file position, jump to the position in
1245
// the file where the preview image starts, store the new
1246
// preview image, and jump back to the saved file position.
1249
Int64 savedPosition = _data->os->tellp();
1253
_data->os->seekp (_data->previewPosition);
1254
pia.writeValueTo (*_data->os, _data->version);
1255
_data->os->seekp (savedPosition);
1257
catch (Iex::BaseExc &e)
1259
REPLACE_EXC (e, "Cannot update preview image pixels for "
1260
"file \"" << fileName() << "\". " << e);
1267
OutputFile::breakScanLine (int y, int offset, int length, char c)
1272
_data->lineOffsets[(y - _data->minY) / _data->linesInBuffer];
1275
THROW (Iex::ArgExc, "Cannot overwrite scan line " << y << ". "
1276
"The scan line has not yet been stored in "
1277
"file \"" << fileName() << "\".");
1279
_data->currentPosition = 0;
1280
_data->os->seekp (position + offset);
1282
for (int i = 0; i < length; ++i)
1283
_data->os->write (&c, 1);