File: ImfMultiPartOutputFile.cpp

package info (click to toggle)
povray 1%3A3.7.0.10-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 147,232 kB
  • sloc: cpp: 845,011; ansic: 122,118; sh: 34,204; pascal: 6,420; asm: 3,355; ada: 1,681; makefile: 1,388; cs: 879; awk: 590; perl: 245; xml: 95
file content (519 lines) | stat: -rw-r--r-- 16,799 bytes parent folder | download | duplicates (7)
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
///////////////////////////////////////////////////////////////////////////
//
// 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.
//
///////////////////////////////////////////////////////////////////////////

#include "ImfMultiPartOutputFile.h"
#include "ImfBoxAttribute.h"
#include "ImfFloatAttribute.h"
#include "ImfTimeCodeAttribute.h"
#include "ImfChromaticitiesAttribute.h"
#include "ImfOutputPartData.h"
#include "ImfPartType.h"
#include "ImfOutputFile.h"
#include "ImfTiledOutputFile.h"
#include "ImfThreading.h"
#include "IlmThreadMutex.h"
#include "ImfMisc.h"
#include "ImfStdIO.h"
#include "ImfDeepScanLineOutputFile.h"
#include "ImfDeepTiledOutputFile.h"
#include "ImfOutputStreamMutex.h"

#include "ImfNamespace.h"
#include <Iex.h>


#include <set>


OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER

using IMATH_NAMESPACE::Box2i;
using ILMTHREAD_NAMESPACE::Lock;
    

using std::vector;
using std::map;
using std::set;


struct MultiPartOutputFile::Data: public OutputStreamMutex
{
        vector<OutputPartData*>         parts;        // Contains data to initialize Output files.
        bool                            deleteStream; // If we should delete the stream when destruction.
        int                             numThreads;   // The number of threads.
        std::map<int, GenericOutputFile*>    _outputFiles;
        std::vector<Header>                  _headers;
        
        
        void                    headerNameUniquenessCheck (const std::vector<Header> &headers);
        
        void                    writeHeadersToFile (const std::vector<Header> &headers); 
        
        void                    writeChunkTableOffsets (std::vector<OutputPartData*> &parts);
        
        
        //-------------------------------------
        // ensure that _headers is valid: called by constructors
        //-------------------------------------
        void                    do_header_sanity_checks(bool overrideSharedAttributes);
        
        // ------------------------------------------------
        // Given a source header, we copy over all the 'shared attributes' to
        // the destination header and remove any conflicting ones.
        // ------------------------------------------------
        void                    overrideSharedAttributesValues (const Header & src,
                                                                Header & dst);
                                                                
        // ------------------------------------------------
        // Given a source header, we check the destination header for any
        // attributes that are part of the shared attribute set. For attributes
        // present in both we check the values. For attribute present in 
        // destination but absent in source we return false.
        // For attributes present in src but missing from dst we return false
        // and add the attribute to dst.
        // We return false for all other cases.
        // If we return true then we also populate the conflictingAttributes
        // vector with the names of the attributes that failed the above.
        // ------------------------------------------------
        bool                    checkSharedAttributesValues (const Header & src,
                                                             const Header & dst, 
                                                             std::vector<std::string> & conflictingAttributes) const;
        Data (bool deleteStream, int numThreads):
            OutputStreamMutex(),
            deleteStream (deleteStream),
            numThreads (numThreads)
        {
        }
        

        ~Data()
        {
            if (deleteStream) delete os;

            for (size_t i = 0; i < parts.size(); i++)
                delete parts[i];
        }
};

void
MultiPartOutputFile::Data::do_header_sanity_checks(bool overrideSharedAttributes)
{
    size_t parts = _headers.size();
    if (parts == 0) 
        throw IEX_NAMESPACE::ArgExc ("Empty header list.");
    
    bool isMultiPart = (parts > 1); 
    
    //
    // Do part 0 checks first.
    //
    
    _headers[0].sanityCheck (_headers[0].hasTileDescription(), isMultiPart);
        
    
    if (isMultiPart)
    {
        // multipart files must contain a chunkCount attribute
        _headers[0].setChunkCount(getChunkOffsetTableSize(_headers[0],true));
        
        for (size_t i = 1; i < parts; i++)
        {
            if (_headers[i].hasType() == false)
                throw IEX_NAMESPACE::ArgExc ("Every header in a multipart file should have a type");
            
            
            _headers[i].setChunkCount(getChunkOffsetTableSize(_headers[i],true));
            _headers[i].sanityCheck (_headers[i].hasTileDescription(), isMultiPart);
            
            
            if (overrideSharedAttributes)
                overrideSharedAttributesValues(_headers[0],_headers[i]);
            else
            {
                std::vector<std::string> conflictingAttributes;
                bool valid =checkSharedAttributesValues (_headers[0],
                                                         _headers[i], 
                                                         conflictingAttributes);
                if (valid)
                {
                    string excMsg("Conflicting attributes found for header :: ");
                    excMsg += _headers[i].name();
                    for (size_t i=0; i<conflictingAttributes.size(); i++)
                        excMsg += " '" + conflictingAttributes[i] + "' ";
                                                             
                    THROW (IEX_NAMESPACE::ArgExc, excMsg);
                }
            }
        }
        
        headerNameUniquenessCheck(_headers);
        
    }else{
        
        // add chunk count offset to single part data (if not an image)
        
        if (_headers[0].hasType() && isImage(_headers[0].type()) == false)
        {
            _headers[0].setChunkCount(getChunkOffsetTableSize(_headers[0],true));
        }
        
    }
}

    
MultiPartOutputFile::MultiPartOutputFile (const char fileName[],
                                          const Header * headers,
                                          int parts,
                                          bool overrideSharedAttributes,
                                          int numThreads)
:
    _data (new Data (true, numThreads))
{
    // grab headers
    _data->_headers.resize(parts);
    
    for(int i=0;i<parts;i++)
    {
       _data->_headers[i]=headers[i];
    }
    try
    {
  
         _data->do_header_sanity_checks(overrideSharedAttributes);

        //
        // Build parts and write headers and offset tables to file.
        //

        _data->os = new StdOFStream (fileName);
        for (size_t i = 0; i < _data->_headers.size(); i++)
            _data->parts.push_back( new OutputPartData(_data, _data->_headers[i], i, numThreads, parts>1 ) );

        writeMagicNumberAndVersionField(*_data->os, &_data->_headers[0],_data->_headers.size());
        _data->writeHeadersToFile(_data->_headers);
        _data->writeChunkTableOffsets(_data->parts);
    }
    catch (IEX_NAMESPACE::BaseExc &e)
    {
        delete _data;

        REPLACE_EXC (e, "Cannot open image file "
                        "\"" << fileName << "\". " << e);
        throw;
    }
    catch (...)
    {
        delete _data;
        throw;
    }
}

MultiPartOutputFile::MultiPartOutputFile(OStream& os, 
                                         const Header* headers, 
                                         int parts, 
                                         bool overrideSharedAttributes, 
                                         int numThreads): 
                                         _data(new Data(false,numThreads))
{
    // grab headers
    _data->_headers.resize(parts);
    _data->os=&os;
    
    for(int i=0;i<parts;i++)
    {
        _data->_headers[i]=headers[i];
    }
    try
    {
        
        _data->do_header_sanity_checks(overrideSharedAttributes);
        
        //
        // Build parts and write headers and offset tables to file.
        //
        
        for (size_t i = 0; i < _data->_headers.size(); i++)
            _data->parts.push_back( new OutputPartData(_data, _data->_headers[i], i, numThreads, parts>1 ) );
        
        writeMagicNumberAndVersionField(*_data->os, &_data->_headers[0],_data->_headers.size());
        _data->writeHeadersToFile(_data->_headers);
        _data->writeChunkTableOffsets(_data->parts);
    }
    catch (IEX_NAMESPACE::BaseExc &e)
    {
        delete _data;
        
        REPLACE_EXC (e, "Cannot open image stream "
        "\"" << os.fileName() << "\". " << e);
        throw;
    }
    catch (...)
    {
        delete _data;
        throw;
    }
}


const Header &
MultiPartOutputFile::header(int n) const
{
    if(n<0 || n>int(_data->_headers.size()))
    {
        throw IEX_NAMESPACE::ArgExc("MultiPartOutputFile::header called with invalid part number");
    }
    return _data->_headers[n];
}

int
MultiPartOutputFile::parts() const
{
   return _data->_headers.size();
}


MultiPartOutputFile::~MultiPartOutputFile ()
{
    for (map<int, GenericOutputFile*>::iterator it = _data->_outputFiles.begin();
         it != _data->_outputFiles.end(); it++)
    {
        delete it->second;
    }

    delete _data;
}

template <class T>
T*
MultiPartOutputFile::getOutputPart(int partNumber)
{
    Lock lock(*_data);
    if (_data->_outputFiles.find(partNumber) == _data->_outputFiles.end())
    {
        T* file = new T(_data->parts[partNumber]);
        _data->_outputFiles.insert(std::make_pair(partNumber, (GenericOutputFile*) file));
        return file;
    }
    else return (T*) _data->_outputFiles[partNumber];
}

// instance above function for all four types
template OutputFile* MultiPartOutputFile::getOutputPart<OutputFile>(int);
template TiledOutputFile * MultiPartOutputFile::getOutputPart<TiledOutputFile>(int);
template DeepScanLineOutputFile * MultiPartOutputFile::getOutputPart<DeepScanLineOutputFile> (int);
template DeepTiledOutputFile * MultiPartOutputFile::getOutputPart<DeepTiledOutputFile> (int);



void 
MultiPartOutputFile::Data::overrideSharedAttributesValues(const Header & src, Header & dst)
{
    //
    // Display Window
    //
    const Box2iAttribute * displayWindow = 
    src.findTypedAttribute<Box2iAttribute> ("displayWindow");
    
    if (displayWindow)
        dst.insert ("displayWindow", *displayWindow);
    else 
        dst.erase ("displayWindow");
    
    
    //
    // Pixel Aspect Ratio
    //
    const FloatAttribute * pixelAspectRatio = 
    src.findTypedAttribute<FloatAttribute> ("pixelAspectRatio");
    
    if (pixelAspectRatio)
        dst.insert ("pixelAspectRatio", *pixelAspectRatio);
    else 
        dst.erase ("pixelAspectRatio");
    
    
    //
    // Timecode
    //
    const TimeCodeAttribute * timeCode = 
    src.findTypedAttribute<TimeCodeAttribute> ("timecode");
    
    if (timeCode)
        dst.insert ("timecode", *timeCode);
    else 
        dst.erase ("timecode");
    
    
    //
    // Chromaticities
    //
    const ChromaticitiesAttribute * chromaticities = 
    src.findTypedAttribute<ChromaticitiesAttribute> ("chromaticities");
    
    if (chromaticities)
        dst.insert ("chromaticities", *chromaticities);
    else 
        dst.erase ("chromaticities");
    
}


bool 
MultiPartOutputFile::Data::checkSharedAttributesValues(const Header & src,
        const Header & dst,
        vector<string> & conflictingAttributes) const
{
    bool conflict = false;

    //
    // Display Window
    //
    if (src.displayWindow() != dst.displayWindow())
    {
        conflict = true;
        conflictingAttributes.push_back ("displayWindow");
    }


    //
    // Pixel Aspect Ratio
    //
    if (src.pixelAspectRatio() != dst.pixelAspectRatio())
    {
        conflict = true;
        conflictingAttributes.push_back ("pixelAspectRatio");
    }


    //
    // Timecode
    //
    const TimeCodeAttribute * srcTimeCode = src.findTypedAttribute<
                                            TimeCodeAttribute> (TimeCodeAttribute::staticTypeName());
    const TimeCodeAttribute * dstTimeCode = dst.findTypedAttribute<
                                            TimeCodeAttribute> (TimeCodeAttribute::staticTypeName());

    if (dstTimeCode)
    {
        if ((srcTimeCode && (srcTimeCode->value() != dstTimeCode->value())) ||
                (!srcTimeCode))
        {
            conflict = true;
            conflictingAttributes.push_back (TimeCodeAttribute::staticTypeName());
        }
    }

    //
    // Chromaticities
    //
    const ChromaticitiesAttribute * srcChrom =  src.findTypedAttribute<
            ChromaticitiesAttribute> (ChromaticitiesAttribute::staticTypeName());
    const ChromaticitiesAttribute * dstChrom =  dst.findTypedAttribute<
            ChromaticitiesAttribute> (ChromaticitiesAttribute::staticTypeName());

    if (dstChrom)
    {
        if ( (srcChrom && (srcChrom->value() != dstChrom->value())) ||
                (!srcChrom))
        {
            conflict = true;
            conflictingAttributes.push_back (ChromaticitiesAttribute::staticTypeName());
        }
    }

    return conflict;
}
                                                      

void
MultiPartOutputFile::Data::headerNameUniquenessCheck (const vector<Header> &headers)
{
    set<string> names;
    for (size_t i = 0; i < headers.size(); i++)
    {
        if (names.find(headers[i].name()) != names.end())
            throw IEX_NAMESPACE::ArgExc ("Each part should have a unique name.");
        names.insert(headers[i].name());
    }
}

void
MultiPartOutputFile::Data::writeHeadersToFile (const vector<Header> &headers)
{
    for (size_t i = 0; i < headers.size(); i++)
    {

        // (TODO) consider deep files' preview images here.
        if (headers[i].type() == TILEDIMAGE)
            parts[i]->previewPosition = headers[i].writeTo(*os, true);
        else
            parts[i]->previewPosition = headers[i].writeTo(*os, false);
    }

    //
    // If a multipart file, write zero-length attribute name to mark the end of all headers.
    //

    if (headers.size() !=1)
         OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (*os, "");
}

void
MultiPartOutputFile::Data::writeChunkTableOffsets (vector<OutputPartData*> &parts)
{
    for (size_t i = 0; i < parts.size(); i++)
    {
        int chunkTableSize = getChunkOffsetTableSize(parts[i]->header,false);

        Int64 pos = os->tellp();

        if (pos == -1)
            IEX_NAMESPACE::throwErrnoExc ("Cannot determine current file position (%T).");

        parts[i]->chunkOffsetTablePosition = os->tellp();

        //
        // Fill in empty data for now. We'll write actual offsets during destruction.
        //

        for (int j = 0; j < chunkTableSize; j++)
        {
            Int64 empty = 0;
            OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (*os, empty);
        }
    }
}


OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT