Coverage Report

Created: 2023-12-08 06:53

/src/freeimage-svn/FreeImage/trunk/Source/OpenEXR/IlmImf/ImfOutputFile.cpp
Line
Count
Source (jump to first uncovered line)
1
//
2
///\todo: version needs fixing!
3
//
4
5
///////////////////////////////////////////////////////////////////////////
6
//
7
// Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
8
// Digital Ltd. LLC
9
// 
10
// All rights reserved.
11
// 
12
// Redistribution and use in source and binary forms, with or without
13
// modification, are permitted provided that the following conditions are
14
// met:
15
// *       Redistributions of source code must retain the above copyright
16
// notice, this list of conditions and the following disclaimer.
17
// *       Redistributions in binary form must reproduce the above
18
// copyright notice, this list of conditions and the following disclaimer
19
// in the documentation and/or other materials provided with the
20
// distribution.
21
// *       Neither the name of Industrial Light & Magic nor the names of
22
// its contributors may be used to endorse or promote products derived
23
// from this software without specific prior written permission. 
24
// 
25
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
//
37
///////////////////////////////////////////////////////////////////////////
38
39
40
//-----------------------------------------------------------------------------
41
//
42
//  class OutputFile
43
//
44
//-----------------------------------------------------------------------------
45
46
#include <ImfOutputFile.h>
47
#include <ImfInputFile.h>
48
#include <ImfChannelList.h>
49
#include <ImfMisc.h>
50
#include <ImfStdIO.h>
51
#include <ImfCompressor.h>
52
#include "ImathBox.h"
53
#include "ImathFun.h"
54
#include <ImfArray.h>
55
#include "ImfXdr.h"
56
#include <ImfPreviewImageAttribute.h>
57
#include <ImfPartType.h>
58
#include "IlmThreadPool.h"
59
#include "ImfOutputStreamMutex.h"
60
#include "IlmThreadSemaphore.h"
61
#include "IlmThreadMutex.h"
62
#include "Iex.h"
63
#include "ImfInputPart.h"
64
#include "ImfNamespace.h"
65
#include "ImfOutputPartData.h"
66
67
#include <string>
68
#include <vector>
69
#include <fstream>
70
#include <assert.h>
71
#include <algorithm>
72
73
OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
74
75
using IMATH_NAMESPACE::Box2i;
76
using IMATH_NAMESPACE::divp;
77
using IMATH_NAMESPACE::modp;
78
using std::string;
79
using std::vector;
80
using std::ofstream;
81
using std::min;
82
using std::max;
83
using ILMTHREAD_NAMESPACE::Mutex;
84
using ILMTHREAD_NAMESPACE::Lock;
85
using ILMTHREAD_NAMESPACE::Semaphore;
86
using ILMTHREAD_NAMESPACE::Task;
87
using ILMTHREAD_NAMESPACE::TaskGroup;
88
using ILMTHREAD_NAMESPACE::ThreadPool;
89
90
91
namespace {
92
93
94
struct OutSliceInfo
95
{
96
    PixelType   type;
97
    const char *  base;
98
    size_t    xStride;
99
    size_t    yStride;
100
    int     xSampling;
101
    int     ySampling;
102
    bool    zero;
103
104
    OutSliceInfo (PixelType type = HALF,
105
            const char *base = 0,
106
            size_t xStride = 0,
107
            size_t yStride = 0,
108
            int xSampling = 1,
109
            int ySampling = 1,
110
            bool zero = false);
111
                  
112
};
113
114
115
OutSliceInfo::OutSliceInfo (PixelType t,
116
                const char *b,
117
                size_t xs, size_t ys,
118
                int xsm, int ysm,
119
                bool z)
120
:
121
    type (t),
122
    base (b),
123
    xStride (xs),
124
    yStride (ys),
125
    xSampling (xsm),
126
    ySampling (ysm),
127
    zero (z)
128
0
{
129
    // empty
130
0
}
131
132
133
struct LineBuffer
134
{
135
    Array<char>   buffer;
136
    const char *  dataPtr;
137
    int     dataSize;
138
    char *    endOfLineBufferData;
139
    int     minY;
140
    int     maxY;
141
    int     scanLineMin;
142
    int     scanLineMax;
143
    Compressor *  compressor;
144
    bool    partiallyFull;        // has incomplete data
145
    bool    hasException;
146
    string    exception;
147
148
    LineBuffer (Compressor *comp);
149
    ~LineBuffer ();
150
151
0
    void    wait () {_sem.wait();}
152
0
    void    post () {_sem.post();}
153
154
  private:
155
156
    Semaphore   _sem;
157
};
158
159
160
LineBuffer::LineBuffer (Compressor *comp) :
161
    dataPtr (0),
162
    dataSize (0),
163
    compressor (comp),
164
    partiallyFull (false),
165
    hasException (false),
166
    exception (),
167
    _sem (1)
168
0
{
169
    // empty
170
0
}
171
172
173
LineBuffer::~LineBuffer ()
174
0
{
175
0
    delete compressor;
176
0
}
177
178
} // namespace
179
180
struct OutputFile::Data
181
{
182
    Header     header;    // the image header
183
    bool                 multiPart;   // is the file multipart?
184
    int      version;               // version attribute \todo NOT BEING WRITTEN PROPERLY
185
    Int64    previewPosition;       // file position for preview
186
    FrameBuffer    frameBuffer;           // framebuffer to write into
187
    int      currentScanLine;       // next scanline to be written
188
    int      missingScanLines;      // number of lines to write
189
    LineOrder    lineOrder;   // the file's lineorder
190
    int      minX;      // data window's min x coord
191
    int      maxX;      // data window's max x coord
192
    int      minY;      // data window's min y coord
193
    int      maxY;      // data window's max x coord
194
    vector<Int64>  lineOffsets;   // stores offsets in file for
195
            // each scanline
196
    vector<size_t>   bytesPerLine;          // combined size of a line over
197
                                                // all channels
198
    vector<size_t>   offsetInLineBuffer;    // offset for each scanline in
199
                                                // its linebuffer
200
    Compressor::Format   format;                // compressor's data format
201
    vector<OutSliceInfo> slices;    // info about channels in file
202
    Int64    lineOffsetsPosition;   // file position for line
203
                                                // offset table
204
205
    vector<LineBuffer*>  lineBuffers;           // each holds one line buffer
206
    int      linesInBuffer;         // number of scanlines each
207
                                                // buffer holds
208
    size_t     lineBufferSize;        // size of the line buffer
209
210
    int                  partNumber;            // the output part number
211
    OutputStreamMutex *  _streamData;         
212
    bool                 _deleteStream;
213
     Data (int numThreads);
214
    ~Data ();
215
216
217
    inline LineBuffer * getLineBuffer (int number); // hash function from line
218
                    // buffer indices into our
219
                // vector of line buffers
220
};
221
222
223
OutputFile::Data::Data (int numThreads):
224
    lineOffsetsPosition (0),
225
    partNumber (-1),
226
    _streamData(0),
227
    _deleteStream(false)
228
0
{
229
    //
230
    // We need at least one lineBuffer, but if threading is used,
231
    // to keep n threads busy we need 2*n lineBuffers.
232
    //
233
234
0
    lineBuffers.resize (max (1, 2 * numThreads));
235
0
}
236
237
238
OutputFile::Data::~Data ()
239
0
{
240
0
    for (size_t i = 0; i < lineBuffers.size(); i++)
241
0
        delete lineBuffers[i];
242
0
}
243
244
245
LineBuffer*
246
OutputFile::Data::getLineBuffer (int number)
247
0
{
248
0
    return lineBuffers[number % lineBuffers.size()];
249
0
}
250
251
namespace {
252
253
Int64
254
writeLineOffsets (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os, const vector<Int64> &lineOffsets)
255
0
{
256
0
    Int64 pos = os.tellp();
257
258
0
    if (pos == -1)
259
0
  IEX_NAMESPACE::throwErrnoExc ("Cannot determine current file position (%T).");
260
    
261
0
    for (unsigned int i = 0; i < lineOffsets.size(); i++)
262
0
  Xdr::write<StreamIO> (os, lineOffsets[i]);
263
264
0
    return pos;
265
0
}
266
267
268
void
269
writePixelData (OutputStreamMutex *filedata,
270
                OutputFile::Data *partdata,
271
                int lineBufferMinY,
272
                const char pixelData[],
273
                int pixelDataSize)
274
0
{
275
    //
276
    // Store a block of pixel data in the output file, and try
277
    // to keep track of the current writing position the file
278
    // without calling tellp() (tellp() can be fairly expensive).
279
    //
280
281
0
    Int64 currentPosition = filedata->currentPosition;
282
0
    filedata->currentPosition = 0;
283
284
0
    if (currentPosition == 0)
285
0
        currentPosition = filedata->os->tellp();
286
287
0
    partdata->lineOffsets[(partdata->currentScanLine - partdata->minY) / partdata->linesInBuffer] =
288
0
        currentPosition;
289
290
    #ifdef DEBUG
291
292
        assert (filedata->os->tellp() == currentPosition);
293
294
    #endif
295
    
296
  
297
  
298
0
    if (partdata->multiPart)
299
0
    {
300
0
        OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (*filedata->os, partdata->partNumber);
301
0
    }
302
    
303
0
    OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (*filedata->os, lineBufferMinY);
304
0
    OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (*filedata->os, pixelDataSize);
305
0
    filedata->os->write (pixelData, pixelDataSize);
306
307
0
    filedata->currentPosition = currentPosition +
308
0
                           Xdr::size<int>() +
309
0
                           Xdr::size<int>() +
310
0
                           pixelDataSize;
311
312
0
    if (partdata->multiPart)
313
0
    {
314
0
        filedata->currentPosition += Xdr::size<int>();
315
0
    }
316
0
}
317
318
319
inline void
320
writePixelData (OutputStreamMutex* filedata,
321
                OutputFile::Data *partdata,
322
                const LineBuffer *lineBuffer)
323
0
{
324
0
    writePixelData (filedata, partdata,
325
0
                    lineBuffer->minY,
326
0
                    lineBuffer->dataPtr,
327
0
                    lineBuffer->dataSize);
328
0
}
329
330
331
void
332
convertToXdr (OutputFile::Data *ofd,
333
              Array<char> &lineBuffer,
334
              int lineBufferMinY,
335
              int lineBufferMaxY,
336
              int inSize)
337
0
{
338
    //
339
    // Convert the contents of a lineBuffer from the machine's native
340
    // representation to Xdr format.  This function is called by
341
    // CompressLineBuffer::execute(), below, if the compressor wanted
342
    // its input pixel data in the machine's native format, but then
343
    // failed to compress the data (most compressors will expand rather
344
    // than compress random input data).
345
    //
346
    // Note that this routine assumes that the machine's native
347
    // representation of the pixel data has the same size as the
348
    // Xdr representation.  This makes it possible to convert the
349
    // pixel data in place, without an intermediate temporary buffer.
350
    //
351
   
352
    //
353
    // Iterate over all scanlines in the lineBuffer to convert.
354
    //
355
356
0
    char *writePtr = &lineBuffer[0];
357
0
    for (int y = lineBufferMinY; y <= lineBufferMaxY; y++)
358
0
    {
359
  //
360
        // Set these to point to the start of line y.
361
        // We will write to writePtr from readPtr.
362
  //
363
364
0
        const char *readPtr = writePtr;
365
        
366
  //
367
        // Iterate over all slices in the file.
368
  //
369
  
370
0
        for (unsigned int i = 0; i < ofd->slices.size(); ++i)
371
0
        {
372
            //
373
            // Test if scan line y of this channel is
374
            // contains any data (the scan line contains
375
            // data only if y % ySampling == 0).
376
            //
377
378
0
            const OutSliceInfo &slice = ofd->slices[i];
379
380
0
            if (modp (y, slice.ySampling) != 0)
381
0
                continue;
382
383
            //
384
            // Find the number of sampled pixels, dMaxX-dMinX+1, for
385
      // slice i in scan line y (i.e. pixels within the data window
386
            // for which x % xSampling == 0).
387
            //
388
389
0
            int dMinX = divp (ofd->minX, slice.xSampling);
390
0
            int dMaxX = divp (ofd->maxX, slice.xSampling);
391
            
392
      //
393
            // Convert the samples in place.
394
      //
395
            
396
0
            convertInPlace (writePtr, readPtr, slice.type, dMaxX - dMinX + 1);
397
0
        }
398
0
    }
399
0
}
400
401
402
//
403
// A LineBufferTask encapsulates the task of copying a set of scanlines
404
// from the user's frame buffer into a LineBuffer object, compressing
405
// the data if necessary.
406
//
407
408
class LineBufferTask: public Task
409
{
410
  public:
411
412
    LineBufferTask (TaskGroup *group,
413
                    OutputFile::Data *ofd,
414
        int number,
415
                    int scanLineMin,
416
        int scanLineMax);
417
418
    virtual ~LineBufferTask (); 
419
420
    virtual void  execute ();
421
422
  private:
423
424
    OutputFile::Data *  _ofd;
425
    LineBuffer *  _lineBuffer;
426
};
427
428
429
LineBufferTask::LineBufferTask
430
    (TaskGroup *group,
431
     OutputFile::Data *ofd,
432
     int number,
433
     int scanLineMin,
434
     int scanLineMax)
435
:
436
    Task (group),
437
    _ofd (ofd),
438
    _lineBuffer (_ofd->getLineBuffer(number))
439
0
{
440
    //
441
    // Wait for the lineBuffer to become available
442
    //
443
444
0
    _lineBuffer->wait ();
445
    
446
    //
447
    // Initialize the lineBuffer data if necessary
448
    //
449
450
0
    if (!_lineBuffer->partiallyFull)
451
0
    {
452
0
        _lineBuffer->endOfLineBufferData = _lineBuffer->buffer;
453
454
0
        _lineBuffer->minY = _ofd->minY + number * _ofd->linesInBuffer;
455
456
0
        _lineBuffer->maxY = min (_lineBuffer->minY + _ofd->linesInBuffer - 1,
457
0
         _ofd->maxY);
458
459
0
        _lineBuffer->partiallyFull = true;
460
0
    }
461
    
462
0
    _lineBuffer->scanLineMin = max (_lineBuffer->minY, scanLineMin);
463
0
    _lineBuffer->scanLineMax = min (_lineBuffer->maxY, scanLineMax);
464
0
}
465
466
467
LineBufferTask::~LineBufferTask ()
468
0
{
469
    //
470
    // Signal that the line buffer is now free
471
    //
472
473
0
    _lineBuffer->post ();
474
0
}
475
476
477
void
478
LineBufferTask::execute ()
479
0
{
480
0
    try
481
0
    {
482
        //
483
        // First copy the pixel data from the
484
  // frame buffer into the line buffer
485
        //
486
        
487
0
        int yStart, yStop, dy;
488
489
0
        if (_ofd->lineOrder == INCREASING_Y)
490
0
        {
491
0
            yStart = _lineBuffer->scanLineMin;
492
0
            yStop = _lineBuffer->scanLineMax + 1;
493
0
            dy = 1;
494
0
        }
495
0
        else
496
0
        {
497
0
            yStart = _lineBuffer->scanLineMax;
498
0
            yStop = _lineBuffer->scanLineMin - 1;
499
0
            dy = -1;
500
0
        }
501
    
502
0
  int y;
503
504
0
        for (y = yStart; y != yStop; y += dy)
505
0
        {
506
            //
507
            // Gather one scan line's worth of pixel data and store
508
            // them in _ofd->lineBuffer.
509
            //
510
        
511
0
            char *writePtr = _lineBuffer->buffer +
512
0
                             _ofd->offsetInLineBuffer[y - _ofd->minY];
513
            //
514
            // Iterate over all image channels.
515
            //
516
517
0
            for (unsigned int i = 0; i < _ofd->slices.size(); ++i)
518
0
            {
519
                //
520
                // Test if scan line y of this channel contains any data
521
    // (the scan line contains data only if y % ySampling == 0).
522
                //
523
        
524
0
                const OutSliceInfo &slice = _ofd->slices[i];
525
        
526
0
                if (modp (y, slice.ySampling) != 0)
527
0
                    continue;
528
        
529
                //
530
                // Find the x coordinates of the leftmost and rightmost
531
                // sampled pixels (i.e. pixels within the data window
532
                // for which x % xSampling == 0).
533
                //
534
        
535
0
                int dMinX = divp (_ofd->minX, slice.xSampling);
536
0
                int dMaxX = divp (_ofd->maxX, slice.xSampling);
537
        
538
                //
539
    // Fill the line buffer with with pixel data.
540
                //
541
        
542
0
                if (slice.zero)
543
0
                {
544
                    //
545
                    // The frame buffer contains no data for this channel.
546
                    // Store zeroes in _lineBuffer->buffer.
547
                    //
548
                    
549
0
                    fillChannelWithZeroes (writePtr, _ofd->format, slice.type,
550
0
                                           dMaxX - dMinX + 1);
551
0
                }
552
0
                else
553
0
                {
554
                    //
555
                    // If necessary, convert the pixel data to Xdr format.
556
        // Then store the pixel data in _ofd->lineBuffer.
557
                    //
558
        
559
0
                    const char *linePtr = slice.base +
560
0
                                          divp (y, slice.ySampling) *
561
0
                                          slice.yStride;
562
        
563
0
                    const char *readPtr = linePtr + dMinX * slice.xStride;
564
0
                    const char *endPtr  = linePtr + dMaxX * slice.xStride;
565
    
566
0
                    copyFromFrameBuffer (writePtr, readPtr, endPtr,
567
0
                                         slice.xStride, _ofd->format,
568
0
                                         slice.type);
569
0
                }
570
0
            }
571
        
572
0
            if (_lineBuffer->endOfLineBufferData < writePtr)
573
0
                _lineBuffer->endOfLineBufferData = writePtr;
574
        
575
            #ifdef DEBUG
576
        
577
                assert (writePtr - (_lineBuffer->buffer +
578
                        _ofd->offsetInLineBuffer[y - _ofd->minY]) ==
579
                        (int) _ofd->bytesPerLine[y - _ofd->minY]);
580
        
581
            #endif
582
        
583
0
        }
584
    
585
        //
586
        // If the next scanline isn't past the bounds of the lineBuffer
587
        // then we are done, otherwise compress the linebuffer
588
        //
589
    
590
0
        if (y >= _lineBuffer->minY && y <= _lineBuffer->maxY)
591
0
            return;
592
    
593
0
        _lineBuffer->dataPtr = _lineBuffer->buffer;
594
595
0
        _lineBuffer->dataSize = _lineBuffer->endOfLineBufferData -
596
0
                                _lineBuffer->buffer;
597
    
598
  //
599
        // Compress the data
600
  //
601
602
0
        Compressor *compressor = _lineBuffer->compressor;
603
604
0
        if (compressor)
605
0
        {
606
0
            const char *compPtr;
607
608
0
            int compSize = compressor->compress (_lineBuffer->dataPtr,
609
0
                                                 _lineBuffer->dataSize,
610
0
                                                 _lineBuffer->minY, compPtr);
611
    
612
0
            if (compSize < _lineBuffer->dataSize)
613
0
            {
614
0
                _lineBuffer->dataSize = compSize;
615
0
                _lineBuffer->dataPtr = compPtr;
616
0
            }
617
0
            else if (_ofd->format == Compressor::NATIVE)
618
0
            {
619
                //
620
                // The data did not shrink during compression, but
621
                // we cannot write to the file using the machine's
622
                // native format, so we need to convert the lineBuffer
623
                // to Xdr.
624
                //
625
    
626
0
                convertToXdr (_ofd, _lineBuffer->buffer, _lineBuffer->minY,
627
0
                              _lineBuffer->maxY, _lineBuffer->dataSize);
628
0
            }
629
0
        }
630
631
0
        _lineBuffer->partiallyFull = false;
632
0
    }
633
0
    catch (std::exception &e)
634
0
    {
635
0
        if (!_lineBuffer->hasException)
636
0
        {
637
0
            _lineBuffer->exception = e.what ();
638
0
            _lineBuffer->hasException = true;
639
0
        }
640
0
    }
641
0
    catch (...)
642
0
    {
643
0
        if (!_lineBuffer->hasException)
644
0
        {
645
0
            _lineBuffer->exception = "unrecognized exception";
646
0
            _lineBuffer->hasException = true;
647
0
        }
648
0
    }
649
0
}
650
651
} // namespace
652
653
654
OutputFile::OutputFile
655
    (const char fileName[],
656
     const Header &header,
657
     int numThreads)
