File: rawfile.cpp

package info (click to toggle)
libopenraw 0.0.8-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,964 kB
  • ctags: 1,936
  • sloc: sh: 10,199; cpp: 9,279; ansic: 1,466; makefile: 544; xml: 465; perl: 42
file content (479 lines) | stat: -rw-r--r-- 13,375 bytes parent folder | download
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
/*
 * libopenraw - rawfile.cpp
 *
 * Copyright (C) 2008 Novell, Inc.
 * Copyright (C) 2006-2008, 2010 Hubert Figuiere
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */


#include <cstring>
#include <cassert>
#include <map>
#include <string>

#include <boost/algorithm/string.hpp>
#include <boost/bind.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/checked_delete.hpp>

#include "debug.h"

#include <libopenraw/metadata.h>
#include <libopenraw++/rawfile.h>
#include <libopenraw++/rawdata.h>
#include <libopenraw++/thumbnail.h>

#include "io/file.h"
#include "io/memstream.h"
#include "cr2file.h"
#include "neffile.h"
#include "orffile.h"
#include "arwfile.h"
#include "peffile.h"
#include "crwfile.h"
#include "erffile.h"
#include "dngfile.h"
#include "mrwfile.h"
#include "metavalue.h"
#include "exception.h"
#include "demosaic.h"

#include "rawfilefactory.h"

using std::string;
using namespace Debug;

namespace OpenRaw {

using Internals::RawFileFactory;

void init(void)
{
    static RawFileFactory fctcr2(OR_RAWFILE_TYPE_CR2, 
                                 boost::bind(&Internals::Cr2File::factory, _1),
                                 "cr2");
    static RawFileFactory fctnef(OR_RAWFILE_TYPE_NEF, 
                                 boost::bind(&Internals::NEFFile::factory, _1),
                                 "nef");
    static RawFileFactory fctarw(OR_RAWFILE_TYPE_ARW, 
                                 boost::bind(&Internals::ARWFile::factory, _1),
                                 "arw");
    static RawFileFactory fctorf(OR_RAWFILE_TYPE_ORF, 
                                 boost::bind(&Internals::ORFFile::factory, _1),
                                 "orf");
    static RawFileFactory fctdng(OR_RAWFILE_TYPE_DNG, 
                                 boost::bind(&Internals::DNGFile::factory, _1),
                                 "dng");
    static RawFileFactory fctpef(OR_RAWFILE_TYPE_PEF, 
                                 boost::bind(&Internals::PEFFile::factory, _1),
                                 "pef");
    static RawFileFactory fctcrw(OR_RAWFILE_TYPE_CRW,
                                 boost::bind(&Internals::CRWFile::factory, _1),
                                 "crw");
    static RawFileFactory fcterf(OR_RAWFILE_TYPE_ERF,
                                 boost::bind(&Internals::ERFFile::factory, _1),
                                 "erf");
    static RawFileFactory fctmrw(OR_RAWFILE_TYPE_MRW,
                                 boost::bind(&Internals::MRWFile::factory, _1),
                                 "mrw");
}

class RawFile::Private 
{
public:
    Private(Type t)
        : m_type(t),
          m_type_id(OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_NONE, OR_TYPEID_UNKNOWN)),
          m_sizes(),
          m_cam_ids(NULL)
        {
        }
    ~Private()
        {
            std::map<int32_t, MetaValue*>::iterator iter;
            for(iter = m_metadata.begin();
                iter != m_metadata.end(); ++iter)
            {
                if(iter->second) {
                    delete iter->second;
                }
            }
        }
    /** the real type of the raw file */
    Type m_type;
    /** the raw file type id */
    TypeId m_type_id;
    /** list of thumbnail sizes */
    std::vector<uint32_t> m_sizes;
    std::map<int32_t, MetaValue*> m_metadata;
    const camera_ids_t *m_cam_ids;
};


const char **RawFile::fileExtensions()
{
    init();

    return RawFileFactory::fileExtensions();
}


RawFile *RawFile::newRawFile(const char*_filename, RawFile::Type _typeHint)
{
    init();

    Type type;
    if (_typeHint == OR_RAWFILE_TYPE_UNKNOWN) {
        type = identify(_filename);
    }
    else {
        type = _typeHint;
    }
    Trace(DEBUG1) << "factory size " << RawFileFactory::table().size() << "\n";
    RawFileFactory::Table::iterator iter = RawFileFactory::table().find(type);
    if (iter == RawFileFactory::table().end()) {
        Trace(WARNING) << "factory not found\n";
        return NULL;
    }
    if (iter->second == NULL) {
        Trace(WARNING) << "factory is NULL\n";
        return NULL;
    }
    IO::Stream *f = new IO::File(_filename);
    return iter->second(f);
}

