PageRenderTime 129ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 2ms

/Source/LibRawLite/internal/dcraw_common.cpp

https://bitbucket.org/tiran/freeimage
C++ | 9385 lines | 8885 code | 356 blank | 144 comment | 2909 complexity | 62d53896f8a38f0d2f613bfbf11e9212 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, GPL-2.0, GPL-3.0, LGPL-3.0, BSD-2-Clause, LGPL-2.1, BSD-3-Clause
  1. /*
  2. Copyright 2008-2013 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. int CLASS fcol (int row, int col)
  27. {
  28. static const char filter[16][16] =
  29. { { 2,1,1,3,2,3,2,0,3,2,3,0,1,2,1,0 },
  30. { 0,3,0,2,0,1,3,1,0,1,1,2,0,3,3,2 },
  31. { 2,3,3,2,3,1,1,3,3,1,2,1,2,0,0,3 },
  32. { 0,1,0,1,0,2,0,2,2,0,3,0,1,3,2,1 },
  33. { 3,1,1,2,0,1,0,2,1,3,1,3,0,1,3,0 },
  34. { 2,0,0,3,3,2,3,1,2,0,2,0,3,2,2,1 },
  35. { 2,3,3,1,2,1,2,1,2,1,1,2,3,0,0,1 },
  36. { 1,0,0,2,3,0,0,3,0,3,0,3,2,1,2,3 },
  37. { 2,3,3,1,1,2,1,0,3,2,3,0,2,3,1,3 },
  38. { 1,0,2,0,3,0,3,2,0,1,1,2,0,1,0,2 },
  39. { 0,1,1,3,3,2,2,1,1,3,3,0,2,1,3,2 },
  40. { 2,3,2,0,0,1,3,0,2,0,1,2,3,0,1,0 },
  41. { 1,3,1,2,3,2,3,2,0,2,0,1,1,0,3,0 },
  42. { 0,2,0,3,1,0,0,1,1,3,3,2,3,2,2,1 },
  43. { 2,1,3,2,3,1,2,1,0,3,0,2,0,2,0,2 },
  44. { 0,3,1,0,0,2,0,3,2,1,3,1,1,3,1,3 } };
  45. static const char filter2[6][6] =
  46. { { 1,1,0,1,1,2 },
  47. { 1,1,2,1,1,0 },
  48. { 2,0,1,0,2,1 },
  49. { 1,1,2,1,1,0 },
  50. { 1,1,0,1,1,2 },
  51. { 0,2,1,2,0,1 } };
  52. if (filters == 1) return filter[(row+top_margin)&15][(col+left_margin)&15];
  53. if (filters == 2) return filter2[(row+6) % 6][(col+6) % 6];
  54. return FC(row,col);
  55. }
  56. #ifndef __GLIBC__
  57. char *my_memmem (char *haystack, size_t haystacklen,
  58. char *needle, size_t needlelen)
  59. {
  60. char *c;
  61. for (c = haystack; c <= haystack + haystacklen - needlelen; c++)
  62. if (!memcmp (c, needle, needlelen))
  63. return c;
  64. return 0;
  65. }
  66. #define memmem my_memmem
  67. #endif
  68. ushort CLASS sget2 (uchar *s)
  69. {
  70. if (order == 0x4949) /* "II" means little-endian */
  71. return s[0] | s[1] << 8;
  72. else /* "MM" means big-endian */
  73. return s[0] << 8 | s[1];
  74. }
  75. ushort CLASS get2()
  76. {
  77. uchar str[2] = { 0xff,0xff };
  78. fread (str, 1, 2, ifp);
  79. return sget2(str);
  80. }
  81. unsigned CLASS sget4 (uchar *s)
  82. {
  83. if (order == 0x4949)
  84. return s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
  85. else
  86. return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3];
  87. }
  88. #define sget4(s) sget4((uchar *)s)
  89. unsigned CLASS get4()
  90. {
  91. uchar str[4] = { 0xff,0xff,0xff,0xff };
  92. fread (str, 1, 4, ifp);
  93. return sget4(str);
  94. }
  95. unsigned CLASS getint (int type)
  96. {
  97. return type == 3 ? get2() : get4();
  98. }
  99. float CLASS int_to_float (int i)
  100. {
  101. union { int i; float f; } u;
  102. u.i = i;
  103. return u.f;
  104. }
  105. double CLASS getreal (int type)
  106. {
  107. union { char c[8]; double d; } u;
  108. int i, rev;
  109. switch (type) {
  110. case 3: return (unsigned short) get2();
  111. case 4: return (unsigned int) get4();
  112. case 5: u.d = (unsigned int) get4();
  113. return u.d / (unsigned int) get4();
  114. case 8: return (signed short) get2();
  115. case 9: return (signed int) get4();
  116. case 10: u.d = (signed int) get4();
  117. return u.d / (signed int) get4();
  118. case 11: return int_to_float (get4());
  119. case 12:
  120. rev = 7 * ((order == 0x4949) == (ntohs(0x1234) == 0x1234));
  121. for (i=0; i < 8; i++)
  122. u.c[i ^ rev] = fgetc(ifp);
  123. return u.d;
  124. default: return fgetc(ifp);
  125. }
  126. }
  127. void CLASS read_shorts (ushort *pixel, int count)
  128. {
  129. if (fread (pixel, 2, count, ifp) < count) derror();
  130. if ((order == 0x4949) == (ntohs(0x1234) == 0x1234))
  131. swab ((char*)pixel, (char*)pixel, count*2);
  132. }
  133. void CLASS canon_600_fixed_wb (int temp)
  134. {
  135. static const short mul[4][5] = {
  136. { 667, 358,397,565,452 },
  137. { 731, 390,367,499,517 },
  138. { 1119, 396,348,448,537 },
  139. { 1399, 485,431,508,688 } };
  140. int lo, hi, i;
  141. float frac=0;
  142. for (lo=4; --lo; )
  143. if (*mul[lo] <= temp) break;
  144. for (hi=0; hi < 3; hi++)
  145. if (*mul[hi] >= temp) break;
  146. if (lo != hi)
  147. frac = (float) (temp - *mul[lo]) / (*mul[hi] - *mul[lo]);
  148. for (i=1; i < 5; i++)
  149. pre_mul[i-1] = 1 / (frac * mul[hi][i] + (1-frac) * mul[lo][i]);
  150. }
  151. /* Return values: 0 = white 1 = near white 2 = not white */
  152. int CLASS canon_600_color (int ratio[2], int mar)
  153. {
  154. int clipped=0, target, miss;
  155. if (flash_used) {
  156. if (ratio[1] < -104)
  157. { ratio[1] = -104; clipped = 1; }
  158. if (ratio[1] > 12)
  159. { ratio[1] = 12; clipped = 1; }
  160. } else {
  161. if (ratio[1] < -264 || ratio[1] > 461) return 2;
  162. if (ratio[1] < -50)
  163. { ratio[1] = -50; clipped = 1; }
  164. if (ratio[1] > 307)
  165. { ratio[1] = 307; clipped = 1; }
  166. }
  167. target = flash_used || ratio[1] < 197
  168. ? -38 - (398 * ratio[1] >> 10)
  169. : -123 + (48 * ratio[1] >> 10);
  170. if (target - mar <= ratio[0] &&
  171. target + 20 >= ratio[0] && !clipped) return 0;
  172. miss = target - ratio[0];
  173. if (abs(miss) >= mar*4) return 2;
  174. if (miss < -20) miss = -20;
  175. if (miss > mar) miss = mar;
  176. ratio[0] = target - miss;
  177. return 1;
  178. }
  179. void CLASS canon_600_auto_wb()
  180. {
  181. int mar, row, col, i, j, st, count[] = { 0,0 };
  182. int test[8], total[2][8], ratio[2][2], stat[2];
  183. memset (&total, 0, sizeof total);
  184. i = canon_ev + 0.5;
  185. if (i < 10) mar = 150;
  186. else if (i > 12) mar = 20;
  187. else mar = 280 - 20 * i;
  188. if (flash_used) mar = 80;
  189. for (row=14; row < height-14; row+=4)
  190. for (col=10; col < width; col+=2) {
  191. for (i=0; i < 8; i++)
  192. test[(i & 4) + FC(row+(i >> 1),col+(i & 1))] =
  193. BAYER(row+(i >> 1),col+(i & 1));
  194. for (i=0; i < 8; i++)
  195. if (test[i] < 150 || test[i] > 1500) goto next;
  196. for (i=0; i < 4; i++)
  197. if (abs(test[i] - test[i+4]) > 50) goto next;
  198. for (i=0; i < 2; i++) {
  199. for (j=0; j < 4; j+=2)
  200. ratio[i][j >> 1] = ((test[i*4+j+1]-test[i*4+j]) << 10) / test[i*4+j];
  201. stat[i] = canon_600_color (ratio[i], mar);
  202. }
  203. if ((st = stat[0] | stat[1]) > 1) goto next;
  204. for (i=0; i < 2; i++)
  205. if (stat[i])
  206. for (j=0; j < 2; j++)
  207. test[i*4+j*2+1] = test[i*4+j*2] * (0x400 + ratio[i][j]) >> 10;
  208. for (i=0; i < 8; i++)
  209. total[st][i] += test[i];
  210. count[st]++;
  211. next: ;
  212. }
  213. if (count[0] | count[1]) {
  214. st = count[0]*200 < count[1];
  215. for (i=0; i < 4; i++)
  216. pre_mul[i] = 1.0 / (total[st][i] + total[st][i+4]);
  217. }
  218. }
  219. void CLASS canon_600_coeff()
  220. {
  221. static const short table[6][12] = {
  222. { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
  223. { -1203,1715,-1136,1648, 1388,-876,267,245, -1641,2153,3921,-3409 },
  224. { -615,1127,-1563,2075, 1437,-925,509,3, -756,1268,2519,-2007 },
  225. { -190,702,-1886,2398, 2153,-1641,763,-251, -452,964,3040,-2528 },
  226. { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
  227. { -807,1319,-1785,2297, 1388,-876,769,-257, -230,742,2067,-1555 } };
  228. int t=0, i, c;
  229. float mc, yc;
  230. mc = pre_mul[1] / pre_mul[2];
  231. yc = pre_mul[3] / pre_mul[2];
  232. if (mc > 1 && mc <= 1.28 && yc < 0.8789) t=1;
  233. if (mc > 1.28 && mc <= 2) {
  234. if (yc < 0.8789) t=3;
  235. else if (yc <= 2) t=4;
  236. }
  237. if (flash_used) t=5;
  238. for (raw_color = i=0; i < 3; i++)
  239. FORCC rgb_cam[i][c] = table[t][i*4 + c] / 1024.0;
  240. }
  241. void CLASS canon_600_load_raw()
  242. {
  243. uchar data[1120], *dp;
  244. ushort *pix;
  245. int irow, row;
  246. for (irow=row=0; irow < height; irow++) {
  247. if (fread (data, 1, 1120, ifp) < 1120) derror();
  248. pix = raw_image + row*raw_width;
  249. for (dp=data; dp < data+1120; dp+=10, pix+=8) {
  250. pix[0] = (dp[0] << 2) + (dp[1] >> 6 );
  251. pix[1] = (dp[2] << 2) + (dp[1] >> 4 & 3);
  252. pix[2] = (dp[3] << 2) + (dp[1] >> 2 & 3);
  253. pix[3] = (dp[4] << 2) + (dp[1] & 3);
  254. pix[4] = (dp[5] << 2) + (dp[9] & 3);
  255. pix[5] = (dp[6] << 2) + (dp[9] >> 2 & 3);
  256. pix[6] = (dp[7] << 2) + (dp[9] >> 4 & 3);
  257. pix[7] = (dp[8] << 2) + (dp[9] >> 6 );
  258. }
  259. if ((row+=2) > height) row = 1;
  260. }
  261. }
  262. void CLASS canon_600_correct()
  263. {
  264. int row, col, val;
  265. static const short mul[4][2] =
  266. { { 1141,1145 }, { 1128,1109 }, { 1178,1149 }, { 1128,1109 } };
  267. for (row=0; row < height; row++)
  268. for (col=0; col < width; col++) {
  269. if ((val = BAYER(row,col) - black) < 0) val = 0;
  270. val = val * mul[row & 3][col & 1] >> 9;
  271. BAYER(row,col) = val;
  272. }
  273. canon_600_fixed_wb(1311);
  274. canon_600_auto_wb();
  275. canon_600_coeff();
  276. maximum = (0x3ff - black) * 1109 >> 9;
  277. black = 0;
  278. }
  279. int CLASS canon_s2is()
  280. {
  281. unsigned row;
  282. for (row=0; row < 100; row++) {
  283. fseek (ifp, row*3340 + 3284, SEEK_SET);
  284. if (getc(ifp) > 15) return 1;
  285. }
  286. return 0;
  287. }
  288. /*
  289. getbits(-1) initializes the buffer
  290. getbits(n) where 0 <= n <= 25 returns an n-bit integer
  291. */
  292. unsigned CLASS getbithuff (int nbits, ushort *huff)
  293. {
  294. #ifdef LIBRAW_NOTHREADS
  295. static unsigned bitbuf=0;
  296. static int vbits=0, reset=0;
  297. #else
  298. #define bitbuf tls->getbits.bitbuf
  299. #define vbits tls->getbits.vbits
  300. #define reset tls->getbits.reset
  301. #endif
  302. unsigned c;
  303. if (nbits == -1)
  304. return bitbuf = vbits = reset = 0;
  305. if (nbits == 0 || vbits < 0) return 0;
  306. while (!reset && vbits < nbits && (c = fgetc(ifp)) != EOF &&
  307. !(reset = zero_after_ff && c == 0xff && fgetc(ifp))) {
  308. bitbuf = (bitbuf << 8) + (uchar) c;
  309. vbits += 8;
  310. }
  311. c = bitbuf << (32-vbits) >> (32-nbits);
  312. if (huff) {
  313. vbits -= huff[c] >> 8;
  314. c = (uchar) huff[c];
  315. } else
  316. vbits -= nbits;
  317. if (vbits < 0) derror();
  318. return c;
  319. #ifndef LIBRAW_NOTHREADS
  320. #undef bitbuf
  321. #undef vbits
  322. #undef reset
  323. #endif
  324. }
  325. #define getbits(n) getbithuff(n,0)
  326. #define gethuff(h) getbithuff(*h,h+1)
  327. /*
  328. Construct a decode tree according the specification in *source.
  329. The first 16 bytes specify how many codes should be 1-bit, 2-bit
  330. 3-bit, etc. Bytes after that are the leaf values.
  331. For example, if the source is
  332. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
  333. 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
  334. then the code is
  335. 00 0x04
  336. 010 0x03
  337. 011 0x05
  338. 100 0x06
  339. 101 0x02
  340. 1100 0x07
  341. 1101 0x01
  342. 11100 0x08
  343. 11101 0x09
  344. 11110 0x00
  345. 111110 0x0a
  346. 1111110 0x0b
  347. 1111111 0xff
  348. */
  349. ushort * CLASS make_decoder_ref (const uchar **source)
  350. {
  351. int max, len, h, i, j;
  352. const uchar *count;
  353. ushort *huff;
  354. count = (*source += 16) - 17;
  355. for (max=16; max && !count[max]; max--);
  356. huff = (ushort *) calloc (1 + (1 << max), sizeof *huff);
  357. merror (huff, "make_decoder()");
  358. huff[0] = max;
  359. for (h=len=1; len <= max; len++)
  360. for (i=0; i < count[len]; i++, ++*source)
  361. for (j=0; j < 1 << (max-len); j++)
  362. if (h <= 1 << max)
  363. huff[h++] = len << 8 | **source;
  364. return huff;
  365. }
  366. ushort * CLASS make_decoder (const uchar *source)
  367. {
  368. return make_decoder_ref (&source);
  369. }
  370. void CLASS crw_init_tables (unsigned table, ushort *huff[2])
  371. {
  372. static const uchar first_tree[3][29] = {
  373. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
  374. 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
  375. { 0,2,2,3,1,1,1,1,2,0,0,0,0,0,0,0,
  376. 0x03,0x02,0x04,0x01,0x05,0x00,0x06,0x07,0x09,0x08,0x0a,0x0b,0xff },
  377. { 0,0,6,3,1,1,2,0,0,0,0,0,0,0,0,0,
  378. 0x06,0x05,0x07,0x04,0x08,0x03,0x09,0x02,0x00,0x0a,0x01,0x0b,0xff },
  379. };
  380. static const uchar second_tree[3][180] = {
  381. { 0,2,2,2,1,4,2,1,2,5,1,1,0,0,0,139,
  382. 0x03,0x04,0x02,0x05,0x01,0x06,0x07,0x08,
  383. 0x12,0x13,0x11,0x14,0x09,0x15,0x22,0x00,0x21,0x16,0x0a,0xf0,
  384. 0x23,0x17,0x24,0x31,0x32,0x18,0x19,0x33,0x25,0x41,0x34,0x42,
  385. 0x35,0x51,0x36,0x37,0x38,0x29,0x79,0x26,0x1a,0x39,0x56,0x57,
  386. 0x28,0x27,0x52,0x55,0x58,0x43,0x76,0x59,0x77,0x54,0x61,0xf9,
  387. 0x71,0x78,0x75,0x96,0x97,0x49,0xb7,0x53,0xd7,0x74,0xb6,0x98,
  388. 0x47,0x48,0x95,0x69,0x99,0x91,0xfa,0xb8,0x68,0xb5,0xb9,0xd6,
  389. 0xf7,0xd8,0x67,0x46,0x45,0x94,0x89,0xf8,0x81,0xd5,0xf6,0xb4,
  390. 0x88,0xb1,0x2a,0x44,0x72,0xd9,0x87,0x66,0xd4,0xf5,0x3a,0xa7,
  391. 0x73,0xa9,0xa8,0x86,0x62,0xc7,0x65,0xc8,0xc9,0xa1,0xf4,0xd1,
  392. 0xe9,0x5a,0x92,0x85,0xa6,0xe7,0x93,0xe8,0xc1,0xc6,0x7a,0x64,
  393. 0xe1,0x4a,0x6a,0xe6,0xb3,0xf1,0xd3,0xa5,0x8a,0xb2,0x9a,0xba,
  394. 0x84,0xa4,0x63,0xe5,0xc5,0xf3,0xd2,0xc4,0x82,0xaa,0xda,0xe4,
  395. 0xf2,0xca,0x83,0xa3,0xa2,0xc3,0xea,0xc2,0xe2,0xe3,0xff,0xff },
  396. { 0,2,2,1,4,1,4,1,3,3,1,0,0,0,0,140,
  397. 0x02,0x03,0x01,0x04,0x05,0x12,0x11,0x06,
  398. 0x13,0x07,0x08,0x14,0x22,0x09,0x21,0x00,0x23,0x15,0x31,0x32,
  399. 0x0a,0x16,0xf0,0x24,0x33,0x41,0x42,0x19,0x17,0x25,0x18,0x51,
  400. 0x34,0x43,0x52,0x29,0x35,0x61,0x39,0x71,0x62,0x36,0x53,0x26,
  401. 0x38,0x1a,0x37,0x81,0x27,0x91,0x79,0x55,0x45,0x28,0x72,0x59,
  402. 0xa1,0xb1,0x44,0x69,0x54,0x58,0xd1,0xfa,0x57,0xe1,0xf1,0xb9,
  403. 0x49,0x47,0x63,0x6a,0xf9,0x56,0x46,0xa8,0x2a,0x4a,0x78,0x99,
  404. 0x3a,0x75,0x74,0x86,0x65,0xc1,0x76,0xb6,0x96,0xd6,0x89,0x85,
  405. 0xc9,0xf5,0x95,0xb4,0xc7,0xf7,0x8a,0x97,0xb8,0x73,0xb7,0xd8,
  406. 0xd9,0x87,0xa7,0x7a,0x48,0x82,0x84,0xea,0xf4,0xa6,0xc5,0x5a,
  407. 0x94,0xa4,0xc6,0x92,0xc3,0x68,0xb5,0xc8,0xe4,0xe5,0xe6,0xe9,
  408. 0xa2,0xa3,0xe3,0xc2,0x66,0x67,0x93,0xaa,0xd4,0xd5,0xe7,0xf8,
  409. 0x88,0x9a,0xd7,0x77,0xc4,0x64,0xe2,0x98,0xa5,0xca,0xda,0xe8,
  410. 0xf3,0xf6,0xa9,0xb2,0xb3,0xf2,0xd2,0x83,0xba,0xd3,0xff,0xff },
  411. { 0,0,6,2,1,3,3,2,5,1,2,2,8,10,0,117,
  412. 0x04,0x05,0x03,0x06,0x02,0x07,0x01,0x08,
  413. 0x09,0x12,0x13,0x14,0x11,0x15,0x0a,0x16,0x17,0xf0,0x00,0x22,
  414. 0x21,0x18,0x23,0x19,0x24,0x32,0x31,0x25,0x33,0x38,0x37,0x34,
  415. 0x35,0x36,0x39,0x79,0x57,0x58,0x59,0x28,0x56,0x78,0x27,0x41,
  416. 0x29,0x77,0x26,0x42,0x76,0x99,0x1a,0x55,0x98,0x97,0xf9,0x48,
  417. 0x54,0x96,0x89,0x47,0xb7,0x49,0xfa,0x75,0x68,0xb6,0x67,0x69,
  418. 0xb9,0xb8,0xd8,0x52,0xd7,0x88,0xb5,0x74,0x51,0x46,0xd9,0xf8,
  419. 0x3a,0xd6,0x87,0x45,0x7a,0x95,0xd5,0xf6,0x86,0xb4,0xa9,0x94,
  420. 0x53,0x2a,0xa8,0x43,0xf5,0xf7,0xd4,0x66,0xa7,0x5a,0x44,0x8a,
  421. 0xc9,0xe8,0xc8,0xe7,0x9a,0x6a,0x73,0x4a,0x61,0xc7,0xf4,0xc6,
  422. 0x65,0xe9,0x72,0xe6,0x71,0x91,0x93,0xa6,0xda,0x92,0x85,0x62,
  423. 0xf3,0xc5,0xb2,0xa4,0x84,0xba,0x64,0xa5,0xb3,0xd2,0x81,0xe5,
  424. 0xd3,0xaa,0xc4,0xca,0xf2,0xb1,0xe4,0xd1,0x83,0x63,0xea,0xc3,
  425. 0xe2,0x82,0xf1,0xa3,0xc2,0xa1,0xc1,0xe3,0xa2,0xe1,0xff,0xff }
  426. };
  427. if (table > 2) table = 2;
  428. huff[0] = make_decoder ( first_tree[table]);
  429. huff[1] = make_decoder (second_tree[table]);
  430. }
  431. /*
  432. Return 0 if the image starts with compressed data,
  433. 1 if it starts with uncompressed low-order bits.
  434. In Canon compressed data, 0xff is always followed by 0x00.
  435. */
  436. int CLASS canon_has_lowbits()
  437. {
  438. uchar test[0x4000];
  439. int ret=1, i;
  440. fseek (ifp, 0, SEEK_SET);
  441. fread (test, 1, sizeof test, ifp);
  442. for (i=540; i < sizeof test - 1; i++)
  443. if (test[i] == 0xff) {
  444. if (test[i+1]) return 1;
  445. ret=0;
  446. }
  447. return ret;
  448. }
  449. void CLASS canon_load_raw()
  450. {
  451. ushort *pixel, *prow, *huff[2];
  452. int nblocks, lowbits, i, c, row, r, save, val;
  453. int block, diffbuf[64], leaf, len, diff, carry=0, pnum=0, base[2];
  454. crw_init_tables (tiff_compress, huff);
  455. lowbits = canon_has_lowbits();
  456. if (!lowbits) maximum = 0x3ff;
  457. fseek (ifp, 540 + lowbits*raw_height*raw_width/4, SEEK_SET);
  458. zero_after_ff = 1;
  459. getbits(-1);
  460. for (row=0; row < raw_height; row+=8) {
  461. pixel = raw_image + row*raw_width;
  462. nblocks = MIN (8, raw_height-row) * raw_width >> 6;
  463. for (block=0; block < nblocks; block++) {
  464. memset (diffbuf, 0, sizeof diffbuf);
  465. for (i=0; i < 64; i++ ) {
  466. leaf = gethuff(huff[i > 0]);
  467. if (leaf == 0 && i) break;
  468. if (leaf == 0xff) continue;
  469. i += leaf >> 4;
  470. len = leaf & 15;
  471. if (len == 0) continue;
  472. diff = getbits(len);
  473. if ((diff & (1 << (len-1))) == 0)
  474. diff -= (1 << len) - 1;
  475. if (i < 64) diffbuf[i] = diff;
  476. }
  477. diffbuf[0] += carry;
  478. carry = diffbuf[0];
  479. for (i=0; i < 64; i++ ) {
  480. if (pnum++ % raw_width == 0)
  481. base[0] = base[1] = 512;
  482. if ((pixel[(block << 6) + i] = base[i & 1] += diffbuf[i]) >> 10)
  483. derror();
  484. }
  485. }
  486. if (lowbits) {
  487. save = ftell(ifp);
  488. fseek (ifp, 26 + row*raw_width/4, SEEK_SET);
  489. for (prow=pixel, i=0; i < raw_width*2; i++) {
  490. c = fgetc(ifp);
  491. for (r=0; r < 8; r+=2, prow++) {
  492. val = (*prow << 2) + ((c >> r) & 3);
  493. if (raw_width == 2672 && val < 512) val += 2;
  494. *prow = val;
  495. }
  496. }
  497. fseek (ifp, save, SEEK_SET);
  498. }
  499. }
  500. FORC(2) free (huff[c]);
  501. }
  502. int CLASS ljpeg_start (struct jhead *jh, int info_only)
  503. {
  504. int c, tag, len;
  505. uchar data[0x10000];
  506. const uchar *dp;
  507. memset (jh, 0, sizeof *jh);
  508. jh->restart = INT_MAX;
  509. fread (data, 2, 1, ifp);
  510. if (data[1] != 0xd8) return 0;
  511. do {
  512. fread (data, 2, 2, ifp);
  513. tag = data[0] << 8 | data[1];
  514. len = (data[2] << 8 | data[3]) - 2;
  515. if (tag <= 0xff00) return 0;
  516. fread (data, 1, len, ifp);
  517. switch (tag) {
  518. case 0xffc3:
  519. jh->sraw = ((data[7] >> 4) * (data[7] & 15) - 1) & 3;
  520. case 0xffc0:
  521. jh->bits = data[0];
  522. jh->high = data[1] << 8 | data[2];
  523. jh->wide = data[3] << 8 | data[4];
  524. jh->clrs = data[5] + jh->sraw;
  525. if (len == 9 && !dng_version) getc(ifp);
  526. break;
  527. case 0xffc4:
  528. if (info_only) break;
  529. for (dp = data; dp < data+len && (c = *dp++) < 4; )
  530. jh->free[c] = jh->huff[c] = make_decoder_ref (&dp);
  531. break;
  532. case 0xffda:
  533. jh->psv = data[1+data[0]*2];
  534. jh->bits -= data[3+data[0]*2] & 15;
  535. break;
  536. case 0xffdd:
  537. jh->restart = data[0] << 8 | data[1];
  538. }
  539. } while (tag != 0xffda);
  540. if (info_only) return 1;
  541. FORC(5) if (!jh->huff[c+1]) jh->huff[c+1] = jh->huff[c];
  542. if (jh->sraw) {
  543. FORC(4) jh->huff[2+c] = jh->huff[1];
  544. FORC(jh->sraw) jh->huff[1+c] = jh->huff[0];
  545. }
  546. jh->row = (ushort *) calloc (jh->wide*jh->clrs, 4);
  547. merror (jh->row, "ljpeg_start()");
  548. return zero_after_ff = 1;
  549. }
  550. void CLASS ljpeg_end (struct jhead *jh)
  551. {
  552. int c;
  553. FORC4 if (jh->free[c]) free (jh->free[c]);
  554. free (jh->row);
  555. }
  556. int CLASS ljpeg_diff (ushort *huff)
  557. {
  558. int len, diff;
  559. len = gethuff(huff);
  560. if (len == 16 && (!dng_version || dng_version >= 0x1010000))
  561. return -32768;
  562. diff = getbits(len);
  563. if ((diff & (1 << (len-1))) == 0)
  564. diff -= (1 << len) - 1;
  565. return diff;
  566. }
  567. #ifdef LIBRAW_LIBRARY_BUILD
  568. int CLASS ljpeg_diff_new (LibRaw_bit_buffer& bits, LibRaw_byte_buffer* buf,ushort *huff)
  569. {
  570. int len, diff;
  571. len = bits._gethuff_lj(buf,*huff,huff+1);
  572. if (len == 16 && (!dng_version || dng_version >= 0x1010000))
  573. return -32768;
  574. diff = bits._getbits_lj(buf,len);
  575. if ((diff & (1 << (len-1))) == 0)
  576. diff -= (1 << len) - 1;
  577. return diff;
  578. }
  579. ushort * CLASS ljpeg_row_new (int jrow, struct jhead *jh, LibRaw_bit_buffer& bits,LibRaw_byte_buffer* bytes)
  580. {
  581. int col, c, diff, pred, spred=0;
  582. ushort mark=0, *row[3];
  583. if (jrow * jh->wide % jh->restart == 0) {
  584. FORC(6) jh->vpred[c] = 1 << (jh->bits-1);
  585. if (jrow) {
  586. bytes->unseek2();
  587. do mark = (mark << 8) + (c = bytes->get_byte());
  588. while (c != EOF && mark >> 4 != 0xffd);
  589. }
  590. bits.reset();
  591. }
  592. FORC3 row[c] = jh->row + jh->wide*jh->clrs*((jrow+c) & 1);
  593. for (col=0; col < jh->wide; col++)
  594. FORC(jh->clrs) {
  595. diff = ljpeg_diff_new (bits,bytes,jh->huff[c]);
  596. if (jh->sraw && c <= jh->sraw && (col | c))
  597. pred = spred;
  598. else if (col) pred = row[0][-jh->clrs];
  599. else pred = (jh->vpred[c] += diff) - diff;
  600. if (jrow && col) switch (jh->psv) {
  601. case 1: break;
  602. case 2: pred = row[1][0]; break;
  603. case 3: pred = row[1][-jh->clrs]; break;
  604. case 4: pred = pred + row[1][0] - row[1][-jh->clrs]; break;
  605. case 5: pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1); break;
  606. case 6: pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1); break;
  607. case 7: pred = (pred + row[1][0]) >> 1; break;
  608. default: pred = 0;
  609. }
  610. if ((**row = pred + diff) >> jh->bits) derror();
  611. if (c <= jh->sraw) spred = **row;
  612. row[0]++; row[1]++;
  613. }
  614. return row[2];
  615. }
  616. #endif
  617. ushort * CLASS ljpeg_row (int jrow, struct jhead *jh)
  618. {
  619. int col, c, diff, pred, spred=0;
  620. ushort mark=0, *row[3];
  621. if (jrow * jh->wide % jh->restart == 0) {
  622. FORC(6) jh->vpred[c] = 1 << (jh->bits-1);
  623. if (jrow) {
  624. fseek (ifp, -2, SEEK_CUR);
  625. do mark = (mark << 8) + (c = fgetc(ifp));
  626. while (c != EOF && mark >> 4 != 0xffd);
  627. }
  628. getbits(-1);
  629. }
  630. FORC3 row[c] = jh->row + jh->wide*jh->clrs*((jrow+c) & 1);
  631. for (col=0; col < jh->wide; col++)
  632. FORC(jh->clrs) {
  633. diff = ljpeg_diff (jh->huff[c]);
  634. if (jh->sraw && c <= jh->sraw && (col | c))
  635. pred = spred;
  636. else if (col) pred = row[0][-jh->clrs];
  637. else pred = (jh->vpred[c] += diff) - diff;
  638. if (jrow && col) switch (jh->psv) {
  639. case 1: break;
  640. case 2: pred = row[1][0]; break;
  641. case 3: pred = row[1][-jh->clrs]; break;
  642. case 4: pred = pred + row[1][0] - row[1][-jh->clrs]; break;
  643. case 5: pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1); break;
  644. case 6: pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1); break;
  645. case 7: pred = (pred + row[1][0]) >> 1; break;
  646. default: pred = 0;
  647. }
  648. if ((**row = pred + diff) >> jh->bits) derror();
  649. if (c <= jh->sraw) spred = **row;
  650. row[0]++; row[1]++;
  651. }
  652. return row[2];
  653. }
  654. void CLASS lossless_jpeg_load_raw()
  655. {
  656. int jwide, jrow, jcol, val, i, row=0, col=0;
  657. #ifndef LIBRAW_LIBRARY_BUILD
  658. int jidx,j;
  659. #endif
  660. struct jhead jh;
  661. ushort *rp;
  662. #ifdef LIBRAW_LIBRARY_BUILD
  663. int save_min = 0;
  664. unsigned slicesW[16],slicesWcnt=0,slices;
  665. unsigned *offset;
  666. unsigned t_y=0,t_x=0,t_s=0,slice=0,pixelsInSlice,pixno;
  667. if (!strcasecmp(make,"KODAK"))
  668. save_min = 1;
  669. #endif
  670. #ifdef LIBRAW_LIBRARY_BUILD
  671. if (cr2_slice[0]>15)
  672. throw LIBRAW_EXCEPTION_IO_EOF; // change many slices
  673. #else
  674. if (cr2_slice[0]>15)
  675. {
  676. fprintf(stderr,"Too many CR2 slices: %d\n",cr2_slice[0]+1);
  677. return;
  678. }
  679. #endif
  680. if (!ljpeg_start (&jh, 0)) return;
  681. jwide = jh.wide * jh.clrs;
  682. #ifdef LIBRAW_LIBRARY_BUILD
  683. if(cr2_slice[0])
  684. {
  685. for(i=0;i<cr2_slice[0];i++)
  686. slicesW[slicesWcnt++] = cr2_slice[1];
  687. slicesW[slicesWcnt++] = cr2_slice[2];
  688. }
  689. else
  690. {
  691. // not sliced
  692. slicesW[slicesWcnt++] = raw_width; // safe fallback
  693. }
  694. slices = slicesWcnt * jh.high;
  695. offset = (unsigned*)calloc(slices+1,sizeof(offset[0]));
  696. for(slice=0;slice<slices;slice++)
  697. {
  698. offset[slice] = (t_x + t_y * raw_width)| (t_s<<28);
  699. if((offset[slice] & 0x0fffffff) >= raw_width * raw_height)
  700. throw LIBRAW_EXCEPTION_IO_BADFILE;
  701. t_y++;
  702. if(t_y == jh.high)
  703. {
  704. t_y = 0;
  705. t_x += slicesW[t_s++];
  706. }
  707. }
  708. offset[slices] = offset[slices-1];
  709. slice = 1; // next slice
  710. pixno = offset[0];
  711. pixelsInSlice = slicesW[0];
  712. #endif
  713. #ifdef LIBRAW_LIBRARY_BUILD
  714. LibRaw_byte_buffer *buf=NULL;
  715. if(data_size)
  716. buf = ifp->make_byte_buffer(data_size);
  717. LibRaw_bit_buffer bits;
  718. #endif
  719. for (jrow=0; jrow < jh.high; jrow++) {
  720. #ifdef LIBRAW_LIBRARY_BUILD
  721. if (buf)
  722. rp = ljpeg_row_new (jrow, &jh,bits,buf);
  723. else
  724. #endif
  725. rp = ljpeg_row (jrow, &jh);
  726. if (load_flags & 1)
  727. row = jrow & 1 ? height-1-jrow/2 : jrow/2;
  728. for (jcol=0; jcol < jwide; jcol++) {
  729. val = curve[*rp++];
  730. #ifndef LIBRAW_LIBRARY_BUILD
  731. // slow dcraw way to calculate row/col
  732. if (cr2_slice[0]) {
  733. jidx = jrow*jwide + jcol;
  734. i = jidx / (cr2_slice[1]*jh.high);
  735. if ((j = i >= cr2_slice[0]))
  736. i = cr2_slice[0];
  737. jidx -= i * (cr2_slice[1]*jh.high);
  738. row = jidx / cr2_slice[1+j];
  739. col = jidx % cr2_slice[1+j] + i*cr2_slice[1];
  740. }
  741. #else
  742. // new fast one, but for data_size defined only (i.e. new CR2 format, not 1D/1Ds)
  743. if(buf)
  744. {
  745. if(!(load_flags & 1))
  746. row = pixno/raw_width;
  747. col = pixno % raw_width;
  748. pixno++;
  749. if (0 == --pixelsInSlice)
  750. {
  751. unsigned o = offset[slice++];
  752. pixno = o & 0x0fffffff;
  753. pixelsInSlice = slicesW[o>>28];
  754. }
  755. }
  756. #endif
  757. if (raw_width == 3984 && (col -= 2) < 0)
  758. col += (row--,raw_width);
  759. if (row >= 0) RAW(row,col) = val;
  760. #ifndef LIBRAW_LIBRARY_BUILD
  761. if (++col >= raw_width)
  762. col = (row++,0);
  763. #else
  764. if(!buf) // 1D or 1Ds case
  765. if (++col >= raw_width)
  766. col = (row++,0);
  767. #endif
  768. }
  769. }
  770. ljpeg_end (&jh);
  771. #ifdef LIBRAW_LIBRARY_BUILD
  772. if(buf)
  773. delete buf;
  774. free(offset);
  775. #endif
  776. }
  777. void CLASS canon_sraw_load_raw()
  778. {
  779. struct jhead jh;
  780. short *rp=0, (*ip)[4];
  781. int jwide, slice, scol, ecol, row, col, jrow=0, jcol=0, pix[3], c;
  782. int v[3]={0,0,0}, ver, hue;
  783. char *cp;
  784. if (!ljpeg_start (&jh, 0)) return;
  785. jwide = (jh.wide >>= 1) * jh.clrs;
  786. #ifdef LIBRAW_LIBRARY_BUILD
  787. if(!data_size)
  788. throw LIBRAW_EXCEPTION_IO_BADFILE;
  789. LibRaw_byte_buffer *buf = ifp->make_byte_buffer(data_size);
  790. LibRaw_bit_buffer bits;
  791. #endif
  792. for (ecol=slice=0; slice <= cr2_slice[0]; slice++) {
  793. scol = ecol;
  794. ecol += cr2_slice[1] * 2 / jh.clrs;
  795. if (!cr2_slice[0] || ecol > raw_width-1) ecol = raw_width & -2;
  796. for (row=0; row < height; row += (jh.clrs >> 1) - 1) {
  797. ip = (short (*)[4]) image + row*width;
  798. for (col=scol; col < ecol; col+=2, jcol+=jh.clrs) {
  799. if ((jcol %= jwide) == 0)
  800. #ifdef LIBRAW_LIBRARY_BUILD
  801. rp = (short*) ljpeg_row_new (jrow++, &jh,bits,buf);
  802. #else
  803. rp = (short *) ljpeg_row (jrow++, &jh);
  804. #endif
  805. if (col >= width) continue;
  806. FORC (jh.clrs-2)
  807. ip[col + (c >> 1)*width + (c & 1)][0] = rp[jcol+c];
  808. ip[col][1] = rp[jcol+jh.clrs-2] - 16384;
  809. ip[col][2] = rp[jcol+jh.clrs-1] - 16384;
  810. }
  811. }
  812. }
  813. for (cp=model2; *cp && !isdigit(*cp); cp++);
  814. sscanf (cp, "%d.%d.%d", v, v+1, v+2);
  815. ver = (v[0]*1000 + v[1])*1000 + v[2];
  816. hue = (jh.sraw+1) << 2;
  817. if (unique_id >= 0x80000281 || (unique_id == 0x80000218 && ver > 1000006))
  818. hue = jh.sraw << 1;
  819. ip = (short (*)[4]) image;
  820. rp = ip[0];
  821. for (row=0; row < height; row++, ip+=width) {
  822. if (row & (jh.sraw >> 1))
  823. for (col=0; col < width; col+=2)
  824. for (c=1; c < 3; c++)
  825. if (row == height-1)
  826. ip[col][c] = ip[col-width][c];
  827. else ip[col][c] = (ip[col-width][c] + ip[col+width][c] + 1) >> 1;
  828. for (col=1; col < width; col+=2)
  829. for (c=1; c < 3; c++)
  830. if (col == width-1)
  831. ip[col][c] = ip[col-1][c];
  832. else ip[col][c] = (ip[col-1][c] + ip[col+1][c] + 1) >> 1;
  833. }
  834. for ( ; rp < ip[0]; rp+=4) {
  835. if (unique_id == 0x80000218 ||
  836. unique_id == 0x80000250 ||
  837. unique_id == 0x80000261 ||
  838. unique_id == 0x80000281 ||
  839. unique_id == 0x80000287) {
  840. rp[1] = (rp[1] << 2) + hue;
  841. rp[2] = (rp[2] << 2) + hue;
  842. pix[0] = rp[0] + (( 50*rp[1] + 22929*rp[2]) >> 14);
  843. pix[1] = rp[0] + ((-5640*rp[1] - 11751*rp[2]) >> 14);
  844. pix[2] = rp[0] + ((29040*rp[1] - 101*rp[2]) >> 14);
  845. } else {
  846. if (unique_id < 0x80000218) rp[0] -= 512;
  847. pix[0] = rp[0] + rp[2];
  848. pix[2] = rp[0] + rp[1];
  849. pix[1] = rp[0] + ((-778*rp[1] - (rp[2] << 11)) >> 12);
  850. }
  851. FORC3 rp[c] = CLIP(pix[c] * sraw_mul[c] >> 10);
  852. }
  853. #ifdef LIBRAW_LIBRARY_BUILD
  854. delete buf;
  855. #endif
  856. ljpeg_end (&jh);
  857. maximum = 0x3fff;
  858. }
  859. #ifndef LIBRAW_LIBRARY_BUILD
  860. void CLASS adobe_copy_pixel (unsigned row, unsigned col, ushort **rp)
  861. {
  862. int c;
  863. if (is_raw == 2 && shot_select) (*rp)++;
  864. if (raw_image) {
  865. if (row < raw_height && col < raw_width)
  866. RAW(row,col) = curve[**rp];
  867. *rp += is_raw;
  868. } else {
  869. if (row < height && col < width)
  870. FORC(tiff_samples)
  871. image[row*width+col][c] = curve[(*rp)[c]];
  872. *rp += tiff_samples;
  873. }
  874. if (is_raw == 2 && shot_select) (*rp)--;
  875. }
  876. #else
  877. void CLASS adobe_copy_pixel_raw (unsigned row, unsigned col, ushort **rp)
  878. {
  879. if (is_raw == 2 && shot_select) (*rp)++;
  880. if (row < raw_height && col < raw_width)
  881. RAW(row,col) = curve[**rp];
  882. *rp += is_raw;
  883. if (is_raw == 2 && shot_select) (*rp)--;
  884. }
  885. void CLASS adobe_copy_pixel_color (unsigned row, unsigned col, ushort **rp)
  886. {
  887. int c;
  888. if (is_raw == 2 && shot_select) (*rp)++;
  889. if (row < height && col < width)
  890. FORC(tiff_samples)
  891. image[row*width+col][c] = curve[(*rp)[c]];
  892. *rp += tiff_samples;
  893. if (is_raw == 2 && shot_select) (*rp)--;
  894. }
  895. #endif
  896. void CLASS lossless_dng_load_raw()
  897. {
  898. unsigned save, trow=0, tcol=0, jwide, jrow, jcol, row, col;
  899. struct jhead jh;
  900. ushort *rp;
  901. while (trow < raw_height) {
  902. save = ftell(ifp);
  903. if (tile_length < INT_MAX)
  904. fseek (ifp, get4(), SEEK_SET);
  905. if (!ljpeg_start (&jh, 0)) break;
  906. jwide = jh.wide;
  907. if (filters) jwide *= jh.clrs;
  908. jwide /= is_raw;
  909. #ifdef LIBRAW_LIBRARY_BUILD
  910. if(!data_size)
  911. throw LIBRAW_EXCEPTION_IO_BADFILE;
  912. LibRaw_byte_buffer *buf = ifp->make_byte_buffer(data_size);
  913. LibRaw_bit_buffer bits;
  914. #endif
  915. #ifndef LIBRAW_LIBRARY_BUILD
  916. for (row=col=jrow=0; jrow < jh.high; jrow++) {
  917. rp = ljpeg_row (jrow, &jh);
  918. for (jcol=0; jcol < jwide; jcol++) {
  919. adobe_copy_pixel (trow+row, tcol+col, &rp);
  920. if (++col >= tile_width || col >= raw_width)
  921. row += 1 + (col = 0);
  922. }
  923. }
  924. #else
  925. if(raw_image)
  926. {
  927. for (row=col=jrow=0; jrow < jh.high; jrow++) {
  928. #ifdef LIBRAW_LIBRARY_BUILD
  929. rp = ljpeg_row_new (jrow, &jh,bits,buf);
  930. #else
  931. rp = ljpeg_row (jrow, &jh);
  932. #endif
  933. for (jcol=0; jcol < jwide; jcol++) {
  934. adobe_copy_pixel_raw (trow+row, tcol+col, &rp);
  935. if (++col >= tile_width || col >= raw_width)
  936. row += 1 + (col = 0);
  937. }
  938. }
  939. }
  940. else
  941. {
  942. for (row=col=jrow=0; jrow < jh.high; jrow++) {
  943. #ifdef LIBRAW_LIBRARY_BUILD
  944. rp = ljpeg_row_new (jrow, &jh,bits,buf);
  945. #else
  946. rp = ljpeg_row (jrow, &jh);
  947. #endif
  948. for (jcol=0; jcol < jwide; jcol++) {
  949. adobe_copy_pixel_color (trow+row, tcol+col, &rp);
  950. if (++col >= tile_width || col >= raw_width)
  951. row += 1 + (col = 0);
  952. }
  953. }
  954. }
  955. #endif
  956. fseek (ifp, save+4, SEEK_SET);
  957. if ((tcol += tile_width) >= raw_width)
  958. trow += tile_length + (tcol = 0);
  959. ljpeg_end (&jh);
  960. #ifdef LIBRAW_LIBRARY_BUILD
  961. delete buf;
  962. #endif
  963. }
  964. }
  965. void CLASS packed_dng_load_raw()
  966. {
  967. ushort *pixel, *rp;
  968. int row, col;
  969. pixel = (ushort *) calloc (raw_width * tiff_samples, sizeof *pixel);
  970. merror (pixel, "packed_dng_load_raw()");
  971. #ifdef LIBRAW_LIBRARY_BUILD
  972. int dsz= raw_height*raw_width * tiff_samples * tiff_bps/8;
  973. LibRaw_byte_buffer *buf = NULL;
  974. if (tiff_bps != 16)
  975. {
  976. buf = ifp->make_byte_buffer(dsz);
  977. }
  978. LibRaw_bit_buffer bits;
  979. #endif
  980. for (row=0; row < raw_height; row++) {
  981. if (tiff_bps == 16)
  982. read_shorts (pixel, raw_width * tiff_samples);
  983. else {
  984. #ifdef LIBRAW_LIBRARY_BUILD
  985. bits.reset();
  986. for (col=0; col < raw_width * tiff_samples; col++)
  987. pixel[col] = bits._getbits(buf,tiff_bps,zero_after_ff);
  988. #else
  989. getbits(-1);
  990. for (col=0; col < raw_width * tiff_samples; col++)
  991. pixel[col] = getbits(tiff_bps);
  992. #endif
  993. }
  994. #ifndef LIBRAW_LIBRARY_BUILD
  995. for (rp=pixel, col=0; col < raw_width; col++)
  996. adobe_copy_pixel (row, col, &rp);
  997. #else
  998. if(raw_image)
  999. for (rp=pixel, col=0; col < raw_width; col++)
  1000. adobe_copy_pixel_raw (row, col, &rp);
  1001. else
  1002. for (rp=pixel, col=0; col < raw_width; col++)
  1003. adobe_copy_pixel_color (row, col, &rp);
  1004. #endif
  1005. }
  1006. free (pixel);
  1007. #ifdef LIBRAW_LIBRARY_BUILD
  1008. if(buf)
  1009. delete buf;
  1010. #endif
  1011. }
  1012. void CLASS pentax_load_raw()
  1013. {
  1014. ushort bit[2][15], huff[4097];
  1015. int dep, row, col, diff, c, i;
  1016. ushort vpred[2][2] = {{0,0},{0,0}}, hpred[2];
  1017. fseek (ifp, meta_offset, SEEK_SET);
  1018. dep = (get2() + 12) & 15;
  1019. fseek (ifp, 12, SEEK_CUR);
  1020. FORC(dep) bit[0][c] = get2();
  1021. FORC(dep) bit[1][c] = fgetc(ifp);
  1022. FORC(dep)
  1023. for (i=bit[0][c]; i <= ((bit[0][c]+(4096 >> bit[1][c])-1) & 4095); )
  1024. huff[++i] = bit[1][c] << 8 | c;
  1025. huff[0] = 12;
  1026. fseek (ifp, data_offset, SEEK_SET);
  1027. getbits(-1);
  1028. for (row=0; row < raw_height; row++)
  1029. for (col=0; col < raw_width; col++) {
  1030. diff = ljpeg_diff (huff);
  1031. if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
  1032. else hpred[col & 1] += diff;
  1033. RAW(row,col) = hpred[col & 1];
  1034. if (hpred[col & 1] >> tiff_bps) derror();
  1035. }
  1036. }
  1037. void CLASS nikon_load_raw()
  1038. {
  1039. static const uchar nikon_tree[][32] = {
  1040. { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy */
  1041. 5,4,3,6,2,7,1,0,8,9,11,10,12 },
  1042. { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy after split */
  1043. 0x39,0x5a,0x38,0x27,0x16,5,4,3,2,1,0,11,12,12 },
  1044. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0, /* 12-bit lossless */
  1045. 5,4,6,3,7,2,8,1,9,0,10,11,12 },
  1046. { 0,1,4,3,1,1,1,1,1,2,0,0,0,0,0,0, /* 14-bit lossy */
  1047. 5,6,4,7,8,3,9,2,1,0,10,11,12,13,14 },
  1048. { 0,1,5,1,1,1,1,1,1,1,2,0,0,0,0,0, /* 14-bit lossy after split */
  1049. 8,0x5c,0x4b,0x3a,0x29,7,6,5,4,3,2,1,0,13,14 },
  1050. { 0,1,4,2,2,3,1,2,0,0,0,0,0,0,0,0, /* 14-bit lossless */
  1051. 7,6,8,5,9,4,10,3,11,12,2,0,1,13,14 } };
  1052. ushort *huff, ver0, ver1, vpred[2][2], hpred[2], csize;
  1053. int i, min, max, step=0, tree=0, split=0, row, col, len, shl, diff;
  1054. fseek (ifp, meta_offset, SEEK_SET);
  1055. ver0 = fgetc(ifp);
  1056. ver1 = fgetc(ifp);
  1057. if (ver0 == 0x49 || ver1 == 0x58)
  1058. fseek (ifp, 2110, SEEK_CUR);
  1059. if (ver0 == 0x46) tree = 2;
  1060. if (tiff_bps == 14) tree += 3;
  1061. read_shorts (vpred[0], 4);
  1062. max = 1 << tiff_bps & 0x7fff;
  1063. if ((csize = get2()) > 1)
  1064. step = max / (csize-1);
  1065. if (ver0 == 0x44 && ver1 == 0x20 && step > 0) {
  1066. for (i=0; i < csize; i++)
  1067. curve[i*step] = get2();
  1068. for (i=0; i < max; i++)
  1069. curve[i] = ( curve[i-i%step]*(step-i%step) +
  1070. curve[i-i%step+step]*(i%step) ) / step;
  1071. fseek (ifp, meta_offset+562, SEEK_SET);
  1072. split = get2();
  1073. } else if (ver0 != 0x46 && csize <= 0x4001)
  1074. read_shorts (curve, max=csize);
  1075. while (curve[max-2] == curve[max-1]) max--;
  1076. huff = make_decoder (nikon_tree[tree]);
  1077. fseek (ifp, data_offset, SEEK_SET);
  1078. #ifdef LIBRAW_LIBRARY_BUILD
  1079. if(!data_size)
  1080. throw LIBRAW_EXCEPTION_IO_BADFILE;
  1081. LibRaw_byte_buffer *buf = ifp->make_byte_buffer(data_size);
  1082. LibRaw_bit_buffer bits;
  1083. bits.reset();
  1084. #else
  1085. getbits(-1);
  1086. #endif
  1087. for (min=row=0; row < height; row++) {
  1088. if (split && row == split) {
  1089. free (huff);
  1090. huff = make_decoder (nikon_tree[tree+1]);
  1091. max += (min = 16) << 1;
  1092. }
  1093. for (col=0; col < raw_width; col++) {
  1094. #ifdef LIBRAW_LIBRARY_BUILD
  1095. i = bits._gethuff(buf,*huff,huff+1,zero_after_ff);
  1096. #else
  1097. i = gethuff(huff);
  1098. #endif
  1099. len = i & 15;
  1100. shl = i >> 4;
  1101. #ifdef LIBRAW_LIBRARY_BUILD
  1102. diff = ((bits._getbits(buf,len-shl,zero_after_ff) << 1) + 1) << shl >> 1;
  1103. #else
  1104. diff = ((getbits(len-shl) << 1) + 1) << shl >> 1;
  1105. #endif
  1106. if ((diff & (1 << (len-1))) == 0)
  1107. diff -= (1 << len) - !shl;
  1108. if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
  1109. else hpred[col & 1] += diff;
  1110. if ((ushort)(hpred[col & 1] + min) >= max) derror();
  1111. RAW(row,col) = curve[LIM((short)hpred[col & 1],0,0x3fff)];
  1112. }
  1113. }
  1114. #ifdef LIBRAW_LIBRARY_BUILD
  1115. delete buf;
  1116. #endif
  1117. free (huff);
  1118. }
  1119. /*
  1120. Returns 1 for a Coolpix 995, 0 for anything else.
  1121. */
  1122. int CLASS nikon_e995()
  1123. {
  1124. int i, histo[256];
  1125. const uchar often[] = { 0x00, 0x55, 0xaa, 0xff };
  1126. memset (histo, 0, sizeof histo);
  1127. fseek (ifp, -2000, SEEK_END);
  1128. for (i=0; i < 2000; i++)
  1129. histo[fgetc(ifp)]++;
  1130. for (i=0; i < 4; i++)
  1131. if (histo[often[i]] < 200)
  1132. return 0;
  1133. return 1;
  1134. }
  1135. /*
  1136. Returns 1 for a Coolpix 2100, 0 for anything else.
  1137. */
  1138. int CLASS nikon_e2100()
  1139. {
  1140. uchar t[12];
  1141. int i;
  1142. fseek (ifp, 0, SEEK_SET);
  1143. for (i=0; i < 1024; i++) {
  1144. fread (t, 1, 12, ifp);
  1145. if (((t[2] & t[4] & t[7] & t[9]) >> 4
  1146. & t[1] & t[6] & t[8] & t[11] & 3) != 3)
  1147. return 0;
  1148. }
  1149. return 1;
  1150. }
  1151. void CLASS nikon_3700()
  1152. {
  1153. int bits, i;
  1154. uchar dp[24];
  1155. static const struct {
  1156. int bits;
  1157. char t_make[12], t_model[15];
  1158. } table[] = {
  1159. { 0x00, "PENTAX", "Optio 33WR" },
  1160. { 0x03, "NIKON", "E3200" },
  1161. { 0x32, "NIKON", "E3700" },
  1162. { 0x33, "OLYMPUS", "C740UZ" } };
  1163. fseek (ifp, 3072, SEEK_SET);
  1164. fread (dp, 1, 24, ifp);
  1165. bits = (dp[8] & 3) << 4 | (dp[20] & 3);
  1166. for (i=0; i < sizeof table / sizeof *table; i++)
  1167. if (bits == table[i].bits) {
  1168. strcpy (make, table[i].t_make );
  1169. strcpy (model, table[i].t_model);
  1170. }
  1171. }
  1172. /*
  1173. Separates a Minolta DiMAGE Z2 from a Nikon E4300.
  1174. */
  1175. int CLASS minolta_z2()
  1176. {
  1177. int i, nz;
  1178. char tail[424];
  1179. fseek (ifp, -sizeof tail, SEEK_END);
  1180. fread (tail, 1, sizeof tail, ifp);
  1181. for (nz=i=0; i < sizeof tail; i++)
  1182. if (tail[i]) nz++;
  1183. return nz > 20;
  1184. }
  1185. void CLASS ppm_thumb()
  1186. {
  1187. char *thumb;
  1188. thumb_length = thumb_width*thumb_height*3;
  1189. thumb = (char *) malloc (thumb_length);
  1190. merror (thumb, "ppm_thumb()");
  1191. fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
  1192. fread (thumb, 1, thumb_length, ifp);
  1193. fwrite (thumb, 1, thumb_length, ofp);
  1194. free (thumb);
  1195. }
  1196. void CLASS ppm16_thumb()
  1197. {
  1198. int i;
  1199. char *thumb;
  1200. thumb_length = thumb_width*thumb_height*3;
  1201. thumb = (char *) calloc (thumb_length,2);
  1202. merror (thumb, "ppm16_thumb()");
  1203. read_shorts ((ushort *) thumb, thumb_length);
  1204. for (i=0; i < thumb_length; i++)
  1205. thumb[i] = ((ushort *) thumb)[i] >> 8;
  1206. fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
  1207. fwrite (thumb, 1, thumb_length, ofp);
  1208. free (thumb);
  1209. }
  1210. void CLASS layer_thumb()
  1211. {
  1212. int i, c;
  1213. char *thumb, map[][4] = { "012","102" };
  1214. colors = thumb_misc >> 5 & 7;
  1215. thumb_length = thumb_width*thumb_height;
  1216. thumb = (char *) calloc (colors, thumb_length);
  1217. merror (thumb, "layer_thumb()");
  1218. fprintf (ofp, "P%d\n%d %d\n255\n",
  1219. 5 + (colors >> 1), thumb_width, thumb_height);
  1220. fread (thumb, thumb_length, colors, ifp);
  1221. for (i=0; i < thumb_length; i++)
  1222. FORCC putc (thumb[i+thumb_length*(map[thumb_misc >> 8][c]-'0')], ofp);
  1223. free (thumb);
  1224. }
  1225. void CLASS rollei_thumb()
  1226. {
  1227. unsigned i;
  1228. ushort *thumb;
  1229. thumb_length = thumb_width * thumb_height;
  1230. thumb = (ushort *) calloc (thumb_length, 2);
  1231. merror (thumb, "rollei_thumb()");
  1232. fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
  1233. read_shorts (thumb, thumb_length);
  1234. for (i=0; i < thumb_length; i++) {
  1235. putc (thumb[i] << 3, ofp);
  1236. putc (thumb[i] >> 5 << 2, ofp);
  1237. putc (thumb[i] >> 11 << 3, ofp);
  1238. }
  1239. free (thumb);
  1240. }
  1241. void CLASS rollei_load_raw()
  1242. {
  1243. uchar pixel[10];
  1244. unsigned iten=0, isix, i, buffer=0, todo[16];
  1245. isix = raw_width * raw_height * 5 / 8;
  1246. while (fread (pixel, 1, 10, ifp) == 10) {
  1247. for (i=0; i < 10; i+=2) {
  1248. todo[i] = iten++;
  1249. todo[i+1] = pixel[i] << 8 | pixel[i+1];
  1250. buffer = pixel[i] >> 2 | buffer << 6;
  1251. }
  1252. for ( ; i < 16; i+=2) {
  1253. todo[i] = isix++;
  1254. todo[i+1] = buffer >> (14-i)*5;
  1255. }
  1256. for (i=0; i < 16; i+=2)
  1257. raw_image[todo[i]] = (todo[i+1] & 0x3ff);
  1258. }
  1259. maximum = 0x3ff;
  1260. }
  1261. int CLASS raw (unsigned row, unsigned col)
  1262. {
  1263. return (row < raw_height && col < raw_width) ? RAW(row,col) : 0;
  1264. }
  1265. void CLASS phase_one_flat_field (int is_float, int nc)
  1266. {
  1267. ushort head[8];
  1268. unsigned wide, y, x, c, rend, cend, row, col;
  1269. float *mrow, num, mult[4];
  1270. read_shorts (head, 8);
  1271. wide = head[2] / head[4];
  1272. mrow = (float *) calloc (nc*wide, sizeof *mrow);
  1273. merror (mrow, "phase_one_flat_field()");
  1274. for (y=0; y < head[3] / head[5]; y++) {
  1275. for (x=0; x < wide; x++)
  1276. for (c=0; c < nc; c+=2) {
  1277. num = is_float ? getreal(11) : get2()/32768.0;
  1278. if (y==0) mrow[c*wide+x] = num;
  1279. else mrow[(c+1)*wide+x] = (num - mrow[c*wide+x]) / head[5];
  1280. }
  1281. if (y==0) continue;
  1282. rend = head[1] + y*head[5];
  1283. for (row = rend-head[5]; row < raw_height && row < rend; row++) {
  1284. for (x=1; x < wide; x++) {
  1285. for (c=0; c < nc; c+=2) {
  1286. mult[c] = mrow[c*wide+x-1];
  1287. mult[c+1] = (mrow[c*wide+x] - mult[c]) / head[4];
  1288. }
  1289. cend = head[0] + x*head[4];
  1290. for (col = cend-head[4]; col < raw_width && col < cend; col++) {
  1291. c = nc > 2 ? FC(row-top_margin,col-left_margin) : 0;
  1292. if (!(c & 1)) {
  1293. c = RAW(row,col) * mult[c];
  1294. RAW(row,col) = LIM(c,0,65535);
  1295. }
  1296. for (c=0; c < nc; c+=2)
  1297. mult[c] += mult[c+1];
  1298. }
  1299. }
  1300. for (x=0; x < wide; x++)
  1301. for (c=0; c < nc; c+=2)
  1302. mrow[c*wide+x] += mrow[(c+1)*wide+x];
  1303. }
  1304. }
  1305. free (mrow);
  1306. }
  1307. void CLASS phase_one_correct()
  1308. {
  1309. unsigned entries, tag, data, save, col, row, type;
  1310. int len, i, j, k, cip, val[4], dev[4], sum, max;
  1311. int head[9], diff, mindiff=INT_MAX, off_412=0;
  1312. static const signed char dir[12][2] =
  1313. { {-1,-1}, {-1,1}, {1,-1}, {1,1}, {-2,0}, {0,-2}, {0,2}, {2,0},
  1314. {-2,-2}, {-2,2}, {2,-2}, {2,2} };
  1315. float poly[8], num, cfrac, frac, mult[2], *yval[2];
  1316. ushort *xval[2];
  1317. if (half_size || !meta_length) return;
  1318. #ifdef DCRAW_VERBOSE
  1319. if (verbose) fprintf (stderr,_("Phase One correction...\n"));
  1320. #endif
  1321. fseek (ifp, meta_offset, SEEK_SET);
  1322. order = get2();
  1323. fseek (ifp, 6, SEEK_CUR);
  1324. fseek (ifp, meta_offset+get4(), SEEK_SET);
  1325. entries = get4(); get4();
  1326. while (entries--) {
  1327. tag = get4();
  1328. len = get4();
  1329. data = get4();
  1330. save = ftell(ifp);
  1331. fseek (ifp, meta_offset+data, SEEK_SET);
  1332. if (tag == 0x419) { /* Polynomial curve */
  1333. for (get4(), i=0; i < 8; i++)
  1334. poly[i] = getreal(11);
  1335. poly[3] += (ph1.tag_210 - poly[7]) * poly[6] + 1;
  1336. for (i=0; i < 0x10000; i++) {
  1337. num = (poly[5]*i + poly[3])*i + poly[1];
  1338. curve[i] = LIM(num,0,65535);
  1339. } goto apply; /* apply to right half */
  1340. } else if (tag == 0x41a) { /* Polynomial curve */
  1341. for (i=0; i < 4; i++)
  1342. poly[i] = getreal(11);
  1343. for (i=0; i < 0x10000; i++) {
  1344. for (num=0, j=4; j--; )
  1345. num = num * i + poly[j];
  1346. curve[i] = LIM(num+i,0,65535);
  1347. } apply: /* apply to whole image */
  1348. for (row=0; row < raw_height; row++)
  1349. for (col = (tag & 1)*ph1.split_col; col < raw_width; col++)
  1350. RAW(row,col) = curve[RAW(row,col)];
  1351. } else if (tag == 0x400) { /* Sensor defects */
  1352. while ((len -= 8) >= 0) {
  1353. col = get2();
  1354. row = get2();
  1355. type = get2(); get2();
  1356. if (col >= raw_width) continue;
  1357. if (type == 131) /* Bad column */
  1358. for (row=0; row < raw_height; row++)
  1359. if (FC(row-top_margin,col-left_margin) == 1) {
  1360. for (sum=i=0; i < 4; i++)
  1361. sum += val[i] = raw (row+dir[i][0], col+dir[i][1]);
  1362. for (max=i=0; i < 4; i++) {
  1363. dev[i] = abs((val[i] << 2) - sum);
  1364. if (dev[max] < dev[i]) max = i;
  1365. }
  1366. RAW(row,col) = (sum - val[max])/3.0 + 0.5;
  1367. } else {
  1368. for (sum=0, i=8; i < 12; i++)
  1369. sum += raw (row+dir[i][0], col+dir[i][1]);
  1370. RAW(row,col) = 0.5 + sum * 0.0732233 +
  1371. (raw(row,col-2) + raw(row,col+2)) * 0.3535534;
  1372. }
  1373. else if (type == 129) { /* Bad pixel */
  1374. if (row >= raw_height) continue;
  1375. j = (FC(row-top_margin,col-left_margin) != 1) * 4;
  1376. for (sum=0, i=j; i < j+8; i++)
  1377. sum += raw (row+dir[i][0], col+dir[i][1]);
  1378. RAW(row,col) = (sum + 4) >> 3;
  1379. }
  1380. }
  1381. } else if (tag == 0x401) { /* All-color flat fields */
  1382. phase_one_flat_field (1, 2);
  1383. } else if (tag == 0x416 || tag == 0x410) {
  1384. phase_one_flat_field (0, 2);
  1385. } else if (tag == 0x40b) { /* Red+blue flat field */
  1386. phase_one_flat_field (0, 4);
  1387. } else if (tag == 0x412) {
  1388. fseek (ifp, 36, SEEK_CUR);
  1389. diff = abs (get2() - ph1.tag_21a);
  1390. if (mindiff > diff) {
  1391. mindiff = diff;
  1392. off_412 = ftell(ifp) - 38;
  1393. }
  1394. }
  1395. fseek (ifp, save, SEEK_SET);
  1396. }
  1397. if (off_412) {
  1398. fseek (ifp, off_412, SEEK_SET);
  1399. for (i=0; i < 9; i++) head[i] = get4() & 0x7fff;
  1400. yval[0] = (float *) calloc (head[1]*head[3] + head[2]*head[4], 6);
  1401. merror (yval[0], "phase_one_correct()");
  1402. yval[1] = (float *) (yval[0] + head[1]*head[3]);
  1403. xval[0] = (ushort *) (yval[1] + head[2]*head[4]);
  1404. xval[1] = (ushort *) (xval[0] + head[1]*head[3]);
  1405. get2();
  1406. for (i=0; i < 2; i++)
  1407. for (j=0; j < head[i+1]*head[i+3]; j++)
  1408. yval[i][j] = getreal(11);
  1409. for (i=0; i < 2; i++)
  1410. for (j=0; j < head[i+1]*head[i+3]; j++)
  1411. xval[i][j] = get2();
  1412. for (row=0; row < raw_height; row++)
  1413. for (col=0; col < raw_width; col++) {
  1414. cfrac = (float) col * head[3] / raw_width;
  1415. cfrac -= cip = cfrac;
  1416. num = RAW(row,col) * 0.5;
  1417. for (i=cip; i < cip+2; i++) {
  1418. for (k=j=0; j < head[1]; j++)
  1419. if (num < xval[0][k = head[1]*i+j]) break;
  1420. frac = (j == 0 || j == head[1]) ? 0 :
  1421. (xval[0][k] - num) / (xval[0][k] - xval[0][k-1]);
  1422. mult[i-cip] = yval[0][k-1] * frac + yval[0][k] * (1-frac);
  1423. }
  1424. i = ((mult[0] * (1-cfrac) + mult[1] * cfrac) * row + num) * 2;
  1425. RAW(row,col) = LIM(i,0,65535);
  1426. }
  1427. free (yval[0]);
  1428. }
  1429. }
  1430. void CLASS phase_one_load_raw()
  1431. {
  1432. int a, b, i;
  1433. ushort akey, bkey, t_mask;
  1434. fseek (ifp, ph1.key_off, SEEK_SET);
  1435. akey = get2();
  1436. bkey = get2();
  1437. t_mask = ph1.format == 1 ? 0x5555:0x1354;
  1438. fseek (ifp, data_offset, SEEK_SET);
  1439. read_shorts (raw_image, raw_width*raw_height);
  1440. if (ph1.format)
  1441. for (i=0; i < raw_width*raw_height; i+=2) {
  1442. a = raw_image[i+0] ^ akey;
  1443. b = raw_image[i+1] ^ bkey;
  1444. raw_image[i+0] = (a & t_mask) | (b & ~t_mask);
  1445. raw_image[i+1] = (b & t_mask) | (a & ~t_mask);
  1446. }
  1447. }
  1448. unsigned CLASS ph1_bithuff (int nbits, ushort *huff)
  1449. {
  1450. #ifndef LIBRAW_NOTHREADS
  1451. #define bitbuf tls->ph1_bits.bitbuf
  1452. #define vbits tls->ph1_bits.vbits
  1453. #else
  1454. static UINT64 bitbuf=0;
  1455. static int vbits=0;
  1456. #endif
  1457. unsigned c;
  1458. if (nbits == -1)
  1459. return bitbuf = vbits = 0;
  1460. if (nbits == 0) return 0;
  1461. if (vbits < nbits) {
  1462. bitbuf = bitbuf << 32 | get4();
  1463. vbits += 32;
  1464. }
  1465. c = bitbuf << (64-vbits) >> (64-nbits);
  1466. if (huff) {
  1467. vbits -= huff[c] >> 8;
  1468. return (uchar) huff[c];
  1469. }
  1470. vbits -= nbits;
  1471. return c;
  1472. #ifndef LIBRAW_NOTHREADS
  1473. #undef bitbuf
  1474. #undef vbits
  1475. #endif
  1476. }
  1477. #define ph1_bits(n) ph1_bithuff(n,0)
  1478. #define ph1_huff(h) ph1_bithuff(*h,h+1)
  1479. void CLASS phase_one_load_raw_c()
  1480. {
  1481. static const int length[] = { 8,7,6,9,11,10,5,12,14,13 };
  1482. int *offset, len[2], pred[2], row, col, i, j;
  1483. ushort *pixel;
  1484. short (*t_black)[2];
  1485. pixel = (ushort *) calloc (raw_width + raw_height*4, 2);
  1486. merror (pixel, "phase_one_load_raw_c()");
  1487. offset = (int *) (pixel + raw_width);
  1488. fseek (ifp, strip_offset, SEEK_SET);
  1489. for (row=0; row < raw_height; row++)
  1490. offset[row] = get4();
  1491. t_black = (short (*)[2]) offset + raw_height;
  1492. fseek (ifp, ph1.black_off, SEEK_SET);
  1493. if (ph1.black_off)
  1494. {
  1495. read_shorts ((ushort *) t_black[0], raw_height*2);
  1496. #ifdef LIBRAW_LIBRARY_BUILD
  1497. imgdata.rawdata.ph1_black = (short (*)[2])calloc(raw_height*2,sizeof(short));
  1498. merror (imgdata.rawdata.ph1_black, "phase_one_load_raw_c()");
  1499. memmove(imgdata.rawdata.ph1_black,(short *) t_black[0],raw_height*2*sizeof(short));
  1500. #endif
  1501. }
  1502. for (i=0; i < 256; i++)
  1503. curve[i] = i*i / 3.969 + 0.5;
  1504. for (row=0; row < raw_height; row++) {
  1505. fseek (ifp, data_offset + offset[row], SEEK_SET);
  1506. ph1_bits(-1);
  1507. pred[0] = pred[1] = 0;
  1508. for (col=0; col < raw_width; col++) {
  1509. if (col >= (raw_width & -8))
  1510. len[0] = len[1] = 14;
  1511. else if ((col & 7) == 0)
  1512. for (i=0; i < 2; i++) {
  1513. for (j=0; j < 5 && !ph1_bits(1); j++);
  1514. if (j--) len[i] = length[j*2 + ph1_bits(1)];
  1515. }
  1516. if ((i = len[col & 1]) == 14)
  1517. pixel[col] = pred[col & 1] = ph1_bits(16);
  1518. else
  1519. pixel[col] = pred[col & 1] += ph1_bits(i) + 1 - (1 << (i - 1));
  1520. if (pred[col & 1] >> 16) derror();
  1521. if (ph1.format == 5 && pixel[col] < 256)
  1522. pixel[col] = curve[pixel[col]];
  1523. }
  1524. for (col=0; col < raw_width; col++) {
  1525. #ifndef LIBRAW_LIBRARY_BUILD
  1526. i = (pixel[col] << 2) - ph1.t_black + t_black[row][col >= ph1.split_col];
  1527. if (i > 0) RAW(row,col) = i;
  1528. #else
  1529. RAW(row,col) = pixel[col] << 2;
  1530. #endif
  1531. }
  1532. }
  1533. free (pixel);
  1534. maximum = 0xfffc - ph1.t_black;
  1535. }
  1536. void CLASS hasselblad_load_raw()
  1537. {
  1538. struct jhead jh;
  1539. int row, col, pred[2], len[2], diff, c;
  1540. if (!ljpeg_start (&jh, 0)) return;
  1541. order = 0x4949;
  1542. ph1_bits(-1);
  1543. for (row=0; row < raw_height; row++) {
  1544. pred[0] = pred[1] = 0x8000 + load_flags;
  1545. for (col=0; col < raw_width; col+=2) {
  1546. FORC(2) len[c] = ph1_huff(jh.huff[0]);
  1547. FORC(2) {
  1548. diff = ph1_bits(len[c]);
  1549. if ((diff & (1 << (len[c]-1))) == 0)
  1550. diff -= (1 << len[c]) - 1;
  1551. if (diff == 65535) diff = -32768;
  1552. RAW(row,col+c) = pred[c] += diff;
  1553. }
  1554. }
  1555. }
  1556. ljpeg_end (&jh);
  1557. maximum = 0xffff;
  1558. }
  1559. void CLASS leaf_hdr_load_raw()
  1560. {
  1561. ushort *pixel=0;
  1562. unsigned tile=0, r, c, row, col;
  1563. if (!filters) {
  1564. pixel = (ushort *) calloc (raw_width, sizeof *pixel);
  1565. merror (pixel, "leaf_hdr_load_raw()");
  1566. }
  1567. FORC(tiff_samples)
  1568. for (r=0; r < raw_height; r++) {
  1569. if (r % tile_length == 0) {
  1570. fseek (ifp, data_offset + 4*tile++, SEEK_SET);
  1571. fseek (ifp, get4(), SEEK_SET);
  1572. }
  1573. if (filters && c != shot_select) continue;
  1574. if (filters) pixel = raw_image + r*raw_width;
  1575. read_shorts (pixel, raw_width);
  1576. if (!filters && (row = r - top_margin) < height)
  1577. for (col=0; col < width; col++)
  1578. image[row*width+col][c] = pixel[col+left_margin];
  1579. }
  1580. if (!filters) {
  1581. maximum = 0xffff;
  1582. raw_color = 1;
  1583. free (pixel);
  1584. }
  1585. }
  1586. void CLASS unpacked_load_raw()
  1587. {
  1588. int row, col, bits=0;
  1589. while (1 << ++bits < maximum);
  1590. read_shorts (raw_image, raw_width*raw_height);
  1591. for (row=0; row < raw_height; row++)
  1592. for (col=0; col < raw_width; col++)
  1593. if ((RAW(row,col) >>= load_flags) >> bits
  1594. && (unsigned) (row-top_margin) < height
  1595. && (unsigned) (col-left_margin) < width) derror();
  1596. }
  1597. void CLASS sinar_4shot_load_raw()
  1598. {
  1599. ushort *pixel;
  1600. unsigned shot, row, col, r, c;
  1601. if ((shot = shot_select) || half_size) {
  1602. if (shot) shot--;
  1603. if (shot > 3) shot = 3;
  1604. fseek (ifp, data_offset + shot*4, SEEK_SET);
  1605. fseek (ifp, get4(), SEEK_SET);
  1606. unpacked_load_raw();
  1607. return;
  1608. }
  1609. #ifndef LIBRAW_LIBRARY_BUILD
  1610. free (raw_image);
  1611. raw_image = 0;
  1612. free (image);
  1613. image = (ushort (*)[4])
  1614. calloc ((iheight=height)*(iwidth=width), sizeof *image);
  1615. merror (image, "sinar_4shot_load_raw()");
  1616. #endif
  1617. pixel = (ushort *) calloc (raw_width, sizeof *pixel);
  1618. merror (pixel, "sinar_4shot_load_raw()");
  1619. for (shot=0; shot < 4; shot++) {
  1620. fseek (ifp, data_offset + shot*4, SEEK_SET);
  1621. fseek (ifp, get4(), SEEK_SET);
  1622. for (row=0; row < raw_height; row++) {
  1623. read_shorts (pixel, raw_width);
  1624. if ((r = row-top_margin - (shot >> 1 & 1)) >= height) continue;
  1625. for (col=0; col < raw_width; col++) {
  1626. if ((c = col-left_margin - (shot & 1)) >= width) continue;
  1627. image[r*width+c][FC(row,col)] = pixel[col];
  1628. }
  1629. }
  1630. }
  1631. free (pixel);
  1632. shrink = filters = 0;
  1633. }
  1634. void CLASS imacon_full_load_raw()
  1635. {
  1636. int row, col;
  1637. for (row=0; row < height; row++)
  1638. for (col=0; col < width; col++)
  1639. read_shorts (image[row*width+col], 3);
  1640. }
  1641. void CLASS packed_load_raw()
  1642. {
  1643. int vbits=0, bwide, pwide, rbits, bite, half, irow, row, col, val, i;
  1644. UINT64 bitbuf=0;
  1645. if (raw_width * 8 >= width * tiff_bps) /* Is raw_width in bytes? */
  1646. pwide = (bwide = raw_width) * 8 / tiff_bps;
  1647. else bwide = (pwide = raw_width) * tiff_bps / 8;
  1648. rbits = bwide * 8 - pwide * tiff_bps;
  1649. if (load_flags & 1) bwide = bwide * 16 / 15;
  1650. bite = 8 + (load_flags & 24);
  1651. half = (raw_height+1) >> 1;
  1652. for (irow=0; irow < raw_height; irow++) {
  1653. row = irow;
  1654. if (load_flags & 2 &&
  1655. (row = irow % half * 2 + irow / half) == 1 &&
  1656. load_flags & 4) {
  1657. if (vbits=0, tiff_compress)
  1658. fseek (ifp, data_offset - (-half*bwide & -2048), SEEK_SET);
  1659. else {
  1660. fseek (ifp, 0, SEEK_END);
  1661. fseek (ifp, ftell(ifp) >> 3 << 2, SEEK_SET);
  1662. }
  1663. }
  1664. for (col=0; col < pwide; col++) {
  1665. for (vbits -= tiff_bps; vbits < 0; vbits += bite) {
  1666. bitbuf <<= bite;
  1667. for (i=0; i < bite; i+=8)
  1668. bitbuf |= (unsigned) (fgetc(ifp) << i);
  1669. }
  1670. val = bitbuf << (64-tiff_bps-vbits) >> (64-tiff_bps);
  1671. RAW(row,col ^ (load_flags >> 6)) = val;
  1672. if (load_flags & 1 && (col % 10) == 9 &&
  1673. fgetc(ifp) && col < width+left_margin) derror();
  1674. }
  1675. vbits -= rbits;
  1676. }
  1677. }
  1678. void CLASS nokia_load_raw()
  1679. {
  1680. uchar *data, *dp;
  1681. int rev, dwide, row, col, c;
  1682. rev = 3 * (order == 0x4949);
  1683. dwide = (raw_width * 5 +1) / 4;
  1684. data = (uchar *) malloc (dwide*2);
  1685. merror (data, "nokia_load_raw()");
  1686. for (row=0; row < raw_height; row++) {
  1687. if (fread (data+dwide, 1, dwide, ifp) < dwide) derror();
  1688. FORC(dwide) data[c] = data[dwide+(c ^ rev)];
  1689. for (dp=data, col=0; col < raw_width; dp+=5, col+=4)
  1690. FORC4 RAW(row,col+c) = (dp[c] << 2) | (dp[4] >> (c << 1) & 3);
  1691. }
  1692. free (data);
  1693. maximum = 0x3ff;
  1694. }
  1695. unsigned CLASS pana_bits (int nbits)
  1696. {
  1697. #ifndef LIBRAW_NOTHREADS
  1698. #define buf tls->pana_bits.buf
  1699. #define vbits tls->pana_bits.vbits
  1700. #else
  1701. static uchar buf[0x4000];
  1702. static int vbits;
  1703. #endif
  1704. int byte;
  1705. if (!nbits) return vbits=0;
  1706. if (!vbits) {
  1707. fread (buf+load_flags, 1, 0x4000-load_flags, ifp);
  1708. fread (buf, 1, load_flags, ifp);
  1709. }
  1710. vbits = (vbits - nbits) & 0x1ffff;
  1711. byte = vbits >> 3 ^ 0x3ff0;
  1712. return (buf[byte] | buf[byte+1] << 8) >> (vbits & 7) & ~(-1 << nbits);
  1713. #ifndef LIBRAW_NOTHREADS
  1714. #undef buf
  1715. #undef vbits
  1716. #endif
  1717. }
  1718. void CLASS panasonic_load_raw()
  1719. {
  1720. int row, col, i, j, sh=0, pred[2], nonz[2];
  1721. pana_bits(0);
  1722. for (row=0; row < height; row++)
  1723. for (col=0; col < raw_width; col++) {
  1724. if ((i = col % 14) == 0)
  1725. pred[0] = pred[1] = nonz[0] = nonz[1] = 0;
  1726. if (i % 3 == 2) sh = 4 >> (3 - pana_bits(2));
  1727. if (nonz[i & 1]) {
  1728. if ((j = pana_bits(8))) {
  1729. if ((pred[i & 1] -= 0x80 << sh) < 0 || sh == 4)
  1730. pred[i & 1] &= ~(-1 << sh);
  1731. pred[i & 1] += j << sh;
  1732. }
  1733. } else if ((nonz[i & 1] = pana_bits(8)) || i > 11)
  1734. pred[i & 1] = nonz[i & 1] << 4 | pana_bits(4);
  1735. if ((RAW(row,col) = pred[col & 1]) > 4098 && col < width) derror();
  1736. }
  1737. }
  1738. void CLASS olympus_load_raw()
  1739. {
  1740. ushort huff[4096];
  1741. int row, col, nbits, sign, low, high, i, c, w, n, nw;
  1742. int acarry[2][3], *carry, pred, diff;
  1743. huff[n=0] = 0xc0c;
  1744. for (i=12; i--; )
  1745. FORC(2048 >> i) huff[++n] = (i+1) << 8 | i;
  1746. fseek (ifp, 7, SEEK_CUR);
  1747. #ifdef LIBRAW_LIBRARY_BUILD
  1748. if(!data_size)
  1749. throw LIBRAW_EXCEPTION_IO_BADFILE;
  1750. LibRaw_byte_buffer *buf = ifp->make_byte_buffer(data_size);
  1751. LibRaw_bit_buffer bits;
  1752. bits.reset();
  1753. #else
  1754. getbits(-1);
  1755. #endif
  1756. for (row=0; row < height; row++) {
  1757. memset (acarry, 0, sizeof acarry);
  1758. for (col=0; col < raw_width; col++) {
  1759. carry = acarry[col & 1];
  1760. i = 2 * (carry[2] < 3);
  1761. for (nbits=2+i; (ushort) carry[0] >> (nbits+i); nbits++);
  1762. #ifdef LIBRAW_LIBRARY_BUILD
  1763. low = (sign = bits._getbits(buf,3,zero_after_ff)) & 3;
  1764. sign = sign << 29 >> 31;
  1765. if ((high = bits._gethuff(buf,12,huff,zero_after_ff)) == 12)
  1766. high = bits._getbits(buf,16-nbits,zero_after_ff) >> 1;
  1767. carry[0] = (high << nbits) | bits._getbits(buf,nbits,zero_after_ff);
  1768. #else
  1769. low = (sign = getbits(3)) & 3;
  1770. sign = sign << 29 >> 31;
  1771. if ((high = getbithuff(12,huff)) == 12)
  1772. high = getbits(16-nbits) >> 1;
  1773. carry[0] = (high << nbits) | getbits(nbits);
  1774. #endif
  1775. diff = (carry[0] ^ sign) + carry[1];
  1776. carry[1] = (diff*3 + carry[1]) >> 5;
  1777. carry[2] = carry[0] > 16 ? 0 : carry[2]+1;
  1778. if (col >= width) continue;
  1779. if (row < 2 && col < 2) pred = 0;
  1780. else if (row < 2) pred = RAW(row,col-2);
  1781. else if (col < 2) pred = RAW(row-2,col);
  1782. else {
  1783. w = RAW(row,col-2);
  1784. n = RAW(row-2,col);
  1785. nw = RAW(row-2,col-2);
  1786. if ((w < nw && nw < n) || (n < nw && nw < w)) {
  1787. if (ABS(w-nw) > 32 || ABS(n-nw) > 32)
  1788. pred = w + n - nw;
  1789. else pred = (w + n) >> 1;
  1790. } else pred = ABS(w-nw) > ABS(n-nw) ? w : n;
  1791. }
  1792. if ((RAW(row,col) = pred + ((diff << 2) | low)) >> 12) derror();
  1793. }
  1794. }
  1795. #ifdef LIBRAW_LIBRARY_BUILD
  1796. delete buf;
  1797. #endif
  1798. }
  1799. void CLASS minolta_rd175_load_raw()
  1800. {
  1801. uchar pixel[768];
  1802. unsigned irow, box, row, col;
  1803. for (irow=0; irow < 1481; irow++) {
  1804. if (fread (pixel, 1, 768, ifp) < 768) derror();
  1805. box = irow / 82;
  1806. row = irow % 82 * 12 + ((box < 12) ? box | 1 : (box-12)*2);
  1807. switch (irow) {
  1808. case 1477: case 1479: continue;
  1809. case 1476: row = 984; break;
  1810. case 1480: row = 985; break;
  1811. case 1478: row = 985; box = 1;
  1812. }
  1813. if ((box < 12) && (box & 1)) {
  1814. for (col=0; col < 1533; col++, row ^= 1)
  1815. if (col != 1) RAW(row,col) = (col+1) & 2 ?
  1816. pixel[col/2-1] + pixel[col/2+1] : pixel[col/2] << 1;
  1817. RAW(row,1) = pixel[1] << 1;
  1818. RAW(row,1533) = pixel[765] << 1;
  1819. } else
  1820. for (col=row & 1; col < 1534; col+=2)
  1821. RAW(row,col) = pixel[col/2] << 1;
  1822. }
  1823. maximum = 0xff << 1;
  1824. }
  1825. void CLASS quicktake_100_load_raw()
  1826. {
  1827. uchar pixel[484][644];
  1828. static const short gstep[16] =
  1829. { -89,-60,-44,-32,-22,-15,-8,-2,2,8,15,22,32,44,60,89 };
  1830. static const short rstep[6][4] =
  1831. { { -3,-1,1,3 }, { -5,-1,1,5 }, { -8,-2,2,8 },
  1832. { -13,-3,3,13 }, { -19,-4,4,19 }, { -28,-6,6,28 } };
  1833. static const short t_curve[256] =
  1834. { 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,
  1835. 28,29,30,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,53,
  1836. 54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,74,75,76,77,78,
  1837. 79,80,81,82,83,84,86,88,90,92,94,97,99,101,103,105,107,110,112,114,116,
  1838. 118,120,123,125,127,129,131,134,136,138,140,142,144,147,149,151,153,155,
  1839. 158,160,162,164,166,168,171,173,175,177,179,181,184,186,188,190,192,195,
  1840. 197,199,201,203,205,208,210,212,214,216,218,221,223,226,230,235,239,244,
  1841. 248,252,257,261,265,270,274,278,283,287,291,296,300,305,309,313,318,322,
  1842. 326,331,335,339,344,348,352,357,361,365,370,374,379,383,387,392,396,400,
  1843. 405,409,413,418,422,426,431,435,440,444,448,453,457,461,466,470,474,479,
  1844. 483,487,492,496,500,508,519,531,542,553,564,575,587,598,609,620,631,643,
  1845. 654,665,676,687,698,710,721,732,743,754,766,777,788,799,810,822,833,844,
  1846. 855,866,878,889,900,911,922,933,945,956,967,978,989,1001,1012,1023 };
  1847. int rb, row, col, sharp, val=0;
  1848. getbits(-1);
  1849. memset (pixel, 0x80, sizeof pixel);
  1850. for (row=2; row < height+2; row++) {
  1851. for (col=2+(row & 1); col < width+2; col+=2) {
  1852. val = ((pixel[row-1][col-1] + 2*pixel[row-1][col+1] +
  1853. pixel[row][col-2]) >> 2) + gstep[getbits(4)];
  1854. pixel[row][col] = val = LIM(val,0,255);
  1855. if (col < 4)
  1856. pixel[row][col-2] = pixel[row+1][~row & 1] = val;
  1857. if (row == 2)
  1858. pixel[row-1][col+1] = pixel[row-1][col+3] = val;
  1859. }
  1860. pixel[row][col] = val;
  1861. }
  1862. for (rb=0; rb < 2; rb++)
  1863. for (row=2+rb; row < height+2; row+=2)
  1864. for (col=3-(row & 1); col < width+2; col+=2) {
  1865. if (row < 4 || col < 4) sharp = 2;
  1866. else {
  1867. val = ABS(pixel[row-2][col] - pixel[row][col-2])
  1868. + ABS(pixel[row-2][col] - pixel[row-2][col-2])
  1869. + ABS(pixel[row][col-2] - pixel[row-2][col-2]);
  1870. sharp = val < 4 ? 0 : val < 8 ? 1 : val < 16 ? 2 :
  1871. val < 32 ? 3 : val < 48 ? 4 : 5;
  1872. }
  1873. val = ((pixel[row-2][col] + pixel[row][col-2]) >> 1)
  1874. + rstep[sharp][getbits(2)];
  1875. pixel[row][col] = val = LIM(val,0,255);
  1876. if (row < 4) pixel[row-2][col+2] = val;
  1877. if (col < 4) pixel[row+2][col-2] = val;
  1878. }
  1879. for (row=2; row < height+2; row++)
  1880. for (col=3-(row & 1); col < width+2; col+=2) {
  1881. val = ((pixel[row][col-1] + (pixel[row][col] << 2) +
  1882. pixel[row][col+1]) >> 1) - 0x100;
  1883. pixel[row][col] = LIM(val,0,255);
  1884. }
  1885. for (row=0; row < height; row++)
  1886. for (col=0; col < width; col++)
  1887. RAW(row,col) = t_curve[pixel[row+2][col+2]];
  1888. maximum = 0x3ff;
  1889. }
  1890. #define radc_token(tree) ((signed char) getbithuff(8,huff[tree]))
  1891. #define FORYX for (y=1; y < 3; y++) for (x=col+1; x >= col; x--)
  1892. #define PREDICTOR (c ? (buf[c][y-1][x] + buf[c][y][x+1]) / 2 \
  1893. : (buf[c][y-1][x+1] + 2*buf[c][y-1][x] + buf[c][y][x+1]) / 4)
  1894. #ifdef __GNUC__
  1895. # if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
  1896. # pragma GCC optimize("no-aggressive-loop-optimizations")
  1897. # endif
  1898. #endif
  1899. void CLASS kodak_radc_load_raw()
  1900. {
  1901. static const char src[] = {
  1902. 1,1, 2,3, 3,4, 4,2, 5,7, 6,5, 7,6, 7,8,
  1903. 1,0, 2,1, 3,3, 4,4, 5,2, 6,7, 7,6, 8,5, 8,8,
  1904. 2,1, 2,3, 3,0, 3,2, 3,4, 4,6, 5,5, 6,7, 6,8,
  1905. 2,0, 2,1, 2,3, 3,2, 4,4, 5,6, 6,7, 7,5, 7,8,
  1906. 2,1, 2,4, 3,0, 3,2, 3,3, 4,7, 5,5, 6,6, 6,8,
  1907. 2,3, 3,1, 3,2, 3,4, 3,5, 3,6, 4,7, 5,0, 5,8,
  1908. 2,3, 2,6, 3,0, 3,1, 4,4, 4,5, 4,7, 5,2, 5,8,
  1909. 2,4, 2,7, 3,3, 3,6, 4,1, 4,2, 4,5, 5,0, 5,8,
  1910. 2,6, 3,1, 3,3, 3,5, 3,7, 3,8, 4,0, 5,2, 5,4,
  1911. 2,0, 2,1, 3,2, 3,3, 4,4, 4,5, 5,6, 5,7, 4,8,
  1912. 1,0, 2,2, 2,-2,
  1913. 1,-3, 1,3,
  1914. 2,-17, 2,-5, 2,5, 2,17,
  1915. 2,-7, 2,2, 2,9, 2,18,
  1916. 2,-18, 2,-9, 2,-2, 2,7,
  1917. 2,-28, 2,28, 3,-49, 3,-9, 3,9, 4,49, 5,-79, 5,79,
  1918. 2,-1, 2,13, 2,26, 3,39, 4,-16, 5,55, 6,-37, 6,76,
  1919. 2,-26, 2,-13, 2,1, 3,-39, 4,16, 5,-55, 6,-76, 6,37
  1920. };
  1921. ushort huff[19][256];
  1922. int row, col, tree, nreps, rep, step, i, c, s, r, x, y, val;
  1923. short last[3] = { 16,16,16 }, mul[3], buf[3][3][386];
  1924. static const ushort pt[] =
  1925. { 0,0, 1280,1344, 2320,3616, 3328,8000, 4095,16383, 65535,16383 };
  1926. for (i=2; i < 12; i+=2)
  1927. for (c=pt[i-2]; c <= pt[i]; c++)
  1928. curve[c] = (float)
  1929. (c-pt[i-2]) / (pt[i]-pt[i-2]) * (pt[i+1]-pt[i-1]) + pt[i-1] + 0.5;
  1930. for (s=i=0; i < sizeof src; i+=2)
  1931. FORC(256 >> src[i])
  1932. huff[0][s++] = src[i] << 8 | (uchar) src[i+1];
  1933. s = kodak_cbpp == 243 ? 2 : 3;
  1934. FORC(256) huff[18][c] = (8-s) << 8 | c >> s << s | 1 << (s-1);
  1935. getbits(-1);
  1936. for (i=0; i < sizeof(buf)/sizeof(short); i++)
  1937. buf[0][0][i] = 2048;
  1938. for (row=0; row < height; row+=4) {
  1939. FORC3 mul[c] = getbits(6);
  1940. FORC3 {
  1941. val = ((0x1000000/last[c] + 0x7ff) >> 12) * mul[c];
  1942. s = val > 65564 ? 10:12;
  1943. x = ~(-1 << (s-1));
  1944. val <<= 12-s;
  1945. for (i=0; i < sizeof(buf[0])/sizeof(short); i++)
  1946. buf[c][0][i] = (buf[c][0][i] * val + x) >> s;
  1947. last[c] = mul[c];
  1948. for (r=0; r <= !c; r++) {
  1949. buf[c][1][width/2] = buf[c][2][width/2] = mul[c] << 7;
  1950. for (tree=1, col=width/2; col > 0; ) {
  1951. if ((tree = radc_token(tree))) {
  1952. col -= 2;
  1953. if (tree == 8)
  1954. FORYX buf[c][y][x] = (uchar) radc_token(18) * mul[c];
  1955. else
  1956. FORYX buf[c][y][x] = radc_token(tree+10) * 16 + PREDICTOR;
  1957. } else
  1958. do {
  1959. nreps = (col > 2) ? radc_token(9) + 1 : 1;
  1960. for (rep=0; rep < 8 && rep < nreps && col > 0; rep++) {
  1961. col -= 2;
  1962. FORYX buf[c][y][x] = PREDICTOR;
  1963. if (rep & 1) {
  1964. step = radc_token(10) << 4;
  1965. FORYX buf[c][y][x] += step;
  1966. }
  1967. }
  1968. } while (nreps == 9);
  1969. }
  1970. for (y=0; y < 2; y++)
  1971. for (x=0; x < width/2; x++) {
  1972. val = (buf[c][y+1][x] << 4) / mul[c];
  1973. if (val < 0) val = 0;
  1974. if (c) RAW(row+y*2+c-1,x*2+2-c) = val;
  1975. else RAW(row+r*2+y,x*2+y) = val;
  1976. }
  1977. memcpy (buf[c][0]+!c, buf[c][2], sizeof buf[c][0]-2*!c);
  1978. }
  1979. }
  1980. for (y=row; y < row+4; y++)
  1981. for (x=0; x < width; x++)
  1982. if ((x+y) & 1) {
  1983. r = x ? x-1 : x+1;
  1984. s = x+1 < width ? x+1 : x-1;
  1985. val = (RAW(y,x)-2048)*2 + (RAW(y,r)+RAW(y,s))/2;
  1986. if (val < 0) val = 0;
  1987. RAW(y,x) = val;
  1988. }
  1989. }
  1990. for (i=0; i < height*width; i++)
  1991. raw_image[i] = curve[raw_image[i]];
  1992. maximum = 0x3fff;
  1993. }
  1994. #undef FORYX
  1995. #undef PREDICTOR
  1996. #ifdef NO_JPEG
  1997. void CLASS kodak_jpeg_load_raw() {}
  1998. void CLASS lossy_dng_load_raw() {}
  1999. #else
  2000. #ifdef LIBRAW_LIBRARY_BUILD
  2001. void CLASS kodak_jpeg_load_raw() {}
  2002. #else
  2003. METHODDEF(boolean)
  2004. fill_input_buffer (j_decompress_ptr cinfo)
  2005. {
  2006. #ifndef LIBRAW_NOTHREADS
  2007. #define jpeg_buffer tls->jpeg_buffer
  2008. #else
  2009. static uchar jpeg_buffer[4096];
  2010. #endif
  2011. size_t nbytes;
  2012. nbytes = fread (jpeg_buffer, 1, 4096, ifp);
  2013. swab (jpeg_buffer, jpeg_buffer, nbytes);
  2014. cinfo->src->next_input_byte = jpeg_buffer;
  2015. cinfo->src->bytes_in_buffer = nbytes;
  2016. return TRUE;
  2017. #ifndef LIBRAW_NOTHREADS
  2018. #undef jpeg_buffer
  2019. #endif
  2020. }
  2021. void CLASS kodak_jpeg_load_raw()
  2022. {
  2023. struct jpeg_decompress_struct cinfo;
  2024. struct jpeg_error_mgr jerr;
  2025. JSAMPARRAY buf;
  2026. JSAMPLE (*pixel)[3];
  2027. int row, col;
  2028. cinfo.err = jpeg_std_error (&jerr);
  2029. jpeg_create_decompress (&cinfo);
  2030. jpeg_stdio_src (&cinfo, ifp);
  2031. cinfo.src->fill_input_buffer = fill_input_buffer;
  2032. jpeg_read_header (&cinfo, TRUE);
  2033. jpeg_start_decompress (&cinfo);
  2034. if ((cinfo.output_width != width ) ||
  2035. (cinfo.output_height*2 != height ) ||
  2036. (cinfo.output_components != 3 )) {
  2037. #ifdef DCRAW_VERBOSE
  2038. fprintf (stderr,_("%s: incorrect JPEG dimensions\n"), ifname);
  2039. #endif
  2040. jpeg_destroy_decompress (&cinfo);
  2041. #ifdef LIBRAW_LIBRARY_BUILD
  2042. throw LIBRAW_EXCEPTION_DECODE_JPEG;
  2043. #else
  2044. longjmp (failure, 3);
  2045. #endif
  2046. }
  2047. buf = (*cinfo.mem->alloc_sarray)
  2048. ((j_common_ptr) &cinfo, JPOOL_IMAGE, width*3, 1);
  2049. while (cinfo.output_scanline < cinfo.output_height) {
  2050. row = cinfo.output_scanline * 2;
  2051. jpeg_read_scanlines (&cinfo, buf, 1);
  2052. pixel = (JSAMPLE (*)[3]) buf[0];
  2053. for (col=0; col < width; col+=2) {
  2054. RAW(row+0,col+0) = pixel[col+0][1] << 1;
  2055. RAW(row+1,col+1) = pixel[col+1][1] << 1;
  2056. RAW(row+0,col+1) = pixel[col][0] + pixel[col+1][0];
  2057. RAW(row+1,col+0) = pixel[col][2] + pixel[col+1][2];
  2058. }
  2059. }
  2060. jpeg_finish_decompress (&cinfo);
  2061. jpeg_destroy_decompress (&cinfo);
  2062. maximum = 0xff << 1;
  2063. }
  2064. #endif
  2065. void CLASS lossy_dng_load_raw()
  2066. {
  2067. struct jpeg_decompress_struct cinfo;
  2068. struct jpeg_error_mgr jerr;
  2069. JSAMPARRAY buf;
  2070. JSAMPLE (*pixel)[3];
  2071. unsigned sorder=order, ntags, opcode, deg, i, j, c;
  2072. unsigned save=data_offset-4, trow=0, tcol=0, row, col;
  2073. ushort t_curve[3][256];
  2074. double coeff[9], tot;
  2075. fseek (ifp, meta_offset, SEEK_SET);
  2076. order = 0x4d4d;
  2077. ntags = get4();
  2078. while (ntags--) {
  2079. opcode = get4(); get4(); get4();
  2080. if (opcode != 8)
  2081. { fseek (ifp, get4(), SEEK_CUR); continue; }
  2082. fseek (ifp, 20, SEEK_CUR);
  2083. if ((c = get4()) > 2) break;
  2084. fseek (ifp, 12, SEEK_CUR);
  2085. if ((deg = get4()) > 8) break;
  2086. for (i=0; i <= deg && i < 9; i++)
  2087. coeff[i] = getreal(12);
  2088. for (i=0; i < 256; i++) {
  2089. for (tot=j=0; j <= deg; j++)
  2090. tot += coeff[j] * pow(i/255.0, (int)j);
  2091. t_curve[c][i] = tot*0xffff;
  2092. }
  2093. }
  2094. order = sorder;
  2095. cinfo.err = jpeg_std_error (&jerr);
  2096. jpeg_create_decompress (&cinfo);
  2097. while (trow < raw_height) {
  2098. fseek (ifp, save+=4, SEEK_SET);
  2099. if (tile_length < INT_MAX)
  2100. fseek (ifp, get4(), SEEK_SET);
  2101. #ifdef LIBRAW_LIBRARY_BUILD
  2102. if(libraw_internal_data.internal_data.input->jpeg_src(&cinfo) == -1)
  2103. {
  2104. jpeg_destroy_decompress(&cinfo);
  2105. throw LIBRAW_EXCEPTION_DECODE_JPEG;
  2106. }
  2107. #else
  2108. jpeg_stdio_src (&cinfo, ifp);
  2109. #endif
  2110. jpeg_read_header (&cinfo, TRUE);
  2111. jpeg_start_decompress (&cinfo);
  2112. buf = (*cinfo.mem->alloc_sarray)
  2113. ((j_common_ptr) &cinfo, JPOOL_IMAGE, cinfo.output_width*3, 1);
  2114. while (cinfo.output_scanline < cinfo.output_height &&
  2115. (row = trow + cinfo.output_scanline) < height) {
  2116. jpeg_read_scanlines (&cinfo, buf, 1);
  2117. pixel = (JSAMPLE (*)[3]) buf[0];
  2118. for (col=0; col < cinfo.output_width && tcol+col < width; col++) {
  2119. FORC3 image[row*width+tcol+col][c] = t_curve[c][pixel[col][c]];
  2120. }
  2121. }
  2122. jpeg_abort_decompress (&cinfo);
  2123. if ((tcol += tile_width) >= raw_width)
  2124. trow += tile_length + (tcol = 0);
  2125. }
  2126. jpeg_destroy_decompress (&cinfo);
  2127. maximum = 0xffff;
  2128. }
  2129. #endif
  2130. void CLASS kodak_dc120_load_raw()
  2131. {
  2132. static const int mul[4] = { 162, 192, 187, 92 };
  2133. static const int add[4] = { 0, 636, 424, 212 };
  2134. uchar pixel[848];
  2135. int row, shift, col;
  2136. for (row=0; row < height; row++) {
  2137. if (fread (pixel, 1, 848, ifp) < 848) derror();
  2138. shift = row * mul[row & 3] + add[row & 3];
  2139. for (col=0; col < width; col++)
  2140. RAW(row,col) = (ushort) pixel[(col + shift) % 848];
  2141. }
  2142. maximum = 0xff;
  2143. }
  2144. void CLASS eight_bit_load_raw()
  2145. {
  2146. uchar *pixel;
  2147. unsigned row, col;
  2148. pixel = (uchar *) calloc (raw_width, sizeof *pixel);
  2149. merror (pixel, "eight_bit_load_raw()");
  2150. for (row=0; row < raw_height; row++) {
  2151. if (fread (pixel, 1, raw_width, ifp) < raw_width) derror();
  2152. for (col=0; col < raw_width; col++)
  2153. RAW(row,col) = curve[pixel[col]];
  2154. }
  2155. free (pixel);
  2156. maximum = curve[0xff];
  2157. }
  2158. void CLASS kodak_yrgb_load_raw()
  2159. {
  2160. uchar *pixel;
  2161. int row, col, y, cb, cr, rgb[3], c;
  2162. pixel = (uchar *) calloc (raw_width, 3*sizeof *pixel);
  2163. merror (pixel, "kodak_yrgb_load_raw()");
  2164. for (row=0; row < height; row++) {
  2165. if (~row & 1)
  2166. if (fread (pixel, raw_width, 3, ifp) < 3) derror();
  2167. for (col=0; col < raw_width; col++) {
  2168. y = pixel[width*2*(row & 1) + col];
  2169. cb = pixel[width + (col & -2)] - 128;
  2170. cr = pixel[width + (col & -2)+1] - 128;
  2171. rgb[1] = y-((cb + cr + 2) >> 2);
  2172. rgb[2] = rgb[1] + cb;
  2173. rgb[0] = rgb[1] + cr;
  2174. FORC3 image[row*width+col][c] = curve[LIM(rgb[c],0,255)];
  2175. }
  2176. }
  2177. free (pixel);
  2178. maximum = curve[0xff];
  2179. }
  2180. void CLASS kodak_262_load_raw()
  2181. {
  2182. static const uchar kodak_tree[2][26] =
  2183. { { 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 },
  2184. { 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 } };
  2185. ushort *huff[2];
  2186. uchar *pixel;
  2187. int *strip, ns, c, row, col, chess, pi=0, pi1, pi2, pred, val;
  2188. FORC(2) huff[c] = make_decoder (kodak_tree[c]);
  2189. ns = (raw_height+63) >> 5;
  2190. pixel = (uchar *) malloc (raw_width*32 + ns*4);
  2191. merror (pixel, "kodak_262_load_raw()");
  2192. strip = (int *) (pixel + raw_width*32);
  2193. order = 0x4d4d;
  2194. FORC(ns) strip[c] = get4();
  2195. for (row=0; row < raw_height; row++) {
  2196. if ((row & 31) == 0) {
  2197. fseek (ifp, strip[row >> 5], SEEK_SET);
  2198. getbits(-1);
  2199. pi = 0;
  2200. }
  2201. for (col=0; col < raw_width; col++) {
  2202. chess = (row + col) & 1;
  2203. pi1 = chess ? pi-2 : pi-raw_width-1;
  2204. pi2 = chess ? pi-2*raw_width : pi-raw_width+1;
  2205. if (col <= chess) pi1 = -1;
  2206. if (pi1 < 0) pi1 = pi2;
  2207. if (pi2 < 0) pi2 = pi1;
  2208. if (pi1 < 0 && col > 1) pi1 = pi2 = pi-2;
  2209. pred = (pi1 < 0) ? 0 : (pixel[pi1] + pixel[pi2]) >> 1;
  2210. pixel[pi] = val = pred + ljpeg_diff (huff[chess]);
  2211. if (val >> 8) derror();
  2212. val = curve[pixel[pi++]];
  2213. RAW(row,col) = val;
  2214. }
  2215. }
  2216. free (pixel);
  2217. FORC(2) free (huff[c]);
  2218. }
  2219. int CLASS kodak_65000_decode (short *out, int bsize)
  2220. {
  2221. uchar c, blen[768];
  2222. ushort raw[6];
  2223. INT64 bitbuf=0;
  2224. int save, bits=0, i, j, len, diff;
  2225. save = ftell(ifp);
  2226. bsize = (bsize + 3) & -4;
  2227. for (i=0; i < bsize; i+=2) {
  2228. c = fgetc(ifp);
  2229. if ((blen[i ] = c & 15) > 12 ||
  2230. (blen[i+1] = c >> 4) > 12 ) {
  2231. fseek (ifp, save, SEEK_SET);
  2232. for (i=0; i < bsize; i+=8) {
  2233. read_shorts (raw, 6);
  2234. out[i ] = raw[0] >> 12 << 8 | raw[2] >> 12 << 4 | raw[4] >> 12;
  2235. out[i+1] = raw[1] >> 12 << 8 | raw[3] >> 12 << 4 | raw[5] >> 12;
  2236. for (j=0; j < 6; j++)
  2237. out[i+2+j] = raw[j] & 0xfff;
  2238. }
  2239. return 1;
  2240. }
  2241. }
  2242. if ((bsize & 7) == 4) {
  2243. bitbuf = fgetc(ifp) << 8;
  2244. bitbuf += fgetc(ifp);
  2245. bits = 16;
  2246. }
  2247. for (i=0; i < bsize; i++) {
  2248. len = blen[i];
  2249. if (bits < len) {
  2250. for (j=0; j < 32; j+=8)
  2251. bitbuf += (INT64) fgetc(ifp) << (bits+(j^8));
  2252. bits += 32;
  2253. }
  2254. diff = bitbuf & (0xffff >> (16-len));
  2255. bitbuf >>= len;
  2256. bits -= len;
  2257. if ((diff & (1 << (len-1))) == 0)
  2258. diff -= (1 << len) - 1;
  2259. out[i] = diff;
  2260. }
  2261. return 0;
  2262. }
  2263. void CLASS kodak_65000_load_raw()
  2264. {
  2265. short buf[256];
  2266. int row, col, len, pred[2], ret, i;
  2267. for (row=0; row < height; row++)
  2268. for (col=0; col < width; col+=256) {
  2269. pred[0] = pred[1] = 0;
  2270. len = MIN (256, width-col);
  2271. ret = kodak_65000_decode (buf, len);
  2272. for (i=0; i < len; i++)
  2273. if ((RAW(row,col+i) = curve[ret ? buf[i] :
  2274. (pred[i & 1] += buf[i])]) >> 12) derror();
  2275. }
  2276. }
  2277. void CLASS kodak_ycbcr_load_raw()
  2278. {
  2279. short buf[384], *bp;
  2280. int row, col, len, c, i, j, k, y[2][2], cb, cr, rgb[3];
  2281. ushort *ip;
  2282. for (row=0; row < height; row+=2)
  2283. for (col=0; col < width; col+=128) {
  2284. len = MIN (128, width-col);
  2285. kodak_65000_decode (buf, len*3);
  2286. y[0][1] = y[1][1] = cb = cr = 0;
  2287. for (bp=buf, i=0; i < len; i+=2, bp+=2) {
  2288. cb += bp[4];
  2289. cr += bp[5];
  2290. rgb[1] = -((cb + cr + 2) >> 2);
  2291. rgb[2] = rgb[1] + cb;
  2292. rgb[0] = rgb[1] + cr;
  2293. for (j=0; j < 2; j++)
  2294. for (k=0; k < 2; k++) {
  2295. if ((y[j][k] = y[j][k^1] + *bp++) >> 10) derror();
  2296. ip = image[(row+j)*width + col+i+k];
  2297. FORC3 ip[c] = curve[LIM(y[j][k]+rgb[c], 0, 0xfff)];
  2298. }
  2299. }
  2300. }
  2301. }
  2302. void CLASS kodak_rgb_load_raw()
  2303. {
  2304. short buf[768], *bp;
  2305. int row, col, len, c, i, rgb[3];
  2306. ushort *ip=image[0];
  2307. #ifndef LIBRAW_LIBRARY_BUILD
  2308. if (raw_image) free (raw_image);
  2309. raw_image = 0;
  2310. #endif
  2311. for (row=0; row < height; row++)
  2312. for (col=0; col < width; col+=256) {
  2313. len = MIN (256, width-col);
  2314. kodak_65000_decode (buf, len*3);
  2315. memset (rgb, 0, sizeof rgb);
  2316. for (bp=buf, i=0; i < len; i++, ip+=4)
  2317. FORC3 if ((ip[c] = rgb[c] += *bp++) >> 12) derror();
  2318. }
  2319. }
  2320. void CLASS kodak_thumb_load_raw()
  2321. {
  2322. int row, col;
  2323. colors = thumb_misc >> 5;
  2324. for (row=0; row < height; row++)
  2325. for (col=0; col < width; col++)
  2326. read_shorts (image[row*width+col], colors);
  2327. maximum = (1 << (thumb_misc & 31)) - 1;
  2328. }
  2329. void CLASS sony_decrypt (unsigned *data, int len, int start, int key)
  2330. {
  2331. #ifndef LIBRAW_NOTHREADS
  2332. #define pad tls->sony_decrypt.pad
  2333. #define p tls->sony_decrypt.p
  2334. #else
  2335. static unsigned pad[128], p;
  2336. #endif
  2337. if (start) {
  2338. for (p=0; p < 4; p++)
  2339. pad[p] = key = key * 48828125 + 1;
  2340. pad[3] = pad[3] << 1 | (pad[0]^pad[2]) >> 31;
  2341. for (p=4; p < 127; p++)
  2342. pad[p] = (pad[p-4]^pad[p-2]) << 1 | (pad[p-3]^pad[p-1]) >> 31;
  2343. for (p=0; p < 127; p++)
  2344. pad[p] = htonl(pad[p]);
  2345. }
  2346. #if 1 // Avoid gcc 4.8 bug
  2347. while (len--)
  2348. {
  2349. *data++ ^= pad[p & 127] = pad[(p+1) & 127] ^ pad[(p+65) & 127];
  2350. p++;
  2351. }
  2352. #else
  2353. while (len--)
  2354. *data++ ^= pad[p++ & 127] = pad[(p+1) & 127] ^ pad[(p+65) & 127];
  2355. #endif
  2356. #ifndef LIBRAW_NOTHREADS
  2357. #undef pad
  2358. #undef p
  2359. #endif
  2360. }
  2361. void CLASS sony_load_raw()
  2362. {
  2363. uchar head[40];
  2364. ushort *pixel;
  2365. unsigned i, key, row, col;
  2366. fseek (ifp, 200896, SEEK_SET);
  2367. fseek (ifp, (unsigned) fgetc(ifp)*4 - 1, SEEK_CUR);
  2368. order = 0x4d4d;
  2369. key = get4();
  2370. fseek (ifp, 164600, SEEK_SET);
  2371. fread (head, 1, 40, ifp);
  2372. sony_decrypt ((unsigned int *) head, 10, 1, key);
  2373. for (i=26; i-- > 22; )
  2374. key = key << 8 | head[i];
  2375. fseek (ifp, data_offset, SEEK_SET);
  2376. for (row=0; row < raw_height; row++) {
  2377. pixel = raw_image + row*raw_width;
  2378. if (fread (pixel, 2, raw_width, ifp) < raw_width) derror();
  2379. sony_decrypt ((unsigned int *) pixel, raw_width/2, !row, key);
  2380. for (col=0; col < raw_width; col++)
  2381. if ((pixel[col] = ntohs(pixel[col])) >> 14) derror();
  2382. }
  2383. maximum = 0x3ff0;
  2384. }
  2385. void CLASS sony_arw_load_raw()
  2386. {
  2387. ushort huff[32768];
  2388. static const ushort tab[18] =
  2389. { 0xf11,0xf10,0xe0f,0xd0e,0xc0d,0xb0c,0xa0b,0x90a,0x809,
  2390. 0x708,0x607,0x506,0x405,0x304,0x303,0x300,0x202,0x201 };
  2391. int i, c, n, col, row, len, diff, sum=0;
  2392. for (n=i=0; i < 18; i++)
  2393. FORC(32768 >> (tab[i] >> 8)) huff[n++] = tab[i];
  2394. #ifdef LIBRAW_LIBRARY_BUILD
  2395. LibRaw_byte_buffer *buf=NULL;
  2396. if(data_size)
  2397. buf = ifp->make_byte_buffer(data_size);
  2398. else
  2399. getbits(-1);
  2400. LibRaw_bit_buffer bits;
  2401. bits.reset();
  2402. #else
  2403. getbits(-1);
  2404. #endif
  2405. for (col = raw_width; col--; )
  2406. for (row=0; row < raw_height+1; row+=2) {
  2407. if (row == raw_height) row = 1;
  2408. #ifdef LIBRAW_LIBRARY_BUILD
  2409. if(data_size)
  2410. {
  2411. len = bits._gethuff(buf,15,huff,zero_after_ff);
  2412. diff = bits._getbits(buf,len,zero_after_ff);
  2413. }
  2414. else
  2415. {
  2416. len = getbithuff(15,huff);
  2417. diff = getbits(len);
  2418. }
  2419. #else
  2420. len = getbithuff(15,huff);
  2421. diff = getbits(len);
  2422. #endif
  2423. if ((diff & (1 << (len-1))) == 0)
  2424. diff -= (1 << len) - 1;
  2425. if ((sum += diff) >> 12) derror();
  2426. if (row < height) RAW(row,col) = sum;
  2427. }
  2428. #ifdef LIBRAW_LIBRARY_BUILD
  2429. if(buf) delete buf;
  2430. #endif
  2431. }
  2432. void CLASS sony_arw2_load_raw()
  2433. {
  2434. uchar *data, *dp;
  2435. ushort pix[16];
  2436. int row, col, val, max, min, imax, imin, sh, bit, i;
  2437. data = (uchar *) malloc (raw_width);
  2438. merror (data, "sony_arw2_load_raw()");
  2439. for (row=0; row < height; row++) {
  2440. fread (data, 1, raw_width, ifp);
  2441. for (dp=data, col=0; col < raw_width-30; dp+=16) {
  2442. max = 0x7ff & (val = sget4(dp));
  2443. min = 0x7ff & val >> 11;
  2444. imax = 0x0f & val >> 22;
  2445. imin = 0x0f & val >> 26;
  2446. for (sh=0; sh < 4 && 0x80 << sh <= max-min; sh++);
  2447. for (bit=30, i=0; i < 16; i++)
  2448. if (i == imax) pix[i] = max;
  2449. else if (i == imin) pix[i] = min;
  2450. else {
  2451. pix[i] = ((sget2(dp+(bit >> 3)) >> (bit & 7) & 0x7f) << sh) + min;
  2452. if (pix[i] > 0x7ff) pix[i] = 0x7ff;
  2453. bit += 7;
  2454. }
  2455. #ifdef LIBRAW_LIBRARY_BUILD
  2456. if(imgdata.params.sony_arw2_hack)
  2457. {
  2458. for (i=0; i < 16; i++, col+=2)
  2459. RAW(row,col) = curve[pix[i] << 1];
  2460. }
  2461. else
  2462. {
  2463. for (i=0; i < 16; i++, col+=2)
  2464. RAW(row,col) = curve[pix[i] << 1] >> 2;
  2465. }
  2466. #else
  2467. for (i=0; i < 16; i++, col+=2)
  2468. RAW(row,col) = curve[pix[i] << 1] >> 2;
  2469. #endif
  2470. col -= col & 1 ? 1:31;
  2471. }
  2472. }
  2473. free (data);
  2474. #ifdef LIBRAW_LIBRARY_BUILD
  2475. if(imgdata.params.sony_arw2_hack)
  2476. {
  2477. black <<= 2;
  2478. maximum <<=2;
  2479. }
  2480. #endif
  2481. }
  2482. #define HOLE(row) ((holes >> (((row) - raw_height) & 7)) & 1)
  2483. /* Kudos to Rich Taylor for figuring out SMaL's compression algorithm. */
  2484. void CLASS smal_decode_segment (unsigned seg[2][2], int holes)
  2485. {
  2486. uchar hist[3][13] = {
  2487. { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
  2488. { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
  2489. { 3, 3, 0, 0, 63, 47, 31, 15, 0 } };
  2490. int low, high=0xff, carry=0, nbits=8;
  2491. int pix, s, count, bin, next, i, sym[3];
  2492. uchar diff, pred[]={0,0};
  2493. ushort data=0, range=0;
  2494. fseek (ifp, seg[0][1]+1, SEEK_SET);
  2495. getbits(-1);
  2496. for (pix=seg[0][0]; pix < seg[1][0]; pix++) {
  2497. for (s=0; s < 3; s++) {
  2498. data = data << nbits | getbits(nbits);
  2499. if (carry < 0)
  2500. carry = (nbits += carry+1) < 1 ? nbits-1 : 0;
  2501. while (--nbits >= 0)
  2502. if ((data >> nbits & 0xff) == 0xff) break;
  2503. if (nbits > 0)
  2504. data = ((data & ((1 << (nbits-1)) - 1)) << 1) |
  2505. ((data + (((data & (1 << (nbits-1)))) << 1)) & (-1 << nbits));
  2506. if (nbits >= 0) {
  2507. data += getbits(1);
  2508. carry = nbits - 8;
  2509. }
  2510. count = ((((data-range+1) & 0xffff) << 2) - 1) / (high >> 4);
  2511. for (bin=0; hist[s][bin+5] > count; bin++);
  2512. low = hist[s][bin+5] * (high >> 4) >> 2;
  2513. if (bin) high = hist[s][bin+4] * (high >> 4) >> 2;
  2514. high -= low;
  2515. for (nbits=0; high << nbits < 128; nbits++);
  2516. range = (range+low) << nbits;
  2517. high <<= nbits;
  2518. next = hist[s][1];
  2519. if (++hist[s][2] > hist[s][3]) {
  2520. next = (next+1) & hist[s][0];
  2521. hist[s][3] = (hist[s][next+4] - hist[s][next+5]) >> 2;
  2522. hist[s][2] = 1;
  2523. }
  2524. if (hist[s][hist[s][1]+4] - hist[s][hist[s][1]+5] > 1) {
  2525. if (bin < hist[s][1])
  2526. for (i=bin; i < hist[s][1]; i++) hist[s][i+5]--;
  2527. else if (next <= bin)
  2528. for (i=hist[s][1]; i < bin; i++) hist[s][i+5]++;
  2529. }
  2530. hist[s][1] = next;
  2531. sym[s] = bin;
  2532. }
  2533. diff = sym[2] << 5 | sym[1] << 2 | (sym[0] & 3);
  2534. if (sym[0] & 4)
  2535. diff = diff ? -diff : 0x80;
  2536. if (ftell(ifp) + 12 >= seg[1][1])
  2537. diff = 0;
  2538. raw_image[pix] = pred[pix & 1] += diff;
  2539. if (!(pix & 1) && HOLE(pix / raw_width)) pix += 2;
  2540. }
  2541. maximum = 0xff;
  2542. }
  2543. void CLASS smal_v6_load_raw()
  2544. {
  2545. unsigned seg[2][2];
  2546. fseek (ifp, 16, SEEK_SET);
  2547. seg[0][0] = 0;
  2548. seg[0][1] = get2();
  2549. seg[1][0] = raw_width * raw_height;
  2550. seg[1][1] = INT_MAX;
  2551. smal_decode_segment (seg, 0);
  2552. }
  2553. int CLASS median4 (int *p)
  2554. {
  2555. int min, max, sum, i;
  2556. min = max = sum = p[0];
  2557. for (i=1; i < 4; i++) {
  2558. sum += p[i];
  2559. if (min > p[i]) min = p[i];
  2560. if (max < p[i]) max = p[i];
  2561. }
  2562. return (sum - min - max) >> 1;
  2563. }
  2564. void CLASS fill_holes (int holes)
  2565. {
  2566. int row, col, val[4];
  2567. for (row=2; row < height-2; row++) {
  2568. if (!HOLE(row)) continue;
  2569. for (col=1; col < width-1; col+=4) {
  2570. val[0] = RAW(row-1,col-1);
  2571. val[1] = RAW(row-1,col+1);
  2572. val[2] = RAW(row+1,col-1);
  2573. val[3] = RAW(row+1,col+1);
  2574. RAW(row,col) = median4(val);
  2575. }
  2576. for (col=2; col < width-2; col+=4)
  2577. if (HOLE(row-2) || HOLE(row+2))
  2578. RAW(row,col) = (RAW(row,col-2) + RAW(row,col+2)) >> 1;
  2579. else {
  2580. val[0] = RAW(row,col-2);
  2581. val[1] = RAW(row,col+2);
  2582. val[2] = RAW(row-2,col);
  2583. val[3] = RAW(row+2,col);
  2584. RAW(row,col) = median4(val);
  2585. }
  2586. }
  2587. }
  2588. void CLASS smal_v9_load_raw()
  2589. {
  2590. unsigned seg[256][2], offset, nseg, holes, i;
  2591. fseek (ifp, 67, SEEK_SET);
  2592. offset = get4();
  2593. nseg = fgetc(ifp);
  2594. fseek (ifp, offset, SEEK_SET);
  2595. for (i=0; i < nseg*2; i++)
  2596. seg[0][i] = get4() + data_offset*(i & 1);
  2597. fseek (ifp, 78, SEEK_SET);
  2598. holes = fgetc(ifp);
  2599. fseek (ifp, 88, SEEK_SET);
  2600. seg[nseg][0] = raw_height * raw_width;
  2601. seg[nseg][1] = get4() + data_offset;
  2602. for (i=0; i < nseg; i++)
  2603. smal_decode_segment (seg+i, holes);
  2604. if (holes) fill_holes (holes);
  2605. }
  2606. void CLASS redcine_load_raw()
  2607. {
  2608. #ifndef NO_JASPER
  2609. int c, row, col;
  2610. jas_stream_t *in;
  2611. jas_image_t *jimg;
  2612. jas_matrix_t *jmat;
  2613. jas_seqent_t *data;
  2614. ushort *img, *pix;
  2615. jas_init();
  2616. #ifndef LIBRAW_LIBRARY_BUILD
  2617. in = jas_stream_fopen (ifname, "rb");
  2618. #else
  2619. in = (jas_stream_t*)ifp->make_jas_stream();
  2620. if(!in)
  2621. throw LIBRAW_EXCEPTION_DECODE_JPEG2000;
  2622. #endif
  2623. jas_stream_seek (in, data_offset+20, SEEK_SET);
  2624. jimg = jas_image_decode (in, -1, 0);
  2625. #ifndef LIBRAW_LIBRARY_BUILD
  2626. if (!jimg) longjmp (failure, 3);
  2627. #else
  2628. if(!jimg)
  2629. {
  2630. jas_stream_close (in);
  2631. throw LIBRAW_EXCEPTION_DECODE_JPEG2000;
  2632. }
  2633. #endif
  2634. jmat = jas_matrix_create (height/2, width/2);
  2635. merror (jmat, "redcine_load_raw()");
  2636. img = (ushort *) calloc ((height+2)*(width+2), 2);
  2637. merror (img, "redcine_load_raw()");
  2638. FORC4 {
  2639. jas_image_readcmpt (jimg, c, 0, 0, width/2, height/2, jmat);
  2640. data = jas_matrix_getref (jmat, 0, 0);
  2641. for (row = c >> 1; row < height; row+=2)
  2642. for (col = c & 1; col < width; col+=2)
  2643. img[(row+1)*(width+2)+col+1] = data[(row/2)*(width/2)+col/2];
  2644. }
  2645. for (col=1; col <= width; col++) {
  2646. img[col] = img[2*(width+2)+col];
  2647. img[(height+1)*(width+2)+col] = img[(height-1)*(width+2)+col];
  2648. }
  2649. for (row=0; row < height+2; row++) {
  2650. img[row*(width+2)] = img[row*(width+2)+2];
  2651. img[(row+1)*(width+2)-1] = img[(row+1)*(width+2)-3];
  2652. }
  2653. for (row=1; row <= height; row++) {
  2654. pix = img + row*(width+2) + (col = 1 + (FC(row,1) & 1));
  2655. for ( ; col <= width; col+=2, pix+=2) {
  2656. c = (((pix[0] - 0x800) << 3) +
  2657. pix[-(width+2)] + pix[width+2] + pix[-1] + pix[1]) >> 2;
  2658. pix[0] = LIM(c,0,4095);
  2659. }
  2660. }
  2661. for (row=0; row < height; row++)
  2662. for (col=0; col < width; col++)
  2663. RAW(row,col) = curve[img[(row+1)*(width+2)+col+1]];
  2664. free (img);
  2665. jas_matrix_destroy (jmat);
  2666. jas_image_destroy (jimg);
  2667. jas_stream_close (in);
  2668. #endif
  2669. }
  2670. void CLASS crop_masked_pixels()
  2671. {
  2672. int row, col;
  2673. unsigned
  2674. #ifndef LIBRAW_LIBRARY_BUILD
  2675. r, raw_pitch = raw_width*2,
  2676. #endif
  2677. c, m, mblack[8], zero, val;
  2678. #ifndef LIBRAW_LIBRARY_BUILD
  2679. if (load_raw == &CLASS phase_one_load_raw ||
  2680. load_raw == &CLASS phase_one_load_raw_c)
  2681. phase_one_correct();
  2682. if (fuji_width) {
  2683. for (row=0; row < raw_height-top_margin*2; row++) {
  2684. for (col=0; col < fuji_width << !fuji_layout; col++) {
  2685. if (fuji_layout) {
  2686. r = fuji_width - 1 - col + (row >> 1);
  2687. c = col + ((row+1) >> 1);
  2688. } else {
  2689. r = fuji_width - 1 + row - (col >> 1);
  2690. c = row + ((col+1) >> 1);
  2691. }
  2692. if (r < height && c < width)
  2693. BAYER(r,c) = RAW(row+top_margin,col+left_margin);
  2694. }
  2695. }
  2696. } else {
  2697. for (row=0; row < height; row++)
  2698. for (col=0; col < width; col++)
  2699. BAYER2(row,col) = RAW(row+top_margin,col+left_margin);
  2700. }
  2701. #endif
  2702. if (mask[0][3]) goto mask_set;
  2703. if (load_raw == &CLASS canon_load_raw ||
  2704. load_raw == &CLASS lossless_jpeg_load_raw) {
  2705. mask[0][1] = mask[1][1] = 2;
  2706. mask[0][3] = -2;
  2707. goto sides;
  2708. }
  2709. if (load_raw == &CLASS canon_600_load_raw ||
  2710. load_raw == &CLASS sony_load_raw ||
  2711. (load_raw == &CLASS eight_bit_load_raw && strncmp(model,"DC2",3)) ||
  2712. load_raw == &CLASS kodak_262_load_raw ||
  2713. (load_raw == &CLASS packed_load_raw && (load_flags & 32))) {
  2714. sides:
  2715. mask[0][0] = mask[1][0] = top_margin;
  2716. mask[0][2] = mask[1][2] = top_margin+height;
  2717. mask[0][3] += left_margin;
  2718. mask[1][1] += left_margin+width;
  2719. mask[1][3] += raw_width;
  2720. }
  2721. if (load_raw == &CLASS nokia_load_raw) {
  2722. mask[0][2] = top_margin;
  2723. mask[0][3] = width;
  2724. }
  2725. mask_set:
  2726. memset (mblack, 0, sizeof mblack);
  2727. for (zero=m=0; m < 8; m++)
  2728. for (row=mask[m][0]; row < mask[m][2]; row++)
  2729. for (col=mask[m][1]; col < mask[m][3]; col++) {
  2730. c = FC(row-top_margin,col-left_margin);
  2731. mblack[c] += val = raw_image[(row)*raw_pitch/2+(col)];
  2732. mblack[4+c]++;
  2733. zero += !val;
  2734. }
  2735. if (load_raw == &CLASS canon_600_load_raw && width < raw_width) {
  2736. black = (mblack[0]+mblack[1]+mblack[2]+mblack[3]) /
  2737. (mblack[4]+mblack[5]+mblack[6]+mblack[7]) - 4;
  2738. #ifndef LIBRAW_LIBRARY_BUILD
  2739. canon_600_correct();
  2740. #endif
  2741. } else if (zero < mblack[4] && mblack[5] && mblack[6] && mblack[7])
  2742. FORC4 cblack[c] = mblack[c] / mblack[4+c];
  2743. }
  2744. void CLASS remove_zeroes()
  2745. {
  2746. unsigned row, col, tot, n, r, c;
  2747. #ifdef LIBRAW_LIBRARY_BUILD
  2748. RUN_CALLBACK(LIBRAW_PROGRESS_REMOVE_ZEROES,0,2);
  2749. #endif
  2750. for (row=0; row < height; row++)
  2751. for (col=0; col < width; col++)
  2752. if (BAYER(row,col) == 0) {
  2753. tot = n = 0;
  2754. for (r = row-2; r <= row+2; r++)
  2755. for (c = col-2; c <= col+2; c++)
  2756. if (r < height && c < width &&
  2757. FC(r,c) == FC(row,col) && BAYER(r,c))
  2758. tot += (n++,BAYER(r,c));
  2759. if (n) BAYER(row,col) = tot/n;
  2760. }
  2761. #ifdef LIBRAW_LIBRARY_BUILD
  2762. RUN_CALLBACK(LIBRAW_PROGRESS_REMOVE_ZEROES,1,2);
  2763. #endif
  2764. }
  2765. void CLASS gamma_curve (double pwr, double ts, int mode, int imax)
  2766. {
  2767. int i;
  2768. double g[6], bnd[2]={0,0}, r;
  2769. g[0] = pwr;
  2770. g[1] = ts;
  2771. g[2] = g[3] = g[4] = 0;
  2772. bnd[g[1] >= 1] = 1;
  2773. if (g[1] && (g[1]-1)*(g[0]-1) <= 0) {
  2774. for (i=0; i < 48; i++) {
  2775. g[2] = (bnd[0] + bnd[1])/2;
  2776. if (g[0]) bnd[(pow(g[2]/g[1],-g[0]) - 1)/g[0] - 1/g[2] > -1] = g[2];
  2777. else bnd[g[2]/exp(1-1/g[2]) < g[1]] = g[2];
  2778. }
  2779. g[3] = g[2] / g[1];
  2780. if (g[0]) g[4] = g[2] * (1/g[0] - 1);
  2781. }
  2782. if (g[0]) g[5] = 1 / (g[1]*SQR(g[3])/2 - g[4]*(1 - g[3]) +
  2783. (1 - pow(g[3],1+g[0]))*(1 + g[4])/(1 + g[0])) - 1;
  2784. else g[5] = 1 / (g[1]*SQR(g[3])/2 + 1
  2785. - g[2] - g[3] - g[2]*g[3]*(log(g[3]) - 1)) - 1;
  2786. if (!mode--) {
  2787. memcpy (gamm, g, sizeof gamm);
  2788. return;
  2789. }
  2790. for (i=0; i < 0x10000; i++) {
  2791. curve[i] = 0xffff;
  2792. if ((r = (double) i / imax) < 1)
  2793. curve[i] = 0x10000 * ( mode
  2794. ? (r < g[3] ? r*g[1] : (g[0] ? pow( r,g[0])*(1+g[4])-g[4] : log(r)*g[2]+1))
  2795. : (r < g[2] ? r/g[1] : (g[0] ? pow((r+g[4])/(1+g[4]),1/g[0]) : exp((r-1)/g[2]))));
  2796. }
  2797. }
  2798. void CLASS pseudoinverse (double (*in)[3], double (*out)[3], int size)
  2799. {
  2800. double work[3][6], num;
  2801. int i, j, k;
  2802. for (i=0; i < 3; i++) {
  2803. for (j=0; j < 6; j++)
  2804. work[i][j] = j == i+3;
  2805. for (j=0; j < 3; j++)
  2806. for (k=0; k < size; k++)
  2807. work[i][j] += in[k][i] * in[k][j];
  2808. }
  2809. for (i=0; i < 3; i++) {
  2810. num = work[i][i];
  2811. for (j=0; j < 6; j++)
  2812. work[i][j] /= num;
  2813. for (k=0; k < 3; k++) {
  2814. if (k==i) continue;
  2815. num = work[k][i];
  2816. for (j=0; j < 6; j++)
  2817. work[k][j] -= work[i][j] * num;
  2818. }
  2819. }
  2820. for (i=0; i < size; i++)
  2821. for (j=0; j < 3; j++)
  2822. for (out[i][j]=k=0; k < 3; k++)
  2823. out[i][j] += work[j][k+3] * in[i][k];
  2824. }
  2825. void CLASS cam_xyz_coeff (double cam_xyz[4][3])
  2826. {
  2827. double cam_rgb[4][3], inverse[4][3], num;
  2828. int i, j, k;
  2829. for (i=0; i < colors; i++) /* Multiply out XYZ colorspace */
  2830. for (j=0; j < 3; j++)
  2831. for (cam_rgb[i][j] = k=0; k < 3; k++)
  2832. cam_rgb[i][j] += cam_xyz[i][k] * xyz_rgb[k][j];
  2833. for (i=0; i < colors; i++) { /* Normalize cam_rgb so that */
  2834. for (num=j=0; j < 3; j++) /* cam_rgb * (1,1,1) is (1,1,1,1) */
  2835. num += cam_rgb[i][j];
  2836. for (j=0; j < 3; j++)
  2837. cam_rgb[i][j] /= num;
  2838. pre_mul[i] = 1 / num;
  2839. }
  2840. pseudoinverse (cam_rgb, inverse, colors);
  2841. for (raw_color = i=0; i < 3; i++)
  2842. for (j=0; j < colors; j++)
  2843. rgb_cam[i][j] = inverse[j][i];
  2844. }
  2845. #ifdef COLORCHECK
  2846. void CLASS colorcheck()
  2847. {
  2848. #define NSQ 24
  2849. // Coordinates of the GretagMacbeth ColorChecker squares
  2850. // width, height, 1st_column, 1st_row
  2851. int cut[NSQ][4]; // you must set these
  2852. // ColorChecker Chart under 6500-kelvin illumination
  2853. static const double gmb_xyY[NSQ][3] = {
  2854. { 0.400, 0.350, 10.1 }, // Dark Skin
  2855. { 0.377, 0.345, 35.8 }, // Light Skin
  2856. { 0.247, 0.251, 19.3 }, // Blue Sky
  2857. { 0.337, 0.422, 13.3 }, // Foliage
  2858. { 0.265, 0.240, 24.3 }, // Blue Flower
  2859. { 0.261, 0.343, 43.1 }, // Bluish Green
  2860. { 0.506, 0.407, 30.1 }, // Orange
  2861. { 0.211, 0.175, 12.0 }, // Purplish Blue
  2862. { 0.453, 0.306, 19.8 }, // Moderate Red
  2863. { 0.285, 0.202, 6.6 }, // Purple
  2864. { 0.380, 0.489, 44.3 }, // Yellow Green
  2865. { 0.473, 0.438, 43.1 }, // Orange Yellow
  2866. { 0.187, 0.129, 6.1 }, // Blue
  2867. { 0.305, 0.478, 23.4 }, // Green
  2868. { 0.539, 0.313, 12.0 }, // Red
  2869. { 0.448, 0.470, 59.1 }, // Yellow
  2870. { 0.364, 0.233, 19.8 }, // Magenta
  2871. { 0.196, 0.252, 19.8 }, // Cyan
  2872. { 0.310, 0.316, 90.0 }, // White
  2873. { 0.310, 0.316, 59.1 }, // Neutral 8
  2874. { 0.310, 0.316, 36.2 }, // Neutral 6.5
  2875. { 0.310, 0.316, 19.8 }, // Neutral 5
  2876. { 0.310, 0.316, 9.0 }, // Neutral 3.5
  2877. { 0.310, 0.316, 3.1 } }; // Black
  2878. double gmb_cam[NSQ][4], gmb_xyz[NSQ][3];
  2879. double inverse[NSQ][3], cam_xyz[4][3], num;
  2880. int c, i, j, k, sq, row, col, count[4];
  2881. memset (gmb_cam, 0, sizeof gmb_cam);
  2882. for (sq=0; sq < NSQ; sq++) {
  2883. FORCC count[c] = 0;
  2884. for (row=cut[sq][3]; row < cut[sq][3]+cut[sq][1]; row++)
  2885. for (col=cut[sq][2]; col < cut[sq][2]+cut[sq][0]; col++) {
  2886. c = FC(row,col);
  2887. if (c >= colors) c -= 2;
  2888. gmb_cam[sq][c] += BAYER(row,col);
  2889. count[c]++;
  2890. }
  2891. FORCC gmb_cam[sq][c] = gmb_cam[sq][c]/count[c] - black;
  2892. gmb_xyz[sq][0] = gmb_xyY[sq][2] * gmb_xyY[sq][0] / gmb_xyY[sq][1];
  2893. gmb_xyz[sq][1] = gmb_xyY[sq][2];
  2894. gmb_xyz[sq][2] = gmb_xyY[sq][2] *
  2895. (1 - gmb_xyY[sq][0] - gmb_xyY[sq][1]) / gmb_xyY[sq][1];
  2896. }
  2897. pseudoinverse (gmb_xyz, inverse, NSQ);
  2898. for (i=0; i < colors; i++)
  2899. for (j=0; j < 3; j++)
  2900. for (cam_xyz[i][j] = k=0; k < NSQ; k++)
  2901. cam_xyz[i][j] += gmb_cam[k][i] * inverse[k][j];
  2902. cam_xyz_coeff (cam_xyz);
  2903. if (verbose) {
  2904. printf (" { \"%s %s\", %d,\n\t{", make, model, black);
  2905. num = 10000 / (cam_xyz[1][0] + cam_xyz[1][1] + cam_xyz[1][2]);
  2906. FORCC for (j=0; j < 3; j++)
  2907. printf ("%c%d", (c | j) ? ',':' ', (int) (cam_xyz[c][j] * num + 0.5));
  2908. puts (" } },");
  2909. }
  2910. #undef NSQ
  2911. }
  2912. #endif
  2913. void CLASS hat_transform (float *temp, float *base, int st, int size, int sc)
  2914. {
  2915. int i;
  2916. for (i=0; i < sc; i++)
  2917. temp[i] = 2*base[st*i] + base[st*(sc-i)] + base[st*(i+sc)];
  2918. for (; i+sc < size; i++)
  2919. temp[i] = 2*base[st*i] + base[st*(i-sc)] + base[st*(i+sc)];
  2920. for (; i < size; i++)
  2921. temp[i] = 2*base[st*i] + base[st*(i-sc)] + base[st*(2*size-2-(i+sc))];
  2922. }
  2923. #if !defined(LIBRAW_USE_OPENMP)
  2924. void CLASS wavelet_denoise()
  2925. {
  2926. float *fimg=0, *temp, thold, mul[2], avg, diff;
  2927. int scale=1, size, lev, hpass, lpass, row, col, nc, c, i, wlast, blk[2];
  2928. ushort *window[4];
  2929. static const float noise[] =
  2930. { 0.8002,0.2735,0.1202,0.0585,0.0291,0.0152,0.0080,0.0044 };
  2931. #ifdef DCRAW_VERBOSE
  2932. if (verbose) fprintf (stderr,_("Wavelet denoising...\n"));
  2933. #endif
  2934. while (maximum << scale < 0x10000) scale++;
  2935. maximum <<= --scale;
  2936. black <<= scale;
  2937. FORC4 cblack[c] <<= scale;
  2938. if ((size = iheight*iwidth) < 0x15550000)
  2939. fimg = (float *) malloc ((size*3 + iheight + iwidth) * sizeof *fimg);
  2940. merror (fimg, "wavelet_denoise()");
  2941. temp = fimg + size*3;
  2942. if ((nc = colors) == 3 && filters) nc++;
  2943. FORC(nc) { /* denoise R,G1,B,G3 individually */
  2944. for (i=0; i < size; i++)
  2945. fimg[i] = 256 * sqrt((double)(image[i][c] << scale));
  2946. for (hpass=lev=0; lev < 5; lev++) {
  2947. lpass = size*((lev & 1)+1);
  2948. for (row=0; row < iheight; row++) {
  2949. hat_transform (temp, fimg+hpass+row*iwidth, 1, iwidth, 1 << lev);
  2950. for (col=0; col < iwidth; col++)
  2951. fimg[lpass + row*iwidth + col] = temp[col] * 0.25;
  2952. }
  2953. for (col=0; col < iwidth; col++) {
  2954. hat_transform (temp, fimg+lpass+col, iwidth, iheight, 1 << lev);
  2955. for (row=0; row < iheight; row++)
  2956. fimg[lpass + row*iwidth + col] = temp[row] * 0.25;
  2957. }
  2958. thold = threshold * noise[lev];
  2959. for (i=0; i < size; i++) {
  2960. fimg[hpass+i] -= fimg[lpass+i];
  2961. if (fimg[hpass+i] < -thold) fimg[hpass+i] += thold;
  2962. else if (fimg[hpass+i] > thold) fimg[hpass+i] -= thold;
  2963. else fimg[hpass+i] = 0;
  2964. if (hpass) fimg[i] += fimg[hpass+i];
  2965. }
  2966. hpass = lpass;
  2967. }
  2968. for (i=0; i < size; i++)
  2969. image[i][c] = CLIP(SQR(fimg[i]+fimg[lpass+i])/0x10000);
  2970. }
  2971. if (filters && colors == 3) { /* pull G1 and G3 closer together */
  2972. for (row=0; row < 2; row++) {
  2973. mul[row] = 0.125 * pre_mul[FC(row+1,0) | 1] / pre_mul[FC(row,0) | 1];
  2974. blk[row] = cblack[FC(row,0) | 1];
  2975. }
  2976. for (i=0; i < 4; i++)
  2977. window[i] = (ushort *) fimg + width*i;
  2978. for (wlast=-1, row=1; row < height-1; row++) {
  2979. while (wlast < row+1) {
  2980. for (wlast++, i=0; i < 4; i++)
  2981. window[(i+3) & 3] = window[i];
  2982. for (col = FC(wlast,1) & 1; col < width; col+=2)
  2983. window[2][col] = BAYER(wlast,col);
  2984. }
  2985. thold = threshold/512;
  2986. for (col = (FC(row,0) & 1)+1; col < width-1; col+=2) {
  2987. avg = ( window[0][col-1] + window[0][col+1] +
  2988. window[2][col-1] + window[2][col+1] - blk[~row & 1]*4 )
  2989. * mul[row & 1] + (window[1][col] + blk[row & 1]) * 0.5;
  2990. avg = avg < 0 ? 0 : sqrt(avg);
  2991. diff = sqrt((double)BAYER(row,col)) - avg;
  2992. if (diff < -thold) diff += thold;
  2993. else if (diff > thold) diff -= thold;
  2994. else diff = 0;
  2995. BAYER(row,col) = CLIP(SQR(avg+diff) + 0.5);
  2996. }
  2997. }
  2998. }
  2999. free (fimg);
  3000. }
  3001. #else /* LIBRAW_USE_OPENMP */
  3002. void CLASS wavelet_denoise()
  3003. {
  3004. float *fimg=0, *temp, thold, mul[2], avg, diff;
  3005. int scale=1, size, lev, hpass, lpass, row, col, nc, c, i, wlast, blk[2];
  3006. ushort *window[4];
  3007. static const float noise[] =
  3008. { 0.8002,0.2735,0.1202,0.0585,0.0291,0.0152,0.0080,0.0044 };
  3009. #ifdef DCRAW_VERBOSE
  3010. if (verbose) fprintf (stderr,_("Wavelet denoising...\n"));
  3011. #endif
  3012. while (maximum << scale < 0x10000) scale++;
  3013. maximum <<= --scale;
  3014. black <<= scale;
  3015. FORC4 cblack[c] <<= scale;
  3016. if ((size = iheight*iwidth) < 0x15550000)
  3017. fimg = (float *) malloc ((size*3 + iheight + iwidth) * sizeof *fimg);
  3018. merror (fimg, "wavelet_denoise()");
  3019. temp = fimg + size*3;
  3020. if ((nc = colors) == 3 && filters) nc++;
  3021. #ifdef LIBRAW_LIBRARY_BUILD
  3022. #pragma omp parallel default(shared) private(i,col,row,thold,lev,lpass,hpass,temp,c) firstprivate(scale,size)
  3023. #endif
  3024. {
  3025. temp = (float*)malloc( (iheight + iwidth) * sizeof *fimg);
  3026. FORC(nc) { /* denoise R,G1,B,G3 individually */
  3027. #ifdef LIBRAW_LIBRARY_BUILD
  3028. #pragma omp for
  3029. #endif
  3030. for (i=0; i < size; i++)
  3031. fimg[i] = 256 * sqrt((double)(image[i][c] << scale));
  3032. for (hpass=lev=0; lev < 5; lev++) {
  3033. lpass = size*((lev & 1)+1);
  3034. #ifdef LIBRAW_LIBRARY_BUILD
  3035. #pragma omp for
  3036. #endif
  3037. for (row=0; row < iheight; row++) {
  3038. hat_transform (temp, fimg+hpass+row*iwidth, 1, iwidth, 1 << lev);
  3039. for (col=0; col < iwidth; col++)
  3040. fimg[lpass + row*iwidth + col] = temp[col] * 0.25;
  3041. }
  3042. #ifdef LIBRAW_LIBRARY_BUILD
  3043. #pragma omp for
  3044. #endif
  3045. for (col=0; col < iwidth; col++) {
  3046. hat_transform (temp, fimg+lpass+col, iwidth, iheight, 1 << lev);
  3047. for (row=0; row < iheight; row++)
  3048. fimg[lpass + row*iwidth + col] = temp[row] * 0.25;
  3049. }
  3050. thold = threshold * noise[lev];
  3051. #ifdef LIBRAW_LIBRARY_BUILD
  3052. #pragma omp for
  3053. #endif
  3054. for (i=0; i < size; i++) {
  3055. fimg[hpass+i] -= fimg[lpass+i];
  3056. if (fimg[hpass+i] < -thold) fimg[hpass+i] += thold;
  3057. else if (fimg[hpass+i] > thold) fimg[hpass+i] -= thold;
  3058. else fimg[hpass+i] = 0;
  3059. if (hpass) fimg[i] += fimg[hpass+i];
  3060. }
  3061. hpass = lpass;
  3062. }
  3063. #ifdef LIBRAW_LIBRARY_BUILD
  3064. #pragma omp for
  3065. #endif
  3066. for (i=0; i < size; i++)
  3067. image[i][c] = CLIP(SQR(fimg[i]+fimg[lpass+i])/0x10000);
  3068. }
  3069. free(temp);
  3070. } /* end omp parallel */
  3071. /* the following loops are hard to parallize, no idea yes,
  3072. * problem is wlast which is carrying dependency
  3073. * second part should be easyer, but did not yet get it right.
  3074. */
  3075. if (filters && colors == 3) { /* pull G1 and G3 closer together */
  3076. for (row=0; row < 2; row++){
  3077. mul[row] = 0.125 * pre_mul[FC(row+1,0) | 1] / pre_mul[FC(row,0) | 1];
  3078. blk[row] = cblack[FC(row,0) | 1];
  3079. }
  3080. for (i=0; i < 4; i++)
  3081. window[i] = (ushort *) fimg + width*i;
  3082. for (wlast=-1, row=1; row < height-1; row++) {
  3083. while (wlast < row+1) {
  3084. for (wlast++, i=0; i < 4; i++)
  3085. window[(i+3) & 3] = window[i];
  3086. for (col = FC(wlast,1) & 1; col < width; col+=2)
  3087. window[2][col] = BAYER(wlast,col);
  3088. }
  3089. thold = threshold/512;
  3090. for (col = (FC(row,0) & 1)+1; col < width-1; col+=2) {
  3091. avg = ( window[0][col-1] + window[0][col+1] +
  3092. window[2][col-1] + window[2][col+1] - blk[~row & 1]*4 )
  3093. * mul[row & 1] + (window[1][col] + blk[row & 1]) * 0.5;
  3094. avg = avg < 0 ? 0 : sqrt(avg);
  3095. diff = sqrt((double)BAYER(row,col)) - avg;
  3096. if (diff < -thold) diff += thold;
  3097. else if (diff > thold) diff -= thold;
  3098. else diff = 0;
  3099. BAYER(row,col) = CLIP(SQR(avg+diff) + 0.5);
  3100. }
  3101. }
  3102. }
  3103. free (fimg);
  3104. }
  3105. #endif
  3106. // green equilibration
  3107. void CLASS green_matching()
  3108. {
  3109. int i,j;
  3110. double m1,m2,c1,c2;
  3111. int o1_1,o1_2,o1_3,o1_4;
  3112. int o2_1,o2_2,o2_3,o2_4;
  3113. ushort (*img)[4];
  3114. const int margin = 3;
  3115. int oj = 2, oi = 2;
  3116. float f;
  3117. const float thr = 0.01f;
  3118. if(half_size || shrink) return;
  3119. if(FC(oj, oi) != 3) oj++;
  3120. if(FC(oj, oi) != 3) oi++;
  3121. if(FC(oj, oi) != 3) oj--;
  3122. img = (ushort (*)[4]) calloc (height*width, sizeof *image);
  3123. merror (img, "green_matching()");
  3124. memcpy(img,image,height*width*sizeof *image);
  3125. for(j=oj;j<height-margin;j+=2)
  3126. for(i=oi;i<width-margin;i+=2){
  3127. o1_1=img[(j-1)*width+i-1][1];
  3128. o1_2=img[(j-1)*width+i+1][1];
  3129. o1_3=img[(j+1)*width+i-1][1];
  3130. o1_4=img[(j+1)*width+i+1][1];
  3131. o2_1=img[(j-2)*width+i][3];
  3132. o2_2=img[(j+2)*width+i][3];
  3133. o2_3=img[j*width+i-2][3];
  3134. o2_4=img[j*width+i+2][3];
  3135. m1=(o1_1+o1_2+o1_3+o1_4)/4.0;
  3136. m2=(o2_1+o2_2+o2_3+o2_4)/4.0;
  3137. 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;
  3138. 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;
  3139. if((img[j*width+i][3]<maximum*0.95)&&(c1<maximum*thr)&&(c2<maximum*thr))
  3140. {
  3141. f = image[j*width+i][3]*m1/m2;
  3142. image[j*width+i][3]=f>0xffff?0xffff:f;
  3143. }
  3144. }
  3145. free(img);
  3146. }
  3147. void CLASS scale_colors()
  3148. {
  3149. unsigned bottom, right, size, row, col, ur, uc, i, x, y, c, sum[8];
  3150. int val, dark, sat;
  3151. double dsum[8], dmin, dmax;
  3152. float scale_mul[4], fr, fc;
  3153. ushort *img=0, *pix;
  3154. #ifdef LIBRAW_LIBRARY_BUILD
  3155. RUN_CALLBACK(LIBRAW_PROGRESS_SCALE_COLORS,0,2);
  3156. #endif
  3157. if (user_mul[0])
  3158. memcpy (pre_mul, user_mul, sizeof pre_mul);
  3159. if (use_auto_wb || (use_camera_wb && cam_mul[0] == -1)) {
  3160. memset (dsum, 0, sizeof dsum);
  3161. bottom = MIN (greybox[1]+greybox[3], height);
  3162. right = MIN (greybox[0]+greybox[2], width);
  3163. for (row=greybox[1]; row < bottom; row += 8)
  3164. for (col=greybox[0]; col < right; col += 8) {
  3165. memset (sum, 0, sizeof sum);
  3166. for (y=row; y < row+8 && y < bottom; y++)
  3167. for (x=col; x < col+8 && x < right; x++)
  3168. FORC4 {
  3169. if (filters) {
  3170. c = fcol(y,x);
  3171. val = BAYER2(y,x);
  3172. } else
  3173. val = image[y*width+x][c];
  3174. if (val > maximum-25) goto skip_block;
  3175. if ((val -= cblack[c]) < 0) val = 0;
  3176. sum[c] += val;
  3177. sum[c+4]++;
  3178. if (filters) break;
  3179. }
  3180. FORC(8) dsum[c] += sum[c];
  3181. skip_block: ;
  3182. }
  3183. FORC4 if (dsum[c]) pre_mul[c] = dsum[c+4] / dsum[c];
  3184. }
  3185. if (use_camera_wb && cam_mul[0] != -1) {
  3186. memset (sum, 0, sizeof sum);
  3187. for (row=0; row < 8; row++)
  3188. for (col=0; col < 8; col++) {
  3189. c = FC(row,col);
  3190. if ((val = white[row][col] - cblack[c]) > 0)
  3191. sum[c] += val;
  3192. sum[c+4]++;
  3193. }
  3194. if (sum[0] && sum[1] && sum[2] && sum[3])
  3195. FORC4 pre_mul[c] = (float) sum[c+4] / sum[c];
  3196. else if (cam_mul[0] && cam_mul[2])
  3197. memcpy (pre_mul, cam_mul, sizeof pre_mul);
  3198. else
  3199. {
  3200. #ifdef LIBRAW_LIBRARY_BUILD
  3201. imgdata.process_warnings |= LIBRAW_WARN_BAD_CAMERA_WB;
  3202. #endif
  3203. #ifdef DCRAW_VERBOSE
  3204. fprintf (stderr,_("%s: Cannot use camera white balance.\n"), ifname);
  3205. #endif
  3206. }
  3207. }
  3208. if (pre_mul[3] == 0) pre_mul[3] = colors < 4 ? pre_mul[1] : 1;
  3209. dark = black;
  3210. sat = maximum;
  3211. if (threshold) wavelet_denoise();
  3212. maximum -= black;
  3213. for (dmin=DBL_MAX, dmax=c=0; c < 4; c++) {
  3214. if (dmin > pre_mul[c])
  3215. dmin = pre_mul[c];
  3216. if (dmax < pre_mul[c])
  3217. dmax = pre_mul[c];
  3218. }
  3219. if (!highlight) dmax = dmin;
  3220. FORC4 scale_mul[c] = (pre_mul[c] /= dmax) * 65535.0 / maximum;
  3221. #ifdef DCRAW_VERBOSE
  3222. if (verbose) {
  3223. fprintf (stderr,
  3224. _("Scaling with darkness %d, saturation %d, and\nmultipliers"), dark, sat);
  3225. FORC4 fprintf (stderr, " %f", pre_mul[c]);
  3226. fputc ('\n', stderr);
  3227. }
  3228. #endif
  3229. size = iheight*iwidth;
  3230. #ifdef LIBRAW_LIBRARY_BUILD
  3231. scale_colors_loop(scale_mul);
  3232. #else
  3233. for (i=0; i < size*4; i++) {
  3234. val = image[0][i];
  3235. if (!val) continue;
  3236. val -= cblack[i & 3];
  3237. val *= scale_mul[i & 3];
  3238. image[0][i] = CLIP(val);
  3239. }
  3240. #endif
  3241. if ((aber[0] != 1 || aber[2] != 1) && colors == 3) {
  3242. #ifdef DCRAW_VERBOSE
  3243. if (verbose)
  3244. fprintf (stderr,_("Correcting chromatic aberration...\n"));
  3245. #endif
  3246. for (c=0; c < 4; c+=2) {
  3247. if (aber[c] == 1) continue;
  3248. img = (ushort *) malloc (size * sizeof *img);
  3249. merror (img, "scale_colors()");
  3250. for (i=0; i < size; i++)
  3251. img[i] = image[i][c];
  3252. for (row=0; row < iheight; row++) {
  3253. ur = fr = (row - iheight*0.5) * aber[c] + iheight*0.5;
  3254. if (ur > iheight-2) continue;
  3255. fr -= ur;
  3256. for (col=0; col < iwidth; col++) {
  3257. uc = fc = (col - iwidth*0.5) * aber[c] + iwidth*0.5;
  3258. if (uc > iwidth-2) continue;
  3259. fc -= uc;
  3260. pix = img + ur*iwidth + uc;
  3261. image[row*iwidth+col][c] =
  3262. (pix[ 0]*(1-fc) + pix[ 1]*fc) * (1-fr) +
  3263. (pix[iwidth]*(1-fc) + pix[iwidth+1]*fc) * fr;
  3264. }
  3265. }
  3266. free(img);
  3267. }
  3268. }
  3269. #ifdef LIBRAW_LIBRARY_BUILD
  3270. RUN_CALLBACK(LIBRAW_PROGRESS_SCALE_COLORS,1,2);
  3271. #endif
  3272. }
  3273. void CLASS pre_interpolate()
  3274. {
  3275. ushort (*img)[4];
  3276. int row, col, c;
  3277. #ifdef LIBRAW_LIBRARY_BUILD
  3278. RUN_CALLBACK(LIBRAW_PROGRESS_PRE_INTERPOLATE,0,2);
  3279. #endif
  3280. if (shrink) {
  3281. if (half_size) {
  3282. height = iheight;
  3283. width = iwidth;
  3284. } else {
  3285. img = (ushort (*)[4]) calloc (height*width, sizeof *img);
  3286. merror (img, "pre_interpolate()");
  3287. for (row=0; row < height; row++)
  3288. for (col=0; col < width; col++) {
  3289. c = fcol(row,col);
  3290. img[row*width+col][c] = image[(row >> 1)*iwidth+(col >> 1)][c];
  3291. }
  3292. free (image);
  3293. image = img;
  3294. shrink = 0;
  3295. }
  3296. }
  3297. if (filters > 1000 && colors == 3) {
  3298. if (four_color_rgb && colors++)
  3299. mix_green = !half_size;
  3300. else {
  3301. for (row = FC(1,0) >> 1; row < height; row+=2)
  3302. for (col = FC(row,1) & 1; col < width; col+=2)
  3303. image[row*width+col][1] = image[row*width+col][3];
  3304. filters &= ~((filters & 0x55555555) << 1);
  3305. }
  3306. }
  3307. if (half_size) filters = 0;
  3308. #ifdef LIBRAW_LIBRARY_BUILD
  3309. RUN_CALLBACK(LIBRAW_PROGRESS_PRE_INTERPOLATE,1,2);
  3310. #endif
  3311. }
  3312. void CLASS border_interpolate (int border)
  3313. {
  3314. unsigned row, col, y, x, f, c, sum[8];
  3315. for (row=0; row < height; row++)
  3316. for (col=0; col < width; col++) {
  3317. if (col==border && row >= border && row < height-border)
  3318. col = width-border;
  3319. memset (sum, 0, sizeof sum);
  3320. for (y=row-1; y != row+2; y++)
  3321. for (x=col-1; x != col+2; x++)
  3322. if (y < height && x < width) {
  3323. f = fcol(y,x);
  3324. sum[f] += image[y*width+x][f];
  3325. sum[f+4]++;
  3326. }
  3327. f = fcol(row,col);
  3328. FORCC if (c != f && sum[c+4])
  3329. image[row*width+col][c] = sum[c] / sum[c+4];
  3330. }
  3331. }
  3332. void CLASS lin_interpolate_loop(int code[16][16][32],int size)
  3333. {
  3334. int row;
  3335. for (row=1; row < height-1; row++)
  3336. {
  3337. int col,*ip;
  3338. ushort *pix;
  3339. for (col=1; col < width-1; col++) {
  3340. int i;
  3341. int sum[4];
  3342. pix = image[row*width+col];
  3343. ip = code[row % size][col % size];
  3344. memset (sum, 0, sizeof sum);
  3345. for (i=*ip++; i--; ip+=3)
  3346. sum[ip[2]] += pix[ip[0]] << ip[1];
  3347. for (i=colors; --i; ip+=2)
  3348. pix[ip[0]] = sum[ip[0]] * ip[1] >> 8;
  3349. }
  3350. }
  3351. }
  3352. void CLASS lin_interpolate()
  3353. {
  3354. int code[16][16][32], size=16, *ip, sum[4];
  3355. int f, c, x, y, row, col, shift, color;
  3356. #ifdef DCRAW_VERBOSE
  3357. if (verbose) fprintf (stderr,_("Bilinear interpolation...\n"));
  3358. #endif
  3359. #ifdef LIBRAW_LIBRARY_BUILD
  3360. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,0,3);
  3361. #endif
  3362. if (filters == 2) size = 6;
  3363. border_interpolate(1);
  3364. for (row=0; row < size; row++)
  3365. for (col=0; col < size; col++) {
  3366. ip = code[row][col]+1;
  3367. f = fcol(row,col);
  3368. memset (sum, 0, sizeof sum);
  3369. for (y=-1; y <= 1; y++)
  3370. for (x=-1; x <= 1; x++) {
  3371. shift = (y==0) + (x==0);
  3372. color = fcol(row+y,col+x);
  3373. if (color == f) continue;
  3374. *ip++ = (width*y + x)*4 + color;
  3375. *ip++ = shift;
  3376. *ip++ = color;
  3377. sum[color] += 1 << shift;
  3378. }
  3379. code[row][col][0] = (ip - code[row][col]) / 3;
  3380. FORCC
  3381. if (c != f) {
  3382. *ip++ = c;
  3383. *ip++ = 256 / sum[c];
  3384. }
  3385. }
  3386. #ifdef LIBRAW_LIBRARY_BUILD
  3387. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,1,3);
  3388. #endif
  3389. lin_interpolate_loop(code,size);
  3390. #ifdef LIBRAW_LIBRARY_BUILD
  3391. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,2,3);
  3392. #endif
  3393. }
  3394. /*
  3395. This algorithm is officially called:
  3396. "Interpolation using a Threshold-based variable number of gradients"
  3397. described in http://scien.stanford.edu/pages/labsite/1999/psych221/projects/99/tingchen/algodep/vargra.html
  3398. I've extended the basic idea to work with non-Bayer filter arrays.
  3399. Gradients are numbered clockwise from NW=0 to W=7.
  3400. */
  3401. void CLASS vng_interpolate()
  3402. {
  3403. static const signed char *cp, terms[] = {
  3404. -2,-2,+0,-1,0,0x01, -2,-2,+0,+0,1,0x01, -2,-1,-1,+0,0,0x01,
  3405. -2,-1,+0,-1,0,0x02, -2,-1,+0,+0,0,0x03, -2,-1,+0,+1,1,0x01,
  3406. -2,+0,+0,-1,0,0x06, -2,+0,+0,+0,1,0x02, -2,+0,+0,+1,0,0x03,
  3407. -2,+1,-1,+0,0,0x04, -2,+1,+0,-1,1,0x04, -2,+1,+0,+0,0,0x06,
  3408. -2,+1,+0,+1,0,0x02, -2,+2,+0,+0,1,0x04, -2,+2,+0,+1,0,0x04,
  3409. -1,-2,-1,+0,0,0x80, -1,-2,+0,-1,0,0x01, -1,-2,+1,-1,0,0x01,
  3410. -1,-2,+1,+0,1,0x01, -1,-1,-1,+1,0,0x88, -1,-1,+1,-2,0,0x40,
  3411. -1,-1,+1,-1,0,0x22, -1,-1,+1,+0,0,0x33, -1,-1,+1,+1,1,0x11,
  3412. -1,+0,-1,+2,0,0x08, -1,+0,+0,-1,0,0x44, -1,+0,+0,+1,0,0x11,
  3413. -1,+0,+1,-2,1,0x40, -1,+0,+1,-1,0,0x66, -1,+0,+1,+0,1,0x22,
  3414. -1,+0,+1,+1,0,0x33, -1,+0,+1,+2,1,0x10, -1,+1,+1,-1,1,0x44,
  3415. -1,+1,+1,+0,0,0x66, -1,+1,+1,+1,0,0x22, -1,+1,+1,+2,0,0x10,
  3416. -1,+2,+0,+1,0,0x04, -1,+2,+1,+0,1,0x04, -1,+2,+1,+1,0,0x04,
  3417. +0,-2,+0,+0,1,0x80, +0,-1,+0,+1,1,0x88, +0,-1,+1,-2,0,0x40,
  3418. +0,-1,+1,+0,0,0x11, +0,-1,+2,-2,0,0x40, +0,-1,+2,-1,0,0x20,
  3419. +0,-1,+2,+0,0,0x30, +0,-1,+2,+1,1,0x10, +0,+0,+0,+2,1,0x08,
  3420. +0,+0,+2,-2,1,0x40, +0,+0,+2,-1,0,0x60, +0,+0,+2,+0,1,0x20,
  3421. +0,+0,+2,+1,0,0x30, +0,+0,+2,+2,1,0x10, +0,+1,+1,+0,0,0x44,
  3422. +0,+1,+1,+2,0,0x10, +0,+1,+2,-1,1,0x40, +0,+1,+2,+0,0,0x60,
  3423. +0,+1,+2,+1,0,0x20, +0,+1,+2,+2,0,0x10, +1,-2,+1,+0,0,0x80,
  3424. +1,-1,+1,+1,0,0x88, +1,+0,+1,+2,0,0x08, +1,+0,+2,-1,0,0x40,
  3425. +1,+0,+2,+1,0,0x10
  3426. }, chood[] = { -1,-1, -1,0, -1,+1, 0,+1, +1,+1, +1,0, +1,-1, 0,-1 };
  3427. ushort (*brow[5])[4], *pix;
  3428. int prow=8, pcol=2, *ip, *code[16][16], gval[8], gmin, gmax, sum[4];
  3429. int row, col, x, y, x1, x2, y1, y2, t, weight, grads, color, diag;
  3430. int g, diff, thold, num, c;
  3431. lin_interpolate();
  3432. #ifdef DCRAW_VERBOSE
  3433. if (verbose) fprintf (stderr,_("VNG interpolation...\n"));
  3434. #endif
  3435. if (filters == 1) prow = pcol = 16;
  3436. if (filters == 2) prow = pcol = 6;
  3437. ip = (int *) calloc (prow*pcol, 1280);
  3438. merror (ip, "vng_interpolate()");
  3439. for (row=0; row < prow; row++) /* Precalculate for VNG */
  3440. for (col=0; col < pcol; col++) {
  3441. code[row][col] = ip;
  3442. for (cp=terms, t=0; t < 64; t++) {
  3443. y1 = *cp++; x1 = *cp++;
  3444. y2 = *cp++; x2 = *cp++;
  3445. weight = *cp++;
  3446. grads = *cp++;
  3447. color = fcol(row+y1,col+x1);
  3448. if (fcol(row+y2,col+x2) != color) continue;
  3449. diag = (fcol(row,col+1) == color && fcol(row+1,col) == color) ? 2:1;
  3450. if (abs(y1-y2) == diag && abs(x1-x2) == diag) continue;
  3451. *ip++ = (y1*width + x1)*4 + color;
  3452. *ip++ = (y2*width + x2)*4 + color;
  3453. *ip++ = weight;
  3454. for (g=0; g < 8; g++)
  3455. if (grads & 1<<g) *ip++ = g;
  3456. *ip++ = -1;
  3457. }
  3458. *ip++ = INT_MAX;
  3459. for (cp=chood, g=0; g < 8; g++) {
  3460. y = *cp++; x = *cp++;
  3461. *ip++ = (y*width + x) * 4;
  3462. color = fcol(row,col);
  3463. if (fcol(row+y,col+x) != color && fcol(row+y*2,col+x*2) == color)
  3464. *ip++ = (y*width + x) * 8 + color;
  3465. else
  3466. *ip++ = 0;
  3467. }
  3468. }
  3469. brow[4] = (ushort (*)[4]) calloc (width*3, sizeof **brow);
  3470. merror (brow[4], "vng_interpolate()");
  3471. for (row=0; row < 3; row++)
  3472. brow[row] = brow[4] + row*width;
  3473. for (row=2; row < height-2; row++) { /* Do VNG interpolation */
  3474. #ifdef LIBRAW_LIBRARY_BUILD
  3475. if(!((row-2)%256))RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,(row-2)/256+1,((height-3)/256)+1);
  3476. #endif
  3477. for (col=2; col < width-2; col++) {
  3478. pix = image[row*width+col];
  3479. ip = code[row % prow][col % pcol];
  3480. memset (gval, 0, sizeof gval);
  3481. while ((g = ip[0]) != INT_MAX) { /* Calculate gradients */
  3482. diff = ABS(pix[g] - pix[ip[1]]) << ip[2];
  3483. gval[ip[3]] += diff;
  3484. ip += 5;
  3485. if ((g = ip[-1]) == -1) continue;
  3486. gval[g] += diff;
  3487. while ((g = *ip++) != -1)
  3488. gval[g] += diff;
  3489. }
  3490. ip++;
  3491. gmin = gmax = gval[0]; /* Choose a threshold */
  3492. for (g=1; g < 8; g++) {
  3493. if (gmin > gval[g]) gmin = gval[g];
  3494. if (gmax < gval[g]) gmax = gval[g];
  3495. }
  3496. if (gmax == 0) {
  3497. memcpy (brow[2][col], pix, sizeof *image);
  3498. continue;
  3499. }
  3500. thold = gmin + (gmax >> 1);
  3501. memset (sum, 0, sizeof sum);
  3502. color = fcol(row,col);
  3503. for (num=g=0; g < 8; g++,ip+=2) { /* Average the neighbors */
  3504. if (gval[g] <= thold) {
  3505. FORCC
  3506. if (c == color && ip[1])
  3507. sum[c] += (pix[c] + pix[ip[1]]) >> 1;
  3508. else
  3509. sum[c] += pix[ip[0] + c];
  3510. num++;
  3511. }
  3512. }
  3513. FORCC { /* Save to buffer */
  3514. t = pix[color];
  3515. if (c != color)
  3516. t += (sum[c] - sum[color]) / num;
  3517. brow[2][col][c] = CLIP(t);
  3518. }
  3519. }
  3520. if (row > 3) /* Write buffer to image */
  3521. memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image);
  3522. for (g=0; g < 4; g++)
  3523. brow[(g-1) & 3] = brow[g];
  3524. }
  3525. memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image);
  3526. memcpy (image[(row-1)*width+2], brow[1]+2, (width-4)*sizeof *image);
  3527. free (brow[4]);
  3528. free (code[0][0]);
  3529. }
  3530. /*
  3531. Patterned Pixel Grouping Interpolation by Alain Desbiolles
  3532. */
  3533. void CLASS ppg_interpolate()
  3534. {
  3535. int dir[5] = { 1, width, -1, -width, 1 };
  3536. int row, col, diff[2], guess[2], c, d, i;
  3537. ushort (*pix)[4];
  3538. border_interpolate(3);
  3539. #ifdef DCRAW_VERBOSE
  3540. if (verbose) fprintf (stderr,_("PPG interpolation...\n"));
  3541. #endif
  3542. /* Fill in the green layer with gradients and pattern recognition: */
  3543. #ifdef LIBRAW_LIBRARY_BUILD
  3544. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,0,3);
  3545. #ifdef LIBRAW_USE_OPENMP
  3546. #pragma omp parallel for default(shared) private(guess, diff, row, col, d, c, i, pix) schedule(static)
  3547. #endif
  3548. #endif
  3549. for (row=3; row < height-3; row++)
  3550. for (col=3+(FC(row,3) & 1), c=FC(row,col); col < width-3; col+=2) {
  3551. pix = image + row*width+col;
  3552. for (i=0; (d=dir[i]) > 0; i++) {
  3553. guess[i] = (pix[-d][1] + pix[0][c] + pix[d][1]) * 2
  3554. - pix[-2*d][c] - pix[2*d][c];
  3555. diff[i] = ( ABS(pix[-2*d][c] - pix[ 0][c]) +
  3556. ABS(pix[ 2*d][c] - pix[ 0][c]) +
  3557. ABS(pix[ -d][1] - pix[ d][1]) ) * 3 +
  3558. ( ABS(pix[ 3*d][1] - pix[ d][1]) +
  3559. ABS(pix[-3*d][1] - pix[-d][1]) ) * 2;
  3560. }
  3561. d = dir[i = diff[0] > diff[1]];
  3562. pix[0][1] = ULIM(guess[i] >> 2, pix[d][1], pix[-d][1]);
  3563. }
  3564. /* Calculate red and blue for each green pixel: */
  3565. #ifdef LIBRAW_LIBRARY_BUILD
  3566. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,1,3);
  3567. #ifdef LIBRAW_USE_OPENMP
  3568. #pragma omp parallel for default(shared) private(guess, diff, row, col, d, c, i, pix) schedule(static)
  3569. #endif
  3570. #endif
  3571. for (row=1; row < height-1; row++)
  3572. for (col=1+(FC(row,2) & 1), c=FC(row,col+1); col < width-1; col+=2) {
  3573. pix = image + row*width+col;
  3574. for (i=0; (d=dir[i]) > 0; c=2-c, i++)
  3575. pix[0][c] = CLIP((pix[-d][c] + pix[d][c] + 2*pix[0][1]
  3576. - pix[-d][1] - pix[d][1]) >> 1);
  3577. }
  3578. /* Calculate blue for red pixels and vice versa: */
  3579. #ifdef LIBRAW_LIBRARY_BUILD
  3580. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,2,3);
  3581. #ifdef LIBRAW_USE_OPENMP
  3582. #pragma omp parallel for default(shared) private(guess, diff, row, col, d, c, i, pix) schedule(static)
  3583. #endif
  3584. #endif
  3585. for (row=1; row < height-1; row++)
  3586. for (col=1+(FC(row,1) & 1), c=2-FC(row,col); col < width-1; col+=2) {
  3587. pix = image + row*width+col;
  3588. for (i=0; (d=dir[i]+dir[i+1]) > 0; i++) {
  3589. diff[i] = ABS(pix[-d][c] - pix[d][c]) +
  3590. ABS(pix[-d][1] - pix[0][1]) +
  3591. ABS(pix[ d][1] - pix[0][1]);
  3592. guess[i] = pix[-d][c] + pix[d][c] + 2*pix[0][1]
  3593. - pix[-d][1] - pix[d][1];
  3594. }
  3595. if (diff[0] != diff[1])
  3596. pix[0][c] = CLIP(guess[diff[0] > diff[1]] >> 1);
  3597. else
  3598. pix[0][c] = CLIP((guess[0]+guess[1]) >> 2);
  3599. }
  3600. }
  3601. #ifdef LIBRAW_LIBRARY_BUILD
  3602. /*
  3603. Adaptive Homogeneity-Directed interpolation is based on
  3604. the work of Keigo Hirakawa, Thomas Parks, and Paul Lee.
  3605. */
  3606. #define TS 256 /* Tile Size */
  3607. static float dcraw_cbrt[0x10000] = {-1.0f};
  3608. static inline float calc_64cbrt(float f)
  3609. {
  3610. unsigned u;
  3611. static float lower = dcraw_cbrt[0];
  3612. static float upper = dcraw_cbrt[0xffff];
  3613. if (f <= 0) {
  3614. return lower;
  3615. }
  3616. u = (unsigned) f;
  3617. if (u >= 0xffff) {
  3618. return upper;
  3619. }
  3620. return dcraw_cbrt[u];
  3621. }
  3622. void CLASS ahd_interpolate_green_h_and_v(int top, int left, ushort (*out_rgb)[TS][TS][3])
  3623. {
  3624. int row, col;
  3625. int c, val;
  3626. ushort (*pix)[4];
  3627. const int rowlimit = MIN(top+TS, height-2);
  3628. const int collimit = MIN(left+TS, width-2);
  3629. for (row = top; row < rowlimit; row++) {
  3630. col = left + (FC(row,left) & 1);
  3631. for (c = FC(row,col); col < collimit; col+=2) {
  3632. pix = image + row*width+col;
  3633. val = ((pix[-1][1] + pix[0][c] + pix[1][1]) * 2
  3634. - pix[-2][c] - pix[2][c]) >> 2;
  3635. out_rgb[0][row-top][col-left][1] = ULIM(val,pix[-1][1],pix[1][1]);
  3636. val = ((pix[-width][1] + pix[0][c] + pix[width][1]) * 2
  3637. - pix[-2*width][c] - pix[2*width][c]) >> 2;
  3638. out_rgb[1][row-top][col-left][1] = ULIM(val,pix[-width][1],pix[width][1]);
  3639. }
  3640. }
  3641. }
  3642. 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])
  3643. {
  3644. unsigned row, col;
  3645. int c, val;
  3646. ushort (*pix)[4];
  3647. ushort (*rix)[3];
  3648. short (*lix)[3];
  3649. float xyz[3];
  3650. const unsigned num_pix_per_row = 4*width;
  3651. const unsigned rowlimit = MIN(top+TS-1, height-3);
  3652. const unsigned collimit = MIN(left+TS-1, width-3);
  3653. ushort *pix_above;
  3654. ushort *pix_below;
  3655. int t1, t2;
  3656. for (row = top+1; row < rowlimit; row++) {
  3657. pix = image + row*width + left;
  3658. rix = &inout_rgb[row-top][0];
  3659. lix = &out_lab[row-top][0];
  3660. for (col = left+1; col < collimit; col++) {
  3661. pix++;
  3662. pix_above = &pix[0][0] - num_pix_per_row;
  3663. pix_below = &pix[0][0] + num_pix_per_row;
  3664. rix++;
  3665. lix++;
  3666. c = 2 - FC(row, col);
  3667. if (c == 1) {
  3668. c = FC(row+1,col);
  3669. t1 = 2-c;
  3670. val = pix[0][1] + (( pix[-1][t1] + pix[1][t1]
  3671. - rix[-1][1] - rix[1][1] ) >> 1);
  3672. rix[0][t1] = CLIP(val);
  3673. val = pix[0][1] + (( pix_above[c] + pix_below[c]
  3674. - rix[-TS][1] - rix[TS][1] ) >> 1);
  3675. } else {
  3676. t1 = -4+c; /* -4+c: pixel of color c to the left */
  3677. t2 = 4+c; /* 4+c: pixel of color c to the right */
  3678. val = rix[0][1] + (( pix_above[t1] + pix_above[t2]
  3679. + pix_below[t1] + pix_below[t2]
  3680. - rix[-TS-1][1] - rix[-TS+1][1]
  3681. - rix[+TS-1][1] - rix[+TS+1][1] + 1) >> 2);
  3682. }
  3683. rix[0][c] = CLIP(val);
  3684. c = FC(row,col);
  3685. rix[0][c] = pix[0][c];
  3686. xyz[0] = xyz[1] = xyz[2] = 0.5;
  3687. FORC3 {
  3688. /*
  3689. * Technically this ought to be FORCC, but the rest of
  3690. * ahd_interpolate() assumes 3 colors so let's help the compiler.
  3691. */
  3692. xyz[0] += xyz_cam[0][c] * rix[0][c];
  3693. xyz[1] += xyz_cam[1][c] * rix[0][c];
  3694. xyz[2] += xyz_cam[2][c] * rix[0][c];
  3695. }
  3696. FORC3 {
  3697. xyz[c] = calc_64cbrt(xyz[c]);
  3698. }
  3699. lix[0][0] = (116 * xyz[1] - 16);
  3700. lix[0][1] = 500 * (xyz[0] - xyz[1]);
  3701. lix[0][2] = 200 * (xyz[1] - xyz[2]);
  3702. }
  3703. }
  3704. }
  3705. 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])
  3706. {
  3707. int direction;
  3708. for (direction = 0; direction < 2; direction++) {
  3709. ahd_interpolate_r_and_b_in_rgb_and_convert_to_cielab(top, left, inout_rgb[direction], out_lab[direction], xyz_cam);
  3710. }
  3711. }
  3712. void CLASS ahd_interpolate_build_homogeneity_map(int top, int left, short (*lab)[TS][TS][3], char (*out_homogeneity_map)[TS][2])
  3713. {
  3714. int row, col;
  3715. int tr, tc;
  3716. int direction;
  3717. int i;
  3718. short (*lix)[3];
  3719. short (*lixs[2])[3];
  3720. short *adjacent_lix;
  3721. unsigned ldiff[2][4], abdiff[2][4], leps, abeps;
  3722. static const int dir[4] = { -1, 1, -TS, TS };
  3723. const int rowlimit = MIN(top+TS-2, height-4);
  3724. const int collimit = MIN(left+TS-2, width-4);
  3725. int homogeneity;
  3726. char (*homogeneity_map_p)[2];
  3727. memset (out_homogeneity_map, 0, 2*TS*TS);
  3728. for (row=top+2; row < rowlimit; row++) {
  3729. tr = row-top;
  3730. homogeneity_map_p = &out_homogeneity_map[tr][1];
  3731. for (direction=0; direction < 2; direction++) {
  3732. lixs[direction] = &lab[direction][tr][1];
  3733. }
  3734. for (col=left+2; col < collimit; col++) {
  3735. tc = col-left;
  3736. homogeneity_map_p++;
  3737. for (direction=0; direction < 2; direction++) {
  3738. lix = ++lixs[direction];
  3739. for (i=0; i < 4; i++) {
  3740. adjacent_lix = lix[dir[i]];
  3741. ldiff[direction][i] = ABS(lix[0][0]-adjacent_lix[0]);
  3742. abdiff[direction][i] = SQR(lix[0][1]-adjacent_lix[1])
  3743. + SQR(lix[0][2]-adjacent_lix[2]);
  3744. }
  3745. }
  3746. leps = MIN(MAX(ldiff[0][0],ldiff[0][1]),
  3747. MAX(ldiff[1][2],ldiff[1][3]));
  3748. abeps = MIN(MAX(abdiff[0][0],abdiff[0][1]),
  3749. MAX(abdiff[1][2],abdiff[1][3]));
  3750. for (direction=0; direction < 2; direction++) {
  3751. homogeneity = 0;
  3752. for (i=0; i < 4; i++) {
  3753. if (ldiff[direction][i] <= leps && abdiff[direction][i] <= abeps) {
  3754. homogeneity++;
  3755. }
  3756. }
  3757. homogeneity_map_p[0][direction] = homogeneity;
  3758. }
  3759. }
  3760. }
  3761. }
  3762. void CLASS ahd_interpolate_combine_homogeneous_pixels(int top, int left, ushort (*rgb)[TS][TS][3], char (*homogeneity_map)[TS][2])
  3763. {
  3764. int row, col;
  3765. int tr, tc;
  3766. int i, j;
  3767. int direction;
  3768. int hm[2];
  3769. int c;
  3770. const int rowlimit = MIN(top+TS-3, height-5);
  3771. const int collimit = MIN(left+TS-3, width-5);
  3772. ushort (*pix)[4];
  3773. ushort (*rix[2])[3];
  3774. for (row=top+3; row < rowlimit; row++) {
  3775. tr = row-top;
  3776. pix = &image[row*width+left+2];
  3777. for (direction = 0; direction < 2; direction++) {
  3778. rix[direction] = &rgb[direction][tr][2];
  3779. }
  3780. for (col=left+3; col < collimit; col++) {
  3781. tc = col-left;
  3782. pix++;
  3783. for (direction = 0; direction < 2; direction++) {
  3784. rix[direction]++;
  3785. }
  3786. for (direction=0; direction < 2; direction++) {
  3787. hm[direction] = 0;
  3788. for (i=tr-1; i <= tr+1; i++) {
  3789. for (j=tc-1; j <= tc+1; j++) {
  3790. hm[direction] += homogeneity_map[i][j][direction];
  3791. }
  3792. }
  3793. }
  3794. if (hm[0] != hm[1]) {
  3795. memcpy(pix[0], rix[hm[1] > hm[0]][0], 3 * sizeof(ushort));
  3796. } else {
  3797. FORC3 {
  3798. pix[0][c] = (rix[0][0][c] + rix[1][0][c]) >> 1;
  3799. }
  3800. }
  3801. }
  3802. }
  3803. }
  3804. void CLASS ahd_interpolate()
  3805. {
  3806. int i, j, k, top, left;
  3807. float xyz_cam[3][4],r;
  3808. char *buffer;
  3809. ushort (*rgb)[TS][TS][3];
  3810. short (*lab)[TS][TS][3];
  3811. char (*homo)[TS][2];
  3812. int terminate_flag = 0;
  3813. if(dcraw_cbrt[0]<-0.1){
  3814. for (i=0x10000-1; i >=0; i--) {
  3815. r = i / 65535.0;
  3816. dcraw_cbrt[i] = 64.0*(r > 0.008856 ? pow((double)r,1/3.0) : 7.787*r + 16/116.0);
  3817. }
  3818. }
  3819. #ifdef DCRAW_VERBOSE
  3820. if (verbose) fprintf (stderr,_("AHD interpolation...\n"));
  3821. #endif
  3822. for (i=0; i < 3; i++) {
  3823. for (j=0; j < colors; j++) {
  3824. xyz_cam[i][j] = 0;
  3825. for (k=0; k < 3; k++) {
  3826. xyz_cam[i][j] += xyz_rgb[i][k] * rgb_cam[k][j] / d65_white[i];
  3827. }
  3828. }
  3829. }
  3830. border_interpolate(5);
  3831. #ifdef LIBRAW_LIBRARY_BUILD
  3832. #ifdef LIBRAW_USE_OPENMP
  3833. #pragma omp parallel private(buffer,rgb,lab,homo,top,left,i,j,k) shared(xyz_cam,terminate_flag)
  3834. #endif
  3835. #endif
  3836. {
  3837. buffer = (char *) malloc (26*TS*TS); /* 1664 kB */
  3838. merror (buffer, "ahd_interpolate()");
  3839. rgb = (ushort(*)[TS][TS][3]) buffer;
  3840. lab = (short (*)[TS][TS][3])(buffer + 12*TS*TS);
  3841. homo = (char (*)[TS][2]) (buffer + 24*TS*TS);
  3842. #ifdef LIBRAW_LIBRARY_BUILD
  3843. #ifdef LIBRAW_USE_OPENMP
  3844. #pragma omp for schedule(dynamic)
  3845. #endif
  3846. #endif
  3847. for (top=2; top < height-5; top += TS-6){
  3848. #ifdef LIBRAW_LIBRARY_BUILD
  3849. #ifdef LIBRAW_USE_OPENMP
  3850. if(0== omp_get_thread_num())
  3851. #endif
  3852. if(callbacks.progress_cb) {
  3853. int rr = (*callbacks.progress_cb)(callbacks.progresscb_data,LIBRAW_PROGRESS_INTERPOLATE,top-2,height-7);
  3854. if(rr)
  3855. terminate_flag = 1;
  3856. }
  3857. #endif
  3858. for (left=2; !terminate_flag && (left < width-5); left += TS-6) {
  3859. ahd_interpolate_green_h_and_v(top, left, rgb);
  3860. ahd_interpolate_r_and_b_and_convert_to_cielab(top, left, rgb, lab, xyz_cam);
  3861. ahd_interpolate_build_homogeneity_map(top, left, lab, homo);
  3862. ahd_interpolate_combine_homogeneous_pixels(top, left, rgb, homo);
  3863. }
  3864. }
  3865. free (buffer);
  3866. }
  3867. #ifdef LIBRAW_LIBRARY_BUILD
  3868. if(terminate_flag)
  3869. throw LIBRAW_EXCEPTION_CANCELLED_BY_CALLBACK;
  3870. #endif
  3871. }
  3872. #undef TS
  3873. #else
  3874. /*
  3875. Adaptive Homogeneity-Directed interpolation is based on
  3876. the work of Keigo Hirakawa, Thomas Parks, and Paul Lee.
  3877. */
  3878. #define TS 256 /* Tile Size */
  3879. void CLASS ahd_interpolate()
  3880. {
  3881. int i, j, k, top, left, row, col, tr, tc, c, d, val, hm[2];
  3882. ushort (*pix)[4], (*rix)[3];
  3883. static const int dir[4] = { -1, 1, -TS, TS };
  3884. unsigned ldiff[2][4], abdiff[2][4], leps, abeps;
  3885. float r, cbrt[0x10000], xyz[3], xyz_cam[3][4];
  3886. ushort (*rgb)[TS][TS][3];
  3887. short (*lab)[TS][TS][3], (*lix)[3];
  3888. char (*homo)[TS][TS], *buffer;
  3889. #ifdef DCRAW_VERBOSE
  3890. if (verbose) fprintf (stderr,_("AHD interpolation...\n"));
  3891. #endif
  3892. for (i=0; i < 0x10000; i++) {
  3893. r = i / 65535.0;
  3894. cbrt[i] = r > 0.008856 ? pow((double)r,(double)(1/3.0)) : 7.787*r + 16/116.0;
  3895. }
  3896. for (i=0; i < 3; i++)
  3897. for (j=0; j < colors; j++)
  3898. for (xyz_cam[i][j] = k=0; k < 3; k++)
  3899. xyz_cam[i][j] += xyz_rgb[i][k] * rgb_cam[k][j] / d65_white[i];
  3900. border_interpolate(5);
  3901. buffer = (char *) malloc (26*TS*TS); /* 1664 kB */
  3902. merror (buffer, "ahd_interpolate()");
  3903. rgb = (ushort(*)[TS][TS][3]) buffer;
  3904. lab = (short (*)[TS][TS][3])(buffer + 12*TS*TS);
  3905. homo = (char (*)[TS][TS]) (buffer + 24*TS*TS);
  3906. for (top=2; top < height-5; top += TS-6)
  3907. for (left=2; left < width-5; left += TS-6) {
  3908. /* Interpolate green horizontally and vertically: */
  3909. for (row = top; row < top+TS && row < height-2; row++) {
  3910. col = left + (FC(row,left) & 1);
  3911. for (c = FC(row,col); col < left+TS && col < width-2; col+=2) {
  3912. pix = image + row*width+col;
  3913. val = ((pix[-1][1] + pix[0][c] + pix[1][1]) * 2
  3914. - pix[-2][c] - pix[2][c]) >> 2;
  3915. rgb[0][row-top][col-left][1] = ULIM(val,pix[-1][1],pix[1][1]);
  3916. val = ((pix[-width][1] + pix[0][c] + pix[width][1]) * 2
  3917. - pix[-2*width][c] - pix[2*width][c]) >> 2;
  3918. rgb[1][row-top][col-left][1] = ULIM(val,pix[-width][1],pix[width][1]);
  3919. }
  3920. }
  3921. /* Interpolate red and blue, and convert to CIELab: */
  3922. for (d=0; d < 2; d++)
  3923. for (row=top+1; row < top+TS-1 && row < height-3; row++)
  3924. for (col=left+1; col < left+TS-1 && col < width-3; col++) {
  3925. pix = image + row*width+col;
  3926. rix = &rgb[d][row-top][col-left];
  3927. lix = &lab[d][row-top][col-left];
  3928. if ((c = 2 - FC(row,col)) == 1) {
  3929. c = FC(row+1,col);
  3930. val = pix[0][1] + (( pix[-1][2-c] + pix[1][2-c]
  3931. - rix[-1][1] - rix[1][1] ) >> 1);
  3932. rix[0][2-c] = CLIP(val);
  3933. val = pix[0][1] + (( pix[-width][c] + pix[width][c]
  3934. - rix[-TS][1] - rix[TS][1] ) >> 1);
  3935. } else
  3936. val = rix[0][1] + (( pix[-width-1][c] + pix[-width+1][c]
  3937. + pix[+width-1][c] + pix[+width+1][c]
  3938. - rix[-TS-1][1] - rix[-TS+1][1]
  3939. - rix[+TS-1][1] - rix[+TS+1][1] + 1) >> 2);
  3940. rix[0][c] = CLIP(val);
  3941. c = FC(row,col);
  3942. rix[0][c] = pix[0][c];
  3943. xyz[0] = xyz[1] = xyz[2] = 0.5;
  3944. FORCC {
  3945. xyz[0] += xyz_cam[0][c] * rix[0][c];
  3946. xyz[1] += xyz_cam[1][c] * rix[0][c];
  3947. xyz[2] += xyz_cam[2][c] * rix[0][c];
  3948. }
  3949. xyz[0] = cbrt[CLIP((int) xyz[0])];
  3950. xyz[1] = cbrt[CLIP((int) xyz[1])];
  3951. xyz[2] = cbrt[CLIP((int) xyz[2])];
  3952. lix[0][0] = 64 * (116 * xyz[1] - 16);
  3953. lix[0][1] = 64 * 500 * (xyz[0] - xyz[1]);
  3954. lix[0][2] = 64 * 200 * (xyz[1] - xyz[2]);
  3955. }
  3956. /* Build homogeneity maps from the CIELab images: */
  3957. memset (homo, 0, 2*TS*TS);
  3958. for (row=top+2; row < top+TS-2 && row < height-4; row++) {
  3959. tr = row-top;
  3960. for (col=left+2; col < left+TS-2 && col < width-4; col++) {
  3961. tc = col-left;
  3962. for (d=0; d < 2; d++) {
  3963. lix = &lab[d][tr][tc];
  3964. for (i=0; i < 4; i++) {
  3965. ldiff[d][i] = ABS(lix[0][0]-lix[dir[i]][0]);
  3966. abdiff[d][i] = SQR(lix[0][1]-lix[dir[i]][1])
  3967. + SQR(lix[0][2]-lix[dir[i]][2]);
  3968. }
  3969. }
  3970. leps = MIN(MAX(ldiff[0][0],ldiff[0][1]),
  3971. MAX(ldiff[1][2],ldiff[1][3]));
  3972. abeps = MIN(MAX(abdiff[0][0],abdiff[0][1]),
  3973. MAX(abdiff[1][2],abdiff[1][3]));
  3974. for (d=0; d < 2; d++)
  3975. for (i=0; i < 4; i++)
  3976. if (ldiff[d][i] <= leps && abdiff[d][i] <= abeps)
  3977. homo[d][tr][tc]++;
  3978. }
  3979. }
  3980. /* Combine the most homogenous pixels for the final result: */
  3981. for (row=top+3; row < top+TS-3 && row < height-5; row++) {
  3982. tr = row-top;
  3983. for (col=left+3; col < left+TS-3 && col < width-5; col++) {
  3984. tc = col-left;
  3985. for (d=0; d < 2; d++)
  3986. for (hm[d]=0, i=tr-1; i <= tr+1; i++)
  3987. for (j=tc-1; j <= tc+1; j++)
  3988. hm[d] += homo[d][i][j];
  3989. if (hm[0] != hm[1])
  3990. FORC3 image[row*width+col][c] = rgb[hm[1] > hm[0]][tr][tc][c];
  3991. else
  3992. FORC3 image[row*width+col][c] =
  3993. (rgb[0][tr][tc][c] + rgb[1][tr][tc][c]) >> 1;
  3994. }
  3995. }
  3996. }
  3997. free (buffer);
  3998. }
  3999. #undef TS
  4000. #endif
  4001. void CLASS median_filter()
  4002. {
  4003. ushort (*pix)[4];
  4004. int pass, c, i, j, k, med[9];
  4005. static const uchar opt[] = /* Optimal 9-element median search */
  4006. { 1,2, 4,5, 7,8, 0,1, 3,4, 6,7, 1,2, 4,5, 7,8,
  4007. 0,3, 5,8, 4,7, 3,6, 1,4, 2,5, 4,7, 4,2, 6,4, 4,2 };
  4008. for (pass=1; pass <= med_passes; pass++) {
  4009. #ifdef LIBRAW_LIBRARY_BUILD
  4010. RUN_CALLBACK(LIBRAW_PROGRESS_MEDIAN_FILTER,pass-1,med_passes);
  4011. #endif
  4012. #ifdef DCRAW_VERBOSE
  4013. if (verbose)
  4014. fprintf (stderr,_("Median filter pass %d...\n"), pass);
  4015. #endif
  4016. for (c=0; c < 3; c+=2) {
  4017. for (pix = image; pix < image+width*height; pix++)
  4018. pix[0][3] = pix[0][c];
  4019. for (pix = image+width; pix < image+width*(height-1); pix++) {
  4020. if ((pix-image+1) % width < 2) continue;
  4021. for (k=0, i = -width; i <= width; i += width)
  4022. for (j = i-1; j <= i+1; j++)
  4023. med[k++] = pix[j][3] - pix[j][1];
  4024. for (i=0; i < sizeof opt; i+=2)
  4025. if (med[opt[i]] > med[opt[i+1]])
  4026. SWAP (med[opt[i]] , med[opt[i+1]]);
  4027. pix[0][c] = CLIP(med[4] + pix[0][1]);
  4028. }
  4029. }
  4030. }
  4031. }
  4032. void CLASS blend_highlights()
  4033. {
  4034. int clip=INT_MAX, row, col, c, i, j;
  4035. static const float trans[2][4][4] =
  4036. { { { 1,1,1 }, { 1.7320508,-1.7320508,0 }, { -1,-1,2 } },
  4037. { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } };
  4038. static const float itrans[2][4][4] =
  4039. { { { 1,0.8660254,-0.5 }, { 1,-0.8660254,-0.5 }, { 1,0,1 } },
  4040. { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } };
  4041. float cam[2][4], lab[2][4], sum[2], chratio;
  4042. if ((unsigned) (colors-3) > 1) return;
  4043. #ifdef DCRAW_VERBOSE
  4044. if (verbose) fprintf (stderr,_("Blending highlights...\n"));
  4045. #endif
  4046. #ifdef LIBRAW_LIBRARY_BUILD
  4047. RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS,0,2);
  4048. #endif
  4049. FORCC if (clip > (i = 65535*pre_mul[c])) clip = i;
  4050. for (row=0; row < height; row++)
  4051. for (col=0; col < width; col++) {
  4052. FORCC if (image[row*width+col][c] > clip) break;
  4053. if (c == colors) continue;
  4054. FORCC {
  4055. cam[0][c] = image[row*width+col][c];
  4056. cam[1][c] = MIN(cam[0][c],clip);
  4057. }
  4058. for (i=0; i < 2; i++) {
  4059. FORCC for (lab[i][c]=j=0; j < colors; j++)
  4060. lab[i][c] += trans[colors-3][c][j] * cam[i][j];
  4061. for (sum[i]=0,c=1; c < colors; c++)
  4062. sum[i] += SQR(lab[i][c]);
  4063. }
  4064. chratio = sqrt(sum[1]/sum[0]);
  4065. for (c=1; c < colors; c++)
  4066. lab[0][c] *= chratio;
  4067. FORCC for (cam[0][c]=j=0; j < colors; j++)
  4068. cam[0][c] += itrans[colors-3][c][j] * lab[0][j];
  4069. FORCC image[row*width+col][c] = cam[0][c] / colors;
  4070. }
  4071. #ifdef LIBRAW_LIBRARY_BUILD
  4072. RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS,1,2);
  4073. #endif
  4074. }
  4075. #define SCALE (4 >> shrink)
  4076. void CLASS recover_highlights()
  4077. {
  4078. float *map, sum, wgt, grow;
  4079. int hsat[4], count, spread, change, val, i;
  4080. unsigned high, wide, mrow, mcol, row, col, kc, c, d, y, x;
  4081. ushort *pixel;
  4082. static const signed char dir[8][2] =
  4083. { {-1,-1}, {-1,0}, {-1,1}, {0,1}, {1,1}, {1,0}, {1,-1}, {0,-1} };
  4084. #ifdef DCRAW_VERBOSE
  4085. if (verbose) fprintf (stderr,_("Rebuilding highlights...\n"));
  4086. #endif
  4087. grow = pow (2.0, 4-highlight);
  4088. FORCC hsat[c] = 32000 * pre_mul[c];
  4089. for (kc=0, c=1; c < colors; c++)
  4090. if (pre_mul[kc] < pre_mul[c]) kc = c;
  4091. high = height / SCALE;
  4092. wide = width / SCALE;
  4093. map = (float *) calloc (high*wide, sizeof *map);
  4094. merror (map, "recover_highlights()");
  4095. FORCC if (c != kc) {
  4096. #ifdef LIBRAW_LIBRARY_BUILD
  4097. RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS,c-1,colors-1);
  4098. #endif
  4099. memset (map, 0, high*wide*sizeof *map);
  4100. for (mrow=0; mrow < high; mrow++)
  4101. for (mcol=0; mcol < wide; mcol++) {
  4102. sum = wgt = count = 0;
  4103. for (row = mrow*SCALE; row < (mrow+1)*SCALE; row++)
  4104. for (col = mcol*SCALE; col < (mcol+1)*SCALE; col++) {
  4105. pixel = image[row*width+col];
  4106. if (pixel[c] / hsat[c] == 1 && pixel[kc] > 24000) {
  4107. sum += pixel[c];
  4108. wgt += pixel[kc];
  4109. count++;
  4110. }
  4111. }
  4112. if (count == SCALE*SCALE)
  4113. map[mrow*wide+mcol] = sum / wgt;
  4114. }
  4115. for (spread = 32/grow; spread--; ) {
  4116. for (mrow=0; mrow < high; mrow++)
  4117. for (mcol=0; mcol < wide; mcol++) {
  4118. if (map[mrow*wide+mcol]) continue;
  4119. sum = count = 0;
  4120. for (d=0; d < 8; d++) {
  4121. y = mrow + dir[d][0];
  4122. x = mcol + dir[d][1];
  4123. if (y < high && x < wide && map[y*wide+x] > 0) {
  4124. sum += (1 + (d & 1)) * map[y*wide+x];
  4125. count += 1 + (d & 1);
  4126. }
  4127. }
  4128. if (count > 3)
  4129. map[mrow*wide+mcol] = - (sum+grow) / (count+grow);
  4130. }
  4131. for (change=i=0; i < high*wide; i++)
  4132. if (map[i] < 0) {
  4133. map[i] = -map[i];
  4134. change = 1;
  4135. }
  4136. if (!change) break;
  4137. }
  4138. for (i=0; i < high*wide; i++)
  4139. if (map[i] == 0) map[i] = 1;
  4140. for (mrow=0; mrow < high; mrow++)
  4141. for (mcol=0; mcol < wide; mcol++) {
  4142. for (row = mrow*SCALE; row < (mrow+1)*SCALE; row++)
  4143. for (col = mcol*SCALE; col < (mcol+1)*SCALE; col++) {
  4144. pixel = image[row*width+col];
  4145. if (pixel[c] / hsat[c] > 1) {
  4146. val = pixel[kc] * map[mrow*wide+mcol];
  4147. if (pixel[c] < val) pixel[c] = CLIP(val);
  4148. }
  4149. }
  4150. }
  4151. }
  4152. free (map);
  4153. }
  4154. #undef SCALE
  4155. void CLASS tiff_get (unsigned base,
  4156. unsigned *tag, unsigned *type, unsigned *len, unsigned *save)
  4157. {
  4158. *tag = get2();
  4159. *type = get2();
  4160. *len = get4();
  4161. *save = ftell(ifp) + 4;
  4162. if (*len * ("11124811248488"[*type < 14 ? *type:0]-'0') > 4)
  4163. fseek (ifp, get4()+base, SEEK_SET);
  4164. }
  4165. void CLASS parse_thumb_note (int base, unsigned toff, unsigned tlen)
  4166. {
  4167. unsigned entries, tag, type, len, save;
  4168. entries = get2();
  4169. while (entries--) {
  4170. tiff_get (base, &tag, &type, &len, &save);
  4171. if (tag == toff) thumb_offset = get4()+base;
  4172. if (tag == tlen) thumb_length = get4();
  4173. fseek (ifp, save, SEEK_SET);
  4174. }
  4175. }
  4176. void CLASS parse_makernote (int base, int uptag)
  4177. {
  4178. static const uchar xlat[2][256] = {
  4179. { 0xc1,0xbf,0x6d,0x0d,0x59,0xc5,0x13,0x9d,0x83,0x61,0x6b,0x4f,0xc7,0x7f,0x3d,0x3d,
  4180. 0x53,0x59,0xe3,0xc7,0xe9,0x2f,0x95,0xa7,0x95,0x1f,0xdf,0x7f,0x2b,0x29,0xc7,0x0d,
  4181. 0xdf,0x07,0xef,0x71,0x89,0x3d,0x13,0x3d,0x3b,0x13,0xfb,0x0d,0x89,0xc1,0x65,0x1f,
  4182. 0xb3,0x0d,0x6b,0x29,0xe3,0xfb,0xef,0xa3,0x6b,0x47,0x7f,0x95,0x35,0xa7,0x47,0x4f,
  4183. 0xc7,0xf1,0x59,0x95,0x35,0x11,0x29,0x61,0xf1,0x3d,0xb3,0x2b,0x0d,0x43,0x89,0xc1,
  4184. 0x9d,0x9d,0x89,0x65,0xf1,0xe9,0xdf,0xbf,0x3d,0x7f,0x53,0x97,0xe5,0xe9,0x95,0x17,
  4185. 0x1d,0x3d,0x8b,0xfb,0xc7,0xe3,0x67,0xa7,0x07,0xf1,0x71,0xa7,0x53,0xb5,0x29,0x89,
  4186. 0xe5,0x2b,0xa7,0x17,0x29,0xe9,0x4f,0xc5,0x65,0x6d,0x6b,0xef,0x0d,0x89,0x49,0x2f,
  4187. 0xb3,0x43,0x53,0x65,0x1d,0x49,0xa3,0x13,0x89,0x59,0xef,0x6b,0xef,0x65,0x1d,0x0b,
  4188. 0x59,0x13,0xe3,0x4f,0x9d,0xb3,0x29,0x43,0x2b,0x07,0x1d,0x95,0x59,0x59,0x47,0xfb,
  4189. 0xe5,0xe9,0x61,0x47,0x2f,0x35,0x7f,0x17,0x7f,0xef,0x7f,0x95,0x95,0x71,0xd3,0xa3,
  4190. 0x0b,0x71,0xa3,0xad,0x0b,0x3b,0xb5,0xfb,0xa3,0xbf,0x4f,0x83,0x1d,0xad,0xe9,0x2f,
  4191. 0x71,0x65,0xa3,0xe5,0x07,0x35,0x3d,0x0d,0xb5,0xe9,0xe5,0x47,0x3b,0x9d,0xef,0x35,
  4192. 0xa3,0xbf,0xb3,0xdf,0x53,0xd3,0x97,0x53,0x49,0x71,0x07,0x35,0x61,0x71,0x2f,0x43,
  4193. 0x2f,0x11,0xdf,0x17,0x97,0xfb,0x95,0x3b,0x7f,0x6b,0xd3,0x25,0xbf,0xad,0xc7,0xc5,
  4194. 0xc5,0xb5,0x8b,0xef,0x2f,0xd3,0x07,0x6b,0x25,0x49,0x95,0x25,0x49,0x6d,0x71,0xc7 },
  4195. { 0xa7,0xbc,0xc9,0xad,0x91,0xdf,0x85,0xe5,0xd4,0x78,0xd5,0x17,0x46,0x7c,0x29,0x4c,
  4196. 0x4d,0x03,0xe9,0x25,0x68,0x11,0x86,0xb3,0xbd,0xf7,0x6f,0x61,0x22,0xa2,0x26,0x34,
  4197. 0x2a,0xbe,0x1e,0x46,0x14,0x68,0x9d,0x44,0x18,0xc2,0x40,0xf4,0x7e,0x5f,0x1b,0xad,
  4198. 0x0b,0x94,0xb6,0x67,0xb4,0x0b,0xe1,0xea,0x95,0x9c,0x66,0xdc,0xe7,0x5d,0x6c,0x05,
  4199. 0xda,0xd5,0xdf,0x7a,0xef,0xf6,0xdb,0x1f,0x82,0x4c,0xc0,0x68,0x47,0xa1,0xbd,0xee,
  4200. 0x39,0x50,0x56,0x4a,0xdd,0xdf,0xa5,0xf8,0xc6,0xda,0xca,0x90,0xca,0x01,0x42,0x9d,
  4201. 0x8b,0x0c,0x73,0x43,0x75,0x05,0x94,0xde,0x24,0xb3,0x80,0x34,0xe5,0x2c,0xdc,0x9b,
  4202. 0x3f,0xca,0x33,0x45,0xd0,0xdb,0x5f,0xf5,0x52,0xc3,0x21,0xda,0xe2,0x22,0x72,0x6b,
  4203. 0x3e,0xd0,0x5b,0xa8,0x87,0x8c,0x06,0x5d,0x0f,0xdd,0x09,0x19,0x93,0xd0,0xb9,0xfc,
  4204. 0x8b,0x0f,0x84,0x60,0x33,0x1c,0x9b,0x45,0xf1,0xf0,0xa3,0x94,0x3a,0x12,0x77,0x33,
  4205. 0x4d,0x44,0x78,0x28,0x3c,0x9e,0xfd,0x65,0x57,0x16,0x94,0x6b,0xfb,0x59,0xd0,0xc8,
  4206. 0x22,0x36,0xdb,0xd2,0x63,0x98,0x43,0xa1,0x04,0x87,0x86,0xf7,0xa6,0x26,0xbb,0xd6,
  4207. 0x59,0x4d,0xbf,0x6a,0x2e,0xaa,0x2b,0xef,0xe6,0x78,0xb6,0x4e,0xe0,0x2f,0xdc,0x7c,
  4208. 0xbe,0x57,0x19,0x32,0x7e,0x2a,0xd0,0xb8,0xba,0x29,0x00,0x3c,0x52,0x7d,0xa8,0x49,
  4209. 0x3b,0x2d,0xeb,0x25,0x49,0xfa,0xa3,0xaa,0x39,0xa7,0xc5,0xa7,0x50,0x11,0x36,0xfb,
  4210. 0xc6,0x67,0x4a,0xf5,0xa5,0x12,0x65,0x7e,0xb0,0xdf,0xaf,0x4e,0xb3,0x61,0x7f,0x2f } };
  4211. unsigned offset=0, entries, tag, type, len, save, c;
  4212. unsigned ver97=0, serial=0, i, wbi=0, wb[4]={0,0,0,0};
  4213. uchar buf97[324], ci, cj, ck;
  4214. short morder, sorder=order;
  4215. char buf[10];
  4216. /*
  4217. The MakerNote might have its own TIFF header (possibly with
  4218. its own byte-order!), or it might just be a table.
  4219. */
  4220. if (!strcmp(make,"Nokia")) return;
  4221. fread (buf, 1, 10, ifp);
  4222. if (!strncmp (buf,"KDK" ,3) || /* these aren't TIFF tables */
  4223. !strncmp (buf,"VER" ,3) ||
  4224. !strncmp (buf,"IIII",4) ||
  4225. !strncmp (buf,"MMMM",4)) return;
  4226. if (!strncmp (buf,"KC" ,2) || /* Konica KD-400Z, KD-510Z */
  4227. !strncmp (buf,"MLY" ,3)) { /* Minolta DiMAGE G series */
  4228. order = 0x4d4d;
  4229. while ((i=ftell(ifp)) < data_offset && i < 16384) {
  4230. wb[0] = wb[2]; wb[2] = wb[1]; wb[1] = wb[3];
  4231. wb[3] = get2();
  4232. if (wb[1] == 256 && wb[3] == 256 &&
  4233. wb[0] > 256 && wb[0] < 640 && wb[2] > 256 && wb[2] < 640)
  4234. FORC4 cam_mul[c] = wb[c];
  4235. }
  4236. goto quit;
  4237. }
  4238. if (!strcmp (buf,"Nikon")) {
  4239. base = ftell(ifp);
  4240. order = get2();
  4241. if (get2() != 42) goto quit;
  4242. offset = get4();
  4243. fseek (ifp, offset-8, SEEK_CUR);
  4244. } else if (!strcmp (buf,"OLYMPUS")) {
  4245. base = ftell(ifp)-10;
  4246. fseek (ifp, -2, SEEK_CUR);
  4247. order = get2(); get2();
  4248. } else if (!strncmp (buf,"SONY",4) ||
  4249. !strcmp (buf,"Panasonic")) {
  4250. goto nf;
  4251. } else if (!strncmp (buf,"FUJIFILM",8)) {
  4252. base = ftell(ifp)-10;
  4253. nf: order = 0x4949;
  4254. fseek (ifp, 2, SEEK_CUR);
  4255. } else if (!strcmp (buf,"OLYMP") ||
  4256. !strcmp (buf,"LEICA") ||
  4257. !strcmp (buf,"Ricoh") ||
  4258. !strcmp (buf,"EPSON"))
  4259. fseek (ifp, -2, SEEK_CUR);
  4260. else if (!strcmp (buf,"AOC") ||
  4261. !strcmp (buf,"QVC"))
  4262. fseek (ifp, -4, SEEK_CUR);
  4263. else {
  4264. fseek (ifp, -10, SEEK_CUR);
  4265. if (!strncmp(make,"SAMSUNG",7))
  4266. base = ftell(ifp);
  4267. }
  4268. entries = get2();
  4269. if (entries > 1000) return;
  4270. morder = order;
  4271. while (entries--) {
  4272. order = morder;
  4273. tiff_get (base, &tag, &type, &len, &save);
  4274. tag |= uptag << 16;
  4275. if (tag == 2 && strstr(make,"NIKON") && !iso_speed)
  4276. iso_speed = (get2(),get2());
  4277. if (tag == 4 && len > 26 && len < 35) {
  4278. if ((i=(get4(),get2())) != 0x7fff && !iso_speed)
  4279. iso_speed = 50 * pow (2.0, i/32.0 - 4);
  4280. if ((i=(get2(),get2())) != 0x7fff && !aperture)
  4281. aperture = pow (2.0, i/64.0);
  4282. if ((i=get2()) != 0xffff && !shutter)
  4283. shutter = pow (2.0, (short) i/-32.0);
  4284. wbi = (get2(),get2());
  4285. shot_order = (get2(),get2());
  4286. }
  4287. if ((tag == 4 || tag == 0x114) && !strncmp(make,"KONICA",6)) {
  4288. fseek (ifp, tag == 4 ? 140:160, SEEK_CUR);
  4289. switch (get2()) {
  4290. case 72: flip = 0; break;
  4291. case 76: flip = 6; break;
  4292. case 82: flip = 5; break;
  4293. }
  4294. }
  4295. if (tag == 7 && type == 2 && len > 20)
  4296. fgets (model2, 64, ifp);
  4297. if (tag == 8 && type == 4)
  4298. shot_order = get4();
  4299. if (tag == 9 && !strcmp(make,"Canon"))
  4300. fread (artist, 64, 1, ifp);
  4301. if (tag == 0xc && len == 4) {
  4302. cam_mul[0] = getreal(type);
  4303. cam_mul[2] = getreal(type);
  4304. }
  4305. if (tag == 0xd && type == 7 && get2() == 0xaaaa) {
  4306. for (c=i=2; (ushort) c != 0xbbbb && i < len; i++)
  4307. c = c << 8 | fgetc(ifp);
  4308. while ((i+=4) < len-5)
  4309. if (get4() == 257 && (i=len) && (c = (get4(),fgetc(ifp))) < 3)
  4310. flip = "065"[c]-'0';
  4311. }
  4312. if (tag == 0x10 && type == 4)
  4313. unique_id = get4();
  4314. if (tag == 0x11 && is_raw && !strncmp(make,"NIKON",5)) {
  4315. fseek (ifp, get4()+base, SEEK_SET);
  4316. parse_tiff_ifd (base);
  4317. }
  4318. if (tag == 0x14 && type == 7) {
  4319. if (len == 2560) {
  4320. fseek (ifp, 1248, SEEK_CUR);
  4321. goto get2_256;
  4322. }
  4323. fread (buf, 1, 10, ifp);
  4324. if (!strncmp(buf,"NRW ",4)) {
  4325. fseek (ifp, strcmp(buf+4,"0100") ? 46:1546, SEEK_CUR);
  4326. cam_mul[0] = get4() << 2;
  4327. cam_mul[1] = get4() + get4();
  4328. cam_mul[2] = get4() << 2;
  4329. }
  4330. }
  4331. if (tag == 0x15 && type == 2 && is_raw)
  4332. fread (model, 64, 1, ifp);
  4333. if (strstr(make,"PENTAX")) {
  4334. if (tag == 0x1b) tag = 0x1018;
  4335. if (tag == 0x1c) tag = 0x1017;
  4336. }
  4337. if (tag == 0x1d)
  4338. while ((c = fgetc(ifp)) && c != EOF)
  4339. serial = serial*10 + (isdigit(c) ? c - '0' : c % 10);
  4340. if (tag == 0x81 && type == 4) {
  4341. data_offset = get4();
  4342. fseek (ifp, data_offset + 41, SEEK_SET);
  4343. raw_height = get2() * 2;
  4344. raw_width = get2();
  4345. filters = 0x61616161;
  4346. }
  4347. if (tag == 0x29 && type == 1) {
  4348. c = wbi < 18 ? "012347800000005896"[wbi]-'0' : 0;
  4349. fseek (ifp, 8 + c*32, SEEK_CUR);
  4350. FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get4();
  4351. }
  4352. if ((tag == 0x81 && type == 7) ||
  4353. (tag == 0x100 && type == 7) ||
  4354. (tag == 0x280 && type == 1)) {
  4355. thumb_offset = ftell(ifp);
  4356. thumb_length = len;
  4357. }
  4358. if (tag == 0x88 && type == 4 && (thumb_offset = get4()))
  4359. thumb_offset += base;
  4360. if (tag == 0x89 && type == 4)
  4361. thumb_length = get4();
  4362. if (tag == 0x8c || tag == 0x96)
  4363. meta_offset = ftell(ifp);
  4364. if (tag == 0x97) {
  4365. for (i=0; i < 4; i++)
  4366. ver97 = ver97 * 10 + fgetc(ifp)-'0';
  4367. switch (ver97) {
  4368. case 100:
  4369. fseek (ifp, 68, SEEK_CUR);
  4370. FORC4 cam_mul[(c >> 1) | ((c & 1) << 1)] = get2();
  4371. break;
  4372. case 102:
  4373. fseek (ifp, 6, SEEK_CUR);
  4374. goto get2_rggb;
  4375. case 103:
  4376. fseek (ifp, 16, SEEK_CUR);
  4377. FORC4 cam_mul[c] = get2();
  4378. }
  4379. if (ver97 >= 200) {
  4380. if (ver97 != 205) fseek (ifp, 280, SEEK_CUR);
  4381. fread (buf97, 324, 1, ifp);
  4382. }
  4383. }
  4384. if (tag == 0xa1 && type == 7) {
  4385. order = 0x4949;
  4386. fseek (ifp, 140, SEEK_CUR);
  4387. FORC3 cam_mul[c] = get4();
  4388. }
  4389. if (tag == 0xa4 && type == 3) {
  4390. fseek (ifp, wbi*48, SEEK_CUR);
  4391. FORC3 cam_mul[c] = get2();
  4392. }
  4393. if (tag == 0xa7 && (unsigned) (ver97-200) < 17) {
  4394. ci = xlat[0][serial & 0xff];
  4395. cj = xlat[1][fgetc(ifp)^fgetc(ifp)^fgetc(ifp)^fgetc(ifp)];
  4396. ck = 0x60;
  4397. for (i=0; i < 324; i++)
  4398. buf97[i] ^= (cj += ci * ck++);
  4399. i = "66666>666;6A;:;55"[ver97-200] - '0';
  4400. FORC4 cam_mul[c ^ (c >> 1) ^ (i & 1)] =
  4401. sget2 (buf97 + (i & -2) + c*2);
  4402. }
  4403. if (tag == 0x200 && len == 3)
  4404. shot_order = (get4(),get4());
  4405. if (tag == 0x200 && len == 4)
  4406. FORC4 cblack[c ^ c >> 1] = get2();
  4407. if (tag == 0x201 && len == 4)
  4408. goto get2_rggb;
  4409. if (tag == 0x220 && type == 7)
  4410. meta_offset = ftell(ifp);
  4411. if (tag == 0x401 && type == 4 && len == 4)
  4412. FORC4 cblack[c ^ c >> 1] = get4();
  4413. if (tag == 0xe01) { /* Nikon Capture Note */
  4414. order = 0x4949;
  4415. fseek (ifp, 22, SEEK_CUR);
  4416. for (offset=22; offset+22 < len; offset += 22+i) {
  4417. tag = get4();
  4418. fseek (ifp, 14, SEEK_CUR);
  4419. i = get4()-4;
  4420. if (tag == 0x76a43207) flip = get2();
  4421. else fseek (ifp, i, SEEK_CUR);
  4422. }
  4423. }
  4424. if (tag == 0xe80 && len == 256 && type == 7) {
  4425. fseek (ifp, 48, SEEK_CUR);
  4426. cam_mul[0] = get2() * 508 * 1.078 / 0x10000;
  4427. cam_mul[2] = get2() * 382 * 1.173 / 0x10000;
  4428. }
  4429. if (tag == 0xf00 && type == 7) {
  4430. if (len == 614)
  4431. fseek (ifp, 176, SEEK_CUR);
  4432. else if (len == 734 || len == 1502)
  4433. fseek (ifp, 148, SEEK_CUR);
  4434. else goto next;
  4435. goto get2_256;
  4436. }
  4437. if ((tag == 0x1011 && len == 9) || tag == 0x20400200)
  4438. for (i=0; i < 3; i++)
  4439. FORC3 cmatrix[i][c] = ((short) get2()) / 256.0;
  4440. if ((tag == 0x1012 || tag == 0x20400600) && len == 4)
  4441. FORC4 cblack[c ^ c >> 1] = get2();
  4442. if (tag == 0x1017 || tag == 0x20400100)
  4443. cam_mul[0] = get2() / 256.0;
  4444. if (tag == 0x1018 || tag == 0x20400100)
  4445. cam_mul[2] = get2() / 256.0;
  4446. if (tag == 0x2011 && len == 2) {
  4447. get2_256:
  4448. order = 0x4d4d;
  4449. cam_mul[0] = get2() / 256.0;
  4450. cam_mul[2] = get2() / 256.0;
  4451. }
  4452. if ((tag | 0x70) == 0x2070 && type == 4)
  4453. fseek (ifp, get4()+base, SEEK_SET);
  4454. if (tag == 0x2010 && type != 7)
  4455. load_raw = &CLASS olympus_load_raw;
  4456. if (tag == 0x2020)
  4457. parse_thumb_note (base, 257, 258);
  4458. if (tag == 0x2040)
  4459. parse_makernote (base, 0x2040);
  4460. if (tag == 0xb028) {
  4461. fseek (ifp, get4()+base, SEEK_SET);
  4462. parse_thumb_note (base, 136, 137);
  4463. }
  4464. if (tag == 0x4001 && len > 500) {
  4465. i = len == 582 ? 50 : len == 653 ? 68 : len == 5120 ? 142 : 126;
  4466. fseek (ifp, i, SEEK_CUR);
  4467. get2_rggb:
  4468. FORC4 cam_mul[c ^ (c >> 1)] = get2();
  4469. i = len >> 3 == 164 ? 112:22;
  4470. fseek (ifp, i, SEEK_CUR);
  4471. FORC4 sraw_mul[c ^ (c >> 1)] = get2();
  4472. }
  4473. if (tag == 0xa021)
  4474. FORC4 cam_mul[c ^ (c >> 1)] = get4();
  4475. if (tag == 0xa028)
  4476. FORC4 cam_mul[c ^ (c >> 1)] -= get4();
  4477. next:
  4478. fseek (ifp, save, SEEK_SET);
  4479. }
  4480. quit:
  4481. order = sorder;
  4482. }
  4483. /*
  4484. Since the TIFF DateTime string has no timezone information,
  4485. assume that the camera's clock was set to Universal Time.
  4486. */
  4487. void CLASS get_timestamp (int reversed)
  4488. {
  4489. struct tm t;
  4490. char str[20];
  4491. int i;
  4492. str[19] = 0;
  4493. if (reversed)
  4494. for (i=19; i--; ) str[i] = fgetc(ifp);
  4495. else
  4496. fread (str, 19, 1, ifp);
  4497. memset (&t, 0, sizeof t);
  4498. if (sscanf (str, "%d:%d:%d %d:%d:%d", &t.tm_year, &t.tm_mon,
  4499. &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec) != 6)
  4500. return;
  4501. t.tm_year -= 1900;
  4502. t.tm_mon -= 1;
  4503. t.tm_isdst = -1;
  4504. if (mktime(&t) > 0)
  4505. timestamp = mktime(&t);
  4506. }
  4507. void CLASS parse_exif (int base)
  4508. {
  4509. unsigned kodak, entries, tag, type, len, save, c;
  4510. double expo;
  4511. kodak = !strncmp(make,"EASTMAN",7) && tiff_nifds < 3;
  4512. entries = get2();
  4513. while (entries--) {
  4514. tiff_get (base, &tag, &type, &len, &save);
  4515. switch (tag) {
  4516. case 33434: shutter = getreal(type); break;
  4517. case 33437: aperture = getreal(type); break;
  4518. case 34855: iso_speed = get2(); break;
  4519. case 36867:
  4520. case 36868: get_timestamp(0); break;
  4521. case 37377: if ((expo = -getreal(type)) < 128)
  4522. shutter = pow (2.0, expo); break;
  4523. case 37378: aperture = pow (2.0, getreal(type)/2); break;
  4524. case 37386: focal_len = getreal(type); break;
  4525. case 37500: parse_makernote (base, 0); break;
  4526. case 40962: if (kodak) raw_width = get4(); break;
  4527. case 40963: if (kodak) raw_height = get4(); break;
  4528. case 41730:
  4529. if (get4() == 0x20002)
  4530. for (exif_cfa=c=0; c < 8; c+=2)
  4531. exif_cfa |= fgetc(ifp) * 0x01010101 << c;
  4532. }
  4533. fseek (ifp, save, SEEK_SET);
  4534. }
  4535. }
  4536. void CLASS parse_gps (int base)
  4537. {
  4538. unsigned entries, tag, type, len, save, c;
  4539. entries = get2();
  4540. while (entries--) {
  4541. tiff_get (base, &tag, &type, &len, &save);
  4542. switch (tag) {
  4543. case 1: case 3: case 5:
  4544. gpsdata[29+tag/2] = getc(ifp); break;
  4545. case 2: case 4: case 7:
  4546. FORC(6) gpsdata[tag/3*6+c] = get4(); break;
  4547. case 6:
  4548. FORC(2) gpsdata[18+c] = get4(); break;
  4549. case 18: case 29:
  4550. fgets ((char *) (gpsdata+14+tag/3), MIN(len,12), ifp);
  4551. }
  4552. fseek (ifp, save, SEEK_SET);
  4553. }
  4554. }
  4555. void CLASS romm_coeff (float romm_cam[3][3])
  4556. {
  4557. static const float rgb_romm[3][3] = /* ROMM == Kodak ProPhoto */
  4558. { { 2.034193, -0.727420, -0.306766 },
  4559. { -0.228811, 1.231729, -0.002922 },
  4560. { -0.008565, -0.153273, 1.161839 } };
  4561. int i, j, k;
  4562. for (i=0; i < 3; i++)
  4563. for (j=0; j < 3; j++)
  4564. for (cmatrix[i][j] = k=0; k < 3; k++)
  4565. cmatrix[i][j] += rgb_romm[i][k] * romm_cam[k][j];
  4566. }
  4567. void CLASS parse_mos (int offset)
  4568. {
  4569. char data[40];
  4570. int skip, from, i, c, neut[4], planes=0, frot=0;
  4571. static const char *mod[] =
  4572. { "","DCB2","Volare","Cantare","CMost","Valeo 6","Valeo 11","Valeo 22",
  4573. "Valeo 11p","Valeo 17","","Aptus 17","Aptus 22","Aptus 75","Aptus 65",
  4574. "Aptus 54S","Aptus 65S","Aptus 75S","AFi 5","AFi 6","AFi 7",
  4575. "","","","","","","","","","","","","","","","","","AFi-II 12" };
  4576. float romm_cam[3][3];
  4577. fseek (ifp, offset, SEEK_SET);
  4578. while (1) {
  4579. if (get4() != 0x504b5453) break;
  4580. get4();
  4581. fread (data, 1, 40, ifp);
  4582. skip = get4();
  4583. from = ftell(ifp);
  4584. if (!strcmp(data,"JPEG_preview_data")) {
  4585. thumb_offset = from;
  4586. thumb_length = skip;
  4587. }
  4588. if (!strcmp(data,"icc_camera_profile")) {
  4589. profile_offset = from;
  4590. profile_length = skip;
  4591. }
  4592. if (!strcmp(data,"ShootObj_back_type")) {
  4593. fscanf (ifp, "%d", &i);
  4594. if ((unsigned) i < sizeof mod / sizeof (*mod))
  4595. strcpy (model, mod[i]);
  4596. }
  4597. if (!strcmp(data,"icc_camera_to_tone_matrix")) {
  4598. for (i=0; i < 9; i++)
  4599. romm_cam[0][i] = int_to_float(get4());
  4600. romm_coeff (romm_cam);
  4601. }
  4602. if (!strcmp(data,"CaptProf_color_matrix")) {
  4603. for (i=0; i < 9; i++)
  4604. fscanf (ifp, "%f", &romm_cam[0][i]);
  4605. romm_coeff (romm_cam);
  4606. }
  4607. if (!strcmp(data,"CaptProf_number_of_planes"))
  4608. fscanf (ifp, "%d", &planes);
  4609. if (!strcmp(data,"CaptProf_raw_data_rotation"))
  4610. fscanf (ifp, "%d", &flip);
  4611. if (!strcmp(data,"CaptProf_mosaic_pattern"))
  4612. FORC4 {
  4613. fscanf (ifp, "%d", &i);
  4614. if (i == 1) frot = c ^ (c >> 1);
  4615. }
  4616. if (!strcmp(data,"ImgProf_rotation_angle")) {
  4617. fscanf (ifp, "%d", &i);
  4618. flip = i - flip;
  4619. }
  4620. if (!strcmp(data,"NeutObj_neutrals") && !cam_mul[0]) {
  4621. FORC4 fscanf (ifp, "%d", neut+c);
  4622. FORC3 cam_mul[c] = (float) neut[0] / neut[c+1];
  4623. }
  4624. if (!strcmp(data,"Rows_data"))
  4625. load_flags = get4();
  4626. parse_mos (from);
  4627. fseek (ifp, skip+from, SEEK_SET);
  4628. }
  4629. if (planes)
  4630. filters = (planes == 1) * 0x01010101 *
  4631. (uchar) "\x94\x61\x16\x49"[(flip/90 + frot) & 3];
  4632. }
  4633. void CLASS linear_table (unsigned len)
  4634. {
  4635. int i;
  4636. if (len > 0x1000) len = 0x1000;
  4637. read_shorts (curve, len);
  4638. for (i=len; i < 0x1000; i++)
  4639. curve[i] = curve[i-1];
  4640. maximum = curve[0xfff];
  4641. }
  4642. void CLASS parse_kodak_ifd (int base)
  4643. {
  4644. unsigned entries, tag, type, len, save;
  4645. int i, c, wbi=-2, wbtemp=6500;
  4646. float mul[3]={1,1,1}, num;
  4647. static const int wbtag[] = { 64037,64040,64039,64041,-1,-1,64042 };
  4648. entries = get2();
  4649. if (entries > 1024) return;
  4650. while (entries--) {
  4651. tiff_get (base, &tag, &type, &len, &save);
  4652. if (tag == 1020) wbi = getint(type);
  4653. if (tag == 1021 && len == 72) { /* WB set in software */
  4654. fseek (ifp, 40, SEEK_CUR);
  4655. FORC3 cam_mul[c] = 2048.0 / get2();
  4656. wbi = -2;
  4657. }
  4658. if (tag == 2118) wbtemp = getint(type);
  4659. if (tag == 2130 + wbi)
  4660. FORC3 mul[c] = getreal(type);
  4661. if (tag == 2140 + wbi && wbi >= 0)
  4662. FORC3 {
  4663. for (num=i=0; i < 4; i++)
  4664. num += getreal(type) * pow (wbtemp/100.0, i);
  4665. cam_mul[c] = 2048 / (num * mul[c]);
  4666. }
  4667. if (tag == 2317) linear_table (len);
  4668. if (tag == 6020) iso_speed = getint(type);
  4669. if (tag == 64013) wbi = fgetc(ifp);
  4670. if ((unsigned) wbi < 7 && tag == wbtag[wbi])
  4671. FORC3 cam_mul[c] = get4();
  4672. if (tag == 64019) width = getint(type);
  4673. if (tag == 64020) height = (getint(type)+1) & -2;
  4674. fseek (ifp, save, SEEK_SET);
  4675. }
  4676. }
  4677. int CLASS parse_tiff_ifd (int base)
  4678. {
  4679. unsigned entries, tag, type, len, plen=16, save;
  4680. int ifd, use_cm=0, cfa, i, j, c, ima_len=0;
  4681. int blrr=1, blrc=1, dblack[] = { 0,0,0,0 };
  4682. char software[64], *cbuf, *cp;
  4683. uchar cfa_pat[16], cfa_pc[] = { 0,1,2,3 }, tab[256];
  4684. double cc[4][4], cm[4][3], cam_xyz[4][3], num;
  4685. double ab[]={ 1,1,1,1 }, asn[] = { 0,0,0,0 }, xyz[] = { 1,1,1 };
  4686. unsigned sony_curve[] = { 0,0,0,0,0,4095 };
  4687. unsigned *buf, sony_offset=0, sony_length=0, sony_key=0;
  4688. struct jhead jh;
  4689. #ifndef LIBRAW_LIBRARY_BUILD
  4690. FILE *sfp;
  4691. #endif
  4692. if (tiff_nifds >= sizeof tiff_ifd / sizeof tiff_ifd[0])
  4693. return 1;
  4694. ifd = tiff_nifds++;
  4695. for (j=0; j < 4; j++)
  4696. for (i=0; i < 4; i++)
  4697. cc[j][i] = i == j;
  4698. entries = get2();
  4699. if (entries > 512) return 1;
  4700. while (entries--) {
  4701. tiff_get (base, &tag, &type, &len, &save);
  4702. switch (tag) {
  4703. case 5: width = get2(); break;
  4704. case 6: height = get2(); break;
  4705. case 7: width += get2(); break;
  4706. case 9: filters = get2(); break;
  4707. case 17: case 18:
  4708. if (type == 3 && len == 1)
  4709. cam_mul[(tag-17)*2] = get2() / 256.0;
  4710. break;
  4711. case 23:
  4712. if (type == 3) iso_speed = get2();
  4713. break;
  4714. case 36: case 37: case 38:
  4715. cam_mul[tag-0x24] = get2();
  4716. break;
  4717. case 39:
  4718. if (len < 50 || cam_mul[0]) break;
  4719. fseek (ifp, 12, SEEK_CUR);
  4720. FORC3 cam_mul[c] = get2();
  4721. break;
  4722. case 46:
  4723. if (type != 7 || fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8) break;
  4724. thumb_offset = ftell(ifp) - 2;
  4725. thumb_length = len;
  4726. break;
  4727. case 61440: /* Fuji HS10 table */
  4728. parse_tiff_ifd (base);
  4729. break;
  4730. case 2: case 256: case 61441: /* ImageWidth */
  4731. tiff_ifd[ifd].t_width = getint(type);
  4732. break;
  4733. case 3: case 257: case 61442: /* ImageHeight */
  4734. tiff_ifd[ifd].t_height = getint(type);
  4735. break;
  4736. case 258: /* BitsPerSample */
  4737. case 61443:
  4738. tiff_ifd[ifd].samples = len & 7;
  4739. tiff_ifd[ifd].bps = getint(type);
  4740. break;
  4741. case 61446:
  4742. raw_height = 0;
  4743. load_raw = &CLASS packed_load_raw;
  4744. load_flags = get4() && (filters=0x16161616) ? 24:80;
  4745. break;
  4746. case 259: /* Compression */
  4747. tiff_ifd[ifd].comp = getint(type);
  4748. break;
  4749. case 262: /* PhotometricInterpretation */
  4750. tiff_ifd[ifd].phint = get2();
  4751. break;
  4752. case 270: /* ImageDescription */
  4753. fread (desc, 512, 1, ifp);
  4754. break;
  4755. case 271: /* Make */
  4756. fgets (make, 64, ifp);
  4757. break;
  4758. case 272: /* Model */
  4759. fgets (model, 64, ifp);
  4760. break;
  4761. case 280: /* Panasonic RW2 offset */
  4762. if (type != 4) break;
  4763. load_raw = &CLASS panasonic_load_raw;
  4764. load_flags = 0x2008;
  4765. case 273: /* StripOffset */
  4766. case 513: /* JpegIFOffset */
  4767. case 61447:
  4768. tiff_ifd[ifd].offset = get4()+base;
  4769. if (!tiff_ifd[ifd].bps && tiff_ifd[ifd].offset > 0) {
  4770. fseek (ifp, tiff_ifd[ifd].offset, SEEK_SET);
  4771. if (ljpeg_start (&jh, 1)) {
  4772. tiff_ifd[ifd].comp = 6;
  4773. tiff_ifd[ifd].t_width = jh.wide;
  4774. tiff_ifd[ifd].t_height = jh.high;
  4775. tiff_ifd[ifd].bps = jh.bits;
  4776. tiff_ifd[ifd].samples = jh.clrs;
  4777. if (!(jh.sraw || (jh.clrs & 1)))
  4778. tiff_ifd[ifd].t_width *= jh.clrs;
  4779. i = order;
  4780. parse_tiff (tiff_ifd[ifd].offset + 12);
  4781. order = i;
  4782. }
  4783. }
  4784. break;
  4785. case 274: /* Orientation */
  4786. tiff_ifd[ifd].t_flip = "50132467"[get2() & 7]-'0';
  4787. break;
  4788. case 277: /* SamplesPerPixel */
  4789. tiff_ifd[ifd].samples = getint(type) & 7;
  4790. break;
  4791. case 279: /* StripByteCounts */
  4792. case 514:
  4793. case 61448:
  4794. tiff_ifd[ifd].bytes = get4();
  4795. break;
  4796. case 61454:
  4797. FORC3 cam_mul[(4-c) % 3] = getint(type);
  4798. break;
  4799. case 305: case 11: /* Software */
  4800. fgets (software, 64, ifp);
  4801. if (!strncmp(software,"Adobe",5) ||
  4802. !strncmp(software,"dcraw",5) ||
  4803. !strncmp(software,"UFRaw",5) ||
  4804. !strncmp(software,"Bibble",6) ||
  4805. !strncmp(software,"Nikon Scan",10) ||
  4806. !strcmp (software,"Digital Photo Professional"))
  4807. is_raw = 0;
  4808. break;
  4809. case 306: /* DateTime */
  4810. get_timestamp(0);
  4811. break;
  4812. case 315: /* Artist */
  4813. fread (artist, 64, 1, ifp);
  4814. break;
  4815. case 322: /* TileWidth */
  4816. tiff_ifd[ifd].t_tile_width = getint(type);
  4817. break;
  4818. case 323: /* TileLength */
  4819. tiff_ifd[ifd].t_tile_length = getint(type);
  4820. break;
  4821. case 324: /* TileOffsets */
  4822. tiff_ifd[ifd].offset = len > 1 ? ftell(ifp) : get4();
  4823. if (len == 4) {
  4824. load_raw = &CLASS sinar_4shot_load_raw;
  4825. is_raw = 5;
  4826. }
  4827. break;
  4828. #ifdef LIBRAW_LIBRARY_BUILD
  4829. case 325: /* TileByteCount */
  4830. tiff_ifd[ifd].tile_maxbytes = 0;
  4831. for(int jj=0;jj<len;jj++)
  4832. {
  4833. int s = get4();
  4834. if(s > tiff_ifd[ifd].tile_maxbytes) tiff_ifd[ifd].tile_maxbytes=s;
  4835. }
  4836. break;
  4837. #endif
  4838. case 330: /* SubIFDs */
  4839. if (!strcmp(model,"DSLR-A100") && tiff_ifd[ifd].t_width == 3872) {
  4840. load_raw = &CLASS sony_arw_load_raw;
  4841. data_offset = get4()+base;
  4842. ifd++; break;
  4843. }
  4844. while (len--) {
  4845. i = ftell(ifp);
  4846. fseek (ifp, get4()+base, SEEK_SET);
  4847. if (parse_tiff_ifd (base)) break;
  4848. fseek (ifp, i+4, SEEK_SET);
  4849. }
  4850. break;
  4851. case 400:
  4852. strcpy (make, "Sarnoff");
  4853. maximum = 0xfff;
  4854. break;
  4855. case 28688:
  4856. FORC4 sony_curve[c+1] = get2() >> 2 & 0xfff;
  4857. for (i=0; i < 5; i++)
  4858. for (j = sony_curve[i]+1; j <= sony_curve[i+1]; j++)
  4859. curve[j] = curve[j-1] + (1 << i);
  4860. break;
  4861. case 29184: sony_offset = get4(); break;
  4862. case 29185: sony_length = get4(); break;
  4863. case 29217: sony_key = get4(); break;
  4864. case 29264:
  4865. parse_minolta (ftell(ifp));
  4866. raw_width = 0;
  4867. break;
  4868. case 29443:
  4869. FORC4 cam_mul[c ^ (c < 2)] = get2();
  4870. break;
  4871. case 29459:
  4872. FORC4 cam_mul[c] = get2();
  4873. i = (cam_mul[1] == 1024 && cam_mul[2] == 1024) << 1;
  4874. SWAP (cam_mul[i],cam_mul[i+1])
  4875. break;
  4876. case 33405: /* Model2 */
  4877. fgets (model2, 64, ifp);
  4878. break;
  4879. case 33422: /* CFAPattern */
  4880. case 64777: /* Kodak P-series */
  4881. if ((plen=len) > 16) plen = 16;
  4882. fread (cfa_pat, 1, plen, ifp);
  4883. for (colors=cfa=i=0; i < plen; i++) {
  4884. colors += !(cfa & (1 << cfa_pat[i]));
  4885. cfa |= 1 << cfa_pat[i];
  4886. }
  4887. if (cfa == 070) memcpy (cfa_pc,"\003\004\005",3); /* CMY */
  4888. if (cfa == 072) memcpy (cfa_pc,"\005\003\004\001",4); /* GMCY */
  4889. goto guess_cfa_pc;
  4890. case 33424:
  4891. case 65024:
  4892. fseek (ifp, get4()+base, SEEK_SET);
  4893. parse_kodak_ifd (base);
  4894. break;
  4895. case 33434: /* ExposureTime */
  4896. shutter = getreal(type);
  4897. break;
  4898. case 33437: /* FNumber */
  4899. aperture = getreal(type);
  4900. break;
  4901. case 34306: /* Leaf white balance */
  4902. FORC4 cam_mul[c ^ 1] = 4096.0 / get2();
  4903. break;
  4904. case 34307: /* Leaf CatchLight color matrix */
  4905. fread (software, 1, 7, ifp);
  4906. if (strncmp(software,"MATRIX",6)) break;
  4907. colors = 4;
  4908. for (raw_color = i=0; i < 3; i++) {
  4909. FORC4 fscanf (ifp, "%f", &rgb_cam[i][c^1]);
  4910. if (!use_camera_wb) continue;
  4911. num = 0;
  4912. FORC4 num += rgb_cam[i][c];
  4913. FORC4 rgb_cam[i][c] /= num;
  4914. }
  4915. break;
  4916. case 34310: /* Leaf metadata */
  4917. parse_mos (ftell(ifp));
  4918. case 34303:
  4919. strcpy (make, "Leaf");
  4920. break;
  4921. case 34665: /* EXIF tag */
  4922. fseek (ifp, get4()+base, SEEK_SET);
  4923. parse_exif (base);
  4924. break;
  4925. case 34853: /* GPSInfo tag */
  4926. fseek (ifp, get4()+base, SEEK_SET);
  4927. parse_gps (base);
  4928. break;
  4929. case 34675: /* InterColorProfile */
  4930. case 50831: /* AsShotICCProfile */
  4931. profile_offset = ftell(ifp);
  4932. profile_length = len;
  4933. break;
  4934. case 37122: /* CompressedBitsPerPixel */
  4935. kodak_cbpp = get4();
  4936. break;
  4937. case 37386: /* FocalLength */
  4938. focal_len = getreal(type);
  4939. break;
  4940. case 37393: /* ImageNumber */
  4941. shot_order = getint(type);
  4942. break;
  4943. case 37400: /* old Kodak KDC tag */
  4944. for (raw_color = i=0; i < 3; i++) {
  4945. getreal(type);
  4946. FORC3 rgb_cam[i][c] = getreal(type);
  4947. }
  4948. break;
  4949. case 46275: /* Imacon tags */
  4950. strcpy (make, "Imacon");
  4951. data_offset = ftell(ifp);
  4952. ima_len = len;
  4953. break;
  4954. case 46279:
  4955. if (!ima_len) break;
  4956. fseek (ifp, 38, SEEK_CUR);
  4957. case 46274:
  4958. fseek (ifp, 40, SEEK_CUR);
  4959. raw_width = get4();
  4960. raw_height = get4();
  4961. left_margin = get4() & 7;
  4962. width = raw_width - left_margin - (get4() & 7);
  4963. top_margin = get4() & 7;
  4964. height = raw_height - top_margin - (get4() & 7);
  4965. if (raw_width == 7262 && ima_len == 234317952 ) {
  4966. height = 5412;
  4967. width = 7216;
  4968. left_margin = 7;
  4969. filters=0;
  4970. } else if (raw_width == 7262) {
  4971. height = 5444;
  4972. width = 7244;
  4973. left_margin = 7;
  4974. }
  4975. fseek (ifp, 52, SEEK_CUR);
  4976. FORC3 cam_mul[c] = getreal(11);
  4977. fseek (ifp, 114, SEEK_CUR);
  4978. flip = (get2() >> 7) * 90;
  4979. if (width * height * 6 == ima_len) {
  4980. if (flip % 180 == 90) SWAP(width,height);
  4981. raw_width = width;
  4982. raw_height = height;
  4983. left_margin = top_margin = filters = flip = 0;
  4984. }
  4985. sprintf (model, "Ixpress %d-Mp", height*width/1000000);
  4986. load_raw = &CLASS imacon_full_load_raw;
  4987. if (filters) {
  4988. if (left_margin & 1) filters = 0x61616161;
  4989. load_raw = &CLASS unpacked_load_raw;
  4990. }
  4991. maximum = 0xffff;
  4992. break;
  4993. case 50454: /* Sinar tag */
  4994. case 50455:
  4995. if (!(cbuf = (char *) malloc(len))) break;
  4996. fread (cbuf, 1, len, ifp);
  4997. for (cp = cbuf-1; cp && cp < cbuf+len; cp = strchr(cp,'\n'))
  4998. if (!strncmp (++cp,"Neutral ",8))
  4999. sscanf (cp+8, "%f %f %f", cam_mul, cam_mul+1, cam_mul+2);
  5000. free (cbuf);
  5001. break;
  5002. case 50458:
  5003. if (!make[0]) strcpy (make, "Hasselblad");
  5004. break;
  5005. case 50459: /* Hasselblad tag */
  5006. i = order;
  5007. j = ftell(ifp);
  5008. c = tiff_nifds;
  5009. order = get2();
  5010. fseek (ifp, j+(get2(),get4()), SEEK_SET);
  5011. parse_tiff_ifd (j);
  5012. maximum = 0xffff;
  5013. tiff_nifds = c;
  5014. order = i;
  5015. break;
  5016. case 50706: /* DNGVersion */
  5017. FORC4 dng_version = (dng_version << 8) + fgetc(ifp);
  5018. if (!make[0]) strcpy (make, "DNG");
  5019. is_raw = 1;
  5020. break;
  5021. case 50710: /* CFAPlaneColor */
  5022. if (len > 4) len = 4;
  5023. colors = len;
  5024. fread (cfa_pc, 1, colors, ifp);
  5025. guess_cfa_pc:
  5026. FORCC tab[cfa_pc[c]] = c;
  5027. cdesc[c] = 0;
  5028. for (i=16; i--; )
  5029. filters = filters << 2 | tab[cfa_pat[i % plen]];
  5030. break;
  5031. case 50711: /* CFALayout */
  5032. if (get2() == 2) {
  5033. fuji_width = 1;
  5034. filters = 0x49494949;
  5035. }
  5036. break;
  5037. case 291:
  5038. case 50712: /* LinearizationTable */
  5039. linear_table (len);
  5040. break;
  5041. case 50713: /* BlackLevelRepeatDim */
  5042. blrr = get2();
  5043. blrc = get2();
  5044. break;
  5045. case 61450:
  5046. blrr = blrc = 2;
  5047. case 50714: /* BlackLevel */
  5048. black = getreal(type);
  5049. if (!filters || !~filters) break;
  5050. dblack[0] = black;
  5051. dblack[1] = (blrc == 2) ? getreal(type):dblack[0];
  5052. dblack[2] = (blrr == 2) ? getreal(type):dblack[0];
  5053. dblack[3] = (blrc == 2 && blrr == 2) ? getreal(type):dblack[1];
  5054. if (colors == 3)
  5055. filters |= ((filters >> 2 & 0x22222222) |
  5056. (filters << 2 & 0x88888888)) & filters << 1;
  5057. FORC4 cblack[filters >> (c << 1) & 3] = dblack[c];
  5058. black = 0;
  5059. break;
  5060. case 50715: /* BlackLevelDeltaH */
  5061. case 50716: /* BlackLevelDeltaV */
  5062. for (num=i=0; i < len; i++)
  5063. num += getreal(type);
  5064. black += num/len + 0.5;
  5065. break;
  5066. case 50717: /* WhiteLevel */
  5067. maximum = getint(type);
  5068. break;
  5069. case 50718: /* DefaultScale */
  5070. pixel_aspect = getreal(type);
  5071. pixel_aspect /= getreal(type);
  5072. break;
  5073. case 50721: /* ColorMatrix1 */
  5074. case 50722: /* ColorMatrix2 */
  5075. FORCC for (j=0; j < 3; j++)
  5076. cm[c][j] = getreal(type);
  5077. use_cm = 1;
  5078. break;
  5079. case 50723: /* CameraCalibration1 */
  5080. case 50724: /* CameraCalibration2 */
  5081. for (i=0; i < colors; i++)
  5082. FORCC cc[i][c] = getreal(type);
  5083. break;
  5084. case 50727: /* AnalogBalance */
  5085. FORCC ab[c] = getreal(type);
  5086. break;
  5087. case 50728: /* AsShotNeutral */
  5088. FORCC asn[c] = getreal(type);
  5089. break;
  5090. case 50729: /* AsShotWhiteXY */
  5091. xyz[0] = getreal(type);
  5092. xyz[1] = getreal(type);
  5093. xyz[2] = 1 - xyz[0] - xyz[1];
  5094. FORC3 xyz[c] /= d65_white[c];
  5095. break;
  5096. case 50740: /* DNGPrivateData */
  5097. if (dng_version) break;
  5098. parse_minolta (j = get4()+base);
  5099. fseek (ifp, j, SEEK_SET);
  5100. parse_tiff_ifd (base);
  5101. break;
  5102. case 50752:
  5103. read_shorts (cr2_slice, 3);
  5104. break;
  5105. case 50829: /* ActiveArea */
  5106. top_margin = getint(type);
  5107. left_margin = getint(type);
  5108. height = getint(type) - top_margin;
  5109. width = getint(type) - left_margin;
  5110. break;
  5111. case 50830: /* MaskedAreas */
  5112. for (i=0; i < len && i < 32; i++)
  5113. mask[0][i] = getint(type);
  5114. black = 0;
  5115. break;
  5116. case 51009: /* OpcodeList2 */
  5117. meta_offset = ftell(ifp);
  5118. break;
  5119. case 64772: /* Kodak P-series */
  5120. if (len < 13) break;
  5121. fseek (ifp, 16, SEEK_CUR);
  5122. data_offset = get4();
  5123. fseek (ifp, 28, SEEK_CUR);
  5124. data_offset += get4();
  5125. load_raw = &CLASS packed_load_raw;
  5126. break;
  5127. case 65026:
  5128. if (type == 2) fgets (model2, 64, ifp);
  5129. }
  5130. fseek (ifp, save, SEEK_SET);
  5131. }
  5132. if (sony_length && (buf = (unsigned *) malloc(sony_length))) {
  5133. fseek (ifp, sony_offset, SEEK_SET);
  5134. fread (buf, sony_length, 1, ifp);
  5135. sony_decrypt (buf, sony_length/4, 1, sony_key);
  5136. #ifndef LIBRAW_LIBRARY_BUILD
  5137. sfp = ifp;
  5138. if ((ifp = tmpfile())) {
  5139. fwrite (buf, sony_length, 1, ifp);
  5140. fseek (ifp, 0, SEEK_SET);
  5141. parse_tiff_ifd (-sony_offset);
  5142. fclose (ifp);
  5143. }
  5144. ifp = sfp;
  5145. #else
  5146. if( !ifp->tempbuffer_open(buf,sony_length))
  5147. {
  5148. parse_tiff_ifd(-sony_offset);
  5149. ifp->tempbuffer_close();
  5150. }
  5151. #endif
  5152. free (buf);
  5153. }
  5154. for (i=0; i < colors; i++)
  5155. FORCC cc[i][c] *= ab[i];
  5156. if (use_cm) {
  5157. FORCC for (i=0; i < 3; i++)
  5158. for (cam_xyz[c][i]=j=0; j < colors; j++)
  5159. cam_xyz[c][i] += cc[c][j] * cm[j][i] * xyz[i];
  5160. cam_xyz_coeff (cam_xyz);
  5161. }
  5162. if (asn[0]) {
  5163. cam_mul[3] = 0;
  5164. FORCC cam_mul[c] = 1 / asn[c];
  5165. }
  5166. if (!use_cm)
  5167. FORCC pre_mul[c] /= cc[c][c];
  5168. return 0;
  5169. }
  5170. int CLASS parse_tiff (int base)
  5171. {
  5172. int doff;
  5173. fseek (ifp, base, SEEK_SET);
  5174. order = get2();
  5175. if (order != 0x4949 && order != 0x4d4d) return 0;
  5176. get2();
  5177. while ((doff = get4())) {
  5178. fseek (ifp, doff+base, SEEK_SET);
  5179. if (parse_tiff_ifd (base)) break;
  5180. }
  5181. return 1;
  5182. }
  5183. void CLASS apply_tiff()
  5184. {
  5185. int max_samp=0, raw=-1, thm=-1, i;
  5186. struct jhead jh;
  5187. thumb_misc = 16;
  5188. if (thumb_offset) {
  5189. fseek (ifp, thumb_offset, SEEK_SET);
  5190. if (ljpeg_start (&jh, 1)) {
  5191. thumb_misc = jh.bits;
  5192. thumb_width = jh.wide;
  5193. thumb_height = jh.high;
  5194. }
  5195. }
  5196. for (i=0; i < tiff_nifds; i++) {
  5197. if (max_samp < tiff_ifd[i].samples)
  5198. max_samp = tiff_ifd[i].samples;
  5199. if (max_samp > 3) max_samp = 3;
  5200. if ((tiff_ifd[i].comp != 6 || tiff_ifd[i].samples != 3) &&
  5201. (tiff_ifd[i].t_width | tiff_ifd[i].t_height) < 0x10000 &&
  5202. tiff_ifd[i].t_width*tiff_ifd[i].t_height > raw_width*raw_height) {
  5203. raw_width = tiff_ifd[i].t_width;
  5204. raw_height = tiff_ifd[i].t_height;
  5205. tiff_bps = tiff_ifd[i].bps;
  5206. tiff_compress = tiff_ifd[i].comp;
  5207. data_offset = tiff_ifd[i].offset;
  5208. tiff_flip = tiff_ifd[i].t_flip;
  5209. tiff_samples = tiff_ifd[i].samples;
  5210. tile_width = tiff_ifd[i].t_tile_width;
  5211. tile_length = tiff_ifd[i].t_tile_length;
  5212. #ifdef LIBRAW_LIBRARY_BUILD
  5213. data_size = tile_length < INT_MAX && tile_length>0 ? tiff_ifd[i].tile_maxbytes: tiff_ifd[i].bytes;
  5214. #endif
  5215. raw = i;
  5216. }
  5217. }
  5218. if (!tile_width ) tile_width = INT_MAX;
  5219. if (!tile_length) tile_length = INT_MAX;
  5220. for (i=tiff_nifds; i--; )
  5221. if (tiff_ifd[i].t_flip) tiff_flip = tiff_ifd[i].t_flip;
  5222. if (raw >= 0 && !load_raw)
  5223. switch (tiff_compress) {
  5224. case 32767:
  5225. if (tiff_ifd[raw].bytes == raw_width*raw_height) {
  5226. tiff_bps = 12;
  5227. load_raw = &CLASS sony_arw2_load_raw; break;
  5228. }
  5229. if (tiff_ifd[raw].bytes*8 != raw_width*raw_height*tiff_bps) {
  5230. raw_height += 8;
  5231. load_raw = &CLASS sony_arw_load_raw; break;
  5232. }
  5233. load_flags = 79;
  5234. case 32769:
  5235. load_flags++;
  5236. case 32770:
  5237. case 32773: goto slr;
  5238. case 0: case 1:
  5239. if (tiff_ifd[raw].bytes*5 == raw_width*raw_height*8) {
  5240. load_flags = 81;
  5241. tiff_bps = 12;
  5242. } slr:
  5243. switch (tiff_bps) {
  5244. case 8: load_raw = &CLASS eight_bit_load_raw; break;
  5245. case 12: if (tiff_ifd[raw].phint == 2)
  5246. load_flags = 6;
  5247. load_raw = &CLASS packed_load_raw; break;
  5248. case 14: load_flags = 0;
  5249. case 16: load_raw = &CLASS unpacked_load_raw; break;
  5250. }
  5251. break;
  5252. case 6: case 7: case 99:
  5253. load_raw = &CLASS lossless_jpeg_load_raw; break;
  5254. case 262:
  5255. load_raw = &CLASS kodak_262_load_raw; break;
  5256. case 34713:
  5257. if ((raw_width+9)/10*16*raw_height == tiff_ifd[raw].bytes) {
  5258. load_raw = &CLASS packed_load_raw;
  5259. load_flags = 1;
  5260. } else if (raw_width*raw_height*2 == tiff_ifd[raw].bytes) {
  5261. load_raw = &CLASS unpacked_load_raw;
  5262. load_flags = 4;
  5263. order = 0x4d4d;
  5264. } else
  5265. load_raw = &CLASS nikon_load_raw; break;
  5266. case 34892:
  5267. load_raw = &CLASS lossy_dng_load_raw; break;
  5268. case 65535:
  5269. load_raw = &CLASS pentax_load_raw; break;
  5270. case 65000:
  5271. switch (tiff_ifd[raw].phint) {
  5272. case 2: load_raw = &CLASS kodak_rgb_load_raw; filters = 0; break;
  5273. case 6: load_raw = &CLASS kodak_ycbcr_load_raw; filters = 0; break;
  5274. case 32803: load_raw = &CLASS kodak_65000_load_raw;
  5275. }
  5276. case 32867: break;
  5277. default: is_raw = 0;
  5278. }
  5279. if (!dng_version)
  5280. if ( (tiff_samples == 3 && tiff_ifd[raw].bytes &&
  5281. tiff_bps != 14 && tiff_bps != 2048 &&
  5282. tiff_compress != 32769 && tiff_compress != 32770)
  5283. || (tiff_bps == 8 && !strstr(make,"KODAK") && !strstr(make,"Kodak") &&
  5284. !strstr(model2,"DEBUG RAW")))
  5285. is_raw = 0;
  5286. for (i=0; i < tiff_nifds; i++)
  5287. if (i != raw && tiff_ifd[i].samples == max_samp &&
  5288. tiff_ifd[i].t_width * tiff_ifd[i].t_height / SQR(tiff_ifd[i].bps+1) >
  5289. thumb_width * thumb_height / SQR(thumb_misc+1)
  5290. && tiff_ifd[i].comp != 34892) {
  5291. thumb_width = tiff_ifd[i].t_width;
  5292. thumb_height = tiff_ifd[i].t_height;
  5293. thumb_offset = tiff_ifd[i].offset;
  5294. thumb_length = tiff_ifd[i].bytes;
  5295. thumb_misc = tiff_ifd[i].bps;
  5296. thm = i;
  5297. }
  5298. if (thm >= 0) {
  5299. thumb_misc |= tiff_ifd[thm].samples << 5;
  5300. switch (tiff_ifd[thm].comp) {
  5301. case 0:
  5302. write_thumb = &CLASS layer_thumb;
  5303. break;
  5304. case 1:
  5305. if (tiff_ifd[thm].bps <= 8)
  5306. write_thumb = &CLASS ppm_thumb;
  5307. else if (!strcmp(make,"Imacon"))
  5308. write_thumb = &CLASS ppm16_thumb;
  5309. else
  5310. thumb_load_raw = &CLASS kodak_thumb_load_raw;
  5311. break;
  5312. case 65000:
  5313. thumb_load_raw = tiff_ifd[thm].phint == 6 ?
  5314. &CLASS kodak_ycbcr_load_raw : &CLASS kodak_rgb_load_raw;
  5315. }
  5316. }
  5317. }
  5318. void CLASS parse_minolta (int base)
  5319. {
  5320. int save, tag, len, offset, high=0, wide=0, i, c;
  5321. short sorder=order;
  5322. fseek (ifp, base, SEEK_SET);
  5323. if (fgetc(ifp) || fgetc(ifp)-'M' || fgetc(ifp)-'R') return;
  5324. order = fgetc(ifp) * 0x101;
  5325. offset = base + get4() + 8;
  5326. while ((save=ftell(ifp)) < offset) {
  5327. for (tag=i=0; i < 4; i++)
  5328. tag = tag << 8 | fgetc(ifp);
  5329. len = get4();
  5330. switch (tag) {
  5331. case 0x505244: /* PRD */
  5332. fseek (ifp, 8, SEEK_CUR);
  5333. high = get2();
  5334. wide = get2();
  5335. break;
  5336. case 0x574247: /* WBG */
  5337. get4();
  5338. i = strcmp(model,"DiMAGE A200") ? 0:3;
  5339. FORC4 cam_mul[c ^ (c >> 1) ^ i] = get2();
  5340. break;
  5341. case 0x545457: /* TTW */
  5342. parse_tiff (ftell(ifp));
  5343. data_offset = offset;
  5344. }
  5345. fseek (ifp, save+len+8, SEEK_SET);
  5346. }
  5347. raw_height = high;
  5348. raw_width = wide;
  5349. order = sorder;
  5350. }
  5351. /*
  5352. Many cameras have a "debug mode" that writes JPEG and raw
  5353. at the same time. The raw file has no header, so try to
  5354. to open the matching JPEG file and read its metadata.
  5355. */
  5356. void CLASS parse_external_jpeg()
  5357. {
  5358. const char *file, *ext;
  5359. char *jname, *jfile, *jext;
  5360. #ifndef LIBRAW_LIBRARY_BUILD
  5361. FILE *save=ifp;
  5362. #else
  5363. #if (defined(_MSC_VER) && _MSC_VER > 1310)
  5364. if(ifp->wfname())
  5365. {
  5366. std::wstring rawfile(ifp->wfname());
  5367. rawfile.replace(rawfile.length()-3,3,L"JPG");
  5368. if(!ifp->subfile_open(rawfile.c_str()))
  5369. {
  5370. parse_tiff (12);
  5371. thumb_offset = 0;
  5372. is_raw = 1;
  5373. ifp->subfile_close();
  5374. }
  5375. else
  5376. imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA ;
  5377. return;
  5378. }
  5379. #endif // _MSC_VER
  5380. if(!ifp->fname())
  5381. {
  5382. imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA ;
  5383. return;
  5384. }
  5385. #endif
  5386. ext = strrchr (ifname, '.');
  5387. file = strrchr (ifname, '/');
  5388. if (!file) file = strrchr (ifname, '\\');
  5389. #ifndef LIBRAW_LIBRARY_BUILD
  5390. if (!file) file = ifname-1;
  5391. #else
  5392. if (!file) file = (char*)ifname-1;
  5393. #endif
  5394. file++;
  5395. if (!ext || strlen(ext) != 4 || ext-file != 8) return;
  5396. jname = (char *) malloc (strlen(ifname) + 1);
  5397. merror (jname, "parse_external_jpeg()");
  5398. strcpy (jname, ifname);
  5399. jfile = file - ifname + jname;
  5400. jext = ext - ifname + jname;
  5401. if (strcasecmp (ext, ".jpg")) {
  5402. strcpy (jext, isupper(ext[1]) ? ".JPG":".jpg");
  5403. if (isdigit(*file)) {
  5404. memcpy (jfile, file+4, 4);
  5405. memcpy (jfile+4, file, 4);
  5406. }
  5407. } else
  5408. while (isdigit(*--jext)) {
  5409. if (*jext != '9') {
  5410. (*jext)++;
  5411. break;
  5412. }
  5413. *jext = '0';
  5414. }
  5415. #ifndef LIBRAW_LIBRARY_BUILD
  5416. if (strcmp (jname, ifname)) {
  5417. if ((ifp = fopen (jname, "rb"))) {
  5418. #ifdef DCRAW_VERBOSE
  5419. if (verbose)
  5420. fprintf (stderr,_("Reading metadata from %s ...\n"), jname);
  5421. #endif
  5422. parse_tiff (12);
  5423. thumb_offset = 0;
  5424. is_raw = 1;
  5425. fclose (ifp);
  5426. }
  5427. }
  5428. #else
  5429. if (strcmp (jname, ifname))
  5430. {
  5431. if(!ifp->subfile_open(jname))
  5432. {
  5433. parse_tiff (12);
  5434. thumb_offset = 0;
  5435. is_raw = 1;
  5436. ifp->subfile_close();
  5437. }
  5438. else
  5439. imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA ;
  5440. }
  5441. #endif
  5442. if (!timestamp)
  5443. {
  5444. #ifdef LIBRAW_LIBRARY_BUILD
  5445. imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA ;
  5446. #endif
  5447. #ifdef DCRAW_VERBOSE
  5448. fprintf (stderr,_("Failed to read metadata from %s\n"), jname);
  5449. #endif
  5450. }
  5451. free (jname);
  5452. #ifndef LIBRAW_LIBRARY_BUILD
  5453. ifp = save;
  5454. #endif
  5455. }
  5456. /*
  5457. CIFF block 0x1030 contains an 8x8 white sample.
  5458. Load this into white[][] for use in scale_colors().
  5459. */
  5460. void CLASS ciff_block_1030()
  5461. {
  5462. static const ushort key[] = { 0x410, 0x45f3 };
  5463. int i, bpp, row, col, vbits=0;
  5464. unsigned long bitbuf=0;
  5465. if ((get2(),get4()) != 0x80008 || !get4()) return;
  5466. bpp = get2();
  5467. if (bpp != 10 && bpp != 12) return;
  5468. for (i=row=0; row < 8; row++)
  5469. for (col=0; col < 8; col++) {
  5470. if (vbits < bpp) {
  5471. bitbuf = bitbuf << 16 | (get2() ^ key[i++ & 1]);
  5472. vbits += 16;
  5473. }
  5474. white[row][col] =
  5475. bitbuf << (LONG_BIT - vbits) >> (LONG_BIT - bpp);
  5476. vbits -= bpp;
  5477. }
  5478. }
  5479. /*
  5480. Parse a CIFF file, better known as Canon CRW format.
  5481. */
  5482. void CLASS parse_ciff (int offset, int length)
  5483. {
  5484. int tboff, nrecs, c, type, len, save, wbi=-1;
  5485. ushort key[] = { 0x410, 0x45f3 };
  5486. fseek (ifp, offset+length-4, SEEK_SET);
  5487. tboff = get4() + offset;
  5488. fseek (ifp, tboff, SEEK_SET);
  5489. nrecs = get2();
  5490. if (nrecs > 100) return;
  5491. while (nrecs--) {
  5492. type = get2();
  5493. len = get4();
  5494. save = ftell(ifp) + 4;
  5495. fseek (ifp, offset+get4(), SEEK_SET);
  5496. if ((((type >> 8) + 8) | 8) == 0x38)
  5497. parse_ciff (ftell(ifp), len); /* Parse a sub-table */
  5498. if (type == 0x0810)
  5499. fread (artist, 64, 1, ifp);
  5500. if (type == 0x080a) {
  5501. fread (make, 64, 1, ifp);
  5502. fseek (ifp, strlen(make) - 63, SEEK_CUR);
  5503. fread (model, 64, 1, ifp);
  5504. }
  5505. if (type == 0x1810) {
  5506. fseek (ifp, 12, SEEK_CUR);
  5507. flip = get4();
  5508. }
  5509. if (type == 0x1835) /* Get the decoder table */
  5510. tiff_compress = get4();
  5511. if (type == 0x2007) {
  5512. thumb_offset = ftell(ifp);
  5513. thumb_length = len;
  5514. }
  5515. if (type == 0x1818) {
  5516. shutter = pow (2.0f, -int_to_float((get4(),get4())));
  5517. aperture = pow (2.0f, int_to_float(get4())/2);
  5518. }
  5519. if (type == 0x102a) {
  5520. iso_speed = pow (2.0, (get4(),get2())/32.0 - 4) * 50;
  5521. aperture = pow (2.0, (get2(),(short)get2())/64.0);
  5522. shutter = pow (2.0,-((short)get2())/32.0);
  5523. wbi = (get2(),get2());
  5524. if (wbi > 17) wbi = 0;
  5525. fseek (ifp, 32, SEEK_CUR);
  5526. if (shutter > 1e6) shutter = get2()/10.0;
  5527. }
  5528. if (type == 0x102c) {
  5529. if (get2() > 512) { /* Pro90, G1 */
  5530. fseek (ifp, 118, SEEK_CUR);
  5531. FORC4 cam_mul[c ^ 2] = get2();
  5532. } else { /* G2, S30, S40 */
  5533. fseek (ifp, 98, SEEK_CUR);
  5534. FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get2();
  5535. }
  5536. }
  5537. if (type == 0x0032) {
  5538. if (len == 768) { /* EOS D30 */
  5539. fseek (ifp, 72, SEEK_CUR);
  5540. FORC4 cam_mul[c ^ (c >> 1)] = 1024.0 / get2();
  5541. if (!wbi) cam_mul[0] = -1; /* use my auto white balance */
  5542. } else if (!cam_mul[0]) {
  5543. if (get2() == key[0]) /* Pro1, G6, S60, S70 */
  5544. c = (strstr(model,"Pro1") ?
  5545. "012346000000000000":"01345:000000006008")[wbi]-'0'+ 2;
  5546. else { /* G3, G5, S45, S50 */
  5547. c = "023457000000006000"[wbi]-'0';
  5548. key[0] = key[1] = 0;
  5549. }
  5550. fseek (ifp, 78 + c*8, SEEK_CUR);
  5551. FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get2() ^ key[c & 1];
  5552. if (!wbi) cam_mul[0] = -1;
  5553. }
  5554. }
  5555. if (type == 0x10a9) { /* D60, 10D, 300D, and clones */
  5556. if (len > 66) wbi = "0134567028"[wbi]-'0';
  5557. fseek (ifp, 2 + wbi*8, SEEK_CUR);
  5558. FORC4 cam_mul[c ^ (c >> 1)] = get2();
  5559. }
  5560. if (type == 0x1030 && (0x18040 >> wbi & 1))
  5561. ciff_block_1030(); /* all that don't have 0x10a9 */
  5562. if (type == 0x1031) {
  5563. raw_width = (get2(),get2());
  5564. raw_height = get2();
  5565. }
  5566. if (type == 0x5029) {
  5567. focal_len = len >> 16;
  5568. if ((len & 0xffff) == 2) focal_len /= 32;
  5569. }
  5570. if (type == 0x5813) flash_used = int_to_float(len);
  5571. if (type == 0x5814) canon_ev = int_to_float(len);
  5572. if (type == 0x5817) shot_order = len;
  5573. if (type == 0x5834) unique_id = len;
  5574. if (type == 0x580e) timestamp = len;
  5575. if (type == 0x180e) timestamp = get4();
  5576. #ifdef LOCALTIME
  5577. if ((type | 0x4000) == 0x580e)
  5578. timestamp = mktime (gmtime (&timestamp));
  5579. #endif
  5580. fseek (ifp, save, SEEK_SET);
  5581. }
  5582. }
  5583. void CLASS parse_rollei()
  5584. {
  5585. char line[128], *val;
  5586. struct tm t;
  5587. fseek (ifp, 0, SEEK_SET);
  5588. memset (&t, 0, sizeof t);
  5589. do {
  5590. fgets (line, 128, ifp);
  5591. if ((val = strchr(line,'=')))
  5592. *val++ = 0;
  5593. else
  5594. val = line + strlen(line);
  5595. if (!strcmp(line,"DAT"))
  5596. sscanf (val, "%d.%d.%d", &t.tm_mday, &t.tm_mon, &t.tm_year);
  5597. if (!strcmp(line,"TIM"))
  5598. sscanf (val, "%d:%d:%d", &t.tm_hour, &t.tm_min, &t.tm_sec);
  5599. if (!strcmp(line,"HDR"))
  5600. thumb_offset = atoi(val);
  5601. if (!strcmp(line,"X "))
  5602. raw_width = atoi(val);
  5603. if (!strcmp(line,"Y "))
  5604. raw_height = atoi(val);
  5605. if (!strcmp(line,"TX "))
  5606. thumb_width = atoi(val);
  5607. if (!strcmp(line,"TY "))
  5608. thumb_height = atoi(val);
  5609. } while (strncmp(line,"EOHD",4));
  5610. data_offset = thumb_offset + thumb_width * thumb_height * 2;
  5611. t.tm_year -= 1900;
  5612. t.tm_mon -= 1;
  5613. if (mktime(&t) > 0)
  5614. timestamp = mktime(&t);
  5615. strcpy (make, "Rollei");
  5616. strcpy (model,"d530flex");
  5617. write_thumb = &CLASS rollei_thumb;
  5618. }
  5619. void CLASS parse_sinar_ia()
  5620. {
  5621. int entries, off;
  5622. char str[8], *cp;
  5623. order = 0x4949;
  5624. fseek (ifp, 4, SEEK_SET);
  5625. entries = get4();
  5626. fseek (ifp, get4(), SEEK_SET);
  5627. while (entries--) {
  5628. off = get4(); get4();
  5629. fread (str, 8, 1, ifp);
  5630. if (!strcmp(str,"META")) meta_offset = off;
  5631. if (!strcmp(str,"THUMB")) thumb_offset = off;
  5632. if (!strcmp(str,"RAW0")) data_offset = off;
  5633. }
  5634. fseek (ifp, meta_offset+20, SEEK_SET);
  5635. fread (make, 64, 1, ifp);
  5636. make[63] = 0;
  5637. if ((cp = strchr(make,' '))) {
  5638. strcpy (model, cp+1);
  5639. *cp = 0;
  5640. }
  5641. raw_width = get2();
  5642. raw_height = get2();
  5643. load_raw = &CLASS unpacked_load_raw;
  5644. thumb_width = (get4(),get2());
  5645. thumb_height = get2();
  5646. write_thumb = &CLASS ppm_thumb;
  5647. maximum = 0x3fff;
  5648. }
  5649. void CLASS parse_phase_one (int base)
  5650. {
  5651. unsigned entries, tag, type, len, data, save, i, c;
  5652. float romm_cam[3][3];
  5653. char *cp;
  5654. memset (&ph1, 0, sizeof ph1);
  5655. fseek (ifp, base, SEEK_SET);
  5656. order = get4() & 0xffff;
  5657. if (get4() >> 8 != 0x526177) return; /* "Raw" */
  5658. fseek (ifp, get4()+base, SEEK_SET);
  5659. entries = get4();
  5660. get4();
  5661. while (entries--) {
  5662. tag = get4();
  5663. type = get4();
  5664. len = get4();
  5665. data = get4();
  5666. save = ftell(ifp);
  5667. fseek (ifp, base+data, SEEK_SET);
  5668. switch (tag) {
  5669. case 0x100: flip = "0653"[data & 3]-'0'; break;
  5670. case 0x106:
  5671. for (i=0; i < 9; i++)
  5672. romm_cam[0][i] = getreal(11);
  5673. romm_coeff (romm_cam);
  5674. break;
  5675. case 0x107:
  5676. FORC3 cam_mul[c] = getreal(11);
  5677. break;
  5678. case 0x108: raw_width = data; break;
  5679. case 0x109: raw_height = data; break;
  5680. case 0x10a: left_margin = data; break;
  5681. case 0x10b: top_margin = data; break;
  5682. case 0x10c: width = data; break;
  5683. case 0x10d: height = data; break;
  5684. case 0x10e: ph1.format = data; break;
  5685. case 0x10f: data_offset = data+base; break;
  5686. case 0x110: meta_offset = data+base;
  5687. meta_length = len; break;
  5688. case 0x112: ph1.key_off = save - 4; break;
  5689. case 0x210: ph1.tag_210 = int_to_float(data); break;
  5690. case 0x21a: ph1.tag_21a = data; break;
  5691. case 0x21c: strip_offset = data+base; break;
  5692. case 0x21d: ph1.t_black = data; break;
  5693. case 0x222: ph1.split_col = data; break;
  5694. case 0x223: ph1.black_off = data+base; break;
  5695. case 0x301:
  5696. model[63] = 0;
  5697. fread (model, 1, 63, ifp);
  5698. if ((cp = strstr(model," camera"))) *cp = 0;
  5699. }
  5700. fseek (ifp, save, SEEK_SET);
  5701. }
  5702. load_raw = ph1.format < 3 ?
  5703. &CLASS phase_one_load_raw : &CLASS phase_one_load_raw_c;
  5704. maximum = 0xffff;
  5705. strcpy (make, "Phase One");
  5706. if (model[0]) return;
  5707. switch (raw_height) {
  5708. case 2060: strcpy (model,"LightPhase"); break;
  5709. case 2682: strcpy (model,"H 10"); break;
  5710. case 4128: strcpy (model,"H 20"); break;
  5711. case 5488: strcpy (model,"H 25"); break;
  5712. }
  5713. }
  5714. void CLASS parse_fuji (int offset)
  5715. {
  5716. unsigned entries, tag, len, save, c;
  5717. fseek (ifp, offset, SEEK_SET);
  5718. entries = get4();
  5719. if (entries > 255) return;
  5720. while (entries--) {
  5721. tag = get2();
  5722. len = get2();
  5723. save = ftell(ifp);
  5724. if (tag == 0x100) {
  5725. raw_height = get2();
  5726. raw_width = get2();
  5727. } else if (tag == 0x121) {
  5728. height = get2();
  5729. if ((width = get2()) == 4284) width += 3;
  5730. } else if (tag == 0x130) {
  5731. fuji_layout = fgetc(ifp) >> 7;
  5732. fuji_width = !(fgetc(ifp) & 8);
  5733. } else if (tag == 0x2ff0) {
  5734. FORC4 cam_mul[c ^ 1] = get2();
  5735. } else if (tag == 0xc000) {
  5736. c = order;
  5737. order = 0x4949;
  5738. if ((width = get4()) > 10000) width = get4();
  5739. height = get4();
  5740. order = c;
  5741. }
  5742. fseek (ifp, save+len, SEEK_SET);
  5743. }
  5744. height <<= fuji_layout;
  5745. width >>= fuji_layout;
  5746. }
  5747. int CLASS parse_jpeg (int offset)
  5748. {
  5749. int len, save, hlen, mark;
  5750. fseek (ifp, offset, SEEK_SET);
  5751. if (fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8) return 0;
  5752. while (fgetc(ifp) == 0xff && (mark = fgetc(ifp)) != 0xda) {
  5753. order = 0x4d4d;
  5754. len = get2() - 2;
  5755. save = ftell(ifp);
  5756. if (mark == 0xc0 || mark == 0xc3) {
  5757. fgetc(ifp);
  5758. raw_height = get2();
  5759. raw_width = get2();
  5760. }
  5761. order = get2();
  5762. hlen = get4();
  5763. if (get4() == 0x48454150) /* "HEAP" */
  5764. parse_ciff (save+hlen, len-hlen);
  5765. if (parse_tiff (save+6)) apply_tiff();
  5766. fseek (ifp, save+len, SEEK_SET);
  5767. }
  5768. return 1;
  5769. }
  5770. void CLASS parse_riff()
  5771. {
  5772. unsigned i, size, end;
  5773. char tag[4], date[64], month[64];
  5774. static const char mon[12][4] =
  5775. { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
  5776. struct tm t;
  5777. order = 0x4949;
  5778. fread (tag, 4, 1, ifp);
  5779. size = get4();
  5780. end = ftell(ifp) + size;
  5781. if (!memcmp(tag,"RIFF",4) || !memcmp(tag,"LIST",4)) {
  5782. get4();
  5783. while (ftell(ifp)+7 < end)
  5784. parse_riff();
  5785. } else if (!memcmp(tag,"nctg",4)) {
  5786. while (ftell(ifp)+7 < end) {
  5787. i = get2();
  5788. size = get2();
  5789. if ((i+1) >> 1 == 10 && size == 20)
  5790. get_timestamp(0);
  5791. else fseek (ifp, size, SEEK_CUR);
  5792. }
  5793. } else if (!memcmp(tag,"IDIT",4) && size < 64) {
  5794. fread (date, 64, 1, ifp);
  5795. date[size] = 0;
  5796. memset (&t, 0, sizeof t);
  5797. if (sscanf (date, "%*s %s %d %d:%d:%d %d", month, &t.tm_mday,
  5798. &t.tm_hour, &t.tm_min, &t.tm_sec, &t.tm_year) == 6) {
  5799. for (i=0; i < 12 && strcasecmp(mon[i],month); i++);
  5800. t.tm_mon = i;
  5801. t.tm_year -= 1900;
  5802. if (mktime(&t) > 0)
  5803. timestamp = mktime(&t);
  5804. }
  5805. } else
  5806. fseek (ifp, size, SEEK_CUR);
  5807. }
  5808. void CLASS parse_smal (int offset, int fsize)
  5809. {
  5810. int ver;
  5811. fseek (ifp, offset+2, SEEK_SET);
  5812. order = 0x4949;
  5813. ver = fgetc(ifp);
  5814. if (ver == 6)
  5815. fseek (ifp, 5, SEEK_CUR);
  5816. if (get4() != fsize) return;
  5817. if (ver > 6) data_offset = get4();
  5818. raw_height = height = get2();
  5819. raw_width = width = get2();
  5820. strcpy (make, "SMaL");
  5821. sprintf (model, "v%d %dx%d", ver, width, height);
  5822. if (ver == 6) load_raw = &CLASS smal_v6_load_raw;
  5823. if (ver == 9) load_raw = &CLASS smal_v9_load_raw;
  5824. }
  5825. void CLASS parse_cine()
  5826. {
  5827. unsigned off_head, off_setup, off_image, i;
  5828. order = 0x4949;
  5829. fseek (ifp, 4, SEEK_SET);
  5830. is_raw = get2() == 2;
  5831. fseek (ifp, 14, SEEK_CUR);
  5832. is_raw *= get4();
  5833. off_head = get4();
  5834. off_setup = get4();
  5835. off_image = get4();
  5836. timestamp = get4();
  5837. if ((i = get4())) timestamp = i;
  5838. fseek (ifp, off_head+4, SEEK_SET);
  5839. raw_width = get4();
  5840. raw_height = get4();
  5841. switch (get2(),get2()) {
  5842. case 8: load_raw = &CLASS eight_bit_load_raw; break;
  5843. case 16: load_raw = &CLASS unpacked_load_raw;
  5844. }
  5845. fseek (ifp, off_setup+792, SEEK_SET);
  5846. strcpy (make, "CINE");
  5847. sprintf (model, "%d", get4());
  5848. fseek (ifp, 12, SEEK_CUR);
  5849. switch ((i=get4()) & 0xffffff) {
  5850. case 3: filters = 0x94949494; break;
  5851. case 4: filters = 0x49494949; break;
  5852. default: is_raw = 0;
  5853. }
  5854. fseek (ifp, 72, SEEK_CUR);
  5855. switch ((get4()+3600) % 360) {
  5856. case 270: flip = 4; break;
  5857. case 180: flip = 1; break;
  5858. case 90: flip = 7; break;
  5859. case 0: flip = 2;
  5860. }
  5861. cam_mul[0] = getreal(11);
  5862. cam_mul[2] = getreal(11);
  5863. maximum = ~(-1 << get4());
  5864. fseek (ifp, 668, SEEK_CUR);
  5865. shutter = get4()/1000000000.0;
  5866. fseek (ifp, off_image, SEEK_SET);
  5867. if (shot_select < is_raw)
  5868. fseek (ifp, shot_select*8, SEEK_CUR);
  5869. data_offset = (INT64) get4() + 8;
  5870. data_offset += (INT64) get4() << 32;
  5871. }
  5872. void CLASS parse_redcine()
  5873. {
  5874. unsigned i, len, rdvo;
  5875. order = 0x4d4d;
  5876. is_raw = 0;
  5877. fseek (ifp, 52, SEEK_SET);
  5878. width = get4();
  5879. height = get4();
  5880. fseek (ifp, 0, SEEK_END);
  5881. fseek (ifp, -(i = ftello(ifp) & 511), SEEK_CUR);
  5882. if (get4() != i || get4() != 0x52454f42) {
  5883. #ifdef DCRAW_VERBOSE
  5884. fprintf (stderr,_("%s: Tail is missing, parsing from head...\n"), ifname);
  5885. #endif
  5886. fseek (ifp, 0, SEEK_SET);
  5887. while ((len = get4()) != EOF) {
  5888. if (get4() == 0x52454456)
  5889. if (is_raw++ == shot_select)
  5890. data_offset = ftello(ifp) - 8;
  5891. fseek (ifp, len-8, SEEK_CUR);
  5892. }
  5893. } else {
  5894. rdvo = get4();
  5895. fseek (ifp, 12, SEEK_CUR);
  5896. is_raw = get4();
  5897. fseeko (ifp, rdvo+8 + shot_select*4, SEEK_SET);
  5898. data_offset = get4();
  5899. }
  5900. }
  5901. char * CLASS foveon_gets (int offset, char *str, int len)
  5902. {
  5903. int i;
  5904. fseek (ifp, offset, SEEK_SET);
  5905. for (i=0; i < len-1; i++)
  5906. if ((str[i] = get2()) == 0) break;
  5907. str[i] = 0;
  5908. return str;
  5909. }
  5910. void CLASS parse_foveon()
  5911. {
  5912. int entries, img=0, off, len, tag, save, i, wide, high, pent, poff[256][2];
  5913. char name[64], value[64];
  5914. order = 0x4949; /* Little-endian */
  5915. fseek (ifp, 36, SEEK_SET);
  5916. flip = get4();
  5917. fseek (ifp, -4, SEEK_END);
  5918. fseek (ifp, get4(), SEEK_SET);
  5919. if (get4() != 0x64434553) return; /* SECd */
  5920. entries = (get4(),get4());
  5921. while (entries--) {
  5922. off = get4();
  5923. len = get4();
  5924. tag = get4();
  5925. save = ftell(ifp);
  5926. fseek (ifp, off, SEEK_SET);
  5927. if (get4() != (0x20434553 | (tag << 24))) return;
  5928. switch (tag) {
  5929. case 0x47414d49: /* IMAG */
  5930. case 0x32414d49: /* IMA2 */
  5931. fseek (ifp, 8, SEEK_CUR);
  5932. pent = get4();
  5933. wide = get4();
  5934. high = get4();
  5935. if (wide > raw_width && high > raw_height) {
  5936. switch (pent) {
  5937. case 5: load_flags = 1;
  5938. case 6: load_raw = &CLASS foveon_sd_load_raw; break;
  5939. case 30: load_raw = &CLASS foveon_dp_load_raw; break;
  5940. default: load_raw = 0;
  5941. }
  5942. raw_width = wide;
  5943. raw_height = high;
  5944. data_offset = off+28;
  5945. }
  5946. fseek (ifp, off+28, SEEK_SET);
  5947. if (fgetc(ifp) == 0xff && fgetc(ifp) == 0xd8
  5948. && thumb_length < len-28) {
  5949. thumb_offset = off+28;
  5950. thumb_length = len-28;
  5951. write_thumb = &CLASS jpeg_thumb;
  5952. }
  5953. if (++img == 2 && !thumb_length) {
  5954. thumb_offset = off+24;
  5955. thumb_width = wide;
  5956. thumb_height = high;
  5957. write_thumb = &CLASS foveon_thumb;
  5958. }
  5959. break;
  5960. case 0x464d4143: /* CAMF */
  5961. meta_offset = off+8;
  5962. meta_length = len-28;
  5963. break;
  5964. case 0x504f5250: /* PROP */
  5965. pent = (get4(),get4());
  5966. fseek (ifp, 12, SEEK_CUR);
  5967. off += pent*8 + 24;
  5968. if ((unsigned) pent > 256) pent=256;
  5969. for (i=0; i < pent*2; i++)
  5970. poff[0][i] = off + get4()*2;
  5971. for (i=0; i < pent; i++) {
  5972. foveon_gets (poff[i][0], name, 64);
  5973. foveon_gets (poff[i][1], value, 64);
  5974. if (!strcmp (name, "ISO"))
  5975. iso_speed = atoi(value);
  5976. if (!strcmp (name, "CAMMANUF"))
  5977. strcpy (make, value);
  5978. if (!strcmp (name, "CAMMODEL"))
  5979. strcpy (model, value);
  5980. if (!strcmp (name, "WB_DESC"))
  5981. strcpy (model2, value);
  5982. if (!strcmp (name, "TIME"))
  5983. timestamp = atoi(value);
  5984. if (!strcmp (name, "EXPTIME"))
  5985. shutter = atoi(value) / 1000000.0;
  5986. if (!strcmp (name, "APERTURE"))
  5987. aperture = atof(value);
  5988. if (!strcmp (name, "FLENGTH"))
  5989. focal_len = atof(value);
  5990. }
  5991. #ifdef LOCALTIME
  5992. timestamp = mktime (gmtime (&timestamp));
  5993. #endif
  5994. }
  5995. fseek (ifp, save, SEEK_SET);
  5996. }
  5997. is_foveon = 1;
  5998. }
  5999. /*
  6000. All matrices are from Adobe DNG Converter unless otherwise noted.
  6001. */
  6002. void CLASS adobe_coeff (const char *t_make, const char *t_model)
  6003. {
  6004. static const struct {
  6005. const char *prefix;
  6006. short t_black, t_maximum, trans[12];
  6007. } table[] = {
  6008. { "AGFAPHOTO DC-833m", 0, 0, /* DJC */
  6009. { 11438,-3762,-1115,-2409,9914,2497,-1227,2295,5300 } },
  6010. { "Apple QuickTake", 0, 0, /* DJC */
  6011. { 21392,-5653,-3353,2406,8010,-415,7166,1427,2078 } },
  6012. { "Canon EOS D2000", 0, 0,
  6013. { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
  6014. { "Canon EOS D6000", 0, 0,
  6015. { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
  6016. { "Canon EOS D30", 0, 0,
  6017. { 9805,-2689,-1312,-5803,13064,3068,-2438,3075,8775 } },
  6018. { "Canon EOS D60", 0, 0xfa0,
  6019. { 6188,-1341,-890,-7168,14489,2937,-2640,3228,8483 } },
  6020. { "Canon EOS 5D Mark III", 0, 0x3c80,
  6021. { 6722,-635,-963,-4287,12460,2028,-908,2162,5668 } },
  6022. { "Canon EOS 5D Mark II", 0, 0x3cf0,
  6023. { 4716,603,-830,-7798,15474,2480,-1496,1937,6651 } },
  6024. { "Canon EOS 5D", 0, 0xe6c,
  6025. { 6347,-479,-972,-8297,15954,2480,-1968,2131,7649 } },
  6026. { "Canon EOS 6D", 0, 0x3c82,
  6027. { 7034,-804,-1014,-4420,12564,2058,-851,1994,5758 } },
  6028. { "Canon EOS 7D", 0, 0x3510,
  6029. { 6844,-996,-856,-3876,11761,2396,-593,1772,6198 } },
  6030. { "Canon EOS 10D", 0, 0xfa0,
  6031. { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
  6032. { "Canon EOS 20Da", 0, 0,
  6033. { 14155,-5065,-1382,-6550,14633,2039,-1623,1824,6561 } },
  6034. { "Canon EOS 20D", 0, 0xfff,
  6035. { 6599,-537,-891,-8071,15783,2424,-1983,2234,7462 } },
  6036. { "Canon EOS 30D", 0, 0,
  6037. { 6257,-303,-1000,-7880,15621,2396,-1714,1904,7046 } },
  6038. { "Canon EOS 40D", 0, 0x3f60,
  6039. { 6071,-747,-856,-7653,15365,2441,-2025,2553,7315 } },
  6040. { "Canon EOS 50D", 0, 0x3d93,
  6041. { 4920,616,-593,-6493,13964,2784,-1774,3178,7005 } },
  6042. { "Canon EOS 60D", 0, 0x2ff7,
  6043. { 6719,-994,-925,-4408,12426,2211,-887,2129,6051 } },
  6044. { "Canon EOS 300D", 0, 0xfa0,
  6045. { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
  6046. { "Canon EOS 350D", 0, 0xfff,
  6047. { 6018,-617,-965,-8645,15881,2975,-1530,1719,7642 } },
  6048. { "Canon EOS 400D", 0, 0xe8e,
  6049. { 7054,-1501,-990,-8156,15544,2812,-1278,1414,7796 } },
  6050. { "Canon EOS 450D", 0, 0x390d,
  6051. { 5784,-262,-821,-7539,15064,2672,-1982,2681,7427 } },
  6052. { "Canon EOS 500D", 0, 0x3479,
  6053. { 4763,712,-646,-6821,14399,2640,-1921,3276,6561 } },
  6054. { "Canon EOS 550D", 0, 0x3dd7,
  6055. { 6941,-1164,-857,-3825,11597,2534,-416,1540,6039 } },
  6056. { "Canon EOS 600D", 0, 0x3510,
  6057. { 6461,-907,-882,-4300,12184,2378,-819,1944,5931 } },
  6058. { "Canon EOS 650D", 0, 0x354d,
  6059. { 6602,-841,-939,-4472,12458,2247,-975,2039,6148 } },
  6060. { "Canon EOS 700D", 0, 0x354d,
  6061. { 6602,-841,-939,-4472,12458,2247,-975,2039,6148 } },
  6062. { "Canon EOS REBEL T5i", 0, 0x354d,
  6063. { 6602,-841,-939,-4472,12458,2247,-975,2039,6148 } },
  6064. { "Canon EOS 1000D", 0, 0xe43,
  6065. { 6771,-1139,-977,-7818,15123,2928,-1244,1437,7533 } },
  6066. { "Canon EOS 1100D", 0, 0x3510,
  6067. { 6444,-904,-893,-4563,12308,2535,-903,2016,6728 } },
  6068. { "Canon EOS M", 0, 0,
  6069. { 6602,-841,-939,-4472,12458,2247,-975,2039,6148 } },
  6070. { "Canon EOS-1Ds Mark III", 0, 0x3bb0,
  6071. { 5859,-211,-930,-8255,16017,2353,-1732,1887,7448 } },
  6072. { "Canon EOS-1Ds Mark II", 0, 0xe80,
  6073. { 6517,-602,-867,-8180,15926,2378,-1618,1771,7633 } },
  6074. { "Canon EOS-1D Mark IV", 0, 0x3bb0,
  6075. { 6014,-220,-795,-4109,12014,2361,-561,1824,5787 } },
  6076. { "Canon EOS-1D Mark III", 0, 0x3bb0,
  6077. { 6291,-540,-976,-8350,16145,2311,-1714,1858,7326 } },
  6078. { "Canon EOS-1D Mark II N", 0, 0xe80,
  6079. { 6240,-466,-822,-8180,15825,2500,-1801,1938,8042 } },
  6080. { "Canon EOS-1D Mark II", 0, 0xe80,
  6081. { 6264,-582,-724,-8312,15948,2504,-1744,1919,8664 } },
  6082. { "Canon EOS-1DS", 0, 0xe20,
  6083. { 4374,3631,-1743,-7520,15212,2472,-2892,3632,8161 } },
  6084. { "Canon EOS-1D X", 0, 0x3c4e,
  6085. { 6847,-614,-1014,-4669,12737,2139,-1197,2488,6846 } },
  6086. { "Canon EOS-1D", 0, 0xe20,
  6087. { 6806,-179,-1020,-8097,16415,1687,-3267,4236,7690 } },
  6088. { "Canon EOS 100D", 0, 0x3cf0,
  6089. { 6602,-841,-939,-4472,12458,2247,-975,2039,6148 } },
  6090. { "Canon EOS REBEL SL1", 0, 0x3cf0,
  6091. { 6602,-841,-939,-4472,12458,2247,-975,2039,6148 } },
  6092. { "Canon EOS", 0, 0,
  6093. { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
  6094. { "Canon PowerShot A530", 0, 0,
  6095. { 0 } }, /* don't want the A5 matrix */
  6096. { "Canon PowerShot A50", 0, 0,
  6097. { -5300,9846,1776,3436,684,3939,-5540,9879,6200,-1404,11175,217 } },
  6098. { "Canon PowerShot A5", 0, 0,
  6099. { -4801,9475,1952,2926,1611,4094,-5259,10164,5947,-1554,10883,547 } },
  6100. { "Canon PowerShot G10", 0, 0,
  6101. { 11093,-3906,-1028,-5047,12492,2879,-1003,1750,5561 } },
  6102. { "Canon PowerShot G11", 0, 0,
  6103. { 12177,-4817,-1069,-1612,9864,2049,-98,850,4471 } },
  6104. { "Canon PowerShot G12", 0, 0,
  6105. { 13244,-5501,-1248,-1508,9858,1935,-270,1083,4366 } },
  6106. { "Canon PowerShot G15", 0, 0,
  6107. { 7474,-2301,-567,-4056,11456,2975,-222,716,4181 } },
  6108. { "Canon PowerShot G1 X", 0, 0,
  6109. { 7378,-1255,-1043,-4088,12251,2048,-876,1946,5805 } },
  6110. { "Canon PowerShot G1", 0, 0,
  6111. { -4778,9467,2172,4743,-1141,4344,-5146,9908,6077,-1566,11051,557 } },
  6112. { "Canon PowerShot G2", 0, 0,
  6113. { 9087,-2693,-1049,-6715,14382,2537,-2291,2819,7790 } },
  6114. { "Canon PowerShot G3", 0, 0,
  6115. { 9212,-2781,-1073,-6573,14189,2605,-2300,2844,7664 } },
  6116. { "Canon PowerShot G5", 0, 0,
  6117. { 9757,-2872,-933,-5972,13861,2301,-1622,2328,7212 } },
  6118. { "Canon PowerShot G6", 0, 0,
  6119. { 9877,-3775,-871,-7613,14807,3072,-1448,1305,7485 } },
  6120. { "Canon PowerShot G9", 0, 0,
  6121. { 7368,-2141,-598,-5621,13254,2625,-1418,1696,5743 } },
  6122. { "Canon PowerShot Pro1", 0, 0,
  6123. { 10062,-3522,-999,-7643,15117,2730,-765,817,7323 } },
  6124. { "Canon PowerShot Pro70", 34, 0,
  6125. { -4155,9818,1529,3939,-25,4522,-5521,9870,6610,-2238,10873,1342 } },
  6126. { "Canon PowerShot Pro90", 0, 0,
  6127. { -4963,9896,2235,4642,-987,4294,-5162,10011,5859,-1770,11230,577 } },
  6128. { "Canon PowerShot S30", 0, 0,
  6129. { 10566,-3652,-1129,-6552,14662,2006,-2197,2581,7670 } },
  6130. { "Canon PowerShot S40", 0, 0,
  6131. { 8510,-2487,-940,-6869,14231,2900,-2318,2829,9013 } },
  6132. { "Canon PowerShot S45", 0, 0,
  6133. { 8163,-2333,-955,-6682,14174,2751,-2077,2597,8041 } },
  6134. { "Canon PowerShot S50", 0, 0,
  6135. { 8882,-2571,-863,-6348,14234,2288,-1516,2172,6569 } },
  6136. { "Canon PowerShot S60", 0, 0,
  6137. { 8795,-2482,-797,-7804,15403,2573,-1422,1996,7082 } },
  6138. { "Canon PowerShot S70", 0, 0,
  6139. { 9976,-3810,-832,-7115,14463,2906,-901,989,7889 } },
  6140. { "Canon PowerShot S90", 0, 0,
  6141. { 12374,-5016,-1049,-1677,9902,2078,-83,852,4683 } },
  6142. { "Canon PowerShot S95", 0, 0,
  6143. { 13440,-5896,-1279,-1236,9598,1931,-180,1001,4651 } },
  6144. { "Canon PowerShot S110", 0, 0,
  6145. { 8039,-2643,-654,-3783,11230,2930,-206,690,4194 } },
  6146. { "Canon PowerShot S100", 0, 0,
  6147. { 7968,-2565,-636,-2873,10697,2513,180,667,4211 } },
  6148. { "Canon PowerShot S110", 0, 0,
  6149. { 8039,-2643,-654,-3783,11230,2930,-206,690,4194 } },
  6150. { "Canon PowerShot SX1 IS", 0, 0,
  6151. { 6578,-259,-502,-5974,13030,3309,-308,1058,4970 } },
  6152. { "Canon PowerShot SX50 HS", 0, 0,
  6153. { 12432,-4753,-1247,-2110,10691,1629,-412,1623,4926 } },
  6154. { "Canon PowerShot A470", 0, 0, /* DJC */
  6155. { 12513,-4407,-1242,-2680,10276,2405,-878,2215,4734 } },
  6156. { "Canon PowerShot A610", 0, 0, /* DJC */
  6157. { 15591,-6402,-1592,-5365,13198,2168,-1300,1824,5075 } },
  6158. { "Canon PowerShot A620", 0, 0, /* DJC */
  6159. { 15265,-6193,-1558,-4125,12116,2010,-888,1639,5220 } },
  6160. { "Canon PowerShot A630", 0, 0, /* DJC */
  6161. { 14201,-5308,-1757,-6087,14472,1617,-2191,3105,5348 } },
  6162. { "Canon PowerShot A640", 0, 0, /* DJC */
  6163. { 13124,-5329,-1390,-3602,11658,1944,-1612,2863,4885 } },
  6164. { "Canon PowerShot A650", 0, 0, /* DJC */
  6165. { 9427,-3036,-959,-2581,10671,1911,-1039,1982,4430 } },
  6166. { "Canon PowerShot A720", 0, 0, /* DJC */
  6167. { 14573,-5482,-1546,-1266,9799,1468,-1040,1912,3810 } },
  6168. { "Canon PowerShot S3 IS", 0, 0, /* DJC */
  6169. { 14062,-5199,-1446,-4712,12470,2243,-1286,2028,4836 } },
  6170. { "Canon PowerShot SX110 IS", 0, 0, /* DJC */
  6171. { 14134,-5576,-1527,-1991,10719,1273,-1158,1929,3581 } },
  6172. { "Canon PowerShot SX220", 0, 0, /* DJC */
  6173. { 13898,-5076,-1447,-1405,10109,1297,-244,1860,3687 } },
  6174. { "Canon PowerShot SX50", 0, 0,
  6175. { 12432,-4753,-1247,-2110,10691,1629,-412,1623,4926 } },
  6176. { "CASIO EX-S20", 0, 0, /* DJC */
  6177. { 11634,-3924,-1128,-4968,12954,2015,-1588,2648,7206 } },
  6178. { "CASIO EX-Z750", 0, 0, /* DJC */
  6179. { 10819,-3873,-1099,-4903,13730,1175,-1755,3751,4632 } },
  6180. { "CASIO EX-Z10", 128, 0xfff, /* DJC */
  6181. { 9790,-3338,-603,-2321,10222,2099,-344,1273,4799 } },
  6182. { "CINE 650", 0, 0,
  6183. { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
  6184. { "CINE 660", 0, 0,
  6185. { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
  6186. { "CINE", 0, 0,
  6187. { 20183,-4295,-423,-3940,15330,3985,-280,4870,9800 } },
  6188. { "Contax N Digital", 0, 0xf1e,
  6189. { 7777,1285,-1053,-9280,16543,2916,-3677,5679,7060 } },
  6190. { "EPSON R-D1", 0, 0,
  6191. { 6827,-1878,-732,-8429,16012,2564,-704,592,7145 } },
  6192. { "FUJIFILM E550", 0, 0,
  6193. { 11044,-3888,-1120,-7248,15168,2208,-1531,2277,8069 } },
  6194. { "FUJIFILM E900", 0, 0,
  6195. { 9183,-2526,-1078,-7461,15071,2574,-2022,2440,8639 } },
  6196. { "FUJIFILM F5", 0, 0,
  6197. { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
  6198. { "FUJIFILM F6", 0, 0,
  6199. { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
  6200. { "FUJIFILM F77", 0, 0xfe9,
  6201. { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
  6202. { "FUJIFILM F7", 0, 0,
  6203. { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
  6204. { "FUJIFILM F800", 0, 0,
  6205. { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
  6206. { "FUJIFILM F8", 0, 0,
  6207. { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
  6208. { "FUJIFILM S100FS", 514, 0,
  6209. { 11521,-4355,-1065,-6524,13767,3058,-1466,1984,6045 } },
  6210. { "FUJIFILM S200EXR", 512, 0x3fff,
  6211. { 11401,-4498,-1312,-5088,12751,2613,-838,1568,5941 } },
  6212. { "FUJIFILM S20Pro", 0, 0,
  6213. { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
  6214. { "FUJIFILM S2Pro", 128, 0,
  6215. { 12492,-4690,-1402,-7033,15423,1647,-1507,2111,7697 } },
  6216. { "FUJIFILM S3Pro", 0, 0,
  6217. { 11807,-4612,-1294,-8927,16968,1988,-2120,2741,8006 } },
  6218. { "FUJIFILM S5Pro", 0, 0,
  6219. { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
  6220. { "FUJIFILM S5000", 0, 0,
  6221. { 8754,-2732,-1019,-7204,15069,2276,-1702,2334,6982 } },
  6222. { "FUJIFILM S5100", 0, 0,
  6223. { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
  6224. { "FUJIFILM S5500", 0, 0,
  6225. { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
  6226. { "FUJIFILM S5200", 0, 0,
  6227. { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
  6228. { "FUJIFILM S5600", 0, 0,
  6229. { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
  6230. { "FUJIFILM S6", 0, 0,
  6231. { 12628,-4887,-1401,-6861,14996,1962,-2198,2782,7091 } },
  6232. { "FUJIFILM S7000", 0, 0,
  6233. { 10190,-3506,-1312,-7153,15051,2238,-2003,2399,7505 } },
  6234. { "FUJIFILM S9000", 0, 0,
  6235. { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
  6236. { "FUJIFILM S9500", 0, 0,
  6237. { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
  6238. { "FUJIFILM S9100", 0, 0,
  6239. { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
  6240. { "FUJIFILM S9600", 0, 0,
  6241. { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
  6242. { "FUJIFILM IS-1", 0, 0,
  6243. { 21461,-10807,-1441,-2332,10599,1999,289,875,7703 } },
  6244. { "FUJIFILM IS Pro", 0, 0,
  6245. { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
  6246. { "FUJIFILM HS10 HS11", 0, 0xf68,
  6247. { 12440,-3954,-1183,-1123,9674,1708,-83,1614,4086 } },
  6248. { "FUJIFILM HS20EXR", 0, 0,
  6249. { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
  6250. { "FUJIFILM HS3", 0, 0,
  6251. { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
  6252. { "FUJIFILM HS50EXR", 0, 0,
  6253. { 12085,-4727,-953,-3257,11489,2002,-511,2046,4592 } },
  6254. { "FUJIFILM SL1000", 0, 0,
  6255. { 11705,-4262,-1107,-2282,10791,1709,-555,1713,4945 } },
  6256. { "FUJIFILM X100S", 0, 0,
  6257. { 10592,-4262,-1008,-3514,11355,2465,-870,2025,6386 } },
  6258. { "FUJIFILM X100", 0, 0,
  6259. { 12161,-4457,-1069,-5034,12874,2400,-795,1724,6904 } },
  6260. { "FUJIFILM X10", 0, 0,
  6261. { 13509,-6199,-1254,-4430,12733,1865,-331,1441,5022 } },
  6262. { "FUJIFILM X20", 0, 0,
  6263. { 11768,-4971,-1133,-4904,12927,2183,-480,1723,4605 } },
  6264. { "FUJIFILM XF1", 0, 0,
  6265. { 13509,-6199,-1254,-4430,12733,1865,-331,1441,5022 } },
  6266. { "FUJIFILM X-Pro1", 0, 0,
  6267. { 10413,-3996,-993,-3721,11640,2361,-733,1540,6011 } },
  6268. { "FUJIFILM X-E1", 0, 0,
  6269. { 10413,-3996,-993,-3721,11640,2361,-733,1540,6011 } },
  6270. { "FUJIFILM XF1", 0, 0,
  6271. { 13509,-6199,-1254,-4430,12733,1865,-331,1441,5022 } },
  6272. { "FUJIFILM X-S1", 0, 0,
  6273. { 13509,-6199,-1254,-4430,12733,1865,-331,1441,5022 } },
  6274. { "Imacon Ixpress", 0, 0, /* DJC */
  6275. { 7025,-1415,-704,-5188,13765,1424,-1248,2742,6038 } },
  6276. { "KODAK NC2000", 0, 0,
  6277. { 13891,-6055,-803,-465,9919,642,2121,82,1291 } },
  6278. { "Kodak DCS315C", 8, 0,
  6279. { 17523,-4827,-2510,756,8546,-137,6113,1649,2250 } },
  6280. { "Kodak DCS330C", 8, 0,
  6281. { 20620,-7572,-2801,-103,10073,-396,3551,-233,2220 } },
  6282. { "KODAK DCS420", 0, 0,
  6283. { 10868,-1852,-644,-1537,11083,484,2343,628,2216 } },
  6284. { "KODAK DCS460", 0, 0,
  6285. { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
  6286. { "KODAK EOSDCS1", 0, 0,
  6287. { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
  6288. { "KODAK EOSDCS3B", 0, 0,
  6289. { 9898,-2700,-940,-2478,12219,206,1985,634,1031 } },
  6290. { "Kodak DCS520C", 178, 0,
  6291. { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
  6292. { "Kodak DCS560C", 177, 0,
  6293. { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
  6294. { "Kodak DCS620C", 177, 0,
  6295. { 23617,-10175,-3149,-2054,11749,-272,2586,-489,3453 } },
  6296. { "Kodak DCS620X", 176, 0,
  6297. { 13095,-6231,154,12221,-21,-2137,895,4602,2258 } },
  6298. { "Kodak DCS660C", 173, 0,
  6299. { 18244,-6351,-2739,-791,11193,-521,3711,-129,2802 } },
  6300. { "Kodak DCS720X", 0, 0,
  6301. { 11775,-5884,950,9556,1846,-1286,-1019,6221,2728 } },
  6302. { "Kodak DCS760C", 0, 0,
  6303. { 16623,-6309,-1411,-4344,13923,323,2285,274,2926 } },
  6304. { "Kodak DCS Pro SLR", 0, 0,
  6305. { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
  6306. { "Kodak DCS Pro 14nx", 0, 0,
  6307. { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
  6308. { "Kodak DCS Pro 14", 0, 0,
  6309. { 7791,3128,-776,-8588,16458,2039,-2455,4006,6198 } },
  6310. { "Kodak ProBack645", 0, 0,
  6311. { 16414,-6060,-1470,-3555,13037,473,2545,122,4948 } },
  6312. { "Kodak ProBack", 0, 0,
  6313. { 21179,-8316,-2918,-915,11019,-165,3477,-180,4210 } },
  6314. { "KODAK P712", 0, 0,
  6315. { 9658,-3314,-823,-5163,12695,2768,-1342,1843,6044 } },
  6316. { "KODAK P850", 0, 0xf7c,
  6317. { 10511,-3836,-1102,-6946,14587,2558,-1481,1792,6246 } },
  6318. { "KODAK P880", 0, 0xfff,
  6319. { 12805,-4662,-1376,-7480,15267,2360,-1626,2194,7904 } },
  6320. { "KODAK EasyShare Z980", 0, 0,
  6321. { 11313,-3559,-1101,-3893,11891,2257,-1214,2398,4908 } },
  6322. { "KODAK EasyShare Z981", 0, 0,
  6323. { 12729,-4717,-1188,-1367,9187,2582,274,860,4411 } },
  6324. { "KODAK EasyShare Z990", 0, 0xfed,
  6325. { 11749,-4048,-1309,-1867,10572,1489,-138,1449,4522 } },
  6326. { "KODAK EASYSHARE Z1015", 0, 0xef1,
  6327. { 11265,-4286,-992,-4694,12343,2647,-1090,1523,5447 } },
  6328. { "Leaf CMost", 0, 0,
  6329. { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
  6330. { "Leaf Valeo 6", 0, 0,
  6331. { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
  6332. { "Leaf Aptus 54S", 0, 0,
  6333. { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
  6334. { "Leaf Aptus 65", 0, 0,
  6335. { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
  6336. { "Leaf Aptus 75", 0, 0,
  6337. { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
  6338. { "Leaf", 0, 0,
  6339. { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
  6340. { "Mamiya ZD", 0, 0,
  6341. { 7645,2579,-1363,-8689,16717,2015,-3712,5941,5961 } },
  6342. { "Micron 2010", 110, 0, /* DJC */
  6343. { 16695,-3761,-2151,155,9682,163,3433,951,4904 } },
  6344. { "Minolta DiMAGE 5", 0, 0xf7d,
  6345. { 8983,-2942,-963,-6556,14476,2237,-2426,2887,8014 } },
  6346. { "Minolta DiMAGE 7Hi", 0, 0xf7d,
  6347. { 11368,-3894,-1242,-6521,14358,2339,-2475,3056,7285 } },
  6348. { "Minolta DiMAGE 7", 0, 0xf7d,
  6349. { 9144,-2777,-998,-6676,14556,2281,-2470,3019,7744 } },
  6350. { "Minolta DiMAGE A1", 0, 0xf8b,
  6351. { 9274,-2547,-1167,-8220,16323,1943,-2273,2720,8340 } },
  6352. { "MINOLTA DiMAGE A200", 0, 0,
  6353. { 8560,-2487,-986,-8112,15535,2771,-1209,1324,7743 } },
  6354. { "Minolta DiMAGE A2", 0, 0xf8f,
  6355. { 9097,-2726,-1053,-8073,15506,2762,-966,981,7763 } },
  6356. { "Minolta DiMAGE Z2", 0, 0, /* DJC */
  6357. { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
  6358. { "MINOLTA DYNAX 5", 0, 0xffb,
  6359. { 10284,-3283,-1086,-7957,15762,2316,-829,882,6644 } },
  6360. { "MINOLTA DYNAX 7", 0, 0xffb,
  6361. { 10239,-3104,-1099,-8037,15727,2451,-927,925,6871 } },
  6362. { "MOTOROLA PIXL", 0, 0, /* DJC */
  6363. { 8898,-989,-1033,-3292,11619,1674,-661,3178,5216 } },
  6364. { "NIKON D100", 0, 0,
  6365. { 5902,-933,-782,-8983,16719,2354,-1402,1455,6464 } },
  6366. { "NIKON D1H", 0, 0,
  6367. { 7577,-2166,-926,-7454,15592,1934,-2377,2808,8606 } },
  6368. { "NIKON D1X", 0, 0,
  6369. { 7702,-2245,-975,-9114,17242,1875,-2679,3055,8521 } },
  6370. { "NIKON D1", 0, 0, /* multiplied by 2.218750, 1.0, 1.148438 */
  6371. { 16772,-4726,-2141,-7611,15713,1972,-2846,3494,9521 } },
  6372. { "NIKON D200", 0, 0xfbc,
  6373. { 8367,-2248,-763,-8758,16447,2422,-1527,1550,8053 } },
  6374. { "NIKON D2H", 0, 0,
  6375. { 5710,-901,-615,-8594,16617,2024,-2975,4120,6830 } },
  6376. { "NIKON D2X", 0, 0,
  6377. { 10231,-2769,-1255,-8301,15900,2552,-797,680,7148 } },
  6378. { "NIKON D3000", 0, 0,
  6379. { 8736,-2458,-935,-9075,16894,2251,-1354,1242,8263 } },
  6380. { "NIKON D3100", 0, 0,
  6381. { 7911,-2167,-813,-5327,13150,2408,-1288,2483,7968 } },
  6382. { "NIKON D3200", 0, 0xfb9,
  6383. { 7013,-1408,-635,-5268,12902,2640,-1470,2801,7379 } },
  6384. { "NIKON D300", 0, 0,
  6385. { 9030,-1992,-715,-8465,16302,2255,-2689,3217,8069 } },
  6386. { "NIKON D3X", 0, 0,
  6387. { 7171,-1986,-648,-8085,15555,2718,-2170,2512,7457 } },
  6388. { "NIKON D3S", 0, 0,
  6389. { 8828,-2406,-694,-4874,12603,2541,-660,1509,7587 } },
  6390. { "NIKON D3", 0, 0,
  6391. { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
  6392. { "NIKON D40X", 0, 0,
  6393. { 8819,-2543,-911,-9025,16928,2151,-1329,1213,8449 } },
  6394. { "NIKON D40", 0, 0,
  6395. { 6992,-1668,-806,-8138,15748,2543,-874,850,7897 } },
  6396. { "NIKON D4", 0, 0,
  6397. { 8598,-2848,-857,-5618,13606,2195,-1002,1773,7137 } },
  6398. { "NIKON D5000", 0, 0xf00,
  6399. { 7309,-1403,-519,-8474,16008,2622,-2433,2826,8064 } },
  6400. { "NIKON D5100", 0, 0x3de6,
  6401. { 8198,-2239,-724,-4871,12389,2798,-1043,2050,7181 } },
  6402. { "NIKON D5200", 0, 0,
  6403. { 8322,-3112,-1047,-6367,14342,2179,-988,1638,6394 } },
  6404. { "NIKON D50", 0, 0,
  6405. { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
  6406. { "NIKON D600", 0, 0x3e07,
  6407. { 8178,-2245,-609,-4857,12394,2776,-1207,2086,7298 } },
  6408. { "NIKON D60", 0, 0,
  6409. { 8736,-2458,-935,-9075,16894,2251,-1354,1242,8263 } },
  6410. { "NIKON D7000", 0, 0,
  6411. { 8198,-2239,-724,-4871,12389,2798,-1043,2050,7181 } },
  6412. { "NIKON D700", 0, 0,
  6413. { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
  6414. { "NIKON D70", 0, 0,
  6415. { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
  6416. { "NIKON D7100", 0, 0,
  6417. { 8322,-3112,-1047,-6367,14342,2179,-988,1638,6394 } },
  6418. { "NIKON D800", 0, 0,
  6419. { 7866,-2108,-555,-4869,12483,2681,-1176,2069,7501 } },
  6420. { "NIKON D80", 0, 0,
  6421. { 8629,-2410,-883,-9055,16940,2171,-1490,1363,8520 } },
  6422. { "NIKON D90", 0, 0xf00,
  6423. { 7309,-1403,-519,-8474,16008,2622,-2434,2826,8064 } },
  6424. { "NIKON E950", 0, 0x3dd, /* DJC */
  6425. { -3746,10611,1665,9621,-1734,2114,-2389,7082,3064,3406,6116,-244 } },
  6426. { "NIKON E995", 0, 0, /* copied from E5000 */
  6427. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  6428. { "NIKON E2100", 0, 0, /* copied from Z2, new white balance */
  6429. { 13142,-4152,-1596,-4655,12374,2282,-1769,2696,6711} },
  6430. { "NIKON E2500", 0, 0,
  6431. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  6432. { "NIKON E3200", 0, 0, /* DJC */
  6433. { 9846,-2085,-1019,-3278,11109,2170,-774,2134,5745 } },
  6434. { "NIKON E4300", 0, 0, /* copied from Minolta DiMAGE Z2 */
  6435. { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
  6436. { "NIKON E4500", 0, 0,
  6437. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  6438. { "NIKON E5000", 0, 0,
  6439. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  6440. { "NIKON E5400", 0, 0,
  6441. { 9349,-2987,-1001,-7919,15766,2266,-2098,2680,6839 } },
  6442. { "NIKON E5700", 0, 0,
  6443. { -5368,11478,2368,5537,-113,3148,-4969,10021,5782,778,9028,211 } },
  6444. { "NIKON E8400", 0, 0,
  6445. { 7842,-2320,-992,-8154,15718,2599,-1098,1342,7560 } },
  6446. { "NIKON E8700", 0, 0,
  6447. { 8489,-2583,-1036,-8051,15583,2643,-1307,1407,7354 } },
  6448. { "NIKON E8800", 0, 0,
  6449. { 7971,-2314,-913,-8451,15762,2894,-1442,1520,7610 } },
  6450. { "NIKON COOLPIX A", 0, 0,
  6451. { 8198,-2239,-724,-4871,12389,2798,-1043,2050,7181 } },
  6452. { "NIKON COOLPIX P330", 0, 0,
  6453. { 10321,-3920,-931,-2750,11146,1824,-442,1545,5539 } },
  6454. { "NIKON COOLPIX P6000", 0, 0,
  6455. { 9698,-3367,-914,-4706,12584,2368,-837,968,5801 } },
  6456. { "NIKON COOLPIX P7000", 0, 0,
  6457. { 11432,-3679,-1111,-3169,11239,2202,-791,1380,4455 } },
  6458. { "NIKON COOLPIX P7100", 0, 0,
  6459. { 11053,-4269,-1024,-1976,10182,2088,-526,1263,4469 } },
  6460. { "NIKON COOLPIX P7700", 200, 0,
  6461. { 10321,-3920,-931,-2750,11146,1824,-442,1545,5539 } },
  6462. { "NIKON 1 J3", 0, 0,
  6463. { 6588,-1305,-693,-3277,10987,2634,-355,2016,5106 } },
  6464. { "NIKON 1 S1", 0, 0,
  6465. { 8994,-2667,-865,-4594,12324,2552,-699,1786,6260 } },
  6466. { "NIKON 1 V2", 0, 0,
  6467. { 6588,-1305,-693,-3277,10987,2634,-355,2016,5106 } },
  6468. { "NIKON 1 ", 0, 0,
  6469. { 8994,-2667,-865,-4594,12324,2552,-699,1786,6260 } },
  6470. { "OLYMPUS C5050", 0, 0,
  6471. { 10508,-3124,-1273,-6079,14294,1901,-1653,2306,6237 } },
  6472. { "OLYMPUS C5060", 0, 0,
  6473. { 10445,-3362,-1307,-7662,15690,2058,-1135,1176,7602 } },
  6474. { "OLYMPUS C7070", 0, 0,
  6475. { 10252,-3531,-1095,-7114,14850,2436,-1451,1723,6365 } },
  6476. { "OLYMPUS C70", 0, 0,
  6477. { 10793,-3791,-1146,-7498,15177,2488,-1390,1577,7321 } },
  6478. { "OLYMPUS C80", 0, 0,
  6479. { 8606,-2509,-1014,-8238,15714,2703,-942,979,7760 } },
  6480. { "OLYMPUS E-10", 0, 0xffc,
  6481. { 12745,-4500,-1416,-6062,14542,1580,-1934,2256,6603 } },
  6482. { "OLYMPUS E-1", 0, 0,
  6483. { 11846,-4767,-945,-7027,15878,1089,-2699,4122,8311 } },
  6484. { "OLYMPUS E-20", 0, 0xffc,
  6485. { 13173,-4732,-1499,-5807,14036,1895,-2045,2452,7142 } },
  6486. { "OLYMPUS E-300", 0, 0,
  6487. { 7828,-1761,-348,-5788,14071,1830,-2853,4518,6557 } },
  6488. { "OLYMPUS E-330", 0, 0,
  6489. { 8961,-2473,-1084,-7979,15990,2067,-2319,3035,8249 } },
  6490. { "OLYMPUS E-30", 0, 0xfbc,
  6491. { 8144,-1861,-1111,-7763,15894,1929,-1865,2542,7607 } },
  6492. { "OLYMPUS E-3", 0, 0xf99,
  6493. { 9487,-2875,-1115,-7533,15606,2010,-1618,2100,7389 } },
  6494. { "OLYMPUS E-400", 0, 0,
  6495. { 6169,-1483,-21,-7107,14761,2536,-2904,3580,8568 } },
  6496. { "OLYMPUS E-410", 0, 0xf6a,
  6497. { 8856,-2582,-1026,-7761,15766,2082,-2009,2575,7469 } },
  6498. { "OLYMPUS E-420", 0, 0xfd7,
  6499. { 8746,-2425,-1095,-7594,15612,2073,-1780,2309,7416 } },
  6500. { "OLYMPUS E-450", 0, 0xfd2,
  6501. { 8745,-2425,-1095,-7594,15613,2073,-1780,2309,7416 } },
  6502. { "OLYMPUS E-500", 0, 0,
  6503. { 8136,-1968,-299,-5481,13742,1871,-2556,4205,6630 } },
  6504. { "OLYMPUS E-510", 0, 0xf6a,
  6505. { 8785,-2529,-1033,-7639,15624,2112,-1783,2300,7817 } },
  6506. { "OLYMPUS E-520", 0, 0xfd2,
  6507. { 8344,-2322,-1020,-7596,15635,2048,-1748,2269,7287 } },
  6508. { "OLYMPUS E-5", 0, 0xeec,
  6509. { 11200,-3783,-1325,-4576,12593,2206,-695,1742,7504 } },
  6510. { "OLYMPUS E-600", 0, 0xfaf,
  6511. { 8453,-2198,-1092,-7609,15681,2008,-1725,2337,7824 } },
  6512. { "OLYMPUS E-620", 0, 0xfaf,
  6513. { 8453,-2198,-1092,-7609,15681,2008,-1725,2337,7824 } },
  6514. { "OLYMPUS E-P1", 0, 0xffd,
  6515. { 8343,-2050,-1021,-7715,15705,2103,-1831,2380,8235 } },
  6516. { "OLYMPUS E-P2", 0, 0xffd,
  6517. { 8343,-2050,-1021,-7715,15705,2103,-1831,2380,8235 } },
  6518. { "OLYMPUS E-P3", 0, 0,
  6519. { 7575,-2159,-571,-3722,11341,2725,-1434,2819,6271 } },
  6520. { "OLYMPUS E-PL1s", 0, 0,
  6521. { 11409,-3872,-1393,-4572,12757,2003,-709,1810,7415 } },
  6522. { "OLYMPUS E-PL1", 0, 0,
  6523. { 11408,-4289,-1215,-4286,12385,2118,-387,1467,7787 } },
  6524. { "OLYMPUS E-PL2", 0, 0,
  6525. { 15030,-5552,-1806,-3987,12387,1767,-592,1670,7023 } },
  6526. { "OLYMPUS E-PL3", 0, 0,
  6527. { 7575,-2159,-571,-3722,11341,2725,-1434,2819,6271 } },
  6528. { "OLYMPUS E-PL5", 0, 0xfcb,
  6529. { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
  6530. { "OLYMPUS E-PM1", 0, 0,
  6531. { 7575,-2159,-571,-3722,11341,2725,-1434,2819,6271 } },
  6532. { "OLYMPUS E-PM2", 0, 0,
  6533. { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
  6534. { "OLYMPUS E-M5", 0, 0xfe1,
  6535. { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
  6536. { "OLYMPUS SP350", 0, 0,
  6537. { 12078,-4836,-1069,-6671,14306,2578,-786,939,7418 } },
  6538. { "OLYMPUS SP3", 0, 0,
  6539. { 11766,-4445,-1067,-6901,14421,2707,-1029,1217,7572 } },
  6540. { "OLYMPUS SP500UZ", 0, 0xfff,
  6541. { 9493,-3415,-666,-5211,12334,3260,-1548,2262,6482 } },
  6542. { "OLYMPUS SP510UZ", 0, 0xffe,
  6543. { 10593,-3607,-1010,-5881,13127,3084,-1200,1805,6721 } },
  6544. { "OLYMPUS SP550UZ", 0, 0xffe,
  6545. { 11597,-4006,-1049,-5432,12799,2957,-1029,1750,6516 } },
  6546. { "OLYMPUS SP560UZ", 0, 0xff9,
  6547. { 10915,-3677,-982,-5587,12986,2911,-1168,1968,6223 } },
  6548. { "OLYMPUS SP570UZ", 0, 0,
  6549. { 11522,-4044,-1146,-4736,12172,2904,-988,1829,6039 } },
  6550. { "OLYMPUS XZ-10", 0, 0,
  6551. { 9777,-3483,-925,-2886,11297,1800,-602,1663,5134 } },
  6552. { "OLYMPUS XZ-1", 0, 0,
  6553. { 10901,-4095,-1074,-1141,9208,2293,-62,1417,5158 } },
  6554. { "OLYMPUS XZ-2", 0, 0,
  6555. { 9777,-3483,-925,-2886,11297,1800,-602,1663,5134 } },
  6556. { "OmniVision ov5647", 0, 0, /* DJC */
  6557. { 12782,-4059,-379,-478,9066,1413,1340,1513,5176 } },
  6558. { "PENTAX *ist DL2", 0, 0,
  6559. { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
  6560. { "PENTAX *ist DL", 0, 0,
  6561. { 10829,-2838,-1115,-8339,15817,2696,-837,680,11939 } },
  6562. { "PENTAX *ist DS2", 0, 0,
  6563. { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
  6564. { "PENTAX *ist DS", 0, 0,
  6565. { 10371,-2333,-1206,-8688,16231,2602,-1230,1116,11282 } },
  6566. { "PENTAX *ist D", 0, 0,
  6567. { 9651,-2059,-1189,-8881,16512,2487,-1460,1345,10687 } },
  6568. { "PENTAX K10D", 0, 0,
  6569. { 9566,-2863,-803,-7170,15172,2112,-818,803,9705 } },
  6570. { "PENTAX K1", 0, 0,
  6571. { 11095,-3157,-1324,-8377,15834,2720,-1108,947,11688 } },
  6572. { "PENTAX K20D", 0, 0,
  6573. { 9427,-2714,-868,-7493,16092,1373,-2199,3264,7180 } },
  6574. { "PENTAX K200D", 0, 0,
  6575. { 9186,-2678,-907,-8693,16517,2260,-1129,1094,8524 } },
  6576. { "PENTAX K2000", 0, 0,
  6577. { 11057,-3604,-1155,-5152,13046,2329,-282,375,8104 } },
  6578. { "PENTAX K-m", 0, 0,
  6579. { 11057,-3604,-1155,-5152,13046,2329,-282,375,8104 } },
  6580. { "PENTAX K-x", 0, 0,
  6581. { 8843,-2837,-625,-5025,12644,2668,-411,1234,7410 } },
  6582. { "PENTAX K-r", 0, 0,
  6583. { 9895,-3077,-850,-5304,13035,2521,-883,1768,6936 } },
  6584. { "PENTAX K-30", 0, 0,
  6585. { 8833,-2670,-1183,-3995,12301,1881,-979,1717,6527 } },
  6586. { "PENTAX K-5 II s", 0, 0,
  6587. { 8366,-2528,-1120,-3995,12301,1881,-998,1749,6649 } },
  6588. { "PENTAX K-5 II", 0, 0,
  6589. { 8773,-2651,-1175,-3995,12301,1881,-973,1706,6486 } },
  6590. { "PENTAX K-5", 0, 0,
  6591. { 8713,-2833,-743,-4342,11900,2772,-722,1543,6247 } },
  6592. { "PENTAX K-7", 0, 0,
  6593. { 9142,-2947,-678,-8648,16967,1663,-2224,2898,8615 } },
  6594. { "PENTAX MX-1", 0, 0,
  6595. { 8804,-2523,-1238,-2423,11627,860,-682,1774,4753 } },
  6596. { "PENTAX Q10", 0, 0,
  6597. { 12995,-5593,-1107,-1879,10139,2027,-64,1233,4919 } },
  6598. { "PENTAX 645D", 0, 0x3e00,
  6599. { 10646,-3593,-1158,-3329,11699,1831,-667,2874,6287 } },
  6600. { "Panasonic DMC-FZ8", 0, 0xf7f,
  6601. { 8986,-2755,-802,-6341,13575,3077,-1476,2144,6379 } },
  6602. { "Panasonic DMC-FZ18", 0, 0,
  6603. { 9932,-3060,-935,-5809,13331,2753,-1267,2155,5575 } },
  6604. { "Panasonic DMC-FZ28", 15, 0xf96,
  6605. { 10109,-3488,-993,-5412,12812,2916,-1305,2140,5543 } },
  6606. { "Panasonic DMC-FZ30", 0, 0xf94,
  6607. { 10976,-4029,-1141,-7918,15491,2600,-1670,2071,8246 } },
  6608. { "Panasonic DMC-FZ3", 143, 0,
  6609. { 9938,-2780,-890,-4604,12393,2480,-1117,2304,4620 } },
  6610. { "Panasonic DMC-FZ4", 143, 0,
  6611. { 13639,-5535,-1371,-1698,9633,2430,316,1152,4108 } },
  6612. { "Panasonic DMC-FZ50", 0, 0,
  6613. { 7906,-2709,-594,-6231,13351,3220,-1922,2631,6537 } },
  6614. { "LEICA V-LUX1", 0, 0,
  6615. { 7906,-2709,-594,-6231,13351,3220,-1922,2631,6537 } },
  6616. { "Panasonic DMC-L10", 15, 0xf96,
  6617. { 8025,-1942,-1050,-7920,15904,2100,-2456,3005,7039 } },
  6618. { "Panasonic DMC-L1", 0, 0xf7f,
  6619. { 8054,-1885,-1025,-8349,16367,2040,-2805,3542,7629 } },
  6620. { "LEICA DIGILUX 3", 0, 0xf7f,
  6621. { 8054,-1885,-1025,-8349,16367,2040,-2805,3542,7629 } },
  6622. { "Panasonic DMC-LC1", 0, 0,
  6623. { 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
  6624. { "LEICA DIGILUX 2", 0, 0,
  6625. { 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
  6626. { "Panasonic DMC-LX1", 0, 0xf7f,
  6627. { 10704,-4187,-1230,-8314,15952,2501,-920,945,8927 } },
  6628. { "LEICA D-LUX2", 0, 0xf7f,
  6629. { 10704,-4187,-1230,-8314,15952,2501,-920,945,8927 } },
  6630. { "Panasonic DMC-LX2", 0, 0,
  6631. { 8048,-2810,-623,-6450,13519,3272,-1700,2146,7049 } },
  6632. { "LEICA D-LUX3", 0, 0,
  6633. { 8048,-2810,-623,-6450,13519,3272,-1700,2146,7049 } },
  6634. { "Panasonic DMC-LX3", 15, 0,
  6635. { 8128,-2668,-655,-6134,13307,3161,-1782,2568,6083 } },
  6636. { "LEICA D-LUX 4", 15, 0,
  6637. { 8128,-2668,-655,-6134,13307,3161,-1782,2568,6083 } },
  6638. { "Panasonic DMC-LX5", 143, 0,
  6639. { 10909,-4295,-948,-1333,9306,2399,22,1738,4582 } },
  6640. { "Panasonic DMC-LX7", 143, 0,
  6641. { 10148,-3743,-991,-2837,11366,1659,-701,1893,4899 } },
  6642. { "LEICA D-LUX 5", 143, 0,
  6643. { 10909,-4295,-948,-1333,9306,2399,22,1738,4582 } },
  6644. { "Panasonic DMC-LX7", 143, 0,
  6645. { 10148,-3743,-991,-2837,11366,1659,-701,1893,4899 } },
  6646. { "LEICA D-LUX 6", 143, 0,
  6647. { 10148,-3743,-991,-2837,11366,1659,-701,1893,4899 } },
  6648. { "Panasonic DMC-FZ100", 143, 0xfff,
  6649. { 16197,-6146,-1761,-2393,10765,1869,366,2238,5248 } },
  6650. { "LEICA V-LUX 2", 143, 0xfff,
  6651. { 16197,-6146,-1761,-2393,10765,1869,366,2238,5248 } },
  6652. { "Panasonic DMC-FZ150", 143, 0xfff,
  6653. { 11904,-4541,-1189,-2355,10899,1662,-296,1586,4289 } },
  6654. { "LEICA V-LUX 3", 143, 0xfff,
  6655. { 11904,-4541,-1189,-2355,10899,1662,-296,1586,4289 } },
  6656. { "Panasonic DMC-FZ200", 143, 0xfff,
  6657. { 8112,-2563,-740,-3730,11784,2197,-941,2075,4933 } },
  6658. { "LEICA V-LUX 4", 143, 0xfff,
  6659. { 8112,-2563,-740,-3730,11784,2197,-941,2075,4933 } },
  6660. { "Panasonic DMC-FX150", 15, 0xfff,
  6661. { 9082,-2907,-925,-6119,13377,3058,-1797,2641,5609 } },
  6662. { "Panasonic DMC-G10", 0, 0,
  6663. { 10113,-3400,-1114,-4765,12683,2317,-377,1437,6710 } },
  6664. { "Panasonic DMC-G1", 15, 0xf94,
  6665. { 8199,-2065,-1056,-8124,16156,2033,-2458,3022,7220 } },
  6666. { "Panasonic DMC-G2", 15, 0xf3c,
  6667. { 10113,-3400,-1114,-4765,12683,2317,-377,1437,6710 } },
  6668. { "Panasonic DMC-G3", 143, 0xfff,
  6669. { 6763,-1919,-863,-3868,11515,2684,-1216,2387,5879 } },
  6670. { "Panasonic DMC-G5", 143, 0xfff,
  6671. { 7798,-2562,-740,-3879,11584,2613,-1055,2248,5434 } },
  6672. { "Panasonic DMC-G6", 143, 0xfff,
  6673. { 8294,-2891,-651,-3869,11590,2595,-1183,2267,5352 } },
  6674. { "Panasonic DMC-GF1", 15, 0xf92,
  6675. { 7888,-1902,-1011,-8106,16085,2099,-2353,2866,7330 } },
  6676. { "Panasonic DMC-GF2", 143, 0xfff,
  6677. { 7888,-1902,-1011,-8106,16085,2099,-2353,2866,7330 } },
  6678. { "Panasonic DMC-GF3", 143, 0xfff,
  6679. { 9051,-2468,-1204,-5212,13276,2121,-1197,2510,6890 } },
  6680. { "Panasonic DMC-GF5", 143, 0xfff,
  6681. { 8228,-2945,-660,-3938,11792,2430,-1094,2278,5793 } },
  6682. { "Panasonic DMC-GF6", 143, 0,
  6683. { 8130,-2801,-946,-3520,11289,2552,-1314,2511,5791 } },
  6684. { "Panasonic DMC-GH1", 15, 0xf92,
  6685. { 6299,-1466,-532,-6535,13852,2969,-2331,3112,5984 } },
  6686. { "Panasonic DMC-GH2", 15, 0xf95,
  6687. { 7780,-2410,-806,-3913,11724,2484,-1018,2390,5298 } },
  6688. { "Panasonic DMC-GH3", 144, 0,
  6689. { 6559,-1752,-491,-3672,11407,2586,-962,1875,5130 } },
  6690. { "Panasonic DMC-GX1", 143, 0,
  6691. { 6763,-1919,-863,-3868,11515,2684,-1216,2387,5879 } },
  6692. { "Phase One H 20", 0, 0, /* DJC */
  6693. { 1313,1855,-109,-6715,15908,808,-327,1840,6020 } },
  6694. { "Phase One H 25", 0, 0,
  6695. { 2905,732,-237,-8134,16626,1476,-3038,4253,7517 } },
  6696. { "Phase One P 2", 0, 0,
  6697. { 2905,732,-237,-8134,16626,1476,-3038,4253,7517 } },
  6698. { "Phase One P 30", 0, 0,
  6699. { 4516,-245,-37,-7020,14976,2173,-3206,4671,7087 } },
  6700. { "Phase One P 45", 0, 0,
  6701. { 5053,-24,-117,-5684,14076,1702,-2619,4492,5849 } },
  6702. { "Phase One P40", 0, 0,
  6703. { 8035,435,-962,-6001,13872,2320,-1159,3065,5434 } },
  6704. { "Phase One P65", 0, 0,
  6705. { 8035,435,-962,-6001,13872,2320,-1159,3065,5434 } },
  6706. { "RED ONE", 704, 0xffff, /* DJC */
  6707. { 21014,-7891,-2613,-3056,12201,856,-2203,5125,8042 } },
  6708. { "SAMSUNG EX1", 0, 0x3e00,
  6709. { 8898,-2498,-994,-3144,11328,2066,-760,1381,4576 } },
  6710. { "SAMSUNG EX2F", 0, 0x7ff,
  6711. { 10648,-3897,-1055,-2022,10573,1668,-492,1611,4742 } },
  6712. { "SAMSUNG NX2", 0, 0xfff, /* NX20, NX200, NX210 */
  6713. { 6933,-2268,-753,-4921,13387,1647,-803,1641,6096 } },
  6714. { "SAMSUNG NX1000", 0, 0,
  6715. { 6933,-2268,-753,-4921,13387,1647,-803,1641,6096 } },
  6716. { "SAMSUNG NX", 0, 0, /* NX5, NX10, NX11, NX100 */
  6717. { 10332,-3234,-1168,-6111,14639,1520,-1352,2647,8331 } },
  6718. { "SAMSUNG WB2000", 0, 0xfff,
  6719. { 12093,-3557,-1155,-1000,9534,1733,-22,1787,4576 } },
  6720. { "SAMSUNG GX-1", 0, 0,
  6721. { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
  6722. { "SAMSUNG S85", 0, 0xffff, /* DJC */
  6723. { 11885,-3968,-1473,-4214,12299,1916,-835,1655,5549 } },
  6724. { "Sinar", 0, 0, /* DJC */
  6725. { 16442,-2956,-2422,-2877,12128,750,-1136,6066,4559 } },
  6726. { "SONY DSC-F828", 0, 0,
  6727. { 7924,-1910,-777,-8226,15459,2998,-1517,2199,6818,-7242,11401,3481 } },
  6728. { "SONY DSC-R1", 512, 0,
  6729. { 8512,-2641,-694,-8042,15670,2526,-1821,2117,7414 } },
  6730. { "SONY DSC-V3", 0, 0,
  6731. { 7511,-2571,-692,-7894,15088,3060,-948,1111,8128 } },
  6732. { "SONY DSC-RX100", 200, 0,
  6733. { 8651,-2754,-1057,-3464,12207,1373,-568,1398,4434 } },
  6734. { "SONY DSC-RX1", 128, 0,
  6735. { 6344,-1612,-462,-4863,12477,2681,-865,1786,6899 } },
  6736. { "SONY DSLR-A100", 0, 0xfeb,
  6737. { 9437,-2811,-774,-8405,16215,2290,-710,596,7181 } },
  6738. { "SONY DSLR-A290", 0, 0,
  6739. { 6038,-1484,-579,-9145,16746,2512,-875,746,7218 } },
  6740. { "SONY DSLR-A2", 0, 0,
  6741. { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
  6742. { "SONY DSLR-A300", 0, 0,
  6743. { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
  6744. { "SONY DSLR-A330", 0, 0,
  6745. { 9847,-3091,-929,-8485,16346,2225,-714,595,7103 } },
  6746. { "SONY DSLR-A350", 0, 0xffc,
  6747. { 6038,-1484,-578,-9146,16746,2513,-875,746,7217 } },
  6748. { "SONY DSLR-A380", 0, 0,
  6749. { 6038,-1484,-579,-9145,16746,2512,-875,746,7218 } },
  6750. { "SONY DSLR-A390", 0, 0,
  6751. { 6038,-1484,-579,-9145,16746,2512,-875,746,7218 } },
  6752. { "SONY DSLR-A450", 128, 0xfeb,
  6753. { 4950,-580,-103,-5228,12542,3029,-709,1435,7371 } },
  6754. { "SONY DSLR-A580", 128, 0xfeb,
  6755. { 5932,-1492,-411,-4813,12285,2856,-741,1524,6739 } },
  6756. { "SONY DSLR-A5", 128, 0xfeb,
  6757. { 4950,-580,-103,-5228,12542,3029,-709,1435,7371 } },
  6758. { "SONY DSLR-A700", 126, 0,
  6759. { 5775,-805,-359,-8574,16295,2391,-1943,2341,7249 } },
  6760. { "SONY DSLR-A850", 128, 0,
  6761. { 5413,-1162,-365,-5665,13098,2866,-608,1179,8440 } },
  6762. { "SONY DSLR-A900", 128, 0,
  6763. { 5209,-1072,-397,-8845,16120,2919,-1618,1803,8654 } },
  6764. { "SONY NEX-3N", 128, 0, /* Adobe */
  6765. { 6129,-1545, -418,-4930,12490,2743,-977,1693,6615 } },
  6766. { "SONY NEX-3", 128, 0, /* Adobe */
  6767. { 6549,-1550,-436,-4880,12435,2753,-854,1868,6976 } },
  6768. { "SONY NEX-5N", 128, 0,
  6769. { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
  6770. { "SONY NEX-5R", 128, 0,
  6771. { 6129,-1545,-418,-4930,12490,2743,-977,1693,6615 } },
  6772. { "SONY NEX-5", 128, 0, /* Adobe */
  6773. { 6549,-1550,-436,-4880,12435,2753,-854,1868,6976 } },
  6774. { "SONY NEX-6", 128, 0,
  6775. { 6129,-1545,-418,-4930,12490,2743,-977,1693,6615 } },
  6776. { "SONY NEX-7", 128, 0,
  6777. { 5491,-1192,-363,-4951,12342,2948,-911,1722,7192 } },
  6778. { "SONY NEX", 128, 0, /* NEX-C3, NEX-F3 */
  6779. { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
  6780. { "SONY SLT-A33", 128, 0,
  6781. { 6069,-1221,-366,-5221,12779,2734,-1024,2066,6834 } },
  6782. { "SONY SLT-A35", 128, 0,
  6783. { 5986,-1618,-415,-4557,11820,3120,-681,1404,6971 } },
  6784. { "SONY SLT-A37", 128, 0,
  6785. { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
  6786. { "SONY SLT-A55", 128, 0,
  6787. { 5932,-1492,-411,-4813,12285,2856,-741,1524,6739 } },
  6788. { "SONY SLT-A57", 128, 0,
  6789. { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
  6790. { "SONY SLT-A58", 128, 0,
  6791. { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
  6792. { "SONY SLT-A65", 128, 0,
  6793. { 5491,-1192,-363,-4951,12342,2948,-911,1722,7192 } },
  6794. { "SONY SLT-A77", 128, 0,
  6795. { 5491,-1192,-363,-4951,12342,2948,-911,1722,7192 } },
  6796. { "SONY SLT-A99", 128, 0,
  6797. { 6344,-1612,-462,-4863,12477,2681,-865,1786,6899 } },
  6798. };
  6799. double cam_xyz[4][3];
  6800. char name[130];
  6801. int i, j;
  6802. sprintf (name, "%s %s", t_make, t_model);
  6803. for (i=0; i < sizeof table / sizeof *table; i++)
  6804. if (!strncmp (name, table[i].prefix, strlen(table[i].prefix))) {
  6805. if (table[i].t_black) black = (ushort) table[i].t_black;
  6806. if (table[i].t_maximum) maximum = (ushort) table[i].t_maximum;
  6807. if (table[i].trans[0]) {
  6808. for (j=0; j < 12; j++)
  6809. #ifdef LIBRAW_LIBRARY_BUILD
  6810. imgdata.color.cam_xyz[0][j] =
  6811. #endif
  6812. cam_xyz[0][j] = table[i].trans[j] / 10000.0;
  6813. cam_xyz_coeff (cam_xyz);
  6814. }
  6815. break;
  6816. }
  6817. }
  6818. void CLASS simple_coeff (int index)
  6819. {
  6820. static const float table[][12] = {
  6821. /* index 0 -- all Foveon cameras */
  6822. { 1.4032,-0.2231,-0.1016,-0.5263,1.4816,0.017,-0.0112,0.0183,0.9113 },
  6823. /* index 1 -- Kodak DC20 and DC25 */
  6824. { 2.25,0.75,-1.75,-0.25,-0.25,0.75,0.75,-0.25,-0.25,-1.75,0.75,2.25 },
  6825. /* index 2 -- Logitech Fotoman Pixtura */
  6826. { 1.893,-0.418,-0.476,-0.495,1.773,-0.278,-1.017,-0.655,2.672 },
  6827. /* index 3 -- Nikon E880, E900, and E990 */
  6828. { -1.936280, 1.800443, -1.448486, 2.584324,
  6829. 1.405365, -0.524955, -0.289090, 0.408680,
  6830. -1.204965, 1.082304, 2.941367, -1.818705 }
  6831. };
  6832. int i, c;
  6833. for (raw_color = i=0; i < 3; i++)
  6834. FORCC rgb_cam[i][c] = table[index][i*colors+c];
  6835. }
  6836. short CLASS guess_byte_order (int words)
  6837. {
  6838. uchar test[4][2];
  6839. int t=2, msb;
  6840. double diff, sum[2] = {0,0};
  6841. fread (test[0], 2, 2, ifp);
  6842. for (words-=2; words--; ) {
  6843. fread (test[t], 2, 1, ifp);
  6844. for (msb=0; msb < 2; msb++) {
  6845. diff = (test[t^2][msb] << 8 | test[t^2][!msb])
  6846. - (test[t ][msb] << 8 | test[t ][!msb]);
  6847. sum[msb] += diff*diff;
  6848. }
  6849. t = (t+1) & 3;
  6850. }
  6851. return sum[0] < sum[1] ? 0x4d4d : 0x4949;
  6852. }
  6853. float CLASS find_green (int bps, int bite, int off0, int off1)
  6854. {
  6855. UINT64 bitbuf=0;
  6856. int vbits, col, i, c;
  6857. ushort img[2][2064];
  6858. double sum[]={0,0};
  6859. FORC(2) {
  6860. fseek (ifp, c ? off1:off0, SEEK_SET);
  6861. for (vbits=col=0; col < width; col++) {
  6862. for (vbits -= bps; vbits < 0; vbits += bite) {
  6863. bitbuf <<= bite;
  6864. for (i=0; i < bite; i+=8)
  6865. bitbuf |= (unsigned) (fgetc(ifp) << i);
  6866. }
  6867. img[c][col] = bitbuf << (64-bps-vbits) >> (64-bps);
  6868. }
  6869. }
  6870. FORC(width-1) {
  6871. sum[ c & 1] += ABS(img[0][c]-img[1][c+1]);
  6872. sum[~c & 1] += ABS(img[1][c]-img[0][c+1]);
  6873. }
  6874. return 100 * log(sum[0]/sum[1]);
  6875. }
  6876. #ifndef LIBRAW_LIBRARY_BUILD
  6877. void CLASS identify2(unsigned fsize, unsigned flen, char *head);
  6878. #endif
  6879. /*
  6880. Identify which camera created this file, and set global variables
  6881. accordingly.
  6882. */
  6883. void CLASS identify()
  6884. {
  6885. char head[32], *cp;
  6886. int hlen, flen, fsize, zero_fsize=1, i, c, is_canon;
  6887. struct jhead jh;
  6888. static const struct {
  6889. int fsize;
  6890. char t_make[12], t_model[19], withjpeg;
  6891. } table[] = {
  6892. { 62464, "Kodak", "DC20" ,0 },
  6893. { 124928, "Kodak", "DC20" ,0 },
  6894. { 1652736, "Kodak", "DCS200" ,0 },
  6895. { 4159302, "Kodak", "C330" ,0 },
  6896. { 4162462, "Kodak", "C330" ,0 },
  6897. { 460800, "Kodak", "C603v" ,0 },
  6898. { 614400, "Kodak", "C603v" ,0 },
  6899. { 6163328, "Kodak", "C603" ,0 },
  6900. { 6166488, "Kodak", "C603" ,0 },
  6901. { 9116448, "Kodak", "C603y" ,0 },
  6902. { 311696, "ST Micro", "STV680 VGA" ,0 }, /* SPYz */
  6903. { 787456, "Creative", "PC-CAM 600" ,0 },
  6904. { 1138688, "Minolta", "RD175" ,0 },
  6905. { 3840000, "Foculus", "531C" ,0 },
  6906. { 307200, "Generic", "640x480" ,0 },
  6907. { 786432, "AVT", "F-080C" ,0 },
  6908. { 1447680, "AVT", "F-145C" ,0 },
  6909. { 1920000, "AVT", "F-201C" ,0 },
  6910. { 5067304, "AVT", "F-510C" ,0 },
  6911. { 5067316, "AVT", "F-510C" ,0 },
  6912. { 10134608, "AVT", "F-510C" ,0 },
  6913. { 10134620, "AVT", "F-510C" ,0 },
  6914. { 16157136, "AVT", "F-810C" ,0 },
  6915. { 1409024, "Sony", "XCD-SX910CR" ,0 },
  6916. { 2818048, "Sony", "XCD-SX910CR" ,0 },
  6917. { 3884928, "Micron", "2010" ,0 },
  6918. { 6624000, "Pixelink", "A782" ,0 },
  6919. { 13248000, "Pixelink", "A782" ,0 },
  6920. { 6291456, "RoverShot","3320AF" ,0 },
  6921. { 6553440, "Canon", "PowerShot A460" ,0 },
  6922. { 6653280, "Canon", "PowerShot A530" ,0 },
  6923. { 6573120, "Canon", "PowerShot A610" ,0 },
  6924. { 9219600, "Canon", "PowerShot A620" ,0 },
  6925. { 9243240, "Canon", "PowerShot A470" ,0 },
  6926. { 10341600, "Canon", "PowerShot A720 IS",0 },
  6927. { 10383120, "Canon", "PowerShot A630" ,0 },
  6928. { 12945240, "Canon", "PowerShot A640" ,0 },
  6929. { 15636240, "Canon", "PowerShot A650" ,0 },
  6930. { 5298000, "Canon", "PowerShot SD300" ,0 },
  6931. { 7710960, "Canon", "PowerShot S3 IS" ,0 },
  6932. { 15467760, "Canon", "PowerShot SX110 IS",0 },
  6933. { 15534576, "Canon", "PowerShot SX120 IS",0 },
  6934. { 18653760, "Canon", "PowerShot SX20 IS",0 },
  6935. { 19131120, "Canon", "PowerShot SX220 HS",0 },
  6936. { 21936096, "Canon", "PowerShot SX30 IS",0 },
  6937. { 5939200, "OLYMPUS", "C770UZ" ,0 },
  6938. { 1581060, "NIKON", "E900" ,1 }, /* or E900s,E910 */
  6939. { 2465792, "NIKON", "E950" ,1 }, /* or E800,E700 */
  6940. { 2940928, "NIKON", "E2100" ,1 }, /* or E2500 */
  6941. { 4771840, "NIKON", "E990" ,1 }, /* or E995, Oly C3030Z */
  6942. { 4775936, "NIKON", "E3700" ,1 }, /* or Optio 33WR */
  6943. { 5869568, "NIKON", "E4300" ,1 }, /* or DiMAGE Z2 */
  6944. { 5865472, "NIKON", "E4500" ,1 },
  6945. { 7438336, "NIKON", "E5000" ,1 }, /* or E5700 */
  6946. { 8998912, "NIKON", "COOLPIX S6" ,1 },
  6947. { 1976352, "CASIO", "QV-2000UX" ,1 },
  6948. { 3217760, "CASIO", "QV-3*00EX" ,1 },
  6949. { 6218368, "CASIO", "QV-5700" ,1 },
  6950. { 6054400, "CASIO", "QV-R41" ,1 },
  6951. { 7530816, "CASIO", "QV-R51" ,1 },
  6952. { 7684000, "CASIO", "QV-4000" ,1 },
  6953. { 2937856, "CASIO", "EX-S20" ,1 },
  6954. { 4948608, "CASIO", "EX-S100" ,1 },
  6955. { 7542528, "CASIO", "EX-Z50" ,1 },
  6956. { 7562048, "CASIO", "EX-Z500" ,1 },
  6957. { 7753344, "CASIO", "EX-Z55" ,1 },
  6958. { 7816704, "CASIO", "EX-Z60" ,1 },
  6959. { 10843712, "CASIO", "EX-Z75" ,1 },
  6960. { 10834368, "CASIO", "EX-Z750" ,1 },
  6961. { 12310144, "CASIO", "EX-Z850" ,1 },
  6962. { 12489984, "CASIO", "EX-Z8" ,1 },
  6963. { 15499264, "CASIO", "EX-Z1050" ,1 },
  6964. { 18702336, "CASIO", "EX-ZR100" ,1 },
  6965. { 7426656, "CASIO", "EX-P505" ,1 },
  6966. { 9313536, "CASIO", "EX-P600" ,1 },
  6967. { 10979200, "CASIO", "EX-P700" ,1 },
  6968. { 3178560, "PENTAX", "Optio S" ,1 },
  6969. { 4841984, "PENTAX", "Optio S" ,1 },
  6970. { 6114240, "PENTAX", "Optio S4" ,1 }, /* or S4i, CASIO EX-Z4 */
  6971. { 10702848, "PENTAX", "Optio 750Z" ,1 },
  6972. { 15980544, "AGFAPHOTO","DC-833m" ,1 },
  6973. { 16098048, "SAMSUNG", "S85" ,1 },
  6974. { 16215552, "SAMSUNG", "S85" ,1 },
  6975. { 20487168, "SAMSUNG", "WB550" ,1 },
  6976. { 24000000, "SAMSUNG", "WB550" ,1 },
  6977. { 9994240, "ptGrey", "GRAS-50S5C" ,0 }, // KC: SUPPORT GRASSHOPPER
  6978. { 10075968, "JaiPulnix","BB-500CL" ,0 }, // KC: SUPPORT BB-500CL
  6979. { 10108896, "JaiPulnix","BB-500GE" ,0 }, // KC: SUPPORT BB-500GE
  6980. { 10036800, "SVS", "SVS625CL" ,0 }, // KC: SUPPORT SVS625 cameralink
  6981. { 12582980, "Sinar", "" ,0 },
  6982. { 33292868, "Sinar", "" ,0 },
  6983. { 44390468, "Sinar", "" ,0 } };
  6984. static const char *corp[] =
  6985. { "Canon", "NIKON", "EPSON", "KODAK", "Kodak", "OLYMPUS", "PENTAX",
  6986. "MINOLTA", "Minolta", "Konica", "CASIO", "Sinar", "Phase One",
  6987. "SAMSUNG", "Mamiya", "MOTOROLA", "LEICA" };
  6988. #ifdef LIBRAW_LIBRARY_BUILD
  6989. RUN_CALLBACK(LIBRAW_PROGRESS_IDENTIFY,0,2);
  6990. #endif
  6991. tiff_flip = flip = filters = -1; /* 0 is valid, so -1 is unknown */
  6992. raw_height = raw_width = fuji_width = fuji_layout = cr2_slice[0] = 0;
  6993. maximum = height = width = top_margin = left_margin = 0;
  6994. cdesc[0] = desc[0] = artist[0] = make[0] = model[0] = model2[0] = 0;
  6995. iso_speed = shutter = aperture = focal_len = unique_id = 0;
  6996. tiff_nifds = 0;
  6997. memset (tiff_ifd, 0, sizeof tiff_ifd);
  6998. memset (gpsdata, 0, sizeof gpsdata);
  6999. memset (cblack, 0, sizeof cblack);
  7000. memset (white, 0, sizeof white);
  7001. memset (mask, 0, sizeof mask);
  7002. thumb_offset = thumb_length = thumb_width = thumb_height = 0;
  7003. load_raw = thumb_load_raw = 0;
  7004. write_thumb = &CLASS jpeg_thumb;
  7005. data_offset = meta_length = tiff_bps = tiff_compress = 0;
  7006. kodak_cbpp = zero_after_ff = dng_version = load_flags = 0;
  7007. timestamp = shot_order = tiff_samples = black = is_foveon = 0;
  7008. mix_green = profile_length = data_error = zero_is_bad = 0;
  7009. pixel_aspect = is_raw = raw_color = 1;
  7010. tile_width = tile_length = 0;
  7011. for (i=0; i < 4; i++) {
  7012. cam_mul[i] = i == 1;
  7013. pre_mul[i] = i < 3;
  7014. FORC3 cmatrix[c][i] = 0;
  7015. FORC3 rgb_cam[c][i] = c == i;
  7016. }
  7017. colors = 3;
  7018. for (i=0; i < 0x10000; i++) curve[i] = i;
  7019. order = get2();
  7020. hlen = get4();
  7021. fseek (ifp, 0, SEEK_SET);
  7022. fread (head, 1, 32, ifp);
  7023. fseek (ifp, 0, SEEK_END);
  7024. flen = fsize = ftell(ifp);
  7025. if ((cp = (char *) memmem (head, 32, (char*)"MMMM", 4)) ||
  7026. (cp = (char *) memmem (head, 32, (char*)"IIII", 4))) {
  7027. parse_phase_one (cp-head);
  7028. if (cp-head && parse_tiff(0)) apply_tiff();
  7029. } else if (order == 0x4949 || order == 0x4d4d) {
  7030. if (!memcmp (head+6,"HEAPCCDR",8)) {
  7031. data_offset = hlen;
  7032. parse_ciff (hlen, flen - hlen);
  7033. } else if (parse_tiff(0)) apply_tiff();
  7034. } else if (!memcmp (head,"\xff\xd8\xff\xe1",4) &&
  7035. !memcmp (head+6,"Exif",4)) {
  7036. fseek (ifp, 4, SEEK_SET);
  7037. data_offset = 4 + get2();
  7038. fseek (ifp, data_offset, SEEK_SET);
  7039. if (fgetc(ifp) != 0xff)
  7040. parse_tiff(12);
  7041. thumb_offset = 0;
  7042. } else if (!memcmp (head+25,"ARECOYK",7)) {
  7043. strcpy (make, "Contax");
  7044. strcpy (model,"N Digital");
  7045. fseek (ifp, 33, SEEK_SET);
  7046. get_timestamp(1);
  7047. fseek (ifp, 60, SEEK_SET);
  7048. FORC4 cam_mul[c ^ (c >> 1)] = get4();
  7049. } else if (!strcmp (head, "PXN")) {
  7050. strcpy (make, "Logitech");
  7051. strcpy (model,"Fotoman Pixtura");
  7052. } else if (!strcmp (head, "qktk")) {
  7053. strcpy (make, "Apple");
  7054. strcpy (model,"QuickTake 100");
  7055. load_raw = &CLASS quicktake_100_load_raw;
  7056. } else if (!strcmp (head, "qktn")) {
  7057. strcpy (make, "Apple");
  7058. strcpy (model,"QuickTake 150");
  7059. load_raw = &CLASS kodak_radc_load_raw;
  7060. } else if (!memcmp (head,"FUJIFILM",8)) {
  7061. fseek (ifp, 84, SEEK_SET);
  7062. thumb_offset = get4();
  7063. thumb_length = get4();
  7064. fseek (ifp, 92, SEEK_SET);
  7065. parse_fuji (get4());
  7066. if (thumb_offset > 120) {
  7067. fseek (ifp, 120, SEEK_SET);
  7068. is_raw += (i = get4()) && 1;
  7069. if (is_raw == 2 && shot_select)
  7070. parse_fuji (i);
  7071. }
  7072. load_raw = &CLASS unpacked_load_raw;
  7073. fseek (ifp, 100+28*(shot_select > 0), SEEK_SET);
  7074. parse_tiff (data_offset = get4());
  7075. parse_tiff (thumb_offset+12);
  7076. apply_tiff();
  7077. } else if (!memcmp (head,"RIFF",4)) {
  7078. fseek (ifp, 0, SEEK_SET);
  7079. parse_riff();
  7080. } else if (!memcmp (head,"\0\001\0\001\0@",6)) {
  7081. fseek (ifp, 6, SEEK_SET);
  7082. fread (make, 1, 8, ifp);
  7083. fread (model, 1, 8, ifp);
  7084. fread (model2, 1, 16, ifp);
  7085. data_offset = get2();
  7086. get2();
  7087. raw_width = get2();
  7088. raw_height = get2();
  7089. load_raw = &CLASS nokia_load_raw;
  7090. filters = 0x61616161;
  7091. } else if (!memcmp (head,"NOKIARAW",8)) {
  7092. strcpy (make, "NOKIA");
  7093. strcpy (model, "X2");
  7094. order = 0x4949;
  7095. fseek (ifp, 300, SEEK_SET);
  7096. data_offset = get4();
  7097. i = get4();
  7098. width = get2();
  7099. height = get2();
  7100. data_offset += i - width * 5 / 4 * height;
  7101. load_raw = &CLASS nokia_load_raw;
  7102. filters = 0x61616161;
  7103. } else if (!memcmp (head,"ARRI",4)) {
  7104. order = 0x4949;
  7105. fseek (ifp, 20, SEEK_SET);
  7106. width = get4();
  7107. height = get4();
  7108. strcpy (make, "ARRI");
  7109. fseek (ifp, 668, SEEK_SET);
  7110. fread (model, 1, 64, ifp);
  7111. data_offset = 4096;
  7112. load_raw = &CLASS packed_load_raw;
  7113. load_flags = 88;
  7114. filters = 0x61616161;
  7115. } else if (!memcmp (head+4,"RED1",4)) {
  7116. strcpy (make, "RED");
  7117. strcpy (model,"ONE");
  7118. parse_redcine();
  7119. load_raw = &CLASS redcine_load_raw;
  7120. gamma_curve (1/2.4, 12.92, 1, 4095);
  7121. filters = 0x49494949;
  7122. } else if (!memcmp (head,"DSC-Image",9))
  7123. parse_rollei();
  7124. else if (!memcmp (head,"PWAD",4))
  7125. parse_sinar_ia();
  7126. else if (!memcmp (head,"\0MRM",4))
  7127. parse_minolta(0);
  7128. #ifdef LIBRAW_DEMOSAIC_PACK_GPL2
  7129. else if (!memcmp (head,"FOVb",4))
  7130. parse_foveon();
  7131. #endif
  7132. else if (!memcmp (head,"CI",2))
  7133. parse_cine();
  7134. else
  7135. for (zero_fsize=i=0; i < sizeof table / sizeof *table; i++)
  7136. if (fsize == table[i].fsize) {
  7137. strcpy (make, table[i].t_make );
  7138. strcpy (model, table[i].t_model);
  7139. if (table[i].withjpeg)
  7140. parse_external_jpeg();
  7141. }
  7142. if (zero_fsize) fsize = 0;
  7143. if (make[0] == 0) parse_smal (0, flen);
  7144. if (make[0] == 0) {
  7145. parse_jpeg(0);
  7146. fseek(ifp,0,SEEK_END);
  7147. int sz = ftell(ifp);
  7148. if (!strncmp(model,"ov",2) && sz>=6404096 && !fseek (ifp, -6404096, SEEK_END) &&
  7149. fread (head, 1, 32, ifp) && !strcmp(head,"BRCMn")) {
  7150. strcpy (make, "OmniVision");
  7151. data_offset = ftell(ifp) + 0x8000-32;
  7152. width = raw_width;
  7153. raw_width = 2611;
  7154. load_raw = &CLASS nokia_load_raw;
  7155. filters = 0x16161616;
  7156. } else is_raw = 0;
  7157. }
  7158. for (i=0; i < sizeof corp / sizeof *corp; i++)
  7159. if (strstr (make, corp[i])) /* Simplify company names */
  7160. strcpy (make, corp[i]);
  7161. if (!strncmp (make,"KODAK",5) &&
  7162. ((cp = strstr(model," DIGITAL CAMERA")) ||
  7163. (cp = strstr(model," Digital Camera")) ||
  7164. (cp = strstr(model,"FILE VERSION"))))
  7165. *cp = 0;
  7166. cp = make + strlen(make); /* Remove trailing spaces */
  7167. while (*--cp == ' ') *cp = 0;
  7168. cp = model + strlen(model);
  7169. while (*--cp == ' ') *cp = 0;
  7170. i = strlen(make); /* Remove make from model */
  7171. if (!strncasecmp (model, make, i) && model[i++] == ' ')
  7172. memmove (model, model+i, 64-i);
  7173. if (!strncmp (model,"FinePix ",8))
  7174. strcpy (model, model+8);
  7175. if (!strncmp (model,"Digital Camera ",15))
  7176. strcpy (model, model+15);
  7177. desc[511] = artist[63] = make[63] = model[63] = model2[63] = 0;
  7178. if (!is_raw) goto notraw;
  7179. if (!height) height = raw_height;
  7180. if (!width) width = raw_width;
  7181. if (height == 2624 && width == 3936) /* Pentax K10D and Samsung GX10 */
  7182. { height = 2616; width = 3896; }
  7183. if (height == 3136 && width == 4864) /* Pentax K20D and Samsung GX20 */
  7184. { height = 3124; width = 4688; filters = 0x16161616; }
  7185. if (width == 4352 && (!strcmp(model,"K-r") || !strcmp(model,"K-x")))
  7186. { width = 4309; filters = 0x16161616; }
  7187. if (width >= 4960 && !strncmp(model,"K-5",3))
  7188. { left_margin = 10; width = 4950; filters = 0x16161616; }
  7189. if (width >= 4960 && !strncmp(model,"K-5 II",6))
  7190. { left_margin = 10; width = 4948; filters = 0x16161616; }
  7191. if (width == 4736 && !strcmp(model,"K-7"))
  7192. { height = 3122; width = 4684; filters = 0x16161616; top_margin = 2; }
  7193. if (width == 7424 && !strcmp(model,"645D"))
  7194. { height = 5502; width = 7328; filters = 0x61616161; top_margin = 29;
  7195. left_margin = 48; }
  7196. if (height == 3014 && width == 4096) /* Ricoh GX200 */
  7197. width = 4014;
  7198. if (dng_version) {
  7199. if (filters == UINT_MAX) filters = 0;
  7200. if (filters) is_raw = tiff_samples;
  7201. else colors = tiff_samples;
  7202. if (tiff_compress == 1)
  7203. load_raw = &CLASS packed_dng_load_raw;
  7204. if (tiff_compress == 7)
  7205. load_raw = &CLASS lossless_dng_load_raw;
  7206. goto dng_skip;
  7207. }
  7208. if ((is_canon = !strcmp(make,"Canon")))
  7209. load_raw = memcmp (head+6,"HEAPCCDR",8) ?
  7210. &CLASS lossless_jpeg_load_raw : &CLASS canon_load_raw;
  7211. if (!strcmp(make,"NIKON")) {
  7212. if (!load_raw)
  7213. load_raw = &CLASS packed_load_raw;
  7214. if (model[0] == 'E')
  7215. load_flags |= !data_offset << 2 | 2;
  7216. }
  7217. if (!strcmp(make,"CASIO")) {
  7218. load_raw = &CLASS packed_load_raw;
  7219. maximum = 0xf7f;
  7220. }
  7221. /* Set parameters based on camera name (for non-DNG files). */
  7222. if (is_foveon) {
  7223. if (height*2 < width) pixel_aspect = 0.5;
  7224. if (height > width) pixel_aspect = 2;
  7225. filters = 0;
  7226. simple_coeff(0);
  7227. } else if (is_canon && tiff_bps == 15) {
  7228. switch (width) {
  7229. case 3344: width -= 66;
  7230. case 3872: width -= 6;
  7231. }
  7232. if (height > width) SWAP(height,width);
  7233. filters = 0;
  7234. load_raw = &CLASS canon_sraw_load_raw;
  7235. } else if (!strcmp(model,"PowerShot 600")) {
  7236. height = 613;
  7237. width = 854;
  7238. raw_width = 896;
  7239. pixel_aspect = 607/628.0;
  7240. colors = 4;
  7241. filters = 0xe1e4e1e4;
  7242. load_raw = &CLASS canon_600_load_raw;
  7243. } else if (!strcmp(model,"PowerShot A5") ||
  7244. !strcmp(model,"PowerShot A5 Zoom")) {
  7245. height = 773;
  7246. width = 960;
  7247. raw_width = 992;
  7248. pixel_aspect = 256/235.0;
  7249. colors = 4;
  7250. filters = 0x1e4e1e4e;
  7251. goto canon_a5;
  7252. } else if (!strcmp(model,"PowerShot A50")) {
  7253. height = 968;
  7254. width = 1290;
  7255. raw_width = 1320;
  7256. colors = 4;
  7257. filters = 0x1b4e4b1e;
  7258. goto canon_a5;
  7259. } else if (!strcmp(model,"PowerShot Pro70")) {
  7260. height = 1024;
  7261. width = 1552;
  7262. colors = 4;
  7263. filters = 0x1e4b4e1b;
  7264. goto canon_a5;
  7265. } else if (!strcmp(model,"PowerShot SD300")) {
  7266. height = 1752;
  7267. width = 2344;
  7268. raw_height = 1766;
  7269. raw_width = 2400;
  7270. top_margin = 12;
  7271. left_margin = 12;
  7272. goto canon_a5;
  7273. } else if (!strcmp(model,"PowerShot A460")) {
  7274. height = 1960;
  7275. width = 2616;
  7276. raw_height = 1968;
  7277. raw_width = 2664;
  7278. top_margin = 4;
  7279. left_margin = 4;
  7280. goto canon_a5;
  7281. } else if (!strcmp(model,"PowerShot A530")) {
  7282. height = 1984;
  7283. width = 2620;
  7284. raw_height = 1992;
  7285. raw_width = 2672;
  7286. top_margin = 6;
  7287. left_margin = 10;
  7288. goto canon_a5;
  7289. } else if (!strcmp(model,"PowerShot A610")) {
  7290. if (canon_s2is()) strcpy (model+10, "S2 IS");
  7291. height = 1960;
  7292. width = 2616;
  7293. raw_height = 1968;
  7294. raw_width = 2672;
  7295. top_margin = 8;
  7296. left_margin = 12;
  7297. goto canon_a5;
  7298. } else if (!strcmp(model,"PowerShot A620")) {
  7299. height = 2328;
  7300. width = 3112;
  7301. raw_height = 2340;
  7302. raw_width = 3152;
  7303. top_margin = 12;
  7304. left_margin = 36;
  7305. goto canon_a5;
  7306. } else if (!strcmp(model,"PowerShot A470")) {
  7307. height = 2328;
  7308. width = 3096;
  7309. raw_height = 2346;
  7310. raw_width = 3152;
  7311. top_margin = 6;
  7312. left_margin = 12;
  7313. goto canon_a5;
  7314. } else if (!strcmp(model,"PowerShot A720 IS")) {
  7315. height = 2472;
  7316. width = 3298;
  7317. raw_height = 2480;
  7318. raw_width = 3336;
  7319. top_margin = 5;
  7320. left_margin = 6;
  7321. goto canon_a5;
  7322. } else if (!strcmp(model,"PowerShot A630")) {
  7323. height = 2472;
  7324. width = 3288;
  7325. raw_height = 2484;
  7326. raw_width = 3344;
  7327. top_margin = 6;
  7328. left_margin = 12;
  7329. goto canon_a5;
  7330. } else if (!strcmp(model,"PowerShot A640")) {
  7331. height = 2760;
  7332. width = 3672;
  7333. raw_height = 2772;
  7334. raw_width = 3736;
  7335. top_margin = 6;
  7336. left_margin = 12;
  7337. goto canon_a5;
  7338. } else if (!strcmp(model,"PowerShot A650")) {
  7339. height = 3024;
  7340. width = 4032;
  7341. raw_height = 3048;
  7342. raw_width = 4104;
  7343. top_margin = 12;
  7344. left_margin = 48;
  7345. goto canon_a5;
  7346. } else if (!strcmp(model,"PowerShot S3 IS")) {
  7347. height = 2128;
  7348. width = 2840;
  7349. raw_height = 2136;
  7350. raw_width = 2888;
  7351. top_margin = 8;
  7352. left_margin = 44;
  7353. canon_a5:
  7354. tiff_bps = 10;
  7355. load_raw = &CLASS packed_load_raw;
  7356. load_flags = 40;
  7357. if (raw_width > 1600) zero_is_bad = 1;
  7358. } else if (!strcmp(model,"PowerShot SX110 IS")) {
  7359. height = 2760;
  7360. width = 3684;
  7361. raw_height = 2772;
  7362. raw_width = 3720;
  7363. top_margin = 12;
  7364. left_margin = 6;
  7365. load_raw = &CLASS packed_load_raw;
  7366. load_flags = 40;
  7367. zero_is_bad = 1;
  7368. } else if (!strcmp(model,"PowerShot SX120 IS")) {
  7369. height = 2742;
  7370. width = 3664;
  7371. raw_height = 2778;
  7372. raw_width = 3728;
  7373. top_margin = 18;
  7374. left_margin = 16;
  7375. filters = 0x49494949;
  7376. load_raw = &CLASS packed_load_raw;
  7377. load_flags = 40;
  7378. zero_is_bad = 1;
  7379. } else if (!strcmp(model,"PowerShot SX20 IS")) {
  7380. height = 3024;
  7381. width = 4032;
  7382. raw_height = 3048;
  7383. raw_width = 4080;
  7384. top_margin = 12;
  7385. left_margin = 24;
  7386. load_raw = &CLASS packed_load_raw;
  7387. load_flags = 40;
  7388. zero_is_bad = 1;
  7389. } else if (!strcmp(model,"PowerShot SX220 HS")) {
  7390. height = 3043;
  7391. width = 4072;
  7392. raw_height = 3060;
  7393. raw_width = 4168;
  7394. mask[0][0] = top_margin = 16;
  7395. mask[0][2] = top_margin + height;
  7396. mask[0][3] = left_margin = 92;
  7397. load_raw = &CLASS packed_load_raw;
  7398. load_flags = 8;
  7399. zero_is_bad = 1;
  7400. } else if (!strcmp(model,"PowerShot SX50 HS")) {
  7401. top_margin=16;
  7402. left_margin=94;
  7403. height = 3043;
  7404. width = 4072;
  7405. } else if (!strcmp(model,"PowerShot SX30 IS")) {
  7406. height = 3254;
  7407. width = 4366;
  7408. raw_height = 3276;
  7409. raw_width = 4464;
  7410. top_margin = 10;
  7411. left_margin = 25;
  7412. filters = 0x16161616;
  7413. load_raw = &CLASS packed_load_raw;
  7414. load_flags = 40;
  7415. zero_is_bad = 1;
  7416. } else if (!strcmp(model,"PowerShot Pro90 IS")) {
  7417. width = 1896;
  7418. colors = 4;
  7419. filters = 0xb4b4b4b4;
  7420. } else if (is_canon && raw_width == 2144) {
  7421. height = 1550;
  7422. width = 2088;
  7423. top_margin = 8;
  7424. left_margin = 4;
  7425. if (!strcmp(model,"PowerShot G1")) {
  7426. colors = 4;
  7427. filters = 0xb4b4b4b4;
  7428. }
  7429. } else if (is_canon && raw_width == 2224) {
  7430. height = 1448;
  7431. width = 2176;
  7432. top_margin = 6;
  7433. left_margin = 48;
  7434. } else if (is_canon && raw_width == 2376) {
  7435. height = 1720;
  7436. width = 2312;
  7437. top_margin = 6;
  7438. left_margin = 12;
  7439. } else if (is_canon && raw_width == 2672) {
  7440. height = 1960;
  7441. width = 2616;
  7442. top_margin = 6;
  7443. left_margin = 12;
  7444. } else if (is_canon && raw_width == 3152) {
  7445. height = 2056;
  7446. width = 3088;
  7447. top_margin = 12;
  7448. left_margin = 64;
  7449. if (unique_id == 0x80000170)
  7450. adobe_coeff ("Canon","EOS 300D");
  7451. } else if (is_canon && raw_width == 3160) {
  7452. height = 2328;
  7453. width = 3112;
  7454. top_margin = 12;
  7455. left_margin = 44;
  7456. } else if (is_canon && raw_width == 3344) {
  7457. height = 2472;
  7458. width = 3288;
  7459. top_margin = 6;
  7460. left_margin = 4;
  7461. } else if (!strcmp(model,"EOS D2000C")) {
  7462. filters = 0x61616161;
  7463. black = curve[200];
  7464. } else if (is_canon && raw_width == 3516) {
  7465. top_margin = 14;
  7466. left_margin = 42;
  7467. if (unique_id == 0x80000189)
  7468. adobe_coeff ("Canon","EOS 350D");
  7469. goto canon_cr2;
  7470. } else if (is_canon && raw_width == 3596) {
  7471. top_margin = 12;
  7472. left_margin = 74;
  7473. goto canon_cr2;
  7474. } else if (is_canon && raw_width == 3744) {
  7475. height = 2760;
  7476. width = 3684;
  7477. top_margin = 16;
  7478. left_margin = 8;
  7479. if (unique_id > 0x2720000) {
  7480. top_margin = 12;
  7481. left_margin = 52;
  7482. }
  7483. } else if (is_canon && raw_width == 3944) {
  7484. height = 2602;
  7485. width = 3908;
  7486. top_margin = 18;
  7487. left_margin = 30;
  7488. } else if (is_canon && raw_width == 3948) {
  7489. top_margin = 18;
  7490. left_margin = 42;
  7491. height -= 2;
  7492. if (unique_id == 0x80000236)
  7493. adobe_coeff ("Canon","EOS 400D");
  7494. if (unique_id == 0x80000254)
  7495. adobe_coeff ("Canon","EOS 1000D");
  7496. goto canon_cr2;
  7497. } else if (is_canon && raw_width == 3984) {
  7498. top_margin = 20;
  7499. left_margin = 76;
  7500. height -= 2;
  7501. goto canon_cr2;
  7502. } else if (is_canon && raw_width == 4104) {
  7503. height = 3024;
  7504. width = 4032;
  7505. top_margin = 12;
  7506. left_margin = 48;
  7507. } else if (is_canon && raw_width == 4152) {
  7508. top_margin = 12;
  7509. left_margin = 192;
  7510. goto canon_cr2;
  7511. } else if (is_canon && raw_width == 4160) {
  7512. height = 3048;
  7513. width = 4048;
  7514. top_margin = 11;
  7515. left_margin = 104;
  7516. } else if (is_canon && raw_width == 4176) {
  7517. height = 3045;
  7518. width = 4072;
  7519. left_margin = 96;
  7520. mask[0][0] = top_margin = 17;
  7521. mask[0][2] = raw_height;
  7522. mask[0][3] = 80;
  7523. filters = 0x49494949;
  7524. } else if (is_canon && raw_width == 4312) {
  7525. top_margin = 18;
  7526. left_margin = 22;
  7527. height -= 2;
  7528. if (unique_id == 0x80000176)
  7529. adobe_coeff ("Canon","EOS 450D");
  7530. goto canon_cr2;
  7531. } else if (is_canon && raw_width == 4352) {
  7532. top_margin = 18;
  7533. left_margin = 62;
  7534. if (unique_id == 0x80000288)
  7535. adobe_coeff ("Canon","EOS 1100D");
  7536. goto canon_cr2;
  7537. } else if (is_canon && raw_width == 4476) {
  7538. top_margin = 34;
  7539. left_margin = 90;
  7540. goto canon_cr2;
  7541. } else if (is_canon && raw_width == 4480) {
  7542. height = 3326;
  7543. width = 4432;
  7544. top_margin = 10;
  7545. left_margin = 12;
  7546. filters = 0x49494949;
  7547. } else if (is_canon && raw_width == 4496) {
  7548. height = 3316;
  7549. width = 4404;
  7550. top_margin = 50;
  7551. left_margin = 80;
  7552. } else if (is_canon && raw_width == 4832) {
  7553. top_margin = unique_id == 0x80000261 ? 51:26;
  7554. left_margin = 62;
  7555. if (unique_id == 0x80000252)
  7556. adobe_coeff ("Canon","EOS 500D");
  7557. goto canon_cr2;
  7558. } else if (is_canon && raw_width == 5108) {
  7559. top_margin = 13;
  7560. left_margin = 98;
  7561. goto canon_cr2;
  7562. } else if (is_canon && raw_width == 5120) {
  7563. height -= top_margin = 45;
  7564. left_margin = 142;
  7565. width = 4916;
  7566. } else if (is_canon && raw_width == 5280) {
  7567. top_margin = 52;
  7568. left_margin = 72;
  7569. if (unique_id == 0x80000301)
  7570. adobe_coeff ("Canon","EOS 650D");
  7571. goto canon_cr2;
  7572. } else if (is_canon && raw_width == 5344) {
  7573. top_margin = 51;
  7574. left_margin = 142;
  7575. if (unique_id == 0x80000269) {
  7576. top_margin = 100;
  7577. left_margin = 126;
  7578. height -= 2;
  7579. adobe_coeff ("Canon","EOS-1D X");
  7580. }
  7581. if (unique_id == 0x80000270)
  7582. adobe_coeff ("Canon","EOS 550D");
  7583. if (unique_id == 0x80000286)
  7584. adobe_coeff ("Canon","EOS 600D");
  7585. goto canon_cr2;
  7586. } else if (is_canon && raw_width == 5360) {
  7587. top_margin = 51;
  7588. left_margin = 158;
  7589. goto canon_cr2;
  7590. } else if (is_canon && raw_width == 5568) {
  7591. top_margin = 38;
  7592. left_margin = 72;
  7593. goto canon_cr2;
  7594. } else if (is_canon && raw_width == 5712) {
  7595. height = 3752;
  7596. width = 5640;
  7597. top_margin = 20;
  7598. left_margin = 62;
  7599. } else if (is_canon && raw_width == 5792) {
  7600. top_margin = 51;
  7601. left_margin = 158;
  7602. canon_cr2:
  7603. height -= top_margin;
  7604. width -= left_margin;
  7605. } else if (is_canon && raw_width == 5920) {
  7606. height = 3870;
  7607. width = 5796;
  7608. top_margin = 80;
  7609. left_margin = 122;
  7610. } else if (!strcmp(model,"D1")) {
  7611. cam_mul[0] *= 256/527.0;
  7612. cam_mul[2] *= 256/317.0;
  7613. } else if (!strcmp(model,"D1X")) {
  7614. width -= 4;
  7615. pixel_aspect = 0.5;
  7616. } else if (!strcmp(model,"D40X") ||
  7617. !strcmp(model,"D60") ||
  7618. !strcmp(model,"D80") ||
  7619. !strcmp(model,"D3000")) {
  7620. height -= 3;
  7621. width -= 4;
  7622. } else if (!strcmp(model,"D3") ||
  7623. !strcmp(model,"D3S") ||
  7624. !strcmp(model,"D700")) {
  7625. width -= 4;
  7626. left_margin = 2;
  7627. } else if (!strcmp(model,"D3100")) {
  7628. width -= 28;
  7629. left_margin = 6;
  7630. } else if (!strcmp(model,"D5000") ||
  7631. !strcmp(model,"D90")) {
  7632. width -= 42;
  7633. } else if (!strcmp(model,"D5100") ||
  7634. !strcmp(model,"D7000")) {
  7635. width -= 44;
  7636. } else if (!strcmp(model,"D3200") ||
  7637. !strcmp(model,"D800E") ||
  7638. !strcmp(model,"D600") ||
  7639. !strcmp(model,"D800")) {
  7640. width -= 46;
  7641. } else if (!strcmp(model,"D4")) {
  7642. width -= 52;
  7643. left_margin = 2;
  7644. } else if (!strcmp(model,"D600")) {
  7645. width -= 48;
  7646. } else if (!strncmp(model,"D40",3) ||
  7647. !strncmp(model,"D50",3) ||
  7648. !strncmp(model,"D70",3)) {
  7649. width--;
  7650. } else if (!strcmp(model,"D100")) {
  7651. if (load_flags)
  7652. raw_width = (width += 3) + 3;
  7653. } else if (!strcmp(model,"D200")) {
  7654. left_margin = 1;
  7655. width -= 4;
  7656. filters = 0x94949494;
  7657. } else if (!strncmp(model,"D2H",3)) {
  7658. left_margin = 6;
  7659. width -= 14;
  7660. } else if (!strncmp(model,"D2X",3)) {
  7661. if (width == 3264) width -= 32;
  7662. else width -= 8;
  7663. } else if (!strncmp(model,"D300",4)) {
  7664. width -= 32;
  7665. } else if (!strcmp(make,"NIKON") && raw_width == 4032) {
  7666. adobe_coeff ("NIKON","COOLPIX P7700");
  7667. } else if (!strncmp(model,"COOLPIX P",9)) {
  7668. load_flags = 24;
  7669. filters = 0x94949494;
  7670. if (model[9] == '7' && iso_speed >= 400)
  7671. black = 255;
  7672. } else if (!strncmp(model,"COOLPIX A",9) && raw_width == 4992) {
  7673. width = 4948;
  7674. } else if (!strncmp(model,"1 ",2)) {
  7675. height -= 2;
  7676. } else if (fsize == 1581060) {
  7677. height = 963;
  7678. width = 1287;
  7679. raw_width = 1632;
  7680. maximum = 0x3f4;
  7681. colors = 4;
  7682. filters = 0x1e1e1e1e;
  7683. simple_coeff(3);
  7684. pre_mul[0] = 1.2085;
  7685. pre_mul[1] = 1.0943;
  7686. pre_mul[3] = 1.1103;
  7687. goto e900;
  7688. } else if (fsize == 2465792) {
  7689. height = 1203;
  7690. width = 1616;
  7691. raw_width = 2048;
  7692. colors = 4;
  7693. filters = 0x4b4b4b4b;
  7694. adobe_coeff ("NIKON","E950");
  7695. e900:
  7696. tiff_bps = 10;
  7697. load_raw = &CLASS packed_load_raw;
  7698. load_flags = 6;
  7699. } else if (fsize == 4771840) {
  7700. height = 1540;
  7701. width = 2064;
  7702. colors = 4;
  7703. filters = 0xe1e1e1e1;
  7704. load_raw = &CLASS packed_load_raw;
  7705. load_flags = 6;
  7706. if (!timestamp && nikon_e995())
  7707. strcpy (model, "E995");
  7708. if (strcmp(model,"E995")) {
  7709. filters = 0xb4b4b4b4;
  7710. simple_coeff(3);
  7711. pre_mul[0] = 1.196;
  7712. pre_mul[1] = 1.246;
  7713. pre_mul[2] = 1.018;
  7714. }
  7715. } else if (!strcmp(model,"E2100")) {
  7716. if (!timestamp && !nikon_e2100()) goto cp_e2500;
  7717. height = 1206;
  7718. width = 1616;
  7719. load_flags = 30;
  7720. } else if (!strcmp(model,"E2500")) {
  7721. cp_e2500:
  7722. strcpy (model, "E2500");
  7723. height = 1204;
  7724. width = 1616;
  7725. colors = 4;
  7726. filters = 0x4b4b4b4b;
  7727. } else if (fsize == 4775936) {
  7728. height = 1542;
  7729. width = 2064;
  7730. load_raw = &CLASS packed_load_raw;
  7731. load_flags = 30;
  7732. if (!timestamp) nikon_3700();
  7733. if (model[0] == 'E' && atoi(model+1) < 3700)
  7734. filters = 0x49494949;
  7735. if (!strcmp(model,"Optio 33WR")) {
  7736. flip = 1;
  7737. filters = 0x16161616;
  7738. }
  7739. if (make[0] == 'O') {
  7740. i = find_green (12, 32, 1188864, 3576832);
  7741. c = find_green (12, 32, 2383920, 2387016);
  7742. if (abs(i) < abs(c)) {
  7743. SWAP(i,c);
  7744. load_flags = 24;
  7745. }
  7746. if (i < 0) filters = 0x61616161;
  7747. }
  7748. } else if (fsize == 5869568) {
  7749. height = 1710;
  7750. width = 2288;
  7751. filters = 0x16161616;
  7752. if (!timestamp && minolta_z2()) {
  7753. strcpy (make, "Minolta");
  7754. strcpy (model,"DiMAGE Z2");
  7755. }
  7756. load_raw = &CLASS packed_load_raw;
  7757. load_flags = 6 + 24*(make[0] == 'M');
  7758. } else if (!strcmp(model,"E4500")) {
  7759. height = 1708;
  7760. width = 2288;
  7761. colors = 4;
  7762. filters = 0xb4b4b4b4;
  7763. } else if (fsize == 7438336) {
  7764. height = 1924;
  7765. width = 2576;
  7766. colors = 4;
  7767. filters = 0xb4b4b4b4;
  7768. } else if (fsize == 8998912) {
  7769. height = 2118;
  7770. width = 2832;
  7771. maximum = 0xf83;
  7772. load_raw = &CLASS packed_load_raw;
  7773. load_flags = 30;
  7774. } else if (!strcmp(make,"FUJIFILM")) {
  7775. if (!strcmp(model+7,"S2Pro")) {
  7776. strcpy (model,"S2Pro");
  7777. height = 2144;
  7778. width = 2880;
  7779. flip = 6;
  7780. } else if (load_raw != &CLASS packed_load_raw)
  7781. maximum = (is_raw == 2 && shot_select) ? 0x2f00 : 0x3e00;
  7782. top_margin = (raw_height - height) >> 2 << 1;
  7783. left_margin = (raw_width - width ) >> 2 << 1;
  7784. if (width == 2848) filters = 0x16161616;
  7785. if (width == 3328) {
  7786. width = 3262;
  7787. left_margin = 34;
  7788. }
  7789. if (width == 4952) {
  7790. left_margin = 0;
  7791. filters = 2;
  7792. }
  7793. if(!strcmp(model,"HS50EXR"))
  7794. {
  7795. filters = 0x1e1e1e1e;
  7796. width=3230;
  7797. }
  7798. if(!strcmp(model,"X20"))
  7799. {
  7800. left_margin = 2;
  7801. top_margin=2;
  7802. width = 4030;
  7803. height = 3010;
  7804. raw_height = 3012;
  7805. filters = 2;
  7806. }
  7807. if(!strcmp(model,"X100S"))
  7808. {
  7809. left_margin = 2;
  7810. top_margin = 1;
  7811. width = 4934;
  7812. height = 3290;
  7813. raw_height = 3295;
  7814. filters = 2;
  7815. data_offset +=8;
  7816. load_raw = &CLASS unpacked_load_raw;
  7817. load_flags = 0;
  7818. maximum = 16383;
  7819. }
  7820. if (fuji_layout) raw_width *= is_raw;
  7821. } else if (!strcmp(model,"RD175")) {
  7822. height = 986;
  7823. width = 1534;
  7824. data_offset = 513;
  7825. filters = 0x61616161;
  7826. load_raw = &CLASS minolta_rd175_load_raw;
  7827. } else if (!strcmp(model,"KD-400Z")) {
  7828. height = 1712;
  7829. width = 2312;
  7830. raw_width = 2336;
  7831. goto konica_400z;
  7832. } else if (!strcmp(model,"KD-510Z")) {
  7833. goto konica_510z;
  7834. } else if (!strcasecmp(make,"MINOLTA")) {
  7835. load_raw = &CLASS unpacked_load_raw;
  7836. maximum = 0xfff;
  7837. if (!strncmp(model,"DiMAGE A",8)) {
  7838. if (!strcmp(model,"DiMAGE A200"))
  7839. filters = 0x49494949;
  7840. tiff_bps = 12;
  7841. load_raw = &CLASS packed_load_raw;
  7842. } else if (!strncmp(model,"ALPHA",5) ||
  7843. !strncmp(model,"DYNAX",5) ||
  7844. !strncmp(model,"MAXXUM",6)) {
  7845. sprintf (model+20, "DYNAX %-10s", model+6+(model[0]=='M'));
  7846. adobe_coeff (make, model+20);
  7847. load_raw = &CLASS packed_load_raw;
  7848. } else if (!strncmp(model,"DiMAGE G",8)) {
  7849. if (model[8] == '4') {
  7850. height = 1716;
  7851. width = 2304;
  7852. } else if (model[8] == '5') {
  7853. konica_510z:
  7854. height = 1956;
  7855. width = 2607;
  7856. raw_width = 2624;
  7857. } else if (model[8] == '6') {
  7858. height = 2136;
  7859. width = 2848;
  7860. }
  7861. data_offset += 14;
  7862. filters = 0x61616161;
  7863. konica_400z:
  7864. load_raw = &CLASS unpacked_load_raw;
  7865. maximum = 0x3df;
  7866. order = 0x4d4d;
  7867. }
  7868. } else if (!strcmp(model,"*ist D")) {
  7869. load_raw = &CLASS unpacked_load_raw;
  7870. data_error = -1;
  7871. } else if (!strcmp(model,"*ist DS")) {
  7872. height -= 2;
  7873. } else if (!strcmp(model,"Optio S")) {
  7874. if (fsize == 3178560) {
  7875. height = 1540;
  7876. width = 2064;
  7877. load_raw = &CLASS eight_bit_load_raw;
  7878. cam_mul[0] *= 4;
  7879. cam_mul[2] *= 4;
  7880. } else {
  7881. height = 1544;
  7882. width = 2068;
  7883. raw_width = 3136;
  7884. load_raw = &CLASS packed_load_raw;
  7885. maximum = 0xf7c;
  7886. }
  7887. } else if (fsize == 6114240) {
  7888. height = 1737;
  7889. width = 2324;
  7890. raw_width = 3520;
  7891. load_raw = &CLASS packed_load_raw;
  7892. maximum = 0xf7a;
  7893. } else if (!strcmp(model,"Optio 750Z")) {
  7894. height = 2302;
  7895. width = 3072;
  7896. load_raw = &CLASS packed_load_raw;
  7897. load_flags = 30;
  7898. } else if (!strcmp(model,"DC-833m")) {
  7899. height = 2448;
  7900. width = 3264;
  7901. order = 0x4949;
  7902. filters = 0x61616161;
  7903. load_raw = &CLASS unpacked_load_raw;
  7904. maximum = 0xfc00;
  7905. } else if (!strncmp(model,"S85",3)) {
  7906. height = 2448;
  7907. width = 3264;
  7908. raw_width = fsize/height/2;
  7909. order = 0x4d4d;
  7910. load_raw = &CLASS unpacked_load_raw;
  7911. } else if (!strcmp(make,"SAMSUNG") && raw_width == 4704) {
  7912. height -= top_margin = 8;
  7913. width -= 2 * (left_margin = 8);
  7914. load_flags = 32;
  7915. } else if (!strcmp(make,"SAMSUNG") && raw_width == 5632) {
  7916. order = 0x4949;
  7917. height = 3694;
  7918. top_margin = 2;
  7919. width = 5574 - (left_margin = 32 + tiff_bps);
  7920. if (tiff_bps == 12) load_flags = 80;
  7921. } else if (!strcmp(make,"SAMSUNG") && !strcmp(model,"NX300")) {
  7922. is_raw =0;
  7923. } else if (!strcmp(model,"EX1")) {
  7924. order = 0x4949;
  7925. height -= 20;
  7926. top_margin = 2;
  7927. if ((width -= 6) > 3682) {
  7928. height -= 10;
  7929. width -= 46;
  7930. top_margin = 8;
  7931. }
  7932. } else if (!strcmp(model,"WB2000")) {
  7933. order = 0x4949;
  7934. height -= 3;
  7935. top_margin = 2;
  7936. if ((width -= 10) > 3718) {
  7937. height -= 28;
  7938. width -= 56;
  7939. top_margin = 8;
  7940. }
  7941. } else if (fsize == 20487168) {
  7942. height = 2808;
  7943. width = 3648;
  7944. goto wb550;
  7945. } else if (fsize == 24000000) {
  7946. height = 3000;
  7947. width = 4000;
  7948. wb550:
  7949. strcpy (model, "WB550");
  7950. order = 0x4d4d;
  7951. load_raw = &CLASS unpacked_load_raw;
  7952. load_flags = 6;
  7953. maximum = 0x3df;
  7954. } else if (!strcmp(model,"EX2F")) {
  7955. height = 3045;
  7956. width = 4070;
  7957. top_margin = 3;
  7958. order = 0x4949;
  7959. filters = 0x49494949;
  7960. load_raw = &CLASS unpacked_load_raw;
  7961. } else if (!strcmp(model,"STV680 VGA")) {
  7962. height = 484;
  7963. width = 644;
  7964. load_raw = &CLASS eight_bit_load_raw;
  7965. flip = 2;
  7966. filters = 0x16161616;
  7967. black = 16;
  7968. } else
  7969. identify2(fsize,flen,head); /* Avoid MS VS 2008/2010 bug */
  7970. if (!model[0])
  7971. sprintf (model, "%dx%d", width, height);
  7972. if (filters == UINT_MAX) filters = 0x94949494;
  7973. if (raw_color) adobe_coeff (make, model);
  7974. if (load_raw == &CLASS kodak_radc_load_raw)
  7975. if (raw_color) adobe_coeff ("Apple","Quicktake");
  7976. if (thumb_offset && !thumb_height) {
  7977. fseek (ifp, thumb_offset, SEEK_SET);
  7978. if (ljpeg_start (&jh, 1)) {
  7979. thumb_width = jh.wide;
  7980. thumb_height = jh.high;
  7981. }
  7982. }
  7983. dng_skip:
  7984. if (fuji_width) {
  7985. fuji_width = width >> !fuji_layout;
  7986. if (~fuji_width & 1) filters = 0x49494949;
  7987. width = (height >> fuji_layout) + fuji_width;
  7988. height = width - 1;
  7989. pixel_aspect = 1;
  7990. } else {
  7991. if (raw_height < height) raw_height = height;
  7992. if (raw_width < width ) raw_width = width;
  7993. }
  7994. if (!tiff_bps) tiff_bps = 12;
  7995. if (!maximum) maximum = (1 << tiff_bps) - 1;
  7996. if (!load_raw || height < 22) is_raw = 0;
  7997. #ifdef NO_JASPER
  7998. if (load_raw == &CLASS redcine_load_raw) {
  7999. #ifdef DCRAW_VERBOSE
  8000. fprintf (stderr,_("%s: You must link dcraw with %s!!\n"),
  8001. ifname, "libjasper");
  8002. #endif
  8003. is_raw = 0;
  8004. #ifdef LIBRAW_LIBRARY_BUILD
  8005. imgdata.process_warnings |= LIBRAW_WARN_NO_JASPER;
  8006. #endif
  8007. }
  8008. #endif
  8009. #ifdef NO_JPEG
  8010. if (load_raw == &CLASS kodak_jpeg_load_raw ||
  8011. load_raw == &CLASS lossy_dng_load_raw) {
  8012. #ifdef DCRAW_VERBOSE
  8013. fprintf (stderr,_("%s: You must link dcraw with %s!!\n"),
  8014. ifname, "libjpeg");
  8015. #endif
  8016. is_raw = 0;
  8017. #ifdef LIBRAW_LIBRARY_BUILD
  8018. imgdata.process_warnings |= LIBRAW_WARN_NO_JPEGLIB;
  8019. #endif
  8020. }
  8021. #endif
  8022. if (!cdesc[0])
  8023. strcpy (cdesc, colors == 3 ? "RGBG":"GMCY");
  8024. if (!raw_height) raw_height = height;
  8025. if (!raw_width ) raw_width = width;
  8026. if (filters && colors == 3)
  8027. filters |= ((filters >> 2 & 0x22222222) |
  8028. (filters << 2 & 0x88888888)) & filters << 1;
  8029. notraw:
  8030. if (flip == -1) flip = tiff_flip;
  8031. if (flip == -1) flip = 0;
  8032. #ifdef LIBRAW_LIBRARY_BUILD
  8033. RUN_CALLBACK(LIBRAW_PROGRESS_IDENTIFY,1,2);
  8034. #endif
  8035. }
  8036. void CLASS identify2(unsigned fsize, unsigned flen, char *head)
  8037. {
  8038. short pana[][6] = {
  8039. { 3130, 1743, 4, 0, -6, 0 },
  8040. { 3130, 2055, 4, 0, -6, 0 },
  8041. { 3130, 2319, 4, 0, -6, 0 },
  8042. { 3170, 2103, 18, 0,-42, 20 },
  8043. { 3170, 2367, 18, 13,-42,-21 },
  8044. { 3177, 2367, 0, 0, -1, 0 },
  8045. { 3304, 2458, 0, 0, -1, 0 },
  8046. { 3330, 2463, 9, 0, -5, 0 },
  8047. { 3330, 2479, 9, 0,-17, 4 },
  8048. { 3370, 1899, 15, 0,-44, 20 },
  8049. { 3370, 2235, 15, 0,-44, 20 },
  8050. { 3370, 2511, 15, 10,-44,-21 },
  8051. { 3690, 2751, 3, 0, -8, -3 },
  8052. { 3710, 2751, 0, 0, -3, 0 },
  8053. { 3724, 2450, 0, 0, 0, -2 },
  8054. { 3770, 2487, 17, 0,-44, 19 },
  8055. { 3770, 2799, 17, 15,-44,-19 },
  8056. { 3880, 2170, 6, 0, -6, 0 },
  8057. { 4060, 3018, 0, 0, 0, -2 },
  8058. { 4290, 2391, 3, 0, -8, -1 },
  8059. { 4330, 2439, 17, 15,-44,-19 },
  8060. { 4508, 2962, 0, 0, -3, -4 },
  8061. { 4508, 3330, 0, 0, -3, -6 } };
  8062. struct jhead jh;
  8063. int i;
  8064. if (!strcmp(model,"N95")) {
  8065. height = raw_height - (top_margin = 2);
  8066. } else if (!strcmp(model,"531C")) {
  8067. height = 1200;
  8068. width = 1600;
  8069. load_raw = &CLASS unpacked_load_raw;
  8070. filters = 0x49494949;
  8071. } else if (!strcmp(model,"640x480")) {
  8072. height = 480;
  8073. width = 640;
  8074. load_raw = &CLASS eight_bit_load_raw;
  8075. gamma_curve (0.45, 4.5, 1, 255);
  8076. } else if (!strcmp(model,"F-080C")) {
  8077. height = 768;
  8078. width = 1024;
  8079. load_raw = &CLASS eight_bit_load_raw;
  8080. } else if (!strcmp(model,"F-145C")) {
  8081. height = 1040;
  8082. width = 1392;
  8083. load_raw = &CLASS eight_bit_load_raw;
  8084. } else if (!strcmp(model,"F-201C")) {
  8085. height = 1200;
  8086. width = 1600;
  8087. load_raw = &CLASS eight_bit_load_raw;
  8088. } else if (!strcmp(model,"F-510C")) {
  8089. height = 1958;
  8090. width = 2588;
  8091. load_raw = fsize < 7500000 ?
  8092. &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
  8093. data_offset = fsize - width*height*(fsize >> 22);
  8094. maximum = 0xfff0;
  8095. } else if (!strcmp(model,"F-810C")) {
  8096. height = 2469;
  8097. width = 3272;
  8098. load_raw = &CLASS unpacked_load_raw;
  8099. maximum = 0xfff0;
  8100. } else if (!strcmp(model,"XCD-SX910CR")) {
  8101. height = 1024;
  8102. width = 1375;
  8103. raw_width = 1376;
  8104. filters = 0x49494949;
  8105. maximum = 0x3ff;
  8106. load_raw = fsize < 2000000 ?
  8107. &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
  8108. } else if (!strcmp(model,"2010")) {
  8109. height = 1207;
  8110. width = 1608;
  8111. order = 0x4949;
  8112. filters = 0x16161616;
  8113. data_offset = 3212;
  8114. maximum = 0x3ff;
  8115. load_raw = &CLASS unpacked_load_raw;
  8116. } else if (!strcmp(model,"A782")) {
  8117. height = 3000;
  8118. width = 2208;
  8119. filters = 0x61616161;
  8120. load_raw = fsize < 10000000 ?
  8121. &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
  8122. maximum = 0xffc0;
  8123. } else if (!strcmp(model,"3320AF")) {
  8124. height = 1536;
  8125. raw_width = width = 2048;
  8126. filters = 0x61616161;
  8127. load_raw = &CLASS unpacked_load_raw;
  8128. maximum = 0x3ff;
  8129. fseek (ifp, 0x300000, SEEK_SET);
  8130. if ((order = guess_byte_order(0x10000)) == 0x4d4d) {
  8131. height -= (top_margin = 16);
  8132. width -= (left_margin = 28);
  8133. maximum = 0xf5c0;
  8134. strcpy (make, "ISG");
  8135. model[0] = 0;
  8136. }
  8137. } else if (!strcmp(make,"Hasselblad")) {
  8138. if (load_raw == &CLASS lossless_jpeg_load_raw)
  8139. load_raw = &CLASS hasselblad_load_raw;
  8140. if (raw_width == 7262) {
  8141. height = 5444;
  8142. width = 7248;
  8143. top_margin = 4;
  8144. left_margin = 7;
  8145. filters = 0x61616161;
  8146. } else if (raw_width == 7410) {
  8147. height = 5502;
  8148. width = 7328;
  8149. top_margin = 4;
  8150. left_margin = 41;
  8151. filters = 0x61616161;
  8152. } else if (raw_width == 9044) {
  8153. height = 6716;
  8154. width = 8964;
  8155. top_margin = 8;
  8156. left_margin = 40;
  8157. black += load_flags = 256;
  8158. maximum = 0x8101;
  8159. } else if (raw_width == 4090) {
  8160. strcpy (model, "V96C");
  8161. height -= (top_margin = 6);
  8162. width -= (left_margin = 3) + 7;
  8163. filters = 0x61616161;
  8164. }
  8165. } else if (!strcmp(make,"Sinar")) {
  8166. if (!memcmp(head,"8BPS",4)) {
  8167. fseek (ifp, 14, SEEK_SET);
  8168. height = get4();
  8169. width = get4();
  8170. filters = 0x61616161;
  8171. data_offset = 68;
  8172. }
  8173. if (!load_raw) load_raw = &CLASS unpacked_load_raw;
  8174. maximum = 0x3fff;
  8175. } else if (!strcmp(make,"Leaf")) {
  8176. maximum = 0x3fff;
  8177. fseek (ifp, data_offset, SEEK_SET);
  8178. if (ljpeg_start (&jh, 1) && jh.bits == 15)
  8179. maximum = 0x1fff;
  8180. if (tiff_samples > 1) filters = 0;
  8181. if (tiff_samples > 1 || tile_length < raw_height) {
  8182. load_raw = &CLASS leaf_hdr_load_raw;
  8183. raw_width = tile_width;
  8184. }
  8185. if ((width | height) == 2048) {
  8186. if (tiff_samples == 1) {
  8187. filters = 1;
  8188. strcpy (cdesc, "RBTG");
  8189. strcpy (model, "CatchLight");
  8190. top_margin = 8; left_margin = 18; height = 2032; width = 2016;
  8191. } else {
  8192. strcpy (model, "DCB2");
  8193. top_margin = 10; left_margin = 16; height = 2028; width = 2022;
  8194. }
  8195. } else if (width+height == 3144+2060) {
  8196. if (!model[0]) strcpy (model, "Cantare");
  8197. if (width > height) {
  8198. top_margin = 6; left_margin = 32; height = 2048; width = 3072;
  8199. filters = 0x61616161;
  8200. } else {
  8201. left_margin = 6; top_margin = 32; width = 2048; height = 3072;
  8202. filters = 0x16161616;
  8203. }
  8204. if (!cam_mul[0] || model[0] == 'V') filters = 0;
  8205. else is_raw = tiff_samples;
  8206. } else if (width == 2116) {
  8207. strcpy (model, "Valeo 6");
  8208. height -= 2 * (top_margin = 30);
  8209. width -= 2 * (left_margin = 55);
  8210. filters = 0x49494949;
  8211. } else if (width == 3171) {
  8212. strcpy (model, "Valeo 6");
  8213. height -= 2 * (top_margin = 24);
  8214. width -= 2 * (left_margin = 24);
  8215. filters = 0x16161616;
  8216. }
  8217. } else if (!strcmp(make,"LEICA") || !strcmp(make,"Panasonic")) {
  8218. if ((flen - data_offset) / (raw_width*8/7) == raw_height)
  8219. load_raw = &CLASS panasonic_load_raw;
  8220. if (!load_raw) {
  8221. load_raw = &CLASS unpacked_load_raw;
  8222. load_flags = 4;
  8223. }
  8224. zero_is_bad = 1;
  8225. if ((height += 12) > raw_height) height = raw_height;
  8226. for (i=0; i < sizeof pana / sizeof *pana; i++)
  8227. if (raw_width == pana[i][0] && raw_height == pana[i][1]) {
  8228. left_margin = pana[i][2];
  8229. top_margin = pana[i][3];
  8230. width += pana[i][4];
  8231. height += pana[i][5];
  8232. }
  8233. filters = 0x01010101 * (uchar) "\x94\x61\x49\x16"
  8234. [((filters-1) ^ (left_margin & 1) ^ (top_margin << 1)) & 3];
  8235. } else if (!strcmp(model,"C770UZ")) {
  8236. height = 1718;
  8237. width = 2304;
  8238. filters = 0x16161616;
  8239. load_raw = &CLASS packed_load_raw;
  8240. load_flags = 30;
  8241. } else if (!strcmp(make,"OLYMPUS")) {
  8242. height += height & 1;
  8243. filters = exif_cfa;
  8244. if (width == 4100) width -= 4;
  8245. if (width == 4080) width -= 24;
  8246. if (load_raw == &CLASS unpacked_load_raw)
  8247. load_flags = 4;
  8248. tiff_bps = 12;
  8249. if (!strcmp(model,"E-300") ||
  8250. !strcmp(model,"E-500")) {
  8251. width -= 20;
  8252. if (load_raw == &CLASS unpacked_load_raw) {
  8253. maximum = 0xfc3;
  8254. memset (cblack, 0, sizeof cblack);
  8255. }
  8256. } else if (!strcmp(model,"E-330")) {
  8257. width -= 30;
  8258. if (load_raw == &CLASS unpacked_load_raw)
  8259. maximum = 0xf79;
  8260. } else if (!strcmp(model,"SP550UZ")) {
  8261. thumb_length = flen - (thumb_offset = 0xa39800);
  8262. thumb_height = 480;
  8263. thumb_width = 640;
  8264. } else if (!strcmp(model,"XZ-2")) {
  8265. load_raw = &CLASS packed_load_raw;
  8266. load_flags = 24;
  8267. } else if (!strcmp(model,"XZ-10")) {
  8268. load_raw = &CLASS packed_load_raw;
  8269. load_flags = 24;
  8270. }
  8271. } else if (!strcmp(model,"N Digital")) {
  8272. height = 2047;
  8273. width = 3072;
  8274. filters = 0x61616161;
  8275. data_offset = 0x1a00;
  8276. load_raw = &CLASS packed_load_raw;
  8277. } else if (!strcmp(model,"DSC-F828")) {
  8278. width = 3288;
  8279. left_margin = 5;
  8280. mask[1][3] = -17;
  8281. data_offset = 862144;
  8282. load_raw = &CLASS sony_load_raw;
  8283. filters = 0x9c9c9c9c;
  8284. colors = 4;
  8285. strcpy (cdesc, "RGBE");
  8286. } else if (!strcmp(model,"DSC-V3")) {
  8287. width = 3109;
  8288. left_margin = 59;
  8289. mask[0][1] = 9;
  8290. data_offset = 787392;
  8291. load_raw = &CLASS sony_load_raw;
  8292. } else if (!strcmp(make,"SONY") && raw_width == 3984) {
  8293. adobe_coeff ("SONY","DSC-R1");
  8294. width = 3925;
  8295. order = 0x4d4d;
  8296. } else if (!strcmp(make,"SONY") && raw_width == 5504) {
  8297. width -= 8;
  8298. } else if (!strcmp(make,"SONY") && raw_width == 6048) {
  8299. width -= 24;
  8300. } else if (!strcmp(model,"DSLR-A100")) {
  8301. if (width == 3880) {
  8302. height--;
  8303. width = ++raw_width;
  8304. } else {
  8305. order = 0x4d4d;
  8306. load_flags = 2;
  8307. }
  8308. filters = 0x61616161;
  8309. } else if (!strcmp(model,"DSLR-A350")) {
  8310. height -= 4;
  8311. } else if (!strcmp(model,"PIXL")) {
  8312. height -= top_margin = 4;
  8313. width -= left_margin = 32;
  8314. gamma_curve (0, 7, 1, 255);
  8315. } else if (!strcmp(model,"C603v")) {
  8316. height = 480;
  8317. width = 640;
  8318. if (fsize < 614400 || find_green (16, 16, 3840, 5120) < 25) goto c603v;
  8319. strcpy (model,"KAI-0340");
  8320. height -= 3;
  8321. data_offset = 3840;
  8322. order = 0x4949;
  8323. load_raw = &CLASS unpacked_load_raw;
  8324. } else if (!strcmp(model,"C603y")) {
  8325. height = 2134;
  8326. width = 2848;
  8327. c603v:
  8328. filters = 0;
  8329. load_raw = &CLASS kodak_yrgb_load_raw;
  8330. gamma_curve (0, 3.875, 1, 255);
  8331. } else if (!strcmp(model,"C603")) {
  8332. raw_height = height = 2152;
  8333. raw_width = width = 2864;
  8334. goto c603;
  8335. } else if (!strcmp(model,"C330")) {
  8336. height = 1744;
  8337. width = 2336;
  8338. raw_height = 1779;
  8339. raw_width = 2338;
  8340. top_margin = 33;
  8341. left_margin = 1;
  8342. c603:
  8343. order = 0x4949;
  8344. if ((data_offset = fsize - raw_height*raw_width)) {
  8345. fseek (ifp, 168, SEEK_SET);
  8346. read_shorts (curve, 256);
  8347. } else gamma_curve (0, 3.875, 1, 255);
  8348. load_raw = &CLASS eight_bit_load_raw;
  8349. } else if (!strncasecmp(model,"EasyShare",9)) {
  8350. data_offset = data_offset < 0x15000 ? 0x15000 : 0x17000;
  8351. load_raw = &CLASS packed_load_raw;
  8352. } else if (!strcasecmp(make,"KODAK")) {
  8353. if (filters == UINT_MAX) filters = 0x61616161;
  8354. if (!strncmp(model,"NC2000",6)) {
  8355. width -= 4;
  8356. left_margin = 2;
  8357. } else if (!strcmp(model,"EOSDCS3B")) {
  8358. width -= 4;
  8359. left_margin = 2;
  8360. } else if (!strcmp(model,"EOSDCS1")) {
  8361. width -= 4;
  8362. left_margin = 2;
  8363. } else if (!strcmp(model,"DCS420")) {
  8364. width -= 4;
  8365. left_margin = 2;
  8366. } else if (!strncmp(model,"DCS460 ",7)) {
  8367. model[6] = 0;
  8368. width -= 4;
  8369. left_margin = 2;
  8370. } else if (!strcmp(model,"DCS460A")) {
  8371. width -= 4;
  8372. left_margin = 2;
  8373. colors = 1;
  8374. filters = 0;
  8375. } else if (!strcmp(model,"DCS660M")) {
  8376. black = 214;
  8377. colors = 1;
  8378. filters = 0;
  8379. } else if (!strcmp(model,"DCS760M")) {
  8380. colors = 1;
  8381. filters = 0;
  8382. }
  8383. if (!strcmp(model+4,"20X"))
  8384. strcpy (cdesc, "MYCY");
  8385. if (strstr(model,"DC25")) {
  8386. strcpy (model, "DC25");
  8387. data_offset = 15424;
  8388. }
  8389. if (!strncmp(model,"DC2",3)) {
  8390. raw_height = height = 242;
  8391. if (flen < 100000) {
  8392. raw_width = 256; width = 249;
  8393. pixel_aspect = (4.0*height) / (3.0*width);
  8394. } else {
  8395. raw_width = 512; width = 501;
  8396. pixel_aspect = (493.0*height) / (373.0*width);
  8397. }
  8398. data_offset += raw_width + 1;
  8399. colors = 4;
  8400. filters = 0x8d8d8d8d;
  8401. simple_coeff(1);
  8402. pre_mul[1] = 1.179;
  8403. pre_mul[2] = 1.209;
  8404. pre_mul[3] = 1.036;
  8405. load_raw = &CLASS eight_bit_load_raw;
  8406. } else if (!strcmp(model,"40")) {
  8407. strcpy (model, "DC40");
  8408. height = 512;
  8409. width = 768;
  8410. data_offset = 1152;
  8411. load_raw = &CLASS kodak_radc_load_raw;
  8412. } else if (strstr(model,"DC50")) {
  8413. strcpy (model, "DC50");
  8414. height = 512;
  8415. width = 768;
  8416. data_offset = 19712;
  8417. load_raw = &CLASS kodak_radc_load_raw;
  8418. } else if (strstr(model,"DC120")) {
  8419. strcpy (model, "DC120");
  8420. height = 976;
  8421. width = 848;
  8422. pixel_aspect = height/0.75/width;
  8423. load_raw = tiff_compress == 7 ?
  8424. &CLASS kodak_jpeg_load_raw : &CLASS kodak_dc120_load_raw;
  8425. } else if (!strcmp(model,"DCS200")) {
  8426. thumb_height = 128;
  8427. thumb_width = 192;
  8428. thumb_offset = 6144;
  8429. thumb_misc = 360;
  8430. write_thumb = &CLASS layer_thumb;
  8431. height = 1024;
  8432. width = 1536;
  8433. data_offset = 79872;
  8434. load_raw = &CLASS eight_bit_load_raw;
  8435. black = 17;
  8436. }
  8437. } else if (!strcmp(model,"Fotoman Pixtura")) {
  8438. height = 512;
  8439. width = 768;
  8440. data_offset = 3632;
  8441. load_raw = &CLASS kodak_radc_load_raw;
  8442. filters = 0x61616161;
  8443. simple_coeff(2);
  8444. } else if (!strncmp(model,"QuickTake",9)) {
  8445. if (head[5]) strcpy (model+10, "200");
  8446. fseek (ifp, 544, SEEK_SET);
  8447. height = get2();
  8448. width = get2();
  8449. data_offset = (get4(),get2()) == 30 ? 738:736;
  8450. if (height > width) {
  8451. SWAP(height,width);
  8452. fseek (ifp, data_offset-6, SEEK_SET);
  8453. flip = ~get2() & 3 ? 5:6;
  8454. }
  8455. filters = 0x61616161;
  8456. } else if (!strcmp(make,"Rollei") && !load_raw) {
  8457. switch (raw_width) {
  8458. case 1316:
  8459. height = 1030;
  8460. width = 1300;
  8461. top_margin = 1;
  8462. left_margin = 6;
  8463. break;
  8464. case 2568:
  8465. height = 1960;
  8466. width = 2560;
  8467. top_margin = 2;
  8468. left_margin = 8;
  8469. }
  8470. filters = 0x16161616;
  8471. load_raw = &CLASS rollei_load_raw;
  8472. } else if (!strcmp(model,"PC-CAM 600")) {
  8473. height = 768;
  8474. data_offset = width = 1024;
  8475. filters = 0x49494949;
  8476. load_raw = &CLASS eight_bit_load_raw;
  8477. } else if (!strcmp(model,"QV-2000UX")) {
  8478. height = 1208;
  8479. width = 1632;
  8480. data_offset = width * 2;
  8481. load_raw = &CLASS eight_bit_load_raw;
  8482. } else if (fsize == 3217760) {
  8483. height = 1546;
  8484. width = 2070;
  8485. raw_width = 2080;
  8486. load_raw = &CLASS eight_bit_load_raw;
  8487. } else if (!strcmp(model,"QV-4000")) {
  8488. height = 1700;
  8489. width = 2260;
  8490. load_raw = &CLASS unpacked_load_raw;
  8491. maximum = 0xffff;
  8492. } else if (!strcmp(model,"QV-5700")) {
  8493. height = 1924;
  8494. width = 2576;
  8495. raw_width = 3232;
  8496. tiff_bps = 10;
  8497. } else if (!strcmp(model,"QV-R41")) {
  8498. height = 1720;
  8499. width = 2312;
  8500. raw_width = 3520;
  8501. left_margin = 2;
  8502. } else if (!strcmp(model,"QV-R51")) {
  8503. height = 1926;
  8504. width = 2580;
  8505. raw_width = 3904;
  8506. } else if (!strcmp(model,"EX-S20")) {
  8507. height = 1208;
  8508. width = 1620;
  8509. raw_width = 2432;
  8510. flip = 3;
  8511. } else if (!strcmp(model,"EX-S100")) {
  8512. height = 1544;
  8513. width = 2058;
  8514. raw_width = 3136;
  8515. } else if (!strcmp(model,"EX-Z50")) {
  8516. height = 1931;
  8517. width = 2570;
  8518. raw_width = 3904;
  8519. } else if (!strcmp(model,"EX-Z500")) {
  8520. height = 1937;
  8521. width = 2577;
  8522. raw_width = 3904;
  8523. filters = 0x16161616;
  8524. } else if (!strcmp(model,"EX-Z55")) {
  8525. height = 1960;
  8526. width = 2570;
  8527. raw_width = 3904;
  8528. } else if (!strcmp(model,"EX-Z60")) {
  8529. height = 2145;
  8530. width = 2833;
  8531. raw_width = 3584;
  8532. filters = 0x16161616;
  8533. tiff_bps = 10;
  8534. } else if (!strcmp(model,"EX-Z75")) {
  8535. height = 2321;
  8536. width = 3089;
  8537. raw_width = 4672;
  8538. maximum = 0xfff;
  8539. } else if (!strcmp(model,"EX-Z750")) {
  8540. height = 2319;
  8541. width = 3087;
  8542. raw_width = 4672;
  8543. maximum = 0xfff;
  8544. } else if (!strcmp(model,"EX-Z850")) {
  8545. height = 2468;
  8546. width = 3279;
  8547. raw_width = 4928;
  8548. maximum = 0xfff;
  8549. } else if (!strcmp(model,"EX-Z8")) {
  8550. height = 2467;
  8551. width = 3281;
  8552. raw_height = 2502;
  8553. raw_width = 4992;
  8554. maximum = 0xfff;
  8555. } else if (fsize == 15499264) { /* EX-Z1050 or EX-Z1080 */
  8556. height = 2752;
  8557. width = 3672;
  8558. raw_width = 5632;
  8559. } else if (!strcmp(model,"EX-ZR100")) {
  8560. height = 3044;
  8561. width = 4072;
  8562. raw_width = 4096;
  8563. load_flags = 80;
  8564. } else if (!strcmp(model,"EX-P505")) {
  8565. height = 1928;
  8566. width = 2568;
  8567. raw_width = 3852;
  8568. maximum = 0xfff;
  8569. } else if (fsize == 9313536) { /* EX-P600 or QV-R61 */
  8570. height = 2142;
  8571. width = 2844;
  8572. raw_width = 4288;
  8573. } else if (!strcmp(model,"EX-P700")) {
  8574. height = 2318;
  8575. width = 3082;
  8576. raw_width = 4672;
  8577. }
  8578. else if (!strcmp(model,"GRAS-50S5C")) {
  8579. height = 2048;
  8580. width = 2440;
  8581. load_raw = &CLASS unpacked_load_raw;
  8582. data_offset = 0;
  8583. filters = 0x49494949;
  8584. order = 0x4949;
  8585. maximum = 0xfffC;
  8586. } else if (!strcmp(model,"BB-500CL")) {
  8587. height = 2058;
  8588. width = 2448;
  8589. load_raw = &CLASS unpacked_load_raw;
  8590. data_offset = 0;
  8591. filters = 0x94949494;
  8592. order = 0x4949;
  8593. maximum = 0x3fff;
  8594. } else if (!strcmp(model,"BB-500GE")) {
  8595. height = 2058;
  8596. width = 2456;
  8597. load_raw = &CLASS unpacked_load_raw;
  8598. data_offset = 0;
  8599. filters = 0x94949494;
  8600. order = 0x4949;
  8601. maximum = 0x3fff;
  8602. } else if (!strcmp(model,"SVS625CL")) {
  8603. height = 2050;
  8604. width = 2448;
  8605. load_raw = &CLASS unpacked_load_raw;
  8606. data_offset = 0;
  8607. filters = 0x94949494;
  8608. order = 0x4949;
  8609. maximum = 0x0fff;
  8610. }
  8611. }
  8612. void CLASS convert_to_rgb()
  8613. {
  8614. #ifndef LIBRAW_LIBRARY_BUILD
  8615. int row, col, c;
  8616. #endif
  8617. int i, j, k;
  8618. #ifndef LIBRAW_LIBRARY_BUILD
  8619. ushort *img;
  8620. float out[3];
  8621. #endif
  8622. float out_cam[3][4];
  8623. double num, inverse[3][3];
  8624. static const double xyzd50_srgb[3][3] =
  8625. { { 0.436083, 0.385083, 0.143055 },
  8626. { 0.222507, 0.716888, 0.060608 },
  8627. { 0.013930, 0.097097, 0.714022 } };
  8628. static const double rgb_rgb[3][3] =
  8629. { { 1,0,0 }, { 0,1,0 }, { 0,0,1 } };
  8630. static const double adobe_rgb[3][3] =
  8631. { { 0.715146, 0.284856, 0.000000 },
  8632. { 0.000000, 1.000000, 0.000000 },
  8633. { 0.000000, 0.041166, 0.958839 } };
  8634. static const double wide_rgb[3][3] =
  8635. { { 0.593087, 0.404710, 0.002206 },
  8636. { 0.095413, 0.843149, 0.061439 },
  8637. { 0.011621, 0.069091, 0.919288 } };
  8638. static const double prophoto_rgb[3][3] =
  8639. { { 0.529317, 0.330092, 0.140588 },
  8640. { 0.098368, 0.873465, 0.028169 },
  8641. { 0.016879, 0.117663, 0.865457 } };
  8642. static const double (*out_rgb[])[3] =
  8643. { rgb_rgb, adobe_rgb, wide_rgb, prophoto_rgb, xyz_rgb };
  8644. static const char *name[] =
  8645. { "sRGB", "Adobe RGB (1998)", "WideGamut D65", "ProPhoto D65", "XYZ" };
  8646. static const unsigned phead[] =
  8647. { 1024, 0, 0x2100000, 0x6d6e7472, 0x52474220, 0x58595a20, 0, 0, 0,
  8648. 0x61637370, 0, 0, 0x6e6f6e65, 0, 0, 0, 0, 0xf6d6, 0x10000, 0xd32d };
  8649. unsigned pbody[] =
  8650. { 10, 0x63707274, 0, 36, /* cprt */
  8651. 0x64657363, 0, 40, /* desc */
  8652. 0x77747074, 0, 20, /* wtpt */
  8653. 0x626b7074, 0, 20, /* bkpt */
  8654. 0x72545243, 0, 14, /* rTRC */
  8655. 0x67545243, 0, 14, /* gTRC */
  8656. 0x62545243, 0, 14, /* bTRC */
  8657. 0x7258595a, 0, 20, /* rXYZ */
  8658. 0x6758595a, 0, 20, /* gXYZ */
  8659. 0x6258595a, 0, 20 }; /* bXYZ */
  8660. static const unsigned pwhite[] = { 0xf351, 0x10000, 0x116cc };
  8661. unsigned pcurve[] = { 0x63757276, 0, 1, 0x1000000 };
  8662. #ifdef LIBRAW_LIBRARY_BUILD
  8663. RUN_CALLBACK(LIBRAW_PROGRESS_CONVERT_RGB,0,2);
  8664. #endif
  8665. gamma_curve (gamm[0], gamm[1], 0, 0);
  8666. memcpy (out_cam, rgb_cam, sizeof out_cam);
  8667. #ifndef LIBRAW_LIBRARY_BUILD
  8668. raw_color |= colors == 1 || document_mode ||
  8669. output_color < 1 || output_color > 5;
  8670. #else
  8671. raw_color |= colors == 1 ||
  8672. output_color < 1 || output_color > 5;
  8673. #endif
  8674. if (!raw_color) {
  8675. oprof = (unsigned *) calloc (phead[0], 1);
  8676. merror (oprof, "convert_to_rgb()");
  8677. memcpy (oprof, phead, sizeof phead);
  8678. if (output_color == 5) oprof[4] = oprof[5];
  8679. oprof[0] = 132 + 12*pbody[0];
  8680. for (i=0; i < pbody[0]; i++) {
  8681. oprof[oprof[0]/4] = i ? (i > 1 ? 0x58595a20 : 0x64657363) : 0x74657874;
  8682. pbody[i*3+2] = oprof[0];
  8683. oprof[0] += (pbody[i*3+3] + 3) & -4;
  8684. }
  8685. memcpy (oprof+32, pbody, sizeof pbody);
  8686. oprof[pbody[5]/4+2] = strlen(name[output_color-1]) + 1;
  8687. memcpy ((char *)oprof+pbody[8]+8, pwhite, sizeof pwhite);
  8688. pcurve[3] = (short)(256/gamm[5]+0.5) << 16;
  8689. for (i=4; i < 7; i++)
  8690. memcpy ((char *)oprof+pbody[i*3+2], pcurve, sizeof pcurve);
  8691. pseudoinverse ((double (*)[3]) out_rgb[output_color-1], inverse, 3);
  8692. for (i=0; i < 3; i++)
  8693. for (j=0; j < 3; j++) {
  8694. for (num = k=0; k < 3; k++)
  8695. num += xyzd50_srgb[i][k] * inverse[j][k];
  8696. oprof[pbody[j*3+23]/4+i+2] = num * 0x10000 + 0.5;
  8697. }
  8698. for (i=0; i < phead[0]/4; i++)
  8699. oprof[i] = htonl(oprof[i]);
  8700. strcpy ((char *)oprof+pbody[2]+8, "auto-generated by dcraw");
  8701. strcpy ((char *)oprof+pbody[5]+12, name[output_color-1]);
  8702. for (i=0; i < 3; i++)
  8703. for (j=0; j < colors; j++)
  8704. for (out_cam[i][j] = k=0; k < 3; k++)
  8705. out_cam[i][j] += out_rgb[output_color-1][i][k] * rgb_cam[k][j];
  8706. }
  8707. #ifdef DCRAW_VERBOSE
  8708. if (verbose)
  8709. fprintf (stderr, raw_color ? _("Building histograms...\n") :
  8710. _("Converting to %s colorspace...\n"), name[output_color-1]);
  8711. #endif
  8712. #ifdef LIBRAW_LIBRARY_BUILD
  8713. convert_to_rgb_loop(out_cam);
  8714. #else
  8715. memset (histogram, 0, sizeof histogram);
  8716. for (img=image[0], row=0; row < height; row++)
  8717. for (col=0; col < width; col++, img+=4) {
  8718. if (!raw_color) {
  8719. out[0] = out[1] = out[2] = 0;
  8720. FORCC {
  8721. out[0] += out_cam[0][c] * img[c];
  8722. out[1] += out_cam[1][c] * img[c];
  8723. out[2] += out_cam[2][c] * img[c];
  8724. }
  8725. FORC3 img[c] = CLIP((int) out[c]);
  8726. }
  8727. else if (document_mode)
  8728. img[0] = img[fcol(row,col)];
  8729. FORCC histogram[c][img[c] >> 3]++;
  8730. }
  8731. #endif
  8732. if (colors == 4 && output_color) colors = 3;
  8733. #ifndef LIBRAW_LIBRARY_BUILD
  8734. if (document_mode && filters) colors = 1;
  8735. #endif
  8736. #ifdef LIBRAW_LIBRARY_BUILD
  8737. RUN_CALLBACK(LIBRAW_PROGRESS_CONVERT_RGB,1,2);
  8738. #endif
  8739. }
  8740. void CLASS fuji_rotate()
  8741. {
  8742. int i, row, col;
  8743. double step;
  8744. float r, c, fr, fc;
  8745. unsigned ur, uc;
  8746. ushort wide, high, (*img)[4], (*pix)[4];
  8747. if (!fuji_width) return;
  8748. #ifdef DCRAW_VERBOSE
  8749. if (verbose)
  8750. fprintf (stderr,_("Rotating image 45 degrees...\n"));
  8751. #endif
  8752. fuji_width = (fuji_width - 1 + shrink) >> shrink;
  8753. step = sqrt(0.5);
  8754. wide = fuji_width / step;
  8755. high = (height - fuji_width) / step;
  8756. img = (ushort (*)[4]) calloc (wide*high, sizeof *img);
  8757. merror (img, "fuji_rotate()");
  8758. #ifdef LIBRAW_LIBRARY_BUILD
  8759. RUN_CALLBACK(LIBRAW_PROGRESS_FUJI_ROTATE,0,2);
  8760. #endif
  8761. for (row=0; row < high; row++)
  8762. for (col=0; col < wide; col++) {
  8763. ur = r = fuji_width + (row-col)*step;
  8764. uc = c = (row+col)*step;
  8765. if (ur > height-2 || uc > width-2) continue;
  8766. fr = r - ur;
  8767. fc = c - uc;
  8768. pix = image + ur*width + uc;
  8769. for (i=0; i < colors; i++)
  8770. img[row*wide+col][i] =
  8771. (pix[ 0][i]*(1-fc) + pix[ 1][i]*fc) * (1-fr) +
  8772. (pix[width][i]*(1-fc) + pix[width+1][i]*fc) * fr;
  8773. }
  8774. free (image);
  8775. width = wide;
  8776. height = high;
  8777. image = img;
  8778. fuji_width = 0;
  8779. #ifdef LIBRAW_LIBRARY_BUILD
  8780. RUN_CALLBACK(LIBRAW_PROGRESS_FUJI_ROTATE,1,2);
  8781. #endif
  8782. }
  8783. void CLASS stretch()
  8784. {
  8785. ushort newdim, (*img)[4], *pix0, *pix1;
  8786. int row, col, c;
  8787. double rc, frac;
  8788. if (pixel_aspect == 1) return;
  8789. #ifdef LIBRAW_LIBRARY_BUILD
  8790. RUN_CALLBACK(LIBRAW_PROGRESS_STRETCH,0,2);
  8791. #endif
  8792. #ifdef DCRAW_VERBOSE
  8793. if (verbose) fprintf (stderr,_("Stretching the image...\n"));
  8794. #endif
  8795. if (pixel_aspect < 1) {
  8796. newdim = height / pixel_aspect + 0.5;
  8797. img = (ushort (*)[4]) calloc (width*newdim, sizeof *img);
  8798. merror (img, "stretch()");
  8799. for (rc=row=0; row < newdim; row++, rc+=pixel_aspect) {
  8800. frac = rc - (c = rc);
  8801. pix0 = pix1 = image[c*width];
  8802. if (c+1 < height) pix1 += width*4;
  8803. for (col=0; col < width; col++, pix0+=4, pix1+=4)
  8804. FORCC img[row*width+col][c] = pix0[c]*(1-frac) + pix1[c]*frac + 0.5;
  8805. }
  8806. height = newdim;
  8807. } else {
  8808. newdim = width * pixel_aspect + 0.5;
  8809. img = (ushort (*)[4]) calloc (height*newdim, sizeof *img);
  8810. merror (img, "stretch()");
  8811. for (rc=col=0; col < newdim; col++, rc+=1/pixel_aspect) {
  8812. frac = rc - (c = rc);
  8813. pix0 = pix1 = image[c];
  8814. if (c+1 < width) pix1 += 4;
  8815. for (row=0; row < height; row++, pix0+=width*4, pix1+=width*4)
  8816. FORCC img[row*newdim+col][c] = pix0[c]*(1-frac) + pix1[c]*frac + 0.5;
  8817. }
  8818. width = newdim;
  8819. }
  8820. free (image);
  8821. image = img;
  8822. #ifdef LIBRAW_LIBRARY_BUILD
  8823. RUN_CALLBACK(LIBRAW_PROGRESS_STRETCH,1,2);
  8824. #endif
  8825. }
  8826. int CLASS flip_index (int row, int col)
  8827. {
  8828. if (flip & 4) SWAP(row,col);
  8829. if (flip & 2) row = iheight - 1 - row;
  8830. if (flip & 1) col = iwidth - 1 - col;
  8831. return row * iwidth + col;
  8832. }
  8833. void CLASS tiff_set (ushort *ntag,
  8834. ushort tag, ushort type, int count, int val)
  8835. {
  8836. struct tiff_tag *tt;
  8837. int c;
  8838. tt = (struct tiff_tag *)(ntag+1) + (*ntag)++;
  8839. tt->tag = tag;
  8840. tt->type = type;
  8841. tt->count = count;
  8842. if (type < 3 && count <= 4)
  8843. FORC(4) tt->val.c[c] = val >> (c << 3);
  8844. else if (type == 3 && count <= 2)
  8845. FORC(2) tt->val.s[c] = val >> (c << 4);
  8846. else tt->val.i = val;
  8847. }
  8848. #define TOFF(ptr) ((char *)(&(ptr)) - (char *)th)
  8849. void CLASS tiff_head (struct tiff_hdr *th, int full)
  8850. {
  8851. int c, psize=0;
  8852. struct tm *t;
  8853. memset (th, 0, sizeof *th);
  8854. th->t_order = htonl(0x4d4d4949) >> 16;
  8855. th->magic = 42;
  8856. th->ifd = 10;
  8857. if (full) {
  8858. tiff_set (&th->ntag, 254, 4, 1, 0);
  8859. tiff_set (&th->ntag, 256, 4, 1, width);
  8860. tiff_set (&th->ntag, 257, 4, 1, height);
  8861. tiff_set (&th->ntag, 258, 3, colors, output_bps);
  8862. if (colors > 2)
  8863. th->tag[th->ntag-1].val.i = TOFF(th->bps);
  8864. FORC4 th->bps[c] = output_bps;
  8865. tiff_set (&th->ntag, 259, 3, 1, 1);
  8866. tiff_set (&th->ntag, 262, 3, 1, 1 + (colors > 1));
  8867. }
  8868. tiff_set (&th->ntag, 270, 2, 512, TOFF(th->t_desc));
  8869. tiff_set (&th->ntag, 271, 2, 64, TOFF(th->t_make));
  8870. tiff_set (&th->ntag, 272, 2, 64, TOFF(th->t_model));
  8871. if (full) {
  8872. if (oprof) psize = ntohl(oprof[0]);
  8873. tiff_set (&th->ntag, 273, 4, 1, sizeof *th + psize);
  8874. tiff_set (&th->ntag, 277, 3, 1, colors);
  8875. tiff_set (&th->ntag, 278, 4, 1, height);
  8876. tiff_set (&th->ntag, 279, 4, 1, height*width*colors*output_bps/8);
  8877. } else
  8878. tiff_set (&th->ntag, 274, 3, 1, "12435867"[flip]-'0');
  8879. tiff_set (&th->ntag, 282, 5, 1, TOFF(th->rat[0]));
  8880. tiff_set (&th->ntag, 283, 5, 1, TOFF(th->rat[2]));
  8881. tiff_set (&th->ntag, 284, 3, 1, 1);
  8882. tiff_set (&th->ntag, 296, 3, 1, 2);
  8883. tiff_set (&th->ntag, 305, 2, 32, TOFF(th->soft));
  8884. tiff_set (&th->ntag, 306, 2, 20, TOFF(th->date));
  8885. tiff_set (&th->ntag, 315, 2, 64, TOFF(th->t_artist));
  8886. tiff_set (&th->ntag, 34665, 4, 1, TOFF(th->nexif));
  8887. if (psize) tiff_set (&th->ntag, 34675, 7, psize, sizeof *th);
  8888. tiff_set (&th->nexif, 33434, 5, 1, TOFF(th->rat[4]));
  8889. tiff_set (&th->nexif, 33437, 5, 1, TOFF(th->rat[6]));
  8890. tiff_set (&th->nexif, 34855, 3, 1, iso_speed);
  8891. tiff_set (&th->nexif, 37386, 5, 1, TOFF(th->rat[8]));
  8892. if (gpsdata[1]) {
  8893. tiff_set (&th->ntag, 34853, 4, 1, TOFF(th->ngps));
  8894. tiff_set (&th->ngps, 0, 1, 4, 0x202);
  8895. tiff_set (&th->ngps, 1, 2, 2, gpsdata[29]);
  8896. tiff_set (&th->ngps, 2, 5, 3, TOFF(th->gps[0]));
  8897. tiff_set (&th->ngps, 3, 2, 2, gpsdata[30]);
  8898. tiff_set (&th->ngps, 4, 5, 3, TOFF(th->gps[6]));
  8899. tiff_set (&th->ngps, 5, 1, 1, gpsdata[31]);
  8900. tiff_set (&th->ngps, 6, 5, 1, TOFF(th->gps[18]));
  8901. tiff_set (&th->ngps, 7, 5, 3, TOFF(th->gps[12]));
  8902. tiff_set (&th->ngps, 18, 2, 12, TOFF(th->gps[20]));
  8903. tiff_set (&th->ngps, 29, 2, 12, TOFF(th->gps[23]));
  8904. memcpy (th->gps, gpsdata, sizeof th->gps);
  8905. }
  8906. th->rat[0] = th->rat[2] = 300;
  8907. th->rat[1] = th->rat[3] = 1;
  8908. FORC(6) th->rat[4+c] = 1000000;
  8909. th->rat[4] *= shutter;
  8910. th->rat[6] *= aperture;
  8911. th->rat[8] *= focal_len;
  8912. strncpy (th->t_desc, desc, 512);
  8913. strncpy (th->t_make, make, 64);
  8914. strncpy (th->t_model, model, 64);
  8915. strcpy (th->soft, "dcraw v"DCRAW_VERSION);
  8916. t = localtime (&timestamp);
  8917. sprintf (th->date, "%04d:%02d:%02d %02d:%02d:%02d",
  8918. t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
  8919. strncpy (th->t_artist, artist, 64);
  8920. }
  8921. #ifdef LIBRAW_LIBRARY_BUILD
  8922. void CLASS jpeg_thumb_writer (FILE *tfp,char *t_humb,int t_humb_length)
  8923. {
  8924. ushort exif[5];
  8925. struct tiff_hdr th;
  8926. fputc (0xff, tfp);
  8927. fputc (0xd8, tfp);
  8928. if (strcmp (t_humb+6, "Exif")) {
  8929. memcpy (exif, "\xff\xe1 Exif\0\0", 10);
  8930. exif[1] = htons (8 + sizeof th);
  8931. fwrite (exif, 1, sizeof exif, tfp);
  8932. tiff_head (&th, 0);
  8933. fwrite (&th, 1, sizeof th, tfp);
  8934. }
  8935. fwrite (t_humb+2, 1, t_humb_length-2, tfp);
  8936. }
  8937. void CLASS jpeg_thumb()
  8938. {
  8939. char *thumb;
  8940. thumb = (char *) malloc (thumb_length);
  8941. merror (thumb, "jpeg_thumb()");
  8942. fread (thumb, 1, thumb_length, ifp);
  8943. jpeg_thumb_writer(ofp,thumb,thumb_length);
  8944. free (thumb);
  8945. }
  8946. #else
  8947. void CLASS jpeg_thumb()
  8948. {
  8949. char *thumb;
  8950. ushort exif[5];
  8951. struct tiff_hdr th;
  8952. thumb = (char *) malloc (thumb_length);
  8953. merror (thumb, "jpeg_thumb()");
  8954. fread (thumb, 1, thumb_length, ifp);
  8955. fputc (0xff, ofp);
  8956. fputc (0xd8, ofp);
  8957. if (strcmp (thumb+6, "Exif")) {
  8958. memcpy (exif, "\xff\xe1 Exif\0\0", 10);
  8959. exif[1] = htons (8 + sizeof th);
  8960. fwrite (exif, 1, sizeof exif, ofp);
  8961. tiff_head (&th, 0);
  8962. fwrite (&th, 1, sizeof th, ofp);
  8963. }
  8964. fwrite (thumb+2, 1, thumb_length-2, ofp);
  8965. free (thumb);
  8966. }
  8967. #endif
  8968. void CLASS write_ppm_tiff()
  8969. {
  8970. struct tiff_hdr th;
  8971. uchar *ppm;
  8972. ushort *ppm2;
  8973. int c, row, col, soff, rstep, cstep;
  8974. int perc, val, total, t_white=0x2000;
  8975. perc = width * height * 0.01; /* 99th percentile white level */
  8976. if (fuji_width) perc /= 2;
  8977. if (!((highlight & ~2) || no_auto_bright))
  8978. for (t_white=c=0; c < colors; c++) {
  8979. for (val=0x2000, total=0; --val > 32; )
  8980. if ((total += histogram[c][val]) > perc) break;
  8981. if (t_white < val) t_white = val;
  8982. }
  8983. gamma_curve (gamm[0], gamm[1], 2, (t_white << 3)/bright);
  8984. iheight = height;
  8985. iwidth = width;
  8986. if (flip & 4) SWAP(height,width);
  8987. ppm = (uchar *) calloc (width, colors*output_bps/8);
  8988. ppm2 = (ushort *) ppm;
  8989. merror (ppm, "write_ppm_tiff()");
  8990. if (output_tiff) {
  8991. tiff_head (&th, 1);
  8992. fwrite (&th, sizeof th, 1, ofp);
  8993. if (oprof)
  8994. fwrite (oprof, ntohl(oprof[0]), 1, ofp);
  8995. } else if (colors > 3)
  8996. fprintf (ofp,
  8997. "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
  8998. width, height, colors, (1 << output_bps)-1, cdesc);
  8999. else
  9000. fprintf (ofp, "P%d\n%d %d\n%d\n",
  9001. colors/2+5, width, height, (1 << output_bps)-1);
  9002. soff = flip_index (0, 0);
  9003. cstep = flip_index (0, 1) - soff;
  9004. rstep = flip_index (1, 0) - flip_index (0, width);
  9005. for (row=0; row < height; row++, soff += rstep) {
  9006. for (col=0; col < width; col++, soff += cstep)
  9007. if (output_bps == 8)
  9008. FORCC ppm [col*colors+c] = curve[image[soff][c]] >> 8;
  9009. else FORCC ppm2[col*colors+c] = curve[image[soff][c]];
  9010. if (output_bps == 16 && !output_tiff && htons(0x55aa) != 0x55aa)
  9011. swab ((char*)ppm2, (char*)ppm2, width*colors*2);
  9012. fwrite (ppm, colors*output_bps/8, width, ofp);
  9013. }
  9014. free (ppm);
  9015. }