658
:
659
    _data (new Data (numThreads))
660
    
661
0
{
662
0
    _data->_streamData=new OutputStreamMutex ();
663
0
    _data->_deleteStream=true;
664
0
    try
665
0
    {
666
0
  header.sanityCheck();
667
0
  _data->_streamData->os = new StdOFStream (fileName);
668
0
        _data->multiPart=false; // only one header, not multipart
669
0
  initialize (header);
670
0
  _data->_streamData->currentPosition = _data->_streamData->os->tellp();
671
        
672
  // Write header and empty offset table to the file.
673
0
  writeMagicNumberAndVersionField(*_data->_streamData->os, _data->header);
674
0
  _data->previewPosition =
675
0
          _data->header.writeTo (*_data->_streamData->os);
676
0
        _data->lineOffsetsPosition =
677
0
                writeLineOffsets (*_data->_streamData->os,_data->lineOffsets);
678
0
    }
679
0
    catch (IEX_NAMESPACE::BaseExc &e)
680
0
    {
681
0
        if (_data && _data->_streamData) delete _data->_streamData;
682
0
  if (_data)       delete _data;
683
684
0
  REPLACE_EXC (e, "Cannot open image file "
685
0
      "\"" << fileName << "\". " << e);
686
0
  throw;
687
0
    }
688
0
    catch (...)
689
0
    {
690
0
        if (_data && _data->_streamData) delete _data->_streamData;
691
0
        if (_data)       delete _data;
692
693
0
        throw;
694
0
    }
695
0
}
696
697
698
OutputFile::OutputFile
699
    (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os,
700
     const Header &header,
701
     int numThreads)
