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 ()
114
struct OutputFile::Data
169
struct OutputFile::Data: public Mutex
118
Int64 previewPosition;
119
FrameBuffer frameBuffer;
121
int missingScanLines;
127
vector<Int64> lineOffsets;
129
size_t lineBufferSize;
132
Array<char> lineBuffer;
133
char * endOfLineBufferData;
134
vector<size_t> bytesPerLine;
135
vector<size_t> offsetInLineBuffer;
136
Compressor * compressor;
137
Compressor::Format format;
138
vector<OutSliceInfo> slices;
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
140
191
bool deleteStream;
141
Int64 lineOffsetsPosition;
142
Int64 currentPosition;
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
149
OutputFile::Data::Data (bool del):
211
OutputFile::Data::Data (bool deleteStream, int numThreads):
213
deleteStream (deleteStream),
153
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));
299
writePixelData (OutputFile::Data *ofd, const LineBuffer *lineBuffer)
304
lineBuffer->dataSize);
224
convertToXdr (OutputFile::Data *ofd, int inSize)
309
convertToXdr (OutputFile::Data *ofd,
310
Array<char> &lineBuffer,
227
// Convert the contents of an OutputFile's lineBuffer from the machine's
228
// native representation to Xdr format. This function is called by
229
// writePixels(), below, if the compressor wanted its input pixel data
230
// in the machine's native format, but then failed to compress the data
231
// (most compressors will expand rather than compress random input data).
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).
233
// Note that this routine assumes that the machine's native representation
234
// of the pixel data has the same size as the Xdr representation. This
235
// makes it possible to convert the pixel data in place, without an
236
// intermediate temporary buffer.
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.
239
329
int startY, endY; // The first and last scanlines in
240
330
// the file that are in the lineBuffer.
243
333
if (ofd->lineOrder == INCREASING_Y)
245
startY = std::max (ofd->lineBufferMinY, ofd->minY);
246
endY = std::min (ofd->lineBufferMaxY, ofd->maxY) + 1;
335
startY = max (lineBufferMinY, ofd->minY);
336
endY = min (lineBufferMaxY, ofd->maxY) + 1;
251
startY = std::min (ofd->lineBufferMaxY, ofd->maxY);
252
endY = std::max (ofd->lineBufferMinY, ofd->minY) - 1;
341
startY = min (lineBufferMaxY, ofd->maxY);
342
endY = max (lineBufferMinY, ofd->minY) - 1;
257
347
// Iterate over all scanlines in the lineBuffer to convert.
260
for (int y = startY; y != endY; y += dy)
350
for (int y = startY; y != endY; y += step)
263
353
// Set these to point to the start of line y.
264
// We will write to writePtr from pixelPtr.
354
// We will write to writePtr from readPtr.
267
char *writePtr = ofd->lineBuffer +
268
ofd->offsetInLineBuffer[y - ofd->minY];
270
char *pixelPtr = writePtr;
357
char *writePtr = lineBuffer + ofd->offsetInLineBuffer[y - ofd->minY];
358
const char *readPtr = writePtr;
273
361
// Iterate over all slices in the file.
299
387
// Convert the samples in place.
306
while (dMinX <= dMaxX)
308
Xdr::write <CharPtrIO>
309
(writePtr, *(const unsigned int *) pixelPtr);
310
pixelPtr += sizeof(unsigned int);
318
while (dMinX <= dMaxX)
320
Xdr::write <CharPtrIO>
321
(writePtr, *(const half *) pixelPtr);
322
pixelPtr += sizeof(half);
330
while (dMinX <= dMaxX)
332
Xdr::write <CharPtrIO>
333
(writePtr, *(const float *) pixelPtr);
334
pixelPtr += sizeof(float);
342
throw Iex::ArgExc ("Unknown pixel data type.");
348
assert (writePtr == pixelPtr);
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;
357
OutputFile::OutputFile (const char fileName[], const Header &header):
358
_data (new Data (true))
648
OutputFile::OutputFile
649
(const char fileName[],
650
const Header &header,
653
_data (new Data (true, numThreads))
601
911
if (_data->slices.size() == 0)
602
912
throw Iex::ArgExc ("No frame buffer specified "
603
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)
607
if (_data->missingScanLines <= 0)
609
throw Iex::ArgExc ("Tried to write more scan lines "
610
"than specified by the data window.");
615
assert (_data->currentScanLine >= _data->lineBufferMinY &&
616
_data->currentScanLine <= _data->lineBufferMaxY);
621
// Convert one scan line's worth of pixel data to
622
// a machine-independent representation, and store
623
// the result in _data->lineBuffer.
626
int y = _data->currentScanLine;
628
char *writePtr = _data->lineBuffer +
629
_data->offsetInLineBuffer[y - _data->minY];
632
// Iterate over all image channels.
635
for (unsigned int i = 0; i < _data->slices.size(); ++i)
638
// Test if scan line y of this channel is
639
// contains any data (the scan line contains
640
// data only if y % ySampling == 0).
643
const OutSliceInfo &slice = _data->slices[i];
645
if (modp (y, slice.ySampling) != 0)
649
// Find the x coordinates of the leftmost and rightmost
650
// sampled pixels (i.e. pixels within the data window
651
// for which x % xSampling == 0).
654
int dMinX = divp (_data->minX, slice.xSampling);
655
int dMaxX = divp (_data->maxX, slice.xSampling);
658
// Iterate over the sampled pixels.
664
// The frame buffer contains no data for this channel.
665
// Store zeroes in _data->lineBuffer.
668
if (_data->format == Compressor::XDR)
671
// The compressor expects data in Xdr format.
678
while (dMinX <= dMaxX)
680
Xdr::write <CharPtrIO>
681
(writePtr, (unsigned int) 0);
688
while (dMinX <= dMaxX)
690
Xdr::write <CharPtrIO>
691
(writePtr, (half) 0);
698
while (dMinX <= dMaxX)
700
Xdr::write <CharPtrIO>
701
(writePtr, (float) 0);
708
throw Iex::ArgExc ("Unknown pixel data type.");
714
// The compressor expects data in
715
// the machines native format.
722
while (dMinX <= dMaxX)
724
static unsigned int ui = 0;
726
for (size_t i = 0; i < sizeof (ui); ++i)
727
*writePtr++ = ((char *) &ui)[i];
735
while (dMinX <= dMaxX)
737
*(half *) writePtr = half (0);
738
writePtr += sizeof (half);
745
while (dMinX <= dMaxX)
749
for (size_t i = 0; i < sizeof (f); ++i)
750
*writePtr++ = ((char *) &f)[i];
758
throw Iex::ArgExc ("Unknown pixel data type.");
765
// If necessary, convert the pixel data to
766
// a machine-independent representation.
767
// Then store the pixel data in _data->lineBuffer.
770
const char *linePtr = slice.base +
771
divp (y, slice.ySampling) *
774
const char *pixelPtr = linePtr + dMinX * slice.xStride;
775
const char *endPtr = linePtr + dMaxX * slice.xStride;
777
if (_data->format == Compressor::XDR)
780
// The compressor expects data in Xdr format
787
while (pixelPtr <= endPtr)
789
Xdr::write <CharPtrIO>
790
(writePtr, *(const unsigned int *) pixelPtr);
791
pixelPtr += slice.xStride;
797
while (pixelPtr <= endPtr)
799
Xdr::write <CharPtrIO>
800
(writePtr, *(const half *) pixelPtr);
801
pixelPtr += slice.xStride;
807
while (pixelPtr <= endPtr)
809
Xdr::write <CharPtrIO>
810
(writePtr, *(const float *) pixelPtr);
811
pixelPtr += slice.xStride;
817
throw Iex::ArgExc ("Unknown pixel data type.");
823
// The compressor expects data in the
824
// machine's native format.
831
while (pixelPtr <= endPtr)
834
i < sizeof (unsigned int);
837
*writePtr++ = pixelPtr[i];
840
pixelPtr += slice.xStride;
846
while (pixelPtr <= endPtr)
848
*(half *) writePtr = *(const half *) pixelPtr;
849
writePtr += sizeof (half);
850
pixelPtr += slice.xStride;
856
while (pixelPtr <= endPtr)
858
for (size_t i = 0; i < sizeof (float); ++i)
859
*writePtr++ = pixelPtr[i];
861
pixelPtr += slice.xStride;
867
throw Iex::ArgExc ("Unknown pixel data type.");
873
if (_data->endOfLineBufferData < writePtr)
874
_data->endOfLineBufferData = writePtr;
878
assert (writePtr - (_data->lineBuffer +
879
_data->offsetInLineBuffer[y - _data->minY]) ==
880
(int) _data->bytesPerLine[y - _data->minY]);
885
// If _data->lineBuffer is full, or if the current scan
886
// line is the last one, then compress the contents of
887
// _data->lineBuffer, and store the compressed data in
891
int nextScanLine = _data->currentScanLine +
892
((_data->lineOrder == INCREASING_Y)? 1: -1);
894
if (nextScanLine < _data->lineBufferMinY ||
895
nextScanLine > _data->lineBufferMaxY ||
896
_data->missingScanLines <= 1)
898
int dataSize = _data->endOfLineBufferData - _data->lineBuffer;
899
const char *dataPtr = _data->lineBuffer;
901
if (_data->compressor)
905
int compSize = _data->compressor->compress
907
_data->lineBufferMinY,
910
if (compSize < dataSize)
915
else if (_data->format == Compressor::NATIVE)
918
// The data did not shrink during compression, but
919
// we cannot write to the file using the machine's
920
// native format, so we need to convert the lineBuffer
924
convertToXdr(_data, dataSize);
928
writePixelData (_data, dataPtr, dataSize);
931
// Clear _data->lineBuffer.
934
_data->endOfLineBufferData = _data->lineBuffer;
936
_data->lineBufferMinY = lineBufferMinY (nextScanLine,
938
_data->linesInBuffer);
940
_data->lineBufferMaxY = lineBufferMaxY (nextScanLine,
942
_data->linesInBuffer);
946
// Advance to the next scan line.
950
_data->currentScanLine = nextScanLine;
951
_data->missingScanLines -= 1;
1109
LineBuffer *lineBuffer = _data->lineBuffers[i];
1111
if (lineBuffer->hasException && !exception)
1112
exception = &lineBuffer->exception;
1114
lineBuffer->hasException = false;
1118
throw Iex::IoExc (*exception);
954
1120
catch (Iex::BaseExc &e)
971
1138
OutputFile::copyPixels (InputFile &in)
974
1143
// Check if this file's and and the InputFile's
975
1144
// headers are compatible.
978
const Header &hdr = header();
1147
const Header &hdr = _data->header;
979
1148
const Header &inHdr = in.header();
981
1150
if (inHdr.find("tiles") != inHdr.end())
983
1151
THROW (Iex::ArgExc, "Cannot copy pixels from image "
984
1152
"file \"" << in.fileName() << "\" to image "
985
"file \"" << fileName() << "\". The input file is "
986
"tiled, but the output file is not. Try using "
987
"TiledOutputFile::copyPixels instead.");
1153
"file \"" << fileName() << "\". "
1154
"The input file is tiled, but the output file is "
1155
"not. Try using TiledOutputFile::copyPixels "
990
1158
if (!(hdr.dataWindow() == inHdr.dataWindow()))
992
1159
THROW (Iex::ArgExc, "Cannot copy pixels from image "
993
1160
"file \"" << in.fileName() << "\" to image "
994
"file \"" << fileName() << "\". The files "
995
"have different data windows.");
1161
"file \"" << fileName() << "\". "
1162
"The files have different data windows.");
998
1164
if (!(hdr.lineOrder() == inHdr.lineOrder()))
1000
1165
THROW (Iex::ArgExc, "Quick pixel copy from image "
1001
1166
"file \"" << in.fileName() << "\" to image "
1002
1167
"file \"" << fileName() << "\" failed. "
1003
1168
"The files have different line orders.");
1006
1170
if (!(hdr.compression() == inHdr.compression()))
1008
1171
THROW (Iex::ArgExc, "Quick pixel copy from image "
1009
1172
"file \"" << in.fileName() << "\" to image "
1010
1173
"file \"" << fileName() << "\" failed. "
1011
1174
"The files use different compression methods.");
1014
1176
if (!(hdr.channels() == inHdr.channels()))
1016
1177
THROW (Iex::ArgExc, "Quick pixel copy from image "
1017
1178
"file \"" << in.fileName() << "\" to image "
1018
1179
"file \"" << fileName() << "\" failed. "
1019
1180
"The files have different channel lists.");
1023
1183
// Verify that no pixel data have been written to this file yet.