PageRenderTime 127ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 1ms

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

https://bitbucket.org/cabalistic/ogredeps/
C++ | 9257 lines | 8724 code | 384 blank | 149 comment | 2770 complexity | 72935f0889776ffd03608fe38e768ba2 MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause, CPL-1.0, Unlicense, GPL-2.0, GPL-3.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, BSD-2-Clause, LGPL-2.1
  1. /*
  2. Copyright 2008-2010 LibRaw LLC (info@libraw.org)
  3. LibRaw is free software; you can redistribute it and/or modify
  4. it under the terms of the one of three licenses as you choose:
  5. 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
  6. (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
  7. 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  8. (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
  9. 3. LibRaw Software License 27032010
  10. (See file LICENSE.LibRaw.pdf provided in LibRaw distribution archive for details).
  11. This file is generated from Dave Coffin's dcraw.c
  12. dcraw.c -- Dave Coffin's raw photo decoder
  13. Copyright 1997-2010 by Dave Coffin, dcoffin a cybercom o net
  14. Look into dcraw homepage (probably http://cybercom.net/~dcoffin/dcraw/)
  15. for more information
  16. */
  17. #include <math.h>
  18. #define CLASS LibRaw::
  19. #include "libraw/libraw_types.h"
  20. #define LIBRAW_LIBRARY_BUILD
  21. #define LIBRAW_IO_REDEFINED
  22. #include "libraw/libraw.h"
  23. #include "internal/defines.h"
  24. #include "internal/var_defines.h"
  25. #include "internal/libraw_bytebuffer.h"
  26. #ifdef ANDROID
  27. #include <asm/byteorder.h>
  28. void swab(const void *from, void*to, ssize_t n)
  29. {
  30. if (n < 0)
  31. return;
  32. for (ssize_t i = 0; i < (n/2)*2; i += 2)
  33. {
  34. *((uint16_t*)to+i) = __arch__swab16(*((uint16_t*)from+i));
  35. }
  36. }
  37. #endif
  38. #ifndef __GLIBC__
  39. char *my_memmem (char *haystack, size_t haystacklen,
  40. char *needle, size_t needlelen)
  41. {
  42. char *c;
  43. for (c = haystack; c <= haystack + haystacklen - needlelen; c++)
  44. if (!memcmp (c, needle, needlelen))
  45. return c;
  46. return 0;
  47. }
  48. #define memmem my_memmem
  49. #endif
  50. ushort CLASS sget2 (uchar *s)
  51. {
  52. if (order == 0x4949) /* "II" means little-endian */
  53. return s[0] | s[1] << 8;
  54. else /* "MM" means big-endian */
  55. return s[0] << 8 | s[1];
  56. }
  57. ushort CLASS get2()
  58. {
  59. uchar str[2] = { 0xff,0xff };
  60. fread (str, 1, 2, ifp);
  61. return sget2(str);
  62. }
  63. unsigned CLASS sget4 (uchar *s)
  64. {
  65. if (order == 0x4949)
  66. return s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
  67. else
  68. return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3];
  69. }
  70. #define sget4(s) sget4((uchar *)s)
  71. unsigned CLASS get4()
  72. {
  73. uchar str[4] = { 0xff,0xff,0xff,0xff };
  74. fread (str, 1, 4, ifp);
  75. return sget4(str);
  76. }
  77. unsigned CLASS getint (int type)
  78. {
  79. return type == 3 ? get2() : get4();
  80. }
  81. float CLASS int_to_float (int i)
  82. {
  83. union { int i; float f; } u;
  84. u.i = i;
  85. return u.f;
  86. }
  87. double CLASS getreal (int type)
  88. {
  89. union { char c[8]; double d; } u;
  90. int i, rev;
  91. switch (type) {
  92. case 3: return (unsigned short) get2();
  93. case 4: return (unsigned int) get4();
  94. case 5: u.d = (unsigned int) get4();
  95. return u.d / (unsigned int) get4();
  96. case 8: return (signed short) get2();
  97. case 9: return (signed int) get4();
  98. case 10: u.d = (signed int) get4();
  99. return u.d / (signed int) get4();
  100. case 11: return int_to_float (get4());
  101. case 12:
  102. rev = 7 * ((order == 0x4949) == (ntohs(0x1234) == 0x1234));
  103. for (i=0; i < 8; i++)
  104. u.c[i ^ rev] = fgetc(ifp);
  105. return u.d;
  106. default: return fgetc(ifp);
  107. }
  108. }
  109. void CLASS read_shorts (ushort *pixel, int count)
  110. {
  111. if (fread (pixel, 2, count, ifp) < count) derror();
  112. if ((order == 0x4949) == (ntohs(0x1234) == 0x1234))
  113. swab ((char*)pixel, (char*)pixel, count*2);
  114. }
  115. #ifdef LIBRAW_LIBRARY_BUILD
  116. #define RBAYER(x,y) raw_image[(x)*raw_width+(y)]
  117. #define RRBAYER(x,y) raw_image[((x)+top_margin)*raw_width+(y)+left_margin]
  118. #define CBAYER(x,y) color_image[((x)+top_margin)*raw_width+(y)+left_margin][FC((x),(y))]
  119. #else
  120. #define CBAYER(x,y) BAYER((x),(y))
  121. #define RBAYER(x,y) BAYER((x),(y))
  122. #define RRBAYER(x,y) BAYER((x),(y))
  123. #endif
  124. void CLASS canon_600_fixed_wb (int temp)
  125. {
  126. static const short mul[4][5] = {
  127. { 667, 358,397,565,452 },
  128. { 731, 390,367,499,517 },
  129. { 1119, 396,348,448,537 },
  130. { 1399, 485,431,508,688 } };
  131. int lo, hi, i;
  132. float frac=0;
  133. for (lo=4; --lo; )
  134. if (*mul[lo] <= temp) break;
  135. for (hi=0; hi < 3; hi++)
  136. if (*mul[hi] >= temp) break;
  137. if (lo != hi)
  138. frac = (float) (temp - *mul[lo]) / (*mul[hi] - *mul[lo]);
  139. for (i=1; i < 5; i++)
  140. pre_mul[i-1] = 1 / (frac * mul[hi][i] + (1-frac) * mul[lo][i]);
  141. #ifdef LIBRAW_LIBRARY_BUILD
  142. color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
  143. #endif
  144. }
  145. /* Return values: 0 = white 1 = near white 2 = not white */
  146. int CLASS canon_600_color (int ratio[2], int mar)
  147. {
  148. int clipped=0, target, miss;
  149. if (flash_used) {
  150. if (ratio[1] < -104)
  151. { ratio[1] = -104; clipped = 1; }
  152. if (ratio[1] > 12)
  153. { ratio[1] = 12; clipped = 1; }
  154. } else {
  155. if (ratio[1] < -264 || ratio[1] > 461) return 2;
  156. if (ratio[1] < -50)
  157. { ratio[1] = -50; clipped = 1; }
  158. if (ratio[1] > 307)
  159. { ratio[1] = 307; clipped = 1; }
  160. }
  161. target = flash_used || ratio[1] < 197
  162. ? -38 - (398 * ratio[1] >> 10)
  163. : -123 + (48 * ratio[1] >> 10);
  164. if (target - mar <= ratio[0] &&
  165. target + 20 >= ratio[0] && !clipped) return 0;
  166. miss = target - ratio[0];
  167. if (abs(miss) >= mar*4) return 2;
  168. if (miss < -20) miss = -20;
  169. if (miss > mar) miss = mar;
  170. ratio[0] = target - miss;
  171. return 1;
  172. }
  173. void CLASS canon_600_auto_wb()
  174. {
  175. int mar, row, col, i, j, st, count[] = { 0,0 };
  176. int test[8], total[2][8], ratio[2][2], stat[2];
  177. memset (&total, 0, sizeof total);
  178. i = canon_ev + 0.5;
  179. if (i < 10) mar = 150;
  180. else if (i > 12) mar = 20;
  181. else mar = 280 - 20 * i;
  182. if (flash_used) mar = 80;
  183. for (row=14; row < height-14; row+=4)
  184. for (col=10; col < width; col+=2) {
  185. for (i=0; i < 8; i++)
  186. test[(i & 4) + FC(row+(i >> 1),col+(i & 1))] =
  187. BAYER(row+(i >> 1),col+(i & 1));
  188. for (i=0; i < 8; i++)
  189. if (test[i] < 150 || test[i] > 1500) goto next;
  190. for (i=0; i < 4; i++)
  191. if (abs(test[i] - test[i+4]) > 50) goto next;
  192. for (i=0; i < 2; i++) {
  193. for (j=0; j < 4; j+=2)
  194. ratio[i][j >> 1] = ((test[i*4+j+1]-test[i*4+j]) << 10) / test[i*4+j];
  195. stat[i] = canon_600_color (ratio[i], mar);
  196. }
  197. if ((st = stat[0] | stat[1]) > 1) goto next;
  198. for (i=0; i < 2; i++)
  199. if (stat[i])
  200. for (j=0; j < 2; j++)
  201. test[i*4+j*2+1] = test[i*4+j*2] * (0x400 + ratio[i][j]) >> 10;
  202. for (i=0; i < 8; i++)
  203. total[st][i] += test[i];
  204. count[st]++;
  205. next: ;
  206. }
  207. if (count[0] | count[1]) {
  208. st = count[0]*200 < count[1];
  209. for (i=0; i < 4; i++)
  210. pre_mul[i] = 1.0 / (total[st][i] + total[st][i+4]);
  211. #ifdef LIBRAW_LIBRARY_BUILD
  212. color_flags.pre_mul_state = LIBRAW_COLORSTATE_CALCULATED;
  213. #endif
  214. }
  215. }
  216. void CLASS canon_600_coeff()
  217. {
  218. static const short table[6][12] = {
  219. { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
  220. { -1203,1715,-1136,1648, 1388,-876,267,245, -1641,2153,3921,-3409 },
  221. { -615,1127,-1563,2075, 1437,-925,509,3, -756,1268,2519,-2007 },
  222. { -190,702,-1886,2398, 2153,-1641,763,-251, -452,964,3040,-2528 },
  223. { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
  224. { -807,1319,-1785,2297, 1388,-876,769,-257, -230,742,2067,-1555 } };
  225. int t=0, i, c;
  226. float mc, yc;
  227. mc = pre_mul[1] / pre_mul[2];
  228. yc = pre_mul[3] / pre_mul[2];
  229. if (mc > 1 && mc <= 1.28 && yc < 0.8789) t=1;
  230. if (mc > 1.28 && mc <= 2) {
  231. if (yc < 0.8789) t=3;
  232. else if (yc <= 2) t=4;
  233. }
  234. if (flash_used) t=5;
  235. for (raw_color = i=0; i < 3; i++)
  236. FORCC rgb_cam[i][c] = table[t][i*4 + c] / 1024.0;
  237. #ifdef LIBRAW_LIBRARY_BUILD
  238. color_flags.rgb_cam_state = LIBRAW_COLORSTATE_CALCULATED;
  239. #endif
  240. }
  241. void CLASS canon_600_load_raw()
  242. {
  243. uchar data[1120], *dp;
  244. ushort pixel[896], *pix;
  245. int irow, row, col, val;
  246. static const short mul[4][2] =
  247. { { 1141,1145 }, { 1128,1109 }, { 1178,1149 }, { 1128,1109 } };
  248. for (irow=row=0; irow < height; irow++) {
  249. if (fread (data, 1, raw_width*5/4, ifp) < raw_width*5/4) derror();
  250. for (dp=data, pix=pixel; dp < data+1120; dp+=10, pix+=8) {
  251. pix[0] = (dp[0] << 2) + (dp[1] >> 6 );
  252. pix[1] = (dp[2] << 2) + (dp[1] >> 4 & 3);
  253. pix[2] = (dp[3] << 2) + (dp[1] >> 2 & 3);
  254. pix[3] = (dp[4] << 2) + (dp[1] & 3);
  255. pix[4] = (dp[5] << 2) + (dp[9] & 3);
  256. pix[5] = (dp[6] << 2) + (dp[9] >> 2 & 3);
  257. pix[6] = (dp[7] << 2) + (dp[9] >> 4 & 3);
  258. pix[7] = (dp[8] << 2) + (dp[9] >> 6 );
  259. }
  260. #ifndef LIBRAW_LIBRARY_BUILD
  261. for (col=0; col < width; col++)
  262. {
  263. BAYER(row,col) = pixel[col];
  264. }
  265. #else
  266. for (col=0; col < raw_width; col++)
  267. RBAYER(row,col) = pixel[col];
  268. #endif
  269. for (col=width; col < raw_width; col++)
  270. {
  271. black += pixel[col];
  272. }
  273. if ((row+=2) > height) row = 1;
  274. }
  275. if (raw_width > width)
  276. black = black / ((raw_width - width) * height) - 4;
  277. #ifndef LIBRAW_LIBRARY_BUILD
  278. for (row=0; row < height; row++)
  279. for (col=0; col < width; col++) {
  280. if ((val = BAYER(row,col) - black) < 0) val = 0;
  281. val = val * mul[row & 3][col & 1] >> 9;
  282. BAYER(row,col) = val;
  283. }
  284. #else
  285. for (row=0; row < height; row++)
  286. for (col=0; col < raw_width; col++) {
  287. if ((val = RBAYER(row,col) - black) < 0) val = 0;
  288. val = val * mul[row & 3][col & 1] >> 9;
  289. RBAYER(row,col) = val;
  290. }
  291. #endif
  292. canon_600_fixed_wb(1311);
  293. canon_600_auto_wb();
  294. canon_600_coeff();
  295. maximum = (0x3ff - black) * 1109 >> 9;
  296. black = 0;
  297. }
  298. void CLASS remove_zeroes()
  299. {
  300. unsigned row, col, tot, n, r, c;
  301. #ifdef LIBRAW_LIBRARY_BUILD
  302. RUN_CALLBACK(LIBRAW_PROGRESS_REMOVE_ZEROES,0,2);
  303. #endif
  304. for (row=0; row < height; row++)
  305. for (col=0; col < width; col++)
  306. if (BAYER(row,col) == 0) {
  307. tot = n = 0;
  308. for (r = row-2; r <= row+2; r++)
  309. for (c = col-2; c <= col+2; c++)
  310. if (r < height && c < width &&
  311. FC(r,c) == FC(row,col) && BAYER(r,c))
  312. tot += (n++,BAYER(r,c));
  313. if (n) BAYER(row,col) = tot/n;
  314. }
  315. #ifdef LIBRAW_LIBRARY_BUILD
  316. RUN_CALLBACK(LIBRAW_PROGRESS_REMOVE_ZEROES,1,2);
  317. #endif
  318. }
  319. int CLASS canon_s2is()
  320. {
  321. unsigned row;
  322. for (row=0; row < 100; row++) {
  323. fseek (ifp, row*3340 + 3284, SEEK_SET);
  324. if (getc(ifp) > 15) return 1;
  325. }
  326. return 0;
  327. }
  328. /*
  329. getbits(-1) initializes the buffer
  330. getbits(n) where 0 <= n <= 25 returns an n-bit integer
  331. */
  332. unsigned CLASS getbithuff (int nbits, ushort *huff)
  333. {
  334. #ifdef LIBRAW_NOTHREADS
  335. static unsigned bitbuf=0;
  336. static int vbits=0, reset=0;
  337. #else
  338. #define bitbuf tls->getbits.bitbuf
  339. #define vbits tls->getbits.vbits
  340. #define reset tls->getbits.reset
  341. #endif
  342. unsigned c;
  343. if (nbits == -1)
  344. return bitbuf = vbits = reset = 0;
  345. if (nbits == 0 || vbits < 0) return 0;
  346. while (!reset && vbits < nbits && (c = fgetc(ifp)) != EOF &&
  347. !(reset = zero_after_ff && c == 0xff && fgetc(ifp))) {
  348. bitbuf = (bitbuf << 8) + (uchar) c;
  349. vbits += 8;
  350. }
  351. c = bitbuf << (32-vbits) >> (32-nbits);
  352. if (huff) {
  353. vbits -= huff[c] >> 8;
  354. c = (uchar) huff[c];
  355. } else
  356. vbits -= nbits;
  357. if (vbits < 0) derror();
  358. return c;
  359. #ifndef LIBRAW_NOTHREADS
  360. #undef bitbuf
  361. #undef vbits
  362. #undef reset
  363. #endif
  364. }
  365. #define getbits(n) getbithuff(n,0)
  366. #define gethuff(h) getbithuff(*h,h+1)
  367. /*
  368. Construct a decode tree according to the specification in *source.
  369. The first 16 bytes specify how many codes should be 1-bit, 2-bit
  370. 3-bit, etc. Bytes after that are the leaf values.
  371. For example, if the source is
  372. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
  373. 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
  374. then the code is
  375. 00 0x04
  376. 010 0x03
  377. 011 0x05
  378. 100 0x06
  379. 101 0x02
  380. 1100 0x07
  381. 1101 0x01
  382. 11100 0x08
  383. 11101 0x09
  384. 11110 0x00
  385. 111110 0x0a
  386. 1111110 0x0b
  387. 1111111 0xff
  388. */
  389. ushort * CLASS make_decoder_ref (const uchar **source)
  390. {
  391. int max, len, h, i, j;
  392. const uchar *count;
  393. ushort *huff;
  394. count = (*source += 16) - 17;
  395. for (max=16; max && !count[max]; max--);
  396. huff = (ushort *) calloc (1 + (1 << max), sizeof *huff);
  397. merror (huff, "make_decoder()");
  398. huff[0] = max;
  399. for (h=len=1; len <= max; len++)
  400. for (i=0; i < count[len]; i++, ++*source)
  401. for (j=0; j < 1 << (max-len); j++)
  402. if (h <= 1 << max)
  403. huff[h++] = len << 8 | **source;
  404. return huff;
  405. }
  406. ushort * CLASS make_decoder (const uchar *source)
  407. {
  408. return make_decoder_ref (&source);
  409. }
  410. void CLASS crw_init_tables (unsigned table, ushort *huff[2])
  411. {
  412. static const uchar first_tree[3][29] = {
  413. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
  414. 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
  415. { 0,2,2,3,1,1,1,1,2,0,0,0,0,0,0,0,
  416. 0x03,0x02,0x04,0x01,0x05,0x00,0x06,0x07,0x09,0x08,0x0a,0x0b,0xff },
  417. { 0,0,6,3,1,1,2,0,0,0,0,0,0,0,0,0,
  418. 0x06,0x05,0x07,0x04,0x08,0x03,0x09,0x02,0x00,0x0a,0x01,0x0b,0xff },
  419. };
  420. static const uchar second_tree[3][180] = {
  421. { 0,2,2,2,1,4,2,1,2,5,1,1,0,0,0,139,
  422. 0x03,0x04,0x02,0x05,0x01,0x06,0x07,0x08,
  423. 0x12,0x13,0x11,0x14,0x09,0x15,0x22,0x00,0x21,0x16,0x0a,0xf0,
  424. 0x23,0x17,0x24,0x31,0x32,0x18,0x19,0x33,0x25,0x41,0x34,0x42,
  425. 0x35,0x51,0x36,0x37,0x38,0x29,0x79,0x26,0x1a,0x39,0x56,0x57,
  426. 0x28,0x27,0x52,0x55,0x58,0x43,0x76,0x59,0x77,0x54,0x61,0xf9,
  427. 0x71,0x78,0x75,0x96,0x97,0x49,0xb7,0x53,0xd7,0x74,0xb6,0x98,
  428. 0x47,0x48,0x95,0x69,0x99,0x91,0xfa,0xb8,0x68,0xb5,0xb9,0xd6,
  429. 0xf7,0xd8,0x67,0x46,0x45,0x94,0x89,0xf8,0x81,0xd5,0xf6,0xb4,
  430. 0x88,0xb1,0x2a,0x44,0x72,0xd9,0x87,0x66,0xd4,0xf5,0x3a,0xa7,
  431. 0x73,0xa9,0xa8,0x86,0x62,0xc7,0x65,0xc8,0xc9,0xa1,0xf4,0xd1,
  432. 0xe9,0x5a,0x92,0x85,0xa6,0xe7,0x93,0xe8,0xc1,0xc6,0x7a,0x64,
  433. 0xe1,0x4a,0x6a,0xe6,0xb3,0xf1,0xd3,0xa5,0x8a,0xb2,0x9a,0xba,
  434. 0x84,0xa4,0x63,0xe5,0xc5,0xf3,0xd2,0xc4,0x82,0xaa,0xda,0xe4,
  435. 0xf2,0xca,0x83,0xa3,0xa2,0xc3,0xea,0xc2,0xe2,0xe3,0xff,0xff },
  436. { 0,2,2,1,4,1,4,1,3,3,1,0,0,0,0,140,
  437. 0x02,0x03,0x01,0x04,0x05,0x12,0x11,0x06,
  438. 0x13,0x07,0x08,0x14,0x22,0x09,0x21,0x00,0x23,0x15,0x31,0x32,
  439. 0x0a,0x16,0xf0,0x24,0x33,0x41,0x42,0x19,0x17,0x25,0x18,0x51,
  440. 0x34,0x43,0x52,0x29,0x35,0x61,0x39,0x71,0x62,0x36,0x53,0x26,
  441. 0x38,0x1a,0x37,0x81,0x27,0x91,0x79,0x55,0x45,0x28,0x72,0x59,
  442. 0xa1,0xb1,0x44,0x69,0x54,0x58,0xd1,0xfa,0x57,0xe1,0xf1,0xb9,
  443. 0x49,0x47,0x63,0x6a,0xf9,0x56,0x46,0xa8,0x2a,0x4a,0x78,0x99,
  444. 0x3a,0x75,0x74,0x86,0x65,0xc1,0x76,0xb6,0x96,0xd6,0x89,0x85,
  445. 0xc9,0xf5,0x95,0xb4,0xc7,0xf7,0x8a,0x97,0xb8,0x73,0xb7,0xd8,
  446. 0xd9,0x87,0xa7,0x7a,0x48,0x82,0x84,0xea,0xf4,0xa6,0xc5,0x5a,
  447. 0x94,0xa4,0xc6,0x92,0xc3,0x68,0xb5,0xc8,0xe4,0xe5,0xe6,0xe9,
  448. 0xa2,0xa3,0xe3,0xc2,0x66,0x67,0x93,0xaa,0xd4,0xd5,0xe7,0xf8,
  449. 0x88,0x9a,0xd7,0x77,0xc4,0x64,0xe2,0x98,0xa5,0xca,0xda,0xe8,
  450. 0xf3,0xf6,0xa9,0xb2,0xb3,0xf2,0xd2,0x83,0xba,0xd3,0xff,0xff },
  451. { 0,0,6,2,1,3,3,2,5,1,2,2,8,10,0,117,
  452. 0x04,0x05,0x03,0x06,0x02,0x07,0x01,0x08,
  453. 0x09,0x12,0x13,0x14,0x11,0x15,0x0a,0x16,0x17,0xf0,0x00,0x22,
  454. 0x21,0x18,0x23,0x19,0x24,0x32,0x31,0x25,0x33,0x38,0x37,0x34,
  455. 0x35,0x36,0x39,0x79,0x57,0x58,0x59,0x28,0x56,0x78,0x27,0x41,
  456. 0x29,0x77,0x26,0x42,0x76,0x99,0x1a,0x55,0x98,0x97,0xf9,0x48,
  457. 0x54,0x96,0x89,0x47,0xb7,0x49,0xfa,0x75,0x68,0xb6,0x67,0x69,
  458. 0xb9,0xb8,0xd8,0x52,0xd7,0x88,0xb5,0x74,0x51,0x46,0xd9,0xf8,
  459. 0x3a,0xd6,0x87,0x45,0x7a,0x95,0xd5,0xf6,0x86,0xb4,0xa9,0x94,
  460. 0x53,0x2a,0xa8,0x43,0xf5,0xf7,0xd4,0x66,0xa7,0x5a,0x44,0x8a,
  461. 0xc9,0xe8,0xc8,0xe7,0x9a,0x6a,0x73,0x4a,0x61,0xc7,0xf4,0xc6,
  462. 0x65,0xe9,0x72,0xe6,0x71,0x91,0x93,0xa6,0xda,0x92,0x85,0x62,
  463. 0xf3,0xc5,0xb2,0xa4,0x84,0xba,0x64,0xa5,0xb3,0xd2,0x81,0xe5,
  464. 0xd3,0xaa,0xc4,0xca,0xf2,0xb1,0xe4,0xd1,0x83,0x63,0xea,0xc3,
  465. 0xe2,0x82,0xf1,0xa3,0xc2,0xa1,0xc1,0xe3,0xa2,0xe1,0xff,0xff }
  466. };
  467. if (table > 2) table = 2;
  468. huff[0] = make_decoder ( first_tree[table]);
  469. huff[1] = make_decoder (second_tree[table]);
  470. }
  471. /*
  472. Return 0 if the image starts with compressed data,
  473. 1 if it starts with uncompressed low-order bits.
  474. In Canon compressed data, 0xff is always followed by 0x00.
  475. */
  476. int CLASS canon_has_lowbits()
  477. {
  478. uchar test[0x4000];
  479. int ret=1;
  480. unsigned i;
  481. fseek (ifp, 0, SEEK_SET);
  482. fread (test, 1, sizeof test, ifp);
  483. for (i=540; i < sizeof test - 1; i++)
  484. if (test[i] == 0xff) {
  485. if (test[i+1]) return 1;
  486. ret=0;
  487. }
  488. return ret;
  489. }
  490. void CLASS canon_compressed_load_raw()
  491. {
  492. ushort *pixel, *prow, *huff[2];
  493. int nblocks, lowbits, i, c, row, r, col, save, val;
  494. unsigned irow, icol;
  495. int block, diffbuf[64], leaf, len, diff, carry=0, pnum=0, base[2];
  496. crw_init_tables (tiff_compress, huff);
  497. pixel = (ushort *) calloc (raw_width*8, sizeof *pixel);
  498. merror (pixel, "canon_compressed_load_raw()");
  499. lowbits = canon_has_lowbits();
  500. if (!lowbits) maximum = 0x3ff;
  501. fseek (ifp, 540 + lowbits*raw_height*raw_width/4, SEEK_SET);
  502. zero_after_ff = 1;
  503. getbits(-1);
  504. for (row=0; row < raw_height; row+=8) {
  505. nblocks = MIN (8, raw_height-row) * raw_width >> 6;
  506. for (block=0; block < nblocks; block++) {
  507. memset (diffbuf, 0, sizeof diffbuf);
  508. for (i=0; i < 64; i++ ) {
  509. leaf = gethuff(huff[i > 0]);
  510. if (leaf == 0 && i) break;
  511. if (leaf == 0xff) continue;
  512. i += leaf >> 4;
  513. len = leaf & 15;
  514. if (len == 0) continue;
  515. diff = getbits(len);
  516. if ((diff & (1 << (len-1))) == 0)
  517. diff -= (1 << len) - 1;
  518. if (i < 64) diffbuf[i] = diff;
  519. }
  520. diffbuf[0] += carry;
  521. carry = diffbuf[0];
  522. for (i=0; i < 64; i++ ) {
  523. if (pnum++ % raw_width == 0)
  524. base[0] = base[1] = 512;
  525. if ((pixel[(block << 6) + i] = base[i & 1] += diffbuf[i]) >> 10)
  526. derror();
  527. }
  528. }
  529. if (lowbits) {
  530. save = ftell(ifp);
  531. fseek (ifp, 26 + row*raw_width/4, SEEK_SET);
  532. for (prow=pixel, i=0; i < raw_width*2; i++) {
  533. c = fgetc(ifp);
  534. for (r=0; r < 8; r+=2, prow++) {
  535. val = (*prow << 2) + ((c >> r) & 3);
  536. if (raw_width == 2672 && val < 512) val += 2;
  537. *prow = val;
  538. }
  539. }
  540. fseek (ifp, save, SEEK_SET);
  541. }
  542. #ifdef LIBRAW_LIBRARY_BUILD
  543. for (r=0; r < 8; r++) {
  544. if(row+r>=raw_height) break; // Not sure that raw_height is always N*8
  545. // MOVE entire row into place
  546. memmove(&raw_image[(row+r)*raw_width],&pixel[r*raw_width],raw_width*sizeof(pixel[0]));
  547. irow = row - top_margin + r;
  548. if (irow >= height) continue; // if row above image area than irow is VERY positive :)
  549. // only margins!
  550. for (col=0; col < left_margin; col++)
  551. {
  552. icol = col - left_margin;
  553. c = FC(irow,icol);
  554. if (icol >= width && col > 1 && (unsigned) (col-left_margin+2) > width+3)
  555. cblack[c] += (cblack[4+c]++,pixel[r*raw_width+col]);
  556. }
  557. for (col=width+left_margin; col < raw_width; col++)
  558. {
  559. icol = col - left_margin;
  560. c = FC(irow,icol);
  561. if (icol >= width && col > 1 && (unsigned) (col-left_margin+2) > width+3)
  562. cblack[c] += (cblack[4+c]++,pixel[r*raw_width+col]);
  563. }
  564. }
  565. #else
  566. // dcraw original code
  567. for (r=0; r < 8; r++) {
  568. irow = row - top_margin + r;
  569. if (irow >= height) continue;
  570. for (col=0; col < raw_width; col++) {
  571. icol = col - left_margin;
  572. c = FC(irow,icol);
  573. if (icol < width)
  574. BAYER(irow,icol) = pixel[r*raw_width+col];
  575. else if (col > 1 && (unsigned) (col-left_margin+2) > width+3)
  576. cblack[c] += (cblack[4+c]++,pixel[r*raw_width+col]);
  577. }
  578. }
  579. #endif
  580. }
  581. free (pixel);
  582. FORC(2) free (huff[c]);
  583. FORC4 if (cblack[4+c]) cblack[c] /= cblack[4+c];
  584. }
  585. int CLASS ljpeg_start (struct jhead *jh, int info_only)
  586. {
  587. int c, tag, len;
  588. uchar data[0x10000];
  589. const uchar *dp;
  590. memset (jh, 0, sizeof *jh);
  591. jh->restart = INT_MAX;
  592. fread (data, 2, 1, ifp);
  593. if (data[1] != 0xd8) return 0;
  594. do {
  595. fread (data, 2, 2, ifp);
  596. tag = data[0] << 8 | data[1];
  597. len = (data[2] << 8 | data[3]) - 2;
  598. if (tag <= 0xff00) return 0;
  599. fread (data, 1, len, ifp);
  600. switch (tag) {
  601. case 0xffc3:
  602. jh->sraw = ((data[7] >> 4) * (data[7] & 15) - 1) & 3;
  603. case 0xffc0:
  604. jh->bits = data[0];
  605. jh->high = data[1] << 8 | data[2];
  606. jh->wide = data[3] << 8 | data[4];
  607. jh->clrs = data[5] + jh->sraw;
  608. if (len == 9 && !dng_version) getc(ifp);
  609. break;
  610. case 0xffc4:
  611. if (info_only) break;
  612. for (dp = data; dp < data+len && (c = *dp++) < 4; )
  613. jh->free[c] = jh->huff[c] = make_decoder_ref (&dp);
  614. break;
  615. case 0xffda:
  616. jh->psv = data[1+data[0]*2];
  617. jh->bits -= data[3+data[0]*2] & 15;
  618. break;
  619. case 0xffdd:
  620. jh->restart = data[0] << 8 | data[1];
  621. }
  622. } while (tag != 0xffda);
  623. if (info_only) return 1;
  624. FORC(5) if (!jh->huff[c+1]) jh->huff[c+1] = jh->huff[c];
  625. if (jh->sraw) {
  626. FORC(4) jh->huff[2+c] = jh->huff[1];
  627. FORC(jh->sraw) jh->huff[1+c] = jh->huff[0];
  628. }
  629. jh->row = (ushort *) calloc (jh->wide*jh->clrs, 4);
  630. merror (jh->row, "ljpeg_start()");
  631. return zero_after_ff = 1;
  632. }
  633. void CLASS ljpeg_end (struct jhead *jh)
  634. {
  635. int c;
  636. FORC4 if (jh->free[c]) free (jh->free[c]);
  637. free (jh->row);
  638. }
  639. // used for kodak-262 decoder
  640. int CLASS ljpeg_diff (ushort *huff)
  641. {
  642. int len, diff;
  643. len = gethuff(huff);
  644. if (len == 16 && (!dng_version || dng_version >= 0x1010000))
  645. return -32768;
  646. diff = getbits(len);
  647. if ((diff & (1 << (len-1))) == 0)
  648. diff -= (1 << len) - 1;
  649. return diff;
  650. }
  651. #ifdef LIBRAW_LIBRARY_BUILD
  652. int CLASS ljpeg_diff_new (LibRaw_bit_buffer& bits, LibRaw_byte_buffer* buf,ushort *huff)
  653. {
  654. int len, diff;
  655. len = bits._gethuff_lj(buf,*huff,huff+1);
  656. if (len == 16 && (!dng_version || dng_version >= 0x1010000))
  657. return -32768;
  658. diff = bits._getbits_lj(buf,len);
  659. if ((diff & (1 << (len-1))) == 0)
  660. diff -= (1 << len) - 1;
  661. return diff;
  662. }
  663. int CLASS ljpeg_diff_pef (LibRaw_bit_buffer& bits, LibRaw_byte_buffer* buf,ushort *huff)
  664. {
  665. int len, diff;
  666. len = bits._gethuff(buf,*huff,huff+1,zero_after_ff);
  667. if (len == 16 && (!dng_version || dng_version >= 0x1010000))
  668. return -32768;
  669. diff = bits._getbits(buf,len,zero_after_ff);
  670. if ((diff & (1 << (len-1))) == 0)
  671. diff -= (1 << len) - 1;
  672. return diff;
  673. }
  674. #endif
  675. ushort * CLASS ljpeg_row (int jrow, struct jhead *jh)
  676. {
  677. int col, c, diff, pred, spred=0;
  678. ushort mark=0, *row[3];
  679. if (jrow * jh->wide % jh->restart == 0) {
  680. FORC(6) jh->vpred[c] = 1 << (jh->bits-1);
  681. if (jrow) {
  682. fseek (ifp, -2, SEEK_CUR);
  683. do mark = (mark << 8) + (c = fgetc(ifp));
  684. while (c != EOF && mark >> 4 != 0xffd);
  685. }
  686. getbits(-1);
  687. }
  688. FORC3 row[c] = jh->row + jh->wide*jh->clrs*((jrow+c) & 1);
  689. for (col=0; col < jh->wide; col++)
  690. FORC(jh->clrs) {
  691. diff = ljpeg_diff (jh->huff[c]);
  692. if (jh->sraw && c <= jh->sraw && (col | c))
  693. pred = spred;
  694. else if (col) pred = row[0][-jh->clrs];
  695. else pred = (jh->vpred[c] += diff) - diff;
  696. if (jrow && col) switch (jh->psv) {
  697. case 1: break;
  698. case 2: pred = row[1][0]; break;
  699. case 3: pred = row[1][-jh->clrs]; break;
  700. case 4: pred = pred + row[1][0] - row[1][-jh->clrs]; break;
  701. case 5: pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1); break;
  702. case 6: pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1); break;
  703. case 7: pred = (pred + row[1][0]) >> 1; break;
  704. default: pred = 0;
  705. }
  706. if ((**row = pred + diff) >> jh->bits) derror();
  707. if (c <= jh->sraw) spred = **row;
  708. row[0]++; row[1]++;
  709. }
  710. return row[2];
  711. }
  712. #ifdef LIBRAW_LIBRARY_BUILD
  713. ushort * CLASS ljpeg_row_new (int jrow, struct jhead *jh, LibRaw_bit_buffer& bits,LibRaw_byte_buffer* bytes)
  714. {
  715. int col, c, diff, pred, spred=0;
  716. ushort mark=0, *row[3];
  717. if (jrow * jh->wide % jh->restart == 0) {
  718. FORC(6) jh->vpred[c] = 1 << (jh->bits-1);
  719. if (jrow) {
  720. bytes->unseek2();
  721. do mark = (mark << 8) + (c = bytes->get_byte());
  722. while (c != EOF && mark >> 4 != 0xffd);
  723. }
  724. bits.reset();
  725. }
  726. FORC3 row[c] = jh->row + jh->wide*jh->clrs*((jrow+c) & 1);
  727. for (col=0; col < jh->wide; col++)
  728. FORC(jh->clrs) {
  729. diff = ljpeg_diff_new (bits,bytes,jh->huff[c]);
  730. if (jh->sraw && c <= jh->sraw && (col | c))
  731. pred = spred;
  732. else if (col) pred = row[0][-jh->clrs];
  733. else pred = (jh->vpred[c] += diff) - diff;
  734. if (jrow && col) switch (jh->psv) {
  735. case 1: break;
  736. case 2: pred = row[1][0]; break;
  737. case 3: pred = row[1][-jh->clrs]; break;
  738. case 4: pred = pred + row[1][0] - row[1][-jh->clrs]; break;
  739. case 5: pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1); break;
  740. case 6: pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1); break;
  741. case 7: pred = (pred + row[1][0]) >> 1; break;
  742. default: pred = 0;
  743. }
  744. if ((**row = pred + diff) >> jh->bits) derror();
  745. if (c <= jh->sraw) spred = **row;
  746. row[0]++; row[1]++;
  747. }
  748. return row[2];
  749. }
  750. #endif
  751. void CLASS lossless_jpeg_load_raw()
  752. {
  753. int jwide, jrow, jcol, val, c, i, row=0, col=0;
  754. #ifndef LIBRAW_LIBRARY_BUILD
  755. int jidx,j;
  756. #endif
  757. struct jhead jh;
  758. int min=INT_MAX;
  759. ushort *rp;
  760. #ifdef LIBRAW_LIBRARY_BUILD
  761. int save_min = 0;
  762. unsigned slicesW[16],slicesWcnt=0,slices;
  763. unsigned *offset;
  764. unsigned t_y=0,t_x=0,t_s=0,slice=0,pixelsInSlice,pixno;
  765. if (!strcasecmp(make,"KODAK"))
  766. save_min = 1;
  767. #endif
  768. #ifdef LIBRAW_LIBRARY_BUILD
  769. if (cr2_slice[0]>15)
  770. throw LIBRAW_EXCEPTION_IO_EOF; // change many slices
  771. #else
  772. fprintf(stderr,"Too many CR2 slices: %d\n",cr2_slice[0]+1);
  773. return;
  774. #endif
  775. if (!ljpeg_start (&jh, 0)) return;
  776. jwide = jh.wide * jh.clrs;
  777. #ifdef LIBRAW_LIBRARY_BUILD
  778. if(cr2_slice[0])
  779. {
  780. for(i=0;i<cr2_slice[0];i++)
  781. slicesW[slicesWcnt++] = cr2_slice[1];
  782. slicesW[slicesWcnt++] = cr2_slice[2];
  783. }
  784. else
  785. {
  786. // not sliced
  787. slicesW[slicesWcnt++] = raw_width; // safe fallback
  788. }
  789. slices = slicesWcnt * jh.high;
  790. offset = (unsigned*)calloc(slices+1,sizeof(offset[0]));
  791. for(slice=0;slice<slices;slice++)
  792. {
  793. offset[slice] = (t_x + t_y * raw_width)| (t_s<<28);
  794. if(offset[slice] & 0x0fffffff >= raw_width * raw_height)
  795. throw LIBRAW_EXCEPTION_IO_BADFILE;
  796. t_y++;
  797. if(t_y == jh.high)
  798. {
  799. t_y = 0;
  800. t_x += slicesW[t_s++];
  801. }
  802. }
  803. offset[slices] = offset[slices-1];
  804. slice = 1; // next slice
  805. pixno = offset[0];
  806. pixelsInSlice = slicesW[0];
  807. #endif
  808. #ifdef LIBRAW_LIBRARY_BUILD
  809. LibRaw_byte_buffer *buf=NULL;
  810. if(data_size)
  811. buf = ifp->make_byte_buffer(data_size);
  812. LibRaw_bit_buffer bits;
  813. #endif
  814. for (jrow=0; jrow < jh.high; jrow++) {
  815. #ifdef LIBRAW_LIBRARY_BUILD
  816. if (data_size)
  817. rp = ljpeg_row_new (jrow, &jh,bits,buf);
  818. else
  819. #endif
  820. rp = ljpeg_row (jrow, &jh);
  821. if (load_flags & 1)
  822. row = jrow & 1 ? height-1-jrow/2 : jrow/2;
  823. for (jcol=0; jcol < jwide; jcol++) {
  824. val = *rp++;
  825. if (jh.bits <= 12)
  826. val = curve[val & 0xfff];
  827. #ifndef LIBRAW_LIBRARY_BUILD
  828. // slow dcraw way to calculate row/col
  829. if (cr2_slice[0]) {
  830. jidx = jrow*jwide + jcol;
  831. i = jidx / (cr2_slice[1]*jh.high);
  832. if ((j = i >= cr2_slice[0]))
  833. i = cr2_slice[0];
  834. jidx -= i * (cr2_slice[1]*jh.high);
  835. row = jidx / cr2_slice[1+j];
  836. col = jidx % cr2_slice[1+j] + i*cr2_slice[1];
  837. }
  838. #else
  839. // new fast one, but for data_size defined only (i.e. new CR2 format, not 1D/1Ds)
  840. if(data_size)
  841. {
  842. row = pixno/raw_width;
  843. col = pixno % raw_width;
  844. pixno++;
  845. if (0 == --pixelsInSlice)
  846. {
  847. unsigned o = offset[slice++];
  848. pixno = o & 0x0fffffff;
  849. pixelsInSlice = slicesW[o>>28];
  850. }
  851. }
  852. #endif
  853. #ifndef LIBRAW_LIBRARY_BUILD
  854. if (raw_width == 3984 && (col -= 2) < 0)
  855. col += (row--,raw_width);
  856. if ((unsigned) (row-top_margin) < height) {
  857. c = FC(row-top_margin,col-left_margin);
  858. if ((unsigned) (col-left_margin) < width) {
  859. BAYER(row-top_margin,col-left_margin) = val;
  860. if (min > val) min = val;
  861. } else if (col > 1 && (unsigned) (col-left_margin+2) > width+3)
  862. cblack[c] += (cblack[4+c]++,val);
  863. }
  864. #else
  865. if (raw_width == 3984)
  866. {
  867. if ( (col -= 2) < 0)
  868. col += (row--,raw_width);
  869. if(row >= 0 && row < raw_height && col >= 0 && col < raw_width)
  870. RBAYER(row,col) = val;
  871. }
  872. else
  873. RBAYER(row,col) = val;
  874. if ((unsigned) (row-top_margin) < height)
  875. {
  876. // within image height
  877. if ((unsigned) (col-left_margin) < width)
  878. {
  879. // within image area, save min
  880. if(save_min)
  881. if (min > val) min = val;
  882. }
  883. else if (col > 1 && (unsigned) (col-left_margin+2) > width+3)
  884. {
  885. c = FC(row-top_margin,col-left_margin);
  886. cblack[c] += (cblack[4+c]++,val);
  887. }
  888. }
  889. #endif
  890. #ifndef LIBRAW_LIBRARY_BUILD
  891. if (++col >= raw_width)
  892. col = (row++,0);
  893. #else
  894. if(!data_size) // 1D or 1Ds case
  895. if (++col >= raw_width)
  896. col = (row++,0);
  897. #endif
  898. }
  899. }
  900. ljpeg_end (&jh);
  901. FORC4 if (cblack[4+c]) cblack[c] /= cblack[4+c];
  902. if (!strcasecmp(make,"KODAK"))
  903. black = min;
  904. #ifdef LIBRAW_LIBRARY_BUILD
  905. if(buf)
  906. delete buf;
  907. free(offset);
  908. #endif
  909. }
  910. void CLASS canon_sraw_load_raw()
  911. {
  912. struct jhead jh;
  913. short *rp=0, (*ip)[4];
  914. int jwide, slice, scol, ecol, row, col, jrow=0, jcol=0, pix[3], c;
  915. int v[3]={0,0,0}, ver, hue;
  916. char *cp;
  917. if (!ljpeg_start (&jh, 0)) return;
  918. jwide = (jh.wide >>= 1) * jh.clrs;
  919. #ifdef LIBRAW_LIBRARY_BUILD
  920. if(!data_size)
  921. throw LIBRAW_EXCEPTION_IO_BADFILE;
  922. LibRaw_byte_buffer *buf = ifp->make_byte_buffer(data_size);
  923. LibRaw_bit_buffer bits;
  924. #endif
  925. for (ecol=slice=0; slice <= cr2_slice[0]; slice++) {
  926. scol = ecol;
  927. ecol += cr2_slice[1] * 2 / jh.clrs;
  928. if (!cr2_slice[0] || ecol > raw_width-1) ecol = raw_width & -2;
  929. for (row=0; row < height; row += (jh.clrs >> 1) - 1) {
  930. ip = (short (*)[4]) image + row*width;
  931. for (col=scol; col < ecol; col+=2, jcol+=jh.clrs) {
  932. if ((jcol %= jwide) == 0)
  933. #ifdef LIBRAW_LIBRARY_BUILD
  934. rp = (short*) ljpeg_row_new (jrow++, &jh,bits,buf);
  935. #else
  936. rp = (short *) ljpeg_row (jrow++, &jh);
  937. #endif
  938. if (col >= width) continue;
  939. FORC (jh.clrs-2)
  940. ip[col + (c >> 1)*width + (c & 1)][0] = rp[jcol+c];
  941. ip[col][1] = rp[jcol+jh.clrs-2] - 16384;
  942. ip[col][2] = rp[jcol+jh.clrs-1] - 16384;
  943. }
  944. }
  945. }
  946. for (cp=model2; *cp && !isdigit(*cp); cp++);
  947. sscanf (cp, "%d.%d.%d", v, v+1, v+2);
  948. ver = (v[0]*1000 + v[1])*1000 + v[2];
  949. hue = (jh.sraw+1) << 2;
  950. if (unique_id >= 0x80000281 || (unique_id == 0x80000218 && ver > 1000006))
  951. hue = jh.sraw << 1;
  952. ip = (short (*)[4]) image;
  953. rp = ip[0];
  954. for (row=0; row < height; row++, ip+=width) {
  955. if (row & (jh.sraw >> 1))
  956. for (col=0; col < width; col+=2)
  957. for (c=1; c < 3; c++)
  958. if (row == height-1)
  959. ip[col][c] = ip[col-width][c];
  960. else ip[col][c] = (ip[col-width][c] + ip[col+width][c] + 1) >> 1;
  961. for (col=1; col < width; col+=2)
  962. for (c=1; c < 3; c++)
  963. if (col == width-1)
  964. ip[col][c] = ip[col-1][c];
  965. else ip[col][c] = (ip[col-1][c] + ip[col+1][c] + 1) >> 1;
  966. }
  967. for ( ; rp < ip[0]; rp+=4) {
  968. if (unique_id < 0x80000218) {
  969. pix[0] = rp[0] + rp[2] - 512;
  970. pix[2] = rp[0] + rp[1] - 512;
  971. pix[1] = rp[0] + ((-778*rp[1] - (rp[2] << 11)) >> 12) - 512;
  972. } else {
  973. rp[1] = (rp[1] << 2) + hue;
  974. rp[2] = (rp[2] << 2) + hue;
  975. pix[0] = rp[0] + (( 50*rp[1] + 22929*rp[2]) >> 14);
  976. pix[1] = rp[0] + ((-5640*rp[1] - 11751*rp[2]) >> 14);
  977. pix[2] = rp[0] + ((29040*rp[1] - 101*rp[2]) >> 14);
  978. }
  979. FORC3
  980. rp[c] = CLIP(pix[c] * sraw_mul[c] >> 10);
  981. }
  982. #ifdef LIBRAW_LIBRARY_BUILD
  983. delete buf;
  984. #endif
  985. ljpeg_end (&jh);
  986. maximum = 0x3fff;
  987. }
  988. void CLASS adobe_copy_pixel (int row, int col, ushort **rp)
  989. {
  990. unsigned r, c;
  991. #ifndef LIBRAW_LIBRARY_BUILD
  992. r = row -= top_margin;
  993. c = col -= left_margin;
  994. if (is_raw == 2 && shot_select) (*rp)++;
  995. if (filters) {
  996. if (fuji_width) {
  997. r = row + fuji_width - 1 - (col >> 1);
  998. c = row + ((col+1) >> 1);
  999. }
  1000. if (r < height && c < width)
  1001. BAYER(r,c) = **rp < 0x1000 ? curve[**rp] : **rp;
  1002. *rp += is_raw;
  1003. } else {
  1004. if (r < height && c < width)
  1005. FORC(tiff_samples)
  1006. image[row*width+col][c] = (*rp)[c] < 0x1000 ? curve[(*rp)[c]]:(*rp)[c];
  1007. *rp += tiff_samples;
  1008. }
  1009. #else
  1010. if (is_raw == 2 && shot_select) (*rp)++;
  1011. if (filters) {
  1012. if(row < raw_height && col < raw_width)
  1013. RBAYER(row,col) = **rp < 0x1000 ? curve[**rp] : **rp;
  1014. *rp += is_raw;
  1015. } else {
  1016. if (row < raw_height && col < raw_width)
  1017. FORC(tiff_samples)
  1018. color_image[row*raw_width+col][c] = (*rp)[c]<0x1000 ? curve[(*rp)[c]]:(*rp)[c];
  1019. *rp += tiff_samples;
  1020. }
  1021. #endif
  1022. if (is_raw == 2 && shot_select) (*rp)--;
  1023. }
  1024. void CLASS adobe_dng_load_raw_lj()
  1025. {
  1026. unsigned save, trow=0, tcol=0, jwide, jrow, jcol, row, col;
  1027. struct jhead jh;
  1028. ushort *rp;
  1029. while (trow < raw_height) {
  1030. save = ftell(ifp);
  1031. if (tile_length < INT_MAX)
  1032. fseek (ifp, get4(), SEEK_SET);
  1033. if (!ljpeg_start (&jh, 0)) break;
  1034. jwide = jh.wide;
  1035. if (filters) jwide *= jh.clrs;
  1036. jwide /= is_raw;
  1037. #ifdef LIBRAW_LIBRARY_BUILD
  1038. if(!data_size)
  1039. throw LIBRAW_EXCEPTION_IO_BADFILE;
  1040. LibRaw_byte_buffer *buf = ifp->make_byte_buffer(data_size);
  1041. LibRaw_bit_buffer bits;
  1042. #endif
  1043. for (row=col=jrow=0; jrow < jh.high; jrow++) {
  1044. #ifdef LIBRAW_LIBRARY_BUILD
  1045. rp = ljpeg_row_new (jrow, &jh,bits,buf);
  1046. #else
  1047. rp = ljpeg_row (jrow, &jh);
  1048. #endif
  1049. for (jcol=0; jcol < jwide; jcol++) {
  1050. adobe_copy_pixel (trow+row, tcol+col, &rp);
  1051. if (++col >= tile_width || col >= raw_width)
  1052. row += 1 + (col = 0);
  1053. }
  1054. }
  1055. fseek (ifp, save+4, SEEK_SET);
  1056. if ((tcol += tile_width) >= raw_width)
  1057. trow += tile_length + (tcol = 0);
  1058. ljpeg_end (&jh);
  1059. #ifdef LIBRAW_LIBRARY_BUILD
  1060. delete buf;
  1061. #endif
  1062. }
  1063. }
  1064. void CLASS adobe_dng_load_raw_nc()
  1065. {
  1066. ushort *pixel, *rp;
  1067. int row, col;
  1068. pixel = (ushort *) calloc (raw_width * tiff_samples, sizeof *pixel);
  1069. merror (pixel, "adobe_dng_load_raw_nc()");
  1070. #ifdef LIBRAW_LIBRARY_BUILD
  1071. int dsz= raw_height*raw_width * tiff_samples * tiff_bps/8;
  1072. LibRaw_byte_buffer *buf = NULL;
  1073. if (tiff_bps != 16)
  1074. {
  1075. buf = ifp->make_byte_buffer(dsz);
  1076. }
  1077. LibRaw_bit_buffer bits;
  1078. #endif
  1079. for (row=0; row < raw_height; row++) {
  1080. if (tiff_bps == 16)
  1081. read_shorts (pixel, raw_width * tiff_samples);
  1082. else {
  1083. #ifdef LIBRAW_LIBRARY_BUILD
  1084. bits.reset();
  1085. for (col=0; col < raw_width * tiff_samples; col++)
  1086. pixel[col] = bits._getbits(buf,tiff_bps,zero_after_ff);
  1087. #else
  1088. getbits(-1);
  1089. for (col=0; col < raw_width * tiff_samples; col++)
  1090. pixel[col] = getbits(tiff_bps);
  1091. #endif
  1092. }
  1093. for (rp=pixel, col=0; col < raw_width; col++)
  1094. adobe_copy_pixel (row, col, &rp);
  1095. }
  1096. free (pixel);
  1097. #ifdef LIBRAW_LIBRARY_BUILD
  1098. if(buf)
  1099. delete buf;
  1100. #endif
  1101. }
  1102. void CLASS pentax_load_raw()
  1103. {
  1104. ushort bit[2][15], huff[4097];
  1105. int dep, row, col, diff, c, i;
  1106. ushort vpred[2][2] = {{0,0},{0,0}}, hpred[2];
  1107. fseek (ifp, meta_offset, SEEK_SET);
  1108. dep = (get2() + 12) & 15;
  1109. fseek (ifp, 12, SEEK_CUR);
  1110. FORC(dep) bit[0][c] = get2();
  1111. FORC(dep) bit[1][c] = fgetc(ifp);
  1112. FORC(dep)
  1113. for (i=bit[0][c]; i <= ((bit[0][c]+(4096 >> bit[1][c])-1) & 4095); )
  1114. huff[++i] = bit[1][c] << 8 | c;
  1115. huff[0] = 12;
  1116. fseek (ifp, data_offset, SEEK_SET);
  1117. #ifdef LIBRAW_LIBRARY_BUILD
  1118. if(!data_size)
  1119. throw LIBRAW_EXCEPTION_IO_BADFILE;
  1120. LibRaw_byte_buffer *buf = ifp->make_byte_buffer(data_size);
  1121. LibRaw_bit_buffer bits;
  1122. bits.reset();
  1123. #else
  1124. getbits(-1);
  1125. #endif
  1126. for (row=0; row < raw_height; row++)
  1127. {
  1128. for (col=0; col < raw_width; col++) {
  1129. #ifdef LIBRAW_LIBRARY_BUILD
  1130. diff = ljpeg_diff_pef(bits,buf,huff);
  1131. #else
  1132. diff = ljpeg_diff (huff);
  1133. #endif
  1134. if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
  1135. else hpred[col & 1] += diff;
  1136. unsigned val = hpred[col & 1];
  1137. #ifndef LIBRAW_LIBRARY_BUILD
  1138. if ((unsigned) (row-top_margin) < height &&
  1139. (unsigned)(col-left_margin) < width)
  1140. BAYER(row-top_margin,col-left_margin) = val;
  1141. #else
  1142. RBAYER(row,col) = val;
  1143. #endif
  1144. if (val >> tiff_bps) derror();
  1145. }
  1146. }
  1147. #ifdef LIBRAW_LIBRARY_BUILD
  1148. delete buf;
  1149. #endif
  1150. }
  1151. void CLASS nikon_compressed_load_raw()
  1152. {
  1153. static const uchar nikon_tree[][32] = {
  1154. { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy */
  1155. 5,4,3,6,2,7,1,0,8,9,11,10,12 },
  1156. { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy after split */
  1157. 0x39,0x5a,0x38,0x27,0x16,5,4,3,2,1,0,11,12,12 },
  1158. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0, /* 12-bit lossless */
  1159. 5,4,6,3,7,2,8,1,9,0,10,11,12 },
  1160. { 0,1,4,3,1,1,1,1,1,2,0,0,0,0,0,0, /* 14-bit lossy */
  1161. 5,6,4,7,8,3,9,2,1,0,10,11,12,13,14 },
  1162. { 0,1,5,1,1,1,1,1,1,1,2,0,0,0,0,0, /* 14-bit lossy after split */
  1163. 8,0x5c,0x4b,0x3a,0x29,7,6,5,4,3,2,1,0,13,14 },
  1164. { 0,1,4,2,2,3,1,2,0,0,0,0,0,0,0,0, /* 14-bit lossless */
  1165. 7,6,8,5,9,4,10,3,11,12,2,0,1,13,14 } };
  1166. ushort *huff, ver0, ver1, vpred[2][2], hpred[2], csize;
  1167. int i, min, max, step=0, tree=0, split=0, row, col, len, shl, diff;
  1168. fseek (ifp, meta_offset, SEEK_SET);
  1169. ver0 = fgetc(ifp);
  1170. ver1 = fgetc(ifp);
  1171. if (ver0 == 0x49 || ver1 == 0x58)
  1172. fseek (ifp, 2110, SEEK_CUR);
  1173. if (ver0 == 0x46) tree = 2;
  1174. if (tiff_bps == 14) tree += 3;
  1175. read_shorts (vpred[0], 4);
  1176. max = 1 << tiff_bps & 0x7fff;
  1177. if ((csize = get2()) > 1)
  1178. step = max / (csize-1);
  1179. if (ver0 == 0x44 && ver1 == 0x20 && step > 0) {
  1180. for (i=0; i < csize; i++)
  1181. curve[i*step] = get2();
  1182. for (i=0; i < max; i++)
  1183. curve[i] = ( curve[i-i%step]*(step-i%step) +
  1184. curve[i-i%step+step]*(i%step) ) / step;
  1185. #ifdef LIBRAW_LIBRARY_BUILD
  1186. color_flags.curve_state = LIBRAW_COLORSTATE_LOADED;
  1187. #endif
  1188. fseek (ifp, meta_offset+562, SEEK_SET);
  1189. split = get2();
  1190. } else if (ver0 != 0x46 && csize <= 0x4001)
  1191. {
  1192. read_shorts (curve, max=csize);
  1193. #ifdef LIBRAW_LIBRARY_BUILD
  1194. color_flags.curve_state = LIBRAW_COLORSTATE_LOADED;
  1195. #endif
  1196. }
  1197. while (curve[max-2] == curve[max-1]) max--;
  1198. huff = make_decoder (nikon_tree[tree]);
  1199. fseek (ifp, data_offset, SEEK_SET);
  1200. #ifdef LIBRAW_LIBRARY_BUILD
  1201. if(!data_size)
  1202. throw LIBRAW_EXCEPTION_IO_BADFILE;
  1203. LibRaw_byte_buffer *buf = ifp->make_byte_buffer(data_size);
  1204. LibRaw_bit_buffer bits;
  1205. bits.reset();
  1206. #else
  1207. getbits(-1);
  1208. #endif
  1209. for (min=row=0; row < height; row++) {
  1210. if (split && row == split) {
  1211. free (huff);
  1212. huff = make_decoder (nikon_tree[tree+1]);
  1213. max += (min = 16) << 1;
  1214. }
  1215. for (col=0; col < raw_width; col++) {
  1216. #ifdef LIBRAW_LIBRARY_BUILD
  1217. i = bits._gethuff(buf,*huff,huff+1,zero_after_ff);
  1218. #else
  1219. i = gethuff(huff);
  1220. #endif
  1221. len = i & 15;
  1222. shl = i >> 4;
  1223. #ifdef LIBRAW_LIBRARY_BUILD
  1224. diff = ((bits._getbits(buf,len-shl,zero_after_ff) << 1) + 1) << shl >> 1;
  1225. #else
  1226. diff = ((getbits(len-shl) << 1) + 1) << shl >> 1;
  1227. #endif
  1228. if ((diff & (1 << (len-1))) == 0)
  1229. diff -= (1 << len) - !shl;
  1230. if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
  1231. else hpred[col & 1] += diff;
  1232. if ((ushort)(hpred[col & 1] + min) >= max) derror();
  1233. #ifndef LIBRAW_LIBRARY_BUILD
  1234. if ((unsigned) (col-left_margin) < width)
  1235. BAYER(row,col-left_margin) = curve[LIM((short)hpred[col & 1],0,0x3fff)];
  1236. #else
  1237. ushort xval = hpred[col & 1];
  1238. xval = curve[LIM((short)xval,0,0x3fff)];
  1239. RBAYER(row,col) = xval;
  1240. #endif
  1241. }
  1242. }
  1243. #ifdef LIBRAW_LIBRARY_BUILD
  1244. delete buf;
  1245. #endif
  1246. free (huff);
  1247. }
  1248. /*
  1249. Figure out if a NEF file is compressed. These fancy heuristics
  1250. are only needed for the D100, thanks to a bug in some cameras
  1251. that tags all images as "compressed".
  1252. */
  1253. int CLASS nikon_is_compressed()
  1254. {
  1255. uchar test[256];
  1256. int i;
  1257. fseek (ifp, data_offset, SEEK_SET);
  1258. fread (test, 1, 256, ifp);
  1259. for (i=15; i < 256; i+=16)
  1260. if (test[i]) return 1;
  1261. return 0;
  1262. }
  1263. /*
  1264. Returns 1 for a Coolpix 995, 0 for anything else.
  1265. */
  1266. int CLASS nikon_e995()
  1267. {
  1268. int i, histo[256];
  1269. const uchar often[] = { 0x00, 0x55, 0xaa, 0xff };
  1270. memset (histo, 0, sizeof histo);
  1271. fseek (ifp, -2000, SEEK_END);
  1272. for (i=0; i < 2000; i++)
  1273. histo[fgetc(ifp)]++;
  1274. for (i=0; i < 4; i++)
  1275. if (histo[often[i]] < 200)
  1276. return 0;
  1277. return 1;
  1278. }
  1279. /*
  1280. Returns 1 for a Coolpix 2100, 0 for anything else.
  1281. */
  1282. int CLASS nikon_e2100()
  1283. {
  1284. uchar t[12];
  1285. int i;
  1286. fseek (ifp, 0, SEEK_SET);
  1287. for (i=0; i < 1024; i++) {
  1288. fread (t, 1, 12, ifp);
  1289. if (((t[2] & t[4] & t[7] & t[9]) >> 4
  1290. & t[1] & t[6] & t[8] & t[11] & 3) != 3)
  1291. return 0;
  1292. }
  1293. return 1;
  1294. }
  1295. void CLASS nikon_3700()
  1296. {
  1297. int bits, i;
  1298. uchar dp[24];
  1299. static const struct {
  1300. int bits;
  1301. char t_make[12], t_model[15];
  1302. } table[] = {
  1303. { 0x00, "PENTAX", "Optio 33WR" },
  1304. { 0x03, "NIKON", "E3200" },
  1305. { 0x32, "NIKON", "E3700" },
  1306. { 0x33, "OLYMPUS", "C740UZ" } };
  1307. fseek (ifp, 3072, SEEK_SET);
  1308. fread (dp, 1, 24, ifp);
  1309. bits = (dp[8] & 3) << 4 | (dp[20] & 3);
  1310. for (i=0; i < sizeof table / sizeof *table; i++)
  1311. if (bits == table[i].bits) {
  1312. strcpy (make, table[i].t_make );
  1313. strcpy (model, table[i].t_model);
  1314. }
  1315. }
  1316. /*
  1317. Separates a Minolta DiMAGE Z2 from a Nikon E4300.
  1318. */
  1319. int CLASS minolta_z2()
  1320. {
  1321. int i, nz;
  1322. char tail[424];
  1323. fseek (ifp, -sizeof tail, SEEK_END);
  1324. fread (tail, 1, sizeof tail, ifp);
  1325. for (nz=i=0; i < sizeof tail; i++)
  1326. if (tail[i]) nz++;
  1327. return nz > 20;
  1328. }
  1329. /*
  1330. The Fuji Super CCD is just a Bayer grid rotated 45 degrees.
  1331. */
  1332. void CLASS fuji_load_raw()
  1333. {
  1334. #ifndef LIBRAW_LIBRARY_BUILD
  1335. ushort *pixel;
  1336. int wide, row, col, r, c;
  1337. fseek (ifp, (top_margin*raw_width + left_margin) * 2, SEEK_CUR);
  1338. wide = fuji_width << !fuji_layout;
  1339. pixel = (ushort *) calloc (wide, sizeof *pixel);
  1340. merror (pixel, "fuji_load_raw()");
  1341. for (row=0; row < raw_height; row++) {
  1342. read_shorts (pixel, wide);
  1343. fseek (ifp, 2*(raw_width - wide), SEEK_CUR);
  1344. for (col=0; col < wide; col++) {
  1345. if (fuji_layout) {
  1346. r = fuji_width - 1 - col + (row >> 1);
  1347. c = col + ((row+1) >> 1);
  1348. } else {
  1349. r = fuji_width - 1 + row - (col >> 1);
  1350. c = row + ((col+1) >> 1);
  1351. }
  1352. BAYER(r,c) = pixel[col];
  1353. }
  1354. }
  1355. free (pixel);
  1356. #else
  1357. read_shorts(raw_image,raw_width*raw_height);
  1358. #endif
  1359. }
  1360. void CLASS ppm_thumb()
  1361. {
  1362. char *thumb;
  1363. thumb_length = thumb_width*thumb_height*3;
  1364. thumb = (char *) malloc (thumb_length);
  1365. merror (thumb, "ppm_thumb()");
  1366. fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
  1367. fread (thumb, 1, thumb_length, ifp);
  1368. fwrite (thumb, 1, thumb_length, ofp);
  1369. free (thumb);
  1370. }
  1371. void CLASS layer_thumb()
  1372. {
  1373. int i, c;
  1374. char *thumb, map[][4] = { "012","102" };
  1375. colors = thumb_misc >> 5 & 7;
  1376. thumb_length = thumb_width*thumb_height;
  1377. thumb = (char *) calloc (colors, thumb_length);
  1378. merror (thumb, "layer_thumb()");
  1379. fprintf (ofp, "P%d\n%d %d\n255\n",
  1380. 5 + (colors >> 1), thumb_width, thumb_height);
  1381. fread (thumb, thumb_length, colors, ifp);
  1382. for (i=0; i < thumb_length; i++)
  1383. FORCC putc (thumb[i+thumb_length*(map[thumb_misc >> 8][c]-'0')], ofp);
  1384. free (thumb);
  1385. }
  1386. void CLASS rollei_thumb()
  1387. {
  1388. unsigned i;
  1389. ushort *thumb;
  1390. thumb_length = thumb_width * thumb_height;
  1391. thumb = (ushort *) calloc (thumb_length, 2);
  1392. merror (thumb, "rollei_thumb()");
  1393. fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
  1394. read_shorts (thumb, thumb_length);
  1395. for (i=0; i < thumb_length; i++) {
  1396. putc (thumb[i] << 3, ofp);
  1397. putc (thumb[i] >> 5 << 2, ofp);
  1398. putc (thumb[i] >> 11 << 3, ofp);
  1399. }
  1400. free (thumb);
  1401. }
  1402. void CLASS rollei_load_raw()
  1403. {
  1404. uchar pixel[10];
  1405. unsigned iten=0, isix, i, buffer=0, row, col, todo[16];
  1406. isix = raw_width * raw_height * 5 / 8;
  1407. while (fread (pixel, 1, 10, ifp) == 10) {
  1408. for (i=0; i < 10; i+=2) {
  1409. todo[i] = iten++;
  1410. todo[i+1] = pixel[i] << 8 | pixel[i+1];
  1411. buffer = pixel[i] >> 2 | buffer << 6;
  1412. }
  1413. for ( ; i < 16; i+=2) {
  1414. todo[i] = isix++;
  1415. todo[i+1] = buffer >> (14-i)*5;
  1416. }
  1417. for (i=0; i < 16; i+=2) {
  1418. #ifndef LIBRAW_LIBRARY_BUILD
  1419. row = todo[i] / raw_width - top_margin;
  1420. col = todo[i] % raw_width - left_margin;
  1421. if (row < height && col < width)
  1422. BAYER(row,col) = (todo[i+1] & 0x3ff);
  1423. #else
  1424. RBAYER(todo[i] / raw_width,todo[i] % raw_width) = (todo[i+1] & 0x3ff);
  1425. #endif
  1426. }
  1427. }
  1428. maximum = 0x3ff;
  1429. }
  1430. int CLASS bayer (unsigned row, unsigned col)
  1431. {
  1432. return (row < height && col < width) ? BAYER(row,col) : 0;
  1433. }
  1434. void CLASS phase_one_flat_field (int is_float, int nc)
  1435. {
  1436. ushort head[8];
  1437. unsigned wide, y, x, c, rend, cend, row, col;
  1438. float *mrow, num, mult[4];
  1439. read_shorts (head, 8);
  1440. wide = head[2] / head[4];
  1441. mrow = (float *) calloc (nc*wide, sizeof *mrow);
  1442. merror (mrow, "phase_one_flat_field()");
  1443. for (y=0; y < head[3] / head[5]; y++) {
  1444. for (x=0; x < wide; x++)
  1445. for (c=0; c < nc; c+=2) {
  1446. num = is_float ? getreal(11) : get2()/32768.0;
  1447. if (y==0) mrow[c*wide+x] = num;
  1448. else mrow[(c+1)*wide+x] = (num - mrow[c*wide+x]) / head[5];
  1449. }
  1450. if (y==0) continue;
  1451. rend = head[1]-top_margin + y*head[5];
  1452. for (row = rend-head[5]; row < height && row < rend; row++) {
  1453. for (x=1; x < wide; x++) {
  1454. for (c=0; c < nc; c+=2) {
  1455. mult[c] = mrow[c*wide+x-1];
  1456. mult[c+1] = (mrow[c*wide+x] - mult[c]) / head[4];
  1457. }
  1458. cend = head[0]-left_margin + x*head[4];
  1459. for (col = cend-head[4]; col < width && col < cend; col++) {
  1460. c = nc > 2 ? FC(row,col) : 0;
  1461. if (!(c & 1)) {
  1462. c = BAYER(row,col) * mult[c];
  1463. BAYER(row,col) = LIM(c,0,65535);
  1464. }
  1465. for (c=0; c < nc; c+=2)
  1466. mult[c] += mult[c+1];
  1467. }
  1468. }
  1469. for (x=0; x < wide; x++)
  1470. for (c=0; c < nc; c+=2)
  1471. mrow[c*wide+x] += mrow[(c+1)*wide+x];
  1472. }
  1473. }
  1474. free (mrow);
  1475. }
  1476. void CLASS phase_one_correct()
  1477. {
  1478. unsigned entries, tag, data, save, col, row, type;
  1479. int len, i, j, k, cip, val[4], dev[4], sum, max;
  1480. int head[9], diff, mindiff=INT_MAX, off_412=0;
  1481. static const signed char dir[12][2] =
  1482. { {-1,-1}, {-1,1}, {1,-1}, {1,1}, {-2,0}, {0,-2}, {0,2}, {2,0},
  1483. {-2,-2}, {-2,2}, {2,-2}, {2,2} };
  1484. float poly[8], num, cfrac, frac, mult[2], *yval[2];
  1485. ushort *xval[2];
  1486. if (half_size || !meta_length) return;
  1487. #ifdef DCRAW_VERBOSE
  1488. if (verbose) fprintf (stderr,_("Phase One correction...\n"));
  1489. #endif
  1490. fseek (ifp, meta_offset, SEEK_SET);
  1491. order = get2();
  1492. fseek (ifp, 6, SEEK_CUR);
  1493. fseek (ifp, meta_offset+get4(), SEEK_SET);
  1494. entries = get4(); get4();
  1495. while (entries--) {
  1496. tag = get4();
  1497. len = get4();
  1498. data = get4();
  1499. save = ftell(ifp);
  1500. fseek (ifp, meta_offset+data, SEEK_SET);
  1501. if (tag == 0x419) { /* Polynomial curve */
  1502. for (get4(), i=0; i < 8; i++)
  1503. poly[i] = getreal(11);
  1504. poly[3] += (ph1.tag_210 - poly[7]) * poly[6] + 1;
  1505. for (i=0; i < 0x10000; i++) {
  1506. num = (poly[5]*i + poly[3])*i + poly[1];
  1507. curve[i] = LIM(num,0,65535);
  1508. } goto apply; /* apply to right half */
  1509. } else if (tag == 0x41a) { /* Polynomial curve */
  1510. for (i=0; i < 4; i++)
  1511. poly[i] = getreal(11);
  1512. for (i=0; i < 0x10000; i++) {
  1513. for (num=0, j=4; j--; )
  1514. num = num * i + poly[j];
  1515. curve[i] = LIM(num+i,0,65535);
  1516. } apply: /* apply to whole image */
  1517. for (row=0; row < height; row++)
  1518. for (col = (tag & 1)*ph1.split_col; col < width; col++)
  1519. BAYER(row,col) = curve[BAYER(row,col)];
  1520. } else if (tag == 0x400) { /* Sensor defects */
  1521. while ((len -= 8) >= 0) {
  1522. col = get2() - left_margin;
  1523. row = get2() - top_margin;
  1524. type = get2(); get2();
  1525. if (col >= width) continue;
  1526. if (type == 131) /* Bad column */
  1527. for (row=0; row < height; row++)
  1528. if (FC(row,col) == 1) {
  1529. for (sum=i=0; i < 4; i++)
  1530. sum += val[i] = bayer (row+dir[i][0], col+dir[i][1]);
  1531. for (max=i=0; i < 4; i++) {
  1532. dev[i] = abs((val[i] << 2) - sum);
  1533. if (dev[max] < dev[i]) max = i;
  1534. }
  1535. BAYER(row,col) = (sum - val[max])/3.0 + 0.5;
  1536. } else {
  1537. for (sum=0, i=8; i < 12; i++)
  1538. sum += bayer (row+dir[i][0], col+dir[i][1]);
  1539. BAYER(row,col) = 0.5 + sum * 0.0732233 +
  1540. (bayer(row,col-2) + bayer(row,col+2)) * 0.3535534;
  1541. }
  1542. else if (type == 129) { /* Bad pixel */
  1543. if (row >= height) continue;
  1544. j = (FC(row,col) != 1) * 4;
  1545. for (sum=0, i=j; i < j+8; i++)
  1546. sum += bayer (row+dir[i][0], col+dir[i][1]);
  1547. BAYER(row,col) = (sum + 4) >> 3;
  1548. }
  1549. }
  1550. } else if (tag == 0x401) { /* All-color flat fields */
  1551. phase_one_flat_field (1, 2);
  1552. } else if (tag == 0x416 || tag == 0x410) {
  1553. phase_one_flat_field (0, 2);
  1554. } else if (tag == 0x40b) { /* Red+blue flat field */
  1555. phase_one_flat_field (0, 4);
  1556. } else if (tag == 0x412) {
  1557. fseek (ifp, 36, SEEK_CUR);
  1558. diff = abs (get2() - ph1.tag_21a);
  1559. if (mindiff > diff) {
  1560. mindiff = diff;
  1561. off_412 = ftell(ifp) - 38;
  1562. }
  1563. }
  1564. fseek (ifp, save, SEEK_SET);
  1565. }
  1566. if (off_412) {
  1567. fseek (ifp, off_412, SEEK_SET);
  1568. for (i=0; i < 9; i++) head[i] = get4() & 0x7fff;
  1569. yval[0] = (float *) calloc (head[1]*head[3] + head[2]*head[4], 6);
  1570. merror (yval[0], "phase_one_correct()");
  1571. yval[1] = (float *) (yval[0] + head[1]*head[3]);
  1572. xval[0] = (ushort *) (yval[1] + head[2]*head[4]);
  1573. xval[1] = (ushort *) (xval[0] + head[1]*head[3]);
  1574. get2();
  1575. for (i=0; i < 2; i++)
  1576. for (j=0; j < head[i+1]*head[i+3]; j++)
  1577. yval[i][j] = getreal(11);
  1578. for (i=0; i < 2; i++)
  1579. for (j=0; j < head[i+1]*head[i+3]; j++)
  1580. xval[i][j] = get2();
  1581. for (row=0; row < height; row++)
  1582. for (col=0; col < width; col++) {
  1583. cfrac = (float) col * head[3] / raw_width;
  1584. cfrac -= cip = cfrac;
  1585. num = BAYER(row,col) * 0.5;
  1586. for (i=cip; i < cip+2; i++) {
  1587. for (k=j=0; j < head[1]; j++)
  1588. if (num < xval[0][k = head[1]*i+j]) break;
  1589. frac = (j == 0 || j == head[1]) ? 0 :
  1590. (xval[0][k] - num) / (xval[0][k] - xval[0][k-1]);
  1591. mult[i-cip] = yval[0][k-1] * frac + yval[0][k] * (1-frac);
  1592. }
  1593. i = ((mult[0] * (1-cfrac) + mult[1] * cfrac)
  1594. * (row + top_margin) + num) * 2;
  1595. BAYER(row,col) = LIM(i,0,65535);
  1596. }
  1597. free (yval[0]);
  1598. }
  1599. }
  1600. void CLASS phase_one_load_raw()
  1601. {
  1602. int row, col, a, b;
  1603. ushort *pixel, akey, bkey, mask;
  1604. fseek (ifp, ph1.key_off, SEEK_SET);
  1605. akey = get2();
  1606. bkey = get2();
  1607. mask = ph1.format == 1 ? 0x5555:0x1354;
  1608. #ifndef LIBRAW_LIBRARY_BUILD
  1609. fseek (ifp, data_offset + top_margin*raw_width*2, SEEK_SET);
  1610. pixel = (ushort *) calloc (raw_width, sizeof *pixel);
  1611. merror (pixel, "phase_one_load_raw()");
  1612. for (row=0; row < height; row++) {
  1613. read_shorts (pixel, raw_width);
  1614. if (ph1.format)
  1615. for (col=0; col < raw_width; col+=2) {
  1616. a = pixel[col+0] ^ akey;
  1617. b = pixel[col+1] ^ bkey;
  1618. pixel[col+0] = (a & mask) | (b & ~mask);
  1619. pixel[col+1] = (b & mask) | (a & ~mask);
  1620. }
  1621. for (col=0; col < width; col++)
  1622. BAYER(row,col) = pixel[col+left_margin];
  1623. }
  1624. free (pixel);
  1625. phase_one_correct();
  1626. #else
  1627. fseek (ifp, data_offset, SEEK_SET);
  1628. pixel = (ushort *) calloc (raw_width, sizeof *pixel);
  1629. merror (pixel, "phase_one_load_raw()");
  1630. for (row=0; row < raw_height; row++) {
  1631. read_shorts (pixel, raw_width);
  1632. if (ph1.format)
  1633. for (col=0; col < raw_width; col+=2) {
  1634. a = pixel[col+0] ^ akey;
  1635. b = pixel[col+1] ^ bkey;
  1636. pixel[col+0] = (a & mask) | (b & ~mask);
  1637. pixel[col+1] = (b & mask) | (a & ~mask);
  1638. }
  1639. memmove(&raw_image[row*raw_width],pixel,raw_width*sizeof(pixel[0]));
  1640. }
  1641. free (pixel);
  1642. // use correct on postprocessing!
  1643. imgdata.rawdata.use_ph1_correct=1;
  1644. #endif
  1645. }
  1646. unsigned CLASS ph1_bithuff (int nbits, ushort *huff)
  1647. {
  1648. #ifndef LIBRAW_NOTHREADS
  1649. #define bitbuf tls->ph1_bits.bitbuf
  1650. #define vbits tls->ph1_bits.vbits
  1651. #else
  1652. static UINT64 bitbuf=0;
  1653. static int vbits=0;
  1654. #endif
  1655. unsigned c;
  1656. if (nbits == -1)
  1657. return bitbuf = vbits = 0;
  1658. if (nbits == 0) return 0;
  1659. if (vbits < nbits) {
  1660. bitbuf = bitbuf << 32 | get4();
  1661. vbits += 32;
  1662. }
  1663. c = bitbuf << (64-vbits) >> (64-nbits);
  1664. if (huff) {
  1665. vbits -= huff[c] >> 8;
  1666. return (uchar) huff[c];
  1667. }
  1668. vbits -= nbits;
  1669. return c;
  1670. #ifndef LIBRAW_NOTHREADS
  1671. #undef bitbuf
  1672. #undef vbits
  1673. #endif
  1674. }
  1675. #define ph1_bits(n) ph1_bithuff(n,0)
  1676. #define ph1_huff(h) ph1_bithuff(*h,h+1)
  1677. void CLASS phase_one_load_raw_c()
  1678. {
  1679. static const int length[] = { 8,7,6,9,11,10,5,12,14,13 };
  1680. int *offset, len[2], pred[2], row, col, i, j;
  1681. ushort *pixel;
  1682. short (*t_black)[2];
  1683. pixel = (ushort *) calloc (raw_width + raw_height*4, 2);
  1684. merror (pixel, "phase_one_load_raw_c()");
  1685. offset = (int *) (pixel + raw_width);
  1686. fseek (ifp, strip_offset, SEEK_SET);
  1687. for (row=0; row < raw_height; row++)
  1688. offset[row] = get4();
  1689. t_black = (short (*)[2]) offset + raw_height;
  1690. fseek (ifp, ph1.black_off, SEEK_SET);
  1691. if (ph1.black_off)
  1692. {
  1693. read_shorts ((ushort *) t_black[0], raw_height*2);
  1694. #ifdef LIBRAW_LIBRARY_BUILD
  1695. imgdata.rawdata.ph1_black = (short (*)[2])calloc(raw_height*2,sizeof(short));
  1696. merror (imgdata.rawdata.ph1_black, "phase_one_load_raw_c()");
  1697. memmove(imgdata.rawdata.ph1_black,(short *) t_black[0],raw_height*2*sizeof(short));
  1698. #endif
  1699. }
  1700. for (i=0; i < 256; i++)
  1701. curve[i] = i*i / 3.969 + 0.5;
  1702. #ifdef LIBRAW_LIBRARY_BUILD
  1703. color_flags.curve_state = LIBRAW_COLORSTATE_CALCULATED;
  1704. #endif
  1705. for (row=0; row < raw_height; row++) {
  1706. fseek (ifp, data_offset + offset[row], SEEK_SET);
  1707. ph1_bits(-1);
  1708. pred[0] = pred[1] = 0;
  1709. for (col=0; col < raw_width; col++) {
  1710. if (col >= (raw_width & -8))
  1711. len[0] = len[1] = 14;
  1712. else if ((col & 7) == 0)
  1713. for (i=0; i < 2; i++) {
  1714. for (j=0; j < 5 && !ph1_bits(1); j++);
  1715. if (j--) len[i] = length[j*2 + ph1_bits(1)];
  1716. }
  1717. if ((i = len[col & 1]) == 14)
  1718. pixel[col] = pred[col & 1] = ph1_bits(16);
  1719. else
  1720. pixel[col] = pred[col & 1] += ph1_bits(i) + 1 - (1 << (i - 1));
  1721. if (pred[col & 1] >> 16) derror();
  1722. if (ph1.format == 5 && pixel[col] < 256)
  1723. pixel[col] = curve[pixel[col]];
  1724. }
  1725. #ifndef LIBRAW_LIBRARY_BUILD
  1726. if ((unsigned) (row-top_margin) < height)
  1727. for (col=0; col < width; col++) {
  1728. i = (pixel[col+left_margin] << 2)
  1729. - ph1.t_black + t_black[row][col >= ph1.split_col];
  1730. if (i > 0) BAYER(row-top_margin,col) = i;
  1731. }
  1732. #else
  1733. for (col=0; col < raw_width; col++)
  1734. {
  1735. i = (pixel[col] << 2);
  1736. RBAYER(row,col) = i;
  1737. }
  1738. #endif
  1739. }
  1740. free (pixel);
  1741. #ifndef LIBRAW_LIBRARY_BUILD
  1742. phase_one_correct();
  1743. maximum = 0xfffc - ph1.t_black;
  1744. #else
  1745. maximum = 0xfffc;
  1746. black = ph1.t_black;
  1747. #endif
  1748. }
  1749. void CLASS hasselblad_load_raw()
  1750. {
  1751. struct jhead jh;
  1752. int row, col, pred[2], len[2], diff, c;
  1753. if (!ljpeg_start (&jh, 0)) return;
  1754. order = 0x4949;
  1755. ph1_bits(-1);
  1756. #ifndef LIBRAW_LIBRARY_BUILD
  1757. for (row=-top_margin; row < height; row++) {
  1758. pred[0] = pred[1] = 0x8000 + load_flags;
  1759. for (col=-left_margin; col < raw_width-left_margin; col+=2) {
  1760. FORC(2) len[c] = ph1_huff(jh.huff[0]);
  1761. FORC(2) {
  1762. diff = ph1_bits(len[c]);
  1763. if ((diff & (1 << (len[c]-1))) == 0)
  1764. diff -= (1 << len[c]) - 1;
  1765. if (diff == 65535) diff = -32768;
  1766. pred[c] += diff;
  1767. if (row >= 0 && (unsigned)(col+c) < width)
  1768. BAYER(row,col+c) = pred[c];
  1769. }
  1770. }
  1771. }
  1772. #else
  1773. for (row=0; row < raw_height; row++) {
  1774. pred[0] = pred[1] = 0x8000 + load_flags;
  1775. for (col=0; col < raw_width; col+=2) {
  1776. FORC(2) len[c] = ph1_huff(jh.huff[0]);
  1777. FORC(2) {
  1778. diff = ph1_bits(len[c]);
  1779. if ((diff & (1 << (len[c]-1))) == 0)
  1780. diff -= (1 << len[c]) - 1;
  1781. if (diff == 65535) diff = -32768;
  1782. pred[c] += diff;
  1783. RBAYER(row,col+c) = pred[c];
  1784. }
  1785. }
  1786. }
  1787. #endif
  1788. ljpeg_end (&jh);
  1789. maximum = 0xffff;
  1790. }
  1791. void CLASS leaf_hdr_load_raw()
  1792. {
  1793. ushort *pixel;
  1794. unsigned tile=0, r, c, row, col;
  1795. pixel = (ushort *) calloc (raw_width, sizeof *pixel);
  1796. merror (pixel, "leaf_hdr_load_raw()");
  1797. FORC(tiff_samples)
  1798. for (r=0; r < raw_height; r++) {
  1799. if (r % tile_length == 0) {
  1800. fseek (ifp, data_offset + 4*tile++, SEEK_SET);
  1801. fseek (ifp, get4() + 2*left_margin, SEEK_SET);
  1802. }
  1803. if (filters && c != shot_select) continue;
  1804. read_shorts (pixel, raw_width);
  1805. #ifndef LIBRAW_LIBRARY_BUILD
  1806. if ((row = r - top_margin) >= height) continue;
  1807. for (col=0; col < width; col++)
  1808. if (filters) BAYER(row,col) = pixel[col];
  1809. else image[row*width+col][c] = pixel[col];
  1810. #else
  1811. if(filters)
  1812. memmove(&raw_image[r*raw_width],pixel,raw_width*sizeof(pixel[0]));
  1813. else
  1814. for (col=0; col < raw_width; col++)
  1815. color_image[r*raw_width+col][c] = pixel[col];
  1816. #endif
  1817. }
  1818. free (pixel);
  1819. if (!filters) {
  1820. maximum = 0xffff;
  1821. raw_color = 1;
  1822. }
  1823. }
  1824. void CLASS sinar_4shot_load_raw()
  1825. {
  1826. ushort *pixel;
  1827. unsigned shot, row, col, r, c;
  1828. if ((shot = shot_select) || half_size) {
  1829. if (shot) shot--;
  1830. if (shot > 3) shot = 3;
  1831. fseek (ifp, data_offset + shot*4, SEEK_SET);
  1832. fseek (ifp, get4(), SEEK_SET);
  1833. unpacked_load_raw();
  1834. return;
  1835. }
  1836. free (image);
  1837. image = (ushort (*)[4])
  1838. calloc ((iheight=height)*(iwidth=width), sizeof *image);
  1839. merror (image, "sinar_4shot_load_raw()");
  1840. pixel = (ushort *) calloc (raw_width, sizeof *pixel);
  1841. merror (pixel, "sinar_4shot_load_raw()");
  1842. for (shot=0; shot < 4; shot++) {
  1843. fseek (ifp, data_offset + shot*4, SEEK_SET);
  1844. fseek (ifp, get4(), SEEK_SET);
  1845. #ifndef LIBRAW_LIBRARY_BUILD
  1846. for (row=0; row < raw_height; row++) {
  1847. read_shorts (pixel, raw_width);
  1848. if ((r = row-top_margin - (shot >> 1 & 1)) >= height) continue;
  1849. for (col=0; col < raw_width; col++) {
  1850. if ((c = col-left_margin - (shot & 1)) >= width) continue;
  1851. image[r*width+c][FC(row,col)] = pixel[col];
  1852. }
  1853. }
  1854. #else
  1855. for (row=0; row < raw_height; row++) {
  1856. read_shorts (pixel, raw_width);
  1857. if ((r = row - (shot >> 1 & 1)) >= raw_height) continue;
  1858. for (col=0; col < raw_width; col++) {
  1859. if ((c = col- (shot & 1)) >= raw_width) continue;
  1860. color_image[r*width+c][FC(row,col)] = pixel[col];
  1861. }
  1862. }
  1863. #endif
  1864. }
  1865. free (pixel);
  1866. shrink = filters = 0;
  1867. }
  1868. void CLASS imacon_full_load_raw()
  1869. {
  1870. int row, col;
  1871. #ifndef LIBRAW_LIBRARY_BUILD
  1872. for (row=0; row < height; row++)
  1873. for (col=0; col < width; col++)
  1874. {
  1875. read_shorts (image[row*width+col], 3);
  1876. }
  1877. #else
  1878. for (row=0; row < height; row++)
  1879. for (col=0; col < width; col++)
  1880. {
  1881. read_shorts (color_image[(row+top_margin)*raw_width+col+left_margin], 3);
  1882. }
  1883. #endif
  1884. }
  1885. void CLASS packed_load_raw()
  1886. {
  1887. int vbits=0, bwide, pwide, rbits, bite, half, irow, row, col, val, i;
  1888. int zero=0;
  1889. UINT64 bitbuf=0;
  1890. if (raw_width * 8 >= width * tiff_bps) /* Is raw_width in bytes? */
  1891. pwide = (bwide = raw_width) * 8 / tiff_bps;
  1892. else bwide = (pwide = raw_width) * tiff_bps / 8;
  1893. rbits = bwide * 8 - pwide * tiff_bps;
  1894. if (load_flags & 1) bwide = bwide * 16 / 15;
  1895. fseek (ifp, top_margin*bwide, SEEK_CUR);
  1896. bite = 8 + (load_flags & 24);
  1897. half = (height+1) >> 1;
  1898. for (irow=0; irow < height; irow++)
  1899. {
  1900. row = irow;
  1901. if (load_flags & 2 &&
  1902. (row = irow % half * 2 + irow / half) == 1 &&
  1903. load_flags & 4) {
  1904. if (vbits=0, tiff_compress)
  1905. fseek (ifp, data_offset - (-half*bwide & -2048), SEEK_SET);
  1906. else {
  1907. fseek (ifp, 0, SEEK_END);
  1908. fseek (ifp, ftell(ifp) >> 3 << 2, SEEK_SET);
  1909. }
  1910. }
  1911. for (col=0; col < pwide; col++) {
  1912. for (vbits -= tiff_bps; vbits < 0; vbits += bite) {
  1913. bitbuf <<= bite;
  1914. for (i=0; i < bite; i+=8)
  1915. bitbuf |= (unsigned) (fgetc(ifp) << i);
  1916. }
  1917. val = bitbuf << (64-tiff_bps-vbits) >> (64-tiff_bps);
  1918. i = (col ^ (load_flags >> 6)) - left_margin;
  1919. #ifdef LIBRAW_LIBRARY_BUILD
  1920. RBAYER(row+top_margin,i+left_margin) = val;
  1921. if (((unsigned)i>=width) && (load_flags & 32) ) {
  1922. black += val;
  1923. zero += !val;
  1924. }
  1925. #else
  1926. if ((unsigned) i < width)
  1927. BAYER(row,i+left_margin) = val;
  1928. else if (load_flags & 32) {
  1929. black += val;
  1930. zero += !val;
  1931. }
  1932. #endif
  1933. if (load_flags & 1 && (col % 10) == 9 &&
  1934. fgetc(ifp) && col < width+left_margin) derror();
  1935. }
  1936. vbits -= rbits;
  1937. }
  1938. if (load_flags & 32 && pwide > width)
  1939. black /= (pwide - width) * height;
  1940. if (zero*4 > (pwide - width) * height)
  1941. black = 0;
  1942. }
  1943. void CLASS unpacked_load_raw()
  1944. {
  1945. ushort *pixel;
  1946. int row, col, bits=0;
  1947. while (1 << ++bits < maximum);
  1948. #ifndef LIBRAW_LIBRARY_BUILD
  1949. fseek (ifp, (top_margin*raw_width + left_margin) * 2, SEEK_CUR);
  1950. pixel = (ushort *) calloc (width, sizeof *pixel);
  1951. merror (pixel, "unpacked_load_raw()");
  1952. for (row=0; row < height; row++) {
  1953. read_shorts (pixel, width);
  1954. fseek (ifp, 2*(raw_width - width), SEEK_CUR);
  1955. for (col=0; col < width; col++)
  1956. if ((BAYER2(row,col) = pixel[col] >> load_flags) >> bits) derror();
  1957. }
  1958. free (pixel);
  1959. #else
  1960. // fseek (ifp, (top_margin*raw_width + left_margin) * 2, SEEK_CUR);
  1961. pixel = (ushort *) calloc (raw_width, sizeof *pixel);
  1962. merror (pixel, "unpacked_load_raw()");
  1963. for (row=0; row < raw_height; row++) {
  1964. read_shorts (pixel, raw_width);
  1965. for (col=0; col < raw_width; col++)
  1966. {
  1967. RBAYER(row,col) = pixel[col]>>load_flags;
  1968. if( ((unsigned)(row-top_margin) < height)
  1969. && ((unsigned)(col-left_margin)<width ))
  1970. if(RBAYER(row,col)>>bits)
  1971. derror();
  1972. }
  1973. }
  1974. free (pixel);
  1975. #endif
  1976. }
  1977. void CLASS nokia_load_raw()
  1978. {
  1979. uchar *data, *dp;
  1980. ushort *pixel, *pix;
  1981. int rev, dwide, row, c;
  1982. rev = 3 * (order == 0x4949);
  1983. dwide = raw_width * 5 / 4;
  1984. data = (uchar *) malloc (dwide + raw_width*2);
  1985. merror (data, "nokia_load_raw()");
  1986. pixel = (ushort *) (data + dwide);
  1987. for (row=0; row < raw_height; row++) {
  1988. if (fread (data+dwide, 1, dwide, ifp) < dwide) derror();
  1989. FORC(dwide) data[c] = data[dwide+(c ^ rev)];
  1990. for (dp=data, pix=pixel; pix < pixel+raw_width; dp+=5, pix+=4)
  1991. FORC4 pix[c] = (dp[c] << 2) | (dp[4] >> (c << 1) & 3);
  1992. if (row < top_margin)
  1993. FORC(width) black += pixel[c];
  1994. #ifndef LIBRAW_LIBRARY_BUILD
  1995. else
  1996. FORC(width) BAYER(row-top_margin,c) = pixel[c];
  1997. #else
  1998. memmove(&raw_image[row*raw_width],pixel,width*sizeof(pixel[0]));
  1999. #endif
  2000. }
  2001. free (data);
  2002. if (top_margin) black /= top_margin * width;
  2003. maximum = 0x3ff;
  2004. }
  2005. unsigned CLASS pana_bits (int nbits)
  2006. {
  2007. #ifndef LIBRAW_NOTHREADS
  2008. #define buf tls->pana_bits.buf
  2009. #define vbits tls->pana_bits.vbits
  2010. #else
  2011. static uchar buf[0x4000];
  2012. static int vbits;
  2013. #endif
  2014. int byte;
  2015. if (!nbits) return vbits=0;
  2016. if (!vbits) {
  2017. fread (buf+load_flags, 1, 0x4000-load_flags, ifp);
  2018. fread (buf, 1, load_flags, ifp);
  2019. }
  2020. vbits = (vbits - nbits) & 0x1ffff;
  2021. byte = vbits >> 3 ^ 0x3ff0;
  2022. return (buf[byte] | buf[byte+1] << 8) >> (vbits & 7) & ~(-1 << nbits);
  2023. #ifndef LIBRAW_NOTHREADS
  2024. #undef buf
  2025. #undef vbits
  2026. #endif
  2027. }
  2028. void CLASS panasonic_load_raw()
  2029. {
  2030. int row, col, i, j, sh=0, pred[2], nonz[2];
  2031. pana_bits(0);
  2032. for (row=0; row < height; row++)
  2033. for (col=0; col < raw_width; col++) {
  2034. if ((i = col % 14) == 0)
  2035. pred[0] = pred[1] = nonz[0] = nonz[1] = 0;
  2036. if (i % 3 == 2) sh = 4 >> (3 - pana_bits(2));
  2037. if (nonz[i & 1]) {
  2038. if ((j = pana_bits(8))) {
  2039. if ((pred[i & 1] -= 0x80 << sh) < 0 || sh == 4)
  2040. pred[i & 1] &= ~(-1 << sh);
  2041. pred[i & 1] += j << sh;
  2042. }
  2043. } else if ((nonz[i & 1] = pana_bits(8)) || i > 11)
  2044. pred[i & 1] = nonz[i & 1] << 4 | pana_bits(4);
  2045. #ifndef LIBRAW_LIBRARY_BUILD
  2046. if (col < width)
  2047. if ((BAYER(row,col) = pred[col & 1]) > 4098) derror();
  2048. #else
  2049. RBAYER(row,col) = pred[col & 1];
  2050. if (col < width)
  2051. if (RBAYER(row,col) > 4098) derror();
  2052. #endif
  2053. }
  2054. }
  2055. void CLASS olympus_load_raw()
  2056. {
  2057. ushort huff[4096];
  2058. int row, col, nbits, sign, low, high, i, c, w, n, nw;
  2059. int acarry[2][3], *carry, pred, diff;
  2060. huff[n=0] = 0xc0c;
  2061. for (i=12; i--; )
  2062. FORC(2048 >> i) huff[++n] = (i+1) << 8 | i;
  2063. fseek (ifp, 7, SEEK_CUR);
  2064. #ifdef LIBRAW_LIBRARY_BUILD
  2065. if(!data_size)
  2066. throw LIBRAW_EXCEPTION_IO_BADFILE;
  2067. LibRaw_byte_buffer *buf = ifp->make_byte_buffer(data_size);
  2068. LibRaw_bit_buffer bits;
  2069. bits.reset();
  2070. #else
  2071. getbits(-1);
  2072. #endif
  2073. for (row=0; row < height; row++) {
  2074. memset (acarry, 0, sizeof acarry);
  2075. for (col=0; col < raw_width; col++) {
  2076. carry = acarry[col & 1];
  2077. i = 2 * (carry[2] < 3);
  2078. for (nbits=2+i; (ushort) carry[0] >> (nbits+i); nbits++);
  2079. #ifdef LIBRAW_LIBRARY_BUILD
  2080. low = (sign = bits._getbits(buf,3,zero_after_ff)) & 3;
  2081. sign = sign << 29 >> 31;
  2082. if ((high = bits._gethuff(buf,12,huff,zero_after_ff)) == 12)
  2083. high = bits._getbits(buf,16-nbits,zero_after_ff) >> 1;
  2084. carry[0] = (high << nbits) | bits._getbits(buf,nbits,zero_after_ff);
  2085. #else
  2086. low = (sign = getbits(3)) & 3;
  2087. sign = sign << 29 >> 31;
  2088. if ((high = getbithuff(12,huff)) == 12)
  2089. high = getbits(16-nbits) >> 1;
  2090. carry[0] = (high << nbits) | getbits(nbits);
  2091. #endif
  2092. diff = (carry[0] ^ sign) + carry[1];
  2093. carry[1] = (diff*3 + carry[1]) >> 5;
  2094. carry[2] = carry[0] > 16 ? 0 : carry[2]+1;
  2095. if (col >= width) continue;
  2096. if (row < 2 && col < 2) pred = 0;
  2097. else if (row < 2) pred = RBAYER(row,col-2);
  2098. else if (col < 2) pred = RBAYER(row-2,col);
  2099. else {
  2100. w = RBAYER(row,col-2);
  2101. n = RBAYER(row-2,col);
  2102. nw = RBAYER(row-2,col-2);
  2103. if ((w < nw && nw < n) || (n < nw && nw < w)) {
  2104. if (ABS(w-nw) > 32 || ABS(n-nw) > 32)
  2105. pred = w + n - nw;
  2106. else pred = (w + n) >> 1;
  2107. } else pred = ABS(w-nw) > ABS(n-nw) ? w : n;
  2108. }
  2109. if ((RBAYER(row,col) = pred + ((diff << 2) | low)) >> 12) derror();
  2110. }
  2111. }
  2112. #ifdef LIBRAW_LIBRARY_BUILD
  2113. delete buf;
  2114. #endif
  2115. }
  2116. void CLASS minolta_rd175_load_raw()
  2117. {
  2118. uchar pixel[768];
  2119. unsigned irow, box, row, col;
  2120. for (irow=0; irow < 1481; irow++) {
  2121. if (fread (pixel, 1, 768, ifp) < 768) derror();
  2122. box = irow / 82;
  2123. row = irow % 82 * 12 + ((box < 12) ? box | 1 : (box-12)*2);
  2124. switch (irow) {
  2125. case 1477: case 1479: continue;
  2126. case 1476: row = 984; break;
  2127. case 1480: row = 985; break;
  2128. case 1478: row = 985; box = 1;
  2129. }
  2130. if ((box < 12) && (box & 1)) {
  2131. for (col=0; col < 1533; col++, row ^= 1)
  2132. if (col != 1) RRBAYER(row,col) = (col+1) & 2 ?
  2133. pixel[col/2-1] + pixel[col/2+1] : pixel[col/2] << 1;
  2134. RRBAYER(row,1) = pixel[1] << 1;
  2135. RRBAYER(row,1533) = pixel[765] << 1;
  2136. } else
  2137. for (col=row & 1; col < 1534; col+=2)
  2138. RRBAYER(row,col) = pixel[col/2] << 1;
  2139. }
  2140. maximum = 0xff << 1;
  2141. }
  2142. void CLASS quicktake_100_load_raw()
  2143. {
  2144. uchar pixel[484][644];
  2145. static const short gstep[16] =
  2146. { -89,-60,-44,-32,-22,-15,-8,-2,2,8,15,22,32,44,60,89 };
  2147. static const short rstep[6][4] =
  2148. { { -3,-1,1,3 }, { -5,-1,1,5 }, { -8,-2,2,8 },
  2149. { -13,-3,3,13 }, { -19,-4,4,19 }, { -28,-6,6,28 } };
  2150. static const short t_curve[256] =
  2151. { 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,
  2152. 28,29,30,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,53,
  2153. 54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,74,75,76,77,78,
  2154. 79,80,81,82,83,84,86,88,90,92,94,97,99,101,103,105,107,110,112,114,116,
  2155. 118,120,123,125,127,129,131,134,136,138,140,142,144,147,149,151,153,155,
  2156. 158,160,162,164,166,168,171,173,175,177,179,181,184,186,188,190,192,195,
  2157. 197,199,201,203,205,208,210,212,214,216,218,221,223,226,230,235,239,244,
  2158. 248,252,257,261,265,270,274,278,283,287,291,296,300,305,309,313,318,322,
  2159. 326,331,335,339,344,348,352,357,361,365,370,374,379,383,387,392,396,400,
  2160. 405,409,413,418,422,426,431,435,440,444,448,453,457,461,466,470,474,479,
  2161. 483,487,492,496,500,508,519,531,542,553,564,575,587,598,609,620,631,643,
  2162. 654,665,676,687,698,710,721,732,743,754,766,777,788,799,810,822,833,844,
  2163. 855,866,878,889,900,911,922,933,945,956,967,978,989,1001,1012,1023 };
  2164. int rb, row, col, sharp, val=0;
  2165. getbits(-1);
  2166. memset (pixel, 0x80, sizeof pixel);
  2167. for (row=2; row < height+2; row++) {
  2168. for (col=2+(row & 1); col < width+2; col+=2) {
  2169. val = ((pixel[row-1][col-1] + 2*pixel[row-1][col+1] +
  2170. pixel[row][col-2]) >> 2) + gstep[getbits(4)];
  2171. pixel[row][col] = val = LIM(val,0,255);
  2172. if (col < 4)
  2173. pixel[row][col-2] = pixel[row+1][~row & 1] = val;
  2174. if (row == 2)
  2175. pixel[row-1][col+1] = pixel[row-1][col+3] = val;
  2176. }
  2177. pixel[row][col] = val;
  2178. }
  2179. for (rb=0; rb < 2; rb++)
  2180. for (row=2+rb; row < height+2; row+=2)
  2181. for (col=3-(row & 1); col < width+2; col+=2) {
  2182. if (row < 4 || col < 4) sharp = 2;
  2183. else {
  2184. val = ABS(pixel[row-2][col] - pixel[row][col-2])
  2185. + ABS(pixel[row-2][col] - pixel[row-2][col-2])
  2186. + ABS(pixel[row][col-2] - pixel[row-2][col-2]);
  2187. sharp = val < 4 ? 0 : val < 8 ? 1 : val < 16 ? 2 :
  2188. val < 32 ? 3 : val < 48 ? 4 : 5;
  2189. }
  2190. val = ((pixel[row-2][col] + pixel[row][col-2]) >> 1)
  2191. + rstep[sharp][getbits(2)];
  2192. pixel[row][col] = val = LIM(val,0,255);
  2193. if (row < 4) pixel[row-2][col+2] = val;
  2194. if (col < 4) pixel[row+2][col-2] = val;
  2195. }
  2196. for (row=2; row < height+2; row++)
  2197. for (col=3-(row & 1); col < width+2; col+=2) {
  2198. val = ((pixel[row][col-1] + (pixel[row][col] << 2) +
  2199. pixel[row][col+1]) >> 1) - 0x100;
  2200. pixel[row][col] = LIM(val,0,255);
  2201. }
  2202. for (row=0; row < height; row++)
  2203. for (col=0; col < width; col++)
  2204. RBAYER(row,col) = t_curve[pixel[row+2][col+2]];
  2205. maximum = 0x3ff;
  2206. }
  2207. #define radc_token(tree) ((signed char) getbithuff(8,huff[tree]))
  2208. #define FORYX for (y=1; y < 3; y++) for (x=col+1; x >= col; x--)
  2209. #define PREDICTOR (c ? (buf[c][y-1][x] + buf[c][y][x+1]) / 2 \
  2210. : (buf[c][y-1][x+1] + 2*buf[c][y-1][x] + buf[c][y][x+1]) / 4)
  2211. void CLASS kodak_radc_load_raw()
  2212. {
  2213. static const char src[] = {
  2214. 1,1, 2,3, 3,4, 4,2, 5,7, 6,5, 7,6, 7,8,
  2215. 1,0, 2,1, 3,3, 4,4, 5,2, 6,7, 7,6, 8,5, 8,8,
  2216. 2,1, 2,3, 3,0, 3,2, 3,4, 4,6, 5,5, 6,7, 6,8,
  2217. 2,0, 2,1, 2,3, 3,2, 4,4, 5,6, 6,7, 7,5, 7,8,
  2218. 2,1, 2,4, 3,0, 3,2, 3,3, 4,7, 5,5, 6,6, 6,8,
  2219. 2,3, 3,1, 3,2, 3,4, 3,5, 3,6, 4,7, 5,0, 5,8,
  2220. 2,3, 2,6, 3,0, 3,1, 4,4, 4,5, 4,7, 5,2, 5,8,
  2221. 2,4, 2,7, 3,3, 3,6, 4,1, 4,2, 4,5, 5,0, 5,8,
  2222. 2,6, 3,1, 3,3, 3,5, 3,7, 3,8, 4,0, 5,2, 5,4,
  2223. 2,0, 2,1, 3,2, 3,3, 4,4, 4,5, 5,6, 5,7, 4,8,
  2224. 1,0, 2,2, 2,-2,
  2225. 1,-3, 1,3,
  2226. 2,-17, 2,-5, 2,5, 2,17,
  2227. 2,-7, 2,2, 2,9, 2,18,
  2228. 2,-18, 2,-9, 2,-2, 2,7,
  2229. 2,-28, 2,28, 3,-49, 3,-9, 3,9, 4,49, 5,-79, 5,79,
  2230. 2,-1, 2,13, 2,26, 3,39, 4,-16, 5,55, 6,-37, 6,76,
  2231. 2,-26, 2,-13, 2,1, 3,-39, 4,16, 5,-55, 6,-76, 6,37
  2232. };
  2233. ushort huff[19][256];
  2234. int row, col, tree, nreps, rep, step, i, c, s, r, x, y, val;
  2235. short last[3] = { 16,16,16 }, mul[3], buf[3][3][386];
  2236. static const ushort pt[] =
  2237. { 0,0, 1280,1344, 2320,3616, 3328,8000, 4095,16383, 65535,16383 };
  2238. for (i=2; i < 12; i+=2)
  2239. for (c=pt[i-2]; c <= pt[i]; c++)
  2240. curve[c] = (float)
  2241. (c-pt[i-2]) / (pt[i]-pt[i-2]) * (pt[i+1]-pt[i-1]) + pt[i-1] + 0.5;
  2242. for (s=i=0; i < sizeof src; i+=2)
  2243. FORC(256 >> src[i])
  2244. huff[0][s++] = src[i] << 8 | (uchar) src[i+1];
  2245. s = kodak_cbpp == 243 ? 2 : 3;
  2246. FORC(256) huff[18][c] = (8-s) << 8 | c >> s << s | 1 << (s-1);
  2247. getbits(-1);
  2248. for (i=0; i < sizeof(buf)/sizeof(short); i++)
  2249. buf[0][0][i] = 2048;
  2250. for (row=0; row < height; row+=4) {
  2251. FORC3 mul[c] = getbits(6);
  2252. FORC3 {
  2253. val = ((0x1000000/last[c] + 0x7ff) >> 12) * mul[c];
  2254. s = val > 65564 ? 10:12;
  2255. x = ~(-1 << (s-1));
  2256. val <<= 12-s;
  2257. for (i=0; i < sizeof(buf[0])/sizeof(short); i++)
  2258. buf[c][0][i] = (buf[c][0][i] * val + x) >> s;
  2259. last[c] = mul[c];
  2260. for (r=0; r <= !c; r++) {
  2261. buf[c][1][width/2] = buf[c][2][width/2] = mul[c] << 7;
  2262. for (tree=1, col=width/2; col > 0; ) {
  2263. if ((tree = radc_token(tree))) {
  2264. col -= 2;
  2265. if (tree == 8)
  2266. FORYX buf[c][y][x] = (uchar) radc_token(18) * mul[c];
  2267. else
  2268. FORYX buf[c][y][x] = radc_token(tree+10) * 16 + PREDICTOR;
  2269. } else
  2270. do {
  2271. nreps = (col > 2) ? radc_token(9) + 1 : 1;
  2272. for (rep=0; rep < 8 && rep < nreps && col > 0; rep++) {
  2273. col -= 2;
  2274. FORYX buf[c][y][x] = PREDICTOR;
  2275. if (rep & 1) {
  2276. step = radc_token(10) << 4;
  2277. FORYX buf[c][y][x] += step;
  2278. }
  2279. }
  2280. } while (nreps == 9);
  2281. }
  2282. for (y=0; y < 2; y++)
  2283. for (x=0; x < width/2; x++) {
  2284. val = (buf[c][y+1][x] << 4) / mul[c];
  2285. if (val < 0) val = 0;
  2286. if (c) CBAYER(row+y*2+c-1,x*2+2-c) = val;
  2287. else CBAYER(row+r*2+y,x*2+y) = val;
  2288. }
  2289. memcpy (buf[c][0]+!c, buf[c][2], sizeof buf[c][0]-2*!c);
  2290. }
  2291. }
  2292. for (y=row; y < row+4; y++)
  2293. for (x=0; x < width; x++)
  2294. if ((x+y) & 1) {
  2295. r = x ? x-1 : x+1;
  2296. s = x+1 < width ? x+1 : x-1;
  2297. val = (CBAYER(y,x)-2048)*2 + (CBAYER(y,r)+CBAYER(y,s))/2;
  2298. if (val < 0) val = 0;
  2299. CBAYER(y,x) = val;
  2300. }
  2301. }
  2302. #ifndef LIBRAW_LIBRARY_BUILD
  2303. for (i=0; i < iheight*iwidth*4; i++)
  2304. image[0][i] = curve[image[0][i]];
  2305. #else
  2306. for (i=0; i < height*width*4; i++)
  2307. color_image[0][i] = curve[color_image[0][i]];
  2308. #endif
  2309. maximum = 0x3fff;
  2310. }
  2311. #undef FORYX
  2312. #undef PREDICTOR
  2313. #ifdef NO_JPEG
  2314. void CLASS kodak_jpeg_load_raw() {}
  2315. #else
  2316. METHODDEF(boolean)
  2317. fill_input_buffer (j_decompress_ptr cinfo)
  2318. {
  2319. #ifndef LIBRAW_NOTHREADS
  2320. #define jpeg_buffer tls->jpeg_buffer
  2321. #else
  2322. static uchar jpeg_buffer[4096];
  2323. #endif
  2324. size_t nbytes;
  2325. nbytes = fread (jpeg_buffer, 1, 4096, ifp);
  2326. swab (jpeg_buffer, jpeg_buffer, nbytes);
  2327. cinfo->src->next_input_byte = jpeg_buffer;
  2328. cinfo->src->bytes_in_buffer = nbytes;
  2329. return TRUE;
  2330. #ifndef LIBRAW_NOTHREADS
  2331. #undef jpeg_buffer
  2332. #endif
  2333. }
  2334. void CLASS kodak_jpeg_load_raw()
  2335. {
  2336. struct jpeg_decompress_struct cinfo;
  2337. struct jpeg_error_mgr jerr;
  2338. JSAMPARRAY buf;
  2339. JSAMPLE (*pixel)[3];
  2340. int row, col;
  2341. cinfo.err = jpeg_std_error (&jerr);
  2342. jpeg_create_decompress (&cinfo);
  2343. jpeg_stdio_src (&cinfo, ifp);
  2344. cinfo.src->fill_input_buffer = fill_input_buffer;
  2345. jpeg_read_header (&cinfo, TRUE);
  2346. jpeg_start_decompress (&cinfo);
  2347. if ((cinfo.output_width != width ) ||
  2348. (cinfo.output_height*2 != height ) ||
  2349. (cinfo.output_components != 3 )) {
  2350. #ifdef DCRAW_VERBOSE
  2351. fprintf (stderr,_("%s: incorrect JPEG dimensions\n"), ifname);
  2352. #endif
  2353. jpeg_destroy_decompress (&cinfo);
  2354. #ifdef LIBRAW_LIBRARY_BUILD
  2355. throw LIBRAW_EXCEPTION_DECODE_JPEG;
  2356. #else
  2357. longjmp (failure, 3);
  2358. #endif
  2359. }
  2360. buf = (*cinfo.mem->alloc_sarray)
  2361. ((j_common_ptr) &cinfo, JPOOL_IMAGE, width*3, 1);
  2362. while (cinfo.output_scanline < cinfo.output_height) {
  2363. row = cinfo.output_scanline * 2;
  2364. jpeg_read_scanlines (&cinfo, buf, 1);
  2365. pixel = (JSAMPLE (*)[3]) buf[0];
  2366. for (col=0; col < width; col+=2) {
  2367. RBAYER(row+0,col+0) = pixel[col+0][1] << 1;
  2368. RBAYER(row+1,col+1) = pixel[col+1][1] << 1;
  2369. RBAYER(row+0,col+1) = pixel[col][0] + pixel[col+1][0];
  2370. RBAYER(row+1,col+0) = pixel[col][2] + pixel[col+1][2];
  2371. }
  2372. }
  2373. jpeg_finish_decompress (&cinfo);
  2374. jpeg_destroy_decompress (&cinfo);
  2375. maximum = 0xff << 1;
  2376. }
  2377. #endif
  2378. void CLASS kodak_dc120_load_raw()
  2379. {
  2380. static const int mul[4] = { 162, 192, 187, 92 };
  2381. static const int add[4] = { 0, 636, 424, 212 };
  2382. uchar pixel[848];
  2383. int row, shift, col;
  2384. for (row=0; row < height; row++) {
  2385. if (fread (pixel, 1, 848, ifp) < 848) derror();
  2386. shift = row * mul[row & 3] + add[row & 3];
  2387. for (col=0; col < width; col++)
  2388. RBAYER(row,col) = (ushort) pixel[(col + shift) % 848];
  2389. }
  2390. maximum = 0xff;
  2391. }
  2392. void CLASS eight_bit_load_raw()
  2393. {
  2394. uchar *pixel;
  2395. unsigned row, col, val, lblack=0;
  2396. pixel = (uchar *) calloc (raw_width, sizeof *pixel);
  2397. merror (pixel, "eight_bit_load_raw()");
  2398. #ifndef LIBRAW_LIBRARY_BUILD
  2399. fseek (ifp, top_margin*raw_width, SEEK_CUR);
  2400. for (row=0; row < height; row++) {
  2401. if (fread (pixel, 1, raw_width, ifp) < raw_width) derror();
  2402. for (col=0; col < raw_width; col++) {
  2403. val = curve[pixel[col]];
  2404. if ((unsigned) (col-left_margin) < width)
  2405. BAYER(row,col-left_margin) = val;
  2406. else lblack += val;
  2407. }
  2408. }
  2409. #else
  2410. for (row=0; row < raw_height; row++) {
  2411. if (fread (pixel, 1, raw_width, ifp) < raw_width) derror();
  2412. for (col=0; col < raw_width; col++) {
  2413. val = curve[pixel[col]];
  2414. RBAYER(row,col) = val;
  2415. if((unsigned) (row-top_margin)< height)
  2416. if ((unsigned) (col-left_margin) >= width)
  2417. lblack+=val;
  2418. }
  2419. }
  2420. #endif
  2421. free (pixel);
  2422. if (raw_width > width+1)
  2423. black = lblack / ((raw_width - width) * height);
  2424. if (!strncmp(model,"DC2",3))
  2425. black = 0;
  2426. maximum = curve[0xff];
  2427. }
  2428. void CLASS kodak_yrgb_load_raw()
  2429. {
  2430. uchar *pixel;
  2431. int row, col, y, cb, cr, rgb[3], c;
  2432. pixel = (uchar *) calloc (raw_width, 3*sizeof *pixel);
  2433. merror (pixel, "kodak_yrgb_load_raw()");
  2434. for (row=0; row < height; row++) {
  2435. if (~row & 1)
  2436. if (fread (pixel, raw_width, 3, ifp) < 3) derror();
  2437. for (col=0; col < raw_width; col++) {
  2438. y = pixel[width*2*(row & 1) + col];
  2439. cb = pixel[width + (col & -2)] - 128;
  2440. cr = pixel[width + (col & -2)+1] - 128;
  2441. rgb[1] = y-((cb + cr + 2) >> 2);
  2442. rgb[2] = rgb[1] + cb;
  2443. rgb[0] = rgb[1] + cr;
  2444. FORC3{
  2445. #ifndef LIBRAW_LIBRARY_BUILD
  2446. image[row*width+col][c] = curve[LIM(rgb[c],0,255)];
  2447. #else
  2448. color_image[(row+top_margin)*raw_width+col+left_margin][c] = curve[LIM(rgb[c],0,255)];
  2449. #endif
  2450. }
  2451. }
  2452. }
  2453. free (pixel);
  2454. maximum = curve[0xff];
  2455. }
  2456. void CLASS kodak_262_load_raw()
  2457. {
  2458. static const uchar kodak_tree[2][26] =
  2459. { { 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 },
  2460. { 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 } };
  2461. ushort *huff[2];
  2462. uchar *pixel;
  2463. int *strip, ns, c, row, col, chess, pi=0, pi1, pi2, pred, val;
  2464. FORC(2) huff[c] = make_decoder (kodak_tree[c]);
  2465. ns = (raw_height+63) >> 5;
  2466. pixel = (uchar *) malloc (raw_width*32 + ns*4);
  2467. merror (pixel, "kodak_262_load_raw()");
  2468. strip = (int *) (pixel + raw_width*32);
  2469. order = 0x4d4d;
  2470. FORC(ns) strip[c] = get4();
  2471. for (row=0; row < raw_height; row++) {
  2472. if ((row & 31) == 0) {
  2473. fseek (ifp, strip[row >> 5], SEEK_SET);
  2474. getbits(-1);
  2475. pi = 0;
  2476. }
  2477. for (col=0; col < raw_width; col++) {
  2478. chess = (row + col) & 1;
  2479. pi1 = chess ? pi-2 : pi-raw_width-1;
  2480. pi2 = chess ? pi-2*raw_width : pi-raw_width+1;
  2481. if (col <= chess) pi1 = -1;
  2482. if (pi1 < 0) pi1 = pi2;
  2483. if (pi2 < 0) pi2 = pi1;
  2484. if (pi1 < 0 && col > 1) pi1 = pi2 = pi-2;
  2485. pred = (pi1 < 0) ? 0 : (pixel[pi1] + pixel[pi2]) >> 1;
  2486. pixel[pi] = val = pred + ljpeg_diff (huff[chess]);
  2487. if (val >> 8) derror();
  2488. val = curve[pixel[pi++]];
  2489. #ifdef LIBRAW_LIBRARY_BUILD
  2490. RBAYER(row,col) = val;
  2491. if ((unsigned) (col-left_margin) >= width)
  2492. black+=val;
  2493. #else
  2494. if ((unsigned) (col-left_margin) < width)
  2495. BAYER(row,col-left_margin) = val;
  2496. else black += val;
  2497. #endif
  2498. }
  2499. }
  2500. free (pixel);
  2501. FORC(2) free (huff[c]);
  2502. if (raw_width > width)
  2503. black /= (raw_width - width) * height;
  2504. }
  2505. int CLASS kodak_65000_decode (short *out, int bsize)
  2506. {
  2507. uchar c, blen[768];
  2508. ushort raw[6];
  2509. INT64 bitbuf=0;
  2510. int save, bits=0, i, j, len, diff;
  2511. save = ftell(ifp);
  2512. bsize = (bsize + 3) & -4;
  2513. for (i=0; i < bsize; i+=2) {
  2514. c = fgetc(ifp);
  2515. if ((blen[i ] = c & 15) > 12 ||
  2516. (blen[i+1] = c >> 4) > 12 ) {
  2517. fseek (ifp, save, SEEK_SET);
  2518. for (i=0; i < bsize; i+=8) {
  2519. read_shorts (raw, 6);
  2520. out[i ] = raw[0] >> 12 << 8 | raw[2] >> 12 << 4 | raw[4] >> 12;
  2521. out[i+1] = raw[1] >> 12 << 8 | raw[3] >> 12 << 4 | raw[5] >> 12;
  2522. for (j=0; j < 6; j++)
  2523. out[i+2+j] = raw[j] & 0xfff;
  2524. }
  2525. return 1;
  2526. }
  2527. }
  2528. if ((bsize & 7) == 4) {
  2529. bitbuf = fgetc(ifp) << 8;
  2530. bitbuf += fgetc(ifp);
  2531. bits = 16;
  2532. }
  2533. for (i=0; i < bsize; i++) {
  2534. len = blen[i];
  2535. if (bits < len) {
  2536. for (j=0; j < 32; j+=8)
  2537. bitbuf += (INT64) fgetc(ifp) << (bits+(j^8));
  2538. bits += 32;
  2539. }
  2540. diff = bitbuf & (0xffff >> (16-len));
  2541. bitbuf >>= len;
  2542. bits -= len;
  2543. if ((diff & (1 << (len-1))) == 0)
  2544. diff -= (1 << len) - 1;
  2545. out[i] = diff;
  2546. }
  2547. return 0;
  2548. }
  2549. void CLASS kodak_65000_load_raw()
  2550. {
  2551. short buf[256];
  2552. int row, col, len, pred[2], ret, i;
  2553. for (row=0; row < height; row++)
  2554. for (col=0; col < width; col+=256) {
  2555. pred[0] = pred[1] = 0;
  2556. len = MIN (256, width-col);
  2557. ret = kodak_65000_decode (buf, len);
  2558. for (i=0; i < len; i++)
  2559. #ifndef LIBRAW_LIBRARY_BUILD
  2560. if ((BAYER(row,col+i) = curve[ret ? buf[i] :
  2561. (pred[i & 1] += buf[i])]) >> 12) derror();
  2562. #else
  2563. {
  2564. ushort val = ret ? buf[i] : (pred[i & 1] += buf[i]);
  2565. val = curve[val];
  2566. RBAYER(row,col+i) = val;
  2567. if(curve[val]>>12) derror();
  2568. }
  2569. #endif
  2570. }
  2571. }
  2572. void CLASS kodak_ycbcr_load_raw()
  2573. {
  2574. short buf[384], *bp;
  2575. int row, col, len, c, i, j, k, y[2][2], cb, cr, rgb[3];
  2576. ushort *ip;
  2577. for (row=0; row < height; row+=2)
  2578. for (col=0; col < width; col+=128) {
  2579. len = MIN (128, width-col);
  2580. kodak_65000_decode (buf, len*3);
  2581. y[0][1] = y[1][1] = cb = cr = 0;
  2582. for (bp=buf, i=0; i < len; i+=2, bp+=2) {
  2583. cb += bp[4];
  2584. cr += bp[5];
  2585. rgb[1] = -((cb + cr + 2) >> 2);
  2586. rgb[2] = rgb[1] + cb;
  2587. rgb[0] = rgb[1] + cr;
  2588. for (j=0; j < 2; j++)
  2589. for (k=0; k < 2; k++) {
  2590. if ((y[j][k] = y[j][k^1] + *bp++) >> 10) derror();
  2591. #ifndef LIBRAW_LIBRARY_BUILD
  2592. ip = image[(row+j)*width + col+i+k];
  2593. FORC3 ip[c] = curve[LIM(y[j][k]+rgb[c], 0, 0xfff)];
  2594. #else
  2595. ip = color_image[(row+top_margin+j)*raw_width + col+i+k+left_margin];
  2596. FORC3 ip[c] = curve[LIM(y[j][k]+rgb[c], 0, 0xfff)];
  2597. #endif
  2598. }
  2599. }
  2600. }
  2601. }
  2602. void CLASS kodak_rgb_load_raw()
  2603. {
  2604. short buf[768], *bp;
  2605. int row, col, len, c, i, rgb[3];
  2606. #ifndef LIBRAW_LIBRARY_BUILD
  2607. ushort *ip=image[0];
  2608. #else
  2609. ushort *ip;
  2610. #endif
  2611. for (row=0; row < height; row++)
  2612. for (col=0; col < width; col+=256) {
  2613. len = MIN (256, width-col);
  2614. kodak_65000_decode (buf, len*3);
  2615. memset (rgb, 0, sizeof rgb);
  2616. #ifdef LIBRAW_LIBRARY_BUILD
  2617. ip = &color_image[(row+top_margin)*raw_width+left_margin][0];
  2618. #endif
  2619. for (bp=buf, i=0; i < len; i++, ip+=4)
  2620. FORC3{
  2621. if ((ip[c] = rgb[c] += *bp++) >> 12) derror();
  2622. }
  2623. }
  2624. }
  2625. void CLASS kodak_ycbcr_load_thumb()
  2626. {
  2627. short buf[384], *bp;
  2628. int row, col, len, c, i, j, k, y[2][2], cb, cr, rgb[3];
  2629. ushort *ip;
  2630. for (row=0; row < height; row+=2)
  2631. for (col=0; col < width; col+=128) {
  2632. len = MIN (128, width-col);
  2633. kodak_65000_decode (buf, len*3);
  2634. y[0][1] = y[1][1] = cb = cr = 0;
  2635. for (bp=buf, i=0; i < len; i+=2, bp+=2) {
  2636. cb += bp[4];
  2637. cr += bp[5];
  2638. rgb[1] = -((cb + cr + 2) >> 2);
  2639. rgb[2] = rgb[1] + cb;
  2640. rgb[0] = rgb[1] + cr;
  2641. for (j=0; j < 2; j++)
  2642. for (k=0; k < 2; k++) {
  2643. if ((y[j][k] = y[j][k^1] + *bp++) >> 10) derror();
  2644. ip = image[(row+j)*width + col+i+k];
  2645. FORC3 ip[c] = curve[LIM(y[j][k]+rgb[c], 0, 0xfff)];
  2646. }
  2647. }
  2648. }
  2649. }
  2650. void CLASS kodak_rgb_load_thumb()
  2651. {
  2652. short buf[768], *bp;
  2653. int row, col, len, c, i, rgb[3];
  2654. ushort *ip=image[0];
  2655. for (row=0; row < height; row++)
  2656. for (col=0; col < width; col+=256) {
  2657. len = MIN (256, width-col);
  2658. kodak_65000_decode (buf, len*3);
  2659. memset (rgb, 0, sizeof rgb);
  2660. for (bp=buf, i=0; i < len; i++, ip+=4)
  2661. FORC3 if ((ip[c] = rgb[c] += *bp++) >> 12) derror();
  2662. }
  2663. }
  2664. void CLASS kodak_thumb_load_raw()
  2665. {
  2666. int row, col;
  2667. colors = thumb_misc >> 5;
  2668. for (row=0; row < height; row++)
  2669. for (col=0; col < width; col++)
  2670. read_shorts (image[row*width+col], colors);
  2671. maximum = (1 << (thumb_misc & 31)) - 1;
  2672. }
  2673. void CLASS sony_decrypt (unsigned *data, int len, int start, int key)
  2674. {
  2675. #ifndef LIBRAW_NOTHREADS
  2676. #define pad tls->sony_decrypt.pad
  2677. #define p tls->sony_decrypt.p
  2678. #else
  2679. static unsigned pad[128], p;
  2680. #endif
  2681. if (start) {
  2682. for (p=0; p < 4; p++)
  2683. pad[p] = key = key * 48828125 + 1;
  2684. pad[3] = pad[3] << 1 | (pad[0]^pad[2]) >> 31;
  2685. for (p=4; p < 127; p++)
  2686. pad[p] = (pad[p-4]^pad[p-2]) << 1 | (pad[p-3]^pad[p-1]) >> 31;
  2687. for (p=0; p < 127; p++)
  2688. pad[p] = htonl(pad[p]);
  2689. }
  2690. while (len--)
  2691. *data++ ^= pad[p++ & 127] = pad[(p+1) & 127] ^ pad[(p+65) & 127];
  2692. #ifndef LIBRAW_NOTHREADS
  2693. #undef pad
  2694. #undef p
  2695. #endif
  2696. }
  2697. void CLASS sony_load_raw()
  2698. {
  2699. uchar head[40];
  2700. ushort *pixel;
  2701. unsigned i, key, row, col;
  2702. fseek (ifp, 200896, SEEK_SET);
  2703. fseek (ifp, (unsigned) fgetc(ifp)*4 - 1, SEEK_CUR);
  2704. order = 0x4d4d;
  2705. key = get4();
  2706. fseek (ifp, 164600, SEEK_SET);
  2707. fread (head, 1, 40, ifp);
  2708. sony_decrypt ((unsigned int *) head, 10, 1, key);
  2709. for (i=26; i-- > 22; )
  2710. key = key << 8 | head[i];
  2711. fseek (ifp, data_offset, SEEK_SET);
  2712. pixel = (ushort *) calloc (raw_width, sizeof *pixel);
  2713. merror (pixel, "sony_load_raw()");
  2714. for (row=0; row < height; row++) {
  2715. if (fread (pixel, 2, raw_width, ifp) < raw_width) derror();
  2716. sony_decrypt ((unsigned int *) pixel, raw_width/2, !row, key);
  2717. for (col=9; col < left_margin; col++)
  2718. black += ntohs(pixel[col]);
  2719. #ifndef LIBRAW_LIBRARY_BUILD
  2720. for (col=0; col < width; col++)
  2721. if ((BAYER(row,col) = ntohs(pixel[col+left_margin])) >> 14)
  2722. derror();
  2723. #else
  2724. for (col=0; col < raw_width; col++)
  2725. {
  2726. RBAYER(row,col) = ntohs(pixel[col]);
  2727. if(col >= left_margin && col < width+left_margin
  2728. && (RBAYER(row,col)>>14))
  2729. derror();
  2730. }
  2731. #endif
  2732. }
  2733. free (pixel);
  2734. if (left_margin > 9)
  2735. black /= (left_margin-9) * height;
  2736. maximum = 0x3ff0;
  2737. }
  2738. void CLASS sony_arw_load_raw()
  2739. {
  2740. ushort huff[32768];
  2741. static const ushort tab[18] =
  2742. { 0xf11,0xf10,0xe0f,0xd0e,0xc0d,0xb0c,0xa0b,0x90a,0x809,
  2743. 0x708,0x607,0x506,0x405,0x304,0x303,0x300,0x202,0x201 };
  2744. int i, c, n, col, row, len, diff, sum=0;
  2745. for (n=i=0; i < 18; i++)
  2746. FORC(32768 >> (tab[i] >> 8)) huff[n++] = tab[i];
  2747. #ifdef LIBRAW_LIBRARY_BUILD
  2748. LibRaw_byte_buffer *buf=NULL;
  2749. if(data_size)
  2750. buf = ifp->make_byte_buffer(data_size);
  2751. else
  2752. getbits(-1);
  2753. LibRaw_bit_buffer bits;
  2754. bits.reset();
  2755. #else
  2756. getbits(-1);
  2757. #endif
  2758. for (col = raw_width; col--; )
  2759. for (row=0; row < raw_height+1; row+=2) {
  2760. if (row == raw_height) row = 1;
  2761. #ifdef LIBRAW_LIBRARY_BUILD
  2762. if(data_size)
  2763. {
  2764. len = bits._gethuff(buf,15,huff,zero_after_ff);
  2765. diff = bits._getbits(buf,len,zero_after_ff);
  2766. }
  2767. else
  2768. {
  2769. len = getbithuff(15,huff);
  2770. diff = getbits(len);
  2771. }
  2772. #else
  2773. len = getbithuff(15,huff);
  2774. diff = getbits(len);
  2775. #endif
  2776. if ((diff & (1 << (len-1))) == 0)
  2777. diff -= (1 << len) - 1;
  2778. if ((sum += diff) >> 12) derror();
  2779. #ifndef LIBRAW_LIBRARY_BUILD
  2780. if (row < height)
  2781. {
  2782. BAYER(row,col) = sum;
  2783. }
  2784. #else
  2785. RBAYER(row,col) = sum;
  2786. #endif
  2787. }
  2788. #ifdef LIBRAW_LIBRARY_BUILD
  2789. if(buf) delete buf;
  2790. #endif
  2791. }
  2792. void CLASS sony_arw2_load_raw()
  2793. {
  2794. uchar *data, *dp;
  2795. ushort pix[16];
  2796. int row, col, val, max, min, imax, imin, sh, bit, i;
  2797. data = (uchar *) malloc (raw_width);
  2798. merror (data, "sony_arw2_load_raw()");
  2799. for (row=0; row < height; row++) {
  2800. fread (data, 1, raw_width, ifp);
  2801. for (dp=data, col=0; col < raw_width-30; dp+=16) {
  2802. max = 0x7ff & (val = sget4(dp));
  2803. min = 0x7ff & val >> 11;
  2804. imax = 0x0f & val >> 22;
  2805. imin = 0x0f & val >> 26;
  2806. for (sh=0; sh < 4 && 0x80 << sh <= max-min; sh++);
  2807. for (bit=30, i=0; i < 16; i++)
  2808. if (i == imax) pix[i] = max;
  2809. else if (i == imin) pix[i] = min;
  2810. else {
  2811. pix[i] = ((sget2(dp+(bit >> 3)) >> (bit & 7) & 0x7f) << sh) + min;
  2812. if (pix[i] > 0x7ff) pix[i] = 0x7ff;
  2813. bit += 7;
  2814. }
  2815. for (i=0; i < 16; i++, col+=2)
  2816. #ifdef LIBRAW_LIBRARY_BUILD
  2817. RBAYER(row,col) = curve[pix[i] << 1] >> 2;
  2818. #else
  2819. if (col < width) BAYER(row,col) = curve[pix[i] << 1] >> 2;
  2820. #endif
  2821. col -= col & 1 ? 1:31;
  2822. }
  2823. }
  2824. free (data);
  2825. }
  2826. #define HOLE(row) ((holes >> (((row) - raw_height) & 7)) & 1)
  2827. /* Kudos to Rich Taylor for figuring out SMaL's compression algorithm. */
  2828. void CLASS smal_decode_segment (unsigned seg[2][2], int holes)
  2829. {
  2830. uchar hist[3][13] = {
  2831. { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
  2832. { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
  2833. { 3, 3, 0, 0, 63, 47, 31, 15, 0 } };
  2834. int low, high=0xff, carry=0, nbits=8;
  2835. int s, count, bin, next, i, sym[3];
  2836. uchar diff, pred[]={0,0};
  2837. ushort data=0, range=0;
  2838. unsigned pix, row, col;
  2839. fseek (ifp, seg[0][1]+1, SEEK_SET);
  2840. getbits(-1);
  2841. for (pix=seg[0][0]; pix < seg[1][0]; pix++) {
  2842. for (s=0; s < 3; s++) {
  2843. data = data << nbits | getbits(nbits);
  2844. if (carry < 0)
  2845. carry = (nbits += carry+1) < 1 ? nbits-1 : 0;
  2846. while (--nbits >= 0)
  2847. if ((data >> nbits & 0xff) == 0xff) break;
  2848. if (nbits > 0)
  2849. data = ((data & ((1 << (nbits-1)) - 1)) << 1) |
  2850. ((data + (((data & (1 << (nbits-1)))) << 1)) & (-1 << nbits));
  2851. if (nbits >= 0) {
  2852. data += getbits(1);
  2853. carry = nbits - 8;
  2854. }
  2855. count = ((((data-range+1) & 0xffff) << 2) - 1) / (high >> 4);
  2856. for (bin=0; hist[s][bin+5] > count; bin++);
  2857. low = hist[s][bin+5] * (high >> 4) >> 2;
  2858. if (bin) high = hist[s][bin+4] * (high >> 4) >> 2;
  2859. high -= low;
  2860. for (nbits=0; high << nbits < 128; nbits++);
  2861. range = (range+low) << nbits;
  2862. high <<= nbits;
  2863. next = hist[s][1];
  2864. if (++hist[s][2] > hist[s][3]) {
  2865. next = (next+1) & hist[s][0];
  2866. hist[s][3] = (hist[s][next+4] - hist[s][next+5]) >> 2;
  2867. hist[s][2] = 1;
  2868. }
  2869. if (hist[s][hist[s][1]+4] - hist[s][hist[s][1]+5] > 1) {
  2870. if (bin < hist[s][1])
  2871. for (i=bin; i < hist[s][1]; i++) hist[s][i+5]--;
  2872. else if (next <= bin)
  2873. for (i=hist[s][1]; i < bin; i++) hist[s][i+5]++;
  2874. }
  2875. hist[s][1] = next;
  2876. sym[s] = bin;
  2877. }
  2878. diff = sym[2] << 5 | sym[1] << 2 | (sym[0] & 3);
  2879. if (sym[0] & 4)
  2880. diff = diff ? -diff : 0x80;
  2881. if (ftell(ifp) + 12 >= seg[1][1])
  2882. diff = 0;
  2883. pred[pix & 1] += diff;
  2884. #ifndef LIBRAW_LIBRARY_BUILD
  2885. row = pix / raw_width - top_margin;
  2886. col = pix % raw_width - left_margin;
  2887. if (row < height && col < width)
  2888. BAYER(row,col) = pred[pix & 1];
  2889. #else
  2890. row = pix / raw_width - top_margin;
  2891. RBAYER(pix / raw_width, pix % raw_width) = pred[pix & 1];
  2892. #endif
  2893. if (!(pix & 1) && HOLE(row)) pix += 2;
  2894. }
  2895. maximum = 0xff;
  2896. }
  2897. void CLASS smal_v6_load_raw()
  2898. {
  2899. unsigned seg[2][2];
  2900. fseek (ifp, 16, SEEK_SET);
  2901. seg[0][0] = 0;
  2902. seg[0][1] = get2();
  2903. seg[1][0] = raw_width * raw_height;
  2904. seg[1][1] = INT_MAX;
  2905. smal_decode_segment (seg, 0);
  2906. }
  2907. int CLASS median4 (int *p)
  2908. {
  2909. int min, max, sum, i;
  2910. min = max = sum = p[0];
  2911. for (i=1; i < 4; i++) {
  2912. sum += p[i];
  2913. if (min > p[i]) min = p[i];
  2914. if (max < p[i]) max = p[i];
  2915. }
  2916. return (sum - min - max) >> 1;
  2917. }
  2918. void CLASS fill_holes (int holes)
  2919. {
  2920. int row, col, val[4];
  2921. for (row=2; row < height-2; row++) {
  2922. if (!HOLE(row)) continue;
  2923. for (col=1; col < width-1; col+=4) {
  2924. val[0] = BAYER(row-1,col-1);
  2925. val[1] = BAYER(row-1,col+1);
  2926. val[2] = BAYER(row+1,col-1);
  2927. val[3] = BAYER(row+1,col+1);
  2928. BAYER(row,col) = median4(val);
  2929. }
  2930. for (col=2; col < width-2; col+=4)
  2931. if (HOLE(row-2) || HOLE(row+2))
  2932. BAYER(row,col) = (BAYER(row,col-2) + BAYER(row,col+2)) >> 1;
  2933. else {
  2934. val[0] = BAYER(row,col-2);
  2935. val[1] = BAYER(row,col+2);
  2936. val[2] = BAYER(row-2,col);
  2937. val[3] = BAYER(row+2,col);
  2938. BAYER(row,col) = median4(val);
  2939. }
  2940. }
  2941. }
  2942. void CLASS smal_v9_load_raw()
  2943. {
  2944. unsigned seg[256][2], offset, nseg, holes, i;
  2945. fseek (ifp, 67, SEEK_SET);
  2946. offset = get4();
  2947. nseg = fgetc(ifp);
  2948. fseek (ifp, offset, SEEK_SET);
  2949. for (i=0; i < nseg*2; i++)
  2950. seg[0][i] = get4() + data_offset*(i & 1);
  2951. fseek (ifp, 78, SEEK_SET);
  2952. holes = fgetc(ifp);
  2953. fseek (ifp, 88, SEEK_SET);
  2954. seg[nseg][0] = raw_height * raw_width;
  2955. seg[nseg][1] = get4() + data_offset;
  2956. for (i=0; i < nseg; i++)
  2957. smal_decode_segment (seg+i, holes);
  2958. if (holes) fill_holes (holes);
  2959. }
  2960. void CLASS redcine_load_raw()
  2961. {
  2962. #ifndef NO_JASPER
  2963. int c, row, col;
  2964. jas_stream_t *in;
  2965. jas_image_t *jimg;
  2966. jas_matrix_t *jmat;
  2967. jas_seqent_t *data;
  2968. ushort *img, *pix;
  2969. jas_init();
  2970. #ifndef LIBRAW_LIBRARY_BUILD
  2971. in = jas_stream_fopen (ifname, "rb");
  2972. #else
  2973. in = (jas_stream_t*)ifp->make_jas_stream();
  2974. if(!in)
  2975. throw LIBRAW_EXCEPTION_DECODE_JPEG2000;
  2976. #endif
  2977. jas_stream_seek (in, data_offset+20, SEEK_SET);
  2978. jimg = jas_image_decode (in, -1, 0);
  2979. #ifndef LIBRAW_LIBRARY_BUILD
  2980. if (!jimg) longjmp (failure, 3);
  2981. #else
  2982. if(!jimg)
  2983. {
  2984. jas_stream_close (in);
  2985. throw LIBRAW_EXCEPTION_DECODE_JPEG2000;
  2986. }
  2987. #endif
  2988. jmat = jas_matrix_create (height/2, width/2);
  2989. merror (jmat, "redcine_load_raw()");
  2990. img = (ushort *) calloc ((height+2)*(width+2), 2);
  2991. merror (img, "redcine_load_raw()");
  2992. FORC4 {
  2993. jas_image_readcmpt (jimg, c, 0, 0, width/2, height/2, jmat);
  2994. data = jas_matrix_getref (jmat, 0, 0);
  2995. for (row = c >> 1; row < height; row+=2)
  2996. for (col = c & 1; col < width; col+=2)
  2997. img[(row+1)*(width+2)+col+1] = data[(row/2)*(width/2)+col/2];
  2998. }
  2999. for (col=1; col <= width; col++) {
  3000. img[col] = img[2*(width+2)+col];
  3001. img[(height+1)*(width+2)+col] = img[(height-1)*(width+2)+col];
  3002. }
  3003. for (row=0; row < height+2; row++) {
  3004. img[row*(width+2)] = img[row*(width+2)+2];
  3005. img[(row+1)*(width+2)-1] = img[(row+1)*(width+2)-3];
  3006. }
  3007. for (row=1; row <= height; row++) {
  3008. pix = img + row*(width+2) + (col = 1 + (FC(row,1) & 1));
  3009. for ( ; col <= width; col+=2, pix+=2) {
  3010. c = (((pix[0] - 0x800) << 3) +
  3011. pix[-(width+2)] + pix[width+2] + pix[-1] + pix[1]) >> 2;
  3012. pix[0] = LIM(c,0,4095);
  3013. }
  3014. }
  3015. for (row=0; row < height; row++)
  3016. for (col=0; col < width; col++)
  3017. RBAYER(row,col) = curve[img[(row+1)*(width+2)+col+1]];
  3018. free (img);
  3019. jas_matrix_destroy (jmat);
  3020. jas_image_destroy (jimg);
  3021. jas_stream_close (in);
  3022. #endif
  3023. }
  3024. void CLASS gamma_curve (double pwr, double ts, int mode, int imax)
  3025. {
  3026. int i;
  3027. double g[6], bnd[2]={0,0}, r;
  3028. g[0] = pwr;
  3029. g[1] = ts;
  3030. g[2] = g[3] = g[4] = 0;
  3031. bnd[g[1] >= 1] = 1;
  3032. if (g[1] && (g[1]-1)*(g[0]-1) <= 0) {
  3033. for (i=0; i < 48; i++) {
  3034. g[2] = (bnd[0] + bnd[1])/2;
  3035. if (g[0]) bnd[(pow(g[2]/g[1],-g[0]) - 1)/g[0] - 1/g[2] > -1] = g[2];
  3036. else bnd[g[2]/exp(1-1/g[2]) < g[1]] = g[2];
  3037. }
  3038. g[3] = g[2] / g[1];
  3039. if (g[0]) g[4] = g[2] * (1/g[0] - 1);
  3040. }
  3041. if (g[0]) g[5] = 1 / (g[1]*SQR(g[3])/2 - g[4]*(1 - g[3]) +
  3042. (1 - pow(g[3],1+g[0]))*(1 + g[4])/(1 + g[0])) - 1;
  3043. else g[5] = 1 / (g[1]*SQR(g[3])/2 + 1
  3044. - g[2] - g[3] - g[2]*g[3]*(log(g[3]) - 1)) - 1;
  3045. if (!mode--) {
  3046. memcpy (gamm, g, sizeof gamm);
  3047. return;
  3048. }
  3049. for (i=0; i < 0x10000; i++) {
  3050. curve[i] = 0xffff;
  3051. if ((r = (double) i / imax) < 1)
  3052. curve[i] = 0x10000 * ( mode
  3053. ? (r < g[3] ? r*g[1] : (g[0] ? pow( r,g[0])*(1+g[4])-g[4] : log(r)*g[2]+1))
  3054. : (r < g[2] ? r/g[1] : (g[0] ? pow((r+g[4])/(1+g[4]),1/g[0]) : exp((r-1)/g[2]))));
  3055. }
  3056. }
  3057. void CLASS pseudoinverse (double (*in)[3], double (*out)[3], int size)
  3058. {
  3059. double work[3][6], num;
  3060. int i, j, k;
  3061. for (i=0; i < 3; i++) {
  3062. for (j=0; j < 6; j++)
  3063. work[i][j] = j == i+3;
  3064. for (j=0; j < 3; j++)
  3065. for (k=0; k < size; k++)
  3066. work[i][j] += in[k][i] * in[k][j];
  3067. }
  3068. for (i=0; i < 3; i++) {
  3069. num = work[i][i];
  3070. for (j=0; j < 6; j++)
  3071. work[i][j] /= num;
  3072. for (k=0; k < 3; k++) {
  3073. if (k==i) continue;
  3074. num = work[k][i];
  3075. for (j=0; j < 6; j++)
  3076. work[k][j] -= work[i][j] * num;
  3077. }
  3078. }
  3079. for (i=0; i < size; i++)
  3080. for (j=0; j < 3; j++)
  3081. for (out[i][j]=k=0; k < 3; k++)
  3082. out[i][j] += work[j][k+3] * in[i][k];
  3083. }
  3084. void CLASS cam_xyz_coeff (double cam_xyz[4][3])
  3085. {
  3086. double cam_rgb[4][3], inverse[4][3], num;
  3087. int i, j, k;
  3088. for (i=0; i < colors; i++) /* Multiply out XYZ colorspace */
  3089. for (j=0; j < 3; j++)
  3090. for (cam_rgb[i][j] = k=0; k < 3; k++)
  3091. cam_rgb[i][j] += cam_xyz[i][k] * xyz_rgb[k][j];
  3092. for (i=0; i < colors; i++) { /* Normalize cam_rgb so that */
  3093. for (num=j=0; j < 3; j++) /* cam_rgb * (1,1,1) is (1,1,1,1) */
  3094. num += cam_rgb[i][j];
  3095. for (j=0; j < 3; j++)
  3096. cam_rgb[i][j] /= num;
  3097. pre_mul[i] = 1 / num;
  3098. }
  3099. pseudoinverse (cam_rgb, inverse, colors);
  3100. for (raw_color = i=0; i < 3; i++)
  3101. for (j=0; j < colors; j++)
  3102. rgb_cam[i][j] = inverse[j][i];
  3103. #ifdef LIBRAW_LIBRARY_BUILD
  3104. color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
  3105. color_flags.rgb_cam_state = LIBRAW_COLORSTATE_CONST;
  3106. #endif
  3107. }
  3108. #ifdef COLORCHECK
  3109. void CLASS colorcheck()
  3110. {
  3111. #define NSQ 24
  3112. // Coordinates of the GretagMacbeth ColorChecker squares
  3113. // width, height, 1st_column, 1st_row
  3114. int cut[NSQ][4]; // you must set these
  3115. // ColorChecker Chart under 6500-kelvin illumination
  3116. static const double gmb_xyY[NSQ][3] = {
  3117. { 0.400, 0.350, 10.1 }, // Dark Skin
  3118. { 0.377, 0.345, 35.8 }, // Light Skin
  3119. { 0.247, 0.251, 19.3 }, // Blue Sky
  3120. { 0.337, 0.422, 13.3 }, // Foliage
  3121. { 0.265, 0.240, 24.3 }, // Blue Flower
  3122. { 0.261, 0.343, 43.1 }, // Bluish Green
  3123. { 0.506, 0.407, 30.1 }, // Orange
  3124. { 0.211, 0.175, 12.0 }, // Purplish Blue
  3125. { 0.453, 0.306, 19.8 }, // Moderate Red
  3126. { 0.285, 0.202, 6.6 }, // Purple
  3127. { 0.380, 0.489, 44.3 }, // Yellow Green
  3128. { 0.473, 0.438, 43.1 }, // Orange Yellow
  3129. { 0.187, 0.129, 6.1 }, // Blue
  3130. { 0.305, 0.478, 23.4 }, // Green
  3131. { 0.539, 0.313, 12.0 }, // Red
  3132. { 0.448, 0.470, 59.1 }, // Yellow
  3133. { 0.364, 0.233, 19.8 }, // Magenta
  3134. { 0.196, 0.252, 19.8 }, // Cyan
  3135. { 0.310, 0.316, 90.0 }, // White
  3136. { 0.310, 0.316, 59.1 }, // Neutral 8
  3137. { 0.310, 0.316, 36.2 }, // Neutral 6.5
  3138. { 0.310, 0.316, 19.8 }, // Neutral 5
  3139. { 0.310, 0.316, 9.0 }, // Neutral 3.5
  3140. { 0.310, 0.316, 3.1 } }; // Black
  3141. double gmb_cam[NSQ][4], gmb_xyz[NSQ][3];
  3142. double inverse[NSQ][3], cam_xyz[4][3], num;
  3143. int c, i, j, k, sq, row, col, count[4];
  3144. memset (gmb_cam, 0, sizeof gmb_cam);
  3145. for (sq=0; sq < NSQ; sq++) {
  3146. FORCC count[c] = 0;
  3147. for (row=cut[sq][3]; row < cut[sq][3]+cut[sq][1]; row++)
  3148. for (col=cut[sq][2]; col < cut[sq][2]+cut[sq][0]; col++) {
  3149. c = FC(row,col);
  3150. if (c >= colors) c -= 2;
  3151. gmb_cam[sq][c] += BAYER(row,col);
  3152. count[c]++;
  3153. }
  3154. FORCC gmb_cam[sq][c] = gmb_cam[sq][c]/count[c] - black;
  3155. gmb_xyz[sq][0] = gmb_xyY[sq][2] * gmb_xyY[sq][0] / gmb_xyY[sq][1];
  3156. gmb_xyz[sq][1] = gmb_xyY[sq][2];
  3157. gmb_xyz[sq][2] = gmb_xyY[sq][2] *
  3158. (1 - gmb_xyY[sq][0] - gmb_xyY[sq][1]) / gmb_xyY[sq][1];
  3159. }
  3160. pseudoinverse (gmb_xyz, inverse, NSQ);
  3161. for (i=0; i < colors; i++)
  3162. for (j=0; j < 3; j++)
  3163. for (cam_xyz[i][j] = k=0; k < NSQ; k++)
  3164. cam_xyz[i][j] += gmb_cam[k][i] * inverse[k][j];
  3165. cam_xyz_coeff (cam_xyz);
  3166. #ifdef DCRAW_VERBOSE
  3167. if (verbose) {
  3168. printf (" { \"%s %s\", %d,\n\t{", make, model, black);
  3169. num = 10000 / (cam_xyz[1][0] + cam_xyz[1][1] + cam_xyz[1][2]);
  3170. FORCC for (j=0; j < 3; j++)
  3171. printf ("%c%d", (c | j) ? ',':' ', (int) (cam_xyz[c][j] * num + 0.5));
  3172. puts (" } },");
  3173. }
  3174. #endif
  3175. #undef NSQ
  3176. }
  3177. #endif
  3178. void CLASS hat_transform (float *temp, float *base, int st, int size, int sc)
  3179. {
  3180. int i;
  3181. for (i=0; i < sc; i++)
  3182. temp[i] = 2*base[st*i] + base[st*(sc-i)] + base[st*(i+sc)];
  3183. for (; i+sc < size; i++)
  3184. temp[i] = 2*base[st*i] + base[st*(i-sc)] + base[st*(i+sc)];
  3185. for (; i < size; i++)
  3186. temp[i] = 2*base[st*i] + base[st*(i-sc)] + base[st*(2*size-2-(i+sc))];
  3187. }
  3188. #if !defined(LIBRAW_USE_OPENMP)
  3189. void CLASS wavelet_denoise()
  3190. {
  3191. float *fimg=0, *temp, thold, mul[2], avg, diff;
  3192. int scale=1, size, lev, hpass, lpass, row, col, nc, c, i, wlast, blk[2];
  3193. ushort *window[4];
  3194. static const float noise[] =
  3195. { 0.8002,0.2735,0.1202,0.0585,0.0291,0.0152,0.0080,0.0044 };
  3196. #ifdef DCRAW_VERBOSE
  3197. if (verbose) fprintf (stderr,_("Wavelet denoising...\n"));
  3198. #endif
  3199. while (maximum << scale < 0x10000) scale++;
  3200. maximum <<= --scale;
  3201. black <<= scale;
  3202. FORC4 cblack[c] <<= scale;
  3203. if ((size = iheight*iwidth) < 0x15550000)
  3204. fimg = (float *) malloc ((size*3 + iheight + iwidth) * sizeof *fimg);
  3205. merror (fimg, "wavelet_denoise()");
  3206. temp = fimg + size*3;
  3207. if ((nc = colors) == 3 && filters) nc++;
  3208. FORC(nc) { /* denoise R,G1,B,G3 individually */
  3209. for (i=0; i < size; i++)
  3210. fimg[i] = 256 * sqrt((double)(image[i][c] << scale));
  3211. for (hpass=lev=0; lev < 5; lev++) {
  3212. lpass = size*((lev & 1)+1);
  3213. for (row=0; row < iheight; row++) {
  3214. hat_transform (temp, fimg+hpass+row*iwidth, 1, iwidth, 1 << lev);
  3215. for (col=0; col < iwidth; col++)
  3216. fimg[lpass + row*iwidth + col] = temp[col] * 0.25;
  3217. }
  3218. for (col=0; col < iwidth; col++) {
  3219. hat_transform (temp, fimg+lpass+col, iwidth, iheight, 1 << lev);
  3220. for (row=0; row < iheight; row++)
  3221. fimg[lpass + row*iwidth + col] = temp[row] * 0.25;
  3222. }
  3223. thold = threshold * noise[lev];
  3224. for (i=0; i < size; i++) {
  3225. fimg[hpass+i] -= fimg[lpass+i];
  3226. if (fimg[hpass+i] < -thold) fimg[hpass+i] += thold;
  3227. else if (fimg[hpass+i] > thold) fimg[hpass+i] -= thold;
  3228. else fimg[hpass+i] = 0;
  3229. if (hpass) fimg[i] += fimg[hpass+i];
  3230. }
  3231. hpass = lpass;
  3232. }
  3233. for (i=0; i < size; i++)
  3234. image[i][c] = CLIP(SQR(fimg[i]+fimg[lpass+i])/0x10000);
  3235. }
  3236. if (filters && colors == 3) { /* pull G1 and G3 closer together */
  3237. for (row=0; row < 2; row++) {
  3238. mul[row] = 0.125 * pre_mul[FC(row+1,0) | 1] / pre_mul[FC(row,0) | 1];
  3239. blk[row] = cblack[FC(row,0) | 1];
  3240. }
  3241. for (i=0; i < 4; i++)
  3242. window[i] = (ushort *) fimg + width*i;
  3243. for (wlast=-1, row=1; row < height-1; row++) {
  3244. while (wlast < row+1) {
  3245. for (wlast++, i=0; i < 4; i++)
  3246. window[(i+3) & 3] = window[i];
  3247. for (col = FC(wlast,1) & 1; col < width; col+=2)
  3248. window[2][col] = BAYER(wlast,col);
  3249. }
  3250. thold = threshold/512;
  3251. for (col = (FC(row,0) & 1)+1; col < width-1; col+=2) {
  3252. avg = ( window[0][col-1] + window[0][col+1] +
  3253. window[2][col-1] + window[2][col+1] - blk[~row & 1]*4 )
  3254. * mul[row & 1] + (window[1][col] + blk[row & 1]) * 0.5;
  3255. avg = avg < 0 ? 0 : sqrt(avg);
  3256. diff = sqrt((double)(BAYER(row,col))) - avg;
  3257. if (diff < -thold) diff += thold;
  3258. else if (diff > thold) diff -= thold;
  3259. else diff = 0;
  3260. BAYER(row,col) = CLIP(SQR(avg+diff) + 0.5);
  3261. }
  3262. }
  3263. }
  3264. free (fimg);
  3265. }
  3266. #else /* LIBRAW_USE_OPENMP */
  3267. void CLASS wavelet_denoise()
  3268. {
  3269. float *fimg=0, *temp, thold, mul[2], avg, diff;
  3270. int scale=1, size, lev, hpass, lpass, row, col, nc, c, i, wlast, blk[2];
  3271. ushort *window[4];
  3272. static const float noise[] =
  3273. { 0.8002,0.2735,0.1202,0.0585,0.0291,0.0152,0.0080,0.0044 };
  3274. #ifdef DCRAW_VERBOSE
  3275. if (verbose) fprintf (stderr,_("Wavelet denoising...\n"));
  3276. #endif
  3277. while (maximum << scale < 0x10000) scale++;
  3278. maximum <<= --scale;
  3279. black <<= scale;
  3280. FORC4 cblack[c] <<= scale;
  3281. if ((size = iheight*iwidth) < 0x15550000)
  3282. fimg = (float *) malloc ((size*3 + iheight + iwidth) * sizeof *fimg);
  3283. merror (fimg, "wavelet_denoise()");
  3284. temp = fimg + size*3;
  3285. if ((nc = colors) == 3 && filters) nc++;
  3286. #ifdef LIBRAW_LIBRARY_BUILD
  3287. #pragma omp parallel default(shared) private(i,col,row,thold,lev,lpass,hpass,temp,c) firstprivate(scale,size)
  3288. #endif
  3289. {
  3290. temp = (float*)malloc( (iheight + iwidth) * sizeof *fimg);
  3291. FORC(nc) { /* denoise R,G1,B,G3 individually */
  3292. #ifdef LIBRAW_LIBRARY_BUILD
  3293. #pragma omp for
  3294. #endif
  3295. for (i=0; i < size; i++)
  3296. fimg[i] = 256 * sqrt((double)(image[i][c] << scale));
  3297. for (hpass=lev=0; lev < 5; lev++) {
  3298. lpass = size*((lev & 1)+1);
  3299. #ifdef LIBRAW_LIBRARY_BUILD
  3300. #pragma omp for
  3301. #endif
  3302. for (row=0; row < iheight; row++) {
  3303. hat_transform (temp, fimg+hpass+row*iwidth, 1, iwidth, 1 << lev);
  3304. for (col=0; col < iwidth; col++)
  3305. fimg[lpass + row*iwidth + col] = temp[col] * 0.25;
  3306. }
  3307. #ifdef LIBRAW_LIBRARY_BUILD
  3308. #pragma omp for
  3309. #endif
  3310. for (col=0; col < iwidth; col++) {
  3311. hat_transform (temp, fimg+lpass+col, iwidth, iheight, 1 << lev);
  3312. for (row=0; row < iheight; row++)
  3313. fimg[lpass + row*iwidth + col] = temp[row] * 0.25;
  3314. }
  3315. thold = threshold * noise[lev];
  3316. #ifdef LIBRAW_LIBRARY_BUILD
  3317. #pragma omp for
  3318. #endif
  3319. for (i=0; i < size; i++) {
  3320. fimg[hpass+i] -= fimg[lpass+i];
  3321. if (fimg[hpass+i] < -thold) fimg[hpass+i] += thold;
  3322. else if (fimg[hpass+i] > thold) fimg[hpass+i] -= thold;
  3323. else fimg[hpass+i] = 0;
  3324. if (hpass) fimg[i] += fimg[hpass+i];
  3325. }
  3326. hpass = lpass;
  3327. }
  3328. #ifdef LIBRAW_LIBRARY_BUILD
  3329. #pragma omp for
  3330. #endif
  3331. for (i=0; i < size; i++)
  3332. image[i][c] = CLIP(SQR(fimg[i]+fimg[lpass+i])/0x10000);
  3333. }
  3334. free(temp);
  3335. } /* end omp parallel */
  3336. /* the following loops are hard to parallize, no idea yes,
  3337. * problem is wlast which is carrying dependency
  3338. * second part should be easyer, but did not yet get it right.
  3339. */
  3340. if (filters && colors == 3) { /* pull G1 and G3 closer together */
  3341. for (row=0; row < 2; row++){
  3342. mul[row] = 0.125 * pre_mul[FC(row+1,0) | 1] / pre_mul[FC(row,0) | 1];
  3343. blk[row] = cblack[FC(row,0) | 1];
  3344. }
  3345. for (i=0; i < 4; i++)
  3346. window[i] = (ushort *) fimg + width*i;
  3347. for (wlast=-1, row=1; row < height-1; row++) {
  3348. while (wlast < row+1) {
  3349. for (wlast++, i=0; i < 4; i++)
  3350. window[(i+3) & 3] = window[i];
  3351. for (col = FC(wlast,1) & 1; col < width; col+=2)
  3352. window[2][col] = BAYER(wlast,col);
  3353. }
  3354. thold = threshold/512;
  3355. for (col = (FC(row,0) & 1)+1; col < width-1; col+=2) {
  3356. avg = ( window[0][col-1] + window[0][col+1] +
  3357. window[2][col-1] + window[2][col+1] - blk[~row & 1]*4 )
  3358. * mul[row & 1] + (window[1][col] + blk[row & 1]) * 0.5;
  3359. avg = avg < 0 ? 0 : sqrt(avg);
  3360. diff = sqrt((double)BAYER(row,col)) - avg;
  3361. if (diff < -thold) diff += thold;
  3362. else if (diff > thold) diff -= thold;
  3363. else diff = 0;
  3364. BAYER(row,col) = CLIP(SQR(avg+diff) + 0.5);
  3365. }
  3366. }
  3367. }
  3368. free (fimg);
  3369. }
  3370. #endif
  3371. // green equilibration
  3372. void CLASS green_matching()
  3373. {
  3374. int i,j;
  3375. double m1,m2,c1,c2;
  3376. int o1_1,o1_2,o1_3,o1_4;
  3377. int o2_1,o2_2,o2_3,o2_4;
  3378. ushort (*img)[4];
  3379. const int margin = 3;
  3380. int oj = 2, oi = 2;
  3381. float f;
  3382. const float thr = 0.01f;
  3383. if(half_size || shrink) return;
  3384. if(FC(oj, oi) != 3) oj++;
  3385. if(FC(oj, oi) != 3) oi++;
  3386. if(FC(oj, oi) != 3) oj--;
  3387. img = (ushort (*)[4]) calloc (height*width, sizeof *image);
  3388. merror (img, "green_matching()");
  3389. memcpy(img,image,height*width*sizeof *image);
  3390. for(j=oj;j<height-margin;j+=2)
  3391. for(i=oi;i<width-margin;i+=2){
  3392. o1_1=img[(j-1)*width+i-1][1];
  3393. o1_2=img[(j-1)*width+i+1][1];
  3394. o1_3=img[(j+1)*width+i-1][1];
  3395. o1_4=img[(j+1)*width+i+1][1];
  3396. o2_1=img[(j-2)*width+i][3];
  3397. o2_2=img[(j+2)*width+i][3];
  3398. o2_3=img[j*width+i-2][3];
  3399. o2_4=img[j*width+i+2][3];
  3400. m1=(o1_1+o1_2+o1_3+o1_4)/4.0;
  3401. m2=(o2_1+o2_2+o2_3+o2_4)/4.0;
  3402. c1=(abs(o1_1-o1_2)+abs(o1_1-o1_3)+abs(o1_1-o1_4)+abs(o1_2-o1_3)+abs(o1_3-o1_4)+abs(o1_2-o1_4))/6.0;
  3403. c2=(abs(o2_1-o2_2)+abs(o2_1-o2_3)+abs(o2_1-o2_4)+abs(o2_2-o2_3)+abs(o2_3-o2_4)+abs(o2_2-o2_4))/6.0;
  3404. if((img[j*width+i][3]<maximum*0.95)&&(c1<maximum*thr)&&(c2<maximum*thr))
  3405. {
  3406. f = image[j*width+i][3]*m1/m2;
  3407. image[j*width+i][3]=f>0xffff?0xffff:f;
  3408. }
  3409. }
  3410. free(img);
  3411. }
  3412. void CLASS scale_colors()
  3413. {
  3414. unsigned bottom, right, size, row, col, ur, uc, x, y, c, sum[8];
  3415. int val, dark, sat,i;
  3416. double dsum[8], dmin, dmax;
  3417. float scale_mul[4], fr, fc;
  3418. ushort *img=0, *pix;
  3419. #ifdef LIBRAW_LIBRARY_BUILD
  3420. RUN_CALLBACK(LIBRAW_PROGRESS_SCALE_COLORS,0,2);
  3421. #endif
  3422. FORC4 cblack[c] += black;
  3423. if (user_mul[0])
  3424. memcpy (pre_mul, user_mul, sizeof pre_mul);
  3425. if (use_auto_wb || (use_camera_wb && cam_mul[0] == -1)) {
  3426. memset (dsum, 0, sizeof dsum);
  3427. bottom = MIN (greybox[1]+greybox[3], height);
  3428. right = MIN (greybox[0]+greybox[2], width);
  3429. for (row=greybox[1]; row < bottom; row += 8)
  3430. for (col=greybox[0]; col < right; col += 8) {
  3431. memset (sum, 0, sizeof sum);
  3432. for (y=row; y < row+8 && y < bottom; y++)
  3433. for (x=col; x < col+8 && x < right; x++)
  3434. FORC4 {
  3435. if (filters) {
  3436. c = FC(y,x);
  3437. val = BAYER(y,x);
  3438. } else
  3439. val = image[y*width+x][c];
  3440. if (val > maximum-25) goto skip_block;
  3441. if ((val -= cblack[c]) < 0) val = 0;
  3442. sum[c] += val;
  3443. sum[c+4]++;
  3444. if (filters) break;
  3445. }
  3446. FORC(8) dsum[c] += sum[c];
  3447. skip_block: ;
  3448. }
  3449. FORC4 if (dsum[c]) pre_mul[c] = dsum[c+4] / dsum[c];
  3450. #ifdef LIBRAW_LIBRARY_BUILD
  3451. color_flags.pre_mul_state = LIBRAW_COLORSTATE_CALCULATED;
  3452. #endif
  3453. }
  3454. if (use_camera_wb && cam_mul[0] != -1) {
  3455. memset (sum, 0, sizeof sum);
  3456. for (row=0; row < 8; row++)
  3457. for (col=0; col < 8; col++) {
  3458. c = FC(row,col);
  3459. if ((val = white[row][col] - cblack[c]) > 0)
  3460. sum[c] += val;
  3461. sum[c+4]++;
  3462. }
  3463. if (sum[0] && sum[1] && sum[2] && sum[3])
  3464. {
  3465. FORC4 pre_mul[c] = (float) sum[c+4] / sum[c];
  3466. #ifdef LIBRAW_LIBRARY_BUILD
  3467. color_flags.pre_mul_state = LIBRAW_COLORSTATE_CALCULATED;
  3468. #endif
  3469. }
  3470. else if (cam_mul[0] && cam_mul[2])
  3471. {
  3472. memcpy (pre_mul, cam_mul, sizeof pre_mul);
  3473. #ifdef LIBRAW_LIBRARY_BUILD
  3474. color_flags.pre_mul_state =color_flags.pre_mul_state;
  3475. #endif
  3476. }
  3477. else
  3478. {
  3479. #ifdef LIBRAW_LIBRARY_BUILD
  3480. imgdata.process_warnings |= LIBRAW_WARN_BAD_CAMERA_WB;
  3481. #endif
  3482. #ifdef DCRAW_VERBOSE
  3483. fprintf (stderr,_("%s: Cannot use camera white balance.\n"), ifname);
  3484. #endif
  3485. }
  3486. }
  3487. if (pre_mul[3] == 0) pre_mul[3] = colors < 4 ? pre_mul[1] : 1;
  3488. dark = black;
  3489. sat = maximum;
  3490. if (threshold) wavelet_denoise();
  3491. maximum -= black;
  3492. for (dmin=DBL_MAX, dmax=c=0; c < 4; c++) {
  3493. if (dmin > pre_mul[c])
  3494. dmin = pre_mul[c];
  3495. if (dmax < pre_mul[c])
  3496. dmax = pre_mul[c];
  3497. }
  3498. if (!highlight) dmax = dmin;
  3499. FORC4 scale_mul[c] = (pre_mul[c] /= dmax) * 65535.0 / maximum;
  3500. #ifdef DCRAW_VERBOSE
  3501. if (verbose) {
  3502. fprintf (stderr,
  3503. _("Scaling with darkness %d, saturation %d, and\nmultipliers"), dark, sat);
  3504. FORC4 fprintf (stderr, " %f", pre_mul[c]);
  3505. fputc ('\n', stderr);
  3506. }
  3507. #endif
  3508. size = iheight*iwidth;
  3509. #if defined(LIBRAW_USE_OPENMP)
  3510. #pragma omp parallel for private(val) default(shared)
  3511. #endif
  3512. for (i=0; i < size*4; i++) {
  3513. val = image[0][i];
  3514. if (!val) continue;
  3515. val -= cblack[i & 3];
  3516. val *= scale_mul[i & 3];
  3517. image[0][i] = CLIP(val);
  3518. }
  3519. if ((aber[0] != 1 || aber[2] != 1) && colors == 3) {
  3520. #ifdef DCRAW_VERBOSE
  3521. if (verbose)
  3522. fprintf (stderr,_("Correcting chromatic aberration...\n"));
  3523. #endif
  3524. for (c=0; c < 4; c+=2) {
  3525. if (aber[c] == 1) continue;
  3526. img = (ushort *) malloc (size * sizeof *img);
  3527. merror (img, "scale_colors()");
  3528. for (i=0; i < size; i++)
  3529. img[i] = image[i][c];
  3530. for (row=0; row < iheight; row++) {
  3531. ur = fr = (row - iheight*0.5) * aber[c] + iheight*0.5;
  3532. if (ur > iheight-2) continue;
  3533. fr -= ur;
  3534. for (col=0; col < iwidth; col++) {
  3535. uc = fc = (col - iwidth*0.5) * aber[c] + iwidth*0.5;
  3536. if (uc > iwidth-2) continue;
  3537. fc -= uc;
  3538. pix = img + ur*iwidth + uc;
  3539. image[row*iwidth+col][c] =
  3540. (pix[ 0]*(1-fc) + pix[ 1]*fc) * (1-fr) +
  3541. (pix[iwidth]*(1-fc) + pix[iwidth+1]*fc) * fr;
  3542. }
  3543. }
  3544. free(img);
  3545. }
  3546. }
  3547. #ifdef LIBRAW_LIBRARY_BUILD
  3548. RUN_CALLBACK(LIBRAW_PROGRESS_SCALE_COLORS,1,2);
  3549. #endif
  3550. }
  3551. void CLASS pre_interpolate()
  3552. {
  3553. ushort (*img)[4];
  3554. int row, col, c;
  3555. #ifdef LIBRAW_LIBRARY_BUILD
  3556. RUN_CALLBACK(LIBRAW_PROGRESS_PRE_INTERPOLATE,0,2);
  3557. #endif
  3558. if (shrink) {
  3559. if (half_size) {
  3560. height = iheight;
  3561. width = iwidth;
  3562. } else {
  3563. img = (ushort (*)[4]) calloc (height*width, sizeof *img);
  3564. merror (img, "pre_interpolate()");
  3565. for (row=0; row < height; row++)
  3566. for (col=0; col < width; col++) {
  3567. c = fc(row,col);
  3568. img[row*width+col][c] = image[(row >> 1)*iwidth+(col >> 1)][c];
  3569. }
  3570. free (image);
  3571. image = img;
  3572. shrink = 0;
  3573. }
  3574. }
  3575. if (filters && colors == 3) {
  3576. if (four_color_rgb && colors++)
  3577. mix_green = !half_size;
  3578. else {
  3579. for (row = FC(1,0) >> 1; row < height; row+=2)
  3580. for (col = FC(row,1) & 1; col < width; col+=2)
  3581. image[row*width+col][1] = image[row*width+col][3];
  3582. filters &= ~((filters & 0x55555555) << 1);
  3583. }
  3584. }
  3585. if (half_size) filters = 0;
  3586. #ifdef LIBRAW_LIBRARY_BUILD
  3587. RUN_CALLBACK(LIBRAW_PROGRESS_PRE_INTERPOLATE,1,2);
  3588. #endif
  3589. }
  3590. void CLASS border_interpolate (int border)
  3591. {
  3592. unsigned row, col, y, x, f, c, sum[8];
  3593. for (row=0; row < height; row++)
  3594. for (col=0; col < width; col++) {
  3595. if (col==border && row >= border && row < height-border)
  3596. col = width-border;
  3597. memset (sum, 0, sizeof sum);
  3598. for (y=row-1; y != row+2; y++)
  3599. for (x=col-1; x != col+2; x++)
  3600. if (y < height && x < width) {
  3601. f = fc(y,x);
  3602. sum[f] += image[y*width+x][f];
  3603. sum[f+4]++;
  3604. }
  3605. f = fc(row,col);
  3606. FORCC if (c != f && sum[c+4])
  3607. image[row*width+col][c] = sum[c] / sum[c+4];
  3608. }
  3609. }
  3610. void CLASS lin_interpolate()
  3611. {
  3612. int code[16][16][32], *ip, sum[4];
  3613. int c, i, x, y, row, col, shift, color;
  3614. ushort *pix;
  3615. #ifdef DCRAW_VERBOSE
  3616. if (verbose) fprintf (stderr,_("Bilinear interpolation...\n"));
  3617. #endif
  3618. #ifdef LIBRAW_LIBRARY_BUILD
  3619. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,0,3);
  3620. #endif
  3621. border_interpolate(1);
  3622. for (row=0; row < 16; row++)
  3623. for (col=0; col < 16; col++) {
  3624. ip = code[row][col];
  3625. memset (sum, 0, sizeof sum);
  3626. for (y=-1; y <= 1; y++)
  3627. for (x=-1; x <= 1; x++) {
  3628. shift = (y==0) + (x==0);
  3629. if (shift == 2) continue;
  3630. color = fc(row+y,col+x);
  3631. *ip++ = (width*y + x)*4 + color;
  3632. *ip++ = shift;
  3633. *ip++ = color;
  3634. sum[color] += 1 << shift;
  3635. }
  3636. FORCC
  3637. if (c != fc(row,col)) {
  3638. *ip++ = c;
  3639. *ip++ = 256 / sum[c];
  3640. }
  3641. }
  3642. #ifdef LIBRAW_LIBRARY_BUILD
  3643. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,1,3);
  3644. #endif
  3645. for (row=1; row < height-1; row++)
  3646. for (col=1; col < width-1; col++) {
  3647. pix = image[row*width+col];
  3648. ip = code[row & 15][col & 15];
  3649. memset (sum, 0, sizeof sum);
  3650. for (i=8; i--; ip+=3)
  3651. sum[ip[2]] += pix[ip[0]] << ip[1];
  3652. for (i=colors; --i; ip+=2)
  3653. pix[ip[0]] = sum[ip[0]] * ip[1] >> 8;
  3654. }
  3655. #ifdef LIBRAW_LIBRARY_BUILD
  3656. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,2,3);
  3657. #endif
  3658. }
  3659. /*
  3660. This algorithm is officially called:
  3661. "Interpolation using a Threshold-based variable number of gradients"
  3662. described in http://scien.stanford.edu/pages/labsite/1999/psych221/projects/99/tingchen/algodep/vargra.html
  3663. I've extended the basic idea to work with non-Bayer filter arrays.
  3664. Gradients are numbered clockwise from NW=0 to W=7.
  3665. */
  3666. void CLASS vng_interpolate()
  3667. {
  3668. static const signed short *cp, terms[] = {
  3669. -2,-2,+0,-1,0,0x01, -2,-2,+0,+0,1,0x01, -2,-1,-1,+0,0,0x01,
  3670. -2,-1,+0,-1,0,0x02, -2,-1,+0,+0,0,0x03, -2,-1,+0,+1,1,0x01,
  3671. -2,+0,+0,-1,0,0x06, -2,+0,+0,+0,1,0x02, -2,+0,+0,+1,0,0x03,
  3672. -2,+1,-1,+0,0,0x04, -2,+1,+0,-1,1,0x04, -2,+1,+0,+0,0,0x06,
  3673. -2,+1,+0,+1,0,0x02, -2,+2,+0,+0,1,0x04, -2,+2,+0,+1,0,0x04,
  3674. -1,-2,-1,+0,0,0x80, -1,-2,+0,-1,0,0x01, -1,-2,+1,-1,0,0x01,
  3675. -1,-2,+1,+0,1,0x01, -1,-1,-1,+1,0,0x88, -1,-1,+1,-2,0,0x40,
  3676. -1,-1,+1,-1,0,0x22, -1,-1,+1,+0,0,0x33, -1,-1,+1,+1,1,0x11,
  3677. -1,+0,-1,+2,0,0x08, -1,+0,+0,-1,0,0x44, -1,+0,+0,+1,0,0x11,
  3678. -1,+0,+1,-2,1,0x40, -1,+0,+1,-1,0,0x66, -1,+0,+1,+0,1,0x22,
  3679. -1,+0,+1,+1,0,0x33, -1,+0,+1,+2,1,0x10, -1,+1,+1,-1,1,0x44,
  3680. -1,+1,+1,+0,0,0x66, -1,+1,+1,+1,0,0x22, -1,+1,+1,+2,0,0x10,
  3681. -1,+2,+0,+1,0,0x04, -1,+2,+1,+0,1,0x04, -1,+2,+1,+1,0,0x04,
  3682. +0,-2,+0,+0,1,0x80, +0,-1,+0,+1,1,0x88, +0,-1,+1,-2,0,0x40,
  3683. +0,-1,+1,+0,0,0x11, +0,-1,+2,-2,0,0x40, +0,-1,+2,-1,0,0x20,
  3684. +0,-1,+2,+0,0,0x30, +0,-1,+2,+1,1,0x10, +0,+0,+0,+2,1,0x08,
  3685. +0,+0,+2,-2,1,0x40, +0,+0,+2,-1,0,0x60, +0,+0,+2,+0,1,0x20,
  3686. +0,+0,+2,+1,0,0x30, +0,+0,+2,+2,1,0x10, +0,+1,+1,+0,0,0x44,
  3687. +0,+1,+1,+2,0,0x10, +0,+1,+2,-1,1,0x40, +0,+1,+2,+0,0,0x60,
  3688. +0,+1,+2,+1,0,0x20, +0,+1,+2,+2,0,0x10, +1,-2,+1,+0,0,0x80,
  3689. +1,-1,+1,+1,0,0x88, +1,+0,+1,+2,0,0x08, +1,+0,+2,-1,0,0x40,
  3690. +1,+0,+2,+1,0,0x10
  3691. }, chood[] = { -1,-1, -1,0, -1,+1, 0,+1, +1,+1, +1,0, +1,-1, 0,-1 };
  3692. ushort (*brow[5])[4], *pix;
  3693. int prow=7, pcol=1, *ip, *code[16][16], gval[8], gmin, gmax, sum[4];
  3694. int row, col, x, y, x1, x2, y1, y2, t, weight, grads, color, diag;
  3695. int g, diff, thold, num, c;
  3696. lin_interpolate();
  3697. #ifdef DCRAW_VERBOSE
  3698. if (verbose) fprintf (stderr,_("VNG interpolation...\n"));
  3699. #endif
  3700. if (filters == 1) prow = pcol = 15;
  3701. ip = (int *) calloc ((prow+1)*(pcol+1), 1280);
  3702. merror (ip, "vng_interpolate()");
  3703. for (row=0; row <= prow; row++) /* Precalculate for VNG */
  3704. for (col=0; col <= pcol; col++) {
  3705. code[row][col] = ip;
  3706. for (cp=terms, t=0; t < 64; t++) {
  3707. y1 = *cp++; x1 = *cp++;
  3708. y2 = *cp++; x2 = *cp++;
  3709. weight = *cp++;
  3710. grads = *cp++;
  3711. color = fc(row+y1,col+x1);
  3712. if (fc(row+y2,col+x2) != color) continue;
  3713. diag = (fc(row,col+1) == color && fc(row+1,col) == color) ? 2:1;
  3714. if (abs(y1-y2) == diag && abs(x1-x2) == diag) continue;
  3715. *ip++ = (y1*width + x1)*4 + color;
  3716. *ip++ = (y2*width + x2)*4 + color;
  3717. *ip++ = weight;
  3718. for (g=0; g < 8; g++)
  3719. if (grads & 1<<g) *ip++ = g;
  3720. *ip++ = -1;
  3721. }
  3722. *ip++ = INT_MAX;
  3723. for (cp=chood, g=0; g < 8; g++) {
  3724. y = *cp++; x = *cp++;
  3725. *ip++ = (y*width + x) * 4;
  3726. color = fc(row,col);
  3727. if (fc(row+y,col+x) != color && fc(row+y*2,col+x*2) == color)
  3728. *ip++ = (y*width + x) * 8 + color;
  3729. else
  3730. *ip++ = 0;
  3731. }
  3732. }
  3733. brow[4] = (ushort (*)[4]) calloc (width*3, sizeof **brow);
  3734. merror (brow[4], "vng_interpolate()");
  3735. for (row=0; row < 3; row++)
  3736. brow[row] = brow[4] + row*width;
  3737. for (row=2; row < height-2; row++) { /* Do VNG interpolation */
  3738. #ifdef LIBRAW_LIBRARY_BUILD
  3739. if(!((row-2)%256))RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,(row-2)/256+1,((height-3)/256)+1);
  3740. #endif
  3741. for (col=2; col < width-2; col++) {
  3742. pix = image[row*width+col];
  3743. ip = code[row & prow][col & pcol];
  3744. memset (gval, 0, sizeof gval);
  3745. while ((g = ip[0]) != INT_MAX) { /* Calculate gradients */
  3746. diff = ABS(pix[g] - pix[ip[1]]) << ip[2];
  3747. gval[ip[3]] += diff;
  3748. ip += 5;
  3749. if ((g = ip[-1]) == -1) continue;
  3750. gval[g] += diff;
  3751. while ((g = *ip++) != -1)
  3752. gval[g] += diff;
  3753. }
  3754. ip++;
  3755. gmin = gmax = gval[0]; /* Choose a threshold */
  3756. for (g=1; g < 8; g++) {
  3757. if (gmin > gval[g]) gmin = gval[g];
  3758. if (gmax < gval[g]) gmax = gval[g];
  3759. }
  3760. if (gmax == 0) {
  3761. memcpy (brow[2][col], pix, sizeof *image);
  3762. continue;
  3763. }
  3764. thold = gmin + (gmax >> 1);
  3765. memset (sum, 0, sizeof sum);
  3766. color = fc(row,col);
  3767. for (num=g=0; g < 8; g++,ip+=2) { /* Average the neighbors */
  3768. if (gval[g] <= thold) {
  3769. FORCC
  3770. if (c == color && ip[1])
  3771. sum[c] += (pix[c] + pix[ip[1]]) >> 1;
  3772. else
  3773. sum[c] += pix[ip[0] + c];
  3774. num++;
  3775. }
  3776. }
  3777. FORCC { /* Save to buffer */
  3778. t = pix[color];
  3779. if (c != color)
  3780. t += (sum[c] - sum[color]) / num;
  3781. brow[2][col][c] = CLIP(t);
  3782. }
  3783. }
  3784. if (row > 3) /* Write buffer to image */
  3785. memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image);
  3786. for (g=0; g < 4; g++)
  3787. brow[(g-1) & 3] = brow[g];
  3788. }
  3789. memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image);
  3790. memcpy (image[(row-1)*width+2], brow[1]+2, (width-4)*sizeof *image);
  3791. free (brow[4]);
  3792. free (code[0][0]);
  3793. }
  3794. /*
  3795. Patterned Pixel Grouping Interpolation by Alain Desbiolles
  3796. */
  3797. void CLASS ppg_interpolate()
  3798. {
  3799. int dir[5] = { 1, width, -1, -width, 1 };
  3800. int row, col, diff[2], guess[2], c, d, i;
  3801. ushort (*pix)[4];
  3802. border_interpolate(3);
  3803. #ifdef DCRAW_VERBOSE
  3804. if (verbose) fprintf (stderr,_("PPG interpolation...\n"));
  3805. #endif
  3806. /* Fill in the green layer with gradients and pattern recognition: */
  3807. #ifdef LIBRAW_LIBRARY_BUILD
  3808. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,0,3);
  3809. #ifdef LIBRAW_USE_OPENMP
  3810. #pragma omp parallel for default(shared) private(guess, diff, row, col, d, c, i, pix) schedule(static)
  3811. #endif
  3812. #endif
  3813. for (row=3; row < height-3; row++)
  3814. for (col=3+(FC(row,3) & 1), c=FC(row,col); col < width-3; col+=2) {
  3815. pix = image + row*width+col;
  3816. for (i=0; (d=dir[i]) > 0; i++) {
  3817. guess[i] = (pix[-d][1] + pix[0][c] + pix[d][1]) * 2
  3818. - pix[-2*d][c] - pix[2*d][c];
  3819. diff[i] = ( ABS(pix[-2*d][c] - pix[ 0][c]) +
  3820. ABS(pix[ 2*d][c] - pix[ 0][c]) +
  3821. ABS(pix[ -d][1] - pix[ d][1]) ) * 3 +
  3822. ( ABS(pix[ 3*d][1] - pix[ d][1]) +
  3823. ABS(pix[-3*d][1] - pix[-d][1]) ) * 2;
  3824. }
  3825. d = dir[i = diff[0] > diff[1]];
  3826. pix[0][1] = ULIM(guess[i] >> 2, pix[d][1], pix[-d][1]);
  3827. }
  3828. /* Calculate red and blue for each green pixel: */
  3829. #ifdef LIBRAW_LIBRARY_BUILD
  3830. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,1,3);
  3831. #ifdef LIBRAW_USE_OPENMP
  3832. #pragma omp parallel for default(shared) private(guess, diff, row, col, d, c, i, pix) schedule(static)
  3833. #endif
  3834. #endif
  3835. for (row=1; row < height-1; row++)
  3836. for (col=1+(FC(row,2) & 1), c=FC(row,col+1); col < width-1; col+=2) {
  3837. pix = image + row*width+col;
  3838. for (i=0; (d=dir[i]) > 0; c=2-c, i++)
  3839. pix[0][c] = CLIP((pix[-d][c] + pix[d][c] + 2*pix[0][1]
  3840. - pix[-d][1] - pix[d][1]) >> 1);
  3841. }
  3842. /* Calculate blue for red pixels and vice versa: */
  3843. #ifdef LIBRAW_LIBRARY_BUILD
  3844. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,2,3);
  3845. #ifdef LIBRAW_USE_OPENMP
  3846. #pragma omp parallel for default(shared) private(guess, diff, row, col, d, c, i, pix) schedule(static)
  3847. #endif
  3848. #endif
  3849. for (row=1; row < height-1; row++)
  3850. for (col=1+(FC(row,1) & 1), c=2-FC(row,col); col < width-1; col+=2) {
  3851. pix = image + row*width+col;
  3852. for (i=0; (d=dir[i]+dir[i+1]) > 0; i++) {
  3853. diff[i] = ABS(pix[-d][c] - pix[d][c]) +
  3854. ABS(pix[-d][1] - pix[0][1]) +
  3855. ABS(pix[ d][1] - pix[0][1]);
  3856. guess[i] = pix[-d][c] + pix[d][c] + 2*pix[0][1]
  3857. - pix[-d][1] - pix[d][1];
  3858. }
  3859. if (diff[0] != diff[1])
  3860. pix[0][c] = CLIP(guess[diff[0] > diff[1]] >> 1);
  3861. else
  3862. pix[0][c] = CLIP((guess[0]+guess[1]) >> 2);
  3863. }
  3864. }
  3865. /*
  3866. Adaptive Homogeneity-Directed interpolation is based on
  3867. the work of Keigo Hirakawa, Thomas Parks, and Paul Lee.
  3868. */
  3869. #define TS 256 /* Tile Size */
  3870. static float dcraw_cbrt[0x10000] = {-1.0f};
  3871. static inline float calc_64cbrt(float f)
  3872. {
  3873. unsigned u;
  3874. static float lower = dcraw_cbrt[0];
  3875. static float upper = dcraw_cbrt[0xffff];
  3876. if (f <= 0) {
  3877. return lower;
  3878. }
  3879. u = (unsigned) f;
  3880. if (u >= 0xffff) {
  3881. return upper;
  3882. }
  3883. return dcraw_cbrt[u];
  3884. }
  3885. void CLASS ahd_interpolate_green_h_and_v(int top, int left, ushort (*out_rgb)[TS][TS][3])
  3886. {
  3887. int row, col;
  3888. int c, val;
  3889. ushort (*pix)[4];
  3890. const int rowlimit = MIN(top+TS, height-2);
  3891. const int collimit = MIN(left+TS, width-2);
  3892. for (row = top; row < rowlimit; row++) {
  3893. col = left + (FC(row,left) & 1);
  3894. for (c = FC(row,col); col < collimit; col+=2) {
  3895. pix = image + row*width+col;
  3896. val = ((pix[-1][1] + pix[0][c] + pix[1][1]) * 2
  3897. - pix[-2][c] - pix[2][c]) >> 2;
  3898. out_rgb[0][row-top][col-left][1] = ULIM(val,pix[-1][1],pix[1][1]);
  3899. val = ((pix[-width][1] + pix[0][c] + pix[width][1]) * 2
  3900. - pix[-2*width][c] - pix[2*width][c]) >> 2;
  3901. out_rgb[1][row-top][col-left][1] = ULIM(val,pix[-width][1],pix[width][1]);
  3902. }
  3903. }
  3904. }
  3905. void CLASS ahd_interpolate_r_and_b_in_rgb_and_convert_to_cielab(int top, int left, ushort (*inout_rgb)[TS][3], short (*out_lab)[TS][3], const float (&xyz_cam)[3][4])
  3906. {
  3907. unsigned row, col;
  3908. int c, val;
  3909. ushort (*pix)[4];
  3910. ushort (*rix)[3];
  3911. short (*lix)[3];
  3912. float xyz[3];
  3913. const unsigned num_pix_per_row = 4*width;
  3914. const unsigned rowlimit = MIN(top+TS-1, height-3);
  3915. const unsigned collimit = MIN(left+TS-1, width-3);
  3916. ushort *pix_above;
  3917. ushort *pix_below;
  3918. int t1, t2;
  3919. for (row = top+1; row < rowlimit; row++) {
  3920. pix = image + row*width + left;
  3921. rix = &inout_rgb[row-top][0];
  3922. lix = &out_lab[row-top][0];
  3923. for (col = left+1; col < collimit; col++) {
  3924. pix++;
  3925. pix_above = &pix[0][0] - num_pix_per_row;
  3926. pix_below = &pix[0][0] + num_pix_per_row;
  3927. rix++;
  3928. lix++;
  3929. c = 2 - FC(row, col);
  3930. if (c == 1) {
  3931. c = FC(row+1,col);
  3932. t1 = 2-c;
  3933. val = pix[0][1] + (( pix[-1][t1] + pix[1][t1]
  3934. - rix[-1][1] - rix[1][1] ) >> 1);
  3935. rix[0][t1] = CLIP(val);
  3936. val = pix[0][1] + (( pix_above[c] + pix_below[c]
  3937. - rix[-TS][1] - rix[TS][1] ) >> 1);
  3938. } else {
  3939. t1 = -4+c; /* -4+c: pixel of color c to the left */
  3940. t2 = 4+c; /* 4+c: pixel of color c to the right */
  3941. val = rix[0][1] + (( pix_above[t1] + pix_above[t2]
  3942. + pix_below[t1] + pix_below[t2]
  3943. - rix[-TS-1][1] - rix[-TS+1][1]
  3944. - rix[+TS-1][1] - rix[+TS+1][1] + 1) >> 2);
  3945. }
  3946. rix[0][c] = CLIP(val);
  3947. c = FC(row,col);
  3948. rix[0][c] = pix[0][c];
  3949. xyz[0] = xyz[1] = xyz[2] = 0.5;
  3950. FORC3 {
  3951. /*
  3952. * Technically this ought to be FORCC, but the rest of
  3953. * ahd_interpolate() assumes 3 colors so let's help the compiler.
  3954. */
  3955. xyz[0] += xyz_cam[0][c] * rix[0][c];
  3956. xyz[1] += xyz_cam[1][c] * rix[0][c];
  3957. xyz[2] += xyz_cam[2][c] * rix[0][c];
  3958. }
  3959. FORC3 {
  3960. xyz[c] = calc_64cbrt(xyz[c]);
  3961. }
  3962. lix[0][0] = (116 * xyz[1] - 16);
  3963. lix[0][1] = 500 * (xyz[0] - xyz[1]);
  3964. lix[0][2] = 200 * (xyz[1] - xyz[2]);
  3965. }
  3966. }
  3967. }
  3968. void CLASS ahd_interpolate_r_and_b_and_convert_to_cielab(int top, int left, ushort (*inout_rgb)[TS][TS][3], short (*out_lab)[TS][TS][3], const float (&xyz_cam)[3][4])
  3969. {
  3970. int direction;
  3971. for (direction = 0; direction < 2; direction++) {
  3972. ahd_interpolate_r_and_b_in_rgb_and_convert_to_cielab(top, left, inout_rgb[direction], out_lab[direction], xyz_cam);
  3973. }
  3974. }
  3975. void CLASS ahd_interpolate_build_homogeneity_map(int top, int left, short (*lab)[TS][TS][3], char (*out_homogeneity_map)[TS][2])
  3976. {
  3977. int row, col;
  3978. int tr, tc;
  3979. int direction;
  3980. int i;
  3981. short (*lix)[3];
  3982. short (*lixs[2])[3];
  3983. short *adjacent_lix;
  3984. unsigned ldiff[2][4], abdiff[2][4], leps, abeps;
  3985. static const int dir[4] = { -1, 1, -TS, TS };
  3986. const int rowlimit = MIN(top+TS-2, height-4);
  3987. const int collimit = MIN(left+TS-2, width-4);
  3988. int homogeneity;
  3989. char (*homogeneity_map_p)[2];
  3990. memset (out_homogeneity_map, 0, 2*TS*TS);
  3991. for (row=top+2; row < rowlimit; row++) {
  3992. tr = row-top;
  3993. homogeneity_map_p = &out_homogeneity_map[tr][1];
  3994. for (direction=0; direction < 2; direction++) {
  3995. lixs[direction] = &lab[direction][tr][1];
  3996. }
  3997. for (col=left+2; col < collimit; col++) {
  3998. tc = col-left;
  3999. homogeneity_map_p++;
  4000. for (direction=0; direction < 2; direction++) {
  4001. lix = ++lixs[direction];
  4002. for (i=0; i < 4; i++) {
  4003. adjacent_lix = lix[dir[i]];
  4004. ldiff[direction][i] = ABS(lix[0][0]-adjacent_lix[0]);
  4005. abdiff[direction][i] = SQR(lix[0][1]-adjacent_lix[1])
  4006. + SQR(lix[0][2]-adjacent_lix[2]);
  4007. }
  4008. }
  4009. leps = MIN(MAX(ldiff[0][0],ldiff[0][1]),
  4010. MAX(ldiff[1][2],ldiff[1][3]));
  4011. abeps = MIN(MAX(abdiff[0][0],abdiff[0][1]),
  4012. MAX(abdiff[1][2],abdiff[1][3]));
  4013. for (direction=0; direction < 2; direction++) {
  4014. homogeneity = 0;
  4015. for (i=0; i < 4; i++) {
  4016. if (ldiff[direction][i] <= leps && abdiff[direction][i] <= abeps) {
  4017. homogeneity++;
  4018. }
  4019. }
  4020. homogeneity_map_p[0][direction] = homogeneity;
  4021. }
  4022. }
  4023. }
  4024. }
  4025. void CLASS ahd_interpolate_combine_homogeneous_pixels(int top, int left, ushort (*rgb)[TS][TS][3], char (*homogeneity_map)[TS][2])
  4026. {
  4027. int row, col;
  4028. int tr, tc;
  4029. int i, j;
  4030. int direction;
  4031. int hm[2];
  4032. int c;
  4033. const int rowlimit = MIN(top+TS-3, height-5);
  4034. const int collimit = MIN(left+TS-3, width-5);
  4035. ushort (*pix)[4];
  4036. ushort (*rix[2])[3];
  4037. for (row=top+3; row < rowlimit; row++) {
  4038. tr = row-top;
  4039. pix = &image[row*width+left+2];
  4040. for (direction = 0; direction < 2; direction++) {
  4041. rix[direction] = &rgb[direction][tr][2];
  4042. }
  4043. for (col=left+3; col < collimit; col++) {
  4044. tc = col-left;
  4045. pix++;
  4046. for (direction = 0; direction < 2; direction++) {
  4047. rix[direction]++;
  4048. }
  4049. for (direction=0; direction < 2; direction++) {
  4050. hm[direction] = 0;
  4051. for (i=tr-1; i <= tr+1; i++) {
  4052. for (j=tc-1; j <= tc+1; j++) {
  4053. hm[direction] += homogeneity_map[i][j][direction];
  4054. }
  4055. }
  4056. }
  4057. if (hm[0] != hm[1]) {
  4058. memcpy(pix[0], rix[hm[1] > hm[0]][0], 3 * sizeof(ushort));
  4059. } else {
  4060. FORC3 {
  4061. pix[0][c] = (rix[0][0][c] + rix[1][0][c]) >> 1;
  4062. }
  4063. }
  4064. }
  4065. }
  4066. }
  4067. void CLASS ahd_interpolate()
  4068. {
  4069. int i, j, k, top, left;
  4070. float xyz_cam[3][4],r;
  4071. char *buffer;
  4072. ushort (*rgb)[TS][TS][3];
  4073. short (*lab)[TS][TS][3];
  4074. char (*homo)[TS][2];
  4075. int terminate_flag = 0;
  4076. if(dcraw_cbrt[0]<-0.1){
  4077. for (i=0x10000-1; i >=0; i--) {
  4078. r = i / 65535.0;
  4079. dcraw_cbrt[i] = 64.0*(r > 0.008856 ? pow((double)r,1/3.0) : 7.787*r + 16/116.0);
  4080. }
  4081. }
  4082. #ifdef DCRAW_VERBOSE
  4083. if (verbose) fprintf (stderr,_("AHD interpolation...\n"));
  4084. #endif
  4085. for (i=0; i < 3; i++) {
  4086. for (j=0; j < colors; j++) {
  4087. xyz_cam[i][j] = 0;
  4088. for (k=0; k < 3; k++) {
  4089. xyz_cam[i][j] += xyz_rgb[i][k] * rgb_cam[k][j] / d65_white[i];
  4090. }
  4091. }
  4092. }
  4093. border_interpolate(5);
  4094. #ifdef LIBRAW_LIBRARY_BUILD
  4095. #ifdef LIBRAW_USE_OPENMP
  4096. #pragma omp parallel private(buffer,rgb,lab,homo,top,left,i,j,k) shared(xyz_cam,terminate_flag)
  4097. #endif
  4098. #endif
  4099. {
  4100. buffer = (char *) malloc (26*TS*TS); /* 1664 kB */
  4101. merror (buffer, "ahd_interpolate()");
  4102. rgb = (ushort(*)[TS][TS][3]) buffer;
  4103. lab = (short (*)[TS][TS][3])(buffer + 12*TS*TS);
  4104. homo = (char (*)[TS][2]) (buffer + 24*TS*TS);
  4105. #ifdef LIBRAW_LIBRARY_BUILD
  4106. #ifdef LIBRAW_USE_OPENMP
  4107. #pragma omp for schedule(dynamic)
  4108. #endif
  4109. #endif
  4110. for (top=2; top < height-5; top += TS-6){
  4111. #ifdef LIBRAW_LIBRARY_BUILD
  4112. #ifdef LIBRAW_USE_OPENMP
  4113. if(0== omp_get_thread_num())
  4114. #endif
  4115. if(callbacks.progress_cb) {
  4116. int rr = (*callbacks.progress_cb)(callbacks.progresscb_data,LIBRAW_PROGRESS_INTERPOLATE,top-2,height-7);
  4117. if(rr)
  4118. terminate_flag = 1;
  4119. }
  4120. #endif
  4121. for (left=2; !terminate_flag && (left < width-5); left += TS-6) {
  4122. ahd_interpolate_green_h_and_v(top, left, rgb);
  4123. ahd_interpolate_r_and_b_and_convert_to_cielab(top, left, rgb, lab, xyz_cam);
  4124. ahd_interpolate_build_homogeneity_map(top, left, lab, homo);
  4125. ahd_interpolate_combine_homogeneous_pixels(top, left, rgb, homo);
  4126. }
  4127. }
  4128. free (buffer);
  4129. }
  4130. #ifdef LIBRAW_LIBRARY_BUILD
  4131. if(terminate_flag)
  4132. throw LIBRAW_EXCEPTION_CANCELLED_BY_CALLBACK;
  4133. #endif
  4134. }
  4135. #undef TS
  4136. void CLASS median_filter()
  4137. {
  4138. ushort (*pix)[4];
  4139. int pass, c, i, j, k, med[9];
  4140. static const uchar opt[] = /* Optimal 9-element median search */
  4141. { 1,2, 4,5, 7,8, 0,1, 3,4, 6,7, 1,2, 4,5, 7,8,
  4142. 0,3, 5,8, 4,7, 3,6, 1,4, 2,5, 4,7, 4,2, 6,4, 4,2 };
  4143. for (pass=1; pass <= med_passes; pass++) {
  4144. #ifdef LIBRAW_LIBRARY_BUILD
  4145. RUN_CALLBACK(LIBRAW_PROGRESS_MEDIAN_FILTER,pass-1,med_passes);
  4146. #endif
  4147. #ifdef DCRAW_VERBOSE
  4148. if (verbose)
  4149. fprintf (stderr,_("Median filter pass %d...\n"), pass);
  4150. #endif
  4151. for (c=0; c < 3; c+=2) {
  4152. for (pix = image; pix < image+width*height; pix++)
  4153. pix[0][3] = pix[0][c];
  4154. for (pix = image+width; pix < image+width*(height-1); pix++) {
  4155. if ((pix-image+1) % width < 2) continue;
  4156. for (k=0, i = -width; i <= width; i += width)
  4157. for (j = i-1; j <= i+1; j++)
  4158. med[k++] = pix[j][3] - pix[j][1];
  4159. for (i=0; i < sizeof opt; i+=2)
  4160. if (med[opt[i]] > med[opt[i+1]])
  4161. SWAP (med[opt[i]] , med[opt[i+1]]);
  4162. pix[0][c] = CLIP(med[4] + pix[0][1]);
  4163. }
  4164. }
  4165. }
  4166. }
  4167. void CLASS blend_highlights()
  4168. {
  4169. int clip=INT_MAX, row, col, c, i, j;
  4170. static const float trans[2][4][4] =
  4171. { { { 1,1,1 }, { 1.7320508,-1.7320508,0 }, { -1,-1,2 } },
  4172. { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } };
  4173. static const float itrans[2][4][4] =
  4174. { { { 1,0.8660254,-0.5 }, { 1,-0.8660254,-0.5 }, { 1,0,1 } },
  4175. { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } };
  4176. float cam[2][4], lab[2][4], sum[2], chratio;
  4177. if ((unsigned) (colors-3) > 1) return;
  4178. #ifdef DCRAW_VERBOSE
  4179. if (verbose) fprintf (stderr,_("Blending highlights...\n"));
  4180. #endif
  4181. FORCC if (clip > (i = 65535*pre_mul[c])) clip = i;
  4182. #ifdef LIBRAW_LIBRARY_BUILD
  4183. RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS,0,2);
  4184. #endif
  4185. for (row=0; row < height; row++)
  4186. for (col=0; col < width; col++) {
  4187. FORCC if (image[row*width+col][c] > clip) break;
  4188. if (c == colors) continue;
  4189. FORCC {
  4190. cam[0][c] = image[row*width+col][c];
  4191. cam[1][c] = MIN(cam[0][c],clip);
  4192. }
  4193. for (i=0; i < 2; i++) {
  4194. FORCC for (lab[i][c]=j=0; j < colors; j++)
  4195. lab[i][c] += trans[colors-3][c][j] * cam[i][j];
  4196. for (sum[i]=0,c=1; c < colors; c++)
  4197. sum[i] += SQR(lab[i][c]);
  4198. }
  4199. chratio = sqrt(sum[1]/sum[0]);
  4200. for (c=1; c < colors; c++)
  4201. lab[0][c] *= chratio;
  4202. FORCC for (cam[0][c]=j=0; j < colors; j++)
  4203. cam[0][c] += itrans[colors-3][c][j] * lab[0][j];
  4204. FORCC image[row*width+col][c] = cam[0][c] / colors;
  4205. }
  4206. #ifdef LIBRAW_LIBRARY_BUILD
  4207. RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS,1,2);
  4208. #endif
  4209. }
  4210. #define SCALE (4 >> shrink)
  4211. void CLASS recover_highlights()
  4212. {
  4213. float *map, sum, wgt, grow;
  4214. int hsat[4], count, spread, change, val, i;
  4215. unsigned high, wide, mrow, mcol, row, col, kc, c, d, y, x;
  4216. ushort *pixel;
  4217. static const signed char dir[8][2] =
  4218. { {-1,-1}, {-1,0}, {-1,1}, {0,1}, {1,1}, {1,0}, {1,-1}, {0,-1} };
  4219. #ifdef DCRAW_VERBOSE
  4220. if (verbose) fprintf (stderr,_("Rebuilding highlights...\n"));
  4221. #endif
  4222. grow = pow (2.0, 4.0-highlight);
  4223. FORCC hsat[c] = 32000 * pre_mul[c];
  4224. for (kc=0, c=1; c < colors; c++)
  4225. if (pre_mul[kc] < pre_mul[c]) kc = c;
  4226. high = height / SCALE;
  4227. wide = width / SCALE;
  4228. map = (float *) calloc (high*wide, sizeof *map);
  4229. merror (map, "recover_highlights()");
  4230. FORCC if (c != kc) {
  4231. #ifdef LIBRAW_LIBRARY_BUILD
  4232. RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS,c-1,colors-1);
  4233. #endif
  4234. memset (map, 0, high*wide*sizeof *map);
  4235. for (mrow=0; mrow < high; mrow++)
  4236. for (mcol=0; mcol < wide; mcol++) {
  4237. sum = wgt = count = 0;
  4238. for (row = mrow*SCALE; row < (mrow+1)*SCALE; row++)
  4239. for (col = mcol*SCALE; col < (mcol+1)*SCALE; col++) {
  4240. pixel = image[row*width+col];
  4241. if (pixel[c] / hsat[c] == 1 && pixel[kc] > 24000) {
  4242. sum += pixel[c];
  4243. wgt += pixel[kc];
  4244. count++;
  4245. }
  4246. }
  4247. if (count == SCALE*SCALE)
  4248. map[mrow*wide+mcol] = sum / wgt;
  4249. }
  4250. for (spread = 32/grow; spread--; ) {
  4251. for (mrow=0; mrow < high; mrow++)
  4252. for (mcol=0; mcol < wide; mcol++) {
  4253. if (map[mrow*wide+mcol]) continue;
  4254. sum = count = 0;
  4255. for (d=0; d < 8; d++) {
  4256. y = mrow + dir[d][0];
  4257. x = mcol + dir[d][1];
  4258. if (y < high && x < wide && map[y*wide+x] > 0) {
  4259. sum += (1 + (d & 1)) * map[y*wide+x];
  4260. count += 1 + (d & 1);
  4261. }
  4262. }
  4263. if (count > 3)
  4264. map[mrow*wide+mcol] = - (sum+grow) / (count+grow);
  4265. }
  4266. for (change=i=0; i < high*wide; i++)
  4267. if (map[i] < 0) {
  4268. map[i] = -map[i];
  4269. change = 1;
  4270. }
  4271. if (!change) break;
  4272. }
  4273. for (i=0; i < high*wide; i++)
  4274. if (map[i] == 0) map[i] = 1;
  4275. for (mrow=0; mrow < high; mrow++)
  4276. for (mcol=0; mcol < wide; mcol++) {
  4277. for (row = mrow*SCALE; row < (mrow+1)*SCALE; row++)
  4278. for (col = mcol*SCALE; col < (mcol+1)*SCALE; col++) {
  4279. pixel = image[row*width+col];
  4280. if (pixel[c] / hsat[c] > 1) {
  4281. val = pixel[kc] * map[mrow*wide+mcol];
  4282. if (pixel[c] < val) pixel[c] = CLIP(val);
  4283. }
  4284. }
  4285. }
  4286. }
  4287. free (map);
  4288. }
  4289. #undef SCALE
  4290. void CLASS tiff_get (unsigned base,
  4291. unsigned *tag, unsigned *type, unsigned *len, unsigned *save)
  4292. {
  4293. *tag = get2();
  4294. *type = get2();
  4295. *len = get4();
  4296. *save = ftell(ifp) + 4;
  4297. if (*len * ("11124811248488"[*type < 14 ? *type:0]-'0') > 4)
  4298. fseek (ifp, get4()+base, SEEK_SET);
  4299. }
  4300. void CLASS parse_thumb_note (int base, unsigned toff, unsigned tlen)
  4301. {
  4302. unsigned entries, tag, type, len, save;
  4303. entries = get2();
  4304. while (entries--) {
  4305. tiff_get (base, &tag, &type, &len, &save);
  4306. if (tag == toff) thumb_offset = get4()+base;
  4307. if (tag == tlen) thumb_length = get4();
  4308. fseek (ifp, save, SEEK_SET);
  4309. }
  4310. }
  4311. void CLASS parse_makernote (int base, int uptag)
  4312. {
  4313. static const uchar xlat[2][256] = {
  4314. { 0xc1,0xbf,0x6d,0x0d,0x59,0xc5,0x13,0x9d,0x83,0x61,0x6b,0x4f,0xc7,0x7f,0x3d,0x3d,
  4315. 0x53,0x59,0xe3,0xc7,0xe9,0x2f,0x95,0xa7,0x95,0x1f,0xdf,0x7f,0x2b,0x29,0xc7,0x0d,
  4316. 0xdf,0x07,0xef,0x71,0x89,0x3d,0x13,0x3d,0x3b,0x13,0xfb,0x0d,0x89,0xc1,0x65,0x1f,
  4317. 0xb3,0x0d,0x6b,0x29,0xe3,0xfb,0xef,0xa3,0x6b,0x47,0x7f,0x95,0x35,0xa7,0x47,0x4f,
  4318. 0xc7,0xf1,0x59,0x95,0x35,0x11,0x29,0x61,0xf1,0x3d,0xb3,0x2b,0x0d,0x43,0x89,0xc1,
  4319. 0x9d,0x9d,0x89,0x65,0xf1,0xe9,0xdf,0xbf,0x3d,0x7f,0x53,0x97,0xe5,0xe9,0x95,0x17,
  4320. 0x1d,0x3d,0x8b,0xfb,0xc7,0xe3,0x67,0xa7,0x07,0xf1,0x71,0xa7,0x53,0xb5,0x29,0x89,
  4321. 0xe5,0x2b,0xa7,0x17,0x29,0xe9,0x4f,0xc5,0x65,0x6d,0x6b,0xef,0x0d,0x89,0x49,0x2f,
  4322. 0xb3,0x43,0x53,0x65,0x1d,0x49,0xa3,0x13,0x89,0x59,0xef,0x6b,0xef,0x65,0x1d,0x0b,
  4323. 0x59,0x13,0xe3,0x4f,0x9d,0xb3,0x29,0x43,0x2b,0x07,0x1d,0x95,0x59,0x59,0x47,0xfb,
  4324. 0xe5,0xe9,0x61,0x47,0x2f,0x35,0x7f,0x17,0x7f,0xef,0x7f,0x95,0x95,0x71,0xd3,0xa3,
  4325. 0x0b,0x71,0xa3,0xad,0x0b,0x3b,0xb5,0xfb,0xa3,0xbf,0x4f,0x83,0x1d,0xad,0xe9,0x2f,
  4326. 0x71,0x65,0xa3,0xe5,0x07,0x35,0x3d,0x0d,0xb5,0xe9,0xe5,0x47,0x3b,0x9d,0xef,0x35,
  4327. 0xa3,0xbf,0xb3,0xdf,0x53,0xd3,0x97,0x53,0x49,0x71,0x07,0x35,0x61,0x71,0x2f,0x43,
  4328. 0x2f,0x11,0xdf,0x17,0x97,0xfb,0x95,0x3b,0x7f,0x6b,0xd3,0x25,0xbf,0xad,0xc7,0xc5,
  4329. 0xc5,0xb5,0x8b,0xef,0x2f,0xd3,0x07,0x6b,0x25,0x49,0x95,0x25,0x49,0x6d,0x71,0xc7 },
  4330. { 0xa7,0xbc,0xc9,0xad,0x91,0xdf,0x85,0xe5,0xd4,0x78,0xd5,0x17,0x46,0x7c,0x29,0x4c,
  4331. 0x4d,0x03,0xe9,0x25,0x68,0x11,0x86,0xb3,0xbd,0xf7,0x6f,0x61,0x22,0xa2,0x26,0x34,
  4332. 0x2a,0xbe,0x1e,0x46,0x14,0x68,0x9d,0x44,0x18,0xc2,0x40,0xf4,0x7e,0x5f,0x1b,0xad,
  4333. 0x0b,0x94,0xb6,0x67,0xb4,0x0b,0xe1,0xea,0x95,0x9c,0x66,0xdc,0xe7,0x5d,0x6c,0x05,
  4334. 0xda,0xd5,0xdf,0x7a,0xef,0xf6,0xdb,0x1f,0x82,0x4c,0xc0,0x68,0x47,0xa1,0xbd,0xee,
  4335. 0x39,0x50,0x56,0x4a,0xdd,0xdf,0xa5,0xf8,0xc6,0xda,0xca,0x90,0xca,0x01,0x42,0x9d,
  4336. 0x8b,0x0c,0x73,0x43,0x75,0x05,0x94,0xde,0x24,0xb3,0x80,0x34,0xe5,0x2c,0xdc,0x9b,
  4337. 0x3f,0xca,0x33,0x45,0xd0,0xdb,0x5f,0xf5,0x52,0xc3,0x21,0xda,0xe2,0x22,0x72,0x6b,
  4338. 0x3e,0xd0,0x5b,0xa8,0x87,0x8c,0x06,0x5d,0x0f,0xdd,0x09,0x19,0x93,0xd0,0xb9,0xfc,
  4339. 0x8b,0x0f,0x84,0x60,0x33,0x1c,0x9b,0x45,0xf1,0xf0,0xa3,0x94,0x3a,0x12,0x77,0x33,
  4340. 0x4d,0x44,0x78,0x28,0x3c,0x9e,0xfd,0x65,0x57,0x16,0x94,0x6b,0xfb,0x59,0xd0,0xc8,
  4341. 0x22,0x36,0xdb,0xd2,0x63,0x98,0x43,0xa1,0x04,0x87,0x86,0xf7,0xa6,0x26,0xbb,0xd6,
  4342. 0x59,0x4d,0xbf,0x6a,0x2e,0xaa,0x2b,0xef,0xe6,0x78,0xb6,0x4e,0xe0,0x2f,0xdc,0x7c,
  4343. 0xbe,0x57,0x19,0x32,0x7e,0x2a,0xd0,0xb8,0xba,0x29,0x00,0x3c,0x52,0x7d,0xa8,0x49,
  4344. 0x3b,0x2d,0xeb,0x25,0x49,0xfa,0xa3,0xaa,0x39,0xa7,0xc5,0xa7,0x50,0x11,0x36,0xfb,
  4345. 0xc6,0x67,0x4a,0xf5,0xa5,0x12,0x65,0x7e,0xb0,0xdf,0xaf,0x4e,0xb3,0x61,0x7f,0x2f } };
  4346. unsigned offset=0, entries, tag, type, len, save, c;
  4347. unsigned ver97=0, serial=0, i, wbi=0, wb[4]={0,0,0,0};
  4348. uchar buf97[324], ci, cj, ck;
  4349. short morder, sorder=order;
  4350. char buf[10];
  4351. /*
  4352. The MakerNote might have its own TIFF header (possibly with
  4353. its own byte-order!), or it might just be a table.
  4354. */
  4355. if (!strcmp(make,"Nokia")) return;
  4356. fread (buf, 1, 10, ifp);
  4357. if (!strncmp (buf,"KDK" ,3) || /* these aren't TIFF tables */
  4358. !strncmp (buf,"VER" ,3) ||
  4359. !strncmp (buf,"IIII",4) ||
  4360. !strncmp (buf,"MMMM",4)) return;
  4361. if (!strncmp (buf,"KC" ,2) || /* Konica KD-400Z, KD-510Z */
  4362. !strncmp (buf,"MLY" ,3)) { /* Minolta DiMAGE G series */
  4363. order = 0x4d4d;
  4364. while ((i=ftell(ifp)) < data_offset && i < 16384) {
  4365. wb[0] = wb[2]; wb[2] = wb[1]; wb[1] = wb[3];
  4366. wb[3] = get2();
  4367. if (wb[1] == 256 && wb[3] == 256 &&
  4368. wb[0] > 256 && wb[0] < 640 && wb[2] > 256 && wb[2] < 640)
  4369. FORC4 cam_mul[c] = wb[c];
  4370. #ifdef LIBRAW_LIBRARY_BUILD
  4371. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4372. #endif
  4373. }
  4374. goto quit;
  4375. }
  4376. if (!strcmp (buf,"Nikon")) {
  4377. base = ftell(ifp);
  4378. order = get2();
  4379. if (get2() != 42) goto quit;
  4380. offset = get4();
  4381. fseek (ifp, offset-8, SEEK_CUR);
  4382. } else if (!strcmp (buf,"OLYMPUS")) {
  4383. base = ftell(ifp)-10;
  4384. fseek (ifp, -2, SEEK_CUR);
  4385. order = get2(); get2();
  4386. } else if (!strncmp (buf,"SONY",4) ||
  4387. !strcmp (buf,"Panasonic")) {
  4388. goto nf;
  4389. } else if (!strncmp (buf,"FUJIFILM",8)) {
  4390. base = ftell(ifp)-10;
  4391. nf: order = 0x4949;
  4392. fseek (ifp, 2, SEEK_CUR);
  4393. } else if (!strcmp (buf,"OLYMP") ||
  4394. !strcmp (buf,"LEICA") ||
  4395. !strcmp (buf,"Ricoh") ||
  4396. !strcmp (buf,"EPSON"))
  4397. fseek (ifp, -2, SEEK_CUR);
  4398. else if (!strcmp (buf,"AOC") ||
  4399. !strcmp (buf,"QVC"))
  4400. fseek (ifp, -4, SEEK_CUR);
  4401. else {
  4402. fseek (ifp, -10, SEEK_CUR);
  4403. if (!strncmp(make,"SAMSUNG",7))
  4404. base = ftell(ifp);
  4405. }
  4406. entries = get2();
  4407. if (entries > 1000) return;
  4408. morder = order;
  4409. while (entries--) {
  4410. order = morder;
  4411. tiff_get (base, &tag, &type, &len, &save);
  4412. tag |= uptag << 16;
  4413. if (tag == 2 && strstr(make,"NIKON") && !iso_speed)
  4414. iso_speed = (get2(),get2());
  4415. if (tag == 4 && len > 26 && len < 35) {
  4416. if ((i=(get4(),get2())) != 0x7fff && !iso_speed)
  4417. iso_speed = 50 * pow (2.0, i/32.0 - 4);
  4418. if ((i=(get2(),get2())) != 0x7fff && !aperture)
  4419. aperture = pow (2.0, i/64.0);
  4420. if ((i=get2()) != 0xffff && !shutter)
  4421. shutter = pow (2.0, (short) i/-32.0);
  4422. wbi = (get2(),get2());
  4423. shot_order = (get2(),get2());
  4424. }
  4425. if ((tag == 4 || tag == 0x114) && !strncmp(make,"KONICA",6)) {
  4426. fseek (ifp, tag == 4 ? 140:160, SEEK_CUR);
  4427. switch (get2()) {
  4428. case 72: flip = 0; break;
  4429. case 76: flip = 6; break;
  4430. case 82: flip = 5; break;
  4431. }
  4432. }
  4433. if (tag == 7 && type == 2 && len > 20)
  4434. fgets (model2, 64, ifp);
  4435. if (tag == 8 && type == 4)
  4436. shot_order = get4();
  4437. if (tag == 9 && !strcmp(make,"Canon"))
  4438. fread (artist, 64, 1, ifp);
  4439. if (tag == 0xc && len == 4) {
  4440. cam_mul[0] = getreal(type);
  4441. cam_mul[2] = getreal(type);
  4442. #ifdef LIBRAW_LIBRARY_BUILD
  4443. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4444. #endif
  4445. }
  4446. if (tag == 0xd && type == 7 && get2() == 0xaaaa) {
  4447. fread (buf97, 1, sizeof buf97, ifp);
  4448. char *pp = (char*)memmem ((char*)buf97, sizeof buf97,(char*)"\xbb\xbb",2);
  4449. i = ((uchar*)pp - buf97) + 10;
  4450. if (i < 70 && buf97[i] < 3)
  4451. flip = "065"[buf97[i]]-'0';
  4452. }
  4453. if (tag == 0x10 && type == 4)
  4454. unique_id = get4();
  4455. if (tag == 0x11 && is_raw && !strncmp(make,"NIKON",5)) {
  4456. fseek (ifp, get4()+base, SEEK_SET);
  4457. parse_tiff_ifd (base);
  4458. }
  4459. if (tag == 0x14 && type == 7) {
  4460. if (len == 2560) {
  4461. fseek (ifp, 1248, SEEK_CUR);
  4462. goto get2_256;
  4463. }
  4464. fread (buf, 1, 10, ifp);
  4465. if (!strncmp(buf,"NRW ",4)) {
  4466. fseek (ifp, strcmp(buf+4,"0100") ? 46:1546, SEEK_CUR);
  4467. cam_mul[0] = get4() << 2;
  4468. cam_mul[1] = get4() + get4();
  4469. cam_mul[2] = get4() << 2;
  4470. }
  4471. }
  4472. if (tag == 0x15 && type == 2 && is_raw)
  4473. fread (model, 64, 1, ifp);
  4474. if (strstr(make,"PENTAX")) {
  4475. if (tag == 0x1b) tag = 0x1018;
  4476. if (tag == 0x1c) tag = 0x1017;
  4477. }
  4478. if (tag == 0x1d)
  4479. while ((c = fgetc(ifp)) && c != EOF)
  4480. serial = serial*10 + (isdigit(c) ? c - '0' : c % 10);
  4481. if (tag == 0x81 && type == 4) {
  4482. data_offset = get4();
  4483. fseek (ifp, data_offset + 41, SEEK_SET);
  4484. raw_height = get2() * 2;
  4485. raw_width = get2();
  4486. filters = 0x61616161;
  4487. }
  4488. if (tag == 0x29 && type == 1) {
  4489. c = wbi < 18 ? "012347800000005896"[wbi]-'0' : 0;
  4490. fseek (ifp, 8 + c*32, SEEK_CUR);
  4491. FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get4();
  4492. #ifdef LIBRAW_LIBRARY_BUILD
  4493. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4494. #endif
  4495. }
  4496. if ((tag == 0x81 && type == 7) ||
  4497. (tag == 0x100 && type == 7) ||
  4498. (tag == 0x280 && type == 1)) {
  4499. thumb_offset = ftell(ifp);
  4500. thumb_length = len;
  4501. }
  4502. if (tag == 0x88 && type == 4 && (thumb_offset = get4()))
  4503. thumb_offset += base;
  4504. if (tag == 0x89 && type == 4)
  4505. thumb_length = get4();
  4506. if (tag == 0x8c || tag == 0x96)
  4507. meta_offset = ftell(ifp);
  4508. if (tag == 0x97) {
  4509. for (i=0; i < 4; i++)
  4510. ver97 = ver97 * 10 + fgetc(ifp)-'0';
  4511. switch (ver97) {
  4512. case 100:
  4513. fseek (ifp, 68, SEEK_CUR);
  4514. FORC4 cam_mul[(c >> 1) | ((c & 1) << 1)] = get2();
  4515. #ifdef LIBRAW_LIBRARY_BUILD
  4516. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4517. #endif
  4518. break;
  4519. case 102:
  4520. fseek (ifp, 6, SEEK_CUR);
  4521. goto get2_rggb;
  4522. case 103:
  4523. fseek (ifp, 16, SEEK_CUR);
  4524. FORC4 cam_mul[c] = get2();
  4525. #ifdef LIBRAW_LIBRARY_BUILD
  4526. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4527. #endif
  4528. }
  4529. if (ver97 >= 200) {
  4530. if (ver97 != 205) fseek (ifp, 280, SEEK_CUR);
  4531. fread (buf97, 324, 1, ifp);
  4532. }
  4533. }
  4534. if (tag == 0xa1 && type == 7) {
  4535. order = 0x4949;
  4536. fseek (ifp, 140, SEEK_CUR);
  4537. FORC3 cam_mul[c] = get4();
  4538. }
  4539. if (tag == 0xa4 && type == 3) {
  4540. fseek (ifp, wbi*48, SEEK_CUR);
  4541. FORC3 cam_mul[c] = get2();
  4542. #ifdef LIBRAW_LIBRARY_BUILD
  4543. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4544. #endif
  4545. }
  4546. if (tag == 0xa7 && (unsigned) (ver97-200) < 17) {
  4547. ci = xlat[0][serial & 0xff];
  4548. cj = xlat[1][fgetc(ifp)^fgetc(ifp)^fgetc(ifp)^fgetc(ifp)];
  4549. ck = 0x60;
  4550. for (i=0; i < 324; i++)
  4551. buf97[i] ^= (cj += ci * ck++);
  4552. i = "66666>666;6A;:;55"[ver97-200] - '0';
  4553. FORC4 cam_mul[c ^ (c >> 1) ^ (i & 1)] =
  4554. sget2 (buf97 + (i & -2) + c*2);
  4555. #ifdef LIBRAW_LIBRARY_BUILD
  4556. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4557. #endif
  4558. }
  4559. if (tag == 0x200 && len == 3)
  4560. shot_order = (get4(),get4());
  4561. if (tag == 0x200 && len == 4)
  4562. FORC4 cblack[c ^ c >> 1] = get2();
  4563. if (tag == 0x201 && len == 4)
  4564. goto get2_rggb;
  4565. if (tag == 0x220 && type == 7)
  4566. meta_offset = ftell(ifp);
  4567. if (tag == 0x401 && type == 4 && len == 4)
  4568. FORC4 cblack[c ^ c >> 1] = get4();
  4569. if (tag == 0xe01) { /* Nikon Capture Note */
  4570. order = 0x4949;
  4571. fseek (ifp, 22, SEEK_CUR);
  4572. for (offset=22; offset+22 < len; offset += 22+i) {
  4573. tag = get4();
  4574. fseek (ifp, 14, SEEK_CUR);
  4575. i = get4()-4;
  4576. if (tag == 0x76a43207) flip = get2();
  4577. else fseek (ifp, i, SEEK_CUR);
  4578. }
  4579. }
  4580. if (tag == 0xe80 && len == 256 && type == 7) {
  4581. fseek (ifp, 48, SEEK_CUR);
  4582. cam_mul[0] = get2() * 508 * 1.078 / 0x10000;
  4583. cam_mul[2] = get2() * 382 * 1.173 / 0x10000;
  4584. #ifdef LIBRAW_LIBRARY_BUILD
  4585. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4586. #endif
  4587. }
  4588. if (tag == 0xf00 && type == 7) {
  4589. if (len == 614)
  4590. fseek (ifp, 176, SEEK_CUR);
  4591. else if (len == 734 || len == 1502)
  4592. fseek (ifp, 148, SEEK_CUR);
  4593. else goto next;
  4594. goto get2_256;
  4595. }
  4596. if ((tag == 0x1011 && len == 9) || tag == 0x20400200)
  4597. {
  4598. for (i=0; i < 3; i++)
  4599. FORC3 cmatrix[i][c] = ((short) get2()) / 256.0;
  4600. #ifdef LIBRAW_LIBRARY_BUILD
  4601. color_flags.cmatrix_state = LIBRAW_COLORSTATE_LOADED;
  4602. #endif
  4603. }
  4604. if ((tag == 0x1012 || tag == 0x20400600) && len == 4)
  4605. FORC4 cblack[c ^ c >> 1] = get2();
  4606. if (tag == 0x1017 || tag == 0x20400100)
  4607. {
  4608. cam_mul[0] = get2() / 256.0;
  4609. #ifdef LIBRAW_LIBRARY_BUILD
  4610. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4611. #endif
  4612. }
  4613. if (tag == 0x1018 || tag == 0x20400100)
  4614. {
  4615. cam_mul[2] = get2() / 256.0;
  4616. #ifdef LIBRAW_LIBRARY_BUILD
  4617. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4618. #endif
  4619. }
  4620. if (tag == 0x2011 && len == 2) {
  4621. get2_256:
  4622. order = 0x4d4d;
  4623. cam_mul[0] = get2() / 256.0;
  4624. cam_mul[2] = get2() / 256.0;
  4625. #ifdef LIBRAW_LIBRARY_BUILD
  4626. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4627. #endif
  4628. }
  4629. if ((tag | 0x70) == 0x2070 && type == 4)
  4630. fseek (ifp, get4()+base, SEEK_SET);
  4631. if (tag == 0x2010 && type != 7)
  4632. load_raw = &CLASS olympus_load_raw;
  4633. if (tag == 0x2020)
  4634. parse_thumb_note (base, 257, 258);
  4635. if (tag == 0x2040)
  4636. parse_makernote (base, 0x2040);
  4637. if (tag == 0xb028) {
  4638. fseek (ifp, get4()+base, SEEK_SET);
  4639. parse_thumb_note (base, 136, 137);
  4640. }
  4641. if (tag == 0x4001 && len > 500) {
  4642. i = len == 582 ? 50 : len == 653 ? 68 : len == 5120 ? 142 : 126;
  4643. fseek (ifp, i, SEEK_CUR);
  4644. get2_rggb:
  4645. FORC4 cam_mul[c ^ (c >> 1)] = get2();
  4646. #ifdef LIBRAW_LIBRARY_BUILD
  4647. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4648. #endif
  4649. fseek (ifp, 22, SEEK_CUR);
  4650. FORC4 sraw_mul[c ^ (c >> 1)] = get2();
  4651. }
  4652. if (tag == 0xa021)
  4653. FORC4 cam_mul[c ^ (c >> 1)] = get4();
  4654. if (tag == 0xa028)
  4655. FORC4 cam_mul[c ^ (c >> 1)] -= get4();
  4656. next:
  4657. fseek (ifp, save, SEEK_SET);
  4658. }
  4659. quit:
  4660. order = sorder;
  4661. }
  4662. /*
  4663. Since the TIFF DateTime string has no timezone information,
  4664. assume that the camera's clock was set to Universal Time.
  4665. */
  4666. void CLASS get_timestamp (int reversed)
  4667. {
  4668. struct tm t;
  4669. char str[20];
  4670. int i;
  4671. str[19] = 0;
  4672. if (reversed)
  4673. for (i=19; i--; ) str[i] = fgetc(ifp);
  4674. else
  4675. fread (str, 19, 1, ifp);
  4676. memset (&t, 0, sizeof t);
  4677. if (sscanf (str, "%d:%d:%d %d:%d:%d", &t.tm_year, &t.tm_mon,
  4678. &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec) != 6)
  4679. return;
  4680. t.tm_year -= 1900;
  4681. t.tm_mon -= 1;
  4682. t.tm_isdst = -1;
  4683. if (mktime(&t) > 0)
  4684. timestamp = mktime(&t);
  4685. }
  4686. void CLASS parse_exif (int base)
  4687. {
  4688. unsigned kodak, entries, tag, type, len, save, c;
  4689. double expo;
  4690. kodak = !strncmp(make,"EASTMAN",7) && tiff_nifds < 3;
  4691. entries = get2();
  4692. while (entries--) {
  4693. tiff_get (base, &tag, &type, &len, &save);
  4694. switch (tag) {
  4695. case 33434: shutter = getreal(type); break;
  4696. case 33437: aperture = getreal(type); break;
  4697. case 34855: iso_speed = get2(); break;
  4698. case 36867:
  4699. case 36868: get_timestamp(0); break;
  4700. case 37377: if ((expo = -getreal(type)) < 128)
  4701. shutter = pow (2.0, expo); break;
  4702. case 37378: aperture = pow (2.0, getreal(type)/2); break;
  4703. case 37386: focal_len = getreal(type); break;
  4704. case 37500: parse_makernote (base, 0); break;
  4705. case 40962: if (kodak) raw_width = get4(); break;
  4706. case 40963: if (kodak) raw_height = get4(); break;
  4707. case 41730:
  4708. if (get4() == 0x20002)
  4709. for (exif_cfa=c=0; c < 8; c+=2)
  4710. exif_cfa |= fgetc(ifp) * 0x01010101 << c;
  4711. }
  4712. fseek (ifp, save, SEEK_SET);
  4713. }
  4714. }
  4715. void CLASS parse_gps (int base)
  4716. {
  4717. unsigned entries, tag, type, len, save, c;
  4718. entries = get2();
  4719. while (entries--) {
  4720. tiff_get (base, &tag, &type, &len, &save);
  4721. switch (tag) {
  4722. case 1: case 3: case 5:
  4723. gpsdata[29+tag/2] = getc(ifp); break;
  4724. case 2: case 4: case 7:
  4725. FORC(6) gpsdata[tag/3*6+c] = get4(); break;
  4726. case 6:
  4727. FORC(2) gpsdata[18+c] = get4(); break;
  4728. case 18: case 29:
  4729. fgets ((char *) (gpsdata+14+tag/3), MIN(len,12), ifp);
  4730. }
  4731. fseek (ifp, save, SEEK_SET);
  4732. }
  4733. }
  4734. void CLASS romm_coeff (float romm_cam[3][3])
  4735. {
  4736. static const float rgb_romm[3][3] = /* ROMM == Kodak ProPhoto */
  4737. { { 2.034193, -0.727420, -0.306766 },
  4738. { -0.228811, 1.231729, -0.002922 },
  4739. { -0.008565, -0.153273, 1.161839 } };
  4740. int i, j, k;
  4741. for (i=0; i < 3; i++)
  4742. for (j=0; j < 3; j++)
  4743. for (cmatrix[i][j] = k=0; k < 3; k++)
  4744. cmatrix[i][j] += rgb_romm[i][k] * romm_cam[k][j];
  4745. #ifdef LIBRAW_LIBRARY_BUILD
  4746. color_flags.cmatrix_state = LIBRAW_COLORSTATE_CALCULATED;
  4747. #endif
  4748. }
  4749. void CLASS parse_mos (int offset)
  4750. {
  4751. char data[40];
  4752. int skip, from, i, c, neut[4], planes=0, frot=0;
  4753. static const char *mod[] =
  4754. { "","DCB2","Volare","Cantare","CMost","Valeo 6","Valeo 11","Valeo 22",
  4755. "Valeo 11p","Valeo 17","","Aptus 17","Aptus 22","Aptus 75","Aptus 65",
  4756. "Aptus 54S","Aptus 65S","Aptus 75S","AFi 5","AFi 6","AFi 7",
  4757. "AFi-II 7","","","AFi-II 6","","","AFi-II 10","AFi-II 5"
  4758. "","","","","","AFi-II 10R","AFi-II 8","","AFi-II 12"};
  4759. float romm_cam[3][3];
  4760. fseek (ifp, offset, SEEK_SET);
  4761. while (1) {
  4762. if (get4() != 0x504b5453) break;
  4763. get4();
  4764. fread (data, 1, 40, ifp);
  4765. skip = get4();
  4766. from = ftell(ifp);
  4767. if (!strcmp(data,"JPEG_preview_data")) {
  4768. thumb_offset = from;
  4769. thumb_length = skip;
  4770. }
  4771. if (!strcmp(data,"icc_camera_profile")) {
  4772. profile_offset = from;
  4773. profile_length = skip;
  4774. }
  4775. if (!strcmp(data,"ShootObj_back_type")) {
  4776. fscanf (ifp, "%d", &i);
  4777. if ((unsigned) i < sizeof mod / sizeof (*mod))
  4778. strcpy (model, mod[i]);
  4779. }
  4780. if (!strcmp(data,"icc_camera_to_tone_matrix")) {
  4781. for (i=0; i < 9; i++)
  4782. romm_cam[0][i] = int_to_float(get4());
  4783. romm_coeff (romm_cam);
  4784. }
  4785. if (!strcmp(data,"CaptProf_color_matrix")) {
  4786. for (i=0; i < 9; i++)
  4787. fscanf (ifp, "%f", &romm_cam[0][i]);
  4788. romm_coeff (romm_cam);
  4789. }
  4790. if (!strcmp(data,"CaptProf_number_of_planes"))
  4791. fscanf (ifp, "%d", &planes);
  4792. if (!strcmp(data,"CaptProf_raw_data_rotation"))
  4793. fscanf (ifp, "%d", &flip);
  4794. if (!strcmp(data,"CaptProf_mosaic_pattern"))
  4795. FORC4 {
  4796. fscanf (ifp, "%d", &i);
  4797. if (i == 1) frot = c ^ (c >> 1);
  4798. }
  4799. if (!strcmp(data,"ImgProf_rotation_angle")) {
  4800. fscanf (ifp, "%d", &i);
  4801. flip = i - flip;
  4802. }
  4803. if (!strcmp(data,"NeutObj_neutrals") && !cam_mul[0]) {
  4804. FORC4 fscanf (ifp, "%d", neut+c);
  4805. FORC3 cam_mul[c] = (float) neut[0] / neut[c+1];
  4806. #ifdef LIBRAW_LIBRARY_BUILD
  4807. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4808. #endif
  4809. }
  4810. if (!strcmp(data,"Rows_data"))
  4811. load_flags = get4();
  4812. parse_mos (from);
  4813. fseek (ifp, skip+from, SEEK_SET);
  4814. }
  4815. if (planes)
  4816. filters = (planes == 1) * 0x01010101 *
  4817. (uchar) "\x94\x61\x16\x49"[(flip/90 + frot) & 3];
  4818. }
  4819. void CLASS linear_table (unsigned len)
  4820. {
  4821. int i;
  4822. if (len > 0x1000) len = 0x1000;
  4823. read_shorts (curve, len);
  4824. #ifdef LIBRAW_LIBRARY_BUILD
  4825. color_flags.curve_state = LIBRAW_COLORSTATE_LOADED;
  4826. #endif
  4827. for (i=len; i < 0x1000; i++)
  4828. curve[i] = curve[i-1];
  4829. maximum = curve[0xfff];
  4830. }
  4831. void CLASS parse_kodak_ifd (int base)
  4832. {
  4833. unsigned entries, tag, type, len, save;
  4834. int i, c, wbi=-2, wbtemp=6500;
  4835. float mul[3]={1,1,1}, num;
  4836. static const int wbtag[] = { 64037,64040,64039,64041,-1,-1,64042 };
  4837. entries = get2();
  4838. if (entries > 1024) return;
  4839. while (entries--) {
  4840. tiff_get (base, &tag, &type, &len, &save);
  4841. if (tag == 1020) wbi = getint(type);
  4842. if (tag == 1021 && len == 72) { /* WB set in software */
  4843. fseek (ifp, 40, SEEK_CUR);
  4844. FORC3 cam_mul[c] = 2048.0 / get2();
  4845. #ifdef LIBRAW_LIBRARY_BUILD
  4846. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4847. #endif
  4848. wbi = -2;
  4849. }
  4850. if (tag == 2118) wbtemp = getint(type);
  4851. if (tag == 2130 + wbi)
  4852. FORC3 mul[c] = getreal(type);
  4853. if (tag == 2140 + wbi && wbi >= 0)
  4854. {
  4855. FORC3 {
  4856. for (num=i=0; i < 4; i++)
  4857. num += getreal(type) * pow (wbtemp/100.0, i);
  4858. cam_mul[c] = 2048 / (num * mul[c]);
  4859. }
  4860. #ifdef LIBRAW_LIBRARY_BUILD
  4861. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4862. #endif
  4863. }
  4864. if (tag == 2317) linear_table (len);
  4865. if (tag == 6020) iso_speed = getint(type);
  4866. if (tag == 64013) wbi = fgetc(ifp);
  4867. if ((unsigned) wbi < 7 && tag == wbtag[wbi])
  4868. FORC3 cam_mul[c] = get4();
  4869. if (tag == 64019) width = getint(type);
  4870. if (tag == 64020) height = (getint(type)+1) & -2;
  4871. fseek (ifp, save, SEEK_SET);
  4872. }
  4873. }
  4874. int CLASS parse_tiff_ifd (int base)
  4875. {
  4876. unsigned entries, tag, type, len, plen=16, save;
  4877. int ifd, use_cm=0, cfa, i, j, c, ima_len=0;
  4878. int blrr=1, blrc=1, dblack[] = { 0,0,0,0 };
  4879. char software[64], *cbuf, *cp;
  4880. uchar cfa_pat[16], cfa_pc[] = { 0,1,2,3 }, tab[256];
  4881. double cc[4][4], cm[4][3], cam_xyz[4][3], num;
  4882. double ab[]={ 1,1,1,1 }, asn[] = { 0,0,0,0 }, xyz[] = { 1,1,1 };
  4883. unsigned sony_curve[] = { 0,0,0,0,0,4095 };
  4884. unsigned *buf, sony_offset=0, sony_length=0, sony_key=0;
  4885. struct jhead jh;
  4886. #ifndef LIBRAW_LIBRARY_BUILD
  4887. FILE *sfp;
  4888. #endif
  4889. if (tiff_nifds >= sizeof tiff_ifd / sizeof tiff_ifd[0])
  4890. return 1;
  4891. ifd = tiff_nifds++;
  4892. for (j=0; j < 4; j++)
  4893. for (i=0; i < 4; i++)
  4894. cc[j][i] = i == j;
  4895. entries = get2();
  4896. if (entries > 512) return 1;
  4897. while (entries--) {
  4898. tiff_get (base, &tag, &type, &len, &save);
  4899. switch (tag) {
  4900. case 5: width = get2(); break;
  4901. case 6: height = get2(); break;
  4902. case 7: width += get2(); break;
  4903. case 9: filters = get2(); break;
  4904. case 17: case 18:
  4905. if (type == 3 && len == 1)
  4906. {
  4907. cam_mul[(tag-17)*2] = get2() / 256.0;
  4908. #ifdef LIBRAW_LIBRARY_BUILD
  4909. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4910. #endif
  4911. }
  4912. break;
  4913. case 23:
  4914. if (type == 3) iso_speed = get2();
  4915. break;
  4916. case 36: case 37: case 38:
  4917. cam_mul[tag-0x24] = get2();
  4918. break;
  4919. case 39:
  4920. if (len < 50 || cam_mul[0]) break;
  4921. fseek (ifp, 12, SEEK_CUR);
  4922. FORC3 cam_mul[c] = get2();
  4923. #ifdef LIBRAW_LIBRARY_BUILD
  4924. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4925. #endif
  4926. break;
  4927. case 46:
  4928. if (type != 7 || fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8) break;
  4929. thumb_offset = ftell(ifp) - 2;
  4930. thumb_length = len;
  4931. break;
  4932. case 61440: /* Fuji HS10 table */
  4933. parse_tiff_ifd (base);
  4934. break;
  4935. case 2: case 256: case 61441: /* ImageWidth */
  4936. tiff_ifd[ifd].t_width = getint(type);
  4937. break;
  4938. case 3: case 257: case 61442: /* ImageHeight */
  4939. tiff_ifd[ifd].t_height = getint(type);
  4940. break;
  4941. case 258: /* BitsPerSample */
  4942. case 61443:
  4943. tiff_ifd[ifd].samples = len & 7;
  4944. tiff_ifd[ifd].bps = getint(type);
  4945. break;
  4946. case 61446:
  4947. raw_height = 0;
  4948. load_raw = &CLASS packed_load_raw;
  4949. load_flags = get4() && (filters=0x16161616) ? 24:80;
  4950. break;
  4951. case 259: /* Compression */
  4952. tiff_ifd[ifd].comp = get2();
  4953. break;
  4954. case 262: /* PhotometricInterpretation */
  4955. tiff_ifd[ifd].phint = get2();
  4956. break;
  4957. case 270: /* ImageDescription */
  4958. fread (desc, 512, 1, ifp);
  4959. break;
  4960. case 271: /* Make */
  4961. fgets (make, 64, ifp);
  4962. break;
  4963. case 272: /* Model */
  4964. fgets (model, 64, ifp);
  4965. break;
  4966. case 280: /* Panasonic RW2 offset */
  4967. if (type != 4) break;
  4968. load_raw = &CLASS panasonic_load_raw;
  4969. load_flags = 0x2008;
  4970. case 273: /* StripOffset */
  4971. case 513: /* JpegIFOffset */
  4972. case 61447:
  4973. tiff_ifd[ifd].offset = get4()+base;
  4974. if (!tiff_ifd[ifd].bps && tiff_ifd[ifd].offset > 0) {
  4975. fseek (ifp, tiff_ifd[ifd].offset, SEEK_SET);
  4976. if (ljpeg_start (&jh, 1)) {
  4977. tiff_ifd[ifd].comp = 6;
  4978. tiff_ifd[ifd].t_width = jh.wide;
  4979. tiff_ifd[ifd].t_height = jh.high;
  4980. tiff_ifd[ifd].bps = jh.bits;
  4981. tiff_ifd[ifd].samples = jh.clrs;
  4982. if (!(jh.sraw || (jh.clrs & 1)))
  4983. tiff_ifd[ifd].t_width *= jh.clrs;
  4984. i = order;
  4985. parse_tiff (tiff_ifd[ifd].offset + 12);
  4986. order = i;
  4987. }
  4988. }
  4989. break;
  4990. case 274: /* Orientation */
  4991. tiff_ifd[ifd].t_flip = "50132467"[get2() & 7]-'0';
  4992. break;
  4993. case 277: /* SamplesPerPixel */
  4994. tiff_ifd[ifd].samples = getint(type) & 7;
  4995. break;
  4996. case 279: /* StripByteCounts */
  4997. case 514:
  4998. case 61448:
  4999. tiff_ifd[ifd].bytes = get4();
  5000. break;
  5001. case 61454:
  5002. FORC3 cam_mul[(4-c) % 3] = getint(type);
  5003. break;
  5004. case 305: case 11: /* Software */
  5005. fgets (software, 64, ifp);
  5006. if (!strncmp(software,"Adobe",5) ||
  5007. !strncmp(software,"dcraw",5) ||
  5008. !strncmp(software,"UFRaw",5) ||
  5009. !strncmp(software,"Bibble",6) ||
  5010. !strncmp(software,"Nikon Scan",10) ||
  5011. !strcmp (software,"Digital Photo Professional"))
  5012. is_raw = 0;
  5013. break;
  5014. case 306: /* DateTime */
  5015. get_timestamp(0);
  5016. break;
  5017. case 315: /* Artist */
  5018. fread (artist, 64, 1, ifp);
  5019. break;
  5020. case 322: /* TileWidth */
  5021. tile_width = getint(type);
  5022. break;
  5023. case 323: /* TileLength */
  5024. tile_length = getint(type);
  5025. break;
  5026. case 324: /* TileOffsets */
  5027. tiff_ifd[ifd].offset = len > 1 ? ftell(ifp) : get4();
  5028. if (len == 4) {
  5029. load_raw = &CLASS sinar_4shot_load_raw;
  5030. is_raw = 5;
  5031. }
  5032. break;
  5033. #ifdef LIBRAW_LIBRARY_BUILD
  5034. case 325: /* TileByteCount */
  5035. tiff_ifd[ifd].tile_maxbytes = 0;
  5036. for(int jj=0;jj<len;jj++)
  5037. {
  5038. int s = get4();
  5039. if(s > tiff_ifd[ifd].tile_maxbytes) tiff_ifd[ifd].tile_maxbytes=s;
  5040. }
  5041. break;
  5042. #endif
  5043. case 330: /* SubIFDs */
  5044. if (!strcmp(model,"DSLR-A100") && tiff_ifd[ifd].t_width == 3872) {
  5045. load_raw = &CLASS sony_arw_load_raw;
  5046. data_offset = get4()+base;
  5047. ifd++; break;
  5048. }
  5049. while (len--) {
  5050. i = ftell(ifp);
  5051. fseek (ifp, get4()+base, SEEK_SET);
  5052. if (parse_tiff_ifd (base)) break;
  5053. fseek (ifp, i+4, SEEK_SET);
  5054. }
  5055. break;
  5056. case 400:
  5057. strcpy (make, "Sarnoff");
  5058. maximum = 0xfff;
  5059. break;
  5060. case 28688:
  5061. FORC4 sony_curve[c+1] = get2() >> 2 & 0xfff;
  5062. for (i=0; i < 5; i++)
  5063. for (j = sony_curve[i]+1; j <= sony_curve[i+1]; j++)
  5064. curve[j] = curve[j-1] + (1 << i);
  5065. #ifdef LIBRAW_LIBRARY_BUILD
  5066. color_flags.curve_state = LIBRAW_COLORSTATE_LOADED;
  5067. #endif
  5068. break;
  5069. case 29184: sony_offset = get4(); break;
  5070. case 29185: sony_length = get4(); break;
  5071. case 29217: sony_key = get4(); break;
  5072. case 29264:
  5073. parse_minolta (ftell(ifp));
  5074. raw_width = 0;
  5075. break;
  5076. case 29443:
  5077. FORC4 cam_mul[c ^ (c < 2)] = get2();
  5078. #ifdef LIBRAW_LIBRARY_BUILD
  5079. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5080. #endif
  5081. break;
  5082. case 29459:
  5083. FORC4 cam_mul[c] = get2();
  5084. i = (cam_mul[1] == 1024 && cam_mul[2] == 1024) << 1;
  5085. SWAP (cam_mul[i],cam_mul[i+1])
  5086. #ifdef LIBRAW_LIBRARY_BUILD
  5087. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5088. #endif
  5089. break;
  5090. case 33405: /* Model2 */
  5091. fgets (model2, 64, ifp);
  5092. break;
  5093. case 33422: /* CFAPattern */
  5094. case 64777: /* Kodak P-series */
  5095. if ((plen=len) > 16) plen = 16;
  5096. fread (cfa_pat, 1, plen, ifp);
  5097. for (colors=cfa=i=0; i < plen; i++) {
  5098. colors += !(cfa & (1 << cfa_pat[i]));
  5099. cfa |= 1 << cfa_pat[i];
  5100. }
  5101. if (cfa == 070) memcpy (cfa_pc,"\003\004\005",3); /* CMY */
  5102. if (cfa == 072) memcpy (cfa_pc,"\005\003\004\001",4); /* GMCY */
  5103. goto guess_cfa_pc;
  5104. case 33424:
  5105. case 65024:
  5106. fseek (ifp, get4()+base, SEEK_SET);
  5107. parse_kodak_ifd (base);
  5108. break;
  5109. case 33434: /* ExposureTime */
  5110. shutter = getreal(type);
  5111. break;
  5112. case 33437: /* FNumber */
  5113. aperture = getreal(type);
  5114. break;
  5115. case 34306: /* Leaf white balance */
  5116. FORC4 cam_mul[c ^ 1] = 4096.0 / get2();
  5117. #ifdef LIBRAW_LIBRARY_BUILD
  5118. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5119. #endif
  5120. break;
  5121. case 34307: /* Leaf CatchLight color matrix */
  5122. fread (software, 1, 7, ifp);
  5123. if (strncmp(software,"MATRIX",6)) break;
  5124. colors = 4;
  5125. for (raw_color = i=0; i < 3; i++) {
  5126. FORC4 fscanf (ifp, "%f", &rgb_cam[i][c^1]);
  5127. if (!use_camera_wb) continue;
  5128. num = 0;
  5129. FORC4 num += rgb_cam[i][c];
  5130. FORC4 rgb_cam[i][c] /= num;
  5131. #ifdef LIBRAW_LIBRARY_BUILD
  5132. color_flags.rgb_cam_state = LIBRAW_COLORSTATE_LOADED;
  5133. #endif
  5134. }
  5135. break;
  5136. case 34310: /* Leaf metadata */
  5137. parse_mos (ftell(ifp));
  5138. case 34303:
  5139. strcpy (make, "Leaf");
  5140. break;
  5141. case 34665: /* EXIF tag */
  5142. fseek (ifp, get4()+base, SEEK_SET);
  5143. parse_exif (base);
  5144. break;
  5145. case 34853: /* GPSInfo tag */
  5146. fseek (ifp, get4()+base, SEEK_SET);
  5147. parse_gps (base);
  5148. break;
  5149. case 34675: /* InterColorProfile */
  5150. case 50831: /* AsShotICCProfile */
  5151. profile_offset = ftell(ifp);
  5152. profile_length = len;
  5153. break;
  5154. case 37122: /* CompressedBitsPerPixel */
  5155. kodak_cbpp = get4();
  5156. break;
  5157. case 37386: /* FocalLength */
  5158. focal_len = getreal(type);
  5159. break;
  5160. case 37393: /* ImageNumber */
  5161. shot_order = getint(type);
  5162. break;
  5163. case 37400: /* old Kodak KDC tag */
  5164. for (raw_color = i=0; i < 3; i++) {
  5165. getreal(type);
  5166. FORC3 rgb_cam[i][c] = getreal(type);
  5167. }
  5168. #ifdef LIBRAW_LIBRARY_BUILD
  5169. color_flags.rgb_cam_state = LIBRAW_COLORSTATE_LOADED;
  5170. #endif
  5171. break;
  5172. case 46275: /* Imacon tags */
  5173. strcpy (make, "Imacon");
  5174. data_offset = ftell(ifp);
  5175. ima_len = len;
  5176. break;
  5177. case 46279:
  5178. if (!ima_len) break;
  5179. fseek (ifp, 78, SEEK_CUR);
  5180. raw_width = get4();
  5181. raw_height = get4();
  5182. left_margin = get4() & 7;
  5183. width = raw_width - left_margin - (get4() & 7);
  5184. top_margin = get4() & 7;
  5185. height = raw_height - top_margin - (get4() & 7);
  5186. if (raw_width == 7262) {
  5187. height = 5444;
  5188. width = 7244;
  5189. left_margin = 7;
  5190. }
  5191. fseek (ifp, 52, SEEK_CUR);
  5192. FORC3 cam_mul[c] = getreal(11);
  5193. #ifdef LIBRAW_LIBRARY_BUILD
  5194. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5195. #endif
  5196. fseek (ifp, 114, SEEK_CUR);
  5197. flip = (get2() >> 7) * 90;
  5198. if (width * height * 6 == ima_len) {
  5199. if (flip % 180 == 90) SWAP(width,height);
  5200. filters = flip = 0;
  5201. }
  5202. sprintf (model, "Ixpress %d-Mp", height*width/1000000);
  5203. load_raw = &CLASS imacon_full_load_raw;
  5204. if (filters) {
  5205. if (left_margin & 1) filters = 0x61616161;
  5206. load_raw = &CLASS unpacked_load_raw;
  5207. }
  5208. maximum = 0xffff;
  5209. break;
  5210. case 50454: /* Sinar tag */
  5211. case 50455:
  5212. if (!(cbuf = (char *) malloc(len))) break;
  5213. fread (cbuf, 1, len, ifp);
  5214. for (cp = cbuf-1; cp && cp < cbuf+len; cp = strchr(cp,'\n'))
  5215. if (!strncmp (++cp,"Neutral ",8))
  5216. {
  5217. sscanf (cp+8, "%f %f %f", cam_mul, cam_mul+1, cam_mul+2);
  5218. #ifdef LIBRAW_LIBRARY_BUILD
  5219. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5220. #endif
  5221. }
  5222. free (cbuf);
  5223. break;
  5224. case 50458:
  5225. if (!make[0]) strcpy (make, "Hasselblad");
  5226. break;
  5227. case 50459: /* Hasselblad tag */
  5228. i = order;
  5229. j = ftell(ifp);
  5230. c = tiff_nifds;
  5231. order = get2();
  5232. fseek (ifp, j+(get2(),get4()), SEEK_SET);
  5233. parse_tiff_ifd (j);
  5234. maximum = 0xffff;
  5235. tiff_nifds = c;
  5236. order = i;
  5237. break;
  5238. case 50706: /* DNGVersion */
  5239. FORC4 dng_version = (dng_version << 8) + fgetc(ifp);
  5240. if (!make[0]) strcpy (make, "DNG");
  5241. is_raw = 1;
  5242. break;
  5243. case 50710: /* CFAPlaneColor */
  5244. if (len > 4) len = 4;
  5245. colors = len;
  5246. fread (cfa_pc, 1, colors, ifp);
  5247. guess_cfa_pc:
  5248. FORCC tab[cfa_pc[c]] = c;
  5249. cdesc[c] = 0;
  5250. for (i=16; i--; )
  5251. filters = filters << 2 | tab[cfa_pat[i % plen]];
  5252. break;
  5253. case 50711: /* CFALayout */
  5254. if (get2() == 2) {
  5255. fuji_width = 1;
  5256. filters = 0x49494949;
  5257. }
  5258. break;
  5259. case 291:
  5260. case 50712: /* LinearizationTable */
  5261. linear_table (len);
  5262. break;
  5263. case 50713: /* BlackLevelRepeatDim */
  5264. blrr = get2();
  5265. blrc = get2();
  5266. break;
  5267. case 61450:
  5268. blrr = blrc = 2;
  5269. case 50714: /* BlackLevel */
  5270. black = getreal(type);
  5271. if (!filters || !~filters) break;
  5272. dblack[0] = black;
  5273. dblack[1] = (blrc == 2) ? getreal(type):dblack[0];
  5274. dblack[2] = (blrr == 2) ? getreal(type):dblack[0];
  5275. dblack[3] = (blrc == 2 && blrr == 2) ? getreal(type):dblack[1];
  5276. if (colors == 3)
  5277. filters |= ((filters >> 2 & 0x22222222) |
  5278. (filters << 2 & 0x88888888)) & filters << 1;
  5279. FORC4 cblack[filters >> (c << 1) & 3] = dblack[c];
  5280. black = 0;
  5281. break;
  5282. case 50715: /* BlackLevelDeltaH */
  5283. case 50716: /* BlackLevelDeltaV */
  5284. for (num=i=0; i < len; i++)
  5285. num += getreal(type);
  5286. black += num/len + 0.5;
  5287. break;
  5288. case 50717: /* WhiteLevel */
  5289. maximum = getint(type);
  5290. break;
  5291. case 50718: /* DefaultScale */
  5292. pixel_aspect = getreal(type);
  5293. pixel_aspect /= getreal(type);
  5294. break;
  5295. case 50721: /* ColorMatrix1 */
  5296. case 50722: /* ColorMatrix2 */
  5297. FORCC for (j=0; j < 3; j++)
  5298. cm[c][j] = getreal(type);
  5299. use_cm = 1;
  5300. break;
  5301. case 50723: /* CameraCalibration1 */
  5302. case 50724: /* CameraCalibration2 */
  5303. for (i=0; i < colors; i++)
  5304. FORCC cc[i][c] = getreal(type);
  5305. break;
  5306. case 50727: /* AnalogBalance */
  5307. FORCC ab[c] = getreal(type);
  5308. break;
  5309. case 50728: /* AsShotNeutral */
  5310. FORCC asn[c] = getreal(type);
  5311. break;
  5312. case 50729: /* AsShotWhiteXY */
  5313. xyz[0] = getreal(type);
  5314. xyz[1] = getreal(type);
  5315. xyz[2] = 1 - xyz[0] - xyz[1];
  5316. FORC3 xyz[c] /= d65_white[c];
  5317. break;
  5318. case 50740: /* DNGPrivateData */
  5319. if (dng_version) break;
  5320. parse_minolta (j = get4()+base);
  5321. fseek (ifp, j, SEEK_SET);
  5322. parse_tiff_ifd (base);
  5323. break;
  5324. case 50752:
  5325. read_shorts (cr2_slice, 3);
  5326. break;
  5327. case 50829: /* ActiveArea */
  5328. top_margin = getint(type);
  5329. left_margin = getint(type);
  5330. height = getint(type) - top_margin;
  5331. width = getint(type) - left_margin;
  5332. break;
  5333. case 64772: /* Kodak P-series */
  5334. if (len < 13) break;
  5335. fseek (ifp, 16, SEEK_CUR);
  5336. data_offset = get4();
  5337. fseek (ifp, 28, SEEK_CUR);
  5338. data_offset += get4();
  5339. load_raw = &CLASS packed_load_raw;
  5340. break;
  5341. case 65026:
  5342. if (type == 2) fgets (model2, 64, ifp);
  5343. }
  5344. fseek (ifp, save, SEEK_SET);
  5345. }
  5346. if (sony_length && (buf = (unsigned *) malloc(sony_length))) {
  5347. fseek (ifp, sony_offset, SEEK_SET);
  5348. fread (buf, sony_length, 1, ifp);
  5349. sony_decrypt (buf, sony_length/4, 1, sony_key);
  5350. #ifndef LIBRAW_LIBRARY_BUILD
  5351. sfp = ifp;
  5352. if ((ifp = tmpfile())) {
  5353. fwrite (buf, sony_length, 1, ifp);
  5354. fseek (ifp, 0, SEEK_SET);
  5355. parse_tiff_ifd (-sony_offset);
  5356. fclose (ifp);
  5357. }
  5358. ifp = sfp;
  5359. #else
  5360. if( !ifp->tempbuffer_open(buf,sony_length))
  5361. {
  5362. parse_tiff_ifd(-sony_offset);
  5363. ifp->tempbuffer_close();
  5364. }
  5365. #endif
  5366. free (buf);
  5367. }
  5368. for (i=0; i < colors; i++)
  5369. FORCC cc[i][c] *= ab[i];
  5370. if (use_cm) {
  5371. FORCC for (i=0; i < 3; i++)
  5372. for (cam_xyz[c][i]=j=0; j < colors; j++)
  5373. cam_xyz[c][i] += cc[c][j] * cm[j][i] * xyz[i];
  5374. cam_xyz_coeff (cam_xyz);
  5375. }
  5376. if (asn[0]) {
  5377. cam_mul[3] = 0;
  5378. FORCC cam_mul[c] = 1 / asn[c];
  5379. #ifdef LIBRAW_LIBRARY_BUILD
  5380. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5381. #endif
  5382. }
  5383. if (!use_cm)
  5384. {
  5385. FORCC pre_mul[c] /= cc[c][c];
  5386. #ifdef LIBRAW_LIBRARY_BUILD
  5387. color_flags.pre_mul_state = LIBRAW_COLORSTATE_LOADED;
  5388. #endif
  5389. }
  5390. return 0;
  5391. }
  5392. int CLASS parse_tiff (int base)
  5393. {
  5394. int doff;
  5395. fseek (ifp, base, SEEK_SET);
  5396. order = get2();
  5397. if (order != 0x4949 && order != 0x4d4d) return 0;
  5398. get2();
  5399. while ((doff = get4())) {
  5400. fseek (ifp, doff+base, SEEK_SET);
  5401. if (parse_tiff_ifd (base)) break;
  5402. }
  5403. return 1;
  5404. }
  5405. void CLASS apply_tiff()
  5406. {
  5407. int max_samp=0, raw=-1, thm=-1, i,max_bps=0;
  5408. struct jhead jh;
  5409. thumb_misc = 16;
  5410. if (thumb_offset) {
  5411. fseek (ifp, thumb_offset, SEEK_SET);
  5412. if (ljpeg_start (&jh, 1)) {
  5413. thumb_misc = jh.bits;
  5414. thumb_width = jh.wide;
  5415. thumb_height = jh.high;
  5416. }
  5417. }
  5418. for (i=0; i < tiff_nifds; i++) {
  5419. if (max_samp < tiff_ifd[i].samples)
  5420. max_samp = tiff_ifd[i].samples;
  5421. if (max_samp > 3) max_samp = 3;
  5422. if (max_bps < tiff_ifd[i].bps)
  5423. max_bps = tiff_ifd[i].bps;
  5424. if ((tiff_ifd[i].comp != 6 || tiff_ifd[i].samples != 3) &&
  5425. (tiff_ifd[i].t_width | tiff_ifd[i].t_height) < 0x10000 &&
  5426. tiff_ifd[i].t_width*tiff_ifd[i].t_height > raw_width*raw_height) {
  5427. raw_width = tiff_ifd[i].t_width;
  5428. raw_height = tiff_ifd[i].t_height;
  5429. tiff_bps = tiff_ifd[i].bps;
  5430. tiff_compress = tiff_ifd[i].comp;
  5431. data_offset = tiff_ifd[i].offset;
  5432. tiff_flip = tiff_ifd[i].t_flip;
  5433. tiff_samples = tiff_ifd[i].samples;
  5434. #ifdef LIBRAW_LIBRARY_BUILD
  5435. data_size = tile_length < INT_MAX ? tiff_ifd[i].tile_maxbytes: tiff_ifd[i].bytes;
  5436. #endif
  5437. raw = i;
  5438. }
  5439. }
  5440. for (i=tiff_nifds; i--; )
  5441. if (tiff_ifd[i].t_flip) tiff_flip = tiff_ifd[i].t_flip;
  5442. if (raw >= 0 && !load_raw)
  5443. switch (tiff_compress) {
  5444. case 0: case 1:
  5445. switch (tiff_bps) {
  5446. case 8: load_raw = &CLASS eight_bit_load_raw; break;
  5447. case 12: load_raw = &CLASS packed_load_raw;
  5448. if (tiff_ifd[raw].phint == 2)
  5449. load_flags = 6;
  5450. if (strncmp(make,"PENTAX",6)) break;
  5451. case 14:
  5452. case 16: load_raw = &CLASS unpacked_load_raw; break;
  5453. }
  5454. if (tiff_ifd[raw].bytes*5 == raw_width*raw_height*8) {
  5455. tiff_bps = 12;
  5456. load_raw = &CLASS packed_load_raw;
  5457. load_flags = 81;
  5458. }
  5459. break;
  5460. case 6: case 7: case 99:
  5461. load_raw = &CLASS lossless_jpeg_load_raw; break;
  5462. case 262:
  5463. load_raw = &CLASS kodak_262_load_raw; break;
  5464. case 32767:
  5465. if (tiff_ifd[raw].bytes == raw_width*raw_height) {
  5466. tiff_bps = 12;
  5467. load_raw = &CLASS sony_arw2_load_raw; break;
  5468. }
  5469. if (tiff_ifd[raw].bytes*8 != raw_width*raw_height*tiff_bps) {
  5470. raw_height += 8;
  5471. load_raw = &CLASS sony_arw_load_raw; break;
  5472. }
  5473. load_flags = 79;
  5474. case 32769:
  5475. load_flags++;
  5476. case 32770:
  5477. case 32773:
  5478. load_raw = &CLASS packed_load_raw; break;
  5479. case 34713:
  5480. load_raw = &CLASS nikon_compressed_load_raw; break;
  5481. case 65535:
  5482. load_raw = &CLASS pentax_load_raw; break;
  5483. case 65000:
  5484. switch (tiff_ifd[raw].phint) {
  5485. case 2: load_raw = &CLASS kodak_rgb_load_raw; filters = 0; break;
  5486. case 6: load_raw = &CLASS kodak_ycbcr_load_raw; filters = 0; break;
  5487. case 32803: load_raw = &CLASS kodak_65000_load_raw;
  5488. }
  5489. case 32867: break;
  5490. default: is_raw = 0;
  5491. }
  5492. if (!dng_version)
  5493. if ( (tiff_samples == 3 && tiff_ifd[raw].bytes &&
  5494. tiff_bps != 14 && tiff_bps != 2048)
  5495. || (tiff_bps == 8 && !strstr(make,"KODAK") && !strstr(make,"Kodak") &&
  5496. !strstr(model2,"DEBUG RAW")))
  5497. is_raw = 0;
  5498. if(dng_version && max_bps > 16)
  5499. is_raw = 0;
  5500. for (i=0; i < tiff_nifds; i++)
  5501. if (i != raw && tiff_ifd[i].samples == max_samp &&
  5502. tiff_ifd[i].t_width * tiff_ifd[i].t_height / SQR(tiff_ifd[i].bps+1) >
  5503. thumb_width * thumb_height / SQR(thumb_misc+1)) {
  5504. thumb_width = tiff_ifd[i].t_width;
  5505. thumb_height = tiff_ifd[i].t_height;
  5506. thumb_offset = tiff_ifd[i].offset;
  5507. thumb_length = tiff_ifd[i].bytes;
  5508. thumb_misc = tiff_ifd[i].bps;
  5509. thm = i;
  5510. }
  5511. if (thm >= 0) {
  5512. thumb_misc |= tiff_ifd[thm].samples << 5;
  5513. switch (tiff_ifd[thm].comp) {
  5514. case 0:
  5515. write_thumb = &CLASS layer_thumb;
  5516. break;
  5517. case 1:
  5518. if (tiff_ifd[thm].bps > 8)
  5519. thumb_load_raw = &CLASS kodak_thumb_load_raw;
  5520. else
  5521. write_thumb = &CLASS ppm_thumb;
  5522. break;
  5523. case 65000:
  5524. thumb_load_raw = tiff_ifd[thm].phint == 6 ?
  5525. &CLASS kodak_ycbcr_load_thumb : &CLASS kodak_rgb_load_thumb;
  5526. }
  5527. }
  5528. }
  5529. void CLASS parse_minolta (int base)
  5530. {
  5531. int save, tag, len, offset, high=0, wide=0, i, c;
  5532. short sorder=order;
  5533. fseek (ifp, base, SEEK_SET);
  5534. if (fgetc(ifp) || fgetc(ifp)-'M' || fgetc(ifp)-'R') return;
  5535. order = fgetc(ifp) * 0x101;
  5536. offset = base + get4() + 8;
  5537. while ((save=ftell(ifp)) < offset) {
  5538. for (tag=i=0; i < 4; i++)
  5539. tag = tag << 8 | fgetc(ifp);
  5540. len = get4();
  5541. switch (tag) {
  5542. case 0x505244: /* PRD */
  5543. fseek (ifp, 8, SEEK_CUR);
  5544. high = get2();
  5545. wide = get2();
  5546. break;
  5547. case 0x574247: /* WBG */
  5548. get4();
  5549. i = strcmp(model,"DiMAGE A200") ? 0:3;
  5550. FORC4 cam_mul[c ^ (c >> 1) ^ i] = get2();
  5551. #ifdef LIBRAW_LIBRARY_BUILD
  5552. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5553. #endif
  5554. break;
  5555. case 0x545457: /* TTW */
  5556. parse_tiff (ftell(ifp));
  5557. data_offset = offset;
  5558. }
  5559. fseek (ifp, save+len+8, SEEK_SET);
  5560. }
  5561. raw_height = high;
  5562. raw_width = wide;
  5563. order = sorder;
  5564. }
  5565. /*
  5566. Many cameras have a "debug mode" that writes JPEG and raw
  5567. at the same time. The raw file has no header, so try to
  5568. to open the matching JPEG file and read its metadata.
  5569. */
  5570. void CLASS parse_external_jpeg()
  5571. {
  5572. const char *file, *ext;
  5573. char *jname, *jfile, *jext;
  5574. #ifndef LIBRAW_LIBRARY_BUILD
  5575. FILE *save=ifp;
  5576. #else
  5577. if(!ifp->fname())
  5578. {
  5579. imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA ;
  5580. return;
  5581. }
  5582. #endif
  5583. ext = strrchr (ifname, '.');
  5584. file = strrchr (ifname, '/');
  5585. if (!file) file = strrchr (ifname, '\\');
  5586. #ifndef LIBRAW_LIBRARY_BUILD
  5587. if (!file) file = ifname-1;
  5588. #else
  5589. if (!file) file = (char*)ifname-1;
  5590. #endif
  5591. file++;
  5592. if (!ext || strlen(ext) != 4 || ext-file != 8) return;
  5593. jname = (char *) malloc (strlen(ifname) + 1);
  5594. merror (jname, "parse_external_jpeg()");
  5595. strcpy (jname, ifname);
  5596. jfile = file - ifname + jname;
  5597. jext = ext - ifname + jname;
  5598. if (strcasecmp (ext, ".jpg")) {
  5599. strcpy (jext, isupper(ext[1]) ? ".JPG":".jpg");
  5600. if (isdigit(*file)) {
  5601. memcpy (jfile, file+4, 4);
  5602. memcpy (jfile+4, file, 4);
  5603. }
  5604. } else
  5605. while (isdigit(*--jext)) {
  5606. if (*jext != '9') {
  5607. (*jext)++;
  5608. break;
  5609. }
  5610. *jext = '0';
  5611. }
  5612. #ifndef LIBRAW_LIBRARY_BUILD
  5613. if (strcmp (jname, ifname)) {
  5614. if ((ifp = fopen (jname, "rb"))) {
  5615. #ifdef DCRAW_VERBOSE
  5616. if (verbose)
  5617. fprintf (stderr,_("Reading metadata from %s ...\n"), jname);
  5618. #endif
  5619. parse_tiff (12);
  5620. thumb_offset = 0;
  5621. is_raw = 1;
  5622. fclose (ifp);
  5623. }
  5624. }
  5625. #else
  5626. if (strcmp (jname, ifname))
  5627. {
  5628. if(!ifp->subfile_open(jname))
  5629. {
  5630. parse_tiff (12);
  5631. thumb_offset = 0;
  5632. is_raw = 1;
  5633. ifp->subfile_close();
  5634. }
  5635. else
  5636. imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA ;
  5637. }
  5638. #endif
  5639. if (!timestamp)
  5640. {
  5641. #ifdef LIBRAW_LIBRARY_BUILD
  5642. imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA ;
  5643. #endif
  5644. #ifdef DCRAW_VERBOSE
  5645. fprintf (stderr,_("Failed to read metadata from %s\n"), jname);
  5646. #endif
  5647. }
  5648. free (jname);
  5649. #ifndef LIBRAW_LIBRARY_BUILD
  5650. ifp = save;
  5651. #endif
  5652. }
  5653. /*
  5654. CIFF block 0x1030 contains an 8x8 white sample.
  5655. Load this into white[][] for use in scale_colors().
  5656. */
  5657. void CLASS ciff_block_1030()
  5658. {
  5659. static const ushort key[] = { 0x410, 0x45f3 };
  5660. int i, bpp, row, col, vbits=0;
  5661. unsigned long bitbuf=0;
  5662. if ((get2(),get4()) != 0x80008 || !get4()) return;
  5663. bpp = get2();
  5664. if (bpp != 10 && bpp != 12) return;
  5665. for (i=row=0; row < 8; row++)
  5666. for (col=0; col < 8; col++) {
  5667. if (vbits < bpp) {
  5668. bitbuf = bitbuf << 16 | (get2() ^ key[i++ & 1]);
  5669. vbits += 16;
  5670. }
  5671. white[row][col] =
  5672. bitbuf << (LONG_BIT - vbits) >> (LONG_BIT - bpp);
  5673. vbits -= bpp;
  5674. }
  5675. }
  5676. /*
  5677. Parse a CIFF file, better known as Canon CRW format.
  5678. */
  5679. void CLASS parse_ciff (int offset, int length)
  5680. {
  5681. int tboff, nrecs, c, type, len, save, wbi=-1;
  5682. ushort key[] = { 0x410, 0x45f3 };
  5683. fseek (ifp, offset+length-4, SEEK_SET);
  5684. tboff = get4() + offset;
  5685. fseek (ifp, tboff, SEEK_SET);
  5686. nrecs = get2();
  5687. if (nrecs > 100) return;
  5688. while (nrecs--) {
  5689. type = get2();
  5690. len = get4();
  5691. save = ftell(ifp) + 4;
  5692. fseek (ifp, offset+get4(), SEEK_SET);
  5693. if ((((type >> 8) + 8) | 8) == 0x38)
  5694. parse_ciff (ftell(ifp), len); /* Parse a sub-table */
  5695. if (type == 0x0810)
  5696. fread (artist, 64, 1, ifp);
  5697. if (type == 0x080a) {
  5698. fread (make, 64, 1, ifp);
  5699. fseek (ifp, ((INT64)strlen(make)) - 63, SEEK_CUR);
  5700. fread (model, 64, 1, ifp);
  5701. }
  5702. if (type == 0x1810) {
  5703. fseek (ifp, 12, SEEK_CUR);
  5704. flip = get4();
  5705. }
  5706. if (type == 0x1835) /* Get the decoder table */
  5707. tiff_compress = get4();
  5708. if (type == 0x2007) {
  5709. thumb_offset = ftell(ifp);
  5710. thumb_length = len;
  5711. }
  5712. if (type == 0x1818) {
  5713. shutter = pow (2.0f, -int_to_float((get4(),get4())));
  5714. aperture = pow (2.0f, int_to_float(get4())/2);
  5715. }
  5716. if (type == 0x102a) {
  5717. iso_speed = pow (2.0, (get4(),get2())/32.0 - 4) * 50;
  5718. aperture = pow (2.0, (get2(),(short)get2())/64.0);
  5719. shutter = pow (2.0,-((short)get2())/32.0);
  5720. wbi = (get2(),get2());
  5721. if (wbi > 17) wbi = 0;
  5722. fseek (ifp, 32, SEEK_CUR);
  5723. if (shutter > 1e6) shutter = get2()/10.0;
  5724. }
  5725. if (type == 0x102c) {
  5726. if (get2() > 512) { /* Pro90, G1 */
  5727. fseek (ifp, 118, SEEK_CUR);
  5728. FORC4 cam_mul[c ^ 2] = get2();
  5729. #ifdef LIBRAW_LIBRARY_BUILD
  5730. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5731. #endif
  5732. } else { /* G2, S30, S40 */
  5733. fseek (ifp, 98, SEEK_CUR);
  5734. FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get2();
  5735. #ifdef LIBRAW_LIBRARY_BUILD
  5736. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5737. #endif
  5738. }
  5739. }
  5740. if (type == 0x0032) {
  5741. if (len == 768) { /* EOS D30 */
  5742. fseek (ifp, 72, SEEK_CUR);
  5743. FORC4 cam_mul[c ^ (c >> 1)] = 1024.0 / get2();
  5744. #ifdef LIBRAW_LIBRARY_BUILD
  5745. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5746. #endif
  5747. if (!wbi) cam_mul[0] = -1; /* use my auto white balance */
  5748. } else if (!cam_mul[0]) {
  5749. if (get2() == key[0]) /* Pro1, G6, S60, S70 */
  5750. c = (strstr(model,"Pro1") ?
  5751. "012346000000000000":"01345:000000006008")[wbi]-'0'+ 2;
  5752. else { /* G3, G5, S45, S50 */
  5753. c = "023457000000006000"[wbi]-'0';
  5754. key[0] = key[1] = 0;
  5755. }
  5756. fseek (ifp, 78 + c*8, SEEK_CUR);
  5757. FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get2() ^ key[c & 1];
  5758. #ifdef LIBRAW_LIBRARY_BUILD
  5759. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5760. #endif
  5761. if (!wbi) cam_mul[0] = -1;
  5762. }
  5763. }
  5764. if (type == 0x10a9) { /* D60, 10D, 300D, and clones */
  5765. if (len > 66) wbi = "0134567028"[wbi]-'0';
  5766. fseek (ifp, 2 + wbi*8, SEEK_CUR);
  5767. FORC4 cam_mul[c ^ (c >> 1)] = get2();
  5768. #ifdef LIBRAW_LIBRARY_BUILD
  5769. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5770. #endif
  5771. }
  5772. if (type == 0x1030 && (0x18040 >> wbi & 1))
  5773. ciff_block_1030(); /* all that don't have 0x10a9 */
  5774. if (type == 0x1031) {
  5775. raw_width = (get2(),get2());
  5776. raw_height = get2();
  5777. }
  5778. if (type == 0x5029) {
  5779. focal_len = len >> 16;
  5780. if ((len & 0xffff) == 2) focal_len /= 32;
  5781. }
  5782. if (type == 0x5813) flash_used = int_to_float(len);
  5783. if (type == 0x5814) canon_ev = int_to_float(len);
  5784. if (type == 0x5817) shot_order = len;
  5785. if (type == 0x5834) unique_id = len;
  5786. if (type == 0x580e) timestamp = len;
  5787. if (type == 0x180e) timestamp = get4();
  5788. #ifdef LOCALTIME
  5789. if ((type | 0x4000) == 0x580e)
  5790. timestamp = mktime (gmtime (&timestamp));
  5791. #endif
  5792. fseek (ifp, save, SEEK_SET);
  5793. }
  5794. }
  5795. void CLASS parse_rollei()
  5796. {
  5797. char line[128], *val;
  5798. struct tm t;
  5799. fseek (ifp, 0, SEEK_SET);
  5800. memset (&t, 0, sizeof t);
  5801. do {
  5802. fgets (line, 128, ifp);
  5803. if ((val = strchr(line,'=')))
  5804. *val++ = 0;
  5805. else
  5806. val = line + strlen(line);
  5807. if (!strcmp(line,"DAT"))
  5808. sscanf (val, "%d.%d.%d", &t.tm_mday, &t.tm_mon, &t.tm_year);
  5809. if (!strcmp(line,"TIM"))
  5810. sscanf (val, "%d:%d:%d", &t.tm_hour, &t.tm_min, &t.tm_sec);
  5811. if (!strcmp(line,"HDR"))
  5812. thumb_offset = atoi(val);
  5813. if (!strcmp(line,"X "))
  5814. raw_width = atoi(val);
  5815. if (!strcmp(line,"Y "))
  5816. raw_height = atoi(val);
  5817. if (!strcmp(line,"TX "))
  5818. thumb_width = atoi(val);
  5819. if (!strcmp(line,"TY "))
  5820. thumb_height = atoi(val);
  5821. } while (strncmp(line,"EOHD",4));
  5822. data_offset = thumb_offset + thumb_width * thumb_height * 2;
  5823. t.tm_year -= 1900;
  5824. t.tm_mon -= 1;
  5825. if (mktime(&t) > 0)
  5826. timestamp = mktime(&t);
  5827. strcpy (make, "Rollei");
  5828. strcpy (model,"d530flex");
  5829. write_thumb = &CLASS rollei_thumb;
  5830. }
  5831. void CLASS parse_sinar_ia()
  5832. {
  5833. int entries, off;
  5834. char str[8], *cp;
  5835. order = 0x4949;
  5836. fseek (ifp, 4, SEEK_SET);
  5837. entries = get4();
  5838. fseek (ifp, get4(), SEEK_SET);
  5839. while (entries--) {
  5840. off = get4(); get4();
  5841. fread (str, 8, 1, ifp);
  5842. if (!strcmp(str,"META")) meta_offset = off;
  5843. if (!strcmp(str,"THUMB")) thumb_offset = off;
  5844. if (!strcmp(str,"RAW0")) data_offset = off;
  5845. }
  5846. fseek (ifp, meta_offset+20, SEEK_SET);
  5847. fread (make, 64, 1, ifp);
  5848. make[63] = 0;
  5849. if ((cp = strchr(make,' '))) {
  5850. strcpy (model, cp+1);
  5851. *cp = 0;
  5852. }
  5853. raw_width = get2();
  5854. raw_height = get2();
  5855. load_raw = &CLASS unpacked_load_raw;
  5856. thumb_width = (get4(),get2());
  5857. thumb_height = get2();
  5858. write_thumb = &CLASS ppm_thumb;
  5859. maximum = 0x3fff;
  5860. }
  5861. void CLASS parse_phase_one (int base)
  5862. {
  5863. unsigned entries, tag, type, len, data, save, i, c;
  5864. float romm_cam[3][3];
  5865. char *cp;
  5866. memset (&ph1, 0, sizeof ph1);
  5867. fseek (ifp, base, SEEK_SET);
  5868. order = get4() & 0xffff;
  5869. if (get4() >> 8 != 0x526177) return; /* "Raw" */
  5870. fseek (ifp, get4()+base, SEEK_SET);
  5871. entries = get4();
  5872. get4();
  5873. while (entries--) {
  5874. tag = get4();
  5875. type = get4();
  5876. len = get4();
  5877. data = get4();
  5878. save = ftell(ifp);
  5879. fseek (ifp, base+data, SEEK_SET);
  5880. switch (tag) {
  5881. case 0x100: flip = "0653"[data & 3]-'0'; break;
  5882. case 0x106:
  5883. for (i=0; i < 9; i++)
  5884. romm_cam[0][i] = getreal(11);
  5885. romm_coeff (romm_cam);
  5886. break;
  5887. case 0x107:
  5888. FORC3 cam_mul[c] = getreal(11);
  5889. #ifdef LIBRAW_LIBRARY_BUILD
  5890. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5891. #endif
  5892. break;
  5893. case 0x108: raw_width = data; break;
  5894. case 0x109: raw_height = data; break;
  5895. case 0x10a: left_margin = data; break;
  5896. case 0x10b: top_margin = data; break;
  5897. case 0x10c: width = data; break;
  5898. case 0x10d: height = data; break;
  5899. case 0x10e: ph1.format = data; break;
  5900. case 0x10f: data_offset = data+base; break;
  5901. case 0x110: meta_offset = data+base;
  5902. meta_length = len; break;
  5903. case 0x112: ph1.key_off = save - 4; break;
  5904. case 0x210: ph1.tag_210 = int_to_float(data); break;
  5905. case 0x21a: ph1.tag_21a = data; break;
  5906. case 0x21c: strip_offset = data+base; break;
  5907. case 0x21d: ph1.t_black = data; break;
  5908. case 0x222: ph1.split_col = data - left_margin; break;
  5909. case 0x223: ph1.black_off = data+base; break;
  5910. case 0x301:
  5911. model[63] = 0;
  5912. fread (model, 1, 63, ifp);
  5913. if ((cp = strstr(model," camera"))) *cp = 0;
  5914. }
  5915. fseek (ifp, save, SEEK_SET);
  5916. }
  5917. load_raw = ph1.format < 3 ?
  5918. &CLASS phase_one_load_raw : &CLASS phase_one_load_raw_c;
  5919. maximum = 0xffff;
  5920. strcpy (make, "Phase One");
  5921. if (model[0]) return;
  5922. switch (raw_height) {
  5923. case 2060: strcpy (model,"LightPhase"); break;
  5924. case 2682: strcpy (model,"H 10"); break;
  5925. case 4128: strcpy (model,"H 20"); break;
  5926. case 5488: strcpy (model,"H 25"); break;
  5927. }
  5928. }
  5929. void CLASS parse_fuji (int offset)
  5930. {
  5931. unsigned entries, tag, len, save, c;
  5932. fseek (ifp, offset, SEEK_SET);
  5933. entries = get4();
  5934. if (entries > 255) return;
  5935. while (entries--) {
  5936. tag = get2();
  5937. len = get2();
  5938. save = ftell(ifp);
  5939. if (tag == 0x100) {
  5940. raw_height = get2();
  5941. raw_width = get2();
  5942. } else if (tag == 0x121) {
  5943. height = get2();
  5944. if ((width = get2()) == 4284) width += 3;
  5945. } else if (tag == 0x130) {
  5946. fuji_layout = fgetc(ifp) >> 7;
  5947. load_raw = fgetc(ifp) & 8 ?
  5948. &CLASS unpacked_load_raw : &CLASS fuji_load_raw;
  5949. } else if (tag == 0x2ff0) {
  5950. FORC4 cam_mul[c ^ 1] = get2();
  5951. #ifdef LIBRAW_LIBRARY_BUILD
  5952. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5953. #endif
  5954. } else if (tag == 0xc000) {
  5955. c = order;
  5956. order = 0x4949;
  5957. width = get4();
  5958. height = get4();
  5959. order = c;
  5960. }
  5961. fseek (ifp, save+len, SEEK_SET);
  5962. }
  5963. height <<= fuji_layout;
  5964. width >>= fuji_layout;
  5965. }
  5966. int CLASS parse_jpeg (int offset)
  5967. {
  5968. int len, save, hlen, mark;
  5969. fseek (ifp, offset, SEEK_SET);
  5970. if (fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8) return 0;
  5971. while (fgetc(ifp) == 0xff && (mark = fgetc(ifp)) != 0xda) {
  5972. order = 0x4d4d;
  5973. len = get2() - 2;
  5974. save = ftell(ifp);
  5975. if (mark == 0xc0 || mark == 0xc3) {
  5976. fgetc(ifp);
  5977. raw_height = get2();
  5978. raw_width = get2();
  5979. }
  5980. order = get2();
  5981. hlen = get4();
  5982. if (get4() == 0x48454150) /* "HEAP" */
  5983. parse_ciff (save+hlen, len-hlen);
  5984. if (parse_tiff (save+6)) apply_tiff();
  5985. fseek (ifp, save+len, SEEK_SET);
  5986. }
  5987. return 1;
  5988. }
  5989. void CLASS parse_riff()
  5990. {
  5991. unsigned i, size, end;
  5992. char tag[4], date[64], month[64];
  5993. static const char mon[12][4] =
  5994. { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
  5995. struct tm t;
  5996. order = 0x4949;
  5997. fread (tag, 4, 1, ifp);
  5998. size = get4();
  5999. end = ftell(ifp) + size;
  6000. if (!memcmp(tag,"RIFF",4) || !memcmp(tag,"LIST",4)) {
  6001. int cnt=0;
  6002. get4();
  6003. while (ftell(ifp)+7 < end)
  6004. {
  6005. parse_riff();
  6006. if(cnt++>10000) break; // no more than 10k times
  6007. }
  6008. } else if (!memcmp(tag,"nctg",4)) {
  6009. while (ftell(ifp)+7 < end) {
  6010. i = get2();
  6011. size = get2();
  6012. if ((i+1) >> 1 == 10 && size == 20)
  6013. get_timestamp(0);
  6014. else fseek (ifp, size, SEEK_CUR);
  6015. }
  6016. } else if (!memcmp(tag,"IDIT",4) && size < 64) {
  6017. fread (date, 64, 1, ifp);
  6018. date[size] = 0;
  6019. memset (&t, 0, sizeof t);
  6020. if (sscanf (date, "%*s %s %d %d:%d:%d %d", month, &t.tm_mday,
  6021. &t.tm_hour, &t.tm_min, &t.tm_sec, &t.tm_year) == 6) {
  6022. for (i=0; i < 12 && strcasecmp(mon[i],month); i++);
  6023. t.tm_mon = i;
  6024. t.tm_year -= 1900;
  6025. if (mktime(&t) > 0)
  6026. timestamp = mktime(&t);
  6027. }
  6028. } else
  6029. fseek (ifp, size, SEEK_CUR);
  6030. }
  6031. void CLASS parse_smal (int offset, int fsize)
  6032. {
  6033. int ver;
  6034. fseek (ifp, offset+2, SEEK_SET);
  6035. order = 0x4949;
  6036. ver = fgetc(ifp);
  6037. if (ver == 6)
  6038. fseek (ifp, 5, SEEK_CUR);
  6039. if (get4() != fsize) return;
  6040. if (ver > 6) data_offset = get4();
  6041. raw_height = height = get2();
  6042. raw_width = width = get2();
  6043. strcpy (make, "SMaL");
  6044. sprintf (model, "v%d %dx%d", ver, width, height);
  6045. if (ver == 6) load_raw = &CLASS smal_v6_load_raw;
  6046. if (ver == 9) load_raw = &CLASS smal_v9_load_raw;
  6047. }
  6048. void CLASS parse_cine()
  6049. {
  6050. unsigned off_head, off_setup, off_image, i;
  6051. order = 0x4949;
  6052. fseek (ifp, 4, SEEK_SET);
  6053. is_raw = get2() == 2;
  6054. fseek (ifp, 14, SEEK_CUR);
  6055. is_raw *= get4();
  6056. off_head = get4();
  6057. off_setup = get4();
  6058. off_image = get4();
  6059. timestamp = get4();
  6060. if ((i = get4())) timestamp = i;
  6061. fseek (ifp, off_head+4, SEEK_SET);
  6062. raw_width = get4();
  6063. raw_height = get4();
  6064. switch (get2(),get2()) {
  6065. case 8: load_raw = &CLASS eight_bit_load_raw; break;
  6066. case 16: load_raw = &CLASS unpacked_load_raw;
  6067. }
  6068. fseek (ifp, off_setup+792, SEEK_SET);
  6069. strcpy (make, "CINE");
  6070. sprintf (model, "%d", get4());
  6071. fseek (ifp, 12, SEEK_CUR);
  6072. switch ((i=get4()) & 0xffffff) {
  6073. case 3: filters = 0x94949494; break;
  6074. case 4: filters = 0x49494949; break;
  6075. default: is_raw = 0;
  6076. }
  6077. fseek (ifp, 72, SEEK_CUR);
  6078. switch ((get4()+3600) % 360) {
  6079. case 270: flip = 4; break;
  6080. case 180: flip = 1; break;
  6081. case 90: flip = 7; break;
  6082. case 0: flip = 2;
  6083. }
  6084. cam_mul[0] = getreal(11);
  6085. cam_mul[2] = getreal(11);
  6086. #ifdef LIBRAW_LIBRARY_BUILD
  6087. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  6088. #endif
  6089. maximum = ~(-1 << get4());
  6090. fseek (ifp, 668, SEEK_CUR);
  6091. shutter = get4()/1000000000.0;
  6092. fseek (ifp, off_image, SEEK_SET);
  6093. if (shot_select < is_raw)
  6094. fseek (ifp, shot_select*8, SEEK_CUR);
  6095. data_offset = (INT64) get4() + 8;
  6096. data_offset += (INT64) get4() << 32;
  6097. }
  6098. void CLASS parse_redcine()
  6099. {
  6100. unsigned i, len, rdvo;
  6101. order = 0x4d4d;
  6102. is_raw = 0;
  6103. fseek (ifp, 52, SEEK_SET);
  6104. width = get4();
  6105. height = get4();
  6106. fseek (ifp, 0, SEEK_END);
  6107. fseek (ifp, -(i = ftello(ifp) & 511), SEEK_CUR);
  6108. if (get4() != i || get4() != 0x52454f42) {
  6109. #ifdef DCRAW_VERBOSE
  6110. fprintf (stderr,_("%s: Tail is missing, parsing from head...\n"), ifname);
  6111. #endif
  6112. fseek (ifp, 0, SEEK_SET);
  6113. while ((len = get4()) != EOF) {
  6114. if (get4() == 0x52454456)
  6115. if (is_raw++ == shot_select)
  6116. data_offset = ftello(ifp) - 8;
  6117. fseek (ifp, len-8, SEEK_CUR);
  6118. }
  6119. } else {
  6120. rdvo = get4();
  6121. fseek (ifp, 12, SEEK_CUR);
  6122. is_raw = get4();
  6123. fseeko (ifp, rdvo+8 + shot_select*4, SEEK_SET);
  6124. data_offset = get4();
  6125. }
  6126. }
  6127. void CLASS adobe_coeff (const char *p_make, const char *p_model)
  6128. {
  6129. static const struct {
  6130. const char *prefix;
  6131. int t_black, t_maximum, trans[12];
  6132. } table[] = {
  6133. { "AGFAPHOTO DC-833m", 0, 0, /* DJC */
  6134. { 11438,-3762,-1115,-2409,9914,2497,-1227,2295,5300 } },
  6135. { "Apple QuickTake", 0, 0, /* DJC */
  6136. { 21392,-5653,-3353,2406,8010,-415,7166,1427,2078 } },
  6137. { "Canon EOS D2000", 0, 0,
  6138. { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
  6139. { "Canon EOS D6000", 0, 0,
  6140. { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
  6141. { "Canon EOS D30", 0, 0,
  6142. { 9805,-2689,-1312,-5803,13064,3068,-2438,3075,8775 } },
  6143. { "Canon EOS D60", 0, 0xfa0,
  6144. { 6188,-1341,-890,-7168,14489,2937,-2640,3228,8483 } },
  6145. { "Canon EOS 5D Mark II", 0, 0x3cf0,
  6146. { 4716,603,-830,-7798,15474,2480,-1496,1937,6651 } },
  6147. { "Canon EOS 5D", 0, 0xe6c,
  6148. { 6347,-479,-972,-8297,15954,2480,-1968,2131,7649 } },
  6149. { "Canon EOS 7D", 0, 0x3510,
  6150. { 6844,-996,-856,-3876,11761,2396,-593,1772,6198 } },
  6151. { "Canon EOS 10D", 0, 0xfa0,
  6152. { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
  6153. { "Canon EOS 20Da", 0, 0,
  6154. { 14155,-5065,-1382,-6550,14633,2039,-1623,1824,6561 } },
  6155. { "Canon EOS 20D", 0, 0xfff,
  6156. { 6599,-537,-891,-8071,15783,2424,-1983,2234,7462 } },
  6157. { "Canon EOS 30D", 0, 0,
  6158. { 6257,-303,-1000,-7880,15621,2396,-1714,1904,7046 } },
  6159. { "Canon EOS 40D", 0, 0x3f60,
  6160. { 6071,-747,-856,-7653,15365,2441,-2025,2553,7315 } },
  6161. { "Canon EOS 50D", 0, 0x3d93,
  6162. { 4920,616,-593,-6493,13964,2784,-1774,3178,7005 } },
  6163. { "Canon EOS 60D", 0, 0x2ff7,
  6164. { 6719,-994,-925,-4408,12426,2211,-887,2129,6051 } },
  6165. { "Canon EOS 300D", 0, 0xfa0,
  6166. { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
  6167. { "Canon EOS 350D", 0, 0xfff,
  6168. { 6018,-617,-965,-8645,15881,2975,-1530,1719,7642 } },
  6169. { "Canon EOS 400D", 0, 0xe8e,
  6170. { 7054,-1501,-990,-8156,15544,2812,-1278,1414,7796 } },
  6171. { "Canon EOS 450D", 0, 0x390d,
  6172. { 5784,-262,-821,-7539,15064,2672,-1982,2681,7427 } },
  6173. { "Canon EOS 500D", 0, 0x3479,
  6174. { 4763,712,-646,-6821,14399,2640,-1921,3276,6561 } },
  6175. { "Canon EOS 550D", 0, 0x3dd7,
  6176. { 6941,-1164,-857,-3825,11597,2534,-416,1540,6039 } },
  6177. { "Canon EOS 600D", 0, 0x3510,
  6178. { 6461,-907,-882,-4300,12184,2378,-819,1944,5931 } },
  6179. { "Canon EOS 1000D", 0, 0xe43,
  6180. { 6771,-1139,-977,-7818,15123,2928,-1244,1437,7533 } },
  6181. { "Canon EOS 1100D", 0, 0x3510,
  6182. { 6444,-904,-893,-4563,12308,2535,-903,2016,6728 } },
  6183. { "Canon EOS-1Ds Mark III", 0, 0x3bb0,
  6184. { 5859,-211,-930,-8255,16017,2353,-1732,1887,7448 } },
  6185. { "Canon EOS-1Ds Mark II", 0, 0xe80,
  6186. { 6517,-602,-867,-8180,15926,2378,-1618,1771,7633 } },
  6187. { "Canon EOS-1D Mark IV", 0, 0x3bb0,
  6188. { 6014,-220,-795,-4109,12014,2361,-561,1824,5787 } },
  6189. { "Canon EOS-1D Mark III", 0, 0x3bb0,
  6190. { 6291,-540,-976,-8350,16145,2311,-1714,1858,7326 } },
  6191. { "Canon EOS-1D Mark II N", 0, 0xe80,
  6192. { 6240,-466,-822,-8180,15825,2500,-1801,1938,8042 } },
  6193. { "Canon EOS-1D Mark II", 0, 0xe80,
  6194. { 6264,-582,-724,-8312,15948,2504,-1744,1919,8664 } },
  6195. { "Canon EOS-1DS", 0, 0xe20,
  6196. { 4374,3631,-1743,-7520,15212,2472,-2892,3632,8161 } },
  6197. { "Canon EOS-1D", 0, 0xe20,
  6198. { 6806,-179,-1020,-8097,16415,1687,-3267,4236,7690 } },
  6199. { "Canon EOS", 0, 0,
  6200. { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
  6201. { "Canon PowerShot A530", 0, 0,
  6202. { 0 } }, /* don't want the A5 matrix */
  6203. { "Canon PowerShot A50", 0, 0,
  6204. { -5300,9846,1776,3436,684,3939,-5540,9879,6200,-1404,11175,217 } },
  6205. { "Canon PowerShot A5", 0, 0,
  6206. { -4801,9475,1952,2926,1611,4094,-5259,10164,5947,-1554,10883,547 } },
  6207. { "Canon PowerShot G10", 0, 0,
  6208. { 11093,-3906,-1028,-5047,12492,2879,-1003,1750,5561 } },
  6209. { "Canon PowerShot G11", 0, 0,
  6210. { 12177,-4817,-1069,-1612,9864,2049,-98,850,4471 } },
  6211. { "Canon PowerShot G12", 0, 0,
  6212. { 13244,-5501,-1248,-1508,9858,1935,-270,1083,4366 } },
  6213. { "Canon PowerShot G1", 0, 0,
  6214. { -4778,9467,2172,4743,-1141,4344,-5146,9908,6077,-1566,11051,557 } },
  6215. { "Canon PowerShot G2", 0, 0,
  6216. { 9087,-2693,-1049,-6715,14382,2537,-2291,2819,7790 } },
  6217. { "Canon PowerShot G3", 0, 0,
  6218. { 9212,-2781,-1073,-6573,14189,2605,-2300,2844,7664 } },
  6219. { "Canon PowerShot G5", 0, 0,
  6220. { 9757,-2872,-933,-5972,13861,2301,-1622,2328,7212 } },
  6221. { "Canon PowerShot G6", 0, 0,
  6222. { 9877,-3775,-871,-7613,14807,3072,-1448,1305,7485 } },
  6223. { "Canon PowerShot G9", 0, 0,
  6224. { 7368,-2141,-598,-5621,13254,2625,-1418,1696,5743 } },
  6225. { "Canon PowerShot Pro1", 0, 0,
  6226. { 10062,-3522,-999,-7643,15117,2730,-765,817,7323 } },
  6227. { "Canon PowerShot Pro70", 34, 0,
  6228. { -4155,9818,1529,3939,-25,4522,-5521,9870,6610,-2238,10873,1342 } },
  6229. { "Canon PowerShot Pro90", 0, 0,
  6230. { -4963,9896,2235,4642,-987,4294,-5162,10011,5859,-1770,11230,577 } },
  6231. { "Canon PowerShot S30", 0, 0,
  6232. { 10566,-3652,-1129,-6552,14662,2006,-2197,2581,7670 } },
  6233. { "Canon PowerShot S40", 0, 0,
  6234. { 8510,-2487,-940,-6869,14231,2900,-2318,2829,9013 } },
  6235. { "Canon PowerShot S45", 0, 0,
  6236. { 8163,-2333,-955,-6682,14174,2751,-2077,2597,8041 } },
  6237. { "Canon PowerShot S50", 0, 0,
  6238. { 8882,-2571,-863,-6348,14234,2288,-1516,2172,6569 } },
  6239. { "Canon PowerShot S60", 0, 0,
  6240. { 8795,-2482,-797,-7804,15403,2573,-1422,1996,7082 } },
  6241. { "Canon PowerShot S70", 0, 0,
  6242. { 9976,-3810,-832,-7115,14463,2906,-901,989,7889 } },
  6243. { "Canon PowerShot S90", 0, 0,
  6244. { 12374,-5016,-1049,-1677,9902,2078,-83,852,4683 } },
  6245. { "Canon PowerShot S95", 0, 0,
  6246. { 13440,-5896,-1279,-1236,9598,1931,-180,1001,4651 } },
  6247. { "Canon PowerShot S100", 0, 0,
  6248. { 7968,-2565,-636,-2873,10697,2513,180,667,4211 } },
  6249. { "Canon PowerShot A470", 0, 0, /* DJC */
  6250. { 12513,-4407,-1242,-2680,10276,2405,-878,2215,4734 } },
  6251. { "Canon PowerShot A610", 0, 0, /* DJC */
  6252. { 15591,-6402,-1592,-5365,13198,2168,-1300,1824,5075 } },
  6253. { "Canon PowerShot A620", 0, 0, /* DJC */
  6254. { 15265,-6193,-1558,-4125,12116,2010,-888,1639,5220 } },
  6255. { "Canon PowerShot A630", 0, 0, /* DJC */
  6256. { 14201,-5308,-1757,-6087,14472,1617,-2191,3105,5348 } },
  6257. { "Canon PowerShot A640", 0, 0, /* DJC */
  6258. { 13124,-5329,-1390,-3602,11658,1944,-1612,2863,4885 } },
  6259. { "Canon PowerShot A650", 0, 0, /* DJC */
  6260. { 9427,-3036,-959,-2581,10671,1911,-1039,1982,4430 } },
  6261. { "Canon PowerShot A720", 0, 0, /* DJC */
  6262. { 14573,-5482,-1546,-1266,9799,1468,-1040,1912,3810 } },
  6263. { "Canon PowerShot S2 IS", 0, 0, /* jlb */
  6264. { 14062,-5199,-1446,-4712,12470,2243,-1286,2028,4836 } }, /* jlb - copied from Powershot S3 IS */
  6265. { "Canon PowerShot S3 IS", 0, 0, /* DJC */
  6266. { 14062,-5199,-1446,-4712,12470,2243,-1286,2028,4836 } },
  6267. { "Canon PowerShot SX1 IS", 0, 0,
  6268. { 6578,-259,-502,-5974,13030,3309,-308,1058,4970 } },
  6269. { "Canon PowerShot SX110 IS", 0, 0, /* DJC */
  6270. { 14134,-5576,-1527,-1991,10719,1273,-1158,1929,3581 } },
  6271. { "CASIO EX-S20", 0, 0, /* DJC */
  6272. { 11634,-3924,-1128,-4968,12954,2015,-1588,2648,7206 } },
  6273. { "CASIO EX-Z750", 0, 0, /* DJC */
  6274. { 10819,-3873,-1099,-4903,13730,1175,-1755,3751,4632 } },
  6275. { "CASIO EX-Z10", 128, 0xfff, /* DJC */
  6276. { 9790,-3338,-603,-2321,10222,2099,-344,1273,4799 } },
  6277. { "CINE 650", 0, 0,
  6278. { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
  6279. { "CINE 660", 0, 0,
  6280. { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
  6281. { "CINE", 0, 0,
  6282. { 20183,-4295,-423,-3940,15330,3985,-280,4870,9800 } },
  6283. { "Contax N Digital", 0, 0xf1e,
  6284. { 7777,1285,-1053,-9280,16543,2916,-3677,5679,7060 } },
  6285. { "EPSON R-D1", 0, 0,
  6286. { 6827,-1878,-732,-8429,16012,2564,-704,592,7145 } },
  6287. { "FUJIFILM FinePix E550", 0, 0,
  6288. { 11044,-3888,-1120,-7248,15168,2208,-1531,2277,8069 } },
  6289. { "FUJIFILM FinePix E900", 0, 0,
  6290. { 9183,-2526,-1078,-7461,15071,2574,-2022,2440,8639 } },
  6291. { "FUJIFILM FinePix F8", 0, 0,
  6292. { 11044,-3888,-1120,-7248,15168,2208,-1531,2277,8069 } },
  6293. { "FUJIFILM FinePix F7", 0, 0,
  6294. { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
  6295. { "FUJIFILM FinePix S100FS", 514, 0,
  6296. { 11521,-4355,-1065,-6524,13767,3058,-1466,1984,6045 } },
  6297. { "FUJIFILM FinePix S200EXR", 512, 0x3fff,
  6298. { 11401,-4498,-1312,-5088,12751,2613,-838,1568,5941 } },
  6299. { "FUJIFILM FinePix S20Pro", 0, 0,
  6300. { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
  6301. { "FUJIFILM FinePix S2Pro", 128, 0,
  6302. { 12492,-4690,-1402,-7033,15423,1647,-1507,2111,7697 } },
  6303. { "FUJIFILM FinePix S3Pro", 0, 0,
  6304. { 11807,-4612,-1294,-8927,16968,1988,-2120,2741,8006 } },
  6305. { "FUJIFILM FinePix S5Pro", 0, 0,
  6306. { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
  6307. { "FUJIFILM FinePix S5000", 0, 0,
  6308. { 8754,-2732,-1019,-7204,15069,2276,-1702,2334,6982 } },
  6309. { "FUJIFILM FinePix S5100", 0, 0,
  6310. { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
  6311. { "FUJIFILM FinePix S5500", 0, 0,
  6312. { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
  6313. { "FUJIFILM FinePix S5200", 0, 0,
  6314. { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
  6315. { "FUJIFILM FinePix S5600", 0, 0,
  6316. { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
  6317. { "FUJIFILM FinePix S6", 0, 0,
  6318. { 12628,-4887,-1401,-6861,14996,1962,-2198,2782,7091 } },
  6319. { "FUJIFILM FinePix S7000", 0, 0,
  6320. { 10190,-3506,-1312,-7153,15051,2238,-2003,2399,7505 } },
  6321. { "FUJIFILM FinePix S9000", 0, 0,
  6322. { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
  6323. { "FUJIFILM FinePix S9500", 0, 0,
  6324. { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
  6325. { "FUJIFILM FinePix S9100", 0, 0,
  6326. { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
  6327. { "FUJIFILM FinePix S9600", 0, 0,
  6328. { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
  6329. { "FUJIFILM IS-1", 0, 0,
  6330. { 21461,-10807,-1441,-2332,10599,1999,289,875,7703 } },
  6331. { "FUJIFILM IS Pro", 0, 0,
  6332. { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
  6333. { "FUJIFILM FinePix HS10 HS11", 0, 0xf68,
  6334. { 12440,-3954,-1183,-1123,9674,1708,-83,1614,4086 } },
  6335. { "FUJIFILM FinePix HS20EXR", 0, 0,
  6336. { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
  6337. { "FUJIFILM FinePix F550EXR", 0, 0,
  6338. { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
  6339. { "FUJIFILM FinePix F600EXR", 0, 0,
  6340. { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
  6341. { "FUJIFILM FinePix X100", 0, 0,
  6342. { 12161,-4457,-1069,-5034,12874,2400,-795,1724,6904 } },
  6343. { "FUJIFILM X10", 0, 0,
  6344. { 13509,-6199,-1254,-4430,12733,1865,-331,1441,5022 } },
  6345. { "Imacon Ixpress", 0, 0, /* DJC */
  6346. { 7025,-1415,-704,-5188,13765,1424,-1248,2742,6038 } },
  6347. { "KODAK NC2000", 0, 0,
  6348. { 13891,-6055,-803,-465,9919,642,2121,82,1291 } },
  6349. { "Kodak DCS315C", 8, 0,
  6350. { 17523,-4827,-2510,756,8546,-137,6113,1649,2250 } },
  6351. { "Kodak DCS330C", 8, 0,
  6352. { 20620,-7572,-2801,-103,10073,-396,3551,-233,2220 } },
  6353. { "KODAK DCS420", 0, 0,
  6354. { 10868,-1852,-644,-1537,11083,484,2343,628,2216 } },
  6355. { "KODAK DCS460", 0, 0,
  6356. { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
  6357. { "KODAK EOSDCS1", 0, 0,
  6358. { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
  6359. { "KODAK EOSDCS3B", 0, 0,
  6360. { 9898,-2700,-940,-2478,12219,206,1985,634,1031 } },
  6361. { "Kodak DCS520C", 180, 0,
  6362. { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
  6363. { "Kodak DCS560C", 188, 0,
  6364. { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
  6365. { "Kodak DCS620C", 180, 0,
  6366. { 23617,-10175,-3149,-2054,11749,-272,2586,-489,3453 } },
  6367. { "Kodak DCS620X", 185, 0,
  6368. { 13095,-6231,154,12221,-21,-2137,895,4602,2258 } },
  6369. { "Kodak DCS660C", 214, 0,
  6370. { 18244,-6351,-2739,-791,11193,-521,3711,-129,2802 } },
  6371. { "Kodak DCS720X", 0, 0,
  6372. { 11775,-5884,950,9556,1846,-1286,-1019,6221,2728 } },
  6373. { "Kodak DCS760C", 0, 0,
  6374. { 16623,-6309,-1411,-4344,13923,323,2285,274,2926 } },
  6375. { "Kodak DCS Pro SLR", 0, 0,
  6376. { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
  6377. { "Kodak DCS Pro 14nx", 0, 0,
  6378. { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
  6379. { "Kodak DCS Pro 14", 0, 0,
  6380. { 7791,3128,-776,-8588,16458,2039,-2455,4006,6198 } },
  6381. { "Kodak ProBack645", 0, 0,
  6382. { 16414,-6060,-1470,-3555,13037,473,2545,122,4948 } },
  6383. { "Kodak ProBack", 0, 0,
  6384. { 21179,-8316,-2918,-915,11019,-165,3477,-180,4210 } },
  6385. { "KODAK P712", 0, 0,
  6386. { 9658,-3314,-823,-5163,12695,2768,-1342,1843,6044 } },
  6387. { "KODAK P850", 0, 0xf7c,
  6388. { 10511,-3836,-1102,-6946,14587,2558,-1481,1792,6246 } },
  6389. { "KODAK P880", 0, 0xfff,
  6390. { 12805,-4662,-1376,-7480,15267,2360,-1626,2194,7904 } },
  6391. { "KODAK EasyShare Z980", 0, 0,
  6392. { 11313,-3559,-1101,-3893,11891,2257,-1214,2398,4908 } },
  6393. { "KODAK EasyShare Z981", 0, 0,
  6394. { 12729,-4717,-1188,-1367,9187,2582,274,860,4411 } },
  6395. { "KODAK EasyShare Z990", 0, 0xfed,
  6396. { 11749,-4048,-1309,-1867,10572,1489,-138,1449,4522 } },
  6397. { "KODAK EASYSHARE Z1015", 0, 0xef1,
  6398. { 11265,-4286,-992,-4694,12343,2647,-1090,1523,5447 } },
  6399. { "Leaf CMost", 0, 0,
  6400. { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
  6401. { "Leaf Valeo 6", 0, 0,
  6402. { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
  6403. { "Leaf Aptus 54S", 0, 0,
  6404. { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
  6405. { "Leaf Aptus 65", 0, 0,
  6406. { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
  6407. { "Leaf Aptus 75", 0, 0,
  6408. { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
  6409. { "Leaf Aptus 22", 0, 0,
  6410. { 8236, 1746, -1314, -8251, 15953, 2428, -3673, 5786, 5770, } },
  6411. { "Leaf Aptus-II 5", 0, 0, // Mamiya 645 AFD
  6412. { 8236, 1746, -1314, -8251, 15953, 2428, -3673, 5786, 5770, } },
  6413. { "Leaf Aptus-II 6", 0, 0,
  6414. { 7914, 1414, -1190, -8777, 16582, 2280, -2811, 4605, 5562, } },
  6415. { "Leaf Aptus-II 7", 0, 0,
  6416. { 7914, 1414, -1190, -8777, 16582, 2280, -2811, 4605, 5562, } },
  6417. { "Leaf Aptus-II 8", 0, 0, // Hasselblad 500 Series
  6418. { 8236, 1746, -1314, -8251, 15953, 2428, -3673, 5786, 5770, } },
  6419. { "Leaf Aptus-II 10", 0, 0,
  6420. { 8236, 1746, -1314, -8251, 15953, 2428, -3673, 5786, 5770, } },
  6421. { "Leaf Aptus-II 10R", 0, 0,
  6422. { 8236, 1746, -1314, -8251, 15953, 2428, -3673, 5786, 5770, } },
  6423. { "Leaf Aptus-II 12", 0, 0,
  6424. { 8236, 1746, -1314, -8251, 15953, 2428, -3673, 5786, 5770, } },
  6425. { "Leaf", 0, 0,
  6426. { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
  6427. { "Mamiya ZD", 0, 0,
  6428. { 7645,2579,-1363,-8689,16717,2015,-3712,5941,5961 } },
  6429. { "Micron 2010", 110, 0, /* DJC */
  6430. { 16695,-3761,-2151,155,9682,163,3433,951,4904 } },
  6431. { "Minolta DiMAGE 5", 0, 0xf7d,
  6432. { 8983,-2942,-963,-6556,14476,2237,-2426,2887,8014 } },
  6433. { "Minolta DiMAGE 7Hi", 0, 0xf7d,
  6434. { 11368,-3894,-1242,-6521,14358,2339,-2475,3056,7285 } },
  6435. { "Minolta DiMAGE 7", 0, 0xf7d,
  6436. { 9144,-2777,-998,-6676,14556,2281,-2470,3019,7744 } },
  6437. { "Minolta DiMAGE A1", 0, 0xf8b,
  6438. { 9274,-2547,-1167,-8220,16323,1943,-2273,2720,8340 } },
  6439. { "MINOLTA DiMAGE A200", 0, 0,
  6440. { 8560,-2487,-986,-8112,15535,2771,-1209,1324,7743 } },
  6441. { "Minolta DiMAGE A2", 0, 0xf8f,
  6442. { 9097,-2726,-1053,-8073,15506,2762,-966,981,7763 } },
  6443. { "Minolta DiMAGE Z2", 0, 0, /* DJC */
  6444. { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
  6445. { "MINOLTA DYNAX 5", 0, 0xffb,
  6446. { 10284,-3283,-1086,-7957,15762,2316,-829,882,6644 } },
  6447. { "MINOLTA DYNAX 7", 0, 0xffb,
  6448. { 10239,-3104,-1099,-8037,15727,2451,-927,925,6871 } },
  6449. { "MOTOROLA PIXL", 0, 0, /* DJC */
  6450. { 8898,-989,-1033,-3292,11619,1674,-661,3178,5216 } },
  6451. { "NIKON D100", 0, 0,
  6452. { 5902,-933,-782,-8983,16719,2354,-1402,1455,6464 } },
  6453. { "NIKON D1H", 0, 0,
  6454. { 7577,-2166,-926,-7454,15592,1934,-2377,2808,8606 } },
  6455. { "NIKON D1X", 0, 0,
  6456. { 7702,-2245,-975,-9114,17242,1875,-2679,3055,8521 } },
  6457. { "NIKON D1", 0, 0, /* multiplied by 2.218750, 1.0, 1.148438 */
  6458. { 16772,-4726,-2141,-7611,15713,1972,-2846,3494,9521 } },
  6459. { "NIKON D200", 0, 0xfbc,
  6460. { 8367,-2248,-763,-8758,16447,2422,-1527,1550,8053 } },
  6461. { "NIKON D2H", 0, 0,
  6462. { 5710,-901,-615,-8594,16617,2024,-2975,4120,6830 } },
  6463. { "NIKON D2X", 0, 0,
  6464. { 10231,-2769,-1255,-8301,15900,2552,-797,680,7148 } },
  6465. { "NIKON D3000", 0, 0,
  6466. { 8736,-2458,-935,-9075,16894,2251,-1354,1242,8263 } },
  6467. { "NIKON D3100", 0, 0,
  6468. { 7911,-2167,-813,-5327,13150,2408,-1288,2483,7968 } },
  6469. { "NIKON D300", 0, 0,
  6470. { 9030,-1992,-715,-8465,16302,2255,-2689,3217,8069 } },
  6471. { "NIKON D3X", 0, 0,
  6472. { 7171,-1986,-648,-8085,15555,2718,-2170,2512,7457 } },
  6473. { "NIKON D3S", 0, 0,
  6474. { 8828,-2406,-694,-4874,12603,2541,-660,1509,7587 } },
  6475. { "NIKON D3", 0, 0,
  6476. { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
  6477. { "NIKON D40X", 0, 0,
  6478. { 8819,-2543,-911,-9025,16928,2151,-1329,1213,8449 } },
  6479. { "NIKON D40", 0, 0,
  6480. { 6992,-1668,-806,-8138,15748,2543,-874,850,7897 } },
  6481. { "NIKON D5000", 0, 0xf00,
  6482. { 7309,-1403,-519,-8474,16008,2622,-2433,2826,8064 } },
  6483. { "NIKON D5100", 0, 0x3de6,
  6484. { 8198,-2239,-724,-4871,12389,2798,-1043,2050,7181 } },
  6485. { "NIKON D50", 0, 0,
  6486. { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
  6487. { "NIKON D60", 0, 0,
  6488. { 8736,-2458,-935,-9075,16894,2251,-1354,1242,8263 } },
  6489. { "NIKON D7000", 0, 0,
  6490. { 8198,-2239,-724,-4871,12389,2798,-1043,2050,7181 } },
  6491. { "NIKON D700", 0, 0,
  6492. { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
  6493. { "NIKON D70", 0, 0,
  6494. { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
  6495. { "NIKON D80", 0, 0,
  6496. { 8629,-2410,-883,-9055,16940,2171,-1490,1363,8520 } },
  6497. { "NIKON D90", 0, 0xf00,
  6498. { 7309,-1403,-519,-8474,16008,2622,-2434,2826,8064 } },
  6499. { "NIKON E950", 0, 0x3dd, /* DJC */
  6500. { -3746,10611,1665,9621,-1734,2114,-2389,7082,3064,3406,6116,-244 } },
  6501. { "NIKON E995", 0, 0, /* copied from E5000 */
  6502. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  6503. { "NIKON E2100", 0, 0, /* copied from Z2, new white balance */
  6504. { 13142,-4152,-1596,-4655,12374,2282,-1769,2696,6711} },
  6505. { "NIKON E2500", 0, 0,
  6506. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  6507. { "NIKON E3200", 0, 0, /* DJC */
  6508. { 9846,-2085,-1019,-3278,11109,2170,-774,2134,5745 } },
  6509. { "NIKON E4300", 0, 0, /* copied from Minolta DiMAGE Z2 */
  6510. { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
  6511. { "NIKON E4500", 0, 0,
  6512. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  6513. { "NIKON E5000", 0, 0,
  6514. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  6515. { "NIKON E5400", 0, 0,
  6516. { 9349,-2987,-1001,-7919,15766,2266,-2098,2680,6839 } },
  6517. { "NIKON E5700", 0, 0,
  6518. { -5368,11478,2368,5537,-113,3148,-4969,10021,5782,778,9028,211 } },
  6519. { "NIKON E8400", 0, 0,
  6520. { 7842,-2320,-992,-8154,15718,2599,-1098,1342,7560 } },
  6521. { "NIKON E8700", 0, 0,
  6522. { 8489,-2583,-1036,-8051,15583,2643,-1307,1407,7354 } },
  6523. { "NIKON E8800", 0, 0,
  6524. { 7971,-2314,-913,-8451,15762,2894,-1442,1520,7610 } },
  6525. { "NIKON COOLPIX P6000", 0, 0,
  6526. { 9698,-3367,-914,-4706,12584,2368,-837,968,5801 } },
  6527. { "NIKON COOLPIX P7000", 0, 0,
  6528. { 11432,-3679,-1111,-3169,11239,2202,-791,1380,4455 } },
  6529. { "NIKON COOLPIX P7100", 0, 0,
  6530. { 11053,-4269,-1024,-1976,10182,2088,-526,1263,4469 } },
  6531. { "NIKON 1 ", 0, 0,
  6532. { 8994,-2667,-865,-4594,12324,2552,-699,1786,6260 } },
  6533. { "OLYMPUS C5050", 0, 0,
  6534. { 10508,-3124,-1273,-6079,14294,1901,-1653,2306,6237 } },
  6535. { "OLYMPUS C5060", 0, 0,
  6536. { 10445,-3362,-1307,-7662,15690,2058,-1135,1176,7602 } },
  6537. { "OLYMPUS C7070", 0, 0,
  6538. { 10252,-3531,-1095,-7114,14850,2436,-1451,1723,6365 } },
  6539. { "OLYMPUS C70", 0, 0,
  6540. { 10793,-3791,-1146,-7498,15177,2488,-1390,1577,7321 } },
  6541. { "OLYMPUS C80", 0, 0,
  6542. { 8606,-2509,-1014,-8238,15714,2703,-942,979,7760 } },
  6543. { "OLYMPUS E-10", 0, 0xffc,
  6544. { 12745,-4500,-1416,-6062,14542,1580,-1934,2256,6603 } },
  6545. { "OLYMPUS E-1", 0, 0,
  6546. { 11846,-4767,-945,-7027,15878,1089,-2699,4122,8311 } },
  6547. { "OLYMPUS E-20", 0, 0xffc,
  6548. { 13173,-4732,-1499,-5807,14036,1895,-2045,2452,7142 } },
  6549. { "OLYMPUS E-300", 0, 0,
  6550. { 7828,-1761,-348,-5788,14071,1830,-2853,4518,6557 } },
  6551. { "OLYMPUS E-330", 0, 0,
  6552. { 8961,-2473,-1084,-7979,15990,2067,-2319,3035,8249 } },
  6553. { "OLYMPUS E-30", 0, 0xfbc,
  6554. { 8144,-1861,-1111,-7763,15894,1929,-1865,2542,7607 } },
  6555. { "OLYMPUS E-3", 0, 0xf99,
  6556. { 9487,-2875,-1115,-7533,15606,2010,-1618,2100,7389 } },
  6557. { "OLYMPUS E-400", 0, 0,
  6558. { 6169,-1483,-21,-7107,14761,2536,-2904,3580,8568 } },
  6559. { "OLYMPUS E-410", 0, 0xf6a,
  6560. { 8856,-2582,-1026,-7761,15766,2082,-2009,2575,7469 } },
  6561. { "OLYMPUS E-420", 0, 0xfd7,
  6562. { 8746,-2425,-1095,-7594,15612,2073,-1780,2309,7416 } },
  6563. { "OLYMPUS E-450", 0, 0xfd2,
  6564. { 8745,-2425,-1095,-7594,15613,2073,-1780,2309,7416 } },
  6565. { "OLYMPUS E-500", 0, 0,
  6566. { 8136,-1968,-299,-5481,13742,1871,-2556,4205,6630 } },
  6567. { "OLYMPUS E-510", 0, 0xf6a,
  6568. { 8785,-2529,-1033,-7639,15624,2112,-1783,2300,7817 } },
  6569. { "OLYMPUS E-520", 0, 0xfd2,
  6570. { 8344,-2322,-1020,-7596,15635,2048,-1748,2269,7287 } },
  6571. { "OLYMPUS E-5", 0, 0,
  6572. { 11200,-3783,-1325,-4576,12593,2206,-695,1742,7504 } },
  6573. { "OLYMPUS E-600", 0, 0xfaf,
  6574. { 8453,-2198,-1092,-7609,15681,2008,-1725,2337,7824 } },
  6575. { "OLYMPUS E-620", 0, 0xfaf,
  6576. { 8453,-2198,-1092,-7609,15681,2008,-1725,2337,7824 } },
  6577. { "OLYMPUS E-P1", 0, 0xffd,
  6578. { 8343,-2050,-1021,-7715,15705,2103,-1831,2380,8235 } },
  6579. { "OLYMPUS E-P2", 0, 0xffd,
  6580. { 8343,-2050,-1021,-7715,15705,2103,-1831,2380,8235 } },
  6581. { "OLYMPUS E-P3", 0, 0,
  6582. { 7575,-2159,-571,-3722,11341,2725,-1434,2819,6271 } },
  6583. { "OLYMPUS E-PL1s", 0, 0,
  6584. { 11409,-3872,-1393,-4572,12757,2003,-709,1810,7415 } },
  6585. { "OLYMPUS E-PL1", 0, 0,
  6586. { 11408,-4289,-1215,-4286,12385,2118,-387,1467,7787 } },
  6587. { "OLYMPUS E-PL2", 0, 0,
  6588. { 15030,-5552,-1806,-3987,12387,1767,-592,1670,7023 } },
  6589. { "OLYMPUS E-PL3", 0, 0,
  6590. { 7575,-2159,-571,-3722,11341,2725,-1434,2819,6271 } },
  6591. { "OLYMPUS E-PM1", 0, 0,
  6592. { 7575,-2159,-571,-3722,11341,2725,-1434,2819,6271 } },
  6593. { "OLYMPUS SP350", 0, 0,
  6594. { 12078,-4836,-1069,-6671,14306,2578,-786,939,7418 } },
  6595. { "OLYMPUS SP3", 0, 0,
  6596. { 11766,-4445,-1067,-6901,14421,2707,-1029,1217,7572 } },
  6597. { "OLYMPUS SP500UZ", 0, 0xfff,
  6598. { 9493,-3415,-666,-5211,12334,3260,-1548,2262,6482 } },
  6599. { "OLYMPUS SP510UZ", 0, 0xffe,
  6600. { 10593,-3607,-1010,-5881,13127,3084,-1200,1805,6721 } },
  6601. { "OLYMPUS SP550UZ", 0, 0xffe,
  6602. { 11597,-4006,-1049,-5432,12799,2957,-1029,1750,6516 } },
  6603. { "OLYMPUS SP560UZ", 0, 0xff9,
  6604. { 10915,-3677,-982,-5587,12986,2911,-1168,1968,6223 } },
  6605. { "OLYMPUS SP570UZ", 0, 0,
  6606. { 11522,-4044,-1146,-4736,12172,2904,-988,1829,6039 } },
  6607. { "OLYMPUS XZ-1", 0, 0,
  6608. { 10901,-4095,-1074,-1141,9208,2293,-62,1417,5158 } },
  6609. { "PENTAX *ist DL2", 0, 0,
  6610. { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
  6611. { "PENTAX *ist DL", 0, 0,
  6612. { 10829,-2838,-1115,-8339,15817,2696,-837,680,11939 } },
  6613. { "PENTAX *ist DS2", 0, 0,
  6614. { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
  6615. { "PENTAX *ist DS", 0, 0,
  6616. { 10371,-2333,-1206,-8688,16231,2602,-1230,1116,11282 } },
  6617. { "PENTAX *ist D", 0, 0,
  6618. { 9651,-2059,-1189,-8881,16512,2487,-1460,1345,10687 } },
  6619. { "PENTAX K10D", 0, 0,
  6620. { 9566,-2863,-803,-7170,15172,2112,-818,803,9705 } },
  6621. { "PENTAX K1", 0, 0,
  6622. { 11095,-3157,-1324,-8377,15834,2720,-1108,947,11688 } },
  6623. { "PENTAX K20D", 0, 0,
  6624. { 9427,-2714,-868,-7493,16092,1373,-2199,3264,7180 } },
  6625. { "PENTAX K200D", 0, 0,
  6626. { 9186,-2678,-907,-8693,16517,2260,-1129,1094,8524 } },
  6627. { "PENTAX K2000", 0, 0,
  6628. { 11057,-3604,-1155,-5152,13046,2329,-282,375,8104 } },
  6629. { "PENTAX K-m", 0, 0,
  6630. { 11057,-3604,-1155,-5152,13046,2329,-282,375,8104 } },
  6631. { "PENTAX K-x", 0, 0,
  6632. { 8843,-2837,-625,-5025,12644,2668,-411,1234,7410 } },
  6633. { "PENTAX K-r", 0, 0,
  6634. { 9895,-3077,-850,-5304,13035,2521,-883,1768,6936 } },
  6635. { "PENTAX K-5", 0, 0,
  6636. { 8713,-2833,-743,-4342,11900,2772,-722,1543,6247 } },
  6637. { "PENTAX K-7", 0, 0,
  6638. { 9142,-2947,-678,-8648,16967,1663,-2224,2898,8615 } },
  6639. { "PENTAX 645D", 0, 0x3e00,
  6640. { 10646,-3593,-1158,-3329,11699,1831,-667,2874,6287 } },
  6641. { "Panasonic DMC-FZ8", 0, 0xf7f,
  6642. { 8986,-2755,-802,-6341,13575,3077,-1476,2144,6379 } },
  6643. { "Panasonic DMC-FZ18", 0, 0,
  6644. { 9932,-3060,-935,-5809,13331,2753,-1267,2155,5575 } },
  6645. { "Panasonic DMC-FZ28", 15, 0xf96,
  6646. { 10109,-3488,-993,-5412,12812,2916,-1305,2140,5543 } },
  6647. { "Panasonic DMC-FZ30", 0, 0xf94,
  6648. { 10976,-4029,-1141,-7918,15491,2600,-1670,2071,8246 } },
  6649. { "Panasonic DMC-FZ3", 143, 0,
  6650. { 9938,-2780,-890,-4604,12393,2480,-1117,2304,4620 } },
  6651. { "Panasonic DMC-FZ4", 143, 0,
  6652. { 13639,-5535,-1371,-1698,9633,2430,316,1152,4108 } },
  6653. { "Panasonic DMC-FZ50", 0, 0,
  6654. { 7906,-2709,-594,-6231,13351,3220,-1922,2631,6537 } },
  6655. { "LEICA V-LUX1", 0, 0,
  6656. { 7906,-2709,-594,-6231,13351,3220,-1922,2631,6537 } },
  6657. { "Panasonic DMC-L10", 15, 0xf96,
  6658. { 8025,-1942,-1050,-7920,15904,2100,-2456,3005,7039 } },
  6659. { "Panasonic DMC-L1", 0, 0xf7f,
  6660. { 8054,-1885,-1025,-8349,16367,2040,-2805,3542,7629 } },
  6661. { "LEICA DIGILUX 3", 0, 0xf7f,
  6662. { 8054,-1885,-1025,-8349,16367,2040,-2805,3542,7629 } },
  6663. { "Panasonic DMC-LC1", 0, 0,
  6664. { 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
  6665. { "LEICA DIGILUX 2", 0, 0,
  6666. { 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
  6667. { "Panasonic DMC-LX1", 0, 0xf7f,
  6668. { 10704,-4187,-1230,-8314,15952,2501,-920,945,8927 } },
  6669. { "LEICA D-LUX2", 0, 0xf7f,
  6670. { 10704,-4187,-1230,-8314,15952,2501,-920,945,8927 } },
  6671. { "Panasonic DMC-LX2", 0, 0,
  6672. { 8048,-2810,-623,-6450,13519,3272,-1700,2146,7049 } },
  6673. { "LEICA D-LUX3", 0, 0,
  6674. { 8048,-2810,-623,-6450,13519,3272,-1700,2146,7049 } },
  6675. { "Panasonic DMC-LX3", 15, 0,
  6676. { 8128,-2668,-655,-6134,13307,3161,-1782,2568,6083 } },
  6677. { "LEICA D-LUX 4", 15, 0,
  6678. { 8128,-2668,-655,-6134,13307,3161,-1782,2568,6083 } },
  6679. { "Panasonic DMC-LX5", 143, 0,
  6680. { 10909,-4295,-948,-1333,9306,2399,22,1738,4582 } },
  6681. { "LEICA D-LUX 5", 143, 0,
  6682. { 10909,-4295,-948,-1333,9306,2399,22,1738,4582 } },
  6683. { "Panasonic DMC-FZ100", 143, 0xfff,
  6684. { 16197,-6146,-1761,-2393,10765,1869,366,2238,5248 } },
  6685. { "LEICA V-LUX 2", 143, 0xfff,
  6686. { 16197,-6146,-1761,-2393,10765,1869,366,2238,5248 } },
  6687. { "Panasonic DMC-FZ150", 143, 0xfff,
  6688. { 11904,-4541,-1189,-2355,10899,1662,-296,1586,4289 } },
  6689. { "LEICA V-LUX 3", 143, 0xfff,
  6690. { 11904,-4541,-1189,-2355,10899,1662,-296,1586,4289 } },
  6691. { "Panasonic DMC-FX150", 15, 0xfff,
  6692. { 9082,-2907,-925,-6119,13377,3058,-1797,2641,5609 } },
  6693. { "Panasonic DMC-G10", 0, 0,
  6694. { 10113,-3400,-1114,-4765,12683,2317,-377,1437,6710 } },
  6695. { "Panasonic DMC-G1", 15, 0xf94,
  6696. { 8199,-2065,-1056,-8124,16156,2033,-2458,3022,7220 } },
  6697. { "Panasonic DMC-G2", 15, 0xf3c,
  6698. { 10113,-3400,-1114,-4765,12683,2317,-377,1437,6710 } },
  6699. { "Panasonic DMC-G3", 143, 0xfff,
  6700. { 6763,-1919,-863,-3868,11515,2684,-1216,2387,5879 } },
  6701. { "Panasonic DMC-GF1", 15, 0xf92,
  6702. { 7888,-1902,-1011,-8106,16085,2099,-2353,2866,7330 } },
  6703. { "Panasonic DMC-GF2", 143, 0xfff,
  6704. { 7888,-1902,-1011,-8106,16085,2099,-2353,2866,7330 } },
  6705. { "Panasonic DMC-GF3", 143, 0xfff,
  6706. { 9051,-2468,-1204,-5212,13276,2121,-1197,2510,6890 } },
  6707. { "Panasonic DMC-GH1", 15, 0xf92,
  6708. { 6299,-1466,-532,-6535,13852,2969,-2331,3112,5984 } },
  6709. { "Panasonic DMC-GH2", 15, 0xf95,
  6710. { 7780,-2410,-806,-3913,11724,2484,-1018,2390,5298 } },
  6711. { "Panasonic DMC-GX1", 143, 0,
  6712. { 6763,-1919,-863,-3868,11515,2684,-1216,2387,5879 } },
  6713. { "Phase One H 20", 0, 0, /* DJC */
  6714. { 1313,1855,-109,-6715,15908,808,-327,1840,6020 } },
  6715. { "Phase One H 25", 0, 0,
  6716. { 2905,732,-237,-8134,16626,1476,-3038,4253,7517 } },
  6717. { "Phase One P 2", 0, 0,
  6718. { 2905,732,-237,-8134,16626,1476,-3038,4253,7517 } },
  6719. { "Phase One P 30", 0, 0,
  6720. { 4516,-245,-37,-7020,14976,2173,-3206,4671,7087 } },
  6721. { "Phase One P 45", 0, 0,
  6722. { 5053,-24,-117,-5684,14076,1702,-2619,4492,5849 } },
  6723. { "Phase One P40", 0, 0,
  6724. { 8035,435,-962,-6001,13872,2320,-1159,3065,5434 } },
  6725. { "Phase One P65", 0, 0,
  6726. { 8035,435,-962,-6001,13872,2320,-1159,3065,5434 } },
  6727. { "RED ONE", 704, 0xffff, /* DJC */
  6728. { 21014,-7891,-2613,-3056,12201,856,-2203,5125,8042 } },
  6729. { "SAMSUNG EX1", 0, 0x3e00,
  6730. { 8898,-2498,-994,-3144,11328,2066,-760,1381,4576 } },
  6731. { "SAMSUNG NX200", 0, 0xfff,
  6732. { 6933,-2268,-753,-4921,13387,1647,-803,1641,6096 } },
  6733. { "SAMSUNG NX", 0, 0, /* NX5, NX10, NX11, NX100 */
  6734. { 10332,-3234,-1168,-6111,14639,1520,-1352,2647,8331 } },
  6735. { "SAMSUNG WB2000", 0, 0xfff,
  6736. { 12093,-3557,-1155,-1000,9534,1733,-22,1787,4576 } },
  6737. { "SAMSUNG GX-1", 0, 0,
  6738. { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
  6739. { "SAMSUNG S85", 0, 0xffff, /* DJC */
  6740. { 11885,-3968,-1473,-4214,12299,1916,-835,1655,5549 } },
  6741. { "Sinar", 0, 0, /* DJC */
  6742. { 16442,-2956,-2422,-2877,12128,750,-1136,6066,4559 } },
  6743. { "SONY DSC-F828", 491, 0,
  6744. { 7924,-1910,-777,-8226,15459,2998,-1517,2199,6818,-7242,11401,3481 } },
  6745. { "SONY DSC-R1", 512, 0,
  6746. { 8512,-2641,-694,-8042,15670,2526,-1821,2117,7414 } },
  6747. { "SONY DSC-V3", 0, 0,
  6748. { 7511,-2571,-692,-7894,15088,3060,-948,1111,8128 } },
  6749. { "SONY DSLR-A100", 0, 0xfeb,
  6750. { 9437,-2811,-774,-8405,16215,2290,-710,596,7181 } },
  6751. { "SONY DSLR-A290", 0, 0,
  6752. { 6038,-1484,-579,-9145,16746,2512,-875,746,7218 } },
  6753. { "SONY DSLR-A2", 0, 0,
  6754. { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
  6755. { "SONY DSLR-A300", 0, 0,
  6756. { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
  6757. { "SONY DSLR-A330", 0, 0,
  6758. { 9847,-3091,-929,-8485,16346,2225,-714,595,7103 } },
  6759. { "SONY DSLR-A350", 0, 0xffc,
  6760. { 6038,-1484,-578,-9146,16746,2513,-875,746,7217 } },
  6761. { "SONY DSLR-A380", 0, 0,
  6762. { 6038,-1484,-579,-9145,16746,2512,-875,746,7218 } },
  6763. { "SONY DSLR-A390", 0, 0,
  6764. { 6038,-1484,-579,-9145,16746,2512,-875,746,7218 } },
  6765. { "SONY DSLR-A450", 128, 0xfeb,
  6766. { 4950,-580,-103,-5228,12542,3029,-709,1435,7371 } },
  6767. { "SONY DSLR-A580", 128, 0xfeb,
  6768. { 5932,-1492,-411,-4813,12285,2856,-741,1524,6739 } },
  6769. { "SONY DSLR-A5", 128, 0xfeb,
  6770. { 4950,-580,-103,-5228,12542,3029,-709,1435,7371 } },
  6771. { "SONY DSLR-A700", 126, 0,
  6772. { 5775,-805,-359,-8574,16295,2391,-1943,2341,7249 } },
  6773. { "SONY DSLR-A850", 128, 0,
  6774. { 5413,-1162,-365,-5665,13098,2866,-608,1179,8440 } },
  6775. { "SONY DSLR-A900", 128, 0,
  6776. { 5209,-1072,-397,-8845,16120,2919,-1618,1803,8654 } },
  6777. { "SONY NEX-5N", 128, 0,
  6778. { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
  6779. { "SONY NEX-C3", 128, 0,
  6780. { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
  6781. { "SONY NEX-3", 138, 0, /* DJC */
  6782. { 6907,-1256,-645,-4940,12621,2320,-1710,2581,6230 } },
  6783. { "SONY NEX-5", 116, 0, /* DJC */
  6784. { 6807,-1350,-342,-4216,11649,2567,-1089,2001,6420 } },
  6785. { "SONY NEX-3", 128, 0, /* Adobe */
  6786. { 6549,-1550,-436,-4880,12435,2753,-854,1868,6976 } },
  6787. { "SONY NEX-5", 128, 0, /* Adobe */
  6788. { 6549,-1550,-436,-4880,12435,2753,-854,1868,6976 } },
  6789. { "SONY NEX-7", 128, 0,
  6790. { 5491,-1192,-363,-4951,12342,2948,-911,1722,7192 } },
  6791. { "SONY SLT-A33", 128, 0,
  6792. { 6069,-1221,-366,-5221,12779,2734,-1024,2066,6834 } },
  6793. { "SONY SLT-A35", 128, 0,
  6794. { 5986,-1618,-415,-4557,11820,3120,-681,1404,6971 } },
  6795. { "SONY SLT-A55", 128, 0,
  6796. { 5932,-1492,-411,-4813,12285,2856,-741,1524,6739 } },
  6797. { "SONY SLT-A65", 128, 0,
  6798. { 5491,-1192,-363,-4951,12342,2948,-911,1722,7192 } },
  6799. { "SONY SLT-A77", 128, 0,
  6800. { 5491,-1192,-363,-4951,12342,2948,-911,1722,7192 } }
  6801. };
  6802. double cam_xyz[4][3];
  6803. char name[130];
  6804. int i, j;
  6805. sprintf (name, "%s %s", p_make, p_model);
  6806. for (i=0; i < sizeof table / sizeof *table; i++)
  6807. if (!strncmp (name, table[i].prefix, strlen(table[i].prefix))) {
  6808. if (table[i].t_black) black = (ushort) table[i].t_black;
  6809. if (table[i].t_maximum) maximum = (ushort) table[i].t_maximum;
  6810. if (table[i].trans[0]) {
  6811. for (j=0; j < 12; j++)
  6812. #ifdef LIBRAW_LIBRARY_BUILD
  6813. imgdata.color.cam_xyz[0][j] =
  6814. #endif
  6815. cam_xyz[0][j] = table[i].trans[j] / 10000.0;
  6816. cam_xyz_coeff (cam_xyz);
  6817. }
  6818. break;
  6819. }
  6820. }
  6821. void CLASS simple_coeff (int index)
  6822. {
  6823. static const float table[][12] = {
  6824. /* index 0 -- all Foveon cameras */
  6825. { 1.4032,-0.2231,-0.1016,-0.5263,1.4816,0.017,-0.0112,0.0183,0.9113 },
  6826. /* index 1 -- Kodak DC20 and DC25 */
  6827. { 2.25,0.75,-1.75,-0.25,-0.25,0.75,0.75,-0.25,-0.25,-1.75,0.75,2.25 },
  6828. /* index 2 -- Logitech Fotoman Pixtura */
  6829. { 1.893,-0.418,-0.476,-0.495,1.773,-0.278,-1.017,-0.655,2.672 },
  6830. /* index 3 -- Nikon E880, E900, and E990 */
  6831. { -1.936280, 1.800443, -1.448486, 2.584324,
  6832. 1.405365, -0.524955, -0.289090, 0.408680,
  6833. -1.204965, 1.082304, 2.941367, -1.818705 }
  6834. };
  6835. int i, c;
  6836. for (raw_color = i=0; i < 3; i++)
  6837. FORCC rgb_cam[i][c] = table[index][i*colors+c];
  6838. #ifdef LIBRAW_LIBRARY_BUILD
  6839. color_flags.rgb_cam_state = LIBRAW_COLORSTATE_CALCULATED;
  6840. #endif
  6841. }
  6842. short CLASS guess_byte_order (int words)
  6843. {
  6844. uchar test[4][2];
  6845. int t=2, msb;
  6846. double diff, sum[2] = {0,0};
  6847. fread (test[0], 2, 2, ifp);
  6848. for (words-=2; words--; ) {
  6849. fread (test[t], 2, 1, ifp);
  6850. for (msb=0; msb < 2; msb++) {
  6851. diff = (test[t^2][msb] << 8 | test[t^2][!msb])
  6852. - (test[t ][msb] << 8 | test[t ][!msb]);
  6853. sum[msb] += diff*diff;
  6854. }
  6855. t = (t+1) & 3;
  6856. }
  6857. return sum[0] < sum[1] ? 0x4d4d : 0x4949;
  6858. }
  6859. float CLASS find_green (int bps, int bite, int off0, int off1)
  6860. {
  6861. UINT64 bitbuf=0;
  6862. int vbits, col, i, c;
  6863. ushort img[2][2064];
  6864. double sum[]={0,0};
  6865. FORC(2) {
  6866. fseek (ifp, c ? off1:off0, SEEK_SET);
  6867. for (vbits=col=0; col < width; col++) {
  6868. for (vbits -= bps; vbits < 0; vbits += bite) {
  6869. bitbuf <<= bite;
  6870. for (i=0; i < bite; i+=8)
  6871. bitbuf |= (unsigned) (fgetc(ifp) << i);
  6872. }
  6873. img[c][col] = bitbuf << (64-bps-vbits) >> (64-bps);
  6874. }
  6875. }
  6876. FORC(width-1) {
  6877. sum[ c & 1] += ABS(img[0][c]-img[1][c+1]);
  6878. sum[~c & 1] += ABS(img[1][c]-img[0][c+1]);
  6879. }
  6880. return 100 * log(sum[0]/sum[1]);
  6881. }
  6882. /*
  6883. Identify which camera created this file, and set global variables
  6884. accordingly.
  6885. */
  6886. void CLASS identify()
  6887. {
  6888. char head[32], *cp;
  6889. int hlen, flen, fsize, zero_fsize=1, i, c, is_canon;
  6890. struct jhead jh;
  6891. short pana[][6] = {
  6892. { 3130, 1743, 4, 0, -6, 0 },
  6893. { 3130, 2055, 4, 0, -6, 0 },
  6894. { 3130, 2319, 4, 0, -6, 0 },
  6895. { 3170, 2103, 18, 0,-42, 20 },
  6896. { 3170, 2367, 18, 13,-42,-21 },
  6897. { 3177, 2367, 0, 0, -1, 0 },
  6898. { 3304, 2458, 0, 0, -1, 0 },
  6899. { 3330, 2463, 9, 0, -5, 0 },
  6900. { 3330, 2479, 9, 0,-17, 4 },
  6901. { 3370, 1899, 15, 0,-44, 20 },
  6902. { 3370, 2235, 15, 0,-44, 20 },
  6903. { 3370, 2511, 15, 10,-44,-21 },
  6904. { 3690, 2751, 3, 0, -8, -3 },
  6905. { 3710, 2751, 0, 0, -3, 0 },
  6906. { 3724, 2450, 0, 0, 0, -2 },
  6907. { 3770, 2487, 17, 0,-44, 19 },
  6908. { 3770, 2799, 17, 15,-44,-19 },
  6909. { 3880, 2170, 6, 0, -6, 0 },
  6910. { 4060, 3018, 0, 0, 0, -2 },
  6911. { 4290, 2391, 3, 0, -8, -1 },
  6912. { 4330, 2439, 17, 15,-44,-19 },
  6913. { 4508, 2962, 0, 0, -3, -4 },
  6914. { 4508, 3330, 0, 0, -3, -6 } };
  6915. static const struct {
  6916. int fsize;
  6917. char t_make[12], t_model[19], withjpeg;
  6918. } table[] = {
  6919. { 62464, "Kodak", "DC20" ,0 },
  6920. { 124928, "Kodak", "DC20" ,0 },
  6921. { 1652736, "Kodak", "DCS200" ,0 },
  6922. { 4159302, "Kodak", "C330" ,0 },
  6923. { 4162462, "Kodak", "C330" ,0 },
  6924. { 460800, "Kodak", "C603v" ,0 },
  6925. { 614400, "Kodak", "C603v" ,0 },
  6926. { 6163328, "Kodak", "C603" ,0 },
  6927. { 6166488, "Kodak", "C603" ,0 },
  6928. { 9116448, "Kodak", "C603y" ,0 },
  6929. { 311696, "ST Micro", "STV680 VGA" ,0 }, /* SPYz */
  6930. { 787456, "Creative", "PC-CAM 600" ,0 },
  6931. { 1138688, "Minolta", "RD175" ,0 },
  6932. { 3840000, "Foculus", "531C" ,0 },
  6933. { 786432, "AVT", "F-080C" ,0 },
  6934. { 1447680, "AVT", "F-145C" ,0 },
  6935. { 1920000, "AVT", "F-201C" ,0 },
  6936. { 5067304, "AVT", "F-510C" ,0 },
  6937. { 5067316, "AVT", "F-510C" ,0 },
  6938. { 10134608, "AVT", "F-510C" ,0 },
  6939. { 10134620, "AVT", "F-510C" ,0 },
  6940. { 16157136, "AVT", "F-810C" ,0 },
  6941. { 1409024, "Sony", "XCD-SX910CR" ,0 },
  6942. { 2818048, "Sony", "XCD-SX910CR" ,0 },
  6943. { 3884928, "Micron", "2010" ,0 },
  6944. { 6624000, "Pixelink", "A782" ,0 },
  6945. { 13248000, "Pixelink", "A782" ,0 },
  6946. { 6291456, "RoverShot","3320AF" ,0 },
  6947. { 6553440, "Canon", "PowerShot A460" ,0 },
  6948. { 6653280, "Canon", "PowerShot A530" ,0 },
  6949. { 6573120, "Canon", "PowerShot A610" ,0 },
  6950. { 9219600, "Canon", "PowerShot A620" ,0 },
  6951. { 9243240, "Canon", "PowerShot A470" ,0 },
  6952. { 10341600, "Canon", "PowerShot A720 IS",0 },
  6953. { 10383120, "Canon", "PowerShot A630" ,0 },
  6954. { 12945240, "Canon", "PowerShot A640" ,0 },
  6955. { 15636240, "Canon", "PowerShot A650" ,0 },
  6956. { 5298000, "Canon", "PowerShot SD300" ,0 },
  6957. { 7710960, "Canon", "PowerShot S3 IS" ,0 },
  6958. { 15467760, "Canon", "PowerShot SX110 IS",0 },
  6959. { 15534576, "Canon", "PowerShot SX120 IS",0 },
  6960. { 18653760, "Canon", "PowerShot SX20 IS",0 },
  6961. { 21936096, "Canon", "PowerShot SX30 IS",0 },
  6962. { 5939200, "OLYMPUS", "C770UZ" ,0 },
  6963. { 1581060, "NIKON", "E900" ,1 }, /* or E900s,E910 */
  6964. { 2465792, "NIKON", "E950" ,1 }, /* or E800,E700 */
  6965. { 2940928, "NIKON", "E2100" ,1 }, /* or E2500 */
  6966. { 4771840, "NIKON", "E990" ,1 }, /* or E995, Oly C3030Z */
  6967. { 4775936, "NIKON", "E3700" ,1 }, /* or Optio 33WR */
  6968. { 5869568, "NIKON", "E4300" ,1 }, /* or DiMAGE Z2 */
  6969. { 5865472, "NIKON", "E4500" ,1 },
  6970. { 7438336, "NIKON", "E5000" ,1 }, /* or E5700 */
  6971. { 8998912, "NIKON", "COOLPIX S6" ,1 },
  6972. { 1976352, "CASIO", "QV-2000UX" ,1 },
  6973. { 3217760, "CASIO", "QV-3*00EX" ,1 },
  6974. { 6218368, "CASIO", "QV-5700" ,1 },
  6975. { 6054400, "CASIO", "QV-R41" ,1 },
  6976. { 7530816, "CASIO", "QV-R51" ,1 },
  6977. { 7684000, "CASIO", "QV-4000" ,1 },
  6978. { 2937856, "CASIO", "EX-S20" ,1 },
  6979. { 4948608, "CASIO", "EX-S100" ,1 },
  6980. { 7542528, "CASIO", "EX-Z50" ,1 },
  6981. { 7753344, "CASIO", "EX-Z55" ,1 },
  6982. { 7816704, "CASIO", "EX-Z60" ,1 },
  6983. { 10843712, "CASIO", "EX-Z75" ,1 },
  6984. { 10834368, "CASIO", "EX-Z750" ,1 },
  6985. { 12310144, "CASIO", "EX-Z850" ,1 },
  6986. { 15499264, "CASIO", "EX-Z1050" ,1 },
  6987. { 7426656, "CASIO", "EX-P505" ,1 },
  6988. { 9313536, "CASIO", "EX-P600" ,1 },
  6989. { 10979200, "CASIO", "EX-P700" ,1 },
  6990. { 3178560, "PENTAX", "Optio S" ,1 },
  6991. { 4841984, "PENTAX", "Optio S" ,1 },
  6992. { 6114240, "PENTAX", "Optio S4" ,1 }, /* or S4i, CASIO EX-Z4 */
  6993. { 10702848, "PENTAX", "Optio 750Z" ,1 },
  6994. { 15980544, "AGFAPHOTO","DC-833m" ,1 },
  6995. { 16098048, "SAMSUNG", "S85" ,1 },
  6996. { 16215552, "SAMSUNG", "S85" ,1 },
  6997. { 20487168, "SAMSUNG", "WB550" ,1 },
  6998. { 24000000, "SAMSUNG", "WB550" ,1 },
  6999. { 9994240, "ptGrey", "GRAS-50S5C" ,0 }, // KC: SUPPORT GRASSHOPPER
  7000. { 10075968, "JaiPulnix","BB-500CL" ,0 }, // KC: SUPPORT BB-500CL
  7001. { 10108896, "JaiPulnix","BB-500GE" ,0 }, // KC: SUPPORT BB-500GE
  7002. { 10036800, "SVS", "SVS625CL" ,0 }, // KC: SUPPORT SVS625 cameralink
  7003. { 12582980, "Sinar", "" ,0 },
  7004. { 33292868, "Sinar", "" ,0 },
  7005. { 44390468, "Sinar", "" ,0 } };
  7006. static const char *corp[] =
  7007. { "Canon", "NIKON", "EPSON", "KODAK", "Kodak", "OLYMPUS", "PENTAX",
  7008. "MINOLTA", "Minolta", "Konica", "CASIO", "Sinar", "Phase One",
  7009. "SAMSUNG", "Mamiya", "MOTOROLA" };
  7010. #ifdef LIBRAW_LIBRARY_BUILD
  7011. RUN_CALLBACK(LIBRAW_PROGRESS_IDENTIFY,0,2);
  7012. #endif
  7013. tiff_flip = flip = filters = -1; /* 0 is valid, so -1 is unknown */
  7014. raw_height = raw_width = fuji_width = fuji_layout = cr2_slice[0] = 0;
  7015. maximum = height = width = top_margin = left_margin = 0;
  7016. cdesc[0] = desc[0] = artist[0] = make[0] = model[0] = model2[0] = 0;
  7017. iso_speed = shutter = aperture = focal_len = unique_id = 0;
  7018. tiff_nifds = 0;
  7019. memset (tiff_ifd, 0, sizeof tiff_ifd);
  7020. memset (gpsdata, 0, sizeof gpsdata);
  7021. memset (cblack, 0, sizeof cblack);
  7022. memset (white, 0, sizeof white);
  7023. thumb_offset = thumb_length = thumb_width = thumb_height = 0;
  7024. load_raw = thumb_load_raw = 0;
  7025. write_thumb = &CLASS jpeg_thumb;
  7026. data_offset = meta_length = tiff_bps = tiff_compress = 0;
  7027. kodak_cbpp = zero_after_ff = dng_version = load_flags = 0;
  7028. timestamp = shot_order = tiff_samples = black = is_foveon = 0;
  7029. mix_green = profile_length = data_error = zero_is_bad = 0;
  7030. pixel_aspect = is_raw = raw_color = 1;
  7031. tile_width = tile_length = INT_MAX;
  7032. for (i=0; i < 4; i++) {
  7033. cam_mul[i] = i == 1;
  7034. pre_mul[i] = i < 3;
  7035. FORC3 cmatrix[c][i] = 0;
  7036. FORC3 rgb_cam[c][i] = c == i;
  7037. }
  7038. #ifdef LIBRAW_LIBRARY_BUILD
  7039. color_flags.cmatrix_state = LIBRAW_COLORSTATE_INIT;
  7040. color_flags.rgb_cam_state = LIBRAW_COLORSTATE_INIT;
  7041. color_flags.pre_mul_state = LIBRAW_COLORSTATE_INIT;
  7042. color_flags.cam_mul_state = LIBRAW_COLORSTATE_INIT;
  7043. #endif
  7044. colors = 3;
  7045. for (i=0; i < 0x4000; i++) curve[i] = i;
  7046. #ifdef LIBRAW_LIBRARY_BUILD
  7047. color_flags.curve_state = LIBRAW_COLORSTATE_INIT;
  7048. #endif
  7049. order = get2();
  7050. hlen = get4();
  7051. fseek (ifp, 0, SEEK_SET);
  7052. fread (head, 1, 32, ifp);
  7053. fseek (ifp, 0, SEEK_END);
  7054. flen = fsize = ftell(ifp);
  7055. if ((cp = (char *) memmem (head, 32, (char*)"MMMM", 4)) ||
  7056. (cp = (char *) memmem (head, 32, (char*)"IIII", 4))) {
  7057. parse_phase_one (cp-head);
  7058. if (cp-head && parse_tiff(0)) apply_tiff();
  7059. } else if (order == 0x4949 || order == 0x4d4d) {
  7060. if (!memcmp (head+6,"HEAPCCDR",8)) {
  7061. data_offset = hlen;
  7062. parse_ciff (hlen, flen - hlen);
  7063. } else if (parse_tiff(0)) apply_tiff();
  7064. } else if (!memcmp (head,"\xff\xd8\xff\xe1",4) &&
  7065. !memcmp (head+6,"Exif",4)) {
  7066. fseek (ifp, 4, SEEK_SET);
  7067. data_offset = 4 + get2();
  7068. fseek (ifp, data_offset, SEEK_SET);
  7069. if (fgetc(ifp) != 0xff)
  7070. parse_tiff(12);
  7071. thumb_offset = 0;
  7072. } else if (!memcmp (head+25,"ARECOYK",7)) {
  7073. strcpy (make, "Contax");
  7074. strcpy (model,"N Digital");
  7075. fseek (ifp, 33, SEEK_SET);
  7076. get_timestamp(1);
  7077. fseek (ifp, 60, SEEK_SET);
  7078. FORC4 cam_mul[c ^ (c >> 1)] = get4();
  7079. #ifdef LIBRAW_LIBRARY_BUILD
  7080. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  7081. #endif
  7082. } else if (!strcmp (head, "PXN")) {
  7083. strcpy (make, "Logitech");
  7084. strcpy (model,"Fotoman Pixtura");
  7085. } else if (!strcmp (head, "qktk")) {
  7086. strcpy (make, "Apple");
  7087. strcpy (model,"QuickTake 100");
  7088. load_raw = &CLASS quicktake_100_load_raw;
  7089. } else if (!strcmp (head, "qktn")) {
  7090. strcpy (make, "Apple");
  7091. strcpy (model,"QuickTake 150");
  7092. load_raw = &CLASS kodak_radc_load_raw;
  7093. } else if (!memcmp (head,"FUJIFILM",8)) {
  7094. fseek (ifp, 84, SEEK_SET);
  7095. thumb_offset = get4();
  7096. thumb_length = get4();
  7097. fseek (ifp, 92, SEEK_SET);
  7098. parse_fuji (get4());
  7099. if (thumb_offset > 120) {
  7100. fseek (ifp, 120, SEEK_SET);
  7101. is_raw += (i = get4()) && 1;
  7102. if (is_raw == 2 && shot_select)
  7103. parse_fuji (i);
  7104. }
  7105. fseek (ifp, 100+28*(shot_select > 0), SEEK_SET);
  7106. parse_tiff (data_offset = get4());
  7107. parse_tiff (thumb_offset+12);
  7108. apply_tiff();
  7109. } else if (!memcmp (head,"RIFF",4)) {
  7110. fseek (ifp, 0, SEEK_SET);
  7111. parse_riff();
  7112. } else if (!memcmp (head,"\0\001\0\001\0@",6)) {
  7113. fseek (ifp, 6, SEEK_SET);
  7114. fread (make, 1, 8, ifp);
  7115. fread (model, 1, 8, ifp);
  7116. fread (model2, 1, 16, ifp);
  7117. data_offset = get2();
  7118. get2();
  7119. raw_width = get2();
  7120. raw_height = get2();
  7121. load_raw = &CLASS nokia_load_raw;
  7122. filters = 0x61616161;
  7123. } else if (!memcmp (head,"NOKIARAW",8)) {
  7124. strcpy (make, "NOKIA");
  7125. strcpy (model, "X2");
  7126. order = 0x4949;
  7127. fseek (ifp, 300, SEEK_SET);
  7128. data_offset = get4();
  7129. i = get4();
  7130. width = get2();
  7131. height = get2();
  7132. data_offset += i - width * 5 / 4 * height;
  7133. load_raw = &CLASS nokia_load_raw;
  7134. filters = 0x61616161;
  7135. } else if (!memcmp (head,"ARRI",4)) {
  7136. order = 0x4949;
  7137. fseek (ifp, 20, SEEK_SET);
  7138. width = get4();
  7139. height = get4();
  7140. strcpy (make, "ARRI");
  7141. fseek (ifp, 668, SEEK_SET);
  7142. fread (model, 1, 64, ifp);
  7143. data_offset = 4096;
  7144. load_raw = &CLASS packed_load_raw;
  7145. load_flags = 88;
  7146. filters = 0x61616161;
  7147. } else if (!memcmp (head+4,"RED1",4)) {
  7148. strcpy (make, "RED");
  7149. strcpy (model,"ONE");
  7150. parse_redcine();
  7151. load_raw = &CLASS redcine_load_raw;
  7152. gamma_curve (1/2.4, 12.92, 1, 4095);
  7153. filters = 0x49494949;
  7154. } else if (!memcmp (head,"DSC-Image",9))
  7155. parse_rollei();
  7156. else if (!memcmp (head,"PWAD",4))
  7157. parse_sinar_ia();
  7158. else if (!memcmp (head,"\0MRM",4))
  7159. parse_minolta(0);
  7160. else if (!memcmp (head,"FOVb",4))
  7161. {
  7162. parse_foveon();
  7163. if(!strcasecmp(make,"SIGMA") && !strncasecmp(model,"SIGMA DP",8))
  7164. {
  7165. make[0] = model[0] = 0;
  7166. is_foveon = 0;
  7167. }
  7168. }
  7169. else if (!memcmp (head,"CI",2))
  7170. parse_cine();
  7171. else
  7172. for (zero_fsize=i=0; i < sizeof table / sizeof *table; i++)
  7173. if (fsize == table[i].fsize) {
  7174. strcpy (make, table[i].t_make );
  7175. strcpy (model, table[i].t_model);
  7176. if (table[i].withjpeg)
  7177. parse_external_jpeg();
  7178. }
  7179. if (zero_fsize) fsize = 0;
  7180. if (make[0] == 0) parse_smal (0, flen);
  7181. if (make[0] == 0) parse_jpeg (is_raw = 0);
  7182. for (i=0; i < sizeof corp / sizeof *corp; i++)
  7183. if (strstr (make, corp[i])) /* Simplify company names */
  7184. strcpy (make, corp[i]);
  7185. if (!strncmp (make,"KODAK",5) &&
  7186. ((cp = strstr(model," DIGITAL CAMERA")) ||
  7187. (cp = strstr(model," Digital Camera")) ||
  7188. (cp = strstr(model,"FILE VERSION"))))
  7189. *cp = 0;
  7190. cp = make + strlen(make); /* Remove trailing spaces */
  7191. while (*--cp == ' ') *cp = 0;
  7192. cp = model + strlen(model);
  7193. while (*--cp == ' ') *cp = 0;
  7194. i = strlen(make); /* Remove make from model */
  7195. if (!strncasecmp (model, make, i) && model[i++] == ' ')
  7196. memmove (model, model+i, 64-i);
  7197. if (!strncmp (model,"Digital Camera ",15))
  7198. strcpy (model, model+15);
  7199. desc[511] = artist[63] = make[63] = model[63] = model2[63] = 0;
  7200. if (!is_raw) goto notraw;
  7201. if (!height) height = raw_height;
  7202. if (!width) width = raw_width;
  7203. if (fuji_width) {
  7204. fuji_width = (raw_width+1)/2;
  7205. width = height + fuji_width;
  7206. height = width - 1;
  7207. pixel_aspect = 1;
  7208. }
  7209. if (height == 2624 && width == 3936) /* Pentax K10D and Samsung GX10 */
  7210. { height = 2616; width = 3896; }
  7211. if (height == 3136 && width == 4864) /* Pentax K20D and Samsung GX20 */
  7212. { height = 3124; width = 4688; filters = 0x16161616; }
  7213. if (width == 4352 && (!strcmp(model,"K-r") || !strcmp(model,"K-x")))
  7214. { width = 4309; filters = 0x16161616; }
  7215. if (width >= 4960 && !strcmp(model,"K-5"))
  7216. { left_margin = 10; width = 4950; filters = 0x16161616; }
  7217. if (width == 4736 && !strcmp(model,"K-7"))
  7218. { height = 3122; width = 4684; filters = 0x16161616; top_margin = 2; }
  7219. if (width == 7424 && !strcmp(model,"645D"))
  7220. { height = 5502; width = 7328; filters = 0x61616161; top_margin = 29;
  7221. left_margin = 48; }
  7222. if (height == 3014 && width == 4096) /* Ricoh GX200 */
  7223. width = 4014;
  7224. if (dng_version) {
  7225. if (filters == UINT_MAX) filters = 0;
  7226. if (filters) is_raw = tiff_samples;
  7227. else colors = tiff_samples;
  7228. if (tiff_compress == 1)
  7229. load_raw = &CLASS adobe_dng_load_raw_nc;
  7230. if (tiff_compress == 7)
  7231. load_raw = &CLASS adobe_dng_load_raw_lj;
  7232. goto dng_skip;
  7233. }
  7234. if ((is_canon = !strcmp(make,"Canon")))
  7235. load_raw = memcmp (head+6,"HEAPCCDR",8) ?
  7236. &CLASS lossless_jpeg_load_raw : &CLASS canon_compressed_load_raw;
  7237. if (!strcmp(make,"NIKON")) {
  7238. if (!load_raw)
  7239. load_raw = &CLASS packed_load_raw;
  7240. if (model[0] == 'E')
  7241. load_flags |= !data_offset << 2 | 2;
  7242. }
  7243. if (!strcmp(make,"CASIO")) {
  7244. load_raw = &CLASS packed_load_raw;
  7245. maximum = 0xf7f;
  7246. }
  7247. /* Set parameters based on camera name (for non-DNG files). */
  7248. if (is_foveon) {
  7249. if (height*2 < width) pixel_aspect = 0.5;
  7250. if (height > width) pixel_aspect = 2;
  7251. filters = 0;
  7252. load_raw = &CLASS foveon_load_raw;
  7253. simple_coeff(0);
  7254. } else if (is_canon && tiff_bps == 15) {
  7255. switch (width) {
  7256. case 3344: width -= 66;
  7257. case 3872: width -= 6;
  7258. }
  7259. filters = 0;
  7260. load_raw = &CLASS canon_sraw_load_raw;
  7261. } else if (!strcmp(model,"PowerShot 600")) {
  7262. height = 613;
  7263. width = 854;
  7264. raw_width = 896;
  7265. pixel_aspect = 607/628.0;
  7266. colors = 4;
  7267. filters = 0xe1e4e1e4;
  7268. load_raw = &CLASS canon_600_load_raw;
  7269. } else if (!strcmp(model,"PowerShot A5") ||
  7270. !strcmp(model,"PowerShot A5 Zoom")) {
  7271. height = 773;
  7272. width = 960;
  7273. raw_width = 992;
  7274. pixel_aspect = 256/235.0;
  7275. colors = 4;
  7276. filters = 0x1e4e1e4e;
  7277. goto canon_a5;
  7278. } else if (!strcmp(model,"PowerShot A50")) {
  7279. height = 968;
  7280. width = 1290;
  7281. raw_width = 1320;
  7282. colors = 4;
  7283. filters = 0x1b4e4b1e;
  7284. goto canon_a5;
  7285. } else if (!strcmp(model,"PowerShot Pro70")) {
  7286. height = 1024;
  7287. width = 1552;
  7288. colors = 4;
  7289. filters = 0x1e4b4e1b;
  7290. goto canon_a5;
  7291. } else if (!strcmp(model,"PowerShot SD300")) {
  7292. height = 1752;
  7293. width = 2344;
  7294. raw_height = 1766;
  7295. raw_width = 2400;
  7296. top_margin = 12;
  7297. left_margin = 12;
  7298. goto canon_a5;
  7299. } else if (!strcmp(model,"PowerShot A460")) {
  7300. height = 1960;
  7301. width = 2616;
  7302. raw_height = 1968;
  7303. raw_width = 2664;
  7304. top_margin = 4;
  7305. left_margin = 4;
  7306. goto canon_a5;
  7307. } else if (!strcmp(model,"PowerShot A530")) {
  7308. height = 1984;
  7309. width = 2620;
  7310. raw_height = 1992;
  7311. raw_width = 2672;
  7312. top_margin = 6;
  7313. left_margin = 10;
  7314. goto canon_a5;
  7315. } else if (!strcmp(model,"PowerShot A610")) {
  7316. if (canon_s2is()) strcpy (model+10, "S2 IS");
  7317. height = 1960;
  7318. width = 2616;
  7319. raw_height = 1968;
  7320. raw_width = 2672;
  7321. top_margin = 8;
  7322. left_margin = 12;
  7323. goto canon_a5;
  7324. } else if (!strcmp(model,"PowerShot A620")) {
  7325. height = 2328;
  7326. width = 3112;
  7327. raw_height = 2340;
  7328. raw_width = 3152;
  7329. top_margin = 12;
  7330. left_margin = 36;
  7331. goto canon_a5;
  7332. } else if (!strcmp(model,"PowerShot A470")) {
  7333. height = 2328;
  7334. width = 3096;
  7335. raw_height = 2346;
  7336. raw_width = 3152;
  7337. top_margin = 6;
  7338. left_margin = 12;
  7339. goto canon_a5;
  7340. } else if (!strcmp(model,"PowerShot A720 IS")) {
  7341. height = 2472;
  7342. width = 3298;
  7343. raw_height = 2480;
  7344. raw_width = 3336;
  7345. top_margin = 5;
  7346. left_margin = 6;
  7347. goto canon_a5;
  7348. } else if (!strcmp(model,"PowerShot A630")) {
  7349. height = 2472;
  7350. width = 3288;
  7351. raw_height = 2484;
  7352. raw_width = 3344;
  7353. top_margin = 6;
  7354. left_margin = 12;
  7355. goto canon_a5;
  7356. } else if (!strcmp(model,"PowerShot A640")) {
  7357. height = 2760;
  7358. width = 3672;
  7359. raw_height = 2772;
  7360. raw_width = 3736;
  7361. top_margin = 6;
  7362. left_margin = 12;
  7363. goto canon_a5;
  7364. } else if (!strcmp(model,"PowerShot A650")) {
  7365. height = 3024;
  7366. width = 4032;
  7367. raw_height = 3048;
  7368. raw_width = 4104;
  7369. top_margin = 12;
  7370. left_margin = 48;
  7371. goto canon_a5;
  7372. } else if (!strcmp(model,"PowerShot S3 IS")) {
  7373. height = 2128;
  7374. width = 2840;
  7375. raw_height = 2136;
  7376. raw_width = 2888;
  7377. top_margin = 8;
  7378. left_margin = 44;
  7379. canon_a5:
  7380. tiff_bps = 10;
  7381. load_raw = &CLASS packed_load_raw;
  7382. load_flags = 40;
  7383. if (raw_width > 1600) zero_is_bad = 1;
  7384. } else if (!strcmp(model,"PowerShot SX110 IS")) {
  7385. height = 2760;
  7386. width = 3684;
  7387. raw_height = 2772;
  7388. raw_width = 3720;
  7389. top_margin = 12;
  7390. left_margin = 6;
  7391. load_raw = &CLASS packed_load_raw;
  7392. load_flags = 40;
  7393. zero_is_bad = 1;
  7394. } else if (!strcmp(model,"PowerShot SX120 IS")) {
  7395. height = 2742;
  7396. width = 3664;
  7397. raw_height = 2778;
  7398. raw_width = 3728;
  7399. top_margin = 18;
  7400. left_margin = 16;
  7401. filters = 0x49494949;
  7402. load_raw = &CLASS packed_load_raw;
  7403. load_flags = 40;
  7404. zero_is_bad = 1;
  7405. } else if (!strcmp(model,"PowerShot SX20 IS")) {
  7406. height = 3024;
  7407. width = 4032;
  7408. raw_height = 3048;
  7409. raw_width = 4080;
  7410. top_margin = 12;
  7411. left_margin = 24;
  7412. load_raw = &CLASS packed_load_raw;
  7413. load_flags = 40;
  7414. zero_is_bad = 1;
  7415. } else if (!strcmp(model,"PowerShot SX30 IS")) {
  7416. height = 3254;
  7417. width = 4366;
  7418. raw_height = 3276;
  7419. raw_width = 4464;
  7420. top_margin = 10;
  7421. left_margin = 25;
  7422. filters = 0x16161616;
  7423. load_raw = &CLASS packed_load_raw;
  7424. load_flags = 40;
  7425. zero_is_bad = 1;
  7426. } else if (!strcmp(model,"PowerShot Pro90 IS")) {
  7427. width = 1896;
  7428. colors = 4;
  7429. filters = 0xb4b4b4b4;
  7430. } else if (is_canon && raw_width == 2144) {
  7431. height = 1550;
  7432. width = 2088;
  7433. top_margin = 8;
  7434. left_margin = 4;
  7435. if (!strcmp(model,"PowerShot G1")) {
  7436. colors = 4;
  7437. filters = 0xb4b4b4b4;
  7438. }
  7439. } else if (is_canon && raw_width == 2224) {
  7440. height = 1448;
  7441. width = 2176;
  7442. top_margin = 6;
  7443. left_margin = 48;
  7444. } else if (is_canon && raw_width == 2376) {
  7445. height = 1720;
  7446. width = 2312;
  7447. top_margin = 6;
  7448. left_margin = 12;
  7449. } else if (is_canon && raw_width == 2672) {
  7450. height = 1960;
  7451. width = 2616;
  7452. top_margin = 6;
  7453. left_margin = 12;
  7454. } else if (is_canon && raw_width == 3152) {
  7455. height = 2056;
  7456. width = 3088;
  7457. top_margin = 12;
  7458. left_margin = 64;
  7459. if (unique_id == 0x80000170)
  7460. adobe_coeff ("Canon","EOS 300D");
  7461. } else if (is_canon && raw_width == 3160) {
  7462. height = 2328;
  7463. width = 3112;
  7464. top_margin = 12;
  7465. left_margin = 44;
  7466. } else if (is_canon && raw_width == 3344) {
  7467. height = 2472;
  7468. width = 3288;
  7469. top_margin = 6;
  7470. left_margin = 4;
  7471. } else if (!strcmp(model,"EOS D2000C")) {
  7472. filters = 0x61616161;
  7473. black = curve[200];
  7474. } else if (is_canon && raw_width == 3516) {
  7475. top_margin = 14;
  7476. left_margin = 42;
  7477. if (unique_id == 0x80000189)
  7478. adobe_coeff ("Canon","EOS 350D");
  7479. goto canon_cr2;
  7480. } else if (is_canon && raw_width == 3596) {
  7481. top_margin = 12;
  7482. left_margin = 74;
  7483. goto canon_cr2;
  7484. } else if (is_canon && raw_width == 3744) {
  7485. height = 2760;
  7486. width = 3684;
  7487. top_margin = 16;
  7488. left_margin = 8;
  7489. if (unique_id > 0x2720000) {
  7490. top_margin = 12;
  7491. left_margin = 52;
  7492. }
  7493. } else if (is_canon && raw_width == 3944) {
  7494. height = 2602;
  7495. width = 3908;
  7496. top_margin = 18;
  7497. left_margin = 30;
  7498. } else if (is_canon && raw_width == 3948) {
  7499. top_margin = 18;
  7500. left_margin = 42;
  7501. height -= 2;
  7502. if (unique_id == 0x80000236)
  7503. adobe_coeff ("Canon","EOS 400D");
  7504. if (unique_id == 0x80000254)
  7505. adobe_coeff ("Canon","EOS 1000D");
  7506. goto canon_cr2;
  7507. } else if (is_canon && raw_width == 3984) {
  7508. top_margin = 20;
  7509. left_margin = 76;
  7510. height -= 2;
  7511. goto canon_cr2;
  7512. } else if (is_canon && raw_width == 4104) {
  7513. height = 3024;
  7514. width = 4032;
  7515. top_margin = 12;
  7516. left_margin = 48;
  7517. } else if (is_canon && raw_width == 4152) {
  7518. top_margin = 12;
  7519. left_margin = 192;
  7520. goto canon_cr2;
  7521. } else if (is_canon && raw_width == 4160) {
  7522. height = 3048;
  7523. width = 4048;
  7524. top_margin = 11;
  7525. left_margin = 104;
  7526. } else if (is_canon && raw_width == 4312) {
  7527. top_margin = 18;
  7528. left_margin = 22;
  7529. height -= 2;
  7530. if (unique_id == 0x80000176)
  7531. adobe_coeff ("Canon","EOS 450D");
  7532. goto canon_cr2;
  7533. } else if (is_canon && raw_width == 4352) {
  7534. top_margin = 18;
  7535. left_margin = 62;
  7536. if (unique_id == 0x80000288)
  7537. adobe_coeff ("Canon","EOS 1100D");
  7538. goto canon_cr2;
  7539. } else if (is_canon && raw_width == 4476) {
  7540. top_margin = 34;
  7541. left_margin = 90;
  7542. goto canon_cr2;
  7543. } else if (is_canon && raw_width == 4480) {
  7544. height = 3326;
  7545. width = 4432;
  7546. top_margin = 10;
  7547. left_margin = 12;
  7548. filters = 0x49494949;
  7549. } else if (is_canon && raw_width == 4832) {
  7550. top_margin = unique_id == 0x80000261 ? 51:26;
  7551. left_margin = 62;
  7552. if (unique_id == 0x80000252)
  7553. adobe_coeff ("Canon","EOS 500D");
  7554. goto canon_cr2;
  7555. } else if (is_canon && raw_width == 5120) {
  7556. height -= top_margin = 45;
  7557. left_margin = 142;
  7558. width = 4916;
  7559. } else if (is_canon && raw_width == 5344) {
  7560. top_margin = 51;
  7561. left_margin = 142;
  7562. if (unique_id == 0x80000270)
  7563. adobe_coeff ("Canon","EOS 550D");
  7564. if (unique_id == 0x80000286)
  7565. adobe_coeff ("Canon","EOS 600D");
  7566. goto canon_cr2;
  7567. } else if (is_canon && raw_width == 5360) {
  7568. top_margin = 51;
  7569. left_margin = 158;
  7570. goto canon_cr2;
  7571. } else if (is_canon && raw_width == 5792) {
  7572. top_margin = 51;
  7573. left_margin = 158;
  7574. goto canon_cr2;
  7575. } else if (is_canon && raw_width == 5108) {
  7576. top_margin = 13;
  7577. left_margin = 98;
  7578. canon_cr2:
  7579. height -= top_margin;
  7580. width -= left_margin;
  7581. } else if (is_canon && raw_width == 5712) {
  7582. height = 3752;
  7583. width = 5640;
  7584. top_margin = 20;
  7585. left_margin = 62;
  7586. } else if (!strcmp(model,"D1")) {
  7587. cam_mul[0] *= 256/527.0;
  7588. cam_mul[2] *= 256/317.0;
  7589. } else if (!strcmp(model,"D1X")) {
  7590. width -= 4;
  7591. pixel_aspect = 0.5;
  7592. } else if (!strcmp(model,"D40X") ||
  7593. !strcmp(model,"D60") ||
  7594. !strcmp(model,"D80") ||
  7595. !strcmp(model,"D3000")) {
  7596. height -= 3;
  7597. width -= 4;
  7598. } else if (!strcmp(model,"D3") ||
  7599. !strcmp(model,"D3S") ||
  7600. !strcmp(model,"D700")) {
  7601. width -= 4;
  7602. left_margin = 2;
  7603. } else if (!strcmp(model,"D5000")) {
  7604. width -= 42;
  7605. } else if (!strcmp(model,"D5100") ||
  7606. !strcmp(model,"D7000")) {
  7607. width -= 44;
  7608. } else if (!strcmp(model,"D3100")) {
  7609. width -= 28;
  7610. left_margin = 6;
  7611. } else if (!strncmp(model,"D40",3) ||
  7612. !strncmp(model,"D50",3) ||
  7613. !strncmp(model,"D70",3)) {
  7614. width--;
  7615. } else if (!strcmp(model,"D90")) {
  7616. width -= 42;
  7617. } else if (!strcmp(model,"D100")) {
  7618. if (tiff_compress == 34713 && !nikon_is_compressed()) {
  7619. load_raw = &CLASS packed_load_raw;
  7620. load_flags |= 1;
  7621. raw_width = (width += 3) + 3;
  7622. }
  7623. } else if (!strcmp(model,"D200")) {
  7624. left_margin = 1;
  7625. width -= 4;
  7626. filters = 0x94949494;
  7627. } else if (!strncmp(model,"D2H",3)) {
  7628. left_margin = 6;
  7629. width -= 14;
  7630. } else if (!strncmp(model,"D2X",3)) {
  7631. if (width == 3264) width -= 32;
  7632. else width -= 8;
  7633. } else if (!strncmp(model,"D300",4)) {
  7634. width -= 32;
  7635. } else if (!strncmp(model,"COOLPIX P",9)) {
  7636. load_flags = 24;
  7637. filters = 0x94949494;
  7638. if (model[9] == '7' && iso_speed >= 400)
  7639. black = 255;
  7640. } else if (!strncmp(model,"1 ",2)) {
  7641. height -= 2;
  7642. } else if (fsize == 1581060) {
  7643. height = 963;
  7644. width = 1287;
  7645. raw_width = 1632;
  7646. maximum = 0x3f4;
  7647. colors = 4;
  7648. filters = 0x1e1e1e1e;
  7649. simple_coeff(3);
  7650. pre_mul[0] = 1.2085;
  7651. pre_mul[1] = 1.0943;
  7652. pre_mul[3] = 1.1103;
  7653. #ifdef LIBRAW_LIBRARY_BUILD
  7654. color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
  7655. #endif
  7656. goto e900;
  7657. } else if (fsize == 2465792) {
  7658. height = 1203;
  7659. width = 1616;
  7660. raw_width = 2048;
  7661. colors = 4;
  7662. filters = 0x4b4b4b4b;
  7663. adobe_coeff ("NIKON","E950");
  7664. e900:
  7665. tiff_bps = 10;
  7666. load_raw = &CLASS packed_load_raw;
  7667. load_flags = 6;
  7668. } else if (fsize == 4771840) {
  7669. height = 1540;
  7670. width = 2064;
  7671. colors = 4;
  7672. filters = 0xe1e1e1e1;
  7673. load_raw = &CLASS packed_load_raw;
  7674. load_flags = 6;
  7675. if (!timestamp && nikon_e995())
  7676. strcpy (model, "E995");
  7677. if (strcmp(model,"E995")) {
  7678. filters = 0xb4b4b4b4;
  7679. simple_coeff(3);
  7680. pre_mul[0] = 1.196;
  7681. pre_mul[1] = 1.246;
  7682. pre_mul[2] = 1.018;
  7683. #ifdef LIBRAW_LIBRARY_BUILD
  7684. color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
  7685. #endif
  7686. }
  7687. } else if (!strcmp(model,"E2100")) {
  7688. if (!timestamp && !nikon_e2100()) goto cp_e2500;
  7689. height = 1206;
  7690. width = 1616;
  7691. load_flags = 30;
  7692. } else if (!strcmp(model,"E2500")) {
  7693. cp_e2500:
  7694. strcpy (model, "E2500");
  7695. height = 1204;
  7696. width = 1616;
  7697. colors = 4;
  7698. filters = 0x4b4b4b4b;
  7699. } else if (fsize == 4775936) {
  7700. height = 1542;
  7701. width = 2064;
  7702. load_raw = &CLASS packed_load_raw;
  7703. load_flags = 30;
  7704. if (!timestamp) nikon_3700();
  7705. if (model[0] == 'E' && atoi(model+1) < 3700)
  7706. filters = 0x49494949;
  7707. if (!strcmp(model,"Optio 33WR")) {
  7708. flip = 1;
  7709. filters = 0x16161616;
  7710. }
  7711. if (make[0] == 'O') {
  7712. i = find_green (12, 32, 1188864, 3576832);
  7713. c = find_green (12, 32, 2383920, 2387016);
  7714. if (abs(i) < abs(c)) {
  7715. SWAP(i,c);
  7716. load_flags = 24;
  7717. }
  7718. if (i < 0) filters = 0x61616161;
  7719. }
  7720. } else if (fsize == 5869568) {
  7721. height = 1710;
  7722. width = 2288;
  7723. filters = 0x16161616;
  7724. if (!timestamp && minolta_z2()) {
  7725. strcpy (make, "Minolta");
  7726. strcpy (model,"DiMAGE Z2");
  7727. }
  7728. load_raw = &CLASS packed_load_raw;
  7729. load_flags = 6 + 24*(make[0] == 'M');
  7730. } else if (!strcmp(model,"E4500")) {
  7731. height = 1708;
  7732. width = 2288;
  7733. colors = 4;
  7734. filters = 0xb4b4b4b4;
  7735. } else if (fsize == 7438336) {
  7736. height = 1924;
  7737. width = 2576;
  7738. colors = 4;
  7739. filters = 0xb4b4b4b4;
  7740. } else if (fsize == 8998912) {
  7741. height = 2118;
  7742. width = 2832;
  7743. maximum = 0xf83;
  7744. load_raw = &CLASS packed_load_raw;
  7745. load_flags = 30;
  7746. } else if (!strcmp(make,"FUJIFILM")) {
  7747. if (!strcmp(model+7,"S2Pro")) {
  7748. strcpy (model+7," S2Pro");
  7749. height = 2144;
  7750. width = 2880;
  7751. flip = 6;
  7752. } else if (load_raw != &CLASS packed_load_raw)
  7753. maximum = (is_raw == 2 && shot_select) ? 0x2f00 : 0x3e00;
  7754. top_margin = (raw_height - height) >> 2 << 1;
  7755. left_margin = (raw_width - width ) >> 2 << 1;
  7756. if (width == 3328) {
  7757. width = 3262;
  7758. left_margin = 34;
  7759. }
  7760. if (!strcmp(model,"X10"))
  7761. filters = 0x16161616;
  7762. if (fuji_layout) raw_width *= is_raw;
  7763. if (load_raw == &CLASS fuji_load_raw) {
  7764. fuji_width = width >> !fuji_layout;
  7765. width = (height >> fuji_layout) + fuji_width;
  7766. raw_height = height;
  7767. height = width - 1;
  7768. if (~fuji_width & 1) filters = 0x49494949;
  7769. }
  7770. } else if (!strcmp(model,"RD175")) {
  7771. height = 986;
  7772. width = 1534;
  7773. data_offset = 513;
  7774. filters = 0x61616161;
  7775. load_raw = &CLASS minolta_rd175_load_raw;
  7776. } else if (!strcmp(model,"KD-400Z")) {
  7777. height = 1712;
  7778. width = 2312;
  7779. raw_width = 2336;
  7780. goto konica_400z;
  7781. } else if (!strcmp(model,"KD-510Z")) {
  7782. goto konica_510z;
  7783. } else if (!strcasecmp(make,"MINOLTA")) {
  7784. load_raw = &CLASS unpacked_load_raw;
  7785. maximum = 0xfff;
  7786. if (!strncmp(model,"DiMAGE A",8)) {
  7787. if (!strcmp(model,"DiMAGE A200"))
  7788. filters = 0x49494949;
  7789. tiff_bps = 12;
  7790. load_raw = &CLASS packed_load_raw;
  7791. } else if (!strncmp(model,"ALPHA",5) ||
  7792. !strncmp(model,"DYNAX",5) ||
  7793. !strncmp(model,"MAXXUM",6)) {
  7794. sprintf (model+20, "DYNAX %-10s", model+6+(model[0]=='M'));
  7795. adobe_coeff (make, model+20);
  7796. load_raw = &CLASS packed_load_raw;
  7797. } else if (!strncmp(model,"DiMAGE G",8)) {
  7798. if (model[8] == '4') {
  7799. height = 1716;
  7800. width = 2304;
  7801. } else if (model[8] == '5') {
  7802. konica_510z:
  7803. height = 1956;
  7804. width = 2607;
  7805. raw_width = 2624;
  7806. } else if (model[8] == '6') {
  7807. height = 2136;
  7808. width = 2848;
  7809. }
  7810. data_offset += 14;
  7811. filters = 0x61616161;
  7812. konica_400z:
  7813. load_raw = &CLASS unpacked_load_raw;
  7814. maximum = 0x3df;
  7815. order = 0x4d4d;
  7816. }
  7817. } else if (!strcmp(model,"*ist D")) {
  7818. data_error = -1;
  7819. } else if (!strcmp(model,"*ist DS")) {
  7820. height -= 2;
  7821. } else if (!strcmp(model,"Optio S")) {
  7822. if (fsize == 3178560) {
  7823. height = 1540;
  7824. width = 2064;
  7825. load_raw = &CLASS eight_bit_load_raw;
  7826. cam_mul[0] *= 4;
  7827. cam_mul[2] *= 4;
  7828. } else {
  7829. height = 1544;
  7830. width = 2068;
  7831. raw_width = 3136;
  7832. load_raw = &CLASS packed_load_raw;
  7833. maximum = 0xf7c;
  7834. }
  7835. } else if (fsize == 6114240) {
  7836. height = 1737;
  7837. width = 2324;
  7838. raw_width = 3520;
  7839. load_raw = &CLASS packed_load_raw;
  7840. maximum = 0xf7a;
  7841. } else if (!strcmp(model,"Optio 750Z")) {
  7842. height = 2302;
  7843. width = 3072;
  7844. load_raw = &CLASS packed_load_raw;
  7845. load_flags = 30;
  7846. } else if (!strcmp(model,"DC-833m")) {
  7847. height = 2448;
  7848. width = 3264;
  7849. order = 0x4949;
  7850. filters = 0x61616161;
  7851. load_raw = &CLASS unpacked_load_raw;
  7852. maximum = 0xfc00;
  7853. } else if (!strncmp(model,"S85",3)) {
  7854. height = 2448;
  7855. width = 3264;
  7856. raw_width = fsize/height/2;
  7857. order = 0x4d4d;
  7858. load_raw = &CLASS unpacked_load_raw;
  7859. } else if (!strncmp(model,"NX1",3)) {
  7860. height -= top_margin = 8;
  7861. width -= 2 * (left_margin = 8);
  7862. load_flags = 32;
  7863. } else if (!strcmp(model,"NX200")) {
  7864. order = 0x4949;
  7865. height = 3662;
  7866. width = 5528;
  7867. top_margin = 2;
  7868. left_margin = 46;
  7869. } else if (!strcmp(model,"EX1")) {
  7870. order = 0x4949;
  7871. height -= 20;
  7872. top_margin = 2;
  7873. if ((width -= 6) > 3682) {
  7874. height -= 10;
  7875. width -= 46;
  7876. top_margin = 8;
  7877. }
  7878. } else if (!strcmp(model,"WB2000")) {
  7879. order = 0x4949;
  7880. height -= 3;
  7881. top_margin = 2;
  7882. if ((width -= 10) > 3718) {
  7883. height -= 28;
  7884. width -= 56;
  7885. top_margin = 8;
  7886. }
  7887. } else if (fsize == 20487168) {
  7888. height = 2808;
  7889. width = 3648;
  7890. goto wb550;
  7891. } else if (fsize == 24000000) {
  7892. height = 3000;
  7893. width = 4000;
  7894. wb550:
  7895. strcpy (model, "WB550");
  7896. order = 0x4d4d;
  7897. load_raw = &CLASS unpacked_load_raw;
  7898. load_flags = 6;
  7899. maximum = 0x3df;
  7900. } else if (!strcmp(model,"STV680 VGA")) {
  7901. height = 484;
  7902. width = 644;
  7903. load_raw = &CLASS eight_bit_load_raw;
  7904. flip = 2;
  7905. filters = 0x16161616;
  7906. black = 16;
  7907. } else if (!strcmp(model,"N95")) {
  7908. height = raw_height - (top_margin = 2);
  7909. } else if (!strcmp(model,"531C")) {
  7910. height = 1200;
  7911. width = 1600;
  7912. load_raw = &CLASS unpacked_load_raw;
  7913. filters = 0x49494949;
  7914. } else if (!strcmp(model,"F-080C")) {
  7915. height = 768;
  7916. width = 1024;
  7917. load_raw = &CLASS eight_bit_load_raw;
  7918. } else if (!strcmp(model,"F-145C")) {
  7919. height = 1040;
  7920. width = 1392;
  7921. load_raw = &CLASS eight_bit_load_raw;
  7922. } else if (!strcmp(model,"F-201C")) {
  7923. height = 1200;
  7924. width = 1600;
  7925. load_raw = &CLASS eight_bit_load_raw;
  7926. } else if (!strcmp(model,"F-510C")) {
  7927. height = 1958;
  7928. width = 2588;
  7929. load_raw = fsize < 7500000 ?
  7930. &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
  7931. data_offset = fsize - width*height*(fsize >> 22);
  7932. maximum = 0xfff0;
  7933. } else if (!strcmp(model,"F-810C")) {
  7934. height = 2469;
  7935. width = 3272;
  7936. load_raw = &CLASS unpacked_load_raw;
  7937. maximum = 0xfff0;
  7938. } else if (!strcmp(model,"XCD-SX910CR")) {
  7939. height = 1024;
  7940. width = 1375;
  7941. raw_width = 1376;
  7942. filters = 0x49494949;
  7943. maximum = 0x3ff;
  7944. load_raw = fsize < 2000000 ?
  7945. &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
  7946. } else if (!strcmp(model,"2010")) {
  7947. height = 1207;
  7948. width = 1608;
  7949. order = 0x4949;
  7950. filters = 0x16161616;
  7951. data_offset = 3212;
  7952. maximum = 0x3ff;
  7953. load_raw = &CLASS unpacked_load_raw;
  7954. } else if (!strcmp(model,"A782")) {
  7955. height = 3000;
  7956. width = 2208;
  7957. filters = 0x61616161;
  7958. load_raw = fsize < 10000000 ?
  7959. &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
  7960. maximum = 0xffc0;
  7961. } else if (!strcmp(model,"3320AF")) {
  7962. height = 1536;
  7963. raw_width = width = 2048;
  7964. filters = 0x61616161;
  7965. load_raw = &CLASS unpacked_load_raw;
  7966. maximum = 0x3ff;
  7967. fseek (ifp, 0x300000, SEEK_SET);
  7968. if ((order = guess_byte_order(0x10000)) == 0x4d4d) {
  7969. height -= (top_margin = 16);
  7970. width -= (left_margin = 28);
  7971. maximum = 0xf5c0;
  7972. strcpy (make, "ISG");
  7973. model[0] = 0;
  7974. }
  7975. } else if (!strcmp(make,"Hasselblad")) {
  7976. if (load_raw == &CLASS lossless_jpeg_load_raw)
  7977. load_raw = &CLASS hasselblad_load_raw;
  7978. if (raw_width == 7262) {
  7979. height = 5444;
  7980. width = 7248;
  7981. top_margin = 4;
  7982. left_margin = 7;
  7983. filters = 0x61616161;
  7984. } else if (raw_width == 7410) {
  7985. height = 5502;
  7986. width = 7328;
  7987. top_margin = 4;
  7988. left_margin = 41;
  7989. filters = 0x61616161;
  7990. } else if (raw_width == 9044) {
  7991. height = 6716;
  7992. width = 8964;
  7993. top_margin = 8;
  7994. left_margin = 40;
  7995. black += load_flags = 256;
  7996. maximum = 0x8101;
  7997. } else if (raw_width == 4090) {
  7998. strcpy (model, "V96C");
  7999. height -= (top_margin = 6);
  8000. width -= (left_margin = 3) + 7;
  8001. filters = 0x61616161;
  8002. }
  8003. } else if (!strcmp(make,"Sinar")) {
  8004. if (!memcmp(head,"8BPS",4)) {
  8005. fseek (ifp, 14, SEEK_SET);
  8006. height = get4();
  8007. width = get4();
  8008. filters = 0x61616161;
  8009. data_offset = 68;
  8010. }
  8011. if (!load_raw) load_raw = &CLASS unpacked_load_raw;
  8012. maximum = 0x3fff;
  8013. } else if (!strcmp(make,"Leaf")) {
  8014. maximum = 0x3fff;
  8015. fseek (ifp, data_offset, SEEK_SET);
  8016. if (ljpeg_start (&jh, 1) && jh.bits == 15)
  8017. maximum = 0x1fff;
  8018. if (tiff_samples > 1) filters = 0;
  8019. if (tiff_samples > 1 || tile_length < raw_height) {
  8020. load_raw = &CLASS leaf_hdr_load_raw;
  8021. raw_width = tile_width;
  8022. }
  8023. if ((width | height) == 2048) {
  8024. if (tiff_samples == 1) {
  8025. filters = 1;
  8026. strcpy (cdesc, "RBTG");
  8027. strcpy (model, "CatchLight");
  8028. top_margin = 8; left_margin = 18; height = 2032; width = 2016;
  8029. } else {
  8030. strcpy (model, "DCB2");
  8031. top_margin = 10; left_margin = 16; height = 2028; width = 2022;
  8032. }
  8033. } else if (width+height == 3144+2060) {
  8034. if (!model[0]) strcpy (model, "Cantare");
  8035. if (width > height) {
  8036. top_margin = 6; left_margin = 32; height = 2048; width = 3072;
  8037. filters = 0x61616161;
  8038. } else {
  8039. left_margin = 6; top_margin = 32; width = 2048; height = 3072;
  8040. filters = 0x16161616;
  8041. }
  8042. if (!cam_mul[0] || model[0] == 'V') filters = 0;
  8043. else is_raw = tiff_samples;
  8044. } else if (width == 2116) {
  8045. strcpy (model, "Valeo 6");
  8046. height -= 2 * (top_margin = 30);
  8047. width -= 2 * (left_margin = 55);
  8048. filters = 0x49494949;
  8049. } else if (width == 3171) {
  8050. strcpy (model, "Valeo 6");
  8051. height -= 2 * (top_margin = 24);
  8052. width -= 2 * (left_margin = 24);
  8053. filters = 0x16161616;
  8054. }
  8055. } else if (!strcmp(make,"LEICA") || !strcmp(make,"Panasonic")) {
  8056. if(raw_width < 1) { is_raw = 0; goto notraw; }
  8057. if ((flen - data_offset) / (raw_width*8/7) == raw_height)
  8058. load_raw = &CLASS panasonic_load_raw;
  8059. if (!load_raw) {
  8060. load_raw = &CLASS unpacked_load_raw;
  8061. load_flags = 4;
  8062. }
  8063. zero_is_bad = 1;
  8064. if ((height += 12) > raw_height) height = raw_height;
  8065. for (i=0; i < sizeof pana / sizeof *pana; i++)
  8066. if (raw_width == pana[i][0] && raw_height == pana[i][1]) {
  8067. left_margin = pana[i][2];
  8068. top_margin = pana[i][3];
  8069. width += pana[i][4];
  8070. height += pana[i][5];
  8071. }
  8072. filters = 0x01010101 * (uchar) "\x94\x61\x49\x16"
  8073. [((filters-1) ^ (left_margin & 1) ^ (top_margin << 1)) & 3];
  8074. } else if (!strcmp(model,"C770UZ")) {
  8075. height = 1718;
  8076. width = 2304;
  8077. filters = 0x16161616;
  8078. load_raw = &CLASS packed_load_raw;
  8079. load_flags = 30;
  8080. } else if (!strcmp(make,"OLYMPUS")) {
  8081. height += height & 1;
  8082. filters = exif_cfa;
  8083. if (width == 4100) width -= 4;
  8084. if (width == 4080) width -= 24;
  8085. if (load_raw == &CLASS unpacked_load_raw)
  8086. load_flags = 4;
  8087. tiff_bps = 12;
  8088. if (!strcmp(model,"E-300") ||
  8089. !strcmp(model,"E-500")) {
  8090. width -= 20;
  8091. if (load_raw == &CLASS unpacked_load_raw) {
  8092. maximum = 0xfc3;
  8093. memset (cblack, 0, sizeof cblack);
  8094. }
  8095. } else if (!strcmp(model,"E-330")) {
  8096. width -= 30;
  8097. if (load_raw == &CLASS unpacked_load_raw)
  8098. maximum = 0xf79;
  8099. } else if (!strcmp(model,"SP550UZ")) {
  8100. thumb_length = flen - (thumb_offset = 0xa39800);
  8101. thumb_height = 480;
  8102. thumb_width = 640;
  8103. }
  8104. } else if (!strcmp(model,"N Digital")) {
  8105. height = 2047;
  8106. width = 3072;
  8107. filters = 0x61616161;
  8108. data_offset = 0x1a00;
  8109. load_raw = &CLASS packed_load_raw;
  8110. } else if (!strcmp(model,"DSC-F828")) {
  8111. width = 3288;
  8112. left_margin = 5;
  8113. data_offset = 862144;
  8114. load_raw = &CLASS sony_load_raw;
  8115. filters = 0x9c9c9c9c;
  8116. colors = 4;
  8117. strcpy (cdesc, "RGBE");
  8118. } else if (!strcmp(model,"DSC-V3")) {
  8119. width = 3109;
  8120. left_margin = 59;
  8121. data_offset = 787392;
  8122. load_raw = &CLASS sony_load_raw;
  8123. } else if (!strcmp(make,"SONY") && raw_width == 3984) {
  8124. adobe_coeff ("SONY","DSC-R1");
  8125. width = 3925;
  8126. order = 0x4d4d;
  8127. } else if (!strcmp(make,"SONY") && raw_width == 6048) {
  8128. width -= 24;
  8129. } else if (!strcmp(model,"DSLR-A100")) {
  8130. if (width == 3880) {
  8131. height--;
  8132. width = ++raw_width;
  8133. } else {
  8134. order = 0x4d4d;
  8135. load_flags = 2;
  8136. }
  8137. filters = 0x61616161;
  8138. } else if (!strcmp(model,"DSLR-A350")) {
  8139. height -= 4;
  8140. }
  8141. else
  8142. identify2(fsize,flen,head); /* Avoid MS VS 2008 bug */
  8143. if (!model[0])
  8144. sprintf (model, "%dx%d", width, height);
  8145. if (filters == UINT_MAX) filters = 0x94949494;
  8146. if (raw_color) adobe_coeff (make, model);
  8147. if (load_raw == &CLASS kodak_radc_load_raw)
  8148. if (raw_color) adobe_coeff ("Apple","Quicktake");
  8149. if (thumb_offset && !thumb_height) {
  8150. fseek (ifp, thumb_offset, SEEK_SET);
  8151. if (ljpeg_start (&jh, 1)) {
  8152. thumb_width = jh.wide;
  8153. thumb_height = jh.high;
  8154. }
  8155. }
  8156. dng_skip:
  8157. if (!tiff_bps) tiff_bps = 12;
  8158. if (!maximum) maximum = (1 << tiff_bps) - 1;
  8159. if (!load_raw || height < 22) is_raw = 0;
  8160. #ifdef NO_JASPER
  8161. if (load_raw == &CLASS redcine_load_raw) {
  8162. #ifdef DCRAW_VERBOSE
  8163. fprintf (stderr,_("%s: You must link dcraw with %s!!\n"),
  8164. ifname, "libjasper");
  8165. #endif
  8166. is_raw = 0;
  8167. #ifdef LIBRAW_LIBRARY_BUILD
  8168. imgdata.process_warnings |= LIBRAW_WARN_NO_JASPER;
  8169. #endif
  8170. }
  8171. #endif
  8172. #ifdef NO_JPEG
  8173. if (load_raw == &CLASS kodak_jpeg_load_raw) {
  8174. #ifdef DCRAW_VERBOSE
  8175. fprintf (stderr,_("%s: You must link dcraw with %s!!\n"),
  8176. ifname, "libjpeg");
  8177. #endif
  8178. is_raw = 0;
  8179. #ifdef LIBRAW_LIBRARY_BUILD
  8180. imgdata.process_warnings |= LIBRAW_WARN_NO_JPEGLIB;
  8181. #endif
  8182. }
  8183. #endif
  8184. if (!cdesc[0])
  8185. strcpy (cdesc, colors == 3 ? "RGBG":"GMCY");
  8186. if (!raw_height) raw_height = height;
  8187. if (!raw_width ) raw_width = width;
  8188. if (filters && colors == 3)
  8189. filters |= ((filters >> 2 & 0x22222222) |
  8190. (filters << 2 & 0x88888888)) & filters << 1;
  8191. notraw:
  8192. if (flip == -1) flip = tiff_flip;
  8193. if (flip == -1) flip = 0;
  8194. #ifdef LIBRAW_LIBRARY_BUILD
  8195. RUN_CALLBACK(LIBRAW_PROGRESS_IDENTIFY,1,2);
  8196. #endif
  8197. }
  8198. void CLASS identify2(unsigned fsize, unsigned flen, char *head)
  8199. {
  8200. if (!strcmp(model,"PIXL")) {
  8201. height -= top_margin = 4;
  8202. width -= left_margin = 32;
  8203. gamma_curve (0, 7, 1, 255);
  8204. } else if (!strcmp(model,"C603v")) {
  8205. height = 480;
  8206. width = 640;
  8207. if (fsize < 614400 || find_green (16, 16, 3840, 5120) < 25) goto c603v;
  8208. strcpy (model,"KAI-0340");
  8209. height -= 3;
  8210. data_offset = 3840;
  8211. order = 0x4949;
  8212. load_raw = &CLASS unpacked_load_raw;
  8213. } else if (!strcmp(model,"C603y")) {
  8214. height = 2134;
  8215. width = 2848;
  8216. c603v:
  8217. filters = 0;
  8218. load_raw = &CLASS kodak_yrgb_load_raw;
  8219. gamma_curve (0, 3.875, 1, 255);
  8220. } else if (!strcmp(model,"C603")) {
  8221. raw_height = height = 2152;
  8222. raw_width = width = 2864;
  8223. goto c603;
  8224. } else if (!strcmp(model,"C330")) {
  8225. height = 1744;
  8226. width = 2336;
  8227. raw_height = 1779;
  8228. raw_width = 2338;
  8229. top_margin = 33;
  8230. left_margin = 1;
  8231. c603:
  8232. order = 0x4949;
  8233. if ((data_offset = fsize - raw_height*raw_width)) {
  8234. fseek (ifp, 168, SEEK_SET);
  8235. read_shorts (curve, 256);
  8236. #ifdef LIBRAW_LIBRARY_BUILD
  8237. color_flags.curve_state = LIBRAW_COLORSTATE_LOADED;
  8238. #endif
  8239. } else gamma_curve (0, 3.875, 1, 255);
  8240. load_raw = &CLASS eight_bit_load_raw;
  8241. }
  8242. else if (!strncasecmp(model,"EasyShare",9)) {
  8243. data_offset = data_offset < 0x15000 ? 0x15000 : 0x17000;
  8244. load_raw = &CLASS packed_load_raw;
  8245. } else if (!strcasecmp(make,"KODAK")) {
  8246. if (filters == UINT_MAX) filters = 0x61616161;
  8247. if (!strncmp(model,"NC2000",6)) {
  8248. width -= 4;
  8249. left_margin = 2;
  8250. } else if (!strcmp(model,"EOSDCS3B")) {
  8251. width -= 4;
  8252. left_margin = 2;
  8253. } else if (!strcmp(model,"EOSDCS1")) {
  8254. width -= 4;
  8255. left_margin = 2;
  8256. } else if (!strcmp(model,"DCS420")) {
  8257. width -= 4;
  8258. left_margin = 2;
  8259. } else if (!strncmp(model,"DCS460 ",7)) {
  8260. model[6] = 0;
  8261. width -= 4;
  8262. left_margin = 2;
  8263. } else if (!strcmp(model,"DCS460A")) {
  8264. width -= 4;
  8265. left_margin = 2;
  8266. colors = 1;
  8267. filters = 0;
  8268. } else if (!strcmp(model,"DCS660M")) {
  8269. black = 214;
  8270. colors = 1;
  8271. filters = 0;
  8272. } else if (!strcmp(model,"DCS760M")) {
  8273. colors = 1;
  8274. filters = 0;
  8275. }
  8276. if (!strcmp(model+4,"20X"))
  8277. strcpy (cdesc, "MYCY");
  8278. if (strstr(model,"DC25")) {
  8279. strcpy (model, "DC25");
  8280. data_offset = 15424;
  8281. }
  8282. if (!strncmp(model,"DC2",3)) {
  8283. height = 242;
  8284. if (flen < 100000) {
  8285. raw_width = 256; width = 249;
  8286. pixel_aspect = (4.0*height) / (3.0*width);
  8287. } else {
  8288. raw_width = 512; width = 501;
  8289. pixel_aspect = (493.0*height) / (373.0*width);
  8290. }
  8291. data_offset += raw_width + 1;
  8292. colors = 4;
  8293. filters = 0x8d8d8d8d;
  8294. simple_coeff(1);
  8295. pre_mul[1] = 1.179;
  8296. pre_mul[2] = 1.209;
  8297. pre_mul[3] = 1.036;
  8298. #ifdef LIBRAW_LIBRARY_BUILD
  8299. color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
  8300. #endif
  8301. load_raw = &CLASS eight_bit_load_raw;
  8302. } else if (!strcmp(model,"40")) {
  8303. strcpy (model, "DC40");
  8304. height = 512;
  8305. width = 768;
  8306. data_offset = 1152;
  8307. load_raw = &CLASS kodak_radc_load_raw;
  8308. } else if (strstr(model,"DC50")) {
  8309. strcpy (model, "DC50");
  8310. height = 512;
  8311. width = 768;
  8312. data_offset = 19712;
  8313. load_raw = &CLASS kodak_radc_load_raw;
  8314. } else if (strstr(model,"DC120")) {
  8315. strcpy (model, "DC120");
  8316. height = 976;
  8317. width = 848;
  8318. pixel_aspect = height/0.75/width;
  8319. load_raw = tiff_compress == 7 ?
  8320. &CLASS kodak_jpeg_load_raw : &CLASS kodak_dc120_load_raw;
  8321. } else if (!strcmp(model,"DCS200")) {
  8322. thumb_height = 128;
  8323. thumb_width = 192;
  8324. thumb_offset = 6144;
  8325. thumb_misc = 360;
  8326. write_thumb = &CLASS layer_thumb;
  8327. height = 1024;
  8328. width = 1536;
  8329. data_offset = 79872;
  8330. load_raw = &CLASS eight_bit_load_raw;
  8331. black = 17;
  8332. }
  8333. } else if (!strcmp(model,"Fotoman Pixtura")) {
  8334. height = 512;
  8335. width = 768;
  8336. data_offset = 3632;
  8337. load_raw = &CLASS kodak_radc_load_raw;
  8338. filters = 0x61616161;
  8339. simple_coeff(2);
  8340. } else if (!strncmp(model,"QuickTake",9)) {
  8341. if (head[5]) strcpy (model+10, "200");
  8342. fseek (ifp, 544, SEEK_SET);
  8343. height = get2();
  8344. width = get2();
  8345. data_offset = (get4(),get2()) == 30 ? 738:736;
  8346. if (height > width) {
  8347. SWAP(height,width);
  8348. fseek (ifp, data_offset-6, SEEK_SET);
  8349. flip = ~get2() & 3 ? 5:6;
  8350. }
  8351. filters = 0x61616161;
  8352. } else if (!strcmp(make,"Rollei") && !load_raw) {
  8353. switch (raw_width) {
  8354. case 1316:
  8355. height = 1030;
  8356. width = 1300;
  8357. top_margin = 1;
  8358. left_margin = 6;
  8359. break;
  8360. case 2568:
  8361. height = 1960;
  8362. width = 2560;
  8363. top_margin = 2;
  8364. left_margin = 8;
  8365. }
  8366. filters = 0x16161616;
  8367. load_raw = &CLASS rollei_load_raw;
  8368. } else if (!strcmp(model,"PC-CAM 600")) {
  8369. height = 768;
  8370. data_offset = width = 1024;
  8371. filters = 0x49494949;
  8372. load_raw = &CLASS eight_bit_load_raw;
  8373. }
  8374. else if (!strcmp(model,"QV-2000UX")) {
  8375. height = 1208;
  8376. width = 1632;
  8377. data_offset = width * 2;
  8378. load_raw = &CLASS eight_bit_load_raw;
  8379. } else if (fsize == 3217760) {
  8380. height = 1546;
  8381. width = 2070;
  8382. raw_width = 2080;
  8383. load_raw = &CLASS eight_bit_load_raw;
  8384. } else if (!strcmp(model,"QV-4000")) {
  8385. height = 1700;
  8386. width = 2260;
  8387. load_raw = &CLASS unpacked_load_raw;
  8388. maximum = 0xffff;
  8389. } else if (!strcmp(model,"QV-5700")) {
  8390. height = 1924;
  8391. width = 2576;
  8392. raw_width = 3232;
  8393. tiff_bps = 10;
  8394. } else if (!strcmp(model,"QV-R41")) {
  8395. height = 1720;
  8396. width = 2312;
  8397. raw_width = 3520;
  8398. left_margin = 2;
  8399. } else if (!strcmp(model,"QV-R51")) {
  8400. height = 1926;
  8401. width = 2580;
  8402. raw_width = 3904;
  8403. } else if (!strcmp(model,"EX-S20")) {
  8404. height = 1208;
  8405. width = 1620;
  8406. raw_width = 2432;
  8407. flip = 3;
  8408. } else if (!strcmp(model,"EX-S100")) {
  8409. height = 1544;
  8410. width = 2058;
  8411. raw_width = 3136;
  8412. } else if (!strcmp(model,"EX-Z50")) {
  8413. height = 1931;
  8414. width = 2570;
  8415. raw_width = 3904;
  8416. } else if (!strcmp(model,"EX-Z55")) {
  8417. height = 1960;
  8418. width = 2570;
  8419. raw_width = 3904;
  8420. } else if (!strcmp(model,"EX-Z60")) {
  8421. height = 2145;
  8422. width = 2833;
  8423. raw_width = 3584;
  8424. filters = 0x16161616;
  8425. tiff_bps = 10;
  8426. } else if (!strcmp(model,"EX-Z75")) {
  8427. height = 2321;
  8428. width = 3089;
  8429. raw_width = 4672;
  8430. } else if (!strcmp(model,"EX-Z750")) {
  8431. height = 2319;
  8432. width = 3087;
  8433. raw_width = 4672;
  8434. maximum = 0xfff;
  8435. } else if (!strcmp(model,"EX-Z850")) {
  8436. height = 2468;
  8437. width = 3279;
  8438. raw_width = 4928;
  8439. maximum = 0xfff;
  8440. } else if (fsize == 15499264) { /* EX-Z1050 or EX-Z1080 */
  8441. height = 2752;
  8442. width = 3672;
  8443. raw_width = 5632;
  8444. } else if (!strcmp(model,"EX-P505")) {
  8445. height = 1928;
  8446. width = 2568;
  8447. raw_width = 3852;
  8448. maximum = 0xfff;
  8449. } else if (fsize == 9313536) { /* EX-P600 or QV-R61 */
  8450. height = 2142;
  8451. width = 2844;
  8452. raw_width = 4288;
  8453. } else if (!strcmp(model,"EX-P700")) {
  8454. height = 2318;
  8455. width = 3082;
  8456. raw_width = 4672;
  8457. }
  8458. else if (!strcmp(model,"GRAS-50S5C")) {
  8459. height = 2048;
  8460. width = 2440;
  8461. load_raw = &CLASS unpacked_load_raw;
  8462. data_offset = 0;
  8463. filters = 0x49494949;
  8464. order = 0x4949;
  8465. maximum = 0xfffC;
  8466. } else if (!strcmp(model,"BB-500CL")) {
  8467. height = 2058;
  8468. width = 2448;
  8469. load_raw = &CLASS unpacked_load_raw;
  8470. data_offset = 0;
  8471. filters = 0x94949494;
  8472. order = 0x4949;
  8473. maximum = 0x3fff;
  8474. } else if (!strcmp(model,"BB-500GE")) {
  8475. height = 2058;
  8476. width = 2456;
  8477. load_raw = &CLASS unpacked_load_raw;
  8478. data_offset = 0;
  8479. filters = 0x94949494;
  8480. order = 0x4949;
  8481. maximum = 0x3fff;
  8482. } else if (!strcmp(model,"SVS625CL")) {
  8483. height = 2050;
  8484. width = 2448;
  8485. load_raw = &CLASS unpacked_load_raw;
  8486. data_offset = 0;
  8487. filters = 0x94949494;
  8488. order = 0x4949;
  8489. maximum = 0x0fff;
  8490. }
  8491. }
  8492. void CLASS convert_to_rgb()
  8493. {
  8494. int row, col, c, i, j, k;
  8495. ushort *img;
  8496. float out[3], out_cam[3][4];
  8497. double num, inverse[3][3];
  8498. static const double xyzd50_srgb[3][3] =
  8499. { { 0.436083, 0.385083, 0.143055 },
  8500. { 0.222507, 0.716888, 0.060608 },
  8501. { 0.013930, 0.097097, 0.714022 } };
  8502. static const double rgb_rgb[3][3] =
  8503. { { 1,0,0 }, { 0,1,0 }, { 0,0,1 } };
  8504. static const double adobe_rgb[3][3] =
  8505. { { 0.715146, 0.284856, 0.000000 },
  8506. { 0.000000, 1.000000, 0.000000 },
  8507. { 0.000000, 0.041166, 0.958839 } };
  8508. static const double wide_rgb[3][3] =
  8509. { { 0.593087, 0.404710, 0.002206 },
  8510. { 0.095413, 0.843149, 0.061439 },
  8511. { 0.011621, 0.069091, 0.919288 } };
  8512. static const double prophoto_rgb[3][3] =
  8513. { { 0.529317, 0.330092, 0.140588 },
  8514. { 0.098368, 0.873465, 0.028169 },
  8515. { 0.016879, 0.117663, 0.865457 } };
  8516. static const double (*out_rgb[])[3] =
  8517. { rgb_rgb, adobe_rgb, wide_rgb, prophoto_rgb, xyz_rgb };
  8518. static const char *name[] =
  8519. { "sRGB", "Adobe RGB (1998)", "WideGamut D65", "ProPhoto D65", "XYZ" };
  8520. static const unsigned phead[] =
  8521. { 1024, 0, 0x2100000, 0x6d6e7472, 0x52474220, 0x58595a20, 0, 0, 0,
  8522. 0x61637370, 0, 0, 0x6e6f6e65, 0, 0, 0, 0, 0xf6d6, 0x10000, 0xd32d };
  8523. unsigned pbody[] =
  8524. { 10, 0x63707274, 0, 36, /* cprt */
  8525. 0x64657363, 0, 40, /* desc */
  8526. 0x77747074, 0, 20, /* wtpt */
  8527. 0x626b7074, 0, 20, /* bkpt */
  8528. 0x72545243, 0, 14, /* rTRC */
  8529. 0x67545243, 0, 14, /* gTRC */
  8530. 0x62545243, 0, 14, /* bTRC */
  8531. 0x7258595a, 0, 20, /* rXYZ */
  8532. 0x6758595a, 0, 20, /* gXYZ */
  8533. 0x6258595a, 0, 20 }; /* bXYZ */
  8534. static const unsigned pwhite[] = { 0xf351, 0x10000, 0x116cc };
  8535. unsigned pcurve[] = { 0x63757276, 0, 1, 0x1000000 };
  8536. #ifdef LIBRAW_LIBRARY_BUILD
  8537. RUN_CALLBACK(LIBRAW_PROGRESS_CONVERT_RGB,0,2);
  8538. #endif
  8539. gamma_curve (gamm[0], gamm[1], 0, 0);
  8540. memcpy (out_cam, rgb_cam, sizeof out_cam);
  8541. raw_color |= colors == 1 || document_mode ||
  8542. output_color < 1 || output_color > 5;
  8543. if (!raw_color) {
  8544. oprof = (unsigned *) calloc (phead[0], 1);
  8545. merror (oprof, "convert_to_rgb()");
  8546. memcpy (oprof, phead, sizeof phead);
  8547. if (output_color == 5) oprof[4] = oprof[5];
  8548. oprof[0] = 132 + 12*pbody[0];
  8549. for (i=0; i < pbody[0]; i++) {
  8550. oprof[oprof[0]/4] = i ? (i > 1 ? 0x58595a20 : 0x64657363) : 0x74657874;
  8551. pbody[i*3+2] = oprof[0];
  8552. oprof[0] += (pbody[i*3+3] + 3) & -4;
  8553. }
  8554. memcpy (oprof+32, pbody, sizeof pbody);
  8555. oprof[pbody[5]/4+2] = strlen(name[output_color-1]) + 1;
  8556. memcpy ((char *)oprof+pbody[8]+8, pwhite, sizeof pwhite);
  8557. pcurve[3] = (short)(256/gamm[5]+0.5) << 16;
  8558. for (i=4; i < 7; i++)
  8559. memcpy ((char *)oprof+pbody[i*3+2], pcurve, sizeof pcurve);
  8560. pseudoinverse ((double (*)[3]) out_rgb[output_color-1], inverse, 3);
  8561. for (i=0; i < 3; i++)
  8562. for (j=0; j < 3; j++) {
  8563. for (num = k=0; k < 3; k++)
  8564. num += xyzd50_srgb[i][k] * inverse[j][k];
  8565. oprof[pbody[j*3+23]/4+i+2] = num * 0x10000 + 0.5;
  8566. }
  8567. for (i=0; i < phead[0]/4; i++)
  8568. oprof[i] = htonl(oprof[i]);
  8569. strcpy ((char *)oprof+pbody[2]+8, "auto-generated by dcraw");
  8570. strcpy ((char *)oprof+pbody[5]+12, name[output_color-1]);
  8571. for (i=0; i < 3; i++)
  8572. for (j=0; j < colors; j++)
  8573. for (out_cam[i][j] = k=0; k < 3; k++)
  8574. out_cam[i][j] += out_rgb[output_color-1][i][k] * rgb_cam[k][j];
  8575. }
  8576. #ifdef DCRAW_VERBOSE
  8577. if (verbose)
  8578. fprintf (stderr, raw_color ? _("Building histograms...\n") :
  8579. _("Converting to %s colorspace...\n"), name[output_color-1]);
  8580. #endif
  8581. #ifdef LIBRAW_LIBRARY_BUILD
  8582. memset(histogram,0,sizeof(int)*LIBRAW_HISTOGRAM_SIZE*4);
  8583. #else
  8584. memset (histogram, 0, sizeof histogram);
  8585. #endif
  8586. for (img=image[0], row=0; row < height; row++)
  8587. for (col=0; col < width; col++, img+=4) {
  8588. if (!raw_color) {
  8589. out[0] = out[1] = out[2] = 0;
  8590. FORCC {
  8591. out[0] += out_cam[0][c] * img[c];
  8592. out[1] += out_cam[1][c] * img[c];
  8593. out[2] += out_cam[2][c] * img[c];
  8594. }
  8595. FORC3 img[c] = CLIP((int) out[c]);
  8596. }
  8597. else if (document_mode)
  8598. img[0] = img[FC(row,col)];
  8599. FORCC histogram[c][img[c] >> 3]++;
  8600. }
  8601. if (colors == 4 && output_color) colors = 3;
  8602. if (document_mode && filters) colors = 1;
  8603. #ifdef LIBRAW_LIBRARY_BUILD
  8604. RUN_CALLBACK(LIBRAW_PROGRESS_CONVERT_RGB,1,2);
  8605. #endif
  8606. }
  8607. void CLASS fuji_rotate()
  8608. {
  8609. int i, row, col;
  8610. double step;
  8611. float r, c, fr, fc;
  8612. unsigned ur, uc;
  8613. ushort wide, high, (*img)[4], (*pix)[4];
  8614. if (!fuji_width) return;
  8615. #ifdef DCRAW_VERBOSE
  8616. if (verbose)
  8617. fprintf (stderr,_("Rotating image 45 degrees...\n"));
  8618. #endif
  8619. fuji_width = (fuji_width - 1 + shrink) >> shrink;
  8620. step = sqrt(0.5);
  8621. wide = fuji_width / step;
  8622. high = (height - fuji_width) / step;
  8623. img = (ushort (*)[4]) calloc (wide*high, sizeof *img);
  8624. merror (img, "fuji_rotate()");
  8625. #ifdef LIBRAW_LIBRARY_BUILD
  8626. RUN_CALLBACK(LIBRAW_PROGRESS_FUJI_ROTATE,0,2);
  8627. #endif
  8628. for (row=0; row < high; row++)
  8629. for (col=0; col < wide; col++) {
  8630. ur = r = fuji_width + (row-col)*step;
  8631. uc = c = (row+col)*step;
  8632. if (ur > height-2 || uc > width-2) continue;
  8633. fr = r - ur;
  8634. fc = c - uc;
  8635. pix = image + ur*width + uc;
  8636. for (i=0; i < colors; i++)
  8637. img[row*wide+col][i] =
  8638. (pix[ 0][i]*(1-fc) + pix[ 1][i]*fc) * (1-fr) +
  8639. (pix[width][i]*(1-fc) + pix[width+1][i]*fc) * fr;
  8640. }
  8641. free (image);
  8642. width = wide;
  8643. height = high;
  8644. image = img;
  8645. fuji_width = 0;
  8646. #ifdef LIBRAW_LIBRARY_BUILD
  8647. RUN_CALLBACK(LIBRAW_PROGRESS_FUJI_ROTATE,1,2);
  8648. #endif
  8649. }
  8650. void CLASS stretch()
  8651. {
  8652. ushort newdim, (*img)[4], *pix0, *pix1;
  8653. int row, col, c;
  8654. double rc, frac;
  8655. if (pixel_aspect == 1) return;
  8656. #ifdef LIBRAW_LIBRARY_BUILD
  8657. RUN_CALLBACK(LIBRAW_PROGRESS_STRETCH,0,2);
  8658. #endif
  8659. #ifdef DCRAW_VERBOSE
  8660. if (verbose) fprintf (stderr,_("Stretching the image...\n"));
  8661. #endif
  8662. if (pixel_aspect < 1) {
  8663. newdim = height / pixel_aspect + 0.5;
  8664. img = (ushort (*)[4]) calloc (width*newdim, sizeof *img);
  8665. merror (img, "stretch()");
  8666. for (rc=row=0; row < newdim; row++, rc+=pixel_aspect) {
  8667. frac = rc - (c = rc);
  8668. pix0 = pix1 = image[c*width];
  8669. if (c+1 < height) pix1 += width*4;
  8670. for (col=0; col < width; col++, pix0+=4, pix1+=4)
  8671. FORCC img[row*width+col][c] = pix0[c]*(1-frac) + pix1[c]*frac + 0.5;
  8672. }
  8673. height = newdim;
  8674. } else {
  8675. newdim = width * pixel_aspect + 0.5;
  8676. img = (ushort (*)[4]) calloc (height*newdim, sizeof *img);
  8677. merror (img, "stretch()");
  8678. for (rc=col=0; col < newdim; col++, rc+=1/pixel_aspect) {
  8679. frac = rc - (c = rc);
  8680. pix0 = pix1 = image[c];
  8681. if (c+1 < width) pix1 += 4;
  8682. for (row=0; row < height; row++, pix0+=width*4, pix1+=width*4)
  8683. FORCC img[row*newdim+col][c] = pix0[c]*(1-frac) + pix1[c]*frac + 0.5;
  8684. }
  8685. width = newdim;
  8686. }
  8687. free (image);
  8688. image = img;
  8689. #ifdef LIBRAW_LIBRARY_BUILD
  8690. RUN_CALLBACK(LIBRAW_PROGRESS_STRETCH,1,2);
  8691. #endif
  8692. }
  8693. int CLASS flip_index (int row, int col)
  8694. {
  8695. if (flip & 4) SWAP(row,col);
  8696. if (flip & 2) row = iheight - 1 - row;
  8697. if (flip & 1) col = iwidth - 1 - col;
  8698. return row * iwidth + col;
  8699. }
  8700. void CLASS tiff_set (ushort *ntag,
  8701. ushort tag, ushort type, int count, int val)
  8702. {
  8703. struct tiff_tag *tt;
  8704. int c;
  8705. tt = (struct tiff_tag *)(ntag+1) + (*ntag)++;
  8706. tt->tag = tag;
  8707. tt->type = type;
  8708. tt->count = count;
  8709. if (type < 3 && count <= 4)
  8710. FORC(4) tt->val.c[c] = val >> (c << 3);
  8711. else if (type == 3 && count <= 2)
  8712. FORC(2) tt->val.s[c] = val >> (c << 4);
  8713. else tt->val.i = val;
  8714. }
  8715. #define TOFF(ptr) ((char *)(&(ptr)) - (char *)th)
  8716. void CLASS tiff_head (struct tiff_hdr *th, int full)
  8717. {
  8718. int c, psize=0;
  8719. struct tm *t;
  8720. memset (th, 0, sizeof *th);
  8721. th->t_order = htonl(0x4d4d4949) >> 16;
  8722. th->magic = 42;
  8723. th->ifd = 10;
  8724. if (full) {
  8725. tiff_set (&th->ntag, 254, 4, 1, 0);
  8726. tiff_set (&th->ntag, 256, 4, 1, width);
  8727. tiff_set (&th->ntag, 257, 4, 1, height);
  8728. tiff_set (&th->ntag, 258, 3, colors, output_bps);
  8729. if (colors > 2)
  8730. th->tag[th->ntag-1].val.i = TOFF(th->bps);
  8731. FORC4 th->bps[c] = output_bps;
  8732. tiff_set (&th->ntag, 259, 3, 1, 1);
  8733. tiff_set (&th->ntag, 262, 3, 1, 1 + (colors > 1));
  8734. }
  8735. tiff_set (&th->ntag, 270, 2, 512, TOFF(th->t_desc));
  8736. tiff_set (&th->ntag, 271, 2, 64, TOFF(th->t_make));
  8737. tiff_set (&th->ntag, 272, 2, 64, TOFF(th->t_model));
  8738. if (full) {
  8739. if (oprof) psize = ntohl(oprof[0]);
  8740. tiff_set (&th->ntag, 273, 4, 1, sizeof *th + psize);
  8741. tiff_set (&th->ntag, 277, 3, 1, colors);
  8742. tiff_set (&th->ntag, 278, 4, 1, height);
  8743. tiff_set (&th->ntag, 279, 4, 1, height*width*colors*output_bps/8);
  8744. } else
  8745. tiff_set (&th->ntag, 274, 3, 1, "12435867"[flip]-'0');
  8746. tiff_set (&th->ntag, 282, 5, 1, TOFF(th->rat[0]));
  8747. tiff_set (&th->ntag, 283, 5, 1, TOFF(th->rat[2]));
  8748. tiff_set (&th->ntag, 284, 3, 1, 1);
  8749. tiff_set (&th->ntag, 296, 3, 1, 2);
  8750. tiff_set (&th->ntag, 305, 2, 32, TOFF(th->soft));
  8751. tiff_set (&th->ntag, 306, 2, 20, TOFF(th->date));
  8752. tiff_set (&th->ntag, 315, 2, 64, TOFF(th->t_artist));
  8753. tiff_set (&th->ntag, 34665, 4, 1, TOFF(th->nexif));
  8754. if (psize) tiff_set (&th->ntag, 34675, 7, psize, sizeof *th);
  8755. tiff_set (&th->nexif, 33434, 5, 1, TOFF(th->rat[4]));
  8756. tiff_set (&th->nexif, 33437, 5, 1, TOFF(th->rat[6]));
  8757. tiff_set (&th->nexif, 34855, 3, 1, iso_speed);
  8758. tiff_set (&th->nexif, 37386, 5, 1, TOFF(th->rat[8]));
  8759. if (gpsdata[1]) {
  8760. tiff_set (&th->ntag, 34853, 4, 1, TOFF(th->ngps));
  8761. tiff_set (&th->ngps, 0, 1, 4, 0x202);
  8762. tiff_set (&th->ngps, 1, 2, 2, gpsdata[29]);
  8763. tiff_set (&th->ngps, 2, 5, 3, TOFF(th->gps[0]));
  8764. tiff_set (&th->ngps, 3, 2, 2, gpsdata[30]);
  8765. tiff_set (&th->ngps, 4, 5, 3, TOFF(th->gps[6]));
  8766. tiff_set (&th->ngps, 5, 1, 1, gpsdata[31]);
  8767. tiff_set (&th->ngps, 6, 5, 1, TOFF(th->gps[18]));
  8768. tiff_set (&th->ngps, 7, 5, 3, TOFF(th->gps[12]));
  8769. tiff_set (&th->ngps, 18, 2, 12, TOFF(th->gps[20]));
  8770. tiff_set (&th->ngps, 29, 2, 12, TOFF(th->gps[23]));
  8771. memcpy (th->gps, gpsdata, sizeof th->gps);
  8772. }
  8773. th->rat[0] = th->rat[2] = 300;
  8774. th->rat[1] = th->rat[3] = 1;
  8775. FORC(6) th->rat[4+c] = 1000000;
  8776. th->rat[4] *= shutter;
  8777. th->rat[6] *= aperture;
  8778. th->rat[8] *= focal_len;
  8779. strncpy (th->t_desc, desc, 512);
  8780. strncpy (th->t_make, make, 64);
  8781. strncpy (th->t_model, model, 64);
  8782. strcpy (th->soft, "dcraw v" DCRAW_VERSION);
  8783. t = localtime (&timestamp);
  8784. sprintf (th->date, "%04d:%02d:%02d %02d:%02d:%02d",
  8785. t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
  8786. strncpy (th->t_artist, artist, 64);
  8787. }
  8788. void CLASS jpeg_thumb_writer (FILE *tfp,char *t_humb,int t_humb_length)
  8789. {
  8790. ushort exif[5];
  8791. struct tiff_hdr th;
  8792. fputc (0xff, tfp);
  8793. fputc (0xd8, tfp);
  8794. if (strcmp (t_humb+6, "Exif")) {
  8795. memcpy (exif, "\xff\xe1 Exif\0\0", 10);
  8796. exif[1] = htons (8 + sizeof th);
  8797. fwrite (exif, 1, sizeof exif, tfp);
  8798. tiff_head (&th, 0);
  8799. fwrite (&th, 1, sizeof th, tfp);
  8800. }
  8801. fwrite (t_humb+2, 1, t_humb_length-2, tfp);
  8802. }
  8803. void CLASS jpeg_thumb()
  8804. {
  8805. char *thumb;
  8806. thumb = (char *) malloc (thumb_length);
  8807. merror (thumb, "jpeg_thumb()");
  8808. fread (thumb, 1, thumb_length, ifp);
  8809. jpeg_thumb_writer(ofp,thumb,thumb_length);
  8810. free (thumb);
  8811. }
  8812. void CLASS write_ppm_tiff()
  8813. {
  8814. struct tiff_hdr th;
  8815. uchar *ppm;
  8816. ushort *ppm2;
  8817. int c, row, col, soff, rstep, cstep;
  8818. int perc, val, total, t_white=0x2000;
  8819. perc = width * height * 0.01; /* 99th percentile white level */
  8820. if (fuji_width) perc /= 2;
  8821. if (!((highlight & ~2) || no_auto_bright))
  8822. for (t_white=c=0; c < colors; c++) {
  8823. for (val=0x2000, total=0; --val > 32; )
  8824. if ((total += histogram[c][val]) > perc) break;
  8825. if (t_white < val) t_white = val;
  8826. }
  8827. gamma_curve (gamm[0], gamm[1], 2, (t_white << 3)/bright);
  8828. iheight = height;
  8829. iwidth = width;
  8830. if (flip & 4) SWAP(height,width);
  8831. ppm = (uchar *) calloc (width, colors*output_bps/8);
  8832. ppm2 = (ushort *) ppm;
  8833. merror (ppm, "write_ppm_tiff()");
  8834. if (output_tiff) {
  8835. tiff_head (&th, 1);
  8836. fwrite (&th, sizeof th, 1, ofp);
  8837. if (oprof)
  8838. fwrite (oprof, ntohl(oprof[0]), 1, ofp);
  8839. } else if (colors > 3)
  8840. fprintf (ofp,
  8841. "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
  8842. width, height, colors, (1 << output_bps)-1, cdesc);
  8843. else
  8844. fprintf (ofp, "P%d\n%d %d\n%d\n",
  8845. colors/2+5, width, height, (1 << output_bps)-1);
  8846. soff = flip_index (0, 0);
  8847. cstep = flip_index (0, 1) - soff;
  8848. rstep = flip_index (1, 0) - flip_index (0, width);
  8849. for (row=0; row < height; row++, soff += rstep) {
  8850. for (col=0; col < width; col++, soff += cstep)
  8851. if (output_bps == 8)
  8852. FORCC ppm [col*colors+c] = curve[image[soff][c]] >> 8;
  8853. else FORCC ppm2[col*colors+c] = curve[image[soff][c]];
  8854. if (output_bps == 16 && !output_tiff && htons(0x55aa) != 0x55aa)
  8855. swab ((char*)ppm2, (char*)ppm2, width*colors*2);
  8856. fwrite (ppm, colors*output_bps/8, width, ofp);
  8857. }
  8858. free (ppm);
  8859. }