PageRenderTime 116ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 1ms

/Dependencies/Source/FreeImage/LibRawLite/internal/dcraw_common.cpp

http://gamekit.googlecode.com/
C++ | 7449 lines | 7024 code | 307 blank | 118 comment | 2370 complexity | cda8664a6c75ec055a3253a2d8f7f911 MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.0, AGPL-3.0, BSD-3-Clause, GPL-2.0, LGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, MIT
  1. /*
  2. GENERATED FILE, DO NOT EDIT
  3. Generated from dcraw/dcraw.c at Mon May 4 22:14:56 2009
  4. Look into original file (probably http://cybercom.net/~dcoffin/dcraw/dcraw.c)
  5. for copyright information.
  6. */
  7. #line 257 "dcraw/dcraw.c"
  8. #define CLASS LibRaw::
  9. #include "libraw/libraw_types.h"
  10. #define LIBRAW_LIBRARY_BUILD
  11. #define LIBRAW_IO_REDEFINED
  12. #include "libraw/libraw.h"
  13. #include "internal/defines.h"
  14. #include "internal/var_defines.h"
  15. #line 267 "dcraw/dcraw.c"
  16. #ifndef __GLIBC__
  17. char *my_memmem (char *haystack, size_t haystacklen,
  18. char *needle, size_t needlelen)
  19. {
  20. char *c;
  21. for (c = haystack; c <= haystack + haystacklen - needlelen; c++)
  22. if (!memcmp (c, needle, needlelen))
  23. return c;
  24. return 0;
  25. }
  26. #define memmem my_memmem
  27. #endif
  28. #line 301 "dcraw/dcraw.c"
  29. ushort CLASS sget2 (uchar *s)
  30. {
  31. if (order == 0x4949) /* "II" means little-endian */
  32. return s[0] | s[1] << 8;
  33. else /* "MM" means big-endian */
  34. return s[0] << 8 | s[1];
  35. }
  36. ushort CLASS get2()
  37. {
  38. uchar str[2] = { 0xff,0xff };
  39. fread (str, 1, 2, ifp);
  40. return sget2(str);
  41. }
  42. unsigned CLASS sget4 (uchar *s)
  43. {
  44. if (order == 0x4949)
  45. return s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
  46. else
  47. return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3];
  48. }
  49. #define sget4(s) sget4((uchar *)s)
  50. unsigned CLASS get4()
  51. {
  52. uchar str[4] = { 0xff,0xff,0xff,0xff };
  53. fread (str, 1, 4, ifp);
  54. return sget4(str);
  55. }
  56. unsigned CLASS getint (int type)
  57. {
  58. return type == 3 ? get2() : get4();
  59. }
  60. float CLASS int_to_float (int i)
  61. {
  62. union { int i; float f; } u;
  63. u.i = i;
  64. return u.f;
  65. }
  66. double CLASS getreal (int type)
  67. {
  68. union { char c[8]; double d; } u;
  69. int i, rev;
  70. switch (type) {
  71. case 3: return (unsigned short) get2();
  72. case 4: return (unsigned int) get4();
  73. case 5: u.d = (unsigned int) get4();
  74. return u.d / (unsigned int) get4();
  75. case 8: return (signed short) get2();
  76. case 9: return (signed int) get4();
  77. case 10: u.d = (signed int) get4();
  78. return u.d / (signed int) get4();
  79. case 11: return int_to_float (get4());
  80. case 12:
  81. rev = 7 * ((order == 0x4949) == (ntohs(0x1234) == 0x1234));
  82. for (i=0; i < 8; i++)
  83. u.c[i ^ rev] = fgetc(ifp);
  84. return u.d;
  85. default: return fgetc(ifp);
  86. }
  87. }
  88. void CLASS read_shorts (ushort *pixel, int count)
  89. {
  90. if (fread (pixel, 2, count, ifp) < count) derror();
  91. if ((order == 0x4949) == (ntohs(0x1234) == 0x1234))
  92. swab ((char*)pixel, (char*)pixel, count*2);
  93. }
  94. #line 378 "dcraw/dcraw.c"
  95. void CLASS canon_black (double dark[2])
  96. {
  97. int c, diff, row, col;
  98. if (raw_width < width+4) return;
  99. FORC(2) dark[c] /= (raw_width-width-2) * height >> 1;
  100. if ((diff = dark[0] - dark[1]))
  101. for (row=0; row < height; row++)
  102. for (col=1; col < width; col+=2)
  103. BAYER(row,col) += diff;
  104. dark[1] += diff;
  105. black = (dark[0] + dark[1] + 1) / 2;
  106. }
  107. void CLASS canon_600_fixed_wb (int temp)
  108. {
  109. static const short mul[4][5] = {
  110. { 667, 358,397,565,452 },
  111. { 731, 390,367,499,517 },
  112. { 1119, 396,348,448,537 },
  113. { 1399, 485,431,508,688 } };
  114. int lo, hi, i;
  115. float frac=0;
  116. for (lo=4; --lo; )
  117. if (*mul[lo] <= temp) break;
  118. for (hi=0; hi < 3; hi++)
  119. if (*mul[hi] >= temp) break;
  120. if (lo != hi)
  121. frac = (float) (temp - *mul[lo]) / (*mul[hi] - *mul[lo]);
  122. for (i=1; i < 5; i++)
  123. pre_mul[i-1] = 1 / (frac * mul[hi][i] + (1-frac) * mul[lo][i]);
  124. }
  125. /* Return values: 0 = white 1 = near white 2 = not white */
  126. int CLASS canon_600_color (int ratio[2], int mar)
  127. {
  128. int clipped=0, target, miss;
  129. if (flash_used) {
  130. if (ratio[1] < -104)
  131. { ratio[1] = -104; clipped = 1; }
  132. if (ratio[1] > 12)
  133. { ratio[1] = 12; clipped = 1; }
  134. } else {
  135. if (ratio[1] < -264 || ratio[1] > 461) return 2;
  136. if (ratio[1] < -50)
  137. { ratio[1] = -50; clipped = 1; }
  138. if (ratio[1] > 307)
  139. { ratio[1] = 307; clipped = 1; }
  140. }
  141. target = flash_used || ratio[1] < 197
  142. ? -38 - (398 * ratio[1] >> 10)
  143. : -123 + (48 * ratio[1] >> 10);
  144. if (target - mar <= ratio[0] &&
  145. target + 20 >= ratio[0] && !clipped) return 0;
  146. miss = target - ratio[0];
  147. if (abs(miss) >= mar*4) return 2;
  148. if (miss < -20) miss = -20;
  149. if (miss > mar) miss = mar;
  150. ratio[0] = target - miss;
  151. return 1;
  152. }
  153. void CLASS canon_600_auto_wb()
  154. {
  155. int mar, row, col, i, j, st, count[] = { 0,0 };
  156. int test[8], total[2][8], ratio[2][2], stat[2];
  157. memset (&total, 0, sizeof total);
  158. i = canon_ev + 0.5;
  159. if (i < 10) mar = 150;
  160. else if (i > 12) mar = 20;
  161. else mar = 280 - 20 * i;
  162. if (flash_used) mar = 80;
  163. for (row=14; row < height-14; row+=4)
  164. for (col=10; col < width; col+=2) {
  165. for (i=0; i < 8; i++)
  166. test[(i & 4) + FC(row+(i >> 1),col+(i & 1))] =
  167. BAYER(row+(i >> 1),col+(i & 1));
  168. for (i=0; i < 8; i++)
  169. if (test[i] < 150 || test[i] > 1500) goto next;
  170. for (i=0; i < 4; i++)
  171. if (abs(test[i] - test[i+4]) > 50) goto next;
  172. for (i=0; i < 2; i++) {
  173. for (j=0; j < 4; j+=2)
  174. ratio[i][j >> 1] = ((test[i*4+j+1]-test[i*4+j]) << 10) / test[i*4+j];
  175. stat[i] = canon_600_color (ratio[i], mar);
  176. }
  177. if ((st = stat[0] | stat[1]) > 1) goto next;
  178. for (i=0; i < 2; i++)
  179. if (stat[i])
  180. for (j=0; j < 2; j++)
  181. test[i*4+j*2+1] = test[i*4+j*2] * (0x400 + ratio[i][j]) >> 10;
  182. for (i=0; i < 8; i++)
  183. total[st][i] += test[i];
  184. count[st]++;
  185. next: ;
  186. }
  187. if (count[0] | count[1]) {
  188. st = count[0]*200 < count[1];
  189. for (i=0; i < 4; i++)
  190. pre_mul[i] = 1.0 / (total[st][i] + total[st][i+4]);
  191. }
  192. }
  193. void CLASS canon_600_coeff()
  194. {
  195. static const short table[6][12] = {
  196. { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
  197. { -1203,1715,-1136,1648, 1388,-876,267,245, -1641,2153,3921,-3409 },
  198. { -615,1127,-1563,2075, 1437,-925,509,3, -756,1268,2519,-2007 },
  199. { -190,702,-1886,2398, 2153,-1641,763,-251, -452,964,3040,-2528 },
  200. { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
  201. { -807,1319,-1785,2297, 1388,-876,769,-257, -230,742,2067,-1555 } };
  202. int t=0, i, c;
  203. float mc, yc;
  204. mc = pre_mul[1] / pre_mul[2];
  205. yc = pre_mul[3] / pre_mul[2];
  206. if (mc > 1 && mc <= 1.28 && yc < 0.8789) t=1;
  207. if (mc > 1.28 && mc <= 2) {
  208. if (yc < 0.8789) t=3;
  209. else if (yc <= 2) t=4;
  210. }
  211. if (flash_used) t=5;
  212. for (raw_color = i=0; i < 3; i++)
  213. FORCC rgb_cam[i][c] = table[t][i*4 + c] / 1024.0;
  214. }
  215. void CLASS canon_600_load_raw()
  216. {
  217. uchar data[1120], *dp;
  218. ushort pixel[896], *pix;
  219. int irow, row, col, val;
  220. static const short mul[4][2] =
  221. { { 1141,1145 }, { 1128,1109 }, { 1178,1149 }, { 1128,1109 } };
  222. for (irow=row=0; irow < height; irow++) {
  223. if (fread (data, 1, raw_width*5/4, ifp) < raw_width*5/4) derror();
  224. for (dp=data, pix=pixel; dp < data+1120; dp+=10, pix+=8) {
  225. pix[0] = (dp[0] << 2) + (dp[1] >> 6 );
  226. pix[1] = (dp[2] << 2) + (dp[1] >> 4 & 3);
  227. pix[2] = (dp[3] << 2) + (dp[1] >> 2 & 3);
  228. pix[3] = (dp[4] << 2) + (dp[1] & 3);
  229. pix[4] = (dp[5] << 2) + (dp[9] & 3);
  230. pix[5] = (dp[6] << 2) + (dp[9] >> 2 & 3);
  231. pix[6] = (dp[7] << 2) + (dp[9] >> 4 & 3);
  232. pix[7] = (dp[8] << 2) + (dp[9] >> 6 );
  233. }
  234. for (col=0; col < width; col++)
  235. BAYER(row,col) = pixel[col];
  236. for (col=width; col < raw_width; col++)
  237. {
  238. black += pixel[col];
  239. }
  240. if ((row+=2) > height) row = 1;
  241. }
  242. if (raw_width > width)
  243. black = black / ((raw_width - width) * height) - 4;
  244. for (row=0; row < height; row++)
  245. for (col=0; col < width; col++) {
  246. if ((val = BAYER(row,col) - black) < 0) val = 0;
  247. val = val * mul[row & 3][col & 1] >> 9;
  248. BAYER(row,col) = val;
  249. }
  250. canon_600_fixed_wb(1311);
  251. canon_600_auto_wb();
  252. canon_600_coeff();
  253. maximum = (0x3ff - black) * 1109 >> 9;
  254. black = 0;
  255. }
  256. void CLASS remove_zeroes()
  257. {
  258. unsigned row, col, tot, n, r, c;
  259. #ifdef LIBRAW_LIBRARY_BUILD
  260. RUN_CALLBACK(LIBRAW_PROGRESS_REMOVE_ZEROES,0,2);
  261. #endif
  262. for (row=0; row < height; row++)
  263. for (col=0; col < width; col++)
  264. if (BAYER(row,col) == 0) {
  265. tot = n = 0;
  266. for (r = row-2; r <= row+2; r++)
  267. for (c = col-2; c <= col+2; c++)
  268. if (r < height && c < width &&
  269. FC(r,c) == FC(row,col) && BAYER(r,c))
  270. tot += (n++,BAYER(r,c));
  271. if (n) BAYER(row,col) = tot/n;
  272. }
  273. #ifdef LIBRAW_LIBRARY_BUILD
  274. RUN_CALLBACK(LIBRAW_PROGRESS_REMOVE_ZEROES,1,2);
  275. #endif
  276. }
  277. int CLASS canon_s2is()
  278. {
  279. unsigned row;
  280. for (row=0; row < 100; row++) {
  281. fseek (ifp, row*3340 + 3284, SEEK_SET);
  282. if (getc(ifp) > 15) return 1;
  283. }
  284. return 0;
  285. }
  286. void CLASS canon_a5_load_raw()
  287. {
  288. ushort data[2565], *dp, pixel;
  289. int vbits=0, buf=0, row, col, bc=0;
  290. order = 0x4949;
  291. for (row=-top_margin; row < raw_height-top_margin; row++) {
  292. read_shorts (dp=data, raw_width * 10 / 16);
  293. for (col=-left_margin; col < raw_width-left_margin; col++) {
  294. if ((vbits -= 10) < 0)
  295. buf = (vbits += 16, (buf << 16) + *dp++);
  296. pixel = buf >> vbits & 0x3ff;
  297. if ((unsigned) row < height && (unsigned) col < width)
  298. BAYER(row,col) = pixel;
  299. else if (col > 1-left_margin && col != width)
  300. black += (bc++,pixel);
  301. }
  302. }
  303. if (bc) black /= bc;
  304. maximum = 0x3ff;
  305. if (raw_width > 1600) remove_zeroes();
  306. }
  307. /*
  308. getbits(-1) initializes the buffer
  309. getbits(n) where 0 <= n <= 25 returns an n-bit integer
  310. */
  311. unsigned CLASS getbits (int nbits)
  312. {
  313. #ifdef LIBRAW_NOTHREADS
  314. static unsigned bitbuf=0;
  315. static int vbits=0, reset=0;
  316. #else
  317. #define bitbuf tls->getbits.bitbuf
  318. #define vbits tls->getbits.vbits
  319. #define reset tls->getbits.reset
  320. #endif
  321. unsigned c;
  322. if (nbits == -1)
  323. return bitbuf = vbits = reset = 0;
  324. if (nbits == 0 || reset) return 0;
  325. while (vbits < nbits) {
  326. if ((c = fgetc(ifp)) == EOF) derror();
  327. if ((reset = zero_after_ff && c == 0xff && fgetc(ifp))) return 0;
  328. bitbuf = (bitbuf << 8) + (uchar) c;
  329. vbits += 8;
  330. }
  331. vbits -= nbits;
  332. return bitbuf << (32-nbits-vbits) >> (32-nbits);
  333. #ifndef LIBRAW_NOTHREADS
  334. #undef bitbuf
  335. #undef vbits
  336. #undef reset
  337. #endif
  338. }
  339. void CLASS init_decoder()
  340. {
  341. memset (first_decode, 0, sizeof first_decode);
  342. free_decode = first_decode;
  343. }
  344. /*
  345. Construct a decode tree according to the specification in *source.
  346. The first 16 bytes specify how many codes should be 1-bit, 2-bit
  347. 3-bit, etc. Bytes after that are the leaf values.
  348. For example, if the source is
  349. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
  350. 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
  351. then the code is
  352. 00 0x04
  353. 010 0x03
  354. 011 0x05
  355. 100 0x06
  356. 101 0x02
  357. 1100 0x07
  358. 1101 0x01
  359. 11100 0x08
  360. 11101 0x09
  361. 11110 0x00
  362. 111110 0x0a
  363. 1111110 0x0b
  364. 1111111 0xff
  365. */
  366. uchar * CLASS make_decoder (const uchar *source, int level)
  367. {
  368. struct decode *cur;
  369. #ifndef LIBRAW_NOTHREADS
  370. #define t_leaf tls->make_decoder_leaf
  371. #else
  372. static int t_leaf;
  373. #endif
  374. int i, next;
  375. if (level==0) t_leaf=0;
  376. cur = free_decode++;
  377. if (free_decode > first_decode+2048) {
  378. #ifdef LIBRAW_LIBRARY_BUILD
  379. throw LIBRAW_EXCEPTION_DECODE_RAW;
  380. #else
  381. fprintf (stderr,_("%s: decoder table overflow\n"), ifname);
  382. longjmp (failure, 2);
  383. #endif
  384. }
  385. for (i=next=0; i <= t_leaf && next < 16; )
  386. i += source[next++];
  387. if (i > t_leaf) {
  388. if (level < next) {
  389. cur->branch[0] = free_decode;
  390. make_decoder (source, level+1);
  391. cur->branch[1] = free_decode;
  392. make_decoder (source, level+1);
  393. } else
  394. cur->leaf = source[16 + t_leaf++];
  395. }
  396. return (uchar *) source + 16 + t_leaf;
  397. #ifndef LIBRAW_NOTHREADS
  398. #undef t_leaf
  399. #endif
  400. }
  401. void CLASS crw_init_tables (unsigned table)
  402. {
  403. static const uchar first_tree[3][29] = {
  404. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
  405. 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
  406. { 0,2,2,3,1,1,1,1,2,0,0,0,0,0,0,0,
  407. 0x03,0x02,0x04,0x01,0x05,0x00,0x06,0x07,0x09,0x08,0x0a,0x0b,0xff },
  408. { 0,0,6,3,1,1,2,0,0,0,0,0,0,0,0,0,
  409. 0x06,0x05,0x07,0x04,0x08,0x03,0x09,0x02,0x00,0x0a,0x01,0x0b,0xff },
  410. };
  411. static const uchar second_tree[3][180] = {
  412. { 0,2,2,2,1,4,2,1,2,5,1,1,0,0,0,139,
  413. 0x03,0x04,0x02,0x05,0x01,0x06,0x07,0x08,
  414. 0x12,0x13,0x11,0x14,0x09,0x15,0x22,0x00,0x21,0x16,0x0a,0xf0,
  415. 0x23,0x17,0x24,0x31,0x32,0x18,0x19,0x33,0x25,0x41,0x34,0x42,
  416. 0x35,0x51,0x36,0x37,0x38,0x29,0x79,0x26,0x1a,0x39,0x56,0x57,
  417. 0x28,0x27,0x52,0x55,0x58,0x43,0x76,0x59,0x77,0x54,0x61,0xf9,
  418. 0x71,0x78,0x75,0x96,0x97,0x49,0xb7,0x53,0xd7,0x74,0xb6,0x98,
  419. 0x47,0x48,0x95,0x69,0x99,0x91,0xfa,0xb8,0x68,0xb5,0xb9,0xd6,
  420. 0xf7,0xd8,0x67,0x46,0x45,0x94,0x89,0xf8,0x81,0xd5,0xf6,0xb4,
  421. 0x88,0xb1,0x2a,0x44,0x72,0xd9,0x87,0x66,0xd4,0xf5,0x3a,0xa7,
  422. 0x73,0xa9,0xa8,0x86,0x62,0xc7,0x65,0xc8,0xc9,0xa1,0xf4,0xd1,
  423. 0xe9,0x5a,0x92,0x85,0xa6,0xe7,0x93,0xe8,0xc1,0xc6,0x7a,0x64,
  424. 0xe1,0x4a,0x6a,0xe6,0xb3,0xf1,0xd3,0xa5,0x8a,0xb2,0x9a,0xba,
  425. 0x84,0xa4,0x63,0xe5,0xc5,0xf3,0xd2,0xc4,0x82,0xaa,0xda,0xe4,
  426. 0xf2,0xca,0x83,0xa3,0xa2,0xc3,0xea,0xc2,0xe2,0xe3,0xff,0xff },
  427. { 0,2,2,1,4,1,4,1,3,3,1,0,0,0,0,140,
  428. 0x02,0x03,0x01,0x04,0x05,0x12,0x11,0x06,
  429. 0x13,0x07,0x08,0x14,0x22,0x09,0x21,0x00,0x23,0x15,0x31,0x32,
  430. 0x0a,0x16,0xf0,0x24,0x33,0x41,0x42,0x19,0x17,0x25,0x18,0x51,
  431. 0x34,0x43,0x52,0x29,0x35,0x61,0x39,0x71,0x62,0x36,0x53,0x26,
  432. 0x38,0x1a,0x37,0x81,0x27,0x91,0x79,0x55,0x45,0x28,0x72,0x59,
  433. 0xa1,0xb1,0x44,0x69,0x54,0x58,0xd1,0xfa,0x57,0xe1,0xf1,0xb9,
  434. 0x49,0x47,0x63,0x6a,0xf9,0x56,0x46,0xa8,0x2a,0x4a,0x78,0x99,
  435. 0x3a,0x75,0x74,0x86,0x65,0xc1,0x76,0xb6,0x96,0xd6,0x89,0x85,
  436. 0xc9,0xf5,0x95,0xb4,0xc7,0xf7,0x8a,0x97,0xb8,0x73,0xb7,0xd8,
  437. 0xd9,0x87,0xa7,0x7a,0x48,0x82,0x84,0xea,0xf4,0xa6,0xc5,0x5a,
  438. 0x94,0xa4,0xc6,0x92,0xc3,0x68,0xb5,0xc8,0xe4,0xe5,0xe6,0xe9,
  439. 0xa2,0xa3,0xe3,0xc2,0x66,0x67,0x93,0xaa,0xd4,0xd5,0xe7,0xf8,
  440. 0x88,0x9a,0xd7,0x77,0xc4,0x64,0xe2,0x98,0xa5,0xca,0xda,0xe8,
  441. 0xf3,0xf6,0xa9,0xb2,0xb3,0xf2,0xd2,0x83,0xba,0xd3,0xff,0xff },
  442. { 0,0,6,2,1,3,3,2,5,1,2,2,8,10,0,117,
  443. 0x04,0x05,0x03,0x06,0x02,0x07,0x01,0x08,
  444. 0x09,0x12,0x13,0x14,0x11,0x15,0x0a,0x16,0x17,0xf0,0x00,0x22,
  445. 0x21,0x18,0x23,0x19,0x24,0x32,0x31,0x25,0x33,0x38,0x37,0x34,
  446. 0x35,0x36,0x39,0x79,0x57,0x58,0x59,0x28,0x56,0x78,0x27,0x41,
  447. 0x29,0x77,0x26,0x42,0x76,0x99,0x1a,0x55,0x98,0x97,0xf9,0x48,
  448. 0x54,0x96,0x89,0x47,0xb7,0x49,0xfa,0x75,0x68,0xb6,0x67,0x69,
  449. 0xb9,0xb8,0xd8,0x52,0xd7,0x88,0xb5,0x74,0x51,0x46,0xd9,0xf8,
  450. 0x3a,0xd6,0x87,0x45,0x7a,0x95,0xd5,0xf6,0x86,0xb4,0xa9,0x94,
  451. 0x53,0x2a,0xa8,0x43,0xf5,0xf7,0xd4,0x66,0xa7,0x5a,0x44,0x8a,
  452. 0xc9,0xe8,0xc8,0xe7,0x9a,0x6a,0x73,0x4a,0x61,0xc7,0xf4,0xc6,
  453. 0x65,0xe9,0x72,0xe6,0x71,0x91,0x93,0xa6,0xda,0x92,0x85,0x62,
  454. 0xf3,0xc5,0xb2,0xa4,0x84,0xba,0x64,0xa5,0xb3,0xd2,0x81,0xe5,
  455. 0xd3,0xaa,0xc4,0xca,0xf2,0xb1,0xe4,0xd1,0x83,0x63,0xea,0xc3,
  456. 0xe2,0x82,0xf1,0xa3,0xc2,0xa1,0xc1,0xe3,0xa2,0xe1,0xff,0xff }
  457. };
  458. if (table > 2) table = 2;
  459. init_decoder();
  460. make_decoder ( first_tree[table], 0);
  461. second_decode = free_decode;
  462. make_decoder (second_tree[table], 0);
  463. }
  464. /*
  465. Return 0 if the image starts with compressed data,
  466. 1 if it starts with uncompressed low-order bits.
  467. In Canon compressed data, 0xff is always followed by 0x00.
  468. */
  469. int CLASS canon_has_lowbits()
  470. {
  471. uchar test[0x4000];
  472. int ret=1, i;
  473. fseek (ifp, 0, SEEK_SET);
  474. fread (test, 1, sizeof test, ifp);
  475. for (i=540; i < sizeof test - 1; i++)
  476. if (test[i] == 0xff) {
  477. if (test[i+1]) return 1;
  478. ret=0;
  479. }
  480. return ret;
  481. }
  482. void CLASS canon_compressed_load_raw()
  483. {
  484. ushort *pixel, *prow;
  485. int nblocks, lowbits, i, row, r, col, save, val;
  486. unsigned irow, icol;
  487. struct decode *decode, *dindex;
  488. int block, diffbuf[64], leaf, len, diff, carry=0, pnum=0, base[2];
  489. double dark[2] = { 0,0 };
  490. uchar c;
  491. crw_init_tables (tiff_compress);
  492. pixel = (ushort *) calloc (raw_width*8, sizeof *pixel);
  493. merror (pixel, "canon_compressed_load_raw()");
  494. lowbits = canon_has_lowbits();
  495. if (!lowbits) maximum = 0x3ff;
  496. fseek (ifp, 540 + lowbits*raw_height*raw_width/4, SEEK_SET);
  497. zero_after_ff = 1;
  498. getbits(-1);
  499. for (row=0; row < raw_height; row+=8) {
  500. nblocks = MIN (8, raw_height-row) * raw_width >> 6;
  501. for (block=0; block < nblocks; block++) {
  502. memset (diffbuf, 0, sizeof diffbuf);
  503. decode = first_decode;
  504. for (i=0; i < 64; i++ ) {
  505. for (dindex=decode; dindex->branch[0]; )
  506. dindex = dindex->branch[getbits(1)];
  507. leaf = dindex->leaf;
  508. decode = second_decode;
  509. if (leaf == 0 && i) break;
  510. if (leaf == 0xff) continue;
  511. i += leaf >> 4;
  512. len = leaf & 15;
  513. if (len == 0) continue;
  514. diff = getbits(len);
  515. if ((diff & (1 << (len-1))) == 0)
  516. diff -= (1 << len) - 1;
  517. if (i < 64) diffbuf[i] = diff;
  518. }
  519. diffbuf[0] += carry;
  520. carry = diffbuf[0];
  521. for (i=0; i < 64; i++ ) {
  522. if (pnum++ % raw_width == 0)
  523. base[0] = base[1] = 512;
  524. if ((pixel[(block << 6) + i] = base[i & 1] += diffbuf[i]) >> 10)
  525. derror();
  526. }
  527. }
  528. if (lowbits) {
  529. save = ftell(ifp);
  530. fseek (ifp, 26 + row*raw_width/4, SEEK_SET);
  531. for (prow=pixel, i=0; i < raw_width*2; i++) {
  532. c = fgetc(ifp);
  533. for (r=0; r < 8; r+=2, prow++) {
  534. val = (*prow << 2) + ((c >> r) & 3);
  535. if (raw_width == 2672 && val < 512) val += 2;
  536. *prow = val;
  537. }
  538. }
  539. fseek (ifp, save, SEEK_SET);
  540. }
  541. for (r=0; r < 8; r++) {
  542. irow = row - top_margin + r;
  543. if (irow >= height) continue;
  544. for (col=0; col < raw_width; col++) {
  545. icol = col - left_margin;
  546. if (icol < width)
  547. BAYER(irow,icol) = pixel[r*raw_width+col];
  548. else if (col > 1)
  549. dark[icol & 1] += pixel[r*raw_width+col];
  550. }
  551. }
  552. }
  553. free (pixel);
  554. canon_black (dark);
  555. }
  556. #line 885 "dcraw/dcraw.c"
  557. int CLASS ljpeg_start (struct jhead *jh, int info_only)
  558. {
  559. int c, tag, len;
  560. uchar data[0x10000], *dp;
  561. if (!info_only) init_decoder();
  562. memset (jh, 0, sizeof *jh);
  563. FORC(6) jh->huff[c] = free_decode;
  564. jh->restart = INT_MAX;
  565. fread (data, 2, 1, ifp);
  566. if (data[1] != 0xd8) return 0;
  567. do {
  568. fread (data, 2, 2, ifp);
  569. tag = data[0] << 8 | data[1];
  570. len = (data[2] << 8 | data[3]) - 2;
  571. if (tag <= 0xff00) return 0;
  572. fread (data, 1, len, ifp);
  573. switch (tag) {
  574. case 0xffc3:
  575. jh->sraw = ((data[7] >> 4) * (data[7] & 15) - 1) & 3;
  576. case 0xffc0:
  577. jh->bits = data[0];
  578. jh->high = data[1] << 8 | data[2];
  579. jh->wide = data[3] << 8 | data[4];
  580. jh->clrs = data[5] + jh->sraw;
  581. if (len == 9 && !dng_version) getc(ifp);
  582. break;
  583. case 0xffc4:
  584. if (info_only) break;
  585. for (dp = data; dp < data+len && *dp < 4; ) {
  586. jh->huff[*dp] = free_decode;
  587. dp = make_decoder (++dp, 0);
  588. }
  589. break;
  590. case 0xffda:
  591. jh->psv = data[1+data[0]*2];
  592. jh->bits -= data[3+data[0]*2] & 15;
  593. break;
  594. case 0xffdd:
  595. jh->restart = data[0] << 8 | data[1];
  596. }
  597. } while (tag != 0xffda);
  598. if (info_only) return 1;
  599. if (jh->sraw) {
  600. FORC(4) jh->huff[2+c] = jh->huff[1];
  601. FORC(jh->sraw) jh->huff[1+c] = jh->huff[0];
  602. }
  603. jh->row = (ushort *) calloc (jh->wide*jh->clrs, 4);
  604. merror (jh->row, "ljpeg_start()");
  605. return zero_after_ff = 1;
  606. }
  607. int CLASS ljpeg_diff (struct decode *dindex)
  608. {
  609. int len, diff;
  610. while (dindex->branch[0])
  611. dindex = dindex->branch[getbits(1)];
  612. len = dindex->leaf;
  613. if (len == 16 && (!dng_version || dng_version >= 0x1010000))
  614. return -32768;
  615. diff = getbits(len);
  616. if ((diff & (1 << (len-1))) == 0)
  617. diff -= (1 << len) - 1;
  618. return diff;
  619. }
  620. ushort * CLASS ljpeg_row (int jrow, struct jhead *jh)
  621. {
  622. int col, c, diff, pred, spred=0;
  623. ushort mark=0, *row[3];
  624. if (jrow * jh->wide % jh->restart == 0) {
  625. FORC(6) jh->vpred[c] = 1 << (jh->bits-1);
  626. if (jrow)
  627. do mark = (mark << 8) + (c = fgetc(ifp));
  628. while (c != EOF && mark >> 4 != 0xffd);
  629. getbits(-1);
  630. }
  631. FORC3 row[c] = jh->row + jh->wide*jh->clrs*((jrow+c) & 1);
  632. for (col=0; col < jh->wide; col++)
  633. FORC(jh->clrs) {
  634. diff = ljpeg_diff (jh->huff[c]);
  635. if (jh->sraw && c <= jh->sraw && (col | c))
  636. pred = spred;
  637. else if (col) pred = row[0][-jh->clrs];
  638. else pred = (jh->vpred[c] += diff) - diff;
  639. if (jrow && col) switch (jh->psv) {
  640. case 1: break;
  641. case 2: pred = row[1][0]; break;
  642. case 3: pred = row[1][-jh->clrs]; break;
  643. case 4: pred = pred + row[1][0] - row[1][-jh->clrs]; break;
  644. case 5: pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1); break;
  645. case 6: pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1); break;
  646. case 7: pred = (pred + row[1][0]) >> 1; break;
  647. default: pred = 0;
  648. }
  649. if ((**row = pred + diff) >> jh->bits) derror();
  650. if (c <= jh->sraw) spred = **row;
  651. row[0]++; row[1]++;
  652. }
  653. return row[2];
  654. }
  655. void CLASS lossless_jpeg_load_raw()
  656. {
  657. int jwide, jrow, jcol, val, jidx, i, j, row=0, col=0;
  658. double dark[2] = { 0,0 };
  659. struct jhead jh;
  660. int min=INT_MAX;
  661. ushort *rp;
  662. if (!ljpeg_start (&jh, 0)) return;
  663. jwide = jh.wide * jh.clrs;
  664. for (jrow=0; jrow < jh.high; jrow++) {
  665. rp = ljpeg_row (jrow, &jh);
  666. for (jcol=0; jcol < jwide; jcol++) {
  667. val = *rp++;
  668. if (jh.bits <= 12)
  669. val = curve[val & 0xfff];
  670. if (cr2_slice[0]) {
  671. jidx = jrow*jwide + jcol;
  672. i = jidx / (cr2_slice[1]*jh.high);
  673. if ((j = i >= cr2_slice[0]))
  674. i = cr2_slice[0];
  675. jidx -= i * (cr2_slice[1]*jh.high);
  676. row = jidx / cr2_slice[1+j];
  677. col = jidx % cr2_slice[1+j] + i*cr2_slice[1];
  678. }
  679. if (raw_width == 3984 && (col -= 2) < 0)
  680. col += (row--,raw_width);
  681. if ((unsigned) (row-top_margin) < height) {
  682. if ((unsigned) (col-left_margin) < width) {
  683. BAYER(row-top_margin,col-left_margin) = val;
  684. if (min > val) min = val;
  685. } else if (col > 1)
  686. dark[(col-left_margin) & 1] += val;
  687. }
  688. if (++col >= raw_width)
  689. col = (row++,0);
  690. }
  691. }
  692. free (jh.row);
  693. canon_black (dark);
  694. if (!strcasecmp(make,"KODAK"))
  695. black = min;
  696. }
  697. void CLASS canon_sraw_load_raw()
  698. {
  699. struct jhead jh;
  700. short *rp=0, (*ip)[4];
  701. int jwide, slice, scol, ecol, row, col, jrow=0, jcol=0, pix[3], c;
  702. int v[3]={0,0,0}, ver, hue;
  703. char *cp;
  704. if (!ljpeg_start (&jh, 0)) return;
  705. jwide = (jh.wide >>= 1) * jh.clrs;
  706. for (ecol=slice=0; slice <= cr2_slice[0]; slice++) {
  707. scol = ecol;
  708. ecol += cr2_slice[1] * 2 / jh.clrs;
  709. if (!cr2_slice[0] || ecol > raw_width-1) ecol = raw_width & -2;
  710. for (row=0; row < height; row += (jh.clrs >> 1) - 1) {
  711. ip = (short (*)[4]) image + row*width;
  712. for (col=scol; col < ecol; col+=2, jcol+=jh.clrs) {
  713. if ((jcol %= jwide) == 0)
  714. rp = (short *) ljpeg_row (jrow++, &jh);
  715. if (col >= width) continue;
  716. FORC (jh.clrs-2)
  717. ip[col + (c >> 1)*width + (c & 1)][0] = rp[jcol+c];
  718. ip[col][1] = rp[jcol+jh.clrs-2] - 16384;
  719. ip[col][2] = rp[jcol+jh.clrs-1] - 16384;
  720. }
  721. }
  722. }
  723. for (cp=model2; *cp && !isdigit(*cp); cp++);
  724. sscanf (cp, "%d.%d.%d", v, v+1, v+2);
  725. ver = (v[0]*1000 + v[1])*1000 + v[2];
  726. hue = (jh.sraw+1) << 2;
  727. if (unique_id == 0x80000218 && ver > 1000006 && ver < 3000000)
  728. hue = jh.sraw << 1;
  729. ip = (short (*)[4]) image;
  730. rp = ip[0];
  731. for (row=0; row < height; row++, ip+=width) {
  732. if (row & (jh.sraw >> 1))
  733. for (col=0; col < width; col+=2)
  734. for (c=1; c < 3; c++)
  735. if (row == height-1)
  736. ip[col][c] = ip[col-width][c];
  737. else ip[col][c] = (ip[col-width][c] + ip[col+width][c] + 1) >> 1;
  738. for (col=1; col < width; col+=2)
  739. for (c=1; c < 3; c++)
  740. if (col == width-1)
  741. ip[col][c] = ip[col-1][c];
  742. else ip[col][c] = (ip[col-1][c] + ip[col+1][c] + 1) >> 1;
  743. }
  744. for ( ; rp < ip[0]; rp+=4) {
  745. if (unique_id < 0x80000200) {
  746. pix[0] = rp[0] + rp[2] - 512;
  747. pix[2] = rp[0] + rp[1] - 512;
  748. pix[1] = rp[0] + ((-778*rp[1] - (rp[2] << 11)) >> 12) - 512;
  749. } else {
  750. rp[1] = (rp[1] << 2) + hue;
  751. rp[2] = (rp[2] << 2) + hue;
  752. pix[0] = rp[0] + (( 200*rp[1] + 22929*rp[2]) >> 14);
  753. pix[1] = rp[0] + ((-5640*rp[1] - 11751*rp[2]) >> 14);
  754. pix[2] = rp[0] + ((29040*rp[1] - 101*rp[2]) >> 14);
  755. }
  756. FORC3 rp[c] = CLIP(pix[c] * sraw_mul[c] >> 10);
  757. }
  758. free (jh.row);
  759. maximum = 0x3fff;
  760. }
  761. void CLASS adobe_copy_pixel (int row, int col, ushort **rp)
  762. {
  763. unsigned r, c;
  764. r = row -= top_margin;
  765. c = col -= left_margin;
  766. if (is_raw == 2 && shot_select) (*rp)++;
  767. if (filters) {
  768. if (fuji_width) {
  769. r = row + fuji_width - 1 - (col >> 1);
  770. c = row + ((col+1) >> 1);
  771. }
  772. if (r < height && c < width)
  773. BAYER(r,c) = **rp < 0x1000 ? curve[**rp] : **rp;
  774. *rp += is_raw;
  775. } else {
  776. if (r < height && c < width)
  777. FORC(tiff_samples)
  778. image[row*width+col][c] = (*rp)[c] < 0x1000 ? curve[(*rp)[c]]:(*rp)[c];
  779. *rp += tiff_samples;
  780. }
  781. if (is_raw == 2 && shot_select) (*rp)--;
  782. }
  783. void CLASS adobe_dng_load_raw_lj()
  784. {
  785. unsigned save, trow=0, tcol=0, jwide, jrow, jcol, row, col;
  786. struct jhead jh;
  787. ushort *rp;
  788. while (trow < raw_height) {
  789. save = ftell(ifp);
  790. if (tile_length < INT_MAX)
  791. fseek (ifp, get4(), SEEK_SET);
  792. if (!ljpeg_start (&jh, 0)) break;
  793. jwide = jh.wide;
  794. if (filters) jwide *= jh.clrs;
  795. jwide /= is_raw;
  796. for (row=col=jrow=0; jrow < jh.high; jrow++) {
  797. rp = ljpeg_row (jrow, &jh);
  798. for (jcol=0; jcol < jwide; jcol++) {
  799. adobe_copy_pixel (trow+row, tcol+col, &rp);
  800. if (++col >= tile_width || col >= raw_width)
  801. row += 1 + (col = 0);
  802. }
  803. }
  804. fseek (ifp, save+4, SEEK_SET);
  805. if ((tcol += tile_width) >= raw_width)
  806. trow += tile_length + (tcol = 0);
  807. free (jh.row);
  808. }
  809. }
  810. void CLASS adobe_dng_load_raw_nc()
  811. {
  812. ushort *pixel, *rp;
  813. int row, col;
  814. pixel = (ushort *) calloc (raw_width * tiff_samples, sizeof *pixel);
  815. merror (pixel, "adobe_dng_load_raw_nc()");
  816. for (row=0; row < raw_height; row++) {
  817. if (tiff_bps == 16)
  818. read_shorts (pixel, raw_width * tiff_samples);
  819. else {
  820. getbits(-1);
  821. for (col=0; col < raw_width * tiff_samples; col++)
  822. pixel[col] = getbits(tiff_bps);
  823. }
  824. for (rp=pixel, col=0; col < raw_width; col++)
  825. adobe_copy_pixel (row, col, &rp);
  826. }
  827. free (pixel);
  828. }
  829. void CLASS pentax_tree()
  830. {
  831. ushort bit[2][13];
  832. struct decode *cur;
  833. int c, i, j;
  834. init_decoder();
  835. FORC(13) bit[0][c] = get2();
  836. FORC(13) bit[1][c] = fgetc(ifp) & 15;
  837. FORC(13) {
  838. cur = first_decode;
  839. for (i=0; i < bit[1][c]; i++) {
  840. j = bit[0][c] >> (11-i) & 1;
  841. if (!cur->branch[j]) cur->branch[j] = ++free_decode;
  842. cur = cur->branch[j];
  843. }
  844. cur->leaf = c;
  845. }
  846. }
  847. void CLASS pentax_k10_load_raw()
  848. {
  849. int row, col, diff;
  850. ushort vpred[2][2] = {{0,0},{0,0}}, hpred[2];
  851. getbits(-1);
  852. for (row=0; row < raw_height; row++)
  853. {
  854. if(row >= height) break;
  855. for (col=0; col < raw_width; col++) {
  856. diff = ljpeg_diff (first_decode);
  857. if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
  858. else hpred[col & 1] += diff;
  859. if (col < width && row < height)
  860. BAYER(row,col) = hpred[col & 1];
  861. if (hpred[col & 1] >> 12) derror();
  862. }
  863. }
  864. }
  865. void CLASS nikon_compressed_load_raw()
  866. {
  867. static const uchar nikon_tree[][32] = {
  868. { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy */
  869. 5,4,3,6,2,7,1,0,8,9,11,10,12 },
  870. { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy after split */
  871. 0x39,0x5a,0x38,0x27,0x16,5,4,3,2,1,0,11,12,12 },
  872. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0, /* 12-bit lossless */
  873. 5,4,6,3,7,2,8,1,9,0,10,11,12 },
  874. { 0,1,4,3,1,1,1,1,1,2,0,0,0,0,0,0, /* 14-bit lossy */
  875. 5,6,4,7,8,3,9,2,1,0,10,11,12,13,14 },
  876. { 0,1,5,1,1,1,1,1,1,1,2,0,0,0,0,0, /* 14-bit lossy after split */
  877. 8,0x5c,0x4b,0x3a,0x29,7,6,5,4,3,2,1,0,13,14 },
  878. { 0,1,4,2,2,3,1,2,0,0,0,0,0,0,0,0, /* 14-bit lossless */
  879. 7,6,8,5,9,4,10,3,11,12,2,0,1,13,14 } };
  880. struct decode *dindex;
  881. ushort ver0, ver1, vpred[2][2], hpred[2], csize;
  882. int i, min, max, step=0, huff=0, split=0, row, col, len, shl, diff;
  883. fseek (ifp, meta_offset, SEEK_SET);
  884. ver0 = fgetc(ifp);
  885. ver1 = fgetc(ifp);
  886. if (ver0 == 0x49 || ver1 == 0x58)
  887. fseek (ifp, 2110, SEEK_CUR);
  888. if (ver0 == 0x46) huff = 2;
  889. if (tiff_bps == 14) huff += 3;
  890. read_shorts (vpred[0], 4);
  891. max = 1 << tiff_bps & 0x7fff;
  892. if ((csize = get2()) > 1)
  893. step = max / (csize-1);
  894. if (ver0 == 0x44 && ver1 == 0x20 && step > 0) {
  895. for (i=0; i < csize; i++)
  896. curve[i*step] = get2();
  897. for (i=0; i < max; i++)
  898. curve[i] = ( curve[i-i%step]*(step-i%step) +
  899. curve[i-i%step+step]*(i%step) ) / step;
  900. fseek (ifp, meta_offset+562, SEEK_SET);
  901. split = get2();
  902. } else if (ver0 != 0x46 && csize <= 0x4001)
  903. {
  904. read_shorts (curve, max=csize);
  905. }
  906. while (curve[max-2] == curve[max-1]) max--;
  907. init_decoder();
  908. make_decoder (nikon_tree[huff], 0);
  909. fseek (ifp, data_offset, SEEK_SET);
  910. getbits(-1);
  911. for (min=row=0; row < height; row++) {
  912. if (split && row == split) {
  913. init_decoder();
  914. make_decoder (nikon_tree[huff+1], 0);
  915. max += (min = 16) << 1;
  916. }
  917. for (col=0; col < raw_width; col++) {
  918. for (dindex=first_decode; dindex->branch[0]; )
  919. dindex = dindex->branch[getbits(1)];
  920. len = dindex->leaf & 15;
  921. shl = dindex->leaf >> 4;
  922. diff = ((getbits(len-shl) << 1) + 1) << shl >> 1;
  923. if ((diff & (1 << (len-1))) == 0)
  924. diff -= (1 << len) - !shl;
  925. if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
  926. else hpred[col & 1] += diff;
  927. if ((ushort)(hpred[col & 1] + min) >= max) derror();
  928. if ((unsigned) (col-left_margin) < width)
  929. BAYER(row,col-left_margin) = curve[LIM((short)hpred[col & 1],0,0x3fff)];
  930. }
  931. }
  932. }
  933. /*
  934. Figure out if a NEF file is compressed. These fancy heuristics
  935. are only needed for the D100, thanks to a bug in some cameras
  936. that tags all images as "compressed".
  937. */
  938. int CLASS nikon_is_compressed()
  939. {
  940. uchar test[256];
  941. int i;
  942. fseek (ifp, data_offset, SEEK_SET);
  943. fread (test, 1, 256, ifp);
  944. for (i=15; i < 256; i+=16)
  945. if (test[i]) return 1;
  946. return 0;
  947. }
  948. /*
  949. Returns 1 for a Coolpix 995, 0 for anything else.
  950. */
  951. int CLASS nikon_e995()
  952. {
  953. int i, histo[256];
  954. const uchar often[] = { 0x00, 0x55, 0xaa, 0xff };
  955. memset (histo, 0, sizeof histo);
  956. fseek (ifp, -2000, SEEK_END);
  957. for (i=0; i < 2000; i++)
  958. histo[fgetc(ifp)]++;
  959. for (i=0; i < 4; i++)
  960. if (histo[often[i]] < 200)
  961. return 0;
  962. return 1;
  963. }
  964. /*
  965. Returns 1 for a Coolpix 2100, 0 for anything else.
  966. */
  967. int CLASS nikon_e2100()
  968. {
  969. uchar t[12];
  970. int i;
  971. fseek (ifp, 0, SEEK_SET);
  972. for (i=0; i < 1024; i++) {
  973. fread (t, 1, 12, ifp);
  974. if (((t[2] & t[4] & t[7] & t[9]) >> 4
  975. & t[1] & t[6] & t[8] & t[11] & 3) != 3)
  976. return 0;
  977. }
  978. return 1;
  979. }
  980. void CLASS nikon_3700()
  981. {
  982. int bits, i;
  983. uchar dp[24];
  984. static const struct {
  985. int bits;
  986. char t_make[12], t_model[15];
  987. } table[] = {
  988. { 0x00, "PENTAX", "Optio 33WR" },
  989. { 0x03, "NIKON", "E3200" },
  990. { 0x32, "NIKON", "E3700" },
  991. { 0x33, "OLYMPUS", "C740UZ" } };
  992. fseek (ifp, 3072, SEEK_SET);
  993. fread (dp, 1, 24, ifp);
  994. bits = (dp[8] & 3) << 4 | (dp[20] & 3);
  995. for (i=0; i < sizeof table / sizeof *table; i++)
  996. if (bits == table[i].bits) {
  997. strcpy (make, table[i].t_make );
  998. strcpy (model, table[i].t_model);
  999. }
  1000. }
  1001. /*
  1002. Separates a Minolta DiMAGE Z2 from a Nikon E4300.
  1003. */
  1004. int CLASS minolta_z2()
  1005. {
  1006. int i, nz;
  1007. char tail[424];
  1008. fseek (ifp, -sizeof tail, SEEK_END);
  1009. fread (tail, 1, sizeof tail, ifp);
  1010. for (nz=i=0; i < sizeof tail; i++)
  1011. if (tail[i]) nz++;
  1012. return nz > 20;
  1013. }
  1014. /* Here raw_width is in bytes, not pixels. */
  1015. void CLASS nikon_e900_load_raw()
  1016. {
  1017. int offset=0, irow, row, col;
  1018. for (irow=0; irow < height; irow++) {
  1019. row = irow * 2 % height;
  1020. if (row == 1)
  1021. offset = - (-offset & -4096);
  1022. fseek (ifp, offset, SEEK_SET);
  1023. offset += raw_width;
  1024. getbits(-1);
  1025. for (col=0; col < width; col++)
  1026. BAYER(row,col) = getbits(10);
  1027. }
  1028. }
  1029. /*
  1030. The Fuji Super CCD is just a Bayer grid rotated 45 degrees.
  1031. */
  1032. void CLASS fuji_load_raw()
  1033. {
  1034. ushort *pixel;
  1035. int wide, row, col, r, c;
  1036. fseek (ifp, (top_margin*raw_width + left_margin) * 2, SEEK_CUR);
  1037. wide = fuji_width << !fuji_layout;
  1038. pixel = (ushort *) calloc (wide, sizeof *pixel);
  1039. merror (pixel, "fuji_load_raw()");
  1040. for (row=0; row < raw_height; row++) {
  1041. read_shorts (pixel, wide);
  1042. fseek (ifp, 2*(raw_width - wide), SEEK_CUR);
  1043. for (col=0; col < wide; col++) {
  1044. if (fuji_layout) {
  1045. r = fuji_width - 1 - col + (row >> 1);
  1046. c = col + ((row+1) >> 1);
  1047. } else {
  1048. r = fuji_width - 1 + row - (col >> 1);
  1049. c = row + ((col+1) >> 1);
  1050. }
  1051. BAYER(r,c) = pixel[col];
  1052. }
  1053. }
  1054. free (pixel);
  1055. }
  1056. #line 1427 "dcraw/dcraw.c"
  1057. void CLASS ppm_thumb (FILE *tfp)
  1058. {
  1059. char *thumb;
  1060. thumb_length = thumb_width*thumb_height*3;
  1061. thumb = (char *) malloc (thumb_length);
  1062. merror (thumb, "ppm_thumb()");
  1063. fprintf (tfp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
  1064. fread (thumb, 1, thumb_length, ifp);
  1065. fwrite (thumb, 1, thumb_length, tfp);
  1066. free (thumb);
  1067. }
  1068. void CLASS layer_thumb (FILE *tfp)
  1069. {
  1070. int i, c;
  1071. char *thumb, map[][4] = { "012","102" };
  1072. colors = thumb_misc >> 5 & 7;
  1073. thumb_length = thumb_width*thumb_height;
  1074. thumb = (char *) calloc (colors, thumb_length);
  1075. merror (thumb, "layer_thumb()");
  1076. fprintf (tfp, "P%d\n%d %d\n255\n",
  1077. 5 + (colors >> 1), thumb_width, thumb_height);
  1078. fread (thumb, thumb_length, colors, ifp);
  1079. for (i=0; i < thumb_length; i++)
  1080. FORCC putc (thumb[i+thumb_length*(map[thumb_misc >> 8][c]-'0')], tfp);
  1081. free (thumb);
  1082. }
  1083. void CLASS rollei_thumb (FILE *tfp)
  1084. {
  1085. unsigned i;
  1086. ushort *thumb;
  1087. thumb_length = thumb_width * thumb_height;
  1088. thumb = (ushort *) calloc (thumb_length, 2);
  1089. merror (thumb, "rollei_thumb()");
  1090. fprintf (tfp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
  1091. read_shorts (thumb, thumb_length);
  1092. for (i=0; i < thumb_length; i++) {
  1093. putc (thumb[i] << 3, tfp);
  1094. putc (thumb[i] >> 5 << 2, tfp);
  1095. putc (thumb[i] >> 11 << 3, tfp);
  1096. }
  1097. free (thumb);
  1098. }
  1099. void CLASS rollei_load_raw()
  1100. {
  1101. uchar pixel[10];
  1102. unsigned iten=0, isix, i, buffer=0, row, col, todo[16];
  1103. isix = raw_width * raw_height * 5 / 8;
  1104. while (fread (pixel, 1, 10, ifp) == 10) {
  1105. for (i=0; i < 10; i+=2) {
  1106. todo[i] = iten++;
  1107. todo[i+1] = pixel[i] << 8 | pixel[i+1];
  1108. buffer = pixel[i] >> 2 | buffer << 6;
  1109. }
  1110. for ( ; i < 16; i+=2) {
  1111. todo[i] = isix++;
  1112. todo[i+1] = buffer >> (14-i)*5;
  1113. }
  1114. for (i=0; i < 16; i+=2) {
  1115. row = todo[i] / raw_width - top_margin;
  1116. col = todo[i] % raw_width - left_margin;
  1117. if (row < height && col < width)
  1118. BAYER(row,col) = (todo[i+1] & 0x3ff);
  1119. }
  1120. }
  1121. maximum = 0x3ff;
  1122. }
  1123. int CLASS bayer (unsigned row, unsigned col)
  1124. {
  1125. return (row < height && col < width) ? BAYER(row,col) : 0;
  1126. }
  1127. void CLASS phase_one_flat_field (int is_float, int nc)
  1128. {
  1129. ushort head[8];
  1130. unsigned wide, y, x, c, rend, cend, row, col;
  1131. float *mrow, num, mult[4];
  1132. read_shorts (head, 8);
  1133. wide = head[2] / head[4];
  1134. mrow = (float *) calloc (nc*wide, sizeof *mrow);
  1135. merror (mrow, "phase_one_flat_field()");
  1136. for (y=0; y < head[3] / head[5]; y++) {
  1137. for (x=0; x < wide; x++)
  1138. for (c=0; c < nc; c+=2) {
  1139. num = is_float ? getreal(11) : get2()/32768.0;
  1140. if (y==0) mrow[c*wide+x] = num;
  1141. else mrow[(c+1)*wide+x] = (num - mrow[c*wide+x]) / head[5];
  1142. }
  1143. if (y==0) continue;
  1144. rend = head[1]-top_margin + y*head[5];
  1145. for (row = rend-head[5]; row < height && row < rend; row++) {
  1146. for (x=1; x < wide; x++) {
  1147. for (c=0; c < nc; c+=2) {
  1148. mult[c] = mrow[c*wide+x-1];
  1149. mult[c+1] = (mrow[c*wide+x] - mult[c]) / head[4];
  1150. }
  1151. cend = head[0]-left_margin + x*head[4];
  1152. for (col = cend-head[4]; col < width && col < cend; col++) {
  1153. c = nc > 2 ? FC(row,col) : 0;
  1154. if (!(c & 1)) {
  1155. c = BAYER(row,col) * mult[c];
  1156. BAYER(row,col) = LIM(c,0,65535);
  1157. }
  1158. for (c=0; c < nc; c+=2)
  1159. mult[c] += mult[c+1];
  1160. }
  1161. }
  1162. for (x=0; x < wide; x++)
  1163. for (c=0; c < nc; c+=2)
  1164. mrow[c*wide+x] += mrow[(c+1)*wide+x];
  1165. }
  1166. }
  1167. free (mrow);
  1168. }
  1169. void CLASS phase_one_correct()
  1170. {
  1171. unsigned entries, tag, data, save, col, row, type;
  1172. int len, i, j, k, cip, val[4], dev[4], sum, max;
  1173. int head[9], diff, mindiff=INT_MAX, off_412=0;
  1174. static const signed char dir[12][2] =
  1175. { {-1,-1}, {-1,1}, {1,-1}, {1,1}, {-2,0}, {0,-2}, {0,2}, {2,0},
  1176. {-2,-2}, {-2,2}, {2,-2}, {2,2} };
  1177. float poly[8], num, cfrac, frac, mult[2], *yval[2];
  1178. ushort t_curve[0x10000], *xval[2];
  1179. if (half_size || !meta_length) return;
  1180. #ifdef DCRAW_VERBOSE
  1181. if (verbose) fprintf (stderr,_("Phase One correction...\n"));
  1182. #endif
  1183. fseek (ifp, meta_offset, SEEK_SET);
  1184. order = get2();
  1185. fseek (ifp, 6, SEEK_CUR);
  1186. fseek (ifp, meta_offset+get4(), SEEK_SET);
  1187. entries = get4(); get4();
  1188. while (entries--) {
  1189. tag = get4();
  1190. len = get4();
  1191. data = get4();
  1192. save = ftell(ifp);
  1193. fseek (ifp, meta_offset+data, SEEK_SET);
  1194. if (tag == 0x419) { /* Polynomial curve */
  1195. for (get4(), i=0; i < 8; i++)
  1196. poly[i] = getreal(11);
  1197. poly[3] += (ph1.tag_210 - poly[7]) * poly[6] + 1;
  1198. for (i=0; i < 0x10000; i++) {
  1199. num = (poly[5]*i + poly[3])*i + poly[1];
  1200. t_curve[i] = LIM(num,0,65535);
  1201. } goto apply; /* apply to right half */
  1202. } else if (tag == 0x41a) { /* Polynomial curve */
  1203. for (i=0; i < 4; i++)
  1204. poly[i] = getreal(11);
  1205. for (i=0; i < 0x10000; i++) {
  1206. for (num=0, j=4; j--; )
  1207. num = num * i + poly[j];
  1208. t_curve[i] = LIM(num+i,0,65535);
  1209. } apply: /* apply to whole image */
  1210. for (row=0; row < height; row++)
  1211. for (col = (tag & 1)*ph1.split_col; col < width; col++)
  1212. BAYER(row,col) = t_curve[BAYER(row,col)];
  1213. } else if (tag == 0x400) { /* Sensor defects */
  1214. while ((len -= 8) >= 0) {
  1215. col = get2() - left_margin;
  1216. row = get2() - top_margin;
  1217. type = get2(); get2();
  1218. if (col >= width) continue;
  1219. if (type == 131) /* Bad column */
  1220. for (row=0; row < height; row++)
  1221. if (FC(row,col) == 1) {
  1222. for (sum=i=0; i < 4; i++)
  1223. sum += val[i] = bayer (row+dir[i][0], col+dir[i][1]);
  1224. for (max=i=0; i < 4; i++) {
  1225. dev[i] = abs((val[i] << 2) - sum);
  1226. if (dev[max] < dev[i]) max = i;
  1227. }
  1228. BAYER(row,col) = (sum - val[max])/3.0 + 0.5;
  1229. } else {
  1230. for (sum=0, i=8; i < 12; i++)
  1231. sum += bayer (row+dir[i][0], col+dir[i][1]);
  1232. BAYER(row,col) = 0.5 + sum * 0.0732233 +
  1233. (bayer(row,col-2) + bayer(row,col+2)) * 0.3535534;
  1234. }
  1235. else if (type == 129) { /* Bad pixel */
  1236. if (row >= height) continue;
  1237. j = (FC(row,col) != 1) * 4;
  1238. for (sum=0, i=j; i < j+8; i++)
  1239. sum += bayer (row+dir[i][0], col+dir[i][1]);
  1240. BAYER(row,col) = (sum + 4) >> 3;
  1241. }
  1242. }
  1243. } else if (tag == 0x401) { /* All-color flat fields */
  1244. phase_one_flat_field (1, 2);
  1245. } else if (tag == 0x416 || tag == 0x410) {
  1246. phase_one_flat_field (0, 2);
  1247. } else if (tag == 0x40b) { /* Red+blue flat field */
  1248. phase_one_flat_field (0, 4);
  1249. } else if (tag == 0x412) {
  1250. fseek (ifp, 36, SEEK_CUR);
  1251. diff = abs (get2() - ph1.tag_21a);
  1252. if (mindiff > diff) {
  1253. mindiff = diff;
  1254. off_412 = ftell(ifp) - 38;
  1255. }
  1256. }
  1257. fseek (ifp, save, SEEK_SET);
  1258. }
  1259. if (off_412) {
  1260. fseek (ifp, off_412, SEEK_SET);
  1261. for (i=0; i < 9; i++) head[i] = get4() & 0x7fff;
  1262. yval[0] = (float *) calloc (head[1]*head[3] + head[2]*head[4], 6);
  1263. merror (yval[0], "phase_one_correct()");
  1264. yval[1] = (float *) (yval[0] + head[1]*head[3]);
  1265. xval[0] = (ushort *) (yval[1] + head[2]*head[4]);
  1266. xval[1] = (ushort *) (xval[0] + head[1]*head[3]);
  1267. get2();
  1268. for (i=0; i < 2; i++)
  1269. for (j=0; j < head[i+1]*head[i+3]; j++)
  1270. yval[i][j] = getreal(11);
  1271. for (i=0; i < 2; i++)
  1272. for (j=0; j < head[i+1]*head[i+3]; j++)
  1273. xval[i][j] = get2();
  1274. for (row=0; row < height; row++)
  1275. for (col=0; col < width; col++) {
  1276. cfrac = (float) col * head[3] / raw_width;
  1277. cfrac -= cip = cfrac;
  1278. num = BAYER(row,col) * 0.5;
  1279. for (i=cip; i < cip+2; i++) {
  1280. for (k=j=0; j < head[1]; j++)
  1281. if (num < xval[0][k = head[1]*i+j]) break;
  1282. frac = (j == 0 || j == head[1]) ? 0 :
  1283. (xval[0][k] - num) / (xval[0][k] - xval[0][k-1]);
  1284. mult[i-cip] = yval[0][k-1] * frac + yval[0][k] * (1-frac);
  1285. }
  1286. i = ((mult[0] * (1-cfrac) + mult[1] * cfrac)
  1287. * (row + top_margin) + num) * 2;
  1288. BAYER(row,col) = LIM(i,0,65535);
  1289. }
  1290. free (yval[0]);
  1291. }
  1292. }
  1293. void CLASS phase_one_load_raw()
  1294. {
  1295. int row, col, a, b;
  1296. ushort *pixel, akey, bkey, mask;
  1297. fseek (ifp, ph1.key_off, SEEK_SET);
  1298. akey = get2();
  1299. bkey = get2();
  1300. mask = ph1.format == 1 ? 0x5555:0x1354;
  1301. fseek (ifp, data_offset + top_margin*raw_width*2, SEEK_SET);
  1302. pixel = (ushort *) calloc (raw_width, sizeof *pixel);
  1303. merror (pixel, "phase_one_load_raw()");
  1304. for (row=0; row < height; row++) {
  1305. read_shorts (pixel, raw_width);
  1306. for (col=0; col < raw_width; col+=2) {
  1307. a = pixel[col+0] ^ akey;
  1308. b = pixel[col+1] ^ bkey;
  1309. pixel[col+0] = (a & mask) | (b & ~mask);
  1310. pixel[col+1] = (b & mask) | (a & ~mask);
  1311. }
  1312. for (col=0; col < width; col++)
  1313. BAYER(row,col) = pixel[col+left_margin];
  1314. }
  1315. free (pixel);
  1316. phase_one_correct();
  1317. }
  1318. unsigned CLASS ph1_bits (int nbits)
  1319. {
  1320. #ifndef LIBRAW_NOTHREADS
  1321. #define bitbuf tls->ph1_bits.bitbuf
  1322. #define vbits tls->ph1_bits.vbits
  1323. #else
  1324. static UINT64 bitbuf=0;
  1325. static int vbits=0;
  1326. #endif
  1327. if (nbits == -1)
  1328. return bitbuf = vbits = 0;
  1329. if (nbits == 0) return 0;
  1330. if ((vbits -= nbits) < 0) {
  1331. bitbuf = bitbuf << 32 | get4();
  1332. vbits += 32;
  1333. }
  1334. return bitbuf << (64-nbits-vbits) >> (64-nbits);
  1335. #ifndef LIBRAW_NOTHREADS
  1336. #undef bitbuf
  1337. #undef vbits
  1338. #endif
  1339. }
  1340. void CLASS phase_one_load_raw_c()
  1341. {
  1342. static const int length[] = { 8,7,6,9,11,10,5,12,14,13 };
  1343. int *offset, len[2], pred[2], row, col, i, j;
  1344. ushort *pixel;
  1345. short (*t_black)[2];
  1346. pixel = (ushort *) calloc (raw_width + raw_height*4, 2);
  1347. merror (pixel, "phase_one_load_raw_c()");
  1348. offset = (int *) (pixel + raw_width);
  1349. fseek (ifp, strip_offset, SEEK_SET);
  1350. for (row=0; row < raw_height; row++)
  1351. offset[row] = get4();
  1352. t_black = (short (*)[2]) offset + raw_height;
  1353. fseek (ifp, ph1.black_off, SEEK_SET);
  1354. if (ph1.black_off)
  1355. read_shorts ((ushort *) t_black[0], raw_height*2);
  1356. for (i=0; i < 256; i++)
  1357. curve[i] = i*i / 3.969 + 0.5;
  1358. for (row=0; row < raw_height; row++) {
  1359. fseek (ifp, data_offset + offset[row], SEEK_SET);
  1360. ph1_bits(-1);
  1361. pred[0] = pred[1] = 0;
  1362. for (col=0; col < raw_width; col++) {
  1363. if (col >= (raw_width & -8))
  1364. len[0] = len[1] = 14;
  1365. else if ((col & 7) == 0)
  1366. for (i=0; i < 2; i++) {
  1367. for (j=0; j < 5 && !ph1_bits(1); j++);
  1368. if (j--) len[i] = length[j*2 + ph1_bits(1)];
  1369. }
  1370. if ((i = len[col & 1]) == 14)
  1371. pixel[col] = pred[col & 1] = ph1_bits(16);
  1372. else
  1373. pixel[col] = pred[col & 1] += ph1_bits(i) + 1 - (1 << (i - 1));
  1374. if (pred[col & 1] >> 16) derror();
  1375. if (ph1.format == 5 && pixel[col] < 256)
  1376. pixel[col] = curve[pixel[col]];
  1377. }
  1378. if ((unsigned) (row-top_margin) < height)
  1379. for (col=0; col < width; col++) {
  1380. i = (pixel[col+left_margin] << 2)
  1381. - ph1.t_black + t_black[row][col >= ph1.split_col];
  1382. if (i > 0) BAYER(row-top_margin,col) = i;
  1383. }
  1384. }
  1385. free (pixel);
  1386. phase_one_correct();
  1387. maximum = 0xfffc - ph1.t_black;
  1388. }
  1389. void CLASS hasselblad_load_raw()
  1390. {
  1391. struct jhead jh;
  1392. struct decode *dindex;
  1393. int row, col, pred[2], len[2], diff, i;
  1394. if (!ljpeg_start (&jh, 0)) return;
  1395. free (jh.row);
  1396. order = 0x4949;
  1397. ph1_bits(-1);
  1398. for (row=-top_margin; row < raw_height-top_margin; row++) {
  1399. pred[0] = pred[1] = 0x8000;
  1400. for (col=-left_margin; col < raw_width-left_margin; col+=2) {
  1401. for (i=0; i < 2; i++) {
  1402. for (dindex=jh.huff[0]; dindex->branch[0]; )
  1403. dindex = dindex->branch[ph1_bits(1)];
  1404. len[i] = dindex->leaf;
  1405. }
  1406. for (i=0; i < 2; i++) {
  1407. diff = ph1_bits(len[i]);
  1408. if ((diff & (1 << (len[i]-1))) == 0)
  1409. diff -= (1 << len[i]) - 1;
  1410. if (diff == 65535) diff = -32768;
  1411. pred[i] += diff;
  1412. if (row >= 0 && row < height && (unsigned)(col+i) < width)
  1413. BAYER(row,col+i) = pred[i];
  1414. }
  1415. }
  1416. }
  1417. maximum = 0xffff;
  1418. }
  1419. void CLASS leaf_hdr_load_raw()
  1420. {
  1421. ushort *pixel;
  1422. unsigned tile=0, r, c, row, col;
  1423. pixel = (ushort *) calloc (raw_width, sizeof *pixel);
  1424. merror (pixel, "leaf_hdr_load_raw()");
  1425. FORC(tiff_samples)
  1426. for (r=0; r < raw_height; r++) {
  1427. if (r % tile_length == 0) {
  1428. fseek (ifp, data_offset + 4*tile++, SEEK_SET);
  1429. fseek (ifp, get4() + 2*left_margin, SEEK_SET);
  1430. }
  1431. if (filters && c != shot_select) continue;
  1432. read_shorts (pixel, raw_width);
  1433. if ((row = r - top_margin) >= height) continue;
  1434. for (col=0; col < width; col++)
  1435. if (filters) BAYER(row,col) = pixel[col];
  1436. else image[row*width+col][c] = pixel[col];
  1437. }
  1438. free (pixel);
  1439. if (!filters) {
  1440. maximum = 0xffff;
  1441. raw_color = 1;
  1442. }
  1443. }
  1444. #line 1839 "dcraw/dcraw.c"
  1445. void CLASS sinar_4shot_load_raw()
  1446. {
  1447. ushort *pixel;
  1448. unsigned shot, row, col, r, c;
  1449. if ((shot = shot_select) || half_size) {
  1450. if (shot) shot--;
  1451. if (shot > 3) shot = 3;
  1452. fseek (ifp, data_offset + shot*4, SEEK_SET);
  1453. fseek (ifp, get4(), SEEK_SET);
  1454. unpacked_load_raw();
  1455. return;
  1456. }
  1457. free (image);
  1458. image = (ushort (*)[4])
  1459. calloc ((iheight=height)*(iwidth=width), sizeof *image);
  1460. merror (image, "sinar_4shot_load_raw()");
  1461. pixel = (ushort *) calloc (raw_width, sizeof *pixel);
  1462. merror (pixel, "sinar_4shot_load_raw()");
  1463. for (shot=0; shot < 4; shot++) {
  1464. fseek (ifp, data_offset + shot*4, SEEK_SET);
  1465. fseek (ifp, get4(), SEEK_SET);
  1466. for (row=0; row < raw_height; row++) {
  1467. read_shorts (pixel, raw_width);
  1468. if ((r = row-top_margin - (shot >> 1 & 1)) >= height) continue;
  1469. for (col=0; col < raw_width; col++) {
  1470. if ((c = col-left_margin - (shot & 1)) >= width) continue;
  1471. image[r*width+c][FC(row,col)] = pixel[col];
  1472. }
  1473. }
  1474. }
  1475. free (pixel);
  1476. shrink = filters = 0;
  1477. }
  1478. void CLASS imacon_full_load_raw()
  1479. {
  1480. int row, col;
  1481. for (row=0; row < height; row++)
  1482. for (col=0; col < width; col++)
  1483. read_shorts (image[row*width+col], 3);
  1484. }
  1485. void CLASS packed_12_load_raw()
  1486. {
  1487. int vbits=0, rbits=0, irow, row, col;
  1488. UINT64 bitbuf=0;
  1489. if (raw_width * 2 >= width * 3) { /* If raw_width is in bytes, */
  1490. rbits = raw_width * 8;
  1491. raw_width = raw_width * 2 / 3; /* convert it to pixels and */
  1492. rbits -= raw_width * 12; /* save the remainder. */
  1493. }
  1494. order = load_flags & 1 ? 0x4949 : 0x4d4d;
  1495. for (irow=0; irow < height; irow++) {
  1496. row = irow;
  1497. if (load_flags & 2 &&
  1498. (row = irow * 2 % height + irow / (height/2)) == 1 &&
  1499. load_flags & 4) {
  1500. if (vbits=0, tiff_compress)
  1501. fseek (ifp, data_offset - (-width*height*3/4 & -2048), SEEK_SET);
  1502. else {
  1503. fseek (ifp, 0, SEEK_END);
  1504. fseek (ifp, ftell(ifp)/2, SEEK_SET);
  1505. }
  1506. }
  1507. for (col=0; col < raw_width; col++) {
  1508. if ((vbits -= 12) < 0) {
  1509. bitbuf = bitbuf << 32 | get4();
  1510. vbits += 32;
  1511. }
  1512. if ((unsigned) (col-left_margin) < width)
  1513. BAYER(row,col-left_margin) = bitbuf << (52-vbits) >> 52;
  1514. if (load_flags & 8 && (col % 10) == 9)
  1515. if (vbits=0, bitbuf & 255) derror();
  1516. }
  1517. vbits -= rbits;
  1518. }
  1519. if (!strcmp(make,"OLYMPUS")) black >>= 4;
  1520. }
  1521. void CLASS unpacked_load_raw()
  1522. {
  1523. ushort *pixel;
  1524. int row, col, bits=0;
  1525. while (1 << ++bits < maximum);
  1526. fseek (ifp, (top_margin*raw_width + left_margin) * 2, SEEK_CUR);
  1527. pixel = (ushort *) calloc (width, sizeof *pixel);
  1528. merror (pixel, "unpacked_load_raw()");
  1529. for (row=0; row < height; row++) {
  1530. read_shorts (pixel, width);
  1531. fseek (ifp, 2*(raw_width - width), SEEK_CUR);
  1532. for (col=0; col < width; col++)
  1533. if ((BAYER2(row,col) = pixel[col]) >> bits) derror();
  1534. }
  1535. free (pixel);
  1536. }
  1537. void CLASS nokia_load_raw()
  1538. {
  1539. uchar *data, *dp;
  1540. ushort *pixel, *pix;
  1541. int dwide, row, c;
  1542. dwide = raw_width * 5 / 4;
  1543. data = (uchar *) malloc (dwide + raw_width*2);
  1544. merror (data, "nokia_load_raw()");
  1545. pixel = (ushort *) (data + dwide);
  1546. for (row=0; row < raw_height; row++) {
  1547. if (fread (data, 1, dwide, ifp) < dwide) derror();
  1548. for (dp=data, pix=pixel; pix < pixel+raw_width; dp+=5, pix+=4)
  1549. FORC4 pix[c] = (dp[c] << 2) | (dp[4] >> (c << 1) & 3);
  1550. if (row < top_margin)
  1551. FORC(width) black += pixel[c];
  1552. else
  1553. FORC(width) BAYER(row-top_margin,c) = pixel[c];
  1554. }
  1555. free (data);
  1556. if (top_margin) black /= top_margin * width;
  1557. maximum = 0x3ff;
  1558. }
  1559. unsigned CLASS pana_bits (int nbits)
  1560. {
  1561. #ifndef LIBRAW_NOTHREADS
  1562. #define buf tls->pana_bits.buf
  1563. #define vbits tls->pana_bits.vbits
  1564. #else
  1565. static uchar buf[0x4000];
  1566. static int vbits;
  1567. #endif
  1568. int byte;
  1569. if (!nbits) return vbits=0;
  1570. if (!vbits) {
  1571. fread (buf+load_flags, 1, 0x4000-load_flags, ifp);
  1572. fread (buf, 1, load_flags, ifp);
  1573. }
  1574. vbits = (vbits - nbits) & 0x1ffff;
  1575. byte = vbits >> 3 ^ 0x3ff0;
  1576. return (buf[byte] | buf[byte+1] << 8) >> (vbits & 7) & ~(-1 << nbits);
  1577. #ifndef LIBRAW_NOTHREADS
  1578. #undef buf
  1579. #undef vbits
  1580. #endif
  1581. }
  1582. void CLASS panasonic_load_raw()
  1583. {
  1584. int row, col, i, j, sh=0, pred[2], nonz[2];
  1585. pana_bits(0);
  1586. for (row=0; row < height; row++)
  1587. for (col=0; col < raw_width; col++) {
  1588. if ((i = col % 14) == 0)
  1589. pred[0] = pred[1] = nonz[0] = nonz[1] = 0;
  1590. if (i % 3 == 2) sh = 4 >> (3 - pana_bits(2));
  1591. if (nonz[i & 1]) {
  1592. if ((j = pana_bits(8))) {
  1593. if ((pred[i & 1] -= 0x80 << sh) < 0 || sh == 4)
  1594. pred[i & 1] &= ~(-1 << sh);
  1595. pred[i & 1] += j << sh;
  1596. }
  1597. } else if ((nonz[i & 1] = pana_bits(8)) || i > 11)
  1598. pred[i & 1] = nonz[i & 1] << 4 | pana_bits(4);
  1599. if (col < width)
  1600. if ((BAYER(row,col) = pred[col & 1]) > 4098) derror();
  1601. }
  1602. }
  1603. void CLASS olympus_e300_load_raw()
  1604. {
  1605. uchar *data, *dp;
  1606. ushort *pixel, *pix;
  1607. int dwide, row, col;
  1608. dwide = raw_width * 16 / 10;
  1609. fseek (ifp, dwide*top_margin, SEEK_CUR);
  1610. data = (uchar *) malloc (dwide + raw_width*2);
  1611. merror (data, "olympus_e300_load_raw()");
  1612. pixel = (ushort *) (data + dwide);
  1613. for (row=0; row < height; row++){
  1614. if (fread (data, 1, dwide, ifp) < dwide) derror();
  1615. for (dp=data, pix=pixel; pix < pixel+raw_width; dp+=3, pix+=2) {
  1616. if (((dp-data) & 15) == 15)
  1617. if (*dp++ && pix < pixel+width+left_margin) derror();
  1618. pix[0] = dp[1] << 8 | dp[0];
  1619. pix[1] = dp[2] << 4 | dp[1] >> 4;
  1620. }
  1621. for (col=0; col < width; col++)
  1622. BAYER(row,col) = (pixel[col+left_margin] & 0xfff);
  1623. }
  1624. free (data);
  1625. maximum >>= 4;
  1626. black >>= 4;
  1627. }
  1628. void CLASS olympus_e410_load_raw()
  1629. {
  1630. int row, col, nbits, sign, low, high, i, w, n, nw;
  1631. int acarry[2][3], *carry, pred, diff;
  1632. fseek (ifp, 7, SEEK_CUR);
  1633. getbits(-1);
  1634. for (row=0; row < height; row++) {
  1635. memset (acarry, 0, sizeof acarry);
  1636. for (col=0; col < width; col++) {
  1637. carry = acarry[col & 1];
  1638. i = 2 * (carry[2] < 3);
  1639. for (nbits=2+i; (ushort) carry[0] >> (nbits+i); nbits++);
  1640. sign = getbits(1) * -1;
  1641. low = getbits(2);
  1642. for (high=0; high < 12; high++)
  1643. if (getbits(1)) break;
  1644. if (high == 12)
  1645. high = getbits(16-nbits) >> 1;
  1646. carry[0] = (high << nbits) | getbits(nbits);
  1647. diff = (carry[0] ^ sign) + carry[1];
  1648. carry[1] = (diff*3 + carry[1]) >> 5;
  1649. carry[2] = carry[0] > 16 ? 0 : carry[2]+1;
  1650. if (row < 2 && col < 2) pred = 0;
  1651. else if (row < 2) pred = BAYER(row,col-2);
  1652. else if (col < 2) pred = BAYER(row-2,col);
  1653. else {
  1654. w = BAYER(row,col-2);
  1655. n = BAYER(row-2,col);
  1656. nw = BAYER(row-2,col-2);
  1657. if ((w < nw && nw < n) || (n < nw && nw < w)) {
  1658. if (ABS(w-nw) > 32 || ABS(n-nw) > 32)
  1659. pred = w + n - nw;
  1660. else pred = (w + n) >> 1;
  1661. } else pred = ABS(w-nw) > ABS(n-nw) ? w : n;
  1662. }
  1663. if ((BAYER(row,col) = pred + ((diff << 2) | low)) >> 12) derror();
  1664. }
  1665. }
  1666. }
  1667. void CLASS minolta_rd175_load_raw()
  1668. {
  1669. uchar pixel[768];
  1670. unsigned irow, box, row, col;
  1671. for (irow=0; irow < 1481; irow++) {
  1672. if (fread (pixel, 1, 768, ifp) < 768) derror();
  1673. box = irow / 82;
  1674. row = irow % 82 * 12 + ((box < 12) ? box | 1 : (box-12)*2);
  1675. switch (irow) {
  1676. case 1477: case 1479: continue;
  1677. case 1476: row = 984; break;
  1678. case 1480: row = 985; break;
  1679. case 1478: row = 985; box = 1;
  1680. }
  1681. if ((box < 12) && (box & 1)) {
  1682. for (col=0; col < 1533; col++, row ^= 1)
  1683. if (col != 1) BAYER(row,col) = (col+1) & 2 ?
  1684. pixel[col/2-1] + pixel[col/2+1] : pixel[col/2] << 1;
  1685. BAYER(row,1) = pixel[1] << 1;
  1686. BAYER(row,1533) = pixel[765] << 1;
  1687. } else
  1688. for (col=row & 1; col < 1534; col+=2)
  1689. BAYER(row,col) = pixel[col/2] << 1;
  1690. }
  1691. maximum = 0xff << 1;
  1692. }
  1693. void CLASS casio_qv5700_load_raw()
  1694. {
  1695. uchar data[3232], *dp;
  1696. ushort pixel[2576], *pix;
  1697. int row, col;
  1698. for (row=0; row < height; row++) {
  1699. fread (data, 1, 3232, ifp);
  1700. for (dp=data, pix=pixel; dp < data+3220; dp+=5, pix+=4) {
  1701. pix[0] = (dp[0] << 2) + (dp[1] >> 6);
  1702. pix[1] = (dp[1] << 4) + (dp[2] >> 4);
  1703. pix[2] = (dp[2] << 6) + (dp[3] >> 2);
  1704. pix[3] = (dp[3] << 8) + (dp[4] );
  1705. }
  1706. for (col=0; col < width; col++)
  1707. BAYER(row,col) = (pixel[col] & 0x3ff);
  1708. }
  1709. maximum = 0x3fc;
  1710. }
  1711. void CLASS quicktake_100_load_raw()
  1712. {
  1713. uchar pixel[484][644];
  1714. static const short gstep[16] =
  1715. { -89,-60,-44,-32,-22,-15,-8,-2,2,8,15,22,32,44,60,89 };
  1716. static const short rstep[6][4] =
  1717. { { -3,-1,1,3 }, { -5,-1,1,5 }, { -8,-2,2,8 },
  1718. { -13,-3,3,13 }, { -19,-4,4,19 }, { -28,-6,6,28 } };
  1719. static const short t_curve[256] =
  1720. { 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,
  1721. 28,29,30,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,53,
  1722. 54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,74,75,76,77,78,
  1723. 79,80,81,82,83,84,86,88,90,92,94,97,99,101,103,105,107,110,112,114,116,
  1724. 118,120,123,125,127,129,131,134,136,138,140,142,144,147,149,151,153,155,
  1725. 158,160,162,164,166,168,171,173,175,177,179,181,184,186,188,190,192,195,
  1726. 197,199,201,203,205,208,210,212,214,216,218,221,223,226,230,235,239,244,
  1727. 248,252,257,261,265,270,274,278,283,287,291,296,300,305,309,313,318,322,
  1728. 326,331,335,339,344,348,352,357,361,365,370,374,379,383,387,392,396,400,
  1729. 405,409,413,418,422,426,431,435,440,444,448,453,457,461,466,470,474,479,
  1730. 483,487,492,496,500,508,519,531,542,553,564,575,587,598,609,620,631,643,
  1731. 654,665,676,687,698,710,721,732,743,754,766,777,788,799,810,822,833,844,
  1732. 855,866,878,889,900,911,922,933,945,956,967,978,989,1001,1012,1023 };
  1733. int rb, row, col, sharp, val=0;
  1734. getbits(-1);
  1735. memset (pixel, 0x80, sizeof pixel);
  1736. for (row=2; row < height+2; row++) {
  1737. for (col=2+(row & 1); col < width+2; col+=2) {
  1738. val = ((pixel[row-1][col-1] + 2*pixel[row-1][col+1] +
  1739. pixel[row][col-2]) >> 2) + gstep[getbits(4)];
  1740. pixel[row][col] = val = LIM(val,0,255);
  1741. if (col < 4)
  1742. pixel[row][col-2] = pixel[row+1][~row & 1] = val;
  1743. if (row == 2)
  1744. pixel[row-1][col+1] = pixel[row-1][col+3] = val;
  1745. }
  1746. pixel[row][col] = val;
  1747. }
  1748. for (rb=0; rb < 2; rb++)
  1749. for (row=2+rb; row < height+2; row+=2)
  1750. for (col=3-(row & 1); col < width+2; col+=2) {
  1751. if (row < 4 || col < 4) sharp = 2;
  1752. else {
  1753. val = ABS(pixel[row-2][col] - pixel[row][col-2])
  1754. + ABS(pixel[row-2][col] - pixel[row-2][col-2])
  1755. + ABS(pixel[row][col-2] - pixel[row-2][col-2]);
  1756. sharp = val < 4 ? 0 : val < 8 ? 1 : val < 16 ? 2 :
  1757. val < 32 ? 3 : val < 48 ? 4 : 5;
  1758. }
  1759. val = ((pixel[row-2][col] + pixel[row][col-2]) >> 1)
  1760. + rstep[sharp][getbits(2)];
  1761. pixel[row][col] = val = LIM(val,0,255);
  1762. if (row < 4) pixel[row-2][col+2] = val;
  1763. if (col < 4) pixel[row+2][col-2] = val;
  1764. }
  1765. for (row=2; row < height+2; row++)
  1766. for (col=3-(row & 1); col < width+2; col+=2) {
  1767. val = ((pixel[row][col-1] + (pixel[row][col] << 2) +
  1768. pixel[row][col+1]) >> 1) - 0x100;
  1769. pixel[row][col] = LIM(val,0,255);
  1770. }
  1771. for (row=0; row < height; row++)
  1772. for (col=0; col < width; col++)
  1773. BAYER(row,col) = t_curve[pixel[row+2][col+2]];
  1774. maximum = 0x3ff;
  1775. }
  1776. const int * CLASS make_decoder_int (const int *source, int level)
  1777. {
  1778. struct decode *cur;
  1779. cur = free_decode++;
  1780. if (level < source[0]) {
  1781. cur->branch[0] = free_decode;
  1782. source = make_decoder_int (source, level+1);
  1783. cur->branch[1] = free_decode;
  1784. source = make_decoder_int (source, level+1);
  1785. } else {
  1786. cur->leaf = source[1];
  1787. source += 2;
  1788. }
  1789. return source;
  1790. }
  1791. int CLASS radc_token (int tree)
  1792. {
  1793. int t;
  1794. #ifndef LIBRAW_NOTHREADS
  1795. #define dstart tls->radc_token.dstart
  1796. #define dindex tls->radc_token.dindex
  1797. #define s tls->radc_token.s
  1798. static const int source[] = {
  1799. #else
  1800. static struct decode *dstart[18], *dindex;
  1801. static const int *s, source[] = {
  1802. #endif
  1803. 1,1, 2,3, 3,4, 4,2, 5,7, 6,5, 7,6, 7,8,
  1804. 1,0, 2,1, 3,3, 4,4, 5,2, 6,7, 7,6, 8,5, 8,8,
  1805. 2,1, 2,3, 3,0, 3,2, 3,4, 4,6, 5,5, 6,7, 6,8,
  1806. 2,0, 2,1, 2,3, 3,2, 4,4, 5,6, 6,7, 7,5, 7,8,
  1807. 2,1, 2,4, 3,0, 3,2, 3,3, 4,7, 5,5, 6,6, 6,8,
  1808. 2,3, 3,1, 3,2, 3,4, 3,5, 3,6, 4,7, 5,0, 5,8,
  1809. 2,3, 2,6, 3,0, 3,1, 4,4, 4,5, 4,7, 5,2, 5,8,
  1810. 2,4, 2,7, 3,3, 3,6, 4,1, 4,2, 4,5, 5,0, 5,8,
  1811. 2,6, 3,1, 3,3, 3,5, 3,7, 3,8, 4,0, 5,2, 5,4,
  1812. 2,0, 2,1, 3,2, 3,3, 4,4, 4,5, 5,6, 5,7, 4,8,
  1813. 1,0, 2,2, 2,-2,
  1814. 1,-3, 1,3,
  1815. 2,-17, 2,-5, 2,5, 2,17,
  1816. 2,-7, 2,2, 2,9, 2,18,
  1817. 2,-18, 2,-9, 2,-2, 2,7,
  1818. 2,-28, 2,28, 3,-49, 3,-9, 3,9, 4,49, 5,-79, 5,79,
  1819. 2,-1, 2,13, 2,26, 3,39, 4,-16, 5,55, 6,-37, 6,76,
  1820. 2,-26, 2,-13, 2,1, 3,-39, 4,16, 5,-55, 6,-76, 6,37
  1821. };
  1822. if (free_decode == first_decode)
  1823. for (s=source, t=0; t < 18; t++) {
  1824. dstart[t] = free_decode;
  1825. s = make_decoder_int (s, 0);
  1826. }
  1827. if (tree == 18) {
  1828. if (kodak_cbpp == 243)
  1829. return (getbits(6) << 2) + 2; /* most DC50 photos */
  1830. else
  1831. return (getbits(5) << 3) + 4; /* DC40, Fotoman Pixtura */
  1832. }
  1833. for (dindex = dstart[tree]; dindex->branch[0]; )
  1834. dindex = dindex->branch[getbits(1)];
  1835. return dindex->leaf;
  1836. #ifndef LIBRAW_NOTHREADS
  1837. #undef dstart
  1838. #undef dindex
  1839. #undef s
  1840. #endif
  1841. }
  1842. #define FORYX for (y=1; y < 3; y++) for (x=col+1; x >= col; x--)
  1843. #define PREDICTOR (c ? (buf[c][y-1][x] + buf[c][y][x+1]) / 2 \
  1844. : (buf[c][y-1][x+1] + 2*buf[c][y-1][x] + buf[c][y][x+1]) / 4)
  1845. void CLASS kodak_radc_load_raw()
  1846. {
  1847. int row, col, tree, nreps, rep, step, i, c, s, r, x, y, val;
  1848. short last[3] = { 16,16,16 }, mul[3], buf[3][3][386];
  1849. init_decoder();
  1850. getbits(-1);
  1851. for (i=0; i < sizeof(buf)/sizeof(short); i++)
  1852. buf[0][0][i] = 2048;
  1853. for (row=0; row < height; row+=4) {
  1854. FORC3 mul[c] = getbits(6);
  1855. FORC3 {
  1856. val = ((0x1000000/last[c] + 0x7ff) >> 12) * mul[c];
  1857. s = val > 65564 ? 10:12;
  1858. x = ~(-1 << (s-1));
  1859. val <<= 12-s;
  1860. for (i=0; i < sizeof(buf[0])/sizeof(short); i++)
  1861. buf[c][0][i] = (buf[c][0][i] * val + x) >> s;
  1862. last[c] = mul[c];
  1863. for (r=0; r <= !c; r++) {
  1864. buf[c][1][width/2] = buf[c][2][width/2] = mul[c] << 7;
  1865. for (tree=1, col=width/2; col > 0; ) {
  1866. if ((tree = radc_token(tree))) {
  1867. col -= 2;
  1868. if (tree == 8)
  1869. FORYX buf[c][y][x] = radc_token(tree+10) * mul[c];
  1870. else
  1871. FORYX buf[c][y][x] = radc_token(tree+10) * 16 + PREDICTOR;
  1872. } else
  1873. do {
  1874. nreps = (col > 2) ? radc_token(9) + 1 : 1;
  1875. for (rep=0; rep < 8 && rep < nreps && col > 0; rep++) {
  1876. col -= 2;
  1877. FORYX buf[c][y][x] = PREDICTOR;
  1878. if (rep & 1) {
  1879. step = radc_token(10) << 4;
  1880. FORYX buf[c][y][x] += step;
  1881. }
  1882. }
  1883. } while (nreps == 9);
  1884. }
  1885. for (y=0; y < 2; y++)
  1886. for (x=0; x < width/2; x++) {
  1887. val = (buf[c][y+1][x] << 4) / mul[c];
  1888. if (val < 0) val = 0;
  1889. if (c) BAYER(row+y*2+c-1,x*2+2-c) = val;
  1890. else BAYER(row+r*2+y,x*2+y) = val;
  1891. }
  1892. memcpy (buf[c][0]+!c, buf[c][2], sizeof buf[c][0]-2*!c);
  1893. }
  1894. }
  1895. for (y=row; y < row+4; y++)
  1896. for (x=0; x < width; x++)
  1897. if ((x+y) & 1) {
  1898. r = x ? x-1 : x+1;
  1899. s = x+1 < width ? x+1 : x-1;
  1900. val = (BAYER(y,x)-2048)*2 + (BAYER(y,r)+BAYER(y,s))/2;
  1901. if (val < 0) val = 0;
  1902. BAYER(y,x) = val;
  1903. }
  1904. }
  1905. maximum = 0xfff;
  1906. use_gamma = 0;
  1907. }
  1908. #undef FORYX
  1909. #undef PREDICTOR
  1910. #ifdef NO_JPEG
  1911. void CLASS kodak_jpeg_load_raw() {}
  1912. #else
  1913. METHODDEF(boolean)
  1914. fill_input_buffer (j_decompress_ptr cinfo)
  1915. {
  1916. #ifndef LIBRAW_NOTHREADS
  1917. #define jpeg_buffer tls->jpeg_buffer
  1918. #else
  1919. static uchar jpeg_buffer[4096];
  1920. #endif
  1921. size_t nbytes;
  1922. nbytes = fread (jpeg_buffer, 1, 4096, ifp);
  1923. swab (jpeg_buffer, jpeg_buffer, nbytes);
  1924. cinfo->src->next_input_byte = jpeg_buffer;
  1925. cinfo->src->bytes_in_buffer = nbytes;
  1926. return TRUE;
  1927. #ifndef LIBRAW_NOTHREADS
  1928. #undef jpeg_buffer
  1929. #endif
  1930. }
  1931. void CLASS kodak_jpeg_load_raw()
  1932. {
  1933. struct jpeg_decompress_struct cinfo;
  1934. struct jpeg_error_mgr jerr;
  1935. JSAMPARRAY buf;
  1936. JSAMPLE (*pixel)[3];
  1937. int row, col;
  1938. cinfo.err = jpeg_std_error (&jerr);
  1939. jpeg_create_decompress (&cinfo);
  1940. jpeg_stdio_src (&cinfo, ifp);
  1941. cinfo.src->fill_input_buffer = fill_input_buffer;
  1942. jpeg_read_header (&cinfo, TRUE);
  1943. jpeg_start_decompress (&cinfo);
  1944. if ((cinfo.output_width != width ) ||
  1945. (cinfo.output_height*2 != height ) ||
  1946. (cinfo.output_components != 3 )) {
  1947. #ifdef DCRAW_VERBOSE
  1948. fprintf (stderr,_("%s: incorrect JPEG dimensions\n"), ifname);
  1949. #endif
  1950. jpeg_destroy_decompress (&cinfo);
  1951. #ifdef LIBRAW_LIBRARY_BUILD
  1952. throw LIBRAW_EXCEPTION_DECODE_JPEG;
  1953. #else
  1954. longjmp (failure, 3);
  1955. #endif
  1956. }
  1957. buf = (*cinfo.mem->alloc_sarray)
  1958. ((j_common_ptr) &cinfo, JPOOL_IMAGE, width*3, 1);
  1959. while (cinfo.output_scanline < cinfo.output_height) {
  1960. row = cinfo.output_scanline * 2;
  1961. jpeg_read_scanlines (&cinfo, buf, 1);
  1962. pixel = (JSAMPLE (*)[3]) buf[0];
  1963. for (col=0; col < width; col+=2) {
  1964. BAYER(row+0,col+0) = pixel[col+0][1] << 1;
  1965. BAYER(row+1,col+1) = pixel[col+1][1] << 1;
  1966. BAYER(row+0,col+1) = pixel[col][0] + pixel[col+1][0];
  1967. BAYER(row+1,col+0) = pixel[col][2] + pixel[col+1][2];
  1968. }
  1969. }
  1970. jpeg_finish_decompress (&cinfo);
  1971. jpeg_destroy_decompress (&cinfo);
  1972. maximum = 0xff << 1;
  1973. }
  1974. #endif
  1975. void CLASS kodak_dc120_load_raw()
  1976. {
  1977. static const int mul[4] = { 162, 192, 187, 92 };
  1978. static const int add[4] = { 0, 636, 424, 212 };
  1979. uchar pixel[848];
  1980. int row, shift, col;
  1981. for (row=0; row < height; row++) {
  1982. if (fread (pixel, 1, 848, ifp) < 848) derror();
  1983. shift = row * mul[row & 3] + add[row & 3];
  1984. for (col=0; col < width; col++)
  1985. BAYER(row,col) = (ushort) pixel[(col + shift) % 848];
  1986. }
  1987. maximum = 0xff;
  1988. }
  1989. void CLASS eight_bit_load_raw()
  1990. {
  1991. uchar *pixel;
  1992. unsigned row, col, val, lblack=0;
  1993. pixel = (uchar *) calloc (raw_width, sizeof *pixel);
  1994. merror (pixel, "eight_bit_load_raw()");
  1995. fseek (ifp, top_margin*raw_width, SEEK_CUR);
  1996. for (row=0; row < height; row++) {
  1997. if (fread (pixel, 1, raw_width, ifp) < raw_width) derror();
  1998. for (col=0; col < raw_width; col++) {
  1999. val = curve[pixel[col]];
  2000. if ((unsigned) (col-left_margin) < width)
  2001. BAYER(row,col-left_margin) = val;
  2002. else lblack += val;
  2003. }
  2004. }
  2005. free (pixel);
  2006. if (raw_width > width+1)
  2007. black = lblack / ((raw_width - width) * height);
  2008. if (!strncmp(model,"DC2",3))
  2009. black = 0;
  2010. maximum = curve[0xff];
  2011. }
  2012. void CLASS kodak_yrgb_load_raw()
  2013. {
  2014. uchar *pixel;
  2015. int row, col, y, cb, cr, rgb[3], c;
  2016. pixel = (uchar *) calloc (raw_width, 3*sizeof *pixel);
  2017. merror (pixel, "kodak_yrgb_load_raw()");
  2018. for (row=0; row < height; row++) {
  2019. if (~row & 1)
  2020. if (fread (pixel, raw_width, 3, ifp) < 3) derror();
  2021. for (col=0; col < raw_width; col++) {
  2022. y = pixel[width*2*(row & 1) + col];
  2023. cb = pixel[width + (col & -2)] - 128;
  2024. cr = pixel[width + (col & -2)+1] - 128;
  2025. rgb[1] = y-((cb + cr + 2) >> 2);
  2026. rgb[2] = rgb[1] + cb;
  2027. rgb[0] = rgb[1] + cr;
  2028. FORC3 image[row*width+col][c] = LIM(rgb[c],0,255);
  2029. }
  2030. }
  2031. free (pixel);
  2032. use_gamma = 0;
  2033. }
  2034. void CLASS kodak_262_load_raw()
  2035. {
  2036. static const uchar kodak_tree[2][26] =
  2037. { { 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 },
  2038. { 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 } };
  2039. struct decode *decode[2];
  2040. uchar *pixel;
  2041. int *strip, ns, i, row, col, chess, pi=0, pi1, pi2, pred, val;
  2042. init_decoder();
  2043. for (i=0; i < 2; i++) {
  2044. decode[i] = free_decode;
  2045. make_decoder (kodak_tree[i], 0);
  2046. }
  2047. ns = (raw_height+63) >> 5;
  2048. pixel = (uchar *) malloc (raw_width*32 + ns*4);
  2049. merror (pixel, "kodak_262_load_raw()");
  2050. strip = (int *) (pixel + raw_width*32);
  2051. order = 0x4d4d;
  2052. for (i=0; i < ns; i++)
  2053. strip[i] = get4();
  2054. for (row=0; row < raw_height; row++) {
  2055. if ((row & 31) == 0) {
  2056. fseek (ifp, strip[row >> 5], SEEK_SET);
  2057. getbits(-1);
  2058. pi = 0;
  2059. }
  2060. for (col=0; col < raw_width; col++) {
  2061. chess = (row + col) & 1;
  2062. pi1 = chess ? pi-2 : pi-raw_width-1;
  2063. pi2 = chess ? pi-2*raw_width : pi-raw_width+1;
  2064. if (col <= chess) pi1 = -1;
  2065. if (pi1 < 0) pi1 = pi2;
  2066. if (pi2 < 0) pi2 = pi1;
  2067. if (pi1 < 0 && col > 1) pi1 = pi2 = pi-2;
  2068. pred = (pi1 < 0) ? 0 : (pixel[pi1] + pixel[pi2]) >> 1;
  2069. pixel[pi] = val = pred + ljpeg_diff (decode[chess]);
  2070. if (val >> 8) derror();
  2071. val = curve[pixel[pi++]];
  2072. if ((unsigned) (col-left_margin) < width)
  2073. BAYER(row,col-left_margin) = val;
  2074. else
  2075. black += val;
  2076. }
  2077. }
  2078. free (pixel);
  2079. if (raw_width > width)
  2080. black /= (raw_width - width) * height;
  2081. }
  2082. int CLASS kodak_65000_decode (short *out, int bsize)
  2083. {
  2084. uchar c, blen[768];
  2085. ushort raw[6];
  2086. INT64 bitbuf=0;
  2087. int save, bits=0, i, j, len, diff;
  2088. save = ftell(ifp);
  2089. bsize = (bsize + 3) & -4;
  2090. for (i=0; i < bsize; i+=2) {
  2091. c = fgetc(ifp);
  2092. if ((blen[i ] = c & 15) > 12 ||
  2093. (blen[i+1] = c >> 4) > 12 ) {
  2094. fseek (ifp, save, SEEK_SET);
  2095. for (i=0; i < bsize; i+=8) {
  2096. read_shorts (raw, 6);
  2097. out[i ] = raw[0] >> 12 << 8 | raw[2] >> 12 << 4 | raw[4] >> 12;
  2098. out[i+1] = raw[1] >> 12 << 8 | raw[3] >> 12 << 4 | raw[5] >> 12;
  2099. for (j=0; j < 6; j++)
  2100. out[i+2+j] = raw[j] & 0xfff;
  2101. }
  2102. return 1;
  2103. }
  2104. }
  2105. if ((bsize & 7) == 4) {
  2106. bitbuf = fgetc(ifp) << 8;
  2107. bitbuf += fgetc(ifp);
  2108. bits = 16;
  2109. }
  2110. for (i=0; i < bsize; i++) {
  2111. len = blen[i];
  2112. if (bits < len) {
  2113. for (j=0; j < 32; j+=8)
  2114. bitbuf += (INT64) fgetc(ifp) << (bits+(j^8));
  2115. bits += 32;
  2116. }
  2117. diff = bitbuf & (0xffff >> (16-len));
  2118. bitbuf >>= len;
  2119. bits -= len;
  2120. if ((diff & (1 << (len-1))) == 0)
  2121. diff -= (1 << len) - 1;
  2122. out[i] = diff;
  2123. }
  2124. return 0;
  2125. }
  2126. void CLASS kodak_65000_load_raw()
  2127. {
  2128. short buf[256];
  2129. int row, col, len, pred[2], ret, i;
  2130. for (row=0; row < height; row++)
  2131. for (col=0; col < width; col+=256) {
  2132. pred[0] = pred[1] = 0;
  2133. len = MIN (256, width-col);
  2134. ret = kodak_65000_decode (buf, len);
  2135. for (i=0; i < len; i++)
  2136. if ((BAYER(row,col+i) = curve[ret ? buf[i] :
  2137. (pred[i & 1] += buf[i])]) >> 12) derror();
  2138. }
  2139. }
  2140. void CLASS kodak_ycbcr_load_raw()
  2141. {
  2142. short buf[384], *bp;
  2143. int row, col, len, c, i, j, k, y[2][2], cb, cr, rgb[3];
  2144. ushort *ip;
  2145. for (row=0; row < height; row+=2)
  2146. for (col=0; col < width; col+=128) {
  2147. len = MIN (128, width-col);
  2148. kodak_65000_decode (buf, len*3);
  2149. y[0][1] = y[1][1] = cb = cr = 0;
  2150. for (bp=buf, i=0; i < len; i+=2, bp+=2) {
  2151. cb += bp[4];
  2152. cr += bp[5];
  2153. rgb[1] = -((cb + cr + 2) >> 2);
  2154. rgb[2] = rgb[1] + cb;
  2155. rgb[0] = rgb[1] + cr;
  2156. for (j=0; j < 2; j++)
  2157. for (k=0; k < 2; k++) {
  2158. if ((y[j][k] = y[j][k^1] + *bp++) >> 10) derror();
  2159. ip = image[(row+j)*width + col+i+k];
  2160. FORC3 ip[c] = curve[LIM(y[j][k]+rgb[c], 0, 0xfff)];
  2161. }
  2162. }
  2163. }
  2164. }
  2165. void CLASS kodak_rgb_load_raw()
  2166. {
  2167. short buf[768], *bp;
  2168. int row, col, len, c, i, rgb[3];
  2169. ushort *ip=image[0];
  2170. for (row=0; row < height; row++)
  2171. for (col=0; col < width; col+=256) {
  2172. len = MIN (256, width-col);
  2173. kodak_65000_decode (buf, len*3);
  2174. memset (rgb, 0, sizeof rgb);
  2175. for (bp=buf, i=0; i < len; i++, ip+=4)
  2176. FORC3 if ((ip[c] = rgb[c] += *bp++) >> 12) derror();
  2177. }
  2178. }
  2179. void CLASS kodak_thumb_load_raw()
  2180. {
  2181. int row, col;
  2182. colors = thumb_misc >> 5;
  2183. for (row=0; row < height; row++)
  2184. for (col=0; col < width; col++)
  2185. read_shorts (image[row*width+col], colors);
  2186. maximum = (1 << (thumb_misc & 31)) - 1;
  2187. }
  2188. void CLASS sony_decrypt (unsigned *data, int len, int start, int key)
  2189. {
  2190. #ifndef LIBRAW_NOTHREADS
  2191. #define pad tls->sony_decrypt.pad
  2192. #define p tls->sony_decrypt.p
  2193. #else
  2194. static unsigned pad[128], p;
  2195. #endif
  2196. if (start) {
  2197. for (p=0; p < 4; p++)
  2198. pad[p] = key = key * 48828125 + 1;
  2199. pad[3] = pad[3] << 1 | (pad[0]^pad[2]) >> 31;
  2200. for (p=4; p < 127; p++)
  2201. pad[p] = (pad[p-4]^pad[p-2]) << 1 | (pad[p-3]^pad[p-1]) >> 31;
  2202. for (p=0; p < 127; p++)
  2203. pad[p] = htonl(pad[p]);
  2204. }
  2205. while (len--)
  2206. *data++ ^= pad[p++ & 127] = pad[(p+1) & 127] ^ pad[(p+65) & 127];
  2207. #ifndef LIBRAW_NOTHREADS
  2208. #undef pad
  2209. #undef p
  2210. #endif
  2211. }
  2212. void CLASS sony_load_raw()
  2213. {
  2214. uchar head[40];
  2215. ushort *pixel;
  2216. unsigned i, key, row, col;
  2217. fseek (ifp, 200896, SEEK_SET);
  2218. fseek (ifp, (unsigned) fgetc(ifp)*4 - 1, SEEK_CUR);
  2219. order = 0x4d4d;
  2220. key = get4();
  2221. fseek (ifp, 164600, SEEK_SET);
  2222. fread (head, 1, 40, ifp);
  2223. sony_decrypt ((unsigned int *) head, 10, 1, key);
  2224. for (i=26; i-- > 22; )
  2225. key = key << 8 | head[i];
  2226. fseek (ifp, data_offset, SEEK_SET);
  2227. pixel = (ushort *) calloc (raw_width, sizeof *pixel);
  2228. merror (pixel, "sony_load_raw()");
  2229. for (row=0; row < height; row++) {
  2230. if (fread (pixel, 2, raw_width, ifp) < raw_width) derror();
  2231. sony_decrypt ((unsigned int *) pixel, raw_width/2, !row, key);
  2232. for (col=9; col < left_margin; col++)
  2233. black += ntohs(pixel[col]);
  2234. for (col=0; col < width; col++)
  2235. if ((BAYER(row,col) = ntohs(pixel[col+left_margin])) >> 14)
  2236. derror();
  2237. }
  2238. free (pixel);
  2239. if (left_margin > 9)
  2240. black /= (left_margin-9) * height;
  2241. maximum = 0x3ff0;
  2242. }
  2243. void CLASS sony_arw_load_raw()
  2244. {
  2245. int col, row, len, diff, sum=0;
  2246. getbits(-1);
  2247. for (col = raw_width; col--; )
  2248. for (row=0; row < raw_height+1; row+=2) {
  2249. if (row == raw_height) row = 1;
  2250. len = 4 - getbits(2);
  2251. if (len == 3 && getbits(1)) len = 0;
  2252. if (len == 4)
  2253. while (len < 17 && !getbits(1)) len++;
  2254. diff = getbits(len);
  2255. if ((diff & (1 << (len-1))) == 0)
  2256. diff -= (1 << len) - 1;
  2257. if ((sum += diff) >> 12) derror();
  2258. if (row < height) BAYER(row,col) = sum;
  2259. }
  2260. }
  2261. void CLASS sony_arw2_load_raw()
  2262. {
  2263. uchar *data, *dp;
  2264. ushort pix[16];
  2265. int row, col, val, max, min, imax, imin, sh, bit, i;
  2266. data = (uchar *) malloc (raw_width*tiff_bps >> 3);
  2267. merror (data, "sony_arw2_load_raw()");
  2268. for (row=0; row < height; row++) {
  2269. fread (data, 1, raw_width*tiff_bps >> 3, ifp);
  2270. if (tiff_bps == 8) {
  2271. for (dp=data, col=0; col < width-30; dp+=16) {
  2272. max = 0x7ff & (val = sget4(dp));
  2273. min = 0x7ff & val >> 11;
  2274. imax = 0x0f & val >> 22;
  2275. imin = 0x0f & val >> 26;
  2276. for (sh=0; sh < 4 && 0x80 << sh <= max-min; sh++);
  2277. for (bit=30, i=0; i < 16; i++)
  2278. if (i == imax) pix[i] = max;
  2279. else if (i == imin) pix[i] = min;
  2280. else {
  2281. pix[i] = ((sget2(dp+(bit >> 3)) >> (bit & 7) & 0x7f) << sh) + min;
  2282. if (pix[i] > 0x7ff) pix[i] = 0x7ff;
  2283. bit += 7;
  2284. }
  2285. for (i=0; i < 16; i++, col+=2)
  2286. BAYER(row,col) = curve[pix[i] << 1] >> 1;
  2287. col -= col & 1 ? 1:31;
  2288. }
  2289. } else if (tiff_bps == 12)
  2290. for (dp=data, col=0; col < width; dp+=3, col+=2) {
  2291. BAYER(row,col) = ((dp[1] << 8 | dp[0]) & 0xfff) << 1;
  2292. BAYER(row,col+1) = (dp[2] << 4 | dp[1] >> 4) << 1;
  2293. }
  2294. }
  2295. free (data);
  2296. }
  2297. #define HOLE(row) ((holes >> (((row) - raw_height) & 7)) & 1)
  2298. /* Kudos to Rich Taylor for figuring out SMaL's compression algorithm. */
  2299. void CLASS smal_decode_segment (unsigned seg[2][2], int holes)
  2300. {
  2301. uchar hist[3][13] = {
  2302. { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
  2303. { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
  2304. { 3, 3, 0, 0, 63, 47, 31, 15, 0 } };
  2305. int low, high=0xff, carry=0, nbits=8;
  2306. int s, count, bin, next, i, sym[3];
  2307. uchar diff, pred[]={0,0};
  2308. ushort data=0, range=0;
  2309. unsigned pix, row, col;
  2310. fseek (ifp, seg[0][1]+1, SEEK_SET);
  2311. getbits(-1);
  2312. for (pix=seg[0][0]; pix < seg[1][0]; pix++) {
  2313. for (s=0; s < 3; s++) {
  2314. data = data << nbits | getbits(nbits);
  2315. if (carry < 0)
  2316. carry = (nbits += carry+1) < 1 ? nbits-1 : 0;
  2317. while (--nbits >= 0)
  2318. if ((data >> nbits & 0xff) == 0xff) break;
  2319. if (nbits > 0)
  2320. data = ((data & ((1 << (nbits-1)) - 1)) << 1) |
  2321. ((data + (((data & (1 << (nbits-1)))) << 1)) & (-1 << nbits));
  2322. if (nbits >= 0) {
  2323. data += getbits(1);
  2324. carry = nbits - 8;
  2325. }
  2326. count = ((((data-range+1) & 0xffff) << 2) - 1) / (high >> 4);
  2327. for (bin=0; hist[s][bin+5] > count; bin++);
  2328. low = hist[s][bin+5] * (high >> 4) >> 2;
  2329. if (bin) high = hist[s][bin+4] * (high >> 4) >> 2;
  2330. high -= low;
  2331. for (nbits=0; high << nbits < 128; nbits++);
  2332. range = (range+low) << nbits;
  2333. high <<= nbits;
  2334. next = hist[s][1];
  2335. if (++hist[s][2] > hist[s][3]) {
  2336. next = (next+1) & hist[s][0];
  2337. hist[s][3] = (hist[s][next+4] - hist[s][next+5]) >> 2;
  2338. hist[s][2] = 1;
  2339. }
  2340. if (hist[s][hist[s][1]+4] - hist[s][hist[s][1]+5] > 1) {
  2341. if (bin < hist[s][1])
  2342. for (i=bin; i < hist[s][1]; i++) hist[s][i+5]--;
  2343. else if (next <= bin)
  2344. for (i=hist[s][1]; i < bin; i++) hist[s][i+5]++;
  2345. }
  2346. hist[s][1] = next;
  2347. sym[s] = bin;
  2348. }
  2349. diff = sym[2] << 5 | sym[1] << 2 | (sym[0] & 3);
  2350. if (sym[0] & 4)
  2351. diff = diff ? -diff : 0x80;
  2352. if (ftell(ifp) + 12 >= seg[1][1])
  2353. diff = 0;
  2354. pred[pix & 1] += diff;
  2355. row = pix / raw_width - top_margin;
  2356. col = pix % raw_width - left_margin;
  2357. if (row < height && col < width)
  2358. BAYER(row,col) = pred[pix & 1];
  2359. if (!(pix & 1) && HOLE(row)) pix += 2;
  2360. }
  2361. maximum = 0xff;
  2362. }
  2363. void CLASS smal_v6_load_raw()
  2364. {
  2365. unsigned seg[2][2];
  2366. fseek (ifp, 16, SEEK_SET);
  2367. seg[0][0] = 0;
  2368. seg[0][1] = get2();
  2369. seg[1][0] = raw_width * raw_height;
  2370. seg[1][1] = INT_MAX;
  2371. smal_decode_segment (seg, 0);
  2372. use_gamma = 0;
  2373. }
  2374. int CLASS median4 (int *p)
  2375. {
  2376. int min, max, sum, i;
  2377. min = max = sum = p[0];
  2378. for (i=1; i < 4; i++) {
  2379. sum += p[i];
  2380. if (min > p[i]) min = p[i];
  2381. if (max < p[i]) max = p[i];
  2382. }
  2383. return (sum - min - max) >> 1;
  2384. }
  2385. void CLASS fill_holes (int holes)
  2386. {
  2387. int row, col, val[4];
  2388. for (row=2; row < height-2; row++) {
  2389. if (!HOLE(row)) continue;
  2390. for (col=1; col < width-1; col+=4) {
  2391. val[0] = BAYER(row-1,col-1);
  2392. val[1] = BAYER(row-1,col+1);
  2393. val[2] = BAYER(row+1,col-1);
  2394. val[3] = BAYER(row+1,col+1);
  2395. BAYER(row,col) = median4(val);
  2396. }
  2397. for (col=2; col < width-2; col+=4)
  2398. if (HOLE(row-2) || HOLE(row+2))
  2399. BAYER(row,col) = (BAYER(row,col-2) + BAYER(row,col+2)) >> 1;
  2400. else {
  2401. val[0] = BAYER(row,col-2);
  2402. val[1] = BAYER(row,col+2);
  2403. val[2] = BAYER(row-2,col);
  2404. val[3] = BAYER(row+2,col);
  2405. BAYER(row,col) = median4(val);
  2406. }
  2407. }
  2408. }
  2409. void CLASS smal_v9_load_raw()
  2410. {
  2411. unsigned seg[256][2], offset, nseg, holes, i;
  2412. fseek (ifp, 67, SEEK_SET);
  2413. offset = get4();
  2414. nseg = fgetc(ifp);
  2415. fseek (ifp, offset, SEEK_SET);
  2416. for (i=0; i < nseg*2; i++)
  2417. seg[0][i] = get4() + data_offset*(i & 1);
  2418. fseek (ifp, 78, SEEK_SET);
  2419. holes = fgetc(ifp);
  2420. fseek (ifp, 88, SEEK_SET);
  2421. seg[nseg][0] = raw_height * raw_width;
  2422. seg[nseg][1] = get4() + data_offset;
  2423. for (i=0; i < nseg; i++)
  2424. smal_decode_segment (seg+i, holes);
  2425. if (holes) fill_holes (holes);
  2426. }
  2427. #line 3772 "dcraw/dcraw.c"
  2428. void CLASS pseudoinverse (double (*in)[3], double (*out)[3], int size)
  2429. {
  2430. double work[3][6], num;
  2431. int i, j, k;
  2432. for (i=0; i < 3; i++) {
  2433. for (j=0; j < 6; j++)
  2434. work[i][j] = j == i+3;
  2435. for (j=0; j < 3; j++)
  2436. for (k=0; k < size; k++)
  2437. work[i][j] += in[k][i] * in[k][j];
  2438. }
  2439. for (i=0; i < 3; i++) {
  2440. num = work[i][i];
  2441. for (j=0; j < 6; j++)
  2442. work[i][j] /= num;
  2443. for (k=0; k < 3; k++) {
  2444. if (k==i) continue;
  2445. num = work[k][i];
  2446. for (j=0; j < 6; j++)
  2447. work[k][j] -= work[i][j] * num;
  2448. }
  2449. }
  2450. for (i=0; i < size; i++)
  2451. for (j=0; j < 3; j++)
  2452. for (out[i][j]=k=0; k < 3; k++)
  2453. out[i][j] += work[j][k+3] * in[i][k];
  2454. }
  2455. void CLASS cam_xyz_coeff (double cam_xyz[4][3])
  2456. {
  2457. double cam_rgb[4][3], inverse[4][3], num;
  2458. int i, j, k;
  2459. for (i=0; i < colors; i++) /* Multiply out XYZ colorspace */
  2460. for (j=0; j < 3; j++)
  2461. for (cam_rgb[i][j] = k=0; k < 3; k++)
  2462. cam_rgb[i][j] += cam_xyz[i][k] * xyz_rgb[k][j];
  2463. for (i=0; i < colors; i++) { /* Normalize cam_rgb so that */
  2464. for (num=j=0; j < 3; j++) /* cam_rgb * (1,1,1) is (1,1,1,1) */
  2465. num += cam_rgb[i][j];
  2466. for (j=0; j < 3; j++)
  2467. cam_rgb[i][j] /= num;
  2468. pre_mul[i] = 1 / num;
  2469. }
  2470. pseudoinverse (cam_rgb, inverse, colors);
  2471. for (raw_color = i=0; i < 3; i++)
  2472. for (j=0; j < colors; j++)
  2473. rgb_cam[i][j] = inverse[j][i];
  2474. }
  2475. #ifdef COLORCHECK
  2476. void CLASS colorcheck()
  2477. {
  2478. #define NSQ 24
  2479. // Coordinates of the GretagMacbeth ColorChecker squares
  2480. // width, height, 1st_column, 1st_row
  2481. static const int cut[NSQ][4] = {
  2482. { 241, 231, 234, 274 },
  2483. { 251, 235, 534, 274 },
  2484. { 255, 239, 838, 272 },
  2485. { 255, 240, 1146, 274 },
  2486. { 251, 237, 1452, 278 },
  2487. { 243, 238, 1758, 288 },
  2488. { 253, 253, 218, 558 },
  2489. { 255, 249, 524, 562 },
  2490. { 261, 253, 830, 562 },
  2491. { 260, 255, 1144, 564 },
  2492. { 261, 255, 1450, 566 },
  2493. { 247, 247, 1764, 576 },
  2494. { 255, 251, 212, 862 },
  2495. { 259, 259, 518, 862 },
  2496. { 263, 261, 826, 864 },
  2497. { 265, 263, 1138, 866 },
  2498. { 265, 257, 1450, 872 },
  2499. { 257, 255, 1762, 874 },
  2500. { 257, 253, 212, 1164 },
  2501. { 262, 251, 516, 1172 },
  2502. { 263, 257, 826, 1172 },
  2503. { 263, 255, 1136, 1176 },
  2504. { 255, 252, 1452, 1182 },
  2505. { 257, 253, 1760, 1180 } };
  2506. // ColorChecker Chart under 6500-kelvin illumination
  2507. static const double gmb_xyY[NSQ][3] = {
  2508. { 0.400, 0.350, 10.1 }, // Dark Skin
  2509. { 0.377, 0.345, 35.8 }, // Light Skin
  2510. { 0.247, 0.251, 19.3 }, // Blue Sky
  2511. { 0.337, 0.422, 13.3 }, // Foliage
  2512. { 0.265, 0.240, 24.3 }, // Blue Flower
  2513. { 0.261, 0.343, 43.1 }, // Bluish Green
  2514. { 0.506, 0.407, 30.1 }, // Orange
  2515. { 0.211, 0.175, 12.0 }, // Purplish Blue
  2516. { 0.453, 0.306, 19.8 }, // Moderate Red
  2517. { 0.285, 0.202, 6.6 }, // Purple
  2518. { 0.380, 0.489, 44.3 }, // Yellow Green
  2519. { 0.473, 0.438, 43.1 }, // Orange Yellow
  2520. { 0.187, 0.129, 6.1 }, // Blue
  2521. { 0.305, 0.478, 23.4 }, // Green
  2522. { 0.539, 0.313, 12.0 }, // Red
  2523. { 0.448, 0.470, 59.1 }, // Yellow
  2524. { 0.364, 0.233, 19.8 }, // Magenta
  2525. { 0.196, 0.252, 19.8 }, // Cyan
  2526. { 0.310, 0.316, 90.0 }, // White
  2527. { 0.310, 0.316, 59.1 }, // Neutral 8
  2528. { 0.310, 0.316, 36.2 }, // Neutral 6.5
  2529. { 0.310, 0.316, 19.8 }, // Neutral 5
  2530. { 0.310, 0.316, 9.0 }, // Neutral 3.5
  2531. { 0.310, 0.316, 3.1 } }; // Black
  2532. double gmb_cam[NSQ][4], gmb_xyz[NSQ][3];
  2533. double inverse[NSQ][3], cam_xyz[4][3], num;
  2534. int c, i, j, k, sq, row, col, count[4];
  2535. memset (gmb_cam, 0, sizeof gmb_cam);
  2536. for (sq=0; sq < NSQ; sq++) {
  2537. FORCC count[c] = 0;
  2538. for (row=cut[sq][3]; row < cut[sq][3]+cut[sq][1]; row++)
  2539. for (col=cut[sq][2]; col < cut[sq][2]+cut[sq][0]; col++) {
  2540. c = FC(row,col);
  2541. if (c >= colors) c -= 2;
  2542. gmb_cam[sq][c] += BAYER(row,col);
  2543. count[c]++;
  2544. }
  2545. FORCC gmb_cam[sq][c] = gmb_cam[sq][c]/count[c] - black;
  2546. gmb_xyz[sq][0] = gmb_xyY[sq][2] * gmb_xyY[sq][0] / gmb_xyY[sq][1];
  2547. gmb_xyz[sq][1] = gmb_xyY[sq][2];
  2548. gmb_xyz[sq][2] = gmb_xyY[sq][2] *
  2549. (1 - gmb_xyY[sq][0] - gmb_xyY[sq][1]) / gmb_xyY[sq][1];
  2550. }
  2551. pseudoinverse (gmb_xyz, inverse, NSQ);
  2552. for (i=0; i < colors; i++)
  2553. for (j=0; j < 3; j++)
  2554. for (cam_xyz[i][j] = k=0; k < NSQ; k++)
  2555. cam_xyz[i][j] += gmb_cam[k][i] * inverse[k][j];
  2556. cam_xyz_coeff (cam_xyz);
  2557. #ifdef DCRAW_VERBOSE
  2558. if (verbose) {
  2559. printf (" { \"%s %s\", %d,\n\t{", make, model, black);
  2560. num = 10000 / (cam_xyz[1][0] + cam_xyz[1][1] + cam_xyz[1][2]);
  2561. FORCC for (j=0; j < 3; j++)
  2562. printf ("%c%d", (c | j) ? ',':' ', (int) (cam_xyz[c][j] * num + 0.5));
  2563. puts (" } },");
  2564. }
  2565. #endif
  2566. #undef NSQ
  2567. }
  2568. #endif
  2569. void CLASS hat_transform (float *temp, float *base, int st, int size, int sc)
  2570. {
  2571. int i;
  2572. for (i=0; i < sc; i++)
  2573. temp[i] = 2*base[st*i] + base[st*(sc-i)] + base[st*(i+sc)];
  2574. for (; i+sc < size; i++)
  2575. temp[i] = 2*base[st*i] + base[st*(i-sc)] + base[st*(i+sc)];
  2576. for (; i < size; i++)
  2577. temp[i] = 2*base[st*i] + base[st*(i-sc)] + base[st*(2*size-2-(i+sc))];
  2578. }
  2579. void CLASS wavelet_denoise()
  2580. {
  2581. float *fimg=0, *temp, thold, mul[2], avg, diff;
  2582. int scale=1, size, lev, hpass, lpass, row, col, nc, c, i, wlast;
  2583. ushort *window[4];
  2584. static const float noise[] =
  2585. { 0.8002,0.2735,0.1202,0.0585,0.0291,0.0152,0.0080,0.0044 };
  2586. #ifdef DCRAW_VERBOSE
  2587. if (verbose) fprintf (stderr,_("Wavelet denoising...\n"));
  2588. #endif
  2589. while (maximum << scale < 0x10000) scale++;
  2590. maximum <<= --scale;
  2591. black <<= scale;
  2592. if ((size = iheight*iwidth) < 0x15550000)
  2593. fimg = (float *) malloc ((size*3 + iheight + iwidth) * sizeof *fimg);
  2594. merror (fimg, "wavelet_denoise()");
  2595. temp = fimg + size*3;
  2596. if ((nc = colors) == 3 && filters) nc++;
  2597. FORC(nc) { /* denoise R,G1,B,G3 individually */
  2598. for (i=0; i < size; i++)
  2599. fimg[i] = 256 * sqrt((double)(image[i][c] << scale));
  2600. for (hpass=lev=0; lev < 5; lev++) {
  2601. lpass = size*((lev & 1)+1);
  2602. for (row=0; row < iheight; row++) {
  2603. hat_transform (temp, fimg+hpass+row*iwidth, 1, iwidth, 1 << lev);
  2604. for (col=0; col < iwidth; col++)
  2605. fimg[lpass + row*iwidth + col] = temp[col] * 0.25;
  2606. }
  2607. for (col=0; col < iwidth; col++) {
  2608. hat_transform (temp, fimg+lpass+col, iwidth, iheight, 1 << lev);
  2609. for (row=0; row < iheight; row++)
  2610. fimg[lpass + row*iwidth + col] = temp[row] * 0.25;
  2611. }
  2612. thold = threshold * noise[lev];
  2613. for (i=0; i < size; i++) {
  2614. fimg[hpass+i] -= fimg[lpass+i];
  2615. if (fimg[hpass+i] < -thold) fimg[hpass+i] += thold;
  2616. else if (fimg[hpass+i] > thold) fimg[hpass+i] -= thold;
  2617. else fimg[hpass+i] = 0;
  2618. if (hpass) fimg[i] += fimg[hpass+i];
  2619. }
  2620. hpass = lpass;
  2621. }
  2622. for (i=0; i < size; i++)
  2623. image[i][c] = CLIP(SQR(fimg[i]+fimg[lpass+i])/0x10000);
  2624. }
  2625. if (filters && colors == 3) { /* pull G1 and G3 closer together */
  2626. for (row=0; row < 2; row++)
  2627. mul[row] = 0.125 * pre_mul[FC(row+1,0) | 1] / pre_mul[FC(row,0) | 1];
  2628. for (i=0; i < 4; i++)
  2629. window[i] = (ushort *) fimg + width*i;
  2630. for (wlast=-1, row=1; row < height-1; row++) {
  2631. while (wlast < row+1) {
  2632. for (wlast++, i=0; i < 4; i++)
  2633. window[(i+3) & 3] = window[i];
  2634. for (col = FC(wlast,1) & 1; col < width; col+=2)
  2635. window[2][col] = BAYER(wlast,col);
  2636. }
  2637. thold = threshold/512;
  2638. for (col = (FC(row,0) & 1)+1; col < width-1; col+=2) {
  2639. avg = ( window[0][col-1] + window[0][col+1] +
  2640. window[2][col-1] + window[2][col+1] - black*4 )
  2641. * mul[row & 1] + (window[1][col] - black) * 0.5 + black;
  2642. avg = avg < 0 ? 0 : sqrt(avg);
  2643. diff = sqrt((double)(BAYER(row,col))) - avg;
  2644. if (diff < -thold) diff += thold;
  2645. else if (diff > thold) diff -= thold;
  2646. else diff = 0;
  2647. BAYER(row,col) = CLIP(SQR(avg+diff) + 0.5);
  2648. }
  2649. }
  2650. }
  2651. free (fimg);
  2652. }
  2653. void CLASS scale_colors()
  2654. {
  2655. unsigned bottom, right, size, row, col, ur, uc, i, x, y, c, sum[8];
  2656. int val, dark, sat;
  2657. double dsum[8], dmin, dmax;
  2658. float scale_mul[4], fr, fc;
  2659. ushort *img=0, *pix;
  2660. #ifdef LIBRAW_LIBRARY_BUILD
  2661. RUN_CALLBACK(LIBRAW_PROGRESS_SCALE_COLORS,0,2);
  2662. #endif
  2663. if (user_mul[0])
  2664. memcpy (pre_mul, user_mul, sizeof pre_mul);
  2665. if (use_auto_wb || (use_camera_wb && cam_mul[0] == -1)) {
  2666. memset (dsum, 0, sizeof dsum);
  2667. bottom = MIN (greybox[1]+greybox[3], height);
  2668. right = MIN (greybox[0]+greybox[2], width);
  2669. for (row=greybox[1]; row < bottom; row += 8)
  2670. for (col=greybox[0]; col < right; col += 8) {
  2671. memset (sum, 0, sizeof sum);
  2672. for (y=row; y < row+8 && y < bottom; y++)
  2673. for (x=col; x < col+8 && x < right; x++)
  2674. FORC4 {
  2675. if (filters) {
  2676. c = FC(y,x);
  2677. val = BAYER(y,x);
  2678. } else
  2679. val = image[y*width+x][c];
  2680. if (val > maximum-25) goto skip_block;
  2681. if ((val -= black) < 0) val = 0;
  2682. sum[c] += val;
  2683. sum[c+4]++;
  2684. if (filters) break;
  2685. }
  2686. FORC(8) dsum[c] += sum[c];
  2687. skip_block: ;
  2688. }
  2689. FORC4 if (dsum[c]) pre_mul[c] = dsum[c+4] / dsum[c];
  2690. }
  2691. if (use_camera_wb && cam_mul[0] != -1) {
  2692. memset (sum, 0, sizeof sum);
  2693. for (row=0; row < 8; row++)
  2694. for (col=0; col < 8; col++) {
  2695. c = FC(row,col);
  2696. if ((val = white[row][col] - black) > 0)
  2697. sum[c] += val;
  2698. sum[c+4]++;
  2699. }
  2700. if (sum[0] && sum[1] && sum[2] && sum[3])
  2701. {
  2702. FORC4 pre_mul[c] = (float) sum[c+4] / sum[c];
  2703. }
  2704. else if (cam_mul[0] && cam_mul[2])
  2705. {
  2706. memcpy (pre_mul, cam_mul, sizeof pre_mul);
  2707. }
  2708. else
  2709. {
  2710. #ifdef LIBRAW_LIBRARY_BUILD
  2711. imgdata.process_warnings |= LIBRAW_WARN_BAD_CAMERA_WB;
  2712. #endif
  2713. #ifdef DCRAW_VERBOSE
  2714. fprintf (stderr,_("%s: Cannot use camera white balance.\n"), ifname);
  2715. #endif
  2716. }
  2717. }
  2718. if (pre_mul[3] == 0) pre_mul[3] = colors < 4 ? pre_mul[1] : 1;
  2719. dark = black;
  2720. sat = maximum;
  2721. if (threshold) wavelet_denoise();
  2722. maximum -= black;
  2723. for (dmin=DBL_MAX, dmax=c=0; c < 4; c++) {
  2724. if (dmin > pre_mul[c])
  2725. dmin = pre_mul[c];
  2726. if (dmax < pre_mul[c])
  2727. dmax = pre_mul[c];
  2728. }
  2729. if (!highlight) dmax = dmin;
  2730. FORC4 scale_mul[c] = (pre_mul[c] /= dmax) * 65535.0 / maximum;
  2731. #ifdef DCRAW_VERBOSE
  2732. if (verbose) {
  2733. fprintf (stderr,
  2734. _("Scaling with darkness %d, saturation %d, and\nmultipliers"), dark, sat);
  2735. FORC4 fprintf (stderr, " %f", pre_mul[c]);
  2736. fputc ('\n', stderr);
  2737. }
  2738. #endif
  2739. size = iheight*iwidth;
  2740. for (i=0; i < size*4; i++) {
  2741. val = image[0][i];
  2742. if (!val) continue;
  2743. val -= black;
  2744. val *= scale_mul[i & 3];
  2745. image[0][i] = CLIP(val);
  2746. }
  2747. if ((aber[0] != 1 || aber[2] != 1) && colors == 3) {
  2748. #ifdef DCRAW_VERBOSE
  2749. if (verbose)
  2750. fprintf (stderr,_("Correcting chromatic aberration...\n"));
  2751. #endif
  2752. for (c=0; c < 4; c+=2) {
  2753. if (aber[c] == 1) continue;
  2754. img = (ushort *) malloc (size * sizeof *img);
  2755. merror (img, "scale_colors()");
  2756. for (i=0; i < size; i++)
  2757. img[i] = image[i][c];
  2758. for (row=0; row < iheight; row++) {
  2759. ur = fr = (row - iheight*0.5) * aber[c] + iheight*0.5;
  2760. if (ur > iheight-2) continue;
  2761. fr -= ur;
  2762. for (col=0; col < iwidth; col++) {
  2763. uc = fc = (col - iwidth*0.5) * aber[c] + iwidth*0.5;
  2764. if (uc > iwidth-2) continue;
  2765. fc -= uc;
  2766. pix = img + ur*iwidth + uc;
  2767. image[row*iwidth+col][c] =
  2768. (pix[ 0]*(1-fc) + pix[ 1]*fc) * (1-fr) +
  2769. (pix[iwidth]*(1-fc) + pix[iwidth+1]*fc) * fr;
  2770. }
  2771. }
  2772. free(img);
  2773. }
  2774. }
  2775. #ifdef LIBRAW_LIBRARY_BUILD
  2776. RUN_CALLBACK(LIBRAW_PROGRESS_SCALE_COLORS,1,2);
  2777. #endif
  2778. }
  2779. void CLASS pre_interpolate()
  2780. {
  2781. ushort (*img)[4];
  2782. int row, col, c;
  2783. #ifdef LIBRAW_LIBRARY_BUILD
  2784. RUN_CALLBACK(LIBRAW_PROGRESS_PRE_INTERPOLATE,0,2);
  2785. #endif
  2786. if (shrink) {
  2787. if (half_size) {
  2788. height = iheight;
  2789. width = iwidth;
  2790. } else {
  2791. img = (ushort (*)[4]) calloc (height*width, sizeof *img);
  2792. merror (img, "pre_interpolate()");
  2793. for (row=0; row < height; row++)
  2794. for (col=0; col < width; col++) {
  2795. c = fc(row,col);
  2796. img[row*width+col][c] = image[(row >> 1)*iwidth+(col >> 1)][c];
  2797. }
  2798. free (image);
  2799. image = img;
  2800. shrink = 0;
  2801. }
  2802. }
  2803. if (filters && colors == 3) {
  2804. if ((mix_green = four_color_rgb)) colors++;
  2805. else {
  2806. for (row = FC(1,0) >> 1; row < height; row+=2)
  2807. for (col = FC(row,1) & 1; col < width; col+=2)
  2808. image[row*width+col][1] = image[row*width+col][3];
  2809. filters &= ~((filters & 0x55555555) << 1);
  2810. }
  2811. }
  2812. if (half_size) filters = 0;
  2813. #ifdef LIBRAW_LIBRARY_BUILD
  2814. RUN_CALLBACK(LIBRAW_PROGRESS_PRE_INTERPOLATE,1,2);
  2815. #endif
  2816. }
  2817. void CLASS border_interpolate (int border)
  2818. {
  2819. unsigned row, col, y, x, f, c, sum[8];
  2820. for (row=0; row < height; row++)
  2821. for (col=0; col < width; col++) {
  2822. if (col==border && row >= border && row < height-border)
  2823. col = width-border;
  2824. memset (sum, 0, sizeof sum);
  2825. for (y=row-1; y != row+2; y++)
  2826. for (x=col-1; x != col+2; x++)
  2827. if (y < height && x < width) {
  2828. f = fc(y,x);
  2829. sum[f] += image[y*width+x][f];
  2830. sum[f+4]++;
  2831. }
  2832. f = fc(row,col);
  2833. FORCC if (c != f && sum[c+4])
  2834. image[row*width+col][c] = sum[c] / sum[c+4];
  2835. }
  2836. }
  2837. void CLASS lin_interpolate()
  2838. {
  2839. int code[16][16][32], *ip, sum[4];
  2840. int c, i, x, y, row, col, shift, color;
  2841. ushort *pix;
  2842. #ifdef DCRAW_VERBOSE
  2843. if (verbose) fprintf (stderr,_("Bilinear interpolation...\n"));
  2844. #endif
  2845. #ifdef LIBRAW_LIBRARY_BUILD
  2846. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,0,3);
  2847. #endif
  2848. border_interpolate(1);
  2849. for (row=0; row < 16; row++)
  2850. for (col=0; col < 16; col++) {
  2851. ip = code[row][col];
  2852. memset (sum, 0, sizeof sum);
  2853. for (y=-1; y <= 1; y++)
  2854. for (x=-1; x <= 1; x++) {
  2855. shift = (y==0) + (x==0);
  2856. if (shift == 2) continue;
  2857. color = fc(row+y,col+x);
  2858. *ip++ = (width*y + x)*4 + color;
  2859. *ip++ = shift;
  2860. *ip++ = color;
  2861. sum[color] += 1 << shift;
  2862. }
  2863. FORCC
  2864. if (c != fc(row,col)) {
  2865. *ip++ = c;
  2866. *ip++ = 256 / sum[c];
  2867. }
  2868. }
  2869. #ifdef LIBRAW_LIBRARY_BUILD
  2870. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,1,3);
  2871. #endif
  2872. for (row=1; row < height-1; row++)
  2873. for (col=1; col < width-1; col++) {
  2874. pix = image[row*width+col];
  2875. ip = code[row & 15][col & 15];
  2876. memset (sum, 0, sizeof sum);
  2877. for (i=8; i--; ip+=3)
  2878. sum[ip[2]] += pix[ip[0]] << ip[1];
  2879. for (i=colors; --i; ip+=2)
  2880. pix[ip[0]] = sum[ip[0]] * ip[1] >> 8;
  2881. }
  2882. #ifdef LIBRAW_LIBRARY_BUILD
  2883. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,2,3);
  2884. #endif
  2885. }
  2886. /*
  2887. This algorithm is officially called:
  2888. "Interpolation using a Threshold-based variable number of gradients"
  2889. described in http://scien.stanford.edu/class/psych221/projects/99/tingchen/algodep/vargra.html
  2890. I've extended the basic idea to work with non-Bayer filter arrays.
  2891. Gradients are numbered clockwise from NW=0 to W=7.
  2892. */
  2893. void CLASS vng_interpolate()
  2894. {
  2895. static const signed char *cp, terms[] = {
  2896. -2,-2,+0,-1,0,0x01, -2,-2,+0,+0,1,0x01, -2,-1,-1,+0,0,0x01,
  2897. -2,-1,+0,-1,0,0x02, -2,-1,+0,+0,0,0x03, -2,-1,+0,+1,1,0x01,
  2898. -2,+0,+0,-1,0,0x06, -2,+0,+0,+0,1,0x02, -2,+0,+0,+1,0,0x03,
  2899. -2,+1,-1,+0,0,0x04, -2,+1,+0,-1,1,0x04, -2,+1,+0,+0,0,0x06,
  2900. -2,+1,+0,+1,0,0x02, -2,+2,+0,+0,1,0x04, -2,+2,+0,+1,0,0x04,
  2901. -1,-2,-1,+0,0,0x80, -1,-2,+0,-1,0,0x01, -1,-2,+1,-1,0,0x01,
  2902. -1,-2,+1,+0,1,0x01, -1,-1,-1,+1,0,0x88, -1,-1,+1,-2,0,0x40,
  2903. -1,-1,+1,-1,0,0x22, -1,-1,+1,+0,0,0x33, -1,-1,+1,+1,1,0x11,
  2904. -1,+0,-1,+2,0,0x08, -1,+0,+0,-1,0,0x44, -1,+0,+0,+1,0,0x11,
  2905. -1,+0,+1,-2,1,0x40, -1,+0,+1,-1,0,0x66, -1,+0,+1,+0,1,0x22,
  2906. -1,+0,+1,+1,0,0x33, -1,+0,+1,+2,1,0x10, -1,+1,+1,-1,1,0x44,
  2907. -1,+1,+1,+0,0,0x66, -1,+1,+1,+1,0,0x22, -1,+1,+1,+2,0,0x10,
  2908. -1,+2,+0,+1,0,0x04, -1,+2,+1,+0,1,0x04, -1,+2,+1,+1,0,0x04,
  2909. +0,-2,+0,+0,1,0x80, +0,-1,+0,+1,1,0x88, +0,-1,+1,-2,0,0x40,
  2910. +0,-1,+1,+0,0,0x11, +0,-1,+2,-2,0,0x40, +0,-1,+2,-1,0,0x20,
  2911. +0,-1,+2,+0,0,0x30, +0,-1,+2,+1,1,0x10, +0,+0,+0,+2,1,0x08,
  2912. +0,+0,+2,-2,1,0x40, +0,+0,+2,-1,0,0x60, +0,+0,+2,+0,1,0x20,
  2913. +0,+0,+2,+1,0,0x30, +0,+0,+2,+2,1,0x10, +0,+1,+1,+0,0,0x44,
  2914. +0,+1,+1,+2,0,0x10, +0,+1,+2,-1,1,0x40, +0,+1,+2,+0,0,0x60,
  2915. +0,+1,+2,+1,0,0x20, +0,+1,+2,+2,0,0x10, +1,-2,+1,+0,0,0x80,
  2916. +1,-1,+1,+1,0,0x88, +1,+0,+1,+2,0,0x08, +1,+0,+2,-1,0,0x40,
  2917. +1,+0,+2,+1,0,0x10
  2918. }, chood[] = { -1,-1, -1,0, -1,+1, 0,+1, +1,+1, +1,0, +1,-1, 0,-1 };
  2919. ushort (*brow[5])[4], *pix;
  2920. int prow=7, pcol=1, *ip, *code[16][16], gval[8], gmin, gmax, sum[4];
  2921. int row, col, x, y, x1, x2, y1, y2, t, weight, grads, color, diag;
  2922. int g, diff, thold, num, c;
  2923. lin_interpolate();
  2924. #ifdef DCRAW_VERBOSE
  2925. if (verbose) fprintf (stderr,_("VNG interpolation...\n"));
  2926. #endif
  2927. if (filters == 1) prow = pcol = 15;
  2928. ip = (int *) calloc ((prow+1)*(pcol+1), 1280);
  2929. merror (ip, "vng_interpolate()");
  2930. for (row=0; row <= prow; row++) /* Precalculate for VNG */
  2931. for (col=0; col <= pcol; col++) {
  2932. code[row][col] = ip;
  2933. for (cp=terms, t=0; t < 64; t++) {
  2934. y1 = *cp++; x1 = *cp++;
  2935. y2 = *cp++; x2 = *cp++;
  2936. weight = *cp++;
  2937. grads = *cp++;
  2938. color = fc(row+y1,col+x1);
  2939. if (fc(row+y2,col+x2) != color) continue;
  2940. diag = (fc(row,col+1) == color && fc(row+1,col) == color) ? 2:1;
  2941. if (abs(y1-y2) == diag && abs(x1-x2) == diag) continue;
  2942. *ip++ = (y1*width + x1)*4 + color;
  2943. *ip++ = (y2*width + x2)*4 + color;
  2944. *ip++ = weight;
  2945. for (g=0; g < 8; g++)
  2946. if (grads & 1<<g) *ip++ = g;
  2947. *ip++ = -1;
  2948. }
  2949. *ip++ = INT_MAX;
  2950. for (cp=chood, g=0; g < 8; g++) {
  2951. y = *cp++; x = *cp++;
  2952. *ip++ = (y*width + x) * 4;
  2953. color = fc(row,col);
  2954. if (fc(row+y,col+x) != color && fc(row+y*2,col+x*2) == color)
  2955. *ip++ = (y*width + x) * 8 + color;
  2956. else
  2957. *ip++ = 0;
  2958. }
  2959. }
  2960. brow[4] = (ushort (*)[4]) calloc (width*3, sizeof **brow);
  2961. merror (brow[4], "vng_interpolate()");
  2962. for (row=0; row < 3; row++)
  2963. brow[row] = brow[4] + row*width;
  2964. for (row=2; row < height-2; row++) { /* Do VNG interpolation */
  2965. #ifdef LIBRAW_LIBRARY_BUILD
  2966. if(!((row-2)%256))RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,(row-2)/256+1,((height-3)/256)+1);
  2967. #endif
  2968. for (col=2; col < width-2; col++) {
  2969. pix = image[row*width+col];
  2970. ip = code[row & prow][col & pcol];
  2971. memset (gval, 0, sizeof gval);
  2972. while ((g = ip[0]) != INT_MAX) { /* Calculate gradients */
  2973. diff = ABS(pix[g] - pix[ip[1]]) << ip[2];
  2974. gval[ip[3]] += diff;
  2975. ip += 5;
  2976. if ((g = ip[-1]) == -1) continue;
  2977. gval[g] += diff;
  2978. while ((g = *ip++) != -1)
  2979. gval[g] += diff;
  2980. }
  2981. ip++;
  2982. gmin = gmax = gval[0]; /* Choose a threshold */
  2983. for (g=1; g < 8; g++) {
  2984. if (gmin > gval[g]) gmin = gval[g];
  2985. if (gmax < gval[g]) gmax = gval[g];
  2986. }
  2987. if (gmax == 0) {
  2988. memcpy (brow[2][col], pix, sizeof *image);
  2989. continue;
  2990. }
  2991. thold = gmin + (gmax >> 1);
  2992. memset (sum, 0, sizeof sum);
  2993. color = fc(row,col);
  2994. for (num=g=0; g < 8; g++,ip+=2) { /* Average the neighbors */
  2995. if (gval[g] <= thold) {
  2996. FORCC
  2997. if (c == color && ip[1])
  2998. sum[c] += (pix[c] + pix[ip[1]]) >> 1;
  2999. else
  3000. sum[c] += pix[ip[0] + c];
  3001. num++;
  3002. }
  3003. }
  3004. FORCC { /* Save to buffer */
  3005. t = pix[color];
  3006. if (c != color)
  3007. t += (sum[c] - sum[color]) / num;
  3008. brow[2][col][c] = CLIP(t);
  3009. }
  3010. }
  3011. if (row > 3) /* Write buffer to image */
  3012. memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image);
  3013. for (g=0; g < 4; g++)
  3014. brow[(g-1) & 3] = brow[g];
  3015. }
  3016. memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image);
  3017. memcpy (image[(row-1)*width+2], brow[1]+2, (width-4)*sizeof *image);
  3018. free (brow[4]);
  3019. free (code[0][0]);
  3020. }
  3021. /*
  3022. Patterned Pixel Grouping Interpolation by Alain Desbiolles
  3023. */
  3024. void CLASS ppg_interpolate()
  3025. {
  3026. int dir[5] = { 1, width, -1, -width, 1 };
  3027. int row, col, diff[2], guess[2], c, d, i;
  3028. ushort (*pix)[4];
  3029. border_interpolate(3);
  3030. #ifdef DCRAW_VERBOSE
  3031. if (verbose) fprintf (stderr,_("PPG interpolation...\n"));
  3032. #endif
  3033. /* Fill in the green layer with gradients and pattern recognition: */
  3034. #ifdef LIBRAW_LIBRARY_BUILD
  3035. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,0,3);
  3036. #endif
  3037. for (row=3; row < height-3; row++)
  3038. for (col=3+(FC(row,3) & 1), c=FC(row,col); col < width-3; col+=2) {
  3039. pix = image + row*width+col;
  3040. for (i=0; (d=dir[i]) > 0; i++) {
  3041. guess[i] = (pix[-d][1] + pix[0][c] + pix[d][1]) * 2
  3042. - pix[-2*d][c] - pix[2*d][c];
  3043. diff[i] = ( ABS(pix[-2*d][c] - pix[ 0][c]) +
  3044. ABS(pix[ 2*d][c] - pix[ 0][c]) +
  3045. ABS(pix[ -d][1] - pix[ d][1]) ) * 3 +
  3046. ( ABS(pix[ 3*d][1] - pix[ d][1]) +
  3047. ABS(pix[-3*d][1] - pix[-d][1]) ) * 2;
  3048. }
  3049. d = dir[i = diff[0] > diff[1]];
  3050. pix[0][1] = ULIM(guess[i] >> 2, pix[d][1], pix[-d][1]);
  3051. }
  3052. /* Calculate red and blue for each green pixel: */
  3053. #ifdef LIBRAW_LIBRARY_BUILD
  3054. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,1,3);
  3055. #endif
  3056. for (row=1; row < height-1; row++)
  3057. for (col=1+(FC(row,2) & 1), c=FC(row,col+1); col < width-1; col+=2) {
  3058. pix = image + row*width+col;
  3059. for (i=0; (d=dir[i]) > 0; c=2-c, i++)
  3060. pix[0][c] = CLIP((pix[-d][c] + pix[d][c] + 2*pix[0][1]
  3061. - pix[-d][1] - pix[d][1]) >> 1);
  3062. }
  3063. /* Calculate blue for red pixels and vice versa: */
  3064. #ifdef LIBRAW_LIBRARY_BUILD
  3065. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,2,3);
  3066. #endif
  3067. for (row=1; row < height-1; row++)
  3068. for (col=1+(FC(row,1) & 1), c=2-FC(row,col); col < width-1; col+=2) {
  3069. pix = image + row*width+col;
  3070. for (i=0; (d=dir[i]+dir[i+1]) > 0; i++) {
  3071. diff[i] = ABS(pix[-d][c] - pix[d][c]) +
  3072. ABS(pix[-d][1] - pix[0][1]) +
  3073. ABS(pix[ d][1] - pix[0][1]);
  3074. guess[i] = pix[-d][c] + pix[d][c] + 2*pix[0][1]
  3075. - pix[-d][1] - pix[d][1];
  3076. }
  3077. if (diff[0] != diff[1])
  3078. pix[0][c] = CLIP(guess[diff[0] > diff[1]] >> 1);
  3079. else
  3080. pix[0][c] = CLIP((guess[0]+guess[1]) >> 2);
  3081. }
  3082. }
  3083. /*
  3084. Adaptive Homogeneity-Directed interpolation is based on
  3085. the work of Keigo Hirakawa, Thomas Parks, and Paul Lee.
  3086. */
  3087. #define TS 256 /* Tile Size */
  3088. void CLASS ahd_interpolate()
  3089. {
  3090. int i, j, k, top, left, row, col, tr, tc, c, d, val, hm[2];
  3091. ushort (*pix)[4], (*rix)[3];
  3092. static const int dir[4] = { -1, 1, -TS, TS };
  3093. unsigned ldiff[2][4], abdiff[2][4], leps, abeps;
  3094. float r, cbrt[0x10000], xyz[3], xyz_cam[3][4];
  3095. ushort (*rgb)[TS][TS][3];
  3096. short (*lab)[TS][TS][3], (*lix)[3];
  3097. char (*homo)[TS][TS], *buffer;
  3098. #ifdef DCRAW_VERBOSE
  3099. if (verbose) fprintf (stderr,_("AHD interpolation...\n"));
  3100. #endif
  3101. for (i=0; i < 0x10000; i++) {
  3102. r = i / 65535.0;
  3103. cbrt[i] = r > 0.008856 ? pow((double)r,1/3.0) : 7.787*r + 16/116.0;
  3104. }
  3105. for (i=0; i < 3; i++)
  3106. for (j=0; j < colors; j++)
  3107. for (xyz_cam[i][j] = k=0; k < 3; k++)
  3108. xyz_cam[i][j] += xyz_rgb[i][k] * rgb_cam[k][j] / d65_white[i];
  3109. border_interpolate(5);
  3110. buffer = (char *) malloc (26*TS*TS); /* 1664 kB */
  3111. merror (buffer, "ahd_interpolate()");
  3112. rgb = (ushort(*)[TS][TS][3]) buffer;
  3113. lab = (short (*)[TS][TS][3])(buffer + 12*TS*TS);
  3114. homo = (char (*)[TS][TS]) (buffer + 24*TS*TS);
  3115. for (top=2; top < height-5; top += TS-6)
  3116. {
  3117. #ifdef LIBRAW_LIBRARY_BUILD
  3118. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,(top-2)/(TS-6),(height-7)/(TS-6)+1);
  3119. #endif
  3120. for (left=2; left < width-5; left += TS-6) {
  3121. /* Interpolate green horizontally and vertically: */
  3122. for (row = top; row < top+TS && row < height-2; row++) {
  3123. col = left + (FC(row,left) & 1);
  3124. for (c = FC(row,col); col < left+TS && col < width-2; col+=2) {
  3125. pix = image + row*width+col;
  3126. val = ((pix[-1][1] + pix[0][c] + pix[1][1]) * 2
  3127. - pix[-2][c] - pix[2][c]) >> 2;
  3128. rgb[0][row-top][col-left][1] = ULIM(val,pix[-1][1],pix[1][1]);
  3129. val = ((pix[-width][1] + pix[0][c] + pix[width][1]) * 2
  3130. - pix[-2*width][c] - pix[2*width][c]) >> 2;
  3131. rgb[1][row-top][col-left][1] = ULIM(val,pix[-width][1],pix[width][1]);
  3132. }
  3133. }
  3134. /* Interpolate red and blue, and convert to CIELab: */
  3135. for (d=0; d < 2; d++)
  3136. for (row=top+1; row < top+TS-1 && row < height-3; row++)
  3137. for (col=left+1; col < left+TS-1 && col < width-3; col++) {
  3138. pix = image + row*width+col;
  3139. rix = &rgb[d][row-top][col-left];
  3140. lix = &lab[d][row-top][col-left];
  3141. if ((c = 2 - FC(row,col)) == 1) {
  3142. c = FC(row+1,col);
  3143. val = pix[0][1] + (( pix[-1][2-c] + pix[1][2-c]
  3144. - rix[-1][1] - rix[1][1] ) >> 1);
  3145. rix[0][2-c] = CLIP(val);
  3146. val = pix[0][1] + (( pix[-width][c] + pix[width][c]
  3147. - rix[-TS][1] - rix[TS][1] ) >> 1);
  3148. } else
  3149. val = rix[0][1] + (( pix[-width-1][c] + pix[-width+1][c]
  3150. + pix[+width-1][c] + pix[+width+1][c]
  3151. - rix[-TS-1][1] - rix[-TS+1][1]
  3152. - rix[+TS-1][1] - rix[+TS+1][1] + 1) >> 2);
  3153. rix[0][c] = CLIP(val);
  3154. c = FC(row,col);
  3155. rix[0][c] = pix[0][c];
  3156. xyz[0] = xyz[1] = xyz[2] = 0.5;
  3157. FORCC {
  3158. xyz[0] += xyz_cam[0][c] * rix[0][c];
  3159. xyz[1] += xyz_cam[1][c] * rix[0][c];
  3160. xyz[2] += xyz_cam[2][c] * rix[0][c];
  3161. }
  3162. xyz[0] = cbrt[CLIP((int) xyz[0])];
  3163. xyz[1] = cbrt[CLIP((int) xyz[1])];
  3164. xyz[2] = cbrt[CLIP((int) xyz[2])];
  3165. lix[0][0] = 64 * (116 * xyz[1] - 16);
  3166. lix[0][1] = 64 * 500 * (xyz[0] - xyz[1]);
  3167. lix[0][2] = 64 * 200 * (xyz[1] - xyz[2]);
  3168. }
  3169. /* Build homogeneity maps from the CIELab images: */
  3170. memset (homo, 0, 2*TS*TS);
  3171. for (row=top+2; row < top+TS-2 && row < height-4; row++) {
  3172. tr = row-top;
  3173. for (col=left+2; col < left+TS-2 && col < width-4; col++) {
  3174. tc = col-left;
  3175. for (d=0; d < 2; d++) {
  3176. lix = &lab[d][tr][tc];
  3177. for (i=0; i < 4; i++) {
  3178. ldiff[d][i] = ABS(lix[0][0]-lix[dir[i]][0]);
  3179. abdiff[d][i] = SQR(lix[0][1]-lix[dir[i]][1])
  3180. + SQR(lix[0][2]-lix[dir[i]][2]);
  3181. }
  3182. }
  3183. leps = MIN(MAX(ldiff[0][0],ldiff[0][1]),
  3184. MAX(ldiff[1][2],ldiff[1][3]));
  3185. abeps = MIN(MAX(abdiff[0][0],abdiff[0][1]),
  3186. MAX(abdiff[1][2],abdiff[1][3]));
  3187. for (d=0; d < 2; d++)
  3188. for (i=0; i < 4; i++)
  3189. if (ldiff[d][i] <= leps && abdiff[d][i] <= abeps)
  3190. homo[d][tr][tc]++;
  3191. }
  3192. }
  3193. /* Combine the most homogenous pixels for the final result: */
  3194. for (row=top+3; row < top+TS-3 && row < height-5; row++) {
  3195. tr = row-top;
  3196. for (col=left+3; col < left+TS-3 && col < width-5; col++) {
  3197. tc = col-left;
  3198. for (d=0; d < 2; d++)
  3199. for (hm[d]=0, i=tr-1; i <= tr+1; i++)
  3200. for (j=tc-1; j <= tc+1; j++)
  3201. hm[d] += homo[d][i][j];
  3202. if (hm[0] != hm[1])
  3203. FORC3 image[row*width+col][c] = rgb[hm[1] > hm[0]][tr][tc][c];
  3204. else
  3205. FORC3 image[row*width+col][c] =
  3206. (rgb[0][tr][tc][c] + rgb[1][tr][tc][c]) >> 1;
  3207. }
  3208. }
  3209. }
  3210. }
  3211. free (buffer);
  3212. }
  3213. #undef TS
  3214. void CLASS median_filter()
  3215. {
  3216. ushort (*pix)[4];
  3217. int pass, c, i, j, k, med[9];
  3218. static const uchar opt[] = /* Optimal 9-element median search */
  3219. { 1,2, 4,5, 7,8, 0,1, 3,4, 6,7, 1,2, 4,5, 7,8,
  3220. 0,3, 5,8, 4,7, 3,6, 1,4, 2,5, 4,7, 4,2, 6,4, 4,2 };
  3221. for (pass=1; pass <= med_passes; pass++) {
  3222. #ifdef LIBRAW_LIBRARY_BUILD
  3223. RUN_CALLBACK(LIBRAW_PROGRESS_MEDIAN_FILTER,pass-1,med_passes);
  3224. #endif
  3225. #ifdef DCRAW_VERBOSE
  3226. if (verbose)
  3227. fprintf (stderr,_("Median filter pass %d...\n"), pass);
  3228. #endif
  3229. for (c=0; c < 3; c+=2) {
  3230. for (pix = image; pix < image+width*height; pix++)
  3231. pix[0][3] = pix[0][c];
  3232. for (pix = image+width; pix < image+width*(height-1); pix++) {
  3233. if ((pix-image+1) % width < 2) continue;
  3234. for (k=0, i = -width; i <= width; i += width)
  3235. for (j = i-1; j <= i+1; j++)
  3236. med[k++] = pix[j][3] - pix[j][1];
  3237. for (i=0; i < sizeof opt; i+=2)
  3238. if (med[opt[i]] > med[opt[i+1]])
  3239. SWAP (med[opt[i]] , med[opt[i+1]]);
  3240. pix[0][c] = CLIP(med[4] + pix[0][1]);
  3241. }
  3242. }
  3243. }
  3244. }
  3245. void CLASS blend_highlights()
  3246. {
  3247. int clip=INT_MAX, row, col, c, i, j;
  3248. static const float trans[2][4][4] =
  3249. { { { 1,1,1 }, { 1.7320508,-1.7320508,0 }, { -1,-1,2 } },
  3250. { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } };
  3251. static const float itrans[2][4][4] =
  3252. { { { 1,0.8660254,-0.5 }, { 1,-0.8660254,-0.5 }, { 1,0,1 } },
  3253. { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } };
  3254. float cam[2][4], lab[2][4], sum[2], chratio;
  3255. if ((unsigned) (colors-3) > 1) return;
  3256. #ifdef DCRAW_VERBOSE
  3257. if (verbose) fprintf (stderr,_("Blending highlights...\n"));
  3258. #endif
  3259. FORCC if (clip > (i = 65535*pre_mul[c])) clip = i;
  3260. #ifdef LIBRAW_LIBRARY_BUILD
  3261. RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS,0,2);
  3262. #endif
  3263. for (row=0; row < height; row++)
  3264. for (col=0; col < width; col++) {
  3265. FORCC if (image[row*width+col][c] > clip) break;
  3266. if (c == colors) continue;
  3267. FORCC {
  3268. cam[0][c] = image[row*width+col][c];
  3269. cam[1][c] = MIN(cam[0][c],clip);
  3270. }
  3271. for (i=0; i < 2; i++) {
  3272. FORCC for (lab[i][c]=j=0; j < colors; j++)
  3273. lab[i][c] += trans[colors-3][c][j] * cam[i][j];
  3274. for (sum[i]=0,c=1; c < colors; c++)
  3275. sum[i] += SQR(lab[i][c]);
  3276. }
  3277. chratio = sqrt(sum[1]/sum[0]);
  3278. for (c=1; c < colors; c++)
  3279. lab[0][c] *= chratio;
  3280. FORCC for (cam[0][c]=j=0; j < colors; j++)
  3281. cam[0][c] += itrans[colors-3][c][j] * lab[0][j];
  3282. FORCC image[row*width+col][c] = cam[0][c] / colors;
  3283. }
  3284. #ifdef LIBRAW_LIBRARY_BUILD
  3285. RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS,1,2);
  3286. #endif
  3287. }
  3288. #define SCALE (4 >> shrink)
  3289. void CLASS recover_highlights()
  3290. {
  3291. float *map, sum, wgt, grow;
  3292. int hsat[4], count, spread, change, val, i;
  3293. unsigned high, wide, mrow, mcol, row, col, kc, c, d, y, x;
  3294. ushort *pixel;
  3295. static const signed char dir[8][2] =
  3296. { {-1,-1}, {-1,0}, {-1,1}, {0,1}, {1,1}, {1,0}, {1,-1}, {0,-1} };
  3297. #ifdef DCRAW_VERBOSE
  3298. if (verbose) fprintf (stderr,_("Rebuilding highlights...\n"));
  3299. #endif
  3300. grow = pow (2.0, 4.0-highlight);
  3301. FORCC hsat[c] = 32000 * pre_mul[c];
  3302. for (kc=0, c=1; c < colors; c++)
  3303. if (pre_mul[kc] < pre_mul[c]) kc = c;
  3304. high = height / SCALE;
  3305. wide = width / SCALE;
  3306. map = (float *) calloc (high*wide, sizeof *map);
  3307. merror (map, "recover_highlights()");
  3308. FORCC if (c != kc) {
  3309. #ifdef LIBRAW_LIBRARY_BUILD
  3310. RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS,c-1,colors-1);
  3311. #endif
  3312. memset (map, 0, high*wide*sizeof *map);
  3313. for (mrow=0; mrow < high; mrow++)
  3314. for (mcol=0; mcol < wide; mcol++) {
  3315. sum = wgt = count = 0;
  3316. for (row = mrow*SCALE; row < (mrow+1)*SCALE; row++)
  3317. for (col = mcol*SCALE; col < (mcol+1)*SCALE; col++) {
  3318. pixel = image[row*width+col];
  3319. if (pixel[c] / hsat[c] == 1 && pixel[kc] > 24000) {
  3320. sum += pixel[c];
  3321. wgt += pixel[kc];
  3322. count++;
  3323. }
  3324. }
  3325. if (count == SCALE*SCALE)
  3326. map[mrow*wide+mcol] = sum / wgt;
  3327. }
  3328. for (spread = 32/grow; spread--; ) {
  3329. for (mrow=0; mrow < high; mrow++)
  3330. for (mcol=0; mcol < wide; mcol++) {
  3331. if (map[mrow*wide+mcol]) continue;
  3332. sum = count = 0;
  3333. for (d=0; d < 8; d++) {
  3334. y = mrow + dir[d][0];
  3335. x = mcol + dir[d][1];
  3336. if (y < high && x < wide && map[y*wide+x] > 0) {
  3337. sum += (1 + (d & 1)) * map[y*wide+x];
  3338. count += 1 + (d & 1);
  3339. }
  3340. }
  3341. if (count > 3)
  3342. map[mrow*wide+mcol] = - (sum+grow) / (count+grow);
  3343. }
  3344. for (change=i=0; i < high*wide; i++)
  3345. if (map[i] < 0) {
  3346. map[i] = -map[i];
  3347. change = 1;
  3348. }
  3349. if (!change) break;
  3350. }
  3351. for (i=0; i < high*wide; i++)
  3352. if (map[i] == 0) map[i] = 1;
  3353. for (mrow=0; mrow < high; mrow++)
  3354. for (mcol=0; mcol < wide; mcol++) {
  3355. for (row = mrow*SCALE; row < (mrow+1)*SCALE; row++)
  3356. for (col = mcol*SCALE; col < (mcol+1)*SCALE; col++) {
  3357. pixel = image[row*width+col];
  3358. if (pixel[c] / hsat[c] > 1) {
  3359. val = pixel[kc] * map[mrow*wide+mcol];
  3360. if (pixel[c] < val) pixel[c] = CLIP(val);
  3361. }
  3362. }
  3363. }
  3364. }
  3365. free (map);
  3366. }
  3367. #undef SCALE
  3368. void CLASS tiff_get (unsigned base,
  3369. unsigned *tag, unsigned *type, unsigned *len, unsigned *save)
  3370. {
  3371. *tag = get2();
  3372. *type = get2();
  3373. *len = get4();
  3374. *save = ftell(ifp) + 4;
  3375. if (*len * ("11124811248488"[*type < 14 ? *type:0]-'0') > 4)
  3376. fseek (ifp, get4()+base, SEEK_SET);
  3377. }
  3378. void CLASS parse_thumb_note (int base, unsigned toff, unsigned tlen)
  3379. {
  3380. unsigned entries, tag, type, len, save;
  3381. entries = get2();
  3382. while (entries--) {
  3383. tiff_get (base, &tag, &type, &len, &save);
  3384. if (tag == toff) thumb_offset = get4()+base;
  3385. if (tag == tlen) thumb_length = get4();
  3386. fseek (ifp, save, SEEK_SET);
  3387. }
  3388. }
  3389. #line 4784 "dcraw/dcraw.c"
  3390. void CLASS parse_makernote (int base, int uptag)
  3391. {
  3392. static const uchar xlat[2][256] = {
  3393. { 0xc1,0xbf,0x6d,0x0d,0x59,0xc5,0x13,0x9d,0x83,0x61,0x6b,0x4f,0xc7,0x7f,0x3d,0x3d,
  3394. 0x53,0x59,0xe3,0xc7,0xe9,0x2f,0x95,0xa7,0x95,0x1f,0xdf,0x7f,0x2b,0x29,0xc7,0x0d,
  3395. 0xdf,0x07,0xef,0x71,0x89,0x3d,0x13,0x3d,0x3b,0x13,0xfb,0x0d,0x89,0xc1,0x65,0x1f,
  3396. 0xb3,0x0d,0x6b,0x29,0xe3,0xfb,0xef,0xa3,0x6b,0x47,0x7f,0x95,0x35,0xa7,0x47,0x4f,
  3397. 0xc7,0xf1,0x59,0x95,0x35,0x11,0x29,0x61,0xf1,0x3d,0xb3,0x2b,0x0d,0x43,0x89,0xc1,
  3398. 0x9d,0x9d,0x89,0x65,0xf1,0xe9,0xdf,0xbf,0x3d,0x7f,0x53,0x97,0xe5,0xe9,0x95,0x17,
  3399. 0x1d,0x3d,0x8b,0xfb,0xc7,0xe3,0x67,0xa7,0x07,0xf1,0x71,0xa7,0x53,0xb5,0x29,0x89,
  3400. 0xe5,0x2b,0xa7,0x17,0x29,0xe9,0x4f,0xc5,0x65,0x6d,0x6b,0xef,0x0d,0x89,0x49,0x2f,
  3401. 0xb3,0x43,0x53,0x65,0x1d,0x49,0xa3,0x13,0x89,0x59,0xef,0x6b,0xef,0x65,0x1d,0x0b,
  3402. 0x59,0x13,0xe3,0x4f,0x9d,0xb3,0x29,0x43,0x2b,0x07,0x1d,0x95,0x59,0x59,0x47,0xfb,
  3403. 0xe5,0xe9,0x61,0x47,0x2f,0x35,0x7f,0x17,0x7f,0xef,0x7f,0x95,0x95,0x71,0xd3,0xa3,
  3404. 0x0b,0x71,0xa3,0xad,0x0b,0x3b,0xb5,0xfb,0xa3,0xbf,0x4f,0x83,0x1d,0xad,0xe9,0x2f,
  3405. 0x71,0x65,0xa3,0xe5,0x07,0x35,0x3d,0x0d,0xb5,0xe9,0xe5,0x47,0x3b,0x9d,0xef,0x35,
  3406. 0xa3,0xbf,0xb3,0xdf,0x53,0xd3,0x97,0x53,0x49,0x71,0x07,0x35,0x61,0x71,0x2f,0x43,
  3407. 0x2f,0x11,0xdf,0x17,0x97,0xfb,0x95,0x3b,0x7f,0x6b,0xd3,0x25,0xbf,0xad,0xc7,0xc5,
  3408. 0xc5,0xb5,0x8b,0xef,0x2f,0xd3,0x07,0x6b,0x25,0x49,0x95,0x25,0x49,0x6d,0x71,0xc7 },
  3409. { 0xa7,0xbc,0xc9,0xad,0x91,0xdf,0x85,0xe5,0xd4,0x78,0xd5,0x17,0x46,0x7c,0x29,0x4c,
  3410. 0x4d,0x03,0xe9,0x25,0x68,0x11,0x86,0xb3,0xbd,0xf7,0x6f,0x61,0x22,0xa2,0x26,0x34,
  3411. 0x2a,0xbe,0x1e,0x46,0x14,0x68,0x9d,0x44,0x18,0xc2,0x40,0xf4,0x7e,0x5f,0x1b,0xad,
  3412. 0x0b,0x94,0xb6,0x67,0xb4,0x0b,0xe1,0xea,0x95,0x9c,0x66,0xdc,0xe7,0x5d,0x6c,0x05,
  3413. 0xda,0xd5,0xdf,0x7a,0xef,0xf6,0xdb,0x1f,0x82,0x4c,0xc0,0x68,0x47,0xa1,0xbd,0xee,
  3414. 0x39,0x50,0x56,0x4a,0xdd,0xdf,0xa5,0xf8,0xc6,0xda,0xca,0x90,0xca,0x01,0x42,0x9d,
  3415. 0x8b,0x0c,0x73,0x43,0x75,0x05,0x94,0xde,0x24,0xb3,0x80,0x34,0xe5,0x2c,0xdc,0x9b,
  3416. 0x3f,0xca,0x33,0x45,0xd0,0xdb,0x5f,0xf5,0x52,0xc3,0x21,0xda,0xe2,0x22,0x72,0x6b,
  3417. 0x3e,0xd0,0x5b,0xa8,0x87,0x8c,0x06,0x5d,0x0f,0xdd,0x09,0x19,0x93,0xd0,0xb9,0xfc,
  3418. 0x8b,0x0f,0x84,0x60,0x33,0x1c,0x9b,0x45,0xf1,0xf0,0xa3,0x94,0x3a,0x12,0x77,0x33,
  3419. 0x4d,0x44,0x78,0x28,0x3c,0x9e,0xfd,0x65,0x57,0x16,0x94,0x6b,0xfb,0x59,0xd0,0xc8,
  3420. 0x22,0x36,0xdb,0xd2,0x63,0x98,0x43,0xa1,0x04,0x87,0x86,0xf7,0xa6,0x26,0xbb,0xd6,
  3421. 0x59,0x4d,0xbf,0x6a,0x2e,0xaa,0x2b,0xef,0xe6,0x78,0xb6,0x4e,0xe0,0x2f,0xdc,0x7c,
  3422. 0xbe,0x57,0x19,0x32,0x7e,0x2a,0xd0,0xb8,0xba,0x29,0x00,0x3c,0x52,0x7d,0xa8,0x49,
  3423. 0x3b,0x2d,0xeb,0x25,0x49,0xfa,0xa3,0xaa,0x39,0xa7,0xc5,0xa7,0x50,0x11,0x36,0xfb,
  3424. 0xc6,0x67,0x4a,0xf5,0xa5,0x12,0x65,0x7e,0xb0,0xdf,0xaf,0x4e,0xb3,0x61,0x7f,0x2f } };
  3425. unsigned offset=0, entries, tag, type, len, save, c;
  3426. unsigned ver97=0, serial=0, i, wbi=0, wb[4]={0,0,0,0};
  3427. uchar buf97[324], ci, cj, ck;
  3428. short sorder=order;
  3429. char buf[10];
  3430. /*
  3431. The MakerNote might have its own TIFF header (possibly with
  3432. its own byte-order!), or it might just be a table.
  3433. */
  3434. fread (buf, 1, 10, ifp);
  3435. if (!strncmp (buf,"KDK" ,3) || /* these aren't TIFF tables */
  3436. !strncmp (buf,"VER" ,3) ||
  3437. !strncmp (buf,"IIII",4) ||
  3438. !strncmp (buf,"MMMM",4)) return;
  3439. if (!strncmp (buf,"KC" ,2) || /* Konica KD-400Z, KD-510Z */
  3440. !strncmp (buf,"MLY" ,3)) { /* Minolta DiMAGE G series */
  3441. order = 0x4d4d;
  3442. while ((i=ftell(ifp)) < data_offset && i < 16384) {
  3443. wb[0] = wb[2]; wb[2] = wb[1]; wb[1] = wb[3];
  3444. wb[3] = get2();
  3445. if (wb[1] == 256 && wb[3] == 256 &&
  3446. wb[0] > 256 && wb[0] < 640 && wb[2] > 256 && wb[2] < 640)
  3447. FORC4 cam_mul[c] = wb[c];
  3448. }
  3449. goto quit;
  3450. }
  3451. if (!strcmp (buf,"Nikon")) {
  3452. base = ftell(ifp);
  3453. order = get2();
  3454. if (get2() != 42) goto quit;
  3455. offset = get4();
  3456. fseek (ifp, offset-8, SEEK_CUR);
  3457. } else if (!strcmp (buf,"OLYMPUS")) {
  3458. base = ftell(ifp)-10;
  3459. fseek (ifp, -2, SEEK_CUR);
  3460. order = get2(); get2();
  3461. } else if (!strncmp (buf,"FUJIFILM",8) ||
  3462. !strncmp (buf,"SONY",4) ||
  3463. !strcmp (buf,"Panasonic")) {
  3464. order = 0x4949;
  3465. fseek (ifp, 2, SEEK_CUR);
  3466. } else if (!strcmp (buf,"OLYMP") ||
  3467. !strcmp (buf,"LEICA") ||
  3468. !strcmp (buf,"Ricoh") ||
  3469. !strcmp (buf,"EPSON"))
  3470. fseek (ifp, -2, SEEK_CUR);
  3471. else if (!strcmp (buf,"AOC") ||
  3472. !strcmp (buf,"QVC"))
  3473. fseek (ifp, -4, SEEK_CUR);
  3474. else fseek (ifp, -10, SEEK_CUR);
  3475. entries = get2();
  3476. if (entries > 1000) return;
  3477. while (entries--) {
  3478. tiff_get (base, &tag, &type, &len, &save);
  3479. tag |= uptag << 16;
  3480. if (tag == 2 && strstr(make,"NIKON"))
  3481. iso_speed = (get2(),get2());
  3482. if (tag == 4 && len > 26 && len < 35) {
  3483. if ((i=(get4(),get2())) != 0x7fff && !iso_speed)
  3484. iso_speed = 50 * pow (2.0, i/32.0 - 4);
  3485. if ((i=(get2(),get2())) != 0x7fff && !aperture)
  3486. aperture = pow (2.0, i/64.0);
  3487. if ((i=get2()) != 0xffff && !shutter)
  3488. shutter = pow (2.0, (short) i/-32.0);
  3489. wbi = (get2(),get2());
  3490. shot_order = (get2(),get2());
  3491. }
  3492. if (tag == 7 && type == 2 && len > 20)
  3493. fgets (model2, 64, ifp);
  3494. if (tag == 8 && type == 4)
  3495. shot_order = get4();
  3496. if (tag == 9 && !strcmp(make,"Canon"))
  3497. fread (artist, 64, 1, ifp);
  3498. if (tag == 0xc && len == 4) {
  3499. cam_mul[0] = getreal(type);
  3500. cam_mul[2] = getreal(type);
  3501. }
  3502. if (tag == 0x10 && type == 4)
  3503. unique_id = get4();
  3504. if (tag == 0x11 && is_raw && !strncmp(make,"NIKON",5)) {
  3505. fseek (ifp, get4()+base, SEEK_SET);
  3506. parse_tiff_ifd (base);
  3507. }
  3508. if (tag == 0x14 && len == 2560 && type == 7) {
  3509. fseek (ifp, 1248, SEEK_CUR);
  3510. goto get2_256;
  3511. }
  3512. if (tag == 0x15 && type == 2 && is_raw)
  3513. fread (model, 64, 1, ifp);
  3514. if (strstr(make,"PENTAX")) {
  3515. if (tag == 0x1b) tag = 0x1018;
  3516. if (tag == 0x1c) tag = 0x1017;
  3517. }
  3518. if (tag == 0x1d)
  3519. while ((c = fgetc(ifp)) && c != EOF)
  3520. serial = serial*10 + (isdigit(c) ? c - '0' : c % 10);
  3521. if (tag == 0x81 && type == 4) {
  3522. data_offset = get4();
  3523. fseek (ifp, data_offset + 41, SEEK_SET);
  3524. raw_height = get2() * 2;
  3525. raw_width = get2();
  3526. filters = 0x61616161;
  3527. }
  3528. if (tag == 0x29 && type == 1) {
  3529. c = wbi < 18 ? "012347800000005896"[wbi]-'0' : 0;
  3530. fseek (ifp, 8 + c*32, SEEK_CUR);
  3531. FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get4();
  3532. }
  3533. if ((tag == 0x81 && type == 7) ||
  3534. (tag == 0x100 && type == 7) ||
  3535. (tag == 0x280 && type == 1)) {
  3536. thumb_offset = ftell(ifp);
  3537. thumb_length = len;
  3538. }
  3539. if (tag == 0x88 && type == 4 && (thumb_offset = get4()))
  3540. thumb_offset += base;
  3541. if (tag == 0x89 && type == 4)
  3542. thumb_length = get4();
  3543. if (tag == 0x8c || tag == 0x96)
  3544. meta_offset = ftell(ifp);
  3545. if (tag == 0x97) {
  3546. for (i=0; i < 4; i++)
  3547. ver97 = ver97 * 10 + fgetc(ifp)-'0';
  3548. switch (ver97) {
  3549. case 100:
  3550. fseek (ifp, 68, SEEK_CUR);
  3551. FORC4 cam_mul[(c >> 1) | ((c & 1) << 1)] = get2();
  3552. break;
  3553. case 102:
  3554. fseek (ifp, 6, SEEK_CUR);
  3555. goto get2_rggb;
  3556. case 103:
  3557. fseek (ifp, 16, SEEK_CUR);
  3558. FORC4 cam_mul[c] = get2();
  3559. }
  3560. if (ver97 >= 200) {
  3561. if (ver97 != 205) fseek (ifp, 280, SEEK_CUR);
  3562. fread (buf97, 324, 1, ifp);
  3563. }
  3564. }
  3565. if (tag == 0xa4 && type == 3) {
  3566. fseek (ifp, wbi*48, SEEK_CUR);
  3567. FORC3 cam_mul[c] = get2();
  3568. }
  3569. if (tag == 0xa7 && (unsigned) (ver97-200) < 12 && !cam_mul[0]) {
  3570. ci = xlat[0][serial & 0xff];
  3571. cj = xlat[1][fgetc(ifp)^fgetc(ifp)^fgetc(ifp)^fgetc(ifp)];
  3572. ck = 0x60;
  3573. for (i=0; i < 324; i++)
  3574. buf97[i] ^= (cj += ci * ck++);
  3575. i = "66666>666;6A"[ver97-200] - '0';
  3576. FORC4 cam_mul[c ^ (c >> 1) ^ (i & 1)] =
  3577. sget2 (buf97 + (i & -2) + c*2);
  3578. }
  3579. if (tag == 0x200 && len == 3)
  3580. shot_order = (get4(),get4());
  3581. if (tag == 0x200 && len == 4)
  3582. black = (get2()+get2()+get2()+get2())/4;
  3583. if (tag == 0x201 && len == 4)
  3584. goto get2_rggb;
  3585. if (tag == 0x220 && len == 53) {
  3586. fseek (ifp, 14, SEEK_CUR);
  3587. pentax_tree();
  3588. }
  3589. if (tag == 0x401 && len == 4) {
  3590. black = (get4()+get4()+get4()+get4())/4;
  3591. }
  3592. if (tag == 0xe01) { /* Nikon Capture Note */
  3593. type = order;
  3594. order = 0x4949;
  3595. fseek (ifp, 22, SEEK_CUR);
  3596. for (offset=22; offset+22 < len; offset += 22+i) {
  3597. tag = get4();
  3598. fseek (ifp, 14, SEEK_CUR);
  3599. i = get4()-4;
  3600. if (tag == 0x76a43207) flip = get2();
  3601. else fseek (ifp, i, SEEK_CUR);
  3602. }
  3603. order = type;
  3604. }
  3605. if (tag == 0xe80 && len == 256 && type == 7) {
  3606. fseek (ifp, 48, SEEK_CUR);
  3607. cam_mul[0] = get2() * 508 * 1.078 / 0x10000;
  3608. cam_mul[2] = get2() * 382 * 1.173 / 0x10000;
  3609. }
  3610. if (tag == 0xf00 && type == 7) {
  3611. if (len == 614)
  3612. fseek (ifp, 176, SEEK_CUR);
  3613. else if (len == 734 || len == 1502)
  3614. fseek (ifp, 148, SEEK_CUR);
  3615. else goto next;
  3616. goto get2_256;
  3617. }
  3618. if ((tag == 0x1011 && len == 9) || tag == 0x20400200)
  3619. {
  3620. for (i=0; i < 3; i++)
  3621. FORC3 cmatrix[i][c] = ((short) get2()) / 256.0;
  3622. }
  3623. if ((tag == 0x1012 || tag == 0x20400600) && len == 4)
  3624. for (black = i=0; i < 4; i++)
  3625. black += get2() << 2;
  3626. if (tag == 0x1017 || tag == 0x20400100)
  3627. {
  3628. cam_mul[0] = get2() / 256.0;
  3629. }
  3630. if (tag == 0x1018 || tag == 0x20400100)
  3631. {
  3632. cam_mul[2] = get2() / 256.0;
  3633. }
  3634. if (tag == 0x2011 && len == 2) {
  3635. get2_256:
  3636. order = 0x4d4d;
  3637. cam_mul[0] = get2() / 256.0;
  3638. cam_mul[2] = get2() / 256.0;
  3639. }
  3640. if ((tag | 0x70) == 0x2070 && type == 4)
  3641. fseek (ifp, get4()+base, SEEK_SET);
  3642. if (tag == 0x2010 && type != 7)
  3643. load_raw = &CLASS olympus_e410_load_raw;
  3644. if (tag == 0x2020)
  3645. parse_thumb_note (base, 257, 258);
  3646. if (tag == 0x2040)
  3647. parse_makernote (base, 0x2040);
  3648. if (tag == 0xb028) {
  3649. fseek (ifp, get4(), SEEK_SET);
  3650. parse_thumb_note (base, 136, 137);
  3651. }
  3652. if (tag == 0x4001 && len > 500) {
  3653. i = len == 582 ? 50 : len == 653 ? 68 : len == 5120 ? 142 : 126;
  3654. fseek (ifp, i, SEEK_CUR);
  3655. get2_rggb:
  3656. FORC4 cam_mul[c ^ (c >> 1)] = get2();
  3657. fseek (ifp, 22, SEEK_CUR);
  3658. FORC4 sraw_mul[c ^ (c >> 1)] = get2();
  3659. }
  3660. next:
  3661. fseek (ifp, save, SEEK_SET);
  3662. }
  3663. quit:
  3664. order = sorder;
  3665. }
  3666. /*
  3667. Since the TIFF DateTime string has no timezone information,
  3668. assume that the camera's clock was set to Universal Time.
  3669. */
  3670. void CLASS get_timestamp (int reversed)
  3671. {
  3672. struct tm t;
  3673. char str[20];
  3674. int i;
  3675. str[19] = 0;
  3676. if (reversed)
  3677. for (i=19; i--; ) str[i] = fgetc(ifp);
  3678. else
  3679. fread (str, 19, 1, ifp);
  3680. memset (&t, 0, sizeof t);
  3681. if (sscanf (str, "%d:%d:%d %d:%d:%d", &t.tm_year, &t.tm_mon,
  3682. &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec) != 6)
  3683. return;
  3684. t.tm_year -= 1900;
  3685. t.tm_mon -= 1;
  3686. if (mktime(&t) > 0)
  3687. timestamp = mktime(&t);
  3688. }
  3689. void CLASS parse_exif (int base)
  3690. {
  3691. unsigned kodak, entries, tag, type, len, save, c;
  3692. double expo;
  3693. kodak = !strncmp(make,"EASTMAN",7);
  3694. entries = get2();
  3695. while (entries--) {
  3696. tiff_get (base, &tag, &type, &len, &save);
  3697. switch (tag) {
  3698. case 33434: shutter = getreal(type); break;
  3699. case 33437: aperture = getreal(type); break;
  3700. case 34855: iso_speed = get2(); break;
  3701. case 36867:
  3702. case 36868: get_timestamp(0); break;
  3703. case 37377: if ((expo = -getreal(type)) < 128)
  3704. shutter = pow (2.0, expo); break;
  3705. case 37378: aperture = pow (2.0, getreal(type)/2); break;
  3706. case 37386: focal_len = getreal(type); break;
  3707. case 37500: parse_makernote (base, 0); break;
  3708. case 40962: if (kodak) raw_width = get4(); break;
  3709. case 40963: if (kodak) raw_height = get4(); break;
  3710. case 41730:
  3711. if (get4() == 0x20002)
  3712. for (exif_cfa=c=0; c < 8; c+=2)
  3713. exif_cfa |= fgetc(ifp) * 0x01010101 << c;
  3714. }
  3715. fseek (ifp, save, SEEK_SET);
  3716. }
  3717. }
  3718. void CLASS parse_gps (int base)
  3719. {
  3720. unsigned entries, tag, type, len, save, c;
  3721. entries = get2();
  3722. while (entries--) {
  3723. tiff_get (base, &tag, &type, &len, &save);
  3724. switch (tag) {
  3725. case 1: case 3: case 5:
  3726. gpsdata[29+tag/2] = getc(ifp); break;
  3727. case 2: case 4: case 7:
  3728. FORC(6) gpsdata[tag/3*6+c] = get4(); break;
  3729. case 6:
  3730. FORC(2) gpsdata[18+c] = get4(); break;
  3731. case 18: case 29:
  3732. fgets ((char *) (gpsdata+14+tag/3), MIN(len,12), ifp);
  3733. }
  3734. fseek (ifp, save, SEEK_SET);
  3735. }
  3736. }
  3737. void CLASS romm_coeff (float romm_cam[3][3])
  3738. {
  3739. static const float rgb_romm[3][3] = /* ROMM == Kodak ProPhoto */
  3740. { { 2.034193, -0.727420, -0.306766 },
  3741. { -0.228811, 1.231729, -0.002922 },
  3742. { -0.008565, -0.153273, 1.161839 } };
  3743. int i, j, k;
  3744. for (i=0; i < 3; i++)
  3745. for (j=0; j < 3; j++)
  3746. for (cmatrix[i][j] = k=0; k < 3; k++)
  3747. cmatrix[i][j] += rgb_romm[i][k] * romm_cam[k][j];
  3748. }
  3749. void CLASS parse_mos (int offset)
  3750. {
  3751. char data[40];
  3752. int skip, from, i, c, neut[4], planes=0, frot=0;
  3753. static const char *mod[] =
  3754. { "","DCB2","Volare","Cantare","CMost","Valeo 6","Valeo 11","Valeo 22",
  3755. "Valeo 11p","Valeo 17","","Aptus 17","Aptus 22","Aptus 75","Aptus 65",
  3756. "Aptus 54S","Aptus 65S","Aptus 75S","AFi 5","AFi 6","AFi 7" };
  3757. float romm_cam[3][3];
  3758. fseek (ifp, offset, SEEK_SET);
  3759. while (1) {
  3760. if (get4() != 0x504b5453) break;
  3761. get4();
  3762. fread (data, 1, 40, ifp);
  3763. skip = get4();
  3764. from = ftell(ifp);
  3765. if (!strcmp(data,"JPEG_preview_data")) {
  3766. thumb_offset = from;
  3767. thumb_length = skip;
  3768. }
  3769. if (!strcmp(data,"icc_camera_profile")) {
  3770. profile_offset = from;
  3771. profile_length = skip;
  3772. }
  3773. if (!strcmp(data,"ShootObj_back_type")) {
  3774. fscanf (ifp, "%d", &i);
  3775. if ((unsigned) i < sizeof mod / sizeof (*mod))
  3776. strcpy (model, mod[i]);
  3777. }
  3778. if (!strcmp(data,"icc_camera_to_tone_matrix")) {
  3779. for (i=0; i < 9; i++)
  3780. romm_cam[0][i] = int_to_float(get4());
  3781. romm_coeff (romm_cam);
  3782. }
  3783. if (!strcmp(data,"CaptProf_color_matrix")) {
  3784. for (i=0; i < 9; i++)
  3785. fscanf (ifp, "%f", &romm_cam[0][i]);
  3786. romm_coeff (romm_cam);
  3787. }
  3788. if (!strcmp(data,"CaptProf_number_of_planes"))
  3789. fscanf (ifp, "%d", &planes);
  3790. if (!strcmp(data,"CaptProf_raw_data_rotation"))
  3791. fscanf (ifp, "%d", &flip);
  3792. if (!strcmp(data,"CaptProf_mosaic_pattern"))
  3793. FORC4 {
  3794. fscanf (ifp, "%d", &i);
  3795. if (i == 1) frot = c ^ (c >> 1);
  3796. }
  3797. if (!strcmp(data,"ImgProf_rotation_angle")) {
  3798. fscanf (ifp, "%d", &i);
  3799. flip = i - flip;
  3800. }
  3801. if (!strcmp(data,"NeutObj_neutrals") && !cam_mul[0]) {
  3802. FORC4 fscanf (ifp, "%d", neut+c);
  3803. FORC3 cam_mul[c] = (float) neut[0] / neut[c+1];
  3804. }
  3805. parse_mos (from);
  3806. fseek (ifp, skip+from, SEEK_SET);
  3807. }
  3808. if (planes)
  3809. filters = (planes == 1) * 0x01010101 *
  3810. (uchar) "\x94\x61\x16\x49"[(flip/90 + frot) & 3];
  3811. }
  3812. void CLASS linear_table (unsigned len)
  3813. {
  3814. int i;
  3815. if (len > 0x1000) len = 0x1000;
  3816. read_shorts (curve, len);
  3817. for (i=len; i < 0x1000; i++)
  3818. curve[i] = curve[i-1];
  3819. maximum = curve[0xfff];
  3820. }
  3821. void CLASS parse_kodak_ifd (int base)
  3822. {
  3823. unsigned entries, tag, type, len, save;
  3824. int i, c, wbi=-2, wbtemp=6500;
  3825. float mul[3], num;
  3826. entries = get2();
  3827. if (entries > 1024) return;
  3828. while (entries--) {
  3829. tiff_get (base, &tag, &type, &len, &save);
  3830. if (tag == 1020) wbi = getint(type);
  3831. if (tag == 1021 && len == 72) { /* WB set in software */
  3832. fseek (ifp, 40, SEEK_CUR);
  3833. FORC3 cam_mul[c] = 2048.0 / get2();
  3834. wbi = -2;
  3835. }
  3836. if (tag == 2118) wbtemp = getint(type);
  3837. if (tag == 2130 + wbi)
  3838. FORC3 mul[c] = getreal(type);
  3839. if (tag == 2140 + wbi && wbi >= 0)
  3840. {
  3841. FORC3 {
  3842. for (num=i=0; i < 4; i++)
  3843. num += getreal(type) * pow (wbtemp/100.0, i);
  3844. cam_mul[c] = 2048 / (num * mul[c]);
  3845. }
  3846. }
  3847. if (tag == 2317) linear_table (len);
  3848. if (tag == 6020) iso_speed = getint(type);
  3849. fseek (ifp, save, SEEK_SET);
  3850. }
  3851. }
  3852. #line 5265 "dcraw/dcraw.c"
  3853. int CLASS parse_tiff_ifd (int base)
  3854. {
  3855. unsigned entries, tag, type, len, plen=16, save;
  3856. int ifd, use_cm=0, cfa, i, j, c, ima_len=0;
  3857. char software[64], *cbuf, *cp;
  3858. uchar cfa_pat[16], cfa_pc[] = { 0,1,2,3 }, tab[256];
  3859. double dblack, cc[4][4], cm[4][3], cam_xyz[4][3], num;
  3860. double ab[]={ 1,1,1,1 }, asn[] = { 0,0,0,0 }, xyz[] = { 1,1,1 };
  3861. unsigned sony_curve[] = { 0,0,0,0,0,4095 };
  3862. unsigned *buf, sony_offset=0, sony_length=0, sony_key=0;
  3863. struct jhead jh;
  3864. FILE *sfp;
  3865. if (tiff_nifds >= sizeof tiff_ifd / sizeof tiff_ifd[0])
  3866. return 1;
  3867. ifd = tiff_nifds++;
  3868. for (j=0; j < 4; j++)
  3869. for (i=0; i < 4; i++)
  3870. cc[j][i] = i == j;
  3871. entries = get2();
  3872. if (entries > 512) return 1;
  3873. while (entries--) {
  3874. tiff_get (base, &tag, &type, &len, &save);
  3875. switch (tag) {
  3876. case 17: case 18:
  3877. if (type == 3 && len == 1)
  3878. {
  3879. cam_mul[(tag-17)*2] = get2() / 256.0;
  3880. }
  3881. break;
  3882. case 23:
  3883. if (type == 3) iso_speed = get2();
  3884. break;
  3885. case 36: case 37: case 38:
  3886. cam_mul[tag-0x24] = get2();
  3887. break;
  3888. case 39:
  3889. if (len < 50 || cam_mul[0]) break;
  3890. fseek (ifp, 12, SEEK_CUR);
  3891. FORC3 cam_mul[c] = get2();
  3892. break;
  3893. case 46:
  3894. if (type != 7 || fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8) break;
  3895. thumb_offset = ftell(ifp) - 2;
  3896. thumb_length = len;
  3897. break;
  3898. case 2: case 256: /* ImageWidth */
  3899. tiff_ifd[ifd].t_width = getint(type);
  3900. break;
  3901. case 3: case 257: /* ImageHeight */
  3902. tiff_ifd[ifd].t_height = getint(type);
  3903. break;
  3904. case 258: /* BitsPerSample */
  3905. tiff_ifd[ifd].samples = len & 7;
  3906. tiff_ifd[ifd].bps = get2();
  3907. break;
  3908. case 259: /* Compression */
  3909. tiff_ifd[ifd].comp = get2();
  3910. break;
  3911. case 262: /* PhotometricInterpretation */
  3912. tiff_ifd[ifd].phint = get2();
  3913. break;
  3914. case 270: /* ImageDescription */
  3915. fread (desc, 512, 1, ifp);
  3916. break;
  3917. case 271: /* Make */
  3918. fgets (make, 64, ifp);
  3919. break;
  3920. case 272: /* Model */
  3921. fgets (model, 64, ifp);
  3922. break;
  3923. case 280: /* Panasonic RW2 offset */
  3924. if (type != 4) break;
  3925. load_raw = &CLASS panasonic_load_raw;
  3926. load_flags = 0x2008;
  3927. case 273: /* StripOffset */
  3928. case 513:
  3929. tiff_ifd[ifd].offset = get4()+base;
  3930. if (!tiff_ifd[ifd].bps) {
  3931. fseek (ifp, tiff_ifd[ifd].offset, SEEK_SET);
  3932. if (ljpeg_start (&jh, 1)) {
  3933. tiff_ifd[ifd].comp = 6;
  3934. tiff_ifd[ifd].t_width = jh.wide << (jh.clrs == 2);
  3935. tiff_ifd[ifd].t_height = jh.high;
  3936. tiff_ifd[ifd].bps = jh.bits;
  3937. tiff_ifd[ifd].samples = jh.clrs;
  3938. }
  3939. }
  3940. break;
  3941. case 274: /* Orientation */
  3942. tiff_ifd[ifd].t_flip = "50132467"[get2() & 7]-'0';
  3943. break;
  3944. case 277: /* SamplesPerPixel */
  3945. tiff_ifd[ifd].samples = getint(type) & 7;
  3946. break;
  3947. case 279: /* StripByteCounts */
  3948. case 514:
  3949. tiff_ifd[ifd].bytes = get4();
  3950. break;
  3951. case 305: case 11: /* Software */
  3952. fgets (software, 64, ifp);
  3953. if (!strncmp(software,"Adobe",5) ||
  3954. !strncmp(software,"dcraw",5) ||
  3955. !strncmp(software,"UFRaw",5) ||
  3956. !strncmp(software,"Bibble",6) ||
  3957. !strncmp(software,"Nikon Scan",10) ||
  3958. !strcmp (software,"Digital Photo Professional"))
  3959. is_raw = 0;
  3960. break;
  3961. case 306: /* DateTime */
  3962. get_timestamp(0);
  3963. break;
  3964. case 315: /* Artist */
  3965. fread (artist, 64, 1, ifp);
  3966. break;
  3967. case 322: /* TileWidth */
  3968. tile_width = getint(type);
  3969. break;
  3970. case 323: /* TileLength */
  3971. tile_length = getint(type);
  3972. break;
  3973. case 324: /* TileOffsets */
  3974. tiff_ifd[ifd].offset = len > 1 ? ftell(ifp) : get4();
  3975. if (len == 4) {
  3976. load_raw = &CLASS sinar_4shot_load_raw;
  3977. is_raw = 5;
  3978. }
  3979. break;
  3980. case 330: /* SubIFDs */
  3981. if (!strcmp(model,"DSLR-A100") && tiff_ifd[ifd].t_width == 3872) {
  3982. load_raw = &CLASS sony_arw_load_raw;
  3983. data_offset = get4()+base;
  3984. ifd++; break;
  3985. }
  3986. while (len--) {
  3987. i = ftell(ifp);
  3988. fseek (ifp, get4()+base, SEEK_SET);
  3989. if (parse_tiff_ifd (base)) break;
  3990. fseek (ifp, i+4, SEEK_SET);
  3991. }
  3992. break;
  3993. case 400:
  3994. strcpy (make, "Sarnoff");
  3995. maximum = 0xfff;
  3996. break;
  3997. case 28688:
  3998. FORC4 sony_curve[c+1] = get2() >> 2 & 0xfff;
  3999. for (i=0; i < 5; i++)
  4000. for (j = sony_curve[i]+1; j <= sony_curve[i+1]; j++)
  4001. curve[j] = curve[j-1] + (1 << i);
  4002. break;
  4003. case 29184: sony_offset = get4(); break;
  4004. case 29185: sony_length = get4(); break;
  4005. case 29217: sony_key = get4(); break;
  4006. case 29264:
  4007. parse_minolta (ftell(ifp));
  4008. raw_width = 0;
  4009. break;
  4010. case 29443:
  4011. FORC4 cam_mul[c ^ (c < 2)] = get2();
  4012. break;
  4013. case 29459:
  4014. FORC4 cam_mul[c ^ (c >> 1)] = get2();
  4015. break;
  4016. case 33405: /* Model2 */
  4017. fgets (model2, 64, ifp);
  4018. break;
  4019. case 33422: /* CFAPattern */
  4020. case 64777: /* Kodak P-series */
  4021. if ((plen=len) > 16) plen = 16;
  4022. fread (cfa_pat, 1, plen, ifp);
  4023. for (colors=cfa=i=0; i < plen; i++) {
  4024. colors += !(cfa & (1 << cfa_pat[i]));
  4025. cfa |= 1 << cfa_pat[i];
  4026. }
  4027. if (cfa == 070) memcpy (cfa_pc,"\003\004\005",3); /* CMY */
  4028. if (cfa == 072) memcpy (cfa_pc,"\005\003\004\001",4); /* GMCY */
  4029. goto guess_cfa_pc;
  4030. case 33424:
  4031. fseek (ifp, get4()+base, SEEK_SET);
  4032. parse_kodak_ifd (base);
  4033. break;
  4034. case 33434: /* ExposureTime */
  4035. shutter = getreal(type);
  4036. break;
  4037. case 33437: /* FNumber */
  4038. aperture = getreal(type);
  4039. break;
  4040. case 34306: /* Leaf white balance */
  4041. FORC4 cam_mul[c ^ 1] = 4096.0 / get2();
  4042. break;
  4043. case 34307: /* Leaf CatchLight color matrix */
  4044. fread (software, 1, 7, ifp);
  4045. if (strncmp(software,"MATRIX",6)) break;
  4046. colors = 4;
  4047. for (raw_color = i=0; i < 3; i++) {
  4048. FORC4 fscanf (ifp, "%f", &rgb_cam[i][c^1]);
  4049. if (!use_camera_wb) continue;
  4050. num = 0;
  4051. FORC4 num += rgb_cam[i][c];
  4052. FORC4 rgb_cam[i][c] /= num;
  4053. }
  4054. break;
  4055. case 34310: /* Leaf metadata */
  4056. parse_mos (ftell(ifp));
  4057. case 34303:
  4058. strcpy (make, "Leaf");
  4059. break;
  4060. case 34665: /* EXIF tag */
  4061. fseek (ifp, get4()+base, SEEK_SET);
  4062. parse_exif (base);
  4063. break;
  4064. case 34853: /* GPSInfo tag */
  4065. fseek (ifp, get4()+base, SEEK_SET);
  4066. parse_gps (base);
  4067. break;
  4068. case 34675: /* InterColorProfile */
  4069. case 50831: /* AsShotICCProfile */
  4070. profile_offset = ftell(ifp);
  4071. profile_length = len;
  4072. break;
  4073. case 37122: /* CompressedBitsPerPixel */
  4074. kodak_cbpp = get4();
  4075. break;
  4076. case 37386: /* FocalLength */
  4077. focal_len = getreal(type);
  4078. break;
  4079. case 37393: /* ImageNumber */
  4080. shot_order = getint(type);
  4081. break;
  4082. case 37400: /* old Kodak KDC tag */
  4083. for (raw_color = i=0; i < 3; i++) {
  4084. getreal(type);
  4085. FORC3 rgb_cam[i][c] = getreal(type);
  4086. }
  4087. break;
  4088. case 46275: /* Imacon tags */
  4089. strcpy (make, "Imacon");
  4090. data_offset = ftell(ifp);
  4091. ima_len = len;
  4092. break;
  4093. case 46279:
  4094. if (!ima_len) break;
  4095. fseek (ifp, 78, SEEK_CUR);
  4096. raw_width = get4();
  4097. raw_height = get4();
  4098. left_margin = get4() & 7;
  4099. width = raw_width - left_margin - (get4() & 7);
  4100. top_margin = get4() & 7;
  4101. height = raw_height - top_margin - (get4() & 7);
  4102. if (raw_width == 7262) {
  4103. height = 5444;
  4104. width = 7244;
  4105. left_margin = 7;
  4106. }
  4107. fseek (ifp, 52, SEEK_CUR);
  4108. FORC3 cam_mul[c] = getreal(11);
  4109. fseek (ifp, 114, SEEK_CUR);
  4110. flip = (get2() >> 7) * 90;
  4111. if (width * height * 6 == ima_len) {
  4112. if (flip % 180 == 90) SWAP(width,height);
  4113. filters = flip = 0;
  4114. }
  4115. sprintf (model, "Ixpress %d-Mp", height*width/1000000);
  4116. load_raw = &CLASS imacon_full_load_raw;
  4117. if (filters) {
  4118. if (left_margin & 1) filters = 0x61616161;
  4119. load_raw = &CLASS unpacked_load_raw;
  4120. }
  4121. maximum = 0xffff;
  4122. break;
  4123. case 50454: /* Sinar tag */
  4124. case 50455:
  4125. if (!(cbuf = (char *) malloc(len))) break;
  4126. fread (cbuf, 1, len, ifp);
  4127. for (cp = cbuf-1; cp && cp < cbuf+len; cp = strchr(cp,'\n'))
  4128. if (!strncmp (++cp,"Neutral ",8))
  4129. {
  4130. sscanf (cp+8, "%f %f %f", cam_mul, cam_mul+1, cam_mul+2);
  4131. }
  4132. free (cbuf);
  4133. break;
  4134. case 50458:
  4135. if (!make[0]) strcpy (make, "Hasselblad");
  4136. break;
  4137. case 50459: /* Hasselblad tag */
  4138. i = order;
  4139. j = ftell(ifp);
  4140. c = tiff_nifds;
  4141. order = get2();
  4142. fseek (ifp, j+(get2(),get4()), SEEK_SET);
  4143. parse_tiff_ifd (j);
  4144. maximum = 0xffff;
  4145. tiff_nifds = c;
  4146. order = i;
  4147. break;
  4148. case 50706: /* DNGVersion */
  4149. FORC4 dng_version = (dng_version << 8) + fgetc(ifp);
  4150. if (!make[0]) strcpy (make, "DNG");
  4151. is_raw = 1;
  4152. break;
  4153. case 50710: /* CFAPlaneColor */
  4154. if (len > 4) len = 4;
  4155. colors = len;
  4156. fread (cfa_pc, 1, colors, ifp);
  4157. guess_cfa_pc:
  4158. FORCC tab[cfa_pc[c]] = c;
  4159. cdesc[c] = 0;
  4160. for (i=16; i--; )
  4161. filters = filters << 2 | tab[cfa_pat[i % plen]];
  4162. break;
  4163. case 50711: /* CFALayout */
  4164. if (get2() == 2) {
  4165. fuji_width = 1;
  4166. filters = 0x49494949;
  4167. }
  4168. break;
  4169. case 291:
  4170. case 50712: /* LinearizationTable */
  4171. linear_table (len);
  4172. break;
  4173. case 50714: /* BlackLevel */
  4174. case 50715: /* BlackLevelDeltaH */
  4175. case 50716: /* BlackLevelDeltaV */
  4176. for (dblack=i=0; i < len; i++)
  4177. dblack += getreal(type);
  4178. black += dblack/len + 0.5;
  4179. break;
  4180. case 50717: /* WhiteLevel */
  4181. maximum = getint(type);
  4182. break;
  4183. case 50718: /* DefaultScale */
  4184. pixel_aspect = getreal(type);
  4185. pixel_aspect /= getreal(type);
  4186. break;
  4187. case 50721: /* ColorMatrix1 */
  4188. case 50722: /* ColorMatrix2 */
  4189. FORCC for (j=0; j < 3; j++)
  4190. cm[c][j] = getreal(type);
  4191. use_cm = 1;
  4192. break;
  4193. case 50723: /* CameraCalibration1 */
  4194. case 50724: /* CameraCalibration2 */
  4195. for (i=0; i < colors; i++)
  4196. FORCC cc[i][c] = getreal(type);
  4197. case 50727: /* AnalogBalance */
  4198. FORCC ab[c] = getreal(type);
  4199. break;
  4200. case 50728: /* AsShotNeutral */
  4201. FORCC asn[c] = getreal(type);
  4202. break;
  4203. case 50729: /* AsShotWhiteXY */
  4204. xyz[0] = getreal(type);
  4205. xyz[1] = getreal(type);
  4206. xyz[2] = 1 - xyz[0] - xyz[1];
  4207. FORC3 xyz[c] /= d65_white[c];
  4208. break;
  4209. case 50740: /* DNGPrivateData */
  4210. if (dng_version) break;
  4211. parse_minolta (j = get4()+base);
  4212. fseek (ifp, j, SEEK_SET);
  4213. parse_tiff_ifd (base);
  4214. break;
  4215. case 50752:
  4216. read_shorts (cr2_slice, 3);
  4217. break;
  4218. case 50829: /* ActiveArea */
  4219. top_margin = getint(type);
  4220. left_margin = getint(type);
  4221. height = getint(type) - top_margin;
  4222. width = getint(type) - left_margin;
  4223. break;
  4224. case 64772: /* Kodak P-series */
  4225. fseek (ifp, 16, SEEK_CUR);
  4226. data_offset = get4();
  4227. fseek (ifp, 28, SEEK_CUR);
  4228. data_offset += get4();
  4229. load_raw = &CLASS packed_12_load_raw;
  4230. }
  4231. fseek (ifp, save, SEEK_SET);
  4232. }
  4233. if (sony_length && (buf = (unsigned *) malloc(sony_length))) {
  4234. fseek (ifp, sony_offset, SEEK_SET);
  4235. fread (buf, sony_length, 1, ifp);
  4236. sony_decrypt (buf, sony_length/4, 1, sony_key);
  4237. #ifndef LIBRAW_LIBRARY_BUILD
  4238. sfp = ifp;
  4239. if ((ifp = tmpfile())) {
  4240. fwrite (buf, sony_length, 1, ifp);
  4241. fseek (ifp, 0, SEEK_SET);
  4242. parse_tiff_ifd (-sony_offset);
  4243. fclose (ifp);
  4244. }
  4245. ifp = sfp;
  4246. #else
  4247. if( !ifp->tempbuffer_open(buf,sony_length))
  4248. {
  4249. parse_tiff_ifd(-sony_offset);
  4250. ifp->tempbuffer_close();
  4251. }
  4252. #endif
  4253. free (buf);
  4254. }
  4255. for (i=0; i < colors; i++)
  4256. FORCC cc[i][c] *= ab[i];
  4257. if (use_cm) {
  4258. FORCC for (i=0; i < 3; i++)
  4259. for (cam_xyz[c][i]=j=0; j < colors; j++)
  4260. cam_xyz[c][i] += cc[c][j] * cm[j][i] * xyz[i];
  4261. cam_xyz_coeff (cam_xyz);
  4262. }
  4263. if (asn[0]) {
  4264. cam_mul[3] = 0;
  4265. FORCC cam_mul[c] = 1 / asn[c];
  4266. }
  4267. if (!use_cm)
  4268. {
  4269. FORCC pre_mul[c] /= cc[c][c];
  4270. }
  4271. return 0;
  4272. }
  4273. void CLASS parse_tiff (int base)
  4274. {
  4275. int doff, max_samp=0, raw=-1, thm=-1, i;
  4276. struct jhead jh;
  4277. fseek (ifp, base, SEEK_SET);
  4278. order = get2();
  4279. if (order != 0x4949 && order != 0x4d4d) return;
  4280. get2();
  4281. memset (tiff_ifd, 0, sizeof tiff_ifd);
  4282. tiff_nifds = 0;
  4283. while ((doff = get4())) {
  4284. fseek (ifp, doff+base, SEEK_SET);
  4285. if (parse_tiff_ifd (base)) break;
  4286. }
  4287. thumb_misc = 16;
  4288. if (thumb_offset) {
  4289. fseek (ifp, thumb_offset, SEEK_SET);
  4290. if (ljpeg_start (&jh, 1)) {
  4291. thumb_misc = jh.bits;
  4292. thumb_width = jh.wide;
  4293. thumb_height = jh.high;
  4294. }
  4295. }
  4296. for (i=0; i < tiff_nifds; i++) {
  4297. if (max_samp < tiff_ifd[i].samples)
  4298. max_samp = tiff_ifd[i].samples;
  4299. if (max_samp > 3) max_samp = 3;
  4300. if ((tiff_ifd[i].comp != 6 || tiff_ifd[i].samples != 3) &&
  4301. tiff_ifd[i].t_width*tiff_ifd[i].t_height > raw_width*raw_height) {
  4302. raw_width = tiff_ifd[i].t_width;
  4303. raw_height = tiff_ifd[i].t_height;
  4304. tiff_bps = tiff_ifd[i].bps;
  4305. tiff_compress = tiff_ifd[i].comp;
  4306. data_offset = tiff_ifd[i].offset;
  4307. tiff_flip = tiff_ifd[i].t_flip;
  4308. tiff_samples = tiff_ifd[i].samples;
  4309. raw = i;
  4310. }
  4311. }
  4312. fuji_width *= (raw_width+1)/2;
  4313. if (tiff_ifd[0].t_flip) tiff_flip = tiff_ifd[0].t_flip;
  4314. if (raw >= 0 && !load_raw)
  4315. switch (tiff_compress) {
  4316. case 0: case 1:
  4317. switch (tiff_bps) {
  4318. case 8: load_raw = &CLASS eight_bit_load_raw; break;
  4319. case 12: load_raw = &CLASS packed_12_load_raw;
  4320. if (tiff_ifd[raw].phint == 2)
  4321. load_flags = 6;
  4322. if (strncmp(make,"PENTAX",6)) break;
  4323. case 14:
  4324. case 16: load_raw = &CLASS unpacked_load_raw; break;
  4325. }
  4326. if (tiff_ifd[raw].bytes*5 == raw_width*raw_height*8)
  4327. load_raw = &CLASS olympus_e300_load_raw;
  4328. break;
  4329. case 6: case 7: case 99:
  4330. load_raw = &CLASS lossless_jpeg_load_raw; break;
  4331. case 262:
  4332. load_raw = &CLASS kodak_262_load_raw; break;
  4333. case 32767:
  4334. load_raw = &CLASS sony_arw2_load_raw;
  4335. if (tiff_ifd[raw].bytes*8 == raw_width*raw_height*tiff_bps)
  4336. break;
  4337. raw_height += 8;
  4338. load_raw = &CLASS sony_arw_load_raw; break;
  4339. case 32769:
  4340. load_flags = 8;
  4341. case 32773:
  4342. load_raw = &CLASS packed_12_load_raw; break;
  4343. case 34713:
  4344. load_raw = &CLASS nikon_compressed_load_raw; break;
  4345. case 65535:
  4346. load_raw = &CLASS pentax_k10_load_raw; break;
  4347. case 65000:
  4348. switch (tiff_ifd[raw].phint) {
  4349. case 2: load_raw = &CLASS kodak_rgb_load_raw; filters = 0; break;
  4350. case 6: load_raw = &CLASS kodak_ycbcr_load_raw; filters = 0; break;
  4351. case 32803: load_raw = &CLASS kodak_65000_load_raw;
  4352. }
  4353. case 32867: break;
  4354. default: is_raw = 0;
  4355. }
  4356. if (!dng_version && tiff_samples == 3)
  4357. if (tiff_ifd[raw].bytes && tiff_bps != 14 && tiff_bps != 2048)
  4358. is_raw = 0;
  4359. if (!dng_version && tiff_bps == 8 && tiff_compress == 1 &&
  4360. tiff_ifd[raw].phint == 1) is_raw = 0;
  4361. if (tiff_bps == 8 && tiff_samples == 4) is_raw = 0;
  4362. for (i=0; i < tiff_nifds; i++)
  4363. if (i != raw && tiff_ifd[i].samples == max_samp &&
  4364. tiff_ifd[i].t_width * tiff_ifd[i].t_height / SQR(tiff_ifd[i].bps+1) >
  4365. thumb_width * thumb_height / SQR(thumb_misc+1)) {
  4366. thumb_width = tiff_ifd[i].t_width;
  4367. thumb_height = tiff_ifd[i].t_height;
  4368. thumb_offset = tiff_ifd[i].offset;
  4369. thumb_length = tiff_ifd[i].bytes;
  4370. thumb_misc = tiff_ifd[i].bps;
  4371. thm = i;
  4372. }
  4373. if (thm >= 0) {
  4374. thumb_misc |= tiff_ifd[thm].samples << 5;
  4375. switch (tiff_ifd[thm].comp) {
  4376. case 0:
  4377. write_thumb = &CLASS layer_thumb;
  4378. break;
  4379. case 1:
  4380. if (tiff_ifd[thm].bps > 8)
  4381. thumb_load_raw = &CLASS kodak_thumb_load_raw;
  4382. else
  4383. write_thumb = &CLASS ppm_thumb;
  4384. break;
  4385. case 65000:
  4386. thumb_load_raw = tiff_ifd[thm].phint == 6 ?
  4387. &CLASS kodak_ycbcr_load_raw : &CLASS kodak_rgb_load_raw;
  4388. }
  4389. }
  4390. }
  4391. void CLASS parse_minolta (int base)
  4392. {
  4393. int save, tag, len, offset, high=0, wide=0, i, c;
  4394. short sorder=order;
  4395. fseek (ifp, base, SEEK_SET);
  4396. if (fgetc(ifp) || fgetc(ifp)-'M' || fgetc(ifp)-'R') return;
  4397. order = fgetc(ifp) * 0x101;
  4398. offset = base + get4() + 8;
  4399. while ((save=ftell(ifp)) < offset) {
  4400. for (tag=i=0; i < 4; i++)
  4401. tag = tag << 8 | fgetc(ifp);
  4402. len = get4();
  4403. switch (tag) {
  4404. case 0x505244: /* PRD */
  4405. fseek (ifp, 8, SEEK_CUR);
  4406. high = get2();
  4407. wide = get2();
  4408. break;
  4409. case 0x574247: /* WBG */
  4410. get4();
  4411. i = strcmp(model,"DiMAGE A200") ? 0:3;
  4412. FORC4 cam_mul[c ^ (c >> 1) ^ i] = get2();
  4413. break;
  4414. case 0x545457: /* TTW */
  4415. parse_tiff (ftell(ifp));
  4416. data_offset = offset;
  4417. }
  4418. fseek (ifp, save+len+8, SEEK_SET);
  4419. }
  4420. raw_height = high;
  4421. raw_width = wide;
  4422. order = sorder;
  4423. }
  4424. /*
  4425. Many cameras have a "debug mode" that writes JPEG and raw
  4426. at the same time. The raw file has no header, so try to
  4427. to open the matching JPEG file and read its metadata.
  4428. */
  4429. void CLASS parse_external_jpeg()
  4430. {
  4431. char *file, *ext, *jname, *jfile, *jext;
  4432. #ifndef LIBRAW_LIBRARY_BUILD
  4433. FILE *save=ifp;
  4434. #else
  4435. if(!ifp->fname())
  4436. {
  4437. imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA ;
  4438. return;
  4439. }
  4440. #endif
  4441. ext = strrchr (ifname, '.');
  4442. file = strrchr (ifname, '/');
  4443. if (!file) file = strrchr (ifname, '\\');
  4444. #ifndef LIBRAW_LIBRARY_BUILD
  4445. if (!file) file = ifname-1;
  4446. #else
  4447. if (!file) file = (char*)ifname-1;
  4448. #endif
  4449. file++;
  4450. if (!ext || strlen(ext) != 4 || ext-file != 8) return;
  4451. jname = (char *) malloc (strlen(ifname) + 1);
  4452. merror (jname, "parse_external_jpeg()");
  4453. strcpy (jname, ifname);
  4454. jfile = file - ifname + jname;
  4455. jext = ext - ifname + jname;
  4456. if (strcasecmp (ext, ".jpg")) {
  4457. strcpy (jext, isupper(ext[1]) ? ".JPG":".jpg");
  4458. if (isdigit(*file)) {
  4459. memcpy (jfile, file+4, 4);
  4460. memcpy (jfile+4, file, 4);
  4461. }
  4462. } else
  4463. while (isdigit(*--jext)) {
  4464. if (*jext != '9') {
  4465. (*jext)++;
  4466. break;
  4467. }
  4468. *jext = '0';
  4469. }
  4470. #ifndef LIBRAW_LIBRARY_BUILD
  4471. if (strcmp (jname, ifname)) {
  4472. if ((ifp = fopen (jname, "rb"))) {
  4473. #ifdef DCRAW_VERBOSE
  4474. if (verbose)
  4475. fprintf (stderr,_("Reading metadata from %s ...\n"), jname);
  4476. #endif
  4477. parse_tiff (12);
  4478. thumb_offset = 0;
  4479. is_raw = 1;
  4480. fclose (ifp);
  4481. }
  4482. }
  4483. #else
  4484. if (strcmp (jname, ifname))
  4485. {
  4486. if(!ifp->subfile_open(jname))
  4487. {
  4488. parse_tiff (12);
  4489. thumb_offset = 0;
  4490. is_raw = 1;
  4491. ifp->subfile_close();
  4492. }
  4493. else
  4494. imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA ;
  4495. }
  4496. #endif
  4497. if (!timestamp)
  4498. {
  4499. #ifdef LIBRAW_LIBRARY_BUILD
  4500. imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA ;
  4501. #endif
  4502. #ifdef DCRAW_VERBOSE
  4503. fprintf (stderr,_("Failed to read metadata from %s\n"), jname);
  4504. #endif
  4505. }
  4506. free (jname);
  4507. #ifndef LIBRAW_LIBRARY_BUILD
  4508. ifp = save;
  4509. #endif
  4510. }
  4511. /*
  4512. CIFF block 0x1030 contains an 8x8 white sample.
  4513. Load this into white[][] for use in scale_colors().
  4514. */
  4515. void CLASS ciff_block_1030()
  4516. {
  4517. static const ushort key[] = { 0x410, 0x45f3 };
  4518. int i, bpp, row, col, vbits=0;
  4519. unsigned long bitbuf=0;
  4520. if ((get2(),get4()) != 0x80008 || !get4()) return;
  4521. bpp = get2();
  4522. if (bpp != 10 && bpp != 12) return;
  4523. for (i=row=0; row < 8; row++)
  4524. for (col=0; col < 8; col++) {
  4525. if (vbits < bpp) {
  4526. bitbuf = bitbuf << 16 | (get2() ^ key[i++ & 1]);
  4527. vbits += 16;
  4528. }
  4529. white[row][col] =
  4530. bitbuf << (LONG_BIT - vbits) >> (LONG_BIT - bpp);
  4531. vbits -= bpp;
  4532. }
  4533. }
  4534. /*
  4535. Parse a CIFF file, better known as Canon CRW format.
  4536. */
  4537. void CLASS parse_ciff (int offset, int length)
  4538. {
  4539. int tboff, nrecs, c, type, len, save, wbi=-1;
  4540. ushort key[] = { 0x410, 0x45f3 };
  4541. fseek (ifp, offset+length-4, SEEK_SET);
  4542. tboff = get4() + offset;
  4543. fseek (ifp, tboff, SEEK_SET);
  4544. nrecs = get2();
  4545. if (nrecs > 100) return;
  4546. while (nrecs--) {
  4547. type = get2();
  4548. len = get4();
  4549. save = ftell(ifp) + 4;
  4550. fseek (ifp, offset+get4(), SEEK_SET);
  4551. if ((((type >> 8) + 8) | 8) == 0x38)
  4552. parse_ciff (ftell(ifp), len); /* Parse a sub-table */
  4553. if (type == 0x0810)
  4554. fread (artist, 64, 1, ifp);
  4555. if (type == 0x080a) {
  4556. fread (make, 64, 1, ifp);
  4557. fseek (ifp, strlen(make) - 63, SEEK_CUR);
  4558. fread (model, 64, 1, ifp);
  4559. }
  4560. if (type == 0x1810) {
  4561. fseek (ifp, 12, SEEK_CUR);
  4562. flip = get4();
  4563. }
  4564. if (type == 0x1835) /* Get the decoder table */
  4565. tiff_compress = get4();
  4566. if (type == 0x2007) {
  4567. thumb_offset = ftell(ifp);
  4568. thumb_length = len;
  4569. }
  4570. if (type == 0x1818) {
  4571. shutter = pow (2.0f, -int_to_float((get4(),get4())));
  4572. aperture = pow (2.0f, int_to_float(get4())/2);
  4573. }
  4574. if (type == 0x102a) {
  4575. iso_speed = pow (2.0, (get4(),get2())/32.0 - 4) * 50;
  4576. aperture = pow (2.0, (get2(),(short)get2())/64.0);
  4577. shutter = pow (2.0,-((short)get2())/32.0);
  4578. wbi = (get2(),get2());
  4579. if (wbi > 17) wbi = 0;
  4580. fseek (ifp, 32, SEEK_CUR);
  4581. if (shutter > 1e6) shutter = get2()/10.0;
  4582. }
  4583. if (type == 0x102c) {
  4584. if (get2() > 512) { /* Pro90, G1 */
  4585. fseek (ifp, 118, SEEK_CUR);
  4586. FORC4 cam_mul[c ^ 2] = get2();
  4587. } else { /* G2, S30, S40 */
  4588. fseek (ifp, 98, SEEK_CUR);
  4589. FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get2();
  4590. }
  4591. }
  4592. if (type == 0x0032) {
  4593. if (len == 768) { /* EOS D30 */
  4594. fseek (ifp, 72, SEEK_CUR);
  4595. FORC4 cam_mul[c ^ (c >> 1)] = 1024.0 / get2();
  4596. if (!wbi) cam_mul[0] = -1; /* use my auto white balance */
  4597. } else if (!cam_mul[0]) {
  4598. if (get2() == key[0]) /* Pro1, G6, S60, S70 */
  4599. c = (strstr(model,"Pro1") ?
  4600. "012346000000000000":"01345:000000006008")[wbi]-'0'+ 2;
  4601. else { /* G3, G5, S45, S50 */
  4602. c = "023457000000006000"[wbi]-'0';
  4603. key[0] = key[1] = 0;
  4604. }
  4605. fseek (ifp, 78 + c*8, SEEK_CUR);
  4606. FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get2() ^ key[c & 1];
  4607. if (!wbi) cam_mul[0] = -1;
  4608. }
  4609. }
  4610. if (type == 0x10a9) { /* D60, 10D, 300D, and clones */
  4611. if (len > 66) wbi = "0134567028"[wbi]-'0';
  4612. fseek (ifp, 2 + wbi*8, SEEK_CUR);
  4613. FORC4 cam_mul[c ^ (c >> 1)] = get2();
  4614. }
  4615. if (type == 0x1030 && (0x18040 >> wbi & 1))
  4616. ciff_block_1030(); /* all that don't have 0x10a9 */
  4617. if (type == 0x1031) {
  4618. raw_width = (get2(),get2());
  4619. raw_height = get2();
  4620. }
  4621. if (type == 0x5029) {
  4622. focal_len = len >> 16;
  4623. if ((len & 0xffff) == 2) focal_len /= 32;
  4624. }
  4625. if (type == 0x5813) flash_used = int_to_float(len);
  4626. if (type == 0x5814) canon_ev = int_to_float(len);
  4627. if (type == 0x5817) shot_order = len;
  4628. if (type == 0x5834) unique_id = len;
  4629. if (type == 0x580e) timestamp = len;
  4630. if (type == 0x180e) timestamp = get4();
  4631. #ifdef LOCALTIME
  4632. if ((type | 0x4000) == 0x580e)
  4633. timestamp = mktime (gmtime (&timestamp));
  4634. #endif
  4635. fseek (ifp, save, SEEK_SET);
  4636. }
  4637. }
  4638. void CLASS parse_rollei()
  4639. {
  4640. char line[128], *val;
  4641. struct tm t;
  4642. fseek (ifp, 0, SEEK_SET);
  4643. memset (&t, 0, sizeof t);
  4644. do {
  4645. fgets (line, 128, ifp);
  4646. if ((val = strchr(line,'=')))
  4647. *val++ = 0;
  4648. else
  4649. val = line + strlen(line);
  4650. if (!strcmp(line,"DAT"))
  4651. sscanf (val, "%d.%d.%d", &t.tm_mday, &t.tm_mon, &t.tm_year);
  4652. if (!strcmp(line,"TIM"))
  4653. sscanf (val, "%d:%d:%d", &t.tm_hour, &t.tm_min, &t.tm_sec);
  4654. if (!strcmp(line,"HDR"))
  4655. thumb_offset = atoi(val);
  4656. if (!strcmp(line,"X "))
  4657. raw_width = atoi(val);
  4658. if (!strcmp(line,"Y "))
  4659. raw_height = atoi(val);
  4660. if (!strcmp(line,"TX "))
  4661. thumb_width = atoi(val);
  4662. if (!strcmp(line,"TY "))
  4663. thumb_height = atoi(val);
  4664. } while (strncmp(line,"EOHD",4));
  4665. data_offset = thumb_offset + thumb_width * thumb_height * 2;
  4666. t.tm_year -= 1900;
  4667. t.tm_mon -= 1;
  4668. if (mktime(&t) > 0)
  4669. timestamp = mktime(&t);
  4670. strcpy (make, "Rollei");
  4671. strcpy (model,"d530flex");
  4672. write_thumb = &CLASS rollei_thumb;
  4673. }
  4674. void CLASS parse_sinar_ia()
  4675. {
  4676. int entries, off;
  4677. char str[8], *cp;
  4678. order = 0x4949;
  4679. fseek (ifp, 4, SEEK_SET);
  4680. entries = get4();
  4681. fseek (ifp, get4(), SEEK_SET);
  4682. while (entries--) {
  4683. off = get4(); get4();
  4684. fread (str, 8, 1, ifp);
  4685. if (!strcmp(str,"META")) meta_offset = off;
  4686. if (!strcmp(str,"THUMB")) thumb_offset = off;
  4687. if (!strcmp(str,"RAW0")) data_offset = off;
  4688. }
  4689. fseek (ifp, meta_offset+20, SEEK_SET);
  4690. fread (make, 64, 1, ifp);
  4691. make[63] = 0;
  4692. if ((cp = strchr(make,' '))) {
  4693. strcpy (model, cp+1);
  4694. *cp = 0;
  4695. }
  4696. raw_width = get2();
  4697. raw_height = get2();
  4698. load_raw = &CLASS unpacked_load_raw;
  4699. thumb_width = (get4(),get2());
  4700. thumb_height = get2();
  4701. write_thumb = &CLASS ppm_thumb;
  4702. maximum = 0x3fff;
  4703. }
  4704. void CLASS parse_phase_one (int base)
  4705. {
  4706. unsigned entries, tag, type, len, data, save, i, c;
  4707. float romm_cam[3][3];
  4708. char *cp;
  4709. memset (&ph1, 0, sizeof ph1);
  4710. fseek (ifp, base, SEEK_SET);
  4711. order = get4() & 0xffff;
  4712. if (get4() >> 8 != 0x526177) return; /* "Raw" */
  4713. fseek (ifp, get4()+base, SEEK_SET);
  4714. entries = get4();
  4715. get4();
  4716. while (entries--) {
  4717. tag = get4();
  4718. type = get4();
  4719. len = get4();
  4720. data = get4();
  4721. save = ftell(ifp);
  4722. fseek (ifp, base+data, SEEK_SET);
  4723. switch (tag) {
  4724. case 0x100: flip = "0653"[data & 3]-'0'; break;
  4725. case 0x106:
  4726. for (i=0; i < 9; i++)
  4727. romm_cam[0][i] = getreal(11);
  4728. romm_coeff (romm_cam);
  4729. break;
  4730. case 0x107:
  4731. FORC3 cam_mul[c] = getreal(11);
  4732. break;
  4733. case 0x108: raw_width = data; break;
  4734. case 0x109: raw_height = data; break;
  4735. case 0x10a: left_margin = data; break;
  4736. case 0x10b: top_margin = data; break;
  4737. case 0x10c: width = data; break;
  4738. case 0x10d: height = data; break;
  4739. case 0x10e: ph1.format = data; break;
  4740. case 0x10f: data_offset = data+base; break;
  4741. case 0x110: meta_offset = data+base;
  4742. meta_length = len; break;
  4743. case 0x112: ph1.key_off = save - 4; break;
  4744. case 0x210: ph1.tag_210 = int_to_float(data); break;
  4745. case 0x21a: ph1.tag_21a = data; break;
  4746. case 0x21c: strip_offset = data+base; break;
  4747. case 0x21d: ph1.t_black = data; break;
  4748. case 0x222: ph1.split_col = data - left_margin; break;
  4749. case 0x223: ph1.black_off = data+base; break;
  4750. case 0x301:
  4751. model[63] = 0;
  4752. fread (model, 1, 63, ifp);
  4753. if ((cp = strstr(model," camera"))) *cp = 0;
  4754. }
  4755. fseek (ifp, save, SEEK_SET);
  4756. }
  4757. load_raw = ph1.format < 3 ?
  4758. &CLASS phase_one_load_raw : &CLASS phase_one_load_raw_c;
  4759. maximum = 0xffff;
  4760. strcpy (make, "Phase One");
  4761. if (model[0]) return;
  4762. switch (raw_height) {
  4763. case 2060: strcpy (model,"LightPhase"); break;
  4764. case 2682: strcpy (model,"H 10"); break;
  4765. case 4128: strcpy (model,"H 20"); break;
  4766. case 5488: strcpy (model,"H 25"); break;
  4767. }
  4768. }
  4769. void CLASS parse_fuji (int offset)
  4770. {
  4771. unsigned entries, tag, len, save, c;
  4772. fseek (ifp, offset, SEEK_SET);
  4773. entries = get4();
  4774. if (entries > 255) return;
  4775. while (entries--) {
  4776. tag = get2();
  4777. len = get2();
  4778. save = ftell(ifp);
  4779. if (tag == 0x100) {
  4780. raw_height = get2();
  4781. raw_width = get2();
  4782. } else if (tag == 0x121) {
  4783. height = get2();
  4784. if ((width = get2()) == 4284) width += 3;
  4785. } else if (tag == 0x130)
  4786. fuji_layout = fgetc(ifp) >> 7;
  4787. if (tag == 0x2ff0)
  4788. {
  4789. FORC4 cam_mul[c ^ 1] = get2();
  4790. }
  4791. fseek (ifp, save+len, SEEK_SET);
  4792. }
  4793. height <<= fuji_layout;
  4794. width >>= fuji_layout;
  4795. }
  4796. int CLASS parse_jpeg (int offset)
  4797. {
  4798. int len, save, hlen, mark;
  4799. fseek (ifp, offset, SEEK_SET);
  4800. if (fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8) return 0;
  4801. while (fgetc(ifp) == 0xff && (mark = fgetc(ifp)) != 0xda) {
  4802. order = 0x4d4d;
  4803. len = get2() - 2;
  4804. save = ftell(ifp);
  4805. if (mark == 0xc0 || mark == 0xc3) {
  4806. fgetc(ifp);
  4807. raw_height = get2();
  4808. raw_width = get2();
  4809. }
  4810. order = get2();
  4811. hlen = get4();
  4812. if (get4() == 0x48454150) /* "HEAP" */
  4813. parse_ciff (save+hlen, len-hlen);
  4814. parse_tiff (save+6);
  4815. fseek (ifp, save+len, SEEK_SET);
  4816. }
  4817. return 1;
  4818. }
  4819. void CLASS parse_riff()
  4820. {
  4821. unsigned i, size, end;
  4822. char tag[4], date[64], month[64];
  4823. static const char mon[12][4] =
  4824. { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
  4825. struct tm t;
  4826. order = 0x4949;
  4827. fread (tag, 4, 1, ifp);
  4828. size = get4();
  4829. end = ftell(ifp) + size;
  4830. if (!memcmp(tag,"RIFF",4) || !memcmp(tag,"LIST",4)) {
  4831. get4();
  4832. while (ftell(ifp)+7 < end)
  4833. parse_riff();
  4834. } else if (!memcmp(tag,"nctg",4)) {
  4835. while (ftell(ifp)+7 < end) {
  4836. i = get2();
  4837. size = get2();
  4838. if ((i+1) >> 1 == 10 && size == 20)
  4839. get_timestamp(0);
  4840. else fseek (ifp, size, SEEK_CUR);
  4841. }
  4842. } else if (!memcmp(tag,"IDIT",4) && size < 64) {
  4843. fread (date, 64, 1, ifp);
  4844. date[size] = 0;
  4845. memset (&t, 0, sizeof t);
  4846. if (sscanf (date, "%*s %s %d %d:%d:%d %d", month, &t.tm_mday,
  4847. &t.tm_hour, &t.tm_min, &t.tm_sec, &t.tm_year) == 6) {
  4848. for (i=0; i < 12 && strcasecmp(mon[i],month); i++);
  4849. t.tm_mon = i;
  4850. t.tm_year -= 1900;
  4851. if (mktime(&t) > 0)
  4852. timestamp = mktime(&t);
  4853. }
  4854. } else
  4855. fseek (ifp, size, SEEK_CUR);
  4856. }
  4857. void CLASS parse_smal (int offset, int fsize)
  4858. {
  4859. int ver;
  4860. fseek (ifp, offset+2, SEEK_SET);
  4861. order = 0x4949;
  4862. ver = fgetc(ifp);
  4863. if (ver == 6)
  4864. fseek (ifp, 5, SEEK_CUR);
  4865. if (get4() != fsize) return;
  4866. if (ver > 6) data_offset = get4();
  4867. raw_height = height = get2();
  4868. raw_width = width = get2();
  4869. strcpy (make, "SMaL");
  4870. sprintf (model, "v%d %dx%d", ver, width, height);
  4871. if (ver == 6) load_raw = &CLASS smal_v6_load_raw;
  4872. if (ver == 9) load_raw = &CLASS smal_v9_load_raw;
  4873. }
  4874. void CLASS parse_cine()
  4875. {
  4876. unsigned off_head, off_setup, off_image, i;
  4877. order = 0x4949;
  4878. fseek (ifp, 4, SEEK_SET);
  4879. is_raw = get2() == 2;
  4880. fseek (ifp, 14, SEEK_CUR);
  4881. is_raw *= get4();
  4882. off_head = get4();
  4883. off_setup = get4();
  4884. off_image = get4();
  4885. timestamp = get4();
  4886. if ((i = get4())) timestamp = i;
  4887. fseek (ifp, off_head+4, SEEK_SET);
  4888. raw_width = get4();
  4889. raw_height = get4();
  4890. switch (get2(),get2()) {
  4891. case 8: load_raw = &CLASS eight_bit_load_raw; break;
  4892. case 16: load_raw = &CLASS unpacked_load_raw;
  4893. }
  4894. fseek (ifp, off_setup+792, SEEK_SET);
  4895. strcpy (make, "CINE");
  4896. sprintf (model, "%d", get4());
  4897. fseek (ifp, 12, SEEK_CUR);
  4898. switch ((i=get4()) & 0xffffff) {
  4899. case 3: filters = 0x94949494; break;
  4900. case 4: filters = 0x49494949; break;
  4901. default: is_raw = 0;
  4902. }
  4903. fseek (ifp, 72, SEEK_CUR);
  4904. switch ((get4()+3600) % 360) {
  4905. case 270: flip = 4; break;
  4906. case 180: flip = 1; break;
  4907. case 90: flip = 7; break;
  4908. case 0: flip = 2;
  4909. }
  4910. cam_mul[0] = getreal(11);
  4911. cam_mul[2] = getreal(11);
  4912. maximum = ~(-1 << get4());
  4913. fseek (ifp, 668, SEEK_CUR);
  4914. shutter = get4()/1000000000.0;
  4915. fseek (ifp, off_image, SEEK_SET);
  4916. if (shot_select < is_raw)
  4917. fseek (ifp, shot_select*8, SEEK_CUR);
  4918. data_offset = (INT64) get4() + 8;
  4919. data_offset += (INT64) get4() << 32;
  4920. }
  4921. #line 6467 "dcraw/dcraw.c"
  4922. #ifdef LIBRAW_LIBRARY_BUILD
  4923. void CLASS adobe_coeff (const char *p_make, const char *p_model)
  4924. #else
  4925. void CLASS adobe_coeff (char *p_make, char *p_model)
  4926. #endif
  4927. {
  4928. static const struct {
  4929. const char *prefix;
  4930. short t_black, t_maximum, trans[12];
  4931. } table[] = {
  4932. { "Apple QuickTake", 0, 0, /* DJC */
  4933. { 17576,-3191,-3318,5210,6733,-1942,9031,1280,-124 } },
  4934. { "Canon EOS D2000", 0, 0,
  4935. { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
  4936. { "Canon EOS D6000", 0, 0,
  4937. { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
  4938. { "Canon EOS D30", 0, 0,
  4939. { 9805,-2689,-1312,-5803,13064,3068,-2438,3075,8775 } },
  4940. { "Canon EOS D60", 0, 0xfa0,
  4941. { 6188,-1341,-890,-7168,14489,2937,-2640,3228,8483 } },
  4942. { "Canon EOS 5D Mark II", 0, 0x3cf0,
  4943. { 4716,603,-830,-7798,15474,2480,-1496,1937,6651 } },
  4944. { "Canon EOS 5D", 0, 0xe6c,
  4945. { 6347,-479,-972,-8297,15954,2480,-1968,2131,7649 } },
  4946. { "Canon EOS 10D", 0, 0xfa0,
  4947. { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
  4948. { "Canon EOS 20Da", 0, 0,
  4949. { 14155,-5065,-1382,-6550,14633,2039,-1623,1824,6561 } },
  4950. { "Canon EOS 20D", 0, 0xfff,
  4951. { 6599,-537,-891,-8071,15783,2424,-1983,2234,7462 } },
  4952. { "Canon EOS 30D", 0, 0,
  4953. { 6257,-303,-1000,-7880,15621,2396,-1714,1904,7046 } },
  4954. { "Canon EOS 40D", 0, 0x3f60,
  4955. { 6071,-747,-856,-7653,15365,2441,-2025,2553,7315 } },
  4956. { "Canon EOS 50D", 0, 0x3d93,
  4957. { 4920,616,-593,-6493,13964,2784,-1774,3178,7005 } },
  4958. { "Canon EOS 300D", 0, 0xfa0,
  4959. { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
  4960. { "Canon EOS 350D", 0, 0xfff,
  4961. { 6018,-617,-965,-8645,15881,2975,-1530,1719,7642 } },
  4962. { "Canon EOS 400D", 0, 0xe8e,
  4963. { 7054,-1501,-990,-8156,15544,2812,-1278,1414,7796 } },
  4964. { "Canon EOS 450D", 0, 0x390d,
  4965. { 5784,-262,-821,-7539,15064,2672,-1982,2681,7427 } },
  4966. { "Canon EOS 1000D", 0, 0xe43,
  4967. { 6771,-1139,-977,-7818,15123,2928,-1244,1437,7533 } },
  4968. { "Canon EOS-1Ds Mark III", 0, 0x3bb0,
  4969. { 5859,-211,-930,-8255,16017,2353,-1732,1887,7448 } },
  4970. { "Canon EOS-1Ds Mark II", 0, 0xe80,
  4971. { 6517,-602,-867,-8180,15926,2378,-1618,1771,7633 } },
  4972. { "Canon EOS-1D Mark II N", 0, 0xe80,
  4973. { 6240,-466,-822,-8180,15825,2500,-1801,1938,8042 } },
  4974. { "Canon EOS-1D Mark III", 0, 0x3bb0,
  4975. { 6291,-540,-976,-8350,16145,2311,-1714,1858,7326 } },
  4976. { "Canon EOS-1D Mark II", 0, 0xe80,
  4977. { 6264,-582,-724,-8312,15948,2504,-1744,1919,8664 } },
  4978. { "Canon EOS-1DS", 0, 0xe20,
  4979. { 4374,3631,-1743,-7520,15212,2472,-2892,3632,8161 } },
  4980. { "Canon EOS-1D", 0, 0xe20,
  4981. { 6806,-179,-1020,-8097,16415,1687,-3267,4236,7690 } },
  4982. { "Canon EOS", 0, 0,
  4983. { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
  4984. { "Canon PowerShot A50", 0, 0,
  4985. { -5300,9846,1776,3436,684,3939,-5540,9879,6200,-1404,11175,217 } },
  4986. { "Canon PowerShot A5", 0, 0,
  4987. { -4801,9475,1952,2926,1611,4094,-5259,10164,5947,-1554,10883,547 } },
  4988. { "Canon PowerShot G10", 0, 0,
  4989. { 11093,-3906,-1028,-5047,12492,2879,-1003,1750,5561 } },
  4990. { "Canon PowerShot G1", 0, 0,
  4991. { -4778,9467,2172,4743,-1141,4344,-5146,9908,6077,-1566,11051,557 } },
  4992. { "Canon PowerShot G2", 0, 0,
  4993. { 9087,-2693,-1049,-6715,14382,2537,-2291,2819,7790 } },
  4994. { "Canon PowerShot G3", 0, 0,
  4995. { 9212,-2781,-1073,-6573,14189,2605,-2300,2844,7664 } },
  4996. { "Canon PowerShot G5", 0, 0,
  4997. { 9757,-2872,-933,-5972,13861,2301,-1622,2328,7212 } },
  4998. { "Canon PowerShot G6", 0, 0,
  4999. { 9877,-3775,-871,-7613,14807,3072,-1448,1305,7485 } },
  5000. { "Canon PowerShot G9", 0, 0,
  5001. { 7368,-2141,-598,-5621,13254,2625,-1418,1696,5743 } },
  5002. { "Canon PowerShot Pro1", 0, 0,
  5003. { 10062,-3522,-999,-7643,15117,2730,-765,817,7323 } },
  5004. { "Canon PowerShot Pro70", 34, 0,
  5005. { -4155,9818,1529,3939,-25,4522,-5521,9870,6610,-2238,10873,1342 } },
  5006. { "Canon PowerShot Pro90", 0, 0,
  5007. { -4963,9896,2235,4642,-987,4294,-5162,10011,5859,-1770,11230,577 } },
  5008. { "Canon PowerShot S30", 0, 0,
  5009. { 10566,-3652,-1129,-6552,14662,2006,-2197,2581,7670 } },
  5010. { "Canon PowerShot S40", 0, 0,
  5011. { 8510,-2487,-940,-6869,14231,2900,-2318,2829,9013 } },
  5012. { "Canon PowerShot S45", 0, 0,
  5013. { 8163,-2333,-955,-6682,14174,2751,-2077,2597,8041 } },
  5014. { "Canon PowerShot S50", 0, 0,
  5015. { 8882,-2571,-863,-6348,14234,2288,-1516,2172,6569 } },
  5016. { "Canon PowerShot S60", 0, 0,
  5017. { 8795,-2482,-797,-7804,15403,2573,-1422,1996,7082 } },
  5018. { "Canon PowerShot S70", 0, 0,
  5019. { 9976,-3810,-832,-7115,14463,2906,-901,989,7889 } },
  5020. { "Canon PowerShot A610", 0, 0, /* DJC */
  5021. { 15591,-6402,-1592,-5365,13198,2168,-1300,1824,5075 } },
  5022. { "Canon PowerShot A620", 0, 0, /* DJC */
  5023. { 15265,-6193,-1558,-4125,12116,2010,-888,1639,5220 } },
  5024. { "Canon PowerShot A630", 0, 0, /* DJC */
  5025. { 14201,-5308,-1757,-6087,14472,1617,-2191,3105,5348 } },
  5026. { "Canon PowerShot A640", 0, 0, /* DJC */
  5027. { 13124,-5329,-1390,-3602,11658,1944,-1612,2863,4885 } },
  5028. { "Canon PowerShot A650", 0, 0, /* DJC */
  5029. { 9427,-3036,-959,-2581,10671,1911,-1039,1982,4430 } },
  5030. { "Canon PowerShot A720", 0, 0, /* DJC */
  5031. { 14573,-5482,-1546,-1266,9799,1468,-1040,1912,3810 } },
  5032. { "Canon PowerShot S3 IS", 0, 0, /* DJC */
  5033. { 14062,-5199,-1446,-4712,12470,2243,-1286,2028,4836 } },
  5034. { "CINE 650", 0, 0,
  5035. { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
  5036. { "CINE 660", 0, 0,
  5037. { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
  5038. { "CINE", 0, 0,
  5039. { 20183,-4295,-423,-3940,15330,3985,-280,4870,9800 } },
  5040. { "Contax N Digital", 0, 0xf1e,
  5041. { 7777,1285,-1053,-9280,16543,2916,-3677,5679,7060 } },
  5042. { "EPSON R-D1", 0, 0,
  5043. { 6827,-1878,-732,-8429,16012,2564,-704,592,7145 } },
  5044. { "FUJIFILM FinePix E550", 0, 0,
  5045. { 11044,-3888,-1120,-7248,15168,2208,-1531,2277,8069 } },
  5046. { "FUJIFILM FinePix E900", 0, 0,
  5047. { 9183,-2526,-1078,-7461,15071,2574,-2022,2440,8639 } },
  5048. { "FUJIFILM FinePix F8", 0, 0,
  5049. { 11044,-3888,-1120,-7248,15168,2208,-1531,2277,8069 } },
  5050. { "FUJIFILM FinePix F7", 0, 0,
  5051. { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
  5052. { "FUJIFILM FinePix S100FS", 514, 0,
  5053. { 11521,-4355,-1065,-6524,13767,3058,-1466,1984,6045 } },
  5054. { "FUJIFILM FinePix S20Pro", 0, 0,
  5055. { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
  5056. { "FUJIFILM FinePix S2Pro", 128, 0,
  5057. { 12492,-4690,-1402,-7033,15423,1647,-1507,2111,7697 } },
  5058. { "FUJIFILM FinePix S3Pro", 0, 0,
  5059. { 11807,-4612,-1294,-8927,16968,1988,-2120,2741,8006 } },
  5060. { "FUJIFILM FinePix S5Pro", 0, 0,
  5061. { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
  5062. { "FUJIFILM FinePix S5000", 0, 0,
  5063. { 8754,-2732,-1019,-7204,15069,2276,-1702,2334,6982 } },
  5064. { "FUJIFILM FinePix S5100", 0, 0x3e00,
  5065. { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
  5066. { "FUJIFILM FinePix S5500", 0, 0x3e00,
  5067. { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
  5068. { "FUJIFILM FinePix S5200", 0, 0,
  5069. { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
  5070. { "FUJIFILM FinePix S5600", 0, 0,
  5071. { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
  5072. { "FUJIFILM FinePix S6", 0, 0,
  5073. { 12628,-4887,-1401,-6861,14996,1962,-2198,2782,7091 } },
  5074. { "FUJIFILM FinePix S7000", 0, 0,
  5075. { 10190,-3506,-1312,-7153,15051,2238,-2003,2399,7505 } },
  5076. { "FUJIFILM FinePix S9000", 0, 0,
  5077. { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
  5078. { "FUJIFILM FinePix S9500", 0, 0,
  5079. { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
  5080. { "FUJIFILM FinePix S9100", 0, 0,
  5081. { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
  5082. { "FUJIFILM FinePix S9600", 0, 0,
  5083. { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
  5084. { "FUJIFILM IS-1", 0, 0,
  5085. { 21461,-10807,-1441,-2332,10599,1999,289,875,7703 } },
  5086. { "FUJIFILM IS Pro", 0, 0,
  5087. { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
  5088. { "Imacon Ixpress", 0, 0, /* DJC */
  5089. { 7025,-1415,-704,-5188,13765,1424,-1248,2742,6038 } },
  5090. { "KODAK NC2000", 0, 0,
  5091. { 13891,-6055,-803,-465,9919,642,2121,82,1291 } },
  5092. { "Kodak DCS315C", 8, 0,
  5093. { 17523,-4827,-2510,756,8546,-137,6113,1649,2250 } },
  5094. { "Kodak DCS330C", 8, 0,
  5095. { 20620,-7572,-2801,-103,10073,-396,3551,-233,2220 } },
  5096. { "KODAK DCS420", 0, 0,
  5097. { 10868,-1852,-644,-1537,11083,484,2343,628,2216 } },
  5098. { "KODAK DCS460", 0, 0,
  5099. { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
  5100. { "KODAK EOSDCS1", 0, 0,
  5101. { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
  5102. { "KODAK EOSDCS3B", 0, 0,
  5103. { 9898,-2700,-940,-2478,12219,206,1985,634,1031 } },
  5104. { "Kodak DCS520C", 180, 0,
  5105. { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
  5106. { "Kodak DCS560C", 188, 0,
  5107. { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
  5108. { "Kodak DCS620C", 180, 0,
  5109. { 23617,-10175,-3149,-2054,11749,-272,2586,-489,3453 } },
  5110. { "Kodak DCS620X", 185, 0,
  5111. { 13095,-6231,154,12221,-21,-2137,895,4602,2258 } },
  5112. { "Kodak DCS660C", 214, 0,
  5113. { 18244,-6351,-2739,-791,11193,-521,3711,-129,2802 } },
  5114. { "Kodak DCS720X", 0, 0,
  5115. { 11775,-5884,950,9556,1846,-1286,-1019,6221,2728 } },
  5116. { "Kodak DCS760C", 0, 0,
  5117. { 16623,-6309,-1411,-4344,13923,323,2285,274,2926 } },
  5118. { "Kodak DCS Pro SLR", 0, 0,
  5119. { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
  5120. { "Kodak DCS Pro 14nx", 0, 0,
  5121. { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
  5122. { "Kodak DCS Pro 14", 0, 0,
  5123. { 7791,3128,-776,-8588,16458,2039,-2455,4006,6198 } },
  5124. { "Kodak ProBack645", 0, 0,
  5125. { 16414,-6060,-1470,-3555,13037,473,2545,122,4948 } },
  5126. { "Kodak ProBack", 0, 0,
  5127. { 21179,-8316,-2918,-915,11019,-165,3477,-180,4210 } },
  5128. { "KODAK P712", 0, 0,
  5129. { 9658,-3314,-823,-5163,12695,2768,-1342,1843,6044 } },
  5130. { "KODAK P850", 0, 0xf7c,
  5131. { 10511,-3836,-1102,-6946,14587,2558,-1481,1792,6246 } },
  5132. { "KODAK P880", 0, 0xfff,
  5133. { 12805,-4662,-1376,-7480,15267,2360,-1626,2194,7904 } },
  5134. { "Leaf CMost", 0, 0,
  5135. { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
  5136. { "Leaf Valeo 6", 0, 0,
  5137. { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
  5138. { "Leaf Aptus 54S", 0, 0,
  5139. { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
  5140. { "Leaf Aptus 65", 0, 0,
  5141. { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
  5142. { "Leaf Aptus 75", 0, 0,
  5143. { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
  5144. { "Leaf", 0, 0,
  5145. { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
  5146. { "Mamiya ZD", 0, 0,
  5147. { 7645,2579,-1363,-8689,16717,2015,-3712,5941,5961 } },
  5148. { "Micron 2010", 110, 0, /* DJC */
  5149. { 16695,-3761,-2151,155,9682,163,3433,951,4904 } },
  5150. { "Minolta DiMAGE 5", 0, 0xf7d,
  5151. { 8983,-2942,-963,-6556,14476,2237,-2426,2887,8014 } },
  5152. { "Minolta DiMAGE 7Hi", 0, 0xf7d,
  5153. { 11368,-3894,-1242,-6521,14358,2339,-2475,3056,7285 } },
  5154. { "Minolta DiMAGE 7", 0, 0xf7d,
  5155. { 9144,-2777,-998,-6676,14556,2281,-2470,3019,7744 } },
  5156. { "Minolta DiMAGE A1", 0, 0xf8b,
  5157. { 9274,-2547,-1167,-8220,16323,1943,-2273,2720,8340 } },
  5158. { "MINOLTA DiMAGE A200", 0, 0,
  5159. { 8560,-2487,-986,-8112,15535,2771,-1209,1324,7743 } },
  5160. { "Minolta DiMAGE A2", 0, 0xf8f,
  5161. { 9097,-2726,-1053,-8073,15506,2762,-966,981,7763 } },
  5162. { "Minolta DiMAGE Z2", 0, 0, /* DJC */
  5163. { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
  5164. { "MINOLTA DYNAX 5", 0, 0xffb,
  5165. { 10284,-3283,-1086,-7957,15762,2316,-829,882,6644 } },
  5166. { "MINOLTA DYNAX 7", 0, 0xffb,
  5167. { 10239,-3104,-1099,-8037,15727,2451,-927,925,6871 } },
  5168. { "NIKON D100", 0, 0,
  5169. { 5902,-933,-782,-8983,16719,2354,-1402,1455,6464 } },
  5170. { "NIKON D1H", 0, 0,
  5171. { 7577,-2166,-926,-7454,15592,1934,-2377,2808,8606 } },
  5172. { "NIKON D1X", 0, 0,
  5173. { 7702,-2245,-975,-9114,17242,1875,-2679,3055,8521 } },
  5174. { "NIKON D1", 0, 0, /* multiplied by 2.218750, 1.0, 1.148438 */
  5175. { 16772,-4726,-2141,-7611,15713,1972,-2846,3494,9521 } },
  5176. { "NIKON D2H", 0, 0,
  5177. { 5710,-901,-615,-8594,16617,2024,-2975,4120,6830 } },
  5178. { "NIKON D2X", 0, 0,
  5179. { 10231,-2769,-1255,-8301,15900,2552,-797,680,7148 } },
  5180. { "NIKON D40X", 0, 0,
  5181. { 8819,-2543,-911,-9025,16928,2151,-1329,1213,8449 } },
  5182. { "NIKON D40", 0, 0,
  5183. { 6992,-1668,-806,-8138,15748,2543,-874,850,7897 } },
  5184. { "NIKON D50", 0, 0,
  5185. { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
  5186. { "NIKON D60", 0, 0,
  5187. { 8736,-2458,-935,-9075,16894,2251,-1354,1242,8263 } },
  5188. { "NIKON D700", 0, 0,
  5189. { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
  5190. { "NIKON D70", 0, 0,
  5191. { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
  5192. { "NIKON D80", 0, 0,
  5193. { 8629,-2410,-883,-9055,16940,2171,-1490,1363,8520 } },
  5194. { "NIKON D90", 0, 0xf00,
  5195. { 7309,-1403,-519,-8474,16008,2622,-2434,2826,8064 } },
  5196. { "NIKON D200", 0, 0xfbc,
  5197. { 8367,-2248,-763,-8758,16447,2422,-1527,1550,8053 } },
  5198. { "NIKON D300", 0, 0,
  5199. { 9030,-1992,-715,-8465,16302,2255,-2689,3217,8069 } },
  5200. { "NIKON D3", 0, 0,
  5201. { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
  5202. { "NIKON E950", 0, 0x3dd, /* DJC */
  5203. { -3746,10611,1665,9621,-1734,2114,-2389,7082,3064,3406,6116,-244 } },
  5204. { "NIKON E995", 0, 0, /* copied from E5000 */
  5205. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  5206. { "NIKON E2100", 0, 0, /* copied from Z2, new white balance */
  5207. { 13142,-4152,-1596,-4655,12374,2282,-1769,2696,6711} },
  5208. { "NIKON E2500", 0, 0,
  5209. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  5210. { "NIKON E4300", 0, 0, /* copied from Minolta DiMAGE Z2 */
  5211. { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
  5212. { "NIKON E4500", 0, 0,
  5213. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  5214. { "NIKON E5000", 0, 0,
  5215. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  5216. { "NIKON E5400", 0, 0,
  5217. { 9349,-2987,-1001,-7919,15766,2266,-2098,2680,6839 } },
  5218. { "NIKON E5700", 0, 0,
  5219. { -5368,11478,2368,5537,-113,3148,-4969,10021,5782,778,9028,211 } },
  5220. { "NIKON E8400", 0, 0,
  5221. { 7842,-2320,-992,-8154,15718,2599,-1098,1342,7560 } },
  5222. { "NIKON E8700", 0, 0,
  5223. { 8489,-2583,-1036,-8051,15583,2643,-1307,1407,7354 } },
  5224. { "NIKON E8800", 0, 0,
  5225. { 7971,-2314,-913,-8451,15762,2894,-1442,1520,7610 } },
  5226. { "NIKON COOLPIX P6000", 0, 0,
  5227. { 9698,-3367,-914,-4706,12584,2368,-837,968,5801 } },
  5228. { "OLYMPUS C5050", 0, 0,
  5229. { 10508,-3124,-1273,-6079,14294,1901,-1653,2306,6237 } },
  5230. { "OLYMPUS C5060", 0, 0,
  5231. { 10445,-3362,-1307,-7662,15690,2058,-1135,1176,7602 } },
  5232. { "OLYMPUS C7070", 0, 0,
  5233. { 10252,-3531,-1095,-7114,14850,2436,-1451,1723,6365 } },
  5234. { "OLYMPUS C70", 0, 0,
  5235. { 10793,-3791,-1146,-7498,15177,2488,-1390,1577,7321 } },
  5236. { "OLYMPUS C80", 0, 0,
  5237. { 8606,-2509,-1014,-8238,15714,2703,-942,979,7760 } },
  5238. { "OLYMPUS E-10", 0, 0xffc0,
  5239. { 12745,-4500,-1416,-6062,14542,1580,-1934,2256,6603 } },
  5240. { "OLYMPUS E-1", 0, 0xfff0,
  5241. { 11846,-4767,-945,-7027,15878,1089,-2699,4122,8311 } },
  5242. { "OLYMPUS E-20", 0, 0xffc0,
  5243. { 13173,-4732,-1499,-5807,14036,1895,-2045,2452,7142 } },
  5244. { "OLYMPUS E-300", 0, 0,
  5245. { 7828,-1761,-348,-5788,14071,1830,-2853,4518,6557 } },
  5246. { "OLYMPUS E-330", 0, 0,
  5247. { 8961,-2473,-1084,-7979,15990,2067,-2319,3035,8249 } },
  5248. { "OLYMPUS E-3", 0, 0xf99,
  5249. { 9487,-2875,-1115,-7533,15606,2010,-1618,2100,7389 } },
  5250. { "OLYMPUS E-400", 0, 0xfff0,
  5251. { 6169,-1483,-21,-7107,14761,2536,-2904,3580,8568 } },
  5252. { "OLYMPUS E-410", 0, 0xf6a,
  5253. { 8856,-2582,-1026,-7761,15766,2082,-2009,2575,7469 } },
  5254. { "OLYMPUS E-420", 0, 0xfd7,
  5255. { 8746,-2425,-1095,-7594,15612,2073,-1780,2309,7416 } },
  5256. { "OLYMPUS E-500", 0, 0,
  5257. { 8136,-1968,-299,-5481,13742,1871,-2556,4205,6630 } },
  5258. { "OLYMPUS E-510", 0, 0xf6a,
  5259. { 8785,-2529,-1033,-7639,15624,2112,-1783,2300,7817 } },
  5260. { "OLYMPUS E-520", 0, 0xfd2,
  5261. { 8344,-2322,-1020,-7596,15635,2048,-1748,2269,7287 } },
  5262. { "OLYMPUS SP350", 0, 0,
  5263. { 12078,-4836,-1069,-6671,14306,2578,-786,939,7418 } },
  5264. { "OLYMPUS SP3", 0, 0,
  5265. { 11766,-4445,-1067,-6901,14421,2707,-1029,1217,7572 } },
  5266. { "OLYMPUS SP500UZ", 0, 0xfff,
  5267. { 9493,-3415,-666,-5211,12334,3260,-1548,2262,6482 } },
  5268. { "OLYMPUS SP510UZ", 0, 0xffe,
  5269. { 10593,-3607,-1010,-5881,13127,3084,-1200,1805,6721 } },
  5270. { "OLYMPUS SP550UZ", 0, 0xffe,
  5271. { 11597,-4006,-1049,-5432,12799,2957,-1029,1750,6516 } },
  5272. { "OLYMPUS SP560UZ", 0, 0xff9,
  5273. { 10915,-3677,-982,-5587,12986,2911,-1168,1968,6223 } },
  5274. { "OLYMPUS SP570UZ", 0, 0,
  5275. { 11522,-4044,-1146,-4736,12172,2904,-988,1829,6039 } },
  5276. { "PENTAX *ist DL2", 0, 0,
  5277. { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
  5278. { "PENTAX *ist DL", 0, 0,
  5279. { 10829,-2838,-1115,-8339,15817,2696,-837,680,11939 } },
  5280. { "PENTAX *ist DS2", 0, 0,
  5281. { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
  5282. { "PENTAX *ist DS", 0, 0,
  5283. { 10371,-2333,-1206,-8688,16231,2602,-1230,1116,11282 } },
  5284. { "PENTAX *ist D", 0, 0,
  5285. { 9651,-2059,-1189,-8881,16512,2487,-1460,1345,10687 } },
  5286. { "PENTAX K10D", 0, 0,
  5287. { 9566,-2863,-803,-7170,15172,2112,-818,803,9705 } },
  5288. { "PENTAX K1", 0, 0,
  5289. { 11095,-3157,-1324,-8377,15834,2720,-1108,947,11688 } },
  5290. { "PENTAX K20D", 0, 0,
  5291. { 9427,-2714,-868,-7493,16092,1373,-2199,3264,7180 } },
  5292. { "PENTAX K200D", 0, 0,
  5293. { 9186,-2678,-907,-8693,16517,2260,-1129,1094,8524 } },
  5294. { "PENTAX K2000", 0, 0,
  5295. { 11057,-3604,-1155,-5152,13046,2329,-282,375,8104 } },
  5296. { "PENTAX K-m", 0, 0,
  5297. { 11057,-3604,-1155,-5152,13046,2329,-282,375,8104 } },
  5298. { "Panasonic DMC-FZ8", 0, 0xf7f0,
  5299. { 8986,-2755,-802,-6341,13575,3077,-1476,2144,6379 } },
  5300. { "Panasonic DMC-FZ18", 0, 0,
  5301. { 9932,-3060,-935,-5809,13331,2753,-1267,2155,5575 } },
  5302. { "Panasonic DMC-FZ28", 15, 0xfff,
  5303. { 10109,-3488,-993,-5412,12812,2916,-1305,2140,5543 } },
  5304. { "Panasonic DMC-FZ30", 0, 0xf94c,
  5305. { 10976,-4029,-1141,-7918,15491,2600,-1670,2071,8246 } },
  5306. { "Panasonic DMC-FZ50", 0, 0xfff0, /* aka "LEICA V-LUX1" */
  5307. { 7906,-2709,-594,-6231,13351,3220,-1922,2631,6537 } },
  5308. { "Panasonic DMC-L10", 15, 0xf96,
  5309. { 8025,-1942,-1050,-7920,15904,2100,-2456,3005,7039 } },
  5310. { "Panasonic DMC-L1", 0, 0xf7fc, /* aka "LEICA DIGILUX 3" */
  5311. { 8054,-1885,-1025,-8349,16367,2040,-2805,3542,7629 } },
  5312. { "Panasonic DMC-LC1", 0, 0, /* aka "LEICA DIGILUX 2" */
  5313. { 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
  5314. { "Panasonic DMC-LX1", 0, 0xf7f0, /* aka "LEICA D-LUX2" */
  5315. { 10704,-4187,-1230,-8314,15952,2501,-920,945,8927 } },
  5316. { "Panasonic DMC-LX2", 0, 0, /* aka "LEICA D-LUX3" */
  5317. { 8048,-2810,-623,-6450,13519,3272,-1700,2146,7049 } },
  5318. { "Panasonic DMC-LX3", 15, 0xfff, /* aka "LEICA D-LUX4" */
  5319. { 8128,-2668,-655,-6134,13307,3161,-1782,2568,6083 } },
  5320. { "Panasonic DMC-FX150", 15, 0xfff,
  5321. { 9082,-2907,-925,-6119,13377,3058,-1797,2641,5609 } },
  5322. { "Panasonic DMC-G1", 15, 0xfff,
  5323. { 8199,-2065,-1056,-8124,16156,2033,-2458,3022,7220 } },
  5324. { "Phase One H 20", 0, 0, /* DJC */
  5325. { 1313,1855,-109,-6715,15908,808,-327,1840,6020 } },
  5326. { "Phase One P 2", 0, 0,
  5327. { 2905,732,-237,-8134,16626,1476,-3038,4253,7517 } },
  5328. { "Phase One P 30", 0, 0,
  5329. { 4516,-245,-37,-7020,14976,2173,-3206,4671,7087 } },
  5330. { "Phase One P 45", 0, 0,
  5331. { 5053,-24,-117,-5684,14076,1702,-2619,4492,5849 } },
  5332. { "SAMSUNG GX-1", 0, 0,
  5333. { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
  5334. { "Sinar", 0, 0, /* DJC */
  5335. { 16442,-2956,-2422,-2877,12128,750,-1136,6066,4559 } },
  5336. { "SONY DSC-F828", 491, 0,
  5337. { 7924,-1910,-777,-8226,15459,2998,-1517,2199,6818,-7242,11401,3481 } },
  5338. { "SONY DSC-R1", 512, 0,
  5339. { 8512,-2641,-694,-8042,15670,2526,-1821,2117,7414 } },
  5340. { "SONY DSC-V3", 0, 0,
  5341. { 7511,-2571,-692,-7894,15088,3060,-948,1111,8128 } },
  5342. { "SONY DSLR-A100", 0, 0xfeb,
  5343. { 9437,-2811,-774,-8405,16215,2290,-710,596,7181 } },
  5344. { "SONY DSLR-A200", 0, 0,
  5345. { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
  5346. { "SONY DSLR-A300", 0, 0,
  5347. { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
  5348. { "SONY DSLR-A350", 0, 0xffc,
  5349. { 6038,-1484,-578,-9146,16746,2513,-875,746,7217 } },
  5350. { "SONY DSLR-A700", 254, 0x1ffe,
  5351. { 5775,-805,-359,-8574,16295,2391,-1943,2341,7249 } },
  5352. { "SONY DSLR-A900", 254, 0x1ffe,
  5353. { 5209,-1072,-397,-8845,16120,2919,-1618,1803,8654 } }
  5354. };
  5355. double cam_xyz[4][3];
  5356. char name[130];
  5357. int i, j;
  5358. sprintf (name, "%s %s", p_make, p_model);
  5359. for (i=0; i < sizeof table / sizeof *table; i++)
  5360. if (!strncmp (name, table[i].prefix, strlen(table[i].prefix))) {
  5361. if (table[i].t_black) black = (ushort) table[i].t_black;
  5362. if (table[i].t_maximum) maximum = (ushort) table[i].t_maximum;
  5363. for (j=0; j < 12; j++)
  5364. #ifdef LIBRAW_LIBRARY_BUILD
  5365. imgdata.color.cam_xyz[0][j] =
  5366. #endif
  5367. cam_xyz[0][j] = table[i].trans[j] / 10000.0;
  5368. cam_xyz_coeff (cam_xyz);
  5369. break;
  5370. }
  5371. }
  5372. void CLASS simple_coeff (int index)
  5373. {
  5374. static const float table[][12] = {
  5375. /* index 0 -- all Foveon cameras */
  5376. { 1.4032,-0.2231,-0.1016,-0.5263,1.4816,0.017,-0.0112,0.0183,0.9113 },
  5377. /* index 1 -- Kodak DC20 and DC25 */
  5378. { 2.25,0.75,-1.75,-0.25,-0.25,0.75,0.75,-0.25,-0.25,-1.75,0.75,2.25 },
  5379. /* index 2 -- Logitech Fotoman Pixtura */
  5380. { 1.893,-0.418,-0.476,-0.495,1.773,-0.278,-1.017,-0.655,2.672 },
  5381. /* index 3 -- Nikon E880, E900, and E990 */
  5382. { -1.936280, 1.800443, -1.448486, 2.584324,
  5383. 1.405365, -0.524955, -0.289090, 0.408680,
  5384. -1.204965, 1.082304, 2.941367, -1.818705 }
  5385. };
  5386. int i, c;
  5387. for (raw_color = i=0; i < 3; i++)
  5388. FORCC rgb_cam[i][c] = table[index][i*colors+c];
  5389. }
  5390. short CLASS guess_byte_order (int words)
  5391. {
  5392. uchar test[4][2];
  5393. int t=2, msb;
  5394. double diff, sum[2] = {0,0};
  5395. fread (test[0], 2, 2, ifp);
  5396. for (words-=2; words--; ) {
  5397. fread (test[t], 2, 1, ifp);
  5398. for (msb=0; msb < 2; msb++) {
  5399. diff = (test[t^2][msb] << 8 | test[t^2][!msb])
  5400. - (test[t ][msb] << 8 | test[t ][!msb]);
  5401. sum[msb] += diff*diff;
  5402. }
  5403. t = (t+1) & 3;
  5404. }
  5405. return sum[0] < sum[1] ? 0x4d4d : 0x4949;
  5406. }
  5407. /*
  5408. Identify which camera created this file, and set global variables
  5409. accordingly.
  5410. */
  5411. void CLASS identify()
  5412. {
  5413. char head[32], *cp;
  5414. unsigned hlen, fsize, i, c, is_canon;
  5415. struct jhead jh;
  5416. static const struct {
  5417. int fsize;
  5418. char t_make[12], t_model[19], withjpeg;
  5419. } table[] = {
  5420. { 62464, "Kodak", "DC20" ,0 },
  5421. { 124928, "Kodak", "DC20" ,0 },
  5422. { 1652736, "Kodak", "DCS200" ,0 },
  5423. { 4159302, "Kodak", "C330" ,0 },
  5424. { 4162462, "Kodak", "C330" ,0 },
  5425. { 460800, "Kodak", "C603v" ,0 },
  5426. { 614400, "Kodak", "C603v" ,0 },
  5427. { 6163328, "Kodak", "C603" ,0 },
  5428. { 6166488, "Kodak", "C603" ,0 },
  5429. { 9116448, "Kodak", "C603y" ,0 },
  5430. { 311696, "ST Micro", "STV680 VGA" ,0 }, /* SPYz */
  5431. { 614400, "Kodak", "KAI-0340" ,0 },
  5432. { 787456, "Creative", "PC-CAM 600" ,0 },
  5433. { 1138688, "Minolta", "RD175" ,0 },
  5434. { 3840000, "Foculus", "531C" ,0 },
  5435. { 786432, "AVT", "F-080C" ,0 },
  5436. { 1447680, "AVT", "F-145C" ,0 },
  5437. { 1920000, "AVT", "F-201C" ,0 },
  5438. { 5067304, "AVT", "F-510C" ,0 },
  5439. { 10134608, "AVT", "F-510C" ,0 },
  5440. { 16157136, "AVT", "F-810C" ,0 },
  5441. { 1409024, "Sony", "XCD-SX910CR" ,0 },
  5442. { 2818048, "Sony", "XCD-SX910CR" ,0 },
  5443. { 3884928, "Micron", "2010" ,0 },
  5444. { 6624000, "Pixelink", "A782" ,0 },
  5445. { 13248000, "Pixelink", "A782" ,0 },
  5446. { 6291456, "RoverShot","3320AF" ,0 },
  5447. { 6553440, "Canon", "PowerShot A460" ,0 },
  5448. { 6653280, "Canon", "PowerShot A530" ,0 },
  5449. { 6573120, "Canon", "PowerShot A610" ,0 },
  5450. { 9219600, "Canon", "PowerShot A620" ,0 },
  5451. { 10341600, "Canon", "PowerShot A720" ,0 },
  5452. { 10383120, "Canon", "PowerShot A630" ,0 },
  5453. { 12945240, "Canon", "PowerShot A640" ,0 },
  5454. { 15636240, "Canon", "PowerShot A650" ,0 },
  5455. { 5298000, "Canon", "PowerShot SD300" ,0 },
  5456. { 7710960, "Canon", "PowerShot S3 IS" ,0 },
  5457. { 5939200, "OLYMPUS", "C770UZ" ,0 },
  5458. { 1581060, "NIKON", "E900" ,1 }, /* or E900s,E910 */
  5459. { 2465792, "NIKON", "E950" ,1 }, /* or E800,E700 */
  5460. { 2940928, "NIKON", "E2100" ,1 }, /* or E2500 */
  5461. { 4771840, "NIKON", "E990" ,1 }, /* or E995, Oly C3030Z */
  5462. { 4775936, "NIKON", "E3700" ,1 }, /* or Optio 33WR */
  5463. { 5869568, "NIKON", "E4300" ,1 }, /* or DiMAGE Z2 */
  5464. { 5865472, "NIKON", "E4500" ,1 },
  5465. { 7438336, "NIKON", "E5000" ,1 }, /* or E5700 */
  5466. { 8998912, "NIKON", "COOLPIX S6" ,1 },
  5467. { 1976352, "CASIO", "QV-2000UX" ,1 },
  5468. { 3217760, "CASIO", "QV-3*00EX" ,1 },
  5469. { 6218368, "CASIO", "QV-5700" ,1 },
  5470. { 6054400, "CASIO", "QV-R41" ,1 },
  5471. { 7530816, "CASIO", "QV-R51" ,1 },
  5472. { 7684000, "CASIO", "QV-4000" ,1 },
  5473. { 4948608, "CASIO", "EX-S100" ,1 },
  5474. { 7542528, "CASIO", "EX-Z50" ,1 },
  5475. { 7753344, "CASIO", "EX-Z55" ,1 },
  5476. { 7426656, "CASIO", "EX-P505" ,1 },
  5477. { 9313536, "CASIO", "EX-P600" ,1 },
  5478. { 10979200, "CASIO", "EX-P700" ,1 },
  5479. { 3178560, "PENTAX", "Optio S" ,1 },
  5480. { 4841984, "PENTAX", "Optio S" ,1 },
  5481. { 6114240, "PENTAX", "Optio S4" ,1 }, /* or S4i, CASIO EX-Z4 */
  5482. { 10702848, "PENTAX", "Optio 750Z" ,1 },
  5483. { 16098048, "SAMSUNG", "S85" ,1 },
  5484. { 16215552, "SAMSUNG", "S85" ,1 },
  5485. { 12582980, "Sinar", "" ,0 },
  5486. { 33292868, "Sinar", "" ,0 },
  5487. { 44390468, "Sinar", "" ,0 } };
  5488. static const char *corp[] =
  5489. { "Canon", "NIKON", "EPSON", "KODAK", "Kodak", "OLYMPUS", "PENTAX",
  5490. "MINOLTA", "Minolta", "Konica", "CASIO", "Sinar", "Phase One",
  5491. "SAMSUNG", "Mamiya" };
  5492. #ifdef LIBRAW_LIBRARY_BUILD
  5493. RUN_CALLBACK(LIBRAW_PROGRESS_IDENTIFY,0,2);
  5494. #endif
  5495. tiff_flip = flip = filters = -1; /* 0 is valid, so -1 is unknown */
  5496. raw_height = raw_width = fuji_width = fuji_layout = cr2_slice[0] = 0;
  5497. maximum = height = width = top_margin = left_margin = 0;
  5498. cdesc[0] = desc[0] = artist[0] = make[0] = model[0] = model2[0] = 0;
  5499. iso_speed = shutter = aperture = focal_len = unique_id = 0;
  5500. memset (gpsdata, 0, sizeof gpsdata);
  5501. memset (white, 0, sizeof white);
  5502. thumb_offset = thumb_length = thumb_width = thumb_height = 0;
  5503. load_raw = thumb_load_raw = 0;
  5504. write_thumb = &CLASS jpeg_thumb;
  5505. data_offset = meta_length = tiff_bps = tiff_compress = 0;
  5506. kodak_cbpp = zero_after_ff = dng_version = load_flags = 0;
  5507. timestamp = shot_order = tiff_samples = black = 0;
  5508. mix_green = profile_length = data_error = zero_is_bad = 0;
  5509. pixel_aspect = is_raw = raw_color = use_gamma = 1;
  5510. tile_width = tile_length = INT_MAX;
  5511. for (i=0; i < 4; i++) {
  5512. cam_mul[i] = i == 1;
  5513. pre_mul[i] = i < 3;
  5514. FORC3 cmatrix[c][i] = 0;
  5515. FORC3 rgb_cam[c][i] = c == i;
  5516. }
  5517. colors = 3;
  5518. tiff_bps = 12;
  5519. for (i=0; i < 0x4000; i++) curve[i] = i;
  5520. order = get2();
  5521. hlen = get4();
  5522. fseek (ifp, 0, SEEK_SET);
  5523. fread (head, 1, 32, ifp);
  5524. fseek (ifp, 0, SEEK_END);
  5525. fsize = ftell(ifp);
  5526. if ((cp = (char *) memmem (head, 32, "MMMM", 4)) ||
  5527. (cp = (char *) memmem (head, 32, "IIII", 4))) {
  5528. parse_phase_one (cp-head);
  5529. if (cp-head) parse_tiff(0);
  5530. } else if (order == 0x4949 || order == 0x4d4d) {
  5531. if (!memcmp (head+6,"HEAPCCDR",8)) {
  5532. data_offset = hlen;
  5533. parse_ciff (hlen, fsize - hlen);
  5534. } else {
  5535. parse_tiff(0);
  5536. }
  5537. } else if (!memcmp (head,"\xff\xd8\xff\xe1",4) &&
  5538. !memcmp (head+6,"Exif",4)) {
  5539. fseek (ifp, 4, SEEK_SET);
  5540. data_offset = 4 + get2();
  5541. fseek (ifp, data_offset, SEEK_SET);
  5542. if (fgetc(ifp) != 0xff)
  5543. parse_tiff(12);
  5544. thumb_offset = 0;
  5545. } else if (!memcmp (head+25,"ARECOYK",7)) {
  5546. strcpy (make, "Contax");
  5547. strcpy (model,"N Digital");
  5548. fseek (ifp, 33, SEEK_SET);
  5549. get_timestamp(1);
  5550. fseek (ifp, 60, SEEK_SET);
  5551. FORC4 cam_mul[c ^ (c >> 1)] = get4();
  5552. } else if (!strcmp (head, "PXN")) {
  5553. strcpy (make, "Logitech");
  5554. strcpy (model,"Fotoman Pixtura");
  5555. } else if (!strcmp (head, "qktk")) {
  5556. strcpy (make, "Apple");
  5557. strcpy (model,"QuickTake 100");
  5558. } else if (!strcmp (head, "qktn")) {
  5559. strcpy (make, "Apple");
  5560. strcpy (model,"QuickTake 150");
  5561. } else if (!memcmp (head,"FUJIFILM",8)) {
  5562. fseek (ifp, 84, SEEK_SET);
  5563. thumb_offset = get4();
  5564. thumb_length = get4();
  5565. fseek (ifp, 92, SEEK_SET);
  5566. parse_fuji (get4());
  5567. if (thumb_offset > 120) {
  5568. fseek (ifp, 120, SEEK_SET);
  5569. is_raw += (i = get4()) && 1;
  5570. if (is_raw == 2 && shot_select)
  5571. parse_fuji (i);
  5572. }
  5573. fseek (ifp, 100, SEEK_SET);
  5574. data_offset = get4();
  5575. parse_tiff (thumb_offset+12);
  5576. } else if (!memcmp (head,"RIFF",4)) {
  5577. fseek (ifp, 0, SEEK_SET);
  5578. parse_riff();
  5579. } else if (!memcmp (head,"\0\001\0\001\0@",6)) {
  5580. fseek (ifp, 6, SEEK_SET);
  5581. fread (make, 1, 8, ifp);
  5582. fread (model, 1, 8, ifp);
  5583. fread (model2, 1, 16, ifp);
  5584. data_offset = get2();
  5585. get2();
  5586. raw_width = get2();
  5587. raw_height = get2();
  5588. load_raw = &CLASS nokia_load_raw;
  5589. filters = 0x61616161;
  5590. } else if (!memcmp (head,"DSC-Image",9))
  5591. parse_rollei();
  5592. else if (!memcmp (head,"PWAD",4))
  5593. parse_sinar_ia();
  5594. else if (!memcmp (head,"\0MRM",4))
  5595. parse_minolta(0);
  5596. else if (!memcmp (head,"CI",2))
  5597. parse_cine();
  5598. else
  5599. for (i=0; i < sizeof table / sizeof *table; i++)
  5600. if (fsize == table[i].fsize) {
  5601. strcpy (make, table[i].t_make );
  5602. strcpy (model, table[i].t_model);
  5603. if (table[i].withjpeg)
  5604. parse_external_jpeg();
  5605. }
  5606. if (make[0] == 0) parse_smal (0, fsize);
  5607. if (make[0] == 0) parse_jpeg (is_raw = 0);
  5608. for (i=0; i < sizeof corp / sizeof *corp; i++)
  5609. if (strstr (make, corp[i])) /* Simplify company names */
  5610. strcpy (make, corp[i]);
  5611. if (!strncmp (make,"KODAK",5))
  5612. make[16] = model[16] = 0;
  5613. cp = make + strlen(make); /* Remove trailing spaces */
  5614. while (*--cp == ' ') *cp = 0;
  5615. cp = model + strlen(model);
  5616. while (*--cp == ' ') *cp = 0;
  5617. i = strlen(make); /* Remove make from model */
  5618. if (!strncasecmp (model, make, i) && model[i++] == ' ')
  5619. memmove (model, model+i, 64-i);
  5620. if (!strncmp (model,"Digital Camera ",15))
  5621. strcpy (model, model+15);
  5622. desc[511] = artist[63] = make[63] = model[63] = model2[63] = 0;
  5623. if (!is_raw) goto notraw;
  5624. if (!maximum) maximum = (1 << tiff_bps) - 1;
  5625. if (!height) height = raw_height;
  5626. if (!width) width = raw_width;
  5627. if (fuji_width) {
  5628. width = height + fuji_width;
  5629. height = width - 1;
  5630. pixel_aspect = 1;
  5631. }
  5632. if (height == 2624 && width == 3936) /* Pentax K10D and Samsung GX10 */
  5633. { height = 2616; width = 3896; }
  5634. if (height == 3136 && width == 4864) /* Pentax K20D */
  5635. { height = 3124; width = 4688; }
  5636. if (height == 3014 && width == 4096) /* Ricoh GX200 */
  5637. width = 4014;
  5638. if (dng_version) {
  5639. if (filters == UINT_MAX) filters = 0;
  5640. if (filters) is_raw = tiff_samples;
  5641. else colors = tiff_samples;
  5642. if (tiff_compress == 1)
  5643. load_raw = &CLASS adobe_dng_load_raw_nc;
  5644. if (tiff_compress == 7)
  5645. load_raw = &CLASS adobe_dng_load_raw_lj;
  5646. goto dng_skip;
  5647. }
  5648. if ((is_canon = !strcmp(make,"Canon")))
  5649. load_raw = memcmp (head+6,"HEAPCCDR",8) ?
  5650. &CLASS lossless_jpeg_load_raw : &CLASS canon_compressed_load_raw;
  5651. if (!strcmp(make,"NIKON")) {
  5652. if (!load_raw)
  5653. load_raw = &CLASS packed_12_load_raw;
  5654. if (model[0] == 'E')
  5655. load_flags |= !data_offset << 2 | 2;
  5656. }
  5657. if (!strcmp(make,"CASIO")) {
  5658. load_raw = &CLASS packed_12_load_raw;
  5659. maximum = 0xf7f;
  5660. }
  5661. /* Set parameters based on camera name (for non-DNG files). */
  5662. if (is_canon && tiff_bps == 15) {
  5663. switch (width) {
  5664. case 3344: width -= 66;
  5665. case 3872: width -= 6;
  5666. }
  5667. filters = 0;
  5668. load_raw = &CLASS canon_sraw_load_raw;
  5669. } else if (!strcmp(model,"PowerShot 600")) {
  5670. height = 613;
  5671. width = 854;
  5672. raw_width = 896;
  5673. pixel_aspect = 607/628.0;
  5674. colors = 4;
  5675. filters = 0xe1e4e1e4;
  5676. load_raw = &CLASS canon_600_load_raw;
  5677. } else if (!strcmp(model,"PowerShot A5") ||
  5678. !strcmp(model,"PowerShot A5 Zoom")) {
  5679. height = 773;
  5680. width = 960;
  5681. raw_width = 992;
  5682. pixel_aspect = 256/235.0;
  5683. colors = 4;
  5684. filters = 0x1e4e1e4e;
  5685. load_raw = &CLASS canon_a5_load_raw;
  5686. } else if (!strcmp(model,"PowerShot A50")) {
  5687. height = 968;
  5688. width = 1290;
  5689. raw_width = 1320;
  5690. colors = 4;
  5691. filters = 0x1b4e4b1e;
  5692. load_raw = &CLASS canon_a5_load_raw;
  5693. } else if (!strcmp(model,"PowerShot Pro70")) {
  5694. height = 1024;
  5695. width = 1552;
  5696. colors = 4;
  5697. filters = 0x1e4b4e1b;
  5698. load_raw = &CLASS canon_a5_load_raw;
  5699. } else if (!strcmp(model,"PowerShot SD300")) {
  5700. height = 1752;
  5701. width = 2344;
  5702. raw_height = 1766;
  5703. raw_width = 2400;
  5704. top_margin = 12;
  5705. left_margin = 12;
  5706. load_raw = &CLASS canon_a5_load_raw;
  5707. } else if (!strcmp(model,"PowerShot A460")) {
  5708. height = 1960;
  5709. width = 2616;
  5710. raw_height = 1968;
  5711. raw_width = 2664;
  5712. top_margin = 4;
  5713. left_margin = 4;
  5714. load_raw = &CLASS canon_a5_load_raw;
  5715. } else if (!strcmp(model,"PowerShot A530")) {
  5716. height = 1984;
  5717. width = 2620;
  5718. raw_height = 1992;
  5719. raw_width = 2672;
  5720. top_margin = 6;
  5721. left_margin = 10;
  5722. load_raw = &CLASS canon_a5_load_raw;
  5723. raw_color = 0;
  5724. } else if (!strcmp(model,"PowerShot A610")) {
  5725. if (canon_s2is()) strcpy (model+10, "S2 IS");
  5726. height = 1960;
  5727. width = 2616;
  5728. raw_height = 1968;
  5729. raw_width = 2672;
  5730. top_margin = 8;
  5731. left_margin = 12;
  5732. load_raw = &CLASS canon_a5_load_raw;
  5733. } else if (!strcmp(model,"PowerShot A620")) {
  5734. height = 2328;
  5735. width = 3112;
  5736. raw_height = 2340;
  5737. raw_width = 3152;
  5738. top_margin = 12;
  5739. left_margin = 36;
  5740. load_raw = &CLASS canon_a5_load_raw;
  5741. } else if (!strcmp(model,"PowerShot A720")) {
  5742. height = 2472;
  5743. width = 3298;
  5744. raw_height = 2480;
  5745. raw_width = 3336;
  5746. top_margin = 5;
  5747. left_margin = 6;
  5748. load_raw = &CLASS canon_a5_load_raw;
  5749. } else if (!strcmp(model,"PowerShot A630")) {
  5750. height = 2472;
  5751. width = 3288;
  5752. raw_height = 2484;
  5753. raw_width = 3344;
  5754. top_margin = 6;
  5755. left_margin = 12;
  5756. load_raw = &CLASS canon_a5_load_raw;
  5757. } else if (!strcmp(model,"PowerShot A640")) {
  5758. height = 2760;
  5759. width = 3672;
  5760. raw_height = 2772;
  5761. raw_width = 3736;
  5762. top_margin = 6;
  5763. left_margin = 12;
  5764. load_raw = &CLASS canon_a5_load_raw;
  5765. } else if (!strcmp(model,"PowerShot A650")) {
  5766. height = 3024;
  5767. width = 4032;
  5768. raw_height = 3048;
  5769. raw_width = 4104;
  5770. top_margin = 12;
  5771. left_margin = 48;
  5772. load_raw = &CLASS canon_a5_load_raw;
  5773. } else if (!strcmp(model,"PowerShot S3 IS")) {
  5774. height = 2128;
  5775. width = 2840;
  5776. raw_height = 2136;
  5777. raw_width = 2888;
  5778. top_margin = 8;
  5779. left_margin = 44;
  5780. load_raw = &CLASS canon_a5_load_raw;
  5781. } else if (!strcmp(model,"PowerShot Pro90 IS")) {
  5782. width = 1896;
  5783. colors = 4;
  5784. filters = 0xb4b4b4b4;
  5785. } else if (is_canon && raw_width == 2144) {
  5786. height = 1550;
  5787. width = 2088;
  5788. top_margin = 8;
  5789. left_margin = 4;
  5790. if (!strcmp(model,"PowerShot G1")) {
  5791. colors = 4;
  5792. filters = 0xb4b4b4b4;
  5793. }
  5794. } else if (is_canon && raw_width == 2224) {
  5795. height = 1448;
  5796. width = 2176;
  5797. top_margin = 6;
  5798. left_margin = 48;
  5799. } else if (is_canon && raw_width == 2376) {
  5800. height = 1720;
  5801. width = 2312;
  5802. top_margin = 6;
  5803. left_margin = 12;
  5804. } else if (is_canon && raw_width == 2672) {
  5805. height = 1960;
  5806. width = 2616;
  5807. top_margin = 6;
  5808. left_margin = 12;
  5809. } else if (is_canon && raw_width == 3152) {
  5810. height = 2056;
  5811. width = 3088;
  5812. top_margin = 12;
  5813. left_margin = 64;
  5814. if (unique_id == 0x80000170)
  5815. adobe_coeff ("Canon","EOS 300D");
  5816. } else if (is_canon && raw_width == 3160) {
  5817. height = 2328;
  5818. width = 3112;
  5819. top_margin = 12;
  5820. left_margin = 44;
  5821. } else if (is_canon && raw_width == 3344) {
  5822. height = 2472;
  5823. width = 3288;
  5824. top_margin = 6;
  5825. left_margin = 4;
  5826. } else if (!strcmp(model,"EOS D2000C")) {
  5827. filters = 0x61616161;
  5828. black = curve[200];
  5829. } else if (is_canon && raw_width == 3516) {
  5830. top_margin = 14;
  5831. left_margin = 42;
  5832. if (unique_id == 0x80000189)
  5833. adobe_coeff ("Canon","EOS 350D");
  5834. goto canon_cr2;
  5835. } else if (is_canon && raw_width == 3596) {
  5836. top_margin = 12;
  5837. left_margin = 74;
  5838. goto canon_cr2;
  5839. } else if (is_canon && raw_width == 3944) {
  5840. height = 2602;
  5841. width = 3908;
  5842. top_margin = 18;
  5843. left_margin = 30;
  5844. } else if (is_canon && raw_width == 3948) {
  5845. top_margin = 18;
  5846. left_margin = 42;
  5847. height -= 2;
  5848. if (unique_id == 0x80000236)
  5849. adobe_coeff ("Canon","EOS 400D");
  5850. if (unique_id == 0x80000254)
  5851. adobe_coeff ("Canon","EOS 1000D");
  5852. goto canon_cr2;
  5853. } else if (is_canon && raw_width == 3984) {
  5854. top_margin = 20;
  5855. left_margin = 76;
  5856. height -= 2;
  5857. goto canon_cr2;
  5858. } else if (is_canon && raw_width == 4104) {
  5859. height = 3024;
  5860. width = 4032;
  5861. top_margin = 12;
  5862. left_margin = 48;
  5863. } else if (is_canon && raw_width == 4312) {
  5864. top_margin = 18;
  5865. left_margin = 22;
  5866. height -= 2;
  5867. if (unique_id == 0x80000176)
  5868. adobe_coeff ("Canon","EOS 450D");
  5869. goto canon_cr2;
  5870. } else if (is_canon && raw_width == 4476) {
  5871. top_margin = 34;
  5872. left_margin = 90;
  5873. goto canon_cr2;
  5874. } else if (is_canon && raw_width == 4480) {
  5875. height = 3326;
  5876. width = 4432;
  5877. top_margin = 10;
  5878. left_margin = 12;
  5879. filters = 0x49494949;
  5880. } else if (is_canon && raw_width == 1208) {
  5881. top_margin = 51;
  5882. left_margin = 62;
  5883. raw_width = width *= 4;
  5884. goto canon_cr2;
  5885. } else if (is_canon && raw_width == 1448) {
  5886. top_margin = 51;
  5887. left_margin = 158;
  5888. raw_width = width *= 4;
  5889. goto canon_cr2;
  5890. } else if (is_canon && raw_width == 5108) {
  5891. top_margin = 13;
  5892. left_margin = 98;
  5893. canon_cr2:
  5894. height -= top_margin;
  5895. width -= left_margin;
  5896. } else if (is_canon && raw_width == 5712) {
  5897. height = 3752;
  5898. width = 5640;
  5899. top_margin = 20;
  5900. left_margin = 62;
  5901. } else if (!strcmp(model,"D1")) {
  5902. cam_mul[0] *= 256/527.0;
  5903. cam_mul[2] *= 256/317.0;
  5904. } else if (!strcmp(model,"D1X")) {
  5905. width -= 4;
  5906. pixel_aspect = 0.5;
  5907. } else if (!strcmp(model,"D40X") ||
  5908. !strcmp(model,"D60") ||
  5909. !strcmp(model,"D80")) {
  5910. height -= 3;
  5911. width -= 4;
  5912. } else if (!strcmp(model,"D3") ||
  5913. !strcmp(model,"D700")) {
  5914. width -= 4;
  5915. left_margin = 2;
  5916. } else if (!strncmp(model,"D40",3) ||
  5917. !strncmp(model,"D50",3) ||
  5918. !strncmp(model,"D70",3)) {
  5919. width--;
  5920. } else if (!strcmp(model,"D90")) {
  5921. width -= 42;
  5922. } else if (!strcmp(model,"D100")) {
  5923. if (tiff_compress == 34713 && !nikon_is_compressed()) {
  5924. load_raw = &CLASS packed_12_load_raw;
  5925. load_flags |= 8;
  5926. raw_width = (width += 3) + 3;
  5927. }
  5928. } else if (!strcmp(model,"D200")) {
  5929. left_margin = 1;
  5930. width -= 4;
  5931. filters = 0x94949494;
  5932. } else if (!strncmp(model,"D2H",3)) {
  5933. left_margin = 6;
  5934. width -= 14;
  5935. } else if (!strncmp(model,"D2X",3)) {
  5936. if (width == 3264) width -= 32;
  5937. else width -= 8;
  5938. } else if (!strcmp(model,"D300")) {
  5939. width -= 32;
  5940. } else if (!strcmp(model,"COOLPIX P6000")) {
  5941. load_flags = 1;
  5942. filters = 0x94949494;
  5943. } else if (fsize == 1581060) {
  5944. height = 963;
  5945. width = 1287;
  5946. raw_width = 1632;
  5947. load_raw = &CLASS nikon_e900_load_raw;
  5948. maximum = 0x3f4;
  5949. colors = 4;
  5950. filters = 0x1e1e1e1e;
  5951. simple_coeff(3);
  5952. pre_mul[0] = 1.2085;
  5953. pre_mul[1] = 1.0943;
  5954. pre_mul[3] = 1.1103;
  5955. } else if (fsize == 2465792) {
  5956. height = 1203;
  5957. width = 1616;
  5958. raw_width = 2048;
  5959. load_raw = &CLASS nikon_e900_load_raw;
  5960. colors = 4;
  5961. filters = 0x4b4b4b4b;
  5962. adobe_coeff ("NIKON","E950");
  5963. } else if (fsize == 4771840) {
  5964. height = 1540;
  5965. width = 2064;
  5966. colors = 4;
  5967. filters = 0xe1e1e1e1;
  5968. load_raw = &CLASS packed_12_load_raw;
  5969. load_flags = 6;
  5970. if (!timestamp && nikon_e995())
  5971. strcpy (model, "E995");
  5972. if (strcmp(model,"E995")) {
  5973. filters = 0xb4b4b4b4;
  5974. simple_coeff(3);
  5975. pre_mul[0] = 1.196;
  5976. pre_mul[1] = 1.246;
  5977. pre_mul[2] = 1.018;
  5978. }
  5979. } else if (!strcmp(model,"E2100")) {
  5980. if (!timestamp && !nikon_e2100()) goto cp_e2500;
  5981. height = 1206;
  5982. width = 1616;
  5983. load_flags = 7;
  5984. } else if (!strcmp(model,"E2500")) {
  5985. cp_e2500:
  5986. strcpy (model, "E2500");
  5987. height = 1204;
  5988. width = 1616;
  5989. colors = 4;
  5990. filters = 0x4b4b4b4b;
  5991. } else if (fsize == 4775936) {
  5992. height = 1542;
  5993. width = 2064;
  5994. load_raw = &CLASS packed_12_load_raw;
  5995. load_flags = 7;
  5996. pre_mul[0] = 1.818;
  5997. pre_mul[2] = 1.618;
  5998. if (!timestamp) nikon_3700();
  5999. if (model[0] == 'E' && atoi(model+1) < 3700)
  6000. filters = 0x49494949;
  6001. if (!strcmp(model,"Optio 33WR")) {
  6002. flip = 1;
  6003. filters = 0x16161616;
  6004. pre_mul[0] = 1.331;
  6005. pre_mul[2] = 1.820;
  6006. }
  6007. } else if (fsize == 5869568) {
  6008. height = 1710;
  6009. width = 2288;
  6010. filters = 0x16161616;
  6011. if (!timestamp && minolta_z2()) {
  6012. strcpy (make, "Minolta");
  6013. strcpy (model,"DiMAGE Z2");
  6014. }
  6015. load_raw = &CLASS packed_12_load_raw;
  6016. load_flags = 6 + (make[0] == 'M');
  6017. } else if (!strcmp(model,"E4500")) {
  6018. height = 1708;
  6019. width = 2288;
  6020. colors = 4;
  6021. filters = 0xb4b4b4b4;
  6022. } else if (fsize == 7438336) {
  6023. height = 1924;
  6024. width = 2576;
  6025. colors = 4;
  6026. filters = 0xb4b4b4b4;
  6027. } else if (fsize == 8998912) {
  6028. height = 2118;
  6029. width = 2832;
  6030. maximum = 0xf83;
  6031. load_raw = &CLASS packed_12_load_raw;
  6032. load_flags = 7;
  6033. } else if (!strcmp(model,"FinePix S5100") ||
  6034. !strcmp(model,"FinePix S5500")) {
  6035. load_raw = &CLASS unpacked_load_raw;
  6036. } else if (!strcmp(make,"FUJIFILM")) {
  6037. if (!strcmp(model+7,"S2Pro")) {
  6038. strcpy (model+7," S2Pro");
  6039. height = 2144;
  6040. width = 2880;
  6041. flip = 6;
  6042. } else
  6043. maximum = 0x3e00;
  6044. if (is_raw == 2 && shot_select)
  6045. maximum = 0x2f00;
  6046. top_margin = (raw_height - height)/2;
  6047. left_margin = (raw_width - width )/2;
  6048. if (is_raw == 2)
  6049. data_offset += (shot_select > 0) * ( fuji_layout ?
  6050. (raw_width *= 2) : raw_height*raw_width*2 );
  6051. fuji_width = width >> !fuji_layout;
  6052. width = (height >> fuji_layout) + fuji_width;
  6053. raw_height = height;
  6054. height = width - 1;
  6055. load_raw = &CLASS fuji_load_raw;
  6056. if (!(fuji_width & 1)) filters = 0x49494949;
  6057. } else if (!strcmp(model,"RD175")) {
  6058. height = 986;
  6059. width = 1534;
  6060. data_offset = 513;
  6061. filters = 0x61616161;
  6062. load_raw = &CLASS minolta_rd175_load_raw;
  6063. } else if (!strcmp(model,"KD-400Z")) {
  6064. height = 1712;
  6065. width = 2312;
  6066. raw_width = 2336;
  6067. goto konica_400z;
  6068. } else if (!strcmp(model,"KD-510Z")) {
  6069. goto konica_510z;
  6070. } else if (!strcasecmp(make,"MINOLTA")) {
  6071. load_raw = &CLASS unpacked_load_raw;
  6072. maximum = 0xfff;
  6073. if (!strncmp(model,"DiMAGE A",8)) {
  6074. if (!strcmp(model,"DiMAGE A200"))
  6075. filters = 0x49494949;
  6076. load_raw = &CLASS packed_12_load_raw;
  6077. } else if (!strncmp(model,"ALPHA",5) ||
  6078. !strncmp(model,"DYNAX",5) ||
  6079. !strncmp(model,"MAXXUM",6)) {
  6080. sprintf (model+20, "DYNAX %-10s", model+6+(model[0]=='M'));
  6081. adobe_coeff (make, model+20);
  6082. load_raw = &CLASS packed_12_load_raw;
  6083. } else if (!strncmp(model,"DiMAGE G",8)) {
  6084. if (model[8] == '4') {
  6085. height = 1716;
  6086. width = 2304;
  6087. } else if (model[8] == '5') {
  6088. konica_510z:
  6089. height = 1956;
  6090. width = 2607;
  6091. raw_width = 2624;
  6092. } else if (model[8] == '6') {
  6093. height = 2136;
  6094. width = 2848;
  6095. }
  6096. data_offset += 14;
  6097. filters = 0x61616161;
  6098. konica_400z:
  6099. load_raw = &CLASS unpacked_load_raw;
  6100. maximum = 0x3df;
  6101. order = 0x4d4d;
  6102. }
  6103. } else if (!strcmp(model,"*ist DS")) {
  6104. height -= 2;
  6105. } else if (!strcmp(model,"K20D")) {
  6106. filters = 0x16161616;
  6107. } else if (!strcmp(model,"Optio S")) {
  6108. if (fsize == 3178560) {
  6109. height = 1540;
  6110. width = 2064;
  6111. load_raw = &CLASS eight_bit_load_raw;
  6112. cam_mul[0] *= 4;
  6113. cam_mul[2] *= 4;
  6114. pre_mul[0] = 1.391;
  6115. pre_mul[2] = 1.188;
  6116. } else {
  6117. height = 1544;
  6118. width = 2068;
  6119. raw_width = 3136;
  6120. load_raw = &CLASS packed_12_load_raw;
  6121. maximum = 0xf7c;
  6122. pre_mul[0] = 1.137;
  6123. pre_mul[2] = 1.453;
  6124. }
  6125. } else if (fsize == 6114240) {
  6126. height = 1737;
  6127. width = 2324;
  6128. raw_width = 3520;
  6129. load_raw = &CLASS packed_12_load_raw;
  6130. maximum = 0xf7a;
  6131. pre_mul[0] = 1.980;
  6132. pre_mul[2] = 1.570;
  6133. } else if (!strcmp(model,"Optio 750Z")) {
  6134. height = 2302;
  6135. width = 3072;
  6136. load_raw = &CLASS packed_12_load_raw;
  6137. load_flags = 7;
  6138. } else if (!strcmp(model,"S85")) {
  6139. height = 2448;
  6140. width = 3264;
  6141. raw_width = fsize/height/2;
  6142. order = 0x4d4d;
  6143. load_raw = &CLASS unpacked_load_raw;
  6144. maximum = 0xffff;
  6145. } else if (!strcmp(model,"STV680 VGA")) {
  6146. height = 484;
  6147. width = 644;
  6148. load_raw = &CLASS eight_bit_load_raw;
  6149. flip = 2;
  6150. filters = 0x16161616;
  6151. black = 16;
  6152. pre_mul[0] = 1.097;
  6153. pre_mul[2] = 1.128;
  6154. } else if (!strcmp(model,"KAI-0340")) {
  6155. height = 477;
  6156. width = 640;
  6157. order = 0x4949;
  6158. data_offset = 3840;
  6159. load_raw = &CLASS unpacked_load_raw;
  6160. pre_mul[0] = 1.561;
  6161. pre_mul[2] = 2.454;
  6162. } else if (!strcmp(model,"N95")) {
  6163. height = raw_height - (top_margin = 2);
  6164. } else if (!strcmp(model,"531C")) {
  6165. height = 1200;
  6166. width = 1600;
  6167. load_raw = &CLASS unpacked_load_raw;
  6168. filters = 0x49494949;
  6169. pre_mul[1] = 1.218;
  6170. } else if (!strcmp(model,"F-080C")) {
  6171. height = 768;
  6172. width = 1024;
  6173. load_raw = &CLASS eight_bit_load_raw;
  6174. } else if (!strcmp(model,"F-145C")) {
  6175. height = 1040;
  6176. width = 1392;
  6177. load_raw = &CLASS eight_bit_load_raw;
  6178. } else if (!strcmp(model,"F-201C")) {
  6179. height = 1200;
  6180. width = 1600;
  6181. load_raw = &CLASS eight_bit_load_raw;
  6182. } else if (!strcmp(model,"F-510C")) {
  6183. height = 1958;
  6184. width = 2588;
  6185. load_raw = fsize < 7500000 ?
  6186. &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
  6187. maximum = 0xfff0;
  6188. } else if (!strcmp(model,"F-810C")) {
  6189. height = 2469;
  6190. width = 3272;
  6191. load_raw = &CLASS unpacked_load_raw;
  6192. maximum = 0xfff0;
  6193. } else if (!strcmp(model,"XCD-SX910CR")) {
  6194. height = 1024;
  6195. width = 1375;
  6196. raw_width = 1376;
  6197. filters = 0x49494949;
  6198. maximum = 0x3ff;
  6199. load_raw = fsize < 2000000 ?
  6200. &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
  6201. } else if (!strcmp(model,"2010")) {
  6202. height = 1207;
  6203. width = 1608;
  6204. order = 0x4949;
  6205. filters = 0x16161616;
  6206. data_offset = 3212;
  6207. maximum = 0x3ff;
  6208. load_raw = &CLASS unpacked_load_raw;
  6209. } else if (!strcmp(model,"A782")) {
  6210. height = 3000;
  6211. width = 2208;
  6212. filters = 0x61616161;
  6213. load_raw = fsize < 10000000 ?
  6214. &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
  6215. maximum = 0xffc0;
  6216. } else if (!strcmp(model,"3320AF")) {
  6217. height = 1536;
  6218. raw_width = width = 2048;
  6219. filters = 0x61616161;
  6220. load_raw = &CLASS unpacked_load_raw;
  6221. maximum = 0x3ff;
  6222. pre_mul[0] = 1.717;
  6223. pre_mul[2] = 1.138;
  6224. fseek (ifp, 0x300000, SEEK_SET);
  6225. if ((order = guess_byte_order(0x10000)) == 0x4d4d) {
  6226. height -= (top_margin = 16);
  6227. width -= (left_margin = 28);
  6228. maximum = 0xf5c0;
  6229. strcpy (make, "ISG");
  6230. model[0] = 0;
  6231. }
  6232. } else if (!strcmp(make,"Hasselblad")) {
  6233. if (load_raw == &CLASS lossless_jpeg_load_raw)
  6234. load_raw = &CLASS hasselblad_load_raw;
  6235. if (raw_width == 7262) {
  6236. height = 5444;
  6237. width = 7248;
  6238. top_margin = 4;
  6239. left_margin = 7;
  6240. filters = 0x61616161;
  6241. } else if (raw_width == 4090) {
  6242. strcpy (model, "V96C");
  6243. height -= (top_margin = 6);
  6244. width -= (left_margin = 3) + 7;
  6245. filters = 0x61616161;
  6246. }
  6247. } else if (!strcmp(make,"Sinar")) {
  6248. if (!memcmp(head,"8BPS",4)) {
  6249. fseek (ifp, 14, SEEK_SET);
  6250. height = get4();
  6251. width = get4();
  6252. filters = 0x61616161;
  6253. data_offset = 68;
  6254. }
  6255. if (!load_raw) load_raw = &CLASS unpacked_load_raw;
  6256. maximum = 0x3fff;
  6257. } else if (!strcmp(make,"Leaf")) {
  6258. maximum = 0x3fff;
  6259. fseek (ifp, data_offset, SEEK_SET);
  6260. if (ljpeg_start (&jh, 1) && jh.bits == 15)
  6261. maximum = 0x1fff;
  6262. if (tiff_samples > 1) filters = 0;
  6263. if (tiff_samples > 1 || tile_length < raw_height)
  6264. load_raw = &CLASS leaf_hdr_load_raw;
  6265. if ((width | height) == 2048) {
  6266. if (tiff_samples == 1) {
  6267. filters = 1;
  6268. strcpy (cdesc, "RBTG");
  6269. strcpy (model, "CatchLight");
  6270. top_margin = 8; left_margin = 18; height = 2032; width = 2016;
  6271. } else {
  6272. strcpy (model, "DCB2");
  6273. top_margin = 10; left_margin = 16; height = 2028; width = 2022;
  6274. }
  6275. } else if (width+height == 3144+2060) {
  6276. if (!model[0]) strcpy (model, "Cantare");
  6277. if (width > height) {
  6278. top_margin = 6; left_margin = 32; height = 2048; width = 3072;
  6279. filters = 0x61616161;
  6280. } else {
  6281. left_margin = 6; top_margin = 32; width = 2048; height = 3072;
  6282. filters = 0x16161616;
  6283. }
  6284. if (!cam_mul[0] || model[0] == 'V') filters = 0;
  6285. else is_raw = tiff_samples;
  6286. } else if (width == 2116) {
  6287. strcpy (model, "Valeo 6");
  6288. height -= 2 * (top_margin = 30);
  6289. width -= 2 * (left_margin = 55);
  6290. filters = 0x49494949;
  6291. } else if (width == 3171) {
  6292. strcpy (model, "Valeo 6");
  6293. height -= 2 * (top_margin = 24);
  6294. width -= 2 * (left_margin = 24);
  6295. filters = 0x16161616;
  6296. }
  6297. } else if (!strcmp(make,"LEICA") || !strcmp(make,"Panasonic")) {
  6298. maximum = 0xfff0;
  6299. if ((fsize-data_offset) / (width*8/7) == height)
  6300. load_raw = &CLASS panasonic_load_raw;
  6301. if (!load_raw) load_raw = &CLASS unpacked_load_raw;
  6302. switch (width) {
  6303. case 2568:
  6304. adobe_coeff ("Panasonic","DMC-LC1"); break;
  6305. case 3130:
  6306. left_margin = -14;
  6307. case 3170:
  6308. left_margin += 18;
  6309. width = 3096;
  6310. if (height > 2326) {
  6311. height = 2326;
  6312. top_margin = 13;
  6313. filters = 0x49494949;
  6314. }
  6315. zero_is_bad = 1;
  6316. adobe_coeff ("Panasonic","DMC-FZ8"); break;
  6317. case 3213:
  6318. width -= 27;
  6319. case 3177:
  6320. width -= 10;
  6321. filters = 0x49494949;
  6322. zero_is_bad = 1;
  6323. adobe_coeff ("Panasonic","DMC-L1"); break;
  6324. case 3304:
  6325. width -= 17;
  6326. zero_is_bad = 1;
  6327. adobe_coeff ("Panasonic","DMC-FZ30"); break;
  6328. case 3330:
  6329. width += 43;
  6330. left_margin = -6;
  6331. maximum = 0xf7f0;
  6332. case 3370:
  6333. width -= 82;
  6334. left_margin += 15;
  6335. if (height > 2480)
  6336. height = 2480 - (top_margin = 10);
  6337. filters = 0x49494949;
  6338. zero_is_bad = 1;
  6339. adobe_coeff ("Panasonic","DMC-FZ18"); break;
  6340. case 3690:
  6341. height -= 2;
  6342. left_margin = -14;
  6343. maximum = 0xf7f0;
  6344. case 3770:
  6345. width = 3672;
  6346. if (--height == 2798 && (height = 2760))
  6347. top_margin = 15;
  6348. else filters = 0x49494949;
  6349. left_margin += 17;
  6350. zero_is_bad = 1;
  6351. adobe_coeff ("Panasonic","DMC-FZ50"); break;
  6352. case 3710:
  6353. width = 3682;
  6354. filters = 0x49494949;
  6355. adobe_coeff ("Panasonic","DMC-L10"); break;
  6356. case 3724:
  6357. width -= 14;
  6358. case 3836:
  6359. width -= 42;
  6360. lx3: filters = 0x16161616;
  6361. if (make[0] != 'P')
  6362. adobe_coeff ("Panasonic","DMC-LX3");
  6363. break;
  6364. case 3880:
  6365. width -= 22;
  6366. left_margin = 6;
  6367. zero_is_bad = 1;
  6368. adobe_coeff ("Panasonic","DMC-LX1"); break;
  6369. case 4060:
  6370. width = 3982;
  6371. if (height == 2250) goto lx3;
  6372. width = 4018;
  6373. filters = 0x49494949;
  6374. zero_is_bad = 1;
  6375. adobe_coeff ("Panasonic","DMC-G1"); break;
  6376. case 4290:
  6377. height += 38;
  6378. left_margin = -14;
  6379. filters = 0x49494949;
  6380. case 4330:
  6381. width = 4248;
  6382. if ((height -= 39) == 2400)
  6383. top_margin = 15;
  6384. left_margin += 17;
  6385. adobe_coeff ("Panasonic","DMC-LX2"); break;
  6386. case 4508:
  6387. height -= 6;
  6388. width = 4429;
  6389. filters = 0x16161616;
  6390. adobe_coeff ("Panasonic","DMC-FX150"); break;
  6391. }
  6392. } else if (!strcmp(model,"C770UZ")) {
  6393. height = 1718;
  6394. width = 2304;
  6395. filters = 0x16161616;
  6396. load_raw = &CLASS packed_12_load_raw;
  6397. load_flags = 7;
  6398. } else if (!strcmp(make,"OLYMPUS")) {
  6399. height += height & 1;
  6400. filters = exif_cfa;
  6401. if (load_raw == &CLASS olympus_e410_load_raw) {
  6402. black >>= 4;
  6403. } else if (!strcmp(model,"E-10") ||
  6404. !strncmp(model,"E-20",4)) {
  6405. black <<= 2;
  6406. } else if (!strcmp(model,"E-300") ||
  6407. !strcmp(model,"E-500")) {
  6408. width -= 20;
  6409. if (load_raw == &CLASS unpacked_load_raw) {
  6410. maximum = 0xfc30;
  6411. black = 0;
  6412. }
  6413. } else if (!strcmp(model,"E-330")) {
  6414. width -= 30;
  6415. if (load_raw == &CLASS unpacked_load_raw)
  6416. maximum = 0xf790;
  6417. } else if (!strcmp(model,"SP550UZ")) {
  6418. thumb_length = fsize - (thumb_offset = 0xa39800);
  6419. thumb_height = 480;
  6420. thumb_width = 640;
  6421. }
  6422. } else if (!strcmp(model,"N Digital")) {
  6423. height = 2047;
  6424. width = 3072;
  6425. filters = 0x61616161;
  6426. data_offset = 0x1a00;
  6427. load_raw = &CLASS packed_12_load_raw;
  6428. } else if (!strcmp(model,"DSC-F828")) {
  6429. width = 3288;
  6430. left_margin = 5;
  6431. data_offset = 862144;
  6432. load_raw = &CLASS sony_load_raw;
  6433. filters = 0x9c9c9c9c;
  6434. colors = 4;
  6435. strcpy (cdesc, "RGBE");
  6436. } else if (!strcmp(model,"DSC-V3")) {
  6437. width = 3109;
  6438. left_margin = 59;
  6439. data_offset = 787392;
  6440. load_raw = &CLASS sony_load_raw;
  6441. } else if (!strcmp(make,"SONY") && raw_width == 3984) {
  6442. adobe_coeff ("SONY","DSC-R1");
  6443. width = 3925;
  6444. order = 0x4d4d;
  6445. } else if (!strcmp(model,"DSLR-A100")) {
  6446. height--;
  6447. } else if (!strcmp(model,"DSLR-A350")) {
  6448. height -= 4;
  6449. } else if (!strcmp(model,"C603v")) {
  6450. height = 480;
  6451. width = 640;
  6452. goto c603v;
  6453. } else if (!strcmp(model,"C603y")) {
  6454. height = 2134;
  6455. width = 2848;
  6456. c603v:
  6457. filters = 0;
  6458. load_raw = &CLASS kodak_yrgb_load_raw;
  6459. } else if (!strcmp(model,"C603")) {
  6460. raw_height = height = 2152;
  6461. raw_width = width = 2864;
  6462. goto c603;
  6463. } else if (!strcmp(model,"C330")) {
  6464. height = 1744;
  6465. width = 2336;
  6466. raw_height = 1779;
  6467. raw_width = 2338;
  6468. top_margin = 33;
  6469. left_margin = 1;
  6470. c603:
  6471. order = 0x4949;
  6472. if ((data_offset = fsize - raw_height*raw_width)) {
  6473. fseek (ifp, 168, SEEK_SET);
  6474. read_shorts (curve, 256);
  6475. } else use_gamma = 0;
  6476. load_raw = &CLASS eight_bit_load_raw;
  6477. } else if (!strcasecmp(make,"KODAK")) {
  6478. if (filters == UINT_MAX) filters = 0x61616161;
  6479. if (!strncmp(model,"NC2000",6)) {
  6480. width -= 4;
  6481. left_margin = 2;
  6482. } else if (!strcmp(model,"EOSDCS3B")) {
  6483. width -= 4;
  6484. left_margin = 2;
  6485. } else if (!strcmp(model,"EOSDCS1")) {
  6486. width -= 4;
  6487. left_margin = 2;
  6488. } else if (!strcmp(model,"DCS420")) {
  6489. width -= 4;
  6490. left_margin = 2;
  6491. } else if (!strcmp(model,"DCS460")) {
  6492. width -= 4;
  6493. left_margin = 2;
  6494. } else if (!strcmp(model,"DCS460A")) {
  6495. width -= 4;
  6496. left_margin = 2;
  6497. colors = 1;
  6498. filters = 0;
  6499. } else if (!strcmp(model,"DCS660M")) {
  6500. black = 214;
  6501. colors = 1;
  6502. filters = 0;
  6503. } else if (!strcmp(model,"DCS760M")) {
  6504. colors = 1;
  6505. filters = 0;
  6506. }
  6507. if (!strcmp(model+4,"20X"))
  6508. strcpy (cdesc, "MYCY");
  6509. if (strstr(model,"DC25")) {
  6510. strcpy (model, "DC25");
  6511. data_offset = 15424;
  6512. }
  6513. if (!strncmp(model,"DC2",3)) {
  6514. height = 242;
  6515. if (fsize < 100000) {
  6516. raw_width = 256; width = 249;
  6517. pixel_aspect = (4.0*height) / (3.0*width);
  6518. } else {
  6519. raw_width = 512; width = 501;
  6520. pixel_aspect = (493.0*height) / (373.0*width);
  6521. }
  6522. data_offset += raw_width + 1;
  6523. colors = 4;
  6524. filters = 0x8d8d8d8d;
  6525. simple_coeff(1);
  6526. pre_mul[1] = 1.179;
  6527. pre_mul[2] = 1.209;
  6528. pre_mul[3] = 1.036;
  6529. load_raw = &CLASS eight_bit_load_raw;
  6530. } else if (!strcmp(model,"40")) {
  6531. strcpy (model, "DC40");
  6532. height = 512;
  6533. width = 768;
  6534. data_offset = 1152;
  6535. load_raw = &CLASS kodak_radc_load_raw;
  6536. } else if (strstr(model,"DC50")) {
  6537. strcpy (model, "DC50");
  6538. height = 512;
  6539. width = 768;
  6540. data_offset = 19712;
  6541. load_raw = &CLASS kodak_radc_load_raw;
  6542. } else if (strstr(model,"DC120")) {
  6543. strcpy (model, "DC120");
  6544. height = 976;
  6545. width = 848;
  6546. pixel_aspect = height/0.75/width;
  6547. load_raw = tiff_compress == 7 ?
  6548. &CLASS kodak_jpeg_load_raw : &CLASS kodak_dc120_load_raw;
  6549. } else if (!strcmp(model,"DCS200")) {
  6550. thumb_height = 128;
  6551. thumb_width = 192;
  6552. thumb_offset = 6144;
  6553. thumb_misc = 360;
  6554. write_thumb = &CLASS layer_thumb;
  6555. height = 1024;
  6556. width = 1536;
  6557. data_offset = 79872;
  6558. load_raw = &CLASS eight_bit_load_raw;
  6559. black = 17;
  6560. }
  6561. } else if (!strcmp(model,"Fotoman Pixtura")) {
  6562. height = 512;
  6563. width = 768;
  6564. data_offset = 3632;
  6565. load_raw = &CLASS kodak_radc_load_raw;
  6566. filters = 0x61616161;
  6567. simple_coeff(2);
  6568. } else if (!strcmp(model,"QuickTake 100")) {
  6569. fseek (ifp, 544, SEEK_SET);
  6570. height = get2();
  6571. width = get2();
  6572. data_offset = (get4(),get2()) == 30 ? 738:736;
  6573. if (height > width) {
  6574. SWAP(height,width);
  6575. fseek (ifp, data_offset-6, SEEK_SET);
  6576. flip = ~get2() & 3 ? 5:6;
  6577. }
  6578. load_raw = &CLASS quicktake_100_load_raw;
  6579. filters = 0x61616161;
  6580. } else if (!strcmp(model,"QuickTake 150")) {
  6581. data_offset = 738 - head[5];
  6582. if (head[5]) strcpy (model+10, "200");
  6583. load_raw = &CLASS kodak_radc_load_raw;
  6584. height = 480;
  6585. width = 640;
  6586. filters = 0x61616161;
  6587. } else if (!strcmp(make,"Rollei") && !load_raw) {
  6588. switch (raw_width) {
  6589. case 1316:
  6590. height = 1030;
  6591. width = 1300;
  6592. top_margin = 1;
  6593. left_margin = 6;
  6594. break;
  6595. case 2568:
  6596. height = 1960;
  6597. width = 2560;
  6598. top_margin = 2;
  6599. left_margin = 8;
  6600. }
  6601. filters = 0x16161616;
  6602. load_raw = &CLASS rollei_load_raw;
  6603. pre_mul[0] = 1.8;
  6604. pre_mul[2] = 1.3;
  6605. } else if (!strcmp(model,"PC-CAM 600")) {
  6606. height = 768;
  6607. data_offset = width = 1024;
  6608. filters = 0x49494949;
  6609. load_raw = &CLASS eight_bit_load_raw;
  6610. pre_mul[0] = 1.14;
  6611. pre_mul[2] = 2.73;
  6612. } else if (!strcmp(model,"QV-2000UX")) {
  6613. height = 1208;
  6614. width = 1632;
  6615. data_offset = width * 2;
  6616. load_raw = &CLASS eight_bit_load_raw;
  6617. } else if (fsize == 3217760) {
  6618. height = 1546;
  6619. width = 2070;
  6620. raw_width = 2080;
  6621. load_raw = &CLASS eight_bit_load_raw;
  6622. } else if (!strcmp(model,"QV-4000")) {
  6623. height = 1700;
  6624. width = 2260;
  6625. load_raw = &CLASS unpacked_load_raw;
  6626. maximum = 0xffff;
  6627. } else if (!strcmp(model,"QV-5700")) {
  6628. height = 1924;
  6629. width = 2576;
  6630. load_raw = &CLASS casio_qv5700_load_raw;
  6631. } else if (!strcmp(model,"QV-R41")) {
  6632. height = 1720;
  6633. width = 2312;
  6634. raw_width = 3520;
  6635. left_margin = 2;
  6636. } else if (!strcmp(model,"QV-R51")) {
  6637. height = 1926;
  6638. width = 2580;
  6639. raw_width = 3904;
  6640. pre_mul[0] = 1.340;
  6641. pre_mul[2] = 1.672;
  6642. } else if (!strcmp(model,"EX-S100")) {
  6643. height = 1544;
  6644. width = 2058;
  6645. raw_width = 3136;
  6646. pre_mul[0] = 1.631;
  6647. pre_mul[2] = 1.106;
  6648. } else if (!strcmp(model,"EX-Z50")) {
  6649. height = 1931;
  6650. width = 2570;
  6651. raw_width = 3904;
  6652. pre_mul[0] = 2.529;
  6653. pre_mul[2] = 1.185;
  6654. } else if (!strcmp(model,"EX-Z55")) {
  6655. height = 1960;
  6656. width = 2570;
  6657. raw_width = 3904;
  6658. pre_mul[0] = 1.520;
  6659. pre_mul[2] = 1.316;
  6660. } else if (!strcmp(model,"EX-P505")) {
  6661. height = 1928;
  6662. width = 2568;
  6663. raw_width = 3852;
  6664. maximum = 0xfff;
  6665. pre_mul[0] = 2.07;
  6666. pre_mul[2] = 1.88;
  6667. } else if (fsize == 9313536) { /* EX-P600 or QV-R61 */
  6668. height = 2142;
  6669. width = 2844;
  6670. raw_width = 4288;
  6671. pre_mul[0] = 1.797;
  6672. pre_mul[2] = 1.219;
  6673. } else if (!strcmp(model,"EX-P700")) {
  6674. height = 2318;
  6675. width = 3082;
  6676. raw_width = 4672;
  6677. pre_mul[0] = 1.758;
  6678. pre_mul[2] = 1.504;
  6679. }
  6680. if (!model[0])
  6681. sprintf (model, "%dx%d", width, height);
  6682. if (filters == UINT_MAX) filters = 0x94949494;
  6683. if (raw_color) adobe_coeff (make, model);
  6684. if (thumb_offset && !thumb_height) {
  6685. fseek (ifp, thumb_offset, SEEK_SET);
  6686. if (ljpeg_start (&jh, 1)) {
  6687. thumb_width = jh.wide;
  6688. thumb_height = jh.high;
  6689. }
  6690. }
  6691. dng_skip:
  6692. if (!load_raw || height < 22) is_raw = 0;
  6693. #ifdef NO_JPEG
  6694. if (load_raw == &CLASS kodak_jpeg_load_raw) {
  6695. #ifdef DCRAW_VERBOSE
  6696. fprintf (stderr,_("%s: You must link dcraw with libjpeg!!\n"), ifname);
  6697. #endif
  6698. is_raw = 0;
  6699. #ifdef LIBRAW_LIBRARY_BUILD
  6700. imgdata.process_warnings |= LIBRAW_WARN_NO_JPEGLIB;
  6701. #endif
  6702. }
  6703. #endif
  6704. if (!cdesc[0])
  6705. strcpy (cdesc, colors == 3 ? "RGB":"GMCY");
  6706. if (!raw_height) raw_height = height;
  6707. if (!raw_width ) raw_width = width;
  6708. if (filters && colors == 3)
  6709. for (i=0; i < 32; i+=4) {
  6710. if ((filters >> i & 15) == 9)
  6711. filters |= 2 << i;
  6712. if ((filters >> i & 15) == 6)
  6713. filters |= 8 << i;
  6714. }
  6715. notraw:
  6716. if (flip == -1) flip = tiff_flip;
  6717. if (flip == -1) flip = 0;
  6718. #ifdef LIBRAW_LIBRARY_BUILD
  6719. RUN_CALLBACK(LIBRAW_PROGRESS_IDENTIFY,1,2);
  6720. #endif
  6721. }
  6722. #line 8372 "dcraw/dcraw.c"
  6723. void CLASS convert_to_rgb()
  6724. {
  6725. int row, col, c, i, j, k;
  6726. ushort *img;
  6727. float out[3], out_cam[3][4];
  6728. double num, inverse[3][3], bnd[2]={0,0};
  6729. static const double xyzd50_srgb[3][3] =
  6730. { { 0.436083, 0.385083, 0.143055 },
  6731. { 0.222507, 0.716888, 0.060608 },
  6732. { 0.013930, 0.097097, 0.714022 } };
  6733. static const double rgb_rgb[3][3] =
  6734. { { 1,0,0 }, { 0,1,0 }, { 0,0,1 } };
  6735. static const double adobe_rgb[3][3] =
  6736. { { 0.715146, 0.284856, 0.000000 },
  6737. { 0.000000, 1.000000, 0.000000 },
  6738. { 0.000000, 0.041166, 0.958839 } };
  6739. static const double wide_rgb[3][3] =
  6740. { { 0.593087, 0.404710, 0.002206 },
  6741. { 0.095413, 0.843149, 0.061439 },
  6742. { 0.011621, 0.069091, 0.919288 } };
  6743. static const double prophoto_rgb[3][3] =
  6744. { { 0.529317, 0.330092, 0.140588 },
  6745. { 0.098368, 0.873465, 0.028169 },
  6746. { 0.016879, 0.117663, 0.865457 } };
  6747. static const double (*out_rgb[])[3] =
  6748. { rgb_rgb, adobe_rgb, wide_rgb, prophoto_rgb, xyz_rgb };
  6749. static const char *name[] =
  6750. { "sRGB", "Adobe RGB (1998)", "WideGamut D65", "ProPhoto D65", "XYZ" };
  6751. static const unsigned phead[] =
  6752. { 1024, 0, 0x2100000, 0x6d6e7472, 0x52474220, 0x58595a20, 0, 0, 0,
  6753. 0x61637370, 0, 0, 0x6e6f6e65, 0, 0, 0, 0, 0xf6d6, 0x10000, 0xd32d };
  6754. unsigned pbody[] =
  6755. { 10, 0x63707274, 0, 36, /* cprt */
  6756. 0x64657363, 0, 40, /* desc */
  6757. 0x77747074, 0, 20, /* wtpt */
  6758. 0x626b7074, 0, 20, /* bkpt */
  6759. 0x72545243, 0, 14, /* rTRC */
  6760. 0x67545243, 0, 14, /* gTRC */
  6761. 0x62545243, 0, 14, /* bTRC */
  6762. 0x7258595a, 0, 20, /* rXYZ */
  6763. 0x6758595a, 0, 20, /* gXYZ */
  6764. 0x6258595a, 0, 20 }; /* bXYZ */
  6765. static const unsigned pwhite[] = { 0xf351, 0x10000, 0x116cc };
  6766. unsigned pcurve[] = { 0x63757276, 0, 1, 0x1000000 };
  6767. #ifdef LIBRAW_LIBRARY_BUILD
  6768. RUN_CALLBACK(LIBRAW_PROGRESS_CONVERT_RGB,0,2);
  6769. #endif
  6770. bnd[gamm[1] >= 1] = 1;
  6771. if (gamm[1] && (gamm[1]-1)*(gamm[0]-1) <= 0) {
  6772. for (i=0; i < 36; i++) {
  6773. gamm[2] = (bnd[0] + bnd[1])/2;
  6774. bnd[(pow(gamm[2]/gamm[1],-gamm[0])-1)/gamm[0]-1/gamm[2] > -1] = gamm[2];
  6775. }
  6776. gamm[3] = gamm[2]*(1/gamm[0]-1);
  6777. gamm[2] /= gamm[1];
  6778. }
  6779. gamm[4] = 1 / (gamm[1]/2*SQR(gamm[2]) - gamm[3]*(1-gamm[2]) +
  6780. (1-pow(gamm[2],1+gamm[0]))*(1+gamm[3])/(1+gamm[0])) - 1;
  6781. memcpy (out_cam, rgb_cam, sizeof out_cam);
  6782. raw_color |= colors == 1 || document_mode ||
  6783. output_color < 1 || output_color > 5;
  6784. if (!raw_color) {
  6785. oprof = (unsigned *) calloc (phead[0], 1);
  6786. merror (oprof, "convert_to_rgb()");
  6787. memcpy (oprof, phead, sizeof phead);
  6788. if (output_color == 5) oprof[4] = oprof[5];
  6789. oprof[0] = 132 + 12*pbody[0];
  6790. for (i=0; i < pbody[0]; i++) {
  6791. oprof[oprof[0]/4] = i ? (i > 1 ? 0x58595a20 : 0x64657363) : 0x74657874;
  6792. pbody[i*3+2] = oprof[0];
  6793. oprof[0] += (pbody[i*3+3] + 3) & -4;
  6794. }
  6795. memcpy (oprof+32, pbody, sizeof pbody);
  6796. oprof[pbody[5]/4+2] = strlen(name[output_color-1]) + 1;
  6797. memcpy ((char *)oprof+pbody[8]+8, pwhite, sizeof pwhite);
  6798. if (output_bps == 8 | gamma_16bit)
  6799. pcurve[3] = (short)(256/gamm[4]+0.5) << 16;
  6800. for (i=4; i < 7; i++)
  6801. memcpy ((char *)oprof+pbody[i*3+2], pcurve, sizeof pcurve);
  6802. pseudoinverse ((double (*)[3]) out_rgb[output_color-1], inverse, 3);
  6803. for (i=0; i < 3; i++)
  6804. for (j=0; j < 3; j++) {
  6805. for (num = k=0; k < 3; k++)
  6806. num += xyzd50_srgb[i][k] * inverse[j][k];
  6807. oprof[pbody[j*3+23]/4+i+2] = num * 0x10000 + 0.5;
  6808. }
  6809. for (i=0; i < phead[0]/4; i++)
  6810. oprof[i] = htonl(oprof[i]);
  6811. strcpy ((char *)oprof+pbody[2]+8, "auto-generated by dcraw");
  6812. strcpy ((char *)oprof+pbody[5]+12, name[output_color-1]);
  6813. for (i=0; i < 3; i++)
  6814. for (j=0; j < colors; j++)
  6815. for (out_cam[i][j] = k=0; k < 3; k++)
  6816. out_cam[i][j] += out_rgb[output_color-1][i][k] * rgb_cam[k][j];
  6817. }
  6818. #ifdef DCRAW_VERBOSE
  6819. if (verbose)
  6820. fprintf (stderr, raw_color ? _("Building histograms...\n") :
  6821. _("Converting to %s colorspace...\n"), name[output_color-1]);
  6822. #endif
  6823. #ifdef LIBRAW_LIBRARY_BUILD
  6824. memset(histogram,0,sizeof(int)*LIBRAW_HISTOGRAM_SIZE*4);
  6825. #else
  6826. memset (histogram, 0, sizeof histogram);
  6827. #endif
  6828. for (img=image[0], row=0; row < height; row++)
  6829. for (col=0; col < width; col++, img+=4) {
  6830. if (!raw_color) {
  6831. out[0] = out[1] = out[2] = 0;
  6832. FORCC {
  6833. out[0] += out_cam[0][c] * img[c];
  6834. out[1] += out_cam[1][c] * img[c];
  6835. out[2] += out_cam[2][c] * img[c];
  6836. }
  6837. FORC3 img[c] = CLIP((int) out[c]);
  6838. }
  6839. else if (document_mode)
  6840. img[0] = img[FC(row,col)];
  6841. FORCC histogram[c][img[c] >> 3]++;
  6842. }
  6843. if (colors == 4 && output_color) colors = 3;
  6844. if (document_mode && filters) colors = 1;
  6845. #ifdef LIBRAW_LIBRARY_BUILD
  6846. RUN_CALLBACK(LIBRAW_PROGRESS_CONVERT_RGB,1,2);
  6847. #endif
  6848. }
  6849. void CLASS fuji_rotate()
  6850. {
  6851. int i, row, col;
  6852. double step;
  6853. float r, c, fr, fc;
  6854. unsigned ur, uc;
  6855. ushort wide, high, (*img)[4], (*pix)[4];
  6856. if (!fuji_width) return;
  6857. #ifdef DCRAW_VERBOSE
  6858. if (verbose)
  6859. fprintf (stderr,_("Rotating image 45 degrees...\n"));
  6860. #endif
  6861. fuji_width = (fuji_width - 1 + shrink) >> shrink;
  6862. step = sqrt(0.5);
  6863. wide = fuji_width / step;
  6864. high = (height - fuji_width) / step;
  6865. img = (ushort (*)[4]) calloc (wide*high, sizeof *img);
  6866. merror (img, "fuji_rotate()");
  6867. #ifdef LIBRAW_LIBRARY_BUILD
  6868. RUN_CALLBACK(LIBRAW_PROGRESS_FUJI_ROTATE,0,2);
  6869. #endif
  6870. for (row=0; row < high; row++)
  6871. for (col=0; col < wide; col++) {
  6872. ur = r = fuji_width + (row-col)*step;
  6873. uc = c = (row+col)*step;
  6874. if (ur > height-2 || uc > width-2) continue;
  6875. fr = r - ur;
  6876. fc = c - uc;
  6877. pix = image + ur*width + uc;
  6878. for (i=0; i < colors; i++)
  6879. img[row*wide+col][i] =
  6880. (pix[ 0][i]*(1-fc) + pix[ 1][i]*fc) * (1-fr) +
  6881. (pix[width][i]*(1-fc) + pix[width+1][i]*fc) * fr;
  6882. }
  6883. free (image);
  6884. width = wide;
  6885. height = high;
  6886. image = img;
  6887. fuji_width = 0;
  6888. #ifdef LIBRAW_LIBRARY_BUILD
  6889. RUN_CALLBACK(LIBRAW_PROGRESS_FUJI_ROTATE,1,2);
  6890. #endif
  6891. }
  6892. void CLASS stretch()
  6893. {
  6894. ushort newdim, (*img)[4], *pix0, *pix1;
  6895. int row, col, c;
  6896. double rc, frac;
  6897. if (pixel_aspect == 1) return;
  6898. #ifdef LIBRAW_LIBRARY_BUILD
  6899. RUN_CALLBACK(LIBRAW_PROGRESS_STRETCH,0,2);
  6900. #endif
  6901. #ifdef DCRAW_VERBOSE
  6902. if (verbose) fprintf (stderr,_("Stretching the image...\n"));
  6903. #endif
  6904. if (pixel_aspect < 1) {
  6905. newdim = height / pixel_aspect + 0.5;
  6906. img = (ushort (*)[4]) calloc (width*newdim, sizeof *img);
  6907. merror (img, "stretch()");
  6908. for (rc=row=0; row < newdim; row++, rc+=pixel_aspect) {
  6909. frac = rc - (c = rc);
  6910. pix0 = pix1 = image[c*width];
  6911. if (c+1 < height) pix1 += width*4;
  6912. for (col=0; col < width; col++, pix0+=4, pix1+=4)
  6913. FORCC img[row*width+col][c] = pix0[c]*(1-frac) + pix1[c]*frac + 0.5;
  6914. }
  6915. height = newdim;
  6916. } else {
  6917. newdim = width * pixel_aspect + 0.5;
  6918. img = (ushort (*)[4]) calloc (height*newdim, sizeof *img);
  6919. merror (img, "stretch()");
  6920. for (rc=col=0; col < newdim; col++, rc+=1/pixel_aspect) {
  6921. frac = rc - (c = rc);
  6922. pix0 = pix1 = image[c];
  6923. if (c+1 < width) pix1 += 4;
  6924. for (row=0; row < height; row++, pix0+=width*4, pix1+=width*4)
  6925. FORCC img[row*newdim+col][c] = pix0[c]*(1-frac) + pix1[c]*frac + 0.5;
  6926. }
  6927. width = newdim;
  6928. }
  6929. free (image);
  6930. image = img;
  6931. #ifdef LIBRAW_LIBRARY_BUILD
  6932. RUN_CALLBACK(LIBRAW_PROGRESS_STRETCH,1,2);
  6933. #endif
  6934. }
  6935. int CLASS flip_index (int row, int col)
  6936. {
  6937. if (flip & 4) SWAP(row,col);
  6938. if (flip & 2) row = iheight - 1 - row;
  6939. if (flip & 1) col = iwidth - 1 - col;
  6940. return row * iwidth + col;
  6941. }
  6942. void CLASS gamma_lut (ushort lut[0x10000])
  6943. {
  6944. int perc, c, val, total, i;
  6945. float t_white=0, r;
  6946. #ifdef LIBRAW_LIBRARY_BUILD
  6947. perc = width * height * imgdata.params.auto_bright_thr;
  6948. #else
  6949. perc = width * height * 0.01; /* 99th percentile white level */
  6950. #endif
  6951. if (fuji_width) perc /= 2;
  6952. if ((highlight & ~2) || no_auto_bright) perc = -1;
  6953. FORCC {
  6954. for (val=0x2000, total=0; --val > 32; )
  6955. if ((total += histogram[c][val]) > perc) break;
  6956. if (t_white < val) t_white = val;
  6957. }
  6958. t_white *= 8 / bright;
  6959. for (i=0; i < 0x10000; i++) {
  6960. r = i / t_white;
  6961. val = 65535 * ( !use_gamma ? r :
  6962. r <= gamm[2] ? r*gamm[1] : pow((double)r,gamm[0])*(1+gamm[3])-gamm[3]);
  6963. if (val > 65535) val = 65535;
  6964. lut[i] = val;
  6965. }
  6966. }
  6967. #line 8656 "dcraw/dcraw.c"
  6968. void CLASS tiff_set (ushort *ntag,
  6969. ushort tag, ushort type, int count, int val)
  6970. {
  6971. struct tiff_tag *tt;
  6972. int c;
  6973. tt = (struct tiff_tag *)(ntag+1) + (*ntag)++;
  6974. tt->tag = tag;
  6975. tt->type = type;
  6976. tt->count = count;
  6977. if (type < 3 && count <= 4)
  6978. FORC(4) tt->val.c[c] = val >> (c << 3);
  6979. else if (type == 3 && count <= 2)
  6980. FORC(2) tt->val.s[c] = val >> (c << 4);
  6981. else tt->val.i = val;
  6982. }
  6983. #define TOFF(ptr) ((char *)(&(ptr)) - (char *)th)
  6984. void CLASS tiff_head (struct tiff_hdr *th, int full)
  6985. {
  6986. int c, psize=0;
  6987. struct tm *t;
  6988. memset (th, 0, sizeof *th);
  6989. th->t_order = htonl(0x4d4d4949) >> 16;
  6990. th->magic = 42;
  6991. th->ifd = 10;
  6992. if (full) {
  6993. tiff_set (&th->ntag, 254, 4, 1, 0);
  6994. tiff_set (&th->ntag, 256, 4, 1, width);
  6995. tiff_set (&th->ntag, 257, 4, 1, height);
  6996. tiff_set (&th->ntag, 258, 3, colors, output_bps);
  6997. if (colors > 2)
  6998. th->tag[th->ntag-1].val.i = TOFF(th->bps);
  6999. FORC4 th->bps[c] = output_bps;
  7000. tiff_set (&th->ntag, 259, 3, 1, 1);
  7001. tiff_set (&th->ntag, 262, 3, 1, 1 + (colors > 1));
  7002. }
  7003. tiff_set (&th->ntag, 270, 2, 512, TOFF(th->t_desc));
  7004. tiff_set (&th->ntag, 271, 2, 64, TOFF(th->t_make));
  7005. tiff_set (&th->ntag, 272, 2, 64, TOFF(th->t_model));
  7006. if (full) {
  7007. if (oprof) psize = ntohl(oprof[0]);
  7008. tiff_set (&th->ntag, 273, 4, 1, sizeof *th + psize);
  7009. tiff_set (&th->ntag, 277, 3, 1, colors);
  7010. tiff_set (&th->ntag, 278, 4, 1, height);
  7011. tiff_set (&th->ntag, 279, 4, 1, height*width*colors*output_bps/8);
  7012. } else
  7013. tiff_set (&th->ntag, 274, 3, 1, "12435867"[flip]-'0');
  7014. tiff_set (&th->ntag, 282, 5, 1, TOFF(th->rat[0]));
  7015. tiff_set (&th->ntag, 283, 5, 1, TOFF(th->rat[2]));
  7016. tiff_set (&th->ntag, 284, 3, 1, 1);
  7017. tiff_set (&th->ntag, 296, 3, 1, 2);
  7018. tiff_set (&th->ntag, 305, 2, 32, TOFF(th->soft));
  7019. tiff_set (&th->ntag, 306, 2, 20, TOFF(th->date));
  7020. tiff_set (&th->ntag, 315, 2, 64, TOFF(th->t_artist));
  7021. tiff_set (&th->ntag, 34665, 4, 1, TOFF(th->nexif));
  7022. if (psize) tiff_set (&th->ntag, 34675, 7, psize, sizeof *th);
  7023. tiff_set (&th->nexif, 33434, 5, 1, TOFF(th->rat[4]));
  7024. tiff_set (&th->nexif, 33437, 5, 1, TOFF(th->rat[6]));
  7025. tiff_set (&th->nexif, 34855, 3, 1, iso_speed);
  7026. tiff_set (&th->nexif, 37386, 5, 1, TOFF(th->rat[8]));
  7027. if (gpsdata[1]) {
  7028. tiff_set (&th->ntag, 34853, 4, 1, TOFF(th->ngps));
  7029. tiff_set (&th->ngps, 0, 1, 4, 0x202);
  7030. tiff_set (&th->ngps, 1, 2, 2, gpsdata[29]);
  7031. tiff_set (&th->ngps, 2, 5, 3, TOFF(th->gps[0]));
  7032. tiff_set (&th->ngps, 3, 2, 2, gpsdata[30]);
  7033. tiff_set (&th->ngps, 4, 5, 3, TOFF(th->gps[6]));
  7034. tiff_set (&th->ngps, 5, 1, 1, gpsdata[31]);
  7035. tiff_set (&th->ngps, 6, 5, 1, TOFF(th->gps[18]));
  7036. tiff_set (&th->ngps, 7, 5, 3, TOFF(th->gps[12]));
  7037. tiff_set (&th->ngps, 18, 2, 12, TOFF(th->gps[20]));
  7038. tiff_set (&th->ngps, 29, 2, 12, TOFF(th->gps[23]));
  7039. memcpy (th->gps, gpsdata, sizeof th->gps);
  7040. }
  7041. th->rat[0] = th->rat[2] = 300;
  7042. th->rat[1] = th->rat[3] = 1;
  7043. FORC(6) th->rat[4+c] = 1000000;
  7044. th->rat[4] *= shutter;
  7045. th->rat[6] *= aperture;
  7046. th->rat[8] *= focal_len;
  7047. strncpy (th->t_desc, desc, 512);
  7048. strncpy (th->t_make, make, 64);
  7049. strncpy (th->t_model, model, 64);
  7050. strcpy (th->soft, "dcraw v"VERSION);
  7051. t = gmtime (&timestamp);
  7052. sprintf (th->date, "%04d:%02d:%02d %02d:%02d:%02d",
  7053. t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
  7054. strncpy (th->t_artist, artist, 64);
  7055. }
  7056. void CLASS jpeg_thumb_writer (FILE *tfp,char *t_humb,int t_humb_length)
  7057. {
  7058. ushort exif[5];
  7059. struct tiff_hdr th;
  7060. fputc (0xff, tfp);
  7061. fputc (0xd8, tfp);
  7062. if (strcmp (t_humb+6, "Exif")) {
  7063. memcpy (exif, "\xff\xe1 Exif\0\0", 10);
  7064. exif[1] = htons (8 + sizeof th);
  7065. fwrite (exif, 1, sizeof exif, tfp);
  7066. tiff_head (&th, 0);
  7067. fwrite (&th, 1, sizeof th, tfp);
  7068. }
  7069. fwrite (t_humb+2, 1, t_humb_length-2, tfp);
  7070. }
  7071. void CLASS jpeg_thumb (FILE *tfp)
  7072. {
  7073. char *thumb;
  7074. ushort exif[5];
  7075. struct tiff_hdr th;
  7076. thumb = (char *) malloc (thumb_length);
  7077. merror (thumb, "jpeg_thumb()");
  7078. fread (thumb, 1, thumb_length, ifp);
  7079. #if 0
  7080. fputc (0xff, tfp);
  7081. fputc (0xd8, tfp);
  7082. if (strcmp (thumb+6, "Exif")) {
  7083. memcpy (exif, "\xff\xe1 Exif\0\0", 10);
  7084. exif[1] = htons (8 + sizeof th);
  7085. fwrite (exif, 1, sizeof exif, tfp);
  7086. tiff_head (&th, 0);
  7087. fwrite (&th, 1, sizeof th, tfp);
  7088. }
  7089. fwrite (thumb+2, 1, thumb_length-2, tfp);
  7090. #else
  7091. jpeg_thumb_writer(tfp,thumb,thumb_length);
  7092. #endif
  7093. free (thumb);
  7094. }
  7095. void CLASS write_ppm_tiff (FILE *ofp)
  7096. {
  7097. struct tiff_hdr th;
  7098. uchar *ppm;
  7099. ushort *ppm2,lut16[0x10000];
  7100. int c, row, col, soff, rstep, cstep;
  7101. iheight = height;
  7102. iwidth = width;
  7103. if (flip & 4) SWAP(height,width);
  7104. ppm = (uchar *) calloc (width, colors*output_bps/8);
  7105. ppm2 = (ushort *) ppm;
  7106. merror (ppm, "write_ppm_tiff()");
  7107. if (output_tiff) {
  7108. tiff_head (&th, 1);
  7109. fwrite (&th, sizeof th, 1, ofp);
  7110. if (oprof)
  7111. fwrite (oprof, ntohl(oprof[0]), 1, ofp);
  7112. } else if (colors > 3)
  7113. fprintf (ofp,
  7114. "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
  7115. width, height, colors, (1 << output_bps)-1, cdesc);
  7116. else
  7117. fprintf (ofp, "P%d\n%d %d\n%d\n",
  7118. colors/2+5, width, height, (1 << output_bps)-1);
  7119. if (output_bps == 8 || gamma_16bit ) gamma_lut (lut16);
  7120. soff = flip_index (0, 0);
  7121. cstep = flip_index (0, 1) - soff;
  7122. rstep = flip_index (1, 0) - flip_index (0, width);
  7123. for (row=0; row < height; row++, soff += rstep) {
  7124. for (col=0; col < width; col++, soff += cstep)
  7125. if (output_bps == 8)
  7126. FORCC ppm [col*colors+c] = lut16[image[soff][c]]/256;
  7127. else if(gamma_16bit) FORCC ppm2[col*colors+c] = lut16[image[soff][c]];
  7128. else FORCC ppm2[col*colors+c] = image[soff][c];
  7129. if (output_bps == 16 && !output_tiff && htons(0x55aa) != 0x55aa)
  7130. swab ((char*)ppm2, (char*)ppm2, width*colors*2);
  7131. fwrite (ppm, colors*output_bps/8, width, ofp);
  7132. }
  7133. free (ppm);
  7134. }