RawFile *RawFile::newRawFileFromMemory(const uint8_t *buffer, 
                                       uint32_t len, 
                                       RawFile::Type _typeHint)
{
    init();
    Type type;
    if (_typeHint == OR_RAWFILE_TYPE_UNKNOWN) {
        ::or_error err = identifyBuffer(buffer, len, type);
        if(err != OR_ERROR_NONE) {
            Trace(ERROR) << "error identifying buffer\n";
            return NULL;
        }
    }
    else {
        type = _typeHint;
    }
    RawFileFactory::Table::iterator iter = RawFileFactory::table().find(type);
    if (iter == RawFileFactory::table().end()) {
        Trace(WARNING) << "factory not found\n";
        return NULL;
    }
    if (iter->second == NULL) {
        Trace(WARNING) << "factory is NULL\n";
        return NULL;
    }
    IO::Stream *f = new IO::MemStream((void*)buffer, len);
    return iter->second(f);
}


RawFile::Type RawFile::identify(const char*_filename)
{
    const char *e = ::strrchr(_filename, '.');
    if (e == NULL) {
        Trace(DEBUG1) << "Extension not found\n";
        return OR_RAWFILE_TYPE_UNKNOWN;
    }
    std::string extension(e + 1);
    if (extension.length() > 3) {
        return OR_RAWFILE_TYPE_UNKNOWN;
    }

    boost::to_lower(extension);

    RawFileFactory::Extensions & extensions = RawFileFactory::extensions();
    RawFileFactory::Extensions::iterator iter = extensions.find(extension);
    if (iter == extensions.end())
    {
        return OR_RAWFILE_TYPE_UNKNOWN;
    }
    return iter->second;
}

::or_error RawFile::identifyBuffer(const uint8_t* buff, size_t len,
                                   RawFile::Type &_type)
{
    _type = OR_RAWFILE_TYPE_UNKNOWN;
    if(len <= 4) {
        return OR_ERROR_BUF_TOO_SMALL;
    }
    if(memcmp(buff, "\0MRM", 4) == 0) {
        _type = OR_RAWFILE_TYPE_MRW;
        return OR_ERROR_NONE;
    }
    if(memcmp(buff, "II\x1a\0\0\0HEAPCCDR", 14) == 0) {
        _type = OR_RAWFILE_TYPE_CRW;
        return OR_ERROR_NONE;
    }
    if(memcmp(buff, "IIRO", 4) == 0) {
        _type = OR_RAWFILE_TYPE_ORF;
        return OR_ERROR_NONE;
    }
    if((memcmp(buff, "II\x2a\0", 4) == 0) 
       || (memcmp(buff, "MM\0\x2a", 4) == 0)) {
        // TIFF based format
        if(len >=12 ) {
            if(memcmp(buff + 8, "CR\x2", 3) == 0) {
                _type = OR_RAWFILE_TYPE_CR2;
                return OR_ERROR_NONE;                
            }
        }
        if(len >= 8) {
            IO::Stream *s = new IO::MemStream((void*)buff, len);
            boost::scoped_ptr<Internals::TiffEpFile> f(new Internals::TiffEpFile(s, OR_RAWFILE_TYPE_TIFF));
            
            // Take into account DNG by checking the DNGVersion tag
            const MetaValue *dng_version = f->getMetaValue(META_NS_TIFF | TIFF_TAG_DNG_VERSION);
            if(dng_version) {
                Trace(DEBUG1) << "found DNG versions\n";
                _type = OR_RAWFILE_TYPE_DNG;
                return OR_ERROR_NONE;
            }

            const MetaValue *makev = f->getMetaValue(META_NS_TIFF | EXIF_TAG_MAKE);
            if(makev){
                std::string makes = makev->getString();
                if(makes == "NIKON CORPORATION") {
                    _type = OR_RAWFILE_TYPE_NEF;
                }
                else if(makes == "SEIKO EPSON CORP."){
                    _type = OR_RAWFILE_TYPE_ERF;
                }
                else if(makes == "PENTAX Corporation ") {
                    _type = OR_RAWFILE_TYPE_PEF;                    
                }
                else if(makes == "SONY           ") {
                    _type = OR_RAWFILE_TYPE_ARW;                    
                }
                else if(makes == "Canon") {
                    _type = OR_RAWFILE_TYPE_CR2;
                }
            }
        }
                        
    }
    return OR_ERROR_NONE;
}

RawFile::RawFile(IO::Stream *, RawFile::Type _type)
    : d(new Private(_type))
{
		
}


RawFile::~RawFile()
{
    delete d;
}


RawFile::Type RawFile::type() const
{
    return d->m_type;
}

RawFile::TypeId RawFile::typeId()
{
    if(d->m_type_id == 0) {
        _identifyId();
    }
    return d->m_type_id;
}

void RawFile::_setTypeId(RawFile::TypeId _type_id)
{
    d->m_type_id = _type_id;
}

