/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

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