/src/FreeImage/Source/LibRawLite/src/libraw_cxx.cpp
C++ | 2789 lines | 2394 code | 297 blank | 98 comment | 519 complexity | b4e1584f4136f83772c17d3d5c8a25df MD5 | raw file
Large files files are truncated, but you can click here to view the full file
1/* -*- C++ -*- 2 * File: libraw_cxx.cpp 3 * Copyright 2008-2010 LibRaw LLC (info@libraw.org) 4 * Created: Sat Mar 8 , 2008 5 * 6 * LibRaw C++ interface (implementation) 7 8LibRaw is free software; you can redistribute it and/or modify 9it under the terms of the one of three licenses as you choose: 10 111. GNU LESSER GENERAL PUBLIC LICENSE version 2.1 12 (See file LICENSE.LGPL provided in LibRaw distribution archive for details). 13 142. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0 15 (See file LICENSE.CDDL provided in LibRaw distribution archive for details). 16 173. LibRaw Software License 27032010 18 (See file LICENSE.LibRaw.pdf provided in LibRaw distribution archive for details). 19 20 */ 21 22#include <math.h> 23#include <errno.h> 24#include <float.h> 25#include <new> 26#include <sys/types.h> 27#include <sys/stat.h> 28#ifndef WIN32 29#include <netinet/in.h> 30#else 31#include <winsock2.h> 32#endif 33#define LIBRAW_LIBRARY_BUILD 34#include "libraw/libraw.h" 35#include "internal/defines.h" 36 37#ifdef __cplusplus 38extern "C" 39{ 40#endif 41 void default_memory_callback(void *,const char *file,const char *where) 42 { 43 fprintf (stderr,"%s: Out of memory in %s\n", file?file:"unknown file", where); 44 } 45 46 void default_data_callback(void*,const char *file, const int offset) 47 { 48 if(offset < 0) 49 fprintf (stderr,"%s: Unexpected end of file\n", file?file:"unknown file"); 50 else 51 fprintf (stderr,"%s: data corrupted at %d\n",file?file:"unknown file",offset); 52 } 53 const char *libraw_strerror(int e) 54 { 55 enum LibRaw_errors errorcode = (LibRaw_errors)e; 56 switch(errorcode) 57 { 58 case LIBRAW_SUCCESS: 59 return "No error"; 60 case LIBRAW_UNSPECIFIED_ERROR: 61 return "Unspecified error"; 62 case LIBRAW_FILE_UNSUPPORTED: 63 return "Unsupported file format or not RAW file"; 64 case LIBRAW_REQUEST_FOR_NONEXISTENT_IMAGE: 65 return "Request for nonexisting image number"; 66 case LIBRAW_OUT_OF_ORDER_CALL: 67 return "Out of order call of libraw function"; 68 case LIBRAW_NO_THUMBNAIL: 69 return "No thumbnail in file"; 70 case LIBRAW_UNSUPPORTED_THUMBNAIL: 71 return "Unsupported thumbnail format"; 72 case LIBRAW_UNSUFFICIENT_MEMORY: 73 return "Unsufficient memory"; 74 case LIBRAW_DATA_ERROR: 75 return "Corrupted data or unexpected EOF"; 76 case LIBRAW_IO_ERROR: 77 return "Input/output error"; 78 case LIBRAW_CANCELLED_BY_CALLBACK: 79 return "Cancelled by user callback"; 80 case LIBRAW_BAD_CROP: 81 return "Bad crop box"; 82 default: 83 return "Unknown error code"; 84 } 85 } 86 87#ifdef __cplusplus 88} 89#endif 90 91 92const double LibRaw_constants::xyz_rgb[3][3] = 93{ 94 { 0.412453, 0.357580, 0.180423 }, 95 { 0.212671, 0.715160, 0.072169 }, 96 { 0.019334, 0.119193, 0.950227 } 97}; 98 99const float LibRaw_constants::d65_white[3] = { 0.950456f, 1.0f, 1.088754f }; 100 101#define P1 imgdata.idata 102#define S imgdata.sizes 103#define O imgdata.params 104#define C imgdata.color 105#define T imgdata.thumbnail 106#define IO libraw_internal_data.internal_output_params 107#define ID libraw_internal_data.internal_data 108 109#define EXCEPTION_HANDLER(e) do{ \ 110 /* fprintf(stderr,"Exception %d caught\n",e);*/ \ 111 switch(e) \ 112 { \ 113 case LIBRAW_EXCEPTION_ALLOC: \ 114 recycle(); \ 115 return LIBRAW_UNSUFFICIENT_MEMORY; \ 116 case LIBRAW_EXCEPTION_DECODE_RAW: \ 117 case LIBRAW_EXCEPTION_DECODE_JPEG: \ 118 recycle(); \ 119 return LIBRAW_DATA_ERROR; \ 120 case LIBRAW_EXCEPTION_DECODE_JPEG2000: \ 121 recycle(); \ 122 return LIBRAW_DATA_ERROR; \ 123 case LIBRAW_EXCEPTION_IO_EOF: \ 124 case LIBRAW_EXCEPTION_IO_CORRUPT: \ 125 recycle(); \ 126 return LIBRAW_IO_ERROR; \ 127 case LIBRAW_EXCEPTION_CANCELLED_BY_CALLBACK:\ 128 recycle(); \ 129 return LIBRAW_CANCELLED_BY_CALLBACK; \ 130 case LIBRAW_EXCEPTION_BAD_CROP: \ 131 recycle(); \ 132 return LIBRAW_BAD_CROP; \ 133 default: \ 134 return LIBRAW_UNSPECIFIED_ERROR; \ 135 } \ 136 }while(0) 137 138const char* LibRaw::version() { return LIBRAW_VERSION_STR;} 139int LibRaw::versionNumber() { return LIBRAW_VERSION; } 140const char* LibRaw::strerror(int p) { return libraw_strerror(p);} 141 142 143void LibRaw::derror() 144{ 145 if (!libraw_internal_data.unpacker_data.data_error && libraw_internal_data.internal_data.input) 146 { 147 if (libraw_internal_data.internal_data.input->eof()) 148 { 149 if(callbacks.data_cb)(*callbacks.data_cb)(callbacks.datacb_data, 150 libraw_internal_data.internal_data.input->fname(),-1); 151 throw LIBRAW_EXCEPTION_IO_EOF; 152 } 153 else 154 { 155 if(callbacks.data_cb)(*callbacks.data_cb)(callbacks.datacb_data, 156 libraw_internal_data.internal_data.input->fname(), 157 libraw_internal_data.internal_data.input->tell()); 158 throw LIBRAW_EXCEPTION_IO_CORRUPT; 159 } 160 } 161 libraw_internal_data.unpacker_data.data_error++; 162} 163 164void LibRaw::dcraw_clear_mem(libraw_processed_image_t* p) 165{ 166 if(p) ::free(p); 167} 168 169#define ZERO(a) memset(&a,0,sizeof(a)) 170 171 172LibRaw:: LibRaw(unsigned int flags) 173{ 174 double aber[4] = {1,1,1,1}; 175 double gamm[6] = { 0.45,4.5,0,0,0,0 }; 176 unsigned greybox[4] = { 0, 0, UINT_MAX, UINT_MAX }; 177 unsigned cropbox[4] = { 0, 0, UINT_MAX, UINT_MAX }; 178#ifdef DCRAW_VERBOSE 179 verbose = 1; 180#else 181 verbose = 0; 182#endif 183 ZERO(imgdata); 184 ZERO(libraw_internal_data); 185 ZERO(callbacks); 186 callbacks.mem_cb = (flags & LIBRAW_OPIONS_NO_MEMERR_CALLBACK) ? NULL: &default_memory_callback; 187 callbacks.data_cb = (flags & LIBRAW_OPIONS_NO_DATAERR_CALLBACK)? NULL : &default_data_callback; 188 memmove(&imgdata.params.aber,&aber,sizeof(aber)); 189 memmove(&imgdata.params.gamm,&gamm,sizeof(gamm)); 190 memmove(&imgdata.params.greybox,&greybox,sizeof(greybox)); 191 memmove(&imgdata.params.cropbox,&cropbox,sizeof(cropbox)); 192 193 imgdata.params.bright=1; 194 imgdata.params.use_camera_matrix=-1; 195 imgdata.params.user_flip=-1; 196 imgdata.params.user_black=-1; 197 imgdata.params.user_sat=-1; 198 imgdata.params.user_qual=-1; 199 imgdata.params.output_color=1; 200 imgdata.params.output_bps=8; 201 imgdata.params.use_fuji_rotate=1; 202 imgdata.params.exp_shift = 1.0; 203 imgdata.params.auto_bright_thr = LIBRAW_DEFAULT_AUTO_BRIGHTNESS_THRESHOLD; 204 imgdata.params.adjust_maximum_thr= LIBRAW_DEFAULT_ADJUST_MAXIMUM_THRESHOLD; 205 imgdata.params.green_matching = 0; 206 imgdata.parent_class = this; 207 imgdata.progress_flags = 0; 208 tls = new LibRaw_TLS; 209 tls->init(); 210} 211 212 213void* LibRaw:: malloc(size_t t) 214{ 215 void *p = memmgr.malloc(t); 216 return p; 217} 218void* LibRaw:: realloc(void *q,size_t t) 219{ 220 void *p = memmgr.realloc(q,t); 221 return p; 222} 223 224 225void* LibRaw:: calloc(size_t n,size_t t) 226{ 227 void *p = memmgr.calloc(n,t); 228 return p; 229} 230void LibRaw:: free(void *p) 231{ 232 memmgr.free(p); 233} 234 235 236int LibRaw:: fc (int row, int col) 237{ 238 static const char filter[16][16] = 239 { { 2,1,1,3,2,3,2,0,3,2,3,0,1,2,1,0 }, 240 { 0,3,0,2,0,1,3,1,0,1,1,2,0,3,3,2 }, 241 { 2,3,3,2,3,1,1,3,3,1,2,1,2,0,0,3 }, 242 { 0,1,0,1,0,2,0,2,2,0,3,0,1,3,2,1 }, 243 { 3,1,1,2,0,1,0,2,1,3,1,3,0,1,3,0 }, 244 { 2,0,0,3,3,2,3,1,2,0,2,0,3,2,2,1 }, 245 { 2,3,3,1,2,1,2,1,2,1,1,2,3,0,0,1 }, 246 { 1,0,0,2,3,0,0,3,0,3,0,3,2,1,2,3 }, 247 { 2,3,3,1,1,2,1,0,3,2,3,0,2,3,1,3 }, 248 { 1,0,2,0,3,0,3,2,0,1,1,2,0,1,0,2 }, 249 { 0,1,1,3,3,2,2,1,1,3,3,0,2,1,3,2 }, 250 { 2,3,2,0,0,1,3,0,2,0,1,2,3,0,1,0 }, 251 { 1,3,1,2,3,2,3,2,0,2,0,1,1,0,3,0 }, 252 { 0,2,0,3,1,0,0,1,1,3,3,2,3,2,2,1 }, 253 { 2,1,3,2,3,1,2,1,0,3,0,2,0,2,0,2 }, 254 { 0,3,1,0,0,2,0,3,2,1,3,1,1,3,1,3 } }; 255 256 if (imgdata.idata.filters != 1) return FC(row,col); 257 return filter[(row+imgdata.sizes.top_margin) & 15][(col+imgdata.sizes.left_margin) & 15]; 258} 259 260void LibRaw:: recycle() 261{ 262 if(libraw_internal_data.internal_data.input && libraw_internal_data.internal_data.input_internal) 263 { 264 delete libraw_internal_data.internal_data.input; 265 libraw_internal_data.internal_data.input = NULL; 266 } 267 libraw_internal_data.internal_data.input_internal = 0; 268#define FREE(a) do { if(a) { free(a); a = NULL;} }while(0) 269 270 FREE(imgdata.image); 271 FREE(imgdata.thumbnail.thumb); 272 FREE(libraw_internal_data.internal_data.meta_data); 273 FREE(libraw_internal_data.output_data.histogram); 274 FREE(libraw_internal_data.output_data.oprof); 275 FREE(imgdata.color.profile); 276 FREE(imgdata.rawdata.ph1_black); 277 FREE(imgdata.rawdata.raw_alloc); 278#undef FREE 279 ZERO(imgdata.rawdata); 280 ZERO(imgdata.sizes); 281 ZERO(imgdata.color); 282 ZERO(libraw_internal_data); 283 memmgr.cleanup(); 284 imgdata.thumbnail.tformat = LIBRAW_THUMBNAIL_UNKNOWN; 285 imgdata.progress_flags = 0; 286 287 tls->init(); 288} 289 290const char * LibRaw::unpack_function_name() 291{ 292 libraw_decoder_info_t decoder_info; 293 get_decoder_info(&decoder_info); 294 return decoder_info.decoder_name; 295} 296 297int LibRaw::get_decoder_info(libraw_decoder_info_t* d_info) 298{ 299 if(!d_info) return LIBRAW_UNSPECIFIED_ERROR; 300 if(!load_raw) return LIBRAW_OUT_OF_ORDER_CALL; 301 302 d_info->decoder_flags = LIBRAW_DECODER_NOTSET; 303 304 // sorted names order 305 if (load_raw == &LibRaw::adobe_dng_load_raw_lj) 306 { 307 // Check rbayer 308 d_info->decoder_name = "adobe_dng_load_raw_lj()"; 309 d_info->decoder_flags = imgdata.idata.filters ? LIBRAW_DECODER_FLATFIELD : LIBRAW_DECODER_4COMPONENT ; 310 d_info->decoder_flags |= LIBRAW_DECODER_HASCURVE; 311 } 312 else if (load_raw == &LibRaw::adobe_dng_load_raw_nc) 313 { 314 // Check rbayer 315 d_info->decoder_name = "adobe_dng_load_raw_nc()"; 316 d_info->decoder_flags = imgdata.idata.filters ? LIBRAW_DECODER_FLATFIELD : LIBRAW_DECODER_4COMPONENT; 317 d_info->decoder_flags |= LIBRAW_DECODER_HASCURVE; 318 } 319 else if (load_raw == &LibRaw::canon_600_load_raw) 320 { 321 d_info->decoder_name = "canon_600_load_raw()"; 322 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; // WB set within decoder, no need to load raw 323 } 324 else if (load_raw == &LibRaw::canon_compressed_load_raw) 325 { 326 d_info->decoder_name = "canon_compressed_load_raw()"; 327 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 328 } 329 else if (load_raw == &LibRaw::canon_sraw_load_raw) 330 { 331 d_info->decoder_name = "canon_sraw_load_raw()"; 332 d_info->decoder_flags = LIBRAW_DECODER_LEGACY; 333 } 334 else if (load_raw == &LibRaw::eight_bit_load_raw ) 335 { 336 d_info->decoder_name = "eight_bit_load_raw()"; 337 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 338 d_info->decoder_flags |= LIBRAW_DECODER_HASCURVE; 339 } 340 else if (load_raw == &LibRaw::foveon_load_raw ) 341 { 342 d_info->decoder_name = "foveon_load_raw()"; 343 d_info->decoder_flags = LIBRAW_DECODER_LEGACY; 344 } 345 else if (load_raw == &LibRaw::fuji_load_raw ) 346 { 347 d_info->decoder_name = "fuji_load_raw()"; 348 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 349 } 350 else if (load_raw == &LibRaw::hasselblad_load_raw ) 351 { 352 d_info->decoder_name = "hasselblad_load_raw()"; 353 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 354 } 355 else if (load_raw == &LibRaw::imacon_full_load_raw ) 356 { 357 d_info->decoder_name = "imacon_full_load_raw()"; 358 d_info->decoder_flags = LIBRAW_DECODER_4COMPONENT; 359 } 360 else if (load_raw == &LibRaw::kodak_262_load_raw ) 361 { 362 d_info->decoder_name = "kodak_262_load_raw()"; // UNTESTED! 363 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 364 d_info->decoder_flags |= LIBRAW_DECODER_HASCURVE; 365 } 366 else if (load_raw == &LibRaw::kodak_65000_load_raw ) 367 { 368 d_info->decoder_name = "kodak_65000_load_raw()"; 369 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 370 d_info->decoder_flags |= LIBRAW_DECODER_HASCURVE; 371 } 372 else if (load_raw == &LibRaw::kodak_dc120_load_raw ) 373 { 374 d_info->decoder_name = "kodak_dc120_load_raw()"; 375 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 376 } 377 else if (load_raw == &LibRaw::kodak_jpeg_load_raw ) 378 { 379 // UNTESTED + RBAYER 380 d_info->decoder_name = "kodak_jpeg_load_raw()"; 381 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 382 } 383 else if (load_raw == &LibRaw::kodak_radc_load_raw ) 384 { 385 d_info->decoder_name = "kodak_radc_load_raw()"; 386 d_info->decoder_flags = LIBRAW_DECODER_4COMPONENT; 387 } 388 else if (load_raw == &LibRaw::kodak_rgb_load_raw ) 389 { 390 // UNTESTED 391 d_info->decoder_name = "kodak_rgb_load_raw()"; 392 d_info->decoder_flags = LIBRAW_DECODER_4COMPONENT; 393 } 394 else if (load_raw == &LibRaw::kodak_yrgb_load_raw ) 395 { 396 d_info->decoder_name = "kodak_yrgb_load_raw()"; 397 d_info->decoder_flags = LIBRAW_DECODER_4COMPONENT; 398 d_info->decoder_flags |= LIBRAW_DECODER_HASCURVE; 399 } 400 else if (load_raw == &LibRaw::kodak_ycbcr_load_raw ) 401 { 402 // UNTESTED 403 d_info->decoder_name = "kodak_ycbcr_load_raw()"; 404 d_info->decoder_flags = LIBRAW_DECODER_4COMPONENT; 405 d_info->decoder_flags |= LIBRAW_DECODER_HASCURVE; 406 } 407 else if (load_raw == &LibRaw::leaf_hdr_load_raw ) 408 { 409 d_info->decoder_name = "leaf_hdr_load_raw()"; 410 d_info->decoder_flags = imgdata.idata.filters ? LIBRAW_DECODER_FLATFIELD : LIBRAW_DECODER_4COMPONENT; 411 } 412 else if (load_raw == &LibRaw::lossless_jpeg_load_raw) 413 { 414 // Check rbayer 415 d_info->decoder_name = "lossless_jpeg_load_raw()"; 416 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD | LIBRAW_DECODER_HASCURVE; 417 } 418 else if (load_raw == &LibRaw::minolta_rd175_load_raw ) 419 { 420 // UNTESTED 421 d_info->decoder_name = "minolta_rd175_load_raw()"; 422 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 423 } 424 else if (load_raw == &LibRaw::nikon_compressed_load_raw) 425 { 426 // Check rbayer 427 d_info->decoder_name = "nikon_compressed_load_raw()"; 428 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 429 } 430 else if (load_raw == &LibRaw::nokia_load_raw ) 431 { 432 // UNTESTED 433 d_info->decoder_name = "nokia_load_raw()"; 434 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 435 } 436 else if (load_raw == &LibRaw::olympus_load_raw ) 437 { 438 d_info->decoder_name = "olympus_load_raw()"; 439 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 440 } 441 else if (load_raw == &LibRaw::packed_load_raw ) 442 { 443 d_info->decoder_name = "packed_load_raw()"; 444 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 445 } 446 else if (load_raw == &LibRaw::panasonic_load_raw ) 447 { 448 d_info->decoder_name = "panasonic_load_raw()"; 449 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 450 } 451 else if (load_raw == &LibRaw::pentax_load_raw ) 452 { 453 d_info->decoder_name = "pentax_load_raw()"; 454 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 455 } 456 else if (load_raw == &LibRaw::phase_one_load_raw ) 457 { 458 d_info->decoder_name = "phase_one_load_raw()"; 459 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 460 } 461 else if (load_raw == &LibRaw::phase_one_load_raw_c ) 462 { 463 d_info->decoder_name = "phase_one_load_raw_c()"; 464 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 465 } 466 else if (load_raw == &LibRaw::quicktake_100_load_raw ) 467 { 468 // UNTESTED 469 d_info->decoder_name = "quicktake_100_load_raw()"; 470 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 471 } 472 else if (load_raw == &LibRaw::rollei_load_raw ) 473 { 474 // UNTESTED 475 d_info->decoder_name = "rollei_load_raw()"; 476 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 477 } 478 else if (load_raw == &LibRaw::sinar_4shot_load_raw ) 479 { 480 // UNTESTED 481 d_info->decoder_name = "sinar_4shot_load_raw()"; 482 d_info->decoder_flags = LIBRAW_DECODER_4COMPONENT; 483 } 484 else if (load_raw == &LibRaw::smal_v6_load_raw ) 485 { 486 // UNTESTED 487 d_info->decoder_name = "smal_v6_load_raw()"; 488 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 489 } 490 else if (load_raw == &LibRaw::smal_v9_load_raw ) 491 { 492 // UNTESTED 493 d_info->decoder_name = "smal_v9_load_raw()"; 494 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 495 } 496 else if (load_raw == &LibRaw::sony_load_raw ) 497 { 498 d_info->decoder_name = "sony_load_raw()"; 499 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 500 } 501 else if (load_raw == &LibRaw::sony_arw_load_raw ) 502 { 503 d_info->decoder_name = "sony_arw_load_raw()"; 504 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 505 } 506 else if (load_raw == &LibRaw::sony_arw2_load_raw ) 507 { 508 d_info->decoder_name = "sony_arw2_load_raw()"; 509 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 510 d_info->decoder_flags |= LIBRAW_DECODER_HASCURVE; 511 } 512 else if (load_raw == &LibRaw::unpacked_load_raw ) 513 { 514 d_info->decoder_name = "unpacked_load_raw()"; 515 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD | LIBRAW_DECODER_USEBAYER2; 516 } 517 else if (load_raw == &LibRaw::redcine_load_raw) 518 { 519 d_info->decoder_name = "redcine_load_raw()"; 520 d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; 521 d_info->decoder_flags |= LIBRAW_DECODER_HASCURVE; 522 } 523 else 524 { 525 d_info->decoder_name = "Unknown unpack function"; 526 d_info->decoder_flags = LIBRAW_DECODER_NOTSET; 527 } 528 return LIBRAW_SUCCESS; 529} 530 531int LibRaw::adjust_maximum() 532{ 533 int i; 534 ushort real_max; 535 float auto_threshold; 536 537 if(O.adjust_maximum_thr < 0.00001) 538 return LIBRAW_SUCCESS; 539 else if (O.adjust_maximum_thr > 0.99999) 540 auto_threshold = LIBRAW_DEFAULT_ADJUST_MAXIMUM_THRESHOLD; 541 else 542 auto_threshold = O.adjust_maximum_thr; 543 544 545 real_max = C.channel_maximum[0]; 546 for(i = 1; i< 4; i++) 547 if(real_max < C.channel_maximum[i]) 548 real_max = C.channel_maximum[i]; 549 550 if (real_max > 0 && real_max < C.maximum && real_max > C.maximum* auto_threshold) 551 { 552 C.maximum = real_max; 553 } 554 return LIBRAW_SUCCESS; 555} 556 557 558void LibRaw:: merror (void *ptr, const char *where) 559{ 560 if (ptr) return; 561 if(callbacks.mem_cb)(*callbacks.mem_cb)(callbacks.memcb_data, 562 libraw_internal_data.internal_data.input 563 ?libraw_internal_data.internal_data.input->fname() 564 :NULL, 565 where); 566 throw LIBRAW_EXCEPTION_ALLOC; 567} 568 569 570 571int LibRaw::open_file(const char *fname, INT64 max_buf_size) 572{ 573#ifndef WIN32 574 struct stat st; 575 if(stat(fname,&st)) 576 return LIBRAW_IO_ERROR; 577 int big = (st.st_size > max_buf_size)?1:0; 578#else 579 struct _stati64 st; 580 if(_stati64(fname,&st)) 581 return LIBRAW_IO_ERROR; 582 int big = (st.st_size > max_buf_size)?1:0; 583#endif 584 585 LibRaw_abstract_datastream *stream; 586 try { 587 if(big) 588 stream = new LibRaw_bigfile_datastream(fname); 589 else 590 stream = new LibRaw_file_datastream(fname); 591 } 592 593 catch (std::bad_alloc) 594 { 595 recycle(); 596 return LIBRAW_UNSUFFICIENT_MEMORY; 597 } 598 if(!stream->valid()) 599 { 600 delete stream; 601 return LIBRAW_IO_ERROR; 602 } 603 ID.input_internal = 0; // preserve from deletion on error 604 int ret = open_datastream(stream); 605 if (ret == LIBRAW_SUCCESS) 606 { 607 ID.input_internal =1 ; // flag to delete datastream on recycle 608 } 609 else 610 { 611 delete stream; 612 ID.input_internal = 0; 613 } 614 return ret; 615} 616 617int LibRaw::open_buffer(void *buffer, size_t size) 618{ 619 // this stream will close on recycle() 620 if(!buffer || buffer==(void*)-1) 621 return LIBRAW_IO_ERROR; 622 623 LibRaw_buffer_datastream *stream; 624 try { 625 stream = new LibRaw_buffer_datastream(buffer,size); 626 } 627 catch (std::bad_alloc) 628 { 629 recycle(); 630 return LIBRAW_UNSUFFICIENT_MEMORY; 631 } 632 if(!stream->valid()) 633 { 634 delete stream; 635 return LIBRAW_IO_ERROR; 636 } 637 ID.input_internal = 0; // preserve from deletion on error 638 int ret = open_datastream(stream); 639 if (ret == LIBRAW_SUCCESS) 640 { 641 ID.input_internal =1 ; // flag to delete datastream on recycle 642 } 643 else 644 { 645 delete stream; 646 ID.input_internal = 0; 647 } 648 return ret; 649} 650 651 652int LibRaw::open_datastream(LibRaw_abstract_datastream *stream) 653{ 654 655 if(!stream) 656 return ENOENT; 657 if(!stream->valid()) 658 return LIBRAW_IO_ERROR; 659 recycle(); 660 661 try { 662 ID.input = stream; 663 SET_PROC_FLAG(LIBRAW_PROGRESS_OPEN); 664 665 if (O.use_camera_matrix < 0) 666 O.use_camera_matrix = O.use_camera_wb; 667 668 identify(); 669 670 if(IO.fuji_width) 671 { 672 IO.fwidth = S.width; 673 IO.fheight = S.height; 674 S.iwidth = S.width = IO.fuji_width << (int)(!libraw_internal_data.unpacker_data.fuji_layout); 675 S.iheight = S.height = S.raw_height; 676 S.raw_height += 2*S.top_margin; 677 } 678 679 if(C.profile_length) 680 { 681 if(C.profile) free(C.profile); 682 C.profile = malloc(C.profile_length); 683 merror(C.profile,"LibRaw::open_file()"); 684 ID.input->seek(ID.profile_offset,SEEK_SET); 685 ID.input->read(C.profile,C.profile_length,1); 686 } 687 688 SET_PROC_FLAG(LIBRAW_PROGRESS_IDENTIFY); 689 } 690 catch ( LibRaw_exceptions err) { 691 EXCEPTION_HANDLER(err); 692 } 693 694 if(P1.raw_count < 1) 695 return LIBRAW_FILE_UNSUPPORTED; 696 697 698 write_fun = &LibRaw::write_ppm_tiff; 699 700 if (load_raw == &LibRaw::kodak_ycbcr_load_raw) 701 { 702 S.height += S.height & 1; 703 S.width += S.width & 1; 704 } 705 706 IO.shrink = P1.filters && (O.half_size || 707 ((O.threshold || O.aber[0] != 1 || O.aber[2] != 1) )); 708 709 S.iheight = (S.height + IO.shrink) >> IO.shrink; 710 S.iwidth = (S.width + IO.shrink) >> IO.shrink; 711 712 // Save color,sizes and internal data into raw_image fields 713 memmove(&imgdata.rawdata.color,&imgdata.color,sizeof(imgdata.color)); 714 memmove(&imgdata.rawdata.sizes,&imgdata.sizes,sizeof(imgdata.sizes)); 715 memmove(&imgdata.rawdata.iparams,&imgdata.idata,sizeof(imgdata.idata)); 716 memmove(&imgdata.rawdata.ioparams,&libraw_internal_data.internal_output_params,sizeof(libraw_internal_data.internal_output_params)); 717 718 SET_PROC_FLAG(LIBRAW_PROGRESS_SIZE_ADJUST); 719 720 721 return LIBRAW_SUCCESS; 722} 723 724int LibRaw::unpack(void) 725{ 726 CHECK_ORDER_HIGH(LIBRAW_PROGRESS_LOAD_RAW); 727 CHECK_ORDER_LOW(LIBRAW_PROGRESS_IDENTIFY); 728 try { 729 730 RUN_CALLBACK(LIBRAW_PROGRESS_LOAD_RAW,0,2); 731 if (O.shot_select >= P1.raw_count) 732 return LIBRAW_REQUEST_FOR_NONEXISTENT_IMAGE; 733 734 if(!load_raw) 735 return LIBRAW_UNSPECIFIED_ERROR; 736 737 if (O.use_camera_matrix && C.cmatrix[0][0] > 0.25) 738 { 739 memcpy (C.rgb_cam, C.cmatrix, sizeof (C.cmatrix)); 740 IO.raw_color = 0; 741 } 742 // already allocated ? 743 if(imgdata.image) 744 { 745 free(imgdata.image); 746 imgdata.image = 0; 747 } 748 749 if (libraw_internal_data.unpacker_data.meta_length) 750 { 751 libraw_internal_data.internal_data.meta_data = 752 (char *) malloc (libraw_internal_data.unpacker_data.meta_length); 753 merror (libraw_internal_data.internal_data.meta_data, "LibRaw::unpack()"); 754 } 755 ID.input->seek(libraw_internal_data.unpacker_data.data_offset, SEEK_SET); 756 int save_document_mode = O.document_mode; 757 O.document_mode = 0; 758 759 libraw_decoder_info_t decoder_info; 760 get_decoder_info(&decoder_info); 761 762 int save_iwidth = S.iwidth, save_iheight = S.iheight, save_shrink = IO.shrink; 763 764 int rwidth = S.raw_width, rheight = S.raw_height; 765 if( !IO.fuji_width) 766 { 767 // adjust non-Fuji allocation 768 if(rwidth < S.width + S.left_margin) 769 rwidth = S.width + S.left_margin; 770 if(rheight < S.height + S.top_margin) 771 rheight = S.height + S.top_margin; 772 } 773 774 if(decoder_info.decoder_flags & LIBRAW_DECODER_FLATFIELD) 775 { 776 imgdata.rawdata.raw_alloc = malloc(rwidth*rheight*sizeof(imgdata.rawdata.raw_image[0])); 777 imgdata.rawdata.raw_image = (ushort*) imgdata.rawdata.raw_alloc; 778 } 779 else if (decoder_info.decoder_flags & LIBRAW_DECODER_4COMPONENT) 780 { 781 S.iwidth = S.width; 782 S.iheight= S.height; 783 IO.shrink = 0; 784 imgdata.rawdata.raw_alloc = calloc(rwidth*rheight,sizeof(*imgdata.rawdata.color_image)); 785 imgdata.rawdata.color_image = (ushort(*)[4]) imgdata.rawdata.raw_alloc; 786 } 787 else if (decoder_info.decoder_flags & LIBRAW_DECODER_LEGACY) 788 { 789 // sRAW and Foveon only, so extra buffer size is just 1/4 790 // Legacy converters does not supports half mode! 791 S.iwidth = S.width; 792 S.iheight= S.height; 793 IO.shrink = 0; 794 // allocate image as temporary buffer, size 795 imgdata.rawdata.raw_alloc = calloc(S.iwidth*S.iheight,sizeof(*imgdata.image)); 796 imgdata.image = (ushort (*)[4]) imgdata.rawdata.raw_alloc; 797 } 798 799 800 (this->*load_raw)(); 801 802 803 // recover saved 804 if( decoder_info.decoder_flags & LIBRAW_DECODER_LEGACY) 805 { 806 imgdata.image = 0; 807 imgdata.rawdata.color_image = (ushort (*)[4]) imgdata.rawdata.raw_alloc; 808 } 809 810 // calculate channel maximum 811 { 812 for(int c=0;c<4;c++) C.channel_maximum[c] = 0; 813 if(decoder_info.decoder_flags & LIBRAW_DECODER_LEGACY) 814 { 815 for(int rc = 0; rc < S.iwidth*S.iheight; rc++) 816 { 817 if(C.channel_maximum[0]<imgdata.rawdata.color_image[rc][0]) 818 C.channel_maximum[0]=imgdata.rawdata.color_image[rc][0]; 819 if(C.channel_maximum[1]<imgdata.rawdata.color_image[rc][1]) 820 C.channel_maximum[1]=imgdata.rawdata.color_image[rc][1]; 821 if(C.channel_maximum[2]<imgdata.rawdata.color_image[rc][2]) 822 C.channel_maximum[2]=imgdata.rawdata.color_image[rc][2]; 823 if(C.channel_maximum[3]<imgdata.rawdata.color_image[rc][3]) 824 C.channel_maximum[3]=imgdata.rawdata.color_image[rc][3]; 825 } 826 } 827 else if(decoder_info.decoder_flags & LIBRAW_DECODER_4COMPONENT) 828 { 829 for(int row = S.top_margin; row < S.height+S.top_margin; row++) 830 for(int col = S.left_margin; col < S.width+S.left_margin; col++) 831 { 832 int rc = row*S.raw_width+col; 833 if(C.channel_maximum[0]<imgdata.rawdata.color_image[rc][0]) 834 C.channel_maximum[0]=imgdata.rawdata.color_image[rc][0]; 835 if(C.channel_maximum[1]<imgdata.rawdata.color_image[rc][1]) 836 C.channel_maximum[1]=imgdata.rawdata.color_image[rc][1]; 837 if(C.channel_maximum[2]<imgdata.rawdata.color_image[rc][2]) 838 C.channel_maximum[2]=imgdata.rawdata.color_image[rc][2]; 839 if(C.channel_maximum[3]<imgdata.rawdata.color_image[rc][3]) 840 C.channel_maximum[3]=imgdata.rawdata.color_image[rc][4]; 841 } 842 } 843 else if (decoder_info.decoder_flags & LIBRAW_DECODER_FLATFIELD) 844 { 845 for(int row = 0; row < S.height; row++) 846 { 847 int colors[4]; 848 for (int xx=0;xx<4;xx++) 849 colors[xx] = COLOR(row,xx); 850 for(int col = 0; col < S.width; col++) 851 { 852 int cc = colors[col&3]; 853 if(C.channel_maximum[cc] 854 < imgdata.rawdata.raw_image[(row+S.top_margin)*S.raw_width 855 +(col+S.left_margin)]) 856 C.channel_maximum[cc] = 857 imgdata.rawdata.raw_image[(row+S.top_margin)*S.raw_width 858 +(col+S.left_margin)]; 859 } 860 } 861 } 862 } 863 // recover image sizes 864 S.iwidth = save_iwidth; 865 S.iheight = save_iheight; 866 IO.shrink = save_shrink; 867 868 // phase-one black 869 if(imgdata.rawdata.ph1_black) 870 C.ph1_black = imgdata.rawdata.ph1_black; 871 O.document_mode = save_document_mode; 872 873 // adjust black to possible maximum 874 unsigned int i = C.cblack[3]; 875 unsigned int c; 876 for(c=0;c<3;c++) 877 if (i > C.cblack[c]) i = C.cblack[c]; 878 for (c=0;c<4;c++) 879 C.cblack[c] -= i; 880 C.black += i; 881 882 883 // Save color,sizes and internal data into raw_image fields 884 memmove(&imgdata.rawdata.color,&imgdata.color,sizeof(imgdata.color)); 885 memmove(&imgdata.rawdata.sizes,&imgdata.sizes,sizeof(imgdata.sizes)); 886 memmove(&imgdata.rawdata.iparams,&imgdata.idata,sizeof(imgdata.idata)); 887 memmove(&imgdata.rawdata.ioparams,&libraw_internal_data.internal_output_params,sizeof(libraw_internal_data.internal_output_params)); 888 889 SET_PROC_FLAG(LIBRAW_PROGRESS_LOAD_RAW); 890 RUN_CALLBACK(LIBRAW_PROGRESS_LOAD_RAW,1,2); 891 892 return 0; 893 } 894 catch ( LibRaw_exceptions err) { 895 EXCEPTION_HANDLER(err); 896 } 897} 898 899void LibRaw::free_image(void) 900{ 901 if(imgdata.image) 902 { 903 free(imgdata.image); 904 imgdata.image = 0; 905 imgdata.progress_flags 906 = LIBRAW_PROGRESS_START|LIBRAW_PROGRESS_OPEN 907 |LIBRAW_PROGRESS_IDENTIFY|LIBRAW_PROGRESS_SIZE_ADJUST|LIBRAW_PROGRESS_LOAD_RAW; 908 } 909} 910 911 912void LibRaw::raw2image_start() 913{ 914 // restore color,sizes and internal data into raw_image fields 915 memmove(&imgdata.color,&imgdata.rawdata.color,sizeof(imgdata.color)); 916 memmove(&imgdata.sizes,&imgdata.rawdata.sizes,sizeof(imgdata.sizes)); 917 memmove(&imgdata.idata,&imgdata.rawdata.iparams,sizeof(imgdata.idata)); 918 memmove(&libraw_internal_data.internal_output_params,&imgdata.rawdata.ioparams,sizeof(libraw_internal_data.internal_output_params)); 919 920 if (O.user_flip >= 0) 921 S.flip = O.user_flip; 922 923 switch ((S.flip+3600) % 360) 924 { 925 case 270: S.flip = 5; break; 926 case 180: S.flip = 3; break; 927 case 90: S.flip = 6; break; 928 } 929 930 // adjust for half mode! 931 IO.shrink = P1.filters && (O.half_size || 932 ((O.threshold || O.aber[0] != 1 || O.aber[2] != 1) )); 933 934 S.iheight = (S.height + IO.shrink) >> IO.shrink; 935 S.iwidth = (S.width + IO.shrink) >> IO.shrink; 936 937 if (O.user_black >= 0) 938 C.black = O.user_black; 939} 940 941// Same as raw2image, but 942// 1) Do raw2image and rotate_fuji_raw in one pass 943// 2) Do raw2image and cropping in one pass 944#ifndef MIN 945#define MIN(a,b) ((a) < (b) ? (a) : (b)) 946#endif 947int LibRaw::raw2image_ex(void) 948{ 949 CHECK_ORDER_LOW(LIBRAW_PROGRESS_LOAD_RAW); 950 951 raw2image_start(); 952 953 // process cropping 954 int do_crop = 0; 955 unsigned save_filters = imgdata.idata.filters; 956 unsigned save_width = S.width; 957 if (~O.cropbox[2] && ~O.cropbox[3]) 958 { 959 int crop[4],c,filt; 960 for(int c=0;c<4;c++) 961 { 962 crop[c] = O.cropbox[c]; 963 if(crop[c]<0) 964 crop[c]=0; 965 } 966 if(IO.fwidth) 967 { 968 crop[0] = (crop[0]/4)*4; 969 crop[1] = (crop[1]/4)*4; 970 } 971 do_crop = 1; 972 crop[2] = MIN (crop[2], (signed) S.width-crop[0]); 973 crop[3] = MIN (crop[3], (signed) S.height-crop[1]); 974 if (crop[2] <= 0 || crop[3] <= 0) 975 throw LIBRAW_EXCEPTION_BAD_CROP; 976 977 // adjust sizes! 978 S.left_margin+=crop[0]; 979 S.top_margin+=crop[1]; 980 S.width=crop[2]; 981 S.height=crop[3]; 982 983 S.iheight = (S.height + IO.shrink) >> IO.shrink; 984 S.iwidth = (S.width + IO.shrink) >> IO.shrink; 985 if(!IO.fwidth && imgdata.idata.filters) 986 { 987 for (filt=c=0; c < 16; c++) 988 filt |= FC((c >> 1)+(crop[1]), 989 (c & 1)+(crop[0])) << c*2; 990 imgdata.idata.filters = filt; 991 } 992 } 993 994 if(IO.fwidth) 995 { 996 ushort fiwidth,fiheight; 997 if(do_crop) 998 { 999 IO.fuji_width = S.width >> !libraw_internal_data.unpacker_data.fuji_layout; 1000 IO.fwidth = (S.height >> libraw_internal_data.unpacker_data.fuji_layout) + IO.fuji_width; 1001 IO.fheight = IO.fwidth - 1; 1002 } 1003 1004 fiheight = (IO.fheight + IO.shrink) >> IO.shrink; 1005 fiwidth = (IO.fwidth + IO.shrink) >> IO.shrink; 1006 if(imgdata.image) 1007 { 1008 imgdata.image = (ushort (*)[4])realloc(imgdata.image,fiheight*fiwidth*sizeof (*imgdata.image)); 1009 memset(imgdata.image,0,fiheight*fiwidth *sizeof (*imgdata.image)); 1010 } 1011 else 1012 imgdata.image = (ushort (*)[4]) calloc (fiheight*fiwidth, sizeof (*imgdata.image)); 1013 merror (imgdata.image, "raw2image_ex()"); 1014 1015 int cblk[4],i; 1016 for(i=0;i<4;i++) 1017 cblk[i] = C.cblack[i]+C.black; 1018 ZERO(C.channel_maximum); 1019 1020 int row,col; 1021 for(row=0;row<S.height;row++) 1022 { 1023 for(col=0;col<S.width;col++) 1024 { 1025 int r,c; 1026 if (libraw_internal_data.unpacker_data.fuji_layout) { 1027 r = IO.fuji_width - 1 - col + (row >> 1); 1028 c = col + ((row+1) >> 1); 1029 } else { 1030 r = IO.fuji_width - 1 + row - (col >> 1); 1031 c = row + ((col+1) >> 1); 1032 } 1033 1034 int val = imgdata.rawdata.raw_image[(row+S.top_margin)*S.raw_width 1035 +(col+S.left_margin)]; 1036 int cc = FCF(row,col); 1037 if(val > cblk[cc]) 1038 val -= cblk[cc]; 1039 else 1040 val = 0; 1041 imgdata.image[((r) >> IO.shrink)*fiwidth + ((c) >> IO.shrink)][cc] = val; 1042 if(C.channel_maximum[cc] < val) C.channel_maximum[cc] = val; 1043 } 1044 } 1045 C.maximum -= C.black; 1046 ZERO(C.cblack); 1047 C.black = 0; 1048 1049 // restore fuji sizes! 1050 S.height = IO.fheight; 1051 S.width = IO.fwidth; 1052 S.iheight = (S.height + IO.shrink) >> IO.shrink; 1053 S.iwidth = (S.width + IO.shrink) >> IO.shrink; 1054 S.raw_height -= 2*S.top_margin; 1055 } 1056 else 1057 { 1058 1059 if(imgdata.image) 1060 { 1061 imgdata.image = (ushort (*)[4]) realloc (imgdata.image,S.iheight*S.iwidth 1062 *sizeof (*imgdata.image)); 1063 memset(imgdata.image,0,S.iheight*S.iwidth *sizeof (*imgdata.image)); 1064 } 1065 else 1066 imgdata.image = (ushort (*)[4]) calloc (S.iheight*S.iwidth, sizeof (*imgdata.image)); 1067 1068 merror (imgdata.image, "raw2image_ex()"); 1069 1070 libraw_decoder_info_t decoder_info; 1071 get_decoder_info(&decoder_info); 1072 1073 1074 if(decoder_info.decoder_flags & LIBRAW_DECODER_FLATFIELD) 1075 { 1076 if(decoder_info.decoder_flags & LIBRAW_DECODER_USEBAYER2) 1077#if defined(LIBRAW_USE_OPENMP) 1078#pragma omp parallel for default(shared) 1079#endif 1080 for(int row = 0; row < S.height; row++) 1081 for(int col = 0; col < S.width; col++) 1082 imgdata.image[(row >> IO.shrink)*S.iwidth + (col>>IO.shrink)][fc(row,col)] 1083 = imgdata.rawdata.raw_image[(row+S.top_margin)*S.raw_width 1084 +(col+S.left_margin)]; 1085 else 1086#if defined(LIBRAW_USE_OPENMP) 1087#pragma omp parallel for default(shared) 1088#endif 1089 for(int row = 0; row < S.height; row++) 1090 { 1091 int colors[2]; 1092 for (int xx=0;xx<2;xx++) 1093 colors[xx] = COLOR(row,xx); 1094 for(int col = 0; col < S.width; col++) 1095 { 1096 int cc = colors[col&1]; 1097 imgdata.image[(row >> IO.shrink)*S.iwidth + (col>>IO.shrink)][cc] = 1098 imgdata.rawdata.raw_image[(row+S.top_margin)*S.raw_width 1099 +(col+S.left_margin)]; 1100 } 1101 } 1102 } 1103 else if (decoder_info.decoder_flags & LIBRAW_DECODER_4COMPONENT) 1104 { 1105#define FC0(row,col) (save_filters >> ((((row) << 1 & 14) + ((col) & 1)) << 1) & 3) 1106 if(IO.shrink) 1107#if defined(LIBRAW_USE_OPENMP) 1108#pragma omp parallel for default(shared) 1109#endif 1110 for(int row = 0; row < S.height; row++) 1111 for(int col = 0; col < S.width; col++) 1112 imgdata.image[(row >> IO.shrink)*S.iwidth + (col>>IO.shrink)][FC(row,col)] 1113 = imgdata.rawdata.color_image[(row+S.top_margin)*S.raw_width 1114 +S.left_margin+col] 1115 [FC0(row+S.top_margin,col+S.left_margin)]; 1116#undef FC0 1117 else 1118#if defined(LIBRAW_USE_OPENMP) 1119#pragma omp parallel for default(shared) 1120#endif 1121 for(int row = 0; row < S.height; row++) 1122 memmove(&imgdata.image[row*S.width], 1123 &imgdata.rawdata.color_image[(row+S.top_margin)*S.raw_width+S.left_margin], 1124 S.width*sizeof(*imgdata.image)); 1125 } 1126 else if(decoder_info.decoder_flags & LIBRAW_DECODER_LEGACY) 1127 { 1128 if(do_crop) 1129#if defined(LIBRAW_USE_OPENMP) 1130#pragma omp parallel for default(shared) 1131#endif 1132 for(int row = 0; row < S.height; row++) 1133 memmove(&imgdata.image[row*S.width], 1134 &imgdata.rawdata.color_image[(row+S.top_margin)*save_width+S.left_margin], 1135 S.width*sizeof(*imgdata.image)); 1136 1137 else 1138 memmove(imgdata.image,imgdata.rawdata.color_image, 1139 S.width*S.height*sizeof(*imgdata.image)); 1140 } 1141 1142 if(imgdata.rawdata.use_ph1_correct) // Phase one unpacked! 1143 phase_one_correct(); 1144 } 1145 return LIBRAW_SUCCESS; 1146} 1147 1148#undef MIN 1149 1150 1151 1152 1153int LibRaw::raw2image(void) 1154{ 1155 1156 CHECK_ORDER_LOW(LIBRAW_PROGRESS_LOAD_RAW); 1157 1158 try { 1159 raw2image_start(); 1160 1161 // free and re-allocate image bitmap 1162 if(imgdata.image) 1163 { 1164 imgdata.image = (ushort (*)[4]) realloc (imgdata.image,S.iheight*S.iwidth *sizeof (*imgdata.image)); 1165 memset(imgdata.image,0,S.iheight*S.iwidth *sizeof (*imgdata.image)); 1166 } 1167 else 1168 imgdata.image = (ushort (*)[4]) calloc (S.iheight*S.iwidth, sizeof (*imgdata.image)); 1169 1170 merror (imgdata.image, "raw2image()"); 1171 1172 libraw_decoder_info_t decoder_info; 1173 get_decoder_info(&decoder_info); 1174 1175 // Move saved bitmap to imgdata.image 1176 if(decoder_info.decoder_flags & LIBRAW_DECODER_FLATFIELD) 1177 { 1178 if(decoder_info.decoder_flags & LIBRAW_DECODER_USEBAYER2) 1179 { 1180 for(int row = 0; row < S.height; row++) 1181 for(int col = 0; col < S.width; col++) 1182 imgdata.image[(row >> IO.shrink)*S.iwidth + (col>>IO.shrink)][fc(row,col)] 1183 = imgdata.rawdata.raw_image[(row+S.top_margin)*S.raw_width 1184 +(col+S.left_margin)]; 1185 } 1186 else 1187 { 1188 for(int row = 0; row < S.height; row++) 1189 { 1190 int colors[4]; 1191 for (int xx=0;xx<4;xx++) 1192 colors[xx] = COLOR(row,xx); 1193 for(int col = 0; col < S.width; col++) 1194 { 1195 int cc = colors[col&3]; 1196 imgdata.image[(row >> IO.shrink)*S.iwidth + (col>>IO.shrink)][cc] = 1197 imgdata.rawdata.raw_image[(row+S.top_margin)*S.raw_width+(col 1198 +S.left_margin)]; 1199 } 1200 } 1201 } 1202 } 1203 else if (decoder_info.decoder_flags & LIBRAW_DECODER_4COMPONENT) 1204 { 1205 if(IO.shrink) 1206 { 1207 for(int row = 0; row < S.height; row++) 1208 for(int col = 0; col < S.width; col++) 1209 { 1210 int cc = FC(row,col); 1211 imgdata.image[(row >> IO.shrink)*S.iwidth + (col>>IO.shrink)][cc] 1212 = imgdata.rawdata.color_image[(row+S.top_margin)*S.raw_width 1213 +S.left_margin+col][cc]; 1214 } 1215 } 1216 else 1217 for(int row = 0; row < S.height; row++) 1218 memmove(&imgdata.image[row*S.width], 1219 &imgdata.rawdata.color_image[(row+S.top_margin)*S.raw_width+S.left_margin], 1220 S.width*sizeof(*imgdata.image)); 1221 } 1222 else if(decoder_info.decoder_flags & LIBRAW_DECODER_LEGACY) 1223 { 1224 // legacy is always 4channel and not shrinked! 1225 memmove(imgdata.image,imgdata.rawdata.color_image,S.width*S.height*sizeof(*imgdata.image)); 1226 } 1227 1228 if(imgdata.rawdata.use_ph1_correct) // Phase one unpacked! 1229 phase_one_correct(); 1230 1231 // hack - clear later flags! 1232 imgdata.progress_flags 1233 = LIBRAW_PROGRESS_START|LIBRAW_PROGRESS_OPEN 1234 |LIBRAW_PROGRESS_IDENTIFY|LIBRAW_PROGRESS_SIZE_ADJUST|LIBRAW_PROGRESS_LOAD_RAW; 1235 return 0; 1236 } 1237 catch ( LibRaw_exceptions err) { 1238 EXCEPTION_HANDLER(err); 1239 } 1240} 1241 1242 1243int LibRaw::dcraw_document_mode_processing(void) 1244{ 1245// CHECK_ORDER_HIGH(LIBRAW_PROGRESS_PRE_INTERPOLATE); 1246 CHECK_ORDER_LOW(LIBRAW_PROGRESS_LOAD_RAW); 1247 1248 try { 1249 1250 int no_crop = 1; 1251 1252 if (~O.cropbox[2] && ~O.cropbox[3]) 1253 no_crop=0; 1254 1255 raw2image_ex(); // raw2image+crop+rotate_fuji_raw 1256 1257 if (IO.zero_is_bad) 1258 { 1259 remove_zeroes(); 1260 SET_PROC_FLAG(LIBRAW_PROGRESS_REMOVE_ZEROES); 1261 } 1262 1263 if(!IO.fuji_width) 1264 subtract_black(); 1265 1266 O.document_mode = 2; 1267 1268 if(P1.is_foveon) 1269 { 1270 // filter image data for foveon document mode 1271 short *iptr = (short *)imgdata.image; 1272 for (int i=0; i < S.height*S.width*4; i++) 1273 { 1274 if ((short) iptr[i] < 0) 1275 iptr[i] = 0; 1276 } 1277 SET_PROC_FLAG(LIBRAW_PROGRESS_FOVEON_INTERPOLATE); 1278 } 1279 1280 O.use_fuji_rotate = 0; 1281 1282 if(O.bad_pixels && no_crop) 1283 { 1284 bad_pixels(O.bad_pixels); 1285 SET_PROC_FLAG(LIBRAW_PROGRESS_BAD_PIXELS); 1286 } 1287 if (O.dark_frame && no_crop) 1288 { 1289 subtract (O.dark_frame); 1290 SET_PROC_FLAG(LIBRAW_PROGRESS_DARK_FRAME); 1291 } 1292 1293 1294 adjust_maximum(); 1295 1296 if (O.user_sat > 0) 1297 C.maximum = O.user_sat; 1298 1299 pre_interpolate(); 1300 SET_PROC_FLAG(LIBRAW_PROGRESS_PRE_INTERPOLATE); 1301 1302 if (libraw_internal_data.internal_output_params.mix_green) 1303 { 1304 int i; 1305 for (P1.colors=3, i=0; i < S.height*S.width; i++) 1306 imgdata.image[i][1] = (imgdata.image[i][1] + imgdata.image[i][3]) >> 1; 1307 } 1308 SET_PROC_FLAG(LIBRAW_PROGRESS_MIX_GREEN); 1309 1310 if (!P1.is_foveon && P1.colors == 3) 1311 median_filter(); 1312 SET_PROC_FLAG(LIBRAW_PROGRESS_MEDIAN_FILTER); 1313 1314 if (!P1.is_foveon && O.highlight == 2) 1315 blend_highlights(); 1316 1317 if (!P1.is_foveon && O.highlight > 2) 1318 recover_highlights(); 1319 SET_PROC_FLAG(LIBRAW_PROGRESS_HIGHLIGHTS); 1320 1321 if (O.use_fuji_rotate) 1322 fuji_rotate(); 1323 SET_PROC_FLAG(LIBRAW_PROGRESS_FUJI_ROTATE); 1324#ifndef NO_LCMS 1325 if(O.camera_profile) 1326 { 1327 apply_profile(O.camera_profile,O.output_profile); 1328 SET_PROC_FLAG(LIBRAW_PROGRESS_APPLY_PROFILE); 1329 } 1330#endif 1331 if(!libraw_internal_data.output_data.histogram) 1332 { 1333 libraw_internal_data.output_data.histogram = (int (*)[LIBRAW_HISTOGRAM_SIZE]) malloc(sizeof(*libraw_internal_data.output_data.histogram)*4); 1334 merror(libraw_internal_data.output_data.histogram,"LibRaw::dcraw_document_mode_processing()"); 1335 } 1336 convert_to_rgb(); 1337 SET_PROC_FLAG(LIBRAW_…
Large files files are truncated, but you can click here to view the full file