const std::vector<uint32_t> & RawFile::listThumbnailSizes(void)
{
    if (d->m_sizes.size() == 0) {
        Trace(DEBUG1) << "_enumThumbnailSizes init\n";
        bool ret = _enumThumbnailSizes(d->m_sizes);
        if (!ret) {
            Trace(DEBUG1) << "_enumThumbnailSizes failed\n";
        }
    }
    return d->m_sizes;
}


::or_error RawFile::getThumbnail(uint32_t tsize, Thumbnail & thumbnail)
{
    ::or_error ret = OR_ERROR_NOT_FOUND;
    uint32_t smallest_bigger = 0xffffffff;
    uint32_t biggest_smaller = 0;
    uint32_t found_size = 0;

    Trace(DEBUG1) << "requested size " << tsize << "\n";

    const std::vector<uint32_t> & sizes(listThumbnailSizes());

    std::vector<uint32_t>::const_iterator iter;

    for (iter = sizes.begin(); iter != sizes.end(); ++iter) {
        Trace(DEBUG1) << "current iter is " << *iter << "\n";
        if (*iter < tsize) {
            if (*iter > biggest_smaller) {
                biggest_smaller = *iter;
            }
        }
        else if(*iter > tsize) {
            if(*iter < smallest_bigger) {
                smallest_bigger = *iter;
            }
        }
        else { // *iter == tsize
            found_size = tsize;
            break;
        }
    }

    if (found_size == 0) {
        found_size = (smallest_bigger != 0xffffffff ? 
                      smallest_bigger : biggest_smaller);
    }

    if (found_size != 0) {
        Trace(DEBUG1) << "size " << found_size << " found\n";
        ret = _getThumbnail(found_size, thumbnail);
    }
    else {
        // no size found, let's fail gracefuly
        Trace(DEBUG1) << "no size found\n";
        ret = OR_ERROR_NOT_FOUND;
    }

    return ret;
}


::or_error RawFile::getRawData(RawData & rawdata, uint32_t options)
{
    Trace(DEBUG1) << "getRawData()\n";
    ::or_error ret = _getRawData(rawdata, options);
    return ret;
}	

::or_error RawFile::getRenderedImage(BitmapData & bitmapdata, uint32_t options)
{
    RawData rawdata;
    Trace(DEBUG1) << "options are " << options << "\n";
    ::or_error ret = getRawData(rawdata, options);
    if(ret == OR_ERROR_NONE) {
        if(rawdata.dataType() != OR_DATA_TYPE_CFA) {
            Trace(DEBUG1) << "wrong data type\n";
            return OR_ERROR_INVALID_FORMAT;
        }
        uint32_t x,y;
        or_cfa_pattern pattern;
        uint16_t *src;
        pattern = rawdata.cfaPattern();
        x = rawdata.x();
        y = rawdata.y();
        bitmapdata.setDimensions(x,y);
        bitmapdata.setDataType(OR_DATA_TYPE_PIXMAP_8RGB);
        uint8_t *dst = (uint8_t *)bitmapdata.allocData(sizeof(uint8_t) * 3 * x * y);
        /*
        rawdata.linearize();
        rawdata.subtractBlack();
        rawdata.rescale();
        rawdata.clip();
        */
        src = (uint16_t*)rawdata.data();

        /* figure out how the demosaic can be plugged for a different 
         * algorithm */
        bimedian_demosaic(src, x, y, pattern, dst);
    }
    return ret;
}


int32_t RawFile::getOrientation()
{
    int32_t idx = 0;
    const MetaValue * value = getMetaValue(META_NS_TIFF 
                                           | EXIF_TAG_ORIENTATION);
    if(value == NULL) {
        return 0;
    }
    try {
        idx = value->getInteger();
    }
    catch(const Internals::BadTypeException & e)	{
        Trace(DEBUG1) << "wrong type - " << e.what() << "\n";
    }
    return idx;
}
	
const MetaValue *RawFile::getMetaValue(int32_t meta_index)
{
    MetaValue *val = NULL;
    std::map<int32_t, MetaValue*>::iterator iter = d->m_metadata.find(meta_index);
    if(iter == d->m_metadata.end()) {
        val = _getMetaValue(meta_index);
        if(val != NULL) {
            d->m_metadata[meta_index] = val;
        }
    }
    else {
        val = iter->second;
    }
    return val;
}


RawFile::TypeId RawFile::_typeIdFromModel(const std::string & model)
{
    const struct camera_ids_t * p = d->m_cam_ids;
    if(!p) {
        return 0;
    }
    while(p->model) {
        if(model == p->model) {
            break;
        }
        p++;
    }
    return p->type_id;
}

void RawFile::_setIdMap(const camera_ids_t *map)
{
    d->m_cam_ids = map;
}

}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0))
  indent-tabs-mode:nil
  fill-column:80
  End:
*/