PageRenderTime 130ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 2ms

/lib/cximage-6.0/raw/libdcr.c

https://bitbucket.org/bgiorgini/xbmc
C | 8452 lines | 7821 code | 411 blank | 220 comment | 2650 complexity | 6fe5bd8b9fbea9d52546b7d12be09173 MD5 | raw file
Possible License(s): GPL-3.0, CC-BY-SA-3.0, BSD-3-Clause, GPL-2.0, LGPL-3.0, 0BSD, LGPL-2.0, AGPL-1.0, LGPL-2.1
  1. /*
  2. libdcr version 0.1.8.89 11/Jan/2009
  3. libdcr : copyright (C) 2007-2009, Davide Pizzolato
  4. based on dcraw.c -- Dave Coffin's raw photo decoder
  5. Copyright 1997-2009 by Dave Coffin, dcoffin a cybercom o net
  6. Covered code is provided under this license on an "as is" basis, without warranty
  7. of any kind, either expressed or implied, including, without limitation, warranties
  8. that the covered code is free of defects, merchantable, fit for a particular purpose
  9. or non-infringing. The entire risk as to the quality and performance of the covered
  10. code is with you. Should any covered code prove defective in any respect, you (not
  11. the initial developer or any other contributor) assume the cost of any necessary
  12. servicing, repair or correction. This disclaimer of warranty constitutes an essential
  13. part of this license. No use of any covered code is authorized hereunder except under
  14. this disclaimer.
  15. No license is required to download and use libdcr. However,
  16. to lawfully redistribute libdcr, you must either (a) offer, at
  17. no extra charge, full source code for all executable files
  18. containing RESTRICTED functions, (b) distribute this code under
  19. the GPL Version 2 or later, (c) remove all RESTRICTED functions,
  20. re-implement them, or copy them from an earlier, unrestricted
  21. revision of dcraw.c, or (d) purchase a license from the author
  22. of dcraw.c.
  23. --------------------------------------------------------------------------------
  24. dcraw.c home page: http://cybercom.net/~dcoffin/dcraw/
  25. libdcr home page: http://www.xdp.it/libdcr/
  26. */
  27. #define _GNU_SOURCE
  28. #define _USE_MATH_DEFINES
  29. #include <ctype.h>
  30. #include <errno.h>
  31. #include <fcntl.h>
  32. #include <float.h>
  33. #include <limits.h>
  34. #include <math.h>
  35. #include <setjmp.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <time.h>
  40. #include <sys/types.h>
  41. #include "libdcr.h"
  42. // XYZ from RGB
  43. const double xyz_rgb[3][3] = {
  44. { 0.412453, 0.357580, 0.180423 },
  45. { 0.212671, 0.715160, 0.072169 },
  46. { 0.019334, 0.119193, 0.950227 } };
  47. const float d65_white[3] = { 0.950456f, 1.0f, 1.088754f };
  48. static int dcr_sfile_read(dcr_stream_obj *obj, void *buf, int size, int cnt);
  49. static int dcr_sfile_write(dcr_stream_obj *obj, void *buf, int size, int cnt);
  50. static long dcr_sfile_seek(dcr_stream_obj *obj, long offset, int origin);
  51. static int dcr_sfile_close(dcr_stream_obj *obj);
  52. static char* dcr_sfile_gets(dcr_stream_obj *obj, char *string, int n);
  53. static int dcr_sfile_eof(dcr_stream_obj *obj);
  54. static long dcr_sfile_tell(dcr_stream_obj *obj);
  55. static int dcr_sfile_getc(dcr_stream_obj *obj);
  56. static int dcr_sfile_scanf(dcr_stream_obj *obj,const char *format, void* output);
  57. static dcr_stream_ops dcr_stream_fileops = {
  58. dcr_sfile_read,
  59. dcr_sfile_write,
  60. dcr_sfile_seek,
  61. dcr_sfile_close,
  62. dcr_sfile_gets,
  63. dcr_sfile_eof,
  64. dcr_sfile_tell,
  65. dcr_sfile_getc,
  66. dcr_sfile_scanf
  67. };
  68. /*
  69. NO_JPEG disables decoding of compressed Kodak DC120 files.
  70. NO_LCMS disables the "-p" option.
  71. */
  72. #ifndef NO_JPEG
  73. #ifdef WIN32
  74. #include "../jpeg/jpeglib.h"
  75. #else
  76. #include <jpeglib.h>
  77. #endif
  78. #endif
  79. #ifndef NO_LCMS
  80. #ifdef WIN32
  81. #include "../lcms/lcms.h"
  82. #else
  83. #include <lcms.h>
  84. #endif
  85. #endif
  86. #ifdef LOCALEDIR
  87. #include <libintl.h>
  88. #define _(String) gettext(String)
  89. #else
  90. #define _(String) (String)
  91. #endif
  92. #ifdef __CYGWIN__
  93. #include <io.h>
  94. #endif
  95. #ifdef WIN32
  96. #include <sys/utime.h>
  97. #include <winsock2.h>
  98. #pragma comment(lib, "ws2_32.lib")
  99. #define snprintf _snprintf
  100. #define strcasecmp _stricmp
  101. #define strncasecmp _strnicmp
  102. typedef __int64 INT64;
  103. typedef unsigned __int64 UINT64;
  104. #else
  105. #include <unistd.h>
  106. #include <utime.h>
  107. #include <netinet/in.h>
  108. typedef long long INT64;
  109. typedef unsigned long long UINT64;
  110. #define __int64 long long
  111. #endif
  112. #ifdef LJPEG_DECODE
  113. #error Please compile dcraw.c by itself.
  114. #error Do not link it with ljpeg_decode.
  115. #endif
  116. /*
  117. In order to inline this calculation, I make the risky
  118. assumption that all filter patterns can be described
  119. by a repeating pattern of eight rows and two columns
  120. Do not use the FC or BAYER macros with the Leaf CatchLight,
  121. because its pattern is 16x16, not 2x8.
  122. Return values are either 0/1/2/3 = G/M/C/Y or 0/1/2/3 = R/G1/B/G2
  123. PowerShot 600 PowerShot A50 PowerShot Pro70 Pro90 & G1
  124. 0xe1e4e1e4: 0x1b4e4b1e: 0x1e4b4e1b: 0xb4b4b4b4:
  125. 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
  126. 0 G M G M G M 0 C Y C Y C Y 0 Y C Y C Y C 0 G M G M G M
  127. 1 C Y C Y C Y 1 M G M G M G 1 M G M G M G 1 Y C Y C Y C
  128. 2 M G M G M G 2 Y C Y C Y C 2 C Y C Y C Y
  129. 3 C Y C Y C Y 3 G M G M G M 3 G M G M G M
  130. 4 C Y C Y C Y 4 Y C Y C Y C
  131. PowerShot A5 5 G M G M G M 5 G M G M G M
  132. 0x1e4e1e4e: 6 Y C Y C Y C 6 C Y C Y C Y
  133. 7 M G M G M G 7 M G M G M G
  134. 0 1 2 3 4 5
  135. 0 C Y C Y C Y
  136. 1 G M G M G M
  137. 2 C Y C Y C Y
  138. 3 M G M G M G
  139. All RGB cameras use one of these Bayer grids:
  140. 0x16161616: 0x61616161: 0x49494949: 0x94949494:
  141. 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
  142. 0 B G B G B G 0 G R G R G R 0 G B G B G B 0 R G R G R G
  143. 1 G R G R G R 1 B G B G B G 1 R G R G R G 1 G B G B G B
  144. 2 B G B G B G 2 G R G R G R 2 G B G B G B 2 R G R G R G
  145. 3 G R G R G R 3 B G B G B G 3 R G R G R G 3 G B G B G B
  146. */
  147. #define FC(row,col) \
  148. (p->filters >> ((((row) << 1 & 14) + ((col) & 1)) << 1) & 3)
  149. #define BAYER(row,col) \
  150. p->image[((row) >> p->shrink)*p->iwidth + ((col) >> p->shrink)][FC(row,col)]
  151. #define BAYER2(row,col) \
  152. p->image[((row) >> p->shrink)*p->iwidth + ((col) >> p->shrink)][dcr_fc(p,row,col)]
  153. int DCR_CLASS dcr_fc (DCRAW* p, int row, int col)
  154. {
  155. static const char filter[16][16] =
  156. { { 2,1,1,3,2,3,2,0,3,2,3,0,1,2,1,0 },
  157. { 0,3,0,2,0,1,3,1,0,1,1,2,0,3,3,2 },
  158. { 2,3,3,2,3,1,1,3,3,1,2,1,2,0,0,3 },
  159. { 0,1,0,1,0,2,0,2,2,0,3,0,1,3,2,1 },
  160. { 3,1,1,2,0,1,0,2,1,3,1,3,0,1,3,0 },
  161. { 2,0,0,3,3,2,3,1,2,0,2,0,3,2,2,1 },
  162. { 2,3,3,1,2,1,2,1,2,1,1,2,3,0,0,1 },
  163. { 1,0,0,2,3,0,0,3,0,3,0,3,2,1,2,3 },
  164. { 2,3,3,1,1,2,1,0,3,2,3,0,2,3,1,3 },
  165. { 1,0,2,0,3,0,3,2,0,1,1,2,0,1,0,2 },
  166. { 0,1,1,3,3,2,2,1,1,3,3,0,2,1,3,2 },
  167. { 2,3,2,0,0,1,3,0,2,0,1,2,3,0,1,0 },
  168. { 1,3,1,2,3,2,3,2,0,2,0,1,1,0,3,0 },
  169. { 0,2,0,3,1,0,0,1,1,3,3,2,3,2,2,1 },
  170. { 2,1,3,2,3,1,2,1,0,3,0,2,0,2,0,2 },
  171. { 0,3,1,0,0,2,0,3,2,1,3,1,1,3,1,3 } };
  172. if (p->filters != 1) return FC(row,col);
  173. return filter[(row+p->top_margin) & 15][(col+p->left_margin) & 15];
  174. }
  175. #ifndef __GLIBC__
  176. char *dcr_memmem (char *haystack, size_t haystacklen,
  177. char *needle, size_t needlelen)
  178. {
  179. char *c;
  180. for (c = haystack; c <= haystack + haystacklen - needlelen; c++)
  181. if (!memcmp (c, needle, needlelen))
  182. return c;
  183. return 0;
  184. }
  185. #define memmem dcr_memmem
  186. #endif
  187. void DCR_CLASS dcr_merror (DCRAW* p, void *ptr, char *where)
  188. {
  189. if (ptr) return;
  190. if (p->sz_error){
  191. sprintf (p->sz_error,_("%s: Out of memory in %s\n"), p->ifname, where);
  192. } else {
  193. fprintf (stderr,_("%s: Out of memory in %s\n"), p->ifname, where);
  194. }
  195. longjmp (p->failure, 1);
  196. }
  197. void DCR_CLASS dcr_derror(DCRAW* p)
  198. {
  199. if (!p->data_error) {
  200. fprintf (stderr, "%s: ", p->ifname);
  201. if (dcr_feof(p->obj_))
  202. fprintf (stderr,_("Unexpected end of file\n"));
  203. else
  204. fprintf (stderr,_("Corrupt data near 0x%llx\n"), (INT64) dcr_ftell(p->obj_));
  205. }
  206. p->data_error = 1;
  207. }
  208. ushort DCR_CLASS dcr_sget2 (DCRAW* p,uchar *s)
  209. {
  210. if (p->order == 0x4949) /* "II" means little-endian */
  211. return s[0] | s[1] << 8;
  212. else /* "MM" means big-endian */
  213. return s[0] << 8 | s[1];
  214. }
  215. ushort DCR_CLASS dcr_get2(DCRAW* p)
  216. {
  217. uchar str[2] = { 0xff,0xff };
  218. dcr_fread(p->obj_, str, 1, 2);
  219. return dcr_sget2(p, str);
  220. }
  221. unsigned DCR_CLASS dcr_sget4 (DCRAW* p,uchar *s)
  222. {
  223. if (p->order == 0x4949)
  224. return s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
  225. else
  226. return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3];
  227. }
  228. #define dcr_sget4(p, s) dcr_sget4(p, (uchar *)s)
  229. unsigned DCR_CLASS dcr_get4(DCRAW* p)
  230. {
  231. uchar str[4] = { 0xff,0xff,0xff,0xff };
  232. dcr_fread(p->obj_, str, 1, 4);
  233. return dcr_sget4(p, str);
  234. }
  235. unsigned DCR_CLASS dcr_getint (DCRAW* p,int type)
  236. {
  237. return type == 3 ? dcr_get2(p) : dcr_get4(p);
  238. }
  239. float DCR_CLASS dcr_int_to_float (int i)
  240. {
  241. union { int i; float f; } u;
  242. u.i = i;
  243. return u.f;
  244. }
  245. double DCR_CLASS dcr_getreal (DCRAW* p,int type)
  246. {
  247. union { char c[8]; double d; } u;
  248. int i, rev;
  249. switch (type) {
  250. case 3: return (unsigned short) dcr_get2(p);
  251. case 4: return (unsigned int) dcr_get4(p);
  252. case 5: u.d = (unsigned int) dcr_get4(p);
  253. return u.d / (unsigned int) dcr_get4(p);
  254. case 8: return (signed short) dcr_get2(p);
  255. case 9: return (signed int) dcr_get4(p);
  256. case 10: u.d = (signed int) dcr_get4(p);
  257. return u.d / (signed int) dcr_get4(p);
  258. case 11: return dcr_int_to_float (dcr_get4(p));
  259. case 12:
  260. rev = 7 * ((p->order == 0x4949) == (ntohs(0x1234) == 0x1234));
  261. for (i=0; i < 8; i++)
  262. u.c[i ^ rev] = dcr_fgetc(p->obj_);
  263. return u.d;
  264. default: return dcr_fgetc(p->obj_);
  265. }
  266. }
  267. void DCR_CLASS dcr_read_shorts (DCRAW* p,ushort *pixel, int count)
  268. {
  269. if ((int)dcr_fread(p->obj_, pixel, 2, count) < count) dcr_derror(p);
  270. if ((p->order == 0x4949) == (ntohs(0x1234) == 0x1234))
  271. _swab ((char*)pixel, (char*)pixel, count*2);
  272. }
  273. void DCR_CLASS dcr_canon_black (DCRAW* p, double dark[2])
  274. {
  275. int c, diff, row, col;
  276. if (p->raw_width < p->width+4) return;
  277. FORC(2) dark[c] /= (p->raw_width-p->width-2) * p->height >> 1;
  278. if ((diff = (int)(dark[0] - dark[1])))
  279. for (row=0; row < p->height; row++)
  280. for (col=1; col < p->width; col+=2)
  281. BAYER(row,col) += diff;
  282. dark[1] += diff;
  283. p->black = (unsigned int)((dark[0] + dark[1] + 1) / 2);
  284. }
  285. void DCR_CLASS dcr_canon_600_fixed_wb (DCRAW* p,int temp)
  286. {
  287. static const short mul[4][5] = {
  288. { 667, 358,397,565,452 },
  289. { 731, 390,367,499,517 },
  290. { 1119, 396,348,448,537 },
  291. { 1399, 485,431,508,688 } };
  292. int lo, hi, i;
  293. float frac=0;
  294. for (lo=4; --lo; )
  295. if (*mul[lo] <= temp) break;
  296. for (hi=0; hi < 3; hi++)
  297. if (*mul[hi] >= temp) break;
  298. if (lo != hi)
  299. frac = (float) (temp - *mul[lo]) / (*mul[hi] - *mul[lo]);
  300. for (i=1; i < 5; i++)
  301. p->pre_mul[i-1] = 1 / (frac * mul[hi][i] + (1-frac) * mul[lo][i]);
  302. }
  303. /* Return values: 0 = p->white 1 = near p->white 2 = not p->white */
  304. int DCR_CLASS dcr_canon_600_color (DCRAW* p,int ratio[2], int mar)
  305. {
  306. int clipped=0, target, miss;
  307. if (p->flash_used) {
  308. if (ratio[1] < -104)
  309. { ratio[1] = -104; clipped = 1; }
  310. if (ratio[1] > 12)
  311. { ratio[1] = 12; clipped = 1; }
  312. } else {
  313. if (ratio[1] < -264 || ratio[1] > 461) return 2;
  314. if (ratio[1] < -50)
  315. { ratio[1] = -50; clipped = 1; }
  316. if (ratio[1] > 307)
  317. { ratio[1] = 307; clipped = 1; }
  318. }
  319. target = p->flash_used || ratio[1] < 197
  320. ? -38 - (398 * ratio[1] >> 10)
  321. : -123 + (48 * ratio[1] >> 10);
  322. if (target - mar <= ratio[0] &&
  323. target + 20 >= ratio[0] && !clipped) return 0;
  324. miss = target - ratio[0];
  325. if (abs(miss) >= mar*4) return 2;
  326. if (miss < -20) miss = -20;
  327. if (miss > mar) miss = mar;
  328. ratio[0] = target - miss;
  329. return 1;
  330. }
  331. void DCR_CLASS dcr_canon_600_auto_wb(DCRAW* p)
  332. {
  333. int mar, row, col, i, j, st, count[] = { 0,0 };
  334. int test[8], total[2][8], ratio[2][2], stat[2];
  335. memset (&total, 0, sizeof total);
  336. i = (int)(p->canon_ev + 0.5);
  337. if (i < 10) mar = 150;
  338. else if (i > 12) mar = 20;
  339. else mar = 280 - 20 * i;
  340. if (p->flash_used) mar = 80;
  341. for (row=14; row < p->height-14; row+=4)
  342. for (col=10; col < p->width; col+=2) {
  343. for (i=0; i < 8; i++)
  344. test[(i & 4) + FC(row+(i >> 1),col+(i & 1))] =
  345. BAYER(row+(i >> 1),col+(i & 1));
  346. for (i=0; i < 8; i++)
  347. if (test[i] < 150 || test[i] > 1500) goto next;
  348. for (i=0; i < 4; i++)
  349. if (abs(test[i] - test[i+4]) > 50) goto next;
  350. for (i=0; i < 2; i++) {
  351. for (j=0; j < 4; j+=2)
  352. ratio[i][j >> 1] = ((test[i*4+j+1]-test[i*4+j]) << 10) / test[i*4+j];
  353. stat[i] = dcr_canon_600_color (p,ratio[i], mar);
  354. }
  355. if ((st = stat[0] | stat[1]) > 1) goto next;
  356. for (i=0; i < 2; i++)
  357. if (stat[i])
  358. for (j=0; j < 2; j++)
  359. test[i*4+j*2+1] = test[i*4+j*2] * (0x400 + ratio[i][j]) >> 10;
  360. for (i=0; i < 8; i++)
  361. total[st][i] += test[i];
  362. count[st]++;
  363. next: ;
  364. }
  365. if (count[0] | count[1]) {
  366. st = count[0]*200 < count[1];
  367. for (i=0; i < 4; i++)
  368. p->pre_mul[i] = 1.0f / (total[st][i] + total[st][i+4]);
  369. }
  370. }
  371. void DCR_CLASS dcr_canon_600_coeff(DCRAW* p)
  372. {
  373. static const short table[6][12] = {
  374. { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
  375. { -1203,1715,-1136,1648, 1388,-876,267,245, -1641,2153,3921,-3409 },
  376. { -615,1127,-1563,2075, 1437,-925,509,3, -756,1268,2519,-2007 },
  377. { -190,702,-1886,2398, 2153,-1641,763,-251, -452,964,3040,-2528 },
  378. { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
  379. { -807,1319,-1785,2297, 1388,-876,769,-257, -230,742,2067,-1555 } };
  380. int t=0, i, c;
  381. float mc, yc;
  382. mc = p->pre_mul[1] / p->pre_mul[2];
  383. yc = p->pre_mul[3] / p->pre_mul[2];
  384. if (mc > 1 && mc <= 1.28 && yc < 0.8789) t=1;
  385. if (mc > 1.28 && mc <= 2) {
  386. if (yc < 0.8789) t=3;
  387. else if (yc <= 2) t=4;
  388. }
  389. if (p->flash_used) t=5;
  390. for (p->raw_color = i=0; i < 3; i++)
  391. FORCC(p) p->rgb_cam[i][c] = table[t][i*4 + c] / 1024.0f;
  392. }
  393. void DCR_CLASS dcr_canon_600_load_raw(DCRAW* p)
  394. {
  395. uchar data[1120], *dp;
  396. ushort pixel[896], *pix;
  397. int irow, row, col, val;
  398. static const short mul[4][2] =
  399. { { 1141,1145 }, { 1128,1109 }, { 1178,1149 }, { 1128,1109 } };
  400. for (irow=row=0; irow < p->height; irow++) {
  401. if ((long)dcr_fread(p->obj_, data, 1, p->raw_width*5/4) < (long)(p->raw_width*5/4)) dcr_derror(p);
  402. for (dp=data, pix=pixel; dp < data+1120; dp+=10, pix+=8) {
  403. pix[0] = (dp[0] << 2) + (dp[1] >> 6 );
  404. pix[1] = (dp[2] << 2) + (dp[1] >> 4 & 3);
  405. pix[2] = (dp[3] << 2) + (dp[1] >> 2 & 3);
  406. pix[3] = (dp[4] << 2) + (dp[1] & 3);
  407. pix[4] = (dp[5] << 2) + (dp[9] & 3);
  408. pix[5] = (dp[6] << 2) + (dp[9] >> 2 & 3);
  409. pix[6] = (dp[7] << 2) + (dp[9] >> 4 & 3);
  410. pix[7] = (dp[8] << 2) + (dp[9] >> 6 );
  411. }
  412. for (col=0; col < p->width; col++)
  413. BAYER(row,col) = pixel[col];
  414. for (col=p->width; col < p->raw_width; col++)
  415. p->black += pixel[col];
  416. if ((row+=2) > p->height) row = 1;
  417. }
  418. if (p->raw_width > p->width)
  419. p->black = p->black / ((p->raw_width - p->width) * p->height) - 4;
  420. for (row=0; row < p->height; row++)
  421. for (col=0; col < p->width; col++) {
  422. if ((val = BAYER(row,col) - p->black) < 0) val = 0;
  423. val = val * mul[row & 3][col & 1] >> 9;
  424. BAYER(row,col) = val;
  425. }
  426. dcr_canon_600_fixed_wb(p,1311);
  427. dcr_canon_600_auto_wb(p);
  428. dcr_canon_600_coeff(p);
  429. p->maximum = (0x3ff - p->black) * 1109 >> 9;
  430. p->black = 0;
  431. }
  432. void DCR_CLASS dcr_remove_zeroes(DCRAW* p)
  433. {
  434. unsigned row, col, tot, n, r, c;
  435. for (row=0; row < p->height; row++)
  436. for (col=0; col < p->width; col++)
  437. if (BAYER(row,col) == 0) {
  438. tot = n = 0;
  439. for (r = row-2; r <= row+2; r++)
  440. for (c = col-2; c <= col+2; c++)
  441. if (r < p->height && c < p->width &&
  442. FC(r,c) == FC(row,col) && BAYER(r,c))
  443. tot += (n++,BAYER(r,c));
  444. if (n) BAYER(row,col) = tot/n;
  445. }
  446. }
  447. int DCR_CLASS dcr_canon_s2is(DCRAW* p)
  448. {
  449. unsigned row;
  450. for (row=0; row < 100; row++) {
  451. dcr_fseek(p->obj_, row*3340 + 3284, SEEK_SET);
  452. if (dcr_fgetc(p->obj_) > 15) return 1;
  453. }
  454. return 0;
  455. }
  456. void DCR_CLASS dcr_canon_a5_load_raw(DCRAW* p)
  457. {
  458. ushort data[2565], *dp, pixel;
  459. int vbits=0, buf=0, row, col, bc=0;
  460. p->order = 0x4949;
  461. for (row=-p->top_margin; row < p->raw_height-p->top_margin; row++) {
  462. dcr_read_shorts (p,dp=data, p->raw_width * 10 / 16);
  463. for (col=-p->left_margin; col < p->raw_width-p->left_margin; col++) {
  464. if ((vbits -= 10) < 0)
  465. buf = (vbits += 16, (buf << 16) + *dp++);
  466. pixel = buf >> vbits & 0x3ff;
  467. if ((unsigned) row < p->height && (unsigned) col < p->width)
  468. BAYER(row,col) = pixel;
  469. else if (col > 1-p->left_margin && col != p->width)
  470. p->black += (bc++,pixel);
  471. }
  472. }
  473. if (bc) p->black /= bc;
  474. p->maximum = 0x3ff;
  475. if (p->raw_width > 1600) dcr_remove_zeroes(p);
  476. }
  477. /*
  478. dcr_getbits(p, -1) initializes the buffer
  479. dcr_getbits(p, n) where 0 <= n <= 25 returns an n-bit integer
  480. */
  481. unsigned DCR_CLASS dcr_getbits (DCRAW* p, int nbits)
  482. {
  483. static unsigned bitbuf=0;
  484. static int vbits=0, reset=0;
  485. unsigned c;
  486. if (nbits == -1)
  487. return bitbuf = vbits = reset = 0;
  488. if (nbits == 0 || reset) return 0;
  489. while (vbits < nbits) {
  490. if ((c = dcr_fgetc(p->obj_)) == EOF) dcr_derror(p);
  491. if ((reset = p->zero_after_ff && c == 0xff && dcr_fgetc(p->obj_))) return 0;
  492. bitbuf = (bitbuf << 8) + (uchar) c;
  493. vbits += 8;
  494. }
  495. vbits -= nbits;
  496. return bitbuf << (32-nbits-vbits) >> (32-nbits);
  497. }
  498. void DCR_CLASS dcr_init_decoder(DCRAW* p)
  499. {
  500. memset (p->first_decode, 0, sizeof p->first_decode);
  501. p->free_decode = p->first_decode;
  502. }
  503. /*
  504. Construct a decode tree according the specification in *source.
  505. The first 16 bytes specify how many codes should be 1-bit, 2-bit
  506. 3-bit, etc. Bytes after that are the leaf values.
  507. For example, if the source is
  508. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
  509. 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
  510. then the code is
  511. 00 0x04
  512. 010 0x03
  513. 011 0x05
  514. 100 0x06
  515. 101 0x02
  516. 1100 0x07
  517. 1101 0x01
  518. 11100 0x08
  519. 11101 0x09
  520. 11110 0x00
  521. 111110 0x0a
  522. 1111110 0x0b
  523. 1111111 0xff
  524. */
  525. uchar * DCR_CLASS dcr_make_decoder (DCRAW* p, const uchar *source, int level)
  526. {
  527. struct dcr_decode *cur;
  528. static int leaf;
  529. int i, next;
  530. if (level==0) leaf=0;
  531. cur = p->free_decode++;
  532. if (p->free_decode > p->first_decode+2048) {
  533. fprintf (stderr,_("%s: decoder table overflow\n"), p->ifname);
  534. longjmp (p->failure, 2);
  535. }
  536. for (i=next=0; i <= leaf && next < 16; )
  537. i += source[next++];
  538. if (i > leaf) {
  539. if (level < next) {
  540. cur->branch[0] = p->free_decode;
  541. dcr_make_decoder (p, source, level+1);
  542. cur->branch[1] = p->free_decode;
  543. dcr_make_decoder (p, source, level+1);
  544. } else
  545. cur->leaf = source[16 + leaf++];
  546. }
  547. return (uchar *) source + 16 + leaf;
  548. }
  549. void DCR_CLASS dcr_crw_init_tables (DCRAW* p, unsigned table)
  550. {
  551. static const uchar first_tree[3][29] = {
  552. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
  553. 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
  554. { 0,2,2,3,1,1,1,1,2,0,0,0,0,0,0,0,
  555. 0x03,0x02,0x04,0x01,0x05,0x00,0x06,0x07,0x09,0x08,0x0a,0x0b,0xff },
  556. { 0,0,6,3,1,1,2,0,0,0,0,0,0,0,0,0,
  557. 0x06,0x05,0x07,0x04,0x08,0x03,0x09,0x02,0x00,0x0a,0x01,0x0b,0xff },
  558. };
  559. static const uchar second_tree[3][180] = {
  560. { 0,2,2,2,1,4,2,1,2,5,1,1,0,0,0,139,
  561. 0x03,0x04,0x02,0x05,0x01,0x06,0x07,0x08,
  562. 0x12,0x13,0x11,0x14,0x09,0x15,0x22,0x00,0x21,0x16,0x0a,0xf0,
  563. 0x23,0x17,0x24,0x31,0x32,0x18,0x19,0x33,0x25,0x41,0x34,0x42,
  564. 0x35,0x51,0x36,0x37,0x38,0x29,0x79,0x26,0x1a,0x39,0x56,0x57,
  565. 0x28,0x27,0x52,0x55,0x58,0x43,0x76,0x59,0x77,0x54,0x61,0xf9,
  566. 0x71,0x78,0x75,0x96,0x97,0x49,0xb7,0x53,0xd7,0x74,0xb6,0x98,
  567. 0x47,0x48,0x95,0x69,0x99,0x91,0xfa,0xb8,0x68,0xb5,0xb9,0xd6,
  568. 0xf7,0xd8,0x67,0x46,0x45,0x94,0x89,0xf8,0x81,0xd5,0xf6,0xb4,
  569. 0x88,0xb1,0x2a,0x44,0x72,0xd9,0x87,0x66,0xd4,0xf5,0x3a,0xa7,
  570. 0x73,0xa9,0xa8,0x86,0x62,0xc7,0x65,0xc8,0xc9,0xa1,0xf4,0xd1,
  571. 0xe9,0x5a,0x92,0x85,0xa6,0xe7,0x93,0xe8,0xc1,0xc6,0x7a,0x64,
  572. 0xe1,0x4a,0x6a,0xe6,0xb3,0xf1,0xd3,0xa5,0x8a,0xb2,0x9a,0xba,
  573. 0x84,0xa4,0x63,0xe5,0xc5,0xf3,0xd2,0xc4,0x82,0xaa,0xda,0xe4,
  574. 0xf2,0xca,0x83,0xa3,0xa2,0xc3,0xea,0xc2,0xe2,0xe3,0xff,0xff },
  575. { 0,2,2,1,4,1,4,1,3,3,1,0,0,0,0,140,
  576. 0x02,0x03,0x01,0x04,0x05,0x12,0x11,0x06,
  577. 0x13,0x07,0x08,0x14,0x22,0x09,0x21,0x00,0x23,0x15,0x31,0x32,
  578. 0x0a,0x16,0xf0,0x24,0x33,0x41,0x42,0x19,0x17,0x25,0x18,0x51,
  579. 0x34,0x43,0x52,0x29,0x35,0x61,0x39,0x71,0x62,0x36,0x53,0x26,
  580. 0x38,0x1a,0x37,0x81,0x27,0x91,0x79,0x55,0x45,0x28,0x72,0x59,
  581. 0xa1,0xb1,0x44,0x69,0x54,0x58,0xd1,0xfa,0x57,0xe1,0xf1,0xb9,
  582. 0x49,0x47,0x63,0x6a,0xf9,0x56,0x46,0xa8,0x2a,0x4a,0x78,0x99,
  583. 0x3a,0x75,0x74,0x86,0x65,0xc1,0x76,0xb6,0x96,0xd6,0x89,0x85,
  584. 0xc9,0xf5,0x95,0xb4,0xc7,0xf7,0x8a,0x97,0xb8,0x73,0xb7,0xd8,
  585. 0xd9,0x87,0xa7,0x7a,0x48,0x82,0x84,0xea,0xf4,0xa6,0xc5,0x5a,
  586. 0x94,0xa4,0xc6,0x92,0xc3,0x68,0xb5,0xc8,0xe4,0xe5,0xe6,0xe9,
  587. 0xa2,0xa3,0xe3,0xc2,0x66,0x67,0x93,0xaa,0xd4,0xd5,0xe7,0xf8,
  588. 0x88,0x9a,0xd7,0x77,0xc4,0x64,0xe2,0x98,0xa5,0xca,0xda,0xe8,
  589. 0xf3,0xf6,0xa9,0xb2,0xb3,0xf2,0xd2,0x83,0xba,0xd3,0xff,0xff },
  590. { 0,0,6,2,1,3,3,2,5,1,2,2,8,10,0,117,
  591. 0x04,0x05,0x03,0x06,0x02,0x07,0x01,0x08,
  592. 0x09,0x12,0x13,0x14,0x11,0x15,0x0a,0x16,0x17,0xf0,0x00,0x22,
  593. 0x21,0x18,0x23,0x19,0x24,0x32,0x31,0x25,0x33,0x38,0x37,0x34,
  594. 0x35,0x36,0x39,0x79,0x57,0x58,0x59,0x28,0x56,0x78,0x27,0x41,
  595. 0x29,0x77,0x26,0x42,0x76,0x99,0x1a,0x55,0x98,0x97,0xf9,0x48,
  596. 0x54,0x96,0x89,0x47,0xb7,0x49,0xfa,0x75,0x68,0xb6,0x67,0x69,
  597. 0xb9,0xb8,0xd8,0x52,0xd7,0x88,0xb5,0x74,0x51,0x46,0xd9,0xf8,
  598. 0x3a,0xd6,0x87,0x45,0x7a,0x95,0xd5,0xf6,0x86,0xb4,0xa9,0x94,
  599. 0x53,0x2a,0xa8,0x43,0xf5,0xf7,0xd4,0x66,0xa7,0x5a,0x44,0x8a,
  600. 0xc9,0xe8,0xc8,0xe7,0x9a,0x6a,0x73,0x4a,0x61,0xc7,0xf4,0xc6,
  601. 0x65,0xe9,0x72,0xe6,0x71,0x91,0x93,0xa6,0xda,0x92,0x85,0x62,
  602. 0xf3,0xc5,0xb2,0xa4,0x84,0xba,0x64,0xa5,0xb3,0xd2,0x81,0xe5,
  603. 0xd3,0xaa,0xc4,0xca,0xf2,0xb1,0xe4,0xd1,0x83,0x63,0xea,0xc3,
  604. 0xe2,0x82,0xf1,0xa3,0xc2,0xa1,0xc1,0xe3,0xa2,0xe1,0xff,0xff }
  605. };
  606. if (table > 2) table = 2;
  607. dcr_init_decoder(p);
  608. dcr_make_decoder (p, first_tree[table], 0);
  609. p->second_decode = p->free_decode;
  610. dcr_make_decoder (p, second_tree[table], 0);
  611. }
  612. /*
  613. Return 0 if the image starts with compressed data,
  614. 1 if it starts with uncompressed low-order bits.
  615. In Canon compressed data, 0xff is always followed by 0x00.
  616. */
  617. int DCR_CLASS dcr_canon_has_lowbits(DCRAW* p)
  618. {
  619. uchar test[0x4000];
  620. int ret=1, i;
  621. dcr_fseek(p->obj_, 0, SEEK_SET);
  622. dcr_fread(p->obj_, test, 1, sizeof test);
  623. for (i=540; i < sizeof test - 1; i++)
  624. if (test[i] == 0xff) {
  625. if (test[i+1]) return 1;
  626. ret=0;
  627. }
  628. return ret;
  629. }
  630. void DCR_CLASS dcr_canon_compressed_load_raw(DCRAW* p)
  631. {
  632. ushort *pixel, *prow;
  633. int nblocks, lowbits, i, row, r, col, save, val;
  634. unsigned irow, icol;
  635. struct dcr_decode *decode, *dindex;
  636. int block, diffbuf[64], leaf, len, diff, carry=0, pnum=0, base[2];
  637. double dark[2] = { 0,0 };
  638. uchar c;
  639. dcr_crw_init_tables (p, p->tiff_compress);
  640. pixel = (ushort *) calloc (p->raw_width*8, sizeof *pixel);
  641. dcr_merror (p, pixel, "canon_compressed_load_raw()");
  642. lowbits = dcr_canon_has_lowbits(p);
  643. if (!lowbits) p->maximum = 0x3ff;
  644. dcr_fseek(p->obj_, 540 + lowbits*p->raw_height*p->raw_width/4, SEEK_SET);
  645. p->zero_after_ff = 1;
  646. dcr_getbits(p, -1);
  647. for (row=0; row < p->raw_height; row+=8) {
  648. nblocks = MIN (8, p->raw_height-row) * p->raw_width >> 6;
  649. for (block=0; block < nblocks; block++) {
  650. memset (diffbuf, 0, sizeof diffbuf);
  651. decode = p->first_decode;
  652. for (i=0; i < 64; i++ ) {
  653. for (dindex=decode; dindex->branch[0]; )
  654. dindex = dindex->branch[dcr_getbits(p, 1)];
  655. leaf = dindex->leaf;
  656. decode = p->second_decode;
  657. if (leaf == 0 && i) break;
  658. if (leaf == 0xff) continue;
  659. i += leaf >> 4;
  660. len = leaf & 15;
  661. if (len == 0) continue;
  662. diff = dcr_getbits(p, len);
  663. if ((diff & (1 << (len-1))) == 0)
  664. diff -= (1 << len) - 1;
  665. if (i < 64) diffbuf[i] = diff;
  666. }
  667. diffbuf[0] += carry;
  668. carry = diffbuf[0];
  669. for (i=0; i < 64; i++ ) {
  670. if (pnum++ % p->raw_width == 0)
  671. base[0] = base[1] = 512;
  672. if ((pixel[(block << 6) + i] = base[i & 1] += diffbuf[i]) >> 10)
  673. dcr_derror(p);
  674. }
  675. }
  676. if (lowbits) {
  677. save = dcr_ftell(p->obj_);
  678. dcr_fseek(p->obj_, 26 + row*p->raw_width/4, SEEK_SET);
  679. for (prow=pixel, i=0; i < p->raw_width*2; i++) {
  680. c = dcr_fgetc(p->obj_);
  681. for (r=0; r < 8; r+=2, prow++) {
  682. val = (*prow << 2) + ((c >> r) & 3);
  683. if (p->raw_width == 2672 && val < 512) val += 2;
  684. *prow = val;
  685. }
  686. }
  687. dcr_fseek(p->obj_, save, SEEK_SET);
  688. }
  689. for (r=0; r < 8; r++) {
  690. irow = row - p->top_margin + r;
  691. if (irow >= p->height) continue;
  692. for (col=0; col < p->raw_width; col++) {
  693. icol = col - p->left_margin;
  694. if (icol < p->width)
  695. BAYER(irow,icol) = pixel[r*p->raw_width+col];
  696. else if (col > 1)
  697. dark[icol & 1] += pixel[r*p->raw_width+col];
  698. }
  699. }
  700. }
  701. free (pixel);
  702. dcr_canon_black (p, dark);
  703. }
  704. /*
  705. Not a full implementation of Lossless JPEG, just
  706. enough to decode Canon, Kodak and Adobe DNG images.
  707. */
  708. int DCR_CLASS dcr_ljpeg_start (DCRAW* p, struct dcr_jhead *jh, int info_only)
  709. {
  710. int c, tag, len;
  711. uchar data[0x10000], *dp;
  712. dcr_init_decoder(p);
  713. memset (jh, 0, sizeof *jh);
  714. FORC(6) jh->huff[c] = p->free_decode;
  715. jh->restart = INT_MAX;
  716. dcr_fread(p->obj_, data, 2, 1);
  717. if (data[1] != 0xd8) return 0;
  718. do {
  719. dcr_fread(p->obj_, data, 2, 2);
  720. tag = data[0] << 8 | data[1];
  721. len = (data[2] << 8 | data[3]) - 2;
  722. if (tag <= 0xff00) return 0;
  723. dcr_fread(p->obj_, data, 1, len);
  724. switch (tag) {
  725. case 0xffc3:
  726. jh->sraw = ((data[7] >> 4) * (data[7] & 15) - 1) & 3;
  727. case 0xffc0:
  728. jh->bits = data[0];
  729. jh->high = data[1] << 8 | data[2];
  730. jh->wide = data[3] << 8 | data[4];
  731. jh->clrs = data[5] + jh->sraw;
  732. if (len == 9 && !p->dng_version) dcr_fgetc(p->obj_);
  733. break;
  734. case 0xffc4:
  735. if (info_only) break;
  736. for (dp = data; dp < data+len && *dp < 4; ) {
  737. jh->huff[*dp] = p->free_decode;
  738. dp = dcr_make_decoder (p, ++dp, 0);
  739. }
  740. break;
  741. case 0xffda:
  742. jh->psv = data[1+data[0]*2];
  743. jh->bits -= data[3+data[0]*2] & 15;
  744. break;
  745. case 0xffdd:
  746. jh->restart = data[0] << 8 | data[1];
  747. }
  748. } while (tag != 0xffda);
  749. if (info_only) return 1;
  750. if (jh->sraw) {
  751. FORC(4) jh->huff[2+c] = jh->huff[1];
  752. FORC(jh->sraw) jh->huff[1+c] = jh->huff[0];
  753. }
  754. jh->row = (ushort *) calloc (jh->wide*jh->clrs, 4);
  755. dcr_merror (p, jh->row, "dcr_ljpeg_start()");
  756. return p->zero_after_ff = 1;
  757. }
  758. int DCR_CLASS dcr_ljpeg_diff (DCRAW* p, struct dcr_decode *dindex)
  759. {
  760. int len, diff;
  761. while (dindex->branch[0])
  762. dindex = dindex->branch[dcr_getbits(p, 1)];
  763. len = dindex->leaf;
  764. if (len == 16 && (!p->dng_version || p->dng_version >= 0x1010000))
  765. return -32768;
  766. diff = dcr_getbits(p, len);
  767. if ((diff & (1 << (len-1))) == 0)
  768. diff -= (1 << len) - 1;
  769. return diff;
  770. }
  771. ushort * DCR_CLASS dcr_ljpeg_row (DCRAW* p, int jrow, struct dcr_jhead *jh)
  772. {
  773. int col, c, diff, pred, spred=0;
  774. ushort mark=0, *row[3];
  775. if (jrow * jh->wide % jh->restart == 0) {
  776. FORC(6) jh->vpred[c] = 1 << (jh->bits-1);
  777. if (jrow)
  778. do mark = (mark << 8) + (c = dcr_fgetc(p->obj_));
  779. while (c != EOF && mark >> 4 != 0xffd);
  780. dcr_getbits(p, -1);
  781. }
  782. FORC3 row[c] = jh->row + jh->wide*jh->clrs*((jrow+c) & 1);
  783. for (col=0; col < jh->wide; col++)
  784. FORC(jh->clrs) {
  785. diff = dcr_ljpeg_diff (p, jh->huff[c]);
  786. if (jh->sraw && c <= jh->sraw && (col | c))
  787. pred = spred;
  788. else if (col) pred = row[0][-jh->clrs];
  789. else pred = (jh->vpred[c] += diff) - diff;
  790. if (jrow && col) switch (jh->psv) {
  791. case 1: break;
  792. case 2: pred = row[1][0]; break;
  793. case 3: pred = row[1][-jh->clrs]; break;
  794. case 4: pred = pred + row[1][0] - row[1][-jh->clrs]; break;
  795. case 5: pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1); break;
  796. case 6: pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1); break;
  797. case 7: pred = (pred + row[1][0]) >> 1; break;
  798. default: pred = 0;
  799. }
  800. if ((**row = pred + diff) >> jh->bits) dcr_derror(p);
  801. if (c <= jh->sraw) spred = **row;
  802. row[0]++; row[1]++;
  803. }
  804. return row[2];
  805. }
  806. void DCR_CLASS dcr_lossless_jpeg_load_raw(DCRAW* p)
  807. {
  808. int jwide, jrow, jcol, val, jidx, i, j, row=0, col=0;
  809. double dark[2] = { 0,0 };
  810. struct dcr_jhead jh;
  811. int min=INT_MAX;
  812. ushort *rp;
  813. if (!dcr_ljpeg_start (p,&jh, 0)) return;
  814. jwide = jh.wide * jh.clrs;
  815. for (jrow=0; jrow < jh.high; jrow++) {
  816. rp = dcr_ljpeg_row (p, jrow, &jh);
  817. for (jcol=0; jcol < jwide; jcol++) {
  818. val = *rp++;
  819. if (jh.bits <= 12)
  820. val = p->curve[val & 0xfff];
  821. if (p->cr2_slice[0]) {
  822. jidx = jrow*jwide + jcol;
  823. i = jidx / (p->cr2_slice[1]*jh.high);
  824. if ((j = i >= p->cr2_slice[0]))
  825. i = p->cr2_slice[0];
  826. jidx -= i * (p->cr2_slice[1]*jh.high);
  827. row = jidx / p->cr2_slice[1+j];
  828. col = jidx % p->cr2_slice[1+j] + i*p->cr2_slice[1];
  829. }
  830. if (p->raw_width == 3984 && (col -= 2) < 0)
  831. col += (row--,p->raw_width);
  832. if ((unsigned) (row-p->top_margin) < p->height) {
  833. if ((unsigned) (col-p->left_margin) < p->width) {
  834. BAYER(row-p->top_margin,col-p->left_margin) = val;
  835. if (min > val) min = val;
  836. } else if (col > 1)
  837. dark[(col-p->left_margin) & 1] += val;
  838. }
  839. if (++col >= p->raw_width)
  840. col = (row++,0);
  841. }
  842. }
  843. free (jh.row);
  844. dcr_canon_black (p, dark);
  845. if (!strcasecmp(p->make,"KODAK"))
  846. p->black = min;
  847. }
  848. void DCR_CLASS dcr_canon_sraw_load_raw(DCRAW* p)
  849. {
  850. struct dcr_jhead jh;
  851. short *rp=0, (*ip)[4];
  852. int jwide, slice, scol, ecol, row, col, jrow=0, jcol=0, pix[3], c;
  853. if (!dcr_ljpeg_start (p, &jh, 0)) return;
  854. jwide = (jh.wide >>= 1) * jh.clrs;
  855. for (ecol=slice=0; slice <= p->cr2_slice[0]; slice++) {
  856. scol = ecol;
  857. ecol += p->cr2_slice[1] * 2 / jh.clrs;
  858. if (!p->cr2_slice[0] || ecol > p->raw_width-1) ecol = p->raw_width & -2;
  859. for (row=0; row < p->height; row += (jh.clrs >> 1) - 1) {
  860. ip = (short (*)[4]) p->image + row*p->width;
  861. for (col=scol; col < ecol; col+=2, jcol+=jh.clrs) {
  862. if ((jcol %= jwide) == 0)
  863. rp = (short *) dcr_ljpeg_row (p, jrow++, &jh);
  864. if (col >= p->width) continue;
  865. FORC (jh.clrs-2)
  866. ip[col + (c >> 1)*p->width + (c & 1)][0] = rp[jcol+c];
  867. ip[col][1] = rp[jcol+jh.clrs-2] - 16384;
  868. ip[col][2] = rp[jcol+jh.clrs-1] - 16384;
  869. }
  870. }
  871. }
  872. ip = (short (*)[4]) p->image;
  873. rp = ip[0];
  874. for (row=0; row < p->height; row++, ip+=p->width) {
  875. if (row & (jh.sraw >> 1))
  876. for (col=0; col < p->width; col+=2)
  877. for (c=1; c < 3; c++)
  878. if (row == p->height-1)
  879. ip[col][c] = ip[col-p->width][c];
  880. else ip[col][c] = (ip[col-p->width][c] + ip[col+p->width][c] + 1) >> 1;
  881. for (col=1; col < p->width; col+=2)
  882. for (c=1; c < 3; c++)
  883. if (col == p->width-1)
  884. ip[col][c] = ip[col-1][c];
  885. else ip[col][c] = (ip[col-1][c] + ip[col+1][c] + 1) >> 1;
  886. }
  887. for ( ; rp < ip[0]; rp+=4) {
  888. if (p->unique_id < 0x80000200) {
  889. pix[0] = rp[0] + rp[2] - 512;
  890. pix[2] = rp[0] + rp[1] - 512;
  891. pix[1] = rp[0] + ((-778*rp[1] - (rp[2] << 11)) >> 12) - 512;
  892. } else {
  893. rp[1] += jh.sraw+1;
  894. rp[2] += jh.sraw+1;
  895. pix[0] = rp[0] + (( 200*rp[1] + 22929*rp[2]) >> 12);
  896. pix[1] = rp[0] + ((-5640*rp[1] - 11751*rp[2]) >> 12);
  897. pix[2] = rp[0] + ((29040*rp[1] - 101*rp[2]) >> 12);
  898. }
  899. FORC3 rp[c] = CLIP(pix[c] * p->sraw_mul[c] >> 10);
  900. }
  901. free (jh.row);
  902. p->maximum = 0x3fff;
  903. }
  904. void DCR_CLASS dcr_adobe_copy_pixel (DCRAW* p, int row, int col, ushort **rp)
  905. {
  906. unsigned r, c;
  907. r = row -= p->top_margin;
  908. c = col -= p->left_margin;
  909. if (p->is_raw == 2 && p->opt.shot_select) (*rp)++;
  910. if (p->filters) {
  911. if (p->fuji_width) {
  912. r = row + p->fuji_width - 1 - (col >> 1);
  913. c = row + ((col+1) >> 1);
  914. }
  915. if (r < p->height && c < p->width)
  916. BAYER(r,c) = **rp < 0x1000 ? p->curve[**rp] : **rp;
  917. *rp += p->is_raw;
  918. } else {
  919. if (r < p->height && c < p->width)
  920. FORC(p->tiff_samples)
  921. p->image[row*p->width+col][c] = (*rp)[c] < 0x1000 ? p->curve[(*rp)[c]]:(*rp)[c];
  922. *rp += p->tiff_samples;
  923. }
  924. if (p->is_raw == 2 && p->opt.shot_select) (*rp)--;
  925. }
  926. void DCR_CLASS dcr_adobe_dng_load_raw_lj(DCRAW* p)
  927. {
  928. unsigned save, trow=0, tcol=0, jwide, jrow, jcol, row, col;
  929. struct dcr_jhead jh;
  930. ushort *rp;
  931. while (trow < p->raw_height) {
  932. save = dcr_ftell(p->obj_);
  933. if (p->tile_length < INT_MAX)
  934. dcr_fseek(p->obj_, dcr_get4(p), SEEK_SET);
  935. if (!dcr_ljpeg_start (p,&jh, 0)) break;
  936. jwide = jh.wide;
  937. if (p->filters) jwide *= jh.clrs;
  938. jwide /= p->is_raw;
  939. for (row=col=jrow=0; (int)jrow < jh.high; jrow++) {
  940. rp = dcr_ljpeg_row (p, jrow, &jh);
  941. for (jcol=0; jcol < jwide; jcol++) {
  942. dcr_adobe_copy_pixel (p,trow+row, tcol+col, &rp);
  943. if (++col >= p->tile_width || col >= p->raw_width)
  944. row += 1 + (col = 0);
  945. }
  946. }
  947. dcr_fseek(p->obj_, save+4, SEEK_SET);
  948. if ((tcol += p->tile_width) >= p->raw_width)
  949. trow += p->tile_length + (tcol = 0);
  950. free (jh.row);
  951. }
  952. }
  953. void DCR_CLASS dcr_adobe_dng_load_raw_nc(DCRAW* p)
  954. {
  955. ushort *pixel, *rp;
  956. unsigned int row, col;
  957. pixel = (ushort *) calloc (p->raw_width * p->tiff_samples, sizeof *pixel);
  958. dcr_merror (p, pixel, "adobe_dng_load_raw_nc()");
  959. for (row=0; row < p->raw_height; row++) {
  960. if (p->tiff_bps == 16)
  961. dcr_read_shorts (p, pixel, p->raw_width * p->tiff_samples);
  962. else {
  963. dcr_getbits(p, -1);
  964. for (col=0; col < p->raw_width * p->tiff_samples; col++)
  965. pixel[col] = dcr_getbits(p, p->tiff_bps);
  966. }
  967. for (rp=pixel, col=0; col < p->raw_width; col++)
  968. dcr_adobe_copy_pixel (p,row, col, &rp);
  969. }
  970. free (pixel);
  971. }
  972. void DCR_CLASS dcr_pentax_k10_load_raw(DCRAW* p)
  973. {
  974. static const uchar pentax_tree[] =
  975. { 0,2,3,1,1,1,1,1,1,2,0,0,0,0,0,0,
  976. 3,4,2,5,1,6,0,7,8,9,10,11,12 };
  977. int row, col, diff;
  978. ushort vpred[2][2] = {{0,0},{0,0}}, hpred[2];
  979. dcr_init_decoder(p);
  980. dcr_make_decoder (p, pentax_tree, 0);
  981. dcr_getbits(p, -1);
  982. for (row=0; row < p->height; row++)
  983. for (col=0; col < p->raw_width; col++) {
  984. diff = dcr_ljpeg_diff (p, p->first_decode);
  985. if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
  986. else hpred[col & 1] += diff;
  987. if (col < p->width)
  988. BAYER(row,col) = hpred[col & 1];
  989. if (hpred[col & 1] >> 12) dcr_derror(p);
  990. }
  991. }
  992. void DCR_CLASS dcr_nikon_compressed_load_raw(DCRAW* p)
  993. {
  994. static const uchar nikon_tree[][32] = {
  995. { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy */
  996. 5,4,3,6,2,7,1,0,8,9,11,10,12 },
  997. { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy after split */
  998. 0x39,0x5a,0x38,0x27,0x16,5,4,3,2,1,0,11,12,12 },
  999. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0, /* 12-bit lossless */
  1000. 5,4,6,3,7,2,8,1,9,0,10,11,12 },
  1001. { 0,1,4,3,1,1,1,1,1,2,0,0,0,0,0,0, /* 14-bit lossy */
  1002. 5,6,4,7,8,3,9,2,1,0,10,11,12,13,14 },
  1003. { 0,1,5,1,1,1,1,1,1,1,2,0,0,0,0,0, /* 14-bit lossy after split */
  1004. 8,0x5c,0x4b,0x3a,0x29,7,6,5,4,3,2,1,0,13,14 },
  1005. { 0,1,4,2,2,3,1,2,0,0,0,0,0,0,0,0, /* 14-bit lossless */
  1006. 7,6,8,5,9,4,10,3,11,12,2,0,1,13,14 } };
  1007. struct dcr_decode *dindex;
  1008. ushort ver0, ver1, vpred[2][2], hpred[2], csize;
  1009. int i, min, max, step=0, huff=0, split=0, row, col, len, shl, diff;
  1010. dcr_fseek(p->obj_, p->meta_offset, SEEK_SET);
  1011. ver0 = dcr_fgetc(p->obj_);
  1012. ver1 = dcr_fgetc(p->obj_);
  1013. if (ver0 == 0x49 || ver1 == 0x58)
  1014. dcr_fseek(p->obj_, 2110, SEEK_CUR);
  1015. if (ver0 == 0x46) huff = 2;
  1016. if (p->tiff_bps == 14) huff += 3;
  1017. dcr_read_shorts (p, vpred[0], 4);
  1018. max = 1 << p->tiff_bps & 0x7fff;
  1019. if ((csize = dcr_get2(p)) > 1)
  1020. step = max / (csize-1);
  1021. if (ver0 == 0x44 && ver1 == 0x20 && step > 0) {
  1022. for (i=0; i < csize; i++)
  1023. p->curve[i*step] = dcr_get2(p);
  1024. for (i=0; i < max; i++)
  1025. p->curve[i] = ( p->curve[i-i%step]*(step-i%step) +
  1026. p->curve[i-i%step+step]*(i%step) ) / step;
  1027. dcr_fseek(p->obj_, p->meta_offset+562, SEEK_SET);
  1028. split = dcr_get2(p);
  1029. } else if (ver0 != 0x46 && csize <= 0x4001)
  1030. dcr_read_shorts (p, p->curve, max=csize);
  1031. while (p->curve[max-2] == p->curve[max-1]) max--;
  1032. dcr_init_decoder(p);
  1033. dcr_make_decoder (p, nikon_tree[huff], 0);
  1034. dcr_fseek(p->obj_, p->data_offset, SEEK_SET);
  1035. dcr_getbits(p, -1);
  1036. for (min=row=0; row < p->height; row++) {
  1037. if (split && row == split) {
  1038. dcr_init_decoder(p);
  1039. dcr_make_decoder (p, nikon_tree[huff+1], 0);
  1040. max += (min = 16) << 1;
  1041. }
  1042. for (col=0; col < p->raw_width; col++) {
  1043. for (dindex=p->first_decode; dindex->branch[0]; )
  1044. dindex = dindex->branch[dcr_getbits(p, 1)];
  1045. len = dindex->leaf & 15;
  1046. shl = dindex->leaf >> 4;
  1047. diff = ((dcr_getbits(p, len-shl) << 1) + 1) << shl >> 1;
  1048. if ((diff & (1 << (len-1))) == 0)
  1049. diff -= (1 << len) - !shl;
  1050. if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
  1051. else hpred[col & 1] += diff;
  1052. if ((ushort)(hpred[col & 1] + min) >= max) dcr_derror(p);
  1053. if ((unsigned) (col-p->left_margin) < p->width)
  1054. BAYER(row,col-p->left_margin) = p->curve[LIM((short)hpred[col & 1],0,0x3fff)];
  1055. }
  1056. }
  1057. }
  1058. /*
  1059. Figure out if a NEF file is compressed. These fancy heuristics
  1060. are only needed for the D100, thanks to a bug in some cameras
  1061. that tags all images as "compressed".
  1062. */
  1063. int DCR_CLASS dcr_nikon_is_compressed(DCRAW* p)
  1064. {
  1065. uchar test[256];
  1066. int i;
  1067. dcr_fseek(p->obj_, p->data_offset, SEEK_SET);
  1068. dcr_fread(p->obj_, test, 1, 256);
  1069. for (i=15; i < 256; i+=16)
  1070. if (test[i]) return 1;
  1071. return 0;
  1072. }
  1073. /*
  1074. Returns 1 for a Coolpix 995, 0 for anything else.
  1075. */
  1076. int DCR_CLASS dcr_nikon_e995(DCRAW* p)
  1077. {
  1078. int i, histo[256];
  1079. const uchar often[] = { 0x00, 0x55, 0xaa, 0xff };
  1080. memset (histo, 0, sizeof histo);
  1081. dcr_fseek(p->obj_, -2000, SEEK_END);
  1082. for (i=0; i < 2000; i++)
  1083. histo[dcr_fgetc(p->obj_)]++;
  1084. for (i=0; i < 4; i++)
  1085. if (histo[often[i]] < 200)
  1086. return 0;
  1087. return 1;
  1088. }
  1089. /*
  1090. Returns 1 for a Coolpix 2100, 0 for anything else.
  1091. */
  1092. int DCR_CLASS dcr_nikon_e2100(DCRAW* p)
  1093. {
  1094. uchar t[12];
  1095. int i;
  1096. dcr_fseek(p->obj_, 0, SEEK_SET);
  1097. for (i=0; i < 1024; i++) {
  1098. dcr_fread(p->obj_, t, 1, 12);
  1099. if (((t[2] & t[4] & t[7] & t[9]) >> 4
  1100. & t[1] & t[6] & t[8] & t[11] & 3) != 3)
  1101. return 0;
  1102. }
  1103. return 1;
  1104. }
  1105. void DCR_CLASS dcr_nikon_3700(DCRAW* p)
  1106. {
  1107. int bits, i;
  1108. uchar dp[24];
  1109. const struct {
  1110. int bits;
  1111. char make[12], model[15];
  1112. } table[] = {
  1113. { 0x00, "PENTAX", "Optio 33WR" },
  1114. { 0x03, "NIKON", "E3200" },
  1115. { 0x32, "NIKON", "E3700" },
  1116. { 0x33, "OLYMPUS", "C740UZ" } };
  1117. dcr_fseek(p->obj_, 3072, SEEK_SET);
  1118. dcr_fread(p->obj_, dp, 1, 24);
  1119. bits = (dp[8] & 3) << 4 | (dp[20] & 3);
  1120. for (i=0; i < sizeof table / sizeof *table; i++)
  1121. if (bits == table[i].bits) {
  1122. strcpy (p->make, table[i].make );
  1123. strcpy (p->model, table[i].model);
  1124. }
  1125. }
  1126. /*
  1127. Separates a Minolta DiMAGE Z2 from a Nikon E4300.
  1128. */
  1129. int DCR_CLASS dcr_minolta_z2(DCRAW* p)
  1130. {
  1131. int i, nz;
  1132. char tail[424];
  1133. dcr_fseek(p->obj_, -(long)(sizeof tail), SEEK_END);
  1134. dcr_fread(p->obj_, tail, 1, sizeof tail);
  1135. for (nz=i=0; i < sizeof tail; i++)
  1136. if (tail[i]) nz++;
  1137. return nz > 20;
  1138. }
  1139. /* Here p->raw_width is in bytes, not pixels. */
  1140. void DCR_CLASS dcr_nikon_e900_load_raw(DCRAW* p)
  1141. {
  1142. int offset=0, irow, row, col;
  1143. for (irow=0; irow < p->height; irow++) {
  1144. row = irow * 2 % p->height;
  1145. if (row == 1)
  1146. offset = - (-offset & -4096);
  1147. dcr_fseek(p->obj_, offset, SEEK_SET);
  1148. offset += p->raw_width;
  1149. dcr_getbits(p, -1);
  1150. for (col=0; col < p->width; col++)
  1151. BAYER(row,col) = dcr_getbits(p, 10);
  1152. }
  1153. }
  1154. /*
  1155. The Fuji Super CCD is just a Bayer grid rotated 45 degrees.
  1156. */
  1157. void DCR_CLASS dcr_fuji_load_raw(DCRAW* p)
  1158. {
  1159. ushort *pixel;
  1160. int wide, row, col, r, c;
  1161. dcr_fseek(p->obj_, (p->top_margin*p->raw_width + p->left_margin) * 2, SEEK_CUR);
  1162. wide = p->fuji_width << !p->fuji_layout;
  1163. pixel = (ushort *) calloc (wide, sizeof *pixel);
  1164. dcr_merror (p, pixel, "fuji_load_raw()");
  1165. for (row=0; row < p->raw_height; row++) {
  1166. dcr_read_shorts (p, pixel, wide);
  1167. dcr_fseek(p->obj_, 2*(p->raw_width - wide), SEEK_CUR);
  1168. for (col=0; col < wide; col++) {
  1169. if (p->fuji_layout) {
  1170. r = p->fuji_width - 1 - col + (row >> 1);
  1171. c = col + ((row+1) >> 1);
  1172. } else {
  1173. r = p->fuji_width - 1 + row - (col >> 1);
  1174. c = row + ((col+1) >> 1);
  1175. }
  1176. BAYER(r,c) = pixel[col];
  1177. }
  1178. }
  1179. free (pixel);
  1180. }
  1181. void DCR_CLASS dcr_jpeg_thumb (DCRAW* p, FILE *tfp);
  1182. void DCR_CLASS dcr_ppm_thumb (DCRAW* p, FILE *tfp)
  1183. {
  1184. char *thumb;
  1185. p->thumb_length = p->thumb_width*p->thumb_height*3;
  1186. thumb = (char *) malloc (p->thumb_length);
  1187. dcr_merror (p, thumb, "ppm_thumb()");
  1188. fprintf (tfp, "P6\n%d %d\n255\n", p->thumb_width, p->thumb_height);
  1189. dcr_fread(p->obj_, thumb, 1, p->thumb_length);
  1190. fwrite (thumb, 1, p->thumb_length, tfp);
  1191. free (thumb);
  1192. }
  1193. void DCR_CLASS dcr_layer_thumb (DCRAW* p, FILE *tfp)
  1194. {
  1195. int i, c;
  1196. char *thumb, map[][4] = { "012","102" };
  1197. p->colors = p->thumb_misc >> 5 & 7;
  1198. p->thumb_length = p->thumb_width*p->thumb_height;
  1199. thumb = (char *) calloc (p->colors, p->thumb_length);
  1200. dcr_merror (p, thumb, "layer_thumb()");
  1201. fprintf (tfp, "P%d\n%d %d\n255\n",
  1202. 5 + (p->colors >> 1), p->thumb_width, p->thumb_height);
  1203. dcr_fread(p->obj_, thumb, p->thumb_length, p->colors);
  1204. for (i=0; i < (int)p->thumb_length; i++)
  1205. FORCC(p) putc (thumb[i+p->thumb_length*(map[p->thumb_misc >> 8][c]-'0')], tfp);
  1206. free (thumb);
  1207. }
  1208. void DCR_CLASS dcr_rollei_thumb (DCRAW* p, FILE *tfp)
  1209. {
  1210. unsigned i;
  1211. ushort *thumb;
  1212. p->thumb_length = p->thumb_width * p->thumb_height;
  1213. thumb = (ushort *) calloc (p->thumb_length, 2);
  1214. dcr_merror (p, thumb, "rollei_thumb()");
  1215. fprintf (tfp, "P6\n%d %d\n255\n", p->thumb_width, p->thumb_height);
  1216. dcr_read_shorts (p, thumb, p->thumb_length);
  1217. for (i=0; i < p->thumb_length; i++) {
  1218. putc (thumb[i] << 3, tfp);
  1219. putc (thumb[i] >> 5 << 2, tfp);
  1220. putc (thumb[i] >> 11 << 3, tfp);
  1221. }
  1222. free (thumb);
  1223. }
  1224. void DCR_CLASS dcr_rollei_load_raw(DCRAW* p)
  1225. {
  1226. uchar pixel[10];
  1227. unsigned iten=0, isix, i, buffer=0, row, col, todo[16];
  1228. isix = p->raw_width * p->raw_height * 5 / 8;
  1229. while (dcr_fread(p->obj_, pixel, 1, 10) == 10) {
  1230. for (i=0; i < 10; i+=2) {
  1231. todo[i] = iten++;
  1232. todo[i+1] = pixel[i] << 8 | pixel[i+1];
  1233. buffer = pixel[i] >> 2 | buffer << 6;
  1234. }
  1235. for ( ; i < 16; i+=2) {
  1236. todo[i] = isix++;
  1237. todo[i+1] = buffer >> (14-i)*5;
  1238. }
  1239. for (i=0; i < 16; i+=2) {
  1240. row = todo[i] / p->raw_width - p->top_margin;
  1241. col = todo[i] % p->raw_width - p->left_margin;
  1242. if (row < p->height && col < p->width)
  1243. BAYER(row,col) = (todo[i+1] & 0x3ff);
  1244. }
  1245. }
  1246. p->maximum = 0x3ff;
  1247. }
  1248. int DCR_CLASS dcr_bayer (DCRAW* p, unsigned row, unsigned col)
  1249. {
  1250. return (row < p->height && col < p->width) ? BAYER(row,col) : 0;
  1251. }
  1252. void DCR_CLASS dcr_phase_one_flat_field (DCRAW* p, int is_float, int nc)
  1253. {
  1254. ushort head[8];
  1255. unsigned wide, y, x, rend, cend, row, col;
  1256. int c;
  1257. float *mrow, num, mult[4];
  1258. dcr_read_shorts (p, head, 8);
  1259. wide = head[2] / head[4];
  1260. mrow = (float *) calloc (nc*wide, sizeof *mrow);
  1261. dcr_merror (p, mrow, "phase_one_flat_field()");
  1262. for (y=0; y < (unsigned int)(head[3] / head[5]); y++) {
  1263. for (x=0; x < wide; x++)
  1264. for (c=0; c < nc; c+=2) {
  1265. num = is_float ? (float)dcr_getreal(p, 11) : dcr_get2(p)/32768.0f;
  1266. if (y==0) mrow[c*wide+x] = num;
  1267. else mrow[(c+1)*wide+x] = (num - mrow[c*wide+x]) / head[5];
  1268. }
  1269. if (y==0) continue;
  1270. rend = head[1]-p->top_margin + y*head[5];
  1271. for (row = rend-head[5]; row < p->height && row < rend; row++) {
  1272. for (x=1; x < wide; x++) {
  1273. for (c=0; c < nc; c+=2) {
  1274. mult[c] = mrow[c*wide+x-1];
  1275. mult[c+1] = (mrow[c*wide+x] - mult[c]) / head[4];
  1276. }
  1277. cend = head[0]-p->left_margin + x*head[4];
  1278. for (col = cend-head[4]; col < p->width && col < cend; col++) {
  1279. c = nc > 2 ? FC(row,col) : 0;
  1280. if (!(c & 1)) {
  1281. c = (int)(BAYER(row,col) * mult[c]);
  1282. BAYER(row,col) = LIM(c,0,65535);
  1283. }
  1284. for (c=0; c < nc; c+=2)
  1285. mult[c] += mult[c+1];
  1286. }
  1287. }
  1288. for (x=0; x < wide; x++)
  1289. for (c=0; c < nc; c+=2)
  1290. mrow[c*wide+x] += mrow[(c+1)*wide+x];
  1291. }
  1292. }
  1293. free (mrow);
  1294. }
  1295. void DCR_CLASS dcr_phase_one_correct(DCRAW* p)
  1296. {
  1297. unsigned entries, tag, data, save, col, row, type;
  1298. int len, i, j, k, cip, val[4], dev[4], sum, max;
  1299. int head[9], diff, mindiff=INT_MAX, off_412=0;
  1300. static const signed char dir[12][2] =
  1301. { {-1,-1}, {-1,1}, {1,-1}, {1,1}, {-2,0}, {0,-2}, {0,2}, {2,0},
  1302. {-2,-2}, {-2,2}, {2,-2}, {2,2} };
  1303. float poly[8], num, cfrac, frac, mult[2], *yval[2];
  1304. ushort curve[0x10000], *xval[2];
  1305. if (p->opt.half_size || !p->meta_length) return;
  1306. if (p->opt.verbose) fprintf (stderr,_("Phase One correction...\n"));
  1307. dcr_fseek(p->obj_, p->meta_offset, SEEK_SET);
  1308. p->order = dcr_get2(p);
  1309. dcr_fseek(p->obj_, 6, SEEK_CUR);
  1310. dcr_fseek(p->obj_, p->meta_offset+dcr_get4(p), SEEK_SET);
  1311. entries = dcr_get4(p); dcr_get4(p);
  1312. while (entries--) {
  1313. tag = dcr_get4(p);
  1314. len = dcr_get4(p);
  1315. data = dcr_get4(p);
  1316. save = dcr_ftell(p->obj_);
  1317. dcr_fseek(p->obj_, p->meta_offset+data, SEEK_SET);
  1318. if (tag == 0x419) { /* Polynomial curve */
  1319. for (dcr_get4(p), i=0; i < 8; i++)
  1320. poly[i] = (float)dcr_getreal(p, 11);
  1321. poly[3] += (p->ph1.tag_210 - poly[7]) * poly[6] + 1;
  1322. for (i=0; i < 0x10000; i++) {
  1323. num = (poly[5]*i + poly[3])*i + poly[1];
  1324. curve[i] = (unsigned short)LIM(num,0,65535);
  1325. } goto apply; /* apply to right half */
  1326. } else if (tag == 0x41a) { /* Polynomial curve */
  1327. for (i=0; i < 4; i++)
  1328. poly[i] = (float)dcr_getreal(p, 11);
  1329. for (i=0; i < 0x10000; i++) {
  1330. for (num=0, j=4; j--; )
  1331. num = num * i + poly[j];
  1332. curve[i] = (unsigned short)LIM(num+i,0,65535);
  1333. } apply: /* apply to whole image */
  1334. for (row=0; row < p->height; row++)
  1335. for (col = (tag & 1)*p->ph1.split_col; col < p->width; col++)
  1336. BAYER(row,col) = curve[BAYER(row,col)];
  1337. } else if (tag == 0x400) { /* Sensor defects */
  1338. while ((len -= 8) >= 0) {
  1339. col = dcr_get2(p) - p->left_margin;
  1340. row = dcr_get2(p) - p->top_margin;
  1341. type = dcr_get2(p); dcr_get2(p);
  1342. if (col >= p->width) continue;
  1343. if (type == 131) /* Bad column */
  1344. for (row=0; row < p->height; row++)
  1345. if (FC(row,col) == 1) {
  1346. for (sum=i=0; i < 4; i++)
  1347. sum += val[i] = dcr_bayer (p, row+dir[i][0], col+dir[i][1]);
  1348. for (max=i=0; i < 4; i++) {
  1349. dev[i] = abs((val[i] << 2) - sum);
  1350. if (dev[max] < dev[i]) max = i;
  1351. }
  1352. BAYER(row,col) = (unsigned short)((sum - val[max])/3.0 + 0.5);
  1353. } else {
  1354. for (sum=0, i=8; i < 12; i++)
  1355. sum += dcr_bayer (p, row+dir[i][0], col+dir[i][1]);
  1356. BAYER(row,col) = (unsigned short)(0.5 + sum * 0.0732233 +
  1357. (dcr_bayer(p, row,col-2) + dcr_bayer(p, row,col+2)) * 0.3535534);
  1358. }
  1359. else if (type == 129) { /* Bad pixel */
  1360. if (row >= p->height) continue;
  1361. j = (FC(row,col) != 1) * 4;
  1362. for (sum=0, i=j; i < j+8; i++)
  1363. sum += dcr_bayer (p, row+dir[i][0], col+dir[i][1]);
  1364. BAYER(row,col) = (sum + 4) >> 3;
  1365. }
  1366. }
  1367. } else if (tag == 0x401) { /* All-color flat fields */
  1368. dcr_phase_one_flat_field (p, 1, 2);
  1369. } else if (tag == 0x416 || tag == 0x410) {
  1370. dcr_phase_one_flat_field (p, 0, 2);
  1371. } else if (tag == 0x40b) { /* Red+blue flat field */
  1372. dcr_phase_one_flat_field (p, 0, 4);
  1373. } else if (tag == 0x412) {
  1374. dcr_fseek(p->obj_, 36, SEEK_CUR);
  1375. diff = abs (dcr_get2(p) - p->ph1.tag_21a);
  1376. if (mindiff > diff) {
  1377. mindiff = diff;
  1378. off_412 = dcr_ftell(p->obj_) - 38;
  1379. }
  1380. }
  1381. dcr_fseek(p->obj_, save, SEEK_SET);
  1382. }
  1383. if (off_412) {
  1384. dcr_fseek(p->obj_, off_412, SEEK_SET);
  1385. for (i=0; i < 9; i++) head[i] = dcr_get4(p) & 0x7fff;
  1386. yval[0] = (float *) calloc (head[1]*head[3] + head[2]*head[4], 6);
  1387. dcr_merror (p, yval[0], "phase_one_correct()");
  1388. yval[1] = (float *) (yval[0] + head[1]*head[3]);
  1389. xval[0] = (ushort *) (yval[1] + head[2]*head[4]);
  1390. xval[1] = (ushort *) (xval[0] + head[1]*head[3]);
  1391. dcr_get2(p);
  1392. for (i=0; i < 2; i++)
  1393. for (j=0; j < head[i+1]*head[i+3]; j++)
  1394. yval[i][j] = (float)dcr_getreal(p, 11);
  1395. for (i=0; i < 2; i++)
  1396. for (j=0; j < head[i+1]*head[i+3]; j++)
  1397. xval[i][j] = dcr_get2(p);
  1398. for (row=0; row < p->height; row++)
  1399. for (col=0; col < p->width; col++) {
  1400. cfrac = (float) col * head[3] / p->raw_width;
  1401. cfrac -= cip = (int)cfrac;
  1402. num = (float)(BAYER(row,col) * 0.5);
  1403. for (i=cip; i < cip+2; i++) {
  1404. for (k=j=0; j < head[1]; j++)
  1405. if (num < xval[0][k = head[1]*i+j]) break;
  1406. frac = (j == 0 || j == head[1]) ? 0 :
  1407. (xval[0][k] - num) / (xval[0][k] - xval[0][k-1]);
  1408. mult[i-cip] = yval[0][k-1] * frac + yval[0][k] * (1-frac);
  1409. }
  1410. i = (int)(((mult[0] * (1-cfrac) + mult[1] * cfrac)
  1411. * (row + p->top_margin) + num) * 2);
  1412. BAYER(row,col) = LIM(i,0,65535);
  1413. }
  1414. free (yval[0]);
  1415. }
  1416. }
  1417. void DCR_CLASS dcr_phase_one_load_raw(DCRAW* p)
  1418. {
  1419. int row, col, a, b;
  1420. ushort *pixel, akey, bkey, mask;
  1421. dcr_fseek(p->obj_, p->ph1.key_off, SEEK_SET);
  1422. akey = dcr_get2(p);
  1423. bkey = dcr_get2(p);
  1424. mask = p->ph1.format == 1 ? 0x5555:0x1354;
  1425. dcr_fseek(p->obj_, p->data_offset + p->top_margin*p->raw_width*2, SEEK_SET);
  1426. pixel = (ushort *) calloc (p->raw_width, sizeof *pixel);
  1427. dcr_merror (p, pixel, "phase_one_load_raw()");
  1428. for (row=0; row < p->height; row++) {
  1429. dcr_read_shorts (p, pixel, p->raw_width);
  1430. for (col=0; col < p->raw_width; col+=2) {
  1431. a = pixel[col+0] ^ akey;
  1432. b = pixel[col+1] ^ bkey;
  1433. pixel[col+0] = (a & mask) | (b & ~mask);
  1434. pixel[col+1] = (b & mask) | (a & ~mask);
  1435. }
  1436. for (col=0; col < p->width; col++)
  1437. BAYER(row,col) = pixel[col+p->left_margin];
  1438. }
  1439. free (pixel);
  1440. dcr_phase_one_correct(p);
  1441. }
  1442. unsigned DCR_CLASS dcr_ph1_bits (DCRAW* p,int nbits)
  1443. {
  1444. static UINT64 bitbuf=0;
  1445. static int vbits=0;
  1446. if (nbits == -1)
  1447. return (unsigned int)(bitbuf = vbits = 0);
  1448. if (nbits == 0) return 0;
  1449. if ((vbits -= nbits) < 0) {
  1450. bitbuf = bitbuf << 32 | dcr_get4(p);
  1451. vbits += 32;
  1452. }
  1453. return (unsigned int)(bitbuf << (64-nbits-vbits) >> (64-nbits));
  1454. }
  1455. void DCR_CLASS dcr_phase_one_load_raw_c(DCRAW* p)
  1456. {
  1457. static const int length[] = { 8,7,6,9,11,10,5,12,14,13 };
  1458. int *offset, len[2], pred[2], row, col, i, j;
  1459. ushort *pixel;
  1460. short (*black)[2];
  1461. pixel = (ushort *) calloc (p->raw_width + p->raw_height*4, 2);
  1462. dcr_merror (p, pixel, "phase_one_load_raw_c()");
  1463. offset = (int *) (pixel + p->raw_width);
  1464. dcr_fseek(p->obj_, p->strip_offset, SEEK_SET);
  1465. for (row=0; row < p->raw_height; row++)
  1466. offset[row] = dcr_get4(p);
  1467. black = (short (*)[2]) offset + p->raw_height;
  1468. dcr_fseek(p->obj_, p->ph1.black_off, SEEK_SET);
  1469. if (p->ph1.black_off)
  1470. dcr_read_shorts (p, (ushort *) black[0], p->raw_height*2);
  1471. for (i=0; i < 256; i++)
  1472. p->curve[i] = (unsigned short)(i*i / 3.969 + 0.5);
  1473. for (row=0; row < p->raw_height; row++) {
  1474. dcr_fseek(p->obj_, p->data_offset + offset[row], SEEK_SET);
  1475. dcr_ph1_bits(p,-1);
  1476. pred[0] = pred[1] = 0;
  1477. for (col=0; col < p->raw_width; col++) {
  1478. if (col >= (p->raw_width & -8))
  1479. len[0] = len[1] = 14;
  1480. else if ((col & 7) == 0)
  1481. for (i=0; i < 2; i++) {
  1482. for (j=0; j < 5 && !dcr_ph1_bits(p,1); j++);
  1483. if (j--) len[i] = length[j*2 + dcr_ph1_bits(p,1)];
  1484. }
  1485. if ((i = len[col & 1]) == 14)
  1486. pixel[col] = pred[col & 1] = dcr_ph1_bits(p,16);
  1487. else
  1488. pixel[col] = pred[col & 1] += dcr_ph1_bits(p,i) + 1 - (1 << (i - 1));
  1489. if (pred[col & 1] >> 16) dcr_derror(p);
  1490. if (p->ph1.format == 5 && pixel[col] < 256)
  1491. pixel[col] = p->curve[pixel[col]];
  1492. }
  1493. if ((unsigned) (row-p->top_margin) < p->height)
  1494. for (col=0; col < p->width; col++) {
  1495. i = (pixel[col+p->left_margin] << 2)
  1496. - p->ph1.black + black[row][col >= p->ph1.split_col];
  1497. if (i > 0) BAYER(row-p->top_margin,col) = i;
  1498. }
  1499. }
  1500. free (pixel);
  1501. dcr_phase_one_correct(p);
  1502. p->maximum = 0xfffc - p->ph1.black;
  1503. }
  1504. void DCR_CLASS dcr_hasselblad_load_raw(DCRAW* p)
  1505. {
  1506. struct dcr_jhead jh;
  1507. struct dcr_decode *dindex;
  1508. int row, col, pred[2], len[2], diff, i;
  1509. if (!dcr_ljpeg_start (p,&jh, 0)) return;
  1510. free (jh.row);
  1511. p->order = 0x4949;
  1512. dcr_ph1_bits(p,-1);
  1513. for (row=-p->top_margin; row < p->height; row++) {
  1514. pred[0] = pred[1] = 0x8000;
  1515. for (col=-p->left_margin; col < p->raw_width-p->left_margin; col+=2) {
  1516. for (i=0; i < 2; i++) {
  1517. for (dindex=jh.huff[0]; dindex->branch[0]; )
  1518. dindex = dindex->branch[dcr_ph1_bits(p,1)];
  1519. len[i] = dindex->leaf;
  1520. }
  1521. for (i=0; i < 2; i++) {
  1522. diff = dcr_ph1_bits(p,len[i]);
  1523. if ((diff & (1 << (len[i]-1))) == 0)
  1524. diff -= (1 << len[i]) - 1;
  1525. if (diff == 65535) diff = -32768;
  1526. pred[i] += diff;
  1527. if (row >= 0 && (unsigned)(col+i) < p->width)
  1528. BAYER(row,col+i) = pred[i];
  1529. }
  1530. }
  1531. }
  1532. p->maximum = 0xffff;
  1533. }
  1534. void DCR_CLASS dcr_leaf_hdr_load_raw(DCRAW* p)
  1535. {
  1536. ushort *pixel;
  1537. unsigned tile=0, r, c, row, col;
  1538. pixel = (ushort *) calloc (p->raw_width, sizeof *pixel);
  1539. dcr_merror (p, pixel, "leaf_hdr_load_raw()");
  1540. FORC(p->tiff_samples) {
  1541. for (r=0; r < p->raw_height; r++) {
  1542. if (r % p->tile_length == 0) {
  1543. dcr_fseek(p->obj_, p->data_offset + 4*tile++, SEEK_SET);
  1544. dcr_fseek(p->obj_, dcr_get4(p) + 2*p->left_margin, SEEK_SET);
  1545. }
  1546. if (p->filters && c != p->opt.shot_select) continue;
  1547. dcr_read_shorts (p, pixel, p->raw_width);
  1548. if ((row = r - p->top_margin) >= p->height) continue;
  1549. for (col=0; col < p->width; col++)
  1550. if (p->filters) BAYER(row,col) = pixel[col];
  1551. else p->image[row*p->width+col][c] = pixel[col];
  1552. }
  1553. }
  1554. free (pixel);
  1555. if (!p->filters) {
  1556. p->maximum = 0xffff;
  1557. p->raw_color = 1;
  1558. }
  1559. }
  1560. void DCR_CLASS dcr_sinar_4shot_load_raw(DCRAW* p)
  1561. {
  1562. ushort *pixel;
  1563. unsigned shot, row, col, r, c;
  1564. if ((shot = p->opt.shot_select) || p->opt.half_size) {
  1565. if (shot) shot--;
  1566. if (shot > 3) shot = 3;
  1567. dcr_fseek(p->obj_, p->data_offset + shot*4, SEEK_SET);
  1568. dcr_fseek(p->obj_, dcr_get4(p), SEEK_SET);
  1569. dcr_unpacked_load_raw(p);
  1570. return;
  1571. }
  1572. free (p->image);
  1573. p->image = (ushort (*)[4])
  1574. calloc ((p->iheight=p->height)*(p->iwidth=p->width), sizeof *p->image);
  1575. dcr_merror (p, p->image, "sinar_4shot_load_raw()");
  1576. pixel = (ushort *) calloc (p->raw_width, sizeof *pixel);
  1577. dcr_merror (p, pixel, "sinar_4shot_load_raw()");
  1578. for (shot=0; shot < 4; shot++) {
  1579. dcr_fseek(p->obj_, p->data_offset + shot*4, SEEK_SET);
  1580. dcr_fseek(p->obj_, dcr_get4(p), SEEK_SET);
  1581. for (row=0; row < p->raw_height; row++) {
  1582. dcr_read_shorts (p, pixel, p->raw_width);
  1583. if ((r = row-p->top_margin - (shot >> 1 & 1)) >= p->height) continue;
  1584. for (col=0; col < p->raw_width; col++) {
  1585. if ((c = col-p->left_margin - (shot & 1)) >= p->width) continue;
  1586. p->image[r*p->width+c][FC(row,col)] = pixel[col];
  1587. }
  1588. }
  1589. }
  1590. free (pixel);
  1591. p->shrink = p->filters = 0;
  1592. }
  1593. void DCR_CLASS dcr_imacon_full_load_raw(DCRAW* p)
  1594. {
  1595. int row, col;
  1596. for (row=0; row < p->height; row++)
  1597. for (col=0; col < p->width; col++)
  1598. dcr_read_shorts (p, p->image[row*p->width+col], 3);
  1599. }
  1600. void DCR_CLASS dcr_packed_12_load_raw(DCRAW* p)
  1601. {
  1602. int vbits=0, rbits=0, irow, row, col;
  1603. UINT64 bitbuf=0;
  1604. if (p->raw_width * 2 >= p->width * 3) { /* If raw_width is in bytes, */
  1605. rbits = p->raw_width * 8;
  1606. p->raw_width = p->raw_width * 2 / 3; /* convert it to pixels and */
  1607. rbits -= p->raw_width * 12; /* save the remainder. */
  1608. }
  1609. p->order = p->load_flags & 1 ? 0x4949 : 0x4d4d;
  1610. for (irow=0; irow < p->height; irow++) {
  1611. row = irow;
  1612. if (p->load_flags & 2 &&
  1613. (row = irow * 2 % p->height + irow / (p->height/2)) == 1 &&
  1614. p->load_flags & 4) {
  1615. if (vbits=0, p->tiff_compress)
  1616. dcr_fseek(p->obj_, p->data_offset - (-p->width*p->height*3/4 & -2048), SEEK_SET);
  1617. else {
  1618. dcr_fseek(p->obj_, 0, SEEK_END);
  1619. dcr_fseek(p->obj_, dcr_ftell(p->obj_)/2, SEEK_SET);
  1620. }
  1621. }
  1622. for (col=0; col < p->raw_width; col++) {
  1623. if ((vbits -= 12) < 0) {
  1624. bitbuf = bitbuf << 32 | dcr_get4(p);
  1625. vbits += 32;
  1626. }
  1627. if ((unsigned) (col-p->left_margin) < p->width)
  1628. BAYER(row,col-p->left_margin) = (unsigned short)(bitbuf << (52-vbits) >> 52);
  1629. if (p->load_flags & 8 && (col % 10) == 9)
  1630. if (vbits=0, bitbuf & 255) dcr_derror(p);
  1631. }
  1632. vbits -= rbits;
  1633. }
  1634. if (!strcmp(p->make,"OLYMPUS")) p->black >>= 4;
  1635. }
  1636. void DCR_CLASS dcr_unpacked_load_raw(DCRAW* p)
  1637. {
  1638. ushort *pixel;
  1639. int row, col, bits=0;
  1640. while (1 << ++bits < (int)p->maximum);
  1641. dcr_fseek(p->obj_, (p->top_margin*p->raw_width + p->left_margin) * 2, SEEK_CUR);
  1642. pixel = (ushort *) calloc (p->width, sizeof *pixel);
  1643. dcr_merror (p, pixel, "unpacked_load_raw()");
  1644. for (row=0; row < p->height; row++) {
  1645. dcr_read_shorts (p, pixel, p->width);
  1646. dcr_fseek(p->obj_, 2*(p->raw_width - p->width), SEEK_CUR);
  1647. for (col=0; col < p->width; col++)
  1648. if ((BAYER2(row,col) = pixel[col]) >> bits) dcr_derror(p);
  1649. }
  1650. free (pixel);
  1651. }
  1652. void DCR_CLASS nokia_load_raw(DCRAW* p)
  1653. {
  1654. uchar *data, *dp;
  1655. ushort *pixel, *pix;
  1656. int dwide, row, c;
  1657. dwide = p->raw_width * 5 / 4;
  1658. data = (uchar *) malloc (dwide + p->raw_width*2);
  1659. dcr_merror (p,data, "nokia_load_raw()");
  1660. pixel = (ushort *) (data + dwide);
  1661. for (row=0; row < p->raw_height; row++) {
  1662. if (dcr_fread(p->obj_, data, 1, dwide) < dwide) dcr_derror(p);
  1663. for (dp=data, pix=pixel; pix < pixel+p->raw_width; dp+=5, pix+=4)
  1664. FORC4 pix[c] = (dp[c] << 2) | (dp[4] >> (c << 1) & 3);
  1665. if (row < p->top_margin)
  1666. FORC(p->width) p->black += pixel[c];
  1667. else
  1668. FORC(p->width) BAYER(row-p->top_margin,c) = pixel[c];
  1669. }
  1670. free (data);
  1671. if (p->top_margin) p->black /= p->top_margin * p->width;
  1672. p->maximum = 0x3ff;
  1673. }
  1674. unsigned DCR_CLASS dcr_pana_bits (DCRAW* p,int nbits)
  1675. {
  1676. static uchar buf[0x4000];
  1677. static int vbits;
  1678. int byte;
  1679. if (!nbits) return vbits=0;
  1680. if (!vbits) {
  1681. dcr_fread(p->obj_, buf+p->load_flags, 1, 0x4000-p->load_flags);
  1682. dcr_fread(p->obj_, buf, 1, p->load_flags);
  1683. }
  1684. vbits = (vbits - nbits) & 0x1ffff;
  1685. byte = vbits >> 3 ^ 0x3ff0;
  1686. return (buf[byte] | buf[byte+1] << 8) >> (vbits & 7) & ~(-1 << nbits);
  1687. }
  1688. void DCR_CLASS dcr_panasonic_load_raw(DCRAW* p)
  1689. {
  1690. int row, col, i, j, sh=0, pred[2], nonz[2];
  1691. dcr_pana_bits(p,0);
  1692. for (row=0; row < p->height; row++)
  1693. for (col=0; col < p->raw_width; col++) {
  1694. if ((i = col % 14) == 0)
  1695. pred[0] = pred[1] = nonz[0] = nonz[1] = 0;
  1696. if (i % 3 == 2) sh = 4 >> (3 - dcr_pana_bits(p,2));
  1697. if (nonz[i & 1]) {
  1698. if ((j = dcr_pana_bits(p,8))) {
  1699. if ((pred[i & 1] -= 0x80 << sh) < 0 || sh == 4)
  1700. pred[i & 1] &= ~(-1 << sh);
  1701. pred[i & 1] += j << sh;
  1702. }
  1703. } else if ((nonz[i & 1] = dcr_pana_bits(p,8)) || i > 11)
  1704. pred[i & 1] = nonz[i & 1] << 4 | dcr_pana_bits(p,4);
  1705. if (col < p->width)
  1706. if ((BAYER(row,col) = pred[col & 1]) > 4098) dcr_derror(p);
  1707. }
  1708. }
  1709. void DCR_CLASS dcr_olympus_e300_load_raw(DCRAW* p)
  1710. {
  1711. uchar *data, *dp;
  1712. ushort *pixel, *pix;
  1713. int dwide, row, col;
  1714. dwide = p->raw_width * 16 / 10;
  1715. dcr_fseek(p->obj_, dwide*p->top_margin, SEEK_CUR);
  1716. data = (uchar *) malloc (dwide + p->raw_width*2);
  1717. dcr_merror (p, data, "olympus_e300_load_raw()");
  1718. pixel = (ushort *) (data + dwide);
  1719. for (row=0; row < p->height; row++) {
  1720. if ((int)dcr_fread(p->obj_, data, 1, dwide) < dwide) dcr_derror(p);
  1721. for (dp=data, pix=pixel; pix < pixel+p->raw_width; dp+=3, pix+=2) {
  1722. if (((dp-data) & 15) == 15)
  1723. if (*dp++ && pix < pixel+p->width+p->left_margin) dcr_derror(p);
  1724. pix[0] = dp[1] << 8 | dp[0];
  1725. pix[1] = dp[2] << 4 | dp[1] >> 4;
  1726. }
  1727. for (col=0; col < p->width; col++)
  1728. BAYER(row,col) = (pixel[col+p->left_margin] & 0xfff);
  1729. }
  1730. free (data);
  1731. p->maximum >>= 4;
  1732. p->black >>= 4;
  1733. }
  1734. void DCR_CLASS dcr_olympus_e410_load_raw(DCRAW* p)
  1735. {
  1736. int row, col, nbits, sign, low, high, i, w, n, nw;
  1737. int acarry[2][3], *carry, pred, diff;
  1738. dcr_fseek(p->obj_, 7, SEEK_CUR);
  1739. dcr_getbits(p, -1);
  1740. for (row=0; row < p->height; row++) {
  1741. memset (acarry, 0, sizeof acarry);
  1742. for (col=0; col < p->width; col++) {
  1743. carry = acarry[col & 1];
  1744. i = 2 * (carry[2] < 3);
  1745. for (nbits=2+i; (ushort) carry[0] >> (nbits+i); nbits++);
  1746. sign = dcr_getbits(p, 1) * -1;
  1747. low = dcr_getbits(p, 2);
  1748. for (high=0; high < 12; high++)
  1749. if (dcr_getbits(p, 1)) break;
  1750. if (high == 12)
  1751. high = dcr_getbits(p, 16-nbits) >> 1;
  1752. carry[0] = (high << nbits) | dcr_getbits(p, nbits);
  1753. diff = (carry[0] ^ sign) + carry[1];
  1754. carry[1] = (diff*3 + carry[1]) >> 5;
  1755. carry[2] = carry[0] > 16 ? 0 : carry[2]+1;
  1756. if (row < 2 && col < 2) pred = 0;
  1757. else if (row < 2) pred = BAYER(row,col-2);
  1758. else if (col < 2) pred = BAYER(row-2,col);
  1759. else {
  1760. w = BAYER(row,col-2);
  1761. n = BAYER(row-2,col);
  1762. nw = BAYER(row-2,col-2);
  1763. if ((w < nw && nw < n) || (n < nw && nw < w)) {
  1764. if (ABS(w-nw) > 32 || ABS(n-nw) > 32)
  1765. pred = w + n - nw;
  1766. else pred = (w + n) >> 1;
  1767. } else pred = ABS(w-nw) > ABS(n-nw) ? w : n;
  1768. }
  1769. if ((BAYER(row,col) = pred + ((diff << 2) | low)) >> 12) dcr_derror(p);
  1770. }
  1771. }
  1772. }
  1773. void DCR_CLASS dcr_minolta_rd175_load_raw(DCRAW* p)
  1774. {
  1775. uchar pixel[768];
  1776. unsigned irow, box, row, col;
  1777. for (irow=0; irow < 1481; irow++) {
  1778. if (dcr_fread(p->obj_, pixel, 1, 768) < 768) dcr_derror(p);
  1779. box = irow / 82;
  1780. row = irow % 82 * 12 + ((box < 12) ? box | 1 : (box-12)*2);
  1781. switch (irow) {
  1782. case 1477: case 1479: continue;
  1783. case 1476: row = 984; break;
  1784. case 1480: row = 985; break;
  1785. case 1478: row = 985; box = 1;
  1786. }
  1787. if ((box < 12) && (box & 1)) {
  1788. for (col=0; col < 1533; col++, row ^= 1)
  1789. if (col != 1) BAYER(row,col) = (col+1) & 2 ?
  1790. pixel[col/2-1] + pixel[col/2+1] : pixel[col/2] << 1;
  1791. BAYER(row,1) = pixel[1] << 1;
  1792. BAYER(row,1533) = pixel[765] << 1;
  1793. } else
  1794. for (col=row & 1; col < 1534; col+=2)
  1795. BAYER(row,col) = pixel[col/2] << 1;
  1796. }
  1797. p->maximum = 0xff << 1;
  1798. }
  1799. void DCR_CLASS dcr_casio_qv5700_load_raw(DCRAW* p)
  1800. {
  1801. uchar data[3232], *dp;
  1802. ushort pixel[2576], *pix;
  1803. int row, col;
  1804. for (row=0; row < p->height; row++) {
  1805. dcr_fread(p->obj_, data, 1, 3232);
  1806. for (dp=data, pix=pixel; dp < data+3220; dp+=5, pix+=4) {
  1807. pix[0] = (dp[0] << 2) + (dp[1] >> 6);
  1808. pix[1] = (dp[1] << 4) + (dp[2] >> 4);
  1809. pix[2] = (dp[2] << 6) + (dp[3] >> 2);
  1810. pix[3] = (dp[3] << 8) + (dp[4] );
  1811. }
  1812. for (col=0; col < p->width; col++)
  1813. BAYER(row,col) = (pixel[col] & 0x3ff);
  1814. }
  1815. p->maximum = 0x3fc;
  1816. }
  1817. void DCR_CLASS dcr_quicktake_100_load_raw(DCRAW* p)
  1818. {
  1819. uchar pixel[484][644];
  1820. static const short gstep[16] =
  1821. { -89,-60,-44,-32,-22,-15,-8,-2,2,8,15,22,32,44,60,89 };
  1822. static const short rstep[6][4] =
  1823. { { -3,-1,1,3 }, { -5,-1,1,5 }, { -8,-2,2,8 },
  1824. { -13,-3,3,13 }, { -19,-4,4,19 }, { -28,-6,6,28 } };
  1825. static const short curve[256] =
  1826. { 0,1,2,3,4,5,6,7,8,9,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,
  1827. 28,29,30,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,53,
  1828. 54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,74,75,76,77,78,
  1829. 79,80,81,82,83,84,86,88,90,92,94,97,99,101,103,105,107,110,112,114,116,
  1830. 118,120,123,125,127,129,131,134,136,138,140,142,144,147,149,151,153,155,
  1831. 158,160,162,164,166,168,171,173,175,177,179,181,184,186,188,190,192,195,
  1832. 197,199,201,203,205,208,210,212,214,216,218,221,223,226,230,235,239,244,
  1833. 248,252,257,261,265,270,274,278,283,287,291,296,300,305,309,313,318,322,
  1834. 326,331,335,339,344,348,352,357,361,365,370,374,379,383,387,392,396,400,
  1835. 405,409,413,418,422,426,431,435,440,444,448,453,457,461,466,470,474,479,
  1836. 483,487,492,496,500,508,519,531,542,553,564,575,587,598,609,620,631,643,
  1837. 654,665,676,687,698,710,721,732,743,754,766,777,788,799,810,822,833,844,
  1838. 855,866,878,889,900,911,922,933,945,956,967,978,989,1001,1012,1023 };
  1839. int rb, row, col, sharp, val=0;
  1840. dcr_getbits(p, -1);
  1841. memset (pixel, 0x80, sizeof pixel);
  1842. for (row=2; row < p->height+2; row++) {
  1843. for (col=2+(row & 1); col < p->width+2; col+=2) {
  1844. val = ((pixel[row-1][col-1] + 2*pixel[row-1][col+1] +
  1845. pixel[row][col-2]) >> 2) + gstep[dcr_getbits(p, 4)];
  1846. pixel[row][col] = val = LIM(val,0,255);
  1847. if (col < 4)
  1848. pixel[row][col-2] = pixel[row+1][~row & 1] = val;
  1849. if (row == 2)
  1850. pixel[row-1][col+1] = pixel[row-1][col+3] = val;
  1851. }
  1852. pixel[row][col] = val;
  1853. }
  1854. for (rb=0; rb < 2; rb++)
  1855. for (row=2+rb; row < p->height+2; row+=2)
  1856. for (col=3-(row & 1); col < p->width+2; col+=2) {
  1857. if (row < 4 || col < 4) sharp = 2;
  1858. else {
  1859. val = ABS(pixel[row-2][col] - pixel[row][col-2])
  1860. + ABS(pixel[row-2][col] - pixel[row-2][col-2])
  1861. + ABS(pixel[row][col-2] - pixel[row-2][col-2]);
  1862. sharp = val < 4 ? 0 : val < 8 ? 1 : val < 16 ? 2 :
  1863. val < 32 ? 3 : val < 48 ? 4 : 5;
  1864. }
  1865. val = ((pixel[row-2][col] + pixel[row][col-2]) >> 1)
  1866. + rstep[sharp][dcr_getbits(p, 2)];
  1867. pixel[row][col] = val = LIM(val,0,255);
  1868. if (row < 4) pixel[row-2][col+2] = val;
  1869. if (col < 4) pixel[row+2][col-2] = val;
  1870. }
  1871. for (row=2; row < p->height+2; row++)
  1872. for (col=3-(row & 1); col < p->width+2; col+=2) {
  1873. val = ((pixel[row][col-1] + (pixel[row][col] << 2) +
  1874. pixel[row][col+1]) >> 1) - 0x100;
  1875. pixel[row][col] = LIM(val,0,255);
  1876. }
  1877. for (row=0; row < p->height; row++)
  1878. for (col=0; col < p->width; col++)
  1879. BAYER(row,col) = curve[pixel[row+2][col+2]];
  1880. p->maximum = 0x3ff;
  1881. }
  1882. const int * DCR_CLASS dcr_make_decoder_int (DCRAW* p, const int *source, int level)
  1883. {
  1884. struct dcr_decode *cur;
  1885. cur = p->free_decode++;
  1886. if (level < source[0]) {
  1887. cur->branch[0] = p->free_decode;
  1888. source = dcr_make_decoder_int (p, source, level+1);
  1889. cur->branch[1] = p->free_decode;
  1890. source = dcr_make_decoder_int (p, source, level+1);
  1891. } else {
  1892. cur->leaf = source[1];
  1893. source += 2;
  1894. }
  1895. return source;
  1896. }
  1897. int DCR_CLASS dcr_radc_token (DCRAW* p, int tree)
  1898. {
  1899. int t;
  1900. static struct dcr_decode *dstart[18], *dindex;
  1901. static const int *s, source[] = {
  1902. 1,1, 2,3, 3,4, 4,2, 5,7, 6,5, 7,6, 7,8,
  1903. 1,0, 2,1, 3,3, 4,4, 5,2, 6,7, 7,6, 8,5, 8,8,
  1904. 2,1, 2,3, 3,0, 3,2, 3,4, 4,6, 5,5, 6,7, 6,8,
  1905. 2,0, 2,1, 2,3, 3,2, 4,4, 5,6, 6,7, 7,5, 7,8,
  1906. 2,1, 2,4, 3,0, 3,2, 3,3, 4,7, 5,5, 6,6, 6,8,
  1907. 2,3, 3,1, 3,2, 3,4, 3,5, 3,6, 4,7, 5,0, 5,8,
  1908. 2,3, 2,6, 3,0, 3,1, 4,4, 4,5, 4,7, 5,2, 5,8,
  1909. 2,4, 2,7, 3,3, 3,6, 4,1, 4,2, 4,5, 5,0, 5,8,
  1910. 2,6, 3,1, 3,3, 3,5, 3,7, 3,8, 4,0, 5,2, 5,4,
  1911. 2,0, 2,1, 3,2, 3,3, 4,4, 4,5, 5,6, 5,7, 4,8,
  1912. 1,0, 2,2, 2,-2,
  1913. 1,-3, 1,3,
  1914. 2,-17, 2,-5, 2,5, 2,17,
  1915. 2,-7, 2,2, 2,9, 2,18,
  1916. 2,-18, 2,-9, 2,-2, 2,7,
  1917. 2,-28, 2,28, 3,-49, 3,-9, 3,9, 4,49, 5,-79, 5,79,
  1918. 2,-1, 2,13, 2,26, 3,39, 4,-16, 5,55, 6,-37, 6,76,
  1919. 2,-26, 2,-13, 2,1, 3,-39, 4,16, 5,-55, 6,-76, 6,37
  1920. };
  1921. if (p->free_decode == p->first_decode)
  1922. for (s=source, t=0; t < 18; t++) {
  1923. dstart[t] = p->free_decode;
  1924. s = dcr_make_decoder_int (p, s, 0);
  1925. }
  1926. if (tree == 18) {
  1927. if (p->kodak_cbpp == 243)
  1928. return (dcr_getbits(p, 6) << 2) + 2; /* most DC50 photos */
  1929. else
  1930. return (dcr_getbits(p, 5) << 3) + 4; /* DC40, Fotoman Pixtura */
  1931. }
  1932. for (dindex = dstart[tree]; dindex->branch[0]; )
  1933. dindex = dindex->branch[dcr_getbits(p, 1)];
  1934. return dindex->leaf;
  1935. }
  1936. #define FORYX for (y=1; y < 3; y++) for (x=col+1; x >= col; x--)
  1937. #define PREDICTOR (c ? (buf[c][y-1][x] + buf[c][y][x+1]) / 2 \
  1938. : (buf[c][y-1][x+1] + 2*buf[c][y-1][x] + buf[c][y][x+1]) / 4)
  1939. void DCR_CLASS dcr_kodak_radc_load_raw(DCRAW* p)
  1940. {
  1941. int row, col, tree, nreps, rep, step, i, c, s, r, x, y, val;
  1942. short last[3] = { 16,16,16 }, mul[3], buf[3][3][386];
  1943. dcr_init_decoder(p);
  1944. dcr_getbits(p, -1);
  1945. for (i=0; i < sizeof(buf)/sizeof(short); i++)
  1946. buf[0][0][i] = 2048;
  1947. for (row=0; row < p->height; row+=4) {
  1948. FORC3 mul[c] = dcr_getbits(p, 6);
  1949. FORC3 {
  1950. val = ((0x1000000/last[c] + 0x7ff) >> 12) * mul[c];
  1951. s = val > 65564 ? 10:12;
  1952. x = ~(-1 << (s-1));
  1953. val <<= 12-s;
  1954. for (i=0; i < sizeof(buf[0])/sizeof(short); i++)
  1955. buf[c][0][i] = (buf[c][0][i] * val + x) >> s;
  1956. last[c] = mul[c];
  1957. for (r=0; r <= !c; r++) {
  1958. buf[c][1][p->width/2] = buf[c][2][p->width/2] = mul[c] << 7;
  1959. for (tree=1, col=p->width/2; col > 0; ) {
  1960. if ((tree = dcr_radc_token(p,tree))) {
  1961. col -= 2;
  1962. if (tree == 8)
  1963. FORYX buf[c][y][x] = dcr_radc_token(p,tree+10) * mul[c];
  1964. else
  1965. FORYX buf[c][y][x] = dcr_radc_token(p,tree+10) * 16 + PREDICTOR;
  1966. } else
  1967. do {
  1968. nreps = (col > 2) ? dcr_radc_token(p,9) + 1 : 1;
  1969. for (rep=0; rep < 8 && rep < nreps && col > 0; rep++) {
  1970. col -= 2;
  1971. FORYX buf[c][y][x] = PREDICTOR;
  1972. if (rep & 1) {
  1973. step = dcr_radc_token(p,10) << 4;
  1974. FORYX buf[c][y][x] += step;
  1975. }
  1976. }
  1977. } while (nreps == 9);
  1978. }
  1979. for (y=0; y < 2; y++)
  1980. for (x=0; x < p->width/2; x++) {
  1981. val = (buf[c][y+1][x] << 4) / mul[c];
  1982. if (val < 0) val = 0;
  1983. if (c) BAYER(row+y*2+c-1,x*2+2-c) = val;
  1984. else BAYER(row+r*2+y,x*2+y) = val;
  1985. }
  1986. memcpy (buf[c][0]+!c, buf[c][2], sizeof buf[c][0]-2*!c);
  1987. }
  1988. }
  1989. for (y=row; y < row+4; y++)
  1990. for (x=0; x < p->width; x++)
  1991. if ((x+y) & 1) {
  1992. r = x ? x-1 : x+1;
  1993. s = x+1 < p->width ? x+1 : x-1;
  1994. val = (BAYER(y,x)-2048)*2 + (BAYER(y,r)+BAYER(y,s))/2;
  1995. if (val < 0) val = 0;
  1996. BAYER(y,x) = val;
  1997. }
  1998. }
  1999. p->maximum = 0xfff;
  2000. p->use_gamma = 0;
  2001. }
  2002. #undef FORYX
  2003. #undef PREDICTOR
  2004. #ifdef NO_JPEG
  2005. void DCR_CLASS dcr_kodak_jpeg_load_raw(DCRAW* p) {}
  2006. #else
  2007. METHODDEF(boolean)
  2008. fill_input_buffer (j_decompress_ptr cinfo)
  2009. {
  2010. static uchar jpeg_buffer[4096];
  2011. size_t nbytes;
  2012. //nbytes = dcr_fread(p->obj_, jpeg_buffer, 1, 4096);
  2013. nbytes = fread (jpeg_buffer, 1, 4096, ifp);
  2014. _swab (jpeg_buffer, jpeg_buffer, nbytes);
  2015. cinfo->src->next_input_byte = jpeg_buffer;
  2016. cinfo->src->bytes_in_buffer = nbytes;
  2017. return TRUE;
  2018. }
  2019. void DCR_CLASS dcr_kodak_jpeg_load_raw(DCRAW* p)
  2020. {
  2021. struct jpeg_decompress_struct cinfo;
  2022. struct jpeg_error_mgr jerr;
  2023. JSAMPARRAY buf;
  2024. JSAMPLE (*pixel)[3];
  2025. int row, col;
  2026. cinfo.err = jpeg_std_error (&jerr);
  2027. jpeg_create_decompress (&cinfo);
  2028. jpeg_stdio_src (&cinfo);
  2029. cinfo.src->fill_input_buffer = fill_input_buffer;
  2030. jpeg_read_header (&cinfo, TRUE);
  2031. jpeg_start_decompress (&cinfo);
  2032. if ((cinfo.output_width != p->width ) ||
  2033. (cinfo.output_height*2 != p->height ) ||
  2034. (cinfo.output_components != 3 )) {
  2035. fprintf (stderr,_("%s: incorrect JPEG dimensions\n"), p->ifname);
  2036. jpeg_destroy_decompress (&cinfo);
  2037. longjmp (p->failure, 3);
  2038. }
  2039. buf = (*cinfo.mem->alloc_sarray)
  2040. ((j_common_ptr) &cinfo, JPOOL_IMAGE, p->width*3, 1);
  2041. while (cinfo.output_scanline < cinfo.output_height) {
  2042. row = cinfo.output_scanline * 2;
  2043. jpeg_read_scanlines (&cinfo, buf, 1);
  2044. pixel = (JSAMPLE (*)[3]) buf[0];
  2045. for (col=0; col < p->width; col+=2) {
  2046. BAYER(row+0,col+0) = pixel[col+0][1] << 1;
  2047. BAYER(row+1,col+1) = pixel[col+1][1] << 1;
  2048. BAYER(row+0,col+1) = pixel[col][0] + pixel[col+1][0];
  2049. BAYER(row+1,col+0) = pixel[col][2] + pixel[col+1][2];
  2050. }
  2051. }
  2052. jpeg_finish_decompress (&cinfo);
  2053. jpeg_destroy_decompress (&cinfo);
  2054. p->maximum = 0xff << 1;
  2055. }
  2056. #endif
  2057. void DCR_CLASS dcr_kodak_dc120_load_raw(DCRAW* p)
  2058. {
  2059. static const int mul[4] = { 162, 192, 187, 92 };
  2060. static const int add[4] = { 0, 636, 424, 212 };
  2061. uchar pixel[848];
  2062. int row, shift, col;
  2063. for (row=0; row < p->height; row++) {
  2064. if (dcr_fread(p->obj_, pixel, 1, 848) < 848) dcr_derror(p);
  2065. shift = row * mul[row & 3] + add[row & 3];
  2066. for (col=0; col < p->width; col++)
  2067. BAYER(row,col) = (ushort) pixel[(col + shift) % 848];
  2068. }
  2069. p->maximum = 0xff;
  2070. }
  2071. void DCR_CLASS dcr_eight_bit_load_raw(DCRAW* p)
  2072. {
  2073. uchar *pixel;
  2074. unsigned row, col, val, lblack=0;
  2075. pixel = (uchar *) calloc (p->raw_width, sizeof *pixel);
  2076. dcr_merror (p, pixel, "eight_bit_load_raw()");
  2077. dcr_fseek(p->obj_, p->top_margin*p->raw_width, SEEK_CUR);
  2078. for (row=0; row < p->height; row++) {
  2079. if (dcr_fread(p->obj_, pixel, 1, p->raw_width) < p->raw_width) dcr_derror(p);
  2080. for (col=0; col < p->raw_width; col++) {
  2081. val = p->curve[pixel[col]];
  2082. if ((unsigned) (col-p->left_margin) < p->width)
  2083. BAYER(row,col-p->left_margin) = val;
  2084. else lblack += val;
  2085. }
  2086. }
  2087. free (pixel);
  2088. if (p->raw_width > p->width+1)
  2089. p->black = lblack / ((p->raw_width - p->width) * p->height);
  2090. if (!strncmp(p->model,"DC2",3))
  2091. p->black = 0;
  2092. p->maximum = p->curve[0xff];
  2093. }
  2094. void DCR_CLASS dcr_kodak_yrgb_load_raw(DCRAW* p)
  2095. {
  2096. uchar *pixel;
  2097. int row, col, y, cb, cr, rgb[3], c;
  2098. pixel = (uchar *) calloc (p->raw_width, 3*sizeof *pixel);
  2099. dcr_merror (p, pixel, "kodak_yrgb_load_raw()");
  2100. for (row=0; row < p->height; row++) {
  2101. if (~row & 1)
  2102. if (dcr_fread(p->obj_, pixel, p->raw_width, 3) < 3) dcr_derror(p);
  2103. for (col=0; col < p->raw_width; col++) {
  2104. y = pixel[p->width*2*(row & 1) + col];
  2105. cb = pixel[p->width + (col & -2)] - 128;
  2106. cr = pixel[p->width + (col & -2)+1] - 128;
  2107. rgb[1] = y-((cb + cr + 2) >> 2);
  2108. rgb[2] = rgb[1] + cb;
  2109. rgb[0] = rgb[1] + cr;
  2110. FORC3 p->image[row*p->width+col][c] = LIM(rgb[c],0,255);
  2111. }
  2112. }
  2113. free (pixel);
  2114. p->use_gamma = 0;
  2115. }
  2116. void DCR_CLASS dcr_kodak_262_load_raw(DCRAW* p)
  2117. {
  2118. static const uchar kodak_tree[2][26] =
  2119. { { 0,1,5,1,1,2,0,0,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9 },
  2120. { 0,3,1,1,1,1,1,2,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9 } };
  2121. struct dcr_decode *decode[2];
  2122. uchar *pixel;
  2123. int *strip, ns, i, row, col, chess, pi=0, pi1, pi2, pred, val;
  2124. dcr_init_decoder(p);
  2125. for (i=0; i < 2; i++) {
  2126. decode[i] = p->free_decode;
  2127. dcr_make_decoder (p, kodak_tree[i], 0);
  2128. }
  2129. ns = (p->raw_height+63) >> 5;
  2130. pixel = (uchar *) malloc (p->raw_width*32 + ns*4);
  2131. dcr_merror (p, pixel, "kodak_262_load_raw()");
  2132. strip = (int *) (pixel + p->raw_width*32);
  2133. p->order = 0x4d4d;
  2134. for (i=0; i < ns; i++)
  2135. strip[i] = dcr_get4(p);
  2136. for (row=0; row < p->raw_height; row++) {
  2137. if ((row & 31) == 0) {
  2138. dcr_fseek(p->obj_, strip[row >> 5], SEEK_SET);
  2139. dcr_getbits(p, -1);
  2140. pi = 0;
  2141. }
  2142. for (col=0; col < p->raw_width; col++) {
  2143. chess = (row + col) & 1;
  2144. pi1 = chess ? pi-2 : pi-p->raw_width-1;
  2145. pi2 = chess ? pi-2*p->raw_width : pi-p->raw_width+1;
  2146. if (col <= chess) pi1 = -1;
  2147. if (pi1 < 0) pi1 = pi2;
  2148. if (pi2 < 0) pi2 = pi1;
  2149. if (pi1 < 0 && col > 1) pi1 = pi2 = pi-2;
  2150. pred = (pi1 < 0) ? 0 : (pixel[pi1] + pixel[pi2]) >> 1;
  2151. pixel[pi] = val = pred + dcr_ljpeg_diff (p, decode[chess]);
  2152. if (val >> 8) dcr_derror(p);
  2153. val = p->curve[pixel[pi++]];
  2154. if ((unsigned) (col-p->left_margin) < p->width)
  2155. BAYER(row,col-p->left_margin) = val;
  2156. else p->black += val;
  2157. }
  2158. }
  2159. free (pixel);
  2160. if (p->raw_width > p->width)
  2161. p->black /= (p->raw_width - p->width) * p->height;
  2162. }
  2163. int DCR_CLASS dcr_kodak_65000_decode (DCRAW* p, short *out, int bsize)
  2164. {
  2165. uchar c, blen[768];
  2166. ushort raw[6];
  2167. INT64 bitbuf=0;
  2168. int save, bits=0, i, j, len, diff;
  2169. save = dcr_ftell(p->obj_);
  2170. bsize = (bsize + 3) & -4;
  2171. for (i=0; i < bsize; i+=2) {
  2172. c = dcr_fgetc(p->obj_);
  2173. if ((blen[i ] = c & 15) > 12 ||
  2174. (blen[i+1] = c >> 4) > 12 ) {
  2175. dcr_fseek(p->obj_, save, SEEK_SET);
  2176. for (i=0; i < bsize; i+=8) {
  2177. dcr_read_shorts (p, raw, 6);
  2178. out[i ] = raw[0] >> 12 << 8 | raw[2] >> 12 << 4 | raw[4] >> 12;
  2179. out[i+1] = raw[1] >> 12 << 8 | raw[3] >> 12 << 4 | raw[5] >> 12;
  2180. for (j=0; j < 6; j++)
  2181. out[i+2+j] = raw[j] & 0xfff;
  2182. }
  2183. return 1;
  2184. }
  2185. }
  2186. if ((bsize & 7) == 4) {
  2187. bitbuf = dcr_fgetc(p->obj_) << 8;
  2188. bitbuf += dcr_fgetc(p->obj_);
  2189. bits = 16;
  2190. }
  2191. for (i=0; i < bsize; i++) {
  2192. len = blen[i];
  2193. if (bits < len) {
  2194. for (j=0; j < 32; j+=8)
  2195. bitbuf += (INT64) dcr_fgetc(p->obj_) << (bits+(j^8));
  2196. bits += 32;
  2197. }
  2198. diff = (int)(bitbuf & (0xffff >> (16-len)));
  2199. bitbuf >>= len;
  2200. bits -= len;
  2201. if ((diff & (1 << (len-1))) == 0)
  2202. diff -= (1 << len) - 1;
  2203. out[i] = diff;
  2204. }
  2205. return 0;
  2206. }
  2207. void DCR_CLASS dcr_kodak_65000_load_raw(DCRAW* p)
  2208. {
  2209. short buf[256];
  2210. int row, col, len, pred[2], ret, i;
  2211. for (row=0; row < p->height; row++)
  2212. for (col=0; col < p->width; col+=256) {
  2213. pred[0] = pred[1] = 0;
  2214. len = MIN (256, p->width-col);
  2215. ret = dcr_kodak_65000_decode (p, buf, len);
  2216. for (i=0; i < len; i++)
  2217. if ((BAYER(row,col+i) = p->curve[ret ? buf[i] :
  2218. (pred[i & 1] += buf[i])]) >> 12) dcr_derror(p);
  2219. }
  2220. }
  2221. void DCR_CLASS dcr_kodak_ycbcr_load_raw(DCRAW* p)
  2222. {
  2223. short buf[384], *bp;
  2224. int row, col, len, c, i, j, k, y[2][2], cb, cr, rgb[3];
  2225. ushort *ip;
  2226. for (row=0; row < p->height; row+=2)
  2227. for (col=0; col < p->width; col+=128) {
  2228. len = MIN (128, p->width-col);
  2229. dcr_kodak_65000_decode (p, buf, len*3);
  2230. y[0][1] = y[1][1] = cb = cr = 0;
  2231. for (bp=buf, i=0; i < len; i+=2, bp+=2) {
  2232. cb += bp[4];
  2233. cr += bp[5];
  2234. rgb[1] = -((cb + cr + 2) >> 2);
  2235. rgb[2] = rgb[1] + cb;
  2236. rgb[0] = rgb[1] + cr;
  2237. for (j=0; j < 2; j++)
  2238. for (k=0; k < 2; k++) {
  2239. if ((y[j][k] = y[j][k^1] + *bp++) >> 10) dcr_derror(p);
  2240. ip = p->image[(row+j)*p->width + col+i+k];
  2241. FORC3 ip[c] = p->curve[LIM(y[j][k]+rgb[c], 0, 0xfff)];
  2242. }
  2243. }
  2244. }
  2245. }
  2246. void DCR_CLASS dcr_kodak_rgb_load_raw(DCRAW* p)
  2247. {
  2248. short buf[768], *bp;
  2249. int row, col, len, c, i, rgb[3];
  2250. ushort *ip=p->image[0];
  2251. for (row=0; row < p->height; row++)
  2252. for (col=0; col < p->width; col+=256) {
  2253. len = MIN (256, p->width-col);
  2254. dcr_kodak_65000_decode (p, buf, len*3);
  2255. memset (rgb, 0, sizeof rgb);
  2256. for (bp=buf, i=0; i < len; i++, ip+=4)
  2257. FORC3 if ((ip[c] = rgb[c] += *bp++) >> 12) dcr_derror(p);
  2258. }
  2259. }
  2260. void DCR_CLASS dcr_kodak_thumb_load_raw(DCRAW* p)
  2261. {
  2262. int row, col;
  2263. p->colors = p->thumb_misc >> 5;
  2264. for (row=0; row < p->height; row++)
  2265. for (col=0; col < p->width; col++)
  2266. dcr_read_shorts (p, p->image[row*p->width+col], p->colors);
  2267. p->maximum = (1 << (p->thumb_misc & 31)) - 1;
  2268. }
  2269. void DCR_CLASS dcr_sony_decrypt (unsigned *data, int len, int start, int key)
  2270. {
  2271. static unsigned pad[128], p;
  2272. if (start) {
  2273. for (p=0; p < 4; p++)
  2274. pad[p] = key = key * 48828125 + 1;
  2275. pad[3] = pad[3] << 1 | (pad[0]^pad[2]) >> 31;
  2276. for (p=4; p < 127; p++)
  2277. pad[p] = (pad[p-4]^pad[p-2]) << 1 | (pad[p-3]^pad[p-1]) >> 31;
  2278. for (p=0; p < 127; p++)
  2279. pad[p] = htonl(pad[p]);
  2280. }
  2281. while (len--)
  2282. *data++ ^= pad[p++ & 127] = pad[(p+1) & 127] ^ pad[(p+65) & 127];
  2283. }
  2284. void DCR_CLASS dcr_sony_load_raw(DCRAW* p)
  2285. {
  2286. uchar head[40];
  2287. ushort *pixel;
  2288. unsigned i, key, row, col;
  2289. dcr_fseek(p->obj_, 200896, SEEK_SET);
  2290. dcr_fseek(p->obj_, (unsigned) dcr_fgetc(p->obj_)*4 - 1, SEEK_CUR);
  2291. p->order = 0x4d4d;
  2292. key = dcr_get4(p);
  2293. dcr_fseek(p->obj_, 164600, SEEK_SET);
  2294. dcr_fread(p->obj_, head, 1, 40);
  2295. dcr_sony_decrypt ((unsigned int *) head, 10, 1, key);
  2296. for (i=26; i-- > 22; )
  2297. key = key << 8 | head[i];
  2298. dcr_fseek(p->obj_, p->data_offset, SEEK_SET);
  2299. pixel = (ushort *) calloc (p->raw_width, sizeof *pixel);
  2300. dcr_merror (p, pixel, "sony_load_raw()");
  2301. for (row=0; row < p->height; row++) {
  2302. if (dcr_fread(p->obj_, pixel, 2, p->raw_width) < p->raw_width) dcr_derror(p);
  2303. dcr_sony_decrypt ((unsigned int *) pixel, p->raw_width/2, !row, key);
  2304. for (col=9; col < p->left_margin; col++)
  2305. p->black += ntohs(pixel[col]);
  2306. for (col=0; col < p->width; col++)
  2307. if ((BAYER(row,col) = ntohs(pixel[col+p->left_margin])) >> 14)
  2308. dcr_derror(p);
  2309. }
  2310. free (pixel);
  2311. if (p->left_margin > 9)
  2312. p->black /= (p->left_margin-9) * p->height;
  2313. p->maximum = 0x3ff0;
  2314. }
  2315. void DCR_CLASS dcr_sony_arw_load_raw(DCRAW* p)
  2316. {
  2317. int col, row, len, diff, sum=0;
  2318. dcr_getbits(p, -1);
  2319. for (col = p->raw_width; col--; )
  2320. for (row=0; row < p->raw_height+1; row+=2) {
  2321. if (row == p->raw_height) row = 1;
  2322. len = 4 - dcr_getbits(p, 2);
  2323. if (len == 3 && dcr_getbits(p, 1)) len = 0;
  2324. if (len == 4)
  2325. while (len < 17 && !dcr_getbits(p, 1)) len++;
  2326. diff = dcr_getbits(p, len);
  2327. if ((diff & (1 << (len-1))) == 0)
  2328. diff -= (1 << len) - 1;
  2329. if ((sum += diff) >> 12) dcr_derror(p);
  2330. if (row < p->height) BAYER(row,col) = sum;
  2331. }
  2332. }
  2333. void DCR_CLASS dcr_sony_arw2_load_raw(DCRAW* p)
  2334. {
  2335. uchar *data, *dp;
  2336. ushort pix[16];
  2337. int row, col, val, max, min, imax, imin, sh, bit, i;
  2338. data = (uchar *) malloc (p->raw_width*p->tiff_bps >> 3);
  2339. dcr_merror (p, data, "sony_arw2_load_raw()");
  2340. for (row=0; row < p->height; row++) {
  2341. dcr_fread(p->obj_, data, 1, p->raw_width*p->tiff_bps >> 3);
  2342. if (p->tiff_bps == 8) {
  2343. for (dp=data, col=0; col < p->width-30; dp+=16) {
  2344. max = 0x7ff & (val = dcr_sget4(p, dp));
  2345. min = 0x7ff & val >> 11;
  2346. imax = 0x0f & val >> 22;
  2347. imin = 0x0f & val >> 26;
  2348. for (sh=0; sh < 4 && 0x80 << sh <= max-min; sh++);
  2349. for (bit=30, i=0; i < 16; i++)
  2350. if (i == imax) pix[i] = max;
  2351. else if (i == imin) pix[i] = min;
  2352. else {
  2353. pix[i] = ((dcr_sget2(p, dp+(bit >> 3)) >> (bit & 7) & 0x7f) << sh) + min;
  2354. if (pix[i] > 0x7ff) pix[i] = 0x7ff;
  2355. bit += 7;
  2356. }
  2357. for (i=0; i < 16; i++, col+=2)
  2358. BAYER(row,col) = p->curve[pix[i] << 1] >> 1;
  2359. col -= col & 1 ? 1:31;
  2360. }
  2361. } else if (p->tiff_bps == 12)
  2362. for (dp=data, col=0; col < p->width; dp+=3, col+=2) {
  2363. BAYER(row,col) = ((dp[1] << 8 | dp[0]) & 0xfff) << 1;
  2364. BAYER(row,col+1) = (dp[2] << 4 | dp[1] >> 4) << 1;
  2365. }
  2366. }
  2367. free (data);
  2368. }
  2369. #define HOLE(row) ((holes >> (((row) - p->raw_height) & 7)) & 1)
  2370. /* Kudos to Rich Taylor for figuring out SMaL's compression algorithm. */
  2371. void DCR_CLASS dcr_smal_decode_segment (DCRAW* p, unsigned seg[2][2], int holes)
  2372. {
  2373. uchar hist[3][13] = {
  2374. { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
  2375. { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
  2376. { 3, 3, 0, 0, 63, 47, 31, 15, 0 } };
  2377. int low, high=0xff, carry=0, nbits=8;
  2378. int s, count, bin, next, i, sym[3];
  2379. uchar diff, pred[]={0,0};
  2380. ushort data=0, range=0;
  2381. unsigned pix, row, col;
  2382. dcr_fseek(p->obj_, seg[0][1]+1, SEEK_SET);
  2383. dcr_getbits(p, -1);
  2384. for (pix=seg[0][0]; pix < seg[1][0]; pix++) {
  2385. for (s=0; s < 3; s++) {
  2386. data = data << nbits | dcr_getbits(p, nbits);
  2387. if (carry < 0)
  2388. carry = (nbits += carry+1) < 1 ? nbits-1 : 0;
  2389. while (--nbits >= 0)
  2390. if ((data >> nbits & 0xff) == 0xff) break;
  2391. if (nbits > 0)
  2392. data = ((data & ((1 << (nbits-1)) - 1)) << 1) |
  2393. ((data + (((data & (1 << (nbits-1)))) << 1)) & (-1 << nbits));
  2394. if (nbits >= 0) {
  2395. data += dcr_getbits(p, 1);
  2396. carry = nbits - 8;
  2397. }
  2398. count = ((((data-range+1) & 0xffff) << 2) - 1) / (high >> 4);
  2399. for (bin=0; hist[s][bin+5] > count; bin++);
  2400. low = hist[s][bin+5] * (high >> 4) >> 2;
  2401. if (bin) high = hist[s][bin+4] * (high >> 4) >> 2;
  2402. high -= low;
  2403. for (nbits=0; high << nbits < 128; nbits++);
  2404. range = (range+low) << nbits;
  2405. high <<= nbits;
  2406. next = hist[s][1];
  2407. if (++hist[s][2] > hist[s][3]) {
  2408. next = (next+1) & hist[s][0];
  2409. hist[s][3] = (hist[s][next+4] - hist[s][next+5]) >> 2;
  2410. hist[s][2] = 1;
  2411. }
  2412. if (hist[s][hist[s][1]+4] - hist[s][hist[s][1]+5] > 1) {
  2413. if (bin < hist[s][1])
  2414. for (i=bin; i < hist[s][1]; i++) hist[s][i+5]--;
  2415. else if (next <= bin)
  2416. for (i=hist[s][1]; i < bin; i++) hist[s][i+5]++;
  2417. }
  2418. hist[s][1] = next;
  2419. sym[s] = bin;
  2420. }
  2421. diff = sym[2] << 5 | sym[1] << 2 | (sym[0] & 3);
  2422. if (sym[0] & 4)
  2423. diff = diff ? -diff : 0x80;
  2424. if (dcr_ftell(p->obj_) + 12 >= (long)seg[1][1])
  2425. diff = 0;
  2426. pred[pix & 1] += diff;
  2427. row = pix / p->raw_width - p->top_margin;
  2428. col = pix % p->raw_width - p->left_margin;
  2429. if (row < p->height && col < p->width)
  2430. BAYER(row,col) = pred[pix & 1];
  2431. if (!(pix & 1) && HOLE(row)) pix += 2;
  2432. }
  2433. p->maximum = 0xff;
  2434. }
  2435. void DCR_CLASS dcr_smal_v6_load_raw(DCRAW* p)
  2436. {
  2437. unsigned seg[2][2];
  2438. dcr_fseek(p->obj_, 16, SEEK_SET);
  2439. seg[0][0] = 0;
  2440. seg[0][1] = dcr_get2(p);
  2441. seg[1][0] = p->raw_width * p->raw_height;
  2442. seg[1][1] = INT_MAX;
  2443. dcr_smal_decode_segment (p,seg, 0);
  2444. p->use_gamma = 0;
  2445. }
  2446. int DCR_CLASS dcr_median4 (int *p)
  2447. {
  2448. int min, max, sum, i;
  2449. min = max = sum = p[0];
  2450. for (i=1; i < 4; i++) {
  2451. sum += p[i];
  2452. if (min > p[i]) min = p[i];
  2453. if (max < p[i]) max = p[i];
  2454. }
  2455. return (sum - min - max) >> 1;
  2456. }
  2457. void DCR_CLASS dcr_fill_holes (DCRAW* p, int holes)
  2458. {
  2459. int row, col, val[4];
  2460. for (row=2; row < p->height-2; row++) {
  2461. if (!HOLE(row)) continue;
  2462. for (col=1; col < p->width-1; col+=4) {
  2463. val[0] = BAYER(row-1,col-1);
  2464. val[1] = BAYER(row-1,col+1);
  2465. val[2] = BAYER(row+1,col-1);
  2466. val[3] = BAYER(row+1,col+1);
  2467. BAYER(row,col) = dcr_median4(val);
  2468. }
  2469. for (col=2; col < p->width-2; col+=4)
  2470. if (HOLE(row-2) || HOLE(row+2))
  2471. BAYER(row,col) = (BAYER(row,col-2) + BAYER(row,col+2)) >> 1;
  2472. else {
  2473. val[0] = BAYER(row,col-2);
  2474. val[1] = BAYER(row,col+2);
  2475. val[2] = BAYER(row-2,col);
  2476. val[3] = BAYER(row+2,col);
  2477. BAYER(row,col) = dcr_median4(val);
  2478. }
  2479. }
  2480. }
  2481. void DCR_CLASS dcr_smal_v9_load_raw(DCRAW* p)
  2482. {
  2483. unsigned seg[256][2], offset, nseg, holes, i;
  2484. dcr_fseek(p->obj_, 67, SEEK_SET);
  2485. offset = dcr_get4(p);
  2486. nseg = dcr_fgetc(p->obj_);
  2487. dcr_fseek(p->obj_, offset, SEEK_SET);
  2488. for (i=0; i < nseg*2; i++)
  2489. seg[0][i] = dcr_get4(p) + p->data_offset*(i & 1);
  2490. dcr_fseek(p->obj_, 78, SEEK_SET);
  2491. holes = dcr_fgetc(p->obj_);
  2492. dcr_fseek(p->obj_, 88, SEEK_SET);
  2493. seg[nseg][0] = p->raw_height * p->raw_width;
  2494. seg[nseg][1] = dcr_get4(p) + p->data_offset;
  2495. for (i=0; i < nseg; i++)
  2496. dcr_smal_decode_segment (p,seg+i, holes);
  2497. if (holes) dcr_fill_holes (p,holes);
  2498. }
  2499. /* RESTRICTED code starts here */
  2500. #if RESTRICTED
  2501. void DCR_CLASS dcr_foveon_decoder (DCRAW* p, unsigned size, unsigned code)
  2502. {
  2503. static unsigned huff[1024];
  2504. struct dcr_decode *cur;
  2505. int i, len;
  2506. if (!code) {
  2507. for (i=0; i < (int)size; i++)
  2508. huff[i] = dcr_get4(p);
  2509. dcr_init_decoder(p);
  2510. }
  2511. cur = p->free_decode++;
  2512. if (p->free_decode > p->first_decode+2048) {
  2513. fprintf (stderr,_("%s: decoder table overflow\n"), p->ifname);
  2514. longjmp (p->failure, 2);
  2515. }
  2516. if (code)
  2517. for (i=0; i < (int)size; i++)
  2518. if (huff[i] == code) {
  2519. cur->leaf = i;
  2520. return;
  2521. }
  2522. if ((len = code >> 27) > 26) return;
  2523. code = (len+1) << 27 | (code & 0x3ffffff) << 1;
  2524. cur->branch[0] = p->free_decode;
  2525. dcr_foveon_decoder (p,size, code);
  2526. cur->branch[1] = p->free_decode;
  2527. dcr_foveon_decoder (p,size, code+1);
  2528. }
  2529. void DCR_CLASS dcr_foveon_thumb (DCRAW* p, FILE *tfp)
  2530. {
  2531. unsigned bwide, row, col, bitbuf=0, bit=1, c, i;
  2532. char *buf;
  2533. struct dcr_decode *dindex;
  2534. short pred[3];
  2535. bwide = dcr_get4(p);
  2536. fprintf (tfp, "P6\n%d %d\n255\n", p->thumb_width, p->thumb_height);
  2537. if (bwide > 0) {
  2538. if (bwide < (unsigned int)(p->thumb_width*3)) return;
  2539. buf = (char *) malloc (bwide);
  2540. dcr_merror (p, buf, "foveon_thumb()");
  2541. for (row=0; row < p->thumb_height; row++) {
  2542. dcr_fread(p->obj_, buf, 1, bwide);
  2543. fwrite (buf, 3, p->thumb_width, tfp);
  2544. }
  2545. free (buf);
  2546. return;
  2547. }
  2548. dcr_foveon_decoder (p,256, 0);
  2549. for (row=0; row < p->thumb_height; row++) {
  2550. memset (pred, 0, sizeof pred);
  2551. if (!bit) dcr_get4(p);
  2552. for (bit=col=0; col < p->thumb_width; col++)
  2553. FORC3 {
  2554. for (dindex=p->first_decode; dindex->branch[0]; ) {
  2555. if ((bit = (bit-1) & 31) == 31)
  2556. for (i=0; i < 4; i++)
  2557. bitbuf = (bitbuf << 8) + dcr_fgetc(p->obj_);
  2558. dindex = dindex->branch[bitbuf >> bit & 1];
  2559. }
  2560. pred[c] += dindex->leaf;
  2561. fputc (pred[c], tfp);
  2562. }
  2563. }
  2564. }
  2565. void DCR_CLASS dcr_foveon_load_camf(DCRAW* p)
  2566. {
  2567. unsigned key, i, val;
  2568. dcr_fseek(p->obj_, p->meta_offset, SEEK_SET);
  2569. key = dcr_get4(p);
  2570. dcr_fread(p->obj_, p->meta_data, 1, p->meta_length);
  2571. for (i=0; i < p->meta_length; i++) {
  2572. key = (key * 1597 + 51749) % 244944;
  2573. val = (unsigned int)(key * (INT64) 301593171 >> 24);
  2574. p->meta_data[i] ^= ((((key << 8) - val) >> 1) + val) >> 17;
  2575. }
  2576. }
  2577. void DCR_CLASS dcr_foveon_load_raw(DCRAW* p)
  2578. {
  2579. struct dcr_decode *dindex;
  2580. short diff[1024];
  2581. unsigned bitbuf=0;
  2582. int pred[3], fixed, row, col, bit=-1, c, i;
  2583. fixed = dcr_get4(p);
  2584. dcr_read_shorts (p, (ushort *) diff, 1024);
  2585. if (!fixed) dcr_foveon_decoder (p, 1024, 0);
  2586. for (row=0; row < p->height; row++) {
  2587. memset (pred, 0, sizeof pred);
  2588. if (!bit && !fixed && atoi(p->model+2) < 14) dcr_get4(p);
  2589. for (col=bit=0; col < p->width; col++) {
  2590. if (fixed) {
  2591. bitbuf = dcr_get4(p);
  2592. FORC3 pred[2-c] += diff[bitbuf >> c*10 & 0x3ff];
  2593. }
  2594. else FORC3 {
  2595. for (dindex=p->first_decode; dindex->branch[0]; ) {
  2596. if ((bit = (bit-1) & 31) == 31)
  2597. for (i=0; i < 4; i++)
  2598. bitbuf = (bitbuf << 8) + dcr_fgetc(p->obj_);
  2599. dindex = dindex->branch[bitbuf >> bit & 1];
  2600. }
  2601. pred[c] += diff[dindex->leaf];
  2602. if (pred[c] >> 16 && ~pred[c] >> 16) dcr_derror(p);
  2603. }
  2604. FORC3 p->image[row*p->width+col][c] = pred[c];
  2605. }
  2606. }
  2607. if (p->opt.document_mode)
  2608. for (i=0; i < p->height*p->width*4; i++)
  2609. if ((short) p->image[0][i] < 0) p->image[0][i] = 0;
  2610. dcr_foveon_load_camf(p);
  2611. }
  2612. const char * DCR_CLASS dcr_foveon_camf_param (DCRAW* p, const char *block, const char *param)
  2613. {
  2614. unsigned idx, num;
  2615. char *pos, *cp, *dp;
  2616. for (idx=0; idx < p->meta_length; idx += dcr_sget4(p, pos+8)) {
  2617. pos = p->meta_data + idx;
  2618. if (strncmp (pos, "CMb", 3)) break;
  2619. if (pos[3] != 'P') continue;
  2620. if (strcmp (block, pos+dcr_sget4(p, pos+12))) continue;
  2621. cp = pos + dcr_sget4(p, pos+16);
  2622. num = dcr_sget4(p, cp);
  2623. dp = pos + dcr_sget4(p, cp+4);
  2624. while (num--) {
  2625. cp += 8;
  2626. if (!strcmp (param, dp+dcr_sget4(p, cp)))
  2627. return dp+dcr_sget4(p, cp+4);
  2628. }
  2629. }
  2630. return 0;
  2631. }
  2632. void * DCR_CLASS dcr_foveon_camf_matrix (DCRAW* p, unsigned dim[3], const char *name)
  2633. {
  2634. unsigned i, idx, type, ndim, size, *mat;
  2635. char *pos, *cp, *dp;
  2636. double dsize;
  2637. for (idx=0; idx < p->meta_length; idx += dcr_sget4(p, pos+8)) {
  2638. pos = p->meta_data + idx;
  2639. if (strncmp (pos, "CMb", 3)) break;
  2640. if (pos[3] != 'M') continue;
  2641. if (strcmp (name, pos+dcr_sget4(p, pos+12))) continue;
  2642. dim[0] = dim[1] = dim[2] = 1;
  2643. cp = pos + dcr_sget4(p, pos+16);
  2644. type = dcr_sget4(p, cp);
  2645. if ((ndim = dcr_sget4(p, cp+4)) > 3) break;
  2646. dp = pos + dcr_sget4(p, cp+8);
  2647. for (i=ndim; i--; ) {
  2648. cp += 12;
  2649. dim[i] = dcr_sget4(p, cp);
  2650. }
  2651. if ((dsize = (double) dim[0]*dim[1]*dim[2]) > p->meta_length/4) break;
  2652. mat = (unsigned *) malloc ((size = (unsigned int)dsize) * 4);
  2653. dcr_merror (p, mat, "dcr_foveon_camf_matrix()");
  2654. for (i=0; i < size; i++)
  2655. if (type && type != 6)
  2656. mat[i] = dcr_sget4(p, dp + i*4);
  2657. else
  2658. mat[i] = dcr_sget4(p, dp + i*2) & 0xffff;
  2659. return mat;
  2660. }
  2661. fprintf (stderr,_("%s: \"%s\" matrix not found!\n"), p->ifname, name);
  2662. return 0;
  2663. }
  2664. int DCR_CLASS dcr_foveon_fixed (DCRAW* p, void *ptr, int size, const char *name)
  2665. {
  2666. void *dp;
  2667. unsigned dim[3];
  2668. dp = dcr_foveon_camf_matrix (p,dim, name);
  2669. if (!dp) return 0;
  2670. memcpy (ptr, dp, size*4);
  2671. free (dp);
  2672. return 1;
  2673. }
  2674. float DCR_CLASS dcr_foveon_avg (short *pix, int range[2], float cfilt)
  2675. {
  2676. int i;
  2677. float val, min=FLT_MAX, max=-FLT_MAX, sum=0;
  2678. for (i=range[0]; i <= range[1]; i++) {
  2679. sum += val = pix[i*4] + (pix[i*4]-pix[(i-1)*4]) * cfilt;
  2680. if (min > val) min = val;
  2681. if (max < val) max = val;
  2682. }
  2683. if (range[1] - range[0] == 1) return sum/2;
  2684. return (sum - min - max) / (range[1] - range[0] - 1);
  2685. }
  2686. short * DCR_CLASS dcr_foveon_make_curve (DCRAW* p, double max, double mul, double filt)
  2687. {
  2688. short *curve;
  2689. unsigned i, size;
  2690. double x;
  2691. if (!filt) filt = 0.8;
  2692. size = (unsigned int)(4*M_PI*max / filt);
  2693. if (size == UINT_MAX) size--;
  2694. curve = (short *) calloc (size+1, sizeof *curve);
  2695. dcr_merror (p, p->curve, "foveon_make_curve()");
  2696. p->curve[0] = size;
  2697. for (i=0; i < size; i++) {
  2698. x = i*filt/max/4;
  2699. p->curve[i+1] = (short)((cos(x)+1)/2 * tanh(i*filt/mul) * mul + 0.5);
  2700. }
  2701. return curve;
  2702. }
  2703. void DCR_CLASS dcr_foveon_make_curves
  2704. (DCRAW* p, short **curvep, float dq[3], float div[3], float filt)
  2705. {
  2706. double mul[3], max=0;
  2707. int c;
  2708. FORC3 mul[c] = dq[c]/div[c];
  2709. FORC3 if (max < mul[c]) max = mul[c];
  2710. FORC3 curvep[c] = dcr_foveon_make_curve (p,max, mul[c], filt);
  2711. }
  2712. int DCR_CLASS dcr_foveon_apply_curve (short *curve, int i)
  2713. {
  2714. if (abs(i) >= curve[0]) return 0;
  2715. return i < 0 ? -curve[1-i] : curve[1+i];
  2716. }
  2717. #define image4 ((short (*)[4]) p->image)
  2718. void DCR_CLASS dcr_foveon_interpolate(DCRAW* p)
  2719. {
  2720. static const short hood[] = { -1,-1, -1,0, -1,1, 0,-1, 0,1, 1,-1, 1,0, 1,1 };
  2721. short *pix, prev[3], *curve[8], (*shrink)[3];
  2722. float cfilt=0, ddft[3][3][2], ppm[3][3][3];
  2723. float cam_xyz[3][3], correct[3][3], last[3][3], trans[3][3];
  2724. float chroma_dq[3], color_dq[3], diag[3][3], div[3];
  2725. float (*black)[3], (*sgain)[3], (*sgrow)[3];
  2726. float fsum[3], val, frow, num;
  2727. int row, col, c, i, j, diff, sgx, irow, sum, min, max, limit;
  2728. int dscr[2][2], dstb[4], (*smrow[7])[3], total[4], ipix[3];
  2729. int work[3][3], smlast, smred, smred_p=0, dev[3];
  2730. int satlev[3], keep[4], active[4];
  2731. unsigned dim[3], *badpix;
  2732. double dsum=0, trsum[3];
  2733. char str[128];
  2734. const char* cp;
  2735. if (p->opt.verbose)
  2736. fprintf (stderr,_("Foveon interpolation...\n"));
  2737. dcr_foveon_fixed (p, dscr, 4, "DarkShieldColRange");
  2738. dcr_foveon_fixed (p, ppm[0][0], 27, "PostPolyMatrix");
  2739. dcr_foveon_fixed (p, satlev, 3, "SaturationLevel");
  2740. dcr_foveon_fixed (p, keep, 4, "KeepImageArea");
  2741. dcr_foveon_fixed (p, active, 4, "ActiveImageArea");
  2742. dcr_foveon_fixed (p, chroma_dq, 3, "ChromaDQ");
  2743. dcr_foveon_fixed (p, color_dq, 3,
  2744. dcr_foveon_camf_param (p, "IncludeBlocks", "ColorDQ") ?
  2745. "ColorDQ" : "ColorDQCamRGB");
  2746. if (dcr_foveon_camf_param (p, "IncludeBlocks", "ColumnFilter"))
  2747. dcr_foveon_fixed (p, &cfilt, 1, "ColumnFilter");
  2748. memset (ddft, 0, sizeof ddft);
  2749. if (!dcr_foveon_camf_param (p, "IncludeBlocks", "DarkDrift")
  2750. || !dcr_foveon_fixed (p, ddft[1][0], 12, "DarkDrift"))
  2751. for (i=0; i < 2; i++) {
  2752. dcr_foveon_fixed (p, dstb, 4, i ? "DarkShieldBottom":"DarkShieldTop");
  2753. for (row = dstb[1]; row <= dstb[3]; row++)
  2754. for (col = dstb[0]; col <= dstb[2]; col++)
  2755. FORC3 ddft[i+1][c][1] += (short) image4[row*p->width+col][c];
  2756. FORC3 ddft[i+1][c][1] /= (dstb[3]-dstb[1]+1) * (dstb[2]-dstb[0]+1);
  2757. }
  2758. if (!(cp = dcr_foveon_camf_param (p, "WhiteBalanceIlluminants", p->model2)))
  2759. { fprintf (stderr,_("%s: Invalid p->white balance \"%s\"\n"), p->ifname, p->model2);
  2760. return; }
  2761. dcr_foveon_fixed (p, cam_xyz, 9, cp);
  2762. dcr_foveon_fixed (p, correct, 9,
  2763. dcr_foveon_camf_param (p, "WhiteBalanceCorrections", p->model2));
  2764. memset (last, 0, sizeof last);
  2765. for (i=0; i < 3; i++)
  2766. for (j=0; j < 3; j++)
  2767. FORC3 last[i][j] += correct[i][c] * cam_xyz[c][j];
  2768. #define LAST(x,y) last[(i+x)%3][(c+y)%3]
  2769. for (i=0; i < 3; i++)
  2770. FORC3 diag[c][i] = LAST(1,1)*LAST(2,2) - LAST(1,2)*LAST(2,1);
  2771. #undef LAST
  2772. FORC3 div[c] = diag[c][0]*0.3127f + diag[c][1]*0.329f + diag[c][2]*0.3583f;
  2773. sprintf (str, "%sRGBNeutral", p->model2);
  2774. if (dcr_foveon_camf_param (p, "IncludeBlocks", str))
  2775. dcr_foveon_fixed (p, div, 3, str);
  2776. num = 0;
  2777. FORC3 if (num < div[c]) num = div[c];
  2778. FORC3 div[c] /= num;
  2779. memset (trans, 0, sizeof trans);
  2780. for (i=0; i < 3; i++)
  2781. for (j=0; j < 3; j++)
  2782. FORC3 trans[i][j] += p->rgb_cam[i][c] * last[c][j] * div[j];
  2783. FORC3 trsum[c] = trans[c][0] + trans[c][1] + trans[c][2];
  2784. dsum = (6*trsum[0] + 11*trsum[1] + 3*trsum[2]) / 20;
  2785. for (i=0; i < 3; i++)
  2786. FORC3 last[i][c] = (float)(trans[i][c] * dsum / trsum[i]);
  2787. memset (trans, 0, sizeof trans);
  2788. for (i=0; i < 3; i++)
  2789. for (j=0; j < 3; j++)
  2790. FORC3 trans[i][j] += (i==c ? 32 : -1) * last[c][j] / 30;
  2791. dcr_foveon_make_curves (p, curve, color_dq, div, cfilt);
  2792. FORC3 chroma_dq[c] /= 3;
  2793. dcr_foveon_make_curves (p, curve+3, chroma_dq, div, cfilt);
  2794. FORC3 dsum += chroma_dq[c] / div[c];
  2795. curve[6] = dcr_foveon_make_curve (p, dsum, dsum, cfilt);
  2796. curve[7] = dcr_foveon_make_curve (p, dsum*2, dsum*2, cfilt);
  2797. sgain = (float (*)[3]) dcr_foveon_camf_matrix (p, dim, "SpatialGain");
  2798. if (!sgain) return;
  2799. sgrow = (float (*)[3]) calloc (dim[1], sizeof *sgrow);
  2800. sgx = (p->width + dim[1]-2) / (dim[1]-1);
  2801. black = (float (*)[3]) calloc (p->height, sizeof *black);
  2802. for (row=0; row < p->height; row++) {
  2803. for (i=0; i < 6; i++)
  2804. ddft[0][0][i] = (float)(ddft[1][0][i] +
  2805. row / (p->height-1.0) * (ddft[2][0][i] - ddft[1][0][i]));
  2806. FORC3 black[row][c] =
  2807. ( dcr_foveon_avg (p->image[row*p->width]+c, dscr[0], cfilt) +
  2808. dcr_foveon_avg (p->image[row*p->width]+c, dscr[1], cfilt) * 3
  2809. - ddft[0][c][0] ) / 4 - ddft[0][c][1];
  2810. }
  2811. memcpy (black, black+8, sizeof *black*8);
  2812. memcpy (black+p->height-11, black+p->height-22, 11*sizeof *black);
  2813. memcpy (last, black, sizeof last);
  2814. for (row=1; row < p->height-1; row++) {
  2815. FORC3 if (last[1][c] > last[0][c]) {
  2816. if (last[1][c] > last[2][c])
  2817. black[row][c] = (last[0][c] > last[2][c]) ? last[0][c]:last[2][c];
  2818. } else
  2819. if (last[1][c] < last[2][c])
  2820. black[row][c] = (last[0][c] < last[2][c]) ? last[0][c]:last[2][c];
  2821. memmove (last, last+1, 2*sizeof last[0]);
  2822. memcpy (last[2], black[row+1], sizeof last[2]);
  2823. }
  2824. FORC3 black[row][c] = (last[0][c] + last[1][c])/2;
  2825. FORC3 black[0][c] = (black[1][c] + black[3][c])/2;
  2826. val = (float)(1 - exp(-1/24.0));
  2827. memcpy (fsum, black, sizeof fsum);
  2828. for (row=1; row < p->height; row++)
  2829. FORC3 fsum[c] += black[row][c] =
  2830. (black[row][c] - black[row-1][c])*val + black[row-1][c];
  2831. memcpy (last[0], black[p->height-1], sizeof last[0]);
  2832. FORC3 fsum[c] /= p->height;
  2833. for (row = p->height; row--; )
  2834. FORC3 last[0][c] = black[row][c] =
  2835. (black[row][c] - fsum[c] - last[0][c])*val + last[0][c];
  2836. memset (total, 0, sizeof total);
  2837. for (row=2; row < p->height; row+=4)
  2838. for (col=2; col < p->width; col+=4) {
  2839. FORC3 total[c] += (short) p->image[row*p->width+col][c];
  2840. total[3]++;
  2841. }
  2842. for (row=0; row < p->height; row++)
  2843. FORC3 black[row][c] += fsum[c]/2 + total[c]/(total[3]*100.0f);
  2844. for (row=0; row < p->height; row++) {
  2845. for (i=0; i < 6; i++)
  2846. ddft[0][0][i] = (float)(ddft[1][0][i] +
  2847. row / (p->height-1.0) * (ddft[2][0][i] - ddft[1][0][i]));
  2848. pix = p->image[row*p->width];
  2849. memcpy (prev, pix, sizeof prev);
  2850. frow = (float)(row / (p->height-1.0) * (dim[2]-1));
  2851. if ((irow = (int)frow) == (int)(dim[2]-1)) irow--;
  2852. frow -= irow;
  2853. for (i=0; i < (int)dim[1]; i++)
  2854. FORC3 sgrow[i][c] = sgain[ irow *dim[1]+i][c] * (1-frow) +
  2855. sgain[(irow+1)*dim[1]+i][c] * frow;
  2856. for (col=0; col < p->width; col++) {
  2857. FORC3 {
  2858. diff = pix[c] - prev[c];
  2859. prev[c] = pix[c];
  2860. ipix[c] = (int)( pix[c] + floor ((diff + (diff*diff >> 14)) * cfilt
  2861. - ddft[0][c][1] - ddft[0][c][0] * ((float) col/p->width - 0.5)
  2862. - black[row][c] ));
  2863. }
  2864. FORC3 {
  2865. work[0][c] = ipix[c] * ipix[c] >> 14;
  2866. work[2][c] = ipix[c] * work[0][c] >> 14;
  2867. work[1][2-c] = ipix[(c+1) % 3] * ipix[(c+2) % 3] >> 14;
  2868. }
  2869. FORC3 {
  2870. for (val=0, i=0; i < 3; i++)
  2871. for ( j=0; j < 3; j++)
  2872. val += ppm[c][i][j] * work[i][j];
  2873. ipix[c] = (int)(floor ((ipix[c] + floor(val)) *
  2874. ( sgrow[col/sgx ][c] * (sgx - col%sgx) +
  2875. sgrow[col/sgx+1][c] * (col%sgx) ) / sgx / div[c]));
  2876. if (ipix[c] > 32000) ipix[c] = 32000;
  2877. pix[c] = ipix[c];
  2878. }
  2879. pix += 4;
  2880. }
  2881. }
  2882. free (black);
  2883. free (sgrow);
  2884. free (sgain);
  2885. if ((badpix = (unsigned int *) dcr_foveon_camf_matrix (p, dim, "BadPixels"))) {
  2886. for (i=0; i < (int)dim[0]; i++) {
  2887. col = (badpix[i] >> 8 & 0xfff) - keep[0];
  2888. row = (badpix[i] >> 20 ) - keep[1];
  2889. if ((int)(row-1) > p->height-3 || (int)(col-1) > p->width-3)
  2890. continue;
  2891. memset (fsum, 0, sizeof fsum);
  2892. for (sum=j=0; j < 8; j++)
  2893. if (badpix[i] & (1 << j)) {
  2894. FORC3 fsum[c] += (short)
  2895. p->image[(row+hood[j*2])*p->width+col+hood[j*2+1]][c];
  2896. sum++;
  2897. }
  2898. if (sum) FORC3 p->image[row*p->width+col][c] = (unsigned short)(fsum[c]/sum);
  2899. }
  2900. free (badpix);
  2901. }
  2902. /* Array for 5x5 Gaussian averaging of red values */
  2903. smrow[6] = (int (*)[3]) calloc (p->width*5, sizeof **smrow);
  2904. dcr_merror (p, smrow[6], "foveon_interpolate()");
  2905. for (i=0; i < 5; i++)
  2906. smrow[i] = smrow[6] + i*p->width;
  2907. /* Sharpen the reds against these Gaussian averages */
  2908. for (smlast=-1, row=2; row < p->height-2; row++) {
  2909. while (smlast < row+2) {
  2910. for (i=0; i < 6; i++)
  2911. smrow[(i+5) % 6] = smrow[i];
  2912. pix = p->image[++smlast*p->width+2];
  2913. for (col=2; col < p->width-2; col++) {
  2914. smrow[4][col][0] =
  2915. (pix[0]*6 + (pix[-4]+pix[4])*4 + pix[-8]+pix[8] + 8) >> 4;
  2916. pix += 4;
  2917. }
  2918. }
  2919. pix = p->image[row*p->width+2];
  2920. for (col=2; col < p->width-2; col++) {
  2921. smred = ( 6 * smrow[2][col][0]
  2922. + 4 * (smrow[1][col][0] + smrow[3][col][0])
  2923. + smrow[0][col][0] + smrow[4][col][0] + 8 ) >> 4;
  2924. if (col == 2)
  2925. smred_p = smred;
  2926. i = pix[0] + ((pix[0] - ((smred*7 + smred_p) >> 3)) >> 3);
  2927. if (i > 32000) i = 32000;
  2928. pix[0] = i;
  2929. smred_p = smred;
  2930. pix += 4;
  2931. }
  2932. }
  2933. /* Adjust the brighter pixels for better linearity */
  2934. min = 0xffff;
  2935. FORC3 {
  2936. i = (int)(satlev[c] / div[c]);
  2937. if (min > i) min = i;
  2938. }
  2939. limit = min * 9 >> 4;
  2940. for (pix=p->image[0]; pix < p->image[p->height*p->width]; pix+=4) {
  2941. if (pix[0] <= limit || pix[1] <= limit || pix[2] <= limit)
  2942. continue;
  2943. min = max = pix[0];
  2944. for (c=1; c < 3; c++) {
  2945. if (min > pix[c]) min = pix[c];
  2946. if (max < pix[c]) max = pix[c];
  2947. }
  2948. if (min >= limit*2) {
  2949. pix[0] = pix[1] = pix[2] = max;
  2950. } else {
  2951. i = 0x4000 - ((min - limit) << 14) / limit;
  2952. i = 0x4000 - (i*i >> 14);
  2953. i = i*i >> 14;
  2954. FORC3 pix[c] += (max - pix[c]) * i >> 14;
  2955. }
  2956. }
  2957. /*
  2958. Because photons that miss one detector often hit another,
  2959. the sum R+G+B is much less noisy than the individual p->colors.
  2960. So smooth the hues without smoothing the total.
  2961. */
  2962. for (smlast=-1, row=2; row < p->height-2; row++) {
  2963. while (smlast < row+2) {
  2964. for (i=0; i < 6; i++)
  2965. smrow[(i+5) % 6] = smrow[i];
  2966. pix = p->image[++smlast*p->width+2];
  2967. for (col=2; col < p->width-2; col++) {
  2968. FORC3 smrow[4][col][c] = (pix[c-4]+2*pix[c]+pix[c+4]+2) >> 2;
  2969. pix += 4;
  2970. }
  2971. }
  2972. pix = p->image[row*p->width+2];
  2973. for (col=2; col < p->width-2; col++) {
  2974. FORC3 dev[c] = -dcr_foveon_apply_curve (curve[7], pix[c] -
  2975. ((smrow[1][col][c] + 2*smrow[2][col][c] + smrow[3][col][c]) >> 2));
  2976. sum = (dev[0] + dev[1] + dev[2]) >> 3;
  2977. FORC3 pix[c] += dev[c] - sum;
  2978. pix += 4;
  2979. }
  2980. }
  2981. for (smlast=-1, row=2; row < p->height-2; row++) {
  2982. while (smlast < row+2) {
  2983. for (i=0; i < 6; i++)
  2984. smrow[(i+5) % 6] = smrow[i];
  2985. pix = p->image[++smlast*p->width+2];
  2986. for (col=2; col < p->width-2; col++) {
  2987. FORC3 smrow[4][col][c] =
  2988. (pix[c-8]+pix[c-4]+pix[c]+pix[c+4]+pix[c+8]+2) >> 2;
  2989. pix += 4;
  2990. }
  2991. }
  2992. pix = p->image[row*p->width+2];
  2993. for (col=2; col < p->width-2; col++) {
  2994. for (total[3]=375, sum=60, c=0; c < 3; c++) {
  2995. for (total[c]=i=0; i < 5; i++)
  2996. total[c] += smrow[i][col][c];
  2997. total[3] += total[c];
  2998. sum += pix[c];
  2999. }
  3000. if (sum < 0) sum = 0;
  3001. j = total[3] > 375 ? (sum << 16) / total[3] : sum * 174;
  3002. FORC3 pix[c] += dcr_foveon_apply_curve (curve[6],
  3003. ((j*total[c] + 0x8000) >> 16) - pix[c]);
  3004. pix += 4;
  3005. }
  3006. }
  3007. /* Transform the image to a different colorspace */
  3008. for (pix=p->image[0]; pix < p->image[p->height*p->width]; pix+=4) {
  3009. FORC3 pix[c] -= dcr_foveon_apply_curve (curve[c], pix[c]);
  3010. sum = (pix[0]+pix[1]+pix[1]+pix[2]) >> 2;
  3011. FORC3 pix[c] -= dcr_foveon_apply_curve (curve[c], pix[c]-sum);
  3012. FORC3 {
  3013. for (dsum=i=0; i < 3; i++)
  3014. dsum += trans[c][i] * pix[i];
  3015. if (dsum < 0) dsum = 0;
  3016. if (dsum > 24000) dsum = 24000;
  3017. ipix[c] = (int)(dsum + 0.5);
  3018. }
  3019. FORC3 pix[c] = ipix[c];
  3020. }
  3021. /* Smooth the image bottom-to-top and save at 1/4 scale */
  3022. shrink = (short (*)[3]) calloc ((p->width/4) * (p->height/4), sizeof *shrink);
  3023. dcr_merror (p, shrink, "foveon_interpolate()");
  3024. for (row = p->height/4; row--; )
  3025. for (col=0; col < p->width/4; col++) {
  3026. ipix[0] = ipix[1] = ipix[2] = 0;
  3027. for (i=0; i < 4; i++)
  3028. for (j=0; j < 4; j++)
  3029. FORC3 ipix[c] += p->image[(row*4+i)*p->width+col*4+j][c];
  3030. FORC3
  3031. if (row+2 > p->height/4)
  3032. shrink[row*(p->width/4)+col][c] = ipix[c] >> 4;
  3033. else
  3034. shrink[row*(p->width/4)+col][c] =
  3035. (shrink[(row+1)*(p->width/4)+col][c]*1840 + ipix[c]*141 + 2048) >> 12;
  3036. }
  3037. /* From the 1/4-scale image, smooth right-to-left */
  3038. for (row=0; row < (p->height & ~3); row++) {
  3039. ipix[0] = ipix[1] = ipix[2] = 0;
  3040. if ((row & 3) == 0)
  3041. for (col = p->width & ~3 ; col--; )
  3042. FORC3 smrow[0][col][c] = ipix[c] =
  3043. (shrink[(row/4)*(p->width/4)+col/4][c]*1485 + ipix[c]*6707 + 4096) >> 13;
  3044. /* Then smooth left-to-right */
  3045. ipix[0] = ipix[1] = ipix[2] = 0;
  3046. for (col=0; col < (p->width & ~3); col++)
  3047. FORC3 smrow[1][col][c] = ipix[c] =
  3048. (smrow[0][col][c]*1485 + ipix[c]*6707 + 4096) >> 13;
  3049. /* Smooth top-to-bottom */
  3050. if (row == 0)
  3051. memcpy (smrow[2], smrow[1], sizeof **smrow * p->width);
  3052. else
  3053. for (col=0; col < (p->width & ~3); col++)
  3054. FORC3 smrow[2][col][c] =
  3055. (smrow[2][col][c]*6707 + smrow[1][col][c]*1485 + 4096) >> 13;
  3056. /* Adjust the chroma toward the smooth values */
  3057. for (col=0; col < (p->width & ~3); col++) {
  3058. for (i=j=30, c=0; c < 3; c++) {
  3059. i += smrow[2][col][c];
  3060. j += p->image[row*p->width+col][c];
  3061. }
  3062. j = (j << 16) / i;
  3063. for (sum=c=0; c < 3; c++) {
  3064. ipix[c] = dcr_foveon_apply_curve (curve[c+3],
  3065. ((smrow[2][col][c] * j + 0x8000) >> 16) - p->image[row*p->width+col][c]);
  3066. sum += ipix[c];
  3067. }
  3068. sum >>= 3;
  3069. FORC3 {
  3070. i = p->image[row*p->width+col][c] + ipix[c] - sum;
  3071. if (i < 0) i = 0;
  3072. p->image[row*p->width+col][c] = i;
  3073. }
  3074. }
  3075. }
  3076. free (shrink);
  3077. free (smrow[6]);
  3078. for (i=0; i < 8; i++)
  3079. free (curve[i]);
  3080. /* Trim off the black border */
  3081. active[1] -= keep[1];
  3082. active[3] -= 2;
  3083. i = active[2] - active[0];
  3084. for (row=0; row < active[3]-active[1]; row++)
  3085. memcpy (p->image[row*i], p->image[(row+active[1])*p->width+active[0]],
  3086. i * sizeof *p->image);
  3087. p->width = i;
  3088. p->height = row;
  3089. }
  3090. //#undef image
  3091. #endif //RESTRICTED
  3092. /* RESTRICTED code ends here */
  3093. /*
  3094. Seach from the current directory up to the root looking for
  3095. a ".badpixels" file, and fix those pixels now.
  3096. */
  3097. void DCR_CLASS dcr_bad_pixels(DCRAW* p, char *fname)
  3098. {
  3099. FILE *fp=0;
  3100. char *cp, line[128];
  3101. int len, time, row, col, r, c, rad, tot, n, fixed=0;
  3102. if (!p->filters) return;
  3103. if (fname)
  3104. fp = fopen (fname, "r");
  3105. else {
  3106. for (len=32 ; ; len *= 2) {
  3107. fname = (char *) malloc (len);
  3108. if (!fname) return;
  3109. if (_getcwd (fname, len-16)) break;
  3110. free (fname);
  3111. if (errno != ERANGE) return;
  3112. }
  3113. #if defined(WIN32) || defined(DJGPP)
  3114. if (fname[1] == ':')
  3115. memmove (fname, fname+2, len-2);
  3116. for (cp=fname; *cp; cp++)
  3117. if (*cp == '\\') *cp = '/';
  3118. #endif
  3119. cp = fname + strlen(fname);
  3120. if (cp[-1] == '/') cp--;
  3121. while (*fname == '/') {
  3122. strcpy (cp, "/.badpixels");
  3123. if ((fp = fopen (fname, "r"))) break;
  3124. if (cp == fname) break;
  3125. while (*--cp != '/');
  3126. }
  3127. free (fname);
  3128. }
  3129. if (!fp) return;
  3130. while (fgets (line, 128, fp)) {
  3131. cp = strchr (line, '#');
  3132. if (cp) *cp = 0;
  3133. if (sscanf (line, "%d %d %d", &col, &row, &time) != 3) continue;
  3134. if ((unsigned) col >= p->width || (unsigned) row >= p->height) continue;
  3135. if (time > p->timestamp) continue;
  3136. for (tot=n=0, rad=1; rad < 3 && n==0; rad++)
  3137. for (r = row-rad; r <= row+rad; r++)
  3138. for (c = col-rad; c <= col+rad; c++)
  3139. if ((unsigned) r < p->height && (unsigned) c < p->width &&
  3140. (r != row || c != col) && dcr_fc(p,r,c) == dcr_fc(p,row,col)) {
  3141. tot += BAYER2(r,c);
  3142. n++;
  3143. }
  3144. BAYER2(row,col) = tot/n;
  3145. if (p->opt.verbose) {
  3146. if (!fixed++)
  3147. fprintf (stderr,_("Fixed dead pixels at:"));
  3148. fprintf (stderr, " %d,%d", col, row);
  3149. }
  3150. }
  3151. if (fixed) fputc ('\n', stderr);
  3152. fclose (fp);
  3153. }
  3154. void DCR_CLASS dcr_subtract (DCRAW* p,char *fname)
  3155. {
  3156. FILE *fp;
  3157. int dim[3]={0,0,0}, comment=0, number=0, error=0, nd=0, c, row, col;
  3158. ushort *pixel;
  3159. if (!(fp = fopen (fname, "rb"))) {
  3160. perror (fname); return;
  3161. }
  3162. if (fgetc(fp) != 'P' || fgetc(fp) != '5') error = 1;
  3163. while (!error && nd < 3 && (c = fgetc(fp)) != EOF) {
  3164. if (c == '#') comment = 1;
  3165. if (c == '\n') comment = 0;
  3166. if (comment) continue;
  3167. if (isdigit(c)) number = 1;
  3168. if (number) {
  3169. if (isdigit(c)) dim[nd] = dim[nd]*10 + c -'0';
  3170. else if (isspace(c)) {
  3171. number = 0; nd++;
  3172. } else error = 1;
  3173. }
  3174. }
  3175. if (error || nd < 3) {
  3176. fprintf (stderr,_("%s is not a valid PGM file!\n"), fname);
  3177. fclose (fp); return;
  3178. } else if (dim[0] != p->width || dim[1] != p->height || dim[2] != 65535) {
  3179. fprintf (stderr,_("%s has the wrong dimensions!\n"), fname);
  3180. fclose (fp); return;
  3181. }
  3182. pixel = (ushort *) calloc (p->width, sizeof *pixel);
  3183. dcr_merror (p, pixel, "subtract()");
  3184. for (row=0; row < p->height; row++) {
  3185. fread (pixel, 2, p->width, fp);
  3186. for (col=0; col < p->width; col++)
  3187. BAYER(row,col) = MAX (BAYER(row,col) - ntohs(pixel[col]), 0);
  3188. }
  3189. fclose (fp);
  3190. free (pixel);
  3191. p->black = 0;
  3192. }
  3193. void DCR_CLASS dcr_pseudoinverse (double (*in)[3], double (*out)[3], int size)
  3194. {
  3195. double work[3][6], num;
  3196. int i, j, k;
  3197. for (i=0; i < 3; i++) {
  3198. for (j=0; j < 6; j++)
  3199. work[i][j] = j == i+3;
  3200. for (j=0; j < 3; j++)
  3201. for (k=0; k < size; k++)
  3202. work[i][j] += in[k][i] * in[k][j];
  3203. }
  3204. for (i=0; i < 3; i++) {
  3205. num = work[i][i];
  3206. for (j=0; j < 6; j++)
  3207. work[i][j] /= num;
  3208. for (k=0; k < 3; k++) {
  3209. if (k==i) continue;
  3210. num = work[k][i];
  3211. for (j=0; j < 6; j++)
  3212. work[k][j] -= work[i][j] * num;
  3213. }
  3214. }
  3215. for (i=0; i < size; i++)
  3216. for (j=0; j < 3; j++)
  3217. for (out[i][j]=k=0; k < 3; k++)
  3218. out[i][j] += work[j][k+3] * in[i][k];
  3219. }
  3220. void DCR_CLASS dcr_cam_xyz_coeff (DCRAW* p, double cam_xyz[4][3])
  3221. {
  3222. double cam_rgb[4][3], inverse[4][3], num;
  3223. int i, j, k;
  3224. for (i=0; i < p->colors; i++) /* Multiply out XYZ colorspace */
  3225. for (j=0; j < 3; j++)
  3226. for (cam_rgb[i][j] = k=0; k < 3; k++)
  3227. cam_rgb[i][j] += cam_xyz[i][k] * xyz_rgb[k][j];
  3228. for (i=0; i < p->colors; i++) { /* Normalize cam_rgb so that */
  3229. for (num=j=0; j < 3; j++) /* cam_rgb * (1,1,1) is (1,1,1,1) */
  3230. num += cam_rgb[i][j];
  3231. for (j=0; j < 3; j++)
  3232. cam_rgb[i][j] /= num;
  3233. p->pre_mul[i] = 1 / (float)num;
  3234. }
  3235. dcr_pseudoinverse (cam_rgb, inverse, p->colors);
  3236. for (p->raw_color = i=0; i < 3; i++)
  3237. for (j=0; j < p->colors; j++)
  3238. p->rgb_cam[i][j] = (float)inverse[j][i];
  3239. }
  3240. #ifdef COLORCHECK
  3241. void DCR_CLASS dcr_colorcheck(DCRAW* p)
  3242. {
  3243. #define NSQ 24
  3244. // Coordinates of the GretagMacbeth ColorChecker squares
  3245. // p->width, p->height, 1st_column, 1st_row
  3246. static const int cut[NSQ][4] = {
  3247. { 241, 231, 234, 274 },
  3248. { 251, 235, 534, 274 },
  3249. { 255, 239, 838, 272 },
  3250. { 255, 240, 1146, 274 },
  3251. { 251, 237, 1452, 278 },
  3252. { 243, 238, 1758, 288 },
  3253. { 253, 253, 218, 558 },
  3254. { 255, 249, 524, 562 },
  3255. { 261, 253, 830, 562 },
  3256. { 260, 255, 1144, 564 },
  3257. { 261, 255, 1450, 566 },
  3258. { 247, 247, 1764, 576 },
  3259. { 255, 251, 212, 862 },
  3260. { 259, 259, 518, 862 },
  3261. { 263, 261, 826, 864 },
  3262. { 265, 263, 1138, 866 },
  3263. { 265, 257, 1450, 872 },
  3264. { 257, 255, 1762, 874 },
  3265. { 257, 253, 212, 1164 },
  3266. { 262, 251, 516, 1172 },
  3267. { 263, 257, 826, 1172 },
  3268. { 263, 255, 1136, 1176 },
  3269. { 255, 252, 1452, 1182 },
  3270. { 257, 253, 1760, 1180 } };
  3271. // ColorChecker Chart under 6500-kelvin illumination
  3272. static const double gmb_xyY[NSQ][3] = {
  3273. { 0.400, 0.350, 10.1 }, // Dark Skin
  3274. { 0.377, 0.345, 35.8 }, // Light Skin
  3275. { 0.247, 0.251, 19.3 }, // Blue Sky
  3276. { 0.337, 0.422, 13.3 }, // Foliage
  3277. { 0.265, 0.240, 24.3 }, // Blue Flower
  3278. { 0.261, 0.343, 43.1 }, // Bluish Green
  3279. { 0.506, 0.407, 30.1 }, // Orange
  3280. { 0.211, 0.175, 12.0 }, // Purplish Blue
  3281. { 0.453, 0.306, 19.8 }, // Moderate Red
  3282. { 0.285, 0.202, 6.6 }, // Purple
  3283. { 0.380, 0.489, 44.3 }, // Yellow Green
  3284. { 0.473, 0.438, 43.1 }, // Orange Yellow
  3285. { 0.187, 0.129, 6.1 }, // Blue
  3286. { 0.305, 0.478, 23.4 }, // Green
  3287. { 0.539, 0.313, 12.0 }, // Red
  3288. { 0.448, 0.470, 59.1 }, // Yellow
  3289. { 0.364, 0.233, 19.8 }, // Magenta
  3290. { 0.196, 0.252, 19.8 }, // Cyan
  3291. { 0.310, 0.316, 90.0 }, // White
  3292. { 0.310, 0.316, 59.1 }, // Neutral 8
  3293. { 0.310, 0.316, 36.2 }, // Neutral 6.5
  3294. { 0.310, 0.316, 19.8 }, // Neutral 5
  3295. { 0.310, 0.316, 9.0 }, // Neutral 3.5
  3296. { 0.310, 0.316, 3.1 } }; // Black
  3297. double gmb_cam[NSQ][4], gmb_xyz[NSQ][3];
  3298. double inverse[NSQ][3], cam_xyz[4][3], num;
  3299. int c, i, j, k, sq, row, col, count[4];
  3300. memset (gmb_cam, 0, sizeof gmb_cam);
  3301. for (sq=0; sq < NSQ; sq++) {
  3302. FORCC(p) count[c] = 0;
  3303. for (row=cut[sq][3]; row < cut[sq][3]+cut[sq][1]; row++)
  3304. for (col=cut[sq][2]; col < cut[sq][2]+cut[sq][0]; col++) {
  3305. c = FC(row,col);
  3306. if (c >= p->colors) c -= 2;
  3307. gmb_cam[sq][c] += BAYER(row,col);
  3308. count[c]++;
  3309. }
  3310. FORCC(p) gmb_cam[sq][c] = gmb_cam[sq][c]/count[c] - p->black;
  3311. gmb_xyz[sq][0] = gmb_xyY[sq][2] * gmb_xyY[sq][0] / gmb_xyY[sq][1];
  3312. gmb_xyz[sq][1] = gmb_xyY[sq][2];
  3313. gmb_xyz[sq][2] = gmb_xyY[sq][2] *
  3314. (1 - gmb_xyY[sq][0] - gmb_xyY[sq][1]) / gmb_xyY[sq][1];
  3315. }
  3316. dcr_pseudoinverse (gmb_xyz, inverse, NSQ);
  3317. for (i=0; i < p->colors; i++)
  3318. for (j=0; j < 3; j++)
  3319. for (cam_xyz[i][j] = k=0; k < NSQ; k++)
  3320. cam_xyz[i][j] += gmb_cam[k][i] * inverse[k][j];
  3321. dcr_cam_xyz_coeff (p, cam_xyz);
  3322. if (p->opt.verbose) {
  3323. printf (" { \"%s %s\", %d,\n\t{", p->make, p->model, p->black);
  3324. num = 10000 / (cam_xyz[1][0] + cam_xyz[1][1] + cam_xyz[1][2]);
  3325. FORCC(p) for (j=0; j < 3; j++)
  3326. printf ("%c%d", (c | j) ? ',':' ', (int) (cam_xyz[c][j] * num + 0.5));
  3327. puts (" } },");
  3328. }
  3329. #undef NSQ
  3330. }
  3331. #endif
  3332. void DCR_CLASS dcr_hat_transform (float *temp, float *base, int st, int size, int sc)
  3333. {
  3334. int i;
  3335. for (i=0; i < sc; i++)
  3336. temp[i] = 2*base[st*i] + base[st*(sc-i)] + base[st*(i+sc)];
  3337. for (; i+sc < size; i++)
  3338. temp[i] = 2*base[st*i] + base[st*(i-sc)] + base[st*(i+sc)];
  3339. for (; i < size; i++)
  3340. temp[i] = 2*base[st*i] + base[st*(i-sc)] + base[st*(2*size-2-(i+sc))];
  3341. }
  3342. void DCR_CLASS dcr_wavelet_denoise(DCRAW* p)
  3343. {
  3344. float *fimg=0, *temp, thold, mul[2], avg, diff;
  3345. int scale=1, size, lev, hpass, lpass, row, col, nc, c, i, wlast;
  3346. ushort *window[4];
  3347. static const float noise[] =
  3348. { 0.8002f,0.2735f,0.1202f,0.0585f,0.0291f,0.0152f,0.0080f,0.0044f };
  3349. if (p->opt.verbose) fprintf (stderr,_("Wavelet denoising...\n"));
  3350. while (p->maximum << scale < 0x10000) scale++;
  3351. p->maximum <<= --scale;
  3352. p->black <<= scale;
  3353. if ((size = p->iheight*p->iwidth) < 0x15550000)
  3354. fimg = (float *) malloc ((size*3 + p->iheight + p->iwidth) * sizeof *fimg);
  3355. dcr_merror (p, fimg, "wavelet_denoise()");
  3356. temp = fimg + size*3;
  3357. if ((nc = p->colors) == 3 && p->filters) nc++;
  3358. FORC(nc) { /* denoise R,G1,B,G3 individually */
  3359. for (i=0; i < size; i++)
  3360. fimg[i] = 256 * (float)sqrt(p->image[i][c] << scale);
  3361. for (hpass=lev=0; lev < 5; lev++) {
  3362. lpass = size*((lev & 1)+1);
  3363. for (row=0; row < p->iheight; row++) {
  3364. dcr_hat_transform (temp, fimg+hpass+row*p->iwidth, 1, p->iwidth, 1 << lev);
  3365. for (col=0; col < p->iwidth; col++)
  3366. fimg[lpass + row*p->iwidth + col] = temp[col] * 0.25f;
  3367. }
  3368. for (col=0; col < p->iwidth; col++) {
  3369. dcr_hat_transform (temp, fimg+lpass+col, p->iwidth, p->iheight, 1 << lev);
  3370. for (row=0; row < p->iheight; row++)
  3371. fimg[lpass + row*p->iwidth + col] = temp[row] * 0.25f;
  3372. }
  3373. thold = p->opt.threshold * noise[lev];
  3374. for (i=0; i < size; i++) {
  3375. fimg[hpass+i] -= fimg[lpass+i];
  3376. if (fimg[hpass+i] < -thold) fimg[hpass+i] += thold;
  3377. else if (fimg[hpass+i] > thold) fimg[hpass+i] -= thold;
  3378. else fimg[hpass+i] = 0;
  3379. if (hpass) fimg[i] += fimg[hpass+i];
  3380. }
  3381. hpass = lpass;
  3382. }
  3383. for (i=0; i < size; i++)
  3384. p->image[i][c] = (unsigned short)(CLIP(SQR(fimg[i]+fimg[lpass+i])/0x10000));
  3385. }
  3386. if (p->filters && p->colors == 3) { /* pull G1 and G3 closer together */
  3387. for (row=0; row < 2; row++)
  3388. mul[row] = 0.125f * p->pre_mul[FC(row+1,0) | 1] / p->pre_mul[FC(row,0) | 1];
  3389. for (i=0; i < 4; i++)
  3390. window[i] = (ushort *) fimg + p->width*i;
  3391. for (wlast=-1, row=1; row < p->height-1; row++) {
  3392. while (wlast < row+1) {
  3393. for (wlast++, i=0; i < 4; i++)
  3394. window[(i+3) & 3] = window[i];
  3395. for (col = FC(wlast,1) & 1; col < p->width; col+=2)
  3396. window[2][col] = BAYER(wlast,col);
  3397. }
  3398. thold = p->opt.threshold/512;
  3399. for (col = (FC(row,0) & 1)+1; col < p->width-1; col+=2) {
  3400. avg = ( window[0][col-1] + window[0][col+1] +
  3401. window[2][col-1] + window[2][col+1] - p->black*4 )
  3402. * mul[row & 1] + (window[1][col] - p->black) * 0.5f + p->black;
  3403. avg = avg < 0 ? 0 : (float)sqrt(avg);
  3404. diff = (float)sqrt(BAYER(row,col)) - avg;
  3405. if (diff < -thold) diff += thold;
  3406. else if (diff > thold) diff -= thold;
  3407. else diff = 0;
  3408. BAYER(row,col) = (unsigned short)(CLIP(SQR(avg+diff) + 0.5f));
  3409. }
  3410. }
  3411. }
  3412. free (fimg);
  3413. }
  3414. void DCR_CLASS dcr_scale_colors(DCRAW* p)
  3415. {
  3416. unsigned bottom, right, size, row, col, ur, uc, i, x, y, c, sum[8];
  3417. int val, dark, sat;
  3418. double dsum[8], dmin, dmax;
  3419. float scale_mul[4], fr, fc;
  3420. ushort *img=0, *pix;
  3421. if (p->opt.user_mul[0])
  3422. memcpy (p->pre_mul, p->opt.user_mul, sizeof p->pre_mul);
  3423. if (p->opt.use_auto_wb || (p->opt.use_camera_wb && p->cam_mul[0] == -1)) {
  3424. memset (dsum, 0, sizeof dsum);
  3425. bottom = MIN (p->opt.greybox[1]+p->opt.greybox[3], p->height);
  3426. right = MIN (p->opt.greybox[0]+p->opt.greybox[2], p->width);
  3427. for (row=p->opt.greybox[1]; row < bottom; row += 8)
  3428. for (col=p->opt.greybox[0]; col < right; col += 8) {
  3429. memset (sum, 0, sizeof sum);
  3430. for (y=row; y < row+8 && y < bottom; y++)
  3431. for (x=col; x < col+8 && x < right; x++)
  3432. FORC4 {
  3433. if (p->filters) {
  3434. c = FC(y,x);
  3435. val = BAYER(y,x);
  3436. } else
  3437. val = p->image[y*p->width+x][c];
  3438. if (val > (int)p->maximum-25) goto skip_block;
  3439. if ((val -= p->black) < 0) val = 0;
  3440. sum[c] += val;
  3441. sum[c+4]++;
  3442. if (p->filters) break;
  3443. }
  3444. FORC(8) dsum[c] += sum[c];
  3445. skip_block: ;
  3446. }
  3447. FORC4 if (dsum[c]) p->pre_mul[c] = (float)(dsum[c+4] / dsum[c]);
  3448. }
  3449. if (p->opt.use_camera_wb && p->cam_mul[0] != -1) {
  3450. memset (sum, 0, sizeof sum);
  3451. for (row=0; row < 8; row++)
  3452. for (col=0; col < 8; col++) {
  3453. c = FC(row,col);
  3454. if ((val = p->white[row][col] - p->black) > 0)
  3455. sum[c] += val;
  3456. sum[c+4]++;
  3457. }
  3458. if (sum[0] && sum[1] && sum[2] && sum[3])
  3459. FORC4 p->pre_mul[c] = (float) sum[c+4] / sum[c];
  3460. else if (p->cam_mul[0] && p->cam_mul[2])
  3461. memcpy (p->pre_mul, p->cam_mul, sizeof p->pre_mul);
  3462. else
  3463. fprintf (stderr,_("%s: Cannot use camera p->white balance.\n"), p->ifname);
  3464. }
  3465. if (p->pre_mul[3] == 0) p->pre_mul[3] = p->colors < 4 ? p->pre_mul[1] : 1;
  3466. dark = p->black;
  3467. sat = p->maximum;
  3468. if (p->opt.threshold) dcr_wavelet_denoise(p);
  3469. p->maximum -= p->black;
  3470. for (dmin=DBL_MAX, dmax=c=0; c < 4; c++) {
  3471. if (dmin > p->pre_mul[c])
  3472. dmin = p->pre_mul[c];
  3473. if (dmax < p->pre_mul[c])
  3474. dmax = p->pre_mul[c];
  3475. }
  3476. if (!p->opt.highlight) dmax = dmin;
  3477. FORC4 scale_mul[c] = (float)((p->pre_mul[c] /= (float)dmax) * 65535.0f / p->maximum);
  3478. if (p->opt.verbose) {
  3479. fprintf (stderr,_("Scaling with darkness %d, saturation %d, and\nmultipliers"), dark, sat);
  3480. FORC4 fprintf (stderr, " %f", p->pre_mul[c]);
  3481. fputc ('\n', stderr);
  3482. }
  3483. size = p->iheight*p->iwidth;
  3484. for (i=0; i < size*4; i++) {
  3485. val = p->image[0][i];
  3486. if (!val) continue;
  3487. val -= p->black;
  3488. val = (int)(val * scale_mul[i & 3]);
  3489. p->image[0][i] = CLIP(val);
  3490. }
  3491. if ((p->opt.aber[0] != 1 || p->opt.aber[2] != 1) && p->colors == 3) {
  3492. if (p->opt.verbose)
  3493. fprintf (stderr,_("Correcting chromatic aberration...\n"));
  3494. for (c=0; c < 4; c+=2) {
  3495. if (p->opt.aber[c] == 1) continue;
  3496. img = (ushort *) malloc (size * sizeof *img);
  3497. dcr_merror (p, img, "scale_colors()");
  3498. for (i=0; i < size; i++)
  3499. img[i] = p->image[i][c];
  3500. for (row=0; row < p->iheight; row++) {
  3501. fr = (float)((row - p->iheight*0.5) * p->opt.aber[c] + p->iheight*0.5);
  3502. ur = (unsigned int)fr;
  3503. if ((int)ur > p->iheight-2) continue;
  3504. fr -= ur;
  3505. for (col=0; col < p->iwidth; col++) {
  3506. fc = (float)((col - p->iwidth*0.5) * p->opt.aber[c] + p->iwidth*0.5);
  3507. uc = (unsigned int)fc;
  3508. if ((int)uc > p->iwidth-2) continue;
  3509. fc -= uc;
  3510. pix = img + ur*p->iwidth + uc;
  3511. p->image[row*p->iwidth+col][c] = (unsigned short)(
  3512. (pix[ 0]*(1-fc) + pix[ 1]*fc) * (1-fr) +
  3513. (pix[p->iwidth]*(1-fc) + pix[p->iwidth+1]*fc) * fr);
  3514. }
  3515. }
  3516. free(img);
  3517. }
  3518. }
  3519. }
  3520. void DCR_CLASS dcr_pre_interpolate(DCRAW* p)
  3521. {
  3522. ushort (*img)[4];
  3523. int row, col, c;
  3524. if (p->shrink) {
  3525. if (p->opt.half_size) {
  3526. p->height = p->iheight;
  3527. p->width = p->iwidth;
  3528. } else {
  3529. img = (ushort (*)[4]) calloc (p->height*p->width, sizeof *img);
  3530. dcr_merror (p, img, "pre_interpolate()");
  3531. for (row=0; row < p->height; row++)
  3532. for (col=0; col < p->width; col++) {
  3533. c = dcr_fc(p,row,col);
  3534. img[row*p->width+col][c] = p->image[(row >> 1)*p->iwidth+(col >> 1)][c];
  3535. }
  3536. free (p->image);
  3537. p->image = img;
  3538. p->shrink = 0;
  3539. }
  3540. }
  3541. if (p->filters && p->colors == 3) {
  3542. if ((p->mix_green = p->opt.four_color_rgb)) p->colors++;
  3543. else {
  3544. for (row = FC(1,0) >> 1; row < p->height; row+=2)
  3545. for (col = FC(row,1) & 1; col < p->width; col+=2)
  3546. p->image[row*p->width+col][1] = p->image[row*p->width+col][3];
  3547. p->filters &= ~((p->filters & 0x55555555) << 1);
  3548. }
  3549. }
  3550. if (p->opt.half_size) p->filters = 0;
  3551. }
  3552. void DCR_CLASS dcr_border_interpolate (DCRAW* p, int border)
  3553. {
  3554. unsigned row, col, y, x, f, sum[8];
  3555. int c;
  3556. for (row=0; row < p->height; row++)
  3557. for (col=0; col < p->width; col++) {
  3558. if (col==(unsigned int)border && row >= (unsigned int)border && row < p->height-(unsigned int)border)
  3559. col = p->width-border;
  3560. memset (sum, 0, sizeof sum);
  3561. for (y=row-1; y != row+2; y++)
  3562. for (x=col-1; x != col+2; x++)
  3563. if (y < p->height && x < p->width) {
  3564. f = dcr_fc(p,y,x);
  3565. sum[f] += p->image[y*p->width+x][f];
  3566. sum[f+4]++;
  3567. }
  3568. f = dcr_fc(p,row,col);
  3569. FORCC(p) if ((unsigned int)c != f && sum[c+4])
  3570. p->image[row*p->width+col][c] = sum[c] / sum[c+4];
  3571. }
  3572. }
  3573. void DCR_CLASS dcr_lin_interpolate(DCRAW* p)
  3574. {
  3575. int code[16][16][32], *ip, sum[4];
  3576. int c, i, x, y, row, col, shift, color;
  3577. ushort *pix;
  3578. if (p->opt.verbose) fprintf (stderr,_("Bilinear interpolation...\n"));
  3579. dcr_border_interpolate(p,1);
  3580. for (row=0; row < 16; row++)
  3581. for (col=0; col < 16; col++) {
  3582. ip = code[row][col];
  3583. memset (sum, 0, sizeof sum);
  3584. for (y=-1; y <= 1; y++)
  3585. for (x=-1; x <= 1; x++) {
  3586. shift = (y==0) + (x==0);
  3587. if (shift == 2) continue;
  3588. color = dcr_fc(p,row+y,col+x);
  3589. *ip++ = (p->width*y + x)*4 + color;
  3590. *ip++ = shift;
  3591. *ip++ = color;
  3592. sum[color] += 1 << shift;
  3593. }
  3594. FORCC(p)
  3595. if (c != dcr_fc(p,row,col)) {
  3596. *ip++ = c;
  3597. *ip++ = 256 / sum[c];
  3598. }
  3599. }
  3600. for (row=1; row < p->height-1; row++)
  3601. for (col=1; col < p->width-1; col++) {
  3602. pix = p->image[row*p->width+col];
  3603. ip = code[row & 15][col & 15];
  3604. memset (sum, 0, sizeof sum);
  3605. for (i=8; i--; ip+=3)
  3606. sum[ip[2]] += pix[ip[0]] << ip[1];
  3607. for (i=p->colors; --i; ip+=2)
  3608. pix[ip[0]] = sum[ip[0]] * ip[1] >> 8;
  3609. }
  3610. }
  3611. /*
  3612. This algorithm is officially called:
  3613. "Interpolation using a Threshold-based variable number of gradients"
  3614. described in http://scien.stanford.edu/class/psych221/projects/99/tingchen/algodep/vargra.html
  3615. I've extended the basic idea to work with non-Bayer filter arrays.
  3616. Gradients are numbered clockwise from NW=0 to W=7.
  3617. */
  3618. void DCR_CLASS dcr_vng_interpolate(DCRAW* p)
  3619. {
  3620. static const signed char *cp, terms[] = {
  3621. -2,-2,+0,-1,0,0x01, -2,-2,+0,+0,1,0x01, -2,-1,-1,+0,0,0x01,
  3622. -2,-1,+0,-1,0,0x02, -2,-1,+0,+0,0,0x03, -2,-1,+0,+1,1,0x01,
  3623. -2,+0,+0,-1,0,0x06, -2,+0,+0,+0,1,0x02, -2,+0,+0,+1,0,0x03,
  3624. -2,+1,-1,+0,0,0x04, -2,+1,+0,-1,1,0x04, -2,+1,+0,+0,0,0x06,
  3625. -2,+1,+0,+1,0,0x02, -2,+2,+0,+0,1,0x04, -2,+2,+0,+1,0,0x04,
  3626. -1,-2,-1,+0,0,(const signed char)0x80, -1,-2,+0,-1,0,0x01, -1,-2,+1,-1,0,0x01,
  3627. -1,-2,+1,+0,1,0x01, -1,-1,-1,+1,0,(const signed char)0x88, -1,-1,+1,-2,0,0x40,
  3628. -1,-1,+1,-1,0,0x22, -1,-1,+1,+0,0,0x33, -1,-1,+1,+1,1,0x11,
  3629. -1,+0,-1,+2,0,0x08, -1,+0,+0,-1,0,0x44, -1,+0,+0,+1,0,0x11,
  3630. -1,+0,+1,-2,1,0x40, -1,+0,+1,-1,0,0x66, -1,+0,+1,+0,1,0x22,
  3631. -1,+0,+1,+1,0,0x33, -1,+0,+1,+2,1,0x10, -1,+1,+1,-1,1,0x44,
  3632. -1,+1,+1,+0,0,0x66, -1,+1,+1,+1,0,0x22, -1,+1,+1,+2,0,0x10,
  3633. -1,+2,+0,+1,0,0x04, -1,+2,+1,+0,1,0x04, -1,+2,+1,+1,0,0x04,
  3634. +0,-2,+0,+0,1,(const signed char)0x80, +0,-1,+0,+1,1,(const signed char)0x88, +0,-1,+1,-2,0,0x40,
  3635. +0,-1,+1,+0,0,0x11, +0,-1,+2,-2,0,0x40, +0,-1,+2,-1,0,0x20,
  3636. +0,-1,+2,+0,0,0x30, +0,-1,+2,+1,1,0x10, +0,+0,+0,+2,1,0x08,
  3637. +0,+0,+2,-2,1,0x40, +0,+0,+2,-1,0,0x60, +0,+0,+2,+0,1,0x20,
  3638. +0,+0,+2,+1,0,0x30, +0,+0,+2,+2,1,0x10, +0,+1,+1,+0,0,0x44,
  3639. +0,+1,+1,+2,0,0x10, +0,+1,+2,-1,1,0x40, +0,+1,+2,+0,0,0x60,
  3640. +0,+1,+2,+1,0,0x20, +0,+1,+2,+2,0,0x10, +1,-2,+1,+0,0,(const signed char)0x80,
  3641. +1,-1,+1,+1,0,(const signed char)0x88, +1,+0,+1,+2,0,0x08, +1,+0,+2,-1,0,0x40,
  3642. +1,+0,+2,+1,0,0x10
  3643. }, chood[] = { -1,-1, -1,0, -1,+1, 0,+1, +1,+1, +1,0, +1,-1, 0,-1 };
  3644. ushort (*brow[5])[4], *pix;
  3645. int prow=7, pcol=1, *ip, *code[16][16], gval[8], gmin, gmax, sum[4];
  3646. int row, col, x, y, x1, x2, y1, y2, t, weight, grads, color, diag;
  3647. int g, diff, thold, num, c;
  3648. dcr_lin_interpolate(p);
  3649. if (p->opt.verbose) fprintf (stderr,_("VNG interpolation...\n"));
  3650. if (p->filters == 1) prow = pcol = 15;
  3651. ip = (int *) calloc ((prow+1)*(pcol+1), 1280);
  3652. dcr_merror (p, ip, "vng_interpolate()");
  3653. for (row=0; row <= prow; row++) /* Precalculate for VNG */
  3654. for (col=0; col <= pcol; col++) {
  3655. code[row][col] = ip;
  3656. for (cp=terms, t=0; t < 64; t++) {
  3657. y1 = *cp++; x1 = *cp++;
  3658. y2 = *cp++; x2 = *cp++;
  3659. weight = *cp++;
  3660. grads = *cp++;
  3661. color = dcr_fc(p, row+y1,col+x1);
  3662. if (dcr_fc(p, row+y2,col+x2) != color) continue;
  3663. diag = (dcr_fc(p, row,col+1) == color && dcr_fc(p, row+1,col) == color) ? 2:1;
  3664. if (abs(y1-y2) == diag && abs(x1-x2) == diag) continue;
  3665. *ip++ = (y1*p->width + x1)*4 + color;
  3666. *ip++ = (y2*p->width + x2)*4 + color;
  3667. *ip++ = weight;
  3668. for (g=0; g < 8; g++)
  3669. if (grads & 1<<g) *ip++ = g;
  3670. *ip++ = -1;
  3671. }
  3672. *ip++ = INT_MAX;
  3673. for (cp=chood, g=0; g < 8; g++) {
  3674. y = *cp++; x = *cp++;
  3675. *ip++ = (y*p->width + x) * 4;
  3676. color = dcr_fc(p, row,col);
  3677. if (dcr_fc(p, row+y,col+x) != color && dcr_fc(p, row+y*2,col+x*2) == color)
  3678. *ip++ = (y*p->width + x) * 8 + color;
  3679. else
  3680. *ip++ = 0;
  3681. }
  3682. }
  3683. brow[4] = (ushort (*)[4]) calloc (p->width*3, sizeof **brow);
  3684. dcr_merror (p, brow[4], "vng_interpolate()");
  3685. for (row=0; row < 3; row++)
  3686. brow[row] = brow[4] + row*p->width;
  3687. for (row=2; row < p->height-2; row++) { /* Do VNG interpolation */
  3688. for (col=2; col < p->width-2; col++) {
  3689. pix = p->image[row*p->width+col];
  3690. ip = code[row & prow][col & pcol];
  3691. memset (gval, 0, sizeof gval);
  3692. while ((g = ip[0]) != INT_MAX) { /* Calculate gradients */
  3693. diff = ABS(pix[g] - pix[ip[1]]) << ip[2];
  3694. gval[ip[3]] += diff;
  3695. ip += 5;
  3696. if ((g = ip[-1]) == -1) continue;
  3697. gval[g] += diff;
  3698. while ((g = *ip++) != -1)
  3699. gval[g] += diff;
  3700. }
  3701. ip++;
  3702. gmin = gmax = gval[0]; /* Choose a p->opt.threshold */
  3703. for (g=1; g < 8; g++) {
  3704. if (gmin > gval[g]) gmin = gval[g];
  3705. if (gmax < gval[g]) gmax = gval[g];
  3706. }
  3707. if (gmax == 0) {
  3708. memcpy (brow[2][col], pix, sizeof *p->image);
  3709. continue;
  3710. }
  3711. thold = gmin + (gmax >> 1);
  3712. memset (sum, 0, sizeof sum);
  3713. color = dcr_fc(p, row,col);
  3714. for (num=g=0; g < 8; g++,ip+=2) { /* Average the neighbors */
  3715. if (gval[g] <= thold) {
  3716. FORCC(p)
  3717. if (c == color && ip[1])
  3718. sum[c] += (pix[c] + pix[ip[1]]) >> 1;
  3719. else
  3720. sum[c] += pix[ip[0] + c];
  3721. num++;
  3722. }
  3723. }
  3724. FORCC(p) { /* Save to buffer */
  3725. t = pix[color];
  3726. if (c != color)
  3727. t += (sum[c] - sum[color]) / num;
  3728. brow[2][col][c] = CLIP(t);
  3729. }
  3730. }
  3731. if (row > 3) /* Write buffer to image */
  3732. memcpy (p->image[(row-2)*p->width+2], brow[0]+2, (p->width-4)*sizeof *p->image);
  3733. for (g=0; g < 4; g++)
  3734. brow[(g-1) & 3] = brow[g];
  3735. }
  3736. memcpy (p->image[(row-2)*p->width+2], brow[0]+2, (p->width-4)*sizeof *p->image);
  3737. memcpy (p->image[(row-1)*p->width+2], brow[1]+2, (p->width-4)*sizeof *p->image);
  3738. free (brow[4]);
  3739. free (code[0][0]);
  3740. }
  3741. /*
  3742. Patterned Pixel Grouping Interpolation by Alain Desbiolles
  3743. */
  3744. void DCR_CLASS dcr_ppg_interpolate(DCRAW* p)
  3745. {
  3746. int dir[5] = { 1, p->width, -1, -p->width, 1 };
  3747. int row, col, diff[2], guess[2], c, d, i;
  3748. ushort (*pix)[4];
  3749. dcr_border_interpolate(p,3);
  3750. if (p->opt.verbose) fprintf (stderr,_("PPG interpolation...\n"));
  3751. /* Fill in the green layer with gradients and pattern recognition: */
  3752. for (row=3; row < p->height-3; row++)
  3753. for (col=3+(FC(row,3) & 1), c=FC(row,col); col < p->width-3; col+=2) {
  3754. pix = p->image + row*p->width+col;
  3755. for (i=0; (d=dir[i]) > 0; i++) {
  3756. guess[i] = (pix[-d][1] + pix[0][c] + pix[d][1]) * 2
  3757. - pix[-2*d][c] - pix[2*d][c];
  3758. diff[i] = ( ABS(pix[-2*d][c] - pix[ 0][c]) +
  3759. ABS(pix[ 2*d][c] - pix[ 0][c]) +
  3760. ABS(pix[ -d][1] - pix[ d][1]) ) * 3 +
  3761. ( ABS(pix[ 3*d][1] - pix[ d][1]) +
  3762. ABS(pix[-3*d][1] - pix[-d][1]) ) * 2;
  3763. }
  3764. d = dir[i = diff[0] > diff[1]];
  3765. pix[0][1] = ULIM(guess[i] >> 2, pix[d][1], pix[-d][1]);
  3766. }
  3767. /* Calculate red and blue for each green pixel: */
  3768. for (row=1; row < p->height-1; row++)
  3769. for (col=1+(FC(row,2) & 1), c=FC(row,col+1); col < p->width-1; col+=2) {
  3770. pix = p->image + row*p->width+col;
  3771. for (i=0; (d=dir[i]) > 0; c=2-c, i++)
  3772. pix[0][c] = CLIP((pix[-d][c] + pix[d][c] + 2*pix[0][1]
  3773. - pix[-d][1] - pix[d][1]) >> 1);
  3774. }
  3775. /* Calculate blue for red pixels and vice versa: */
  3776. for (row=1; row < p->height-1; row++)
  3777. for (col=1+(FC(row,1) & 1), c=2-FC(row,col); col < p->width-1; col+=2) {
  3778. pix = p->image + row*p->width+col;
  3779. for (i=0; (d=dir[i]+dir[i+1]) > 0; i++) {
  3780. diff[i] = ABS(pix[-d][c] - pix[d][c]) +
  3781. ABS(pix[-d][1] - pix[0][1]) +
  3782. ABS(pix[ d][1] - pix[0][1]);
  3783. guess[i] = pix[-d][c] + pix[d][c] + 2*pix[0][1]
  3784. - pix[-d][1] - pix[d][1];
  3785. }
  3786. if (diff[0] != diff[1])
  3787. pix[0][c] = CLIP(guess[diff[0] > diff[1]] >> 1);
  3788. else
  3789. pix[0][c] = CLIP((guess[0]+guess[1]) >> 2);
  3790. }
  3791. }
  3792. /*
  3793. Adaptive Homogeneity-Directed interpolation is based on
  3794. the work of Keigo Hirakawa, Thomas Parks, and Paul Lee.
  3795. */
  3796. #define TS 256 /* Tile Size */
  3797. void DCR_CLASS dcr_ahd_interpolate(DCRAW* p)
  3798. {
  3799. int i, j, k, top, left, row, col, tr, tc, c, d, val, hm[2];
  3800. ushort (*pix)[4], (*rix)[3];
  3801. static const int dir[4] = { -1, 1, -TS, TS };
  3802. unsigned ldiff[2][4], abdiff[2][4], leps, abeps;
  3803. float r, cbrt[0x10000], xyz[3], xyz_cam[3][4];
  3804. ushort (*rgb)[TS][TS][3];
  3805. short (*lab)[TS][TS][3], (*lix)[3];
  3806. char (*homo)[TS][TS], *buffer;
  3807. if (p->opt.verbose) fprintf (stderr,_("AHD interpolation...\n"));
  3808. for (i=0; i < 0x10000; i++) {
  3809. r = i / 65535.0f;
  3810. cbrt[i] = r > 0.008856f ? (float)pow(r,1/3.0f) : 7.787f*r + 16/116.0f;
  3811. }
  3812. for (i=0; i < 3; i++)
  3813. for (j=0; j < p->colors; j++)
  3814. for (xyz_cam[i][j] =0, k=0; k < 3; k++)
  3815. xyz_cam[i][j] += (float)(xyz_rgb[i][k] * p->rgb_cam[k][j] / d65_white[i]);
  3816. dcr_border_interpolate(p,5);
  3817. buffer = (char *) malloc (26*TS*TS); /* 1664 kB */
  3818. dcr_merror (p, buffer, "ahd_interpolate()");
  3819. rgb = (ushort(*)[TS][TS][3]) buffer;
  3820. lab = (short (*)[TS][TS][3])(buffer + 12*TS*TS);
  3821. homo = (char (*)[TS][TS]) (buffer + 24*TS*TS);
  3822. for (top=2; top < p->height-5; top += TS-6)
  3823. for (left=2; left < p->width-5; left += TS-6) {
  3824. /* Interpolate green horizontally and vertically: */
  3825. for (row = top; row < top+TS && row < p->height-2; row++) {
  3826. col = left + (FC(row,left) & 1);
  3827. for (c = FC(row,col); col < left+TS && col < p->width-2; col+=2) {
  3828. pix = p->image + row*p->width+col;
  3829. val = ((pix[-1][1] + pix[0][c] + pix[1][1]) * 2
  3830. - pix[-2][c] - pix[2][c]) >> 2;
  3831. rgb[0][row-top][col-left][1] = ULIM(val,pix[-1][1],pix[1][1]);
  3832. val = ((pix[-p->width][1] + pix[0][c] + pix[p->width][1]) * 2
  3833. - pix[-2*p->width][c] - pix[2*p->width][c]) >> 2;
  3834. rgb[1][row-top][col-left][1] = ULIM(val,pix[-p->width][1],pix[p->width][1]);
  3835. }
  3836. }
  3837. /* Interpolate red and blue, and convert to CIELab: */
  3838. for (d=0; d < 2; d++)
  3839. for (row=top+1; row < top+TS-1 && row < p->height-3; row++)
  3840. for (col=left+1; col < left+TS-1 && col < p->width-3; col++) {
  3841. pix = p->image + row*p->width+col;
  3842. rix = &rgb[d][row-top][col-left];
  3843. lix = &lab[d][row-top][col-left];
  3844. if ((c = 2 - FC(row,col)) == 1) {
  3845. c = FC(row+1,col);
  3846. val = pix[0][1] + (( pix[-1][2-c] + pix[1][2-c]
  3847. - rix[-1][1] - rix[1][1] ) >> 1);
  3848. rix[0][2-c] = CLIP(val);
  3849. val = pix[0][1] + (( pix[-p->width][c] + pix[p->width][c]
  3850. - rix[-TS][1] - rix[TS][1] ) >> 1);
  3851. } else
  3852. val = rix[0][1] + (( pix[-p->width-1][c] + pix[-p->width+1][c]
  3853. + pix[+p->width-1][c] + pix[+p->width+1][c]
  3854. - rix[-TS-1][1] - rix[-TS+1][1]
  3855. - rix[+TS-1][1] - rix[+TS+1][1] + 1) >> 2);
  3856. rix[0][c] = CLIP(val);
  3857. c = FC(row,col);
  3858. rix[0][c] = pix[0][c];
  3859. xyz[0] = xyz[1] = xyz[2] = 0.5;
  3860. FORCC(p) {
  3861. xyz[0] += xyz_cam[0][c] * rix[0][c];
  3862. xyz[1] += xyz_cam[1][c] * rix[0][c];
  3863. xyz[2] += xyz_cam[2][c] * rix[0][c];
  3864. }
  3865. xyz[0] = cbrt[CLIP((int) xyz[0])];
  3866. xyz[1] = cbrt[CLIP((int) xyz[1])];
  3867. xyz[2] = cbrt[CLIP((int) xyz[2])];
  3868. lix[0][0] = (short)(64 * (116 * xyz[1] - 16));
  3869. lix[0][1] = (short)(64 * 500 * (xyz[0] - xyz[1]));
  3870. lix[0][2] = (short)(64 * 200 * (xyz[1] - xyz[2]));
  3871. }
  3872. /* Build homogeneity maps from the CIELab images: */
  3873. memset (homo, 0, 2*TS*TS);
  3874. for (row=top+2; row < top+TS-2 && row < p->height-4; row++) {
  3875. tr = row-top;
  3876. for (col=left+2; col < left+TS-2 && col < p->width-4; col++) {
  3877. tc = col-left;
  3878. for (d=0; d < 2; d++) {
  3879. lix = &lab[d][tr][tc];
  3880. for (i=0; i < 4; i++) {
  3881. ldiff[d][i] = ABS(lix[0][0]-lix[dir[i]][0]);
  3882. abdiff[d][i] = SQR(lix[0][1]-lix[dir[i]][1])
  3883. + SQR(lix[0][2]-lix[dir[i]][2]);
  3884. }
  3885. }
  3886. leps = MIN(MAX(ldiff[0][0],ldiff[0][1]),
  3887. MAX(ldiff[1][2],ldiff[1][3]));
  3888. abeps = MIN(MAX(abdiff[0][0],abdiff[0][1]),
  3889. MAX(abdiff[1][2],abdiff[1][3]));
  3890. for (d=0; d < 2; d++)
  3891. for (i=0; i < 4; i++)
  3892. if (ldiff[d][i] <= leps && abdiff[d][i] <= abeps)
  3893. homo[d][tr][tc]++;
  3894. }
  3895. }
  3896. /* Combine the most homogenous pixels for the final result: */
  3897. for (row=top+3; row < top+TS-3 && row < p->height-5; row++) {
  3898. tr = row-top;
  3899. for (col=left+3; col < left+TS-3 && col < p->width-5; col++) {
  3900. tc = col-left;
  3901. for (d=0; d < 2; d++)
  3902. for (hm[d]=0, i=tr-1; i <= tr+1; i++)
  3903. for (j=tc-1; j <= tc+1; j++)
  3904. hm[d] += homo[d][i][j];
  3905. if (hm[0] != hm[1])
  3906. FORC3 p->image[row*p->width+col][c] = rgb[hm[1] > hm[0]][tr][tc][c];
  3907. else
  3908. FORC3 p->image[row*p->width+col][c] =
  3909. (rgb[0][tr][tc][c] + rgb[1][tr][tc][c]) >> 1;
  3910. }
  3911. }
  3912. }
  3913. free (buffer);
  3914. }
  3915. #undef TS
  3916. void DCR_CLASS dcr_median_filter(DCRAW* p)
  3917. {
  3918. ushort (*pix)[4];
  3919. int pass, c, i, j, k, med[9];
  3920. static const uchar opt[] = /* Optimal 9-element median search */
  3921. { 1,2, 4,5, 7,8, 0,1, 3,4, 6,7, 1,2, 4,5, 7,8,
  3922. 0,3, 5,8, 4,7, 3,6, 1,4, 2,5, 4,7, 4,2, 6,4, 4,2 };
  3923. for (pass=1; pass <= p->opt.med_passes; pass++) {
  3924. if (p->opt.verbose)
  3925. fprintf (stderr,_("Median filter pass %d...\n"), pass);
  3926. for (c=0; c < 3; c+=2) {
  3927. for (pix = p->image; pix < p->image+p->width*p->height; pix++)
  3928. pix[0][3] = pix[0][c];
  3929. for (pix = p->image+p->width; pix < p->image+p->width*(p->height-1); pix++) {
  3930. if ((pix-p->image+1) % p->width < 2) continue;
  3931. for (k=0, i = -p->width; i <= p->width; i += p->width)
  3932. for (j = i-1; j <= i+1; j++)
  3933. med[k++] = pix[j][3] - pix[j][1];
  3934. for (i=0; i < sizeof opt; i+=2)
  3935. if (med[opt[i]] > med[opt[i+1]])
  3936. SWAP (med[opt[i]] , med[opt[i+1]]);
  3937. pix[0][c] = CLIP(med[4] + pix[0][1]);
  3938. }
  3939. }
  3940. }
  3941. }
  3942. void DCR_CLASS dcr_blend_highlights(DCRAW* p)
  3943. {
  3944. int clip=INT_MAX, row, col, c, i, j;
  3945. static const float trans[2][4][4] =
  3946. { { { 1,1,1 }, { 1.7320508f,-1.7320508f,0 }, { -1,-1,2 } },
  3947. { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } };
  3948. static const float itrans[2][4][4] =
  3949. { { { 1,0.8660254f,-0.5f }, { 1,-0.8660254f,-0.5f }, { 1,0,1 } },
  3950. { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } };
  3951. float cam[2][4], lab[2][4], sum[2], chratio;
  3952. if ((unsigned) (p->colors-3) > 1) return;
  3953. if (p->opt.verbose) fprintf (stderr,_("Blending highlights...\n"));
  3954. FORCC(p) if (clip > (i = (int)(65535*p->pre_mul[c]))) clip = i;
  3955. for (row=0; row < p->height; row++)
  3956. for (col=0; col < p->width; col++) {
  3957. FORCC(p) if (p->image[row*p->width+col][c] > clip) break;
  3958. if (c == p->colors) continue;
  3959. FORCC(p) {
  3960. cam[0][c] = p->image[row*p->width+col][c];
  3961. cam[1][c] = MIN(cam[0][c],clip);
  3962. }
  3963. for (i=0; i < 2; i++) {
  3964. FORCC(p) for (lab[i][c]=0, j=0; j < p->colors; j++)
  3965. lab[i][c] += trans[p->colors-3][c][j] * cam[i][j];
  3966. for (sum[i]=0,c=1; c < p->colors; c++)
  3967. sum[i] += SQR(lab[i][c]);
  3968. }
  3969. chratio = (float)sqrt(sum[1]/sum[0]);
  3970. for (c=1; c < p->colors; c++)
  3971. lab[0][c] *= chratio;
  3972. FORCC(p) for (cam[0][c]=0, j=0; j < p->colors; j++)
  3973. cam[0][c] += itrans[p->colors-3][c][j] * lab[0][j];
  3974. FORCC(p) p->image[row*p->width+col][c] = (unsigned short)(cam[0][c] / p->colors);
  3975. }
  3976. }
  3977. #define SCALE (4 >> p->shrink)
  3978. void DCR_CLASS dcr_recover_highlights(DCRAW* p)
  3979. {
  3980. float *map, sum, wgt, grow;
  3981. int hsat[4], count, spread, change, val, i, c;
  3982. unsigned high, wide, mrow, mcol, row, col, kc, d, y, x;
  3983. ushort *pixel;
  3984. static const signed char dir[8][2] =
  3985. { {-1,-1}, {-1,0}, {-1,1}, {0,1}, {1,1}, {1,0}, {1,-1}, {0,-1} };
  3986. if (p->opt.verbose) fprintf (stderr,_("Rebuilding highlights...\n"));
  3987. grow = (float)pow (2, 4-p->opt.highlight);
  3988. FORCC(p) hsat[c] = (int)(32000 * p->pre_mul[c]);
  3989. for (kc=0, c=1; (int)c < p->colors; c++)
  3990. if (p->pre_mul[kc] < p->pre_mul[c]) kc = c;
  3991. high = p->height / SCALE;
  3992. wide = p->width / SCALE;
  3993. map = (float *) calloc (high*wide, sizeof *map);
  3994. dcr_merror (p, map, "recover_highlights()");
  3995. FORCC(p) if ((unsigned int)c != kc) {
  3996. memset (map, 0, high*wide*sizeof *map);
  3997. for (mrow=0; mrow < high; mrow++)
  3998. for (mcol=0; mcol < wide; mcol++) {
  3999. sum = wgt = 0;
  4000. count = 0;
  4001. for (row = mrow*SCALE; row < (mrow+1)*SCALE; row++)
  4002. for (col = mcol*SCALE; col < (mcol+1)*SCALE; col++) {
  4003. pixel = p->image[row*p->width+col];
  4004. if (pixel[c] / hsat[c] == 1 && pixel[kc] > 24000) {
  4005. sum += pixel[c];
  4006. wgt += pixel[kc];
  4007. count++;
  4008. }
  4009. }
  4010. if (count == SCALE*SCALE)
  4011. map[mrow*wide+mcol] = sum / wgt;
  4012. }
  4013. for (spread = (int)(32/grow); spread--; ) {
  4014. for (mrow=0; mrow < high; mrow++)
  4015. for (mcol=0; mcol < wide; mcol++) {
  4016. if (map[mrow*wide+mcol]) continue;
  4017. sum = 0;
  4018. count = 0;
  4019. for (d=0; d < 8; d++) {
  4020. y = mrow + dir[d][0];
  4021. x = mcol + dir[d][1];
  4022. if (y < high && x < wide && map[y*wide+x] > 0) {
  4023. sum += (1 + (d & 1)) * map[y*wide+x];
  4024. count += 1 + (d & 1);
  4025. }
  4026. }
  4027. if (count > 3)
  4028. map[mrow*wide+mcol] = - (sum+grow) / (count+grow);
  4029. }
  4030. for (change=i=0; i < (int)(high*wide); i++)
  4031. if (map[i] < 0) {
  4032. map[i] = -map[i];
  4033. change = 1;
  4034. }
  4035. if (!change) break;
  4036. }
  4037. for (i=0; i < (int)(high*wide); i++)
  4038. if (map[i] == 0) map[i] = 1;
  4039. for (mrow=0; mrow < high; mrow++)
  4040. for (mcol=0; mcol < wide; mcol++) {
  4041. for (row = mrow*SCALE; row < (mrow+1)*SCALE; row++)
  4042. for (col = mcol*SCALE; col < (mcol+1)*SCALE; col++) {
  4043. pixel = p->image[row*p->width+col];
  4044. if (pixel[c] / hsat[c] > 1) {
  4045. val = (int)(pixel[kc] * map[mrow*wide+mcol]);
  4046. if (pixel[c] < val) pixel[c] = CLIP(val);
  4047. }
  4048. }
  4049. }
  4050. }
  4051. free (map);
  4052. }
  4053. #undef SCALE
  4054. void DCR_CLASS dcr_tiff_get (DCRAW* p, unsigned base,
  4055. unsigned *tag, unsigned *type, unsigned *len, unsigned *save)
  4056. {
  4057. *tag = dcr_get2(p);
  4058. *type = dcr_get2(p);
  4059. *len = dcr_get4(p);
  4060. *save = dcr_ftell(p->obj_) + 4;
  4061. if (*len * ("11124811248488"[*type < 14 ? *type:0]-'0') > 4)
  4062. dcr_fseek(p->obj_, dcr_get4(p)+base, SEEK_SET);
  4063. }
  4064. void DCR_CLASS dcr_parse_thumb_note (DCRAW* p, int base, unsigned toff, unsigned tlen)
  4065. {
  4066. unsigned entries, tag, type, len, save;
  4067. entries = dcr_get2(p);
  4068. while (entries--) {
  4069. dcr_tiff_get (p, base, &tag, &type, &len, &save);
  4070. if (tag == toff) p->thumb_offset = dcr_get4(p)+base;
  4071. if (tag == tlen) p->thumb_length = dcr_get4(p);
  4072. dcr_fseek(p->obj_, save, SEEK_SET);
  4073. }
  4074. }
  4075. int DCR_CLASS dcr_parse_tiff_ifd (DCRAW* p, int base);
  4076. void DCR_CLASS dcr_parse_makernote (DCRAW* p, int base, int uptag)
  4077. {
  4078. static const uchar xlat[2][256] = {
  4079. { 0xc1,0xbf,0x6d,0x0d,0x59,0xc5,0x13,0x9d,0x83,0x61,0x6b,0x4f,0xc7,0x7f,0x3d,0x3d,
  4080. 0x53,0x59,0xe3,0xc7,0xe9,0x2f,0x95,0xa7,0x95,0x1f,0xdf,0x7f,0x2b,0x29,0xc7,0x0d,
  4081. 0xdf,0x07,0xef,0x71,0x89,0x3d,0x13,0x3d,0x3b,0x13,0xfb,0x0d,0x89,0xc1,0x65,0x1f,
  4082. 0xb3,0x0d,0x6b,0x29,0xe3,0xfb,0xef,0xa3,0x6b,0x47,0x7f,0x95,0x35,0xa7,0x47,0x4f,
  4083. 0xc7,0xf1,0x59,0x95,0x35,0x11,0x29,0x61,0xf1,0x3d,0xb3,0x2b,0x0d,0x43,0x89,0xc1,
  4084. 0x9d,0x9d,0x89,0x65,0xf1,0xe9,0xdf,0xbf,0x3d,0x7f,0x53,0x97,0xe5,0xe9,0x95,0x17,
  4085. 0x1d,0x3d,0x8b,0xfb,0xc7,0xe3,0x67,0xa7,0x07,0xf1,0x71,0xa7,0x53,0xb5,0x29,0x89,
  4086. 0xe5,0x2b,0xa7,0x17,0x29,0xe9,0x4f,0xc5,0x65,0x6d,0x6b,0xef,0x0d,0x89,0x49,0x2f,
  4087. 0xb3,0x43,0x53,0x65,0x1d,0x49,0xa3,0x13,0x89,0x59,0xef,0x6b,0xef,0x65,0x1d,0x0b,
  4088. 0x59,0x13,0xe3,0x4f,0x9d,0xb3,0x29,0x43,0x2b,0x07,0x1d,0x95,0x59,0x59,0x47,0xfb,
  4089. 0xe5,0xe9,0x61,0x47,0x2f,0x35,0x7f,0x17,0x7f,0xef,0x7f,0x95,0x95,0x71,0xd3,0xa3,
  4090. 0x0b,0x71,0xa3,0xad,0x0b,0x3b,0xb5,0xfb,0xa3,0xbf,0x4f,0x83,0x1d,0xad,0xe9,0x2f,
  4091. 0x71,0x65,0xa3,0xe5,0x07,0x35,0x3d,0x0d,0xb5,0xe9,0xe5,0x47,0x3b,0x9d,0xef,0x35,
  4092. 0xa3,0xbf,0xb3,0xdf,0x53,0xd3,0x97,0x53,0x49,0x71,0x07,0x35,0x61,0x71,0x2f,0x43,
  4093. 0x2f,0x11,0xdf,0x17,0x97,0xfb,0x95,0x3b,0x7f,0x6b,0xd3,0x25,0xbf,0xad,0xc7,0xc5,
  4094. 0xc5,0xb5,0x8b,0xef,0x2f,0xd3,0x07,0x6b,0x25,0x49,0x95,0x25,0x49,0x6d,0x71,0xc7 },
  4095. { 0xa7,0xbc,0xc9,0xad,0x91,0xdf,0x85,0xe5,0xd4,0x78,0xd5,0x17,0x46,0x7c,0x29,0x4c,
  4096. 0x4d,0x03,0xe9,0x25,0x68,0x11,0x86,0xb3,0xbd,0xf7,0x6f,0x61,0x22,0xa2,0x26,0x34,
  4097. 0x2a,0xbe,0x1e,0x46,0x14,0x68,0x9d,0x44,0x18,0xc2,0x40,0xf4,0x7e,0x5f,0x1b,0xad,
  4098. 0x0b,0x94,0xb6,0x67,0xb4,0x0b,0xe1,0xea,0x95,0x9c,0x66,0xdc,0xe7,0x5d,0x6c,0x05,
  4099. 0xda,0xd5,0xdf,0x7a,0xef,0xf6,0xdb,0x1f,0x82,0x4c,0xc0,0x68,0x47,0xa1,0xbd,0xee,
  4100. 0x39,0x50,0x56,0x4a,0xdd,0xdf,0xa5,0xf8,0xc6,0xda,0xca,0x90,0xca,0x01,0x42,0x9d,
  4101. 0x8b,0x0c,0x73,0x43,0x75,0x05,0x94,0xde,0x24,0xb3,0x80,0x34,0xe5,0x2c,0xdc,0x9b,
  4102. 0x3f,0xca,0x33,0x45,0xd0,0xdb,0x5f,0xf5,0x52,0xc3,0x21,0xda,0xe2,0x22,0x72,0x6b,
  4103. 0x3e,0xd0,0x5b,0xa8,0x87,0x8c,0x06,0x5d,0x0f,0xdd,0x09,0x19,0x93,0xd0,0xb9,0xfc,
  4104. 0x8b,0x0f,0x84,0x60,0x33,0x1c,0x9b,0x45,0xf1,0xf0,0xa3,0x94,0x3a,0x12,0x77,0x33,
  4105. 0x4d,0x44,0x78,0x28,0x3c,0x9e,0xfd,0x65,0x57,0x16,0x94,0x6b,0xfb,0x59,0xd0,0xc8,
  4106. 0x22,0x36,0xdb,0xd2,0x63,0x98,0x43,0xa1,0x04,0x87,0x86,0xf7,0xa6,0x26,0xbb,0xd6,
  4107. 0x59,0x4d,0xbf,0x6a,0x2e,0xaa,0x2b,0xef,0xe6,0x78,0xb6,0x4e,0xe0,0x2f,0xdc,0x7c,
  4108. 0xbe,0x57,0x19,0x32,0x7e,0x2a,0xd0,0xb8,0xba,0x29,0x00,0x3c,0x52,0x7d,0xa8,0x49,
  4109. 0x3b,0x2d,0xeb,0x25,0x49,0xfa,0xa3,0xaa,0x39,0xa7,0xc5,0xa7,0x50,0x11,0x36,0xfb,
  4110. 0xc6,0x67,0x4a,0xf5,0xa5,0x12,0x65,0x7e,0xb0,0xdf,0xaf,0x4e,0xb3,0x61,0x7f,0x2f } };
  4111. unsigned offset=0, entries, tag, type, len, save, c;
  4112. unsigned ver97=0, serial=0, i, wbi=0, wb[4]={0,0,0,0};
  4113. uchar buf97[324], ci, cj, ck;
  4114. short sorder=p->order;
  4115. char buf[10];
  4116. /*
  4117. The MakerNote might have its own TIFF header (possibly with
  4118. its own byte-order!), or it might just be a table.
  4119. */
  4120. dcr_fread(p->obj_, buf, 1, 10);
  4121. if (!strncmp (buf,"KDK" ,3) || /* these aren't TIFF tables */
  4122. !strncmp (buf,"VER" ,3) ||
  4123. !strncmp (buf,"IIII",4) ||
  4124. !strncmp (buf,"MMMM",4)) return;
  4125. if (!strncmp (buf,"KC" ,2) || /* Konica KD-400Z, KD-510Z */
  4126. !strncmp (buf,"MLY" ,3)) { /* Minolta DiMAGE G series */
  4127. p->order = 0x4d4d;
  4128. while ((long)(i=dcr_ftell(p->obj_)) < p->data_offset && i < 16384) {
  4129. wb[0] = wb[2]; wb[2] = wb[1]; wb[1] = wb[3];
  4130. wb[3] = dcr_get2(p);
  4131. if (wb[1] == 256 && wb[3] == 256 &&
  4132. wb[0] > 256 && wb[0] < 640 && wb[2] > 256 && wb[2] < 640)
  4133. FORC4 p->cam_mul[c] = (float)wb[c];
  4134. }
  4135. goto quit;
  4136. }
  4137. if (!strcmp (buf,"Nikon")) {
  4138. base = dcr_ftell(p->obj_);
  4139. p->order = dcr_get2(p);
  4140. if (dcr_get2(p) != 42) goto quit;
  4141. offset = dcr_get4(p);
  4142. dcr_fseek(p->obj_, offset-8, SEEK_CUR);
  4143. } else if (!strcmp (buf,"OLYMPUS")) {
  4144. base = dcr_ftell(p->obj_)-10;
  4145. dcr_fseek(p->obj_, -2, SEEK_CUR);
  4146. p->order = dcr_get2(p); dcr_get2(p);
  4147. } else if (!strncmp (buf,"FUJIFILM",8) ||
  4148. !strncmp (buf,"SONY",4) ||
  4149. !strcmp (buf,"Panasonic")) {
  4150. p->order = 0x4949;
  4151. dcr_fseek(p->obj_, 2, SEEK_CUR);
  4152. } else if (!strcmp (buf,"OLYMP") ||
  4153. !strcmp (buf,"LEICA") ||
  4154. !strcmp (buf,"Ricoh") ||
  4155. !strcmp (buf,"EPSON"))
  4156. dcr_fseek(p->obj_, -2, SEEK_CUR);
  4157. else if (!strcmp (buf,"AOC") ||
  4158. !strcmp (buf,"QVC"))
  4159. dcr_fseek(p->obj_, -4, SEEK_CUR);
  4160. else dcr_fseek(p->obj_, -10, SEEK_CUR);
  4161. entries = dcr_get2(p);
  4162. if (entries > 1000) return;
  4163. while (entries--) {
  4164. dcr_tiff_get (p, base, &tag, &type, &len, &save);
  4165. tag |= uptag << 16;
  4166. if (tag == 2 && strstr(p->make,"NIKON"))
  4167. p->iso_speed = (dcr_get2(p),dcr_get2(p));
  4168. if (tag == 4 && len > 26 && len < 35) {
  4169. if ((i=(dcr_get4(p),dcr_get2(p))) != 0x7fff && !p->iso_speed)
  4170. p->iso_speed = 50 * (float)pow (2, i/32.0 - 4);
  4171. if ((i=(dcr_get2(p),dcr_get2(p))) != 0x7fff && !p->aperture)
  4172. p->aperture = (float)pow (2, i/64.0);
  4173. if ((i=dcr_get2(p)) != 0xffff && !p->shutter)
  4174. p->shutter = (float)pow (2, (short) i/-32.0);
  4175. wbi = (dcr_get2(p),dcr_get2(p));
  4176. p->shot_order = (dcr_get2(p),dcr_get2(p));
  4177. }
  4178. if (tag == 8 && type == 4)
  4179. p->shot_order = dcr_get4(p);
  4180. if (tag == 9 && !strcmp(p->make,"Canon"))
  4181. dcr_fread(p->obj_, p->artist, 64, 1);
  4182. if (tag == 0xc && len == 4) {
  4183. p->cam_mul[0] = (float)dcr_getreal(p,type);
  4184. p->cam_mul[2] = (float)dcr_getreal(p,type);
  4185. }
  4186. if (tag == 0x10 && type == 4)
  4187. p->unique_id = dcr_get4(p);
  4188. if (tag == 0x11 && p->is_raw && !strncmp(p->make,"NIKON",5)) {
  4189. dcr_fseek(p->obj_, dcr_get4(p)+base, SEEK_SET);
  4190. dcr_parse_tiff_ifd (p, base);
  4191. }
  4192. if (tag == 0x14 && len == 2560 && type == 7) {
  4193. dcr_fseek(p->obj_, 1248, SEEK_CUR);
  4194. goto get2_256;
  4195. }
  4196. if (tag == 0x15 && type == 2 && p->is_raw)
  4197. dcr_fread(p->obj_, p->model, 64, 1);
  4198. if (strstr(p->make,"PENTAX")) {
  4199. if (tag == 0x1b) tag = 0x1018;
  4200. if (tag == 0x1c) tag = 0x1017;
  4201. }
  4202. if (tag == 0x1d)
  4203. while ((c = dcr_fgetc(p->obj_)) && c != EOF)
  4204. serial = serial*10 + (isdigit(c) ? c - '0' : c % 10);
  4205. if (tag == 0x81 && type == 4) {
  4206. p->data_offset = dcr_get4(p);
  4207. dcr_fseek(p->obj_, p->data_offset + 41, SEEK_SET);
  4208. p->raw_height = dcr_get2(p) * 2;
  4209. p->raw_width = dcr_get2(p);
  4210. p->filters = 0x61616161;
  4211. }
  4212. if (tag == 0x29 && type == 1) {
  4213. c = wbi < 18 ? "012347800000005896"[wbi]-'0' : 0;
  4214. dcr_fseek(p->obj_, 8 + c*32, SEEK_CUR);
  4215. FORC4 p->cam_mul[c ^ (c >> 1) ^ 1] = (float)dcr_get4(p);
  4216. }
  4217. if ((tag == 0x81 && type == 7) ||
  4218. (tag == 0x100 && type == 7) ||
  4219. (tag == 0x280 && type == 1)) {
  4220. p->thumb_offset = dcr_ftell(p->obj_);
  4221. p->thumb_length = len;
  4222. }
  4223. if (tag == 0x88 && type == 4 && (p->thumb_offset = dcr_get4(p)))
  4224. p->thumb_offset += base;
  4225. if (tag == 0x89 && type == 4)
  4226. p->thumb_length = dcr_get4(p);
  4227. if (tag == 0x8c || tag == 0x96)
  4228. p->meta_offset = dcr_ftell(p->obj_);
  4229. if (tag == 0x97) {
  4230. for (i=0; i < 4; i++)
  4231. ver97 = ver97 * 10 + dcr_fgetc(p->obj_)-'0';
  4232. switch (ver97) {
  4233. case 100:
  4234. dcr_fseek(p->obj_, 68, SEEK_CUR);
  4235. FORC4 p->cam_mul[(c >> 1) | ((c & 1) << 1)] = dcr_get2(p);
  4236. break;
  4237. case 102:
  4238. dcr_fseek(p->obj_, 6, SEEK_CUR);
  4239. goto get2_rggb;
  4240. case 103:
  4241. dcr_fseek(p->obj_, 16, SEEK_CUR);
  4242. FORC4 p->cam_mul[c] = dcr_get2(p);
  4243. }
  4244. if (ver97 >= 200) {
  4245. if (ver97 != 205) dcr_fseek(p->obj_, 280, SEEK_CUR);
  4246. dcr_fread(p->obj_, buf97, 324, 1);
  4247. }
  4248. }
  4249. if (tag == 0xa4 && type == 3) {
  4250. dcr_fseek(p->obj_, wbi*48, SEEK_CUR);
  4251. FORC3 p->cam_mul[c] = dcr_get2(p);
  4252. }
  4253. if (tag == 0xa7 && (unsigned) (ver97-200) < 12 && !p->cam_mul[0]) {
  4254. ci = xlat[0][serial & 0xff];
  4255. cj = xlat[1][dcr_fgetc(p->obj_)^dcr_fgetc(p->obj_)^dcr_fgetc(p->obj_)^dcr_fgetc(p->obj_)];
  4256. ck = 0x60;
  4257. for (i=0; i < 324; i++)
  4258. buf97[i] ^= (cj += ci * ck++);
  4259. i = "66666>666;6A"[ver97-200] - '0';
  4260. FORC4 p->cam_mul[c ^ (c >> 1) ^ (i & 1)] =
  4261. dcr_sget2 (p, buf97 + (i & -2) + c*2);
  4262. }
  4263. if (tag == 0x200 && len == 3)
  4264. p->shot_order = (dcr_get4(p),dcr_get4(p));
  4265. if (tag == 0x200 && len == 4)
  4266. p->black = (dcr_get2(p)+dcr_get2(p)+dcr_get2(p)+dcr_get2(p))/4;
  4267. if (tag == 0x201 && len == 4)
  4268. goto get2_rggb;
  4269. if (tag == 0x401 && len == 4) {
  4270. p->black = (dcr_get4(p)+dcr_get4(p)+dcr_get4(p)+dcr_get4(p))/4;
  4271. }
  4272. if (tag == 0xe01) { /* Nikon Capture Note */
  4273. type = p->order;
  4274. p->order = 0x4949;
  4275. dcr_fseek(p->obj_, 22, SEEK_CUR);
  4276. for (offset=22; offset+22 < len; offset += 22+i) {
  4277. tag = dcr_get4(p);
  4278. dcr_fseek(p->obj_, 14, SEEK_CUR);
  4279. i = dcr_get4(p)-4;
  4280. if (tag == 0x76a43207) p->flip = dcr_get2(p);
  4281. else dcr_fseek(p->obj_, i, SEEK_CUR);
  4282. }
  4283. p->order = type;
  4284. }
  4285. if (tag == 0xe80 && len == 256 && type == 7) {
  4286. dcr_fseek(p->obj_, 48, SEEK_CUR);
  4287. p->cam_mul[0] = dcr_get2(p) * 508 * 1.078f / 0x10000;
  4288. p->cam_mul[2] = dcr_get2(p) * 382 * 1.173f / 0x10000;
  4289. }
  4290. if (tag == 0xf00 && type == 7) {
  4291. if (len == 614)
  4292. dcr_fseek(p->obj_, 176, SEEK_CUR);
  4293. else if (len == 734 || len == 1502)
  4294. dcr_fseek(p->obj_, 148, SEEK_CUR);
  4295. else goto next;
  4296. goto get2_256;
  4297. }
  4298. if ((tag == 0x1011 && len == 9) || tag == 0x20400200)
  4299. for (i=0; i < 3; i++)
  4300. FORC3 p->cmatrix[i][c] = ((short) dcr_get2(p)) / 256.0f;
  4301. if ((tag == 0x1012 || tag == 0x20400600) && len == 4)
  4302. for (p->black = i=0; i < 4; i++)
  4303. p->black += dcr_get2(p) << 2;
  4304. if (tag == 0x1017 || tag == 0x20400100)
  4305. p->cam_mul[0] = dcr_get2(p) / 256.0f;
  4306. if (tag == 0x1018 || tag == 0x20400100)
  4307. p->cam_mul[2] = dcr_get2(p) / 256.0f;
  4308. if (tag == 0x2011 && len == 2) {
  4309. get2_256:
  4310. p->order = 0x4d4d;
  4311. p->cam_mul[0] = dcr_get2(p) / 256.0f;
  4312. p->cam_mul[2] = dcr_get2(p) / 256.0f;
  4313. }
  4314. if ((tag | 0x70) == 0x2070 && type == 4)
  4315. dcr_fseek(p->obj_, dcr_get4(p)+base, SEEK_SET);
  4316. if (tag == 0x2010 && type != 7)
  4317. p->load_raw = &DCR_CLASS dcr_olympus_e410_load_raw;
  4318. if (tag == 0x2020)
  4319. dcr_parse_thumb_note (p, base, 257, 258);
  4320. if (tag == 0x2040)
  4321. dcr_parse_makernote (p, base, 0x2040);
  4322. if (tag == 0xb028) {
  4323. dcr_fseek(p->obj_, dcr_get4(p), SEEK_SET);
  4324. dcr_parse_thumb_note (p, base, 136, 137);
  4325. }
  4326. if (tag == 0x4001 && len > 500) {
  4327. i = len == 582 ? 50 : len == 653 ? 68 : len == 5120 ? 142 : 126;
  4328. dcr_fseek(p->obj_, i, SEEK_CUR);
  4329. get2_rggb:
  4330. FORC4 p->cam_mul[c ^ (c >> 1)] = dcr_get2(p);
  4331. dcr_fseek(p->obj_, 22, SEEK_CUR);
  4332. FORC4 p->sraw_mul[c ^ (c >> 1)] = dcr_get2(p);
  4333. }
  4334. next:
  4335. dcr_fseek(p->obj_, save, SEEK_SET);
  4336. }
  4337. quit:
  4338. p->order = sorder;
  4339. }
  4340. /*
  4341. Since the TIFF DateTime string has no timezone information,
  4342. assume that the camera's clock was set to Universal Time.
  4343. */
  4344. void DCR_CLASS dcr_get_timestamp (DCRAW* p, int reversed)
  4345. {
  4346. struct tm t;
  4347. char str[20];
  4348. int i;
  4349. str[19] = 0;
  4350. if (reversed)
  4351. for (i=19; i--; ) str[i] = dcr_fgetc(p->obj_);
  4352. else
  4353. dcr_fread(p->obj_, str, 19, 1);
  4354. memset (&t, 0, sizeof t);
  4355. if (sscanf (str, "%d:%d:%d %d:%d:%d", &t.tm_year, &t.tm_mon,
  4356. &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec) != 6)
  4357. return;
  4358. t.tm_year -= 1900;
  4359. t.tm_mon -= 1;
  4360. if (mktime(&t) > 0)
  4361. p->timestamp = mktime(&t);
  4362. }
  4363. void DCR_CLASS dcr_parse_exif (DCRAW* p, int base)
  4364. {
  4365. unsigned kodak, entries, tag, type, len, save, c;
  4366. double expo;
  4367. kodak = !strncmp(p->make,"EASTMAN",7);
  4368. entries = dcr_get2(p);
  4369. while (entries--) {
  4370. dcr_tiff_get (p, base, &tag, &type, &len, &save);
  4371. switch (tag) {
  4372. case 33434: p->shutter = (float)dcr_getreal(p,type); break;
  4373. case 33437: p->aperture = (float)dcr_getreal(p,type); break;
  4374. case 34855: p->iso_speed = dcr_get2(p); break;
  4375. case 36867:
  4376. case 36868: dcr_get_timestamp(p,0); break;
  4377. case 37377: if ((expo = -dcr_getreal(p,type)) < 128)
  4378. p->shutter = (float)pow (2, expo); break;
  4379. case 37378: p->aperture = (float)pow (2, dcr_getreal(p,type)/2); break;
  4380. case 37386: p->focal_len = (float)dcr_getreal(p,type); break;
  4381. case 37500: dcr_parse_makernote (p,base, 0); break;
  4382. case 40962: if (kodak) p->raw_width = dcr_get4(p); break;
  4383. case 40963: if (kodak) p->raw_height = dcr_get4(p); break;
  4384. case 41730:
  4385. if (dcr_get4(p) == 0x20002)
  4386. for (p->exif_cfa=c=0; c < 8; c+=2)
  4387. p->exif_cfa |= dcr_fgetc(p->obj_) * 0x01010101 << c;
  4388. }
  4389. dcr_fseek(p->obj_, save, SEEK_SET);
  4390. }
  4391. }
  4392. void DCR_CLASS dcr_parse_gps (DCRAW* p, int base)
  4393. {
  4394. unsigned entries, tag, type, len, save, c;
  4395. entries = dcr_get2(p);
  4396. while (entries--) {
  4397. dcr_tiff_get (p, base, &tag, &type, &len, &save);
  4398. switch (tag) {
  4399. case 1: case 3: case 5:
  4400. p->gpsdata[29+tag/2] = dcr_fgetc(p->obj_); break;
  4401. case 2: case 4: case 7:
  4402. FORC(6) p->gpsdata[tag/3*6+c] = dcr_get4(p); break;
  4403. case 6:
  4404. FORC(2) p->gpsdata[18+c] = dcr_get4(p); break;
  4405. case 18: case 29:
  4406. dcr_fgets(p->obj_, (char *) (p->gpsdata+14+tag/3), MIN(len,12));
  4407. }
  4408. dcr_fseek(p->obj_, save, SEEK_SET);
  4409. }
  4410. }
  4411. void DCR_CLASS dcr_romm_coeff (DCRAW* p, float romm_cam[3][3])
  4412. {
  4413. static const float rgb_romm[3][3] = /* ROMM == Kodak ProPhoto */
  4414. { { 2.034193f, -0.727420f, -0.306766f },
  4415. { -0.228811f, 1.231729f, -0.002922f },
  4416. { -0.008565f, -0.153273f, 1.161839f } };
  4417. int i, j, k;
  4418. for (i=0; i < 3; i++)
  4419. for (j=0; j < 3; j++)
  4420. for (p->cmatrix[i][j]=0, k=0; k < 3; k++)
  4421. p->cmatrix[i][j] += rgb_romm[i][k] * romm_cam[k][j];
  4422. }
  4423. void DCR_CLASS dcr_parse_mos (DCRAW* p, int offset)
  4424. {
  4425. char data[40];
  4426. int skip, from, i=0, c, neut[4], planes=0, frot=0;
  4427. static const char *mod[] =
  4428. { "","DCB2","Volare","Cantare","CMost","Valeo 6","Valeo 11","Valeo 22",
  4429. "Valeo 11p","Valeo 17","","Aptus 17","Aptus 22","Aptus 75","Aptus 65",
  4430. "Aptus 54S","Aptus 65S","Aptus 75S","AFi 5","AFi 6","AFi 7" };
  4431. float romm_cam[3][3];
  4432. dcr_fseek(p->obj_, offset, SEEK_SET);
  4433. while (1) {
  4434. if (dcr_get4(p) != 0x504b5453) break;
  4435. dcr_get4(p);
  4436. dcr_fread(p->obj_, data, 1, 40);
  4437. skip = dcr_get4(p);
  4438. from = dcr_ftell(p->obj_);
  4439. if (!strcmp(data,"JPEG_preview_data")) {
  4440. p->thumb_offset = from;
  4441. p->thumb_length = skip;
  4442. }
  4443. if (!strcmp(data,"icc_camera_profile")) {
  4444. p->profile_offset = from;
  4445. p->profile_length = skip;
  4446. }
  4447. if (!strcmp(data,"ShootObj_back_type")) {
  4448. dcr_fscanf(p->obj_, "%d", &i);
  4449. if ((unsigned) i < sizeof mod / sizeof (*mod))
  4450. strcpy (p->model, mod[i]);
  4451. }
  4452. if (!strcmp(data,"icc_camera_to_tone_matrix")) {
  4453. for (i=0; i < 9; i++)
  4454. romm_cam[0][i] = dcr_int_to_float(dcr_get4(p));
  4455. dcr_romm_coeff (p,romm_cam);
  4456. }
  4457. if (!strcmp(data,"CaptProf_color_matrix")) {
  4458. for (i=0; i < 9; i++)
  4459. dcr_fscanf(p->obj_, "%f", &romm_cam[0][i]);
  4460. dcr_romm_coeff (p,romm_cam);
  4461. }
  4462. if (!strcmp(data,"CaptProf_number_of_planes"))
  4463. dcr_fscanf(p->obj_, "%d", &planes);
  4464. if (!strcmp(data,"CaptProf_raw_data_rotation"))
  4465. dcr_fscanf(p->obj_, "%d", &p->flip);
  4466. if (!strcmp(data,"CaptProf_mosaic_pattern"))
  4467. FORC4 {
  4468. dcr_fscanf(p->obj_, "%d", &i);
  4469. if (i == 1) frot = c ^ (c >> 1);
  4470. }
  4471. if (!strcmp(data,"ImgProf_rotation_angle")) {
  4472. dcr_fscanf(p->obj_, "%d", &i);
  4473. p->flip = i - p->flip;
  4474. }
  4475. if (!strcmp(data,"NeutObj_neutrals") && !p->cam_mul[0]) {
  4476. FORC4 dcr_fscanf(p->obj_, "%d", neut+c);
  4477. FORC3 p->cam_mul[c] = (neut[c+1] ? (float) neut[0] / neut[c+1] : 0);
  4478. }
  4479. dcr_parse_mos (p,from);
  4480. dcr_fseek(p->obj_, skip+from, SEEK_SET);
  4481. }
  4482. if (planes)
  4483. p->filters = (planes == 1) * 0x01010101 *
  4484. (uchar) "\x94\x61\x16\x49"[(p->flip/90 + frot) & 3];
  4485. }
  4486. void DCR_CLASS dcr_linear_table (DCRAW* p, unsigned len)
  4487. {
  4488. int i;
  4489. if (len > 0x1000) len = 0x1000;
  4490. dcr_read_shorts (p, p->curve, len);
  4491. for (i=len; i < 0x1000; i++)
  4492. p->curve[i] = p->curve[i-1];
  4493. p->maximum = p->curve[0xfff];
  4494. }
  4495. void DCR_CLASS dcr_parse_kodak_ifd (DCRAW* p, int base)
  4496. {
  4497. unsigned entries, tag, type, len, save;
  4498. int i, c, wbi=-2, wbtemp=6500;
  4499. float mul[3], num;
  4500. entries = dcr_get2(p);
  4501. if (entries > 1024) return;
  4502. while (entries--) {
  4503. dcr_tiff_get (p, base, &tag, &type, &len, &save);
  4504. if (tag == 1020) wbi = dcr_getint(p, type);
  4505. if (tag == 1021 && len == 72) { /* WB set in software */
  4506. dcr_fseek(p->obj_, 40, SEEK_CUR);
  4507. FORC3 p->cam_mul[c] = 2048.0f / dcr_get2(p);
  4508. wbi = -2;
  4509. }
  4510. if (tag == (unsigned int)2118) wbtemp = dcr_getint(p, type);
  4511. if (tag == (unsigned int)2130 + wbi)
  4512. FORC3 mul[c] = (float)dcr_getreal(p, type);
  4513. if (tag == (unsigned int)2140 + wbi && wbi >= 0)
  4514. FORC3 {
  4515. for (num=0.0f, i=0; i < 4; i++)
  4516. num += (float)(dcr_getreal(p, type) * pow (wbtemp/100.0, i));
  4517. p->cam_mul[c] = 2048 / (num * mul[c]);
  4518. }
  4519. if (tag == 2317) dcr_linear_table (p,len);
  4520. if (tag == 6020) p->iso_speed = (float)dcr_getint(p, type);
  4521. dcr_fseek(p->obj_, save, SEEK_SET);
  4522. }
  4523. }
  4524. void DCR_CLASS dcr_parse_minolta (DCRAW* p, int base);
  4525. int DCR_CLASS dcr_parse_tiff_ifd (DCRAW* p, int base)
  4526. {
  4527. unsigned entries, tag, type, len, plen=16, save;
  4528. int ifd, use_cm=0, cfa, i, j, c, ima_len=0;
  4529. char software[64], *cbuf, *cp;
  4530. uchar cfa_pat[16], cfa_pc[] = { 0,1,2,3 }, tab[256];
  4531. double dblack, cc[4][4], cm[4][3], cam_xyz[4][3], num;
  4532. double ab[]={ 1,1,1,1 }, asn[] = { 0,0,0,0 }, xyz[] = { 1,1,1 };
  4533. unsigned sony_curve[] = { 0,0,0,0,0,4095 };
  4534. unsigned *buf, sony_offset=0, sony_length=0, sony_key=0;
  4535. struct dcr_jhead jh;
  4536. if (p->tiff_nifds >= sizeof p->tiff_ifd / sizeof p->tiff_ifd[0])
  4537. return 1;
  4538. ifd = p->tiff_nifds++;
  4539. for (j=0; j < 4; j++)
  4540. for (i=0; i < 4; i++)
  4541. cc[j][i] = i == j;
  4542. entries = dcr_get2(p);
  4543. if (entries > 512) return 1;
  4544. while (entries--) {
  4545. dcr_tiff_get (p, base, &tag, &type, &len, &save);
  4546. switch (tag) {
  4547. case 17: case 18:
  4548. if (type == 3 && len == 1)
  4549. p->cam_mul[(tag-17)*2] = dcr_get2(p) / 256.0f;
  4550. break;
  4551. case 23:
  4552. if (type == 3) p->iso_speed = dcr_get2(p);
  4553. break;
  4554. case 36: case 37: case 38:
  4555. p->cam_mul[tag-0x24] = dcr_get2(p);
  4556. break;
  4557. case 39:
  4558. if (len < 50 || p->cam_mul[0]) break;
  4559. dcr_fseek(p->obj_, 12, SEEK_CUR);
  4560. FORC3 p->cam_mul[c] = dcr_get2(p);
  4561. break;
  4562. case 46:
  4563. if (type != 7 || dcr_fgetc(p->obj_) != 0xff || dcr_fgetc(p->obj_) != 0xd8) break;
  4564. p->thumb_offset = dcr_ftell(p->obj_) - 2;
  4565. p->thumb_length = len;
  4566. break;
  4567. case 2: case 256: /* ImageWidth */
  4568. p->tiff_ifd[ifd].width = dcr_getint(p, type);
  4569. break;
  4570. case 3: case 257: /* ImageHeight */
  4571. p->tiff_ifd[ifd].height = dcr_getint(p, type);
  4572. break;
  4573. case 258: /* BitsPerSample */
  4574. p->tiff_ifd[ifd].samples = len & 7;
  4575. p->tiff_ifd[ifd].bps = dcr_get2(p);
  4576. break;
  4577. case 259: /* Compression */
  4578. p->tiff_ifd[ifd].comp = dcr_get2(p);
  4579. break;
  4580. case 262: /* PhotometricInterpretation */
  4581. p->tiff_ifd[ifd].phint = dcr_get2(p);
  4582. break;
  4583. case 270: /* ImageDescription */
  4584. dcr_fread(p->obj_, p->desc, 512, 1);
  4585. break;
  4586. case 271: /* Make */
  4587. dcr_fgets(p->obj_, p->make, 64);
  4588. break;
  4589. case 272: /* Model */
  4590. dcr_fgets(p->obj_, p->model, 64);
  4591. break;
  4592. case 280: /* Panasonic RW2 offset */
  4593. if (type != 4) break;
  4594. p->load_raw = &DCR_CLASS dcr_panasonic_load_raw;
  4595. p->load_flags = 0x2008;
  4596. case 273: /* StripOffset */
  4597. case 513:
  4598. p->tiff_ifd[ifd].offset = dcr_get4(p)+base;
  4599. if (!p->tiff_ifd[ifd].bps) {
  4600. dcr_fseek(p->obj_, p->tiff_ifd[ifd].offset, SEEK_SET);
  4601. if (dcr_ljpeg_start (p,&jh, 1)) {
  4602. p->tiff_ifd[ifd].comp = 6;
  4603. p->tiff_ifd[ifd].width = jh.wide << (jh.clrs == 2);
  4604. p->tiff_ifd[ifd].height = jh.high;
  4605. p->tiff_ifd[ifd].bps = jh.bits;
  4606. p->tiff_ifd[ifd].samples = jh.clrs;
  4607. }
  4608. }
  4609. break;
  4610. case 274: /* Orientation */
  4611. p->tiff_ifd[ifd].flip = "50132467"[dcr_get2(p) & 7]-'0';
  4612. break;
  4613. case 277: /* SamplesPerPixel */
  4614. p->tiff_ifd[ifd].samples = dcr_getint(p, type) & 7;
  4615. break;
  4616. case 279: /* StripByteCounts */
  4617. case 514:
  4618. p->tiff_ifd[ifd].bytes = dcr_get4(p);
  4619. break;
  4620. case 305: case 11: /* Software */
  4621. dcr_fgets(p->obj_, software, 64);
  4622. if (!strncmp(software,"Adobe",5) ||
  4623. !strncmp(software,"dcraw",5) ||
  4624. !strncmp(software,"UFRaw",5) ||
  4625. !strncmp(software,"Bibble",6) ||
  4626. !strncmp(software,"Nikon Scan",10) ||
  4627. !strcmp (software,"Digital Photo Professional"))
  4628. p->is_raw = 0;
  4629. break;
  4630. case 306: /* DateTime */
  4631. dcr_get_timestamp(p,0);
  4632. break;
  4633. case 315: /* Artist */
  4634. dcr_fread(p->obj_, p->artist, 64, 1);
  4635. break;
  4636. case 322: /* TileWidth */
  4637. p->tile_width = dcr_getint(p, type);
  4638. break;
  4639. case 323: /* TileLength */
  4640. p->tile_length = dcr_getint(p, type);
  4641. break;
  4642. case 324: /* TileOffsets */
  4643. p->tiff_ifd[ifd].offset = len > 1 ? dcr_ftell(p->obj_) : dcr_get4(p);
  4644. if (len == 4) {
  4645. p->load_raw = &DCR_CLASS dcr_sinar_4shot_load_raw;
  4646. p->is_raw = 5;
  4647. }
  4648. break;
  4649. case 330: /* SubIFDs */
  4650. if (!strcmp(p->model,"DSLR-A100") && p->tiff_ifd[ifd].width == 3872) {
  4651. p->load_raw = &DCR_CLASS dcr_sony_arw_load_raw;
  4652. p->data_offset = dcr_get4(p)+base;
  4653. ifd++; break;
  4654. }
  4655. while (len--) {
  4656. i = dcr_ftell(p->obj_);
  4657. dcr_fseek(p->obj_, dcr_get4(p)+base, SEEK_SET);
  4658. if (dcr_parse_tiff_ifd (p, base)) break;
  4659. dcr_fseek(p->obj_, i+4, SEEK_SET);
  4660. }
  4661. break;
  4662. case 400:
  4663. strcpy (p->make, "Sarnoff");
  4664. p->maximum = 0xfff;
  4665. break;
  4666. case 28688:
  4667. FORC4 sony_curve[c+1] = dcr_get2(p) >> 2 & 0xfff;
  4668. for (i=0; i < 5; i++)
  4669. for (j = sony_curve[i]+1; j <= (int)sony_curve[i+1]; j++)
  4670. p->curve[j] = p->curve[j-1] + (1 << i);
  4671. break;
  4672. case 29184: sony_offset = dcr_get4(p); break;
  4673. case 29185: sony_length = dcr_get4(p); break;
  4674. case 29217: sony_key = dcr_get4(p); break;
  4675. case 29264:
  4676. dcr_parse_minolta (p, dcr_ftell(p->obj_));
  4677. p->raw_width = 0;
  4678. break;
  4679. case 29443:
  4680. FORC4 p->cam_mul[c ^ (c < 2)] = dcr_get2(p);
  4681. break;
  4682. case 29459:
  4683. FORC4 p->cam_mul[c ^ (c >> 1)] = dcr_get2(p);
  4684. break;
  4685. case 33405: /* Model2 */
  4686. dcr_fgets(p->obj_, p->model2, 64);
  4687. break;
  4688. case 33422: /* CFAPattern */
  4689. case 64777: /* Kodak P-series */
  4690. if ((plen=len) > 16) plen = 16;
  4691. dcr_fread(p->obj_, cfa_pat, 1, plen);
  4692. for (p->colors=cfa=i=0; i < (int)plen; i++) {
  4693. p->colors += !(cfa & (1 << cfa_pat[i]));
  4694. cfa |= 1 << cfa_pat[i];
  4695. }
  4696. if (cfa == 070) memcpy (cfa_pc,"\003\004\005",3); /* CMY */
  4697. if (cfa == 072) memcpy (cfa_pc,"\005\003\004\001",4); /* GMCY */
  4698. goto guess_cfa_pc;
  4699. case 33424:
  4700. dcr_fseek(p->obj_, dcr_get4(p)+base, SEEK_SET);
  4701. dcr_parse_kodak_ifd (p,base);
  4702. break;
  4703. case 33434: /* ExposureTime */
  4704. p->shutter = (float)dcr_getreal(p,type);
  4705. break;
  4706. case 33437: /* FNumber */
  4707. p->aperture = (float)dcr_getreal(p,type);
  4708. break;
  4709. case 34306: /* Leaf p->white balance */
  4710. FORC4 p->cam_mul[c ^ 1] = 4096.0f / dcr_get2(p);
  4711. break;
  4712. case 34307: /* Leaf CatchLight color matrix */
  4713. dcr_fread(p->obj_, software, 1, 7);
  4714. if (strncmp(software,"MATRIX",6)) break;
  4715. p->colors = 4;
  4716. for (p->raw_color = i=0; i < 3; i++) {
  4717. FORC4 dcr_fscanf(p->obj_, "%f", &p->rgb_cam[i][c^1]);
  4718. if (!p->opt.use_camera_wb) continue;
  4719. num = 0;
  4720. FORC4 num += p->rgb_cam[i][c];
  4721. FORC4 p->rgb_cam[i][c] /= (float)num;
  4722. }
  4723. break;
  4724. case 34310: /* Leaf metadata */
  4725. dcr_parse_mos (p,dcr_ftell(p->obj_));
  4726. case 34303:
  4727. strcpy (p->make, "Leaf");
  4728. break;
  4729. case 34665: /* EXIF tag */
  4730. dcr_fseek(p->obj_, dcr_get4(p)+base, SEEK_SET);
  4731. dcr_parse_exif (p,base);
  4732. break;
  4733. case 34853: /* GPSInfo tag */
  4734. dcr_fseek(p->obj_, dcr_get4(p)+base, SEEK_SET);
  4735. dcr_parse_gps (p,base);
  4736. break;
  4737. case 34675: /* InterColorProfile */
  4738. case 50831: /* AsShotICCProfile */
  4739. p->profile_offset = dcr_ftell(p->obj_);
  4740. p->profile_length = len;
  4741. break;
  4742. case 37122: /* CompressedBitsPerPixel */
  4743. p->kodak_cbpp = dcr_get4(p);
  4744. break;
  4745. case 37386: /* FocalLength */
  4746. p->focal_len = (float)dcr_getreal(p,type);
  4747. break;
  4748. case 37393: /* ImageNumber */
  4749. p->shot_order = dcr_getint(p, type);
  4750. break;
  4751. case 37400: /* old Kodak KDC tag */
  4752. for (p->raw_color = i=0; i < 3; i++) {
  4753. dcr_getreal(p,type);
  4754. FORC3 p->rgb_cam[i][c] = (float)dcr_getreal(p,type);
  4755. }
  4756. break;
  4757. case 46275: /* Imacon tags */
  4758. strcpy (p->make, "Imacon");
  4759. p->data_offset = dcr_ftell(p->obj_);
  4760. ima_len = len;
  4761. break;
  4762. case 46279:
  4763. if (!ima_len) break;
  4764. dcr_fseek(p->obj_, 78, SEEK_CUR);
  4765. p->raw_width = dcr_get4(p);
  4766. p->raw_height = dcr_get4(p);
  4767. p->left_margin = dcr_get4(p) & 7;
  4768. p->width = p->raw_width - p->left_margin - (dcr_get4(p) & 7);
  4769. p->top_margin = dcr_get4(p) & 7;
  4770. p->height = p->raw_height - p->top_margin - (dcr_get4(p) & 7);
  4771. if (p->raw_width == 7262) {
  4772. p->height = 5444;
  4773. p->width = 7244;
  4774. p->left_margin = 7;
  4775. }
  4776. dcr_fseek(p->obj_, 52, SEEK_CUR);
  4777. FORC3 p->cam_mul[c] = (float)dcr_getreal(p, 11);
  4778. dcr_fseek(p->obj_, 114, SEEK_CUR);
  4779. p->flip = (dcr_get2(p) >> 7) * 90;
  4780. if (p->width * p->height * 6 == ima_len) {
  4781. if (p->flip % 180 == 90) SWAP(p->width,p->height);
  4782. p->filters = p->flip = 0;
  4783. }
  4784. sprintf (p->model, "Ixpress %d-Mp", p->height*p->width/1000000);
  4785. p->load_raw = &DCR_CLASS dcr_imacon_full_load_raw;
  4786. if (p->filters) {
  4787. if (p->left_margin & 1) p->filters = 0x61616161;
  4788. p->load_raw = &DCR_CLASS dcr_unpacked_load_raw;
  4789. }
  4790. p->maximum = 0xffff;
  4791. break;
  4792. case 50454: /* Sinar tag */
  4793. case 50455:
  4794. if (!(cbuf = (char *) malloc(len))) break;
  4795. dcr_fread(p->obj_, cbuf, 1, len);
  4796. for (cp = cbuf-1; cp && cp < cbuf+len; cp = strchr(cp,'\n'))
  4797. if (!strncmp (++cp,"Neutral ",8))
  4798. sscanf (cp+8, "%f %f %f", p->cam_mul, p->cam_mul+1, p->cam_mul+2);
  4799. free (cbuf);
  4800. break;
  4801. case 50458:
  4802. if (!p->make[0]) strcpy (p->make, "Hasselblad");
  4803. break;
  4804. case 50459: /* Hasselblad tag */
  4805. i = p->order;
  4806. j = dcr_ftell(p->obj_);
  4807. c = p->tiff_nifds;
  4808. p->order = dcr_get2(p);
  4809. dcr_fseek(p->obj_, j+(dcr_get2(p),dcr_get4(p)), SEEK_SET);
  4810. dcr_parse_tiff_ifd (p, j);
  4811. p->maximum = 0xffff;
  4812. p->tiff_nifds = c;
  4813. p->order = i;
  4814. break;
  4815. case 50706: /* DNGVersion */
  4816. FORC4 p->dng_version = (p->dng_version << 8) + dcr_fgetc(p->obj_);
  4817. if (!p->make[0]) strcpy (p->make, "DNG");
  4818. p->is_raw = 1;
  4819. break;
  4820. case 50710: /* CFAPlaneColor */
  4821. if (len > 4) len = 4;
  4822. p->colors = len;
  4823. dcr_fread(p->obj_, cfa_pc, 1, p->colors);
  4824. guess_cfa_pc:
  4825. FORCC(p) tab[cfa_pc[c]] = c;
  4826. p->cdesc[c] = 0;
  4827. for (i=16; i--; )
  4828. p->filters = p->filters << 2 | tab[cfa_pat[i % plen]];
  4829. break;
  4830. case 50711: /* CFALayout */
  4831. if (dcr_get2(p) == 2) {
  4832. p->fuji_width = 1;
  4833. p->filters = 0x49494949;
  4834. }
  4835. break;
  4836. case 291:
  4837. case 50712: /* LinearizationTable */
  4838. dcr_linear_table (p,len);
  4839. break;
  4840. case 50714: /* BlackLevel */
  4841. case 50715: /* BlackLevelDeltaH */
  4842. case 50716: /* BlackLevelDeltaV */
  4843. for (dblack=i=0; i < (int)len; i++)
  4844. dblack += dcr_getreal(p, type);
  4845. p->black += (unsigned int)(dblack/len + 0.5);
  4846. break;
  4847. case 50717: /* WhiteLevel */
  4848. p->maximum = dcr_getint(p, type);
  4849. break;
  4850. case 50718: /* DefaultScale */
  4851. p->pixel_aspect = dcr_getreal(p,type);
  4852. p->pixel_aspect /= dcr_getreal(p,type);
  4853. break;
  4854. case 50721: /* ColorMatrix1 */
  4855. case 50722: /* ColorMatrix2 */
  4856. FORCC(p) for (j=0; j < 3; j++)
  4857. cm[c][j] = dcr_getreal(p,type);
  4858. use_cm = 1;
  4859. break;
  4860. case 50723: /* CameraCalibration1 */
  4861. case 50724: /* CameraCalibration2 */
  4862. for (i=0; i < p->colors; i++)
  4863. FORCC(p) cc[i][c] = dcr_getreal(p,type);
  4864. case 50727: /* AnalogBalance */
  4865. FORCC(p) ab[c] = dcr_getreal(p,type);
  4866. break;
  4867. case 50728: /* AsShotNeutral */
  4868. FORCC(p) asn[c] = dcr_getreal(p, type);
  4869. break;
  4870. case 50729: /* AsShotWhiteXY */
  4871. xyz[0] = dcr_getreal(p,type);
  4872. xyz[1] = dcr_getreal(p,type);
  4873. xyz[2] = 1 - xyz[0] - xyz[1];
  4874. FORC3 xyz[c] /= d65_white[c];
  4875. break;
  4876. case 50740: /* DNGPrivateData */
  4877. if (p->dng_version) break;
  4878. dcr_parse_minolta (p, j = dcr_get4(p)+base);
  4879. dcr_fseek(p->obj_, j, SEEK_SET);
  4880. dcr_parse_tiff_ifd (p, base);
  4881. break;
  4882. case 50752:
  4883. dcr_read_shorts (p, p->cr2_slice, 3);
  4884. break;
  4885. case 50829: /* ActiveArea */
  4886. p->top_margin = dcr_getint(p, type);
  4887. p->left_margin = dcr_getint(p, type);
  4888. p->height = dcr_getint(p, type) - p->top_margin;
  4889. p->width = dcr_getint(p, type) - p->left_margin;
  4890. break;
  4891. case 64772: /* Kodak P-series */
  4892. dcr_fseek(p->obj_, 16, SEEK_CUR);
  4893. p->data_offset = dcr_get4(p);
  4894. dcr_fseek(p->obj_, 28, SEEK_CUR);
  4895. p->data_offset += dcr_get4(p);
  4896. p->load_raw = &DCR_CLASS dcr_packed_12_load_raw;
  4897. }
  4898. dcr_fseek(p->obj_, save, SEEK_SET);
  4899. }
  4900. if (sony_length && (buf = (unsigned *) malloc(sony_length))) {
  4901. dcr_stream_ops *sops_;
  4902. dcr_stream_obj *sobj_;
  4903. dcr_fseek(p->obj_, sony_offset, SEEK_SET);
  4904. dcr_fread(p->obj_, buf, sony_length, 1);
  4905. dcr_sony_decrypt (buf, sony_length/4, 1, sony_key);
  4906. sops_ = p->ops_;
  4907. sobj_ = p->obj_;
  4908. p->ops_ = &dcr_stream_fileops;
  4909. if ((p->obj_ = tmpfile())) {
  4910. dcr_fwrite(p->obj_, buf, sony_length, 1);
  4911. dcr_fseek(p->obj_, 0, SEEK_SET);
  4912. dcr_parse_tiff_ifd (p, -(int)sony_offset);
  4913. dcr_fclose(p->obj_);
  4914. }
  4915. p->ops_ = sops_;
  4916. p->obj_ = sobj_;
  4917. free (buf);
  4918. }
  4919. for (i=0; i < p->colors; i++)
  4920. FORCC(p) cc[i][c] *= ab[i];
  4921. if (use_cm) {
  4922. FORCC(p) for (i=0; i < 3; i++)
  4923. for (cam_xyz[c][i]=j=0; j < p->colors; j++)
  4924. cam_xyz[c][i] += cc[c][j] * cm[j][i] * xyz[i];
  4925. dcr_cam_xyz_coeff (p, cam_xyz);
  4926. }
  4927. if (asn[0]) {
  4928. p->cam_mul[3] = 0;
  4929. FORCC(p) p->cam_mul[c] = 1.0f / (float)asn[c];
  4930. }
  4931. if (!use_cm)
  4932. FORCC(p) p->pre_mul[c] /= (float)cc[c][c];
  4933. return 0;
  4934. }
  4935. void DCR_CLASS dcr_parse_tiff (DCRAW* p, int base)
  4936. {
  4937. int doff, max_samp=0, raw=-1, thm=-1, i;
  4938. struct dcr_jhead jh;
  4939. dcr_fseek(p->obj_, base, SEEK_SET);
  4940. p->order = dcr_get2(p);
  4941. if (p->order != 0x4949 && p->order != 0x4d4d) return;
  4942. dcr_get2(p);
  4943. memset (p->tiff_ifd, 0, sizeof p->tiff_ifd);
  4944. p->tiff_nifds = 0;
  4945. while ((doff = dcr_get4(p))) {
  4946. dcr_fseek(p->obj_, doff+base, SEEK_SET);
  4947. if (dcr_parse_tiff_ifd (p, base)) break;
  4948. }
  4949. p->thumb_misc = 16;
  4950. if (p->thumb_offset) {
  4951. dcr_fseek(p->obj_, p->thumb_offset, SEEK_SET);
  4952. if (dcr_ljpeg_start (p,&jh, 1)) {
  4953. p->thumb_misc = jh.bits;
  4954. p->thumb_width = jh.wide;
  4955. p->thumb_height = jh.high;
  4956. }
  4957. }
  4958. for (i=0; i < (int)p->tiff_nifds; i++) {
  4959. if (max_samp < p->tiff_ifd[i].samples)
  4960. max_samp = p->tiff_ifd[i].samples;
  4961. if (max_samp > 3) max_samp = 3;
  4962. if ((p->tiff_ifd[i].comp != 6 || p->tiff_ifd[i].samples != 3) &&
  4963. p->tiff_ifd[i].width*p->tiff_ifd[i].height > p->raw_width*p->raw_height) {
  4964. p->raw_width = p->tiff_ifd[i].width;
  4965. p->raw_height = p->tiff_ifd[i].height;
  4966. p->tiff_bps = p->tiff_ifd[i].bps;
  4967. p->tiff_compress = p->tiff_ifd[i].comp;
  4968. p->data_offset = p->tiff_ifd[i].offset;
  4969. p->tiff_flip = p->tiff_ifd[i].flip;
  4970. p->tiff_samples = p->tiff_ifd[i].samples;
  4971. raw = i;
  4972. }
  4973. }
  4974. p->fuji_width *= (p->raw_width+1)/2;
  4975. if (p->tiff_ifd[0].flip) p->tiff_flip = p->tiff_ifd[0].flip;
  4976. if (raw >= 0 && !p->load_raw)
  4977. switch (p->tiff_compress) {
  4978. case 0: case 1:
  4979. switch (p->tiff_bps) {
  4980. case 8: p->load_raw = &DCR_CLASS dcr_eight_bit_load_raw; break;
  4981. case 12: p->load_raw = &DCR_CLASS dcr_packed_12_load_raw;
  4982. if (p->tiff_ifd[raw].phint == 2)
  4983. p->load_flags = 6;
  4984. if (strncmp(p->make,"PENTAX",6)) break;
  4985. case 14:
  4986. case 16: p->load_raw = &DCR_CLASS dcr_unpacked_load_raw; break;
  4987. }
  4988. if (p->tiff_ifd[raw].bytes*5 == p->raw_width*p->raw_height*8)
  4989. p->load_raw = &DCR_CLASS dcr_olympus_e300_load_raw;
  4990. break;
  4991. case 6: case 7: case 99:
  4992. p->load_raw = &DCR_CLASS dcr_lossless_jpeg_load_raw; break;
  4993. case 262:
  4994. p->load_raw = &DCR_CLASS dcr_kodak_262_load_raw; break;
  4995. case 32767:
  4996. p->load_raw = &DCR_CLASS dcr_sony_arw2_load_raw;
  4997. if (p->tiff_ifd[raw].bytes*8 == (int)(p->raw_width*p->raw_height*p->tiff_bps))
  4998. break;
  4999. p->raw_height += 8;
  5000. p->load_raw = &DCR_CLASS dcr_sony_arw_load_raw; break;
  5001. case 32769:
  5002. p->load_flags = 8;
  5003. case 32773:
  5004. p->load_raw = &DCR_CLASS dcr_packed_12_load_raw; break;
  5005. case 34713:
  5006. p->load_raw = &DCR_CLASS dcr_nikon_compressed_load_raw; break;
  5007. case 65535:
  5008. p->load_raw = &DCR_CLASS dcr_pentax_k10_load_raw; break;
  5009. case 65000:
  5010. switch (p->tiff_ifd[raw].phint) {
  5011. case 2: p->load_raw = &DCR_CLASS dcr_kodak_rgb_load_raw; p->filters = 0; break;
  5012. case 6: p->load_raw = &DCR_CLASS dcr_kodak_ycbcr_load_raw; p->filters = 0; break;
  5013. case 32803: p->load_raw = &DCR_CLASS dcr_kodak_65000_load_raw;
  5014. }
  5015. case 32867: break;
  5016. default: p->is_raw = 0;
  5017. }
  5018. if (!p->dng_version && p->tiff_samples == 3)
  5019. if (p->tiff_ifd[raw].bytes && p->tiff_bps != 14 && p->tiff_bps != 2048)
  5020. p->is_raw = 0;
  5021. if (!p->dng_version && p->tiff_bps == 8 && p->tiff_compress == 1 &&
  5022. p->tiff_ifd[raw].phint == 1) p->is_raw = 0;
  5023. if (p->tiff_bps == 8 && p->tiff_samples == 4) p->is_raw = 0;
  5024. for (i=0; i < (int)p->tiff_nifds; i++)
  5025. if (i != raw && p->tiff_ifd[i].samples == max_samp &&
  5026. p->tiff_ifd[i].width * p->tiff_ifd[i].height / SQR(p->tiff_ifd[i].bps+1) >
  5027. (int)(p->thumb_width * p->thumb_height / SQR(p->thumb_misc+1))) {
  5028. p->thumb_width = p->tiff_ifd[i].width;
  5029. p->thumb_height = p->tiff_ifd[i].height;
  5030. p->thumb_offset = p->tiff_ifd[i].offset;
  5031. p->thumb_length = p->tiff_ifd[i].bytes;
  5032. p->thumb_misc = p->tiff_ifd[i].bps;
  5033. thm = i;
  5034. }
  5035. if (thm >= 0) {
  5036. p->thumb_misc |= p->tiff_ifd[thm].samples << 5;
  5037. switch (p->tiff_ifd[thm].comp) {
  5038. case 0:
  5039. p->write_thumb = &DCR_CLASS dcr_layer_thumb;
  5040. break;
  5041. case 1:
  5042. if (p->tiff_ifd[thm].bps > 8)
  5043. p->thumb_load_raw = &DCR_CLASS dcr_kodak_thumb_load_raw;
  5044. else
  5045. p->write_thumb = &DCR_CLASS dcr_ppm_thumb;
  5046. break;
  5047. case 65000:
  5048. p->thumb_load_raw = p->tiff_ifd[thm].phint == 6 ?
  5049. &DCR_CLASS dcr_kodak_ycbcr_load_raw : &DCR_CLASS dcr_kodak_rgb_load_raw;
  5050. }
  5051. }
  5052. }
  5053. void DCR_CLASS dcr_parse_minolta (DCRAW* p, int base)
  5054. {
  5055. int save, tag, len, offset, high=0, wide=0, i, c;
  5056. short sorder=p->order;
  5057. dcr_fseek(p->obj_, base, SEEK_SET);
  5058. if (dcr_fgetc(p->obj_) || dcr_fgetc(p->obj_)-'M' || dcr_fgetc(p->obj_)-'R') return;
  5059. p->order = dcr_fgetc(p->obj_) * 0x101;
  5060. offset = base + dcr_get4(p) + 8;
  5061. while ((save=dcr_ftell(p->obj_)) < offset) {
  5062. for (tag=i=0; i < 4; i++)
  5063. tag = tag << 8 | dcr_fgetc(p->obj_);
  5064. len = dcr_get4(p);
  5065. switch (tag) {
  5066. case 0x505244: /* PRD */
  5067. dcr_fseek(p->obj_, 8, SEEK_CUR);
  5068. high = dcr_get2(p);
  5069. wide = dcr_get2(p);
  5070. break;
  5071. case 0x574247: /* WBG */
  5072. dcr_get4(p);
  5073. i = strcmp(p->model,"DiMAGE A200") ? 0:3;
  5074. FORC4 p->cam_mul[c ^ (c >> 1) ^ i] = dcr_get2(p);
  5075. break;
  5076. case 0x545457: /* TTW */
  5077. dcr_parse_tiff (p, dcr_ftell(p->obj_));
  5078. p->data_offset = offset;
  5079. }
  5080. dcr_fseek(p->obj_, save+len+8, SEEK_SET);
  5081. }
  5082. p->raw_height = high;
  5083. p->raw_width = wide;
  5084. p->order = sorder;
  5085. }
  5086. /*
  5087. Many cameras have a "debug mode" that writes JPEG and raw
  5088. at the same time. The raw file has no header, so try to
  5089. to open the matching JPEG file and read its metadata.
  5090. */
  5091. void DCR_CLASS dcr_parse_external_jpeg(DCRAW* p)
  5092. {
  5093. char *file, *ext, *jname, *jfile, *jext;
  5094. dcr_stream_ops *sops_;
  5095. dcr_stream_obj *sobj_;
  5096. ext = strrchr (p->ifname, '.');
  5097. file = strrchr (p->ifname, '/');
  5098. if (!file) file = strrchr (p->ifname, '\\');
  5099. if (!file) file = p->ifname-1;
  5100. file++;
  5101. if (!ext || strlen(ext) != 4 || ext-file != 8) return;
  5102. jname = (char *) malloc (strlen(p->ifname) + 1);
  5103. dcr_merror (p, jname, "parse_external_jpeg()");
  5104. strcpy (jname, p->ifname);
  5105. jfile = file - p->ifname + jname;
  5106. jext = ext - p->ifname + jname;
  5107. if (strcasecmp (ext, ".jpg")) {
  5108. strcpy (jext, isupper(ext[1]) ? ".JPG":".jpg");
  5109. if (isdigit(*file)) {
  5110. memcpy (jfile, file+4, 4);
  5111. memcpy (jfile+4, file, 4);
  5112. }
  5113. } else
  5114. while (isdigit(*--jext)) {
  5115. if (*jext != '9') {
  5116. (*jext)++;
  5117. break;
  5118. }
  5119. *jext = '0';
  5120. }
  5121. if (strcmp (jname, p->ifname)) {
  5122. sops_ = p->ops_;
  5123. sobj_ = p->obj_;
  5124. p->ops_ = &dcr_stream_fileops;
  5125. if ((p->obj_ = fopen (jname, "rb"))) {
  5126. if (p->opt.verbose)
  5127. fprintf (stderr,_("Reading metadata from %s ...\n"), jname);
  5128. dcr_parse_tiff (p, 12);
  5129. p->thumb_offset = 0;
  5130. p->is_raw = 1;
  5131. dcr_fclose(p->obj_);
  5132. }
  5133. p->ops_ = sops_;
  5134. p->obj_ = sobj_;
  5135. }
  5136. if (!p->timestamp)
  5137. fprintf (stderr,_("Failed to read metadata from %s\n"), jname);
  5138. free (jname);
  5139. }
  5140. /*
  5141. CIFF block 0x1030 contains an 8x8 p->white sample.
  5142. Load this into p->white[][] for use in scale_colors().
  5143. */
  5144. void DCR_CLASS dcr_ciff_block_1030(DCRAW* p)
  5145. {
  5146. static const ushort key[] = { 0x410, 0x45f3 };
  5147. int i, bpp, row, col, vbits=0;
  5148. unsigned long bitbuf=0;
  5149. if ((dcr_get2(p),dcr_get4(p)) != 0x80008 || !dcr_get4(p)) return;
  5150. bpp = dcr_get2(p);
  5151. if (bpp != 10 && bpp != 12) return;
  5152. for (i=row=0; row < 8; row++)
  5153. for (col=0; col < 8; col++) {
  5154. if (vbits < bpp) {
  5155. bitbuf = bitbuf << 16 | (dcr_get2(p) ^ key[i++ & 1]);
  5156. vbits += 16;
  5157. }
  5158. p->white[row][col] = (unsigned short)(
  5159. bitbuf << (LONG_BIT - vbits) >> (LONG_BIT - bpp));
  5160. vbits -= bpp;
  5161. }
  5162. }
  5163. /*
  5164. Parse a CIFF file, better known as Canon CRW format.
  5165. */
  5166. void DCR_CLASS dcr_parse_ciff (DCRAW* p, int offset, int length)
  5167. {
  5168. int tboff, nrecs, c, type, len, save, wbi=-1;
  5169. ushort key[] = { 0x410, 0x45f3 };
  5170. dcr_fseek(p->obj_, offset+length-4, SEEK_SET);
  5171. tboff = dcr_get4(p) + offset;
  5172. dcr_fseek(p->obj_, tboff, SEEK_SET);
  5173. nrecs = dcr_get2(p);
  5174. if (nrecs > 100) return;
  5175. while (nrecs--) {
  5176. type = dcr_get2(p);
  5177. len = dcr_get4(p);
  5178. save = dcr_ftell(p->obj_) + 4;
  5179. dcr_fseek(p->obj_, offset+dcr_get4(p), SEEK_SET);
  5180. if ((((type >> 8) + 8) | 8) == 0x38)
  5181. dcr_parse_ciff (p,dcr_ftell(p->obj_), len); /* Parse a sub-table */
  5182. if (type == 0x0810)
  5183. dcr_fread(p->obj_, p->artist, 64, 1);
  5184. if (type == 0x080a) {
  5185. dcr_fread(p->obj_, p->make, 64, 1);
  5186. dcr_fseek(p->obj_, strlen(p->make) - 63, SEEK_CUR);
  5187. dcr_fread(p->obj_, p->model, 64, 1);
  5188. }
  5189. if (type == 0x1810) {
  5190. dcr_fseek(p->obj_, 12, SEEK_CUR);
  5191. p->flip = dcr_get4(p);
  5192. }
  5193. if (type == 0x1835) /* Get the decoder table */
  5194. p->tiff_compress = dcr_get4(p);
  5195. if (type == 0x2007) {
  5196. p->thumb_offset = dcr_ftell(p->obj_);
  5197. p->thumb_length = len;
  5198. }
  5199. if (type == 0x1818) {
  5200. p->shutter = (float)pow (2, -dcr_int_to_float((dcr_get4(p),dcr_get4(p))));
  5201. p->aperture = (float)pow (2, dcr_int_to_float(dcr_get4(p))/2);
  5202. }
  5203. if (type == 0x102a) {
  5204. p->iso_speed = (float)pow (2, (dcr_get4(p),dcr_get2(p))/32.0f - 4) * 50;
  5205. p->aperture = (float)pow (2, (dcr_get2(p),(short)dcr_get2(p))/64.0f);
  5206. p->shutter = (float)pow (2,-((short)dcr_get2(p))/32.0f);
  5207. wbi = (dcr_get2(p),dcr_get2(p));
  5208. if (wbi > 17) wbi = 0;
  5209. dcr_fseek(p->obj_, 32, SEEK_CUR);
  5210. if (p->shutter > 1e6) p->shutter = dcr_get2(p)/10.0f;
  5211. }
  5212. if (type == 0x102c) {
  5213. if (dcr_get2(p) > 512) { /* Pro90, G1 */
  5214. dcr_fseek(p->obj_, 118, SEEK_CUR);
  5215. FORC4 p->cam_mul[c ^ 2] = dcr_get2(p);
  5216. } else { /* G2, S30, S40 */
  5217. dcr_fseek(p->obj_, 98, SEEK_CUR);
  5218. FORC4 p->cam_mul[c ^ (c >> 1) ^ 1] = dcr_get2(p);
  5219. }
  5220. }
  5221. if (type == 0x0032) {
  5222. if (len == 768) { /* EOS D30 */
  5223. dcr_fseek(p->obj_, 72, SEEK_CUR);
  5224. FORC4 p->cam_mul[c ^ (c >> 1)] = 1024.0f / dcr_get2(p);
  5225. if (!wbi) p->cam_mul[0] = -1; /* use my auto p->white balance */
  5226. } else if (!p->cam_mul[0]) {
  5227. if (dcr_get2(p) == key[0]) /* Pro1, G6, S60, S70 */
  5228. c = (strstr(p->model,"Pro1") ?
  5229. "012346000000000000":"01345:000000006008")[wbi]-'0'+ 2;
  5230. else { /* G3, G5, S45, S50 */
  5231. c = "023457000000006000"[wbi]-'0';
  5232. key[0] = key[1] = 0;
  5233. }
  5234. dcr_fseek(p->obj_, 78 + c*8, SEEK_CUR);
  5235. FORC4 p->cam_mul[c ^ (c >> 1) ^ 1] = (float)(dcr_get2(p) ^ key[c & 1]);
  5236. if (!wbi) p->cam_mul[0] = -1;
  5237. }
  5238. }
  5239. if (type == 0x10a9) { /* D60, 10D, 300D, and clones */
  5240. if (len > 66) wbi = "0134567028"[wbi]-'0';
  5241. dcr_fseek(p->obj_, 2 + wbi*8, SEEK_CUR);
  5242. FORC4 p->cam_mul[c ^ (c >> 1)] = dcr_get2(p);
  5243. }
  5244. if (type == 0x1030 && (0x18040 >> wbi & 1))
  5245. dcr_ciff_block_1030(p); /* all that don't have 0x10a9 */
  5246. if (type == 0x1031) {
  5247. p->raw_width = (dcr_get2(p),dcr_get2(p));
  5248. p->raw_height = dcr_get2(p);
  5249. }
  5250. if (type == 0x5029) {
  5251. p->focal_len = (float)(len >> 16);
  5252. if ((len & 0xffff) == 2) p->focal_len /= 32;
  5253. }
  5254. if (type == 0x5813) p->flash_used = dcr_int_to_float(len);
  5255. if (type == 0x5814) p->canon_ev = dcr_int_to_float(len);
  5256. if (type == 0x5817) p->shot_order = len;
  5257. if (type == 0x5834) p->unique_id = len;
  5258. if (type == 0x580e) p->timestamp = len;
  5259. if (type == 0x180e) p->timestamp = dcr_get4(p);
  5260. #ifdef LOCALTIME
  5261. if ((type | 0x4000) == 0x580e)
  5262. p->timestamp = mktime (gmtime (&p->timestamp));
  5263. #endif
  5264. dcr_fseek(p->obj_, save, SEEK_SET);
  5265. }
  5266. }
  5267. void DCR_CLASS dcr_parse_rollei(DCRAW* p)
  5268. {
  5269. char line[128], *val;
  5270. struct tm t;
  5271. dcr_fseek(p->obj_, 0, SEEK_SET);
  5272. memset (&t, 0, sizeof t);
  5273. do {
  5274. dcr_fgets(p->obj_, line, 128);
  5275. if ((val = strchr(line,'=')))
  5276. *val++ = 0;
  5277. else
  5278. val = line + strlen(line);
  5279. if (!strcmp(line,"DAT"))
  5280. sscanf (val, "%d.%d.%d", &t.tm_mday, &t.tm_mon, &t.tm_year);
  5281. if (!strcmp(line,"TIM"))
  5282. sscanf (val, "%d:%d:%d", &t.tm_hour, &t.tm_min, &t.tm_sec);
  5283. if (!strcmp(line,"HDR"))
  5284. p->thumb_offset = atoi(val);
  5285. if (!strcmp(line,"X "))
  5286. p->raw_width = atoi(val);
  5287. if (!strcmp(line,"Y "))
  5288. p->raw_height = atoi(val);
  5289. if (!strcmp(line,"TX "))
  5290. p->thumb_width = atoi(val);
  5291. if (!strcmp(line,"TY "))
  5292. p->thumb_height = atoi(val);
  5293. } while (strncmp(line,"EOHD",4));
  5294. p->data_offset = p->thumb_offset + p->thumb_width * p->thumb_height * 2;
  5295. t.tm_year -= 1900;
  5296. t.tm_mon -= 1;
  5297. if (mktime(&t) > 0)
  5298. p->timestamp = mktime(&t);
  5299. strcpy (p->make, "Rollei");
  5300. strcpy (p->model,"d530flex");
  5301. p->write_thumb = &DCR_CLASS dcr_rollei_thumb;
  5302. }
  5303. void DCR_CLASS dcr_parse_sinar_ia(DCRAW* p)
  5304. {
  5305. int entries, off;
  5306. char str[8], *cp;
  5307. p->order = 0x4949;
  5308. dcr_fseek(p->obj_, 4, SEEK_SET);
  5309. entries = dcr_get4(p);
  5310. dcr_fseek(p->obj_, dcr_get4(p), SEEK_SET);
  5311. while (entries--) {
  5312. off = dcr_get4(p); dcr_get4(p);
  5313. dcr_fread(p->obj_, str, 8, 1);
  5314. if (!strcmp(str,"META")) p->meta_offset = off;
  5315. if (!strcmp(str,"THUMB")) p->thumb_offset = off;
  5316. if (!strcmp(str,"RAW0")) p->data_offset = off;
  5317. }
  5318. dcr_fseek(p->obj_, p->meta_offset+20, SEEK_SET);
  5319. dcr_fread(p->obj_, p->make, 64, 1);
  5320. p->make[63] = 0;
  5321. if ((cp = strchr(p->make,' '))) {
  5322. strcpy (p->model, cp+1);
  5323. *cp = 0;
  5324. }
  5325. p->raw_width = dcr_get2(p);
  5326. p->raw_height = dcr_get2(p);
  5327. p->load_raw = &DCR_CLASS dcr_unpacked_load_raw;
  5328. p->thumb_width = (dcr_get4(p),dcr_get2(p));
  5329. p->thumb_height = dcr_get2(p);
  5330. p->write_thumb = &DCR_CLASS dcr_ppm_thumb;
  5331. p->maximum = 0x3fff;
  5332. }
  5333. void DCR_CLASS dcr_parse_phase_one (DCRAW* p, int base)
  5334. {
  5335. unsigned entries, tag, type, len, data, save, i, c;
  5336. float romm_cam[3][3];
  5337. char *cp;
  5338. memset (&p->ph1, 0, sizeof p->ph1);
  5339. dcr_fseek(p->obj_, base, SEEK_SET);
  5340. p->order = dcr_get4(p) & 0xffff;
  5341. if (dcr_get4(p) >> 8 != 0x526177) return; /* "Raw" */
  5342. dcr_fseek(p->obj_, dcr_get4(p)+base, SEEK_SET);
  5343. entries = dcr_get4(p);
  5344. dcr_get4(p);
  5345. while (entries--) {
  5346. tag = dcr_get4(p);
  5347. type = dcr_get4(p);
  5348. len = dcr_get4(p);
  5349. data = dcr_get4(p);
  5350. save = dcr_ftell(p->obj_);
  5351. dcr_fseek(p->obj_, base+data, SEEK_SET);
  5352. switch (tag) {
  5353. case 0x100: p->flip = "0653"[data & 3]-'0'; break;
  5354. case 0x106:
  5355. for (i=0; i < 9; i++)
  5356. romm_cam[0][i] = (float)dcr_getreal(p, 11);
  5357. dcr_romm_coeff (p,romm_cam);
  5358. break;
  5359. case 0x107:
  5360. FORC3 p->cam_mul[c] = (float)dcr_getreal(p, 11);
  5361. break;
  5362. case 0x108: p->raw_width = data; break;
  5363. case 0x109: p->raw_height = data; break;
  5364. case 0x10a: p->left_margin = data; break;
  5365. case 0x10b: p->top_margin = data; break;
  5366. case 0x10c: p->width = data; break;
  5367. case 0x10d: p->height = data; break;
  5368. case 0x10e: p->ph1.format = data; break;
  5369. case 0x10f: p->data_offset = data+base; break;
  5370. case 0x110: p->meta_offset = data+base;
  5371. p->meta_length = len; break;
  5372. case 0x112: p->ph1.key_off = save - 4; break;
  5373. case 0x210: p->ph1.tag_210 = dcr_int_to_float(data); break;
  5374. case 0x21a: p->ph1.tag_21a = data; break;
  5375. case 0x21c: p->strip_offset = data+base; break;
  5376. case 0x21d: p->ph1.black = data; break;
  5377. case 0x222: p->ph1.split_col = data - p->left_margin; break;
  5378. case 0x223: p->ph1.black_off = data+base; break;
  5379. case 0x301:
  5380. p->model[63] = 0;
  5381. dcr_fread(p->obj_, p->model, 1, 63);
  5382. if ((cp = strstr(p->model," camera"))) *cp = 0;
  5383. }
  5384. dcr_fseek(p->obj_, save, SEEK_SET);
  5385. }
  5386. p->load_raw = p->ph1.format < 3 ?
  5387. &DCR_CLASS dcr_phase_one_load_raw : &DCR_CLASS dcr_phase_one_load_raw_c;
  5388. p->maximum = 0xffff;
  5389. strcpy (p->make, "Phase One");
  5390. if (p->model[0]) return;
  5391. switch (p->raw_height) {
  5392. case 2060: strcpy (p->model,"LightPhase"); break;
  5393. case 2682: strcpy (p->model,"H 10"); break;
  5394. case 4128: strcpy (p->model,"H 20"); break;
  5395. case 5488: strcpy (p->model,"H 25"); break;
  5396. }
  5397. }
  5398. void DCR_CLASS dcr_parse_fuji (DCRAW* p, int offset)
  5399. {
  5400. unsigned entries, tag, len, save, c;
  5401. dcr_fseek(p->obj_, offset, SEEK_SET);
  5402. entries = dcr_get4(p);
  5403. if (entries > 255) return;
  5404. while (entries--) {
  5405. tag = dcr_get2(p);
  5406. len = dcr_get2(p);
  5407. save = dcr_ftell(p->obj_);
  5408. if (tag == 0x100) {
  5409. p->raw_height = dcr_get2(p);
  5410. p->raw_width = dcr_get2(p);
  5411. } else if (tag == 0x121) {
  5412. p->height = dcr_get2(p);
  5413. if ((p->width = dcr_get2(p)) == 4284) p->width += 3;
  5414. } else if (tag == 0x130)
  5415. p->fuji_layout = dcr_fgetc(p->obj_) >> 7;
  5416. if (tag == 0x2ff0)
  5417. FORC4 p->cam_mul[c ^ 1] = dcr_get2(p);
  5418. dcr_fseek(p->obj_, save+len, SEEK_SET);
  5419. }
  5420. p->height <<= p->fuji_layout;
  5421. p->width >>= p->fuji_layout;
  5422. }
  5423. int DCR_CLASS dcr_parse_jpeg (DCRAW* p, int offset)
  5424. {
  5425. int len, save, hlen, mark;
  5426. dcr_fseek(p->obj_, offset, SEEK_SET);
  5427. if (dcr_fgetc(p->obj_) != 0xff || dcr_fgetc(p->obj_) != 0xd8) return 0;
  5428. while (dcr_fgetc(p->obj_) == 0xff && (mark = dcr_fgetc(p->obj_)) != 0xda) {
  5429. p->order = 0x4d4d;
  5430. len = dcr_get2(p) - 2;
  5431. save = dcr_ftell(p->obj_);
  5432. if (mark == 0xc0 || mark == 0xc3) {
  5433. dcr_fgetc(p->obj_);
  5434. p->raw_height = dcr_get2(p);
  5435. p->raw_width = dcr_get2(p);
  5436. }
  5437. p->order = dcr_get2(p);
  5438. hlen = dcr_get4(p);
  5439. if (dcr_get4(p) == 0x48454150) /* "HEAP" */
  5440. dcr_parse_ciff (p,save+hlen, len-hlen);
  5441. dcr_parse_tiff (p, save+6);
  5442. dcr_fseek(p->obj_, save+len, SEEK_SET);
  5443. }
  5444. return 1;
  5445. }
  5446. void DCR_CLASS dcr_parse_riff(DCRAW* p)
  5447. {
  5448. unsigned i, size, end;
  5449. char tag[4], date[64], month[64];
  5450. static const char mon[12][4] =
  5451. { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
  5452. struct tm t;
  5453. p->order = 0x4949;
  5454. dcr_fread(p->obj_, tag, 4, 1);
  5455. size = dcr_get4(p);
  5456. end = dcr_ftell(p->obj_) + size;
  5457. if (!memcmp(tag,"RIFF",4) || !memcmp(tag,"LIST",4)) {
  5458. dcr_get4(p);
  5459. while (dcr_ftell(p->obj_)+7 < (long)end)
  5460. dcr_parse_riff(p);
  5461. } else if (!memcmp(tag,"nctg",4)) {
  5462. while (dcr_ftell(p->obj_)+7 < (long)end) {
  5463. i = dcr_get2(p);
  5464. size = dcr_get2(p);
  5465. if ((i+1) >> 1 == 10 && size == 20)
  5466. dcr_get_timestamp(p,0);
  5467. else dcr_fseek(p->obj_, size, SEEK_CUR);
  5468. }
  5469. } else if (!memcmp(tag,"IDIT",4) && size < 64) {
  5470. dcr_fread(p->obj_, date, 64, 1);
  5471. date[size] = 0;
  5472. memset (&t, 0, sizeof t);
  5473. if (sscanf (date, "%*s %s %d %d:%d:%d %d", month, &t.tm_mday,
  5474. &t.tm_hour, &t.tm_min, &t.tm_sec, &t.tm_year) == 6) {
  5475. for (i=0; i < 12 && strcasecmp(mon[i],month); i++);
  5476. t.tm_mon = i;
  5477. t.tm_year -= 1900;
  5478. if (mktime(&t) > 0)
  5479. p->timestamp = mktime(&t);
  5480. }
  5481. } else
  5482. dcr_fseek(p->obj_, size, SEEK_CUR);
  5483. }
  5484. void DCR_CLASS dcr_parse_smal (DCRAW* p, int offset, int fsize)
  5485. {
  5486. int ver;
  5487. dcr_fseek(p->obj_, offset+2, SEEK_SET);
  5488. p->order = 0x4949;
  5489. ver = dcr_fgetc(p->obj_);
  5490. if (ver == 6)
  5491. dcr_fseek(p->obj_, 5, SEEK_CUR);
  5492. if ((int)dcr_get4(p) != fsize) return;
  5493. if (ver > 6) p->data_offset = dcr_get4(p);
  5494. p->raw_height = p->height = dcr_get2(p);
  5495. p->raw_width = p->width = dcr_get2(p);
  5496. strcpy (p->make, "SMaL");
  5497. sprintf (p->model, "v%d %dx%d", ver, p->width, p->height);
  5498. if (ver == 6) p->load_raw = &DCR_CLASS dcr_smal_v6_load_raw;
  5499. if (ver == 9) p->load_raw = &DCR_CLASS dcr_smal_v9_load_raw;
  5500. }
  5501. void DCR_CLASS dcr_parse_cine(DCRAW* p)
  5502. {
  5503. unsigned off_head, off_setup, off_image, i;
  5504. p->order = 0x4949;
  5505. dcr_fseek(p->obj_, 4, SEEK_SET);
  5506. p->is_raw = dcr_get2(p) == 2;
  5507. dcr_fseek(p->obj_, 14, SEEK_CUR);
  5508. p->is_raw *= dcr_get4(p);
  5509. off_head = dcr_get4(p);
  5510. off_setup = dcr_get4(p);
  5511. off_image = dcr_get4(p);
  5512. p->timestamp = dcr_get4(p);
  5513. if ((i = dcr_get4(p))) p->timestamp = i;
  5514. dcr_fseek(p->obj_, off_head+4, SEEK_SET);
  5515. p->raw_width = dcr_get4(p);
  5516. p->raw_height = dcr_get4(p);
  5517. switch (dcr_get2(p),dcr_get2(p)) {
  5518. case 8: p->load_raw = &DCR_CLASS dcr_eight_bit_load_raw; break;
  5519. case 16: p->load_raw = &DCR_CLASS dcr_unpacked_load_raw;
  5520. }
  5521. dcr_fseek(p->obj_, off_setup+792, SEEK_SET);
  5522. strcpy (p->make, "CINE");
  5523. sprintf (p->model, "%d", dcr_get4(p));
  5524. dcr_fseek(p->obj_, 12, SEEK_CUR);
  5525. switch ((i=dcr_get4(p)) & 0xffffff) {
  5526. case 3: p->filters = 0x94949494; break;
  5527. case 4: p->filters = 0x49494949; break;
  5528. default: p->is_raw = 0;
  5529. }
  5530. dcr_fseek(p->obj_, 72, SEEK_CUR);
  5531. switch ((dcr_get4(p)+3600) % 360) {
  5532. case 270: p->flip = 4; break;
  5533. case 180: p->flip = 1; break;
  5534. case 90: p->flip = 7; break;
  5535. case 0: p->flip = 2;
  5536. }
  5537. p->cam_mul[0] = (float)dcr_getreal(p, 11);
  5538. p->cam_mul[2] = (float)dcr_getreal(p, 11);
  5539. p->maximum = ~(-1 << dcr_get4(p));
  5540. dcr_fseek(p->obj_, 668, SEEK_CUR);
  5541. p->shutter = dcr_get4(p)/1000000000.0f;
  5542. dcr_fseek(p->obj_, off_image, SEEK_SET);
  5543. if (p->opt.shot_select < p->is_raw)
  5544. dcr_fseek(p->obj_, p->opt.shot_select*8, SEEK_CUR);
  5545. p->data_offset = (off_t) dcr_get4(p) + 8;
  5546. p->data_offset += (off_t) ((__int64)dcr_get4(p) << 32);
  5547. }
  5548. char * DCR_CLASS dcr_foveon_gets (DCRAW* p, int offset, char *str, int len)
  5549. {
  5550. int i;
  5551. dcr_fseek(p->obj_, offset, SEEK_SET);
  5552. for (i=0; i < len-1; i++)
  5553. if ((str[i] = (char)dcr_get2(p)) == 0) break;
  5554. str[i] = 0;
  5555. return str;
  5556. }
  5557. void DCR_CLASS dcr_parse_foveon(DCRAW* p)
  5558. {
  5559. int entries, img=0, off, len, tag, save, i, wide, high, pent, poff[256][2];
  5560. char name[64], value[64];
  5561. p->order = 0x4949; /* Little-endian */
  5562. dcr_fseek(p->obj_, 36, SEEK_SET);
  5563. p->flip = dcr_get4(p);
  5564. dcr_fseek(p->obj_, -4, SEEK_END);
  5565. dcr_fseek(p->obj_, dcr_get4(p), SEEK_SET);
  5566. if (dcr_get4(p) != 0x64434553) return; /* SECd */
  5567. entries = (dcr_get4(p),dcr_get4(p));
  5568. while (entries--) {
  5569. off = dcr_get4(p);
  5570. len = dcr_get4(p);
  5571. tag = dcr_get4(p);
  5572. save = dcr_ftell(p->obj_);
  5573. dcr_fseek(p->obj_, off, SEEK_SET);
  5574. if (dcr_get4(p) != (unsigned int)(0x20434553 | (tag << 24))) return;
  5575. switch (tag) {
  5576. case 0x47414d49: /* IMAG */
  5577. case 0x32414d49: /* IMA2 */
  5578. dcr_fseek(p->obj_, 12, SEEK_CUR);
  5579. wide = dcr_get4(p);
  5580. high = dcr_get4(p);
  5581. if (wide > p->raw_width && high > p->raw_height) {
  5582. p->raw_width = wide;
  5583. p->raw_height = high;
  5584. p->data_offset = off+24;
  5585. }
  5586. dcr_fseek(p->obj_, off+28, SEEK_SET);
  5587. if (dcr_fgetc(p->obj_) == 0xff && dcr_fgetc(p->obj_) == 0xd8
  5588. && (int)p->thumb_length < len-28) {
  5589. p->thumb_offset = off+28;
  5590. p->thumb_length = len-28;
  5591. p->write_thumb = &DCR_CLASS dcr_jpeg_thumb;
  5592. }
  5593. if (++img == 2 && !p->thumb_length) {
  5594. #if RESTRICTED
  5595. p->thumb_offset = off+24;
  5596. p->thumb_width = wide;
  5597. p->thumb_height = high;
  5598. p->write_thumb = &DCR_CLASS dcr_foveon_thumb;
  5599. #endif //RESTRICTED
  5600. }
  5601. break;
  5602. case 0x464d4143: /* CAMF */
  5603. p->meta_offset = off+24;
  5604. p->meta_length = len-28;
  5605. if (p->meta_length > 0x20000)
  5606. p->meta_length = 0x20000;
  5607. break;
  5608. case 0x504f5250: /* PROP */
  5609. pent = (dcr_get4(p),dcr_get4(p));
  5610. dcr_fseek(p->obj_, 12, SEEK_CUR);
  5611. off += pent*8 + 24;
  5612. if ((unsigned) pent > 256) pent=256;
  5613. for (i=0; i < pent*2; i++)
  5614. poff[0][i] = off + dcr_get4(p)*2;
  5615. for (i=0; i < pent; i++) {
  5616. dcr_foveon_gets (p, poff[i][0], name, 64);
  5617. dcr_foveon_gets (p, poff[i][1], value, 64);
  5618. if (!strcmp (name, "ISO"))
  5619. p->iso_speed = (float)atoi(value);
  5620. if (!strcmp (name, "CAMMANUF"))
  5621. strcpy (p->make, value);
  5622. if (!strcmp (name, "CAMMODEL"))
  5623. strcpy (p->model, value);
  5624. if (!strcmp (name, "WB_DESC"))
  5625. strcpy (p->model2, value);
  5626. if (!strcmp (name, "TIME"))
  5627. p->timestamp = atoi(value);
  5628. if (!strcmp (name, "EXPTIME"))
  5629. p->shutter = atoi(value) / 1000000.0f;
  5630. if (!strcmp (name, "APERTURE"))
  5631. p->aperture = (float)atof(value);
  5632. if (!strcmp (name, "FLENGTH"))
  5633. p->focal_len = (float)atof(value);
  5634. }
  5635. #ifdef LOCALTIME
  5636. p->timestamp = mktime (gmtime (&p->timestamp));
  5637. #endif
  5638. }
  5639. dcr_fseek(p->obj_, save, SEEK_SET);
  5640. }
  5641. p->is_foveon = 1;
  5642. }
  5643. /*
  5644. Thanks to Adobe for providing these excellent CAM -> XYZ matrices!
  5645. */
  5646. void DCR_CLASS dcr_adobe_coeff (DCRAW* p, char *make, char *model)
  5647. {
  5648. static const struct {
  5649. const char *prefix;
  5650. short black, maximum, trans[12];
  5651. } table[] = {
  5652. { "Apple QuickTake", 0, 0, /* DJC */
  5653. { 17576,-3191,-3318,5210,6733,-1942,9031,1280,-124 } },
  5654. { "Canon EOS D2000", 0, 0,
  5655. { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
  5656. { "Canon EOS D6000", 0, 0,
  5657. { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
  5658. { "Canon EOS D30", 0, 0,
  5659. { 9805,-2689,-1312,-5803,13064,3068,-2438,3075,8775 } },
  5660. { "Canon EOS D60", 0, 0xfa0,
  5661. { 6188,-1341,-890,-7168,14489,2937,-2640,3228,8483 } },
  5662. { "Canon EOS 5D Mark II", 0, 0x3cf0,
  5663. { 4716,603,-830,-7798,15474,2480,-1496,1937,6651 } },
  5664. { "Canon EOS 5D", 0, 0xe6c,
  5665. { 6347,-479,-972,-8297,15954,2480,-1968,2131,7649 } },
  5666. { "Canon EOS 10D", 0, 0xfa0,
  5667. { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
  5668. { "Canon EOS 20Da", 0, 0,
  5669. { 14155,-5065,-1382,-6550,14633,2039,-1623,1824,6561 } },
  5670. { "Canon EOS 20D", 0, 0xfff,
  5671. { 6599,-537,-891,-8071,15783,2424,-1983,2234,7462 } },
  5672. { "Canon EOS 30D", 0, 0,
  5673. { 6257,-303,-1000,-7880,15621,2396,-1714,1904,7046 } },
  5674. { "Canon EOS 40D", 0, 0x3f60,
  5675. { 6071,-747,-856,-7653,15365,2441,-2025,2553,7315 } },
  5676. { "Canon EOS 50D", 0, 0x3d93,
  5677. { 4920,616,-593,-6493,13964,2784,-1774,3178,7005 } },
  5678. { "Canon EOS 300D", 0, 0xfa0,
  5679. { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
  5680. { "Canon EOS 350D", 0, 0xfff,
  5681. { 6018,-617,-965,-8645,15881,2975,-1530,1719,7642 } },
  5682. { "Canon EOS 400D", 0, 0xe8e,
  5683. { 7054,-1501,-990,-8156,15544,2812,-1278,1414,7796 } },
  5684. { "Canon EOS 450D", 0, 0x390d,
  5685. { 5784,-262,-821,-7539,15064,2672,-1982,2681,7427 } },
  5686. { "Canon EOS 1000D", 0, 0xe43,
  5687. { 6771,-1139,-977,-7818,15123,2928,-1244,1437,7533 } },
  5688. { "Canon EOS-1Ds Mark III", 0, 0x3bb0,
  5689. { 5859,-211,-930,-8255,16017,2353,-1732,1887,7448 } },
  5690. { "Canon EOS-1Ds Mark II", 0, 0xe80,
  5691. { 6517,-602,-867,-8180,15926,2378,-1618,1771,7633 } },
  5692. { "Canon EOS-1D Mark II N", 0, 0xe80,
  5693. { 6240,-466,-822,-8180,15825,2500,-1801,1938,8042 } },
  5694. { "Canon EOS-1D Mark III", 0, 0x3bb0,
  5695. { 6291,-540,-976,-8350,16145,2311,-1714,1858,7326 } },
  5696. { "Canon EOS-1D Mark II", 0, 0xe80,
  5697. { 6264,-582,-724,-8312,15948,2504,-1744,1919,8664 } },
  5698. { "Canon EOS-1DS", 0, 0xe20,
  5699. { 4374,3631,-1743,-7520,15212,2472,-2892,3632,8161 } },
  5700. { "Canon EOS-1D", 0, 0xe20,
  5701. { 6806,-179,-1020,-8097,16415,1687,-3267,4236,7690 } },
  5702. { "Canon EOS", 0, 0,
  5703. { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
  5704. { "Canon PowerShot A50", 0, 0,
  5705. { -5300,9846,1776,3436,684,3939,-5540,9879,6200,-1404,11175,217 } },
  5706. { "Canon PowerShot A5", 0, 0,
  5707. { -4801,9475,1952,2926,1611,4094,-5259,10164,5947,-1554,10883,547 } },
  5708. { "Canon PowerShot G10", 0, 0,
  5709. { 11093,-3906,-1028,-5047,12492,2879,-1003,1750,5561 } },
  5710. { "Canon PowerShot G1", 0, 0,
  5711. { -4778,9467,2172,4743,-1141,4344,-5146,9908,6077,-1566,11051,557 } },
  5712. { "Canon PowerShot G2", 0, 0,
  5713. { 9087,-2693,-1049,-6715,14382,2537,-2291,2819,7790 } },
  5714. { "Canon PowerShot G3", 0, 0,
  5715. { 9212,-2781,-1073,-6573,14189,2605,-2300,2844,7664 } },
  5716. { "Canon PowerShot G5", 0, 0,
  5717. { 9757,-2872,-933,-5972,13861,2301,-1622,2328,7212 } },
  5718. { "Canon PowerShot G6", 0, 0,
  5719. { 9877,-3775,-871,-7613,14807,3072,-1448,1305,7485 } },
  5720. { "Canon PowerShot G9", 0, 0,
  5721. { 7368,-2141,-598,-5621,13254,2625,-1418,1696,5743 } },
  5722. { "Canon PowerShot Pro1", 0, 0,
  5723. { 10062,-3522,-999,-7643,15117,2730,-765,817,7323 } },
  5724. { "Canon PowerShot Pro70", 34, 0,
  5725. { -4155,9818,1529,3939,-25,4522,-5521,9870,6610,-2238,10873,1342 } },
  5726. { "Canon PowerShot Pro90", 0, 0,
  5727. { -4963,9896,2235,4642,-987,4294,-5162,10011,5859,-1770,11230,577 } },
  5728. { "Canon PowerShot S30", 0, 0,
  5729. { 10566,-3652,-1129,-6552,14662,2006,-2197,2581,7670 } },
  5730. { "Canon PowerShot S40", 0, 0,
  5731. { 8510,-2487,-940,-6869,14231,2900,-2318,2829,9013 } },
  5732. { "Canon PowerShot S45", 0, 0,
  5733. { 8163,-2333,-955,-6682,14174,2751,-2077,2597,8041 } },
  5734. { "Canon PowerShot S50", 0, 0,
  5735. { 8882,-2571,-863,-6348,14234,2288,-1516,2172,6569 } },
  5736. { "Canon PowerShot S60", 0, 0,
  5737. { 8795,-2482,-797,-7804,15403,2573,-1422,1996,7082 } },
  5738. { "Canon PowerShot S70", 0, 0,
  5739. { 9976,-3810,-832,-7115,14463,2906,-901,989,7889 } },
  5740. { "Canon PowerShot A610", 0, 0, /* DJC */
  5741. { 15591,-6402,-1592,-5365,13198,2168,-1300,1824,5075 } },
  5742. { "Canon PowerShot A620", 0, 0, /* DJC */
  5743. { 15265,-6193,-1558,-4125,12116,2010,-888,1639,5220 } },
  5744. { "Canon PowerShot A630", 0, 0, /* DJC */
  5745. { 14201,-5308,-1757,-6087,14472,1617,-2191,3105,5348 } },
  5746. { "Canon PowerShot A640", 0, 0, /* DJC */
  5747. { 13124,-5329,-1390,-3602,11658,1944,-1612,2863,4885 } },
  5748. { "Canon PowerShot A650", 0, 0, /* DJC */
  5749. { 9427,-3036,-959,-2581,10671,1911,-1039,1982,4430 } },
  5750. { "Canon PowerShot A720", 0, 0, /* DJC */
  5751. { 14573,-5482,-1546,-1266,9799,1468,-1040,1912,3810 } },
  5752. { "Canon PowerShot S3 IS", 0, 0, /* DJC */
  5753. { 14062,-5199,-1446,-4712,12470,2243,-1286,2028,4836 } },
  5754. { "CINE 650", 0, 0,
  5755. { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
  5756. { "CINE 660", 0, 0,
  5757. { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
  5758. { "CINE", 0, 0,
  5759. { 20183,-4295,-423,-3940,15330,3985,-280,4870,9800 } },
  5760. { "Contax N Digital", 0, 0xf1e,
  5761. { 7777,1285,-1053,-9280,16543,2916,-3677,5679,7060 } },
  5762. { "EPSON R-D1", 0, 0,
  5763. { 6827,-1878,-732,-8429,16012,2564,-704,592,7145 } },
  5764. { "FUJIFILM FinePix E550", 0, 0,
  5765. { 11044,-3888,-1120,-7248,15168,2208,-1531,2277,8069 } },
  5766. { "FUJIFILM FinePix E900", 0, 0,
  5767. { 9183,-2526,-1078,-7461,15071,2574,-2022,2440,8639 } },
  5768. { "FUJIFILM FinePix F8", 0, 0,
  5769. { 11044,-3888,-1120,-7248,15168,2208,-1531,2277,8069 } },
  5770. { "FUJIFILM FinePix F7", 0, 0,
  5771. { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
  5772. { "FUJIFILM FinePix S100FS", 514, 0,
  5773. { 11521,-4355,-1065,-6524,13767,3058,-1466,1984,6045 } },
  5774. { "FUJIFILM FinePix S20Pro", 0, 0,
  5775. { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
  5776. { "FUJIFILM FinePix S2Pro", 128, 0,
  5777. { 12492,-4690,-1402,-7033,15423,1647,-1507,2111,7697 } },
  5778. { "FUJIFILM FinePix S3Pro", 0, 0,
  5779. { 11807,-4612,-1294,-8927,16968,1988,-2120,2741,8006 } },
  5780. { "FUJIFILM FinePix S5Pro", 0, 0,
  5781. { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
  5782. { "FUJIFILM FinePix S5000", 0, 0,
  5783. { 8754,-2732,-1019,-7204,15069,2276,-1702,2334,6982 } },
  5784. { "FUJIFILM FinePix S5100", 0, 0x3e00,
  5785. { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
  5786. { "FUJIFILM FinePix S5500", 0, 0x3e00,
  5787. { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
  5788. { "FUJIFILM FinePix S5200", 0, 0,
  5789. { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
  5790. { "FUJIFILM FinePix S5600", 0, 0,
  5791. { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
  5792. { "FUJIFILM FinePix S6", 0, 0,
  5793. { 12628,-4887,-1401,-6861,14996,1962,-2198,2782,7091 } },
  5794. { "FUJIFILM FinePix S7000", 0, 0,
  5795. { 10190,-3506,-1312,-7153,15051,2238,-2003,2399,7505 } },
  5796. { "FUJIFILM FinePix S9000", 0, 0,
  5797. { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
  5798. { "FUJIFILM FinePix S9500", 0, 0,
  5799. { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
  5800. { "FUJIFILM FinePix S9100", 0, 0,
  5801. { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
  5802. { "FUJIFILM FinePix S9600", 0, 0,
  5803. { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
  5804. { "FUJIFILM IS-1", 0, 0,
  5805. { 21461,-10807,-1441,-2332,10599,1999,289,875,7703 } },
  5806. { "FUJIFILM IS Pro", 0, 0,
  5807. { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
  5808. { "Imacon Ixpress", 0, 0, /* DJC */
  5809. { 7025,-1415,-704,-5188,13765,1424,-1248,2742,6038 } },
  5810. { "KODAK NC2000", 0, 0,
  5811. { 13891,-6055,-803,-465,9919,642,2121,82,1291 } },
  5812. { "Kodak DCS315C", 8, 0,
  5813. { 17523,-4827,-2510,756,8546,-137,6113,1649,2250 } },
  5814. { "Kodak DCS330C", 8, 0,
  5815. { 20620,-7572,-2801,-103,10073,-396,3551,-233,2220 } },
  5816. { "KODAK DCS420", 0, 0,
  5817. { 10868,-1852,-644,-1537,11083,484,2343,628,2216 } },
  5818. { "KODAK DCS460", 0, 0,
  5819. { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
  5820. { "KODAK EOSDCS1", 0, 0,
  5821. { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
  5822. { "KODAK EOSDCS3B", 0, 0,
  5823. { 9898,-2700,-940,-2478,12219,206,1985,634,1031 } },
  5824. { "Kodak DCS520C", 180, 0,
  5825. { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
  5826. { "Kodak DCS560C", 188, 0,
  5827. { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
  5828. { "Kodak DCS620C", 180, 0,
  5829. { 23617,-10175,-3149,-2054,11749,-272,2586,-489,3453 } },
  5830. { "Kodak DCS620X", 185, 0,
  5831. { 13095,-6231,154,12221,-21,-2137,895,4602,2258 } },
  5832. { "Kodak DCS660C", 214, 0,
  5833. { 18244,-6351,-2739,-791,11193,-521,3711,-129,2802 } },
  5834. { "Kodak DCS720X", 0, 0,
  5835. { 11775,-5884,950,9556,1846,-1286,-1019,6221,2728 } },
  5836. { "Kodak DCS760C", 0, 0,
  5837. { 16623,-6309,-1411,-4344,13923,323,2285,274,2926 } },
  5838. { "Kodak DCS Pro SLR", 0, 0,
  5839. { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
  5840. { "Kodak DCS Pro 14nx", 0, 0,
  5841. { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
  5842. { "Kodak DCS Pro 14", 0, 0,
  5843. { 7791,3128,-776,-8588,16458,2039,-2455,4006,6198 } },
  5844. { "Kodak ProBack645", 0, 0,
  5845. { 16414,-6060,-1470,-3555,13037,473,2545,122,4948 } },
  5846. { "Kodak ProBack", 0, 0,
  5847. { 21179,-8316,-2918,-915,11019,-165,3477,-180,4210 } },
  5848. { "KODAK P712", 0, 0,
  5849. { 9658,-3314,-823,-5163,12695,2768,-1342,1843,6044 } },
  5850. { "KODAK P850", 0, 0xf7c,
  5851. { 10511,-3836,-1102,-6946,14587,2558,-1481,1792,6246 } },
  5852. { "KODAK P880", 0, 0xfff,
  5853. { 12805,-4662,-1376,-7480,15267,2360,-1626,2194,7904 } },
  5854. { "Leaf CMost", 0, 0,
  5855. { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
  5856. { "Leaf Valeo 6", 0, 0,
  5857. { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
  5858. { "Leaf Aptus 54S", 0, 0,
  5859. { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
  5860. { "Leaf Aptus 65", 0, 0,
  5861. { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
  5862. { "Leaf Aptus 75", 0, 0,
  5863. { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
  5864. { "Leaf", 0, 0,
  5865. { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
  5866. { "Mamiya ZD", 0, 0,
  5867. { 7645,2579,-1363,-8689,16717,2015,-3712,5941,5961 } },
  5868. { "Micron 2010", 110, 0, /* DJC */
  5869. { 16695,-3761,-2151,155,9682,163,3433,951,4904 } },
  5870. { "Minolta DiMAGE 5", 0, 0xf7d,
  5871. { 8983,-2942,-963,-6556,14476,2237,-2426,2887,8014 } },
  5872. { "Minolta DiMAGE 7Hi", 0, 0xf7d,
  5873. { 11368,-3894,-1242,-6521,14358,2339,-2475,3056,7285 } },
  5874. { "Minolta DiMAGE 7", 0, 0xf7d,
  5875. { 9144,-2777,-998,-6676,14556,2281,-2470,3019,7744 } },
  5876. { "Minolta DiMAGE A1", 0, 0xf8b,
  5877. { 9274,-2547,-1167,-8220,16323,1943,-2273,2720,8340 } },
  5878. { "MINOLTA DiMAGE A200", 0, 0,
  5879. { 8560,-2487,-986,-8112,15535,2771,-1209,1324,7743 } },
  5880. { "Minolta DiMAGE A2", 0, 0xf8f,
  5881. { 9097,-2726,-1053,-8073,15506,2762,-966,981,7763 } },
  5882. { "Minolta DiMAGE Z2", 0, 0, /* DJC */
  5883. { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
  5884. { "MINOLTA DYNAX 5", 0, 0xffb,
  5885. { 10284,-3283,-1086,-7957,15762,2316,-829,882,6644 } },
  5886. { "MINOLTA DYNAX 7", 0, 0xffb,
  5887. { 10239,-3104,-1099,-8037,15727,2451,-927,925,6871 } },
  5888. { "NIKON D100", 0, 0,
  5889. { 5902,-933,-782,-8983,16719,2354,-1402,1455,6464 } },
  5890. { "NIKON D1H", 0, 0,
  5891. { 7577,-2166,-926,-7454,15592,1934,-2377,2808,8606 } },
  5892. { "NIKON D1X", 0, 0,
  5893. { 7702,-2245,-975,-9114,17242,1875,-2679,3055,8521 } },
  5894. { "NIKON D1", 0, 0, /* multiplied by 2.218750, 1.0, 1.148438 */
  5895. { 16772,-4726,-2141,-7611,15713,1972,-2846,3494,9521 } },
  5896. { "NIKON D2H", 0, 0,
  5897. { 5710,-901,-615,-8594,16617,2024,-2975,4120,6830 } },
  5898. { "NIKON D2X", 0, 0,
  5899. { 10231,-2769,-1255,-8301,15900,2552,-797,680,7148 } },
  5900. { "NIKON D40X", 0, 0,
  5901. { 8819,-2543,-911,-9025,16928,2151,-1329,1213,8449 } },
  5902. { "NIKON D40", 0, 0,
  5903. { 6992,-1668,-806,-8138,15748,2543,-874,850,7897 } },
  5904. { "NIKON D50", 0, 0,
  5905. { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
  5906. { "NIKON D60", 0, 0,
  5907. { 8736,-2458,-935,-9075,16894,2251,-1354,1242,8263 } },
  5908. { "NIKON D700", 0, 0,
  5909. { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
  5910. { "NIKON D70", 0, 0,
  5911. { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
  5912. { "NIKON D80", 0, 0,
  5913. { 8629,-2410,-883,-9055,16940,2171,-1490,1363,8520 } },
  5914. { "NIKON D90", 0, 0xf00,
  5915. { 7309,-1403,-519,-8474,16008,2622,-2434,2826,8064 } },
  5916. { "NIKON D200", 0, 0xfbc,
  5917. { 8367,-2248,-763,-8758,16447,2422,-1527,1550,8053 } },
  5918. { "NIKON D300", 0, 0,
  5919. { 9030,-1992,-715,-8465,16302,2255,-2689,3217,8069 } },
  5920. { "NIKON D3", 0, 0,
  5921. { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
  5922. { "NIKON E950", 0, 0x3dd, /* DJC */
  5923. { -3746,10611,1665,9621,-1734,2114,-2389,7082,3064,3406,6116,-244 } },
  5924. { "NIKON E995", 0, 0, /* copied from E5000 */
  5925. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  5926. { "NIKON E2100", 0, 0, /* copied from Z2, new white balance */
  5927. { 13142,-4152,-1596,-4655,12374,2282,-1769,2696,6711} },
  5928. { "NIKON E2500", 0, 0,
  5929. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  5930. { "NIKON E4300", 0, 0, /* copied from Minolta DiMAGE Z2 */
  5931. { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
  5932. { "NIKON E4500", 0, 0,
  5933. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  5934. { "NIKON E5000", 0, 0,
  5935. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  5936. { "NIKON E5400", 0, 0,
  5937. { 9349,-2987,-1001,-7919,15766,2266,-2098,2680,6839 } },
  5938. { "NIKON E5700", 0, 0,
  5939. { -5368,11478,2368,5537,-113,3148,-4969,10021,5782,778,9028,211 } },
  5940. { "NIKON E8400", 0, 0,
  5941. { 7842,-2320,-992,-8154,15718,2599,-1098,1342,7560 } },
  5942. { "NIKON E8700", 0, 0,
  5943. { 8489,-2583,-1036,-8051,15583,2643,-1307,1407,7354 } },
  5944. { "NIKON E8800", 0, 0,
  5945. { 7971,-2314,-913,-8451,15762,2894,-1442,1520,7610 } },
  5946. { "NIKON COOLPIX P6000", 0, 0,
  5947. { 9698,-3367,-914,-4706,12584,2368,-837,968,5801 } },
  5948. { "OLYMPUS C5050", 0, 0,
  5949. { 10508,-3124,-1273,-6079,14294,1901,-1653,2306,6237 } },
  5950. { "OLYMPUS C5060", 0, 0,
  5951. { 10445,-3362,-1307,-7662,15690,2058,-1135,1176,7602 } },
  5952. { "OLYMPUS C7070", 0, 0,
  5953. { 10252,-3531,-1095,-7114,14850,2436,-1451,1723,6365 } },
  5954. { "OLYMPUS C70", 0, 0,
  5955. { 10793,-3791,-1146,-7498,15177,2488,-1390,1577,7321 } },
  5956. { "OLYMPUS C80", 0, 0,
  5957. { 8606,-2509,-1014,-8238,15714,2703,-942,979,7760 } },
  5958. { "OLYMPUS E-10", 0, (short)0xffc0,
  5959. { 12745,-4500,-1416,-6062,14542,1580,-1934,2256,6603 } },
  5960. { "OLYMPUS E-1", 0, (short)0xfff0,
  5961. { 11846,-4767,-945,-7027,15878,1089,-2699,4122,8311 } },
  5962. { "OLYMPUS E-20", 0, (short)0xffc0,
  5963. { 13173,-4732,-1499,-5807,14036,1895,-2045,2452,7142 } },
  5964. { "OLYMPUS E-300", 0, 0,
  5965. { 7828,-1761,-348,-5788,14071,1830,-2853,4518,6557 } },
  5966. { "OLYMPUS E-330", 0, 0,
  5967. { 8961,-2473,-1084,-7979,15990,2067,-2319,3035,8249 } },
  5968. { "OLYMPUS E-3", 0, 0xf99,
  5969. { 9487,-2875,-1115,-7533,15606,2010,-1618,2100,7389 } },
  5970. { "OLYMPUS E-400", 0, (short)0xfff0,
  5971. { 6169,-1483,-21,-7107,14761,2536,-2904,3580,8568 } },
  5972. { "OLYMPUS E-410", 0, 0xf6a,
  5973. { 8856,-2582,-1026,-7761,15766,2082,-2009,2575,7469 } },
  5974. { "OLYMPUS E-420", 0, 0xfd7,
  5975. { 8746,-2425,-1095,-7594,15612,2073,-1780,2309,7416 } },
  5976. { "OLYMPUS E-500", 0, 0,
  5977. { 8136,-1968,-299,-5481,13742,1871,-2556,4205,6630 } },
  5978. { "OLYMPUS E-510", 0, 0xf6a,
  5979. { 8785,-2529,-1033,-7639,15624,2112,-1783,2300,7817 } },
  5980. { "OLYMPUS E-520", 0, 0xfd2,
  5981. { 8344,-2322,-1020,-7596,15635,2048,-1748,2269,7287 } },
  5982. { "OLYMPUS SP350", 0, 0,
  5983. { 12078,-4836,-1069,-6671,14306,2578,-786,939,7418 } },
  5984. { "OLYMPUS SP3", 0, 0,
  5985. { 11766,-4445,-1067,-6901,14421,2707,-1029,1217,7572 } },
  5986. { "OLYMPUS SP500UZ", 0, 0xfff,
  5987. { 9493,-3415,-666,-5211,12334,3260,-1548,2262,6482 } },
  5988. { "OLYMPUS SP510UZ", 0, 0xffe,
  5989. { 10593,-3607,-1010,-5881,13127,3084,-1200,1805,6721 } },
  5990. { "OLYMPUS SP550UZ", 0, 0xffe,
  5991. { 11597,-4006,-1049,-5432,12799,2957,-1029,1750,6516 } },
  5992. { "OLYMPUS SP560UZ", 0, 0xff9,
  5993. { 10915,-3677,-982,-5587,12986,2911,-1168,1968,6223 } },
  5994. { "OLYMPUS SP570UZ", 0, 0,
  5995. { 11522,-4044,-1146,-4736,12172,2904,-988,1829,6039 } },
  5996. { "PENTAX *ist DL2", 0, 0,
  5997. { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
  5998. { "PENTAX *ist DL", 0, 0,
  5999. { 10829,-2838,-1115,-8339,15817,2696,-837,680,11939 } },
  6000. { "PENTAX *ist DS2", 0, 0,
  6001. { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
  6002. { "PENTAX *ist DS", 0, 0,
  6003. { 10371,-2333,-1206,-8688,16231,2602,-1230,1116,11282 } },
  6004. { "PENTAX *ist D", 0, 0,
  6005. { 9651,-2059,-1189,-8881,16512,2487,-1460,1345,10687 } },
  6006. { "PENTAX K10D", 0, 0,
  6007. { 9566,-2863,-803,-7170,15172,2112,-818,803,9705 } },
  6008. { "PENTAX K1", 0, 0,
  6009. { 11095,-3157,-1324,-8377,15834,2720,-1108,947,11688 } },
  6010. { "PENTAX K20D", 0, 0,
  6011. { 9427,-2714,-868,-7493,16092,1373,-2199,3264,7180 } },
  6012. { "PENTAX K200D", 0, 0,
  6013. { 9186,-2678,-907,-8693,16517,2260,-1129,1094,8524 } },
  6014. { "PENTAX K2000", 0, 0,
  6015. { 11057,-3604,-1155,-5152,13046,2329,-282,375,8104 } },
  6016. { "Panasonic DMC-FZ8", 0, (short)0xf7f0,
  6017. { 8986,-2755,-802,-6341,13575,3077,-1476,2144,6379 } },
  6018. { "Panasonic DMC-FZ18", 0, 0,
  6019. { 9932,-3060,-935,-5809,13331,2753,-1267,2155,5575 } },
  6020. { "Panasonic DMC-FZ28", 15, 0xfff,
  6021. { 10109,-3488,-993,-5412,12812,2916,-1305,2140,5543 } },
  6022. { "Panasonic DMC-FZ30", 0, (short)0xf94c,
  6023. { 10976,-4029,-1141,-7918,15491,2600,-1670,2071,8246 } },
  6024. { "Panasonic DMC-FZ50", 0, (short)0xfff0, /* aka "LEICA V-LUX1" */
  6025. { 7906,-2709,-594,-6231,13351,3220,-1922,2631,6537 } },
  6026. { "Panasonic DMC-L10", 15, 0xf96,
  6027. { 8025,-1942,-1050,-7920,15904,2100,-2456,3005,7039 } },
  6028. { "Panasonic DMC-L1", 0, (short)0xf7fc, /* aka "LEICA DIGILUX 3" */
  6029. { 8054,-1885,-1025,-8349,16367,2040,-2805,3542,7629 } },
  6030. { "Panasonic DMC-LC1", 0, 0, /* aka "LEICA DIGILUX 2" */
  6031. { 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
  6032. { "Panasonic DMC-LX1", 0, (short)0xf7f0, /* aka "LEICA D-LUX2" */
  6033. { 10704,-4187,-1230,-8314,15952,2501,-920,945,8927 } },
  6034. { "Panasonic DMC-LX2", 0, 0, /* aka "LEICA D-LUX3" */
  6035. { 8048,-2810,-623,-6450,13519,3272,-1700,2146,7049 } },
  6036. { "Panasonic DMC-LX3", 15, 0xfff, /* aka "LEICA D-LUX4" */
  6037. { 8128,-2668,-655,-6134,13307,3161,-1782,2568,6083 } },
  6038. { "Panasonic DMC-FX150", 15, 0xfff,
  6039. { 9082,-2907,-925,-6119,13377,3058,-1797,2641,5609 } },
  6040. { "Panasonic DMC-G1", 15, 0xfff,
  6041. { 8199,-2065,-1056,-8124,16156,2033,-2458,3022,7220 } },
  6042. { "Phase One H 20", 0, 0, /* DJC */
  6043. { 1313,1855,-109,-6715,15908,808,-327,1840,6020 } },
  6044. { "Phase One P 2", 0, 0,
  6045. { 2905,732,-237,-8134,16626,1476,-3038,4253,7517 } },
  6046. { "Phase One P 30", 0, 0,
  6047. { 4516,-245,-37,-7020,14976,2173,-3206,4671,7087 } },
  6048. { "Phase One P 45", 0, 0,
  6049. { 5053,-24,-117,-5684,14076,1702,-2619,4492,5849 } },
  6050. { "SAMSUNG GX-1", 0, 0,
  6051. { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
  6052. { "Sinar", 0, 0, /* DJC */
  6053. { 16442,-2956,-2422,-2877,12128,750,-1136,6066,4559 } },
  6054. { "SONY DSC-F828", 491, 0,
  6055. { 7924,-1910,-777,-8226,15459,2998,-1517,2199,6818,-7242,11401,3481 } },
  6056. { "SONY DSC-R1", 512, 0,
  6057. { 8512,-2641,-694,-8042,15670,2526,-1821,2117,7414 } },
  6058. { "SONY DSC-V3", 0, 0,
  6059. { 7511,-2571,-692,-7894,15088,3060,-948,1111,8128 } },
  6060. { "SONY DSLR-A100", 0, 0xfeb,
  6061. { 9437,-2811,-774,-8405,16215,2290,-710,596,7181 } },
  6062. { "SONY DSLR-A200", 0, 0,
  6063. { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
  6064. { "SONY DSLR-A300", 0, 0,
  6065. { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
  6066. { "SONY DSLR-A350", 0, 0xffc,
  6067. { 6038,-1484,-578,-9146,16746,2513,-875,746,7217 } },
  6068. { "SONY DSLR-A700", 254, 0x1ffe,
  6069. { 5775,-805,-359,-8574,16295,2391,-1943,2341,7249 } },
  6070. { "SONY DSLR-A900", 254, 0x1ffe,
  6071. { 5209,-1072,-397,-8845,16120,2919,-1618,1803,8654 } }
  6072. };
  6073. double cam_xyz[4][3];
  6074. char name[130];
  6075. int i, j;
  6076. sprintf (name, "%s %s", make, model);
  6077. for (i=0; i < sizeof table / sizeof *table; i++)
  6078. if (!strncmp (name, table[i].prefix, strlen(table[i].prefix))) {
  6079. if (table[i].black) p->black = (ushort) table[i].black;
  6080. if (table[i].maximum) p->maximum = (ushort) table[i].maximum;
  6081. for (j=0; j < 12; j++)
  6082. cam_xyz[0][j] = table[i].trans[j] / 10000.0;
  6083. dcr_cam_xyz_coeff (p, cam_xyz);
  6084. break;
  6085. }
  6086. }
  6087. void DCR_CLASS dcr_simple_coeff (DCRAW* p, int index)
  6088. {
  6089. static const float table[][12] = {
  6090. /* index 0 -- all Foveon cameras */
  6091. { 1.4032f,-0.2231f,-0.1016f,-0.5263f,1.4816f,0.017f,-0.0112f,0.0183f,0.9113f },
  6092. /* index 1 -- Kodak DC20 and DC25 */
  6093. { 2.25f,0.75f,-1.75f,-0.25f,-0.25f,0.75f,0.75f,-0.25f,-0.25f,-1.75f,0.75f,2.25f },
  6094. /* index 2 -- Logitech Fotoman Pixtura */
  6095. { 1.893f,-0.418f,-0.476f,-0.495f,1.773f,-0.278f,-1.017f,-0.655f,2.672f },
  6096. /* index 3 -- Nikon E880, E900, and E990 */
  6097. { -1.936280f, 1.800443f, -1.448486f, 2.584324f,
  6098. 1.405365f, -0.524955f, -0.289090f, 0.408680f,
  6099. -1.204965f, 1.082304f, 2.941367f, -1.818705f }
  6100. };
  6101. int i, c;
  6102. for (p->raw_color = i=0; i < 3; i++)
  6103. FORCC(p) p->rgb_cam[i][c] = table[index][i*p->colors+c];
  6104. }
  6105. short DCR_CLASS dcr_guess_byte_order (DCRAW* p, int words)
  6106. {
  6107. uchar test[4][2];
  6108. int t=2, msb;
  6109. double diff, sum[2] = {0,0};
  6110. dcr_fread(p->obj_, test[0], 2, 2);
  6111. for (words-=2; words--; ) {
  6112. dcr_fread(p->obj_, test[t], 2, 1);
  6113. for (msb=0; msb < 2; msb++) {
  6114. diff = (test[t^2][msb] << 8 | test[t^2][!msb])
  6115. - (test[t ][msb] << 8 | test[t ][!msb]);
  6116. sum[msb] += diff*diff;
  6117. }
  6118. t = (t+1) & 3;
  6119. }
  6120. return sum[0] < sum[1] ? 0x4d4d : 0x4949;
  6121. }
  6122. /*
  6123. Identify which camera created this file, and set global variables
  6124. accordingly.
  6125. */
  6126. void DCR_CLASS dcr_identify(DCRAW* p)
  6127. {
  6128. char head[32], *cp;
  6129. unsigned hlen, fsize, i, c, is_canon;
  6130. struct dcr_jhead jh;
  6131. static const struct {
  6132. int fsize;
  6133. char make[12], model[19], withjpeg;
  6134. } table[] = {
  6135. { 62464, "Kodak", "DC20" ,0 },
  6136. { 124928, "Kodak", "DC20" ,0 },
  6137. { 1652736, "Kodak", "DCS200" ,0 },
  6138. { 4159302, "Kodak", "C330" ,0 },
  6139. { 4162462, "Kodak", "C330" ,0 },
  6140. { 460800, "Kodak", "C603v" ,0 },
  6141. { 614400, "Kodak", "C603v" ,0 },
  6142. { 6163328, "Kodak", "C603" ,0 },
  6143. { 6166488, "Kodak", "C603" ,0 },
  6144. { 9116448, "Kodak", "C603y" ,0 },
  6145. { 311696, "ST Micro", "STV680 VGA" ,0 }, /* SPYz */
  6146. { 614400, "Kodak", "KAI-0340" ,0 },
  6147. { 787456, "Creative", "PC-CAM 600" ,0 },
  6148. { 1138688, "Minolta", "RD175" ,0 },
  6149. { 3840000, "Foculus", "531C" ,0 },
  6150. { 786432, "AVT", "F-080C" ,0 },
  6151. { 1447680, "AVT", "F-145C" ,0 },
  6152. { 1920000, "AVT", "F-201C" ,0 },
  6153. { 5067304, "AVT", "F-510C" ,0 },
  6154. { 10134608, "AVT", "F-510C" ,0 },
  6155. { 16157136, "AVT", "F-810C" ,0 },
  6156. { 1409024, "Sony", "XCD-SX910CR" ,0 },
  6157. { 2818048, "Sony", "XCD-SX910CR" ,0 },
  6158. { 3884928, "Micron", "2010" ,0 },
  6159. { 6624000, "Pixelink", "A782" ,0 },
  6160. { 13248000, "Pixelink", "A782" ,0 },
  6161. { 6291456, "RoverShot","3320AF" ,0 },
  6162. { 6553440, "Canon", "PowerShot A460" ,0 },
  6163. { 6653280, "Canon", "PowerShot A530" ,0 },
  6164. { 6573120, "Canon", "PowerShot A610" ,0 },
  6165. { 9219600, "Canon", "PowerShot A620" ,0 },
  6166. { 10341600, "Canon", "PowerShot A720" ,0 },
  6167. { 10383120, "Canon", "PowerShot A630" ,0 },
  6168. { 12945240, "Canon", "PowerShot A640" ,0 },
  6169. { 15636240, "Canon", "PowerShot A650" ,0 },
  6170. { 5298000, "Canon", "PowerShot SD300" ,0 },
  6171. { 7710960, "Canon", "PowerShot S3 IS" ,0 },
  6172. { 5939200, "OLYMPUS", "C770UZ" ,0 },
  6173. { 1581060, "NIKON", "E900" ,1 }, /* or E900s,E910 */
  6174. { 2465792, "NIKON", "E950" ,1 }, /* or E800,E700 */
  6175. { 2940928, "NIKON", "E2100" ,1 }, /* or E2500 */
  6176. { 4771840, "NIKON", "E990" ,1 }, /* or E995, Oly C3030Z */
  6177. { 4775936, "NIKON", "E3700" ,1 }, /* or Optio 33WR */
  6178. { 5869568, "NIKON", "E4300" ,1 }, /* or DiMAGE Z2 */
  6179. { 5865472, "NIKON", "E4500" ,1 },
  6180. { 7438336, "NIKON", "E5000" ,1 }, /* or E5700 */
  6181. { 8998912, "NIKON", "COOLPIX S6" ,1 },
  6182. { 1976352, "CASIO", "QV-2000UX" ,1 },
  6183. { 3217760, "CASIO", "QV-3*00EX" ,1 },
  6184. { 6218368, "CASIO", "QV-5700" ,1 },
  6185. { 6054400, "CASIO", "QV-R41" ,1 },
  6186. { 7530816, "CASIO", "QV-R51" ,1 },
  6187. { 7684000, "CASIO", "QV-4000" ,1 },
  6188. { 4948608, "CASIO", "EX-S100" ,1 },
  6189. { 7542528, "CASIO", "EX-Z50" ,1 },
  6190. { 7753344, "CASIO", "EX-Z55" ,1 },
  6191. { 7426656, "CASIO", "EX-P505" ,1 },
  6192. { 9313536, "CASIO", "EX-P600" ,1 },
  6193. { 10979200, "CASIO", "EX-P700" ,1 },
  6194. { 3178560, "PENTAX", "Optio S" ,1 },
  6195. { 4841984, "PENTAX", "Optio S" ,1 },
  6196. { 6114240, "PENTAX", "Optio S4" ,1 }, /* or S4i, CASIO EX-Z4 */
  6197. { 10702848, "PENTAX", "Optio 750Z" ,1 },
  6198. { 16098048, "SAMSUNG", "S85" ,1 },
  6199. { 16215552, "SAMSUNG", "S85" ,1 },
  6200. { 12582980, "Sinar", "" ,0 },
  6201. { 33292868, "Sinar", "" ,0 },
  6202. { 44390468, "Sinar", "" ,0 } };
  6203. static const char *corp[] =
  6204. { "Canon", "NIKON", "EPSON", "KODAK", "Kodak", "OLYMPUS", "PENTAX",
  6205. "MINOLTA", "Minolta", "Konica", "CASIO", "Sinar", "Phase One",
  6206. "SAMSUNG", "Mamiya" };
  6207. p->tiff_flip = p->flip = p->filters = -1; /* 0 is valid, so -1 is unknown */
  6208. p->raw_height = p->raw_width = p->fuji_width = p->fuji_layout = p->cr2_slice[0] = 0;
  6209. p->maximum = p->height = p->width = p->top_margin = p->left_margin = 0;
  6210. p->cdesc[0] = p->desc[0] = p->artist[0] = p->make[0] = p->model[0] = p->model2[0] = 0;
  6211. p->iso_speed = p->shutter = p->aperture = p->focal_len = 0.0f; p->unique_id = 0;
  6212. memset (p->gpsdata, 0, sizeof p->gpsdata);
  6213. memset (p->white, 0, sizeof p->white);
  6214. p->thumb_offset = p->thumb_length = p->thumb_width = p->thumb_height = 0;
  6215. p->load_raw = p->thumb_load_raw = 0;
  6216. p->write_thumb = &DCR_CLASS dcr_jpeg_thumb;
  6217. p->data_offset = p->meta_length = p->tiff_bps = p->tiff_compress = 0;
  6218. p->kodak_cbpp = p->zero_after_ff = p->dng_version = p->load_flags = 0;
  6219. p->timestamp = p->shot_order = p->tiff_samples = p->black = p->is_foveon = 0;
  6220. p->mix_green = p->profile_length = p->data_error = p->zero_is_bad = 0;
  6221. p->pixel_aspect = p->is_raw = p->raw_color = p->use_gamma = 1;
  6222. p->tile_width = p->tile_length = INT_MAX;
  6223. for (i=0; i < 4; i++) {
  6224. p->cam_mul[i] = (float)(i == 1);
  6225. p->pre_mul[i] = (float)(i < 3);
  6226. FORC3 p->cmatrix[c][i] = 0.0f;
  6227. FORC3 p->rgb_cam[c][i] = (float)(c == i);
  6228. }
  6229. p->colors = 3;
  6230. p->tiff_bps = 12;
  6231. for (i=0; i < 0x4000; i++) p->curve[i] = i;
  6232. p->order = dcr_get2(p);
  6233. hlen = dcr_get4(p);
  6234. dcr_fseek(p->obj_, 0, SEEK_SET);
  6235. dcr_fread(p->obj_, head, 1, 32);
  6236. dcr_fseek(p->obj_, 0, SEEK_END);
  6237. fsize = dcr_ftell(p->obj_);
  6238. if ((cp = (char *) memmem (head, 32, "MMMM", 4)) ||
  6239. (cp = (char *) memmem (head, 32, "IIII", 4))) {
  6240. dcr_parse_phase_one (p,cp-head);
  6241. if (cp-head) dcr_parse_tiff(p,0);
  6242. } else if (p->order == 0x4949 || p->order == 0x4d4d) {
  6243. if (!memcmp (head+6,"HEAPCCDR",8)) {
  6244. p->data_offset = hlen;
  6245. dcr_parse_ciff (p,hlen, fsize - hlen);
  6246. } else {
  6247. dcr_parse_tiff(p,0);
  6248. }
  6249. } else if (!memcmp (head,"\xff\xd8\xff\xe1",4) &&
  6250. !memcmp (head+6,"Exif",4)) {
  6251. dcr_fseek(p->obj_, 4, SEEK_SET);
  6252. p->data_offset = 4 + dcr_get2(p);
  6253. dcr_fseek(p->obj_, p->data_offset, SEEK_SET);
  6254. if (dcr_fgetc(p->obj_) != 0xff)
  6255. dcr_parse_tiff(p,12);
  6256. p->thumb_offset = 0;
  6257. } else if (!memcmp (head+25,"ARECOYK",7)) {
  6258. strcpy (p->make, "Contax");
  6259. strcpy (p->model,"N Digital");
  6260. dcr_fseek(p->obj_, 33, SEEK_SET);
  6261. dcr_get_timestamp(p,1);
  6262. dcr_fseek(p->obj_, 60, SEEK_SET);
  6263. FORC4 p->cam_mul[c ^ (c >> 1)] = (float)dcr_get4(p);
  6264. } else if (!strcmp (head, "PXN")) {
  6265. strcpy (p->make, "Logitech");
  6266. strcpy (p->model,"Fotoman Pixtura");
  6267. } else if (!strcmp (head, "qktk")) {
  6268. strcpy (p->make, "Apple");
  6269. strcpy (p->model,"QuickTake 100");
  6270. } else if (!strcmp (head, "qktn")) {
  6271. strcpy (p->make, "Apple");
  6272. strcpy (p->model,"QuickTake 150");
  6273. } else if (!memcmp (head,"FUJIFILM",8)) {
  6274. dcr_fseek(p->obj_, 84, SEEK_SET);
  6275. p->thumb_offset = dcr_get4(p);
  6276. p->thumb_length = dcr_get4(p);
  6277. dcr_fseek(p->obj_, 92, SEEK_SET);
  6278. dcr_parse_fuji (p,dcr_get4(p));
  6279. if (p->thumb_offset > 120) {
  6280. dcr_fseek(p->obj_, 120, SEEK_SET);
  6281. p->is_raw += (i = dcr_get4(p)) && 1;
  6282. if (p->is_raw == 2 && p->opt.shot_select)
  6283. dcr_parse_fuji (p,i);
  6284. }
  6285. dcr_fseek(p->obj_, 100, SEEK_SET);
  6286. p->data_offset = dcr_get4(p);
  6287. dcr_parse_tiff (p, p->thumb_offset+12);
  6288. } else if (!memcmp (head,"RIFF",4)) {
  6289. dcr_fseek(p->obj_, 0, SEEK_SET);
  6290. dcr_parse_riff(p);
  6291. } else if (!memcmp (head,"\0\001\0\001\0@",6)) {
  6292. dcr_fseek(p->obj_, 6, SEEK_SET);
  6293. dcr_fread(p->obj_, p->make, 1, 8);
  6294. dcr_fread(p->obj_, p->model, 1, 8);
  6295. dcr_fread(p->obj_, p->model2, 1, 16);
  6296. p->data_offset = dcr_get2(p);
  6297. dcr_get2(p);
  6298. p->raw_width = dcr_get2(p);
  6299. p->raw_height = dcr_get2(p);
  6300. p->load_raw = &DCR_CLASS nokia_load_raw;
  6301. p->filters = 0x61616161;
  6302. } else if (!memcmp (head,"DSC-Image",9))
  6303. dcr_parse_rollei(p);
  6304. else if (!memcmp (head,"PWAD",4))
  6305. dcr_parse_sinar_ia(p);
  6306. else if (!memcmp (head,"\0MRM",4))
  6307. dcr_parse_minolta(p, 0);
  6308. else if (!memcmp (head,"FOVb",4))
  6309. dcr_parse_foveon(p);
  6310. else if (!memcmp (head,"CI",2))
  6311. dcr_parse_cine(p);
  6312. else
  6313. for (i=0; i < sizeof table / sizeof *table; i++)
  6314. if ((int)fsize == table[i].fsize) {
  6315. strcpy (p->make, table[i].make );
  6316. strcpy (p->model, table[i].model);
  6317. if (table[i].withjpeg)
  6318. dcr_parse_external_jpeg(p);
  6319. }
  6320. if (p->make[0] == 0) dcr_parse_smal (p,0, fsize);
  6321. if (p->make[0] == 0) dcr_parse_jpeg (p,p->is_raw = 0);
  6322. for (i=0; i < sizeof corp / sizeof *corp; i++)
  6323. if (strstr (p->make, corp[i])) /* Simplify company names */
  6324. strcpy (p->make, corp[i]);
  6325. if (!strncmp (p->make,"KODAK",5))
  6326. p->make[16] = p->model[16] = 0;
  6327. cp = p->make + strlen(p->make); /* Remove trailing spaces */
  6328. while (*--cp == ' ') *cp = 0;
  6329. cp = p->model + strlen(p->model);
  6330. while (*--cp == ' ') *cp = 0;
  6331. i = strlen(p->make); /* Remove p->make from p->model */
  6332. if (!strncasecmp (p->model, p->make, i) && p->model[i++] == ' ')
  6333. memmove (p->model, p->model+i, 64-i);
  6334. if (!strncmp (p->model,"Digital Camera ",15))
  6335. strcpy (p->model, p->model+15);
  6336. p->desc[511] = p->artist[63] = p->make[63] = p->model[63] = p->model2[63] = 0;
  6337. if (!p->is_raw) goto notraw;
  6338. if (!p->maximum) p->maximum = (1 << p->tiff_bps) - 1;
  6339. if (!p->height) p->height = p->raw_height;
  6340. if (!p->width) p->width = p->raw_width;
  6341. if (p->fuji_width) {
  6342. p->width = p->height + p->fuji_width;
  6343. p->height = p->width - 1;
  6344. p->pixel_aspect = 1;
  6345. }
  6346. if (p->height == 2624 && p->width == 3936) /* Pentax K10D and Samsung GX10 */
  6347. { p->height = 2616; p->width = 3896; }
  6348. if (p->height == 3136 && p->width == 4864) /* Pentax K20D */
  6349. { p->height = 3124; p->width = 4688; }
  6350. if (p->height == 3014 && p->width == 4096) /* Ricoh GX200 */
  6351. p->width = 4014;
  6352. if (p->dng_version) {
  6353. if (p->filters == UINT_MAX) p->filters = 0;
  6354. if (p->filters) p->is_raw = p->tiff_samples;
  6355. else p->colors = p->tiff_samples;
  6356. if (p->tiff_compress == 1)
  6357. p->load_raw = &DCR_CLASS dcr_adobe_dng_load_raw_nc;
  6358. if (p->tiff_compress == 7)
  6359. p->load_raw = &DCR_CLASS dcr_adobe_dng_load_raw_lj;
  6360. goto dng_skip;
  6361. }
  6362. if ((is_canon = !strcmp(p->make,"Canon")))
  6363. p->load_raw = memcmp (head+6,"HEAPCCDR",8) ?
  6364. &DCR_CLASS dcr_lossless_jpeg_load_raw : &DCR_CLASS dcr_canon_compressed_load_raw;
  6365. if (!strcmp(p->make,"NIKON")) {
  6366. if (!p->load_raw)
  6367. p->load_raw = &DCR_CLASS dcr_packed_12_load_raw;
  6368. if (p->model[0] == 'E')
  6369. p->load_flags |= !p->data_offset << 2 | 2;
  6370. }
  6371. if (!strcmp(p->make,"CASIO")) {
  6372. p->load_raw = &DCR_CLASS dcr_packed_12_load_raw;
  6373. p->maximum = 0xf7f;
  6374. }
  6375. /* Set parameters based on camera name (for non-DNG files). */
  6376. if (p->is_foveon) {
  6377. #if RESTRICTED
  6378. if (p->height*2 < p->width) p->pixel_aspect = 0.5;
  6379. if (p->height > p->width) p->pixel_aspect = 2;
  6380. p->filters = 0;
  6381. p->load_raw = &DCR_CLASS dcr_foveon_load_raw;
  6382. dcr_simple_coeff(p, 0);
  6383. #endif //RESTRICTED
  6384. } else if (is_canon && p->tiff_bps == 15) {
  6385. switch (p->width) {
  6386. case 3344: p->width -= 66;
  6387. case 3872: p->width -= 6;
  6388. }
  6389. p->filters = 0;
  6390. p->load_raw = &DCR_CLASS dcr_canon_sraw_load_raw;
  6391. } else if (!strcmp(p->model,"PowerShot 600")) {
  6392. p->height = 613;
  6393. p->width = 854;
  6394. p->raw_width = 896;
  6395. p->pixel_aspect = 607/628.0;
  6396. p->colors = 4;
  6397. p->filters = 0xe1e4e1e4;
  6398. p->load_raw = &DCR_CLASS dcr_canon_600_load_raw;
  6399. } else if (!strcmp(p->model,"PowerShot A5") ||
  6400. !strcmp(p->model,"PowerShot A5 Zoom")) {
  6401. p->height = 773;
  6402. p->width = 960;
  6403. p->raw_width = 992;
  6404. p->pixel_aspect = 256/235.0;
  6405. p->colors = 4;
  6406. p->filters = 0x1e4e1e4e;
  6407. p->load_raw = &DCR_CLASS dcr_canon_a5_load_raw;
  6408. } else if (!strcmp(p->model,"PowerShot A50")) {
  6409. p->height = 968;
  6410. p->width = 1290;
  6411. p->raw_width = 1320;
  6412. p->colors = 4;
  6413. p->filters = 0x1b4e4b1e;
  6414. p->load_raw = &DCR_CLASS dcr_canon_a5_load_raw;
  6415. } else if (!strcmp(p->model,"PowerShot Pro70")) {
  6416. p->height = 1024;
  6417. p->width = 1552;
  6418. p->colors = 4;
  6419. p->filters = 0x1e4b4e1b;
  6420. p->load_raw = &DCR_CLASS dcr_canon_a5_load_raw;
  6421. } else if (!strcmp(p->model,"PowerShot SD300")) {
  6422. p->height = 1752;
  6423. p->width = 2344;
  6424. p->raw_height = 1766;
  6425. p->raw_width = 2400;
  6426. p->top_margin = 12;
  6427. p->left_margin = 12;
  6428. p->load_raw = &DCR_CLASS dcr_canon_a5_load_raw;
  6429. } else if (!strcmp(p->model,"PowerShot A460")) {
  6430. p->height = 1960;
  6431. p->width = 2616;
  6432. p->raw_height = 1968;
  6433. p->raw_width = 2664;
  6434. p->top_margin = 4;
  6435. p->left_margin = 4;
  6436. p->load_raw = &DCR_CLASS dcr_canon_a5_load_raw;
  6437. } else if (!strcmp(p->model,"PowerShot A530")) {
  6438. p->height = 1984;
  6439. p->width = 2620;
  6440. p->raw_height = 1992;
  6441. p->raw_width = 2672;
  6442. p->top_margin = 6;
  6443. p->left_margin = 10;
  6444. p->load_raw = &DCR_CLASS dcr_canon_a5_load_raw;
  6445. p->raw_color = 0;
  6446. } else if (!strcmp(p->model,"PowerShot A610")) {
  6447. if (dcr_canon_s2is(p)) strcpy (p->model+10, "S2 IS");
  6448. p->height = 1960;
  6449. p->width = 2616;
  6450. p->raw_height = 1968;
  6451. p->raw_width = 2672;
  6452. p->top_margin = 8;
  6453. p->left_margin = 12;
  6454. p->load_raw = &DCR_CLASS dcr_canon_a5_load_raw;
  6455. } else if (!strcmp(p->model,"PowerShot A620")) {
  6456. p->height = 2328;
  6457. p->width = 3112;
  6458. p->raw_height = 2340;
  6459. p->raw_width = 3152;
  6460. p->top_margin = 12;
  6461. p->left_margin = 36;
  6462. p->load_raw = &DCR_CLASS dcr_canon_a5_load_raw;
  6463. } else if (!strcmp(p->model,"PowerShot A720")) {
  6464. p->height = 2472;
  6465. p->width = 3298;
  6466. p->raw_height = 2480;
  6467. p->raw_width = 3336;
  6468. p->top_margin = 5;
  6469. p->left_margin = 6;
  6470. p->load_raw = &DCR_CLASS dcr_canon_a5_load_raw;
  6471. } else if (!strcmp(p->model,"PowerShot A630")) {
  6472. p->height = 2472;
  6473. p->width = 3288;
  6474. p->raw_height = 2484;
  6475. p->raw_width = 3344;
  6476. p->top_margin = 6;
  6477. p->left_margin = 12;
  6478. p->load_raw = &DCR_CLASS dcr_canon_a5_load_raw;
  6479. } else if (!strcmp(p->model,"PowerShot A640")) {
  6480. p->height = 2760;
  6481. p->width = 3672;
  6482. p->raw_height = 2772;
  6483. p->raw_width = 3736;
  6484. p->top_margin = 6;
  6485. p->left_margin = 12;
  6486. p->load_raw = &DCR_CLASS dcr_canon_a5_load_raw;
  6487. } else if (!strcmp(p->model,"PowerShot A650")) {
  6488. p->height = 3024;
  6489. p->width = 4032;
  6490. p->raw_height = 3048;
  6491. p->raw_width = 4104;
  6492. p->top_margin = 12;
  6493. p->left_margin = 48;
  6494. p->load_raw = &DCR_CLASS dcr_canon_a5_load_raw;
  6495. } else if (!strcmp(p->model,"PowerShot S3 IS")) {
  6496. p->height = 2128;
  6497. p->width = 2840;
  6498. p->raw_height = 2136;
  6499. p->raw_width = 2888;
  6500. p->top_margin = 8;
  6501. p->left_margin = 44;
  6502. p->load_raw = &DCR_CLASS dcr_canon_a5_load_raw;
  6503. } else if (!strcmp(p->model,"PowerShot Pro90 IS")) {
  6504. p->width = 1896;
  6505. p->colors = 4;
  6506. p->filters = 0xb4b4b4b4;
  6507. } else if (is_canon && p->raw_width == 2144) {
  6508. p->height = 1550;
  6509. p->width = 2088;
  6510. p->top_margin = 8;
  6511. p->left_margin = 4;
  6512. if (!strcmp(p->model,"PowerShot G1")) {
  6513. p->colors = 4;
  6514. p->filters = 0xb4b4b4b4;
  6515. }
  6516. } else if (is_canon && p->raw_width == 2224) {
  6517. p->height = 1448;
  6518. p->width = 2176;
  6519. p->top_margin = 6;
  6520. p->left_margin = 48;
  6521. } else if (is_canon && p->raw_width == 2376) {
  6522. p->height = 1720;
  6523. p->width = 2312;
  6524. p->top_margin = 6;
  6525. p->left_margin = 12;
  6526. } else if (is_canon && p->raw_width == 2672) {
  6527. p->height = 1960;
  6528. p->width = 2616;
  6529. p->top_margin = 6;
  6530. p->left_margin = 12;
  6531. } else if (is_canon && p->raw_width == 3152) {
  6532. p->height = 2056;
  6533. p->width = 3088;
  6534. p->top_margin = 12;
  6535. p->left_margin = 64;
  6536. if (p->unique_id == 0x80000170)
  6537. dcr_adobe_coeff (p, "Canon","EOS 300D");
  6538. } else if (is_canon && p->raw_width == 3160) {
  6539. p->height = 2328;
  6540. p->width = 3112;
  6541. p->top_margin = 12;
  6542. p->left_margin = 44;
  6543. } else if (is_canon && p->raw_width == 3344) {
  6544. p->height = 2472;
  6545. p->width = 3288;
  6546. p->top_margin = 6;
  6547. p->left_margin = 4;
  6548. } else if (!strcmp(p->model,"EOS D2000C")) {
  6549. p->filters = 0x61616161;
  6550. p->black = p->curve[200];
  6551. } else if (is_canon && p->raw_width == 3516) {
  6552. p->top_margin = 14;
  6553. p->left_margin = 42;
  6554. if (p->unique_id == 0x80000189)
  6555. dcr_adobe_coeff (p, "Canon","EOS 350D");
  6556. goto canon_cr2;
  6557. } else if (is_canon && p->raw_width == 3596) {
  6558. p->top_margin = 12;
  6559. p->left_margin = 74;
  6560. goto canon_cr2;
  6561. } else if (is_canon && p->raw_width == 3944) {
  6562. p->height = 2602;
  6563. p->width = 3908;
  6564. p->top_margin = 18;
  6565. p->left_margin = 30;
  6566. } else if (is_canon && p->raw_width == 3948) {
  6567. p->top_margin = 18;
  6568. p->left_margin = 42;
  6569. p->height -= 2;
  6570. if (p->unique_id == 0x80000236)
  6571. dcr_adobe_coeff (p, "Canon","EOS 400D");
  6572. if (p->unique_id == 0x80000254)
  6573. dcr_adobe_coeff (p, "Canon","EOS 1000D");
  6574. goto canon_cr2;
  6575. } else if (is_canon && p->raw_width == 3984) {
  6576. p->top_margin = 20;
  6577. p->left_margin = 76;
  6578. p->height -= 2;
  6579. goto canon_cr2;
  6580. } else if (is_canon && p->raw_width == 4104) {
  6581. p->height = 3024;
  6582. p->width = 4032;
  6583. p->top_margin = 12;
  6584. p->left_margin = 48;
  6585. } else if (is_canon && p->raw_width == 4312) {
  6586. p->top_margin = 18;
  6587. p->left_margin = 22;
  6588. p->height -= 2;
  6589. if (p->unique_id == 0x80000176)
  6590. dcr_adobe_coeff (p, "Canon","EOS 450D");
  6591. goto canon_cr2;
  6592. } else if (is_canon && p->raw_width == 4476) {
  6593. p->top_margin = 34;
  6594. p->left_margin = 90;
  6595. goto canon_cr2;
  6596. } else if (is_canon && p->raw_width == 4480) {
  6597. p->height = 3326;
  6598. p->width = 4432;
  6599. p->top_margin = 10;
  6600. p->left_margin = 12;
  6601. p->filters = 0x49494949;
  6602. } else if (is_canon && p->raw_width == 1208) {
  6603. p->top_margin = 51;
  6604. p->left_margin = 62;
  6605. p->raw_width = p->width *= 4;
  6606. goto canon_cr2;
  6607. } else if (is_canon && p->raw_width == 1448) {
  6608. p->top_margin = 51;
  6609. p->left_margin = 158;
  6610. p->raw_width = p->width *= 4;
  6611. goto canon_cr2;
  6612. } else if (is_canon && p->raw_width == 5108) {
  6613. p->top_margin = 13;
  6614. p->left_margin = 98;
  6615. canon_cr2:
  6616. p->height -= p->top_margin;
  6617. p->width -= p->left_margin;
  6618. } else if (is_canon && p->raw_width == 5712) {
  6619. p->height = 3752;
  6620. p->width = 5640;
  6621. p->top_margin = 20;
  6622. p->left_margin = 62;
  6623. } else if (!strcmp(p->model,"D1")) {
  6624. p->cam_mul[0] *= 256/527.0f;
  6625. p->cam_mul[2] *= 256/317.0f;
  6626. } else if (!strcmp(p->model,"D1X")) {
  6627. p->width -= 4;
  6628. p->pixel_aspect = 0.5;
  6629. } else if (!strcmp(p->model,"D40X") ||
  6630. !strcmp(p->model,"D60") ||
  6631. !strcmp(p->model,"D80")) {
  6632. p->height -= 3;
  6633. p->width -= 4;
  6634. } else if (!strcmp(p->model,"D3") ||
  6635. !strcmp(p->model,"D700")) {
  6636. p->width -= 4;
  6637. p->left_margin = 2;
  6638. } else if (!strncmp(p->model,"D40",3) ||
  6639. !strncmp(p->model,"D50",3) ||
  6640. !strncmp(p->model,"D70",3)) {
  6641. p->width--;
  6642. } else if (!strcmp(p->model,"D90")) {
  6643. p->width -= 42;
  6644. } else if (!strcmp(p->model,"D100")) {
  6645. if (p->tiff_compress == 34713 && !dcr_nikon_is_compressed(p)) {
  6646. p->load_raw = &DCR_CLASS dcr_packed_12_load_raw;
  6647. p->load_flags |= 8;
  6648. p->raw_width = (p->width += 3) + 3;
  6649. }
  6650. } else if (!strcmp(p->model,"D200")) {
  6651. p->left_margin = 1;
  6652. p->width -= 4;
  6653. p->filters = 0x94949494;
  6654. } else if (!strncmp(p->model,"D2H",3)) {
  6655. p->left_margin = 6;
  6656. p->width -= 14;
  6657. } else if (!strncmp(p->model,"D2X",3)) {
  6658. if (p->width == 3264) p->width -= 32;
  6659. else p->width -= 8;
  6660. } else if (!strcmp(p->model,"D300")) {
  6661. p->width -= 32;
  6662. } else if (!strcmp(p->model,"COOLPIX P6000")) {
  6663. p->load_flags = 1;
  6664. p->filters = 0x94949494;
  6665. } else if (fsize == 1581060) {
  6666. p->height = 963;
  6667. p->width = 1287;
  6668. p->raw_width = 1632;
  6669. p->load_raw = &DCR_CLASS dcr_nikon_e900_load_raw;
  6670. p->maximum = 0x3f4;
  6671. p->colors = 4;
  6672. p->filters = 0x1e1e1e1e;
  6673. dcr_simple_coeff(p, 3);
  6674. p->pre_mul[0] = 1.2085f;
  6675. p->pre_mul[1] = 1.0943f;
  6676. p->pre_mul[3] = 1.1103f;
  6677. } else if (fsize == 2465792) {
  6678. p->height = 1203;
  6679. p->width = 1616;
  6680. p->raw_width = 2048;
  6681. p->load_raw = &DCR_CLASS dcr_nikon_e900_load_raw;
  6682. p->colors = 4;
  6683. p->filters = 0x4b4b4b4b;
  6684. dcr_adobe_coeff (p, "NIKON","E950");
  6685. } else if (fsize == 4771840) {
  6686. p->height = 1540;
  6687. p->width = 2064;
  6688. p->colors = 4;
  6689. p->filters = 0xe1e1e1e1;
  6690. p->load_raw = &DCR_CLASS dcr_packed_12_load_raw;
  6691. p->load_flags = 6;
  6692. if (!p->timestamp && dcr_nikon_e995(p))
  6693. strcpy (p->model, "E995");
  6694. if (strcmp(p->model,"E995")) {
  6695. p->filters = 0xb4b4b4b4;
  6696. dcr_simple_coeff(p, 3);
  6697. p->pre_mul[0] = 1.196f;
  6698. p->pre_mul[1] = 1.246f;
  6699. p->pre_mul[2] = 1.018f;
  6700. }
  6701. } else if (!strcmp(p->model,"E2100")) {
  6702. if (!p->timestamp && !dcr_nikon_e2100(p)) goto cp_e2500;
  6703. p->height = 1206;
  6704. p->width = 1616;
  6705. p->load_flags = 7;
  6706. } else if (!strcmp(p->model,"E2500")) {
  6707. cp_e2500:
  6708. strcpy (p->model, "E2500");
  6709. p->height = 1204;
  6710. p->width = 1616;
  6711. p->colors = 4;
  6712. p->filters = 0x4b4b4b4b;
  6713. } else if (fsize == 4775936) {
  6714. p->height = 1542;
  6715. p->width = 2064;
  6716. p->load_raw = &DCR_CLASS dcr_packed_12_load_raw;
  6717. p->load_flags = 7;
  6718. p->pre_mul[0] = 1.818f;
  6719. p->pre_mul[2] = 1.618f;
  6720. if (!p->timestamp) dcr_nikon_3700(p);
  6721. if (p->model[0] == 'E' && atoi(p->model+1) < 3700)
  6722. p->filters = 0x49494949;
  6723. if (!strcmp(p->model,"Optio 33WR")) {
  6724. p->flip = 1;
  6725. p->filters = 0x16161616;
  6726. p->pre_mul[0] = 1.331f;
  6727. p->pre_mul[2] = 1.820f;
  6728. }
  6729. } else if (fsize == 5869568) {
  6730. p->height = 1710;
  6731. p->width = 2288;
  6732. p->filters = 0x16161616;
  6733. if (!p->timestamp && dcr_minolta_z2(p)) {
  6734. strcpy (p->make, "Minolta");
  6735. strcpy (p->model,"DiMAGE Z2");
  6736. }
  6737. p->load_raw = &DCR_CLASS dcr_packed_12_load_raw;
  6738. p->load_flags = 6 + (p->make[0] == 'M');
  6739. } else if (!strcmp(p->model,"E4500")) {
  6740. p->height = 1708;
  6741. p->width = 2288;
  6742. p->colors = 4;
  6743. p->filters = 0xb4b4b4b4;
  6744. } else if (fsize == 7438336) {
  6745. p->height = 1924;
  6746. p->width = 2576;
  6747. p->colors = 4;
  6748. p->filters = 0xb4b4b4b4;
  6749. } else if (fsize == 8998912) {
  6750. p->height = 2118;
  6751. p->width = 2832;
  6752. p->maximum = 0xf83;
  6753. p->load_raw = &DCR_CLASS dcr_packed_12_load_raw;
  6754. p->load_flags = 7;
  6755. } else if (!strcmp(p->model,"FinePix S5100") ||
  6756. !strcmp(p->model,"FinePix S5500")) {
  6757. p->load_raw = &DCR_CLASS dcr_unpacked_load_raw;
  6758. } else if (!strcmp(p->make,"FUJIFILM")) {
  6759. if (!strcmp(p->model+7,"S2Pro")) {
  6760. strcpy (p->model+7," S2Pro");
  6761. p->height = 2144;
  6762. p->width = 2880;
  6763. p->flip = 6;
  6764. } else
  6765. p->maximum = 0x3e00;
  6766. if (p->is_raw == 2 && p->opt.shot_select)
  6767. p->maximum = 0x2f00;
  6768. p->top_margin = (p->raw_height - p->height)/2;
  6769. p->left_margin = (p->raw_width - p->width )/2;
  6770. if (p->is_raw == 2)
  6771. p->data_offset += (p->opt.shot_select > 0) * ( p->fuji_layout ?
  6772. (p->raw_width *= 2) : p->raw_height*p->raw_width*2 );
  6773. p->fuji_width = p->width >> !p->fuji_layout;
  6774. p->width = (p->height >> p->fuji_layout) + p->fuji_width;
  6775. p->raw_height = p->height;
  6776. p->height = p->width - 1;
  6777. p->load_raw = &DCR_CLASS dcr_fuji_load_raw;
  6778. if (!(p->fuji_width & 1)) p->filters = 0x49494949;
  6779. } else if (!strcmp(p->model,"RD175")) {
  6780. p->height = 986;
  6781. p->width = 1534;
  6782. p->data_offset = 513;
  6783. p->filters = 0x61616161;
  6784. p->load_raw = &DCR_CLASS dcr_minolta_rd175_load_raw;
  6785. } else if (!strcmp(p->model,"KD-400Z")) {
  6786. p->height = 1712;
  6787. p->width = 2312;
  6788. p->raw_width = 2336;
  6789. goto konica_400z;
  6790. } else if (!strcmp(p->model,"KD-510Z")) {
  6791. goto konica_510z;
  6792. } else if (!strcasecmp(p->make,"MINOLTA")) {
  6793. p->load_raw = &DCR_CLASS dcr_unpacked_load_raw;
  6794. if (!strncmp(p->model,"DiMAGE A",8)) {
  6795. if (!strcmp(p->model,"DiMAGE A200"))
  6796. p->filters = 0x49494949;
  6797. p->load_raw = &DCR_CLASS dcr_packed_12_load_raw;
  6798. } else if (!strncmp(p->model,"ALPHA",5) ||
  6799. !strncmp(p->model,"DYNAX",5) ||
  6800. !strncmp(p->model,"MAXXUM",6)) {
  6801. sprintf (p->model+20, "DYNAX %-10s", p->model+6+(p->model[0]=='M'));
  6802. dcr_adobe_coeff (p, p->make, p->model+20);
  6803. p->load_raw = &DCR_CLASS dcr_packed_12_load_raw;
  6804. } else if (!strncmp(p->model,"DiMAGE G",8)) {
  6805. if (p->model[8] == '4') {
  6806. p->height = 1716;
  6807. p->width = 2304;
  6808. } else if (p->model[8] == '5') {
  6809. konica_510z:
  6810. p->height = 1956;
  6811. p->width = 2607;
  6812. p->raw_width = 2624;
  6813. } else if (p->model[8] == '6') {
  6814. p->height = 2136;
  6815. p->width = 2848;
  6816. }
  6817. p->data_offset += 14;
  6818. p->filters = 0x61616161;
  6819. konica_400z:
  6820. p->load_raw = &DCR_CLASS dcr_unpacked_load_raw;
  6821. p->maximum = 0x3df;
  6822. p->order = 0x4d4d;
  6823. }
  6824. } else if (!strcmp(p->model,"*ist DS")) {
  6825. p->height -= 2;
  6826. } else if (!strcmp(p->model,"K20D")) {
  6827. p->filters = 0x16161616;
  6828. } else if (!strcmp(p->model,"Optio S")) {
  6829. if (fsize == 3178560) {
  6830. p->height = 1540;
  6831. p->width = 2064;
  6832. p->load_raw = &DCR_CLASS dcr_eight_bit_load_raw;
  6833. p->cam_mul[0] *= 4;
  6834. p->cam_mul[2] *= 4;
  6835. p->pre_mul[0] = 1.391f;
  6836. p->pre_mul[2] = 1.188f;
  6837. } else {
  6838. p->height = 1544;
  6839. p->width = 2068;
  6840. p->raw_width = 3136;
  6841. p->load_raw = &DCR_CLASS dcr_packed_12_load_raw;
  6842. p->maximum = 0xf7c;
  6843. p->pre_mul[0] = 1.137f;
  6844. p->pre_mul[2] = 1.453f;
  6845. }
  6846. } else if (fsize == 6114240) {
  6847. p->height = 1737;
  6848. p->width = 2324;
  6849. p->raw_width = 3520;
  6850. p->load_raw = &DCR_CLASS dcr_packed_12_load_raw;
  6851. p->maximum = 0xf7a;
  6852. p->pre_mul[0] = 1.980f;
  6853. p->pre_mul[2] = 1.570f;
  6854. } else if (!strcmp(p->model,"Optio 750Z")) {
  6855. p->height = 2302;
  6856. p->width = 3072;
  6857. p->load_raw = &DCR_CLASS dcr_packed_12_load_raw;
  6858. p->load_flags = 7;
  6859. } else if (!strcmp(p->model,"S85")) {
  6860. p->height = 2448;
  6861. p->width = 3264;
  6862. p->raw_width = fsize/p->height/2;
  6863. p->order = 0x4d4d;
  6864. p->load_raw = &DCR_CLASS dcr_unpacked_load_raw;
  6865. p->maximum = 0xffff;
  6866. } else if (!strcmp(p->model,"STV680 VGA")) {
  6867. p->height = 484;
  6868. p->width = 644;
  6869. p->load_raw = &DCR_CLASS dcr_eight_bit_load_raw;
  6870. p->flip = 2;
  6871. p->filters = 0x16161616;
  6872. p->black = 16;
  6873. p->pre_mul[0] = 1.097f;
  6874. p->pre_mul[2] = 1.128f;
  6875. } else if (!strcmp(p->model,"KAI-0340")) {
  6876. p->height = 477;
  6877. p->width = 640;
  6878. p->order = 0x4949;
  6879. p->data_offset = 3840;
  6880. p->load_raw = &DCR_CLASS dcr_unpacked_load_raw;
  6881. p->pre_mul[0] = 1.561f;
  6882. p->pre_mul[2] = 2.454f;
  6883. } else if (!strcmp(p->model,"N95")) {
  6884. p->height = p->raw_height - (p->top_margin = 2);
  6885. } else if (!strcmp(p->model,"531C")) {
  6886. p->height = 1200;
  6887. p->width = 1600;
  6888. p->load_raw = &DCR_CLASS dcr_unpacked_load_raw;
  6889. p->filters = 0x49494949;
  6890. p->pre_mul[1] = 1.218f;
  6891. } else if (!strcmp(p->model,"F-080C")) {
  6892. p->height = 768;
  6893. p->width = 1024;
  6894. p->load_raw = &DCR_CLASS dcr_eight_bit_load_raw;
  6895. } else if (!strcmp(p->model,"F-145C")) {
  6896. p->height = 1040;
  6897. p->width = 1392;
  6898. p->load_raw = &DCR_CLASS dcr_eight_bit_load_raw;
  6899. } else if (!strcmp(p->model,"F-201C")) {
  6900. p->height = 1200;
  6901. p->width = 1600;
  6902. p->load_raw = &DCR_CLASS dcr_eight_bit_load_raw;
  6903. } else if (!strcmp(p->model,"F-510C")) {
  6904. p->height = 1958;
  6905. p->width = 2588;
  6906. p->load_raw = fsize < 7500000 ?
  6907. &DCR_CLASS dcr_eight_bit_load_raw : &DCR_CLASS dcr_unpacked_load_raw;
  6908. p->maximum = 0xfff0;
  6909. } else if (!strcmp(p->model,"F-810C")) {
  6910. p->height = 2469;
  6911. p->width = 3272;
  6912. p->load_raw = &DCR_CLASS dcr_unpacked_load_raw;
  6913. p->maximum = 0xfff0;
  6914. } else if (!strcmp(p->model,"XCD-SX910CR")) {
  6915. p->height = 1024;
  6916. p->width = 1375;
  6917. p->raw_width = 1376;
  6918. p->filters = 0x49494949;
  6919. p->maximum = 0x3ff;
  6920. p->load_raw = fsize < 2000000 ?
  6921. &DCR_CLASS dcr_eight_bit_load_raw : &DCR_CLASS dcr_unpacked_load_raw;
  6922. } else if (!strcmp(p->model,"2010")) {
  6923. p->height = 1207;
  6924. p->width = 1608;
  6925. p->order = 0x4949;
  6926. p->filters = 0x16161616;
  6927. p->data_offset = 3212;
  6928. p->maximum = 0x3ff;
  6929. p->load_raw = &DCR_CLASS dcr_unpacked_load_raw;
  6930. } else if (!strcmp(p->model,"A782")) {
  6931. p->height = 3000;
  6932. p->width = 2208;
  6933. p->filters = 0x61616161;
  6934. p->load_raw = fsize < 10000000 ?
  6935. &DCR_CLASS dcr_eight_bit_load_raw : &DCR_CLASS dcr_unpacked_load_raw;
  6936. p->maximum = 0xffc0;
  6937. } else if (!strcmp(p->model,"3320AF")) {
  6938. p->height = 1536;
  6939. p->raw_width = p->width = 2048;
  6940. p->filters = 0x61616161;
  6941. p->load_raw = &DCR_CLASS dcr_unpacked_load_raw;
  6942. p->maximum = 0x3ff;
  6943. p->pre_mul[0] = 1.717f;
  6944. p->pre_mul[2] = 1.138f;
  6945. dcr_fseek(p->obj_, 0x300000, SEEK_SET);
  6946. if ((p->order = dcr_guess_byte_order(p, 0x10000)) == 0x4d4d) {
  6947. p->height -= (p->top_margin = 16);
  6948. p->width -= (p->left_margin = 28);
  6949. p->maximum = 0xf5c0;
  6950. strcpy (p->make, "ISG");
  6951. p->model[0] = 0;
  6952. }
  6953. } else if (!strcmp(p->make,"Hasselblad")) {
  6954. if (p->load_raw == &DCR_CLASS dcr_lossless_jpeg_load_raw)
  6955. p->load_raw = &DCR_CLASS dcr_hasselblad_load_raw;
  6956. if (p->raw_width == 7262) {
  6957. p->height = 5444;
  6958. p->width = 7248;
  6959. p->top_margin = 4;
  6960. p->left_margin = 7;
  6961. p->filters = 0x61616161;
  6962. } else if (p->raw_width == 4090) {
  6963. strcpy (p->model, "V96C");
  6964. p->height -= (p->top_margin = 6);
  6965. p->width -= (p->left_margin = 3) + 7;
  6966. p->filters = 0x61616161;
  6967. }
  6968. } else if (!strcmp(p->make,"Sinar")) {
  6969. if (!memcmp(head,"8BPS",4)) {
  6970. dcr_fseek(p->obj_, 14, SEEK_SET);
  6971. p->height = dcr_get4(p);
  6972. p->width = dcr_get4(p);
  6973. p->filters = 0x61616161;
  6974. p->data_offset = 68;
  6975. }
  6976. if (!p->load_raw) p->load_raw = &DCR_CLASS dcr_unpacked_load_raw;
  6977. p->maximum = 0x3fff;
  6978. } else if (!strcmp(p->make,"Leaf")) {
  6979. p->maximum = 0x3fff;
  6980. dcr_fseek(p->obj_, p->data_offset, SEEK_SET);
  6981. if (dcr_ljpeg_start (p, &jh, 1) && jh.bits == 15)
  6982. p->maximum = 0x1fff;
  6983. if (p->tiff_samples > 1) p->filters = 0;
  6984. if (p->tiff_samples > 1 || p->tile_length < p->raw_height)
  6985. p->load_raw = &DCR_CLASS dcr_leaf_hdr_load_raw;
  6986. if ((p->width | p->height) == 2048) {
  6987. if (p->tiff_samples == 1) {
  6988. p->filters = 1;
  6989. strcpy (p->cdesc, "RBTG");
  6990. strcpy (p->model, "CatchLight");
  6991. p->top_margin = 8; p->left_margin = 18; p->height = 2032; p->width = 2016;
  6992. } else {
  6993. strcpy (p->model, "DCB2");
  6994. p->top_margin = 10; p->left_margin = 16; p->height = 2028; p->width = 2022;
  6995. }
  6996. } else if (p->width+p->height == 3144+2060) {
  6997. if (!p->model[0]) strcpy (p->model, "Cantare");
  6998. if (p->width > p->height) {
  6999. p->top_margin = 6; p->left_margin = 32; p->height = 2048; p->width = 3072;
  7000. p->filters = 0x61616161;
  7001. } else {
  7002. p->left_margin = 6; p->top_margin = 32; p->width = 2048; p->height = 3072;
  7003. p->filters = 0x16161616;
  7004. }
  7005. if (!p->cam_mul[0] || p->model[0] == 'V') p->filters = 0;
  7006. else p->is_raw = p->tiff_samples;
  7007. } else if (p->width == 2116) {
  7008. strcpy (p->model, "Valeo 6");
  7009. p->height -= 2 * (p->top_margin = 30);
  7010. p->width -= 2 * (p->left_margin = 55);
  7011. p->filters = 0x49494949;
  7012. } else if (p->width == 3171) {
  7013. strcpy (p->model, "Valeo 6");
  7014. p->height -= 2 * (p->top_margin = 24);
  7015. p->width -= 2 * (p->left_margin = 24);
  7016. p->filters = 0x16161616;
  7017. }
  7018. } else if (!strcmp(p->make,"LEICA") || !strcmp(p->make,"Panasonic")) {
  7019. p->maximum = 0xfff0;
  7020. if ((fsize-p->data_offset) / (p->width*8/7) == p->height)
  7021. p->load_raw = &DCR_CLASS dcr_panasonic_load_raw;
  7022. if (!p->load_raw) p->load_raw = &DCR_CLASS dcr_unpacked_load_raw;
  7023. switch (p->width) {
  7024. case 2568:
  7025. dcr_adobe_coeff (p, "Panasonic","DMC-LC1"); break;
  7026. case 3130:
  7027. p->left_margin = -14;
  7028. case 3170:
  7029. p->left_margin += 18;
  7030. p->width = 3096;
  7031. if (p->height > 2326) {
  7032. p->height = 2326;
  7033. p->top_margin = 13;
  7034. p->filters = 0x49494949;
  7035. }
  7036. p->zero_is_bad = 1;
  7037. dcr_adobe_coeff (p, "Panasonic","DMC-FZ8"); break;
  7038. case 3213:
  7039. p->width -= 27;
  7040. case 3177:
  7041. p->width -= 10;
  7042. p->filters = 0x49494949;
  7043. p->zero_is_bad = 1;
  7044. dcr_adobe_coeff (p, "Panasonic","DMC-L1"); break;
  7045. case 3304:
  7046. p->width -= 17;
  7047. p->zero_is_bad = 1;
  7048. dcr_adobe_coeff (p, "Panasonic","DMC-FZ30"); break;
  7049. case 3330:
  7050. p->width += 43;
  7051. p->left_margin = -6;
  7052. p->maximum = 0xf7f0;
  7053. case 3370:
  7054. p->width -= 82;
  7055. p->left_margin += 15;
  7056. if (p->height > 2480)
  7057. p->height = 2480 - (p->top_margin = 10);
  7058. p->filters = 0x49494949;
  7059. p->zero_is_bad = 1;
  7060. dcr_adobe_coeff (p, "Panasonic","DMC-FZ18"); break;
  7061. case 3690:
  7062. p->height -= 2;
  7063. p->left_margin = -14;
  7064. p->maximum = 0xf7f0;
  7065. case 3770:
  7066. p->width = 3672;
  7067. if (--p->height == 2798 && (p->height = 2760))
  7068. p->top_margin = 15;
  7069. else p->filters = 0x49494949;
  7070. p->left_margin += 17;
  7071. p->zero_is_bad = 1;
  7072. dcr_adobe_coeff (p, "Panasonic","DMC-FZ50"); break;
  7073. case 3710:
  7074. p->width = 3682;
  7075. p->filters = 0x49494949;
  7076. dcr_adobe_coeff (p, "Panasonic","DMC-L10"); break;
  7077. case 3724:
  7078. p->width -= 14;
  7079. case 3836:
  7080. p->width -= 42;
  7081. lx3: p->filters = 0x16161616;
  7082. if (p->make[0] != 'P')
  7083. dcr_adobe_coeff (p, "Panasonic","DMC-LX3");
  7084. break;
  7085. case 3880:
  7086. p->width -= 22;
  7087. p->left_margin = 6;
  7088. p->zero_is_bad = 1;
  7089. dcr_adobe_coeff (p, "Panasonic","DMC-LX1"); break;
  7090. case 4060:
  7091. p->width = 3982;
  7092. if (p->height == 2250) goto lx3;
  7093. p->width = 4018;
  7094. p->filters = 0x49494949;
  7095. p->zero_is_bad = 1;
  7096. dcr_adobe_coeff (p, "Panasonic","DMC-G1"); break;
  7097. case 4290:
  7098. p->height += 38;
  7099. p->left_margin = -14;
  7100. p->filters = 0x49494949;
  7101. case 4330:
  7102. p->width = 4248;
  7103. if ((p->height -= 39) == 2400)
  7104. p->top_margin = 15;
  7105. p->left_margin += 17;
  7106. dcr_adobe_coeff (p, "Panasonic","DMC-LX2"); break;
  7107. case 4508:
  7108. p->height -= 6;
  7109. p->width = 4429;
  7110. p->filters = 0x16161616;
  7111. dcr_adobe_coeff (p, "Panasonic","DMC-FX150"); break;
  7112. }
  7113. } else if (!strcmp(p->model,"C770UZ")) {
  7114. p->height = 1718;
  7115. p->width = 2304;
  7116. p->filters = 0x16161616;
  7117. p->load_raw = &DCR_CLASS dcr_packed_12_load_raw;
  7118. p->load_flags = 7;
  7119. } else if (!strcmp(p->make,"OLYMPUS")) {
  7120. p->height += p->height & 1;
  7121. p->filters = p->exif_cfa;
  7122. if (p->load_raw == &DCR_CLASS dcr_olympus_e410_load_raw) {
  7123. p->black >>= 4;
  7124. } else if (!strcmp(p->model,"E-10") ||
  7125. !strncmp(p->model,"E-20",4)) {
  7126. p->black <<= 2;
  7127. } else if (!strcmp(p->model,"E-300") ||
  7128. !strcmp(p->model,"E-500")) {
  7129. p->width -= 20;
  7130. if (p->load_raw == &DCR_CLASS dcr_unpacked_load_raw) {
  7131. p->maximum = 0xfc30;
  7132. p->black = 0;
  7133. }
  7134. } else if (!strcmp(p->model,"E-330")) {
  7135. p->width -= 30;
  7136. if (p->load_raw == &DCR_CLASS dcr_unpacked_load_raw)
  7137. p->maximum = 0xf790;
  7138. } else if (!strcmp(p->model,"SP550UZ")) {
  7139. p->thumb_length = fsize - (p->thumb_offset = 0xa39800);
  7140. p->thumb_height = 480;
  7141. p->thumb_width = 640;
  7142. }
  7143. } else if (!strcmp(p->model,"N Digital")) {
  7144. p->height = 2047;
  7145. p->width = 3072;
  7146. p->filters = 0x61616161;
  7147. p->data_offset = 0x1a00;
  7148. p->load_raw = &DCR_CLASS dcr_packed_12_load_raw;
  7149. } else if (!strcmp(p->model,"DSC-F828")) {
  7150. p->width = 3288;
  7151. p->left_margin = 5;
  7152. p->data_offset = 862144;
  7153. p->load_raw = &DCR_CLASS dcr_sony_load_raw;
  7154. p->filters = 0x9c9c9c9c;
  7155. p->colors = 4;
  7156. strcpy (p->cdesc, "RGBE");
  7157. } else if (!strcmp(p->model,"DSC-V3")) {
  7158. p->width = 3109;
  7159. p->left_margin = 59;
  7160. p->data_offset = 787392;
  7161. p->load_raw = &DCR_CLASS dcr_sony_load_raw;
  7162. } else if (!strcmp(p->make,"SONY") && p->raw_width == 3984) {
  7163. dcr_adobe_coeff (p, "SONY","DSC-R1");
  7164. p->width = 3925;
  7165. p->order = 0x4d4d;
  7166. } else if (!strcmp(p->model,"DSLR-A100")) {
  7167. p->height--;
  7168. } else if (!strcmp(p->model,"DSLR-A350")) {
  7169. p->height -= 4;
  7170. } else if (!strcmp(p->model,"C603v")) {
  7171. p->height = 480;
  7172. p->width = 640;
  7173. goto c603v;
  7174. } else if (!strcmp(p->model,"C603y")) {
  7175. p->height = 2134;
  7176. p->width = 2848;
  7177. c603v:
  7178. p->filters = 0;
  7179. p->load_raw = &DCR_CLASS dcr_kodak_yrgb_load_raw;
  7180. } else if (!strcmp(p->model,"C603")) {
  7181. p->raw_height = p->height = 2152;
  7182. p->raw_width = p->width = 2864;
  7183. goto c603;
  7184. } else if (!strcmp(p->model,"C330")) {
  7185. p->height = 1744;
  7186. p->width = 2336;
  7187. p->raw_height = 1779;
  7188. p->raw_width = 2338;
  7189. p->top_margin = 33;
  7190. p->left_margin = 1;
  7191. c603:
  7192. p->order = 0x4949;
  7193. if ((p->data_offset = fsize - p->raw_height*p->raw_width)) {
  7194. dcr_fseek(p->obj_, 168, SEEK_SET);
  7195. dcr_read_shorts (p, p->curve, 256);
  7196. } else p->use_gamma = 0;
  7197. p->load_raw = &DCR_CLASS dcr_eight_bit_load_raw;
  7198. } else if (!strcasecmp(p->make,"KODAK")) {
  7199. if (p->filters == UINT_MAX) p->filters = 0x61616161;
  7200. if (!strncmp(p->model,"NC2000",6)) {
  7201. p->width -= 4;
  7202. p->left_margin = 2;
  7203. } else if (!strcmp(p->model,"EOSDCS3B")) {
  7204. p->width -= 4;
  7205. p->left_margin = 2;
  7206. } else if (!strcmp(p->model,"EOSDCS1")) {
  7207. p->width -= 4;
  7208. p->left_margin = 2;
  7209. } else if (!strcmp(p->model,"DCS420")) {
  7210. p->width -= 4;
  7211. p->left_margin = 2;
  7212. } else if (!strcmp(p->model,"DCS460")) {
  7213. p->width -= 4;
  7214. p->left_margin = 2;
  7215. } else if (!strcmp(p->model,"DCS460A")) {
  7216. p->width -= 4;
  7217. p->left_margin = 2;
  7218. p->colors = 1;
  7219. p->filters = 0;
  7220. } else if (!strcmp(p->model,"DCS660M")) {
  7221. p->black = 214;
  7222. p->colors = 1;
  7223. p->filters = 0;
  7224. } else if (!strcmp(p->model,"DCS760M")) {
  7225. p->colors = 1;
  7226. p->filters = 0;
  7227. }
  7228. if (strstr(p->model,"DC25")) {
  7229. strcpy (p->model, "DC25");
  7230. p->data_offset = 15424;
  7231. }
  7232. if (!strncmp(p->model,"DC2",3)) {
  7233. p->height = 242;
  7234. if (fsize < 100000) {
  7235. p->raw_width = 256; p->width = 249;
  7236. p->pixel_aspect = (4.0*p->height) / (3.0*p->width);
  7237. } else {
  7238. p->raw_width = 512; p->width = 501;
  7239. p->pixel_aspect = (493.0*p->height) / (373.0*p->width);
  7240. }
  7241. p->data_offset += p->raw_width + 1;
  7242. p->colors = 4;
  7243. p->filters = 0x8d8d8d8d;
  7244. dcr_simple_coeff(p, 1);
  7245. p->pre_mul[1] = 1.179f;
  7246. p->pre_mul[2] = 1.209f;
  7247. p->pre_mul[3] = 1.036f;
  7248. p->load_raw = &DCR_CLASS dcr_eight_bit_load_raw;
  7249. } else if (!strcmp(p->model,"40")) {
  7250. strcpy (p->model, "DC40");
  7251. p->height = 512;
  7252. p->width = 768;
  7253. p->data_offset = 1152;
  7254. p->load_raw = &DCR_CLASS dcr_kodak_radc_load_raw;
  7255. } else if (strstr(p->model,"DC50")) {
  7256. strcpy (p->model, "DC50");
  7257. p->height = 512;
  7258. p->width = 768;
  7259. p->data_offset = 19712;
  7260. p->load_raw = &DCR_CLASS dcr_kodak_radc_load_raw;
  7261. } else if (strstr(p->model,"DC120")) {
  7262. strcpy (p->model, "DC120");
  7263. p->height = 976;
  7264. p->width = 848;
  7265. p->pixel_aspect = p->height/0.75/p->width;
  7266. p->load_raw = p->tiff_compress == 7 ?
  7267. &DCR_CLASS dcr_kodak_jpeg_load_raw : &DCR_CLASS dcr_kodak_dc120_load_raw;
  7268. } else if (!strcmp(p->model,"DCS200")) {
  7269. p->thumb_height = 128;
  7270. p->thumb_width = 192;
  7271. p->thumb_offset = 6144;
  7272. p->thumb_misc = 360;
  7273. p->write_thumb = &DCR_CLASS dcr_layer_thumb;
  7274. p->height = 1024;
  7275. p->width = 1536;
  7276. p->data_offset = 79872;
  7277. p->load_raw = &DCR_CLASS dcr_eight_bit_load_raw;
  7278. p->black = 17;
  7279. }
  7280. } else if (!strcmp(p->model,"Fotoman Pixtura")) {
  7281. p->height = 512;
  7282. p->width = 768;
  7283. p->data_offset = 3632;
  7284. p->load_raw = &DCR_CLASS dcr_kodak_radc_load_raw;
  7285. p->filters = 0x61616161;
  7286. dcr_simple_coeff(p, 2);
  7287. } else if (!strcmp(p->model,"QuickTake 100")) {
  7288. dcr_fseek(p->obj_, 544, SEEK_SET);
  7289. p->height = dcr_get2(p);
  7290. p->width = dcr_get2(p);
  7291. p->data_offset = (dcr_get4(p),dcr_get2(p)) == 30 ? 738:736;
  7292. if (p->height > p->width) {
  7293. SWAP(p->height,p->width);
  7294. dcr_fseek(p->obj_, p->data_offset-6, SEEK_SET);
  7295. p->flip = ~dcr_get2(p) & 3 ? 5:6;
  7296. }
  7297. p->load_raw = &DCR_CLASS dcr_quicktake_100_load_raw;
  7298. p->filters = 0x61616161;
  7299. } else if (!strcmp(p->model,"QuickTake 150")) {
  7300. p->data_offset = 738 - head[5];
  7301. if (head[5]) strcpy (p->model+10, "200");
  7302. p->load_raw = &DCR_CLASS dcr_kodak_radc_load_raw;
  7303. p->height = 480;
  7304. p->width = 640;
  7305. p->filters = 0x61616161;
  7306. } else if (!strcmp(p->make,"Rollei") && !p->load_raw) {
  7307. switch (p->raw_width) {
  7308. case 1316:
  7309. p->height = 1030;
  7310. p->width = 1300;
  7311. p->top_margin = 1;
  7312. p->left_margin = 6;
  7313. break;
  7314. case 2568:
  7315. p->height = 1960;
  7316. p->width = 2560;
  7317. p->top_margin = 2;
  7318. p->left_margin = 8;
  7319. }
  7320. p->filters = 0x16161616;
  7321. p->load_raw = &DCR_CLASS dcr_rollei_load_raw;
  7322. p->pre_mul[0] = 1.8f;
  7323. p->pre_mul[2] = 1.3f;
  7324. } else if (!strcmp(p->model,"PC-CAM 600")) {
  7325. p->height = 768;
  7326. p->data_offset = p->width = 1024;
  7327. p->filters = 0x49494949;
  7328. p->load_raw = &DCR_CLASS dcr_eight_bit_load_raw;
  7329. p->pre_mul[0] = 1.14f;
  7330. p->pre_mul[2] = 2.73f;
  7331. } else if (!strcmp(p->model,"QV-2000UX")) {
  7332. p->height = 1208;
  7333. p->width = 1632;
  7334. p->data_offset = p->width * 2;
  7335. p->load_raw = &DCR_CLASS dcr_eight_bit_load_raw;
  7336. } else if (fsize == 3217760) {
  7337. p->height = 1546;
  7338. p->width = 2070;
  7339. p->raw_width = 2080;
  7340. p->load_raw = &DCR_CLASS dcr_eight_bit_load_raw;
  7341. } else if (!strcmp(p->model,"QV-4000")) {
  7342. p->height = 1700;
  7343. p->width = 2260;
  7344. p->load_raw = &DCR_CLASS dcr_unpacked_load_raw;
  7345. p->maximum = 0xffff;
  7346. } else if (!strcmp(p->model,"QV-5700")) {
  7347. p->height = 1924;
  7348. p->width = 2576;
  7349. p->load_raw = &DCR_CLASS dcr_casio_qv5700_load_raw;
  7350. } else if (!strcmp(p->model,"QV-R41")) {
  7351. p->height = 1720;
  7352. p->width = 2312;
  7353. p->raw_width = 3520;
  7354. p->left_margin = 2;
  7355. } else if (!strcmp(p->model,"QV-R51")) {
  7356. p->height = 1926;
  7357. p->width = 2580;
  7358. p->raw_width = 3904;
  7359. p->pre_mul[0] = 1.340f;
  7360. p->pre_mul[2] = 1.672f;
  7361. } else if (!strcmp(p->model,"EX-S100")) {
  7362. p->height = 1544;
  7363. p->width = 2058;
  7364. p->raw_width = 3136;
  7365. p->pre_mul[0] = 1.631f;
  7366. p->pre_mul[2] = 1.106f;
  7367. } else if (!strcmp(p->model,"EX-Z50")) {
  7368. p->height = 1931;
  7369. p->width = 2570;
  7370. p->raw_width = 3904;
  7371. p->pre_mul[0] = 2.529f;
  7372. p->pre_mul[2] = 1.185f;
  7373. } else if (!strcmp(p->model,"EX-Z55")) {
  7374. p->height = 1960;
  7375. p->width = 2570;
  7376. p->raw_width = 3904;
  7377. p->pre_mul[0] = 1.520f;
  7378. p->pre_mul[2] = 1.316f;
  7379. } else if (!strcmp(p->model,"EX-P505")) {
  7380. p->height = 1928;
  7381. p->width = 2568;
  7382. p->raw_width = 3852;
  7383. p->maximum = 0xfff;
  7384. p->pre_mul[0] = 2.07f;
  7385. p->pre_mul[2] = 1.88f;
  7386. } else if (fsize == 9313536) { /* EX-P600 or QV-R61 */
  7387. p->height = 2142;
  7388. p->width = 2844;
  7389. p->raw_width = 4288;
  7390. p->pre_mul[0] = 1.797f;
  7391. p->pre_mul[2] = 1.219f;
  7392. } else if (!strcmp(p->model,"EX-P700")) {
  7393. p->height = 2318;
  7394. p->width = 3082;
  7395. p->raw_width = 4672;
  7396. p->pre_mul[0] = 1.758f;
  7397. p->pre_mul[2] = 1.504f;
  7398. }
  7399. if (!p->model[0])
  7400. sprintf (p->model, "%dx%d", p->width, p->height);
  7401. if (p->filters == UINT_MAX) p->filters = 0x94949494;
  7402. if (p->raw_color) dcr_adobe_coeff (p, p->make, p->model);
  7403. if (p->thumb_offset && !p->thumb_height) {
  7404. dcr_fseek(p->obj_, p->thumb_offset, SEEK_SET);
  7405. if (dcr_ljpeg_start (p,&jh, 1)) {
  7406. p->thumb_width = jh.wide;
  7407. p->thumb_height = jh.high;
  7408. }
  7409. }
  7410. dng_skip:
  7411. if (!p->load_raw || p->height < 22) p->is_raw = 0;
  7412. #ifdef NO_JPEG
  7413. if (p->load_raw == &DCR_CLASS dcr_kodak_jpeg_load_raw) {
  7414. fprintf (stderr,_("%s: You must link dcraw with libjpeg!!\n"), p->ifname);
  7415. p->is_raw = 0;
  7416. }
  7417. #endif
  7418. if (!p->cdesc[0])
  7419. strcpy (p->cdesc, p->colors == 3 ? "RGB":"GMCY");
  7420. if (!p->raw_height) p->raw_height = p->height;
  7421. if (!p->raw_width ) p->raw_width = p->width;
  7422. if (p->filters && p->colors == 3)
  7423. for (i=0; i < 32; i+=4) {
  7424. if ((p->filters >> i & 15) == 9)
  7425. p->filters |= 2 << i;
  7426. if ((p->filters >> i & 15) == 6)
  7427. p->filters |= 8 << i;
  7428. }
  7429. notraw:
  7430. if (p->flip == -1) p->flip = p->tiff_flip;
  7431. if (p->flip == -1) p->flip = 0;
  7432. }
  7433. #ifndef NO_LCMS
  7434. void DCR_CLASS dcr_apply_profile (DCRAW* p, char *input, char *output)
  7435. {
  7436. char *prof;
  7437. cmsHPROFILE hInProfile=0, hOutProfile=0;
  7438. cmsHTRANSFORM hTransform;
  7439. FILE *fp;
  7440. unsigned size;
  7441. cmsErrorAction (LCMS_ERROR_SHOW);
  7442. if (strcmp (input, "embed"))
  7443. hInProfile = cmsOpenProfileFromFile (input, "r");
  7444. else if (p->profile_length) {
  7445. prof = (char *) malloc (p->profile_length);
  7446. dcr_merror (p, prof, "apply_profile()");
  7447. dcr_fseek(p->obj_, p->profile_offset, SEEK_SET);
  7448. dcr_fread(p->obj_, prof, 1, p->profile_length);
  7449. hInProfile = cmsOpenProfileFromMem (prof, p->profile_length);
  7450. free (prof);
  7451. } else
  7452. fprintf (stderr,_("%s has no embedded profile.\n"), p->ifname);
  7453. if (!hInProfile) return;
  7454. if (!output)
  7455. hOutProfile = cmsCreate_sRGBProfile();
  7456. else if ((fp = fopen (output, "rb"))) {
  7457. dcr_fread(p->obj_, &size, 4, 1);
  7458. fseek (fp, 0, SEEK_SET);
  7459. p->oprof = (unsigned *) malloc (size = ntohl(size));
  7460. dcr_merror (p, p->oprof, "apply_profile()");
  7461. dcr_fread(p->obj_, p->oprof, 1, size);
  7462. fclose (fp);
  7463. if (!(hOutProfile = cmsOpenProfileFromMem (p->oprof, size))) {
  7464. free (p->oprof);
  7465. p->oprof = 0;
  7466. }
  7467. } else
  7468. fprintf (stderr,_("Cannot open file %s!\n"), output);
  7469. if (!hOutProfile) goto quit;
  7470. if (p->opt.verbose)
  7471. fprintf (stderr,_("Applying color profile...\n"));
  7472. hTransform = cmsCreateTransform (hInProfile, TYPE_RGBA_16,
  7473. hOutProfile, TYPE_RGBA_16, INTENT_PERCEPTUAL, 0);
  7474. cmsDoTransform (hTransform, p->image, p->image, p->width*p->height);
  7475. p->raw_color = 1; /* Don't use rgb_cam with a profile */
  7476. cmsDeleteTransform (hTransform);
  7477. cmsCloseProfile (hOutProfile);
  7478. quit:
  7479. cmsCloseProfile (hInProfile);
  7480. }
  7481. #endif
  7482. void DCR_CLASS dcr_convert_to_rgb(DCRAW* p)
  7483. {
  7484. int row, col, c, i, j, k;
  7485. ushort *img;
  7486. float out[3], out_cam[3][4];
  7487. double num, inverse[3][3];
  7488. static const double xyzd50_srgb[3][3] =
  7489. { { 0.436083, 0.385083, 0.143055 },
  7490. { 0.222507, 0.716888, 0.060608 },
  7491. { 0.013930, 0.097097, 0.714022 } };
  7492. static const double rgb_rgb[3][3] =
  7493. { { 1,0,0 }, { 0,1,0 }, { 0,0,1 } };
  7494. static const double adobe_rgb[3][3] =
  7495. { { 0.715146, 0.284856, 0.000000 },
  7496. { 0.000000, 1.000000, 0.000000 },
  7497. { 0.000000, 0.041166, 0.958839 } };
  7498. static const double wide_rgb[3][3] =
  7499. { { 0.593087, 0.404710, 0.002206 },
  7500. { 0.095413, 0.843149, 0.061439 },
  7501. { 0.011621, 0.069091, 0.919288 } };
  7502. static const double prophoto_rgb[3][3] =
  7503. { { 0.529317, 0.330092, 0.140588 },
  7504. { 0.098368, 0.873465, 0.028169 },
  7505. { 0.016879, 0.117663, 0.865457 } };
  7506. static const double (*out_rgb[])[3] =
  7507. { rgb_rgb, adobe_rgb, wide_rgb, prophoto_rgb, xyz_rgb };
  7508. static const char *name[] =
  7509. { "sRGB", "Adobe RGB (1998)", "WideGamut D65", "ProPhoto D65", "XYZ" };
  7510. static const unsigned phead[] =
  7511. { 1024, 0, 0x2100000, 0x6d6e7472, 0x52474220, 0x58595a20, 0, 0, 0,
  7512. 0x61637370, 0, 0, 0x6e6f6e65, 0, 0, 0, 0, 0xf6d6, 0x10000, 0xd32d };
  7513. unsigned pbody[] =
  7514. { 10, 0x63707274, 0, 36, /* cprt */
  7515. 0x64657363, 0, 40, /* desc */
  7516. 0x77747074, 0, 20, /* wtpt */
  7517. 0x626b7074, 0, 20, /* bkpt */
  7518. 0x72545243, 0, 14, /* rTRC */
  7519. 0x67545243, 0, 14, /* gTRC */
  7520. 0x62545243, 0, 14, /* bTRC */
  7521. 0x7258595a, 0, 20, /* rXYZ */
  7522. 0x6758595a, 0, 20, /* gXYZ */
  7523. 0x6258595a, 0, 20 }; /* bXYZ */
  7524. static const unsigned pwhite[] = { 0xf351, 0x10000, 0x116cc };
  7525. unsigned pcurve[] = { 0x63757276, 0, 1, 0x1000000 };
  7526. memcpy (out_cam, p->rgb_cam, sizeof out_cam);
  7527. p->raw_color |= p->colors == 1 || p->opt.document_mode ||
  7528. p->opt.output_color < 1 || p->opt.output_color > 5;
  7529. if (!p->raw_color) {
  7530. p->oprof = (unsigned *) calloc (phead[0], 1);
  7531. dcr_merror (p, p->oprof, "convert_to_rgb()");
  7532. memcpy (p->oprof, phead, sizeof phead);
  7533. if (p->opt.output_color == 5) p->oprof[4] = p->oprof[5];
  7534. p->oprof[0] = 132 + 12*pbody[0];
  7535. for (i=0; i < (int)pbody[0]; i++) {
  7536. p->oprof[p->oprof[0]/4] = i ? (i > 1 ? 0x58595a20 : 0x64657363) : 0x74657874;
  7537. pbody[i*3+2] = p->oprof[0];
  7538. p->oprof[0] += (pbody[i*3+3] + 3) & -4;
  7539. }
  7540. memcpy (p->oprof+32, pbody, sizeof pbody);
  7541. p->oprof[pbody[5]/4+2] = strlen(name[p->opt.output_color-1]) + 1;
  7542. memcpy ((char *)p->oprof+pbody[8]+8, pwhite, sizeof pwhite);
  7543. if (p->opt.output_bps == 8)
  7544. #ifdef SRGB_GAMMA
  7545. pcurve[3] = 0x2330000;
  7546. #else
  7547. pcurve[3] = 0x1f00000;
  7548. #endif
  7549. for (i=4; i < 7; i++)
  7550. memcpy ((char *)p->oprof+pbody[i*3+2], pcurve, sizeof pcurve);
  7551. dcr_pseudoinverse ((double (*)[3]) out_rgb[p->opt.output_color-1], inverse, 3);
  7552. for (i=0; i < 3; i++)
  7553. for (j=0; j < 3; j++) {
  7554. for (num = k=0; k < 3; k++)
  7555. num += xyzd50_srgb[i][k] * inverse[j][k];
  7556. p->oprof[pbody[j*3+23]/4+i+2] = (unsigned int)(num * 0x10000 + 0.5);
  7557. }
  7558. for (i=0; i < (int)(phead[0]/4); i++)
  7559. p->oprof[i] = htonl(p->oprof[i]);
  7560. strcpy ((char *)p->oprof+pbody[2]+8, "auto-generated by dcraw");
  7561. strcpy ((char *)p->oprof+pbody[5]+12, name[p->opt.output_color-1]);
  7562. for (i=0; i < 3; i++)
  7563. for (j=0; j < p->colors; j++)
  7564. for (out_cam[i][j] = (float)(k=0); k < 3; k++)
  7565. out_cam[i][j] += (float)out_rgb[p->opt.output_color-1][i][k] * p->rgb_cam[k][j];
  7566. }
  7567. if (p->opt.verbose)
  7568. fprintf (stderr, p->raw_color ? _("Building histograms...\n") :
  7569. _("Converting to %s colorspace...\n"), name[p->opt.output_color-1]);
  7570. memset (p->histogram, 0, sizeof p->histogram);
  7571. for (img=p->image[0], row=0; row < p->height; row++)
  7572. for (col=0; col < p->width; col++, img+=4) {
  7573. if (!p->raw_color) {
  7574. out[0] = out[1] = out[2] = 0;
  7575. FORCC(p) {
  7576. out[0] += out_cam[0][c] * img[c];
  7577. out[1] += out_cam[1][c] * img[c];
  7578. out[2] += out_cam[2][c] * img[c];
  7579. }
  7580. FORC3 img[c] = CLIP((int) out[c]);
  7581. }
  7582. else if (p->opt.document_mode)
  7583. img[0] = img[FC(row,col)];
  7584. FORCC(p) p->histogram[c][img[c] >> 3]++;
  7585. }
  7586. if (p->colors == 4 && p->opt.output_color) p->colors = 3;
  7587. if (p->opt.document_mode && p->filters) p->colors = 1;
  7588. }
  7589. void DCR_CLASS dcr_fuji_rotate(DCRAW* p)
  7590. {
  7591. int i, row, col;
  7592. double step;
  7593. float r, c, fr, fc;
  7594. unsigned ur, uc;
  7595. ushort wide, high, (*img)[4], (*pix)[4];
  7596. if (!p->fuji_width) return;
  7597. if (p->opt.verbose)
  7598. fprintf (stderr,_("Rotating image 45 degrees...\n"));
  7599. p->fuji_width = (p->fuji_width - 1 + p->shrink) >> p->shrink;
  7600. step = sqrt(0.5);
  7601. wide = (unsigned short)(p->fuji_width / step);
  7602. high = (unsigned short)((p->height - p->fuji_width) / step);
  7603. img = (ushort (*)[4]) calloc (wide*high, sizeof *img);
  7604. dcr_merror (p, img, "fuji_rotate()");
  7605. for (row=0; row < high; row++)
  7606. for (col=0; col < wide; col++) {
  7607. r = (float)(p->fuji_width + (row-col)*step);
  7608. ur = (unsigned int)r;
  7609. c = (float)((row+col)*step);
  7610. uc = (unsigned int)c;
  7611. if ((int)ur > (int)(p->height-2) || (int)uc > (int)(p->width-2)) continue;
  7612. fr = r - ur;
  7613. fc = c - uc;
  7614. pix = p->image + ur*p->width + uc;
  7615. for (i=0; i < p->colors; i++)
  7616. img[row*wide+col][i] = (unsigned short)(
  7617. (pix[ 0][i]*(1-fc) + pix[ 1][i]*fc) * (1-fr) +
  7618. (pix[p->width][i]*(1-fc) + pix[p->width+1][i]*fc) * fr);
  7619. }
  7620. free (p->image);
  7621. p->width = wide;
  7622. p->height = high;
  7623. p->image = img;
  7624. p->fuji_width = 0;
  7625. }
  7626. void DCR_CLASS dcr_stretch(DCRAW* p)
  7627. {
  7628. ushort newdim, (*img)[4], *pix0, *pix1;
  7629. int row, col, c;
  7630. double rc, frac;
  7631. if (p->pixel_aspect == 1) return;
  7632. if (p->opt.verbose) fprintf (stderr,_("Stretching the image...\n"));
  7633. if (p->pixel_aspect < 1) {
  7634. newdim = (unsigned short)(p->height / p->pixel_aspect + 0.5);
  7635. img = (ushort (*)[4]) calloc (p->width*newdim, sizeof *img);
  7636. dcr_merror (p, img, "stretch()");
  7637. for (rc=row=0; row < newdim; row++, rc+=p->pixel_aspect) {
  7638. frac = rc - (c = (int)rc);
  7639. pix0 = pix1 = p->image[c*p->width];
  7640. if (c+1 < p->height) pix1 += p->width*4;
  7641. for (col=0; col < p->width; col++, pix0+=4, pix1+=4)
  7642. FORCC(p) img[row*p->width+col][c] = (unsigned short)(pix0[c]*(1-frac) + pix1[c]*frac + 0.5);
  7643. }
  7644. p->height = newdim;
  7645. } else {
  7646. newdim = (unsigned short)(p->width * p->pixel_aspect + 0.5);
  7647. img = (ushort (*)[4]) calloc (p->height*newdim, sizeof *img);
  7648. dcr_merror (p, img, "stretch()");
  7649. for (rc=col=0; col < newdim; col++, rc+=1/p->pixel_aspect) {
  7650. frac = rc - (c = (int)rc);
  7651. pix0 = pix1 = p->image[c];
  7652. if (c+1 < p->width) pix1 += 4;
  7653. for (row=0; row < p->height; row++, pix0+=p->width*4, pix1+=p->width*4)
  7654. FORCC(p) img[row*newdim+col][c] = (unsigned short)(pix0[c]*(1-frac) + pix1[c]*frac + 0.5);
  7655. }
  7656. p->width = newdim;
  7657. }
  7658. free (p->image);
  7659. p->image = img;
  7660. }
  7661. int DCR_CLASS dcr_flip_index (DCRAW* p, int row, int col)
  7662. {
  7663. if (p->flip & 4) SWAP(row,col);
  7664. if (p->flip & 2) row = p->iheight - 1 - row;
  7665. if (p->flip & 1) col = p->iwidth - 1 - col;
  7666. return row * p->iwidth + col;
  7667. }
  7668. void DCR_CLASS dcr_gamma_lut (DCRAW* p, uchar lut[0x10000])
  7669. {
  7670. int perc, c, val, total, i;
  7671. float white=0, r;
  7672. perc = (int)(p->width * p->height * 0.01); /* 99th percentile white level */
  7673. if (p->fuji_width) perc /= 2;
  7674. if ((p->opt.highlight & ~2) || p->opt.no_auto_bright) perc = -1;
  7675. FORCC(p) {
  7676. for (val=0x2000, total=0; --val > 32; )
  7677. if ((total += p->histogram[c][val]) > perc) break;
  7678. if (white < val) white = (float)val;
  7679. }
  7680. white *= 8 / p->opt.bright;
  7681. for (i=0; i < 0x10000; i++) {
  7682. r = i / white;
  7683. val = (int)(256 * ( !p->use_gamma ? r :
  7684. #ifdef SRGB_GAMMA
  7685. r <= 0.00304 ? r*12.92 : pow(r,2.5/6)*1.055-0.055 ));
  7686. #else
  7687. r <= 0.018 ? r*4.5 : pow(r,0.45)*1.099-0.099 ));
  7688. #endif
  7689. if (val > 255) val = 255;
  7690. lut[i] = val;
  7691. }
  7692. }
  7693. void DCR_CLASS dcr_tiff_set (ushort *ntag,
  7694. ushort tag, ushort type, int count, int val)
  7695. {
  7696. struct dcr_tiff_tag *tt;
  7697. int c;
  7698. tt = (struct dcr_tiff_tag *)(ntag+1) + (*ntag)++;
  7699. tt->tag = tag;
  7700. tt->type = type;
  7701. tt->count = count;
  7702. if (type < 3 && count <= 4)
  7703. FORC(4) tt->val.c[c] = val >> (c << 3);
  7704. else if (type == 3 && count <= 2)
  7705. FORC(2) tt->val.s[c] = val >> (c << 4);
  7706. else tt->val.i = val;
  7707. }
  7708. #define TOFF(ptr) ((char *)(&(ptr)) - (char *)th)
  7709. void DCR_CLASS dcr_tiff_head (DCRAW* p, struct dcr_tiff_hdr *th, int full)
  7710. {
  7711. int c, psize=0;
  7712. struct tm *t;
  7713. memset (th, 0, sizeof *th);
  7714. th->order = (unsigned short)(htonl(0x4d4d4949) >> 16);
  7715. th->magic = 42;
  7716. th->ifd = 10;
  7717. if (full) {
  7718. dcr_tiff_set (&th->ntag, 254, 4, 1, 0);
  7719. dcr_tiff_set (&th->ntag, 256, 4, 1, p->width);
  7720. dcr_tiff_set (&th->ntag, 257, 4, 1, p->height);
  7721. dcr_tiff_set (&th->ntag, 258, 3, p->colors, p->opt.output_bps);
  7722. if (p->colors > 2)
  7723. th->tag[th->ntag-1].val.i = TOFF(th->bps);
  7724. FORC4 th->bps[c] = p->opt.output_bps;
  7725. dcr_tiff_set (&th->ntag, 259, 3, 1, 1);
  7726. dcr_tiff_set (&th->ntag, 262, 3, 1, 1 + (p->colors > 1));
  7727. }
  7728. dcr_tiff_set (&th->ntag, 270, 2, 512, TOFF(th->desc));
  7729. dcr_tiff_set (&th->ntag, 271, 2, 64, TOFF(th->make));
  7730. dcr_tiff_set (&th->ntag, 272, 2, 64, TOFF(th->model));
  7731. if (full) {
  7732. if (p->oprof) psize = ntohl(p->oprof[0]);
  7733. dcr_tiff_set (&th->ntag, 273, 4, 1, sizeof *th + psize);
  7734. dcr_tiff_set (&th->ntag, 277, 3, 1, p->colors);
  7735. dcr_tiff_set (&th->ntag, 278, 4, 1, p->height);
  7736. dcr_tiff_set (&th->ntag, 279, 4, 1, p->height*p->width*p->colors*p->opt.output_bps/8);
  7737. } else
  7738. dcr_tiff_set (&th->ntag, 274, 3, 1, "12435867"[p->flip]-'0');
  7739. dcr_tiff_set (&th->ntag, 282, 5, 1, TOFF(th->rat[0]));
  7740. dcr_tiff_set (&th->ntag, 283, 5, 1, TOFF(th->rat[2]));
  7741. dcr_tiff_set (&th->ntag, 284, 3, 1, 1);
  7742. dcr_tiff_set (&th->ntag, 296, 3, 1, 2);
  7743. dcr_tiff_set (&th->ntag, 305, 2, 32, TOFF(th->soft));
  7744. dcr_tiff_set (&th->ntag, 306, 2, 20, TOFF(th->date));
  7745. dcr_tiff_set (&th->ntag, 315, 2, 64, TOFF(th->artist));
  7746. dcr_tiff_set (&th->ntag, 34665, 4, 1, TOFF(th->nexif));
  7747. if (psize) dcr_tiff_set (&th->ntag, 34675, 7, psize, sizeof *th);
  7748. dcr_tiff_set (&th->nexif, 33434, 5, 1, TOFF(th->rat[4]));
  7749. dcr_tiff_set (&th->nexif, 33437, 5, 1, TOFF(th->rat[6]));
  7750. dcr_tiff_set (&th->nexif, 34855, 3, 1, (int)p->iso_speed);
  7751. dcr_tiff_set (&th->nexif, 37386, 5, 1, TOFF(th->rat[8]));
  7752. if (p->gpsdata[1]) {
  7753. dcr_tiff_set (&th->ntag, 34853, 4, 1, TOFF(th->ngps));
  7754. dcr_tiff_set (&th->ngps, 0, 1, 4, 0x202);
  7755. dcr_tiff_set (&th->ngps, 1, 2, 2, p->gpsdata[29]);
  7756. dcr_tiff_set (&th->ngps, 2, 5, 3, TOFF(th->gps[0]));
  7757. dcr_tiff_set (&th->ngps, 3, 2, 2, p->gpsdata[30]);
  7758. dcr_tiff_set (&th->ngps, 4, 5, 3, TOFF(th->gps[6]));
  7759. dcr_tiff_set (&th->ngps, 5, 1, 1, p->gpsdata[31]);
  7760. dcr_tiff_set (&th->ngps, 6, 5, 1, TOFF(th->gps[18]));
  7761. dcr_tiff_set (&th->ngps, 7, 5, 3, TOFF(th->gps[12]));
  7762. dcr_tiff_set (&th->ngps, 18, 2, 12, TOFF(th->gps[20]));
  7763. dcr_tiff_set (&th->ngps, 29, 2, 12, TOFF(th->gps[23]));
  7764. memcpy (th->gps, p->gpsdata, sizeof th->gps);
  7765. }
  7766. th->rat[0] = th->rat[2] = 300;
  7767. th->rat[1] = th->rat[3] = 1;
  7768. FORC(6) th->rat[4+c] = 1000000;
  7769. th->rat[4] = (int)(th->rat[4] * p->shutter);
  7770. th->rat[6] = (int)(th->rat[6] * p->aperture);
  7771. th->rat[8] = (int)(th->rat[8] * p->focal_len);
  7772. strncpy (th->desc, p->desc, 512);
  7773. strncpy (th->make, p->make, 64);
  7774. strncpy (th->model, p->model, 64);
  7775. strcpy (th->soft, "dcraw v"DCR_VERSION);
  7776. t = gmtime (&p->timestamp);
  7777. sprintf (th->date, "%04d:%02d:%02d %02d:%02d:%02d",
  7778. t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
  7779. strncpy (th->artist, p->artist, 64);
  7780. }
  7781. void DCR_CLASS dcr_jpeg_thumb (DCRAW* p, FILE *tfp)
  7782. {
  7783. char *thumb;
  7784. ushort exif[5];
  7785. struct dcr_tiff_hdr th;
  7786. thumb = (char *) malloc (p->thumb_length);
  7787. dcr_merror (p, thumb, "jpeg_thumb()");
  7788. dcr_fread(p->obj_, thumb, 1, p->thumb_length);
  7789. fputc (0xff, tfp);
  7790. fputc (0xd8, tfp);
  7791. if (strcmp (thumb+6, "Exif")) {
  7792. memcpy (exif, "\xff\xe1 Exif\0\0", 10);
  7793. exif[1] = htons (8 + sizeof th);
  7794. fwrite (exif, 1, sizeof exif, tfp);
  7795. dcr_tiff_head (p,&th, 0);
  7796. fwrite (&th, 1, sizeof th, tfp);
  7797. }
  7798. fwrite (thumb+2, 1, p->thumb_length-2, tfp);
  7799. free (thumb);
  7800. }
  7801. void DCR_CLASS dcr_write_ppm_tiff (DCRAW* p, FILE *ofp)
  7802. {
  7803. struct dcr_tiff_hdr th;
  7804. uchar *ppm, lut[0x10000];
  7805. ushort *ppm2;
  7806. int c, row, col, soff, rstep, cstep;
  7807. p->iheight = p->height;
  7808. p->iwidth = p->width;
  7809. if (p->flip & 4) SWAP(p->height,p->width);
  7810. ppm = (uchar *) calloc (p->width, p->colors*p->opt.output_bps/8);
  7811. ppm2 = (ushort *) ppm;
  7812. dcr_merror (p, ppm, "write_ppm_tiff()");
  7813. if (p->opt.output_tiff) {
  7814. dcr_tiff_head (p, &th, 1);
  7815. fwrite (&th, sizeof th, 1, ofp);
  7816. if (p->oprof)
  7817. fwrite (p->oprof, ntohl(p->oprof[0]), 1, ofp);
  7818. } else if (p->colors > 3)
  7819. fprintf (ofp,
  7820. "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
  7821. p->width, p->height, p->colors, (1 << p->opt.output_bps)-1, p->cdesc);
  7822. else
  7823. fprintf (ofp, "P%d\n%d %d\n%d\n",
  7824. p->colors/2+5, p->width, p->height, (1 << p->opt.output_bps)-1);
  7825. if (p->opt.output_bps == 8) dcr_gamma_lut (p, lut);
  7826. soff = dcr_flip_index (p, 0, 0);
  7827. cstep = dcr_flip_index (p, 0, 1) - soff;
  7828. rstep = dcr_flip_index (p, 1, 0) - dcr_flip_index (p, 0, p->width);
  7829. for (row=0; row < p->height; row++, soff += rstep) {
  7830. for (col=0; col < p->width; col++, soff += cstep){
  7831. if (p->opt.output_bps == 8)
  7832. FORCC(p) ppm [col*p->colors+c] = lut[p->image[soff][c]];
  7833. else
  7834. FORCC(p) ppm2[col*p->colors+c] = p->image[soff][c];
  7835. }
  7836. if (p->opt.output_bps == 16 && !p->opt.output_tiff && htons(0x55aa) != 0x55aa)
  7837. _swab ((char*)ppm2, (char*)ppm2, p->width*p->colors*2);
  7838. fwrite (ppm, p->colors*p->opt.output_bps/8, p->width, ofp);
  7839. }
  7840. free (ppm);
  7841. }
  7842. ///////////////////////////////////////////////////////////////////////////////
  7843. static int dcr_sfile_read(dcr_stream_obj *obj, void *buf, int size, int cnt)
  7844. {
  7845. return fread(buf, size, cnt, (FILE *)obj);
  7846. }
  7847. static int dcr_sfile_write(dcr_stream_obj *obj, void *buf, int size, int cnt)
  7848. {
  7849. return fwrite(buf, size, cnt, (FILE *)obj);
  7850. }
  7851. static long dcr_sfile_seek(dcr_stream_obj *obj, long offset, int origin)
  7852. {
  7853. return fseek((FILE *)obj, offset, origin);
  7854. }
  7855. static int dcr_sfile_close(dcr_stream_obj *obj)
  7856. {
  7857. return fclose((FILE *)obj);
  7858. }
  7859. static char* dcr_sfile_gets(dcr_stream_obj *obj, char *string, int n)
  7860. {
  7861. return fgets(string,n,(FILE *)obj);
  7862. }
  7863. static int dcr_sfile_eof(dcr_stream_obj *obj)
  7864. {
  7865. return feof((FILE *)obj);
  7866. }
  7867. static long dcr_sfile_tell(dcr_stream_obj *obj)
  7868. {
  7869. return ftell((FILE *)obj);
  7870. }
  7871. static int dcr_sfile_getc(dcr_stream_obj *obj)
  7872. {
  7873. return fgetc((FILE *)obj);
  7874. }
  7875. static int dcr_sfile_scanf(dcr_stream_obj *obj,const char *format, void* output)
  7876. {
  7877. return fscanf((FILE *)obj, format, output);
  7878. }
  7879. ///////////////////////////////////////////////////////////////////////////////
  7880. void DCR_CLASS dcr_init_dcraw(DCRAW* p)
  7881. {
  7882. memset(p,0,sizeof(DCRAW));
  7883. p->ops_ = &dcr_stream_fileops;
  7884. p->opt.dark_frame = NULL;
  7885. p->opt.bpfile = NULL;
  7886. p->opt.user_flip = -1;
  7887. p->opt.user_black = -1;
  7888. p->opt.user_qual = -1;
  7889. p->opt.user_sat = -1;
  7890. p->opt.timestamp_only = 0;
  7891. p->opt.thumbnail_only = 0;
  7892. p->opt.identify_only = 0;
  7893. p->opt.use_fuji_rotate = 1;
  7894. p->opt.write_to_stdout = 0;
  7895. p->opt.bright = 1;
  7896. p->opt.aber[0] = p->opt.aber[1] = p->opt.aber[2] = p->opt.aber[3] = 1;
  7897. p->opt.output_color = 1;
  7898. p->opt.output_bps =8;
  7899. p->opt.greybox[0] = p->opt.greybox[1] = 0;
  7900. p->opt.greybox[2] = p->opt.greybox[3] = UINT_MAX;
  7901. p->opt.use_camera_matrix = -1;
  7902. }
  7903. void DCR_CLASS dcr_cleanup_dcraw(DCRAW* p)
  7904. {
  7905. if (p->meta_data) free (p->meta_data);
  7906. if (p->oprof) free (p->oprof);
  7907. if (p->image) free (p->image);
  7908. }
  7909. void DCR_CLASS dcr_print_manual(int argc, char **argv)
  7910. {
  7911. printf(_("\nRaw photo decoder \"dcraw\" v%s"), DCR_VERSION);
  7912. printf(_("\nby Dave Coffin, dcoffin a cybercom o net\n"));
  7913. printf(_("\nUsage: %s [OPTION]... [FILE]...\n\n"), argv[0]);
  7914. puts(_("-v Print verbose messages"));
  7915. puts(_("-c Write image data to standard output"));
  7916. puts(_("-e Extract embedded thumbnail image"));
  7917. puts(_("-i Identify files without decoding them"));
  7918. puts(_("-i -v Identify files and show metadata"));
  7919. puts(_("-z Change file dates to camera timestamp"));
  7920. puts(_("-w Use camera white balance, if possible"));
  7921. puts(_("-a Average the whole image for white balance"));
  7922. puts(_("-A <x y w h> Average a grey box for white balance"));
  7923. puts(_("-r <r g b g> Set custom white balance"));
  7924. puts(_("+M/-M Use/don't use an embedded color matrix"));
  7925. puts(_("-C <r b> Correct chromatic aberration"));
  7926. puts(_("-P <file> Fix the dead pixels listed in this file"));
  7927. puts(_("-K <file> Subtract dark frame (16-bit raw PGM)"));
  7928. puts(_("-k <num> Set the darkness level"));
  7929. puts(_("-S <num> Set the saturation level"));
  7930. puts(_("-n <num> Set threshold for wavelet denoising"));
  7931. puts(_("-H [0-9] Highlight mode (0=clip, 1=unclip, 2=blend, 3+=rebuild)"));
  7932. puts(_("-t [0-7] Flip image (0=none, 3=180, 5=90CCW, 6=90CW)"));
  7933. puts(_("-o [0-5] Output colorspace (raw,sRGB,Adobe,Wide,ProPhoto,XYZ)"));
  7934. #ifndef NO_LCMS
  7935. puts(_("-o <file> Apply output ICC profile from file"));
  7936. puts(_("-p <file> Apply camera ICC profile from file or \"embed\""));
  7937. #endif
  7938. puts(_("-d Document mode (no color, no interpolation)"));
  7939. puts(_("-D Document mode without scaling (totally raw)"));
  7940. puts(_("-j Don't stretch or rotate raw pixels"));
  7941. puts(_("-W Don't automatically brighten the image"));
  7942. puts(_("-b <num> Adjust brightness (default = 1.0)"));
  7943. puts(_("-q [0-3] Set the interpolation quality"));
  7944. puts(_("-h Half-size color image (twice as fast as \"-q 0\")"));
  7945. puts(_("-f Interpolate RGGB as four colors"));
  7946. puts(_("-m <num> Apply a 3x3 median filter to R-G and B-G"));
  7947. puts(_("-s [0..N-1] Select one raw image or \"all\" from each file"));
  7948. puts(_("-4 Write 16-bit linear instead of 8-bit with gamma"));
  7949. puts(_("-T Write TIFF instead of PPM"));
  7950. puts("");
  7951. }
  7952. int DCR_CLASS dcr_parse_command_line_options(DCRAW* p, int argc, char **argv, int *arg)
  7953. {
  7954. char opm, opt, *sp, *cp;
  7955. int i, c;
  7956. if (argv && arg) {
  7957. argv[argc] = "";
  7958. for (*arg=1; (((opm = argv[*arg][0]) - 2) | 2) == '+'; ) {
  7959. opt = argv[(*arg)++][1];
  7960. if ((cp = strchr (sp="nbrkStqmHAC", opt)))
  7961. for (i=0; i < "11411111142"[cp-sp]-'0'; i++)
  7962. if (!isdigit(argv[(*arg)+i][0])) {
  7963. fprintf (stderr,_("Non-numeric argument to \"-%c\"\n"), opt);
  7964. return 1;
  7965. }
  7966. switch (opt) {
  7967. case 'n': p->opt.threshold = (float)atof(argv[(*arg)++]); break;
  7968. case 'b': p->opt.bright = (float)atof(argv[(*arg)++]); break;
  7969. case 'r': FORC4 p->opt.user_mul[c] = (float)atof(argv[(*arg)++]); break;
  7970. case 'C': p->opt.aber[0] = 1 / atof(argv[(*arg)++]);
  7971. p->opt.aber[2] = 1 / atof(argv[(*arg)++]); break;
  7972. case 'k': p->opt.user_black = atoi(argv[(*arg)++]); break;
  7973. case 'S': p->opt.user_sat = atoi(argv[(*arg)++]); break;
  7974. case 't': p->opt.user_flip = atoi(argv[(*arg)++]); break;
  7975. case 'q': p->opt.user_qual = atoi(argv[(*arg)++]); break;
  7976. case 'm': p->opt.med_passes = atoi(argv[(*arg)++]); break;
  7977. case 'H': p->opt.highlight = atoi(argv[(*arg)++]); break;
  7978. case 's':
  7979. p->opt.shot_select = abs(atoi(argv[*arg]));
  7980. p->opt.multi_out = !strcmp(argv[(*arg)++],"all");
  7981. break;
  7982. case 'o':
  7983. if (isdigit(argv[*arg][0]) && !argv[*arg][1])
  7984. p->opt.output_color = atoi(argv[(*arg)++]);
  7985. #ifndef NO_LCMS
  7986. else p->opt.out_profile = argv[(*arg)++];
  7987. break;
  7988. case 'p': p->opt.cam_profile = argv[(*arg)++];
  7989. #endif
  7990. break;
  7991. case 'P': p->opt.bpfile = argv[(*arg)++]; break;
  7992. case 'K': p->opt.dark_frame = argv[(*arg)++]; break;
  7993. case 'z': p->opt.timestamp_only = 1; break;
  7994. case 'e': p->opt.thumbnail_only = 1; break;
  7995. case 'i': p->opt.identify_only = 1; break;
  7996. case 'c': p->opt.write_to_stdout = 1; break;
  7997. case 'v': p->opt.verbose = 1; break;
  7998. case 'h': p->opt.half_size = 1; /* "-h" implies "-f" */
  7999. case 'f': p->opt.four_color_rgb = 1; break;
  8000. case 'A': FORC4 p->opt.greybox[c] = atoi(argv[(*arg)++]);
  8001. case 'a': p->opt.use_auto_wb = 1; break;
  8002. case 'w': p->opt.use_camera_wb = 1; break;
  8003. case 'M': p->opt.use_camera_matrix = (opm == '+'); break;
  8004. case 'D':
  8005. case 'd': p->opt.document_mode = 1 + (opt == 'D');
  8006. case 'j': p->opt.use_fuji_rotate = 0; break;
  8007. case 'W': p->opt.no_auto_bright = 1; break;
  8008. case 'T': p->opt.output_tiff = 1; break;
  8009. case '4': p->opt.output_bps = 16; break;
  8010. default:
  8011. fprintf (stderr,_("Unknown option \"-%c\".\n"), opt);
  8012. return 1;
  8013. }
  8014. }
  8015. }
  8016. if (p->opt.use_camera_matrix < 0)
  8017. p->opt.use_camera_matrix = p->opt.use_camera_wb;
  8018. return 0;
  8019. }