702
:
703
    _data (new Data (numThreads))
704
0
{
705
    
706
0
    _data->_streamData=new OutputStreamMutex ();
707
0
    _data->_deleteStream=false;
708
0
    try
709
0
    {
710
0
  header.sanityCheck();
711
0
  _data->_streamData->os = &os;
712
0
        _data->multiPart=false;
713
0
  initialize (header);
714
0
  _data->_streamData->currentPosition = _data->_streamData->os->tellp();
715
716
  // Write header and empty offset table to the file.
717
0
  writeMagicNumberAndVersionField(*_data->_streamData->os, _data->header);
718
0
  _data->previewPosition =
719
0
          _data->header.writeTo (*_data->_streamData->os);
720
0
        _data->lineOffsetsPosition =
721
0
                writeLineOffsets (*_data->_streamData->os, _data->lineOffsets);
722
0
    }
723
0
    catch (IEX_NAMESPACE::BaseExc &e)
724
0
    {
725
0
        if (_data && _data->_streamData) delete _data->_streamData;
726
0
  if (_data)       delete _data;
727
728
0
  REPLACE_EXC (e, "Cannot open image file "
729
0
      "\"" << os.fileName() << "\". " << e);
730
0
  throw;
731
0
    }
732
0
    catch (...)
733
0
    {
734
0
        if (_data && _data->_streamData) delete _data->_streamData;
735
0
        if (_data)       delete _data;
736
737
0
        throw;
738
0
    }
739
0
}
740
741
OutputFile::OutputFile(const OutputPartData* part) : _data(NULL)
742
0
{
743
0
    try
744
0
    {
745
0
        if (part->header.type() != SCANLINEIMAGE)
746
0
            throw IEX_NAMESPACE::ArgExc("Can't build a OutputFile from a type-mismatched part.");
747
748
0
        _data = new Data (part->numThreads);
749
0
        _data->_streamData = part->mutex;
750
0
        _data->_deleteStream=false;
751
0
        _data->multiPart=part->multipart;
752
753
0
        initialize (part->header);
754
0
        _data->partNumber = part->partNumber;
755
0
        _data->lineOffsetsPosition = part->chunkOffsetTablePosition;
756
0
        _data->previewPosition = part->previewPosition;
757
0
    }
758
0
    catch (IEX_NAMESPACE::BaseExc &e)
759
0
    {
760
0
        if (_data) delete _data;
761
762
0
        REPLACE_EXC (e, "Cannot initialize output part "
763
0
                        "\"" << part->partNumber << "\". " << e);
764
0
        throw;
765
0
    }
766
0
    catch (...)
767
0
    {
768
0
        if (_data) delete _data;
769
770
0
        throw;
771
0
    }
772
0
}
773
774
void
775
OutputFile::initialize (const Header &header)
776
0
{
777
0
    _data->header = header;
778
779
    // "fix" the type if it happens to be set incorrectly
780
    // (attribute is optional, but ensure it is correct if it exists)
781
0
    if(_data->header.hasType())
782
0
    {
783
0
        _data->header.setType(SCANLINEIMAGE);
784
0
    }
785
    
786
0
    const Box2i &dataWindow = header.dataWindow();
787
788
0
    _data->currentScanLine = (header.lineOrder() == INCREASING_Y)?
789
0
         dataWindow.min.y: dataWindow.max.y;
790
791
0
    _data->missingScanLines = dataWindow.max.y - dataWindow.min.y + 1;
792
0
    _data->lineOrder = header.lineOrder();
793
0
    _data->minX = dataWindow.min.x;
794
0
    _data->maxX = dataWindow.max.x;
795
0
    _data->minY = dataWindow.min.y;
796
0
    _data->maxY = dataWindow.max.y;
797
798
0
    size_t maxBytesPerLine = bytesPerLineTable (_data->header,
799
0
            _data->bytesPerLine);
800
801
0
    for (size_t i = 0; i < _data->lineBuffers.size(); ++i)
802
0
    {
803
0
        _data->lineBuffers[i] =
804
0
      new LineBuffer (newCompressor (_data->header.compression(),
805
0
             maxBytesPerLine,
806
0
             _data->header));
807
0
    }
808
809
0
    LineBuffer *lineBuffer = _data->lineBuffers[0];
810
0
    _data->format = defaultFormat (lineBuffer->compressor);
811
0
    _data->linesInBuffer = numLinesInBuffer (lineBuffer->compressor);
812
0
    _data->lineBufferSize = maxBytesPerLine * _data->linesInBuffer;
813
814
0
    for (size_t i = 0; i < _data->lineBuffers.size(); i++)
815
0
        _data->lineBuffers[i]->buffer.resizeErase(_data->lineBufferSize);
816
817
0
    int lineOffsetSize = (dataWindow.max.y - dataWindow.min.y +
818
0
        _data->linesInBuffer) / _data->linesInBuffer;
819
820
0
    _data->lineOffsets.resize (lineOffsetSize);
821
    
822
    
823
0
    offsetInLineBufferTable (_data->bytesPerLine,
824
0
           _data->linesInBuffer,
825
0
           _data->offsetInLineBuffer);
826
0
}
827
828
829
OutputFile::~OutputFile ()
830
0
{
831
0
    if (_data)
832
0
    {
833
0
        {
834
0
            Lock lock(*_data->_streamData);
835
0
            Int64 originalPosition = _data->_streamData->os->tellp();
836
837
0
            if (_data->lineOffsetsPosition > 0)
838
0
            {
839
0
                try
840
0
                {
841
0
                    _data->_streamData->os->seekp (_data->lineOffsetsPosition);
842
0
                    writeLineOffsets (*_data->_streamData->os, _data->lineOffsets);
843
844
                    //
845
                    // Restore the original position.
846
                    //
847
0
                    _data->_streamData->os->seekp (originalPosition);
848
0
                }
849
0
                catch (...)
850
0
                {
851
                    //
852
                    // We cannot safely throw any exceptions from here.
853
                    // This destructor may have been called because the
854
                    // stack is currently being unwound for another
855
                    // exception.
856
                    //
857
0
                }
858
0
            }
859
0
        }
860
861
0
        if (_data->_deleteStream && _data->_streamData)
862
0
            delete _data->_streamData->os;
863
864
0
        if (_data->partNumber == -1 && _data->_streamData)
865
0
            delete _data->_streamData;
866
867
0
  delete _data;
868
0
    }
869
870
0
}
871
872
873
const char *
874
OutputFile::fileName () const
875
0
{
876
0
    return _data->_streamData->os->fileName();
877
0
}
878
879
880
const Header &
881
OutputFile::header () const
882
0
{
883
0
    return _data->header;
884
0
}
885
886
887
void  
888
OutputFile::setFrameBuffer (const FrameBuffer &frameBuffer)
889
0
{
890
0
    Lock lock (*_data->_streamData);
891
    
892
    //
893
    // Check if the new frame buffer descriptor
894
    // is compatible with the image file header.
895
    //
896
897
0
    const ChannelList &channels = _data->header.channels();
898
899
0
    for (ChannelList::ConstIterator i = channels.begin();
900
0
   i != channels.end();
901
0
   ++i)
902
0
    {
903
0
  FrameBuffer::ConstIterator j = frameBuffer.find (i.name());
904
905
0
  if (j == frameBuffer.end())
906
0
      continue;
907
908
0
  if (i.channel().type != j.slice().type)
909
0
  {
910
0
      THROW (IEX_NAMESPACE::ArgExc, "Pixel type of \"" << i.name() << "\" channel "
911
0
              "of output file \"" << fileName() << "\" is "
912
0
              "not compatible with the frame buffer's "
913
0
              "pixel type.");
914
0
  }
915
916
0
  if (i.channel().xSampling != j.slice().xSampling ||
917
0
      i.channel().ySampling != j.slice().ySampling)
918
0
  {
919
0
      THROW (IEX_NAMESPACE::ArgExc, "X and/or y subsampling factors "
920
0
        "of \"" << i.name() << "\" channel "
921
0
        "of output file \"" << fileName() << "\" are "
922
0
        "not compatible with the frame buffer's "
923
0
        "subsampling factors.");
924
0
  }
925
0
    }
926
    
927
    //
928
    // Initialize slice table for writePixels().
929
    //
930
931
0
    vector<OutSliceInfo> slices;
932
933
0
    for (ChannelList::ConstIterator i = channels.begin();
934
0
   i != channels.end();
935
0
   ++i)
936
0
    {
937
0
  FrameBuffer::ConstIterator j = frameBuffer.find (i.name());
938
939
0
  if (j == frameBuffer.end())
940
0
  {
941
      //
942
      // Channel i is not present in the frame buffer.
943
      // In the file, channel i will contain only zeroes.
944
      //
945
946
0
      slices.push_back (OutSliceInfo (i.channel().type,
947
0
              0, // base
948
0
              0, // xStride,
949
0
              0, // yStride,
950
0
              i.channel().xSampling,
951
0
              i.channel().ySampling,
952
0
              true)); // zero
953
0
  }
954
0
  else
955
0
  {
956
      //
957
      // Channel i is present in the frame buffer.
958
      //
959
960
0
      slices.push_back (OutSliceInfo (j.slice().type,
961
0
              j.slice().base,
962
0
              j.slice().xStride,
963
0
              j.slice().yStride,
964
0
              j.slice().xSampling,
965
0
              j.slice().ySampling,
966
0
              false)); // zero
967
0
  }
968
0
    }
969
970
    //
971
    // Store the new frame buffer.
972
    //
973
974
0
    _data->frameBuffer = frameBuffer;
975
0
    _data->slices = slices;
976
0
}
977
978
979
const FrameBuffer &
980
OutputFile::frameBuffer () const
981
0
{
982
0
    Lock lock (*_data->_streamData);
983
0
    return _data->frameBuffer;
984
0
}
985
986
987
void  
988
OutputFile::writePixels (int numScanLines)
989
0
{
990
0
    try
991
0
    {
992
0
        Lock lock (*_data->_streamData);
993
994
0
  if (_data->slices.size() == 0)
995
0
      throw IEX_NAMESPACE::ArgExc ("No frame buffer specified "
996
0
             "as pixel data source.");
997
998
        //
999
        // Maintain two iterators:
1000
        //     nextWriteBuffer: next linebuffer to be written to the file
1001
        //     nextCompressBuffer: next linebuffer to compress
1002
        //
1003
1004
0
        int first = (_data->currentScanLine - _data->minY) /
1005
0
                         _data->linesInBuffer;
1006
1007
0
        int nextWriteBuffer = first;
1008
0
        int nextCompressBuffer;
1009
0
        int stop;
1010
0
        int step;
1011
0
        int scanLineMin;
1012
0
        int scanLineMax;
1013
1014
0
        {
1015
            //
1016
            // Create a task group for all line buffer tasks. When the
1017
            // taskgroup goes out of scope, the destructor waits until
1018
      // all tasks are complete.
1019
            //
1020
            
1021
0
            TaskGroup taskGroup;
1022
            
1023
            //
1024
            // Determine the range of lineBuffers that intersect the scan
1025
      // line range.  Then add the initial compression tasks to the
1026
      // thread pool.  We always add in at least one task but the
1027
      // individual task might not do anything if numScanLines == 0.
1028
            //
1029
    
1030
0
            if (_data->lineOrder == INCREASING_Y)
1031
0
            {
1032
0
                int last = (_data->currentScanLine + (numScanLines - 1) -
1033
0
                            _data->minY) / _data->linesInBuffer;
1034
    
1035
0
                scanLineMin = _data->currentScanLine;
1036
0
                scanLineMax = _data->currentScanLine + numScanLines - 1;
1037
    
1038
0
                int numTasks = max (min ((int)_data->lineBuffers.size(),
1039
0
                                         last - first + 1),
1040
0
            1);
1041
1042
0
                for (int i = 0; i < numTasks; i++)
1043
0
    {
1044
0
                    ThreadPool::addGlobalTask
1045
0
                        (new LineBufferTask (&taskGroup, _data, first + i,
1046
0
                                             scanLineMin, scanLineMax));
1047
0
    }
1048
    
1049
0
                nextCompressBuffer = first + numTasks;
1050
0
                stop = last + 1;
1051
0
                step = 1;
1052
0
            }
1053
0
            else
1054
0
            {
1055
0
                int last = (_data->currentScanLine - (numScanLines - 1) -
1056
0
                            _data->minY) / _data->linesInBuffer;
1057
    
1058
0
                scanLineMax = _data->currentScanLine;
1059
0
                scanLineMin = _data->currentScanLine - numScanLines + 1;
1060
    
1061
0
                int numTasks = max (min ((int)_data->lineBuffers.size(),
1062
0
                                         first - last + 1),
1063
0
            1);
1064
1065
0
                for (int i = 0; i < numTasks; i++)
1066
0
    {
1067
0
                    ThreadPool::addGlobalTask
1068
0
                        (new LineBufferTask (&taskGroup, _data, first - i,
1069
0
                                             scanLineMin, scanLineMax));
1070
0
    }
1071
    
1072
0
                nextCompressBuffer = first - numTasks;
1073
0
                stop = last - 1;
1074
0
                step = -1;
1075
0
            }
1076
            
1077
0
            while (true)
1078
0
            {
1079
0
                if (_data->missingScanLines <= 0)
1080
0
                {
1081
0
                    throw IEX_NAMESPACE::ArgExc ("Tried to write more scan lines "
1082
0
                                       "than specified by the data window.");
1083
0
                }
1084
    
1085
    //
1086
                // Wait until the next line buffer is ready to be written
1087
    //
1088
1089
0
                LineBuffer *writeBuffer =
1090
0
        _data->getLineBuffer (nextWriteBuffer);
1091
1092
0
                writeBuffer->wait();
1093
                
1094
0
                int numLines = writeBuffer->scanLineMax - 
1095
0
                               writeBuffer->scanLineMin + 1;
1096
1097
0
                _data->missingScanLines -= numLines;
1098
    
1099
    //
1100
                // If the line buffer is only partially full, then it is
1101
    // not complete and we cannot write it to disk yet.
1102
    //
1103
1104
0
                if (writeBuffer->partiallyFull)
1105
0
                {
1106
0
                    _data->currentScanLine = _data->currentScanLine +
1107
0
                                             step * numLines;
1108
0
                    writeBuffer->post();
1109
    
1110
0
                    return;
1111
0
                }
1112
    
1113
    //
1114
                // Write the line buffer
1115
    //
1116
1117
0
                writePixelData (_data->_streamData, _data, writeBuffer);
1118
0
                nextWriteBuffer += step;
1119
1120
0
                _data->currentScanLine = _data->currentScanLine +
1121
0
                                         step * numLines;
1122
    
1123
                #ifdef DEBUG
1124
    
1125
                    assert (_data->currentScanLine ==
1126
                            ((_data->lineOrder == INCREASING_Y) ?
1127
                             writeBuffer->scanLineMax + 1:
1128
                             writeBuffer->scanLineMin - 1));
1129
    
1130
                #endif
1131
                
1132
    //
1133
                // Release the lock on the line buffer
1134
    //
1135
1136
0
                writeBuffer->post();
1137
                
1138
    //
1139
                // If this was the last line buffer in the scanline range
1140
    //
1141
1142
0
                if (nextWriteBuffer == stop)
1143
0
                    break;
1144
    
1145
    //
1146
                // If there are no more line buffers to compress,
1147
                // then only continue to write out remaining lineBuffers
1148
    //
1149
1150
0
                if (nextCompressBuffer == stop)
1151
0
                    continue;
1152
    
1153
    //
1154
                // Add nextCompressBuffer as a compression task
1155
    //
1156
1157
0
                ThreadPool::addGlobalTask
1158
0
                    (new LineBufferTask (&taskGroup, _data, nextCompressBuffer,
1159
0
                                         scanLineMin, scanLineMax));
1160
                
1161
    //
1162
                // Update the next line buffer we need to compress
1163
    //
1164
1165
0
                nextCompressBuffer += step;
1166
0
            }
1167
        
1168
      //
1169
            // Finish all tasks
1170
      //
1171
0
        }
1172
        
1173
  //
1174
  // Exeption handling:
1175
  //
1176
  // LineBufferTask::execute() may have encountered exceptions, but
1177
  // those exceptions occurred in another thread, not in the thread
1178
  // that is executing this call to OutputFile::writePixels().
1179
  // LineBufferTask::execute() has caught all exceptions and stored
1180
  // the exceptions' what() strings in the line buffers.
1181
  // Now we check if any line buffer contains a stored exception; if
1182
  // this is the case then we re-throw the exception in this thread.
1183
  // (It is possible that multiple line buffers contain stored
1184
  // exceptions.  We re-throw the first exception we find and
1185
  // ignore all others.)
1186
  //
1187
1188
0
  const string *exception = 0;
1189
1190
0
        for (size_t i = 0; i < _data->lineBuffers.size(); ++i)
1191
0
  {
1192
0
            LineBuffer *lineBuffer = _data->lineBuffers[i];
1193
1194
0
      if (lineBuffer->hasException && !exception)
1195
0
    exception = &lineBuffer->exception;
1196
1197
0
      lineBuffer->hasException = false;
1198
0
  }
1199
1200
0
  if (exception)
1201
0
      throw IEX_NAMESPACE::IoExc (*exception);
1202
0
    }
1203
0
    catch (IEX_NAMESPACE::BaseExc &e)
1204
0
    {
1205
0
  REPLACE_EXC (e, "Failed to write pixel data to image "
1206
0
            "file \"" << fileName() << "\". " << e);
1207
0
  throw;
1208
0
    }
1209
0
}
1210
1211
1212
int 
1213
OutputFile::currentScanLine () const
1214
0
{
1215
0
    Lock lock (*_data->_streamData);
1216
0
    return _data->currentScanLine;
1217
0
}
1218
1219
1220
void  
1221
OutputFile::copyPixels (InputFile &in)
1222
0
{
1223
0
    Lock lock (*_data->_streamData);
1224
1225
    //
1226
    // Check if this file's and and the InputFile's
1227
    // headers are compatible.
1228
    //
1229
1230
0
    const Header &hdr = _data->header;
1231
0
    const Header &inHdr = in.header();
1232
1233
0
    if (inHdr.find("tiles") != inHdr.end())
1234
0
  THROW (IEX_NAMESPACE::ArgExc, "Cannot copy pixels from image "
1235
0
          "file \"" << in.fileName() << "\" to image "
1236
0
          "file \"" << fileName() << "\". "
1237
0
                            "The input file is tiled, but the output file is "
1238
0
                            "not. Try using TiledOutputFile::copyPixels "
1239
0
                            "instead.");
1240
1241
0
    if (!(hdr.dataWindow() == inHdr.dataWindow()))
1242
0
  THROW (IEX_NAMESPACE::ArgExc, "Cannot copy pixels from image "
1243
0
          "file \"" << in.fileName() << "\" to image "
1244
0
          "file \"" << fileName() << "\". "
1245
0
                            "The files have different data windows.");
1246
1247
0
    if (!(hdr.lineOrder() == inHdr.lineOrder()))
1248
0
  THROW (IEX_NAMESPACE::ArgExc, "Quick pixel copy from image "
1249
0
          "file \"" << in.fileName() << "\" to image "
1250
0
          "file \"" << fileName() << "\" failed. "
1251
0
          "The files have different line orders.");
1252
1253
0
    if (!(hdr.compression() == inHdr.compression()))
1254
0
  THROW (IEX_NAMESPACE::ArgExc, "Quick pixel copy from image "
1255
0
          "file \"" << in.fileName() << "\" to image "
1256
0
          "file \"" << fileName() << "\" failed. "
1257
0
          "The files use different compression methods.");
1258
1259
0
    if (!(hdr.channels() == inHdr.channels()))
1260
0
  THROW (IEX_NAMESPACE::ArgExc, "Quick pixel copy from image "
1261
0
          "file \"" << in.fileName() << "\" to image "
1262
0
          "file \"" << fileName() << "\" failed.  "
1263
0
          "The files have different channel lists.");
1264
1265
    //
1266
    // Verify that no pixel data have been written to this file yet.
1267
    //
1268
1269
0
    const Box2i &dataWindow = hdr.dataWindow();
1270
1271
0
    if (_data->missingScanLines != dataWindow.max.y - dataWindow.min.y + 1)
1272
0
  THROW (IEX_NAMESPACE::LogicExc, "Quick pixel copy from image "
1273
0
            "file \"" << in.fileName() << "\" to image "
1274
0
            "file \"" << fileName() << "\" failed. "
1275
0
            "\"" << fileName() << "\" already contains "
1276
0
            "pixel data.");
1277
1278
    //
1279
    // Copy the pixel data.
1280
    //
1281
1282
0
    while (_data->missingScanLines > 0)
1283
0
    {
1284
0
  const char *pixelData;
1285
0
  int pixelDataSize;
1286
1287
0
  in.rawPixelData (_data->currentScanLine, pixelData, pixelDataSize);
1288
1289
0
        writePixelData (_data->_streamData, _data, lineBufferMinY (_data->currentScanLine,
1290
0
                                               _data->minY,
1291
0
                                               _data->linesInBuffer),
1292
0
                        pixelData, pixelDataSize);
1293
1294
0
  _data->currentScanLine += (_data->lineOrder == INCREASING_Y)?
1295
0
           _data->linesInBuffer: -_data->linesInBuffer;
1296
1297
0
  _data->missingScanLines -= _data->linesInBuffer;
1298
0
    }
1299
0
}
1300
1301
1302
void
1303
OutputFile::copyPixels( InputPart & in)
1304
0
{
1305
0
    copyPixels(*in.file);
1306
0
}
1307
1308
1309
1310
void
1311
OutputFile::updatePreviewImage (const PreviewRgba newPixels[])
1312
0
{
1313
0
    Lock lock (*_data->_streamData);
1314
1315
0
    if (_data->previewPosition <= 0)
1316
0
  THROW (IEX_NAMESPACE::LogicExc, "Cannot update preview image pixels. "
1317
0
            "File \"" << fileName() << "\" does not "
1318
0
            "contain a preview image.");
1319
1320
    //
1321
    // Store the new pixels in the header's preview image attribute.
1322
    //
1323
1324
0
    PreviewImageAttribute &pia =
1325
0
  _data->header.typedAttribute <PreviewImageAttribute> ("preview");
1326
1327
0
    PreviewImage &pi = pia.value();
1328
0
    PreviewRgba *pixels = pi.pixels();
1329
0
    int numPixels = pi.width() * pi.height();
1330
1331
0
    for (int i = 0; i < numPixels; ++i)
1332
0
  pixels[i] = newPixels[i];
1333
1334
    //
1335
    // Save the current file position, jump to the position in
1336
    // the file where the preview image starts, store the new
1337
    // preview image, and jump back to the saved file position.
1338
    //
1339
1340
0
    Int64 savedPosition = _data->_streamData->os->tellp();
1341
1342
0
    try
1343
0
    {
1344
0
        _data->_streamData->os->seekp (_data->previewPosition);
1345
0
  pia.writeValueTo (*_data->_streamData->os, _data->version);
1346
0
  _data->_streamData->os->seekp (savedPosition);
1347
0
    }
1348
0
    catch (IEX_NAMESPACE::BaseExc &e)
1349
0
    {
1350
0
  REPLACE_EXC (e, "Cannot update preview image pixels for "
1351
0
      "file \"" << fileName() << "\". " << e);
1352
0
  throw;
1353
0
    }
1354
0
}
1355
1356
1357
void  
1358
OutputFile::breakScanLine  (int y, int offset, int length, char c)
1359
0
{
1360
0
    Lock lock (*_data->_streamData);
1361
1362
0
    Int64 position = 
1363
0
  _data->lineOffsets[(y - _data->minY) / _data->linesInBuffer];
1364
1365
0
    if (!position)
1366
0
  THROW (IEX_NAMESPACE::ArgExc, "Cannot overwrite scan line " << y << ". "
1367
0
          "The scan line has not yet been stored in "
1368
0
          "file \"" << fileName() << "\".");
1369
1370
0
    _data->_streamData->currentPosition = 0;
1371
0
    _data->_streamData->os->seekp (position + offset);
1372
1373
0
    for (int i = 0; i < length; ++i)
1374
0
        _data->_streamData->os->write (&c, 1);
1375
0
}
1376
1377
1378
OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT