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
|
/*
* libopenraw - rawfile.cpp
*
* Copyright (C) 2008 Novell, Inc.
* Copyright (C) 2006-2016 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 <stddef.h>
#include <stdint.h>
#include <cstring>
#include <map>
#include <string>
#include <functional>
#include <memory>
#include <utility>
#include <vector>
#include <boost/algorithm/string.hpp>
#include "trace.hpp"
#include <libopenraw/metadata.h>
#include <libopenraw/cameraids.h>
#include <libopenraw/consts.h>
#include <libopenraw/debug.h>
#include "rawfile.hpp"
#include "rawdata.hpp"
#include "thumbnail.hpp"
#include "metavalue.hpp"
#include "io/stream.hpp"
#include "io/file.hpp"
#include "io/memstream.hpp"
#include "rawcontainer.hpp"
#include "tiffepfile.hpp"
#include "cr2file.hpp"
#include "neffile.hpp"
#include "orffile.hpp"
#include "arwfile.hpp"
#include "peffile.hpp"
#include "crwfile.hpp"
#include "erffile.hpp"
#include "dngfile.hpp"
#include "mrwfile.hpp"
#include "rw2file.hpp"
#include "raffile.hpp"
#include "exception.hpp"
#include "rawfile_private.hpp"
#include "rawfilefactory.hpp"
using std::string;
using namespace Debug;
namespace OpenRaw {
class BitmapData;
using Internals::RawFileFactory;
void init(void)
{
using namespace std::placeholders;
static RawFileFactory fctcr2(OR_RAWFILE_TYPE_CR2,
std::bind(&Internals::Cr2File::factory, _1),
"cr2");
static RawFileFactory fctnef(OR_RAWFILE_TYPE_NEF,
std::bind(&Internals::NefFile::factory, _1),
"nef");
static RawFileFactory fctnrw(OR_RAWFILE_TYPE_NRW,
std::bind(&Internals::NefFile::factory, _1),
"nrw");
static RawFileFactory fctarw(OR_RAWFILE_TYPE_ARW,
std::bind(&Internals::ArwFile::factory, _1),
"arw");
static RawFileFactory fctorf(OR_RAWFILE_TYPE_ORF,
std::bind(&Internals::OrfFile::factory, _1),
"orf");
static RawFileFactory fctdng(OR_RAWFILE_TYPE_DNG,
std::bind(&Internals::DngFile::factory, _1),
"dng");
static RawFileFactory fctpef(OR_RAWFILE_TYPE_PEF,
std::bind(&Internals::PEFFile::factory, _1),
"pef");
static RawFileFactory fctcrw(OR_RAWFILE_TYPE_CRW,
std::bind(&Internals::CRWFile::factory, _1),
"crw");
static RawFileFactory fcterf(OR_RAWFILE_TYPE_ERF,
std::bind(&Internals::ERFFile::factory, _1),
"erf");
static RawFileFactory fctmrw(OR_RAWFILE_TYPE_MRW,
std::bind(&Internals::MRWFile::factory, _1),
"mrw");
static RawFileFactory fctraw(OR_RAWFILE_TYPE_RW2,
std::bind(&Internals::Rw2File::factory, _1),
"raw");
static RawFileFactory fctrw2(OR_RAWFILE_TYPE_RW2,
std::bind(&Internals::Rw2File::factory, _1),
"rw2");
static RawFileFactory fctraf(OR_RAWFILE_TYPE_RAF,
std::bind(&Internals::RafFile::factory, _1),
"raf");
}
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),
m_matrices(NULL)
{
}
~Private()
{
for(auto value : m_metadata)
{
if(value.second) {
delete value.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;
Internals::ThumbLocations m_thumbLocations;
std::map<int32_t, MetaValue*> m_metadata;
const camera_ids_t *m_cam_ids;
const Internals::BuiltinColourMatrix* m_matrices;
};
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;
}
LOGDBG1("factory size %ld\n", RawFileFactory::table().size());
auto iter = RawFileFactory::table().find(type);
if (iter == RawFileFactory::table().end()) {
LOGWARN("factory not found\n");
return NULL;
}
if (iter->second == NULL) {
LOGWARN("factory is NULL\n");
return NULL;
}
IO::Stream::Ptr 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) {
LOGERR("error identifying buffer\n");
return NULL;
}
}
else {
type = _typeHint;
}
auto iter = RawFileFactory::table().find(type);
if (iter == RawFileFactory::table().end()) {
LOGWARN("factory not found\n");
return NULL;
}
if (iter->second == nullptr) {
LOGWARN("factory is NULL\n");
return NULL;
}
IO::Stream::Ptr 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) {
LOGDBG1("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();
auto 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, "IIU\0", 4) == 0) {
_type = OR_RAWFILE_TYPE_RW2;
return OR_ERROR_NONE;
}
if(memcmp(buff, RAF_MAGIC, RAF_MAGIC_LEN) == 0) {
_type = OR_RAWFILE_TYPE_RAF;
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::Ptr s(new IO::MemStream((void*)buff, len));
std::unique_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) {
LOGDBG1("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(0);
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(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;
}
RawFile::TypeId RawFile::_typeId() const
{
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.empty()) {
LOGDBG1("_enumThumbnailSizes init\n");
::or_error ret = _enumThumbnailSizes(d->m_sizes);
if (ret != OR_ERROR_NONE) {
LOGDBG1("_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;
LOGDBG1("requested size %u\n", tsize);
auto sizes(listThumbnailSizes());
for (auto s : sizes) {
LOGDBG1("current iter is %u\n", s);
if (s < tsize) {
if (s > biggest_smaller) {
biggest_smaller = s;
}
}
else if(s > tsize) {
if(s < smallest_bigger) {
smallest_bigger = s;
}
}
else { // s == tsize
found_size = tsize;
break;
}
}
if (found_size == 0) {
found_size = (smallest_bigger != 0xffffffff ?
smallest_bigger : biggest_smaller);
}
if (found_size != 0) {
LOGDBG1("size %u found\n", found_size);
ret = _getThumbnail(found_size, thumbnail);
}
else {
// no size found, let's fail gracefuly
LOGDBG1("no size found\n");
ret = OR_ERROR_NOT_FOUND;
}
return ret;
}
/**
* Internal implementation of getThumbnail. The size must match.
*/
::or_error RawFile::_getThumbnail(uint32_t size, Thumbnail & thumbnail)
{
::or_error ret = OR_ERROR_NOT_FOUND;
auto iter = d->m_thumbLocations.find(size);
if(iter != d->m_thumbLocations.end())
{
const Internals::ThumbDesc & desc = iter->second;
thumbnail.setDataType(desc.type);
uint32_t byte_length= desc.length; /**< of the buffer */
uint32_t offset = desc.offset;
LOGDBG1("Thumbnail at %u of %u bytes.\n", offset, byte_length);
if (byte_length != 0) {
void *p = thumbnail.allocData(byte_length);
size_t real_size = getContainer()->fetchData(p, offset,
byte_length);
if (real_size < byte_length) {
LOGWARN("Size mismatch for data: got %lu expected %u ignoring.\n",
real_size, byte_length);
}
thumbnail.setDimensions(desc.x, desc.y);
ret = OR_ERROR_NONE;
}
}
return ret;
}
void RawFile::_addThumbnail(uint32_t size, const Internals::ThumbDesc& desc)
{
d->m_thumbLocations[size] = desc;
}
::or_error RawFile::getRawData(RawData & rawdata, uint32_t options)
{
LOGDBG1("getRawData()\n");
::or_error ret = _getRawData(rawdata, options);
if (ret != OR_ERROR_NONE) {
return ret;
}
// if the colour matrix isn't copied already, do it now.
uint32_t matrix_size = 0;
if (!rawdata.getColourMatrix1(matrix_size) || !matrix_size) {
matrix_size = colourMatrixSize();
double *matrix = new double[matrix_size];
if (getColourMatrix1(matrix, matrix_size) == OR_ERROR_NONE) {
rawdata.setColourMatrix1(matrix, matrix_size);
}
delete [] matrix;
}
return ret;
}
::or_error RawFile::getRenderedImage(BitmapData & bitmapdata, uint32_t options)
{
RawData rawdata;
LOGDBG1("options are %u\n", options);
::or_error ret = getRawData(rawdata, options);
if(ret == OR_ERROR_NONE) {
ret = rawdata.getRenderedImage(bitmapdata, options);
}
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(0);
}
catch(const Internals::BadTypeException & e) {
LOGDBG1("wrong type - %s\n", e.what());
}
return idx;
}
uint32_t RawFile::colourMatrixSize()
{
return 9;
}
::or_error RawFile::getColourMatrix1(double* matrix, uint32_t & size)
{
return _getColourMatrix(1, matrix, size);
}
::or_error RawFile::getColourMatrix2(double* matrix, uint32_t & size)
{
return _getColourMatrix(2, matrix, size);
}
::or_error RawFile::_getColourMatrix(uint32_t index, double* matrix, uint32_t & size)
{
int32_t meta_index = 0;
switch(index) {
case 1:
meta_index = META_NS_TIFF | DNG_TAG_COLORMATRIX1;
break;
case 2:
meta_index = META_NS_TIFF | DNG_TAG_COLORMATRIX2;
break;
default:
size = 0;
return OR_ERROR_INVALID_PARAM;
}
const MetaValue* meta = getMetaValue(meta_index);
if(!meta) {
if (index != 1) {
size = 0;
return OR_ERROR_INVALID_PARAM;
}
return _getBuiltinColourMatrix(d->m_matrices, typeId(), matrix, size);
}
uint32_t count = meta->getCount();
if(size < count) {
// return the expected size
size = count;
return OR_ERROR_BUF_TOO_SMALL;
}
for(uint32_t i = 0; i < count; i++) {
matrix[i] = meta->getDouble(i);
}
size = count;
return OR_ERROR_NONE;
}
ExifLightsourceValue RawFile::getCalibrationIlluminant1()
{
return _getCalibrationIlluminant(1);
}
ExifLightsourceValue RawFile::getCalibrationIlluminant2()
{
return _getCalibrationIlluminant(2);
}
ExifLightsourceValue RawFile::_getCalibrationIlluminant(uint16_t index)
{
int32_t meta_index = 0;
switch(index) {
case 1:
meta_index = META_NS_TIFF | DNG_TAG_CALIBRATION_ILLUMINANT1;
break;
case 2:
meta_index = META_NS_TIFF | DNG_TAG_CALIBRATION_ILLUMINANT2;
break;
default:
return EV_LIGHTSOURCE_UNKNOWN;
}
const MetaValue* meta = getMetaValue(meta_index);
if(!meta) {
return (index == 1) ? EV_LIGHTSOURCE_D65 : EV_LIGHTSOURCE_UNKNOWN;
}
return (ExifLightsourceValue)meta->getInteger(0);
}
const MetaValue *RawFile::getMetaValue(int32_t meta_index)
{
MetaValue *val = NULL;
auto 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;
}
const RawFile::camera_ids_t*
RawFile::_lookupCameraId(const camera_ids_t * map, const std::string& value)
{
const camera_ids_t * p = map;
if(!p) {
return NULL;
}
while(p->model) {
if(value == p->model) {
return p;
}
p++;
}
return NULL;
}
RawFile::TypeId RawFile::_typeIdFromModel(const std::string & make,
const std::string & model)
{
const camera_ids_t * p = _lookupCameraId(d->m_cam_ids, model);
if (!p) {
return _typeIdFromMake(make);
}
return p->type_id;
}
const RawFile::camera_ids_t RawFile::s_make[] = {
{ "Canon", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_CANON, 0) },
{ "NIKON CORPORATION", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_NIKON, 0) },
{ "LEICA CAMERA AG ", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_LEICA, 0) },
{ "Leica Camera AG", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_LEICA, 0) },
{ "Panasonic", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_PANASONIC, 0) },
// Hardcoded
{ "Minolta", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_MINOLTA, 0) },
{ "FujiFilm", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_FUJIFILM, 0) },
{ NULL, 0 }
};
RawFile::TypeId
RawFile::_typeIdFromMake(const std::string& make)
{
const camera_ids_t * p = _lookupCameraId(s_make, make);
if (!p) {
return 0;
}
return p->type_id;
}
void RawFile::_setIdMap(const camera_ids_t *map)
{
d->m_cam_ids = map;
}
const Internals::BuiltinColourMatrix*
RawFile::_getMatrices() const
{
return d->m_matrices;
}
void RawFile::_setMatrices(const Internals::BuiltinColourMatrix* matrices)
{
d->m_matrices = matrices;
}
::or_error
RawFile::_getBuiltinLevels(const Internals::BuiltinColourMatrix* m,
TypeId type_id,
uint16_t & black, uint16_t & white)
{
if(!m) {
return OR_ERROR_NOT_FOUND;
}
while(m->camera) {
if(m->camera == type_id) {
black = m->black;
white = m->white;
return OR_ERROR_NONE;
}
++m;
}
return OR_ERROR_NOT_FOUND;
}
::or_error
RawFile::_getBuiltinColourMatrix(const Internals::BuiltinColourMatrix* m,
TypeId type_id,
double* matrix,
uint32_t & size)
{
if(!m) {
return OR_ERROR_NOT_FOUND;
}
if(size < 9) {
return OR_ERROR_BUF_TOO_SMALL;
}
while(m->camera) {
if(m->camera == type_id) {
for(int i = 0; i < 9; i++) {
matrix[i] = static_cast<double>(m->matrix[i]) / 10000.0;
}
size = 9;
return OR_ERROR_NONE;
}
++m;
}
size = 0;
return OR_ERROR_NOT_FOUND;
}
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0))
indent-tabs-mode:nil
fill-column:80
End:
*/
|