/
ImfDeepScanLineInputFile.cpp
2072 lines (1660 loc) · 65.9 KB
/
ImfDeepScanLineInputFile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2011, Industrial Light & Magic, a division of Lucas
// Digital Ltd. LLC
//
// All rights reserved.
//
// 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
// from this software without specific prior written permission.
//
// 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 DeepScanLineInputFile
//
//-----------------------------------------------------------------------------
#include <ImfDeepScanLineInputFile.h>
#include <ImfChannelList.h>
#include <ImfMisc.h>
#include <ImfStdIO.h>
#include <ImfCompressor.h>
#include <ImfXdr.h>
#include <ImfConvert.h>
#include <ImfThreading.h>
#include <ImfPartType.h>
#include <ImfVersion.h>
#include "ImfMultiPartInputFile.h"
#include "ImfDeepFrameBuffer.h"
#include "ImfInputStreamMutex.h"
#include "ImfInputPartData.h"
#include "ImathBox.h"
#include "ImathFun.h"
#include "IlmThreadPool.h"
#include "IlmThreadSemaphore.h"
#include "IlmThreadMutex.h"
#include "Iex.h"
#include <algorithm>
#include <assert.h>
#include <string>
#include <vector>
#include <limits>
#include "ImfNamespace.h"
OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
using IMATH_NAMESPACE::Box2i;
using IMATH_NAMESPACE::divp;
using IMATH_NAMESPACE::modp;
using std::string;
using std::vector;
using std::min;
using std::max;
using ILMTHREAD_NAMESPACE::Mutex;
using ILMTHREAD_NAMESPACE::Lock;
using ILMTHREAD_NAMESPACE::Semaphore;
using ILMTHREAD_NAMESPACE::Task;
using ILMTHREAD_NAMESPACE::TaskGroup;
using ILMTHREAD_NAMESPACE::ThreadPool;
namespace {
struct InSliceInfo
{
PixelType typeInFrameBuffer;
PixelType typeInFile;
char * base;
char* pointerArrayBase;
size_t xPointerStride;
size_t yPointerStride;
size_t sampleStride;
int xSampling;
int ySampling;
bool fill;
bool skip;
double fillValue;
InSliceInfo (PixelType typeInFrameBuffer = HALF,
char * base = NULL,
PixelType typeInFile = HALF,
size_t xPointerStride = 0,
size_t yPointerStride = 0,
size_t sampleStride = 0,
int xSampling = 1,
int ySampling = 1,
bool fill = false,
bool skip = false,
double fillValue = 0.0);
};
InSliceInfo::InSliceInfo (PixelType tifb,
char * b,
PixelType tifl,
size_t xpst,
size_t ypst,
size_t spst,
int xsm, int ysm,
bool f, bool s,
double fv)
:
typeInFrameBuffer (tifb),
typeInFile (tifl),
base(b),
xPointerStride (xpst),
yPointerStride (ypst),
sampleStride (spst),
xSampling (xsm),
ySampling (ysm),
fill (f),
skip (s),
fillValue (fv)
{
// empty
}
struct LineBuffer
{
const char * uncompressedData;
char * buffer;
Int64 packedDataSize;
Int64 unpackedDataSize;
int minY;
int maxY;
Compressor * compressor;
Compressor::Format format;
int number;
bool hasException;
string exception;
LineBuffer ();
~LineBuffer ();
inline void wait () {_sem.wait();}
inline void post () {_sem.post();}
private:
Semaphore _sem;
};
LineBuffer::LineBuffer ():
uncompressedData (0),
buffer (0),
packedDataSize (0),
compressor (0),
format (defaultFormat(compressor)),
number (-1),
hasException (false),
exception (),
_sem (1)
{
// empty
}
LineBuffer::~LineBuffer ()
{
if (compressor != 0)
delete compressor;
}
} // namespace
struct DeepScanLineInputFile::Data: public Mutex
{
Header header; // the image header
int version; // file's version
DeepFrameBuffer frameBuffer; // framebuffer to write into
LineOrder lineOrder; // order of the scanlines in file
int minX; // data window's min x coord
int maxX; // data window's max x coord
int minY; // data window's min y coord
int maxY; // data window's max x coord
vector<Int64> lineOffsets; // stores offsets in file for
// each line
bool fileIsComplete; // True if no scanlines are missing
// in the file
int nextLineBufferMinY; // minimum y of the next linebuffer
vector<size_t> bytesPerLine; // combined size of a line over all
// channels
vector<size_t> offsetInLineBuffer; // offset for each scanline in its
// linebuffer
vector<InSliceInfo*> slices; // info about channels in file
vector<LineBuffer*> lineBuffers; // each holds one line buffer
int linesInBuffer; // number of scanlines each buffer
// holds
int partNumber; // part number
int numThreads; // number of threads
bool multiPartBackwardSupport; // if we are reading a multipart file using single file API
MultiPartInputFile* multiPartFile; // for multipart files opened as single part
bool memoryMapped; // if the stream is memory mapped
Array2D<unsigned int> sampleCount; // the number of samples
// in each pixel
Array<unsigned int> lineSampleCount; // the number of samples
// in each line
Array<bool> gotSampleCount; // for each scanline, indicating if
// we have got its sample count table
char* sampleCountSliceBase; // pointer to the start of
// the sample count array
int sampleCountXStride; // x stride of the sample count array
int sampleCountYStride; // y stride of the sample count array
bool frameBufferValid; // set by setFrameBuffer: excepts if readPixelSampleCounts if false
Array<char> sampleCountTableBuffer;
// the buffer for sample count table
Compressor* sampleCountTableComp;
// the decompressor for sample count table
int combinedSampleSize; // total size of all channels combined: used to sanity check sample table size
int maxSampleCountTableSize;
// the max size in bytes for a pixel
// sample count table
InputStreamMutex* _streamData;
bool _deleteStream;
Data (int numThreads);
~Data ();
Data (const Data& data) = delete;
Data& operator = (const Data& data) = delete;
Data (Data&& data) = delete;
Data& operator = (Data&& data) = delete;
inline LineBuffer * getLineBuffer (int number); // hash function from line
// buffer indices into our
// vector of line buffers
};
DeepScanLineInputFile::Data::Data (int numThreads):
partNumber(-1),
numThreads(numThreads),
multiPartBackwardSupport(false),
multiPartFile(NULL),
memoryMapped(false),
frameBufferValid(false),
_streamData(NULL),
_deleteStream(false)
{
//
// We need at least one lineBuffer, but if threading is used,
// to keep n threads busy we need 2*n lineBuffers
//
lineBuffers.resize (max (1, 2 * numThreads));
for (size_t i = 0; i < lineBuffers.size(); i++)
lineBuffers[i] = 0;
sampleCountTableComp = 0;
}
DeepScanLineInputFile::Data::~Data ()
{
for (size_t i = 0; i < lineBuffers.size(); i++)
if (lineBuffers[i] != 0)
delete lineBuffers[i];
for (size_t i = 0; i < slices.size(); i++)
delete slices[i];
if (sampleCountTableComp != 0)
delete sampleCountTableComp;
if (multiPartBackwardSupport)
delete multiPartFile;
}
inline LineBuffer *
DeepScanLineInputFile::Data::getLineBuffer (int lineBufferNumber)
{
return lineBuffers[lineBufferNumber % lineBuffers.size()];
}
namespace {
void
reconstructLineOffsets (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is,
LineOrder lineOrder,
vector<Int64> &lineOffsets)
{
Int64 position = is.tellg();
try
{
for (unsigned int i = 0; i < lineOffsets.size(); i++)
{
Int64 lineOffset = is.tellg();
int y;
OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (is, y);
Int64 packed_offset;
Int64 packed_sample;
OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (is, packed_offset);
OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (is, packed_sample);
//next is unpacked sample table size - skip this too
Xdr::skip <StreamIO> (is, packed_offset+packed_sample+8);
if (lineOrder == INCREASING_Y)
lineOffsets[i] = lineOffset;
else
lineOffsets[lineOffsets.size() - i - 1] = lineOffset;
}
}
catch (...) //NOSONAR - suppress vulnerability reports from SonarCloud.
{
//
// Suppress all exceptions. This functions is
// called only to reconstruct the line offset
// table for incomplete files, and exceptions
// are likely.
//
}
is.clear();
is.seekg (position);
}
void
readLineOffsets (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is,
LineOrder lineOrder,
vector<Int64> &lineOffsets,
bool &complete)
{
for (unsigned int i = 0; i < lineOffsets.size(); i++)
{
OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (is, lineOffsets[i]);
}
complete = true;
for (unsigned int i = 0; i < lineOffsets.size(); i++)
{
if (lineOffsets[i] <= 0)
{
//
// Invalid data in the line offset table mean that
// the file is probably incomplete (the table is
// the last thing written to the file). Either
// some process is still busy writing the file,
// or writing the file was aborted.
//
// We should still be able to read the existing
// parts of the file. In order to do this, we
// have to make a sequential scan over the scan
// line data to reconstruct the line offset table.
//
complete = false;
reconstructLineOffsets (is, lineOrder, lineOffsets);
break;
}
}
}
void
readPixelData (InputStreamMutex *streamData,
DeepScanLineInputFile::Data *ifd,
int minY,
char *&buffer,
Int64 &packedDataSize,
Int64 &unpackedDataSize)
{
//
// Read a single line buffer from the input file.
//
// If the input file is not memory-mapped, we copy the pixel data into
// into the array pointed to by buffer. If the file is memory-mapped,
// then we change where buffer points to instead of writing into the
// array (hence buffer needs to be a reference to a char *).
//
int lineBufferNumber = (minY - ifd->minY) / ifd->linesInBuffer;
Int64 lineOffset = ifd->lineOffsets[lineBufferNumber];
if (lineOffset == 0)
THROW (IEX_NAMESPACE::InputExc, "Scan line " << minY << " is missing.");
//
// Seek to the start of the scan line in the file,
// if necessary.
//
if (!isMultiPart(ifd->version))
{
if (ifd->nextLineBufferMinY != minY)
streamData->is->seekg (lineOffset);
}
else
{
//
// In a multi-part file, the file pointer may have been moved by
// other parts, so we have to ask tellg() where we are.
//
if (streamData->is->tellg() != ifd->lineOffsets[lineBufferNumber])
streamData->is->seekg (lineOffset);
}
//
// Read the data block's header.
//
int yInFile;
//
// Read the part number when we are dealing with a multi-part file.
//
if (isMultiPart(ifd->version))
{
int partNumber;
OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (*streamData->is, partNumber);
if (partNumber != ifd->partNumber)
{
THROW (IEX_NAMESPACE::ArgExc, "Unexpected part number " << partNumber
<< ", should be " << ifd->partNumber << ".");
}
}
OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (*streamData->is, yInFile);
if (yInFile != minY)
throw IEX_NAMESPACE::InputExc ("Unexpected data block y coordinate.");
Int64 sampleCountTableSize;
OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (*streamData->is, sampleCountTableSize);
OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (*streamData->is, packedDataSize);
OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (*streamData->is, unpackedDataSize);
//
// We make a check on the data size requirements here.
// Whilst we wish to store 64bit sizes on disk, not all the compressors
// have been made to work with such data sizes and are still limited to
// using signed 32 bit (int) for the data size. As such, this version
// insists that we validate that the data size does not exceed the data
// type max limit.
// @TODO refactor the compressor code to ensure full 64-bit support.
//
int compressorMaxDataSize = std::numeric_limits<int>::max();
if (packedDataSize > Int64(compressorMaxDataSize) ||
unpackedDataSize > Int64(compressorMaxDataSize))
{
THROW (IEX_NAMESPACE::ArgExc, "This version of the library does not support "
<< "the allocation of data with size > " << compressorMaxDataSize
<< " file unpacked size :" << unpackedDataSize
<< " file packed size :" << packedDataSize << ".\n");
}
//
// Skip the pixel sample count table because we have read this data.
//
Xdr::skip <StreamIO> (*streamData->is, sampleCountTableSize);
//
// Read the pixel data.
//
if (streamData->is->isMemoryMapped ())
buffer = streamData->is->readMemoryMapped (packedDataSize);
else
{
// (TODO) check if the packed data size is too big?
// (TODO) better memory management. Don't delete buffer all the time.
if (buffer != 0) delete[] buffer;
buffer = new char[packedDataSize];
streamData->is->read (buffer, packedDataSize);
}
//
// Keep track of which scan line is the next one in
// the file, so that we can avoid redundant seekg()
// operations (seekg() can be fairly expensive).
//
if (ifd->lineOrder == INCREASING_Y)
ifd->nextLineBufferMinY = minY + ifd->linesInBuffer;
else
ifd->nextLineBufferMinY = minY - ifd->linesInBuffer;
}
//
// A LineBufferTask encapsulates the task uncompressing a set of
// scanlines (line buffer) and copying them into the frame buffer.
//
class LineBufferTask : public Task
{
public:
LineBufferTask (TaskGroup *group,
DeepScanLineInputFile::Data *ifd,
LineBuffer *lineBuffer,
int scanLineMin,
int scanLineMax);
virtual ~LineBufferTask ();
virtual void execute ();
private:
DeepScanLineInputFile::Data * _ifd;
LineBuffer * _lineBuffer;
int _scanLineMin;
int _scanLineMax;
};
LineBufferTask::LineBufferTask
(TaskGroup *group,
DeepScanLineInputFile::Data *ifd,
LineBuffer *lineBuffer,
int scanLineMin,
int scanLineMax)
:
Task (group),
_ifd (ifd),
_lineBuffer (lineBuffer),
_scanLineMin (scanLineMin),
_scanLineMax (scanLineMax)
{
// empty
}
LineBufferTask::~LineBufferTask ()
{
//
// Signal that the line buffer is now free
//
_lineBuffer->post ();
}
void
LineBufferTask::execute ()
{
try
{
//
// Uncompress the data, if necessary
//
if (_lineBuffer->uncompressedData == 0)
{
Int64 uncompressedSize = 0;
int maxY = min (_lineBuffer->maxY, _ifd->maxY);
for (int i = _lineBuffer->minY - _ifd->minY;
i <= maxY - _ifd->minY;
++i)
{
uncompressedSize += (int) _ifd->bytesPerLine[i];
}
//
// Create the compressor everytime when we want to use it,
// because we don't know maxBytesPerLine beforehand.
// (TODO) optimize this. don't do this every time.
//
if (_lineBuffer->compressor != 0)
delete _lineBuffer->compressor;
Int64 maxBytesPerLine = 0;
for (int i = _lineBuffer->minY - _ifd->minY;
i <= maxY - _ifd->minY;
++i)
{
if (_ifd->bytesPerLine[i] > maxBytesPerLine)
maxBytesPerLine = _ifd->bytesPerLine[i];
}
_lineBuffer->compressor = newCompressor(_ifd->header.compression(),
maxBytesPerLine,
_ifd->header);
if (_lineBuffer->compressor &&
_lineBuffer->packedDataSize < uncompressedSize)
{
_lineBuffer->format = _lineBuffer->compressor->format();
_lineBuffer->packedDataSize = _lineBuffer->compressor->uncompress
(_lineBuffer->buffer, _lineBuffer->packedDataSize,
_lineBuffer->minY, _lineBuffer->uncompressedData);
}
else
{
//
// If the line is uncompressed, it's in XDR format,
// regardless of the compressor's output format.
//
_lineBuffer->format = Compressor::XDR;
_lineBuffer->uncompressedData = _lineBuffer->buffer;
}
}
int yStart, yStop, dy;
if (_ifd->lineOrder == INCREASING_Y)
{
yStart = _scanLineMin;
yStop = _scanLineMax + 1;
dy = 1;
}
else
{
yStart = _scanLineMax;
yStop = _scanLineMin - 1;
dy = -1;
}
for (int y = yStart; y != yStop; y += dy)
{
//
// Convert one scan line's worth of pixel data back
// from the machine-independent representation, and
// store the result in the frame buffer.
//
const char *readPtr = _lineBuffer->uncompressedData +
_ifd->offsetInLineBuffer[y - _ifd->minY];
//
// Iterate over all image channels.
//
for (unsigned int i = 0; i < _ifd->slices.size(); ++i)
{
//
// Test if scan line y of this channel contains any data
// (the scan line contains data only if y % ySampling == 0).
//
InSliceInfo &slice = *_ifd->slices[i];
if (modp (y, slice.ySampling) != 0)
continue;
//
// Find the x coordinates of the leftmost and rightmost
// sampled pixels (i.e. pixels within the data window
// for which x % xSampling == 0).
//
//
// Fill the frame buffer with pixel data.
//
if (slice.skip)
{
//
// The file contains data for this channel, but
// the frame buffer contains no slice for this channel.
//
skipChannel (readPtr, slice.typeInFile,
_ifd->lineSampleCount[y - _ifd->minY]);
}
else
{
//
// The frame buffer contains a slice for this channel.
//
int width = (_ifd->maxX - _ifd->minX + 1);
ptrdiff_t base = reinterpret_cast<ptrdiff_t>(&_ifd->sampleCount[0][0]);
base -= sizeof(unsigned int)*_ifd->minX;
base -= sizeof(unsigned int)*static_cast<ptrdiff_t>(_ifd->minY) * static_cast<ptrdiff_t>(width);
copyIntoDeepFrameBuffer (readPtr, slice.base,
reinterpret_cast<char*>(base),
sizeof(unsigned int) * 1,
sizeof(unsigned int) * width,
y, _ifd->minX, _ifd->maxX,
0, 0,
0, 0,
slice.sampleStride,
slice.xPointerStride,
slice.yPointerStride,
slice.fill,
slice.fillValue, _lineBuffer->format,
slice.typeInFrameBuffer,
slice.typeInFile);
}
}
}
}
catch (std::exception &e)
{
if (!_lineBuffer->hasException)
{
_lineBuffer->exception = e.what();
_lineBuffer->hasException = true;
}
}
catch (...)
{
if (!_lineBuffer->hasException)
{
_lineBuffer->exception = "unrecognized exception";
_lineBuffer->hasException = true;
}
}
}
LineBufferTask *
newLineBufferTask
(TaskGroup *group,
DeepScanLineInputFile::Data *ifd,
int number,
int scanLineMin,
int scanLineMax)
{
//
// Wait for a line buffer to become available, fill the line
// buffer with raw data from the file if necessary, and create
// a new LineBufferTask whose execute() method will uncompress
// the contents of the buffer and copy the pixels into the
// frame buffer.
//
LineBuffer *lineBuffer = ifd->getLineBuffer (number);
try
{
lineBuffer->wait ();
if (lineBuffer->number != number)
{
lineBuffer->minY = ifd->minY + number * ifd->linesInBuffer;
lineBuffer->maxY = lineBuffer->minY + ifd->linesInBuffer - 1;
lineBuffer->number = number;
lineBuffer->uncompressedData = 0;
readPixelData (ifd->_streamData, ifd, lineBuffer->minY,
lineBuffer->buffer,
lineBuffer->packedDataSize,
lineBuffer->unpackedDataSize);
}
}
catch (std::exception &e)
{
if (!lineBuffer->hasException)
{
lineBuffer->exception = e.what();
lineBuffer->hasException = true;
}
lineBuffer->number = -1;
lineBuffer->post();
throw;
}
catch (...)
{
//
// Reading from the file caused an exception.
// Signal that the line buffer is free, and
// re-throw the exception.
//
lineBuffer->exception = "unrecognized exception";
lineBuffer->hasException = true;
lineBuffer->number = -1;
lineBuffer->post();
throw;
}
scanLineMin = max (lineBuffer->minY, scanLineMin);
scanLineMax = min (lineBuffer->maxY, scanLineMax);
return new LineBufferTask (group, ifd, lineBuffer,
scanLineMin, scanLineMax);
}
} // namespace
void DeepScanLineInputFile::initialize(const Header& header)
{
try
{
if (header.type() != DEEPSCANLINE)
throw IEX_NAMESPACE::ArgExc("Can't build a DeepScanLineInputFile from "
"a type-mismatched part.");
if(header.version()!=1)
{
THROW(IEX_NAMESPACE::ArgExc, "Version " << header.version() << " not supported for deepscanline images in this version of the library");
}
_data->header = header;
_data->lineOrder = _data->header.lineOrder();
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;
_data->sampleCount.resizeErase(_data->maxY - _data->minY + 1,
_data->maxX - _data->minX + 1);
_data->lineSampleCount.resizeErase(_data->maxY - _data->minY + 1);
Compressor* compressor = newCompressor(_data->header.compression(),
0,
_data->header);
_data->linesInBuffer = numLinesInBuffer (compressor);
delete compressor;
_data->nextLineBufferMinY = _data->minY - 1;
int lineOffsetSize = (dataWindow.max.y - dataWindow.min.y +
_data->linesInBuffer) / _data->linesInBuffer;
_data->lineOffsets.resize (lineOffsetSize);
for (size_t i = 0; i < _data->lineBuffers.size(); i++)
_data->lineBuffers[i] = new LineBuffer ();
_data->gotSampleCount.resizeErase(_data->maxY - _data->minY + 1);
for (int i = 0; i < _data->maxY - _data->minY + 1; i++)
_data->gotSampleCount[i] = false;
_data->maxSampleCountTableSize = min(_data->linesInBuffer, _data->maxY - _data->minY + 1) *
(_data->maxX - _data->minX + 1) *
sizeof(unsigned int);
_data->sampleCountTableBuffer.resizeErase(_data->maxSampleCountTableSize);
_data->sampleCountTableComp = newCompressor(_data->header.compression(),
_data->maxSampleCountTableSize,
_data->header);
_data->bytesPerLine.resize (_data->maxY - _data->minY + 1);
const ChannelList & c=header.channels();
_data->combinedSampleSize=0;
for(ChannelList::ConstIterator i=c.begin();i!=c.end();i++)
{
switch(i.channel().type)
{
case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF :
_data->combinedSampleSize+=Xdr::size<half>();
break;
case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT :
_data->combinedSampleSize+=Xdr::size<float>();
break;
case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT :
_data->combinedSampleSize+=Xdr::size<unsigned int>();
break;
default :
THROW(IEX_NAMESPACE::ArgExc, "Bad type for channel " << i.name() << " initializing deepscanline reader");
}
}
}
catch (...)
{
// Don't delete _data here, leave that to caller
throw;
}
}
DeepScanLineInputFile::DeepScanLineInputFile(InputPartData* part)
{
_data = new Data(part->numThreads);
_data->_deleteStream=false;
_data->_streamData = part->mutex;
_data->memoryMapped = _data->_streamData->is->isMemoryMapped();
_data->version = part->version;
try
{
initialize(part->header);
}
catch(...)
{
delete _data;
throw;
}
_data->lineOffsets = part->chunkOffsets;
_data->partNumber = part->partNumber;
}
DeepScanLineInputFile::DeepScanLineInputFile
(const char fileName[], int numThreads)
:
_data (new Data (numThreads))
{
_data->_deleteStream = true;
OPENEXR_IMF_INTERNAL_NAMESPACE::IStream* is = 0;
try
{
is = new StdIFStream (fileName);
readMagicNumberAndVersionField(*is, _data->version);
//
// Backward compatibility to read multpart file.
// multiPartInitialize will create _streamData
if (isMultiPart(_data->version))
{
compatibilityInitialize(*is);
return;
}
}
catch (IEX_NAMESPACE::BaseExc &e)
{
if (is) delete is;
if (_data) delete _data;
REPLACE_EXC (e, "Cannot read image file "
"\"" << fileName << "\". " << e.what());
throw;
}
//
// not multiPart - allocate stream data and intialise as normal
//
try
{
_data->_streamData = new InputStreamMutex();
_data->_streamData->is = is;
_data->memoryMapped = is->isMemoryMapped();
_data->header.readFrom (*_data->_streamData->is, _data->version);
_data->header.sanityCheck (isTiled (_data->version));
initialize(_data->header);
readLineOffsets (*_data->_streamData->is,
_data->lineOrder,
_data->lineOffsets,