/src/FreeImage/Source/LibRawLite/src/libraw_cxx.cpp
https://bitbucket.org/cabalistic/ogredeps/ · C++ · 2789 lines · 2394 code · 297 blank · 98 comment · 522 complexity · b4e1584f4136f83772c17d3d5c8a25df MD5 · raw file
Large files are truncated click here to view the full file
- /* -*- C++ -*-
- * File: libraw_cxx.cpp
- * Copyright 2008-2010 LibRaw LLC (info@libraw.org)
- * Created: Sat Mar 8 , 2008
- *
- * LibRaw C++ interface (implementation)
- LibRaw is free software; you can redistribute it and/or modify
- it under the terms of the one of three licenses as you choose:
- 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
- (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
- 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
- (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
- 3. LibRaw Software License 27032010
- (See file LICENSE.LibRaw.pdf provided in LibRaw distribution archive for details).
- */
- #include <math.h>
- #include <errno.h>
- #include <float.h>
- #include <new>
- #include <sys/types.h>
- #include <sys/stat.h>
- #ifndef WIN32
- #include <netinet/in.h>
- #else
- #include <winsock2.h>
- #endif
- #define LIBRAW_LIBRARY_BUILD
- #include "libraw/libraw.h"
- #include "internal/defines.h"
- #ifdef __cplusplus
- extern "C"
- {
- #endif
- void default_memory_callback(void *,const char *file,const char *where)
- {
- fprintf (stderr,"%s: Out of memory in %s\n", file?file:"unknown file", where);
- }
- void default_data_callback(void*,const char *file, const int offset)
- {
- if(offset < 0)
- fprintf (stderr,"%s: Unexpected end of file\n", file?file:"unknown file");
- else
- fprintf (stderr,"%s: data corrupted at %d\n",file?file:"unknown file",offset);
- }
- const char *libraw_strerror(int e)
- {
- enum LibRaw_errors errorcode = (LibRaw_errors)e;
- switch(errorcode)
- {
- case LIBRAW_SUCCESS:
- return "No error";
- case LIBRAW_UNSPECIFIED_ERROR:
- return "Unspecified error";
- case LIBRAW_FILE_UNSUPPORTED:
- return "Unsupported file format or not RAW file";
- case LIBRAW_REQUEST_FOR_NONEXISTENT_IMAGE:
- return "Request for nonexisting image number";
- case LIBRAW_OUT_OF_ORDER_CALL:
- return "Out of order call of libraw function";
- case LIBRAW_NO_THUMBNAIL:
- return "No thumbnail in file";
- case LIBRAW_UNSUPPORTED_THUMBNAIL:
- return "Unsupported thumbnail format";
- case LIBRAW_UNSUFFICIENT_MEMORY:
- return "Unsufficient memory";
- case LIBRAW_DATA_ERROR:
- return "Corrupted data or unexpected EOF";
- case LIBRAW_IO_ERROR:
- return "Input/output error";
- case LIBRAW_CANCELLED_BY_CALLBACK:
- return "Cancelled by user callback";
- case LIBRAW_BAD_CROP:
- return "Bad crop box";
- default:
- return "Unknown error code";
- }
- }
- #ifdef __cplusplus
- }
- #endif
- const double LibRaw_constants::xyz_rgb[3][3] =
- {
- { 0.412453, 0.357580, 0.180423 },
- { 0.212671, 0.715160, 0.072169 },
- { 0.019334, 0.119193, 0.950227 }
- };
- const float LibRaw_constants::d65_white[3] = { 0.950456f, 1.0f, 1.088754f };
- #define P1 imgdata.idata
- #define S imgdata.sizes
- #define O imgdata.params
- #define C imgdata.color
- #define T imgdata.thumbnail
- #define IO libraw_internal_data.internal_output_params
- #define ID libraw_internal_data.internal_data
- #define EXCEPTION_HANDLER(e) do{ \
- /* fprintf(stderr,"Exception %d caught\n",e);*/ \
- switch(e) \
- { \
- case LIBRAW_EXCEPTION_ALLOC: \
- recycle(); \
- return LIBRAW_UNSUFFICIENT_MEMORY; \
- case LIBRAW_EXCEPTION_DECODE_RAW: \
- case LIBRAW_EXCEPTION_DECODE_JPEG: \
- recycle(); \
- return LIBRAW_DATA_ERROR; \
- case LIBRAW_EXCEPTION_DECODE_JPEG2000: \
- recycle(); \
- return LIBRAW_DATA_ERROR; \
- case LIBRAW_EXCEPTION_IO_EOF: \
- case LIBRAW_EXCEPTION_IO_CORRUPT: \
- recycle(); \
- return LIBRAW_IO_ERROR; \
- case LIBRAW_EXCEPTION_CANCELLED_BY_CALLBACK:\
- recycle(); \
- return LIBRAW_CANCELLED_BY_CALLBACK; \
- case LIBRAW_EXCEPTION_BAD_CROP: \
- recycle(); \
- return LIBRAW_BAD_CROP; \
- default: \
- return LIBRAW_UNSPECIFIED_ERROR; \
- } \
- }while(0)
- const char* LibRaw::version() { return LIBRAW_VERSION_STR;}
- int LibRaw::versionNumber() { return LIBRAW_VERSION; }
- const char* LibRaw::strerror(int p) { return libraw_strerror(p);}
- void LibRaw::derror()
- {
- if (!libraw_internal_data.unpacker_data.data_error && libraw_internal_data.internal_data.input)
- {
- if (libraw_internal_data.internal_data.input->eof())
- {
- if(callbacks.data_cb)(*callbacks.data_cb)(callbacks.datacb_data,
- libraw_internal_data.internal_data.input->fname(),-1);
- throw LIBRAW_EXCEPTION_IO_EOF;
- }
- else
- {
- if(callbacks.data_cb)(*callbacks.data_cb)(callbacks.datacb_data,
- libraw_internal_data.internal_data.input->fname(),
- libraw_internal_data.internal_data.input->tell());
- throw LIBRAW_EXCEPTION_IO_CORRUPT;
- }
- }
- libraw_internal_data.unpacker_data.data_error++;
- }
- void LibRaw::dcraw_clear_mem(libraw_processed_image_t* p)
- {
- if(p) ::free(p);
- }
- #define ZERO(a) memset(&a,0,sizeof(a))
- LibRaw:: LibRaw(unsigned int flags)
- {
- double aber[4] = {1,1,1,1};
- double gamm[6] = { 0.45,4.5,0,0,0,0 };
- unsigned greybox[4] = { 0, 0, UINT_MAX, UINT_MAX };
- unsigned cropbox[4] = { 0, 0, UINT_MAX, UINT_MAX };
- #ifdef DCRAW_VERBOSE
- verbose = 1;
- #else
- verbose = 0;
- #endif
- ZERO(imgdata);
- ZERO(libraw_internal_data);
- ZERO(callbacks);
- callbacks.mem_cb = (flags & LIBRAW_OPIONS_NO_MEMERR_CALLBACK) ? NULL: &default_memory_callback;
- callbacks.data_cb = (flags & LIBRAW_OPIONS_NO_DATAERR_CALLBACK)? NULL : &default_data_callback;
- memmove(&imgdata.params.aber,&aber,sizeof(aber));
- memmove(&imgdata.params.gamm,&gamm,sizeof(gamm));
- memmove(&imgdata.params.greybox,&greybox,sizeof(greybox));
- memmove(&imgdata.params.cropbox,&cropbox,sizeof(cropbox));
-
- imgdata.params.bright=1;
- imgdata.params.use_camera_matrix=-1;
- imgdata.params.user_flip=-1;
- imgdata.params.user_black=-1;
- imgdata.params.user_sat=-1;
- imgdata.params.user_qual=-1;
- imgdata.params.output_color=1;
- imgdata.params.output_bps=8;
- imgdata.params.use_fuji_rotate=1;
- imgdata.params.exp_shift = 1.0;
- imgdata.params.auto_bright_thr = LIBRAW_DEFAULT_AUTO_BRIGHTNESS_THRESHOLD;
- imgdata.params.adjust_maximum_thr= LIBRAW_DEFAULT_ADJUST_MAXIMUM_THRESHOLD;
- imgdata.params.green_matching = 0;
- imgdata.parent_class = this;
- imgdata.progress_flags = 0;
- tls = new LibRaw_TLS;
- tls->init();
- }
- void* LibRaw:: malloc(size_t t)
- {
- void *p = memmgr.malloc(t);
- return p;
- }
- void* LibRaw:: realloc(void *q,size_t t)
- {
- void *p = memmgr.realloc(q,t);
- return p;
- }
- void* LibRaw:: calloc(size_t n,size_t t)
- {
- void *p = memmgr.calloc(n,t);
- return p;
- }
- void LibRaw:: free(void *p)
- {
- memmgr.free(p);
- }
- int LibRaw:: fc (int row, int col)
- {
- static const char filter[16][16] =
- { { 2,1,1,3,2,3,2,0,3,2,3,0,1,2,1,0 },
- { 0,3,0,2,0,1,3,1,0,1,1,2,0,3,3,2 },
- { 2,3,3,2,3,1,1,3,3,1,2,1,2,0,0,3 },
- { 0,1,0,1,0,2,0,2,2,0,3,0,1,3,2,1 },
- { 3,1,1,2,0,1,0,2,1,3,1,3,0,1,3,0 },
- { 2,0,0,3,3,2,3,1,2,0,2,0,3,2,2,1 },
- { 2,3,3,1,2,1,2,1,2,1,1,2,3,0,0,1 },
- { 1,0,0,2,3,0,0,3,0,3,0,3,2,1,2,3 },
- { 2,3,3,1,1,2,1,0,3,2,3,0,2,3,1,3 },
- { 1,0,2,0,3,0,3,2,0,1,1,2,0,1,0,2 },
- { 0,1,1,3,3,2,2,1,1,3,3,0,2,1,3,2 },
- { 2,3,2,0,0,1,3,0,2,0,1,2,3,0,1,0 },
- { 1,3,1,2,3,2,3,2,0,2,0,1,1,0,3,0 },
- { 0,2,0,3,1,0,0,1,1,3,3,2,3,2,2,1 },
- { 2,1,3,2,3,1,2,1,0,3,0,2,0,2,0,2 },
- { 0,3,1,0,0,2,0,3,2,1,3,1,1,3,1,3 } };
-
- if (imgdata.idata.filters != 1) return FC(row,col);
- return filter[(row+imgdata.sizes.top_margin) & 15][(col+imgdata.sizes.left_margin) & 15];
- }
- void LibRaw:: recycle()
- {
- if(libraw_internal_data.internal_data.input && libraw_internal_data.internal_data.input_internal)
- {
- delete libraw_internal_data.internal_data.input;
- libraw_internal_data.internal_data.input = NULL;
- }
- libraw_internal_data.internal_data.input_internal = 0;
- #define FREE(a) do { if(a) { free(a); a = NULL;} }while(0)
-
- FREE(imgdata.image);
- FREE(imgdata.thumbnail.thumb);
- FREE(libraw_internal_data.internal_data.meta_data);
- FREE(libraw_internal_data.output_data.histogram);
- FREE(libraw_internal_data.output_data.oprof);
- FREE(imgdata.color.profile);
- FREE(imgdata.rawdata.ph1_black);
- FREE(imgdata.rawdata.raw_alloc);
- #undef FREE
- ZERO(imgdata.rawdata);
- ZERO(imgdata.sizes);
- ZERO(imgdata.color);
- ZERO(libraw_internal_data);
- memmgr.cleanup();
- imgdata.thumbnail.tformat = LIBRAW_THUMBNAIL_UNKNOWN;
- imgdata.progress_flags = 0;
-
- tls->init();
- }
- const char * LibRaw::unpack_function_name()
- {
- libraw_decoder_info_t decoder_info;
- get_decoder_info(&decoder_info);
- return decoder_info.decoder_name;
- }
- int LibRaw::get_decoder_info(libraw_decoder_info_t* d_info)
- {
- if(!d_info) return LIBRAW_UNSPECIFIED_ERROR;
- if(!load_raw) return LIBRAW_OUT_OF_ORDER_CALL;
-
- d_info->decoder_flags = LIBRAW_DECODER_NOTSET;
- // sorted names order
- if (load_raw == &LibRaw::adobe_dng_load_raw_lj)
- {
- // Check rbayer
- d_info->decoder_name = "adobe_dng_load_raw_lj()";
- d_info->decoder_flags = imgdata.idata.filters ? LIBRAW_DECODER_FLATFIELD : LIBRAW_DECODER_4COMPONENT ;
- d_info->decoder_flags |= LIBRAW_DECODER_HASCURVE;
- }
- else if (load_raw == &LibRaw::adobe_dng_load_raw_nc)
- {
- // Check rbayer
- d_info->decoder_name = "adobe_dng_load_raw_nc()";
- d_info->decoder_flags = imgdata.idata.filters ? LIBRAW_DECODER_FLATFIELD : LIBRAW_DECODER_4COMPONENT;
- d_info->decoder_flags |= LIBRAW_DECODER_HASCURVE;
- }
- else if (load_raw == &LibRaw::canon_600_load_raw)
- {
- d_info->decoder_name = "canon_600_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD; // WB set within decoder, no need to load raw
- }
- else if (load_raw == &LibRaw::canon_compressed_load_raw)
- {
- d_info->decoder_name = "canon_compressed_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- }
- else if (load_raw == &LibRaw::canon_sraw_load_raw)
- {
- d_info->decoder_name = "canon_sraw_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_LEGACY;
- }
- else if (load_raw == &LibRaw::eight_bit_load_raw )
- {
- d_info->decoder_name = "eight_bit_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- d_info->decoder_flags |= LIBRAW_DECODER_HASCURVE;
- }
- else if (load_raw == &LibRaw::foveon_load_raw )
- {
- d_info->decoder_name = "foveon_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_LEGACY;
- }
- else if (load_raw == &LibRaw::fuji_load_raw )
- {
- d_info->decoder_name = "fuji_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- }
- else if (load_raw == &LibRaw::hasselblad_load_raw )
- {
- d_info->decoder_name = "hasselblad_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- }
- else if (load_raw == &LibRaw::imacon_full_load_raw )
- {
- d_info->decoder_name = "imacon_full_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_4COMPONENT;
- }
- else if (load_raw == &LibRaw::kodak_262_load_raw )
- {
- d_info->decoder_name = "kodak_262_load_raw()"; // UNTESTED!
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- d_info->decoder_flags |= LIBRAW_DECODER_HASCURVE;
- }
- else if (load_raw == &LibRaw::kodak_65000_load_raw )
- {
- d_info->decoder_name = "kodak_65000_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- d_info->decoder_flags |= LIBRAW_DECODER_HASCURVE;
- }
- else if (load_raw == &LibRaw::kodak_dc120_load_raw )
- {
- d_info->decoder_name = "kodak_dc120_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- }
- else if (load_raw == &LibRaw::kodak_jpeg_load_raw )
- {
- // UNTESTED + RBAYER
- d_info->decoder_name = "kodak_jpeg_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- }
- else if (load_raw == &LibRaw::kodak_radc_load_raw )
- {
- d_info->decoder_name = "kodak_radc_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_4COMPONENT;
- }
- else if (load_raw == &LibRaw::kodak_rgb_load_raw )
- {
- // UNTESTED
- d_info->decoder_name = "kodak_rgb_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_4COMPONENT;
- }
- else if (load_raw == &LibRaw::kodak_yrgb_load_raw )
- {
- d_info->decoder_name = "kodak_yrgb_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_4COMPONENT;
- d_info->decoder_flags |= LIBRAW_DECODER_HASCURVE;
- }
- else if (load_raw == &LibRaw::kodak_ycbcr_load_raw )
- {
- // UNTESTED
- d_info->decoder_name = "kodak_ycbcr_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_4COMPONENT;
- d_info->decoder_flags |= LIBRAW_DECODER_HASCURVE;
- }
- else if (load_raw == &LibRaw::leaf_hdr_load_raw )
- {
- d_info->decoder_name = "leaf_hdr_load_raw()";
- d_info->decoder_flags = imgdata.idata.filters ? LIBRAW_DECODER_FLATFIELD : LIBRAW_DECODER_4COMPONENT;
- }
- else if (load_raw == &LibRaw::lossless_jpeg_load_raw)
- {
- // Check rbayer
- d_info->decoder_name = "lossless_jpeg_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD | LIBRAW_DECODER_HASCURVE;
- }
- else if (load_raw == &LibRaw::minolta_rd175_load_raw )
- {
- // UNTESTED
- d_info->decoder_name = "minolta_rd175_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- }
- else if (load_raw == &LibRaw::nikon_compressed_load_raw)
- {
- // Check rbayer
- d_info->decoder_name = "nikon_compressed_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- }
- else if (load_raw == &LibRaw::nokia_load_raw )
- {
- // UNTESTED
- d_info->decoder_name = "nokia_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- }
- else if (load_raw == &LibRaw::olympus_load_raw )
- {
- d_info->decoder_name = "olympus_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- }
- else if (load_raw == &LibRaw::packed_load_raw )
- {
- d_info->decoder_name = "packed_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- }
- else if (load_raw == &LibRaw::panasonic_load_raw )
- {
- d_info->decoder_name = "panasonic_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- }
- else if (load_raw == &LibRaw::pentax_load_raw )
- {
- d_info->decoder_name = "pentax_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- }
- else if (load_raw == &LibRaw::phase_one_load_raw )
- {
- d_info->decoder_name = "phase_one_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- }
- else if (load_raw == &LibRaw::phase_one_load_raw_c )
- {
- d_info->decoder_name = "phase_one_load_raw_c()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- }
- else if (load_raw == &LibRaw::quicktake_100_load_raw )
- {
- // UNTESTED
- d_info->decoder_name = "quicktake_100_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- }
- else if (load_raw == &LibRaw::rollei_load_raw )
- {
- // UNTESTED
- d_info->decoder_name = "rollei_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- }
- else if (load_raw == &LibRaw::sinar_4shot_load_raw )
- {
- // UNTESTED
- d_info->decoder_name = "sinar_4shot_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_4COMPONENT;
- }
- else if (load_raw == &LibRaw::smal_v6_load_raw )
- {
- // UNTESTED
- d_info->decoder_name = "smal_v6_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- }
- else if (load_raw == &LibRaw::smal_v9_load_raw )
- {
- // UNTESTED
- d_info->decoder_name = "smal_v9_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- }
- else if (load_raw == &LibRaw::sony_load_raw )
- {
- d_info->decoder_name = "sony_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- }
- else if (load_raw == &LibRaw::sony_arw_load_raw )
- {
- d_info->decoder_name = "sony_arw_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- }
- else if (load_raw == &LibRaw::sony_arw2_load_raw )
- {
- d_info->decoder_name = "sony_arw2_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- d_info->decoder_flags |= LIBRAW_DECODER_HASCURVE;
- }
- else if (load_raw == &LibRaw::unpacked_load_raw )
- {
- d_info->decoder_name = "unpacked_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD | LIBRAW_DECODER_USEBAYER2;
- }
- else if (load_raw == &LibRaw::redcine_load_raw)
- {
- d_info->decoder_name = "redcine_load_raw()";
- d_info->decoder_flags = LIBRAW_DECODER_FLATFIELD;
- d_info->decoder_flags |= LIBRAW_DECODER_HASCURVE;
- }
- else
- {
- d_info->decoder_name = "Unknown unpack function";
- d_info->decoder_flags = LIBRAW_DECODER_NOTSET;
- }
- return LIBRAW_SUCCESS;
- }
- int LibRaw::adjust_maximum()
- {
- int i;
- ushort real_max;
- float auto_threshold;
- if(O.adjust_maximum_thr < 0.00001)
- return LIBRAW_SUCCESS;
- else if (O.adjust_maximum_thr > 0.99999)
- auto_threshold = LIBRAW_DEFAULT_ADJUST_MAXIMUM_THRESHOLD;
- else
- auto_threshold = O.adjust_maximum_thr;
-
-
- real_max = C.channel_maximum[0];
- for(i = 1; i< 4; i++)
- if(real_max < C.channel_maximum[i])
- real_max = C.channel_maximum[i];
- if (real_max > 0 && real_max < C.maximum && real_max > C.maximum* auto_threshold)
- {
- C.maximum = real_max;
- }
- return LIBRAW_SUCCESS;
- }
- void LibRaw:: merror (void *ptr, const char *where)
- {
- if (ptr) return;
- if(callbacks.mem_cb)(*callbacks.mem_cb)(callbacks.memcb_data,
- libraw_internal_data.internal_data.input
- ?libraw_internal_data.internal_data.input->fname()
- :NULL,
- where);
- throw LIBRAW_EXCEPTION_ALLOC;
- }
- int LibRaw::open_file(const char *fname, INT64 max_buf_size)
- {
- #ifndef WIN32
- struct stat st;
- if(stat(fname,&st))
- return LIBRAW_IO_ERROR;
- int big = (st.st_size > max_buf_size)?1:0;
- #else
- struct _stati64 st;
- if(_stati64(fname,&st))
- return LIBRAW_IO_ERROR;
- int big = (st.st_size > max_buf_size)?1:0;
- #endif
- LibRaw_abstract_datastream *stream;
- try {
- if(big)
- stream = new LibRaw_bigfile_datastream(fname);
- else
- stream = new LibRaw_file_datastream(fname);
- }
- catch (std::bad_alloc)
- {
- recycle();
- return LIBRAW_UNSUFFICIENT_MEMORY;
- }
- if(!stream->valid())
- {
- delete stream;
- return LIBRAW_IO_ERROR;
- }
- ID.input_internal = 0; // preserve from deletion on error
- int ret = open_datastream(stream);
- if (ret == LIBRAW_SUCCESS)
- {
- ID.input_internal =1 ; // flag to delete datastream on recycle
- }
- else
- {
- delete stream;
- ID.input_internal = 0;
- }
- return ret;
- }
- int LibRaw::open_buffer(void *buffer, size_t size)
- {
- // this stream will close on recycle()
- if(!buffer || buffer==(void*)-1)
- return LIBRAW_IO_ERROR;
- LibRaw_buffer_datastream *stream;
- try {
- stream = new LibRaw_buffer_datastream(buffer,size);
- }
- catch (std::bad_alloc)
- {
- recycle();
- return LIBRAW_UNSUFFICIENT_MEMORY;
- }
- if(!stream->valid())
- {
- delete stream;
- return LIBRAW_IO_ERROR;
- }
- ID.input_internal = 0; // preserve from deletion on error
- int ret = open_datastream(stream);
- if (ret == LIBRAW_SUCCESS)
- {
- ID.input_internal =1 ; // flag to delete datastream on recycle
- }
- else
- {
- delete stream;
- ID.input_internal = 0;
- }
- return ret;
- }
- int LibRaw::open_datastream(LibRaw_abstract_datastream *stream)
- {
- if(!stream)
- return ENOENT;
- if(!stream->valid())
- return LIBRAW_IO_ERROR;
- recycle();
- try {
- ID.input = stream;
- SET_PROC_FLAG(LIBRAW_PROGRESS_OPEN);
- if (O.use_camera_matrix < 0)
- O.use_camera_matrix = O.use_camera_wb;
- identify();
- if(IO.fuji_width)
- {
- IO.fwidth = S.width;
- IO.fheight = S.height;
- S.iwidth = S.width = IO.fuji_width << (int)(!libraw_internal_data.unpacker_data.fuji_layout);
- S.iheight = S.height = S.raw_height;
- S.raw_height += 2*S.top_margin;
- }
- if(C.profile_length)
- {
- if(C.profile) free(C.profile);
- C.profile = malloc(C.profile_length);
- merror(C.profile,"LibRaw::open_file()");
- ID.input->seek(ID.profile_offset,SEEK_SET);
- ID.input->read(C.profile,C.profile_length,1);
- }
-
- SET_PROC_FLAG(LIBRAW_PROGRESS_IDENTIFY);
- }
- catch ( LibRaw_exceptions err) {
- EXCEPTION_HANDLER(err);
- }
- if(P1.raw_count < 1)
- return LIBRAW_FILE_UNSUPPORTED;
-
- write_fun = &LibRaw::write_ppm_tiff;
-
- if (load_raw == &LibRaw::kodak_ycbcr_load_raw)
- {
- S.height += S.height & 1;
- S.width += S.width & 1;
- }
- IO.shrink = P1.filters && (O.half_size ||
- ((O.threshold || O.aber[0] != 1 || O.aber[2] != 1) ));
- S.iheight = (S.height + IO.shrink) >> IO.shrink;
- S.iwidth = (S.width + IO.shrink) >> IO.shrink;
- // Save color,sizes and internal data into raw_image fields
- memmove(&imgdata.rawdata.color,&imgdata.color,sizeof(imgdata.color));
- memmove(&imgdata.rawdata.sizes,&imgdata.sizes,sizeof(imgdata.sizes));
- memmove(&imgdata.rawdata.iparams,&imgdata.idata,sizeof(imgdata.idata));
- memmove(&imgdata.rawdata.ioparams,&libraw_internal_data.internal_output_params,sizeof(libraw_internal_data.internal_output_params));
-
- SET_PROC_FLAG(LIBRAW_PROGRESS_SIZE_ADJUST);
- return LIBRAW_SUCCESS;
- }
- int LibRaw::unpack(void)
- {
- CHECK_ORDER_HIGH(LIBRAW_PROGRESS_LOAD_RAW);
- CHECK_ORDER_LOW(LIBRAW_PROGRESS_IDENTIFY);
- try {
- RUN_CALLBACK(LIBRAW_PROGRESS_LOAD_RAW,0,2);
- if (O.shot_select >= P1.raw_count)
- return LIBRAW_REQUEST_FOR_NONEXISTENT_IMAGE;
-
- if(!load_raw)
- return LIBRAW_UNSPECIFIED_ERROR;
-
- if (O.use_camera_matrix && C.cmatrix[0][0] > 0.25)
- {
- memcpy (C.rgb_cam, C.cmatrix, sizeof (C.cmatrix));
- IO.raw_color = 0;
- }
- // already allocated ?
- if(imgdata.image)
- {
- free(imgdata.image);
- imgdata.image = 0;
- }
- if (libraw_internal_data.unpacker_data.meta_length)
- {
- libraw_internal_data.internal_data.meta_data =
- (char *) malloc (libraw_internal_data.unpacker_data.meta_length);
- merror (libraw_internal_data.internal_data.meta_data, "LibRaw::unpack()");
- }
- ID.input->seek(libraw_internal_data.unpacker_data.data_offset, SEEK_SET);
- int save_document_mode = O.document_mode;
- O.document_mode = 0;
- libraw_decoder_info_t decoder_info;
- get_decoder_info(&decoder_info);
- int save_iwidth = S.iwidth, save_iheight = S.iheight, save_shrink = IO.shrink;
- int rwidth = S.raw_width, rheight = S.raw_height;
- if( !IO.fuji_width)
- {
- // adjust non-Fuji allocation
- if(rwidth < S.width + S.left_margin)
- rwidth = S.width + S.left_margin;
- if(rheight < S.height + S.top_margin)
- rheight = S.height + S.top_margin;
- }
-
- if(decoder_info.decoder_flags & LIBRAW_DECODER_FLATFIELD)
- {
- imgdata.rawdata.raw_alloc = malloc(rwidth*rheight*sizeof(imgdata.rawdata.raw_image[0]));
- imgdata.rawdata.raw_image = (ushort*) imgdata.rawdata.raw_alloc;
- }
- else if (decoder_info.decoder_flags & LIBRAW_DECODER_4COMPONENT)
- {
- S.iwidth = S.width;
- S.iheight= S.height;
- IO.shrink = 0;
- imgdata.rawdata.raw_alloc = calloc(rwidth*rheight,sizeof(*imgdata.rawdata.color_image));
- imgdata.rawdata.color_image = (ushort(*)[4]) imgdata.rawdata.raw_alloc;
- }
- else if (decoder_info.decoder_flags & LIBRAW_DECODER_LEGACY)
- {
- // sRAW and Foveon only, so extra buffer size is just 1/4
- // Legacy converters does not supports half mode!
- S.iwidth = S.width;
- S.iheight= S.height;
- IO.shrink = 0;
- // allocate image as temporary buffer, size
- imgdata.rawdata.raw_alloc = calloc(S.iwidth*S.iheight,sizeof(*imgdata.image));
- imgdata.image = (ushort (*)[4]) imgdata.rawdata.raw_alloc;
- }
- (this->*load_raw)();
- // recover saved
- if( decoder_info.decoder_flags & LIBRAW_DECODER_LEGACY)
- {
- imgdata.image = 0;
- imgdata.rawdata.color_image = (ushort (*)[4]) imgdata.rawdata.raw_alloc;
- }
- // calculate channel maximum
- {
- for(int c=0;c<4;c++) C.channel_maximum[c] = 0;
- if(decoder_info.decoder_flags & LIBRAW_DECODER_LEGACY)
- {
- for(int rc = 0; rc < S.iwidth*S.iheight; rc++)
- {
- if(C.channel_maximum[0]<imgdata.rawdata.color_image[rc][0])
- C.channel_maximum[0]=imgdata.rawdata.color_image[rc][0];
- if(C.channel_maximum[1]<imgdata.rawdata.color_image[rc][1])
- C.channel_maximum[1]=imgdata.rawdata.color_image[rc][1];
- if(C.channel_maximum[2]<imgdata.rawdata.color_image[rc][2])
- C.channel_maximum[2]=imgdata.rawdata.color_image[rc][2];
- if(C.channel_maximum[3]<imgdata.rawdata.color_image[rc][3])
- C.channel_maximum[3]=imgdata.rawdata.color_image[rc][3];
- }
- }
- else if(decoder_info.decoder_flags & LIBRAW_DECODER_4COMPONENT)
- {
- for(int row = S.top_margin; row < S.height+S.top_margin; row++)
- for(int col = S.left_margin; col < S.width+S.left_margin; col++)
- {
- int rc = row*S.raw_width+col;
- if(C.channel_maximum[0]<imgdata.rawdata.color_image[rc][0])
- C.channel_maximum[0]=imgdata.rawdata.color_image[rc][0];
- if(C.channel_maximum[1]<imgdata.rawdata.color_image[rc][1])
- C.channel_maximum[1]=imgdata.rawdata.color_image[rc][1];
- if(C.channel_maximum[2]<imgdata.rawdata.color_image[rc][2])
- C.channel_maximum[2]=imgdata.rawdata.color_image[rc][2];
- if(C.channel_maximum[3]<imgdata.rawdata.color_image[rc][3])
- C.channel_maximum[3]=imgdata.rawdata.color_image[rc][4];
- }
- }
- else if (decoder_info.decoder_flags & LIBRAW_DECODER_FLATFIELD)
- {
- for(int row = 0; row < S.height; row++)
- {
- int colors[4];
- for (int xx=0;xx<4;xx++)
- colors[xx] = COLOR(row,xx);
- for(int col = 0; col < S.width; col++)
- {
- int cc = colors[col&3];
- if(C.channel_maximum[cc]
- < imgdata.rawdata.raw_image[(row+S.top_margin)*S.raw_width
- +(col+S.left_margin)])
- C.channel_maximum[cc] =
- imgdata.rawdata.raw_image[(row+S.top_margin)*S.raw_width
- +(col+S.left_margin)];
- }
- }
- }
- }
- // recover image sizes
- S.iwidth = save_iwidth;
- S.iheight = save_iheight;
- IO.shrink = save_shrink;
- // phase-one black
- if(imgdata.rawdata.ph1_black)
- C.ph1_black = imgdata.rawdata.ph1_black;
- O.document_mode = save_document_mode;
- // adjust black to possible maximum
- unsigned int i = C.cblack[3];
- unsigned int c;
- for(c=0;c<3;c++)
- if (i > C.cblack[c]) i = C.cblack[c];
- for (c=0;c<4;c++)
- C.cblack[c] -= i;
- C.black += i;
- // Save color,sizes and internal data into raw_image fields
- memmove(&imgdata.rawdata.color,&imgdata.color,sizeof(imgdata.color));
- memmove(&imgdata.rawdata.sizes,&imgdata.sizes,sizeof(imgdata.sizes));
- memmove(&imgdata.rawdata.iparams,&imgdata.idata,sizeof(imgdata.idata));
- memmove(&imgdata.rawdata.ioparams,&libraw_internal_data.internal_output_params,sizeof(libraw_internal_data.internal_output_params));
- SET_PROC_FLAG(LIBRAW_PROGRESS_LOAD_RAW);
- RUN_CALLBACK(LIBRAW_PROGRESS_LOAD_RAW,1,2);
-
- return 0;
- }
- catch ( LibRaw_exceptions err) {
- EXCEPTION_HANDLER(err);
- }
- }
- void LibRaw::free_image(void)
- {
- if(imgdata.image)
- {
- free(imgdata.image);
- imgdata.image = 0;
- imgdata.progress_flags
- = LIBRAW_PROGRESS_START|LIBRAW_PROGRESS_OPEN
- |LIBRAW_PROGRESS_IDENTIFY|LIBRAW_PROGRESS_SIZE_ADJUST|LIBRAW_PROGRESS_LOAD_RAW;
- }
- }
- void LibRaw::raw2image_start()
- {
- // restore color,sizes and internal data into raw_image fields
- memmove(&imgdata.color,&imgdata.rawdata.color,sizeof(imgdata.color));
- memmove(&imgdata.sizes,&imgdata.rawdata.sizes,sizeof(imgdata.sizes));
- memmove(&imgdata.idata,&imgdata.rawdata.iparams,sizeof(imgdata.idata));
- memmove(&libraw_internal_data.internal_output_params,&imgdata.rawdata.ioparams,sizeof(libraw_internal_data.internal_output_params));
- if (O.user_flip >= 0)
- S.flip = O.user_flip;
-
- switch ((S.flip+3600) % 360)
- {
- case 270: S.flip = 5; break;
- case 180: S.flip = 3; break;
- case 90: S.flip = 6; break;
- }
- // adjust for half mode!
- IO.shrink = P1.filters && (O.half_size ||
- ((O.threshold || O.aber[0] != 1 || O.aber[2] != 1) ));
-
- S.iheight = (S.height + IO.shrink) >> IO.shrink;
- S.iwidth = (S.width + IO.shrink) >> IO.shrink;
- if (O.user_black >= 0)
- C.black = O.user_black;
- }
- // Same as raw2image, but
- // 1) Do raw2image and rotate_fuji_raw in one pass
- // 2) Do raw2image and cropping in one pass
- #ifndef MIN
- #define MIN(a,b) ((a) < (b) ? (a) : (b))
- #endif
- int LibRaw::raw2image_ex(void)
- {
- CHECK_ORDER_LOW(LIBRAW_PROGRESS_LOAD_RAW);
- raw2image_start();
- // process cropping
- int do_crop = 0;
- unsigned save_filters = imgdata.idata.filters;
- unsigned save_width = S.width;
- if (~O.cropbox[2] && ~O.cropbox[3])
- {
- int crop[4],c,filt;
- for(int c=0;c<4;c++)
- {
- crop[c] = O.cropbox[c];
- if(crop[c]<0)
- crop[c]=0;
- }
- if(IO.fwidth)
- {
- crop[0] = (crop[0]/4)*4;
- crop[1] = (crop[1]/4)*4;
- }
- do_crop = 1;
- crop[2] = MIN (crop[2], (signed) S.width-crop[0]);
- crop[3] = MIN (crop[3], (signed) S.height-crop[1]);
- if (crop[2] <= 0 || crop[3] <= 0)
- throw LIBRAW_EXCEPTION_BAD_CROP;
-
- // adjust sizes!
- S.left_margin+=crop[0];
- S.top_margin+=crop[1];
- S.width=crop[2];
- S.height=crop[3];
-
- S.iheight = (S.height + IO.shrink) >> IO.shrink;
- S.iwidth = (S.width + IO.shrink) >> IO.shrink;
- if(!IO.fwidth && imgdata.idata.filters)
- {
- for (filt=c=0; c < 16; c++)
- filt |= FC((c >> 1)+(crop[1]),
- (c & 1)+(crop[0])) << c*2;
- imgdata.idata.filters = filt;
- }
- }
- if(IO.fwidth)
- {
- ushort fiwidth,fiheight;
- if(do_crop)
- {
- IO.fuji_width = S.width >> !libraw_internal_data.unpacker_data.fuji_layout;
- IO.fwidth = (S.height >> libraw_internal_data.unpacker_data.fuji_layout) + IO.fuji_width;
- IO.fheight = IO.fwidth - 1;
- }
- fiheight = (IO.fheight + IO.shrink) >> IO.shrink;
- fiwidth = (IO.fwidth + IO.shrink) >> IO.shrink;
- if(imgdata.image)
- {
- imgdata.image = (ushort (*)[4])realloc(imgdata.image,fiheight*fiwidth*sizeof (*imgdata.image));
- memset(imgdata.image,0,fiheight*fiwidth *sizeof (*imgdata.image));
- }
- else
- imgdata.image = (ushort (*)[4]) calloc (fiheight*fiwidth, sizeof (*imgdata.image));
- merror (imgdata.image, "raw2image_ex()");
- int cblk[4],i;
- for(i=0;i<4;i++)
- cblk[i] = C.cblack[i]+C.black;
- ZERO(C.channel_maximum);
- int row,col;
- for(row=0;row<S.height;row++)
- {
- for(col=0;col<S.width;col++)
- {
- int r,c;
- if (libraw_internal_data.unpacker_data.fuji_layout) {
- r = IO.fuji_width - 1 - col + (row >> 1);
- c = col + ((row+1) >> 1);
- } else {
- r = IO.fuji_width - 1 + row - (col >> 1);
- c = row + ((col+1) >> 1);
- }
-
- int val = imgdata.rawdata.raw_image[(row+S.top_margin)*S.raw_width
- +(col+S.left_margin)];
- int cc = FCF(row,col);
- if(val > cblk[cc])
- val -= cblk[cc];
- else
- val = 0;
- imgdata.image[((r) >> IO.shrink)*fiwidth + ((c) >> IO.shrink)][cc] = val;
- if(C.channel_maximum[cc] < val) C.channel_maximum[cc] = val;
- }
- }
- C.maximum -= C.black;
- ZERO(C.cblack);
- C.black = 0;
- // restore fuji sizes!
- S.height = IO.fheight;
- S.width = IO.fwidth;
- S.iheight = (S.height + IO.shrink) >> IO.shrink;
- S.iwidth = (S.width + IO.shrink) >> IO.shrink;
- S.raw_height -= 2*S.top_margin;
- }
- else
- {
- if(imgdata.image)
- {
- imgdata.image = (ushort (*)[4]) realloc (imgdata.image,S.iheight*S.iwidth
- *sizeof (*imgdata.image));
- memset(imgdata.image,0,S.iheight*S.iwidth *sizeof (*imgdata.image));
- }
- else
- imgdata.image = (ushort (*)[4]) calloc (S.iheight*S.iwidth, sizeof (*imgdata.image));
- merror (imgdata.image, "raw2image_ex()");
-
- libraw_decoder_info_t decoder_info;
- get_decoder_info(&decoder_info);
- if(decoder_info.decoder_flags & LIBRAW_DECODER_FLATFIELD)
- {
- if(decoder_info.decoder_flags & LIBRAW_DECODER_USEBAYER2)
- #if defined(LIBRAW_USE_OPENMP)
- #pragma omp parallel for default(shared)
- #endif
- for(int row = 0; row < S.height; row++)
- for(int col = 0; col < S.width; col++)
- imgdata.image[(row >> IO.shrink)*S.iwidth + (col>>IO.shrink)][fc(row,col)]
- = imgdata.rawdata.raw_image[(row+S.top_margin)*S.raw_width
- +(col+S.left_margin)];
- else
- #if defined(LIBRAW_USE_OPENMP)
- #pragma omp parallel for default(shared)
- #endif
- for(int row = 0; row < S.height; row++)
- {
- int colors[2];
- for (int xx=0;xx<2;xx++)
- colors[xx] = COLOR(row,xx);
- for(int col = 0; col < S.width; col++)
- {
- int cc = colors[col&1];
- imgdata.image[(row >> IO.shrink)*S.iwidth + (col>>IO.shrink)][cc] =
- imgdata.rawdata.raw_image[(row+S.top_margin)*S.raw_width
- +(col+S.left_margin)];
- }
- }
- }
- else if (decoder_info.decoder_flags & LIBRAW_DECODER_4COMPONENT)
- {
- #define FC0(row,col) (save_filters >> ((((row) << 1 & 14) + ((col) & 1)) << 1) & 3)
- if(IO.shrink)
- #if defined(LIBRAW_USE_OPENMP)
- #pragma omp parallel for default(shared)
- #endif
- for(int row = 0; row < S.height; row++)
- for(int col = 0; col < S.width; col++)
- imgdata.image[(row >> IO.shrink)*S.iwidth + (col>>IO.shrink)][FC(row,col)]
- = imgdata.rawdata.color_image[(row+S.top_margin)*S.raw_width
- +S.left_margin+col]
- [FC0(row+S.top_margin,col+S.left_margin)];
- #undef FC0
- else
- #if defined(LIBRAW_USE_OPENMP)
- #pragma omp parallel for default(shared)
- #endif
- for(int row = 0; row < S.height; row++)
- memmove(&imgdata.image[row*S.width],
- &imgdata.rawdata.color_image[(row+S.top_margin)*S.raw_width+S.left_margin],
- S.width*sizeof(*imgdata.image));
- }
- else if(decoder_info.decoder_flags & LIBRAW_DECODER_LEGACY)
- {
- if(do_crop)
- #if defined(LIBRAW_USE_OPENMP)
- #pragma omp parallel for default(shared)
- #endif
- for(int row = 0; row < S.height; row++)
- memmove(&imgdata.image[row*S.width],
- &imgdata.rawdata.color_image[(row+S.top_margin)*save_width+S.left_margin],
- S.width*sizeof(*imgdata.image));
-
- else
- memmove(imgdata.image,imgdata.rawdata.color_image,
- S.width*S.height*sizeof(*imgdata.image));
- }
- if(imgdata.rawdata.use_ph1_correct) // Phase one unpacked!
- phase_one_correct();
- }
- return LIBRAW_SUCCESS;
- }
- #undef MIN
- int LibRaw::raw2image(void)
- {
- CHECK_ORDER_LOW(LIBRAW_PROGRESS_LOAD_RAW);
- try {
- raw2image_start();
- // free and re-allocate image bitmap
- if(imgdata.image)
- {
- imgdata.image = (ushort (*)[4]) realloc (imgdata.image,S.iheight*S.iwidth *sizeof (*imgdata.image));
- memset(imgdata.image,0,S.iheight*S.iwidth *sizeof (*imgdata.image));
- }
- else
- imgdata.image = (ushort (*)[4]) calloc (S.iheight*S.iwidth, sizeof (*imgdata.image));
- merror (imgdata.image, "raw2image()");
- libraw_decoder_info_t decoder_info;
- get_decoder_info(&decoder_info);
-
- // Move saved bitmap to imgdata.image
- if(decoder_info.decoder_flags & LIBRAW_DECODER_FLATFIELD)
- {
- if(decoder_info.decoder_flags & LIBRAW_DECODER_USEBAYER2)
- {
- for(int row = 0; row < S.height; row++)
- for(int col = 0; col < S.width; col++)
- imgdata.image[(row >> IO.shrink)*S.iwidth + (col>>IO.shrink)][fc(row,col)]
- = imgdata.rawdata.raw_image[(row+S.top_margin)*S.raw_width
- +(col+S.left_margin)];
- }
- else
- {
- for(int row = 0; row < S.height; row++)
- {
- int colors[4];
- for (int xx=0;xx<4;xx++)
- colors[xx] = COLOR(row,xx);
- for(int col = 0; col < S.width; col++)
- {
- int cc = colors[col&3];
- imgdata.image[(row >> IO.shrink)*S.iwidth + (col>>IO.shrink)][cc] =
- imgdata.rawdata.raw_image[(row+S.top_margin)*S.raw_width+(col
- +S.left_margin)];
- }
- }
- }
- }
- else if (decoder_info.decoder_flags & LIBRAW_DECODER_4COMPONENT)
- {
- if(IO.shrink)
- {
- for(int row = 0; row < S.height; row++)
- for(int col = 0; col < S.width; col++)
- {
- int cc = FC(row,col);
- imgdata.image[(row >> IO.shrink)*S.iwidth + (col>>IO.shrink)][cc]
- = imgdata.rawdata.color_image[(row+S.top_margin)*S.raw_width
- +S.left_margin+col][cc];
- }
- }
- else
- for(int row = 0; row < S.height; row++)
- memmove(&imgdata.image[row*S.width],
- &imgdata.rawdata.color_image[(row+S.top_margin)*S.raw_width+S.left_margin],
- S.width*sizeof(*imgdata.image));
- }
- else if(decoder_info.decoder_flags & LIBRAW_DECODER_LEGACY)
- {
- // legacy is always 4channel and not shrinked!
- memmove(imgdata.image,imgdata.rawdata.color_image,S.width*S.height*sizeof(*imgdata.image));
- }
- if(imgdata.rawdata.use_ph1_correct) // Phase one unpacked!
- phase_one_correct();
- // hack - clear later flags!
- imgdata.progress_flags
- = LIBRAW_PROGRESS_START|LIBRAW_PROGRESS_OPEN
- |LIBRAW_PROGRESS_IDENTIFY|LIBRAW_PROGRESS_SIZE_ADJUST|LIBRAW_PROGRESS_LOAD_RAW;
- return 0;
- }
- catch ( LibRaw_exceptions err) {
- EXCEPTION_HANDLER(err);
- }
- }
-
- int LibRaw::dcraw_document_mode_processing(void)
- {
- // CHECK_ORDER_HIGH(LIBRAW_PROGRESS_PRE_INTERPOLATE);
- CHECK_ORDER_LOW(LIBRAW_PROGRESS_LOAD_RAW);
- try {
- int no_crop = 1;
- if (~O.cropbox[2] && ~O.cropbox[3])
- no_crop=0;
- raw2image_ex(); // raw2image+crop+rotate_fuji_raw
- if (IO.zero_is_bad)
- {
- remove_zeroes();
- SET_PROC_FLAG(LIBRAW_PROGRESS_REMOVE_ZEROES);
- }
- if(!IO.fuji_width)
- subtract_black();
-
- O.document_mode = 2;
-
- if(P1.is_foveon)
- {
- // filter image data for foveon document mode
- short *iptr = (short *)imgdata.image;
- for (int i=0; i < S.height*S.width*4; i++)
- {
- if ((short) iptr[i] < 0)
- iptr[i] = 0;
- }
- SET_PROC_FLAG(LIBRAW_PROGRESS_FOVEON_INTERPOLATE);
- }
- O.use_fuji_rotate = 0;
- if(O.bad_pixels && no_crop)
- {
- bad_pixels(O.bad_pixels);
- SET_PROC_FLAG(LIBRAW_PROGRESS_BAD_PIXELS);
- }
- if (O.dark_frame && no_crop)
- {
- subtract (O.dark_frame);
- SET_PROC_FLAG(LIBRAW_PROGRESS_DARK_FRAME);
- }
- adjust_maximum();
- if (O.user_sat > 0)
- C.maximum = O.user_sat;
- pre_interpolate();
- SET_PROC_FLAG(LIBRAW_PROGRESS_PRE_INTERPOLATE);
- if (libraw_internal_data.internal_output_params.mix_green)
- {
- int i;
- for (P1.colors=3, i=0; i < S.height*S.width; i++)
- imgdata.image[i][1] = (imgdata.image[i][1] + imgdata.image[i][3]) >> 1;
- }
- SET_PROC_FLAG(LIBRAW_PROGRESS_MIX_GREEN);
- if (!P1.is_foveon && P1.colors == 3)
- median_filter();
- SET_PROC_FLAG(LIBRAW_PROGRESS_MEDIAN_FILTER);
- if (!P1.is_foveon && O.highlight == 2)
- blend_highlights();
- if (!P1.is_foveon && O.highlight > 2)
- recover_highlights();
- SET_PROC_FLAG(LIBRAW_PROGRESS_HIGHLIGHTS);
- if (O.use_fuji_rotate)
- fuji_rotate();
- SET_PROC_FLAG(LIBRAW_PROGRESS_FUJI_ROTATE);
- #ifndef NO_LCMS
- if(O.camera_profile)
- {
- apply_profile(O.camera_profile,O.output_profile);
- SET_PROC_FLAG(LIBRAW_PROGRESS_APPLY_PROFILE);
- }
- #endif
- if(!libraw_internal_data.output_data.histogram)
- {
- libraw_internal_data.output_data.histogram = (int (*)[LIBRAW_HISTOGRAM_SIZE]) malloc(sizeof(*libraw_internal_data.output_data.histogram)*4);
- merror(libraw_internal_data.output_data.histogram,"LibRaw::dcraw_document_mode_processing()");
- }
- convert_to_rgb();
- SET_PROC_FLAG(LIBRAW_…