PageRenderTime 119ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 3ms

/internal/dcraw_common.cpp

https://github.com/jabbervorx/LibRaw
C++ | 9324 lines | 8823 code | 357 blank | 144 comment | 2893 complexity | c4d4d41542e4407f7663f13c8f9812c0 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*
  2. Copyright 2008-2010 LibRaw LLC (info@libraw.org)
  3. LibRaw is free software; you can redistribute it and/or modify
  4. it under the terms of the one of three licenses as you choose:
  5. 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
  6. (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
  7. 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  8. (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
  9. 3. LibRaw Software License 27032010
  10. (See file LICENSE.LibRaw.pdf provided in LibRaw distribution archive for details).
  11. This file is generated from Dave Coffin's dcraw.c
  12. dcraw.c -- Dave Coffin's raw photo decoder
  13. Copyright 1997-2010 by Dave Coffin, dcoffin a cybercom o net
  14. Look into dcraw homepage (probably http://cybercom.net/~dcoffin/dcraw/)
  15. for more information
  16. */
  17. #line 257 "dcraw/dcraw.c"
  18. #include <math.h>
  19. #define CLASS LibRaw::
  20. #include "libraw/libraw_types.h"
  21. #define LIBRAW_LIBRARY_BUILD
  22. #define LIBRAW_IO_REDEFINED
  23. #include "libraw/libraw.h"
  24. #include "internal/defines.h"
  25. #include "internal/var_defines.h"
  26. #include "internal/libraw_bytebuffer.h"
  27. #line 269 "dcraw/dcraw.c"
  28. int CLASS fcol (int row, int col)
  29. {
  30. static const char filter[16][16] =
  31. { { 2,1,1,3,2,3,2,0,3,2,3,0,1,2,1,0 },
  32. { 0,3,0,2,0,1,3,1,0,1,1,2,0,3,3,2 },
  33. { 2,3,3,2,3,1,1,3,3,1,2,1,2,0,0,3 },
  34. { 0,1,0,1,0,2,0,2,2,0,3,0,1,3,2,1 },
  35. { 3,1,1,2,0,1,0,2,1,3,1,3,0,1,3,0 },
  36. { 2,0,0,3,3,2,3,1,2,0,2,0,3,2,2,1 },
  37. { 2,3,3,1,2,1,2,1,2,1,1,2,3,0,0,1 },
  38. { 1,0,0,2,3,0,0,3,0,3,0,3,2,1,2,3 },
  39. { 2,3,3,1,1,2,1,0,3,2,3,0,2,3,1,3 },
  40. { 1,0,2,0,3,0,3,2,0,1,1,2,0,1,0,2 },
  41. { 0,1,1,3,3,2,2,1,1,3,3,0,2,1,3,2 },
  42. { 2,3,2,0,0,1,3,0,2,0,1,2,3,0,1,0 },
  43. { 1,3,1,2,3,2,3,2,0,2,0,1,1,0,3,0 },
  44. { 0,2,0,3,1,0,0,1,1,3,3,2,3,2,2,1 },
  45. { 2,1,3,2,3,1,2,1,0,3,0,2,0,2,0,2 },
  46. { 0,3,1,0,0,2,0,3,2,1,3,1,1,3,1,3 } };
  47. static const char filter2[6][6] =
  48. { { 1,1,0,1,1,2 },
  49. { 1,1,2,1,1,0 },
  50. { 2,0,1,0,2,1 },
  51. { 1,1,2,1,1,0 },
  52. { 1,1,0,1,1,2 },
  53. { 0,2,1,2,0,1 } };
  54. if (filters == 1) return filter[(row+top_margin)&15][(col+left_margin)&15];
  55. if (filters == 2) return filter2[(row+6) % 6][(col+6) % 6];
  56. return FC(row,col);
  57. }
  58. #ifndef __GLIBC__
  59. char *my_memmem (char *haystack, size_t haystacklen,
  60. char *needle, size_t needlelen)
  61. {
  62. char *c;
  63. for (c = haystack; c <= haystack + haystacklen - needlelen; c++)
  64. if (!memcmp (c, needle, needlelen))
  65. return c;
  66. return 0;
  67. }
  68. #define memmem my_memmem
  69. #endif
  70. #line 335 "dcraw/dcraw.c"
  71. ushort CLASS sget2 (uchar *s)
  72. {
  73. if (order == 0x4949) /* "II" means little-endian */
  74. return s[0] | s[1] << 8;
  75. else /* "MM" means big-endian */
  76. return s[0] << 8 | s[1];
  77. }
  78. ushort CLASS get2()
  79. {
  80. uchar str[2] = { 0xff,0xff };
  81. fread (str, 1, 2, ifp);
  82. return sget2(str);
  83. }
  84. unsigned CLASS sget4 (uchar *s)
  85. {
  86. if (order == 0x4949)
  87. return s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
  88. else
  89. return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3];
  90. }
  91. #define sget4(s) sget4((uchar *)s)
  92. unsigned CLASS get4()
  93. {
  94. uchar str[4] = { 0xff,0xff,0xff,0xff };
  95. fread (str, 1, 4, ifp);
  96. return sget4(str);
  97. }
  98. unsigned CLASS getint (int type)
  99. {
  100. return type == 3 ? get2() : get4();
  101. }
  102. float CLASS int_to_float (int i)
  103. {
  104. union { int i; float f; } u;
  105. u.i = i;
  106. return u.f;
  107. }
  108. double CLASS getreal (int type)
  109. {
  110. union { char c[8]; double d; } u;
  111. int i, rev;
  112. switch (type) {
  113. case 3: return (unsigned short) get2();
  114. case 4: return (unsigned int) get4();
  115. case 5: u.d = (unsigned int) get4();
  116. return u.d / (unsigned int) get4();
  117. case 8: return (signed short) get2();
  118. case 9: return (signed int) get4();
  119. case 10: u.d = (signed int) get4();
  120. return u.d / (signed int) get4();
  121. case 11: return int_to_float (get4());
  122. case 12:
  123. rev = 7 * ((order == 0x4949) == (ntohs(0x1234) == 0x1234));
  124. for (i=0; i < 8; i++)
  125. u.c[i ^ rev] = fgetc(ifp);
  126. return u.d;
  127. default: return fgetc(ifp);
  128. }
  129. }
  130. void CLASS read_shorts (ushort *pixel, int count)
  131. {
  132. if (fread (pixel, 2, count, ifp) < count) derror();
  133. if ((order == 0x4949) == (ntohs(0x1234) == 0x1234))
  134. swab ((char*)pixel, (char*)pixel, count*2);
  135. }
  136. void CLASS canon_600_fixed_wb (int temp)
  137. {
  138. static const short mul[4][5] = {
  139. { 667, 358,397,565,452 },
  140. { 731, 390,367,499,517 },
  141. { 1119, 396,348,448,537 },
  142. { 1399, 485,431,508,688 } };
  143. int lo, hi, i;
  144. float frac=0;
  145. for (lo=4; --lo; )
  146. if (*mul[lo] <= temp) break;
  147. for (hi=0; hi < 3; hi++)
  148. if (*mul[hi] >= temp) break;
  149. if (lo != hi)
  150. frac = (float) (temp - *mul[lo]) / (*mul[hi] - *mul[lo]);
  151. for (i=1; i < 5; i++)
  152. pre_mul[i-1] = 1 / (frac * mul[hi][i] + (1-frac) * mul[lo][i]);
  153. }
  154. /* Return values: 0 = white 1 = near white 2 = not white */
  155. int CLASS canon_600_color (int ratio[2], int mar)
  156. {
  157. int clipped=0, target, miss;
  158. if (flash_used) {
  159. if (ratio[1] < -104)
  160. { ratio[1] = -104; clipped = 1; }
  161. if (ratio[1] > 12)
  162. { ratio[1] = 12; clipped = 1; }
  163. } else {
  164. if (ratio[1] < -264 || ratio[1] > 461) return 2;
  165. if (ratio[1] < -50)
  166. { ratio[1] = -50; clipped = 1; }
  167. if (ratio[1] > 307)
  168. { ratio[1] = 307; clipped = 1; }
  169. }
  170. target = flash_used || ratio[1] < 197
  171. ? -38 - (398 * ratio[1] >> 10)
  172. : -123 + (48 * ratio[1] >> 10);
  173. if (target - mar <= ratio[0] &&
  174. target + 20 >= ratio[0] && !clipped) return 0;
  175. miss = target - ratio[0];
  176. if (abs(miss) >= mar*4) return 2;
  177. if (miss < -20) miss = -20;
  178. if (miss > mar) miss = mar;
  179. ratio[0] = target - miss;
  180. return 1;
  181. }
  182. void CLASS canon_600_auto_wb()
  183. {
  184. int mar, row, col, i, j, st, count[] = { 0,0 };
  185. int test[8], total[2][8], ratio[2][2], stat[2];
  186. memset (&total, 0, sizeof total);
  187. i = canon_ev + 0.5;
  188. if (i < 10) mar = 150;
  189. else if (i > 12) mar = 20;
  190. else mar = 280 - 20 * i;
  191. if (flash_used) mar = 80;
  192. for (row=14; row < height-14; row+=4)
  193. for (col=10; col < width; col+=2) {
  194. for (i=0; i < 8; i++)
  195. test[(i & 4) + FC(row+(i >> 1),col+(i & 1))] =
  196. BAYER(row+(i >> 1),col+(i & 1));
  197. for (i=0; i < 8; i++)
  198. if (test[i] < 150 || test[i] > 1500) goto next;
  199. for (i=0; i < 4; i++)
  200. if (abs(test[i] - test[i+4]) > 50) goto next;
  201. for (i=0; i < 2; i++) {
  202. for (j=0; j < 4; j+=2)
  203. ratio[i][j >> 1] = ((test[i*4+j+1]-test[i*4+j]) << 10) / test[i*4+j];
  204. stat[i] = canon_600_color (ratio[i], mar);
  205. }
  206. if ((st = stat[0] | stat[1]) > 1) goto next;
  207. for (i=0; i < 2; i++)
  208. if (stat[i])
  209. for (j=0; j < 2; j++)
  210. test[i*4+j*2+1] = test[i*4+j*2] * (0x400 + ratio[i][j]) >> 10;
  211. for (i=0; i < 8; i++)
  212. total[st][i] += test[i];
  213. count[st]++;
  214. next: ;
  215. }
  216. if (count[0] | count[1]) {
  217. st = count[0]*200 < count[1];
  218. for (i=0; i < 4; i++)
  219. pre_mul[i] = 1.0 / (total[st][i] + total[st][i+4]);
  220. }
  221. }
  222. void CLASS canon_600_coeff()
  223. {
  224. static const short table[6][12] = {
  225. { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
  226. { -1203,1715,-1136,1648, 1388,-876,267,245, -1641,2153,3921,-3409 },
  227. { -615,1127,-1563,2075, 1437,-925,509,3, -756,1268,2519,-2007 },
  228. { -190,702,-1886,2398, 2153,-1641,763,-251, -452,964,3040,-2528 },
  229. { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
  230. { -807,1319,-1785,2297, 1388,-876,769,-257, -230,742,2067,-1555 } };
  231. int t=0, i, c;
  232. float mc, yc;
  233. mc = pre_mul[1] / pre_mul[2];
  234. yc = pre_mul[3] / pre_mul[2];
  235. if (mc > 1 && mc <= 1.28 && yc < 0.8789) t=1;
  236. if (mc > 1.28 && mc <= 2) {
  237. if (yc < 0.8789) t=3;
  238. else if (yc <= 2) t=4;
  239. }
  240. if (flash_used) t=5;
  241. for (raw_color = i=0; i < 3; i++)
  242. FORCC rgb_cam[i][c] = table[t][i*4 + c] / 1024.0;
  243. }
  244. void CLASS canon_600_load_raw()
  245. {
  246. uchar data[1120], *dp;
  247. ushort *pix;
  248. int irow, row;
  249. for (irow=row=0; irow < height; irow++) {
  250. if (fread (data, 1, 1120, ifp) < 1120) derror();
  251. pix = raw_image + row*raw_width;
  252. for (dp=data; dp < data+1120; dp+=10, pix+=8) {
  253. pix[0] = (dp[0] << 2) + (dp[1] >> 6 );
  254. pix[1] = (dp[2] << 2) + (dp[1] >> 4 & 3);
  255. pix[2] = (dp[3] << 2) + (dp[1] >> 2 & 3);
  256. pix[3] = (dp[4] << 2) + (dp[1] & 3);
  257. pix[4] = (dp[5] << 2) + (dp[9] & 3);
  258. pix[5] = (dp[6] << 2) + (dp[9] >> 2 & 3);
  259. pix[6] = (dp[7] << 2) + (dp[9] >> 4 & 3);
  260. pix[7] = (dp[8] << 2) + (dp[9] >> 6 );
  261. }
  262. if ((row+=2) > height) row = 1;
  263. }
  264. }
  265. void CLASS canon_600_correct()
  266. {
  267. int row, col, val;
  268. static const short mul[4][2] =
  269. { { 1141,1145 }, { 1128,1109 }, { 1178,1149 }, { 1128,1109 } };
  270. for (row=0; row < height; row++)
  271. for (col=0; col < width; col++) {
  272. if ((val = BAYER(row,col) - black) < 0) val = 0;
  273. val = val * mul[row & 3][col & 1] >> 9;
  274. BAYER(row,col) = val;
  275. }
  276. canon_600_fixed_wb(1311);
  277. canon_600_auto_wb();
  278. canon_600_coeff();
  279. maximum = (0x3ff - black) * 1109 >> 9;
  280. black = 0;
  281. }
  282. int CLASS canon_s2is()
  283. {
  284. unsigned row;
  285. for (row=0; row < 100; row++) {
  286. fseek (ifp, row*3340 + 3284, SEEK_SET);
  287. if (getc(ifp) > 15) return 1;
  288. }
  289. return 0;
  290. }
  291. /*
  292. getbits(-1) initializes the buffer
  293. getbits(n) where 0 <= n <= 25 returns an n-bit integer
  294. */
  295. unsigned CLASS getbithuff (int nbits, ushort *huff)
  296. {
  297. #ifdef LIBRAW_NOTHREADS
  298. static unsigned bitbuf=0;
  299. static int vbits=0, reset=0;
  300. #else
  301. #define bitbuf tls->getbits.bitbuf
  302. #define vbits tls->getbits.vbits
  303. #define reset tls->getbits.reset
  304. #endif
  305. unsigned c;
  306. if (nbits == -1)
  307. return bitbuf = vbits = reset = 0;
  308. if (nbits == 0 || vbits < 0) return 0;
  309. while (!reset && vbits < nbits && (c = fgetc(ifp)) != EOF &&
  310. !(reset = zero_after_ff && c == 0xff && fgetc(ifp))) {
  311. bitbuf = (bitbuf << 8) + (uchar) c;
  312. vbits += 8;
  313. }
  314. c = bitbuf << (32-vbits) >> (32-nbits);
  315. if (huff) {
  316. vbits -= huff[c] >> 8;
  317. c = (uchar) huff[c];
  318. } else
  319. vbits -= nbits;
  320. if (vbits < 0) derror();
  321. return c;
  322. #ifndef LIBRAW_NOTHREADS
  323. #undef bitbuf
  324. #undef vbits
  325. #undef reset
  326. #endif
  327. }
  328. #define getbits(n) getbithuff(n,0)
  329. #define gethuff(h) getbithuff(*h,h+1)
  330. /*
  331. Construct a decode tree according the specification in *source.
  332. The first 16 bytes specify how many codes should be 1-bit, 2-bit
  333. 3-bit, etc. Bytes after that are the leaf values.
  334. For example, if the source is
  335. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
  336. 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
  337. then the code is
  338. 00 0x04
  339. 010 0x03
  340. 011 0x05
  341. 100 0x06
  342. 101 0x02
  343. 1100 0x07
  344. 1101 0x01
  345. 11100 0x08
  346. 11101 0x09
  347. 11110 0x00
  348. 111110 0x0a
  349. 1111110 0x0b
  350. 1111111 0xff
  351. */
  352. ushort * CLASS make_decoder_ref (const uchar **source)
  353. {
  354. int max, len, h, i, j;
  355. const uchar *count;
  356. ushort *huff;
  357. count = (*source += 16) - 17;
  358. for (max=16; max && !count[max]; max--);
  359. huff = (ushort *) calloc (1 + (1 << max), sizeof *huff);
  360. merror (huff, "make_decoder()");
  361. huff[0] = max;
  362. for (h=len=1; len <= max; len++)
  363. for (i=0; i < count[len]; i++, ++*source)
  364. for (j=0; j < 1 << (max-len); j++)
  365. if (h <= 1 << max)
  366. huff[h++] = len << 8 | **source;
  367. return huff;
  368. }
  369. ushort * CLASS make_decoder (const uchar *source)
  370. {
  371. return make_decoder_ref (&source);
  372. }
  373. void CLASS crw_init_tables (unsigned table, ushort *huff[2])
  374. {
  375. static const uchar first_tree[3][29] = {
  376. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
  377. 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
  378. { 0,2,2,3,1,1,1,1,2,0,0,0,0,0,0,0,
  379. 0x03,0x02,0x04,0x01,0x05,0x00,0x06,0x07,0x09,0x08,0x0a,0x0b,0xff },
  380. { 0,0,6,3,1,1,2,0,0,0,0,0,0,0,0,0,
  381. 0x06,0x05,0x07,0x04,0x08,0x03,0x09,0x02,0x00,0x0a,0x01,0x0b,0xff },
  382. };
  383. static const uchar second_tree[3][180] = {
  384. { 0,2,2,2,1,4,2,1,2,5,1,1,0,0,0,139,
  385. 0x03,0x04,0x02,0x05,0x01,0x06,0x07,0x08,
  386. 0x12,0x13,0x11,0x14,0x09,0x15,0x22,0x00,0x21,0x16,0x0a,0xf0,
  387. 0x23,0x17,0x24,0x31,0x32,0x18,0x19,0x33,0x25,0x41,0x34,0x42,
  388. 0x35,0x51,0x36,0x37,0x38,0x29,0x79,0x26,0x1a,0x39,0x56,0x57,
  389. 0x28,0x27,0x52,0x55,0x58,0x43,0x76,0x59,0x77,0x54,0x61,0xf9,
  390. 0x71,0x78,0x75,0x96,0x97,0x49,0xb7,0x53,0xd7,0x74,0xb6,0x98,
  391. 0x47,0x48,0x95,0x69,0x99,0x91,0xfa,0xb8,0x68,0xb5,0xb9,0xd6,
  392. 0xf7,0xd8,0x67,0x46,0x45,0x94,0x89,0xf8,0x81,0xd5,0xf6,0xb4,
  393. 0x88,0xb1,0x2a,0x44,0x72,0xd9,0x87,0x66,0xd4,0xf5,0x3a,0xa7,
  394. 0x73,0xa9,0xa8,0x86,0x62,0xc7,0x65,0xc8,0xc9,0xa1,0xf4,0xd1,
  395. 0xe9,0x5a,0x92,0x85,0xa6,0xe7,0x93,0xe8,0xc1,0xc6,0x7a,0x64,
  396. 0xe1,0x4a,0x6a,0xe6,0xb3,0xf1,0xd3,0xa5,0x8a,0xb2,0x9a,0xba,
  397. 0x84,0xa4,0x63,0xe5,0xc5,0xf3,0xd2,0xc4,0x82,0xaa,0xda,0xe4,
  398. 0xf2,0xca,0x83,0xa3,0xa2,0xc3,0xea,0xc2,0xe2,0xe3,0xff,0xff },
  399. { 0,2,2,1,4,1,4,1,3,3,1,0,0,0,0,140,
  400. 0x02,0x03,0x01,0x04,0x05,0x12,0x11,0x06,
  401. 0x13,0x07,0x08,0x14,0x22,0x09,0x21,0x00,0x23,0x15,0x31,0x32,
  402. 0x0a,0x16,0xf0,0x24,0x33,0x41,0x42,0x19,0x17,0x25,0x18,0x51,
  403. 0x34,0x43,0x52,0x29,0x35,0x61,0x39,0x71,0x62,0x36,0x53,0x26,
  404. 0x38,0x1a,0x37,0x81,0x27,0x91,0x79,0x55,0x45,0x28,0x72,0x59,
  405. 0xa1,0xb1,0x44,0x69,0x54,0x58,0xd1,0xfa,0x57,0xe1,0xf1,0xb9,
  406. 0x49,0x47,0x63,0x6a,0xf9,0x56,0x46,0xa8,0x2a,0x4a,0x78,0x99,
  407. 0x3a,0x75,0x74,0x86,0x65,0xc1,0x76,0xb6,0x96,0xd6,0x89,0x85,
  408. 0xc9,0xf5,0x95,0xb4,0xc7,0xf7,0x8a,0x97,0xb8,0x73,0xb7,0xd8,
  409. 0xd9,0x87,0xa7,0x7a,0x48,0x82,0x84,0xea,0xf4,0xa6,0xc5,0x5a,
  410. 0x94,0xa4,0xc6,0x92,0xc3,0x68,0xb5,0xc8,0xe4,0xe5,0xe6,0xe9,
  411. 0xa2,0xa3,0xe3,0xc2,0x66,0x67,0x93,0xaa,0xd4,0xd5,0xe7,0xf8,
  412. 0x88,0x9a,0xd7,0x77,0xc4,0x64,0xe2,0x98,0xa5,0xca,0xda,0xe8,
  413. 0xf3,0xf6,0xa9,0xb2,0xb3,0xf2,0xd2,0x83,0xba,0xd3,0xff,0xff },
  414. { 0,0,6,2,1,3,3,2,5,1,2,2,8,10,0,117,
  415. 0x04,0x05,0x03,0x06,0x02,0x07,0x01,0x08,
  416. 0x09,0x12,0x13,0x14,0x11,0x15,0x0a,0x16,0x17,0xf0,0x00,0x22,
  417. 0x21,0x18,0x23,0x19,0x24,0x32,0x31,0x25,0x33,0x38,0x37,0x34,
  418. 0x35,0x36,0x39,0x79,0x57,0x58,0x59,0x28,0x56,0x78,0x27,0x41,
  419. 0x29,0x77,0x26,0x42,0x76,0x99,0x1a,0x55,0x98,0x97,0xf9,0x48,
  420. 0x54,0x96,0x89,0x47,0xb7,0x49,0xfa,0x75,0x68,0xb6,0x67,0x69,
  421. 0xb9,0xb8,0xd8,0x52,0xd7,0x88,0xb5,0x74,0x51,0x46,0xd9,0xf8,
  422. 0x3a,0xd6,0x87,0x45,0x7a,0x95,0xd5,0xf6,0x86,0xb4,0xa9,0x94,
  423. 0x53,0x2a,0xa8,0x43,0xf5,0xf7,0xd4,0x66,0xa7,0x5a,0x44,0x8a,
  424. 0xc9,0xe8,0xc8,0xe7,0x9a,0x6a,0x73,0x4a,0x61,0xc7,0xf4,0xc6,
  425. 0x65,0xe9,0x72,0xe6,0x71,0x91,0x93,0xa6,0xda,0x92,0x85,0x62,
  426. 0xf3,0xc5,0xb2,0xa4,0x84,0xba,0x64,0xa5,0xb3,0xd2,0x81,0xe5,
  427. 0xd3,0xaa,0xc4,0xca,0xf2,0xb1,0xe4,0xd1,0x83,0x63,0xea,0xc3,
  428. 0xe2,0x82,0xf1,0xa3,0xc2,0xa1,0xc1,0xe3,0xa2,0xe1,0xff,0xff }
  429. };
  430. if (table > 2) table = 2;
  431. huff[0] = make_decoder ( first_tree[table]);
  432. huff[1] = make_decoder (second_tree[table]);
  433. }
  434. /*
  435. Return 0 if the image starts with compressed data,
  436. 1 if it starts with uncompressed low-order bits.
  437. In Canon compressed data, 0xff is always followed by 0x00.
  438. */
  439. int CLASS canon_has_lowbits()
  440. {
  441. uchar test[0x4000];
  442. int ret=1, i;
  443. fseek (ifp, 0, SEEK_SET);
  444. fread (test, 1, sizeof test, ifp);
  445. for (i=540; i < sizeof test - 1; i++)
  446. if (test[i] == 0xff) {
  447. if (test[i+1]) return 1;
  448. ret=0;
  449. }
  450. return ret;
  451. }
  452. void CLASS canon_load_raw()
  453. {
  454. ushort *pixel, *prow, *huff[2];
  455. int nblocks, lowbits, i, c, row, r, save, val;
  456. int block, diffbuf[64], leaf, len, diff, carry=0, pnum=0, base[2];
  457. crw_init_tables (tiff_compress, huff);
  458. lowbits = canon_has_lowbits();
  459. if (!lowbits) maximum = 0x3ff;
  460. fseek (ifp, 540 + lowbits*raw_height*raw_width/4, SEEK_SET);
  461. zero_after_ff = 1;
  462. getbits(-1);
  463. for (row=0; row < raw_height; row+=8) {
  464. pixel = raw_image + row*raw_width;
  465. nblocks = MIN (8, raw_height-row) * raw_width >> 6;
  466. for (block=0; block < nblocks; block++) {
  467. memset (diffbuf, 0, sizeof diffbuf);
  468. for (i=0; i < 64; i++ ) {
  469. leaf = gethuff(huff[i > 0]);
  470. if (leaf == 0 && i) break;
  471. if (leaf == 0xff) continue;
  472. i += leaf >> 4;
  473. len = leaf & 15;
  474. if (len == 0) continue;
  475. diff = getbits(len);
  476. if ((diff & (1 << (len-1))) == 0)
  477. diff -= (1 << len) - 1;
  478. if (i < 64) diffbuf[i] = diff;
  479. }
  480. diffbuf[0] += carry;
  481. carry = diffbuf[0];
  482. for (i=0; i < 64; i++ ) {
  483. if (pnum++ % raw_width == 0)
  484. base[0] = base[1] = 512;
  485. if ((pixel[(block << 6) + i] = base[i & 1] += diffbuf[i]) >> 10)
  486. derror();
  487. }
  488. }
  489. if (lowbits) {
  490. save = ftell(ifp);
  491. fseek (ifp, 26 + row*raw_width/4, SEEK_SET);
  492. for (prow=pixel, i=0; i < raw_width*2; i++) {
  493. c = fgetc(ifp);
  494. for (r=0; r < 8; r+=2, prow++) {
  495. val = (*prow << 2) + ((c >> r) & 3);
  496. if (raw_width == 2672 && val < 512) val += 2;
  497. *prow = val;
  498. }
  499. }
  500. fseek (ifp, save, SEEK_SET);
  501. }
  502. }
  503. FORC(2) free (huff[c]);
  504. }
  505. #line 819 "dcraw/dcraw.c"
  506. int CLASS ljpeg_start (struct jhead *jh, int info_only)
  507. {
  508. int c, tag, len;
  509. uchar data[0x10000];
  510. const uchar *dp;
  511. memset (jh, 0, sizeof *jh);
  512. jh->restart = INT_MAX;
  513. fread (data, 2, 1, ifp);
  514. if (data[1] != 0xd8) return 0;
  515. do {
  516. fread (data, 2, 2, ifp);
  517. tag = data[0] << 8 | data[1];
  518. len = (data[2] << 8 | data[3]) - 2;
  519. if (tag <= 0xff00) return 0;
  520. fread (data, 1, len, ifp);
  521. switch (tag) {
  522. case 0xffc3:
  523. jh->sraw = ((data[7] >> 4) * (data[7] & 15) - 1) & 3;
  524. case 0xffc0:
  525. jh->bits = data[0];
  526. jh->high = data[1] << 8 | data[2];
  527. jh->wide = data[3] << 8 | data[4];
  528. jh->clrs = data[5] + jh->sraw;
  529. if (len == 9 && !dng_version) getc(ifp);
  530. break;
  531. case 0xffc4:
  532. if (info_only) break;
  533. for (dp = data; dp < data+len && (c = *dp++) < 4; )
  534. jh->free[c] = jh->huff[c] = make_decoder_ref (&dp);
  535. break;
  536. case 0xffda:
  537. jh->psv = data[1+data[0]*2];
  538. jh->bits -= data[3+data[0]*2] & 15;
  539. break;
  540. case 0xffdd:
  541. jh->restart = data[0] << 8 | data[1];
  542. }
  543. } while (tag != 0xffda);
  544. if (info_only) return 1;
  545. FORC(5) if (!jh->huff[c+1]) jh->huff[c+1] = jh->huff[c];
  546. if (jh->sraw) {
  547. FORC(4) jh->huff[2+c] = jh->huff[1];
  548. FORC(jh->sraw) jh->huff[1+c] = jh->huff[0];
  549. }
  550. jh->row = (ushort *) calloc (jh->wide*jh->clrs, 4);
  551. merror (jh->row, "ljpeg_start()");
  552. return zero_after_ff = 1;
  553. }
  554. void CLASS ljpeg_end (struct jhead *jh)
  555. {
  556. int c;
  557. FORC4 if (jh->free[c]) free (jh->free[c]);
  558. free (jh->row);
  559. }
  560. int CLASS ljpeg_diff (ushort *huff)
  561. {
  562. int len, diff;
  563. len = gethuff(huff);
  564. if (len == 16 && (!dng_version || dng_version >= 0x1010000))
  565. return -32768;
  566. diff = getbits(len);
  567. if ((diff & (1 << (len-1))) == 0)
  568. diff -= (1 << len) - 1;
  569. return diff;
  570. }
  571. #ifdef LIBRAW_LIBRARY_BUILD
  572. int CLASS ljpeg_diff_new (LibRaw_bit_buffer& bits, LibRaw_byte_buffer* buf,ushort *huff)
  573. {
  574. int len, diff;
  575. len = bits._gethuff_lj(buf,*huff,huff+1);
  576. if (len == 16 && (!dng_version || dng_version >= 0x1010000))
  577. return -32768;
  578. diff = bits._getbits_lj(buf,len);
  579. if ((diff & (1 << (len-1))) == 0)
  580. diff -= (1 << len) - 1;
  581. return diff;
  582. }
  583. int CLASS ljpeg_diff_pef (LibRaw_bit_buffer& bits, LibRaw_byte_buffer* buf,ushort *huff)
  584. {
  585. int len, diff;
  586. len = bits._gethuff(buf,*huff,huff+1,zero_after_ff);
  587. if (len == 16 && (!dng_version || dng_version >= 0x1010000))
  588. return -32768;
  589. diff = bits._getbits(buf,len,zero_after_ff);
  590. if ((diff & (1 << (len-1))) == 0)
  591. diff -= (1 << len) - 1;
  592. return diff;
  593. }
  594. ushort * CLASS ljpeg_row_new (int jrow, struct jhead *jh, LibRaw_bit_buffer& bits,LibRaw_byte_buffer* bytes)
  595. {
  596. int col, c, diff, pred, spred=0;
  597. ushort mark=0, *row[3];
  598. if (jrow * jh->wide % jh->restart == 0) {
  599. FORC(6) jh->vpred[c] = 1 << (jh->bits-1);
  600. if (jrow) {
  601. bytes->unseek2();
  602. do mark = (mark << 8) + (c = bytes->get_byte());
  603. while (c != EOF && mark >> 4 != 0xffd);
  604. }
  605. bits.reset();
  606. }
  607. FORC3 row[c] = jh->row + jh->wide*jh->clrs*((jrow+c) & 1);
  608. for (col=0; col < jh->wide; col++)
  609. FORC(jh->clrs) {
  610. diff = ljpeg_diff_new (bits,bytes,jh->huff[c]);
  611. if (jh->sraw && c <= jh->sraw && (col | c))
  612. pred = spred;
  613. else if (col) pred = row[0][-jh->clrs];
  614. else pred = (jh->vpred[c] += diff) - diff;
  615. if (jrow && col) switch (jh->psv) {
  616. case 1: break;
  617. case 2: pred = row[1][0]; break;
  618. case 3: pred = row[1][-jh->clrs]; break;
  619. case 4: pred = pred + row[1][0] - row[1][-jh->clrs]; break;
  620. case 5: pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1); break;
  621. case 6: pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1); break;
  622. case 7: pred = (pred + row[1][0]) >> 1; break;
  623. default: pred = 0;
  624. }
  625. if ((**row = pred + diff) >> jh->bits) derror();
  626. if (c <= jh->sraw) spred = **row;
  627. row[0]++; row[1]++;
  628. }
  629. return row[2];
  630. }
  631. #endif
  632. ushort * CLASS ljpeg_row (int jrow, struct jhead *jh)
  633. {
  634. int col, c, diff, pred, spred=0;
  635. ushort mark=0, *row[3];
  636. if (jrow * jh->wide % jh->restart == 0) {
  637. FORC(6) jh->vpred[c] = 1 << (jh->bits-1);
  638. if (jrow) {
  639. fseek (ifp, -2, SEEK_CUR);
  640. do mark = (mark << 8) + (c = fgetc(ifp));
  641. while (c != EOF && mark >> 4 != 0xffd);
  642. }
  643. getbits(-1);
  644. }
  645. FORC3 row[c] = jh->row + jh->wide*jh->clrs*((jrow+c) & 1);
  646. for (col=0; col < jh->wide; col++)
  647. FORC(jh->clrs) {
  648. diff = ljpeg_diff (jh->huff[c]);
  649. if (jh->sraw && c <= jh->sraw && (col | c))
  650. pred = spred;
  651. else if (col) pred = row[0][-jh->clrs];
  652. else pred = (jh->vpred[c] += diff) - diff;
  653. if (jrow && col) switch (jh->psv) {
  654. case 1: break;
  655. case 2: pred = row[1][0]; break;
  656. case 3: pred = row[1][-jh->clrs]; break;
  657. case 4: pred = pred + row[1][0] - row[1][-jh->clrs]; break;
  658. case 5: pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1); break;
  659. case 6: pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1); break;
  660. case 7: pred = (pred + row[1][0]) >> 1; break;
  661. default: pred = 0;
  662. }
  663. if ((**row = pred + diff) >> jh->bits) derror();
  664. if (c <= jh->sraw) spred = **row;
  665. row[0]++; row[1]++;
  666. }
  667. return row[2];
  668. }
  669. void CLASS lossless_jpeg_load_raw()
  670. {
  671. int jwide, jrow, jcol, val, i, row=0, col=0;
  672. #ifndef LIBRAW_LIBRARY_BUILD
  673. int jidx,j;
  674. #endif
  675. struct jhead jh;
  676. ushort *rp;
  677. #ifdef LIBRAW_LIBRARY_BUILD
  678. int save_min = 0;
  679. unsigned slicesW[16],slicesWcnt=0,slices;
  680. unsigned *offset;
  681. unsigned t_y=0,t_x=0,t_s=0,slice=0,pixelsInSlice,pixno;
  682. if (!strcasecmp(make,"KODAK"))
  683. save_min = 1;
  684. #endif
  685. #ifdef LIBRAW_LIBRARY_BUILD
  686. if (cr2_slice[0]>15)
  687. throw LIBRAW_EXCEPTION_IO_EOF; // change many slices
  688. #else
  689. if (cr2_slice[0]>15)
  690. {
  691. fprintf(stderr,"Too many CR2 slices: %d\n",cr2_slice[0]+1);
  692. return;
  693. }
  694. #endif
  695. if (!ljpeg_start (&jh, 0)) return;
  696. jwide = jh.wide * jh.clrs;
  697. #ifdef LIBRAW_LIBRARY_BUILD
  698. if(cr2_slice[0])
  699. {
  700. for(i=0;i<cr2_slice[0];i++)
  701. slicesW[slicesWcnt++] = cr2_slice[1];
  702. slicesW[slicesWcnt++] = cr2_slice[2];
  703. }
  704. else
  705. {
  706. // not sliced
  707. slicesW[slicesWcnt++] = raw_width; // safe fallback
  708. }
  709. slices = slicesWcnt * jh.high;
  710. offset = (unsigned*)calloc(slices+1,sizeof(offset[0]));
  711. for(slice=0;slice<slices;slice++)
  712. {
  713. offset[slice] = (t_x + t_y * raw_width)| (t_s<<28);
  714. if((offset[slice] & 0x0fffffff) >= raw_width * raw_height)
  715. throw LIBRAW_EXCEPTION_IO_BADFILE;
  716. t_y++;
  717. if(t_y == jh.high)
  718. {
  719. t_y = 0;
  720. t_x += slicesW[t_s++];
  721. }
  722. }
  723. offset[slices] = offset[slices-1];
  724. slice = 1; // next slice
  725. pixno = offset[0];
  726. pixelsInSlice = slicesW[0];
  727. #endif
  728. #ifdef LIBRAW_LIBRARY_BUILD
  729. LibRaw_byte_buffer *buf=NULL;
  730. if(data_size)
  731. buf = ifp->make_byte_buffer(data_size);
  732. LibRaw_bit_buffer bits;
  733. #endif
  734. for (jrow=0; jrow < jh.high; jrow++) {
  735. #ifdef LIBRAW_LIBRARY_BUILD
  736. if (buf)
  737. rp = ljpeg_row_new (jrow, &jh,bits,buf);
  738. else
  739. #endif
  740. rp = ljpeg_row (jrow, &jh);
  741. if (load_flags & 1)
  742. row = jrow & 1 ? height-1-jrow/2 : jrow/2;
  743. for (jcol=0; jcol < jwide; jcol++) {
  744. val = curve[*rp++];
  745. #ifndef LIBRAW_LIBRARY_BUILD
  746. // slow dcraw way to calculate row/col
  747. if (cr2_slice[0]) {
  748. jidx = jrow*jwide + jcol;
  749. i = jidx / (cr2_slice[1]*jh.high);
  750. if ((j = i >= cr2_slice[0]))
  751. i = cr2_slice[0];
  752. jidx -= i * (cr2_slice[1]*jh.high);
  753. row = jidx / cr2_slice[1+j];
  754. col = jidx % cr2_slice[1+j] + i*cr2_slice[1];
  755. }
  756. #else
  757. // new fast one, but for data_size defined only (i.e. new CR2 format, not 1D/1Ds)
  758. if(buf)
  759. {
  760. if(!(load_flags & 1))
  761. row = pixno/raw_width;
  762. col = pixno % raw_width;
  763. pixno++;
  764. if (0 == --pixelsInSlice)
  765. {
  766. unsigned o = offset[slice++];
  767. pixno = o & 0x0fffffff;
  768. pixelsInSlice = slicesW[o>>28];
  769. }
  770. }
  771. #endif
  772. if (raw_width == 3984 && (col -= 2) < 0)
  773. col += (row--,raw_width);
  774. if (row >= 0) RAW(row,col) = val;
  775. #ifndef LIBRAW_LIBRARY_BUILD
  776. if (++col >= raw_width)
  777. col = (row++,0);
  778. #else
  779. if(!buf) // 1D or 1Ds case
  780. if (++col >= raw_width)
  781. col = (row++,0);
  782. #endif
  783. }
  784. }
  785. ljpeg_end (&jh);
  786. #ifdef LIBRAW_LIBRARY_BUILD
  787. if(buf)
  788. delete buf;
  789. free(offset);
  790. #endif
  791. }
  792. void CLASS canon_sraw_load_raw()
  793. {
  794. struct jhead jh;
  795. short *rp=0, (*ip)[4];
  796. int jwide, slice, scol, ecol, row, col, jrow=0, jcol=0, pix[3], c;
  797. int v[3]={0,0,0}, ver, hue;
  798. char *cp;
  799. if (!ljpeg_start (&jh, 0)) return;
  800. jwide = (jh.wide >>= 1) * jh.clrs;
  801. #ifdef LIBRAW_LIBRARY_BUILD
  802. if(!data_size)
  803. throw LIBRAW_EXCEPTION_IO_BADFILE;
  804. LibRaw_byte_buffer *buf = ifp->make_byte_buffer(data_size);
  805. LibRaw_bit_buffer bits;
  806. #endif
  807. for (ecol=slice=0; slice <= cr2_slice[0]; slice++) {
  808. scol = ecol;
  809. ecol += cr2_slice[1] * 2 / jh.clrs;
  810. if (!cr2_slice[0] || ecol > raw_width-1) ecol = raw_width & -2;
  811. for (row=0; row < height; row += (jh.clrs >> 1) - 1) {
  812. ip = (short (*)[4]) image + row*width;
  813. for (col=scol; col < ecol; col+=2, jcol+=jh.clrs) {
  814. if ((jcol %= jwide) == 0)
  815. #ifdef LIBRAW_LIBRARY_BUILD
  816. rp = (short*) ljpeg_row_new (jrow++, &jh,bits,buf);
  817. #else
  818. rp = (short *) ljpeg_row (jrow++, &jh);
  819. #endif
  820. if (col >= width) continue;
  821. FORC (jh.clrs-2)
  822. ip[col + (c >> 1)*width + (c & 1)][0] = rp[jcol+c];
  823. ip[col][1] = rp[jcol+jh.clrs-2] - 16384;
  824. ip[col][2] = rp[jcol+jh.clrs-1] - 16384;
  825. }
  826. }
  827. }
  828. for (cp=model2; *cp && !isdigit(*cp); cp++);
  829. sscanf (cp, "%d.%d.%d", v, v+1, v+2);
  830. ver = (v[0]*1000 + v[1])*1000 + v[2];
  831. hue = (jh.sraw+1) << 2;
  832. if (unique_id >= 0x80000281 || (unique_id == 0x80000218 && ver > 1000006))
  833. hue = jh.sraw << 1;
  834. ip = (short (*)[4]) image;
  835. rp = ip[0];
  836. for (row=0; row < height; row++, ip+=width) {
  837. if (row & (jh.sraw >> 1))
  838. for (col=0; col < width; col+=2)
  839. for (c=1; c < 3; c++)
  840. if (row == height-1)
  841. ip[col][c] = ip[col-width][c];
  842. else ip[col][c] = (ip[col-width][c] + ip[col+width][c] + 1) >> 1;
  843. for (col=1; col < width; col+=2)
  844. for (c=1; c < 3; c++)
  845. if (col == width-1)
  846. ip[col][c] = ip[col-1][c];
  847. else ip[col][c] = (ip[col-1][c] + ip[col+1][c] + 1) >> 1;
  848. }
  849. for ( ; rp < ip[0]; rp+=4) {
  850. if (unique_id == 0x80000218 ||
  851. unique_id == 0x80000250 ||
  852. unique_id == 0x80000261 ||
  853. unique_id == 0x80000281 ||
  854. unique_id == 0x80000287) {
  855. rp[1] = (rp[1] << 2) + hue;
  856. rp[2] = (rp[2] << 2) + hue;
  857. pix[0] = rp[0] + (( 50*rp[1] + 22929*rp[2]) >> 14);
  858. pix[1] = rp[0] + ((-5640*rp[1] - 11751*rp[2]) >> 14);
  859. pix[2] = rp[0] + ((29040*rp[1] - 101*rp[2]) >> 14);
  860. } else {
  861. if (unique_id < 0x80000218) rp[0] -= 512;
  862. pix[0] = rp[0] + rp[2];
  863. pix[2] = rp[0] + rp[1];
  864. pix[1] = rp[0] + ((-778*rp[1] - (rp[2] << 11)) >> 12);
  865. }
  866. FORC3 rp[c] = CLIP(pix[c] * sraw_mul[c] >> 10);
  867. }
  868. #ifdef LIBRAW_LIBRARY_BUILD
  869. delete buf;
  870. #endif
  871. ljpeg_end (&jh);
  872. maximum = 0x3fff;
  873. }
  874. #ifndef LIBRAW_LIBRARY_BUILD
  875. void CLASS adobe_copy_pixel (unsigned row, unsigned col, ushort **rp)
  876. {
  877. int c;
  878. if (is_raw == 2 && shot_select) (*rp)++;
  879. if (raw_image) {
  880. if (row < raw_height && col < raw_width)
  881. RAW(row,col) = curve[**rp];
  882. *rp += is_raw;
  883. } else {
  884. if (row < height && col < width)
  885. FORC(tiff_samples)
  886. image[row*width+col][c] = curve[(*rp)[c]];
  887. *rp += tiff_samples;
  888. }
  889. if (is_raw == 2 && shot_select) (*rp)--;
  890. }
  891. #else
  892. void CLASS adobe_copy_pixel_raw (unsigned row, unsigned col, ushort **rp)
  893. {
  894. if (is_raw == 2 && shot_select) (*rp)++;
  895. if (row < raw_height && col < raw_width)
  896. RAW(row,col) = curve[**rp];
  897. *rp += is_raw;
  898. if (is_raw == 2 && shot_select) (*rp)--;
  899. }
  900. void CLASS adobe_copy_pixel_color (unsigned row, unsigned col, ushort **rp)
  901. {
  902. int c;
  903. if (is_raw == 2 && shot_select) (*rp)++;
  904. if (row < height && col < width)
  905. FORC(tiff_samples)
  906. image[row*width+col][c] = curve[(*rp)[c]];
  907. *rp += tiff_samples;
  908. if (is_raw == 2 && shot_select) (*rp)--;
  909. }
  910. #endif
  911. void CLASS lossless_dng_load_raw()
  912. {
  913. unsigned save, trow=0, tcol=0, jwide, jrow, jcol, row, col;
  914. struct jhead jh;
  915. ushort *rp;
  916. while (trow < raw_height) {
  917. save = ftell(ifp);
  918. if (tile_length < INT_MAX)
  919. fseek (ifp, get4(), SEEK_SET);
  920. if (!ljpeg_start (&jh, 0)) break;
  921. jwide = jh.wide;
  922. if (filters) jwide *= jh.clrs;
  923. jwide /= is_raw;
  924. #ifdef LIBRAW_LIBRARY_BUILD
  925. if(!data_size)
  926. throw LIBRAW_EXCEPTION_IO_BADFILE;
  927. LibRaw_byte_buffer *buf = ifp->make_byte_buffer(data_size);
  928. LibRaw_bit_buffer bits;
  929. #endif
  930. #ifndef LIBRAW_LIBRARY_BUILD
  931. for (row=col=jrow=0; jrow < jh.high; jrow++) {
  932. rp = ljpeg_row (jrow, &jh);
  933. for (jcol=0; jcol < jwide; jcol++) {
  934. adobe_copy_pixel (trow+row, tcol+col, &rp);
  935. if (++col >= tile_width || col >= raw_width)
  936. row += 1 + (col = 0);
  937. }
  938. }
  939. #else
  940. if(raw_image)
  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_raw (trow+row, tcol+col, &rp);
  950. if (++col >= tile_width || col >= raw_width)
  951. row += 1 + (col = 0);
  952. }
  953. }
  954. }
  955. else
  956. {
  957. for (row=col=jrow=0; jrow < jh.high; jrow++) {
  958. #ifdef LIBRAW_LIBRARY_BUILD
  959. rp = ljpeg_row_new (jrow, &jh,bits,buf);
  960. #else
  961. rp = ljpeg_row (jrow, &jh);
  962. #endif
  963. for (jcol=0; jcol < jwide; jcol++) {
  964. adobe_copy_pixel_color (trow+row, tcol+col, &rp);
  965. if (++col >= tile_width || col >= raw_width)
  966. row += 1 + (col = 0);
  967. }
  968. }
  969. }
  970. #endif
  971. fseek (ifp, save+4, SEEK_SET);
  972. if ((tcol += tile_width) >= raw_width)
  973. trow += tile_length + (tcol = 0);
  974. ljpeg_end (&jh);
  975. #ifdef LIBRAW_LIBRARY_BUILD
  976. delete buf;
  977. #endif
  978. }
  979. }
  980. void CLASS packed_dng_load_raw()
  981. {
  982. ushort *pixel, *rp;
  983. int row, col;
  984. pixel = (ushort *) calloc (raw_width * tiff_samples, sizeof *pixel);
  985. merror (pixel, "packed_dng_load_raw()");
  986. #ifdef LIBRAW_LIBRARY_BUILD
  987. int dsz= raw_height*raw_width * tiff_samples * tiff_bps/8;
  988. LibRaw_byte_buffer *buf = NULL;
  989. if (tiff_bps != 16)
  990. {
  991. buf = ifp->make_byte_buffer(dsz);
  992. }
  993. LibRaw_bit_buffer bits;
  994. #endif
  995. for (row=0; row < raw_height; row++) {
  996. if (tiff_bps == 16)
  997. read_shorts (pixel, raw_width * tiff_samples);
  998. else {
  999. #ifdef LIBRAW_LIBRARY_BUILD
  1000. bits.reset();
  1001. for (col=0; col < raw_width * tiff_samples; col++)
  1002. pixel[col] = bits._getbits(buf,tiff_bps,zero_after_ff);
  1003. #else
  1004. getbits(-1);
  1005. for (col=0; col < raw_width * tiff_samples; col++)
  1006. pixel[col] = getbits(tiff_bps);
  1007. #endif
  1008. }
  1009. #ifndef LIBRAW_LIBRARY_BUILD
  1010. for (rp=pixel, col=0; col < raw_width; col++)
  1011. adobe_copy_pixel (row, col, &rp);
  1012. #else
  1013. if(raw_image)
  1014. for (rp=pixel, col=0; col < raw_width; col++)
  1015. adobe_copy_pixel_raw (row, col, &rp);
  1016. else
  1017. for (rp=pixel, col=0; col < raw_width; col++)
  1018. adobe_copy_pixel_color (row, col, &rp);
  1019. #endif
  1020. }
  1021. free (pixel);
  1022. #ifdef LIBRAW_LIBRARY_BUILD
  1023. if(buf)
  1024. delete buf;
  1025. #endif
  1026. }
  1027. void CLASS pentax_load_raw()
  1028. {
  1029. ushort bit[2][15], huff[4097];
  1030. int dep, row, col, diff, c, i;
  1031. ushort vpred[2][2] = {{0,0},{0,0}}, hpred[2];
  1032. fseek (ifp, meta_offset, SEEK_SET);
  1033. dep = (get2() + 12) & 15;
  1034. fseek (ifp, 12, SEEK_CUR);
  1035. FORC(dep) bit[0][c] = get2();
  1036. FORC(dep) bit[1][c] = fgetc(ifp);
  1037. FORC(dep)
  1038. for (i=bit[0][c]; i <= ((bit[0][c]+(4096 >> bit[1][c])-1) & 4095); )
  1039. huff[++i] = bit[1][c] << 8 | c;
  1040. huff[0] = 12;
  1041. fseek (ifp, data_offset, SEEK_SET);
  1042. #ifdef LIBRAW_LIBRARY_BUILD
  1043. if(!data_size)
  1044. throw LIBRAW_EXCEPTION_IO_BADFILE;
  1045. LibRaw_byte_buffer *buf = ifp->make_byte_buffer(data_size);
  1046. LibRaw_bit_buffer bits;
  1047. bits.reset();
  1048. #else
  1049. getbits(-1);
  1050. #endif
  1051. for (row=0; row < raw_height; row++)
  1052. for (col=0; col < raw_width; col++) {
  1053. #ifdef LIBRAW_LIBRARY_BUILD
  1054. diff = ljpeg_diff_pef(bits,buf,huff);
  1055. #else
  1056. diff = ljpeg_diff (huff);
  1057. #endif
  1058. if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
  1059. else hpred[col & 1] += diff;
  1060. RAW(row,col) = hpred[col & 1];
  1061. if (hpred[col & 1] >> tiff_bps) derror();
  1062. }
  1063. #ifdef LIBRAW_LIBRARY_BUILD
  1064. delete buf;
  1065. #endif
  1066. }
  1067. void CLASS nikon_load_raw()
  1068. {
  1069. static const uchar nikon_tree[][32] = {
  1070. { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy */
  1071. 5,4,3,6,2,7,1,0,8,9,11,10,12 },
  1072. { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy after split */
  1073. 0x39,0x5a,0x38,0x27,0x16,5,4,3,2,1,0,11,12,12 },
  1074. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0, /* 12-bit lossless */
  1075. 5,4,6,3,7,2,8,1,9,0,10,11,12 },
  1076. { 0,1,4,3,1,1,1,1,1,2,0,0,0,0,0,0, /* 14-bit lossy */
  1077. 5,6,4,7,8,3,9,2,1,0,10,11,12,13,14 },
  1078. { 0,1,5,1,1,1,1,1,1,1,2,0,0,0,0,0, /* 14-bit lossy after split */
  1079. 8,0x5c,0x4b,0x3a,0x29,7,6,5,4,3,2,1,0,13,14 },
  1080. { 0,1,4,2,2,3,1,2,0,0,0,0,0,0,0,0, /* 14-bit lossless */
  1081. 7,6,8,5,9,4,10,3,11,12,2,0,1,13,14 } };
  1082. ushort *huff, ver0, ver1, vpred[2][2], hpred[2], csize;
  1083. int i, min, max, step=0, tree=0, split=0, row, col, len, shl, diff;
  1084. fseek (ifp, meta_offset, SEEK_SET);
  1085. ver0 = fgetc(ifp);
  1086. ver1 = fgetc(ifp);
  1087. if (ver0 == 0x49 || ver1 == 0x58)
  1088. fseek (ifp, 2110, SEEK_CUR);
  1089. if (ver0 == 0x46) tree = 2;
  1090. if (tiff_bps == 14) tree += 3;
  1091. read_shorts (vpred[0], 4);
  1092. max = 1 << tiff_bps & 0x7fff;
  1093. if ((csize = get2()) > 1)
  1094. step = max / (csize-1);
  1095. if (ver0 == 0x44 && ver1 == 0x20 && step > 0) {
  1096. for (i=0; i < csize; i++)
  1097. curve[i*step] = get2();
  1098. for (i=0; i < max; i++)
  1099. curve[i] = ( curve[i-i%step]*(step-i%step) +
  1100. curve[i-i%step+step]*(i%step) ) / step;
  1101. fseek (ifp, meta_offset+562, SEEK_SET);
  1102. split = get2();
  1103. } else if (ver0 != 0x46 && csize <= 0x4001)
  1104. read_shorts (curve, max=csize);
  1105. while (curve[max-2] == curve[max-1]) max--;
  1106. huff = make_decoder (nikon_tree[tree]);
  1107. fseek (ifp, data_offset, SEEK_SET);
  1108. #ifdef LIBRAW_LIBRARY_BUILD
  1109. if(!data_size)
  1110. throw LIBRAW_EXCEPTION_IO_BADFILE;
  1111. LibRaw_byte_buffer *buf = ifp->make_byte_buffer(data_size);
  1112. LibRaw_bit_buffer bits;
  1113. bits.reset();
  1114. #else
  1115. getbits(-1);
  1116. #endif
  1117. for (min=row=0; row < height; row++) {
  1118. if (split && row == split) {
  1119. free (huff);
  1120. huff = make_decoder (nikon_tree[tree+1]);
  1121. max += (min = 16) << 1;
  1122. }
  1123. for (col=0; col < raw_width; col++) {
  1124. #ifdef LIBRAW_LIBRARY_BUILD
  1125. i = bits._gethuff(buf,*huff,huff+1,zero_after_ff);
  1126. #else
  1127. i = gethuff(huff);
  1128. #endif
  1129. len = i & 15;
  1130. shl = i >> 4;
  1131. #ifdef LIBRAW_LIBRARY_BUILD
  1132. diff = ((bits._getbits(buf,len-shl,zero_after_ff) << 1) + 1) << shl >> 1;
  1133. #else
  1134. diff = ((getbits(len-shl) << 1) + 1) << shl >> 1;
  1135. #endif
  1136. if ((diff & (1 << (len-1))) == 0)
  1137. diff -= (1 << len) - !shl;
  1138. if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
  1139. else hpred[col & 1] += diff;
  1140. if ((ushort)(hpred[col & 1] + min) >= max) derror();
  1141. RAW(row,col) = curve[LIM((short)hpred[col & 1],0,0x3fff)];
  1142. }
  1143. }
  1144. #ifdef LIBRAW_LIBRARY_BUILD
  1145. delete buf;
  1146. #endif
  1147. free (huff);
  1148. }
  1149. /*
  1150. Returns 1 for a Coolpix 995, 0 for anything else.
  1151. */
  1152. int CLASS nikon_e995()
  1153. {
  1154. int i, histo[256];
  1155. const uchar often[] = { 0x00, 0x55, 0xaa, 0xff };
  1156. memset (histo, 0, sizeof histo);
  1157. fseek (ifp, -2000, SEEK_END);
  1158. for (i=0; i < 2000; i++)
  1159. histo[fgetc(ifp)]++;
  1160. for (i=0; i < 4; i++)
  1161. if (histo[often[i]] < 200)
  1162. return 0;
  1163. return 1;
  1164. }
  1165. /*
  1166. Returns 1 for a Coolpix 2100, 0 for anything else.
  1167. */
  1168. int CLASS nikon_e2100()
  1169. {
  1170. uchar t[12];
  1171. int i;
  1172. fseek (ifp, 0, SEEK_SET);
  1173. for (i=0; i < 1024; i++) {
  1174. fread (t, 1, 12, ifp);
  1175. if (((t[2] & t[4] & t[7] & t[9]) >> 4
  1176. & t[1] & t[6] & t[8] & t[11] & 3) != 3)
  1177. return 0;
  1178. }
  1179. return 1;
  1180. }
  1181. void CLASS nikon_3700()
  1182. {
  1183. int bits, i;
  1184. uchar dp[24];
  1185. static const struct {
  1186. int bits;
  1187. char t_make[12], t_model[15];
  1188. } table[] = {
  1189. { 0x00, "PENTAX", "Optio 33WR" },
  1190. { 0x03, "NIKON", "E3200" },
  1191. { 0x32, "NIKON", "E3700" },
  1192. { 0x33, "OLYMPUS", "C740UZ" } };
  1193. fseek (ifp, 3072, SEEK_SET);
  1194. fread (dp, 1, 24, ifp);
  1195. bits = (dp[8] & 3) << 4 | (dp[20] & 3);
  1196. for (i=0; i < sizeof table / sizeof *table; i++)
  1197. if (bits == table[i].bits) {
  1198. strcpy (make, table[i].t_make );
  1199. strcpy (model, table[i].t_model);
  1200. }
  1201. }
  1202. /*
  1203. Separates a Minolta DiMAGE Z2 from a Nikon E4300.
  1204. */
  1205. int CLASS minolta_z2()
  1206. {
  1207. int i, nz;
  1208. char tail[424];
  1209. fseek (ifp, -sizeof tail, SEEK_END);
  1210. fread (tail, 1, sizeof tail, ifp);
  1211. for (nz=i=0; i < sizeof tail; i++)
  1212. if (tail[i]) nz++;
  1213. return nz > 20;
  1214. }
  1215. #line 1585 "dcraw/dcraw.c"
  1216. void CLASS ppm_thumb()
  1217. {
  1218. char *thumb;
  1219. thumb_length = thumb_width*thumb_height*3;
  1220. thumb = (char *) malloc (thumb_length);
  1221. merror (thumb, "ppm_thumb()");
  1222. fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
  1223. fread (thumb, 1, thumb_length, ifp);
  1224. fwrite (thumb, 1, thumb_length, ofp);
  1225. free (thumb);
  1226. }
  1227. void CLASS ppm16_thumb()
  1228. {
  1229. int i;
  1230. char *thumb;
  1231. thumb_length = thumb_width*thumb_height*3;
  1232. thumb = (char *) calloc (thumb_length,2);
  1233. merror (thumb, "ppm16_thumb()");
  1234. read_shorts ((ushort *) thumb, thumb_length);
  1235. for (i=0; i < thumb_length; i++)
  1236. thumb[i] = ((ushort *) thumb)[i] >> 8;
  1237. fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
  1238. fwrite (thumb, 1, thumb_length, ofp);
  1239. free (thumb);
  1240. }
  1241. void CLASS layer_thumb()
  1242. {
  1243. int i, c;
  1244. char *thumb, map[][4] = { "012","102" };
  1245. colors = thumb_misc >> 5 & 7;
  1246. thumb_length = thumb_width*thumb_height;
  1247. thumb = (char *) calloc (colors, thumb_length);
  1248. merror (thumb, "layer_thumb()");
  1249. fprintf (ofp, "P%d\n%d %d\n255\n",
  1250. 5 + (colors >> 1), thumb_width, thumb_height);
  1251. fread (thumb, thumb_length, colors, ifp);
  1252. for (i=0; i < thumb_length; i++)
  1253. FORCC putc (thumb[i+thumb_length*(map[thumb_misc >> 8][c]-'0')], ofp);
  1254. free (thumb);
  1255. }
  1256. void CLASS rollei_thumb()
  1257. {
  1258. unsigned i;
  1259. ushort *thumb;
  1260. thumb_length = thumb_width * thumb_height;
  1261. thumb = (ushort *) calloc (thumb_length, 2);
  1262. merror (thumb, "rollei_thumb()");
  1263. fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
  1264. read_shorts (thumb, thumb_length);
  1265. for (i=0; i < thumb_length; i++) {
  1266. putc (thumb[i] << 3, ofp);
  1267. putc (thumb[i] >> 5 << 2, ofp);
  1268. putc (thumb[i] >> 11 << 3, ofp);
  1269. }
  1270. free (thumb);
  1271. }
  1272. void CLASS rollei_load_raw()
  1273. {
  1274. uchar pixel[10];
  1275. unsigned iten=0, isix, i, buffer=0, todo[16];
  1276. isix = raw_width * raw_height * 5 / 8;
  1277. while (fread (pixel, 1, 10, ifp) == 10) {
  1278. for (i=0; i < 10; i+=2) {
  1279. todo[i] = iten++;
  1280. todo[i+1] = pixel[i] << 8 | pixel[i+1];
  1281. buffer = pixel[i] >> 2 | buffer << 6;
  1282. }
  1283. for ( ; i < 16; i+=2) {
  1284. todo[i] = isix++;
  1285. todo[i+1] = buffer >> (14-i)*5;
  1286. }
  1287. for (i=0; i < 16; i+=2)
  1288. raw_image[todo[i]] = (todo[i+1] & 0x3ff);
  1289. }
  1290. maximum = 0x3ff;
  1291. }
  1292. int CLASS raw (unsigned row, unsigned col)
  1293. {
  1294. return (row < raw_height && col < raw_width) ? RAW(row,col) : 0;
  1295. }
  1296. void CLASS phase_one_flat_field (int is_float, int nc)
  1297. {
  1298. ushort head[8];
  1299. unsigned wide, y, x, c, rend, cend, row, col;
  1300. float *mrow, num, mult[4];
  1301. read_shorts (head, 8);
  1302. wide = head[2] / head[4];
  1303. mrow = (float *) calloc (nc*wide, sizeof *mrow);
  1304. merror (mrow, "phase_one_flat_field()");
  1305. for (y=0; y < head[3] / head[5]; y++) {
  1306. for (x=0; x < wide; x++)
  1307. for (c=0; c < nc; c+=2) {
  1308. num = is_float ? getreal(11) : get2()/32768.0;
  1309. if (y==0) mrow[c*wide+x] = num;
  1310. else mrow[(c+1)*wide+x] = (num - mrow[c*wide+x]) / head[5];
  1311. }
  1312. if (y==0) continue;
  1313. rend = head[1] + y*head[5];
  1314. for (row = rend-head[5]; row < raw_height && row < rend; row++) {
  1315. for (x=1; x < wide; x++) {
  1316. for (c=0; c < nc; c+=2) {
  1317. mult[c] = mrow[c*wide+x-1];
  1318. mult[c+1] = (mrow[c*wide+x] - mult[c]) / head[4];
  1319. }
  1320. cend = head[0] + x*head[4];
  1321. for (col = cend-head[4]; col < raw_width && col < cend; col++) {
  1322. c = nc > 2 ? FC(row-top_margin,col-left_margin) : 0;
  1323. if (!(c & 1)) {
  1324. c = RAW(row,col) * mult[c];
  1325. RAW(row,col) = LIM(c,0,65535);
  1326. }
  1327. for (c=0; c < nc; c+=2)
  1328. mult[c] += mult[c+1];
  1329. }
  1330. }
  1331. for (x=0; x < wide; x++)
  1332. for (c=0; c < nc; c+=2)
  1333. mrow[c*wide+x] += mrow[(c+1)*wide+x];
  1334. }
  1335. }
  1336. free (mrow);
  1337. }
  1338. void CLASS phase_one_correct()
  1339. {
  1340. unsigned entries, tag, data, save, col, row, type;
  1341. int len, i, j, k, cip, val[4], dev[4], sum, max;
  1342. int head[9], diff, mindiff=INT_MAX, off_412=0;
  1343. static const signed char dir[12][2] =
  1344. { {-1,-1}, {-1,1}, {1,-1}, {1,1}, {-2,0}, {0,-2}, {0,2}, {2,0},
  1345. {-2,-2}, {-2,2}, {2,-2}, {2,2} };
  1346. float poly[8], num, cfrac, frac, mult[2], *yval[2];
  1347. ushort *xval[2];
  1348. if (half_size || !meta_length) return;
  1349. #ifdef DCRAW_VERBOSE
  1350. if (verbose) fprintf (stderr,_("Phase One correction...\n"));
  1351. #endif
  1352. fseek (ifp, meta_offset, SEEK_SET);
  1353. order = get2();
  1354. fseek (ifp, 6, SEEK_CUR);
  1355. fseek (ifp, meta_offset+get4(), SEEK_SET);
  1356. entries = get4(); get4();
  1357. while (entries--) {
  1358. tag = get4();
  1359. len = get4();
  1360. data = get4();
  1361. save = ftell(ifp);
  1362. fseek (ifp, meta_offset+data, SEEK_SET);
  1363. if (tag == 0x419) { /* Polynomial curve */
  1364. for (get4(), i=0; i < 8; i++)
  1365. poly[i] = getreal(11);
  1366. poly[3] += (ph1.tag_210 - poly[7]) * poly[6] + 1;
  1367. for (i=0; i < 0x10000; i++) {
  1368. num = (poly[5]*i + poly[3])*i + poly[1];
  1369. curve[i] = LIM(num,0,65535);
  1370. } goto apply; /* apply to right half */
  1371. } else if (tag == 0x41a) { /* Polynomial curve */
  1372. for (i=0; i < 4; i++)
  1373. poly[i] = getreal(11);
  1374. for (i=0; i < 0x10000; i++) {
  1375. for (num=0, j=4; j--; )
  1376. num = num * i + poly[j];
  1377. curve[i] = LIM(num+i,0,65535);
  1378. } apply: /* apply to whole image */
  1379. for (row=0; row < raw_height; row++)
  1380. for (col = (tag & 1)*ph1.split_col; col < raw_width; col++)
  1381. RAW(row,col) = curve[RAW(row,col)];
  1382. } else if (tag == 0x400) { /* Sensor defects */
  1383. while ((len -= 8) >= 0) {
  1384. col = get2();
  1385. row = get2();
  1386. type = get2(); get2();
  1387. if (col >= raw_width) continue;
  1388. if (type == 131) /* Bad column */
  1389. for (row=0; row < raw_height; row++)
  1390. if (FC(row-top_margin,col-left_margin) == 1) {
  1391. for (sum=i=0; i < 4; i++)
  1392. sum += val[i] = raw (row+dir[i][0], col+dir[i][1]);
  1393. for (max=i=0; i < 4; i++) {
  1394. dev[i] = abs((val[i] << 2) - sum);
  1395. if (dev[max] < dev[i]) max = i;
  1396. }
  1397. RAW(row,col) = (sum - val[max])/3.0 + 0.5;
  1398. } else {
  1399. for (sum=0, i=8; i < 12; i++)
  1400. sum += raw (row+dir[i][0], col+dir[i][1]);
  1401. RAW(row,col) = 0.5 + sum * 0.0732233 +
  1402. (raw(row,col-2) + raw(row,col+2)) * 0.3535534;
  1403. }
  1404. else if (type == 129) { /* Bad pixel */
  1405. if (row >= raw_height) continue;
  1406. j = (FC(row-top_margin,col-left_margin) != 1) * 4;
  1407. for (sum=0, i=j; i < j+8; i++)
  1408. sum += raw (row+dir[i][0], col+dir[i][1]);
  1409. RAW(row,col) = (sum + 4) >> 3;
  1410. }
  1411. }
  1412. } else if (tag == 0x401) { /* All-color flat fields */
  1413. phase_one_flat_field (1, 2);
  1414. } else if (tag == 0x416 || tag == 0x410) {
  1415. phase_one_flat_field (0, 2);
  1416. } else if (tag == 0x40b) { /* Red+blue flat field */
  1417. phase_one_flat_field (0, 4);
  1418. } else if (tag == 0x412) {
  1419. fseek (ifp, 36, SEEK_CUR);
  1420. diff = abs (get2() - ph1.tag_21a);
  1421. if (mindiff > diff) {
  1422. mindiff = diff;
  1423. off_412 = ftell(ifp) - 38;
  1424. }
  1425. }
  1426. fseek (ifp, save, SEEK_SET);
  1427. }
  1428. if (off_412) {
  1429. fseek (ifp, off_412, SEEK_SET);
  1430. for (i=0; i < 9; i++) head[i] = get4() & 0x7fff;
  1431. yval[0] = (float *) calloc (head[1]*head[3] + head[2]*head[4], 6);
  1432. merror (yval[0], "phase_one_correct()");
  1433. yval[1] = (float *) (yval[0] + head[1]*head[3]);
  1434. xval[0] = (ushort *) (yval[1] + head[2]*head[4]);
  1435. xval[1] = (ushort *) (xval[0] + head[1]*head[3]);
  1436. get2();
  1437. for (i=0; i < 2; i++)
  1438. for (j=0; j < head[i+1]*head[i+3]; j++)
  1439. yval[i][j] = getreal(11);
  1440. for (i=0; i < 2; i++)
  1441. for (j=0; j < head[i+1]*head[i+3]; j++)
  1442. xval[i][j] = get2();
  1443. for (row=0; row < raw_height; row++)
  1444. for (col=0; col < raw_width; col++) {
  1445. cfrac = (float) col * head[3] / raw_width;
  1446. cfrac -= cip = cfrac;
  1447. num = RAW(row,col) * 0.5;
  1448. for (i=cip; i < cip+2; i++) {
  1449. for (k=j=0; j < head[1]; j++)
  1450. if (num < xval[0][k = head[1]*i+j]) break;
  1451. frac = (j == 0 || j == head[1]) ? 0 :
  1452. (xval[0][k] - num) / (xval[0][k] - xval[0][k-1]);
  1453. mult[i-cip] = yval[0][k-1] * frac + yval[0][k] * (1-frac);
  1454. }
  1455. i = ((mult[0] * (1-cfrac) + mult[1] * cfrac) * row + num) * 2;
  1456. RAW(row,col) = LIM(i,0,65535);
  1457. }
  1458. free (yval[0]);
  1459. }
  1460. }
  1461. void CLASS phase_one_load_raw()
  1462. {
  1463. int a, b, i;
  1464. ushort akey, bkey, t_mask;
  1465. fseek (ifp, ph1.key_off, SEEK_SET);
  1466. akey = get2();
  1467. bkey = get2();
  1468. t_mask = ph1.format == 1 ? 0x5555:0x1354;
  1469. fseek (ifp, data_offset, SEEK_SET);
  1470. read_shorts (raw_image, raw_width*raw_height);
  1471. if (ph1.format)
  1472. for (i=0; i < raw_width*raw_height; i+=2) {
  1473. a = raw_image[i+0] ^ akey;
  1474. b = raw_image[i+1] ^ bkey;
  1475. raw_image[i+0] = (a & t_mask) | (b & ~t_mask);
  1476. raw_image[i+1] = (b & t_mask) | (a & ~t_mask);
  1477. }
  1478. }
  1479. unsigned CLASS ph1_bithuff (int nbits, ushort *huff)
  1480. {
  1481. #ifndef LIBRAW_NOTHREADS
  1482. #define bitbuf tls->ph1_bits.bitbuf
  1483. #define vbits tls->ph1_bits.vbits
  1484. #else
  1485. static UINT64 bitbuf=0;
  1486. static int vbits=0;
  1487. #endif
  1488. unsigned c;
  1489. if (nbits == -1)
  1490. return bitbuf = vbits = 0;
  1491. if (nbits == 0) return 0;
  1492. if (vbits < nbits) {
  1493. bitbuf = bitbuf << 32 | get4();
  1494. vbits += 32;
  1495. }
  1496. c = bitbuf << (64-vbits) >> (64-nbits);
  1497. if (huff) {
  1498. vbits -= huff[c] >> 8;
  1499. return (uchar) huff[c];
  1500. }
  1501. vbits -= nbits;
  1502. return c;
  1503. #ifndef LIBRAW_NOTHREADS
  1504. #undef bitbuf
  1505. #undef vbits
  1506. #endif
  1507. }
  1508. #define ph1_bits(n) ph1_bithuff(n,0)
  1509. #define ph1_huff(h) ph1_bithuff(*h,h+1)
  1510. void CLASS phase_one_load_raw_c()
  1511. {
  1512. static const int length[] = { 8,7,6,9,11,10,5,12,14,13 };
  1513. int *offset, len[2], pred[2], row, col, i, j;
  1514. ushort *pixel;
  1515. short (*t_black)[2];
  1516. pixel = (ushort *) calloc (raw_width + raw_height*4, 2);
  1517. merror (pixel, "phase_one_load_raw_c()");
  1518. offset = (int *) (pixel + raw_width);
  1519. fseek (ifp, strip_offset, SEEK_SET);
  1520. for (row=0; row < raw_height; row++)
  1521. offset[row] = get4();
  1522. t_black = (short (*)[2]) offset + raw_height;
  1523. fseek (ifp, ph1.black_off, SEEK_SET);
  1524. if (ph1.black_off)
  1525. {
  1526. read_shorts ((ushort *) t_black[0], raw_height*2);
  1527. #ifdef LIBRAW_LIBRARY_BUILD
  1528. imgdata.rawdata.ph1_black = (short (*)[2])calloc(raw_height*2,sizeof(short));
  1529. merror (imgdata.rawdata.ph1_black, "phase_one_load_raw_c()");
  1530. memmove(imgdata.rawdata.ph1_black,(short *) t_black[0],raw_height*2*sizeof(short));
  1531. #endif
  1532. }
  1533. for (i=0; i < 256; i++)
  1534. curve[i] = i*i / 3.969 + 0.5;
  1535. for (row=0; row < raw_height; row++) {
  1536. fseek (ifp, data_offset + offset[row], SEEK_SET);
  1537. ph1_bits(-1);
  1538. pred[0] = pred[1] = 0;
  1539. for (col=0; col < raw_width; col++) {
  1540. if (col >= (raw_width & -8))
  1541. len[0] = len[1] = 14;
  1542. else if ((col & 7) == 0)
  1543. for (i=0; i < 2; i++) {
  1544. for (j=0; j < 5 && !ph1_bits(1); j++);
  1545. if (j--) len[i] = length[j*2 + ph1_bits(1)];
  1546. }
  1547. if ((i = len[col & 1]) == 14)
  1548. pixel[col] = pred[col & 1] = ph1_bits(16);
  1549. else
  1550. pixel[col] = pred[col & 1] += ph1_bits(i) + 1 - (1 << (i - 1));
  1551. if (pred[col & 1] >> 16) derror();
  1552. if (ph1.format == 5 && pixel[col] < 256)
  1553. pixel[col] = curve[pixel[col]];
  1554. }
  1555. for (col=0; col < raw_width; col++) {
  1556. #ifndef LIBRAW_LIBRARY_BUILD
  1557. i = (pixel[col] << 2) - ph1.t_black + t_black[row][col >= ph1.split_col];
  1558. if (i > 0) RAW(row,col) = i;
  1559. #else
  1560. RAW(row,col) = pixel[col] << 2;
  1561. #endif
  1562. }
  1563. }
  1564. free (pixel);
  1565. maximum = 0xfffc - ph1.t_black;
  1566. }
  1567. void CLASS hasselblad_load_raw()
  1568. {
  1569. struct jhead jh;
  1570. int row, col, pred[2], len[2], diff, c;
  1571. if (!ljpeg_start (&jh, 0)) return;
  1572. order = 0x4949;
  1573. ph1_bits(-1);
  1574. for (row=0; row < raw_height; row++) {
  1575. pred[0] = pred[1] = 0x8000 + load_flags;
  1576. for (col=0; col < raw_width; col+=2) {
  1577. FORC(2) len[c] = ph1_huff(jh.huff[0]);
  1578. FORC(2) {
  1579. diff = ph1_bits(len[c]);
  1580. if ((diff & (1 << (len[c]-1))) == 0)
  1581. diff -= (1 << len[c]) - 1;
  1582. if (diff == 65535) diff = -32768;
  1583. RAW(row,col+c) = pred[c] += diff;
  1584. }
  1585. }
  1586. }
  1587. ljpeg_end (&jh);
  1588. maximum = 0xffff;
  1589. }
  1590. void CLASS leaf_hdr_load_raw()
  1591. {
  1592. ushort *pixel=0;
  1593. unsigned tile=0, r, c, row, col;
  1594. if (!filters) {
  1595. pixel = (ushort *) calloc (raw_width, sizeof *pixel);
  1596. merror (pixel, "leaf_hdr_load_raw()");
  1597. }
  1598. FORC(tiff_samples)
  1599. for (r=0; r < raw_height; r++) {
  1600. if (r % tile_length == 0) {
  1601. fseek (ifp, data_offset + 4*tile++, SEEK_SET);
  1602. fseek (ifp, get4(), SEEK_SET);
  1603. }
  1604. if (filters && c != shot_select) continue;
  1605. if (filters) pixel = raw_image + r*raw_width;
  1606. read_shorts (pixel, raw_width);
  1607. if (!filters && (row = r - top_margin) < height)
  1608. for (col=0; col < width; col++)
  1609. image[row*width+col][c] = pixel[col+left_margin];
  1610. }
  1611. if (!filters) {
  1612. maximum = 0xffff;
  1613. raw_color = 1;
  1614. free (pixel);
  1615. }
  1616. }
  1617. void CLASS unpacked_load_raw()
  1618. {
  1619. int row, col, bits=0;
  1620. while (1 << ++bits < maximum);
  1621. read_shorts (raw_image, raw_width*raw_height);
  1622. for (row=0; row < raw_height; row++)
  1623. for (col=0; col < raw_width; col++)
  1624. if ((RAW(row,col) >>= load_flags) >> bits
  1625. && (unsigned) (row-top_margin) < height
  1626. && (unsigned) (col-left_margin) < width) derror();
  1627. }
  1628. void CLASS sinar_4shot_load_raw()
  1629. {
  1630. ushort *pixel;
  1631. unsigned shot, row, col, r, c;
  1632. if ((shot = shot_select) || half_size) {
  1633. if (shot) shot--;
  1634. if (shot > 3) shot = 3;
  1635. fseek (ifp, data_offset + shot*4, SEEK_SET);
  1636. fseek (ifp, get4(), SEEK_SET);
  1637. unpacked_load_raw();
  1638. return;
  1639. }
  1640. #ifndef LIBRAW_LIBRARY_BUILD
  1641. free (raw_image);
  1642. raw_image = 0;
  1643. free (image);
  1644. image = (ushort (*)[4])
  1645. calloc ((iheight=height)*(iwidth=width), sizeof *image);
  1646. merror (image, "sinar_4shot_load_raw()");
  1647. #endif
  1648. pixel = (ushort *) calloc (raw_width, sizeof *pixel);
  1649. merror (pixel, "sinar_4shot_load_raw()");
  1650. for (shot=0; shot < 4; shot++) {
  1651. fseek (ifp, data_offset + shot*4, SEEK_SET);
  1652. fseek (ifp, get4(), SEEK_SET);
  1653. for (row=0; row < raw_height; row++) {
  1654. read_shorts (pixel, raw_width);
  1655. if ((r = row-top_margin - (shot >> 1 & 1)) >= height) continue;
  1656. for (col=0; col < raw_width; col++) {
  1657. if ((c = col-left_margin - (shot & 1)) >= width) continue;
  1658. image[r*width+c][FC(row,col)] = pixel[col];
  1659. }
  1660. }
  1661. }
  1662. free (pixel);
  1663. shrink = filters = 0;
  1664. }
  1665. void CLASS imacon_full_load_raw()
  1666. {
  1667. int row, col;
  1668. for (row=0; row < height; row++)
  1669. for (col=0; col < width; col++)
  1670. read_shorts (image[row*width+col], 3);
  1671. }
  1672. void CLASS packed_load_raw()
  1673. {
  1674. int vbits=0, bwide, pwide, rbits, bite, half, irow, row, col, val, i;
  1675. UINT64 bitbuf=0;
  1676. if (raw_width * 8 >= width * tiff_bps) /* Is raw_width in bytes? */
  1677. pwide = (bwide = raw_width) * 8 / tiff_bps;
  1678. else bwide = (pwide = raw_width) * tiff_bps / 8;
  1679. rbits = bwide * 8 - pwide * tiff_bps;
  1680. if (load_flags & 1) bwide = bwide * 16 / 15;
  1681. bite = 8 + (load_flags & 24);
  1682. half = (raw_height+1) >> 1;
  1683. for (irow=0; irow < raw_height; irow++) {
  1684. row = irow;
  1685. if (load_flags & 2 &&
  1686. (row = irow % half * 2 + irow / half) == 1 &&
  1687. load_flags & 4) {
  1688. if (vbits=0, tiff_compress)
  1689. fseek (ifp, data_offset - (-half*bwide & -2048), SEEK_SET);
  1690. else {
  1691. fseek (ifp, 0, SEEK_END);
  1692. fseek (ifp, ftell(ifp) >> 3 << 2, SEEK_SET);
  1693. }
  1694. }
  1695. for (col=0; col < pwide; col++) {
  1696. for (vbits -= tiff_bps; vbits < 0; vbits += bite) {
  1697. bitbuf <<= bite;
  1698. for (i=0; i < bite; i+=8)
  1699. bitbuf |= (unsigned) (fgetc(ifp) << i);
  1700. }
  1701. val = bitbuf << (64-tiff_bps-vbits) >> (64-tiff_bps);
  1702. RAW(row,col ^ (load_flags >> 6)) = val;
  1703. if (load_flags & 1 && (col % 10) == 9 &&
  1704. fgetc(ifp) && col < width+left_margin) derror();
  1705. }
  1706. vbits -= rbits;
  1707. }
  1708. }
  1709. void CLASS nokia_load_raw()
  1710. {
  1711. uchar *data, *dp;
  1712. int rev, dwide, row, col, c;
  1713. rev = 3 * (order == 0x4949);
  1714. dwide = raw_width * 5 / 4;
  1715. data = (uchar *) malloc (dwide*2);
  1716. merror (data, "nokia_load_raw()");
  1717. for (row=0; row < raw_height; row++) {
  1718. if (fread (data+dwide, 1, dwide, ifp) < dwide) derror();
  1719. FORC(dwide) data[c] = data[dwide+(c ^ rev)];
  1720. for (dp=data, col=0; col < raw_width; dp+=5, col+=4)
  1721. FORC4 RAW(row,col+c) = (dp[c] << 2) | (dp[4] >> (c << 1) & 3);
  1722. }
  1723. free (data);
  1724. maximum = 0x3ff;
  1725. }
  1726. unsigned CLASS pana_bits (int nbits)
  1727. {
  1728. #ifndef LIBRAW_NOTHREADS
  1729. #define buf tls->pana_bits.buf
  1730. #define vbits tls->pana_bits.vbits
  1731. #else
  1732. static uchar buf[0x4000];
  1733. static int vbits;
  1734. #endif
  1735. int byte;
  1736. if (!nbits) return vbits=0;
  1737. if (!vbits) {
  1738. fread (buf+load_flags, 1, 0x4000-load_flags, ifp);
  1739. fread (buf, 1, load_flags, ifp);
  1740. }
  1741. vbits = (vbits - nbits) & 0x1ffff;
  1742. byte = vbits >> 3 ^ 0x3ff0;
  1743. return (buf[byte] | buf[byte+1] << 8) >> (vbits & 7) & ~(-1 << nbits);
  1744. #ifndef LIBRAW_NOTHREADS
  1745. #undef buf
  1746. #undef vbits
  1747. #endif
  1748. }
  1749. void CLASS panasonic_load_raw()
  1750. {
  1751. int row, col, i, j, sh=0, pred[2], nonz[2];
  1752. pana_bits(0);
  1753. for (row=0; row < height; row++)
  1754. for (col=0; col < raw_width; col++) {
  1755. if ((i = col % 14) == 0)
  1756. pred[0] = pred[1] = nonz[0] = nonz[1] = 0;
  1757. if (i % 3 == 2) sh = 4 >> (3 - pana_bits(2));
  1758. if (nonz[i & 1]) {
  1759. if ((j = pana_bits(8))) {
  1760. if ((pred[i & 1] -= 0x80 << sh) < 0 || sh == 4)
  1761. pred[i & 1] &= ~(-1 << sh);
  1762. pred[i & 1] += j << sh;
  1763. }
  1764. } else if ((nonz[i & 1] = pana_bits(8)) || i > 11)
  1765. pred[i & 1] = nonz[i & 1] << 4 | pana_bits(4);
  1766. if ((RAW(row,col) = pred[col & 1]) > 4098 && col < width) derror();
  1767. }
  1768. }
  1769. void CLASS olympus_load_raw()
  1770. {
  1771. ushort huff[4096];
  1772. int row, col, nbits, sign, low, high, i, c, w, n, nw;
  1773. int acarry[2][3], *carry, pred, diff;
  1774. huff[n=0] = 0xc0c;
  1775. for (i=12; i--; )
  1776. FORC(2048 >> i) huff[++n] = (i+1) << 8 | i;
  1777. fseek (ifp, 7, SEEK_CUR);
  1778. #ifdef LIBRAW_LIBRARY_BUILD
  1779. if(!data_size)
  1780. throw LIBRAW_EXCEPTION_IO_BADFILE;
  1781. LibRaw_byte_buffer *buf = ifp->make_byte_buffer(data_size);
  1782. LibRaw_bit_buffer bits;
  1783. bits.reset();
  1784. #else
  1785. getbits(-1);
  1786. #endif
  1787. for (row=0; row < height; row++) {
  1788. memset (acarry, 0, sizeof acarry);
  1789. for (col=0; col < raw_width; col++) {
  1790. carry = acarry[col & 1];
  1791. i = 2 * (carry[2] < 3);
  1792. for (nbits=2+i; (ushort) carry[0] >> (nbits+i); nbits++);
  1793. #ifdef LIBRAW_LIBRARY_BUILD
  1794. low = (sign = bits._getbits(buf,3,zero_after_ff)) & 3;
  1795. sign = sign << 29 >> 31;
  1796. if ((high = bits._gethuff(buf,12,huff,zero_after_ff)) == 12)
  1797. high = bits._getbits(buf,16-nbits,zero_after_ff) >> 1;
  1798. carry[0] = (high << nbits) | bits._getbits(buf,nbits,zero_after_ff);
  1799. #else
  1800. low = (sign = getbits(3)) & 3;
  1801. sign = sign << 29 >> 31;
  1802. if ((high = getbithuff(12,huff)) == 12)
  1803. high = getbits(16-nbits) >> 1;
  1804. carry[0] = (high << nbits) | getbits(nbits);
  1805. #endif
  1806. diff = (carry[0] ^ sign) + carry[1];
  1807. carry[1] = (diff*3 + carry[1]) >> 5;
  1808. carry[2] = carry[0] > 16 ? 0 : carry[2]+1;
  1809. if (col >= width) continue;
  1810. if (row < 2 && col < 2) pred = 0;
  1811. else if (row < 2) pred = RAW(row,col-2);
  1812. else if (col < 2) pred = RAW(row-2,col);
  1813. else {
  1814. w = RAW(row,col-2);
  1815. n = RAW(row-2,col);
  1816. nw = RAW(row-2,col-2);
  1817. if ((w < nw && nw < n) || (n < nw && nw < w)) {
  1818. if (ABS(w-nw) > 32 || ABS(n-nw) > 32)
  1819. pred = w + n - nw;
  1820. else pred = (w + n) >> 1;
  1821. } else pred = ABS(w-nw) > ABS(n-nw) ? w : n;
  1822. }
  1823. if ((RAW(row,col) = pred + ((diff << 2) | low)) >> 12) derror();
  1824. }
  1825. }
  1826. #ifdef LIBRAW_LIBRARY_BUILD
  1827. delete buf;
  1828. #endif
  1829. }
  1830. void CLASS minolta_rd175_load_raw()
  1831. {
  1832. uchar pixel[768];
  1833. unsigned irow, box, row, col;
  1834. for (irow=0; irow < 1481; irow++) {
  1835. if (fread (pixel, 1, 768, ifp) < 768) derror();
  1836. box = irow / 82;
  1837. row = irow % 82 * 12 + ((box < 12) ? box | 1 : (box-12)*2);
  1838. switch (irow) {
  1839. case 1477: case 1479: continue;
  1840. case 1476: row = 984; break;
  1841. case 1480: row = 985; break;
  1842. case 1478: row = 985; box = 1;
  1843. }
  1844. if ((box < 12) && (box & 1)) {
  1845. for (col=0; col < 1533; col++, row ^= 1)
  1846. if (col != 1) RAW(row,col) = (col+1) & 2 ?
  1847. pixel[col/2-1] + pixel[col/2+1] : pixel[col/2] << 1;
  1848. RAW(row,1) = pixel[1] << 1;
  1849. RAW(row,1533) = pixel[765] << 1;
  1850. } else
  1851. for (col=row & 1; col < 1534; col+=2)
  1852. RAW(row,col) = pixel[col/2] << 1;
  1853. }
  1854. maximum = 0xff << 1;
  1855. }
  1856. void CLASS quicktake_100_load_raw()
  1857. {
  1858. uchar pixel[484][644];
  1859. static const short gstep[16] =
  1860. { -89,-60,-44,-32,-22,-15,-8,-2,2,8,15,22,32,44,60,89 };
  1861. static const short rstep[6][4] =
  1862. { { -3,-1,1,3 }, { -5,-1,1,5 }, { -8,-2,2,8 },
  1863. { -13,-3,3,13 }, { -19,-4,4,19 }, { -28,-6,6,28 } };
  1864. static const short t_curve[256] =
  1865. { 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,
  1866. 28,29,30,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,53,
  1867. 54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,74,75,76,77,78,
  1868. 79,80,81,82,83,84,86,88,90,92,94,97,99,101,103,105,107,110,112,114,116,
  1869. 118,120,123,125,127,129,131,134,136,138,140,142,144,147,149,151,153,155,
  1870. 158,160,162,164,166,168,171,173,175,177,179,181,184,186,188,190,192,195,
  1871. 197,199,201,203,205,208,210,212,214,216,218,221,223,226,230,235,239,244,
  1872. 248,252,257,261,265,270,274,278,283,287,291,296,300,305,309,313,318,322,
  1873. 326,331,335,339,344,348,352,357,361,365,370,374,379,383,387,392,396,400,
  1874. 405,409,413,418,422,426,431,435,440,444,448,453,457,461,466,470,474,479,
  1875. 483,487,492,496,500,508,519,531,542,553,564,575,587,598,609,620,631,643,
  1876. 654,665,676,687,698,710,721,732,743,754,766,777,788,799,810,822,833,844,
  1877. 855,866,878,889,900,911,922,933,945,956,967,978,989,1001,1012,1023 };
  1878. int rb, row, col, sharp, val=0;
  1879. getbits(-1);
  1880. memset (pixel, 0x80, sizeof pixel);
  1881. for (row=2; row < height+2; row++) {
  1882. for (col=2+(row & 1); col < width+2; col+=2) {
  1883. val = ((pixel[row-1][col-1] + 2*pixel[row-1][col+1] +
  1884. pixel[row][col-2]) >> 2) + gstep[getbits(4)];
  1885. pixel[row][col] = val = LIM(val,0,255);
  1886. if (col < 4)
  1887. pixel[row][col-2] = pixel[row+1][~row & 1] = val;
  1888. if (row == 2)
  1889. pixel[row-1][col+1] = pixel[row-1][col+3] = val;
  1890. }
  1891. pixel[row][col] = val;
  1892. }
  1893. for (rb=0; rb < 2; rb++)
  1894. for (row=2+rb; row < height+2; row+=2)
  1895. for (col=3-(row & 1); col < width+2; col+=2) {
  1896. if (row < 4 || col < 4) sharp = 2;
  1897. else {
  1898. val = ABS(pixel[row-2][col] - pixel[row][col-2])
  1899. + ABS(pixel[row-2][col] - pixel[row-2][col-2])
  1900. + ABS(pixel[row][col-2] - pixel[row-2][col-2]);
  1901. sharp = val < 4 ? 0 : val < 8 ? 1 : val < 16 ? 2 :
  1902. val < 32 ? 3 : val < 48 ? 4 : 5;
  1903. }
  1904. val = ((pixel[row-2][col] + pixel[row][col-2]) >> 1)
  1905. + rstep[sharp][getbits(2)];
  1906. pixel[row][col] = val = LIM(val,0,255);
  1907. if (row < 4) pixel[row-2][col+2] = val;
  1908. if (col < 4) pixel[row+2][col-2] = val;
  1909. }
  1910. for (row=2; row < height+2; row++)
  1911. for (col=3-(row & 1); col < width+2; col+=2) {
  1912. val = ((pixel[row][col-1] + (pixel[row][col] << 2) +
  1913. pixel[row][col+1]) >> 1) - 0x100;
  1914. pixel[row][col] = LIM(val,0,255);
  1915. }
  1916. for (row=0; row < height; row++)
  1917. for (col=0; col < width; col++)
  1918. RAW(row,col) = t_curve[pixel[row+2][col+2]];
  1919. maximum = 0x3ff;
  1920. }
  1921. #define radc_token(tree) ((signed char) getbithuff(8,huff[tree]))
  1922. #define FORYX for (y=1; y < 3; y++) for (x=col+1; x >= col; x--)
  1923. #define PREDICTOR (c ? (buf[c][y-1][x] + buf[c][y][x+1]) / 2 \
  1924. : (buf[c][y-1][x+1] + 2*buf[c][y-1][x] + buf[c][y][x+1]) / 4)
  1925. void CLASS kodak_radc_load_raw()
  1926. {
  1927. static const char src[] = {
  1928. 1,1, 2,3, 3,4, 4,2, 5,7, 6,5, 7,6, 7,8,
  1929. 1,0, 2,1, 3,3, 4,4, 5,2, 6,7, 7,6, 8,5, 8,8,
  1930. 2,1, 2,3, 3,0, 3,2, 3,4, 4,6, 5,5, 6,7, 6,8,
  1931. 2,0, 2,1, 2,3, 3,2, 4,4, 5,6, 6,7, 7,5, 7,8,
  1932. 2,1, 2,4, 3,0, 3,2, 3,3, 4,7, 5,5, 6,6, 6,8,
  1933. 2,3, 3,1, 3,2, 3,4, 3,5, 3,6, 4,7, 5,0, 5,8,
  1934. 2,3, 2,6, 3,0, 3,1, 4,4, 4,5, 4,7, 5,2, 5,8,
  1935. 2,4, 2,7, 3,3, 3,6, 4,1, 4,2, 4,5, 5,0, 5,8,
  1936. 2,6, 3,1, 3,3, 3,5, 3,7, 3,8, 4,0, 5,2, 5,4,
  1937. 2,0, 2,1, 3,2, 3,3, 4,4, 4,5, 5,6, 5,7, 4,8,
  1938. 1,0, 2,2, 2,-2,
  1939. 1,-3, 1,3,
  1940. 2,-17, 2,-5, 2,5, 2,17,
  1941. 2,-7, 2,2, 2,9, 2,18,
  1942. 2,-18, 2,-9, 2,-2, 2,7,
  1943. 2,-28, 2,28, 3,-49, 3,-9, 3,9, 4,49, 5,-79, 5,79,
  1944. 2,-1, 2,13, 2,26, 3,39, 4,-16, 5,55, 6,-37, 6,76,
  1945. 2,-26, 2,-13, 2,1, 3,-39, 4,16, 5,-55, 6,-76, 6,37
  1946. };
  1947. ushort huff[19][256];
  1948. int row, col, tree, nreps, rep, step, i, c, s, r, x, y, val;
  1949. short last[3] = { 16,16,16 }, mul[3], buf[3][3][386];
  1950. static const ushort pt[] =
  1951. { 0,0, 1280,1344, 2320,3616, 3328,8000, 4095,16383, 65535,16383 };
  1952. for (i=2; i < 12; i+=2)
  1953. for (c=pt[i-2]; c <= pt[i]; c++)
  1954. curve[c] = (float)
  1955. (c-pt[i-2]) / (pt[i]-pt[i-2]) * (pt[i+1]-pt[i-1]) + pt[i-1] + 0.5;
  1956. for (s=i=0; i < sizeof src; i+=2)
  1957. FORC(256 >> src[i])
  1958. huff[0][s++] = src[i] << 8 | (uchar) src[i+1];
  1959. s = kodak_cbpp == 243 ? 2 : 3;
  1960. FORC(256) huff[18][c] = (8-s) << 8 | c >> s << s | 1 << (s-1);
  1961. getbits(-1);
  1962. for (i=0; i < sizeof(buf)/sizeof(short); i++)
  1963. buf[0][0][i] = 2048;
  1964. for (row=0; row < height; row+=4) {
  1965. FORC3 mul[c] = getbits(6);
  1966. FORC3 {
  1967. val = ((0x1000000/last[c] + 0x7ff) >> 12) * mul[c];
  1968. s = val > 65564 ? 10:12;
  1969. x = ~(-1 << (s-1));
  1970. val <<= 12-s;
  1971. for (i=0; i < sizeof(buf[0])/sizeof(short); i++)
  1972. buf[c][0][i] = (buf[c][0][i] * val + x) >> s;
  1973. last[c] = mul[c];
  1974. for (r=0; r <= !c; r++) {
  1975. buf[c][1][width/2] = buf[c][2][width/2] = mul[c] << 7;
  1976. for (tree=1, col=width/2; col > 0; ) {
  1977. if ((tree = radc_token(tree))) {
  1978. col -= 2;
  1979. if (tree == 8)
  1980. FORYX buf[c][y][x] = (uchar) radc_token(18) * mul[c];
  1981. else
  1982. FORYX buf[c][y][x] = radc_token(tree+10) * 16 + PREDICTOR;
  1983. } else
  1984. do {
  1985. nreps = (col > 2) ? radc_token(9) + 1 : 1;
  1986. for (rep=0; rep < 8 && rep < nreps && col > 0; rep++) {
  1987. col -= 2;
  1988. FORYX buf[c][y][x] = PREDICTOR;
  1989. if (rep & 1) {
  1990. step = radc_token(10) << 4;
  1991. FORYX buf[c][y][x] += step;
  1992. }
  1993. }
  1994. } while (nreps == 9);
  1995. }
  1996. for (y=0; y < 2; y++)
  1997. for (x=0; x < width/2; x++) {
  1998. val = (buf[c][y+1][x] << 4) / mul[c];
  1999. if (val < 0) val = 0;
  2000. if (c) RAW(row+y*2+c-1,x*2+2-c) = val;
  2001. else RAW(row+r*2+y,x*2+y) = val;
  2002. }
  2003. memcpy (buf[c][0]+!c, buf[c][2], sizeof buf[c][0]-2*!c);
  2004. }
  2005. }
  2006. for (y=row; y < row+4; y++)
  2007. for (x=0; x < width; x++)
  2008. if ((x+y) & 1) {
  2009. r = x ? x-1 : x+1;
  2010. s = x+1 < width ? x+1 : x-1;
  2011. val = (RAW(y,x)-2048)*2 + (RAW(y,r)+RAW(y,s))/2;
  2012. if (val < 0) val = 0;
  2013. RAW(y,x) = val;
  2014. }
  2015. }
  2016. for (i=0; i < height*width; i++)
  2017. raw_image[i] = curve[raw_image[i]];
  2018. maximum = 0x3fff;
  2019. }
  2020. #undef FORYX
  2021. #undef PREDICTOR
  2022. #ifdef NO_JPEG
  2023. void CLASS kodak_jpeg_load_raw() {}
  2024. void CLASS lossy_dng_load_raw() {}
  2025. #else
  2026. #ifdef LIBRAW_LIBRARY_BUILD
  2027. void CLASS kodak_jpeg_load_raw() {}
  2028. #else
  2029. METHODDEF(boolean)
  2030. fill_input_buffer (j_decompress_ptr cinfo)
  2031. {
  2032. #ifndef LIBRAW_NOTHREADS
  2033. #define jpeg_buffer tls->jpeg_buffer
  2034. #else
  2035. static uchar jpeg_buffer[4096];
  2036. #endif
  2037. size_t nbytes;
  2038. nbytes = fread (jpeg_buffer, 1, 4096, ifp);
  2039. swab (jpeg_buffer, jpeg_buffer, nbytes);
  2040. cinfo->src->next_input_byte = jpeg_buffer;
  2041. cinfo->src->bytes_in_buffer = nbytes;
  2042. return TRUE;
  2043. #ifndef LIBRAW_NOTHREADS
  2044. #undef jpeg_buffer
  2045. #endif
  2046. }
  2047. void CLASS kodak_jpeg_load_raw()
  2048. {
  2049. struct jpeg_decompress_struct cinfo;
  2050. struct jpeg_error_mgr jerr;
  2051. JSAMPARRAY buf;
  2052. JSAMPLE (*pixel)[3];
  2053. int row, col;
  2054. cinfo.err = jpeg_std_error (&jerr);
  2055. jpeg_create_decompress (&cinfo);
  2056. jpeg_stdio_src (&cinfo, ifp);
  2057. cinfo.src->fill_input_buffer = fill_input_buffer;
  2058. jpeg_read_header (&cinfo, TRUE);
  2059. jpeg_start_decompress (&cinfo);
  2060. if ((cinfo.output_width != width ) ||
  2061. (cinfo.output_height*2 != height ) ||
  2062. (cinfo.output_components != 3 )) {
  2063. #ifdef DCRAW_VERBOSE
  2064. fprintf (stderr,_("%s: incorrect JPEG dimensions\n"), ifname);
  2065. #endif
  2066. jpeg_destroy_decompress (&cinfo);
  2067. #ifdef LIBRAW_LIBRARY_BUILD
  2068. throw LIBRAW_EXCEPTION_DECODE_JPEG;
  2069. #else
  2070. longjmp (failure, 3);
  2071. #endif
  2072. }
  2073. buf = (*cinfo.mem->alloc_sarray)
  2074. ((j_common_ptr) &cinfo, JPOOL_IMAGE, width*3, 1);
  2075. while (cinfo.output_scanline < cinfo.output_height) {
  2076. row = cinfo.output_scanline * 2;
  2077. jpeg_read_scanlines (&cinfo, buf, 1);
  2078. pixel = (JSAMPLE (*)[3]) buf[0];
  2079. for (col=0; col < width; col+=2) {
  2080. RAW(row+0,col+0) = pixel[col+0][1] << 1;
  2081. RAW(row+1,col+1) = pixel[col+1][1] << 1;
  2082. RAW(row+0,col+1) = pixel[col][0] + pixel[col+1][0];
  2083. RAW(row+1,col+0) = pixel[col][2] + pixel[col+1][2];
  2084. }
  2085. }
  2086. jpeg_finish_decompress (&cinfo);
  2087. jpeg_destroy_decompress (&cinfo);
  2088. maximum = 0xff << 1;
  2089. }
  2090. #endif
  2091. void CLASS lossy_dng_load_raw()
  2092. {
  2093. struct jpeg_decompress_struct cinfo;
  2094. struct jpeg_error_mgr jerr;
  2095. JSAMPARRAY buf;
  2096. JSAMPLE (*pixel)[3];
  2097. unsigned sorder=order, ntags, opcode, deg, i, j, c;
  2098. unsigned save=data_offset-4, trow=0, tcol=0, row, col;
  2099. ushort t_curve[3][256];
  2100. double coeff[9], tot;
  2101. fseek (ifp, meta_offset, SEEK_SET);
  2102. order = 0x4d4d;
  2103. ntags = get4();
  2104. while (ntags--) {
  2105. opcode = get4(); get4(); get4();
  2106. if (opcode != 8)
  2107. { fseek (ifp, get4(), SEEK_CUR); continue; }
  2108. fseek (ifp, 20, SEEK_CUR);
  2109. if ((c = get4()) > 2) break;
  2110. fseek (ifp, 12, SEEK_CUR);
  2111. if ((deg = get4()) > 8) break;
  2112. for (i=0; i <= deg && i < 9; i++)
  2113. coeff[i] = getreal(12);
  2114. for (i=0; i < 256; i++) {
  2115. for (tot=j=0; j <= deg; j++)
  2116. tot += coeff[j] * pow(i/255.0, (int)j);
  2117. t_curve[c][i] = tot*0xffff;
  2118. }
  2119. }
  2120. order = sorder;
  2121. cinfo.err = jpeg_std_error (&jerr);
  2122. jpeg_create_decompress (&cinfo);
  2123. while (trow < raw_height) {
  2124. fseek (ifp, save+=4, SEEK_SET);
  2125. if (tile_length < INT_MAX)
  2126. fseek (ifp, get4(), SEEK_SET);
  2127. #ifdef LIBRAW_LIBRARY_BUILD
  2128. if(libraw_internal_data.internal_data.input->jpeg_src(&cinfo) == -1)
  2129. {
  2130. jpeg_destroy_decompress(&cinfo);
  2131. throw LIBRAW_EXCEPTION_DECODE_JPEG;
  2132. }
  2133. #else
  2134. jpeg_stdio_src (&cinfo, ifp);
  2135. #endif
  2136. jpeg_read_header (&cinfo, TRUE);
  2137. jpeg_start_decompress (&cinfo);
  2138. buf = (*cinfo.mem->alloc_sarray)
  2139. ((j_common_ptr) &cinfo, JPOOL_IMAGE, cinfo.output_width*3, 1);
  2140. while (cinfo.output_scanline < cinfo.output_height &&
  2141. (row = trow + cinfo.output_scanline) < height) {
  2142. jpeg_read_scanlines (&cinfo, buf, 1);
  2143. pixel = (JSAMPLE (*)[3]) buf[0];
  2144. for (col=0; col < cinfo.output_width && tcol+col < width; col++) {
  2145. FORC3 image[row*width+tcol+col][c] = t_curve[c][pixel[col][c]];
  2146. }
  2147. }
  2148. jpeg_abort_decompress (&cinfo);
  2149. if ((tcol += tile_width) >= raw_width)
  2150. trow += tile_length + (tcol = 0);
  2151. }
  2152. jpeg_destroy_decompress (&cinfo);
  2153. maximum = 0xffff;
  2154. }
  2155. #endif
  2156. void CLASS kodak_dc120_load_raw()
  2157. {
  2158. static const int mul[4] = { 162, 192, 187, 92 };
  2159. static const int add[4] = { 0, 636, 424, 212 };
  2160. uchar pixel[848];
  2161. int row, shift, col;
  2162. for (row=0; row < height; row++) {
  2163. if (fread (pixel, 1, 848, ifp) < 848) derror();
  2164. shift = row * mul[row & 3] + add[row & 3];
  2165. for (col=0; col < width; col++)
  2166. RAW(row,col) = (ushort) pixel[(col + shift) % 848];
  2167. }
  2168. maximum = 0xff;
  2169. }
  2170. void CLASS eight_bit_load_raw()
  2171. {
  2172. uchar *pixel;
  2173. unsigned row, col;
  2174. pixel = (uchar *) calloc (raw_width, sizeof *pixel);
  2175. merror (pixel, "eight_bit_load_raw()");
  2176. for (row=0; row < raw_height; row++) {
  2177. if (fread (pixel, 1, raw_width, ifp) < raw_width) derror();
  2178. for (col=0; col < raw_width; col++)
  2179. RAW(row,col) = curve[pixel[col]];
  2180. }
  2181. free (pixel);
  2182. maximum = curve[0xff];
  2183. }
  2184. void CLASS kodak_yrgb_load_raw()
  2185. {
  2186. uchar *pixel;
  2187. int row, col, y, cb, cr, rgb[3], c;
  2188. pixel = (uchar *) calloc (raw_width, 3*sizeof *pixel);
  2189. merror (pixel, "kodak_yrgb_load_raw()");
  2190. for (row=0; row < height; row++) {
  2191. if (~row & 1)
  2192. if (fread (pixel, raw_width, 3, ifp) < 3) derror();
  2193. for (col=0; col < raw_width; col++) {
  2194. y = pixel[width*2*(row & 1) + col];
  2195. cb = pixel[width + (col & -2)] - 128;
  2196. cr = pixel[width + (col & -2)+1] - 128;
  2197. rgb[1] = y-((cb + cr + 2) >> 2);
  2198. rgb[2] = rgb[1] + cb;
  2199. rgb[0] = rgb[1] + cr;
  2200. FORC3 image[row*width+col][c] = curve[LIM(rgb[c],0,255)];
  2201. }
  2202. }
  2203. free (pixel);
  2204. maximum = curve[0xff];
  2205. }
  2206. void CLASS kodak_262_load_raw()
  2207. {
  2208. static const uchar kodak_tree[2][26] =
  2209. { { 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 },
  2210. { 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 } };
  2211. ushort *huff[2];
  2212. uchar *pixel;
  2213. int *strip, ns, c, row, col, chess, pi=0, pi1, pi2, pred, val;
  2214. FORC(2) huff[c] = make_decoder (kodak_tree[c]);
  2215. ns = (raw_height+63) >> 5;
  2216. pixel = (uchar *) malloc (raw_width*32 + ns*4);
  2217. merror (pixel, "kodak_262_load_raw()");
  2218. strip = (int *) (pixel + raw_width*32);
  2219. order = 0x4d4d;
  2220. FORC(ns) strip[c] = get4();
  2221. for (row=0; row < raw_height; row++) {
  2222. if ((row & 31) == 0) {
  2223. fseek (ifp, strip[row >> 5], SEEK_SET);
  2224. getbits(-1);
  2225. pi = 0;
  2226. }
  2227. for (col=0; col < raw_width; col++) {
  2228. chess = (row + col) & 1;
  2229. pi1 = chess ? pi-2 : pi-raw_width-1;
  2230. pi2 = chess ? pi-2*raw_width : pi-raw_width+1;
  2231. if (col <= chess) pi1 = -1;
  2232. if (pi1 < 0) pi1 = pi2;
  2233. if (pi2 < 0) pi2 = pi1;
  2234. if (pi1 < 0 && col > 1) pi1 = pi2 = pi-2;
  2235. pred = (pi1 < 0) ? 0 : (pixel[pi1] + pixel[pi2]) >> 1;
  2236. pixel[pi] = val = pred + ljpeg_diff (huff[chess]);
  2237. if (val >> 8) derror();
  2238. val = curve[pixel[pi++]];
  2239. RAW(row,col) = val;
  2240. }
  2241. }
  2242. free (pixel);
  2243. FORC(2) free (huff[c]);
  2244. }
  2245. int CLASS kodak_65000_decode (short *out, int bsize)
  2246. {
  2247. uchar c, blen[768];
  2248. ushort raw[6];
  2249. INT64 bitbuf=0;
  2250. int save, bits=0, i, j, len, diff;
  2251. save = ftell(ifp);
  2252. bsize = (bsize + 3) & -4;
  2253. for (i=0; i < bsize; i+=2) {
  2254. c = fgetc(ifp);
  2255. if ((blen[i ] = c & 15) > 12 ||
  2256. (blen[i+1] = c >> 4) > 12 ) {
  2257. fseek (ifp, save, SEEK_SET);
  2258. for (i=0; i < bsize; i+=8) {
  2259. read_shorts (raw, 6);
  2260. out[i ] = raw[0] >> 12 << 8 | raw[2] >> 12 << 4 | raw[4] >> 12;
  2261. out[i+1] = raw[1] >> 12 << 8 | raw[3] >> 12 << 4 | raw[5] >> 12;
  2262. for (j=0; j < 6; j++)
  2263. out[i+2+j] = raw[j] & 0xfff;
  2264. }
  2265. return 1;
  2266. }
  2267. }
  2268. if ((bsize & 7) == 4) {
  2269. bitbuf = fgetc(ifp) << 8;
  2270. bitbuf += fgetc(ifp);
  2271. bits = 16;
  2272. }
  2273. for (i=0; i < bsize; i++) {
  2274. len = blen[i];
  2275. if (bits < len) {
  2276. for (j=0; j < 32; j+=8)
  2277. bitbuf += (INT64) fgetc(ifp) << (bits+(j^8));
  2278. bits += 32;
  2279. }
  2280. diff = bitbuf & (0xffff >> (16-len));
  2281. bitbuf >>= len;
  2282. bits -= len;
  2283. if ((diff & (1 << (len-1))) == 0)
  2284. diff -= (1 << len) - 1;
  2285. out[i] = diff;
  2286. }
  2287. return 0;
  2288. }
  2289. void CLASS kodak_65000_load_raw()
  2290. {
  2291. short buf[256];
  2292. int row, col, len, pred[2], ret, i;
  2293. for (row=0; row < height; row++)
  2294. for (col=0; col < width; col+=256) {
  2295. pred[0] = pred[1] = 0;
  2296. len = MIN (256, width-col);
  2297. ret = kodak_65000_decode (buf, len);
  2298. for (i=0; i < len; i++)
  2299. if ((RAW(row,col+i) = curve[ret ? buf[i] :
  2300. (pred[i & 1] += buf[i])]) >> 12) derror();
  2301. }
  2302. }
  2303. void CLASS kodak_ycbcr_load_raw()
  2304. {
  2305. short buf[384], *bp;
  2306. int row, col, len, c, i, j, k, y[2][2], cb, cr, rgb[3];
  2307. ushort *ip;
  2308. for (row=0; row < height; row+=2)
  2309. for (col=0; col < width; col+=128) {
  2310. len = MIN (128, width-col);
  2311. kodak_65000_decode (buf, len*3);
  2312. y[0][1] = y[1][1] = cb = cr = 0;
  2313. for (bp=buf, i=0; i < len; i+=2, bp+=2) {
  2314. cb += bp[4];
  2315. cr += bp[5];
  2316. rgb[1] = -((cb + cr + 2) >> 2);
  2317. rgb[2] = rgb[1] + cb;
  2318. rgb[0] = rgb[1] + cr;
  2319. for (j=0; j < 2; j++)
  2320. for (k=0; k < 2; k++) {
  2321. if ((y[j][k] = y[j][k^1] + *bp++) >> 10) derror();
  2322. ip = image[(row+j)*width + col+i+k];
  2323. FORC3 ip[c] = curve[LIM(y[j][k]+rgb[c], 0, 0xfff)];
  2324. }
  2325. }
  2326. }
  2327. }
  2328. void CLASS kodak_rgb_load_raw()
  2329. {
  2330. short buf[768], *bp;
  2331. int row, col, len, c, i, rgb[3];
  2332. ushort *ip=image[0];
  2333. #ifndef LIBRAW_LIBRARY_BUILD
  2334. if (raw_image) free (raw_image);
  2335. raw_image = 0;
  2336. #endif
  2337. for (row=0; row < height; row++)
  2338. for (col=0; col < width; col+=256) {
  2339. len = MIN (256, width-col);
  2340. kodak_65000_decode (buf, len*3);
  2341. memset (rgb, 0, sizeof rgb);
  2342. for (bp=buf, i=0; i < len; i++, ip+=4)
  2343. FORC3 if ((ip[c] = rgb[c] += *bp++) >> 12) derror();
  2344. }
  2345. }
  2346. void CLASS kodak_thumb_load_raw()
  2347. {
  2348. int row, col;
  2349. colors = thumb_misc >> 5;
  2350. for (row=0; row < height; row++)
  2351. for (col=0; col < width; col++)
  2352. read_shorts (image[row*width+col], colors);
  2353. maximum = (1 << (thumb_misc & 31)) - 1;
  2354. }
  2355. void CLASS sony_decrypt (unsigned *data, int len, int start, int key)
  2356. {
  2357. #ifndef LIBRAW_NOTHREADS
  2358. #define pad tls->sony_decrypt.pad
  2359. #define p tls->sony_decrypt.p
  2360. #else
  2361. static unsigned pad[128], p;
  2362. #endif
  2363. if (start) {
  2364. for (p=0; p < 4; p++)
  2365. pad[p] = key = key * 48828125 + 1;
  2366. pad[3] = pad[3] << 1 | (pad[0]^pad[2]) >> 31;
  2367. for (p=4; p < 127; p++)
  2368. pad[p] = (pad[p-4]^pad[p-2]) << 1 | (pad[p-3]^pad[p-1]) >> 31;
  2369. for (p=0; p < 127; p++)
  2370. pad[p] = htonl(pad[p]);
  2371. }
  2372. while (len--)
  2373. *data++ ^= pad[p++ & 127] = pad[(p+1) & 127] ^ pad[(p+65) & 127];
  2374. #ifndef LIBRAW_NOTHREADS
  2375. #undef pad
  2376. #undef p
  2377. #endif
  2378. }
  2379. void CLASS sony_load_raw()
  2380. {
  2381. uchar head[40];
  2382. ushort *pixel;
  2383. unsigned i, key, row, col;
  2384. fseek (ifp, 200896, SEEK_SET);
  2385. fseek (ifp, (unsigned) fgetc(ifp)*4 - 1, SEEK_CUR);
  2386. order = 0x4d4d;
  2387. key = get4();
  2388. fseek (ifp, 164600, SEEK_SET);
  2389. fread (head, 1, 40, ifp);
  2390. sony_decrypt ((unsigned int *) head, 10, 1, key);
  2391. for (i=26; i-- > 22; )
  2392. key = key << 8 | head[i];
  2393. fseek (ifp, data_offset, SEEK_SET);
  2394. for (row=0; row < raw_height; row++) {
  2395. pixel = raw_image + row*raw_width;
  2396. if (fread (pixel, 2, raw_width, ifp) < raw_width) derror();
  2397. sony_decrypt ((unsigned int *) pixel, raw_width/2, !row, key);
  2398. for (col=0; col < raw_width; col++)
  2399. if ((pixel[col] = ntohs(pixel[col])) >> 14) derror();
  2400. }
  2401. maximum = 0x3ff0;
  2402. }
  2403. void CLASS sony_arw_load_raw()
  2404. {
  2405. ushort huff[32768];
  2406. static const ushort tab[18] =
  2407. { 0xf11,0xf10,0xe0f,0xd0e,0xc0d,0xb0c,0xa0b,0x90a,0x809,
  2408. 0x708,0x607,0x506,0x405,0x304,0x303,0x300,0x202,0x201 };
  2409. int i, c, n, col, row, len, diff, sum=0;
  2410. for (n=i=0; i < 18; i++)
  2411. FORC(32768 >> (tab[i] >> 8)) huff[n++] = tab[i];
  2412. #ifdef LIBRAW_LIBRARY_BUILD
  2413. LibRaw_byte_buffer *buf=NULL;
  2414. if(data_size)
  2415. buf = ifp->make_byte_buffer(data_size);
  2416. else
  2417. getbits(-1);
  2418. LibRaw_bit_buffer bits;
  2419. bits.reset();
  2420. #else
  2421. getbits(-1);
  2422. #endif
  2423. for (col = raw_width; col--; )
  2424. for (row=0; row < raw_height+1; row+=2) {
  2425. if (row == raw_height) row = 1;
  2426. #ifdef LIBRAW_LIBRARY_BUILD
  2427. if(data_size)
  2428. {
  2429. len = bits._gethuff(buf,15,huff,zero_after_ff);
  2430. diff = bits._getbits(buf,len,zero_after_ff);
  2431. }
  2432. else
  2433. {
  2434. len = getbithuff(15,huff);
  2435. diff = getbits(len);
  2436. }
  2437. #else
  2438. len = getbithuff(15,huff);
  2439. diff = getbits(len);
  2440. #endif
  2441. if ((diff & (1 << (len-1))) == 0)
  2442. diff -= (1 << len) - 1;
  2443. if ((sum += diff) >> 12) derror();
  2444. if (row < height) RAW(row,col) = sum;
  2445. }
  2446. #ifdef LIBRAW_LIBRARY_BUILD
  2447. if(buf) delete buf;
  2448. #endif
  2449. }
  2450. void CLASS sony_arw2_load_raw()
  2451. {
  2452. uchar *data, *dp;
  2453. ushort pix[16];
  2454. int row, col, val, max, min, imax, imin, sh, bit, i;
  2455. data = (uchar *) malloc (raw_width);
  2456. merror (data, "sony_arw2_load_raw()");
  2457. for (row=0; row < height; row++) {
  2458. fread (data, 1, raw_width, ifp);
  2459. for (dp=data, col=0; col < raw_width-30; dp+=16) {
  2460. max = 0x7ff & (val = sget4(dp));
  2461. min = 0x7ff & val >> 11;
  2462. imax = 0x0f & val >> 22;
  2463. imin = 0x0f & val >> 26;
  2464. for (sh=0; sh < 4 && 0x80 << sh <= max-min; sh++);
  2465. for (bit=30, i=0; i < 16; i++)
  2466. if (i == imax) pix[i] = max;
  2467. else if (i == imin) pix[i] = min;
  2468. else {
  2469. pix[i] = ((sget2(dp+(bit >> 3)) >> (bit & 7) & 0x7f) << sh) + min;
  2470. if (pix[i] > 0x7ff) pix[i] = 0x7ff;
  2471. bit += 7;
  2472. }
  2473. #ifdef LIBRAW_LIBRARY_BUILD
  2474. if(imgdata.params.sony_arw2_hack)
  2475. {
  2476. for (i=0; i < 16; i++, col+=2)
  2477. RAW(row,col) = curve[pix[i] << 1];
  2478. }
  2479. else
  2480. {
  2481. for (i=0; i < 16; i++, col+=2)
  2482. RAW(row,col) = curve[pix[i] << 1] >> 2;
  2483. }
  2484. #else
  2485. for (i=0; i < 16; i++, col+=2)
  2486. RAW(row,col) = curve[pix[i] << 1] >> 2;
  2487. #endif
  2488. col -= col & 1 ? 1:31;
  2489. }
  2490. }
  2491. free (data);
  2492. #ifdef LIBRAW_LIBRARY_BUILD
  2493. if(imgdata.params.sony_arw2_hack)
  2494. {
  2495. black <<= 2;
  2496. maximum <<=2;
  2497. }
  2498. #endif
  2499. }
  2500. #define HOLE(row) ((holes >> (((row) - raw_height) & 7)) & 1)
  2501. /* Kudos to Rich Taylor for figuring out SMaL's compression algorithm. */
  2502. void CLASS smal_decode_segment (unsigned seg[2][2], int holes)
  2503. {
  2504. uchar hist[3][13] = {
  2505. { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
  2506. { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
  2507. { 3, 3, 0, 0, 63, 47, 31, 15, 0 } };
  2508. int low, high=0xff, carry=0, nbits=8;
  2509. int pix, s, count, bin, next, i, sym[3];
  2510. uchar diff, pred[]={0,0};
  2511. ushort data=0, range=0;
  2512. fseek (ifp, seg[0][1]+1, SEEK_SET);
  2513. getbits(-1);
  2514. for (pix=seg[0][0]; pix < seg[1][0]; pix++) {
  2515. for (s=0; s < 3; s++) {
  2516. data = data << nbits | getbits(nbits);
  2517. if (carry < 0)
  2518. carry = (nbits += carry+1) < 1 ? nbits-1 : 0;
  2519. while (--nbits >= 0)
  2520. if ((data >> nbits & 0xff) == 0xff) break;
  2521. if (nbits > 0)
  2522. data = ((data & ((1 << (nbits-1)) - 1)) << 1) |
  2523. ((data + (((data & (1 << (nbits-1)))) << 1)) & (-1 << nbits));
  2524. if (nbits >= 0) {
  2525. data += getbits(1);
  2526. carry = nbits - 8;
  2527. }
  2528. count = ((((data-range+1) & 0xffff) << 2) - 1) / (high >> 4);
  2529. for (bin=0; hist[s][bin+5] > count; bin++);
  2530. low = hist[s][bin+5] * (high >> 4) >> 2;
  2531. if (bin) high = hist[s][bin+4] * (high >> 4) >> 2;
  2532. high -= low;
  2533. for (nbits=0; high << nbits < 128; nbits++);
  2534. range = (range+low) << nbits;
  2535. high <<= nbits;
  2536. next = hist[s][1];
  2537. if (++hist[s][2] > hist[s][3]) {
  2538. next = (next+1) & hist[s][0];
  2539. hist[s][3] = (hist[s][next+4] - hist[s][next+5]) >> 2;
  2540. hist[s][2] = 1;
  2541. }
  2542. if (hist[s][hist[s][1]+4] - hist[s][hist[s][1]+5] > 1) {
  2543. if (bin < hist[s][1])
  2544. for (i=bin; i < hist[s][1]; i++) hist[s][i+5]--;
  2545. else if (next <= bin)
  2546. for (i=hist[s][1]; i < bin; i++) hist[s][i+5]++;
  2547. }
  2548. hist[s][1] = next;
  2549. sym[s] = bin;
  2550. }
  2551. diff = sym[2] << 5 | sym[1] << 2 | (sym[0] & 3);
  2552. if (sym[0] & 4)
  2553. diff = diff ? -diff : 0x80;
  2554. if (ftell(ifp) + 12 >= seg[1][1])
  2555. diff = 0;
  2556. raw_image[pix] = pred[pix & 1] += diff;
  2557. if (!(pix & 1) && HOLE(pix / raw_width)) pix += 2;
  2558. }
  2559. maximum = 0xff;
  2560. }
  2561. void CLASS smal_v6_load_raw()
  2562. {
  2563. unsigned seg[2][2];
  2564. fseek (ifp, 16, SEEK_SET);
  2565. seg[0][0] = 0;
  2566. seg[0][1] = get2();
  2567. seg[1][0] = raw_width * raw_height;
  2568. seg[1][1] = INT_MAX;
  2569. smal_decode_segment (seg, 0);
  2570. }
  2571. int CLASS median4 (int *p)
  2572. {
  2573. int min, max, sum, i;
  2574. min = max = sum = p[0];
  2575. for (i=1; i < 4; i++) {
  2576. sum += p[i];
  2577. if (min > p[i]) min = p[i];
  2578. if (max < p[i]) max = p[i];
  2579. }
  2580. return (sum - min - max) >> 1;
  2581. }
  2582. void CLASS fill_holes (int holes)
  2583. {
  2584. int row, col, val[4];
  2585. for (row=2; row < height-2; row++) {
  2586. if (!HOLE(row)) continue;
  2587. for (col=1; col < width-1; col+=4) {
  2588. val[0] = RAW(row-1,col-1);
  2589. val[1] = RAW(row-1,col+1);
  2590. val[2] = RAW(row+1,col-1);
  2591. val[3] = RAW(row+1,col+1);
  2592. RAW(row,col) = median4(val);
  2593. }
  2594. for (col=2; col < width-2; col+=4)
  2595. if (HOLE(row-2) || HOLE(row+2))
  2596. RAW(row,col) = (RAW(row,col-2) + RAW(row,col+2)) >> 1;
  2597. else {
  2598. val[0] = RAW(row,col-2);
  2599. val[1] = RAW(row,col+2);
  2600. val[2] = RAW(row-2,col);
  2601. val[3] = RAW(row+2,col);
  2602. RAW(row,col) = median4(val);
  2603. }
  2604. }
  2605. }
  2606. void CLASS smal_v9_load_raw()
  2607. {
  2608. unsigned seg[256][2], offset, nseg, holes, i;
  2609. fseek (ifp, 67, SEEK_SET);
  2610. offset = get4();
  2611. nseg = fgetc(ifp);
  2612. fseek (ifp, offset, SEEK_SET);
  2613. for (i=0; i < nseg*2; i++)
  2614. seg[0][i] = get4() + data_offset*(i & 1);
  2615. fseek (ifp, 78, SEEK_SET);
  2616. holes = fgetc(ifp);
  2617. fseek (ifp, 88, SEEK_SET);
  2618. seg[nseg][0] = raw_height * raw_width;
  2619. seg[nseg][1] = get4() + data_offset;
  2620. for (i=0; i < nseg; i++)
  2621. smal_decode_segment (seg+i, holes);
  2622. if (holes) fill_holes (holes);
  2623. }
  2624. void CLASS redcine_load_raw()
  2625. {
  2626. #ifndef NO_JASPER
  2627. int c, row, col;
  2628. jas_stream_t *in;
  2629. jas_image_t *jimg;
  2630. jas_matrix_t *jmat;
  2631. jas_seqent_t *data;
  2632. ushort *img, *pix;
  2633. jas_init();
  2634. #ifndef LIBRAW_LIBRARY_BUILD
  2635. in = jas_stream_fopen (ifname, "rb");
  2636. #else
  2637. in = (jas_stream_t*)ifp->make_jas_stream();
  2638. if(!in)
  2639. throw LIBRAW_EXCEPTION_DECODE_JPEG2000;
  2640. #endif
  2641. jas_stream_seek (in, data_offset+20, SEEK_SET);
  2642. jimg = jas_image_decode (in, -1, 0);
  2643. #ifndef LIBRAW_LIBRARY_BUILD
  2644. if (!jimg) longjmp (failure, 3);
  2645. #else
  2646. if(!jimg)
  2647. {
  2648. jas_stream_close (in);
  2649. throw LIBRAW_EXCEPTION_DECODE_JPEG2000;
  2650. }
  2651. #endif
  2652. jmat = jas_matrix_create (height/2, width/2);
  2653. merror (jmat, "redcine_load_raw()");
  2654. img = (ushort *) calloc ((height+2)*(width+2), 2);
  2655. merror (img, "redcine_load_raw()");
  2656. FORC4 {
  2657. jas_image_readcmpt (jimg, c, 0, 0, width/2, height/2, jmat);
  2658. data = jas_matrix_getref (jmat, 0, 0);
  2659. for (row = c >> 1; row < height; row+=2)
  2660. for (col = c & 1; col < width; col+=2)
  2661. img[(row+1)*(width+2)+col+1] = data[(row/2)*(width/2)+col/2];
  2662. }
  2663. for (col=1; col <= width; col++) {
  2664. img[col] = img[2*(width+2)+col];
  2665. img[(height+1)*(width+2)+col] = img[(height-1)*(width+2)+col];
  2666. }
  2667. for (row=0; row < height+2; row++) {
  2668. img[row*(width+2)] = img[row*(width+2)+2];
  2669. img[(row+1)*(width+2)-1] = img[(row+1)*(width+2)-3];
  2670. }
  2671. for (row=1; row <= height; row++) {
  2672. pix = img + row*(width+2) + (col = 1 + (FC(row,1) & 1));
  2673. for ( ; col <= width; col+=2, pix+=2) {
  2674. c = (((pix[0] - 0x800) << 3) +
  2675. pix[-(width+2)] + pix[width+2] + pix[-1] + pix[1]) >> 2;
  2676. pix[0] = LIM(c,0,4095);
  2677. }
  2678. }
  2679. for (row=0; row < height; row++)
  2680. for (col=0; col < width; col++)
  2681. RAW(row,col) = curve[img[(row+1)*(width+2)+col+1]];
  2682. free (img);
  2683. jas_matrix_destroy (jmat);
  2684. jas_image_destroy (jimg);
  2685. jas_stream_close (in);
  2686. #endif
  2687. }
  2688. #line 3860 "dcraw/dcraw.c"
  2689. void CLASS crop_masked_pixels()
  2690. {
  2691. int row, col;
  2692. unsigned
  2693. #ifndef LIBRAW_LIBRARY_BUILD
  2694. r, raw_pitch = raw_width*2,
  2695. #endif
  2696. c, m, mblack[8], zero, val;
  2697. #ifndef LIBRAW_LIBRARY_BUILD
  2698. if (load_raw == &CLASS phase_one_load_raw ||
  2699. load_raw == &CLASS phase_one_load_raw_c)
  2700. phase_one_correct();
  2701. if (fuji_width) {
  2702. for (row=0; row < raw_height-top_margin*2; row++) {
  2703. for (col=0; col < fuji_width << !fuji_layout; col++) {
  2704. if (fuji_layout) {
  2705. r = fuji_width - 1 - col + (row >> 1);
  2706. c = col + ((row+1) >> 1);
  2707. } else {
  2708. r = fuji_width - 1 + row - (col >> 1);
  2709. c = row + ((col+1) >> 1);
  2710. }
  2711. if (r < height && c < width)
  2712. BAYER(r,c) = RAW(row+top_margin,col+left_margin);
  2713. }
  2714. }
  2715. } else {
  2716. for (row=0; row < height; row++)
  2717. for (col=0; col < width; col++)
  2718. BAYER2(row,col) = RAW(row+top_margin,col+left_margin);
  2719. }
  2720. #endif
  2721. if (mask[0][3]) goto mask_set;
  2722. if (load_raw == &CLASS canon_load_raw ||
  2723. load_raw == &CLASS lossless_jpeg_load_raw) {
  2724. mask[0][1] = mask[1][1] = 2;
  2725. mask[0][3] = -2;
  2726. goto sides;
  2727. }
  2728. if (load_raw == &CLASS canon_600_load_raw ||
  2729. load_raw == &CLASS sony_load_raw ||
  2730. (load_raw == &CLASS eight_bit_load_raw && strncmp(model,"DC2",3)) ||
  2731. load_raw == &CLASS kodak_262_load_raw ||
  2732. (load_raw == &CLASS packed_load_raw && (load_flags & 32))) {
  2733. sides:
  2734. mask[0][0] = mask[1][0] = top_margin;
  2735. mask[0][2] = mask[1][2] = top_margin+height;
  2736. mask[0][3] += left_margin;
  2737. mask[1][1] += left_margin+width;
  2738. mask[1][3] += raw_width;
  2739. }
  2740. if (load_raw == &CLASS nokia_load_raw) {
  2741. mask[0][2] = top_margin;
  2742. mask[0][3] = width;
  2743. }
  2744. mask_set:
  2745. memset (mblack, 0, sizeof mblack);
  2746. for (zero=m=0; m < 8; m++)
  2747. for (row=mask[m][0]; row < mask[m][2]; row++)
  2748. for (col=mask[m][1]; col < mask[m][3]; col++) {
  2749. c = FC(row-top_margin,col-left_margin);
  2750. mblack[c] += val = raw_image[(row)*raw_pitch/2+(col)];
  2751. mblack[4+c]++;
  2752. zero += !val;
  2753. }
  2754. if (load_raw == &CLASS canon_600_load_raw && width < raw_width) {
  2755. black = (mblack[0]+mblack[1]+mblack[2]+mblack[3]) /
  2756. (mblack[4]+mblack[5]+mblack[6]+mblack[7]) - 4;
  2757. #ifndef LIBRAW_LIBRARY_BUILD
  2758. canon_600_correct();
  2759. #endif
  2760. } else if (zero < mblack[4] && mblack[5] && mblack[6] && mblack[7])
  2761. FORC4 cblack[c] = mblack[c] / mblack[4+c];
  2762. }
  2763. void CLASS remove_zeroes()
  2764. {
  2765. unsigned row, col, tot, n, r, c;
  2766. #ifdef LIBRAW_LIBRARY_BUILD
  2767. RUN_CALLBACK(LIBRAW_PROGRESS_REMOVE_ZEROES,0,2);
  2768. #endif
  2769. for (row=0; row < height; row++)
  2770. for (col=0; col < width; col++)
  2771. if (BAYER(row,col) == 0) {
  2772. tot = n = 0;
  2773. for (r = row-2; r <= row+2; r++)
  2774. for (c = col-2; c <= col+2; c++)
  2775. if (r < height && c < width &&
  2776. FC(r,c) == FC(row,col) && BAYER(r,c))
  2777. tot += (n++,BAYER(r,c));
  2778. if (n) BAYER(row,col) = tot/n;
  2779. }
  2780. #ifdef LIBRAW_LIBRARY_BUILD
  2781. RUN_CALLBACK(LIBRAW_PROGRESS_REMOVE_ZEROES,1,2);
  2782. #endif
  2783. }
  2784. #line 4125 "dcraw/dcraw.c"
  2785. void CLASS gamma_curve (double pwr, double ts, int mode, int imax)
  2786. {
  2787. int i;
  2788. double g[6], bnd[2]={0,0}, r;
  2789. g[0] = pwr;
  2790. g[1] = ts;
  2791. g[2] = g[3] = g[4] = 0;
  2792. bnd[g[1] >= 1] = 1;
  2793. if (g[1] && (g[1]-1)*(g[0]-1) <= 0) {
  2794. for (i=0; i < 48; i++) {
  2795. g[2] = (bnd[0] + bnd[1])/2;
  2796. if (g[0]) bnd[(pow(g[2]/g[1],-g[0]) - 1)/g[0] - 1/g[2] > -1] = g[2];
  2797. else bnd[g[2]/exp(1-1/g[2]) < g[1]] = g[2];
  2798. }
  2799. g[3] = g[2] / g[1];
  2800. if (g[0]) g[4] = g[2] * (1/g[0] - 1);
  2801. }
  2802. if (g[0]) g[5] = 1 / (g[1]*SQR(g[3])/2 - g[4]*(1 - g[3]) +
  2803. (1 - pow(g[3],1+g[0]))*(1 + g[4])/(1 + g[0])) - 1;
  2804. else g[5] = 1 / (g[1]*SQR(g[3])/2 + 1
  2805. - g[2] - g[3] - g[2]*g[3]*(log(g[3]) - 1)) - 1;
  2806. if (!mode--) {
  2807. memcpy (gamm, g, sizeof gamm);
  2808. return;
  2809. }
  2810. for (i=0; i < 0x10000; i++) {
  2811. curve[i] = 0xffff;
  2812. if ((r = (double) i / imax) < 1)
  2813. curve[i] = 0x10000 * ( mode
  2814. ? (r < g[3] ? r*g[1] : (g[0] ? pow( r,g[0])*(1+g[4])-g[4] : log(r)*g[2]+1))
  2815. : (r < g[2] ? r/g[1] : (g[0] ? pow((r+g[4])/(1+g[4]),1/g[0]) : exp((r-1)/g[2]))));
  2816. }
  2817. }
  2818. void CLASS pseudoinverse (double (*in)[3], double (*out)[3], int size)
  2819. {
  2820. double work[3][6], num;
  2821. int i, j, k;
  2822. for (i=0; i < 3; i++) {
  2823. for (j=0; j < 6; j++)
  2824. work[i][j] = j == i+3;
  2825. for (j=0; j < 3; j++)
  2826. for (k=0; k < size; k++)
  2827. work[i][j] += in[k][i] * in[k][j];
  2828. }
  2829. for (i=0; i < 3; i++) {
  2830. num = work[i][i];
  2831. for (j=0; j < 6; j++)
  2832. work[i][j] /= num;
  2833. for (k=0; k < 3; k++) {
  2834. if (k==i) continue;
  2835. num = work[k][i];
  2836. for (j=0; j < 6; j++)
  2837. work[k][j] -= work[i][j] * num;
  2838. }
  2839. }
  2840. for (i=0; i < size; i++)
  2841. for (j=0; j < 3; j++)
  2842. for (out[i][j]=k=0; k < 3; k++)
  2843. out[i][j] += work[j][k+3] * in[i][k];
  2844. }
  2845. void CLASS cam_xyz_coeff (double cam_xyz[4][3])
  2846. {
  2847. double cam_rgb[4][3], inverse[4][3], num;
  2848. int i, j, k;
  2849. for (i=0; i < colors; i++) /* Multiply out XYZ colorspace */
  2850. for (j=0; j < 3; j++)
  2851. for (cam_rgb[i][j] = k=0; k < 3; k++)
  2852. cam_rgb[i][j] += cam_xyz[i][k] * xyz_rgb[k][j];
  2853. for (i=0; i < colors; i++) { /* Normalize cam_rgb so that */
  2854. for (num=j=0; j < 3; j++) /* cam_rgb * (1,1,1) is (1,1,1,1) */
  2855. num += cam_rgb[i][j];
  2856. for (j=0; j < 3; j++)
  2857. cam_rgb[i][j] /= num;
  2858. pre_mul[i] = 1 / num;
  2859. }
  2860. pseudoinverse (cam_rgb, inverse, colors);
  2861. for (raw_color = i=0; i < 3; i++)
  2862. for (j=0; j < colors; j++)
  2863. rgb_cam[i][j] = inverse[j][i];
  2864. }
  2865. #ifdef COLORCHECK
  2866. void CLASS colorcheck()
  2867. {
  2868. #define NSQ 24
  2869. // Coordinates of the GretagMacbeth ColorChecker squares
  2870. // width, height, 1st_column, 1st_row
  2871. int cut[NSQ][4]; // you must set these
  2872. // ColorChecker Chart under 6500-kelvin illumination
  2873. static const double gmb_xyY[NSQ][3] = {
  2874. { 0.400, 0.350, 10.1 }, // Dark Skin
  2875. { 0.377, 0.345, 35.8 }, // Light Skin
  2876. { 0.247, 0.251, 19.3 }, // Blue Sky
  2877. { 0.337, 0.422, 13.3 }, // Foliage
  2878. { 0.265, 0.240, 24.3 }, // Blue Flower
  2879. { 0.261, 0.343, 43.1 }, // Bluish Green
  2880. { 0.506, 0.407, 30.1 }, // Orange
  2881. { 0.211, 0.175, 12.0 }, // Purplish Blue
  2882. { 0.453, 0.306, 19.8 }, // Moderate Red
  2883. { 0.285, 0.202, 6.6 }, // Purple
  2884. { 0.380, 0.489, 44.3 }, // Yellow Green
  2885. { 0.473, 0.438, 43.1 }, // Orange Yellow
  2886. { 0.187, 0.129, 6.1 }, // Blue
  2887. { 0.305, 0.478, 23.4 }, // Green
  2888. { 0.539, 0.313, 12.0 }, // Red
  2889. { 0.448, 0.470, 59.1 }, // Yellow
  2890. { 0.364, 0.233, 19.8 }, // Magenta
  2891. { 0.196, 0.252, 19.8 }, // Cyan
  2892. { 0.310, 0.316, 90.0 }, // White
  2893. { 0.310, 0.316, 59.1 }, // Neutral 8
  2894. { 0.310, 0.316, 36.2 }, // Neutral 6.5
  2895. { 0.310, 0.316, 19.8 }, // Neutral 5
  2896. { 0.310, 0.316, 9.0 }, // Neutral 3.5
  2897. { 0.310, 0.316, 3.1 } }; // Black
  2898. double gmb_cam[NSQ][4], gmb_xyz[NSQ][3];
  2899. double inverse[NSQ][3], cam_xyz[4][3], num;
  2900. int c, i, j, k, sq, row, col, count[4];
  2901. memset (gmb_cam, 0, sizeof gmb_cam);
  2902. for (sq=0; sq < NSQ; sq++) {
  2903. FORCC count[c] = 0;
  2904. for (row=cut[sq][3]; row < cut[sq][3]+cut[sq][1]; row++)
  2905. for (col=cut[sq][2]; col < cut[sq][2]+cut[sq][0]; col++) {
  2906. c = FC(row,col);
  2907. if (c >= colors) c -= 2;
  2908. gmb_cam[sq][c] += BAYER(row,col);
  2909. count[c]++;
  2910. }
  2911. FORCC gmb_cam[sq][c] = gmb_cam[sq][c]/count[c] - black;
  2912. gmb_xyz[sq][0] = gmb_xyY[sq][2] * gmb_xyY[sq][0] / gmb_xyY[sq][1];
  2913. gmb_xyz[sq][1] = gmb_xyY[sq][2];
  2914. gmb_xyz[sq][2] = gmb_xyY[sq][2] *
  2915. (1 - gmb_xyY[sq][0] - gmb_xyY[sq][1]) / gmb_xyY[sq][1];
  2916. }
  2917. pseudoinverse (gmb_xyz, inverse, NSQ);
  2918. for (i=0; i < colors; i++)
  2919. for (j=0; j < 3; j++)
  2920. for (cam_xyz[i][j] = k=0; k < NSQ; k++)
  2921. cam_xyz[i][j] += gmb_cam[k][i] * inverse[k][j];
  2922. cam_xyz_coeff (cam_xyz);
  2923. if (verbose) {
  2924. printf (" { \"%s %s\", %d,\n\t{", make, model, black);
  2925. num = 10000 / (cam_xyz[1][0] + cam_xyz[1][1] + cam_xyz[1][2]);
  2926. FORCC for (j=0; j < 3; j++)
  2927. printf ("%c%d", (c | j) ? ',':' ', (int) (cam_xyz[c][j] * num + 0.5));
  2928. puts (" } },");
  2929. }
  2930. #undef NSQ
  2931. }
  2932. #endif
  2933. void CLASS hat_transform (float *temp, float *base, int st, int size, int sc)
  2934. {
  2935. int i;
  2936. for (i=0; i < sc; i++)
  2937. temp[i] = 2*base[st*i] + base[st*(sc-i)] + base[st*(i+sc)];
  2938. for (; i+sc < size; i++)
  2939. temp[i] = 2*base[st*i] + base[st*(i-sc)] + base[st*(i+sc)];
  2940. for (; i < size; i++)
  2941. temp[i] = 2*base[st*i] + base[st*(i-sc)] + base[st*(2*size-2-(i+sc))];
  2942. }
  2943. #if !defined(LIBRAW_USE_OPENMP)
  2944. void CLASS wavelet_denoise()
  2945. {
  2946. float *fimg=0, *temp, thold, mul[2], avg, diff;
  2947. int scale=1, size, lev, hpass, lpass, row, col, nc, c, i, wlast, blk[2];
  2948. ushort *window[4];
  2949. static const float noise[] =
  2950. { 0.8002,0.2735,0.1202,0.0585,0.0291,0.0152,0.0080,0.0044 };
  2951. #ifdef DCRAW_VERBOSE
  2952. if (verbose) fprintf (stderr,_("Wavelet denoising...\n"));
  2953. #endif
  2954. while (maximum << scale < 0x10000) scale++;
  2955. maximum <<= --scale;
  2956. black <<= scale;
  2957. FORC4 cblack[c] <<= scale;
  2958. if ((size = iheight*iwidth) < 0x15550000)
  2959. fimg = (float *) malloc ((size*3 + iheight + iwidth) * sizeof *fimg);
  2960. merror (fimg, "wavelet_denoise()");
  2961. temp = fimg + size*3;
  2962. if ((nc = colors) == 3 && filters) nc++;
  2963. FORC(nc) { /* denoise R,G1,B,G3 individually */
  2964. for (i=0; i < size; i++)
  2965. fimg[i] = 256 * sqrt((double)(image[i][c] << scale));
  2966. for (hpass=lev=0; lev < 5; lev++) {
  2967. lpass = size*((lev & 1)+1);
  2968. for (row=0; row < iheight; row++) {
  2969. hat_transform (temp, fimg+hpass+row*iwidth, 1, iwidth, 1 << lev);
  2970. for (col=0; col < iwidth; col++)
  2971. fimg[lpass + row*iwidth + col] = temp[col] * 0.25;
  2972. }
  2973. for (col=0; col < iwidth; col++) {
  2974. hat_transform (temp, fimg+lpass+col, iwidth, iheight, 1 << lev);
  2975. for (row=0; row < iheight; row++)
  2976. fimg[lpass + row*iwidth + col] = temp[row] * 0.25;
  2977. }
  2978. thold = threshold * noise[lev];
  2979. for (i=0; i < size; i++) {
  2980. fimg[hpass+i] -= fimg[lpass+i];
  2981. if (fimg[hpass+i] < -thold) fimg[hpass+i] += thold;
  2982. else if (fimg[hpass+i] > thold) fimg[hpass+i] -= thold;
  2983. else fimg[hpass+i] = 0;
  2984. if (hpass) fimg[i] += fimg[hpass+i];
  2985. }
  2986. hpass = lpass;
  2987. }
  2988. for (i=0; i < size; i++)
  2989. image[i][c] = CLIP(SQR(fimg[i]+fimg[lpass+i])/0x10000);
  2990. }
  2991. if (filters && colors == 3) { /* pull G1 and G3 closer together */
  2992. for (row=0; row < 2; row++) {
  2993. mul[row] = 0.125 * pre_mul[FC(row+1,0) | 1] / pre_mul[FC(row,0) | 1];
  2994. blk[row] = cblack[FC(row,0) | 1];
  2995. }
  2996. for (i=0; i < 4; i++)
  2997. window[i] = (ushort *) fimg + width*i;
  2998. for (wlast=-1, row=1; row < height-1; row++) {
  2999. while (wlast < row+1) {
  3000. for (wlast++, i=0; i < 4; i++)
  3001. window[(i+3) & 3] = window[i];
  3002. for (col = FC(wlast,1) & 1; col < width; col+=2)
  3003. window[2][col] = BAYER(wlast,col);
  3004. }
  3005. thold = threshold/512;
  3006. for (col = (FC(row,0) & 1)+1; col < width-1; col+=2) {
  3007. avg = ( window[0][col-1] + window[0][col+1] +
  3008. window[2][col-1] + window[2][col+1] - blk[~row & 1]*4 )
  3009. * mul[row & 1] + (window[1][col] + blk[row & 1]) * 0.5;
  3010. avg = avg < 0 ? 0 : sqrt(avg);
  3011. diff = sqrt((double)BAYER(row,col)) - avg;
  3012. if (diff < -thold) diff += thold;
  3013. else if (diff > thold) diff -= thold;
  3014. else diff = 0;
  3015. BAYER(row,col) = CLIP(SQR(avg+diff) + 0.5);
  3016. }
  3017. }
  3018. }
  3019. free (fimg);
  3020. }
  3021. #else /* LIBRAW_USE_OPENMP */
  3022. void CLASS wavelet_denoise()
  3023. {
  3024. float *fimg=0, *temp, thold, mul[2], avg, diff;
  3025. int scale=1, size, lev, hpass, lpass, row, col, nc, c, i, wlast, blk[2];
  3026. ushort *window[4];
  3027. static const float noise[] =
  3028. { 0.8002,0.2735,0.1202,0.0585,0.0291,0.0152,0.0080,0.0044 };
  3029. #ifdef DCRAW_VERBOSE
  3030. if (verbose) fprintf (stderr,_("Wavelet denoising...\n"));
  3031. #endif
  3032. while (maximum << scale < 0x10000) scale++;
  3033. maximum <<= --scale;
  3034. black <<= scale;
  3035. FORC4 cblack[c] <<= scale;
  3036. if ((size = iheight*iwidth) < 0x15550000)
  3037. fimg = (float *) malloc ((size*3 + iheight + iwidth) * sizeof *fimg);
  3038. merror (fimg, "wavelet_denoise()");
  3039. temp = fimg + size*3;
  3040. if ((nc = colors) == 3 && filters) nc++;
  3041. #ifdef LIBRAW_LIBRARY_BUILD
  3042. #pragma omp parallel default(shared) private(i,col,row,thold,lev,lpass,hpass,temp,c) firstprivate(scale,size)
  3043. #endif
  3044. {
  3045. temp = (float*)malloc( (iheight + iwidth) * sizeof *fimg);
  3046. FORC(nc) { /* denoise R,G1,B,G3 individually */
  3047. #ifdef LIBRAW_LIBRARY_BUILD
  3048. #pragma omp for
  3049. #endif
  3050. for (i=0; i < size; i++)
  3051. fimg[i] = 256 * sqrt((double)(image[i][c] << scale));
  3052. for (hpass=lev=0; lev < 5; lev++) {
  3053. lpass = size*((lev & 1)+1);
  3054. #ifdef LIBRAW_LIBRARY_BUILD
  3055. #pragma omp for
  3056. #endif
  3057. for (row=0; row < iheight; row++) {
  3058. hat_transform (temp, fimg+hpass+row*iwidth, 1, iwidth, 1 << lev);
  3059. for (col=0; col < iwidth; col++)
  3060. fimg[lpass + row*iwidth + col] = temp[col] * 0.25;
  3061. }
  3062. #ifdef LIBRAW_LIBRARY_BUILD
  3063. #pragma omp for
  3064. #endif
  3065. for (col=0; col < iwidth; col++) {
  3066. hat_transform (temp, fimg+lpass+col, iwidth, iheight, 1 << lev);
  3067. for (row=0; row < iheight; row++)
  3068. fimg[lpass + row*iwidth + col] = temp[row] * 0.25;
  3069. }
  3070. thold = threshold * noise[lev];
  3071. #ifdef LIBRAW_LIBRARY_BUILD
  3072. #pragma omp for
  3073. #endif
  3074. for (i=0; i < size; i++) {
  3075. fimg[hpass+i] -= fimg[lpass+i];
  3076. if (fimg[hpass+i] < -thold) fimg[hpass+i] += thold;
  3077. else if (fimg[hpass+i] > thold) fimg[hpass+i] -= thold;
  3078. else fimg[hpass+i] = 0;
  3079. if (hpass) fimg[i] += fimg[hpass+i];
  3080. }
  3081. hpass = lpass;
  3082. }
  3083. #ifdef LIBRAW_LIBRARY_BUILD
  3084. #pragma omp for
  3085. #endif
  3086. for (i=0; i < size; i++)
  3087. image[i][c] = CLIP(SQR(fimg[i]+fimg[lpass+i])/0x10000);
  3088. }
  3089. free(temp);
  3090. } /* end omp parallel */
  3091. /* the following loops are hard to parallize, no idea yes,
  3092. * problem is wlast which is carrying dependency
  3093. * second part should be easyer, but did not yet get it right.
  3094. */
  3095. if (filters && colors == 3) { /* pull G1 and G3 closer together */
  3096. for (row=0; row < 2; row++){
  3097. mul[row] = 0.125 * pre_mul[FC(row+1,0) | 1] / pre_mul[FC(row,0) | 1];
  3098. blk[row] = cblack[FC(row,0) | 1];
  3099. }
  3100. for (i=0; i < 4; i++)
  3101. window[i] = (ushort *) fimg + width*i;
  3102. for (wlast=-1, row=1; row < height-1; row++) {
  3103. while (wlast < row+1) {
  3104. for (wlast++, i=0; i < 4; i++)
  3105. window[(i+3) & 3] = window[i];
  3106. for (col = FC(wlast,1) & 1; col < width; col+=2)
  3107. window[2][col] = BAYER(wlast,col);
  3108. }
  3109. thold = threshold/512;
  3110. for (col = (FC(row,0) & 1)+1; col < width-1; col+=2) {
  3111. avg = ( window[0][col-1] + window[0][col+1] +
  3112. window[2][col-1] + window[2][col+1] - blk[~row & 1]*4 )
  3113. * mul[row & 1] + (window[1][col] + blk[row & 1]) * 0.5;
  3114. avg = avg < 0 ? 0 : sqrt(avg);
  3115. diff = sqrt((double)BAYER(row,col)) - avg;
  3116. if (diff < -thold) diff += thold;
  3117. else if (diff > thold) diff -= thold;
  3118. else diff = 0;
  3119. BAYER(row,col) = CLIP(SQR(avg+diff) + 0.5);
  3120. }
  3121. }
  3122. }
  3123. free (fimg);
  3124. }
  3125. #endif
  3126. // green equilibration
  3127. void CLASS green_matching()
  3128. {
  3129. int i,j;
  3130. double m1,m2,c1,c2;
  3131. int o1_1,o1_2,o1_3,o1_4;
  3132. int o2_1,o2_2,o2_3,o2_4;
  3133. ushort (*img)[4];
  3134. const int margin = 3;
  3135. int oj = 2, oi = 2;
  3136. float f;
  3137. const float thr = 0.01f;
  3138. if(half_size || shrink) return;
  3139. if(FC(oj, oi) != 3) oj++;
  3140. if(FC(oj, oi) != 3) oi++;
  3141. if(FC(oj, oi) != 3) oj--;
  3142. img = (ushort (*)[4]) calloc (height*width, sizeof *image);
  3143. merror (img, "green_matching()");
  3144. memcpy(img,image,height*width*sizeof *image);
  3145. for(j=oj;j<height-margin;j+=2)
  3146. for(i=oi;i<width-margin;i+=2){
  3147. o1_1=img[(j-1)*width+i-1][1];
  3148. o1_2=img[(j-1)*width+i+1][1];
  3149. o1_3=img[(j+1)*width+i-1][1];
  3150. o1_4=img[(j+1)*width+i+1][1];
  3151. o2_1=img[(j-2)*width+i][3];
  3152. o2_2=img[(j+2)*width+i][3];
  3153. o2_3=img[j*width+i-2][3];
  3154. o2_4=img[j*width+i+2][3];
  3155. m1=(o1_1+o1_2+o1_3+o1_4)/4.0;
  3156. m2=(o2_1+o2_2+o2_3+o2_4)/4.0;
  3157. 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;
  3158. 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;
  3159. if((img[j*width+i][3]<maximum*0.95)&&(c1<maximum*thr)&&(c2<maximum*thr))
  3160. {
  3161. f = image[j*width+i][3]*m1/m2;
  3162. image[j*width+i][3]=f>0xffff?0xffff:f;
  3163. }
  3164. }
  3165. free(img);
  3166. }
  3167. void CLASS scale_colors()
  3168. {
  3169. unsigned bottom, right, size, row, col, ur, uc, i, x, y, c, sum[8];
  3170. int val, dark, sat;
  3171. double dsum[8], dmin, dmax;
  3172. float scale_mul[4], fr, fc;
  3173. ushort *img=0, *pix;
  3174. #ifdef LIBRAW_LIBRARY_BUILD
  3175. RUN_CALLBACK(LIBRAW_PROGRESS_SCALE_COLORS,0,2);
  3176. #endif
  3177. if (user_mul[0])
  3178. memcpy (pre_mul, user_mul, sizeof pre_mul);
  3179. if (use_auto_wb || (use_camera_wb && cam_mul[0] == -1)) {
  3180. memset (dsum, 0, sizeof dsum);
  3181. bottom = MIN (greybox[1]+greybox[3], height);
  3182. right = MIN (greybox[0]+greybox[2], width);
  3183. for (row=greybox[1]; row < bottom; row += 8)
  3184. for (col=greybox[0]; col < right; col += 8) {
  3185. memset (sum, 0, sizeof sum);
  3186. for (y=row; y < row+8 && y < bottom; y++)
  3187. for (x=col; x < col+8 && x < right; x++)
  3188. FORC4 {
  3189. if (filters) {
  3190. c = fcol(y,x);
  3191. val = BAYER2(y,x);
  3192. } else
  3193. val = image[y*width+x][c];
  3194. if (val > maximum-25) goto skip_block;
  3195. if ((val -= cblack[c]) < 0) val = 0;
  3196. sum[c] += val;
  3197. sum[c+4]++;
  3198. if (filters) break;
  3199. }
  3200. FORC(8) dsum[c] += sum[c];
  3201. skip_block: ;
  3202. }
  3203. FORC4 if (dsum[c]) pre_mul[c] = dsum[c+4] / dsum[c];
  3204. }
  3205. if (use_camera_wb && cam_mul[0] != -1) {
  3206. memset (sum, 0, sizeof sum);
  3207. for (row=0; row < 8; row++)
  3208. for (col=0; col < 8; col++) {
  3209. c = FC(row,col);
  3210. if ((val = white[row][col] - cblack[c]) > 0)
  3211. sum[c] += val;
  3212. sum[c+4]++;
  3213. }
  3214. if (sum[0] && sum[1] && sum[2] && sum[3])
  3215. FORC4 pre_mul[c] = (float) sum[c+4] / sum[c];
  3216. else if (cam_mul[0] && cam_mul[2])
  3217. memcpy (pre_mul, cam_mul, sizeof pre_mul);
  3218. else
  3219. {
  3220. #ifdef LIBRAW_LIBRARY_BUILD
  3221. imgdata.process_warnings |= LIBRAW_WARN_BAD_CAMERA_WB;
  3222. #endif
  3223. #ifdef DCRAW_VERBOSE
  3224. fprintf (stderr,_("%s: Cannot use camera white balance.\n"), ifname);
  3225. #endif
  3226. }
  3227. }
  3228. if (pre_mul[3] == 0) pre_mul[3] = colors < 4 ? pre_mul[1] : 1;
  3229. dark = black;
  3230. sat = maximum;
  3231. if (threshold) wavelet_denoise();
  3232. maximum -= black;
  3233. for (dmin=DBL_MAX, dmax=c=0; c < 4; c++) {
  3234. if (dmin > pre_mul[c])
  3235. dmin = pre_mul[c];
  3236. if (dmax < pre_mul[c])
  3237. dmax = pre_mul[c];
  3238. }
  3239. if (!highlight) dmax = dmin;
  3240. FORC4 scale_mul[c] = (pre_mul[c] /= dmax) * 65535.0 / maximum;
  3241. #ifdef DCRAW_VERBOSE
  3242. if (verbose) {
  3243. fprintf (stderr,
  3244. _("Scaling with darkness %d, saturation %d, and\nmultipliers"), dark, sat);
  3245. FORC4 fprintf (stderr, " %f", pre_mul[c]);
  3246. fputc ('\n', stderr);
  3247. }
  3248. #endif
  3249. size = iheight*iwidth;
  3250. #ifdef LIBRAW_LIBRARY_BUILD
  3251. scale_colors_loop(scale_mul);
  3252. #else
  3253. for (i=0; i < size*4; i++) {
  3254. val = image[0][i];
  3255. if (!val) continue;
  3256. val -= cblack[i & 3];
  3257. val *= scale_mul[i & 3];
  3258. image[0][i] = CLIP(val);
  3259. }
  3260. #endif
  3261. if ((aber[0] != 1 || aber[2] != 1) && colors == 3) {
  3262. #ifdef DCRAW_VERBOSE
  3263. if (verbose)
  3264. fprintf (stderr,_("Correcting chromatic aberration...\n"));
  3265. #endif
  3266. for (c=0; c < 4; c+=2) {
  3267. if (aber[c] == 1) continue;
  3268. img = (ushort *) malloc (size * sizeof *img);
  3269. merror (img, "scale_colors()");
  3270. for (i=0; i < size; i++)
  3271. img[i] = image[i][c];
  3272. for (row=0; row < iheight; row++) {
  3273. ur = fr = (row - iheight*0.5) * aber[c] + iheight*0.5;
  3274. if (ur > iheight-2) continue;
  3275. fr -= ur;
  3276. for (col=0; col < iwidth; col++) {
  3277. uc = fc = (col - iwidth*0.5) * aber[c] + iwidth*0.5;
  3278. if (uc > iwidth-2) continue;
  3279. fc -= uc;
  3280. pix = img + ur*iwidth + uc;
  3281. image[row*iwidth+col][c] =
  3282. (pix[ 0]*(1-fc) + pix[ 1]*fc) * (1-fr) +
  3283. (pix[iwidth]*(1-fc) + pix[iwidth+1]*fc) * fr;
  3284. }
  3285. }
  3286. free(img);
  3287. }
  3288. }
  3289. #ifdef LIBRAW_LIBRARY_BUILD
  3290. RUN_CALLBACK(LIBRAW_PROGRESS_SCALE_COLORS,1,2);
  3291. #endif
  3292. }
  3293. void CLASS pre_interpolate()
  3294. {
  3295. ushort (*img)[4];
  3296. int row, col, c;
  3297. #ifdef LIBRAW_LIBRARY_BUILD
  3298. RUN_CALLBACK(LIBRAW_PROGRESS_PRE_INTERPOLATE,0,2);
  3299. #endif
  3300. if (shrink) {
  3301. if (half_size) {
  3302. height = iheight;
  3303. width = iwidth;
  3304. } else {
  3305. img = (ushort (*)[4]) calloc (height*width, sizeof *img);
  3306. merror (img, "pre_interpolate()");
  3307. for (row=0; row < height; row++)
  3308. for (col=0; col < width; col++) {
  3309. c = fcol(row,col);
  3310. img[row*width+col][c] = image[(row >> 1)*iwidth+(col >> 1)][c];
  3311. }
  3312. free (image);
  3313. image = img;
  3314. shrink = 0;
  3315. }
  3316. }
  3317. if (filters > 1000 && colors == 3) {
  3318. if (four_color_rgb && colors++)
  3319. mix_green = !half_size;
  3320. else {
  3321. for (row = FC(1,0) >> 1; row < height; row+=2)
  3322. for (col = FC(row,1) & 1; col < width; col+=2)
  3323. image[row*width+col][1] = image[row*width+col][3];
  3324. filters &= ~((filters & 0x55555555) << 1);
  3325. }
  3326. }
  3327. if (half_size) filters = 0;
  3328. #ifdef LIBRAW_LIBRARY_BUILD
  3329. RUN_CALLBACK(LIBRAW_PROGRESS_PRE_INTERPOLATE,1,2);
  3330. #endif
  3331. }
  3332. void CLASS border_interpolate (int border)
  3333. {
  3334. unsigned row, col, y, x, f, c, sum[8];
  3335. for (row=0; row < height; row++)
  3336. for (col=0; col < width; col++) {
  3337. if (col==border && row >= border && row < height-border)
  3338. col = width-border;
  3339. memset (sum, 0, sizeof sum);
  3340. for (y=row-1; y != row+2; y++)
  3341. for (x=col-1; x != col+2; x++)
  3342. if (y < height && x < width) {
  3343. f = fcol(y,x);
  3344. sum[f] += image[y*width+x][f];
  3345. sum[f+4]++;
  3346. }
  3347. f = fcol(row,col);
  3348. FORCC if (c != f && sum[c+4])
  3349. image[row*width+col][c] = sum[c] / sum[c+4];
  3350. }
  3351. }
  3352. void CLASS lin_interpolate_loop(int code[16][16][32],int size)
  3353. {
  3354. int row;
  3355. for (row=1; row < height-1; row++)
  3356. {
  3357. int col,*ip;
  3358. ushort *pix;
  3359. for (col=1; col < width-1; col++) {
  3360. int i;
  3361. int sum[4];
  3362. pix = image[row*width+col];
  3363. ip = code[row % size][col % size];
  3364. memset (sum, 0, sizeof sum);
  3365. for (i=*ip++; i--; ip+=3)
  3366. sum[ip[2]] += pix[ip[0]] << ip[1];
  3367. for (i=colors; --i; ip+=2)
  3368. pix[ip[0]] = sum[ip[0]] * ip[1] >> 8;
  3369. }
  3370. }
  3371. }
  3372. void CLASS lin_interpolate()
  3373. {
  3374. int code[16][16][32], size=16, *ip, sum[4];
  3375. int f, c, x, y, row, col, shift, color;
  3376. #ifdef DCRAW_VERBOSE
  3377. if (verbose) fprintf (stderr,_("Bilinear interpolation...\n"));
  3378. #endif
  3379. #ifdef LIBRAW_LIBRARY_BUILD
  3380. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,0,3);
  3381. #endif
  3382. if (filters == 2) size = 6;
  3383. border_interpolate(1);
  3384. for (row=0; row < size; row++)
  3385. for (col=0; col < size; col++) {
  3386. ip = code[row][col]+1;
  3387. f = fcol(row,col);
  3388. memset (sum, 0, sizeof sum);
  3389. for (y=-1; y <= 1; y++)
  3390. for (x=-1; x <= 1; x++) {
  3391. shift = (y==0) + (x==0);
  3392. color = fcol(row+y,col+x);
  3393. if (color == f) continue;
  3394. *ip++ = (width*y + x)*4 + color;
  3395. *ip++ = shift;
  3396. *ip++ = color;
  3397. sum[color] += 1 << shift;
  3398. }
  3399. code[row][col][0] = (ip - code[row][col]) / 3;
  3400. FORCC
  3401. if (c != f) {
  3402. *ip++ = c;
  3403. *ip++ = 256 / sum[c];
  3404. }
  3405. }
  3406. #ifdef LIBRAW_LIBRARY_BUILD
  3407. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,1,3);
  3408. #endif
  3409. lin_interpolate_loop(code,size);
  3410. #ifdef LIBRAW_LIBRARY_BUILD
  3411. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,2,3);
  3412. #endif
  3413. }
  3414. /*
  3415. This algorithm is officially called:
  3416. "Interpolation using a Threshold-based variable number of gradients"
  3417. described in http://scien.stanford.edu/pages/labsite/1999/psych221/projects/99/tingchen/algodep/vargra.html
  3418. I've extended the basic idea to work with non-Bayer filter arrays.
  3419. Gradients are numbered clockwise from NW=0 to W=7.
  3420. */
  3421. void CLASS vng_interpolate()
  3422. {
  3423. static const signed char *cp, terms[] = {
  3424. -2,-2,+0,-1,0,0x01, -2,-2,+0,+0,1,0x01, -2,-1,-1,+0,0,0x01,
  3425. -2,-1,+0,-1,0,0x02, -2,-1,+0,+0,0,0x03, -2,-1,+0,+1,1,0x01,
  3426. -2,+0,+0,-1,0,0x06, -2,+0,+0,+0,1,0x02, -2,+0,+0,+1,0,0x03,
  3427. -2,+1,-1,+0,0,0x04, -2,+1,+0,-1,1,0x04, -2,+1,+0,+0,0,0x06,
  3428. -2,+1,+0,+1,0,0x02, -2,+2,+0,+0,1,0x04, -2,+2,+0,+1,0,0x04,
  3429. -1,-2,-1,+0,0,0x80, -1,-2,+0,-1,0,0x01, -1,-2,+1,-1,0,0x01,
  3430. -1,-2,+1,+0,1,0x01, -1,-1,-1,+1,0,0x88, -1,-1,+1,-2,0,0x40,
  3431. -1,-1,+1,-1,0,0x22, -1,-1,+1,+0,0,0x33, -1,-1,+1,+1,1,0x11,
  3432. -1,+0,-1,+2,0,0x08, -1,+0,+0,-1,0,0x44, -1,+0,+0,+1,0,0x11,
  3433. -1,+0,+1,-2,1,0x40, -1,+0,+1,-1,0,0x66, -1,+0,+1,+0,1,0x22,
  3434. -1,+0,+1,+1,0,0x33, -1,+0,+1,+2,1,0x10, -1,+1,+1,-1,1,0x44,
  3435. -1,+1,+1,+0,0,0x66, -1,+1,+1,+1,0,0x22, -1,+1,+1,+2,0,0x10,
  3436. -1,+2,+0,+1,0,0x04, -1,+2,+1,+0,1,0x04, -1,+2,+1,+1,0,0x04,
  3437. +0,-2,+0,+0,1,0x80, +0,-1,+0,+1,1,0x88, +0,-1,+1,-2,0,0x40,
  3438. +0,-1,+1,+0,0,0x11, +0,-1,+2,-2,0,0x40, +0,-1,+2,-1,0,0x20,
  3439. +0,-1,+2,+0,0,0x30, +0,-1,+2,+1,1,0x10, +0,+0,+0,+2,1,0x08,
  3440. +0,+0,+2,-2,1,0x40, +0,+0,+2,-1,0,0x60, +0,+0,+2,+0,1,0x20,
  3441. +0,+0,+2,+1,0,0x30, +0,+0,+2,+2,1,0x10, +0,+1,+1,+0,0,0x44,
  3442. +0,+1,+1,+2,0,0x10, +0,+1,+2,-1,1,0x40, +0,+1,+2,+0,0,0x60,
  3443. +0,+1,+2,+1,0,0x20, +0,+1,+2,+2,0,0x10, +1,-2,+1,+0,0,0x80,
  3444. +1,-1,+1,+1,0,0x88, +1,+0,+1,+2,0,0x08, +1,+0,+2,-1,0,0x40,
  3445. +1,+0,+2,+1,0,0x10
  3446. }, chood[] = { -1,-1, -1,0, -1,+1, 0,+1, +1,+1, +1,0, +1,-1, 0,-1 };
  3447. ushort (*brow[5])[4], *pix;
  3448. int prow=8, pcol=2, *ip, *code[16][16], gval[8], gmin, gmax, sum[4];
  3449. int row, col, x, y, x1, x2, y1, y2, t, weight, grads, color, diag;
  3450. int g, diff, thold, num, c;
  3451. lin_interpolate();
  3452. #ifdef DCRAW_VERBOSE
  3453. if (verbose) fprintf (stderr,_("VNG interpolation...\n"));
  3454. #endif
  3455. if (filters == 1) prow = pcol = 16;
  3456. if (filters == 2) prow = pcol = 6;
  3457. ip = (int *) calloc (prow*pcol, 1280);
  3458. merror (ip, "vng_interpolate()");
  3459. for (row=0; row < prow; row++) /* Precalculate for VNG */
  3460. for (col=0; col < pcol; col++) {
  3461. code[row][col] = ip;
  3462. for (cp=terms, t=0; t < 64; t++) {
  3463. y1 = *cp++; x1 = *cp++;
  3464. y2 = *cp++; x2 = *cp++;
  3465. weight = *cp++;
  3466. grads = *cp++;
  3467. color = fcol(row+y1,col+x1);
  3468. if (fcol(row+y2,col+x2) != color) continue;
  3469. diag = (fcol(row,col+1) == color && fcol(row+1,col) == color) ? 2:1;
  3470. if (abs(y1-y2) == diag && abs(x1-x2) == diag) continue;
  3471. *ip++ = (y1*width + x1)*4 + color;
  3472. *ip++ = (y2*width + x2)*4 + color;
  3473. *ip++ = weight;
  3474. for (g=0; g < 8; g++)
  3475. if (grads & 1<<g) *ip++ = g;
  3476. *ip++ = -1;
  3477. }
  3478. *ip++ = INT_MAX;
  3479. for (cp=chood, g=0; g < 8; g++) {
  3480. y = *cp++; x = *cp++;
  3481. *ip++ = (y*width + x) * 4;
  3482. color = fcol(row,col);
  3483. if (fcol(row+y,col+x) != color && fcol(row+y*2,col+x*2) == color)
  3484. *ip++ = (y*width + x) * 8 + color;
  3485. else
  3486. *ip++ = 0;
  3487. }
  3488. }
  3489. brow[4] = (ushort (*)[4]) calloc (width*3, sizeof **brow);
  3490. merror (brow[4], "vng_interpolate()");
  3491. for (row=0; row < 3; row++)
  3492. brow[row] = brow[4] + row*width;
  3493. for (row=2; row < height-2; row++) { /* Do VNG interpolation */
  3494. #ifdef LIBRAW_LIBRARY_BUILD
  3495. if(!((row-2)%256))RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,(row-2)/256+1,((height-3)/256)+1);
  3496. #endif
  3497. for (col=2; col < width-2; col++) {
  3498. pix = image[row*width+col];
  3499. ip = code[row % prow][col % pcol];
  3500. memset (gval, 0, sizeof gval);
  3501. while ((g = ip[0]) != INT_MAX) { /* Calculate gradients */
  3502. diff = ABS(pix[g] - pix[ip[1]]) << ip[2];
  3503. gval[ip[3]] += diff;
  3504. ip += 5;
  3505. if ((g = ip[-1]) == -1) continue;
  3506. gval[g] += diff;
  3507. while ((g = *ip++) != -1)
  3508. gval[g] += diff;
  3509. }
  3510. ip++;
  3511. gmin = gmax = gval[0]; /* Choose a threshold */
  3512. for (g=1; g < 8; g++) {
  3513. if (gmin > gval[g]) gmin = gval[g];
  3514. if (gmax < gval[g]) gmax = gval[g];
  3515. }
  3516. if (gmax == 0) {
  3517. memcpy (brow[2][col], pix, sizeof *image);
  3518. continue;
  3519. }
  3520. thold = gmin + (gmax >> 1);
  3521. memset (sum, 0, sizeof sum);
  3522. color = fcol(row,col);
  3523. for (num=g=0; g < 8; g++,ip+=2) { /* Average the neighbors */
  3524. if (gval[g] <= thold) {
  3525. FORCC
  3526. if (c == color && ip[1])
  3527. sum[c] += (pix[c] + pix[ip[1]]) >> 1;
  3528. else
  3529. sum[c] += pix[ip[0] + c];
  3530. num++;
  3531. }
  3532. }
  3533. FORCC { /* Save to buffer */
  3534. t = pix[color];
  3535. if (c != color)
  3536. t += (sum[c] - sum[color]) / num;
  3537. brow[2][col][c] = CLIP(t);
  3538. }
  3539. }
  3540. if (row > 3) /* Write buffer to image */
  3541. memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image);
  3542. for (g=0; g < 4; g++)
  3543. brow[(g-1) & 3] = brow[g];
  3544. }
  3545. memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image);
  3546. memcpy (image[(row-1)*width+2], brow[1]+2, (width-4)*sizeof *image);
  3547. free (brow[4]);
  3548. free (code[0][0]);
  3549. }
  3550. /*
  3551. Patterned Pixel Grouping Interpolation by Alain Desbiolles
  3552. */
  3553. void CLASS ppg_interpolate()
  3554. {
  3555. int dir[5] = { 1, width, -1, -width, 1 };
  3556. int row, col, diff[2], guess[2], c, d, i;
  3557. ushort (*pix)[4];
  3558. border_interpolate(3);
  3559. #ifdef DCRAW_VERBOSE
  3560. if (verbose) fprintf (stderr,_("PPG interpolation...\n"));
  3561. #endif
  3562. /* Fill in the green layer with gradients and pattern recognition: */
  3563. #ifdef LIBRAW_LIBRARY_BUILD
  3564. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,0,3);
  3565. #ifdef LIBRAW_USE_OPENMP
  3566. #pragma omp parallel for default(shared) private(guess, diff, row, col, d, c, i, pix) schedule(static)
  3567. #endif
  3568. #endif
  3569. for (row=3; row < height-3; row++)
  3570. for (col=3+(FC(row,3) & 1), c=FC(row,col); col < width-3; col+=2) {
  3571. pix = image + row*width+col;
  3572. for (i=0; (d=dir[i]) > 0; i++) {
  3573. guess[i] = (pix[-d][1] + pix[0][c] + pix[d][1]) * 2
  3574. - pix[-2*d][c] - pix[2*d][c];
  3575. diff[i] = ( ABS(pix[-2*d][c] - pix[ 0][c]) +
  3576. ABS(pix[ 2*d][c] - pix[ 0][c]) +
  3577. ABS(pix[ -d][1] - pix[ d][1]) ) * 3 +
  3578. ( ABS(pix[ 3*d][1] - pix[ d][1]) +
  3579. ABS(pix[-3*d][1] - pix[-d][1]) ) * 2;
  3580. }
  3581. d = dir[i = diff[0] > diff[1]];
  3582. pix[0][1] = ULIM(guess[i] >> 2, pix[d][1], pix[-d][1]);
  3583. }
  3584. /* Calculate red and blue for each green pixel: */
  3585. #ifdef LIBRAW_LIBRARY_BUILD
  3586. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,1,3);
  3587. #ifdef LIBRAW_USE_OPENMP
  3588. #pragma omp parallel for default(shared) private(guess, diff, row, col, d, c, i, pix) schedule(static)
  3589. #endif
  3590. #endif
  3591. for (row=1; row < height-1; row++)
  3592. for (col=1+(FC(row,2) & 1), c=FC(row,col+1); col < width-1; col+=2) {
  3593. pix = image + row*width+col;
  3594. for (i=0; (d=dir[i]) > 0; c=2-c, i++)
  3595. pix[0][c] = CLIP((pix[-d][c] + pix[d][c] + 2*pix[0][1]
  3596. - pix[-d][1] - pix[d][1]) >> 1);
  3597. }
  3598. /* Calculate blue for red pixels and vice versa: */
  3599. #ifdef LIBRAW_LIBRARY_BUILD
  3600. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,2,3);
  3601. #ifdef LIBRAW_USE_OPENMP
  3602. #pragma omp parallel for default(shared) private(guess, diff, row, col, d, c, i, pix) schedule(static)
  3603. #endif
  3604. #endif
  3605. for (row=1; row < height-1; row++)
  3606. for (col=1+(FC(row,1) & 1), c=2-FC(row,col); col < width-1; col+=2) {
  3607. pix = image + row*width+col;
  3608. for (i=0; (d=dir[i]+dir[i+1]) > 0; i++) {
  3609. diff[i] = ABS(pix[-d][c] - pix[d][c]) +
  3610. ABS(pix[-d][1] - pix[0][1]) +
  3611. ABS(pix[ d][1] - pix[0][1]);
  3612. guess[i] = pix[-d][c] + pix[d][c] + 2*pix[0][1]
  3613. - pix[-d][1] - pix[d][1];
  3614. }
  3615. if (diff[0] != diff[1])
  3616. pix[0][c] = CLIP(guess[diff[0] > diff[1]] >> 1);
  3617. else
  3618. pix[0][c] = CLIP((guess[0]+guess[1]) >> 2);
  3619. }
  3620. }
  3621. #ifdef LIBRAW_LIBRARY_BUILD
  3622. /*
  3623. Adaptive Homogeneity-Directed interpolation is based on
  3624. the work of Keigo Hirakawa, Thomas Parks, and Paul Lee.
  3625. */
  3626. #define TS 256 /* Tile Size */
  3627. static float dcraw_cbrt[0x10000] = {-1.0f};
  3628. static inline float calc_64cbrt(float f)
  3629. {
  3630. unsigned u;
  3631. static float lower = dcraw_cbrt[0];
  3632. static float upper = dcraw_cbrt[0xffff];
  3633. if (f <= 0) {
  3634. return lower;
  3635. }
  3636. u = (unsigned) f;
  3637. if (u >= 0xffff) {
  3638. return upper;
  3639. }
  3640. return dcraw_cbrt[u];
  3641. }
  3642. void CLASS ahd_interpolate_green_h_and_v(int top, int left, ushort (*out_rgb)[TS][TS][3])
  3643. {
  3644. int row, col;
  3645. int c, val;
  3646. ushort (*pix)[4];
  3647. const int rowlimit = MIN(top+TS, height-2);
  3648. const int collimit = MIN(left+TS, width-2);
  3649. for (row = top; row < rowlimit; row++) {
  3650. col = left + (FC(row,left) & 1);
  3651. for (c = FC(row,col); col < collimit; col+=2) {
  3652. pix = image + row*width+col;
  3653. val = ((pix[-1][1] + pix[0][c] + pix[1][1]) * 2
  3654. - pix[-2][c] - pix[2][c]) >> 2;
  3655. out_rgb[0][row-top][col-left][1] = ULIM(val,pix[-1][1],pix[1][1]);
  3656. val = ((pix[-width][1] + pix[0][c] + pix[width][1]) * 2
  3657. - pix[-2*width][c] - pix[2*width][c]) >> 2;
  3658. out_rgb[1][row-top][col-left][1] = ULIM(val,pix[-width][1],pix[width][1]);
  3659. }
  3660. }
  3661. }
  3662. 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])
  3663. {
  3664. unsigned row, col;
  3665. int c, val;
  3666. ushort (*pix)[4];
  3667. ushort (*rix)[3];
  3668. short (*lix)[3];
  3669. float xyz[3];
  3670. const unsigned num_pix_per_row = 4*width;
  3671. const unsigned rowlimit = MIN(top+TS-1, height-3);
  3672. const unsigned collimit = MIN(left+TS-1, width-3);
  3673. ushort *pix_above;
  3674. ushort *pix_below;
  3675. int t1, t2;
  3676. for (row = top+1; row < rowlimit; row++) {
  3677. pix = image + row*width + left;
  3678. rix = &inout_rgb[row-top][0];
  3679. lix = &out_lab[row-top][0];
  3680. for (col = left+1; col < collimit; col++) {
  3681. pix++;
  3682. pix_above = &pix[0][0] - num_pix_per_row;
  3683. pix_below = &pix[0][0] + num_pix_per_row;
  3684. rix++;
  3685. lix++;
  3686. c = 2 - FC(row, col);
  3687. if (c == 1) {
  3688. c = FC(row+1,col);
  3689. t1 = 2-c;
  3690. val = pix[0][1] + (( pix[-1][t1] + pix[1][t1]
  3691. - rix[-1][1] - rix[1][1] ) >> 1);
  3692. rix[0][t1] = CLIP(val);
  3693. val = pix[0][1] + (( pix_above[c] + pix_below[c]
  3694. - rix[-TS][1] - rix[TS][1] ) >> 1);
  3695. } else {
  3696. t1 = -4+c; /* -4+c: pixel of color c to the left */
  3697. t2 = 4+c; /* 4+c: pixel of color c to the right */
  3698. val = rix[0][1] + (( pix_above[t1] + pix_above[t2]
  3699. + pix_below[t1] + pix_below[t2]
  3700. - rix[-TS-1][1] - rix[-TS+1][1]
  3701. - rix[+TS-1][1] - rix[+TS+1][1] + 1) >> 2);
  3702. }
  3703. rix[0][c] = CLIP(val);
  3704. c = FC(row,col);
  3705. rix[0][c] = pix[0][c];
  3706. xyz[0] = xyz[1] = xyz[2] = 0.5;
  3707. FORC3 {
  3708. /*
  3709. * Technically this ought to be FORCC, but the rest of
  3710. * ahd_interpolate() assumes 3 colors so let's help the compiler.
  3711. */
  3712. xyz[0] += xyz_cam[0][c] * rix[0][c];
  3713. xyz[1] += xyz_cam[1][c] * rix[0][c];
  3714. xyz[2] += xyz_cam[2][c] * rix[0][c];
  3715. }
  3716. FORC3 {
  3717. xyz[c] = calc_64cbrt(xyz[c]);
  3718. }
  3719. lix[0][0] = (116 * xyz[1] - 16);
  3720. lix[0][1] = 500 * (xyz[0] - xyz[1]);
  3721. lix[0][2] = 200 * (xyz[1] - xyz[2]);
  3722. }
  3723. }
  3724. }
  3725. 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])
  3726. {
  3727. int direction;
  3728. for (direction = 0; direction < 2; direction++) {
  3729. ahd_interpolate_r_and_b_in_rgb_and_convert_to_cielab(top, left, inout_rgb[direction], out_lab[direction], xyz_cam);
  3730. }
  3731. }
  3732. void CLASS ahd_interpolate_build_homogeneity_map(int top, int left, short (*lab)[TS][TS][3], char (*out_homogeneity_map)[TS][2])
  3733. {
  3734. int row, col;
  3735. int tr, tc;
  3736. int direction;
  3737. int i;
  3738. short (*lix)[3];
  3739. short (*lixs[2])[3];
  3740. short *adjacent_lix;
  3741. unsigned ldiff[2][4], abdiff[2][4], leps, abeps;
  3742. static const int dir[4] = { -1, 1, -TS, TS };
  3743. const int rowlimit = MIN(top+TS-2, height-4);
  3744. const int collimit = MIN(left+TS-2, width-4);
  3745. int homogeneity;
  3746. char (*homogeneity_map_p)[2];
  3747. memset (out_homogeneity_map, 0, 2*TS*TS);
  3748. for (row=top+2; row < rowlimit; row++) {
  3749. tr = row-top;
  3750. homogeneity_map_p = &out_homogeneity_map[tr][1];
  3751. for (direction=0; direction < 2; direction++) {
  3752. lixs[direction] = &lab[direction][tr][1];
  3753. }
  3754. for (col=left+2; col < collimit; col++) {
  3755. tc = col-left;
  3756. homogeneity_map_p++;
  3757. for (direction=0; direction < 2; direction++) {
  3758. lix = ++lixs[direction];
  3759. for (i=0; i < 4; i++) {
  3760. adjacent_lix = lix[dir[i]];
  3761. ldiff[direction][i] = ABS(lix[0][0]-adjacent_lix[0]);
  3762. abdiff[direction][i] = SQR(lix[0][1]-adjacent_lix[1])
  3763. + SQR(lix[0][2]-adjacent_lix[2]);
  3764. }
  3765. }
  3766. leps = MIN(MAX(ldiff[0][0],ldiff[0][1]),
  3767. MAX(ldiff[1][2],ldiff[1][3]));
  3768. abeps = MIN(MAX(abdiff[0][0],abdiff[0][1]),
  3769. MAX(abdiff[1][2],abdiff[1][3]));
  3770. for (direction=0; direction < 2; direction++) {
  3771. homogeneity = 0;
  3772. for (i=0; i < 4; i++) {
  3773. if (ldiff[direction][i] <= leps && abdiff[direction][i] <= abeps) {
  3774. homogeneity++;
  3775. }
  3776. }
  3777. homogeneity_map_p[0][direction] = homogeneity;
  3778. }
  3779. }
  3780. }
  3781. }
  3782. void CLASS ahd_interpolate_combine_homogeneous_pixels(int top, int left, ushort (*rgb)[TS][TS][3], char (*homogeneity_map)[TS][2])
  3783. {
  3784. int row, col;
  3785. int tr, tc;
  3786. int i, j;
  3787. int direction;
  3788. int hm[2];
  3789. int c;
  3790. const int rowlimit = MIN(top+TS-3, height-5);
  3791. const int collimit = MIN(left+TS-3, width-5);
  3792. ushort (*pix)[4];
  3793. ushort (*rix[2])[3];
  3794. for (row=top+3; row < rowlimit; row++) {
  3795. tr = row-top;
  3796. pix = &image[row*width+left+2];
  3797. for (direction = 0; direction < 2; direction++) {
  3798. rix[direction] = &rgb[direction][tr][2];
  3799. }
  3800. for (col=left+3; col < collimit; col++) {
  3801. tc = col-left;
  3802. pix++;
  3803. for (direction = 0; direction < 2; direction++) {
  3804. rix[direction]++;
  3805. }
  3806. for (direction=0; direction < 2; direction++) {
  3807. hm[direction] = 0;
  3808. for (i=tr-1; i <= tr+1; i++) {
  3809. for (j=tc-1; j <= tc+1; j++) {
  3810. hm[direction] += homogeneity_map[i][j][direction];
  3811. }
  3812. }
  3813. }
  3814. if (hm[0] != hm[1]) {
  3815. memcpy(pix[0], rix[hm[1] > hm[0]][0], 3 * sizeof(ushort));
  3816. } else {
  3817. FORC3 {
  3818. pix[0][c] = (rix[0][0][c] + rix[1][0][c]) >> 1;
  3819. }
  3820. }
  3821. }
  3822. }
  3823. }
  3824. void CLASS ahd_interpolate()
  3825. {
  3826. int i, j, k, top, left;
  3827. float xyz_cam[3][4],r;
  3828. char *buffer;
  3829. ushort (*rgb)[TS][TS][3];
  3830. short (*lab)[TS][TS][3];
  3831. char (*homo)[TS][2];
  3832. int terminate_flag = 0;
  3833. if(dcraw_cbrt[0]<-0.1){
  3834. for (i=0x10000-1; i >=0; i--) {
  3835. r = i / 65535.0;
  3836. dcraw_cbrt[i] = 64.0*(r > 0.008856 ? pow((double)r,1/3.0) : 7.787*r + 16/116.0);
  3837. }
  3838. }
  3839. #ifdef DCRAW_VERBOSE
  3840. if (verbose) fprintf (stderr,_("AHD interpolation...\n"));
  3841. #endif
  3842. for (i=0; i < 3; i++) {
  3843. for (j=0; j < colors; j++) {
  3844. xyz_cam[i][j] = 0;
  3845. for (k=0; k < 3; k++) {
  3846. xyz_cam[i][j] += xyz_rgb[i][k] * rgb_cam[k][j] / d65_white[i];
  3847. }
  3848. }
  3849. }
  3850. border_interpolate(5);
  3851. #ifdef LIBRAW_LIBRARY_BUILD
  3852. #ifdef LIBRAW_USE_OPENMP
  3853. #pragma omp parallel private(buffer,rgb,lab,homo,top,left,i,j,k) shared(xyz_cam,terminate_flag)
  3854. #endif
  3855. #endif
  3856. {
  3857. buffer = (char *) malloc (26*TS*TS); /* 1664 kB */
  3858. merror (buffer, "ahd_interpolate()");
  3859. rgb = (ushort(*)[TS][TS][3]) buffer;
  3860. lab = (short (*)[TS][TS][3])(buffer + 12*TS*TS);
  3861. homo = (char (*)[TS][2]) (buffer + 24*TS*TS);
  3862. #ifdef LIBRAW_LIBRARY_BUILD
  3863. #ifdef LIBRAW_USE_OPENMP
  3864. #pragma omp for schedule(dynamic)
  3865. #endif
  3866. #endif
  3867. for (top=2; top < height-5; top += TS-6){
  3868. #ifdef LIBRAW_LIBRARY_BUILD
  3869. #ifdef LIBRAW_USE_OPENMP
  3870. if(0== omp_get_thread_num())
  3871. #endif
  3872. if(callbacks.progress_cb) {
  3873. int rr = (*callbacks.progress_cb)(callbacks.progresscb_data,LIBRAW_PROGRESS_INTERPOLATE,top-2,height-7);
  3874. if(rr)
  3875. terminate_flag = 1;
  3876. }
  3877. #endif
  3878. for (left=2; !terminate_flag && (left < width-5); left += TS-6) {
  3879. ahd_interpolate_green_h_and_v(top, left, rgb);
  3880. ahd_interpolate_r_and_b_and_convert_to_cielab(top, left, rgb, lab, xyz_cam);
  3881. ahd_interpolate_build_homogeneity_map(top, left, lab, homo);
  3882. ahd_interpolate_combine_homogeneous_pixels(top, left, rgb, homo);
  3883. }
  3884. }
  3885. free (buffer);
  3886. }
  3887. #ifdef LIBRAW_LIBRARY_BUILD
  3888. if(terminate_flag)
  3889. throw LIBRAW_EXCEPTION_CANCELLED_BY_CALLBACK;
  3890. #endif
  3891. }
  3892. #undef TS
  3893. #else
  3894. /*
  3895. Adaptive Homogeneity-Directed interpolation is based on
  3896. the work of Keigo Hirakawa, Thomas Parks, and Paul Lee.
  3897. */
  3898. #define TS 256 /* Tile Size */
  3899. void CLASS ahd_interpolate()
  3900. {
  3901. int i, j, k, top, left, row, col, tr, tc, c, d, val, hm[2];
  3902. ushort (*pix)[4], (*rix)[3];
  3903. static const int dir[4] = { -1, 1, -TS, TS };
  3904. unsigned ldiff[2][4], abdiff[2][4], leps, abeps;
  3905. float r, cbrt[0x10000], xyz[3], xyz_cam[3][4];
  3906. ushort (*rgb)[TS][TS][3];
  3907. short (*lab)[TS][TS][3], (*lix)[3];
  3908. char (*homo)[TS][TS], *buffer;
  3909. #ifdef DCRAW_VERBOSE
  3910. if (verbose) fprintf (stderr,_("AHD interpolation...\n"));
  3911. #endif
  3912. for (i=0; i < 0x10000; i++) {
  3913. r = i / 65535.0;
  3914. cbrt[i] = r > 0.008856 ? pow((double)r,(double)(1/3.0)) : 7.787*r + 16/116.0;
  3915. }
  3916. for (i=0; i < 3; i++)
  3917. for (j=0; j < colors; j++)
  3918. for (xyz_cam[i][j] = k=0; k < 3; k++)
  3919. xyz_cam[i][j] += xyz_rgb[i][k] * rgb_cam[k][j] / d65_white[i];
  3920. border_interpolate(5);
  3921. buffer = (char *) malloc (26*TS*TS); /* 1664 kB */
  3922. merror (buffer, "ahd_interpolate()");
  3923. rgb = (ushort(*)[TS][TS][3]) buffer;
  3924. lab = (short (*)[TS][TS][3])(buffer + 12*TS*TS);
  3925. homo = (char (*)[TS][TS]) (buffer + 24*TS*TS);
  3926. for (top=2; top < height-5; top += TS-6)
  3927. for (left=2; left < width-5; left += TS-6) {
  3928. /* Interpolate green horizontally and vertically: */
  3929. for (row = top; row < top+TS && row < height-2; row++) {
  3930. col = left + (FC(row,left) & 1);
  3931. for (c = FC(row,col); col < left+TS && col < width-2; col+=2) {
  3932. pix = image + row*width+col;
  3933. val = ((pix[-1][1] + pix[0][c] + pix[1][1]) * 2
  3934. - pix[-2][c] - pix[2][c]) >> 2;
  3935. rgb[0][row-top][col-left][1] = ULIM(val,pix[-1][1],pix[1][1]);
  3936. val = ((pix[-width][1] + pix[0][c] + pix[width][1]) * 2
  3937. - pix[-2*width][c] - pix[2*width][c]) >> 2;
  3938. rgb[1][row-top][col-left][1] = ULIM(val,pix[-width][1],pix[width][1]);
  3939. }
  3940. }
  3941. /* Interpolate red and blue, and convert to CIELab: */
  3942. for (d=0; d < 2; d++)
  3943. for (row=top+1; row < top+TS-1 && row < height-3; row++)
  3944. for (col=left+1; col < left+TS-1 && col < width-3; col++) {
  3945. pix = image + row*width+col;
  3946. rix = &rgb[d][row-top][col-left];
  3947. lix = &lab[d][row-top][col-left];
  3948. if ((c = 2 - FC(row,col)) == 1) {
  3949. c = FC(row+1,col);
  3950. val = pix[0][1] + (( pix[-1][2-c] + pix[1][2-c]
  3951. - rix[-1][1] - rix[1][1] ) >> 1);
  3952. rix[0][2-c] = CLIP(val);
  3953. val = pix[0][1] + (( pix[-width][c] + pix[width][c]
  3954. - rix[-TS][1] - rix[TS][1] ) >> 1);
  3955. } else
  3956. val = rix[0][1] + (( pix[-width-1][c] + pix[-width+1][c]
  3957. + pix[+width-1][c] + pix[+width+1][c]
  3958. - rix[-TS-1][1] - rix[-TS+1][1]
  3959. - rix[+TS-1][1] - rix[+TS+1][1] + 1) >> 2);
  3960. rix[0][c] = CLIP(val);
  3961. c = FC(row,col);
  3962. rix[0][c] = pix[0][c];
  3963. xyz[0] = xyz[1] = xyz[2] = 0.5;
  3964. FORCC {
  3965. xyz[0] += xyz_cam[0][c] * rix[0][c];
  3966. xyz[1] += xyz_cam[1][c] * rix[0][c];
  3967. xyz[2] += xyz_cam[2][c] * rix[0][c];
  3968. }
  3969. xyz[0] = cbrt[CLIP((int) xyz[0])];
  3970. xyz[1] = cbrt[CLIP((int) xyz[1])];
  3971. xyz[2] = cbrt[CLIP((int) xyz[2])];
  3972. lix[0][0] = 64 * (116 * xyz[1] - 16);
  3973. lix[0][1] = 64 * 500 * (xyz[0] - xyz[1]);
  3974. lix[0][2] = 64 * 200 * (xyz[1] - xyz[2]);
  3975. }
  3976. /* Build homogeneity maps from the CIELab images: */
  3977. memset (homo, 0, 2*TS*TS);
  3978. for (row=top+2; row < top+TS-2 && row < height-4; row++) {
  3979. tr = row-top;
  3980. for (col=left+2; col < left+TS-2 && col < width-4; col++) {
  3981. tc = col-left;
  3982. for (d=0; d < 2; d++) {
  3983. lix = &lab[d][tr][tc];
  3984. for (i=0; i < 4; i++) {
  3985. ldiff[d][i] = ABS(lix[0][0]-lix[dir[i]][0]);
  3986. abdiff[d][i] = SQR(lix[0][1]-lix[dir[i]][1])
  3987. + SQR(lix[0][2]-lix[dir[i]][2]);
  3988. }
  3989. }
  3990. leps = MIN(MAX(ldiff[0][0],ldiff[0][1]),
  3991. MAX(ldiff[1][2],ldiff[1][3]));
  3992. abeps = MIN(MAX(abdiff[0][0],abdiff[0][1]),
  3993. MAX(abdiff[1][2],abdiff[1][3]));
  3994. for (d=0; d < 2; d++)
  3995. for (i=0; i < 4; i++)
  3996. if (ldiff[d][i] <= leps && abdiff[d][i] <= abeps)
  3997. homo[d][tr][tc]++;
  3998. }
  3999. }
  4000. /* Combine the most homogenous pixels for the final result: */
  4001. for (row=top+3; row < top+TS-3 && row < height-5; row++) {
  4002. tr = row-top;
  4003. for (col=left+3; col < left+TS-3 && col < width-5; col++) {
  4004. tc = col-left;
  4005. for (d=0; d < 2; d++)
  4006. for (hm[d]=0, i=tr-1; i <= tr+1; i++)
  4007. for (j=tc-1; j <= tc+1; j++)
  4008. hm[d] += homo[d][i][j];
  4009. if (hm[0] != hm[1])
  4010. FORC3 image[row*width+col][c] = rgb[hm[1] > hm[0]][tr][tc][c];
  4011. else
  4012. FORC3 image[row*width+col][c] =
  4013. (rgb[0][tr][tc][c] + rgb[1][tr][tc][c]) >> 1;
  4014. }
  4015. }
  4016. }
  4017. free (buffer);
  4018. }
  4019. #undef TS
  4020. #endif
  4021. void CLASS median_filter()
  4022. {
  4023. ushort (*pix)[4];
  4024. int pass, c, i, j, k, med[9];
  4025. static const uchar opt[] = /* Optimal 9-element median search */
  4026. { 1,2, 4,5, 7,8, 0,1, 3,4, 6,7, 1,2, 4,5, 7,8,
  4027. 0,3, 5,8, 4,7, 3,6, 1,4, 2,5, 4,7, 4,2, 6,4, 4,2 };
  4028. for (pass=1; pass <= med_passes; pass++) {
  4029. #ifdef LIBRAW_LIBRARY_BUILD
  4030. RUN_CALLBACK(LIBRAW_PROGRESS_MEDIAN_FILTER,pass-1,med_passes);
  4031. #endif
  4032. #ifdef DCRAW_VERBOSE
  4033. if (verbose)
  4034. fprintf (stderr,_("Median filter pass %d...\n"), pass);
  4035. #endif
  4036. for (c=0; c < 3; c+=2) {
  4037. for (pix = image; pix < image+width*height; pix++)
  4038. pix[0][3] = pix[0][c];
  4039. for (pix = image+width; pix < image+width*(height-1); pix++) {
  4040. if ((pix-image+1) % width < 2) continue;
  4041. for (k=0, i = -width; i <= width; i += width)
  4042. for (j = i-1; j <= i+1; j++)
  4043. med[k++] = pix[j][3] - pix[j][1];
  4044. for (i=0; i < sizeof opt; i+=2)
  4045. if (med[opt[i]] > med[opt[i+1]])
  4046. SWAP (med[opt[i]] , med[opt[i+1]]);
  4047. pix[0][c] = CLIP(med[4] + pix[0][1]);
  4048. }
  4049. }
  4050. }
  4051. }
  4052. void CLASS blend_highlights()
  4053. {
  4054. int clip=INT_MAX, row, col, c, i, j;
  4055. static const float trans[2][4][4] =
  4056. { { { 1,1,1 }, { 1.7320508,-1.7320508,0 }, { -1,-1,2 } },
  4057. { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } };
  4058. static const float itrans[2][4][4] =
  4059. { { { 1,0.8660254,-0.5 }, { 1,-0.8660254,-0.5 }, { 1,0,1 } },
  4060. { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } };
  4061. float cam[2][4], lab[2][4], sum[2], chratio;
  4062. if ((unsigned) (colors-3) > 1) return;
  4063. #ifdef DCRAW_VERBOSE
  4064. if (verbose) fprintf (stderr,_("Blending highlights...\n"));
  4065. #endif
  4066. #ifdef LIBRAW_LIBRARY_BUILD
  4067. RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS,0,2);
  4068. #endif
  4069. FORCC if (clip > (i = 65535*pre_mul[c])) clip = i;
  4070. for (row=0; row < height; row++)
  4071. for (col=0; col < width; col++) {
  4072. FORCC if (image[row*width+col][c] > clip) break;
  4073. if (c == colors) continue;
  4074. FORCC {
  4075. cam[0][c] = image[row*width+col][c];
  4076. cam[1][c] = MIN(cam[0][c],clip);
  4077. }
  4078. for (i=0; i < 2; i++) {
  4079. FORCC for (lab[i][c]=j=0; j < colors; j++)
  4080. lab[i][c] += trans[colors-3][c][j] * cam[i][j];
  4081. for (sum[i]=0,c=1; c < colors; c++)
  4082. sum[i] += SQR(lab[i][c]);
  4083. }
  4084. chratio = sqrt(sum[1]/sum[0]);
  4085. for (c=1; c < colors; c++)
  4086. lab[0][c] *= chratio;
  4087. FORCC for (cam[0][c]=j=0; j < colors; j++)
  4088. cam[0][c] += itrans[colors-3][c][j] * lab[0][j];
  4089. FORCC image[row*width+col][c] = cam[0][c] / colors;
  4090. }
  4091. #ifdef LIBRAW_LIBRARY_BUILD
  4092. RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS,1,2);
  4093. #endif
  4094. }
  4095. #define SCALE (4 >> shrink)
  4096. void CLASS recover_highlights()
  4097. {
  4098. float *map, sum, wgt, grow;
  4099. int hsat[4], count, spread, change, val, i;
  4100. unsigned high, wide, mrow, mcol, row, col, kc, c, d, y, x;
  4101. ushort *pixel;
  4102. static const signed char dir[8][2] =
  4103. { {-1,-1}, {-1,0}, {-1,1}, {0,1}, {1,1}, {1,0}, {1,-1}, {0,-1} };
  4104. #ifdef DCRAW_VERBOSE
  4105. if (verbose) fprintf (stderr,_("Rebuilding highlights...\n"));
  4106. #endif
  4107. grow = pow (2.0, 4-highlight);
  4108. FORCC hsat[c] = 32000 * pre_mul[c];
  4109. for (kc=0, c=1; c < colors; c++)
  4110. if (pre_mul[kc] < pre_mul[c]) kc = c;
  4111. high = height / SCALE;
  4112. wide = width / SCALE;
  4113. map = (float *) calloc (high*wide, sizeof *map);
  4114. merror (map, "recover_highlights()");
  4115. FORCC if (c != kc) {
  4116. #ifdef LIBRAW_LIBRARY_BUILD
  4117. RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS,c-1,colors-1);
  4118. #endif
  4119. memset (map, 0, high*wide*sizeof *map);
  4120. for (mrow=0; mrow < high; mrow++)
  4121. for (mcol=0; mcol < wide; mcol++) {
  4122. sum = wgt = count = 0;
  4123. for (row = mrow*SCALE; row < (mrow+1)*SCALE; row++)
  4124. for (col = mcol*SCALE; col < (mcol+1)*SCALE; col++) {
  4125. pixel = image[row*width+col];
  4126. if (pixel[c] / hsat[c] == 1 && pixel[kc] > 24000) {
  4127. sum += pixel[c];
  4128. wgt += pixel[kc];
  4129. count++;
  4130. }
  4131. }
  4132. if (count == SCALE*SCALE)
  4133. map[mrow*wide+mcol] = sum / wgt;
  4134. }
  4135. for (spread = 32/grow; spread--; ) {
  4136. for (mrow=0; mrow < high; mrow++)
  4137. for (mcol=0; mcol < wide; mcol++) {
  4138. if (map[mrow*wide+mcol]) continue;
  4139. sum = count = 0;
  4140. for (d=0; d < 8; d++) {
  4141. y = mrow + dir[d][0];
  4142. x = mcol + dir[d][1];
  4143. if (y < high && x < wide && map[y*wide+x] > 0) {
  4144. sum += (1 + (d & 1)) * map[y*wide+x];
  4145. count += 1 + (d & 1);
  4146. }
  4147. }
  4148. if (count > 3)
  4149. map[mrow*wide+mcol] = - (sum+grow) / (count+grow);
  4150. }
  4151. for (change=i=0; i < high*wide; i++)
  4152. if (map[i] < 0) {
  4153. map[i] = -map[i];
  4154. change = 1;
  4155. }
  4156. if (!change) break;
  4157. }
  4158. for (i=0; i < high*wide; i++)
  4159. if (map[i] == 0) map[i] = 1;
  4160. for (mrow=0; mrow < high; mrow++)
  4161. for (mcol=0; mcol < wide; mcol++) {
  4162. for (row = mrow*SCALE; row < (mrow+1)*SCALE; row++)
  4163. for (col = mcol*SCALE; col < (mcol+1)*SCALE; col++) {
  4164. pixel = image[row*width+col];
  4165. if (pixel[c] / hsat[c] > 1) {
  4166. val = pixel[kc] * map[mrow*wide+mcol];
  4167. if (pixel[c] < val) pixel[c] = CLIP(val);
  4168. }
  4169. }
  4170. }
  4171. }
  4172. free (map);
  4173. }
  4174. #undef SCALE
  4175. void CLASS tiff_get (unsigned base,
  4176. unsigned *tag, unsigned *type, unsigned *len, unsigned *save)
  4177. {
  4178. *tag = get2();
  4179. *type = get2();
  4180. *len = get4();
  4181. *save = ftell(ifp) + 4;
  4182. if (*len * ("11124811248488"[*type < 14 ? *type:0]-'0') > 4)
  4183. fseek (ifp, get4()+base, SEEK_SET);
  4184. }
  4185. void CLASS parse_thumb_note (int base, unsigned toff, unsigned tlen)
  4186. {
  4187. unsigned entries, tag, type, len, save;
  4188. entries = get2();
  4189. while (entries--) {
  4190. tiff_get (base, &tag, &type, &len, &save);
  4191. if (tag == toff) thumb_offset = get4()+base;
  4192. if (tag == tlen) thumb_length = get4();
  4193. fseek (ifp, save, SEEK_SET);
  4194. }
  4195. }
  4196. #line 5624 "dcraw/dcraw.c"
  4197. void CLASS parse_makernote (int base, int uptag)
  4198. {
  4199. static const uchar xlat[2][256] = {
  4200. { 0xc1,0xbf,0x6d,0x0d,0x59,0xc5,0x13,0x9d,0x83,0x61,0x6b,0x4f,0xc7,0x7f,0x3d,0x3d,
  4201. 0x53,0x59,0xe3,0xc7,0xe9,0x2f,0x95,0xa7,0x95,0x1f,0xdf,0x7f,0x2b,0x29,0xc7,0x0d,
  4202. 0xdf,0x07,0xef,0x71,0x89,0x3d,0x13,0x3d,0x3b,0x13,0xfb,0x0d,0x89,0xc1,0x65,0x1f,
  4203. 0xb3,0x0d,0x6b,0x29,0xe3,0xfb,0xef,0xa3,0x6b,0x47,0x7f,0x95,0x35,0xa7,0x47,0x4f,
  4204. 0xc7,0xf1,0x59,0x95,0x35,0x11,0x29,0x61,0xf1,0x3d,0xb3,0x2b,0x0d,0x43,0x89,0xc1,
  4205. 0x9d,0x9d,0x89,0x65,0xf1,0xe9,0xdf,0xbf,0x3d,0x7f,0x53,0x97,0xe5,0xe9,0x95,0x17,
  4206. 0x1d,0x3d,0x8b,0xfb,0xc7,0xe3,0x67,0xa7,0x07,0xf1,0x71,0xa7,0x53,0xb5,0x29,0x89,
  4207. 0xe5,0x2b,0xa7,0x17,0x29,0xe9,0x4f,0xc5,0x65,0x6d,0x6b,0xef,0x0d,0x89,0x49,0x2f,
  4208. 0xb3,0x43,0x53,0x65,0x1d,0x49,0xa3,0x13,0x89,0x59,0xef,0x6b,0xef,0x65,0x1d,0x0b,
  4209. 0x59,0x13,0xe3,0x4f,0x9d,0xb3,0x29,0x43,0x2b,0x07,0x1d,0x95,0x59,0x59,0x47,0xfb,
  4210. 0xe5,0xe9,0x61,0x47,0x2f,0x35,0x7f,0x17,0x7f,0xef,0x7f,0x95,0x95,0x71,0xd3,0xa3,
  4211. 0x0b,0x71,0xa3,0xad,0x0b,0x3b,0xb5,0xfb,0xa3,0xbf,0x4f,0x83,0x1d,0xad,0xe9,0x2f,
  4212. 0x71,0x65,0xa3,0xe5,0x07,0x35,0x3d,0x0d,0xb5,0xe9,0xe5,0x47,0x3b,0x9d,0xef,0x35,
  4213. 0xa3,0xbf,0xb3,0xdf,0x53,0xd3,0x97,0x53,0x49,0x71,0x07,0x35,0x61,0x71,0x2f,0x43,
  4214. 0x2f,0x11,0xdf,0x17,0x97,0xfb,0x95,0x3b,0x7f,0x6b,0xd3,0x25,0xbf,0xad,0xc7,0xc5,
  4215. 0xc5,0xb5,0x8b,0xef,0x2f,0xd3,0x07,0x6b,0x25,0x49,0x95,0x25,0x49,0x6d,0x71,0xc7 },
  4216. { 0xa7,0xbc,0xc9,0xad,0x91,0xdf,0x85,0xe5,0xd4,0x78,0xd5,0x17,0x46,0x7c,0x29,0x4c,
  4217. 0x4d,0x03,0xe9,0x25,0x68,0x11,0x86,0xb3,0xbd,0xf7,0x6f,0x61,0x22,0xa2,0x26,0x34,
  4218. 0x2a,0xbe,0x1e,0x46,0x14,0x68,0x9d,0x44,0x18,0xc2,0x40,0xf4,0x7e,0x5f,0x1b,0xad,
  4219. 0x0b,0x94,0xb6,0x67,0xb4,0x0b,0xe1,0xea,0x95,0x9c,0x66,0xdc,0xe7,0x5d,0x6c,0x05,
  4220. 0xda,0xd5,0xdf,0x7a,0xef,0xf6,0xdb,0x1f,0x82,0x4c,0xc0,0x68,0x47,0xa1,0xbd,0xee,
  4221. 0x39,0x50,0x56,0x4a,0xdd,0xdf,0xa5,0xf8,0xc6,0xda,0xca,0x90,0xca,0x01,0x42,0x9d,
  4222. 0x8b,0x0c,0x73,0x43,0x75,0x05,0x94,0xde,0x24,0xb3,0x80,0x34,0xe5,0x2c,0xdc,0x9b,
  4223. 0x3f,0xca,0x33,0x45,0xd0,0xdb,0x5f,0xf5,0x52,0xc3,0x21,0xda,0xe2,0x22,0x72,0x6b,
  4224. 0x3e,0xd0,0x5b,0xa8,0x87,0x8c,0x06,0x5d,0x0f,0xdd,0x09,0x19,0x93,0xd0,0xb9,0xfc,
  4225. 0x8b,0x0f,0x84,0x60,0x33,0x1c,0x9b,0x45,0xf1,0xf0,0xa3,0x94,0x3a,0x12,0x77,0x33,
  4226. 0x4d,0x44,0x78,0x28,0x3c,0x9e,0xfd,0x65,0x57,0x16,0x94,0x6b,0xfb,0x59,0xd0,0xc8,
  4227. 0x22,0x36,0xdb,0xd2,0x63,0x98,0x43,0xa1,0x04,0x87,0x86,0xf7,0xa6,0x26,0xbb,0xd6,
  4228. 0x59,0x4d,0xbf,0x6a,0x2e,0xaa,0x2b,0xef,0xe6,0x78,0xb6,0x4e,0xe0,0x2f,0xdc,0x7c,
  4229. 0xbe,0x57,0x19,0x32,0x7e,0x2a,0xd0,0xb8,0xba,0x29,0x00,0x3c,0x52,0x7d,0xa8,0x49,
  4230. 0x3b,0x2d,0xeb,0x25,0x49,0xfa,0xa3,0xaa,0x39,0xa7,0xc5,0xa7,0x50,0x11,0x36,0xfb,
  4231. 0xc6,0x67,0x4a,0xf5,0xa5,0x12,0x65,0x7e,0xb0,0xdf,0xaf,0x4e,0xb3,0x61,0x7f,0x2f } };
  4232. unsigned offset=0, entries, tag, type, len, save, c;
  4233. unsigned ver97=0, serial=0, i, wbi=0, wb[4]={0,0,0,0};
  4234. uchar buf97[324], ci, cj, ck;
  4235. short morder, sorder=order;
  4236. char buf[10];
  4237. /*
  4238. The MakerNote might have its own TIFF header (possibly with
  4239. its own byte-order!), or it might just be a table.
  4240. */
  4241. if (!strcmp(make,"Nokia")) return;
  4242. fread (buf, 1, 10, ifp);
  4243. if (!strncmp (buf,"KDK" ,3) || /* these aren't TIFF tables */
  4244. !strncmp (buf,"VER" ,3) ||
  4245. !strncmp (buf,"IIII",4) ||
  4246. !strncmp (buf,"MMMM",4)) return;
  4247. if (!strncmp (buf,"KC" ,2) || /* Konica KD-400Z, KD-510Z */
  4248. !strncmp (buf,"MLY" ,3)) { /* Minolta DiMAGE G series */
  4249. order = 0x4d4d;
  4250. while ((i=ftell(ifp)) < data_offset && i < 16384) {
  4251. wb[0] = wb[2]; wb[2] = wb[1]; wb[1] = wb[3];
  4252. wb[3] = get2();
  4253. if (wb[1] == 256 && wb[3] == 256 &&
  4254. wb[0] > 256 && wb[0] < 640 && wb[2] > 256 && wb[2] < 640)
  4255. FORC4 cam_mul[c] = wb[c];
  4256. }
  4257. goto quit;
  4258. }
  4259. if (!strcmp (buf,"Nikon")) {
  4260. base = ftell(ifp);
  4261. order = get2();
  4262. if (get2() != 42) goto quit;
  4263. offset = get4();
  4264. fseek (ifp, offset-8, SEEK_CUR);
  4265. } else if (!strcmp (buf,"OLYMPUS")) {
  4266. base = ftell(ifp)-10;
  4267. fseek (ifp, -2, SEEK_CUR);
  4268. order = get2(); get2();
  4269. } else if (!strncmp (buf,"SONY",4) ||
  4270. !strcmp (buf,"Panasonic")) {
  4271. goto nf;
  4272. } else if (!strncmp (buf,"FUJIFILM",8)) {
  4273. base = ftell(ifp)-10;
  4274. nf: order = 0x4949;
  4275. fseek (ifp, 2, SEEK_CUR);
  4276. } else if (!strcmp (buf,"OLYMP") ||
  4277. !strcmp (buf,"LEICA") ||
  4278. !strcmp (buf,"Ricoh") ||
  4279. !strcmp (buf,"EPSON"))
  4280. fseek (ifp, -2, SEEK_CUR);
  4281. else if (!strcmp (buf,"AOC") ||
  4282. !strcmp (buf,"QVC"))
  4283. fseek (ifp, -4, SEEK_CUR);
  4284. else {
  4285. fseek (ifp, -10, SEEK_CUR);
  4286. if (!strncmp(make,"SAMSUNG",7))
  4287. base = ftell(ifp);
  4288. }
  4289. entries = get2();
  4290. if (entries > 1000) return;
  4291. morder = order;
  4292. while (entries--) {
  4293. order = morder;
  4294. tiff_get (base, &tag, &type, &len, &save);
  4295. tag |= uptag << 16;
  4296. if (tag == 2 && strstr(make,"NIKON") && !iso_speed)
  4297. iso_speed = (get2(),get2());
  4298. if (tag == 4 && len > 26 && len < 35) {
  4299. if ((i=(get4(),get2())) != 0x7fff && !iso_speed)
  4300. iso_speed = 50 * pow (2, i/32.0 - 4);
  4301. if ((i=(get2(),get2())) != 0x7fff && !aperture)
  4302. aperture = pow (2, i/64.0);
  4303. if ((i=get2()) != 0xffff && !shutter)
  4304. shutter = pow (2, (short) i/-32.0);
  4305. wbi = (get2(),get2());
  4306. shot_order = (get2(),get2());
  4307. }
  4308. if ((tag == 4 || tag == 0x114) && !strncmp(make,"KONICA",6)) {
  4309. fseek (ifp, tag == 4 ? 140:160, SEEK_CUR);
  4310. switch (get2()) {
  4311. case 72: flip = 0; break;
  4312. case 76: flip = 6; break;
  4313. case 82: flip = 5; break;
  4314. }
  4315. }
  4316. if (tag == 7 && type == 2 && len > 20)
  4317. fgets (model2, 64, ifp);
  4318. if (tag == 8 && type == 4)
  4319. shot_order = get4();
  4320. if (tag == 9 && !strcmp(make,"Canon"))
  4321. fread (artist, 64, 1, ifp);
  4322. if (tag == 0xc && len == 4) {
  4323. cam_mul[0] = getreal(type);
  4324. cam_mul[2] = getreal(type);
  4325. }
  4326. if (tag == 0xd && type == 7 && get2() == 0xaaaa) {
  4327. for (c=i=2; (ushort) c != 0xbbbb && i < len; i++)
  4328. c = c << 8 | fgetc(ifp);
  4329. while ((i+=4) < len-5)
  4330. if (get4() == 257 && (i=len) && (c = (get4(),fgetc(ifp))) < 3)
  4331. flip = "065"[c]-'0';
  4332. }
  4333. if (tag == 0x10 && type == 4)
  4334. unique_id = get4();
  4335. if (tag == 0x11 && is_raw && !strncmp(make,"NIKON",5)) {
  4336. fseek (ifp, get4()+base, SEEK_SET);
  4337. parse_tiff_ifd (base);
  4338. }
  4339. if (tag == 0x14 && type == 7) {
  4340. if (len == 2560) {
  4341. fseek (ifp, 1248, SEEK_CUR);
  4342. goto get2_256;
  4343. }
  4344. fread (buf, 1, 10, ifp);
  4345. if (!strncmp(buf,"NRW ",4)) {
  4346. fseek (ifp, strcmp(buf+4,"0100") ? 46:1546, SEEK_CUR);
  4347. cam_mul[0] = get4() << 2;
  4348. cam_mul[1] = get4() + get4();
  4349. cam_mul[2] = get4() << 2;
  4350. }
  4351. }
  4352. if (tag == 0x15 && type == 2 && is_raw)
  4353. fread (model, 64, 1, ifp);
  4354. if (strstr(make,"PENTAX")) {
  4355. if (tag == 0x1b) tag = 0x1018;
  4356. if (tag == 0x1c) tag = 0x1017;
  4357. }
  4358. if (tag == 0x1d)
  4359. while ((c = fgetc(ifp)) && c != EOF)
  4360. serial = serial*10 + (isdigit(c) ? c - '0' : c % 10);
  4361. if (tag == 0x81 && type == 4) {
  4362. data_offset = get4();
  4363. fseek (ifp, data_offset + 41, SEEK_SET);
  4364. raw_height = get2() * 2;
  4365. raw_width = get2();
  4366. filters = 0x61616161;
  4367. }
  4368. if (tag == 0x29 && type == 1) {
  4369. c = wbi < 18 ? "012347800000005896"[wbi]-'0' : 0;
  4370. fseek (ifp, 8 + c*32, SEEK_CUR);
  4371. FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get4();
  4372. }
  4373. if ((tag == 0x81 && type == 7) ||
  4374. (tag == 0x100 && type == 7) ||
  4375. (tag == 0x280 && type == 1)) {
  4376. thumb_offset = ftell(ifp);
  4377. thumb_length = len;
  4378. }
  4379. if (tag == 0x88 && type == 4 && (thumb_offset = get4()))
  4380. thumb_offset += base;
  4381. if (tag == 0x89 && type == 4)
  4382. thumb_length = get4();
  4383. if (tag == 0x8c || tag == 0x96)
  4384. meta_offset = ftell(ifp);
  4385. if (tag == 0x97) {
  4386. for (i=0; i < 4; i++)
  4387. ver97 = ver97 * 10 + fgetc(ifp)-'0';
  4388. switch (ver97) {
  4389. case 100:
  4390. fseek (ifp, 68, SEEK_CUR);
  4391. FORC4 cam_mul[(c >> 1) | ((c & 1) << 1)] = get2();
  4392. break;
  4393. case 102:
  4394. fseek (ifp, 6, SEEK_CUR);
  4395. goto get2_rggb;
  4396. case 103:
  4397. fseek (ifp, 16, SEEK_CUR);
  4398. FORC4 cam_mul[c] = get2();
  4399. }
  4400. if (ver97 >= 200) {
  4401. if (ver97 != 205) fseek (ifp, 280, SEEK_CUR);
  4402. fread (buf97, 324, 1, ifp);
  4403. }
  4404. }
  4405. if (tag == 0xa1 && type == 7) {
  4406. order = 0x4949;
  4407. fseek (ifp, 140, SEEK_CUR);
  4408. FORC3 cam_mul[c] = get4();
  4409. }
  4410. if (tag == 0xa4 && type == 3) {
  4411. fseek (ifp, wbi*48, SEEK_CUR);
  4412. FORC3 cam_mul[c] = get2();
  4413. }
  4414. if (tag == 0xa7 && (unsigned) (ver97-200) < 17) {
  4415. ci = xlat[0][serial & 0xff];
  4416. cj = xlat[1][fgetc(ifp)^fgetc(ifp)^fgetc(ifp)^fgetc(ifp)];
  4417. ck = 0x60;
  4418. for (i=0; i < 324; i++)
  4419. buf97[i] ^= (cj += ci * ck++);
  4420. i = "66666>666;6A;:;55"[ver97-200] - '0';
  4421. FORC4 cam_mul[c ^ (c >> 1) ^ (i & 1)] =
  4422. sget2 (buf97 + (i & -2) + c*2);
  4423. }
  4424. if (tag == 0x200 && len == 3)
  4425. shot_order = (get4(),get4());
  4426. if (tag == 0x200 && len == 4)
  4427. FORC4 cblack[c ^ c >> 1] = get2();
  4428. if (tag == 0x201 && len == 4)
  4429. goto get2_rggb;
  4430. if (tag == 0x220 && type == 7)
  4431. meta_offset = ftell(ifp);
  4432. if (tag == 0x401 && type == 4 && len == 4)
  4433. FORC4 cblack[c ^ c >> 1] = get4();
  4434. if (tag == 0xe01) { /* Nikon Capture Note */
  4435. order = 0x4949;
  4436. fseek (ifp, 22, SEEK_CUR);
  4437. for (offset=22; offset+22 < len; offset += 22+i) {
  4438. tag = get4();
  4439. fseek (ifp, 14, SEEK_CUR);
  4440. i = get4()-4;
  4441. if (tag == 0x76a43207) flip = get2();
  4442. else fseek (ifp, i, SEEK_CUR);
  4443. }
  4444. }
  4445. if (tag == 0xe80 && len == 256 && type == 7) {
  4446. fseek (ifp, 48, SEEK_CUR);
  4447. cam_mul[0] = get2() * 508 * 1.078 / 0x10000;
  4448. cam_mul[2] = get2() * 382 * 1.173 / 0x10000;
  4449. }
  4450. if (tag == 0xf00 && type == 7) {
  4451. if (len == 614)
  4452. fseek (ifp, 176, SEEK_CUR);
  4453. else if (len == 734 || len == 1502)
  4454. fseek (ifp, 148, SEEK_CUR);
  4455. else goto next;
  4456. goto get2_256;
  4457. }
  4458. if ((tag == 0x1011 && len == 9) || tag == 0x20400200)
  4459. for (i=0; i < 3; i++)
  4460. FORC3 cmatrix[i][c] = ((short) get2()) / 256.0;
  4461. if ((tag == 0x1012 || tag == 0x20400600) && len == 4)
  4462. FORC4 cblack[c ^ c >> 1] = get2();
  4463. if (tag == 0x1017 || tag == 0x20400100)
  4464. cam_mul[0] = get2() / 256.0;
  4465. if (tag == 0x1018 || tag == 0x20400100)
  4466. cam_mul[2] = get2() / 256.0;
  4467. if (tag == 0x2011 && len == 2) {
  4468. get2_256:
  4469. order = 0x4d4d;
  4470. cam_mul[0] = get2() / 256.0;
  4471. cam_mul[2] = get2() / 256.0;
  4472. }
  4473. if ((tag | 0x70) == 0x2070 && type == 4)
  4474. fseek (ifp, get4()+base, SEEK_SET);
  4475. if (tag == 0x2010 && type != 7)
  4476. load_raw = &CLASS olympus_load_raw;
  4477. if (tag == 0x2020)
  4478. parse_thumb_note (base, 257, 258);
  4479. if (tag == 0x2040)
  4480. parse_makernote (base, 0x2040);
  4481. if (tag == 0xb028) {
  4482. fseek (ifp, get4()+base, SEEK_SET);
  4483. parse_thumb_note (base, 136, 137);
  4484. }
  4485. if (tag == 0x4001 && len > 500) {
  4486. i = len == 582 ? 50 : len == 653 ? 68 : len == 5120 ? 142 : 126;
  4487. fseek (ifp, i, SEEK_CUR);
  4488. get2_rggb:
  4489. FORC4 cam_mul[c ^ (c >> 1)] = get2();
  4490. i = len >> 3 == 164 ? 112:22;
  4491. fseek (ifp, i, SEEK_CUR);
  4492. FORC4 sraw_mul[c ^ (c >> 1)] = get2();
  4493. }
  4494. if (tag == 0xa021)
  4495. FORC4 cam_mul[c ^ (c >> 1)] = get4();
  4496. if (tag == 0xa028)
  4497. FORC4 cam_mul[c ^ (c >> 1)] -= get4();
  4498. next:
  4499. fseek (ifp, save, SEEK_SET);
  4500. }
  4501. quit:
  4502. order = sorder;
  4503. }
  4504. /*
  4505. Since the TIFF DateTime string has no timezone information,
  4506. assume that the camera's clock was set to Universal Time.
  4507. */
  4508. void CLASS get_timestamp (int reversed)
  4509. {
  4510. struct tm t;
  4511. char str[20];
  4512. int i;
  4513. str[19] = 0;
  4514. if (reversed)
  4515. for (i=19; i--; ) str[i] = fgetc(ifp);
  4516. else
  4517. fread (str, 19, 1, ifp);
  4518. memset (&t, 0, sizeof t);
  4519. if (sscanf (str, "%d:%d:%d %d:%d:%d", &t.tm_year, &t.tm_mon,
  4520. &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec) != 6)
  4521. return;
  4522. t.tm_year -= 1900;
  4523. t.tm_mon -= 1;
  4524. t.tm_isdst = -1;
  4525. if (mktime(&t) > 0)
  4526. timestamp = mktime(&t);
  4527. }
  4528. void CLASS parse_exif (int base)
  4529. {
  4530. unsigned kodak, entries, tag, type, len, save, c;
  4531. double expo;
  4532. kodak = !strncmp(make,"EASTMAN",7) && tiff_nifds < 3;
  4533. entries = get2();
  4534. while (entries--) {
  4535. tiff_get (base, &tag, &type, &len, &save);
  4536. switch (tag) {
  4537. case 33434: shutter = getreal(type); break;
  4538. case 33437: aperture = getreal(type); break;
  4539. case 34855: iso_speed = get2(); break;
  4540. case 36867:
  4541. case 36868: get_timestamp(0); break;
  4542. case 37377: if ((expo = -getreal(type)) < 128)
  4543. shutter = pow (2, expo); break;
  4544. case 37378: aperture = pow (2, getreal(type)/2); break;
  4545. case 37386: focal_len = getreal(type); break;
  4546. case 37500: parse_makernote (base, 0); break;
  4547. case 40962: if (kodak) raw_width = get4(); break;
  4548. case 40963: if (kodak) raw_height = get4(); break;
  4549. case 41730:
  4550. if (get4() == 0x20002)
  4551. for (exif_cfa=c=0; c < 8; c+=2)
  4552. exif_cfa |= fgetc(ifp) * 0x01010101 << c;
  4553. }
  4554. fseek (ifp, save, SEEK_SET);
  4555. }
  4556. }
  4557. void CLASS parse_gps (int base)
  4558. {
  4559. unsigned entries, tag, type, len, save, c;
  4560. entries = get2();
  4561. while (entries--) {
  4562. tiff_get (base, &tag, &type, &len, &save);
  4563. switch (tag) {
  4564. case 1: case 3: case 5:
  4565. gpsdata[29+tag/2] = getc(ifp); break;
  4566. case 2: case 4: case 7:
  4567. FORC(6) gpsdata[tag/3*6+c] = get4(); break;
  4568. case 6:
  4569. FORC(2) gpsdata[18+c] = get4(); break;
  4570. case 18: case 29:
  4571. fgets ((char *) (gpsdata+14+tag/3), MIN(len,12), ifp);
  4572. }
  4573. fseek (ifp, save, SEEK_SET);
  4574. }
  4575. }
  4576. void CLASS romm_coeff (float romm_cam[3][3])
  4577. {
  4578. static const float rgb_romm[3][3] = /* ROMM == Kodak ProPhoto */
  4579. { { 2.034193, -0.727420, -0.306766 },
  4580. { -0.228811, 1.231729, -0.002922 },
  4581. { -0.008565, -0.153273, 1.161839 } };
  4582. int i, j, k;
  4583. for (i=0; i < 3; i++)
  4584. for (j=0; j < 3; j++)
  4585. for (cmatrix[i][j] = k=0; k < 3; k++)
  4586. cmatrix[i][j] += rgb_romm[i][k] * romm_cam[k][j];
  4587. }
  4588. void CLASS parse_mos (int offset)
  4589. {
  4590. char data[40];
  4591. int skip, from, i, c, neut[4], planes=0, frot=0;
  4592. static const char *mod[] =
  4593. { "","DCB2","Volare","Cantare","CMost","Valeo 6","Valeo 11","Valeo 22",
  4594. "Valeo 11p","Valeo 17","","Aptus 17","Aptus 22","Aptus 75","Aptus 65",
  4595. "Aptus 54S","Aptus 65S","Aptus 75S","AFi 5","AFi 6","AFi 7",
  4596. "","","","","","","","","","","","","","","","","","AFi-II 12" };
  4597. float romm_cam[3][3];
  4598. fseek (ifp, offset, SEEK_SET);
  4599. while (1) {
  4600. if (get4() != 0x504b5453) break;
  4601. get4();
  4602. fread (data, 1, 40, ifp);
  4603. skip = get4();
  4604. from = ftell(ifp);
  4605. if (!strcmp(data,"JPEG_preview_data")) {
  4606. thumb_offset = from;
  4607. thumb_length = skip;
  4608. }
  4609. if (!strcmp(data,"icc_camera_profile")) {
  4610. profile_offset = from;
  4611. profile_length = skip;
  4612. }
  4613. if (!strcmp(data,"ShootObj_back_type")) {
  4614. fscanf (ifp, "%d", &i);
  4615. if ((unsigned) i < sizeof mod / sizeof (*mod))
  4616. strcpy (model, mod[i]);
  4617. }
  4618. if (!strcmp(data,"icc_camera_to_tone_matrix")) {
  4619. for (i=0; i < 9; i++)
  4620. romm_cam[0][i] = int_to_float(get4());
  4621. romm_coeff (romm_cam);
  4622. }
  4623. if (!strcmp(data,"CaptProf_color_matrix")) {
  4624. for (i=0; i < 9; i++)
  4625. fscanf (ifp, "%f", &romm_cam[0][i]);
  4626. romm_coeff (romm_cam);
  4627. }
  4628. if (!strcmp(data,"CaptProf_number_of_planes"))
  4629. fscanf (ifp, "%d", &planes);
  4630. if (!strcmp(data,"CaptProf_raw_data_rotation"))
  4631. fscanf (ifp, "%d", &flip);
  4632. if (!strcmp(data,"CaptProf_mosaic_pattern"))
  4633. FORC4 {
  4634. fscanf (ifp, "%d", &i);
  4635. if (i == 1) frot = c ^ (c >> 1);
  4636. }
  4637. if (!strcmp(data,"ImgProf_rotation_angle")) {
  4638. fscanf (ifp, "%d", &i);
  4639. flip = i - flip;
  4640. }
  4641. if (!strcmp(data,"NeutObj_neutrals") && !cam_mul[0]) {
  4642. FORC4 fscanf (ifp, "%d", neut+c);
  4643. FORC3 cam_mul[c] = (float) neut[0] / neut[c+1];
  4644. }
  4645. if (!strcmp(data,"Rows_data"))
  4646. load_flags = get4();
  4647. parse_mos (from);
  4648. fseek (ifp, skip+from, SEEK_SET);
  4649. }
  4650. if (planes)
  4651. filters = (planes == 1) * 0x01010101 *
  4652. (uchar) "\x94\x61\x16\x49"[(flip/90 + frot) & 3];
  4653. }
  4654. void CLASS linear_table (unsigned len)
  4655. {
  4656. int i;
  4657. if (len > 0x1000) len = 0x1000;
  4658. read_shorts (curve, len);
  4659. for (i=len; i < 0x1000; i++)
  4660. curve[i] = curve[i-1];
  4661. maximum = curve[0xfff];
  4662. }
  4663. void CLASS parse_kodak_ifd (int base)
  4664. {
  4665. unsigned entries, tag, type, len, save;
  4666. int i, c, wbi=-2, wbtemp=6500;
  4667. float mul[3]={1,1,1}, num;
  4668. static const int wbtag[] = { 64037,64040,64039,64041,-1,-1,64042 };
  4669. entries = get2();
  4670. if (entries > 1024) return;
  4671. while (entries--) {
  4672. tiff_get (base, &tag, &type, &len, &save);
  4673. if (tag == 1020) wbi = getint(type);
  4674. if (tag == 1021 && len == 72) { /* WB set in software */
  4675. fseek (ifp, 40, SEEK_CUR);
  4676. FORC3 cam_mul[c] = 2048.0 / get2();
  4677. wbi = -2;
  4678. }
  4679. if (tag == 2118) wbtemp = getint(type);
  4680. if (tag == 2130 + wbi)
  4681. FORC3 mul[c] = getreal(type);
  4682. if (tag == 2140 + wbi && wbi >= 0)
  4683. FORC3 {
  4684. for (num=i=0; i < 4; i++)
  4685. num += getreal(type) * pow (wbtemp/100.0, i);
  4686. cam_mul[c] = 2048 / (num * mul[c]);
  4687. }
  4688. if (tag == 2317) linear_table (len);
  4689. if (tag == 6020) iso_speed = getint(type);
  4690. if (tag == 64013) wbi = fgetc(ifp);
  4691. if ((unsigned) wbi < 7 && tag == wbtag[wbi])
  4692. FORC3 cam_mul[c] = get4();
  4693. if (tag == 64019) width = getint(type);
  4694. if (tag == 64020) height = (getint(type)+1) & -2;
  4695. fseek (ifp, save, SEEK_SET);
  4696. }
  4697. }
  4698. #line 6144 "dcraw/dcraw.c"
  4699. int CLASS parse_tiff_ifd (int base)
  4700. {
  4701. unsigned entries, tag, type, len, plen=16, save;
  4702. int ifd, use_cm=0, cfa, i, j, c, ima_len=0;
  4703. int blrr=1, blrc=1, dblack[] = { 0,0,0,0 };
  4704. char software[64], *cbuf, *cp;
  4705. uchar cfa_pat[16], cfa_pc[] = { 0,1,2,3 }, tab[256];
  4706. double cc[4][4], cm[4][3], cam_xyz[4][3], num;
  4707. double ab[]={ 1,1,1,1 }, asn[] = { 0,0,0,0 }, xyz[] = { 1,1,1 };
  4708. unsigned sony_curve[] = { 0,0,0,0,0,4095 };
  4709. unsigned *buf, sony_offset=0, sony_length=0, sony_key=0;
  4710. struct jhead jh;
  4711. #ifndef LIBRAW_LIBRARY_BUILD
  4712. FILE *sfp;
  4713. #endif
  4714. if (tiff_nifds >= sizeof tiff_ifd / sizeof tiff_ifd[0])
  4715. return 1;
  4716. ifd = tiff_nifds++;
  4717. for (j=0; j < 4; j++)
  4718. for (i=0; i < 4; i++)
  4719. cc[j][i] = i == j;
  4720. entries = get2();
  4721. if (entries > 512) return 1;
  4722. while (entries--) {
  4723. tiff_get (base, &tag, &type, &len, &save);
  4724. switch (tag) {
  4725. case 5: width = get2(); break;
  4726. case 6: height = get2(); break;
  4727. case 7: width += get2(); break;
  4728. case 9: filters = get2(); break;
  4729. case 17: case 18:
  4730. if (type == 3 && len == 1)
  4731. cam_mul[(tag-17)*2] = get2() / 256.0;
  4732. break;
  4733. case 23:
  4734. if (type == 3) iso_speed = get2();
  4735. break;
  4736. case 36: case 37: case 38:
  4737. cam_mul[tag-0x24] = get2();
  4738. break;
  4739. case 39:
  4740. if (len < 50 || cam_mul[0]) break;
  4741. fseek (ifp, 12, SEEK_CUR);
  4742. FORC3 cam_mul[c] = get2();
  4743. break;
  4744. case 46:
  4745. if (type != 7 || fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8) break;
  4746. thumb_offset = ftell(ifp) - 2;
  4747. thumb_length = len;
  4748. break;
  4749. case 61440: /* Fuji HS10 table */
  4750. parse_tiff_ifd (base);
  4751. break;
  4752. case 2: case 256: case 61441: /* ImageWidth */
  4753. tiff_ifd[ifd].t_width = getint(type);
  4754. break;
  4755. case 3: case 257: case 61442: /* ImageHeight */
  4756. tiff_ifd[ifd].t_height = getint(type);
  4757. break;
  4758. case 258: /* BitsPerSample */
  4759. case 61443:
  4760. tiff_ifd[ifd].samples = len & 7;
  4761. tiff_ifd[ifd].bps = getint(type);
  4762. break;
  4763. case 61446:
  4764. raw_height = 0;
  4765. load_raw = &CLASS packed_load_raw;
  4766. load_flags = get4() && (filters=0x16161616) ? 24:80;
  4767. break;
  4768. case 259: /* Compression */
  4769. tiff_ifd[ifd].comp = getint(type);
  4770. break;
  4771. case 262: /* PhotometricInterpretation */
  4772. tiff_ifd[ifd].phint = get2();
  4773. break;
  4774. case 270: /* ImageDescription */
  4775. fread (desc, 512, 1, ifp);
  4776. break;
  4777. case 271: /* Make */
  4778. fgets (make, 64, ifp);
  4779. break;
  4780. case 272: /* Model */
  4781. fgets (model, 64, ifp);
  4782. break;
  4783. case 280: /* Panasonic RW2 offset */
  4784. if (type != 4) break;
  4785. load_raw = &CLASS panasonic_load_raw;
  4786. load_flags = 0x2008;
  4787. case 273: /* StripOffset */
  4788. case 513: /* JpegIFOffset */
  4789. case 61447:
  4790. tiff_ifd[ifd].offset = get4()+base;
  4791. if (!tiff_ifd[ifd].bps && tiff_ifd[ifd].offset > 0) {
  4792. fseek (ifp, tiff_ifd[ifd].offset, SEEK_SET);
  4793. if (ljpeg_start (&jh, 1)) {
  4794. tiff_ifd[ifd].comp = 6;
  4795. tiff_ifd[ifd].t_width = jh.wide;
  4796. tiff_ifd[ifd].t_height = jh.high;
  4797. tiff_ifd[ifd].bps = jh.bits;
  4798. tiff_ifd[ifd].samples = jh.clrs;
  4799. if (!(jh.sraw || (jh.clrs & 1)))
  4800. tiff_ifd[ifd].t_width *= jh.clrs;
  4801. i = order;
  4802. parse_tiff (tiff_ifd[ifd].offset + 12);
  4803. order = i;
  4804. }
  4805. }
  4806. break;
  4807. case 274: /* Orientation */
  4808. tiff_ifd[ifd].t_flip = "50132467"[get2() & 7]-'0';
  4809. break;
  4810. case 277: /* SamplesPerPixel */
  4811. tiff_ifd[ifd].samples = getint(type) & 7;
  4812. break;
  4813. case 279: /* StripByteCounts */
  4814. case 514:
  4815. case 61448:
  4816. tiff_ifd[ifd].bytes = get4();
  4817. break;
  4818. case 61454:
  4819. FORC3 cam_mul[(4-c) % 3] = getint(type);
  4820. break;
  4821. case 305: case 11: /* Software */
  4822. fgets (software, 64, ifp);
  4823. if (!strncmp(software,"Adobe",5) ||
  4824. !strncmp(software,"dcraw",5) ||
  4825. !strncmp(software,"UFRaw",5) ||
  4826. !strncmp(software,"Bibble",6) ||
  4827. !strncmp(software,"Nikon Scan",10) ||
  4828. !strcmp (software,"Digital Photo Professional"))
  4829. is_raw = 0;
  4830. break;
  4831. case 306: /* DateTime */
  4832. get_timestamp(0);
  4833. break;
  4834. case 315: /* Artist */
  4835. fread (artist, 64, 1, ifp);
  4836. break;
  4837. case 322: /* TileWidth */
  4838. tiff_ifd[ifd].t_tile_width = getint(type);
  4839. break;
  4840. case 323: /* TileLength */
  4841. tiff_ifd[ifd].t_tile_length = getint(type);
  4842. break;
  4843. case 324: /* TileOffsets */
  4844. tiff_ifd[ifd].offset = len > 1 ? ftell(ifp) : get4();
  4845. if (len == 4) {
  4846. load_raw = &CLASS sinar_4shot_load_raw;
  4847. is_raw = 5;
  4848. }
  4849. break;
  4850. #ifdef LIBRAW_LIBRARY_BUILD
  4851. case 325: /* TileByteCount */
  4852. tiff_ifd[ifd].tile_maxbytes = 0;
  4853. for(int jj=0;jj<len;jj++)
  4854. {
  4855. int s = get4();
  4856. if(s > tiff_ifd[ifd].tile_maxbytes) tiff_ifd[ifd].tile_maxbytes=s;
  4857. }
  4858. break;
  4859. #endif
  4860. case 330: /* SubIFDs */
  4861. if (!strcmp(model,"DSLR-A100") && tiff_ifd[ifd].t_width == 3872) {
  4862. load_raw = &CLASS sony_arw_load_raw;
  4863. data_offset = get4()+base;
  4864. ifd++; break;
  4865. }
  4866. while (len--) {
  4867. i = ftell(ifp);
  4868. fseek (ifp, get4()+base, SEEK_SET);
  4869. if (parse_tiff_ifd (base)) break;
  4870. fseek (ifp, i+4, SEEK_SET);
  4871. }
  4872. break;
  4873. case 400:
  4874. strcpy (make, "Sarnoff");
  4875. maximum = 0xfff;
  4876. break;
  4877. case 28688:
  4878. FORC4 sony_curve[c+1] = get2() >> 2 & 0xfff;
  4879. for (i=0; i < 5; i++)
  4880. for (j = sony_curve[i]+1; j <= sony_curve[i+1]; j++)
  4881. curve[j] = curve[j-1] + (1 << i);
  4882. break;
  4883. case 29184: sony_offset = get4(); break;
  4884. case 29185: sony_length = get4(); break;
  4885. case 29217: sony_key = get4(); break;
  4886. case 29264:
  4887. parse_minolta (ftell(ifp));
  4888. raw_width = 0;
  4889. break;
  4890. case 29443:
  4891. FORC4 cam_mul[c ^ (c < 2)] = get2();
  4892. break;
  4893. case 29459:
  4894. FORC4 cam_mul[c] = get2();
  4895. i = (cam_mul[1] == 1024 && cam_mul[2] == 1024) << 1;
  4896. SWAP (cam_mul[i],cam_mul[i+1])
  4897. break;
  4898. case 33405: /* Model2 */
  4899. fgets (model2, 64, ifp);
  4900. break;
  4901. case 33422: /* CFAPattern */
  4902. case 64777: /* Kodak P-series */
  4903. if ((plen=len) > 16) plen = 16;
  4904. fread (cfa_pat, 1, plen, ifp);
  4905. for (colors=cfa=i=0; i < plen; i++) {
  4906. colors += !(cfa & (1 << cfa_pat[i]));
  4907. cfa |= 1 << cfa_pat[i];
  4908. }
  4909. if (cfa == 070) memcpy (cfa_pc,"\003\004\005",3); /* CMY */
  4910. if (cfa == 072) memcpy (cfa_pc,"\005\003\004\001",4); /* GMCY */
  4911. goto guess_cfa_pc;
  4912. case 33424:
  4913. case 65024:
  4914. fseek (ifp, get4()+base, SEEK_SET);
  4915. parse_kodak_ifd (base);
  4916. break;
  4917. case 33434: /* ExposureTime */
  4918. shutter = getreal(type);
  4919. break;
  4920. case 33437: /* FNumber */
  4921. aperture = getreal(type);
  4922. break;
  4923. case 34306: /* Leaf white balance */
  4924. FORC4 cam_mul[c ^ 1] = 4096.0 / get2();
  4925. break;
  4926. case 34307: /* Leaf CatchLight color matrix */
  4927. fread (software, 1, 7, ifp);
  4928. if (strncmp(software,"MATRIX",6)) break;
  4929. colors = 4;
  4930. for (raw_color = i=0; i < 3; i++) {
  4931. FORC4 fscanf (ifp, "%f", &rgb_cam[i][c^1]);
  4932. if (!use_camera_wb) continue;
  4933. num = 0;
  4934. FORC4 num += rgb_cam[i][c];
  4935. FORC4 rgb_cam[i][c] /= num;
  4936. }
  4937. break;
  4938. case 34310: /* Leaf metadata */
  4939. parse_mos (ftell(ifp));
  4940. case 34303:
  4941. strcpy (make, "Leaf");
  4942. break;
  4943. case 34665: /* EXIF tag */
  4944. fseek (ifp, get4()+base, SEEK_SET);
  4945. parse_exif (base);
  4946. break;
  4947. case 34853: /* GPSInfo tag */
  4948. fseek (ifp, get4()+base, SEEK_SET);
  4949. parse_gps (base);
  4950. break;
  4951. case 34675: /* InterColorProfile */
  4952. case 50831: /* AsShotICCProfile */
  4953. profile_offset = ftell(ifp);
  4954. profile_length = len;
  4955. break;
  4956. case 37122: /* CompressedBitsPerPixel */
  4957. kodak_cbpp = get4();
  4958. break;
  4959. case 37386: /* FocalLength */
  4960. focal_len = getreal(type);
  4961. break;
  4962. case 37393: /* ImageNumber */
  4963. shot_order = getint(type);
  4964. break;
  4965. case 37400: /* old Kodak KDC tag */
  4966. for (raw_color = i=0; i < 3; i++) {
  4967. getreal(type);
  4968. FORC3 rgb_cam[i][c] = getreal(type);
  4969. }
  4970. break;
  4971. case 46275: /* Imacon tags */
  4972. strcpy (make, "Imacon");
  4973. data_offset = ftell(ifp);
  4974. ima_len = len;
  4975. printf("Data len: %d\n",ima_len);
  4976. break;
  4977. case 46279:
  4978. if (!ima_len) break;
  4979. fseek (ifp, 38, SEEK_CUR);
  4980. case 46274:
  4981. fseek (ifp, 40, SEEK_CUR);
  4982. raw_width = get4();
  4983. raw_height = get4();
  4984. left_margin = get4() & 7;
  4985. width = raw_width - left_margin - (get4() & 7);
  4986. top_margin = get4() & 7;
  4987. height = raw_height - top_margin - (get4() & 7);
  4988. if (raw_width == 7262 && ima_len == 234317952 ) {
  4989. height = 5412;
  4990. width = 7216;
  4991. left_margin = 7;
  4992. filters=0;
  4993. } else if (raw_width == 7262) {
  4994. height = 5444;
  4995. width = 7244;
  4996. left_margin = 7;
  4997. }
  4998. fseek (ifp, 52, SEEK_CUR);
  4999. FORC3 cam_mul[c] = getreal(11);
  5000. fseek (ifp, 114, SEEK_CUR);
  5001. flip = (get2() >> 7) * 90;
  5002. if (width * height * 6 == ima_len) {
  5003. if (flip % 180 == 90) SWAP(width,height);
  5004. raw_width = width;
  5005. raw_height = height;
  5006. left_margin = top_margin = filters = flip = 0;
  5007. }
  5008. sprintf (model, "Ixpress %d-Mp", height*width/1000000);
  5009. load_raw = &CLASS imacon_full_load_raw;
  5010. if (filters) {
  5011. if (left_margin & 1) filters = 0x61616161;
  5012. load_raw = &CLASS unpacked_load_raw;
  5013. }
  5014. maximum = 0xffff;
  5015. break;
  5016. case 50454: /* Sinar tag */
  5017. case 50455:
  5018. if (!(cbuf = (char *) malloc(len))) break;
  5019. fread (cbuf, 1, len, ifp);
  5020. for (cp = cbuf-1; cp && cp < cbuf+len; cp = strchr(cp,'\n'))
  5021. if (!strncmp (++cp,"Neutral ",8))
  5022. sscanf (cp+8, "%f %f %f", cam_mul, cam_mul+1, cam_mul+2);
  5023. free (cbuf);
  5024. break;
  5025. case 50458:
  5026. if (!make[0]) strcpy (make, "Hasselblad");
  5027. break;
  5028. case 50459: /* Hasselblad tag */
  5029. i = order;
  5030. j = ftell(ifp);
  5031. c = tiff_nifds;
  5032. order = get2();
  5033. fseek (ifp, j+(get2(),get4()), SEEK_SET);
  5034. parse_tiff_ifd (j);
  5035. maximum = 0xffff;
  5036. tiff_nifds = c;
  5037. order = i;
  5038. break;
  5039. case 50706: /* DNGVersion */
  5040. FORC4 dng_version = (dng_version << 8) + fgetc(ifp);
  5041. if (!make[0]) strcpy (make, "DNG");
  5042. is_raw = 1;
  5043. break;
  5044. case 50710: /* CFAPlaneColor */
  5045. if (len > 4) len = 4;
  5046. colors = len;
  5047. fread (cfa_pc, 1, colors, ifp);
  5048. guess_cfa_pc:
  5049. FORCC tab[cfa_pc[c]] = c;
  5050. cdesc[c] = 0;
  5051. for (i=16; i--; )
  5052. filters = filters << 2 | tab[cfa_pat[i % plen]];
  5053. break;
  5054. case 50711: /* CFALayout */
  5055. if (get2() == 2) {
  5056. fuji_width = 1;
  5057. filters = 0x49494949;
  5058. }
  5059. break;
  5060. case 291:
  5061. case 50712: /* LinearizationTable */
  5062. linear_table (len);
  5063. break;
  5064. case 50713: /* BlackLevelRepeatDim */
  5065. blrr = get2();
  5066. blrc = get2();
  5067. break;
  5068. case 61450:
  5069. blrr = blrc = 2;
  5070. case 50714: /* BlackLevel */
  5071. black = getreal(type);
  5072. if (!filters || !~filters) break;
  5073. dblack[0] = black;
  5074. dblack[1] = (blrc == 2) ? getreal(type):dblack[0];
  5075. dblack[2] = (blrr == 2) ? getreal(type):dblack[0];
  5076. dblack[3] = (blrc == 2 && blrr == 2) ? getreal(type):dblack[1];
  5077. if (colors == 3)
  5078. filters |= ((filters >> 2 & 0x22222222) |
  5079. (filters << 2 & 0x88888888)) & filters << 1;
  5080. FORC4 cblack[filters >> (c << 1) & 3] = dblack[c];
  5081. black = 0;
  5082. break;
  5083. case 50715: /* BlackLevelDeltaH */
  5084. case 50716: /* BlackLevelDeltaV */
  5085. for (num=i=0; i < len; i++)
  5086. num += getreal(type);
  5087. black += num/len + 0.5;
  5088. break;
  5089. case 50717: /* WhiteLevel */
  5090. maximum = getint(type);
  5091. break;
  5092. case 50718: /* DefaultScale */
  5093. pixel_aspect = getreal(type);
  5094. pixel_aspect /= getreal(type);
  5095. break;
  5096. case 50721: /* ColorMatrix1 */
  5097. case 50722: /* ColorMatrix2 */
  5098. FORCC for (j=0; j < 3; j++)
  5099. cm[c][j] = getreal(type);
  5100. use_cm = 1;
  5101. break;
  5102. case 50723: /* CameraCalibration1 */
  5103. case 50724: /* CameraCalibration2 */
  5104. for (i=0; i < colors; i++)
  5105. FORCC cc[i][c] = getreal(type);
  5106. break;
  5107. case 50727: /* AnalogBalance */
  5108. FORCC ab[c] = getreal(type);
  5109. break;
  5110. case 50728: /* AsShotNeutral */
  5111. FORCC asn[c] = getreal(type);
  5112. break;
  5113. case 50729: /* AsShotWhiteXY */
  5114. xyz[0] = getreal(type);
  5115. xyz[1] = getreal(type);
  5116. xyz[2] = 1 - xyz[0] - xyz[1];
  5117. FORC3 xyz[c] /= d65_white[c];
  5118. break;
  5119. case 50740: /* DNGPrivateData */
  5120. if (dng_version) break;
  5121. parse_minolta (j = get4()+base);
  5122. fseek (ifp, j, SEEK_SET);
  5123. parse_tiff_ifd (base);
  5124. break;
  5125. case 50752:
  5126. read_shorts (cr2_slice, 3);
  5127. break;
  5128. case 50829: /* ActiveArea */
  5129. top_margin = getint(type);
  5130. left_margin = getint(type);
  5131. height = getint(type) - top_margin;
  5132. width = getint(type) - left_margin;
  5133. break;
  5134. case 50830: /* MaskedAreas */
  5135. for (i=0; i < len && i < 32; i++)
  5136. mask[0][i] = getint(type);
  5137. black = 0;
  5138. break;
  5139. case 51009: /* OpcodeList2 */
  5140. meta_offset = ftell(ifp);
  5141. break;
  5142. case 64772: /* Kodak P-series */
  5143. if (len < 13) break;
  5144. fseek (ifp, 16, SEEK_CUR);
  5145. data_offset = get4();
  5146. fseek (ifp, 28, SEEK_CUR);
  5147. data_offset += get4();
  5148. load_raw = &CLASS packed_load_raw;
  5149. break;
  5150. case 65026:
  5151. if (type == 2) fgets (model2, 64, ifp);
  5152. }
  5153. fseek (ifp, save, SEEK_SET);
  5154. }
  5155. if (sony_length && (buf = (unsigned *) malloc(sony_length))) {
  5156. fseek (ifp, sony_offset, SEEK_SET);
  5157. fread (buf, sony_length, 1, ifp);
  5158. sony_decrypt (buf, sony_length/4, 1, sony_key);
  5159. #ifndef LIBRAW_LIBRARY_BUILD
  5160. sfp = ifp;
  5161. if ((ifp = tmpfile())) {
  5162. fwrite (buf, sony_length, 1, ifp);
  5163. fseek (ifp, 0, SEEK_SET);
  5164. parse_tiff_ifd (-sony_offset);
  5165. fclose (ifp);
  5166. }
  5167. ifp = sfp;
  5168. #else
  5169. if( !ifp->tempbuffer_open(buf,sony_length))
  5170. {
  5171. parse_tiff_ifd(-sony_offset);
  5172. ifp->tempbuffer_close();
  5173. }
  5174. #endif
  5175. free (buf);
  5176. }
  5177. for (i=0; i < colors; i++)
  5178. FORCC cc[i][c] *= ab[i];
  5179. if (use_cm) {
  5180. FORCC for (i=0; i < 3; i++)
  5181. for (cam_xyz[c][i]=j=0; j < colors; j++)
  5182. cam_xyz[c][i] += cc[c][j] * cm[j][i] * xyz[i];
  5183. cam_xyz_coeff (cam_xyz);
  5184. }
  5185. if (asn[0]) {
  5186. cam_mul[3] = 0;
  5187. FORCC cam_mul[c] = 1 / asn[c];
  5188. }
  5189. if (!use_cm)
  5190. FORCC pre_mul[c] /= cc[c][c];
  5191. return 0;
  5192. }
  5193. int CLASS parse_tiff (int base)
  5194. {
  5195. int doff;
  5196. fseek (ifp, base, SEEK_SET);
  5197. order = get2();
  5198. if (order != 0x4949 && order != 0x4d4d) return 0;
  5199. get2();
  5200. while ((doff = get4())) {
  5201. fseek (ifp, doff+base, SEEK_SET);
  5202. if (parse_tiff_ifd (base)) break;
  5203. }
  5204. return 1;
  5205. }
  5206. void CLASS apply_tiff()
  5207. {
  5208. int max_samp=0, raw=-1, thm=-1, i;
  5209. struct jhead jh;
  5210. thumb_misc = 16;
  5211. if (thumb_offset) {
  5212. fseek (ifp, thumb_offset, SEEK_SET);
  5213. if (ljpeg_start (&jh, 1)) {
  5214. thumb_misc = jh.bits;
  5215. thumb_width = jh.wide;
  5216. thumb_height = jh.high;
  5217. }
  5218. }
  5219. for (i=0; i < tiff_nifds; i++) {
  5220. if (max_samp < tiff_ifd[i].samples)
  5221. max_samp = tiff_ifd[i].samples;
  5222. if (max_samp > 3) max_samp = 3;
  5223. if ((tiff_ifd[i].comp != 6 || tiff_ifd[i].samples != 3) &&
  5224. (tiff_ifd[i].t_width | tiff_ifd[i].t_height) < 0x10000 &&
  5225. tiff_ifd[i].t_width*tiff_ifd[i].t_height > raw_width*raw_height) {
  5226. raw_width = tiff_ifd[i].t_width;
  5227. raw_height = tiff_ifd[i].t_height;
  5228. tiff_bps = tiff_ifd[i].bps;
  5229. tiff_compress = tiff_ifd[i].comp;
  5230. data_offset = tiff_ifd[i].offset;
  5231. tiff_flip = tiff_ifd[i].t_flip;
  5232. tiff_samples = tiff_ifd[i].samples;
  5233. tile_width = tiff_ifd[i].t_tile_width;
  5234. tile_length = tiff_ifd[i].t_tile_length;
  5235. #ifdef LIBRAW_LIBRARY_BUILD
  5236. data_size = tile_length < INT_MAX && tile_length>0 ? tiff_ifd[i].tile_maxbytes: tiff_ifd[i].bytes;
  5237. #endif
  5238. raw = i;
  5239. }
  5240. }
  5241. if (!tile_width ) tile_width = INT_MAX;
  5242. if (!tile_length) tile_length = INT_MAX;
  5243. for (i=tiff_nifds; i--; )
  5244. if (tiff_ifd[i].t_flip) tiff_flip = tiff_ifd[i].t_flip;
  5245. if (raw >= 0 && !load_raw)
  5246. switch (tiff_compress) {
  5247. case 32767:
  5248. if (tiff_ifd[raw].bytes == raw_width*raw_height) {
  5249. tiff_bps = 12;
  5250. load_raw = &CLASS sony_arw2_load_raw; break;
  5251. }
  5252. if (tiff_ifd[raw].bytes*8 != raw_width*raw_height*tiff_bps) {
  5253. raw_height += 8;
  5254. load_raw = &CLASS sony_arw_load_raw; break;
  5255. }
  5256. load_flags = 79;
  5257. case 32769:
  5258. load_flags++;
  5259. case 32770:
  5260. case 32773: goto slr;
  5261. case 0: case 1:
  5262. if (tiff_ifd[raw].bytes*5 == raw_width*raw_height*8) {
  5263. load_flags = 81;
  5264. tiff_bps = 12;
  5265. } slr:
  5266. switch (tiff_bps) {
  5267. case 8: load_raw = &CLASS eight_bit_load_raw; break;
  5268. case 12: if (tiff_ifd[raw].phint == 2)
  5269. load_flags = 6;
  5270. load_raw = &CLASS packed_load_raw; break;
  5271. case 14: load_flags = 0;
  5272. case 16: load_raw = &CLASS unpacked_load_raw; break;
  5273. }
  5274. break;
  5275. case 6: case 7: case 99:
  5276. load_raw = &CLASS lossless_jpeg_load_raw; break;
  5277. case 262:
  5278. load_raw = &CLASS kodak_262_load_raw; break;
  5279. case 34713:
  5280. if ((raw_width+9)/10*16*raw_height == tiff_ifd[raw].bytes) {
  5281. load_raw = &CLASS packed_load_raw;
  5282. load_flags = 1;
  5283. } else if (raw_width*raw_height*2 == tiff_ifd[raw].bytes) {
  5284. load_raw = &CLASS unpacked_load_raw;
  5285. load_flags = 4;
  5286. order = 0x4d4d;
  5287. } else
  5288. load_raw = &CLASS nikon_load_raw; break;
  5289. case 34892:
  5290. load_raw = &CLASS lossy_dng_load_raw; break;
  5291. case 65535:
  5292. load_raw = &CLASS pentax_load_raw; break;
  5293. case 65000:
  5294. switch (tiff_ifd[raw].phint) {
  5295. case 2: load_raw = &CLASS kodak_rgb_load_raw; filters = 0; break;
  5296. case 6: load_raw = &CLASS kodak_ycbcr_load_raw; filters = 0; break;
  5297. case 32803: load_raw = &CLASS kodak_65000_load_raw;
  5298. }
  5299. case 32867: break;
  5300. default: is_raw = 0;
  5301. }
  5302. if (!dng_version)
  5303. if ( (tiff_samples == 3 && tiff_ifd[raw].bytes &&
  5304. tiff_bps != 14 && tiff_bps != 2048 &&
  5305. tiff_compress != 32769 && tiff_compress != 32770)
  5306. || (tiff_bps == 8 && !strstr(make,"KODAK") && !strstr(make,"Kodak") &&
  5307. !strstr(model2,"DEBUG RAW")))
  5308. is_raw = 0;
  5309. for (i=0; i < tiff_nifds; i++)
  5310. if (i != raw && tiff_ifd[i].samples == max_samp &&
  5311. tiff_ifd[i].t_width * tiff_ifd[i].t_height / SQR(tiff_ifd[i].bps+1) >
  5312. thumb_width * thumb_height / SQR(thumb_misc+1)
  5313. && tiff_ifd[i].comp != 34892) {
  5314. thumb_width = tiff_ifd[i].t_width;
  5315. thumb_height = tiff_ifd[i].t_height;
  5316. thumb_offset = tiff_ifd[i].offset;
  5317. thumb_length = tiff_ifd[i].bytes;
  5318. thumb_misc = tiff_ifd[i].bps;
  5319. thm = i;
  5320. }
  5321. if (thm >= 0) {
  5322. thumb_misc |= tiff_ifd[thm].samples << 5;
  5323. switch (tiff_ifd[thm].comp) {
  5324. case 0:
  5325. write_thumb = &CLASS layer_thumb;
  5326. break;
  5327. case 1:
  5328. if (tiff_ifd[thm].bps <= 8)
  5329. write_thumb = &CLASS ppm_thumb;
  5330. else if (!strcmp(make,"Imacon"))
  5331. write_thumb = &CLASS ppm16_thumb;
  5332. else
  5333. thumb_load_raw = &CLASS kodak_thumb_load_raw;
  5334. break;
  5335. case 65000:
  5336. thumb_load_raw = tiff_ifd[thm].phint == 6 ?
  5337. &CLASS kodak_ycbcr_load_raw : &CLASS kodak_rgb_load_raw;
  5338. }
  5339. }
  5340. }
  5341. void CLASS parse_minolta (int base)
  5342. {
  5343. int save, tag, len, offset, high=0, wide=0, i, c;
  5344. short sorder=order;
  5345. fseek (ifp, base, SEEK_SET);
  5346. if (fgetc(ifp) || fgetc(ifp)-'M' || fgetc(ifp)-'R') return;
  5347. order = fgetc(ifp) * 0x101;
  5348. offset = base + get4() + 8;
  5349. while ((save=ftell(ifp)) < offset) {
  5350. for (tag=i=0; i < 4; i++)
  5351. tag = tag << 8 | fgetc(ifp);
  5352. len = get4();
  5353. switch (tag) {
  5354. case 0x505244: /* PRD */
  5355. fseek (ifp, 8, SEEK_CUR);
  5356. high = get2();
  5357. wide = get2();
  5358. break;
  5359. case 0x574247: /* WBG */
  5360. get4();
  5361. i = strcmp(model,"DiMAGE A200") ? 0:3;
  5362. FORC4 cam_mul[c ^ (c >> 1) ^ i] = get2();
  5363. break;
  5364. case 0x545457: /* TTW */
  5365. parse_tiff (ftell(ifp));
  5366. data_offset = offset;
  5367. }
  5368. fseek (ifp, save+len+8, SEEK_SET);
  5369. }
  5370. raw_height = high;
  5371. raw_width = wide;
  5372. order = sorder;
  5373. }
  5374. /*
  5375. Many cameras have a "debug mode" that writes JPEG and raw
  5376. at the same time. The raw file has no header, so try to
  5377. to open the matching JPEG file and read its metadata.
  5378. */
  5379. void CLASS parse_external_jpeg()
  5380. {
  5381. const char *file, *ext;
  5382. char *jname, *jfile, *jext;
  5383. #ifndef LIBRAW_LIBRARY_BUILD
  5384. FILE *save=ifp;
  5385. #else
  5386. #ifdef WIN32
  5387. if(ifp->wfname())
  5388. {
  5389. std::wstring rawfile(ifp->wfname());
  5390. rawfile.replace(rawfile.length()-3,3,L"JPG");
  5391. if(!ifp->subfile_open(rawfile.c_str()))
  5392. {
  5393. parse_tiff (12);
  5394. thumb_offset = 0;
  5395. is_raw = 1;
  5396. ifp->subfile_close();
  5397. }
  5398. else
  5399. imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA ;
  5400. return;
  5401. }
  5402. #endif
  5403. if(!ifp->fname())
  5404. {
  5405. imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA ;
  5406. return;
  5407. }
  5408. #endif
  5409. ext = strrchr (ifname, '.');
  5410. file = strrchr (ifname, '/');
  5411. if (!file) file = strrchr (ifname, '\\');
  5412. #ifndef LIBRAW_LIBRARY_BUILD
  5413. if (!file) file = ifname-1;
  5414. #else
  5415. if (!file) file = (char*)ifname-1;
  5416. #endif
  5417. file++;
  5418. if (!ext || strlen(ext) != 4 || ext-file != 8) return;
  5419. jname = (char *) malloc (strlen(ifname) + 1);
  5420. merror (jname, "parse_external_jpeg()");
  5421. strcpy (jname, ifname);
  5422. jfile = file - ifname + jname;
  5423. jext = ext - ifname + jname;
  5424. if (strcasecmp (ext, ".jpg")) {
  5425. strcpy (jext, isupper(ext[1]) ? ".JPG":".jpg");
  5426. if (isdigit(*file)) {
  5427. memcpy (jfile, file+4, 4);
  5428. memcpy (jfile+4, file, 4);
  5429. }
  5430. } else
  5431. while (isdigit(*--jext)) {
  5432. if (*jext != '9') {
  5433. (*jext)++;
  5434. break;
  5435. }
  5436. *jext = '0';
  5437. }
  5438. #ifndef LIBRAW_LIBRARY_BUILD
  5439. if (strcmp (jname, ifname)) {
  5440. if ((ifp = fopen (jname, "rb"))) {
  5441. #ifdef DCRAW_VERBOSE
  5442. if (verbose)
  5443. fprintf (stderr,_("Reading metadata from %s ...\n"), jname);
  5444. #endif
  5445. parse_tiff (12);
  5446. thumb_offset = 0;
  5447. is_raw = 1;
  5448. fclose (ifp);
  5449. }
  5450. }
  5451. #else
  5452. if (strcmp (jname, ifname))
  5453. {
  5454. if(!ifp->subfile_open(jname))
  5455. {
  5456. parse_tiff (12);
  5457. thumb_offset = 0;
  5458. is_raw = 1;
  5459. ifp->subfile_close();
  5460. }
  5461. else
  5462. imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA ;
  5463. }
  5464. #endif
  5465. if (!timestamp)
  5466. {
  5467. #ifdef LIBRAW_LIBRARY_BUILD
  5468. imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA ;
  5469. #endif
  5470. #ifdef DCRAW_VERBOSE
  5471. fprintf (stderr,_("Failed to read metadata from %s\n"), jname);
  5472. #endif
  5473. }
  5474. free (jname);
  5475. #ifndef LIBRAW_LIBRARY_BUILD
  5476. ifp = save;
  5477. #endif
  5478. }
  5479. /*
  5480. CIFF block 0x1030 contains an 8x8 white sample.
  5481. Load this into white[][] for use in scale_colors().
  5482. */
  5483. void CLASS ciff_block_1030()
  5484. {
  5485. static const ushort key[] = { 0x410, 0x45f3 };
  5486. int i, bpp, row, col, vbits=0;
  5487. unsigned long bitbuf=0;
  5488. if ((get2(),get4()) != 0x80008 || !get4()) return;
  5489. bpp = get2();
  5490. if (bpp != 10 && bpp != 12) return;
  5491. for (i=row=0; row < 8; row++)
  5492. for (col=0; col < 8; col++) {
  5493. if (vbits < bpp) {
  5494. bitbuf = bitbuf << 16 | (get2() ^ key[i++ & 1]);
  5495. vbits += 16;
  5496. }
  5497. white[row][col] =
  5498. bitbuf << (LONG_BIT - vbits) >> (LONG_BIT - bpp);
  5499. vbits -= bpp;
  5500. }
  5501. }
  5502. /*
  5503. Parse a CIFF file, better known as Canon CRW format.
  5504. */
  5505. void CLASS parse_ciff (int offset, int length)
  5506. {
  5507. int tboff, nrecs, c, type, len, save, wbi=-1;
  5508. ushort key[] = { 0x410, 0x45f3 };
  5509. fseek (ifp, offset+length-4, SEEK_SET);
  5510. tboff = get4() + offset;
  5511. fseek (ifp, tboff, SEEK_SET);
  5512. nrecs = get2();
  5513. if (nrecs > 100) return;
  5514. while (nrecs--) {
  5515. type = get2();
  5516. len = get4();
  5517. save = ftell(ifp) + 4;
  5518. fseek (ifp, offset+get4(), SEEK_SET);
  5519. if ((((type >> 8) + 8) | 8) == 0x38)
  5520. parse_ciff (ftell(ifp), len); /* Parse a sub-table */
  5521. if (type == 0x0810)
  5522. fread (artist, 64, 1, ifp);
  5523. if (type == 0x080a) {
  5524. fread (make, 64, 1, ifp);
  5525. fseek (ifp, strlen(make) - 63, SEEK_CUR);
  5526. fread (model, 64, 1, ifp);
  5527. }
  5528. if (type == 0x1810) {
  5529. fseek (ifp, 12, SEEK_CUR);
  5530. flip = get4();
  5531. }
  5532. if (type == 0x1835) /* Get the decoder table */
  5533. tiff_compress = get4();
  5534. if (type == 0x2007) {
  5535. thumb_offset = ftell(ifp);
  5536. thumb_length = len;
  5537. }
  5538. if (type == 0x1818) {
  5539. shutter = pow (2, -int_to_float((get4(),get4())));
  5540. aperture = pow (2, int_to_float(get4())/2);
  5541. }
  5542. if (type == 0x102a) {
  5543. iso_speed = pow (2, (get4(),get2())/32.0 - 4) * 50;
  5544. aperture = pow (2, (get2(),(short)get2())/64.0);
  5545. shutter = pow (2,-((short)get2())/32.0);
  5546. wbi = (get2(),get2());
  5547. if (wbi > 17) wbi = 0;
  5548. fseek (ifp, 32, SEEK_CUR);
  5549. if (shutter > 1e6) shutter = get2()/10.0;
  5550. }
  5551. if (type == 0x102c) {
  5552. if (get2() > 512) { /* Pro90, G1 */
  5553. fseek (ifp, 118, SEEK_CUR);
  5554. FORC4 cam_mul[c ^ 2] = get2();
  5555. } else { /* G2, S30, S40 */
  5556. fseek (ifp, 98, SEEK_CUR);
  5557. FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get2();
  5558. }
  5559. }
  5560. if (type == 0x0032) {
  5561. if (len == 768) { /* EOS D30 */
  5562. fseek (ifp, 72, SEEK_CUR);
  5563. FORC4 cam_mul[c ^ (c >> 1)] = 1024.0 / get2();
  5564. if (!wbi) cam_mul[0] = -1; /* use my auto white balance */
  5565. } else if (!cam_mul[0]) {
  5566. if (get2() == key[0]) /* Pro1, G6, S60, S70 */
  5567. c = (strstr(model,"Pro1") ?
  5568. "012346000000000000":"01345:000000006008")[wbi]-'0'+ 2;
  5569. else { /* G3, G5, S45, S50 */
  5570. c = "023457000000006000"[wbi]-'0';
  5571. key[0] = key[1] = 0;
  5572. }
  5573. fseek (ifp, 78 + c*8, SEEK_CUR);
  5574. FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get2() ^ key[c & 1];
  5575. if (!wbi) cam_mul[0] = -1;
  5576. }
  5577. }
  5578. if (type == 0x10a9) { /* D60, 10D, 300D, and clones */
  5579. if (len > 66) wbi = "0134567028"[wbi]-'0';
  5580. fseek (ifp, 2 + wbi*8, SEEK_CUR);
  5581. FORC4 cam_mul[c ^ (c >> 1)] = get2();
  5582. }
  5583. if (type == 0x1030 && (0x18040 >> wbi & 1))
  5584. ciff_block_1030(); /* all that don't have 0x10a9 */
  5585. if (type == 0x1031) {
  5586. raw_width = (get2(),get2());
  5587. raw_height = get2();
  5588. }
  5589. if (type == 0x5029) {
  5590. focal_len = len >> 16;
  5591. if ((len & 0xffff) == 2) focal_len /= 32;
  5592. }
  5593. if (type == 0x5813) flash_used = int_to_float(len);
  5594. if (type == 0x5814) canon_ev = int_to_float(len);
  5595. if (type == 0x5817) shot_order = len;
  5596. if (type == 0x5834) unique_id = len;
  5597. if (type == 0x580e) timestamp = len;
  5598. if (type == 0x180e) timestamp = get4();
  5599. #ifdef LOCALTIME
  5600. if ((type | 0x4000) == 0x580e)
  5601. timestamp = mktime (gmtime (&timestamp));
  5602. #endif
  5603. fseek (ifp, save, SEEK_SET);
  5604. }
  5605. }
  5606. void CLASS parse_rollei()
  5607. {
  5608. char line[128], *val;
  5609. struct tm t;
  5610. fseek (ifp, 0, SEEK_SET);
  5611. memset (&t, 0, sizeof t);
  5612. do {
  5613. fgets (line, 128, ifp);
  5614. if ((val = strchr(line,'=')))
  5615. *val++ = 0;
  5616. else
  5617. val = line + strlen(line);
  5618. if (!strcmp(line,"DAT"))
  5619. sscanf (val, "%d.%d.%d", &t.tm_mday, &t.tm_mon, &t.tm_year);
  5620. if (!strcmp(line,"TIM"))
  5621. sscanf (val, "%d:%d:%d", &t.tm_hour, &t.tm_min, &t.tm_sec);
  5622. if (!strcmp(line,"HDR"))
  5623. thumb_offset = atoi(val);
  5624. if (!strcmp(line,"X "))
  5625. raw_width = atoi(val);
  5626. if (!strcmp(line,"Y "))
  5627. raw_height = atoi(val);
  5628. if (!strcmp(line,"TX "))
  5629. thumb_width = atoi(val);
  5630. if (!strcmp(line,"TY "))
  5631. thumb_height = atoi(val);
  5632. } while (strncmp(line,"EOHD",4));
  5633. data_offset = thumb_offset + thumb_width * thumb_height * 2;
  5634. t.tm_year -= 1900;
  5635. t.tm_mon -= 1;
  5636. if (mktime(&t) > 0)
  5637. timestamp = mktime(&t);
  5638. strcpy (make, "Rollei");
  5639. strcpy (model,"d530flex");
  5640. write_thumb = &CLASS rollei_thumb;
  5641. }
  5642. void CLASS parse_sinar_ia()
  5643. {
  5644. int entries, off;
  5645. char str[8], *cp;
  5646. order = 0x4949;
  5647. fseek (ifp, 4, SEEK_SET);
  5648. entries = get4();
  5649. fseek (ifp, get4(), SEEK_SET);
  5650. while (entries--) {
  5651. off = get4(); get4();
  5652. fread (str, 8, 1, ifp);
  5653. if (!strcmp(str,"META")) meta_offset = off;
  5654. if (!strcmp(str,"THUMB")) thumb_offset = off;
  5655. if (!strcmp(str,"RAW0")) data_offset = off;
  5656. }
  5657. fseek (ifp, meta_offset+20, SEEK_SET);
  5658. fread (make, 64, 1, ifp);
  5659. make[63] = 0;
  5660. if ((cp = strchr(make,' '))) {
  5661. strcpy (model, cp+1);
  5662. *cp = 0;
  5663. }
  5664. raw_width = get2();
  5665. raw_height = get2();
  5666. load_raw = &CLASS unpacked_load_raw;
  5667. thumb_width = (get4(),get2());
  5668. thumb_height = get2();
  5669. write_thumb = &CLASS ppm_thumb;
  5670. maximum = 0x3fff;
  5671. }
  5672. void CLASS parse_phase_one (int base)
  5673. {
  5674. unsigned entries, tag, type, len, data, save, i, c;
  5675. float romm_cam[3][3];
  5676. char *cp;
  5677. memset (&ph1, 0, sizeof ph1);
  5678. fseek (ifp, base, SEEK_SET);
  5679. order = get4() & 0xffff;
  5680. if (get4() >> 8 != 0x526177) return; /* "Raw" */
  5681. fseek (ifp, get4()+base, SEEK_SET);
  5682. entries = get4();
  5683. get4();
  5684. while (entries--) {
  5685. tag = get4();
  5686. type = get4();
  5687. len = get4();
  5688. data = get4();
  5689. save = ftell(ifp);
  5690. fseek (ifp, base+data, SEEK_SET);
  5691. switch (tag) {
  5692. case 0x100: flip = "0653"[data & 3]-'0'; break;
  5693. case 0x106:
  5694. for (i=0; i < 9; i++)
  5695. romm_cam[0][i] = getreal(11);
  5696. romm_coeff (romm_cam);
  5697. break;
  5698. case 0x107:
  5699. FORC3 cam_mul[c] = getreal(11);
  5700. break;
  5701. case 0x108: raw_width = data; break;
  5702. case 0x109: raw_height = data; break;
  5703. case 0x10a: left_margin = data; break;
  5704. case 0x10b: top_margin = data; break;
  5705. case 0x10c: width = data; break;
  5706. case 0x10d: height = data; break;
  5707. case 0x10e: ph1.format = data; break;
  5708. case 0x10f: data_offset = data+base; break;
  5709. case 0x110: meta_offset = data+base;
  5710. meta_length = len; break;
  5711. case 0x112: ph1.key_off = save - 4; break;
  5712. case 0x210: ph1.tag_210 = int_to_float(data); break;
  5713. case 0x21a: ph1.tag_21a = data; break;
  5714. case 0x21c: strip_offset = data+base; break;
  5715. case 0x21d: ph1.t_black = data; break;
  5716. case 0x222: ph1.split_col = data; break;
  5717. case 0x223: ph1.black_off = data+base; break;
  5718. case 0x301:
  5719. model[63] = 0;
  5720. fread (model, 1, 63, ifp);
  5721. if ((cp = strstr(model," camera"))) *cp = 0;
  5722. }
  5723. fseek (ifp, save, SEEK_SET);
  5724. }
  5725. load_raw = ph1.format < 3 ?
  5726. &CLASS phase_one_load_raw : &CLASS phase_one_load_raw_c;
  5727. maximum = 0xffff;
  5728. strcpy (make, "Phase One");
  5729. if (model[0]) return;
  5730. switch (raw_height) {
  5731. case 2060: strcpy (model,"LightPhase"); break;
  5732. case 2682: strcpy (model,"H 10"); break;
  5733. case 4128: strcpy (model,"H 20"); break;
  5734. case 5488: strcpy (model,"H 25"); break;
  5735. }
  5736. }
  5737. void CLASS parse_fuji (int offset)
  5738. {
  5739. unsigned entries, tag, len, save, c;
  5740. fseek (ifp, offset, SEEK_SET);
  5741. entries = get4();
  5742. if (entries > 255) return;
  5743. while (entries--) {
  5744. tag = get2();
  5745. len = get2();
  5746. save = ftell(ifp);
  5747. if (tag == 0x100) {
  5748. raw_height = get2();
  5749. raw_width = get2();
  5750. } else if (tag == 0x121) {
  5751. height = get2();
  5752. if ((width = get2()) == 4284) width += 3;
  5753. } else if (tag == 0x130) {
  5754. fuji_layout = fgetc(ifp) >> 7;
  5755. fuji_width = !(fgetc(ifp) & 8);
  5756. } else if (tag == 0x2ff0) {
  5757. FORC4 cam_mul[c ^ 1] = get2();
  5758. } else if (tag == 0xc000) {
  5759. c = order;
  5760. order = 0x4949;
  5761. if ((width = get4()) > 10000) width = get4();
  5762. height = get4();
  5763. order = c;
  5764. }
  5765. fseek (ifp, save+len, SEEK_SET);
  5766. }
  5767. height <<= fuji_layout;
  5768. width >>= fuji_layout;
  5769. }
  5770. int CLASS parse_jpeg (int offset)
  5771. {
  5772. int len, save, hlen, mark;
  5773. fseek (ifp, offset, SEEK_SET);
  5774. if (fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8) return 0;
  5775. while (fgetc(ifp) == 0xff && (mark = fgetc(ifp)) != 0xda) {
  5776. order = 0x4d4d;
  5777. len = get2() - 2;
  5778. save = ftell(ifp);
  5779. if (mark == 0xc0 || mark == 0xc3) {
  5780. fgetc(ifp);
  5781. raw_height = get2();
  5782. raw_width = get2();
  5783. }
  5784. order = get2();
  5785. hlen = get4();
  5786. if (get4() == 0x48454150) /* "HEAP" */
  5787. parse_ciff (save+hlen, len-hlen);
  5788. if (parse_tiff (save+6)) apply_tiff();
  5789. fseek (ifp, save+len, SEEK_SET);
  5790. }
  5791. return 1;
  5792. }
  5793. void CLASS parse_riff()
  5794. {
  5795. unsigned i, size, end;
  5796. char tag[4], date[64], month[64];
  5797. static const char mon[12][4] =
  5798. { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
  5799. struct tm t;
  5800. order = 0x4949;
  5801. fread (tag, 4, 1, ifp);
  5802. size = get4();
  5803. end = ftell(ifp) + size;
  5804. if (!memcmp(tag,"RIFF",4) || !memcmp(tag,"LIST",4)) {
  5805. get4();
  5806. while (ftell(ifp)+7 < end)
  5807. parse_riff();
  5808. } else if (!memcmp(tag,"nctg",4)) {
  5809. while (ftell(ifp)+7 < end) {
  5810. i = get2();
  5811. size = get2();
  5812. if ((i+1) >> 1 == 10 && size == 20)
  5813. get_timestamp(0);
  5814. else fseek (ifp, size, SEEK_CUR);
  5815. }
  5816. } else if (!memcmp(tag,"IDIT",4) && size < 64) {
  5817. fread (date, 64, 1, ifp);
  5818. date[size] = 0;
  5819. memset (&t, 0, sizeof t);
  5820. if (sscanf (date, "%*s %s %d %d:%d:%d %d", month, &t.tm_mday,
  5821. &t.tm_hour, &t.tm_min, &t.tm_sec, &t.tm_year) == 6) {
  5822. for (i=0; i < 12 && strcasecmp(mon[i],month); i++);
  5823. t.tm_mon = i;
  5824. t.tm_year -= 1900;
  5825. if (mktime(&t) > 0)
  5826. timestamp = mktime(&t);
  5827. }
  5828. } else
  5829. fseek (ifp, size, SEEK_CUR);
  5830. }
  5831. void CLASS parse_smal (int offset, int fsize)
  5832. {
  5833. int ver;
  5834. fseek (ifp, offset+2, SEEK_SET);
  5835. order = 0x4949;
  5836. ver = fgetc(ifp);
  5837. if (ver == 6)
  5838. fseek (ifp, 5, SEEK_CUR);
  5839. if (get4() != fsize) return;
  5840. if (ver > 6) data_offset = get4();
  5841. raw_height = height = get2();
  5842. raw_width = width = get2();
  5843. strcpy (make, "SMaL");
  5844. sprintf (model, "v%d %dx%d", ver, width, height);
  5845. if (ver == 6) load_raw = &CLASS smal_v6_load_raw;
  5846. if (ver == 9) load_raw = &CLASS smal_v9_load_raw;
  5847. }
  5848. void CLASS parse_cine()
  5849. {
  5850. unsigned off_head, off_setup, off_image, i;
  5851. order = 0x4949;
  5852. fseek (ifp, 4, SEEK_SET);
  5853. is_raw = get2() == 2;
  5854. fseek (ifp, 14, SEEK_CUR);
  5855. is_raw *= get4();
  5856. off_head = get4();
  5857. off_setup = get4();
  5858. off_image = get4();
  5859. timestamp = get4();
  5860. if ((i = get4())) timestamp = i;
  5861. fseek (ifp, off_head+4, SEEK_SET);
  5862. raw_width = get4();
  5863. raw_height = get4();
  5864. switch (get2(),get2()) {
  5865. case 8: load_raw = &CLASS eight_bit_load_raw; break;
  5866. case 16: load_raw = &CLASS unpacked_load_raw;
  5867. }
  5868. fseek (ifp, off_setup+792, SEEK_SET);
  5869. strcpy (make, "CINE");
  5870. sprintf (model, "%d", get4());
  5871. fseek (ifp, 12, SEEK_CUR);
  5872. switch ((i=get4()) & 0xffffff) {
  5873. case 3: filters = 0x94949494; break;
  5874. case 4: filters = 0x49494949; break;
  5875. default: is_raw = 0;
  5876. }
  5877. fseek (ifp, 72, SEEK_CUR);
  5878. switch ((get4()+3600) % 360) {
  5879. case 270: flip = 4; break;
  5880. case 180: flip = 1; break;
  5881. case 90: flip = 7; break;
  5882. case 0: flip = 2;
  5883. }
  5884. cam_mul[0] = getreal(11);
  5885. cam_mul[2] = getreal(11);
  5886. maximum = ~(-1 << get4());
  5887. fseek (ifp, 668, SEEK_CUR);
  5888. shutter = get4()/1000000000.0;
  5889. fseek (ifp, off_image, SEEK_SET);
  5890. if (shot_select < is_raw)
  5891. fseek (ifp, shot_select*8, SEEK_CUR);
  5892. data_offset = (INT64) get4() + 8;
  5893. data_offset += (INT64) get4() << 32;
  5894. }
  5895. void CLASS parse_redcine()
  5896. {
  5897. unsigned i, len, rdvo;
  5898. order = 0x4d4d;
  5899. is_raw = 0;
  5900. fseek (ifp, 52, SEEK_SET);
  5901. width = get4();
  5902. height = get4();
  5903. fseek (ifp, 0, SEEK_END);
  5904. fseek (ifp, -(i = ftello(ifp) & 511), SEEK_CUR);
  5905. if (get4() != i || get4() != 0x52454f42) {
  5906. #ifdef DCRAW_VERBOSE
  5907. fprintf (stderr,_("%s: Tail is missing, parsing from head...\n"), ifname);
  5908. #endif
  5909. fseek (ifp, 0, SEEK_SET);
  5910. while ((len = get4()) != EOF) {
  5911. if (get4() == 0x52454456)
  5912. if (is_raw++ == shot_select)
  5913. data_offset = ftello(ifp) - 8;
  5914. fseek (ifp, len-8, SEEK_CUR);
  5915. }
  5916. } else {
  5917. rdvo = get4();
  5918. fseek (ifp, 12, SEEK_CUR);
  5919. is_raw = get4();
  5920. fseeko (ifp, rdvo+8 + shot_select*4, SEEK_SET);
  5921. data_offset = get4();
  5922. }
  5923. }
  5924. #line 7405 "dcraw/dcraw.c"
  5925. char * CLASS foveon_gets (int offset, char *str, int len)
  5926. {
  5927. int i;
  5928. fseek (ifp, offset, SEEK_SET);
  5929. for (i=0; i < len-1; i++)
  5930. if ((str[i] = get2()) == 0) break;
  5931. str[i] = 0;
  5932. return str;
  5933. }
  5934. void CLASS parse_foveon()
  5935. {
  5936. int entries, img=0, off, len, tag, save, i, wide, high, pent, poff[256][2];
  5937. char name[64], value[64];
  5938. order = 0x4949; /* Little-endian */
  5939. fseek (ifp, 36, SEEK_SET);
  5940. flip = get4();
  5941. fseek (ifp, -4, SEEK_END);
  5942. fseek (ifp, get4(), SEEK_SET);
  5943. if (get4() != 0x64434553) return; /* SECd */
  5944. entries = (get4(),get4());
  5945. while (entries--) {
  5946. off = get4();
  5947. len = get4();
  5948. tag = get4();
  5949. save = ftell(ifp);
  5950. fseek (ifp, off, SEEK_SET);
  5951. if (get4() != (0x20434553 | (tag << 24))) return;
  5952. switch (tag) {
  5953. case 0x47414d49: /* IMAG */
  5954. case 0x32414d49: /* IMA2 */
  5955. fseek (ifp, 8, SEEK_CUR);
  5956. pent = get4();
  5957. wide = get4();
  5958. high = get4();
  5959. if (wide > raw_width && high > raw_height) {
  5960. switch (pent) {
  5961. case 5: load_flags = 1;
  5962. case 6: load_raw = &CLASS foveon_sd_load_raw; break;
  5963. case 30: load_raw = &CLASS foveon_dp_load_raw; break;
  5964. default: load_raw = 0;
  5965. }
  5966. raw_width = wide;
  5967. raw_height = high;
  5968. data_offset = off+28;
  5969. }
  5970. fseek (ifp, off+28, SEEK_SET);
  5971. if (fgetc(ifp) == 0xff && fgetc(ifp) == 0xd8
  5972. && thumb_length < len-28) {
  5973. thumb_offset = off+28;
  5974. thumb_length = len-28;
  5975. write_thumb = &CLASS jpeg_thumb;
  5976. }
  5977. if (++img == 2 && !thumb_length) {
  5978. thumb_offset = off+24;
  5979. thumb_width = wide;
  5980. thumb_height = high;
  5981. write_thumb = &CLASS foveon_thumb;
  5982. }
  5983. break;
  5984. case 0x464d4143: /* CAMF */
  5985. meta_offset = off+8;
  5986. meta_length = len-28;
  5987. break;
  5988. case 0x504f5250: /* PROP */
  5989. pent = (get4(),get4());
  5990. fseek (ifp, 12, SEEK_CUR);
  5991. off += pent*8 + 24;
  5992. if ((unsigned) pent > 256) pent=256;
  5993. for (i=0; i < pent*2; i++)
  5994. poff[0][i] = off + get4()*2;
  5995. for (i=0; i < pent; i++) {
  5996. foveon_gets (poff[i][0], name, 64);
  5997. foveon_gets (poff[i][1], value, 64);
  5998. if (!strcmp (name, "ISO"))
  5999. iso_speed = atoi(value);
  6000. if (!strcmp (name, "CAMMANUF"))
  6001. strcpy (make, value);
  6002. if (!strcmp (name, "CAMMODEL"))
  6003. strcpy (model, value);
  6004. if (!strcmp (name, "WB_DESC"))
  6005. strcpy (model2, value);
  6006. if (!strcmp (name, "TIME"))
  6007. timestamp = atoi(value);
  6008. if (!strcmp (name, "EXPTIME"))
  6009. shutter = atoi(value) / 1000000.0;
  6010. if (!strcmp (name, "APERTURE"))
  6011. aperture = atof(value);
  6012. if (!strcmp (name, "FLENGTH"))
  6013. focal_len = atof(value);
  6014. }
  6015. #ifdef LOCALTIME
  6016. timestamp = mktime (gmtime (&timestamp));
  6017. #endif
  6018. }
  6019. fseek (ifp, save, SEEK_SET);
  6020. }
  6021. is_foveon = 1;
  6022. }
  6023. #line 7508 "dcraw/dcraw.c"
  6024. /*
  6025. All matrices are from Adobe DNG Converter unless otherwise noted.
  6026. */
  6027. void CLASS adobe_coeff (const char *t_make, const char *t_model)
  6028. {
  6029. static const struct {
  6030. const char *prefix;
  6031. short t_black, t_maximum, trans[12];
  6032. } table[] = {
  6033. { "AGFAPHOTO DC-833m", 0, 0, /* DJC */
  6034. { 11438,-3762,-1115,-2409,9914,2497,-1227,2295,5300 } },
  6035. { "Apple QuickTake", 0, 0, /* DJC */
  6036. { 21392,-5653,-3353,2406,8010,-415,7166,1427,2078 } },
  6037. { "Canon EOS D2000", 0, 0,
  6038. { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
  6039. { "Canon EOS D6000", 0, 0,
  6040. { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
  6041. { "Canon EOS D30", 0, 0,
  6042. { 9805,-2689,-1312,-5803,13064,3068,-2438,3075,8775 } },
  6043. { "Canon EOS D60", 0, 0xfa0,
  6044. { 6188,-1341,-890,-7168,14489,2937,-2640,3228,8483 } },
  6045. { "Canon EOS 5D Mark III", 0, 0x3c80,
  6046. { 6722,-635,-963,-4287,12460,2028,-908,2162,5668 } },
  6047. { "Canon EOS 5D Mark II", 0, 0x3cf0,
  6048. { 4716,603,-830,-7798,15474,2480,-1496,1937,6651 } },
  6049. { "Canon EOS 5D", 0, 0xe6c,
  6050. { 6347,-479,-972,-8297,15954,2480,-1968,2131,7649 } },
  6051. { "Canon EOS 6D", 0, 0x3c82,
  6052. { 7034,-804,-1014,-4420,12564,2058,-851,1994,5758 } },
  6053. { "Canon EOS 7D", 0, 0x3510,
  6054. { 6844,-996,-856,-3876,11761,2396,-593,1772,6198 } },
  6055. { "Canon EOS 10D", 0, 0xfa0,
  6056. { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
  6057. { "Canon EOS 20Da", 0, 0,
  6058. { 14155,-5065,-1382,-6550,14633,2039,-1623,1824,6561 } },
  6059. { "Canon EOS 20D", 0, 0xfff,
  6060. { 6599,-537,-891,-8071,15783,2424,-1983,2234,7462 } },
  6061. { "Canon EOS 30D", 0, 0,
  6062. { 6257,-303,-1000,-7880,15621,2396,-1714,1904,7046 } },
  6063. { "Canon EOS 40D", 0, 0x3f60,
  6064. { 6071,-747,-856,-7653,15365,2441,-2025,2553,7315 } },
  6065. { "Canon EOS 50D", 0, 0x3d93,
  6066. { 4920,616,-593,-6493,13964,2784,-1774,3178,7005 } },
  6067. { "Canon EOS 60D", 0, 0x2ff7,
  6068. { 6719,-994,-925,-4408,12426,2211,-887,2129,6051 } },
  6069. { "Canon EOS 300D", 0, 0xfa0,
  6070. { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
  6071. { "Canon EOS 350D", 0, 0xfff,
  6072. { 6018,-617,-965,-8645,15881,2975,-1530,1719,7642 } },
  6073. { "Canon EOS 400D", 0, 0xe8e,
  6074. { 7054,-1501,-990,-8156,15544,2812,-1278,1414,7796 } },
  6075. { "Canon EOS 450D", 0, 0x390d,
  6076. { 5784,-262,-821,-7539,15064,2672,-1982,2681,7427 } },
  6077. { "Canon EOS 500D", 0, 0x3479,
  6078. { 4763,712,-646,-6821,14399,2640,-1921,3276,6561 } },
  6079. { "Canon EOS 550D", 0, 0x3dd7,
  6080. { 6941,-1164,-857,-3825,11597,2534,-416,1540,6039 } },
  6081. { "Canon EOS 600D", 0, 0x3510,
  6082. { 6461,-907,-882,-4300,12184,2378,-819,1944,5931 } },
  6083. { "Canon EOS 650D", 0, 0x354d,
  6084. { 6602,-841,-939,-4472,12458,2247,-975,2039,6148 } },
  6085. { "Canon EOS 1000D", 0, 0xe43,
  6086. { 6771,-1139,-977,-7818,15123,2928,-1244,1437,7533 } },
  6087. { "Canon EOS 1100D", 0, 0x3510,
  6088. { 6444,-904,-893,-4563,12308,2535,-903,2016,6728 } },
  6089. { "Canon EOS M", 0, 0,
  6090. { 6602,-841,-939,-4472,12458,2247,-975,2039,6148 } },
  6091. { "Canon EOS-1Ds Mark III", 0, 0x3bb0,
  6092. { 5859,-211,-930,-8255,16017,2353,-1732,1887,7448 } },
  6093. { "Canon EOS-1Ds Mark II", 0, 0xe80,
  6094. { 6517,-602,-867,-8180,15926,2378,-1618,1771,7633 } },
  6095. { "Canon EOS-1D Mark IV", 0, 0x3bb0,
  6096. { 6014,-220,-795,-4109,12014,2361,-561,1824,5787 } },
  6097. { "Canon EOS-1D Mark III", 0, 0x3bb0,
  6098. { 6291,-540,-976,-8350,16145,2311,-1714,1858,7326 } },
  6099. { "Canon EOS-1D Mark II N", 0, 0xe80,
  6100. { 6240,-466,-822,-8180,15825,2500,-1801,1938,8042 } },
  6101. { "Canon EOS-1D Mark II", 0, 0xe80,
  6102. { 6264,-582,-724,-8312,15948,2504,-1744,1919,8664 } },
  6103. { "Canon EOS-1DS", 0, 0xe20,
  6104. { 4374,3631,-1743,-7520,15212,2472,-2892,3632,8161 } },
  6105. { "Canon EOS-1D X", 0, 0x3c4e,
  6106. { 6847,-614,-1014,-4669,12737,2139,-1197,2488,6846 } },
  6107. { "Canon EOS-1D", 0, 0xe20,
  6108. { 6806,-179,-1020,-8097,16415,1687,-3267,4236,7690 } },
  6109. { "Canon EOS", 0, 0,
  6110. { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
  6111. { "Canon PowerShot A530", 0, 0,
  6112. { 0 } }, /* don't want the A5 matrix */
  6113. { "Canon PowerShot A50", 0, 0,
  6114. { -5300,9846,1776,3436,684,3939,-5540,9879,6200,-1404,11175,217 } },
  6115. { "Canon PowerShot A5", 0, 0,
  6116. { -4801,9475,1952,2926,1611,4094,-5259,10164,5947,-1554,10883,547 } },
  6117. { "Canon PowerShot G10", 0, 0,
  6118. { 11093,-3906,-1028,-5047,12492,2879,-1003,1750,5561 } },
  6119. { "Canon PowerShot G11", 0, 0,
  6120. { 12177,-4817,-1069,-1612,9864,2049,-98,850,4471 } },
  6121. { "Canon PowerShot G12", 0, 0,
  6122. { 13244,-5501,-1248,-1508,9858,1935,-270,1083,4366 } },
  6123. { "Canon PowerShot G15", 0, 0,
  6124. { 7474,-2301,-567,-4056,11456,2975,-222,716,4181 } },
  6125. { "Canon PowerShot G1 X", 0, 0,
  6126. { 7378,-1255,-1043,-4088,12251,2048,-876,1946,5805 } },
  6127. { "Canon PowerShot G1", 0, 0,
  6128. { -4778,9467,2172,4743,-1141,4344,-5146,9908,6077,-1566,11051,557 } },
  6129. { "Canon PowerShot G2", 0, 0,
  6130. { 9087,-2693,-1049,-6715,14382,2537,-2291,2819,7790 } },
  6131. { "Canon PowerShot G3", 0, 0,
  6132. { 9212,-2781,-1073,-6573,14189,2605,-2300,2844,7664 } },
  6133. { "Canon PowerShot G5", 0, 0,
  6134. { 9757,-2872,-933,-5972,13861,2301,-1622,2328,7212 } },
  6135. { "Canon PowerShot G6", 0, 0,
  6136. { 9877,-3775,-871,-7613,14807,3072,-1448,1305,7485 } },
  6137. { "Canon PowerShot G9", 0, 0,
  6138. { 7368,-2141,-598,-5621,13254,2625,-1418,1696,5743 } },
  6139. { "Canon PowerShot Pro1", 0, 0,
  6140. { 10062,-3522,-999,-7643,15117,2730,-765,817,7323 } },
  6141. { "Canon PowerShot Pro70", 34, 0,
  6142. { -4155,9818,1529,3939,-25,4522,-5521,9870,6610,-2238,10873,1342 } },
  6143. { "Canon PowerShot Pro90", 0, 0,
  6144. { -4963,9896,2235,4642,-987,4294,-5162,10011,5859,-1770,11230,577 } },
  6145. { "Canon PowerShot S30", 0, 0,
  6146. { 10566,-3652,-1129,-6552,14662,2006,-2197,2581,7670 } },
  6147. { "Canon PowerShot S40", 0, 0,
  6148. { 8510,-2487,-940,-6869,14231,2900,-2318,2829,9013 } },
  6149. { "Canon PowerShot S45", 0, 0,
  6150. { 8163,-2333,-955,-6682,14174,2751,-2077,2597,8041 } },
  6151. { "Canon PowerShot S50", 0, 0,
  6152. { 8882,-2571,-863,-6348,14234,2288,-1516,2172,6569 } },
  6153. { "Canon PowerShot S60", 0, 0,
  6154. { 8795,-2482,-797,-7804,15403,2573,-1422,1996,7082 } },
  6155. { "Canon PowerShot S70", 0, 0,
  6156. { 9976,-3810,-832,-7115,14463,2906,-901,989,7889 } },
  6157. { "Canon PowerShot S90", 0, 0,
  6158. { 12374,-5016,-1049,-1677,9902,2078,-83,852,4683 } },
  6159. { "Canon PowerShot S95", 0, 0,
  6160. { 13440,-5896,-1279,-1236,9598,1931,-180,1001,4651 } },
  6161. { "Canon PowerShot S110", 0, 0,
  6162. { 8039,-2643,-654,-3783,11230,2930,-206,690,4194 } },
  6163. { "Canon PowerShot S100", 0, 0,
  6164. { 7968,-2565,-636,-2873,10697,2513,180,667,4211 } },
  6165. { "Canon PowerShot S110", 0, 0,
  6166. { 8039,-2643,-654,-3783,11230,2930,-206,690,4194 } },
  6167. { "Canon PowerShot SX1 IS", 0, 0,
  6168. { 6578,-259,-502,-5974,13030,3309,-308,1058,4970 } },
  6169. { "Canon PowerShot SX50 HS", 0, 0,
  6170. { 12432,-4753,-1247,-2110,10691,1629,-412,1623,4926 } },
  6171. { "Canon PowerShot A470", 0, 0, /* DJC */
  6172. { 12513,-4407,-1242,-2680,10276,2405,-878,2215,4734 } },
  6173. { "Canon PowerShot A610", 0, 0, /* DJC */
  6174. { 15591,-6402,-1592,-5365,13198,2168,-1300,1824,5075 } },
  6175. { "Canon PowerShot A620", 0, 0, /* DJC */
  6176. { 15265,-6193,-1558,-4125,12116,2010,-888,1639,5220 } },
  6177. { "Canon PowerShot A630", 0, 0, /* DJC */
  6178. { 14201,-5308,-1757,-6087,14472,1617,-2191,3105,5348 } },
  6179. { "Canon PowerShot A640", 0, 0, /* DJC */
  6180. { 13124,-5329,-1390,-3602,11658,1944,-1612,2863,4885 } },
  6181. { "Canon PowerShot A650", 0, 0, /* DJC */
  6182. { 9427,-3036,-959,-2581,10671,1911,-1039,1982,4430 } },
  6183. { "Canon PowerShot A720", 0, 0, /* DJC */
  6184. { 14573,-5482,-1546,-1266,9799,1468,-1040,1912,3810 } },
  6185. { "Canon PowerShot S3 IS", 0, 0, /* DJC */
  6186. { 14062,-5199,-1446,-4712,12470,2243,-1286,2028,4836 } },
  6187. { "Canon PowerShot SX110 IS", 0, 0, /* DJC */
  6188. { 14134,-5576,-1527,-1991,10719,1273,-1158,1929,3581 } },
  6189. { "Canon PowerShot SX220", 0, 0, /* DJC */
  6190. { 13898,-5076,-1447,-1405,10109,1297,-244,1860,3687 } },
  6191. { "Canon PowerShot SX50", 0, 0,
  6192. { 12432,-4753,-1247,-2110,10691,1629,-412,1623,4926 } },
  6193. { "CASIO EX-S20", 0, 0, /* DJC */
  6194. { 11634,-3924,-1128,-4968,12954,2015,-1588,2648,7206 } },
  6195. { "CASIO EX-Z750", 0, 0, /* DJC */
  6196. { 10819,-3873,-1099,-4903,13730,1175,-1755,3751,4632 } },
  6197. { "CASIO EX-Z10", 128, 0xfff, /* DJC */
  6198. { 9790,-3338,-603,-2321,10222,2099,-344,1273,4799 } },
  6199. { "CINE 650", 0, 0,
  6200. { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
  6201. { "CINE 660", 0, 0,
  6202. { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
  6203. { "CINE", 0, 0,
  6204. { 20183,-4295,-423,-3940,15330,3985,-280,4870,9800 } },
  6205. { "Contax N Digital", 0, 0xf1e,
  6206. { 7777,1285,-1053,-9280,16543,2916,-3677,5679,7060 } },
  6207. { "EPSON R-D1", 0, 0,
  6208. { 6827,-1878,-732,-8429,16012,2564,-704,592,7145 } },
  6209. { "FUJIFILM E550", 0, 0,
  6210. { 11044,-3888,-1120,-7248,15168,2208,-1531,2277,8069 } },
  6211. { "FUJIFILM E900", 0, 0,
  6212. { 9183,-2526,-1078,-7461,15071,2574,-2022,2440,8639 } },
  6213. { "FUJIFILM F5", 0, 0,
  6214. { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
  6215. { "FUJIFILM F6", 0, 0,
  6216. { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
  6217. { "FUJIFILM F77", 0, 0xfe9,
  6218. { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
  6219. { "FUJIFILM F7", 0, 0,
  6220. { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
  6221. { "FUJIFILM F800", 0, 0,
  6222. { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
  6223. { "FUJIFILM F8", 0, 0,
  6224. { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
  6225. { "FUJIFILM S100FS", 514, 0,
  6226. { 11521,-4355,-1065,-6524,13767,3058,-1466,1984,6045 } },
  6227. { "FUJIFILM S200EXR", 512, 0x3fff,
  6228. { 11401,-4498,-1312,-5088,12751,2613,-838,1568,5941 } },
  6229. { "FUJIFILM S20Pro", 0, 0,
  6230. { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
  6231. { "FUJIFILM S2Pro", 128, 0,
  6232. { 12492,-4690,-1402,-7033,15423,1647,-1507,2111,7697 } },
  6233. { "FUJIFILM S3Pro", 0, 0,
  6234. { 11807,-4612,-1294,-8927,16968,1988,-2120,2741,8006 } },
  6235. { "FUJIFILM S5Pro", 0, 0,
  6236. { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
  6237. { "FUJIFILM S5000", 0, 0,
  6238. { 8754,-2732,-1019,-7204,15069,2276,-1702,2334,6982 } },
  6239. { "FUJIFILM S5100", 0, 0,
  6240. { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
  6241. { "FUJIFILM S5500", 0, 0,
  6242. { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
  6243. { "FUJIFILM S5200", 0, 0,
  6244. { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
  6245. { "FUJIFILM S5600", 0, 0,
  6246. { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
  6247. { "FUJIFILM S6", 0, 0,
  6248. { 12628,-4887,-1401,-6861,14996,1962,-2198,2782,7091 } },
  6249. { "FUJIFILM S7000", 0, 0,
  6250. { 10190,-3506,-1312,-7153,15051,2238,-2003,2399,7505 } },
  6251. { "FUJIFILM S9000", 0, 0,
  6252. { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
  6253. { "FUJIFILM S9500", 0, 0,
  6254. { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
  6255. { "FUJIFILM S9100", 0, 0,
  6256. { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
  6257. { "FUJIFILM S9600", 0, 0,
  6258. { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
  6259. { "FUJIFILM IS-1", 0, 0,
  6260. { 21461,-10807,-1441,-2332,10599,1999,289,875,7703 } },
  6261. { "FUJIFILM IS Pro", 0, 0,
  6262. { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
  6263. { "FUJIFILM HS10 HS11", 0, 0xf68,
  6264. { 12440,-3954,-1183,-1123,9674,1708,-83,1614,4086 } },
  6265. { "FUJIFILM HS20EXR", 0, 0,
  6266. { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
  6267. { "FUJIFILM HS3", 0, 0,
  6268. { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
  6269. { "FUJIFILM X100", 0, 0,
  6270. { 12161,-4457,-1069,-5034,12874,2400,-795,1724,6904 } },
  6271. { "FUJIFILM X10", 0, 0,
  6272. { 13509,-6199,-1254,-4430,12733,1865,-331,1441,5022 } },
  6273. { "FUJIFILM XF1", 0, 0,
  6274. { 13509,-6199,-1254,-4430,12733,1865,-331,1441,5022 } },
  6275. { "FUJIFILM X-Pro1", 0, 0,
  6276. { 10413,-3996,-993,-3721,11640,2361,-733,1540,6011 } },
  6277. { "FUJIFILM X-E1", 0, 0,
  6278. { 10413,-3996,-993,-3721,11640,2361,-733,1540,6011 } },
  6279. { "FUJIFILM XF1", 0, 0,
  6280. { 13509,-6199,-1254,-4430,12733,1865,-331,1441,5022 } },
  6281. { "FUJIFILM X-S1", 0, 0,
  6282. { 13509,-6199,-1254,-4430,12733,1865,-331,1441,5022 } },
  6283. { "Imacon Ixpress", 0, 0, /* DJC */
  6284. { 7025,-1415,-704,-5188,13765,1424,-1248,2742,6038 } },
  6285. { "KODAK NC2000", 0, 0,
  6286. { 13891,-6055,-803,-465,9919,642,2121,82,1291 } },
  6287. { "Kodak DCS315C", 8, 0,
  6288. { 17523,-4827,-2510,756,8546,-137,6113,1649,2250 } },
  6289. { "Kodak DCS330C", 8, 0,
  6290. { 20620,-7572,-2801,-103,10073,-396,3551,-233,2220 } },
  6291. { "KODAK DCS420", 0, 0,
  6292. { 10868,-1852,-644,-1537,11083,484,2343,628,2216 } },
  6293. { "KODAK DCS460", 0, 0,
  6294. { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
  6295. { "KODAK EOSDCS1", 0, 0,
  6296. { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
  6297. { "KODAK EOSDCS3B", 0, 0,
  6298. { 9898,-2700,-940,-2478,12219,206,1985,634,1031 } },
  6299. { "Kodak DCS520C", 178, 0,
  6300. { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
  6301. { "Kodak DCS560C", 177, 0,
  6302. { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
  6303. { "Kodak DCS620C", 177, 0,
  6304. { 23617,-10175,-3149,-2054,11749,-272,2586,-489,3453 } },
  6305. { "Kodak DCS620X", 176, 0,
  6306. { 13095,-6231,154,12221,-21,-2137,895,4602,2258 } },
  6307. { "Kodak DCS660C", 173, 0,
  6308. { 18244,-6351,-2739,-791,11193,-521,3711,-129,2802 } },
  6309. { "Kodak DCS720X", 0, 0,
  6310. { 11775,-5884,950,9556,1846,-1286,-1019,6221,2728 } },
  6311. { "Kodak DCS760C", 0, 0,
  6312. { 16623,-6309,-1411,-4344,13923,323,2285,274,2926 } },
  6313. { "Kodak DCS Pro SLR", 0, 0,
  6314. { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
  6315. { "Kodak DCS Pro 14nx", 0, 0,
  6316. { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
  6317. { "Kodak DCS Pro 14", 0, 0,
  6318. { 7791,3128,-776,-8588,16458,2039,-2455,4006,6198 } },
  6319. { "Kodak ProBack645", 0, 0,
  6320. { 16414,-6060,-1470,-3555,13037,473,2545,122,4948 } },
  6321. { "Kodak ProBack", 0, 0,
  6322. { 21179,-8316,-2918,-915,11019,-165,3477,-180,4210 } },
  6323. { "KODAK P712", 0, 0,
  6324. { 9658,-3314,-823,-5163,12695,2768,-1342,1843,6044 } },
  6325. { "KODAK P850", 0, 0xf7c,
  6326. { 10511,-3836,-1102,-6946,14587,2558,-1481,1792,6246 } },
  6327. { "KODAK P880", 0, 0xfff,
  6328. { 12805,-4662,-1376,-7480,15267,2360,-1626,2194,7904 } },
  6329. { "KODAK EasyShare Z980", 0, 0,
  6330. { 11313,-3559,-1101,-3893,11891,2257,-1214,2398,4908 } },
  6331. { "KODAK EasyShare Z981", 0, 0,
  6332. { 12729,-4717,-1188,-1367,9187,2582,274,860,4411 } },
  6333. { "KODAK EasyShare Z990", 0, 0xfed,
  6334. { 11749,-4048,-1309,-1867,10572,1489,-138,1449,4522 } },
  6335. { "KODAK EASYSHARE Z1015", 0, 0xef1,
  6336. { 11265,-4286,-992,-4694,12343,2647,-1090,1523,5447 } },
  6337. { "Leaf CMost", 0, 0,
  6338. { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
  6339. { "Leaf Valeo 6", 0, 0,
  6340. { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
  6341. { "Leaf Aptus 54S", 0, 0,
  6342. { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
  6343. { "Leaf Aptus 65", 0, 0,
  6344. { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
  6345. { "Leaf Aptus 75", 0, 0,
  6346. { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
  6347. { "Leaf", 0, 0,
  6348. { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
  6349. { "Mamiya ZD", 0, 0,
  6350. { 7645,2579,-1363,-8689,16717,2015,-3712,5941,5961 } },
  6351. { "Micron 2010", 110, 0, /* DJC */
  6352. { 16695,-3761,-2151,155,9682,163,3433,951,4904 } },
  6353. { "Minolta DiMAGE 5", 0, 0xf7d,
  6354. { 8983,-2942,-963,-6556,14476,2237,-2426,2887,8014 } },
  6355. { "Minolta DiMAGE 7Hi", 0, 0xf7d,
  6356. { 11368,-3894,-1242,-6521,14358,2339,-2475,3056,7285 } },
  6357. { "Minolta DiMAGE 7", 0, 0xf7d,
  6358. { 9144,-2777,-998,-6676,14556,2281,-2470,3019,7744 } },
  6359. { "Minolta DiMAGE A1", 0, 0xf8b,
  6360. { 9274,-2547,-1167,-8220,16323,1943,-2273,2720,8340 } },
  6361. { "MINOLTA DiMAGE A200", 0, 0,
  6362. { 8560,-2487,-986,-8112,15535,2771,-1209,1324,7743 } },
  6363. { "Minolta DiMAGE A2", 0, 0xf8f,
  6364. { 9097,-2726,-1053,-8073,15506,2762,-966,981,7763 } },
  6365. { "Minolta DiMAGE Z2", 0, 0, /* DJC */
  6366. { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
  6367. { "MINOLTA DYNAX 5", 0, 0xffb,
  6368. { 10284,-3283,-1086,-7957,15762,2316,-829,882,6644 } },
  6369. { "MINOLTA DYNAX 7", 0, 0xffb,
  6370. { 10239,-3104,-1099,-8037,15727,2451,-927,925,6871 } },
  6371. { "MOTOROLA PIXL", 0, 0, /* DJC */
  6372. { 8898,-989,-1033,-3292,11619,1674,-661,3178,5216 } },
  6373. { "NIKON D100", 0, 0,
  6374. { 5902,-933,-782,-8983,16719,2354,-1402,1455,6464 } },
  6375. { "NIKON D1H", 0, 0,
  6376. { 7577,-2166,-926,-7454,15592,1934,-2377,2808,8606 } },
  6377. { "NIKON D1X", 0, 0,
  6378. { 7702,-2245,-975,-9114,17242,1875,-2679,3055,8521 } },
  6379. { "NIKON D1", 0, 0, /* multiplied by 2.218750, 1.0, 1.148438 */
  6380. { 16772,-4726,-2141,-7611,15713,1972,-2846,3494,9521 } },
  6381. { "NIKON D200", 0, 0xfbc,
  6382. { 8367,-2248,-763,-8758,16447,2422,-1527,1550,8053 } },
  6383. { "NIKON D2H", 0, 0,
  6384. { 5710,-901,-615,-8594,16617,2024,-2975,4120,6830 } },
  6385. { "NIKON D2X", 0, 0,
  6386. { 10231,-2769,-1255,-8301,15900,2552,-797,680,7148 } },
  6387. { "NIKON D3000", 0, 0,
  6388. { 8736,-2458,-935,-9075,16894,2251,-1354,1242,8263 } },
  6389. { "NIKON D3100", 0, 0,
  6390. { 7911,-2167,-813,-5327,13150,2408,-1288,2483,7968 } },
  6391. { "NIKON D3200", 0, 0xfb9,
  6392. { 7013,-1408,-635,-5268,12902,2640,-1470,2801,7379 } },
  6393. { "NIKON D300", 0, 0,
  6394. { 9030,-1992,-715,-8465,16302,2255,-2689,3217,8069 } },
  6395. { "NIKON D3X", 0, 0,
  6396. { 7171,-1986,-648,-8085,15555,2718,-2170,2512,7457 } },
  6397. { "NIKON D3S", 0, 0,
  6398. { 8828,-2406,-694,-4874,12603,2541,-660,1509,7587 } },
  6399. { "NIKON D3", 0, 0,
  6400. { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
  6401. { "NIKON D40X", 0, 0,
  6402. { 8819,-2543,-911,-9025,16928,2151,-1329,1213,8449 } },
  6403. { "NIKON D40", 0, 0,
  6404. { 6992,-1668,-806,-8138,15748,2543,-874,850,7897 } },
  6405. { "NIKON D4", 0, 0,
  6406. { 8598,-2848,-857,-5618,13606,2195,-1002,1773,7137 } },
  6407. { "NIKON D5000", 0, 0xf00,
  6408. { 7309,-1403,-519,-8474,16008,2622,-2433,2826,8064 } },
  6409. { "NIKON D5100", 0, 0x3de6,
  6410. { 8198,-2239,-724,-4871,12389,2798,-1043,2050,7181 } },
  6411. { "NIKON D50", 0, 0,
  6412. { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
  6413. { "NIKON D600", 0, 0x3e07,
  6414. { 8178,-2245,-609,-4857,12394,2776,-1207,2086,7298 } },
  6415. { "NIKON D60", 0, 0,
  6416. { 8736,-2458,-935,-9075,16894,2251,-1354,1242,8263 } },
  6417. { "NIKON D7000", 0, 0,
  6418. { 8198,-2239,-724,-4871,12389,2798,-1043,2050,7181 } },
  6419. { "NIKON D700", 0, 0,
  6420. { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
  6421. { "NIKON D70", 0, 0,
  6422. { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
  6423. { "NIKON D800", 0, 0,
  6424. { 7866,-2108,-555,-4869,12483,2681,-1176,2069,7501 } },
  6425. { "NIKON D80", 0, 0,
  6426. { 8629,-2410,-883,-9055,16940,2171,-1490,1363,8520 } },
  6427. { "NIKON D90", 0, 0xf00,
  6428. { 7309,-1403,-519,-8474,16008,2622,-2434,2826,8064 } },
  6429. { "NIKON E950", 0, 0x3dd, /* DJC */
  6430. { -3746,10611,1665,9621,-1734,2114,-2389,7082,3064,3406,6116,-244 } },
  6431. { "NIKON E995", 0, 0, /* copied from E5000 */
  6432. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  6433. { "NIKON E2100", 0, 0, /* copied from Z2, new white balance */
  6434. { 13142,-4152,-1596,-4655,12374,2282,-1769,2696,6711} },
  6435. { "NIKON E2500", 0, 0,
  6436. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  6437. { "NIKON E3200", 0, 0, /* DJC */
  6438. { 9846,-2085,-1019,-3278,11109,2170,-774,2134,5745 } },
  6439. { "NIKON E4300", 0, 0, /* copied from Minolta DiMAGE Z2 */
  6440. { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
  6441. { "NIKON E4500", 0, 0,
  6442. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  6443. { "NIKON E5000", 0, 0,
  6444. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  6445. { "NIKON E5400", 0, 0,
  6446. { 9349,-2987,-1001,-7919,15766,2266,-2098,2680,6839 } },
  6447. { "NIKON E5700", 0, 0,
  6448. { -5368,11478,2368,5537,-113,3148,-4969,10021,5782,778,9028,211 } },
  6449. { "NIKON E8400", 0, 0,
  6450. { 7842,-2320,-992,-8154,15718,2599,-1098,1342,7560 } },
  6451. { "NIKON E8700", 0, 0,
  6452. { 8489,-2583,-1036,-8051,15583,2643,-1307,1407,7354 } },
  6453. { "NIKON E8800", 0, 0,
  6454. { 7971,-2314,-913,-8451,15762,2894,-1442,1520,7610 } },
  6455. { "NIKON COOLPIX P6000", 0, 0,
  6456. { 9698,-3367,-914,-4706,12584,2368,-837,968,5801 } },
  6457. { "NIKON COOLPIX P7000", 0, 0,
  6458. { 11432,-3679,-1111,-3169,11239,2202,-791,1380,4455 } },
  6459. { "NIKON COOLPIX P7100", 0, 0,
  6460. { 11053,-4269,-1024,-1976,10182,2088,-526,1263,4469 } },
  6461. { "NIKON COOLPIX P7700", 200, 0,
  6462. { 10321,-3920,-931,-2750,11146,1824,-442,1545,5539 } },
  6463. { "NIKON 1 V2", 0, 0,
  6464. { 6588,-1305,-693,-3277,10987,2634,-355,2016,5106 } },
  6465. { "NIKON 1 ", 0, 0,
  6466. { 8994,-2667,-865,-4594,12324,2552,-699,1786,6260 } },
  6467. { "OLYMPUS C5050", 0, 0,
  6468. { 10508,-3124,-1273,-6079,14294,1901,-1653,2306,6237 } },
  6469. { "OLYMPUS C5060", 0, 0,
  6470. { 10445,-3362,-1307,-7662,15690,2058,-1135,1176,7602 } },
  6471. { "OLYMPUS C7070", 0, 0,
  6472. { 10252,-3531,-1095,-7114,14850,2436,-1451,1723,6365 } },
  6473. { "OLYMPUS C70", 0, 0,
  6474. { 10793,-3791,-1146,-7498,15177,2488,-1390,1577,7321 } },
  6475. { "OLYMPUS C80", 0, 0,
  6476. { 8606,-2509,-1014,-8238,15714,2703,-942,979,7760 } },
  6477. { "OLYMPUS E-10", 0, 0xffc,
  6478. { 12745,-4500,-1416,-6062,14542,1580,-1934,2256,6603 } },
  6479. { "OLYMPUS E-1", 0, 0,
  6480. { 11846,-4767,-945,-7027,15878,1089,-2699,4122,8311 } },
  6481. { "OLYMPUS E-20", 0, 0xffc,
  6482. { 13173,-4732,-1499,-5807,14036,1895,-2045,2452,7142 } },
  6483. { "OLYMPUS E-300", 0, 0,
  6484. { 7828,-1761,-348,-5788,14071,1830,-2853,4518,6557 } },
  6485. { "OLYMPUS E-330", 0, 0,
  6486. { 8961,-2473,-1084,-7979,15990,2067,-2319,3035,8249 } },
  6487. { "OLYMPUS E-30", 0, 0xfbc,
  6488. { 8144,-1861,-1111,-7763,15894,1929,-1865,2542,7607 } },
  6489. { "OLYMPUS E-3", 0, 0xf99,
  6490. { 9487,-2875,-1115,-7533,15606,2010,-1618,2100,7389 } },
  6491. { "OLYMPUS E-400", 0, 0,
  6492. { 6169,-1483,-21,-7107,14761,2536,-2904,3580,8568 } },
  6493. { "OLYMPUS E-410", 0, 0xf6a,
  6494. { 8856,-2582,-1026,-7761,15766,2082,-2009,2575,7469 } },
  6495. { "OLYMPUS E-420", 0, 0xfd7,
  6496. { 8746,-2425,-1095,-7594,15612,2073,-1780,2309,7416 } },
  6497. { "OLYMPUS E-450", 0, 0xfd2,
  6498. { 8745,-2425,-1095,-7594,15613,2073,-1780,2309,7416 } },
  6499. { "OLYMPUS E-500", 0, 0,
  6500. { 8136,-1968,-299,-5481,13742,1871,-2556,4205,6630 } },
  6501. { "OLYMPUS E-510", 0, 0xf6a,
  6502. { 8785,-2529,-1033,-7639,15624,2112,-1783,2300,7817 } },
  6503. { "OLYMPUS E-520", 0, 0xfd2,
  6504. { 8344,-2322,-1020,-7596,15635,2048,-1748,2269,7287 } },
  6505. { "OLYMPUS E-5", 0, 0xeec,
  6506. { 11200,-3783,-1325,-4576,12593,2206,-695,1742,7504 } },
  6507. { "OLYMPUS E-600", 0, 0xfaf,
  6508. { 8453,-2198,-1092,-7609,15681,2008,-1725,2337,7824 } },
  6509. { "OLYMPUS E-620", 0, 0xfaf,
  6510. { 8453,-2198,-1092,-7609,15681,2008,-1725,2337,7824 } },
  6511. { "OLYMPUS E-P1", 0, 0xffd,
  6512. { 8343,-2050,-1021,-7715,15705,2103,-1831,2380,8235 } },
  6513. { "OLYMPUS E-P2", 0, 0xffd,
  6514. { 8343,-2050,-1021,-7715,15705,2103,-1831,2380,8235 } },
  6515. { "OLYMPUS E-P3", 0, 0,
  6516. { 7575,-2159,-571,-3722,11341,2725,-1434,2819,6271 } },
  6517. { "OLYMPUS E-PL1s", 0, 0,
  6518. { 11409,-3872,-1393,-4572,12757,2003,-709,1810,7415 } },
  6519. { "OLYMPUS E-PL1", 0, 0,
  6520. { 11408,-4289,-1215,-4286,12385,2118,-387,1467,7787 } },
  6521. { "OLYMPUS E-PL2", 0, 0,
  6522. { 15030,-5552,-1806,-3987,12387,1767,-592,1670,7023 } },
  6523. { "OLYMPUS E-PL3", 0, 0,
  6524. { 7575,-2159,-571,-3722,11341,2725,-1434,2819,6271 } },
  6525. { "OLYMPUS E-PL5", 0, 0xfcb,
  6526. { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
  6527. { "OLYMPUS E-PM1", 0, 0,
  6528. { 7575,-2159,-571,-3722,11341,2725,-1434,2819,6271 } },
  6529. { "OLYMPUS E-PM2", 0, 0,
  6530. { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
  6531. { "OLYMPUS E-M5", 0, 0xfe1,
  6532. { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
  6533. { "OLYMPUS SP350", 0, 0,
  6534. { 12078,-4836,-1069,-6671,14306,2578,-786,939,7418 } },
  6535. { "OLYMPUS SP3", 0, 0,
  6536. { 11766,-4445,-1067,-6901,14421,2707,-1029,1217,7572 } },
  6537. { "OLYMPUS SP500UZ", 0, 0xfff,
  6538. { 9493,-3415,-666,-5211,12334,3260,-1548,2262,6482 } },
  6539. { "OLYMPUS SP510UZ", 0, 0xffe,
  6540. { 10593,-3607,-1010,-5881,13127,3084,-1200,1805,6721 } },
  6541. { "OLYMPUS SP550UZ", 0, 0xffe,
  6542. { 11597,-4006,-1049,-5432,12799,2957,-1029,1750,6516 } },
  6543. { "OLYMPUS SP560UZ", 0, 0xff9,
  6544. { 10915,-3677,-982,-5587,12986,2911,-1168,1968,6223 } },
  6545. { "OLYMPUS SP570UZ", 0, 0,
  6546. { 11522,-4044,-1146,-4736,12172,2904,-988,1829,6039 } },
  6547. { "OLYMPUS XZ-1", 0, 0,
  6548. { 10901,-4095,-1074,-1141,9208,2293,-62,1417,5158 } },
  6549. { "OLYMPUS XZ-2", 0, 0,
  6550. { 9777,-3483,-925,-2886,11297,1800,-602,1663,5134 } },
  6551. { "PENTAX *ist DL2", 0, 0,
  6552. { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
  6553. { "PENTAX *ist DL", 0, 0,
  6554. { 10829,-2838,-1115,-8339,15817,2696,-837,680,11939 } },
  6555. { "PENTAX *ist DS2", 0, 0,
  6556. { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
  6557. { "PENTAX *ist DS", 0, 0,
  6558. { 10371,-2333,-1206,-8688,16231,2602,-1230,1116,11282 } },
  6559. { "PENTAX *ist D", 0, 0,
  6560. { 9651,-2059,-1189,-8881,16512,2487,-1460,1345,10687 } },
  6561. { "PENTAX K10D", 0, 0,
  6562. { 9566,-2863,-803,-7170,15172,2112,-818,803,9705 } },
  6563. { "PENTAX K1", 0, 0,
  6564. { 11095,-3157,-1324,-8377,15834,2720,-1108,947,11688 } },
  6565. { "PENTAX K20D", 0, 0,
  6566. { 9427,-2714,-868,-7493,16092,1373,-2199,3264,7180 } },
  6567. { "PENTAX K200D", 0, 0,
  6568. { 9186,-2678,-907,-8693,16517,2260,-1129,1094,8524 } },
  6569. { "PENTAX K2000", 0, 0,
  6570. { 11057,-3604,-1155,-5152,13046,2329,-282,375,8104 } },
  6571. { "PENTAX K-m", 0, 0,
  6572. { 11057,-3604,-1155,-5152,13046,2329,-282,375,8104 } },
  6573. { "PENTAX K-x", 0, 0,
  6574. { 8843,-2837,-625,-5025,12644,2668,-411,1234,7410 } },
  6575. { "PENTAX K-r", 0, 0,
  6576. { 9895,-3077,-850,-5304,13035,2521,-883,1768,6936 } },
  6577. { "PENTAX K-30", 0, 0,
  6578. { 8833,-2670,-1183,-3995,12301,1881,-979,1717,6527 } },
  6579. { "PENTAX K-5 II s", 0, 0,
  6580. { 8366,-2528,-1120,-3995,12301,1881,-998,1749,6649 } },
  6581. { "PENTAX K-5 II", 0, 0,
  6582. { 8773,-2651,-1175,-3995,12301,1881,-973,1706,6486 } },
  6583. { "PENTAX K-5", 0, 0,
  6584. { 8713,-2833,-743,-4342,11900,2772,-722,1543,6247 } },
  6585. { "PENTAX K-7", 0, 0,
  6586. { 9142,-2947,-678,-8648,16967,1663,-2224,2898,8615 } },
  6587. { "PENTAX Q10", 0, 0,
  6588. { 12995,-5593,-1107,-1879,10139,2027,-64,1233,4919 } },
  6589. { "PENTAX 645D", 0, 0x3e00,
  6590. { 10646,-3593,-1158,-3329,11699,1831,-667,2874,6287 } },
  6591. { "Panasonic DMC-FZ8", 0, 0xf7f,
  6592. { 8986,-2755,-802,-6341,13575,3077,-1476,2144,6379 } },
  6593. { "Panasonic DMC-FZ18", 0, 0,
  6594. { 9932,-3060,-935,-5809,13331,2753,-1267,2155,5575 } },
  6595. { "Panasonic DMC-FZ28", 15, 0xf96,
  6596. { 10109,-3488,-993,-5412,12812,2916,-1305,2140,5543 } },
  6597. { "Panasonic DMC-FZ30", 0, 0xf94,
  6598. { 10976,-4029,-1141,-7918,15491,2600,-1670,2071,8246 } },
  6599. { "Panasonic DMC-FZ3", 143, 0,
  6600. { 9938,-2780,-890,-4604,12393,2480,-1117,2304,4620 } },
  6601. { "Panasonic DMC-FZ4", 143, 0,
  6602. { 13639,-5535,-1371,-1698,9633,2430,316,1152,4108 } },
  6603. { "Panasonic DMC-FZ50", 0, 0,
  6604. { 7906,-2709,-594,-6231,13351,3220,-1922,2631,6537 } },
  6605. { "LEICA V-LUX1", 0, 0,
  6606. { 7906,-2709,-594,-6231,13351,3220,-1922,2631,6537 } },
  6607. { "Panasonic DMC-L10", 15, 0xf96,
  6608. { 8025,-1942,-1050,-7920,15904,2100,-2456,3005,7039 } },
  6609. { "Panasonic DMC-L1", 0, 0xf7f,
  6610. { 8054,-1885,-1025,-8349,16367,2040,-2805,3542,7629 } },
  6611. { "LEICA DIGILUX 3", 0, 0xf7f,
  6612. { 8054,-1885,-1025,-8349,16367,2040,-2805,3542,7629 } },
  6613. { "Panasonic DMC-LC1", 0, 0,
  6614. { 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
  6615. { "LEICA DIGILUX 2", 0, 0,
  6616. { 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
  6617. { "Panasonic DMC-LX1", 0, 0xf7f,
  6618. { 10704,-4187,-1230,-8314,15952,2501,-920,945,8927 } },
  6619. { "LEICA D-LUX2", 0, 0xf7f,
  6620. { 10704,-4187,-1230,-8314,15952,2501,-920,945,8927 } },
  6621. { "Panasonic DMC-LX2", 0, 0,
  6622. { 8048,-2810,-623,-6450,13519,3272,-1700,2146,7049 } },
  6623. { "LEICA D-LUX3", 0, 0,
  6624. { 8048,-2810,-623,-6450,13519,3272,-1700,2146,7049 } },
  6625. { "Panasonic DMC-LX3", 15, 0,
  6626. { 8128,-2668,-655,-6134,13307,3161,-1782,2568,6083 } },
  6627. { "LEICA D-LUX 4", 15, 0,
  6628. { 8128,-2668,-655,-6134,13307,3161,-1782,2568,6083 } },
  6629. { "Panasonic DMC-LX5", 143, 0,
  6630. { 10909,-4295,-948,-1333,9306,2399,22,1738,4582 } },
  6631. { "Panasonic DMC-LX7", 143, 0,
  6632. { 10148,-3743,-991,-2837,11366,1659,-701,1893,4899 } },
  6633. { "LEICA D-LUX 5", 143, 0,
  6634. { 10909,-4295,-948,-1333,9306,2399,22,1738,4582 } },
  6635. { "Panasonic DMC-LX7", 143, 0,
  6636. { 10148,-3743,-991,-2837,11366,1659,-701,1893,4899 } },
  6637. { "LEICA D-LUX 6", 143, 0,
  6638. { 10148,-3743,-991,-2837,11366,1659,-701,1893,4899 } },
  6639. { "Panasonic DMC-FZ100", 143, 0xfff,
  6640. { 16197,-6146,-1761,-2393,10765,1869,366,2238,5248 } },
  6641. { "LEICA V-LUX 2", 143, 0xfff,
  6642. { 16197,-6146,-1761,-2393,10765,1869,366,2238,5248 } },
  6643. { "Panasonic DMC-FZ150", 143, 0xfff,
  6644. { 11904,-4541,-1189,-2355,10899,1662,-296,1586,4289 } },
  6645. { "LEICA V-LUX 3", 143, 0xfff,
  6646. { 11904,-4541,-1189,-2355,10899,1662,-296,1586,4289 } },
  6647. { "Panasonic DMC-FZ200", 143, 0xfff,
  6648. { 8112,-2563,-740,-3730,11784,2197,-941,2075,4933 } },
  6649. { "LEICA V-LUX 4", 143, 0xfff,
  6650. { 8112,-2563,-740,-3730,11784,2197,-941,2075,4933 } },
  6651. { "Panasonic DMC-FX150", 15, 0xfff,
  6652. { 9082,-2907,-925,-6119,13377,3058,-1797,2641,5609 } },
  6653. { "Panasonic DMC-G10", 0, 0,
  6654. { 10113,-3400,-1114,-4765,12683,2317,-377,1437,6710 } },
  6655. { "Panasonic DMC-G1", 15, 0xf94,
  6656. { 8199,-2065,-1056,-8124,16156,2033,-2458,3022,7220 } },
  6657. { "Panasonic DMC-G2", 15, 0xf3c,
  6658. { 10113,-3400,-1114,-4765,12683,2317,-377,1437,6710 } },
  6659. { "Panasonic DMC-G3", 143, 0xfff,
  6660. { 6763,-1919,-863,-3868,11515,2684,-1216,2387,5879 } },
  6661. { "Panasonic DMC-G5", 143, 0xfff,
  6662. { 7798,-2562,-740,-3879,11584,2613,-1055,2248,5434 } },
  6663. { "Panasonic DMC-GF1", 15, 0xf92,
  6664. { 7888,-1902,-1011,-8106,16085,2099,-2353,2866,7330 } },
  6665. { "Panasonic DMC-GF2", 143, 0xfff,
  6666. { 7888,-1902,-1011,-8106,16085,2099,-2353,2866,7330 } },
  6667. { "Panasonic DMC-GF3", 143, 0xfff,
  6668. { 9051,-2468,-1204,-5212,13276,2121,-1197,2510,6890 } },
  6669. { "Panasonic DMC-GF5", 143, 0xfff,
  6670. { 8228,-2945,-660,-3938,11792,2430,-1094,2278,5793 } },
  6671. { "Panasonic DMC-GH1", 15, 0xf92,
  6672. { 6299,-1466,-532,-6535,13852,2969,-2331,3112,5984 } },
  6673. { "Panasonic DMC-GH2", 15, 0xf95,
  6674. { 7780,-2410,-806,-3913,11724,2484,-1018,2390,5298 } },
  6675. { "Panasonic DMC-GH3", 144, 0,
  6676. { 6559,-1752,-491,-3672,11407,2586,-962,1875,5130 } },
  6677. { "Panasonic DMC-GX1", 143, 0,
  6678. { 6763,-1919,-863,-3868,11515,2684,-1216,2387,5879 } },
  6679. { "Phase One H 20", 0, 0, /* DJC */
  6680. { 1313,1855,-109,-6715,15908,808,-327,1840,6020 } },
  6681. { "Phase One H 25", 0, 0,
  6682. { 2905,732,-237,-8134,16626,1476,-3038,4253,7517 } },
  6683. { "Phase One P 2", 0, 0,
  6684. { 2905,732,-237,-8134,16626,1476,-3038,4253,7517 } },
  6685. { "Phase One P 30", 0, 0,
  6686. { 4516,-245,-37,-7020,14976,2173,-3206,4671,7087 } },
  6687. { "Phase One P 45", 0, 0,
  6688. { 5053,-24,-117,-5684,14076,1702,-2619,4492,5849 } },
  6689. { "Phase One P40", 0, 0,
  6690. { 8035,435,-962,-6001,13872,2320,-1159,3065,5434 } },
  6691. { "Phase One P65", 0, 0,
  6692. { 8035,435,-962,-6001,13872,2320,-1159,3065,5434 } },
  6693. { "RED ONE", 704, 0xffff, /* DJC */
  6694. { 21014,-7891,-2613,-3056,12201,856,-2203,5125,8042 } },
  6695. { "SAMSUNG EX1", 0, 0x3e00,
  6696. { 8898,-2498,-994,-3144,11328,2066,-760,1381,4576 } },
  6697. { "SAMSUNG EX2F", 0, 0x7ff,
  6698. { 10648,-3897,-1055,-2022,10573,1668,-492,1611,4742 } },
  6699. { "SAMSUNG NX2", 0, 0xfff, /* NX20, NX200, NX210 */
  6700. { 6933,-2268,-753,-4921,13387,1647,-803,1641,6096 } },
  6701. { "SAMSUNG NX1000", 0, 0,
  6702. { 6933,-2268,-753,-4921,13387,1647,-803,1641,6096 } },
  6703. { "SAMSUNG NX", 0, 0, /* NX5, NX10, NX11, NX100 */
  6704. { 10332,-3234,-1168,-6111,14639,1520,-1352,2647,8331 } },
  6705. { "SAMSUNG WB2000", 0, 0xfff,
  6706. { 12093,-3557,-1155,-1000,9534,1733,-22,1787,4576 } },
  6707. { "SAMSUNG GX-1", 0, 0,
  6708. { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
  6709. { "SAMSUNG S85", 0, 0xffff, /* DJC */
  6710. { 11885,-3968,-1473,-4214,12299,1916,-835,1655,5549 } },
  6711. { "Sinar", 0, 0, /* DJC */
  6712. { 16442,-2956,-2422,-2877,12128,750,-1136,6066,4559 } },
  6713. { "SONY DSC-F828", 0, 0,
  6714. { 7924,-1910,-777,-8226,15459,2998,-1517,2199,6818,-7242,11401,3481 } },
  6715. { "SONY DSC-R1", 512, 0,
  6716. { 8512,-2641,-694,-8042,15670,2526,-1821,2117,7414 } },
  6717. { "SONY DSC-V3", 0, 0,
  6718. { 7511,-2571,-692,-7894,15088,3060,-948,1111,8128 } },
  6719. { "SONY DSC-RX100", 200, 0,
  6720. { 8651,-2754,-1057,-3464,12207,1373,-568,1398,4434 } },
  6721. { "SONY DSC-RX1", 128, 0,
  6722. { 6344,-1612,-462,-4863,12477,2681,-865,1786,6899 } },
  6723. { "SONY DSLR-A100", 0, 0xfeb,
  6724. { 9437,-2811,-774,-8405,16215,2290,-710,596,7181 } },
  6725. { "SONY DSLR-A290", 0, 0,
  6726. { 6038,-1484,-579,-9145,16746,2512,-875,746,7218 } },
  6727. { "SONY DSLR-A2", 0, 0,
  6728. { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
  6729. { "SONY DSLR-A300", 0, 0,
  6730. { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
  6731. { "SONY DSLR-A330", 0, 0,
  6732. { 9847,-3091,-929,-8485,16346,2225,-714,595,7103 } },
  6733. { "SONY DSLR-A350", 0, 0xffc,
  6734. { 6038,-1484,-578,-9146,16746,2513,-875,746,7217 } },
  6735. { "SONY DSLR-A380", 0, 0,
  6736. { 6038,-1484,-579,-9145,16746,2512,-875,746,7218 } },
  6737. { "SONY DSLR-A390", 0, 0,
  6738. { 6038,-1484,-579,-9145,16746,2512,-875,746,7218 } },
  6739. { "SONY DSLR-A450", 128, 0xfeb,
  6740. { 4950,-580,-103,-5228,12542,3029,-709,1435,7371 } },
  6741. { "SONY DSLR-A580", 128, 0xfeb,
  6742. { 5932,-1492,-411,-4813,12285,2856,-741,1524,6739 } },
  6743. { "SONY DSLR-A5", 128, 0xfeb,
  6744. { 4950,-580,-103,-5228,12542,3029,-709,1435,7371 } },
  6745. { "SONY DSLR-A700", 126, 0,
  6746. { 5775,-805,-359,-8574,16295,2391,-1943,2341,7249 } },
  6747. { "SONY DSLR-A850", 128, 0,
  6748. { 5413,-1162,-365,-5665,13098,2866,-608,1179,8440 } },
  6749. { "SONY DSLR-A900", 128, 0,
  6750. { 5209,-1072,-397,-8845,16120,2919,-1618,1803,8654 } },
  6751. { "SONY NEX-3", 128, 0, /* Adobe */
  6752. { 6549,-1550,-436,-4880,12435,2753,-854,1868,6976 } },
  6753. { "SONY NEX-5N", 128, 0,
  6754. { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
  6755. { "SONY NEX-5R", 128, 0,
  6756. { 6129,-1545,-418,-4930,12490,2743,-977,1693,6615 } },
  6757. { "SONY NEX-5", 128, 0, /* Adobe */
  6758. { 6549,-1550,-436,-4880,12435,2753,-854,1868,6976 } },
  6759. { "SONY NEX-6", 128, 0,
  6760. { 6129,-1545,-418,-4930,12490,2743,-977,1693,6615 } },
  6761. { "SONY NEX-7", 128, 0,
  6762. { 5491,-1192,-363,-4951,12342,2948,-911,1722,7192 } },
  6763. { "SONY NEX", 128, 0, /* NEX-C3, NEX-F3 */
  6764. { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
  6765. { "SONY SLT-A33", 128, 0,
  6766. { 6069,-1221,-366,-5221,12779,2734,-1024,2066,6834 } },
  6767. { "SONY SLT-A35", 128, 0,
  6768. { 5986,-1618,-415,-4557,11820,3120,-681,1404,6971 } },
  6769. { "SONY SLT-A37", 128, 0,
  6770. { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
  6771. { "SONY SLT-A55", 128, 0,
  6772. { 5932,-1492,-411,-4813,12285,2856,-741,1524,6739 } },
  6773. { "SONY SLT-A57", 128, 0,
  6774. { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
  6775. { "SONY SLT-A65", 128, 0,
  6776. { 5491,-1192,-363,-4951,12342,2948,-911,1722,7192 } },
  6777. { "SONY SLT-A77", 128, 0,
  6778. { 5491,-1192,-363,-4951,12342,2948,-911,1722,7192 } },
  6779. { "SONY SLT-A99", 128, 0,
  6780. { 6344,-1612,-462,-4863,12477,2681,-865,1786,6899 } },
  6781. };
  6782. double cam_xyz[4][3];
  6783. char name[130];
  6784. int i, j;
  6785. sprintf (name, "%s %s", t_make, t_model);
  6786. for (i=0; i < sizeof table / sizeof *table; i++)
  6787. if (!strncmp (name, table[i].prefix, strlen(table[i].prefix))) {
  6788. if (table[i].t_black) black = (ushort) table[i].t_black;
  6789. if (table[i].t_maximum) maximum = (ushort) table[i].t_maximum;
  6790. if (table[i].trans[0]) {
  6791. for (j=0; j < 12; j++)
  6792. #ifdef LIBRAW_LIBRARY_BUILD
  6793. imgdata.color.cam_xyz[0][j] =
  6794. #endif
  6795. cam_xyz[0][j] = table[i].trans[j] / 10000.0;
  6796. cam_xyz_coeff (cam_xyz);
  6797. }
  6798. break;
  6799. }
  6800. }
  6801. void CLASS simple_coeff (int index)
  6802. {
  6803. static const float table[][12] = {
  6804. /* index 0 -- all Foveon cameras */
  6805. { 1.4032,-0.2231,-0.1016,-0.5263,1.4816,0.017,-0.0112,0.0183,0.9113 },
  6806. /* index 1 -- Kodak DC20 and DC25 */
  6807. { 2.25,0.75,-1.75,-0.25,-0.25,0.75,0.75,-0.25,-0.25,-1.75,0.75,2.25 },
  6808. /* index 2 -- Logitech Fotoman Pixtura */
  6809. { 1.893,-0.418,-0.476,-0.495,1.773,-0.278,-1.017,-0.655,2.672 },
  6810. /* index 3 -- Nikon E880, E900, and E990 */
  6811. { -1.936280, 1.800443, -1.448486, 2.584324,
  6812. 1.405365, -0.524955, -0.289090, 0.408680,
  6813. -1.204965, 1.082304, 2.941367, -1.818705 }
  6814. };
  6815. int i, c;
  6816. for (raw_color = i=0; i < 3; i++)
  6817. FORCC rgb_cam[i][c] = table[index][i*colors+c];
  6818. }
  6819. short CLASS guess_byte_order (int words)
  6820. {
  6821. uchar test[4][2];
  6822. int t=2, msb;
  6823. double diff, sum[2] = {0,0};
  6824. fread (test[0], 2, 2, ifp);
  6825. for (words-=2; words--; ) {
  6826. fread (test[t], 2, 1, ifp);
  6827. for (msb=0; msb < 2; msb++) {
  6828. diff = (test[t^2][msb] << 8 | test[t^2][!msb])
  6829. - (test[t ][msb] << 8 | test[t ][!msb]);
  6830. sum[msb] += diff*diff;
  6831. }
  6832. t = (t+1) & 3;
  6833. }
  6834. return sum[0] < sum[1] ? 0x4d4d : 0x4949;
  6835. }
  6836. float CLASS find_green (int bps, int bite, int off0, int off1)
  6837. {
  6838. UINT64 bitbuf=0;
  6839. int vbits, col, i, c;
  6840. ushort img[2][2064];
  6841. double sum[]={0,0};
  6842. FORC(2) {
  6843. fseek (ifp, c ? off1:off0, SEEK_SET);
  6844. for (vbits=col=0; col < width; col++) {
  6845. for (vbits -= bps; vbits < 0; vbits += bite) {
  6846. bitbuf <<= bite;
  6847. for (i=0; i < bite; i+=8)
  6848. bitbuf |= (unsigned) (fgetc(ifp) << i);
  6849. }
  6850. img[c][col] = bitbuf << (64-bps-vbits) >> (64-bps);
  6851. }
  6852. }
  6853. FORC(width-1) {
  6854. sum[ c & 1] += ABS(img[0][c]-img[1][c+1]);
  6855. sum[~c & 1] += ABS(img[1][c]-img[0][c+1]);
  6856. }
  6857. return 100 * log(sum[0]/sum[1]);
  6858. }
  6859. #ifndef LIBRAW_LIBRARY_BUILD
  6860. void CLASS identify2(unsigned fsize, unsigned flen, char *head);
  6861. #endif
  6862. /*
  6863. Identify which camera created this file, and set global variables
  6864. accordingly.
  6865. */
  6866. void CLASS identify()
  6867. {
  6868. char head[32], *cp;
  6869. int hlen, flen, fsize, zero_fsize=1, i, c, is_canon;
  6870. struct jhead jh;
  6871. static const struct {
  6872. int fsize;
  6873. char t_make[12], t_model[19], withjpeg;
  6874. } table[] = {
  6875. { 62464, "Kodak", "DC20" ,0 },
  6876. { 124928, "Kodak", "DC20" ,0 },
  6877. { 1652736, "Kodak", "DCS200" ,0 },
  6878. { 4159302, "Kodak", "C330" ,0 },
  6879. { 4162462, "Kodak", "C330" ,0 },
  6880. { 460800, "Kodak", "C603v" ,0 },
  6881. { 614400, "Kodak", "C603v" ,0 },
  6882. { 6163328, "Kodak", "C603" ,0 },
  6883. { 6166488, "Kodak", "C603" ,0 },
  6884. { 9116448, "Kodak", "C603y" ,0 },
  6885. { 311696, "ST Micro", "STV680 VGA" ,0 }, /* SPYz */
  6886. { 787456, "Creative", "PC-CAM 600" ,0 },
  6887. { 1138688, "Minolta", "RD175" ,0 },
  6888. { 3840000, "Foculus", "531C" ,0 },
  6889. { 307200, "Generic", "640x480" ,0 },
  6890. { 786432, "AVT", "F-080C" ,0 },
  6891. { 1447680, "AVT", "F-145C" ,0 },
  6892. { 1920000, "AVT", "F-201C" ,0 },
  6893. { 5067304, "AVT", "F-510C" ,0 },
  6894. { 5067316, "AVT", "F-510C" ,0 },
  6895. { 10134608, "AVT", "F-510C" ,0 },
  6896. { 10134620, "AVT", "F-510C" ,0 },
  6897. { 16157136, "AVT", "F-810C" ,0 },
  6898. { 1409024, "Sony", "XCD-SX910CR" ,0 },
  6899. { 2818048, "Sony", "XCD-SX910CR" ,0 },
  6900. { 3884928, "Micron", "2010" ,0 },
  6901. { 6624000, "Pixelink", "A782" ,0 },
  6902. { 13248000, "Pixelink", "A782" ,0 },
  6903. { 6291456, "RoverShot","3320AF" ,0 },
  6904. { 6553440, "Canon", "PowerShot A460" ,0 },
  6905. { 6653280, "Canon", "PowerShot A530" ,0 },
  6906. { 6573120, "Canon", "PowerShot A610" ,0 },
  6907. { 9219600, "Canon", "PowerShot A620" ,0 },
  6908. { 9243240, "Canon", "PowerShot A470" ,0 },
  6909. { 10341600, "Canon", "PowerShot A720 IS",0 },
  6910. { 10383120, "Canon", "PowerShot A630" ,0 },
  6911. { 12945240, "Canon", "PowerShot A640" ,0 },
  6912. { 15636240, "Canon", "PowerShot A650" ,0 },
  6913. { 5298000, "Canon", "PowerShot SD300" ,0 },
  6914. { 7710960, "Canon", "PowerShot S3 IS" ,0 },
  6915. { 15467760, "Canon", "PowerShot SX110 IS",0 },
  6916. { 15534576, "Canon", "PowerShot SX120 IS",0 },
  6917. { 18653760, "Canon", "PowerShot SX20 IS",0 },
  6918. { 19131120, "Canon", "PowerShot SX220 HS",0 },
  6919. { 21936096, "Canon", "PowerShot SX30 IS",0 },
  6920. { 5939200, "OLYMPUS", "C770UZ" ,0 },
  6921. { 1581060, "NIKON", "E900" ,1 }, /* or E900s,E910 */
  6922. { 2465792, "NIKON", "E950" ,1 }, /* or E800,E700 */
  6923. { 2940928, "NIKON", "E2100" ,1 }, /* or E2500 */
  6924. { 4771840, "NIKON", "E990" ,1 }, /* or E995, Oly C3030Z */
  6925. { 4775936, "NIKON", "E3700" ,1 }, /* or Optio 33WR */
  6926. { 5869568, "NIKON", "E4300" ,1 }, /* or DiMAGE Z2 */
  6927. { 5865472, "NIKON", "E4500" ,1 },
  6928. { 7438336, "NIKON", "E5000" ,1 }, /* or E5700 */
  6929. { 8998912, "NIKON", "COOLPIX S6" ,1 },
  6930. { 1976352, "CASIO", "QV-2000UX" ,1 },
  6931. { 3217760, "CASIO", "QV-3*00EX" ,1 },
  6932. { 6218368, "CASIO", "QV-5700" ,1 },
  6933. { 6054400, "CASIO", "QV-R41" ,1 },
  6934. { 7530816, "CASIO", "QV-R51" ,1 },
  6935. { 7684000, "CASIO", "QV-4000" ,1 },
  6936. { 2937856, "CASIO", "EX-S20" ,1 },
  6937. { 4948608, "CASIO", "EX-S100" ,1 },
  6938. { 7542528, "CASIO", "EX-Z50" ,1 },
  6939. { 7562048, "CASIO", "EX-Z500" ,1 },
  6940. { 7753344, "CASIO", "EX-Z55" ,1 },
  6941. { 7816704, "CASIO", "EX-Z60" ,1 },
  6942. { 10843712, "CASIO", "EX-Z75" ,1 },
  6943. { 10834368, "CASIO", "EX-Z750" ,1 },
  6944. { 12310144, "CASIO", "EX-Z850" ,1 },
  6945. { 12489984, "CASIO", "EX-Z8" ,1 },
  6946. { 15499264, "CASIO", "EX-Z1050" ,1 },
  6947. { 18702336, "CASIO", "EX-ZR100" ,1 },
  6948. { 7426656, "CASIO", "EX-P505" ,1 },
  6949. { 9313536, "CASIO", "EX-P600" ,1 },
  6950. { 10979200, "CASIO", "EX-P700" ,1 },
  6951. { 3178560, "PENTAX", "Optio S" ,1 },
  6952. { 4841984, "PENTAX", "Optio S" ,1 },
  6953. { 6114240, "PENTAX", "Optio S4" ,1 }, /* or S4i, CASIO EX-Z4 */
  6954. { 10702848, "PENTAX", "Optio 750Z" ,1 },
  6955. { 15980544, "AGFAPHOTO","DC-833m" ,1 },
  6956. { 16098048, "SAMSUNG", "S85" ,1 },
  6957. { 16215552, "SAMSUNG", "S85" ,1 },
  6958. { 20487168, "SAMSUNG", "WB550" ,1 },
  6959. { 24000000, "SAMSUNG", "WB550" ,1 },
  6960. { 9994240, "ptGrey", "GRAS-50S5C" ,0 }, // KC: SUPPORT GRASSHOPPER
  6961. { 10075968, "JaiPulnix","BB-500CL" ,0 }, // KC: SUPPORT BB-500CL
  6962. { 10108896, "JaiPulnix","BB-500GE" ,0 }, // KC: SUPPORT BB-500GE
  6963. { 10036800, "SVS", "SVS625CL" ,0 }, // KC: SUPPORT SVS625 cameralink
  6964. { 12582980, "Sinar", "" ,0 },
  6965. { 33292868, "Sinar", "" ,0 },
  6966. { 44390468, "Sinar", "" ,0 } };
  6967. static const char *corp[] =
  6968. { "Canon", "NIKON", "EPSON", "KODAK", "Kodak", "OLYMPUS", "PENTAX",
  6969. "MINOLTA", "Minolta", "Konica", "CASIO", "Sinar", "Phase One",
  6970. "SAMSUNG", "Mamiya", "MOTOROLA", "LEICA" };
  6971. #ifdef LIBRAW_LIBRARY_BUILD
  6972. RUN_CALLBACK(LIBRAW_PROGRESS_IDENTIFY,0,2);
  6973. #endif
  6974. tiff_flip = flip = filters = -1; /* 0 is valid, so -1 is unknown */
  6975. raw_height = raw_width = fuji_width = fuji_layout = cr2_slice[0] = 0;
  6976. maximum = height = width = top_margin = left_margin = 0;
  6977. cdesc[0] = desc[0] = artist[0] = make[0] = model[0] = model2[0] = 0;
  6978. iso_speed = shutter = aperture = focal_len = unique_id = 0;
  6979. tiff_nifds = 0;
  6980. memset (tiff_ifd, 0, sizeof tiff_ifd);
  6981. memset (gpsdata, 0, sizeof gpsdata);
  6982. memset (cblack, 0, sizeof cblack);
  6983. memset (white, 0, sizeof white);
  6984. memset (mask, 0, sizeof mask);
  6985. thumb_offset = thumb_length = thumb_width = thumb_height = 0;
  6986. load_raw = thumb_load_raw = 0;
  6987. write_thumb = &CLASS jpeg_thumb;
  6988. data_offset = meta_length = tiff_bps = tiff_compress = 0;
  6989. kodak_cbpp = zero_after_ff = dng_version = load_flags = 0;
  6990. timestamp = shot_order = tiff_samples = black = is_foveon = 0;
  6991. mix_green = profile_length = data_error = zero_is_bad = 0;
  6992. pixel_aspect = is_raw = raw_color = 1;
  6993. tile_width = tile_length = 0;
  6994. for (i=0; i < 4; i++) {
  6995. cam_mul[i] = i == 1;
  6996. pre_mul[i] = i < 3;
  6997. FORC3 cmatrix[c][i] = 0;
  6998. FORC3 rgb_cam[c][i] = c == i;
  6999. }
  7000. colors = 3;
  7001. for (i=0; i < 0x10000; i++) curve[i] = i;
  7002. order = get2();
  7003. hlen = get4();
  7004. fseek (ifp, 0, SEEK_SET);
  7005. fread (head, 1, 32, ifp);
  7006. fseek (ifp, 0, SEEK_END);
  7007. flen = fsize = ftell(ifp);
  7008. if ((cp = (char *) memmem (head, 32, (char*)"MMMM", 4)) ||
  7009. (cp = (char *) memmem (head, 32, (char*)"IIII", 4))) {
  7010. parse_phase_one (cp-head);
  7011. if (cp-head && parse_tiff(0)) apply_tiff();
  7012. } else if (order == 0x4949 || order == 0x4d4d) {
  7013. if (!memcmp (head+6,"HEAPCCDR",8)) {
  7014. data_offset = hlen;
  7015. parse_ciff (hlen, flen - hlen);
  7016. } else if (parse_tiff(0)) apply_tiff();
  7017. } else if (!memcmp (head,"\xff\xd8\xff\xe1",4) &&
  7018. !memcmp (head+6,"Exif",4)) {
  7019. fseek (ifp, 4, SEEK_SET);
  7020. data_offset = 4 + get2();
  7021. fseek (ifp, data_offset, SEEK_SET);
  7022. if (fgetc(ifp) != 0xff)
  7023. parse_tiff(12);
  7024. thumb_offset = 0;
  7025. } else if (!memcmp (head+25,"ARECOYK",7)) {
  7026. strcpy (make, "Contax");
  7027. strcpy (model,"N Digital");
  7028. fseek (ifp, 33, SEEK_SET);
  7029. get_timestamp(1);
  7030. fseek (ifp, 60, SEEK_SET);
  7031. FORC4 cam_mul[c ^ (c >> 1)] = get4();
  7032. } else if (!strcmp (head, "PXN")) {
  7033. strcpy (make, "Logitech");
  7034. strcpy (model,"Fotoman Pixtura");
  7035. } else if (!strcmp (head, "qktk")) {
  7036. strcpy (make, "Apple");
  7037. strcpy (model,"QuickTake 100");
  7038. load_raw = &CLASS quicktake_100_load_raw;
  7039. } else if (!strcmp (head, "qktn")) {
  7040. strcpy (make, "Apple");
  7041. strcpy (model,"QuickTake 150");
  7042. load_raw = &CLASS kodak_radc_load_raw;
  7043. } else if (!memcmp (head,"FUJIFILM",8)) {
  7044. fseek (ifp, 84, SEEK_SET);
  7045. thumb_offset = get4();
  7046. thumb_length = get4();
  7047. fseek (ifp, 92, SEEK_SET);
  7048. parse_fuji (get4());
  7049. if (thumb_offset > 120) {
  7050. fseek (ifp, 120, SEEK_SET);
  7051. is_raw += (i = get4()) && 1;
  7052. if (is_raw == 2 && shot_select)
  7053. parse_fuji (i);
  7054. }
  7055. load_raw = &CLASS unpacked_load_raw;
  7056. fseek (ifp, 100+28*(shot_select > 0), SEEK_SET);
  7057. parse_tiff (data_offset = get4());
  7058. parse_tiff (thumb_offset+12);
  7059. apply_tiff();
  7060. } else if (!memcmp (head,"RIFF",4)) {
  7061. fseek (ifp, 0, SEEK_SET);
  7062. parse_riff();
  7063. } else if (!memcmp (head,"\0\001\0\001\0@",6)) {
  7064. fseek (ifp, 6, SEEK_SET);
  7065. fread (make, 1, 8, ifp);
  7066. fread (model, 1, 8, ifp);
  7067. fread (model2, 1, 16, ifp);
  7068. data_offset = get2();
  7069. get2();
  7070. raw_width = get2();
  7071. raw_height = get2();
  7072. load_raw = &CLASS nokia_load_raw;
  7073. filters = 0x61616161;
  7074. } else if (!memcmp (head,"NOKIARAW",8)) {
  7075. strcpy (make, "NOKIA");
  7076. strcpy (model, "X2");
  7077. order = 0x4949;
  7078. fseek (ifp, 300, SEEK_SET);
  7079. data_offset = get4();
  7080. i = get4();
  7081. width = get2();
  7082. height = get2();
  7083. data_offset += i - width * 5 / 4 * height;
  7084. load_raw = &CLASS nokia_load_raw;
  7085. filters = 0x61616161;
  7086. } else if (!memcmp (head,"ARRI",4)) {
  7087. order = 0x4949;
  7088. fseek (ifp, 20, SEEK_SET);
  7089. width = get4();
  7090. height = get4();
  7091. strcpy (make, "ARRI");
  7092. fseek (ifp, 668, SEEK_SET);
  7093. fread (model, 1, 64, ifp);
  7094. data_offset = 4096;
  7095. load_raw = &CLASS packed_load_raw;
  7096. load_flags = 88;
  7097. filters = 0x61616161;
  7098. } else if (!memcmp (head+4,"RED1",4)) {
  7099. strcpy (make, "RED");
  7100. strcpy (model,"ONE");
  7101. parse_redcine();
  7102. load_raw = &CLASS redcine_load_raw;
  7103. gamma_curve (1/2.4, 12.92, 1, 4095);
  7104. filters = 0x49494949;
  7105. } else if (!memcmp (head,"DSC-Image",9))
  7106. parse_rollei();
  7107. else if (!memcmp (head,"PWAD",4))
  7108. parse_sinar_ia();
  7109. else if (!memcmp (head,"\0MRM",4))
  7110. parse_minolta(0);
  7111. #ifdef LIBRAW_DEMOSAIC_PACK_GPL2
  7112. else if (!memcmp (head,"FOVb",4))
  7113. parse_foveon();
  7114. #endif
  7115. else if (!memcmp (head,"CI",2))
  7116. parse_cine();
  7117. else
  7118. for (zero_fsize=i=0; i < sizeof table / sizeof *table; i++)
  7119. if (fsize == table[i].fsize) {
  7120. strcpy (make, table[i].t_make );
  7121. strcpy (model, table[i].t_model);
  7122. if (table[i].withjpeg)
  7123. parse_external_jpeg();
  7124. }
  7125. if (zero_fsize) fsize = 0;
  7126. if (make[0] == 0) parse_smal (0, flen);
  7127. if (make[0] == 0) parse_jpeg (is_raw = 0);
  7128. for (i=0; i < sizeof corp / sizeof *corp; i++)
  7129. if (strstr (make, corp[i])) /* Simplify company names */
  7130. strcpy (make, corp[i]);
  7131. if (!strncmp (make,"KODAK",5) &&
  7132. ((cp = strstr(model," DIGITAL CAMERA")) ||
  7133. (cp = strstr(model," Digital Camera")) ||
  7134. (cp = strstr(model,"FILE VERSION"))))
  7135. *cp = 0;
  7136. cp = make + strlen(make); /* Remove trailing spaces */
  7137. while (*--cp == ' ') *cp = 0;
  7138. cp = model + strlen(model);
  7139. while (*--cp == ' ') *cp = 0;
  7140. i = strlen(make); /* Remove make from model */
  7141. if (!strncasecmp (model, make, i) && model[i++] == ' ')
  7142. memmove (model, model+i, 64-i);
  7143. if (!strncmp (model,"FinePix ",8))
  7144. strcpy (model, model+8);
  7145. if (!strncmp (model,"Digital Camera ",15))
  7146. strcpy (model, model+15);
  7147. desc[511] = artist[63] = make[63] = model[63] = model2[63] = 0;
  7148. if (!is_raw) goto notraw;
  7149. if (!height) height = raw_height;
  7150. if (!width) width = raw_width;
  7151. if (height == 2624 && width == 3936) /* Pentax K10D and Samsung GX10 */
  7152. { height = 2616; width = 3896; }
  7153. if (height == 3136 && width == 4864) /* Pentax K20D and Samsung GX20 */
  7154. { height = 3124; width = 4688; filters = 0x16161616; }
  7155. if (width == 4352 && (!strcmp(model,"K-r") || !strcmp(model,"K-x")))
  7156. { width = 4309; filters = 0x16161616; }
  7157. if (width >= 4960 && !strncmp(model,"K-5",3))
  7158. { left_margin = 10; width = 4950; filters = 0x16161616; }
  7159. if (width >= 4960 && !strncmp(model,"K-5 II",6))
  7160. { left_margin = 10; width = 4948; filters = 0x16161616; }
  7161. if (width == 4736 && !strcmp(model,"K-7"))
  7162. { height = 3122; width = 4684; filters = 0x16161616; top_margin = 2; }
  7163. if (width == 7424 && !strcmp(model,"645D"))
  7164. { height = 5502; width = 7328; filters = 0x61616161; top_margin = 29;
  7165. left_margin = 48; }
  7166. if (height == 3014 && width == 4096) /* Ricoh GX200 */
  7167. width = 4014;
  7168. if (dng_version) {
  7169. if (filters == UINT_MAX) filters = 0;
  7170. if (filters) is_raw = tiff_samples;
  7171. else colors = tiff_samples;
  7172. if (tiff_compress == 1)
  7173. load_raw = &CLASS packed_dng_load_raw;
  7174. if (tiff_compress == 7)
  7175. load_raw = &CLASS lossless_dng_load_raw;
  7176. goto dng_skip;
  7177. }
  7178. if ((is_canon = !strcmp(make,"Canon")))
  7179. load_raw = memcmp (head+6,"HEAPCCDR",8) ?
  7180. &CLASS lossless_jpeg_load_raw : &CLASS canon_load_raw;
  7181. if (!strcmp(make,"NIKON")) {
  7182. if (!load_raw)
  7183. load_raw = &CLASS packed_load_raw;
  7184. if (model[0] == 'E')
  7185. load_flags |= !data_offset << 2 | 2;
  7186. }
  7187. if (!strcmp(make,"CASIO")) {
  7188. load_raw = &CLASS packed_load_raw;
  7189. maximum = 0xf7f;
  7190. }
  7191. /* Set parameters based on camera name (for non-DNG files). */
  7192. if (is_foveon) {
  7193. if (height*2 < width) pixel_aspect = 0.5;
  7194. if (height > width) pixel_aspect = 2;
  7195. filters = 0;
  7196. simple_coeff(0);
  7197. } else if (is_canon && tiff_bps == 15) {
  7198. switch (width) {
  7199. case 3344: width -= 66;
  7200. case 3872: width -= 6;
  7201. }
  7202. if (height > width) SWAP(height,width);
  7203. filters = 0;
  7204. load_raw = &CLASS canon_sraw_load_raw;
  7205. } else if (!strcmp(model,"PowerShot 600")) {
  7206. height = 613;
  7207. width = 854;
  7208. raw_width = 896;
  7209. pixel_aspect = 607/628.0;
  7210. colors = 4;
  7211. filters = 0xe1e4e1e4;
  7212. load_raw = &CLASS canon_600_load_raw;
  7213. } else if (!strcmp(model,"PowerShot A5") ||
  7214. !strcmp(model,"PowerShot A5 Zoom")) {
  7215. height = 773;
  7216. width = 960;
  7217. raw_width = 992;
  7218. pixel_aspect = 256/235.0;
  7219. colors = 4;
  7220. filters = 0x1e4e1e4e;
  7221. goto canon_a5;
  7222. } else if (!strcmp(model,"PowerShot A50")) {
  7223. height = 968;
  7224. width = 1290;
  7225. raw_width = 1320;
  7226. colors = 4;
  7227. filters = 0x1b4e4b1e;
  7228. goto canon_a5;
  7229. } else if (!strcmp(model,"PowerShot Pro70")) {
  7230. height = 1024;
  7231. width = 1552;
  7232. colors = 4;
  7233. filters = 0x1e4b4e1b;
  7234. goto canon_a5;
  7235. } else if (!strcmp(model,"PowerShot SD300")) {
  7236. height = 1752;
  7237. width = 2344;
  7238. raw_height = 1766;
  7239. raw_width = 2400;
  7240. top_margin = 12;
  7241. left_margin = 12;
  7242. goto canon_a5;
  7243. } else if (!strcmp(model,"PowerShot A460")) {
  7244. height = 1960;
  7245. width = 2616;
  7246. raw_height = 1968;
  7247. raw_width = 2664;
  7248. top_margin = 4;
  7249. left_margin = 4;
  7250. goto canon_a5;
  7251. } else if (!strcmp(model,"PowerShot A530")) {
  7252. height = 1984;
  7253. width = 2620;
  7254. raw_height = 1992;
  7255. raw_width = 2672;
  7256. top_margin = 6;
  7257. left_margin = 10;
  7258. goto canon_a5;
  7259. } else if (!strcmp(model,"PowerShot A610")) {
  7260. if (canon_s2is()) strcpy (model+10, "S2 IS");
  7261. height = 1960;
  7262. width = 2616;
  7263. raw_height = 1968;
  7264. raw_width = 2672;
  7265. top_margin = 8;
  7266. left_margin = 12;
  7267. goto canon_a5;
  7268. } else if (!strcmp(model,"PowerShot A620")) {
  7269. height = 2328;
  7270. width = 3112;
  7271. raw_height = 2340;
  7272. raw_width = 3152;
  7273. top_margin = 12;
  7274. left_margin = 36;
  7275. goto canon_a5;
  7276. } else if (!strcmp(model,"PowerShot A470")) {
  7277. height = 2328;
  7278. width = 3096;
  7279. raw_height = 2346;
  7280. raw_width = 3152;
  7281. top_margin = 6;
  7282. left_margin = 12;
  7283. goto canon_a5;
  7284. } else if (!strcmp(model,"PowerShot A720 IS")) {
  7285. height = 2472;
  7286. width = 3298;
  7287. raw_height = 2480;
  7288. raw_width = 3336;
  7289. top_margin = 5;
  7290. left_margin = 6;
  7291. goto canon_a5;
  7292. } else if (!strcmp(model,"PowerShot A630")) {
  7293. height = 2472;
  7294. width = 3288;
  7295. raw_height = 2484;
  7296. raw_width = 3344;
  7297. top_margin = 6;
  7298. left_margin = 12;
  7299. goto canon_a5;
  7300. } else if (!strcmp(model,"PowerShot A640")) {
  7301. height = 2760;
  7302. width = 3672;
  7303. raw_height = 2772;
  7304. raw_width = 3736;
  7305. top_margin = 6;
  7306. left_margin = 12;
  7307. goto canon_a5;
  7308. } else if (!strcmp(model,"PowerShot A650")) {
  7309. height = 3024;
  7310. width = 4032;
  7311. raw_height = 3048;
  7312. raw_width = 4104;
  7313. top_margin = 12;
  7314. left_margin = 48;
  7315. goto canon_a5;
  7316. } else if (!strcmp(model,"PowerShot S3 IS")) {
  7317. height = 2128;
  7318. width = 2840;
  7319. raw_height = 2136;
  7320. raw_width = 2888;
  7321. top_margin = 8;
  7322. left_margin = 44;
  7323. canon_a5:
  7324. tiff_bps = 10;
  7325. load_raw = &CLASS packed_load_raw;
  7326. load_flags = 40;
  7327. if (raw_width > 1600) zero_is_bad = 1;
  7328. } else if (!strcmp(model,"PowerShot SX110 IS")) {
  7329. height = 2760;
  7330. width = 3684;
  7331. raw_height = 2772;
  7332. raw_width = 3720;
  7333. top_margin = 12;
  7334. left_margin = 6;
  7335. load_raw = &CLASS packed_load_raw;
  7336. load_flags = 40;
  7337. zero_is_bad = 1;
  7338. } else if (!strcmp(model,"PowerShot SX120 IS")) {
  7339. height = 2742;
  7340. width = 3664;
  7341. raw_height = 2778;
  7342. raw_width = 3728;
  7343. top_margin = 18;
  7344. left_margin = 16;
  7345. filters = 0x49494949;
  7346. load_raw = &CLASS packed_load_raw;
  7347. load_flags = 40;
  7348. zero_is_bad = 1;
  7349. } else if (!strcmp(model,"PowerShot SX20 IS")) {
  7350. height = 3024;
  7351. width = 4032;
  7352. raw_height = 3048;
  7353. raw_width = 4080;
  7354. top_margin = 12;
  7355. left_margin = 24;
  7356. load_raw = &CLASS packed_load_raw;
  7357. load_flags = 40;
  7358. zero_is_bad = 1;
  7359. } else if (!strcmp(model,"PowerShot SX220 HS")) {
  7360. height = 3043;
  7361. width = 4072;
  7362. raw_height = 3060;
  7363. raw_width = 4168;
  7364. mask[0][0] = top_margin = 16;
  7365. mask[0][2] = top_margin + height;
  7366. mask[0][3] = left_margin = 92;
  7367. load_raw = &CLASS packed_load_raw;
  7368. load_flags = 8;
  7369. zero_is_bad = 1;
  7370. } else if (!strcmp(model,"PowerShot SX50 HS")) {
  7371. top_margin=16;
  7372. left_margin=94;
  7373. height = 3043;
  7374. width = 4072;
  7375. } else if (!strcmp(model,"PowerShot SX30 IS")) {
  7376. height = 3254;
  7377. width = 4366;
  7378. raw_height = 3276;
  7379. raw_width = 4464;
  7380. top_margin = 10;
  7381. left_margin = 25;
  7382. filters = 0x16161616;
  7383. load_raw = &CLASS packed_load_raw;
  7384. load_flags = 40;
  7385. zero_is_bad = 1;
  7386. } else if (!strcmp(model,"PowerShot Pro90 IS")) {
  7387. width = 1896;
  7388. colors = 4;
  7389. filters = 0xb4b4b4b4;
  7390. } else if (is_canon && raw_width == 2144) {
  7391. height = 1550;
  7392. width = 2088;
  7393. top_margin = 8;
  7394. left_margin = 4;
  7395. if (!strcmp(model,"PowerShot G1")) {
  7396. colors = 4;
  7397. filters = 0xb4b4b4b4;
  7398. }
  7399. } else if (is_canon && raw_width == 2224) {
  7400. height = 1448;
  7401. width = 2176;
  7402. top_margin = 6;
  7403. left_margin = 48;
  7404. } else if (is_canon && raw_width == 2376) {
  7405. height = 1720;
  7406. width = 2312;
  7407. top_margin = 6;
  7408. left_margin = 12;
  7409. } else if (is_canon && raw_width == 2672) {
  7410. height = 1960;
  7411. width = 2616;
  7412. top_margin = 6;
  7413. left_margin = 12;
  7414. } else if (is_canon && raw_width == 3152) {
  7415. height = 2056;
  7416. width = 3088;
  7417. top_margin = 12;
  7418. left_margin = 64;
  7419. if (unique_id == 0x80000170)
  7420. adobe_coeff ("Canon","EOS 300D");
  7421. } else if (is_canon && raw_width == 3160) {
  7422. height = 2328;
  7423. width = 3112;
  7424. top_margin = 12;
  7425. left_margin = 44;
  7426. } else if (is_canon && raw_width == 3344) {
  7427. height = 2472;
  7428. width = 3288;
  7429. top_margin = 6;
  7430. left_margin = 4;
  7431. } else if (!strcmp(model,"EOS D2000C")) {
  7432. filters = 0x61616161;
  7433. black = curve[200];
  7434. } else if (is_canon && raw_width == 3516) {
  7435. top_margin = 14;
  7436. left_margin = 42;
  7437. if (unique_id == 0x80000189)
  7438. adobe_coeff ("Canon","EOS 350D");
  7439. goto canon_cr2;
  7440. } else if (is_canon && raw_width == 3596) {
  7441. top_margin = 12;
  7442. left_margin = 74;
  7443. goto canon_cr2;
  7444. } else if (is_canon && raw_width == 3744) {
  7445. height = 2760;
  7446. width = 3684;
  7447. top_margin = 16;
  7448. left_margin = 8;
  7449. if (unique_id > 0x2720000) {
  7450. top_margin = 12;
  7451. left_margin = 52;
  7452. }
  7453. } else if (is_canon && raw_width == 3944) {
  7454. height = 2602;
  7455. width = 3908;
  7456. top_margin = 18;
  7457. left_margin = 30;
  7458. } else if (is_canon && raw_width == 3948) {
  7459. top_margin = 18;
  7460. left_margin = 42;
  7461. height -= 2;
  7462. if (unique_id == 0x80000236)
  7463. adobe_coeff ("Canon","EOS 400D");
  7464. if (unique_id == 0x80000254)
  7465. adobe_coeff ("Canon","EOS 1000D");
  7466. goto canon_cr2;
  7467. } else if (is_canon && raw_width == 3984) {
  7468. top_margin = 20;
  7469. left_margin = 76;
  7470. height -= 2;
  7471. goto canon_cr2;
  7472. } else if (is_canon && raw_width == 4104) {
  7473. height = 3024;
  7474. width = 4032;
  7475. top_margin = 12;
  7476. left_margin = 48;
  7477. } else if (is_canon && raw_width == 4152) {
  7478. top_margin = 12;
  7479. left_margin = 192;
  7480. goto canon_cr2;
  7481. } else if (is_canon && raw_width == 4160) {
  7482. height = 3048;
  7483. width = 4048;
  7484. top_margin = 11;
  7485. left_margin = 104;
  7486. } else if (is_canon && raw_width == 4176) {
  7487. height = 3045;
  7488. width = 4072;
  7489. left_margin = 96;
  7490. mask[0][0] = top_margin = 17;
  7491. mask[0][2] = raw_height;
  7492. mask[0][3] = 80;
  7493. filters = 0x49494949;
  7494. } else if (is_canon && raw_width == 4312) {
  7495. top_margin = 18;
  7496. left_margin = 22;
  7497. height -= 2;
  7498. if (unique_id == 0x80000176)
  7499. adobe_coeff ("Canon","EOS 450D");
  7500. goto canon_cr2;
  7501. } else if (is_canon && raw_width == 4352) {
  7502. top_margin = 18;
  7503. left_margin = 62;
  7504. if (unique_id == 0x80000288)
  7505. adobe_coeff ("Canon","EOS 1100D");
  7506. goto canon_cr2;
  7507. } else if (is_canon && raw_width == 4476) {
  7508. top_margin = 34;
  7509. left_margin = 90;
  7510. goto canon_cr2;
  7511. } else if (is_canon && raw_width == 4480) {
  7512. height = 3326;
  7513. width = 4432;
  7514. top_margin = 10;
  7515. left_margin = 12;
  7516. filters = 0x49494949;
  7517. } else if (is_canon && raw_width == 4496) {
  7518. height = 3316;
  7519. width = 4404;
  7520. top_margin = 50;
  7521. left_margin = 80;
  7522. } else if (is_canon && raw_width == 4832) {
  7523. top_margin = unique_id == 0x80000261 ? 51:26;
  7524. left_margin = 62;
  7525. if (unique_id == 0x80000252)
  7526. adobe_coeff ("Canon","EOS 500D");
  7527. goto canon_cr2;
  7528. } else if (is_canon && raw_width == 5108) {
  7529. top_margin = 13;
  7530. left_margin = 98;
  7531. goto canon_cr2;
  7532. } else if (is_canon && raw_width == 5120) {
  7533. height -= top_margin = 45;
  7534. left_margin = 142;
  7535. width = 4916;
  7536. } else if (is_canon && raw_width == 5280) {
  7537. top_margin = 52;
  7538. left_margin = 72;
  7539. if (unique_id == 0x80000301)
  7540. adobe_coeff ("Canon","EOS 650D");
  7541. goto canon_cr2;
  7542. } else if (is_canon && raw_width == 5344) {
  7543. top_margin = 51;
  7544. left_margin = 142;
  7545. if (unique_id == 0x80000269) {
  7546. top_margin = 100;
  7547. left_margin = 126;
  7548. height -= 2;
  7549. adobe_coeff ("Canon","EOS-1D X");
  7550. }
  7551. if (unique_id == 0x80000270)
  7552. adobe_coeff ("Canon","EOS 550D");
  7553. if (unique_id == 0x80000286)
  7554. adobe_coeff ("Canon","EOS 600D");
  7555. goto canon_cr2;
  7556. } else if (is_canon && raw_width == 5360) {
  7557. top_margin = 51;
  7558. left_margin = 158;
  7559. goto canon_cr2;
  7560. } else if (is_canon && raw_width == 5568) {
  7561. top_margin = 38;
  7562. left_margin = 72;
  7563. goto canon_cr2;
  7564. } else if (is_canon && raw_width == 5712) {
  7565. height = 3752;
  7566. width = 5640;
  7567. top_margin = 20;
  7568. left_margin = 62;
  7569. } else if (is_canon && raw_width == 5792) {
  7570. top_margin = 51;
  7571. left_margin = 158;
  7572. canon_cr2:
  7573. height -= top_margin;
  7574. width -= left_margin;
  7575. } else if (is_canon && raw_width == 5920) {
  7576. height = 3870;
  7577. width = 5796;
  7578. top_margin = 80;
  7579. left_margin = 122;
  7580. } else if (!strcmp(model,"D1")) {
  7581. cam_mul[0] *= 256/527.0;
  7582. cam_mul[2] *= 256/317.0;
  7583. } else if (!strcmp(model,"D1X")) {
  7584. width -= 4;
  7585. pixel_aspect = 0.5;
  7586. } else if (!strcmp(model,"D40X") ||
  7587. !strcmp(model,"D60") ||
  7588. !strcmp(model,"D80") ||
  7589. !strcmp(model,"D3000")) {
  7590. height -= 3;
  7591. width -= 4;
  7592. } else if (!strcmp(model,"D3") ||
  7593. !strcmp(model,"D3S") ||
  7594. !strcmp(model,"D700")) {
  7595. width -= 4;
  7596. left_margin = 2;
  7597. } else if (!strcmp(model,"D3100")) {
  7598. width -= 28;
  7599. left_margin = 6;
  7600. } else if (!strcmp(model,"D5000") ||
  7601. !strcmp(model,"D90")) {
  7602. width -= 42;
  7603. } else if (!strcmp(model,"D5100") ||
  7604. !strcmp(model,"D7000")) {
  7605. width -= 44;
  7606. } else if (!strcmp(model,"D3200") ||
  7607. !strcmp(model,"D800E") ||
  7608. !strcmp(model,"D600") ||
  7609. !strcmp(model,"D800")) {
  7610. width -= 46;
  7611. } else if (!strcmp(model,"D4")) {
  7612. width -= 52;
  7613. left_margin = 2;
  7614. } else if (!strcmp(model,"D600")) {
  7615. width -= 48;
  7616. } else if (!strncmp(model,"D40",3) ||
  7617. !strncmp(model,"D50",3) ||
  7618. !strncmp(model,"D70",3)) {
  7619. width--;
  7620. } else if (!strcmp(model,"D100")) {
  7621. if (load_flags)
  7622. raw_width = (width += 3) + 3;
  7623. } else if (!strcmp(model,"D200")) {
  7624. left_margin = 1;
  7625. width -= 4;
  7626. filters = 0x94949494;
  7627. } else if (!strncmp(model,"D2H",3)) {
  7628. left_margin = 6;
  7629. width -= 14;
  7630. } else if (!strncmp(model,"D2X",3)) {
  7631. if (width == 3264) width -= 32;
  7632. else width -= 8;
  7633. } else if (!strncmp(model,"D300",4)) {
  7634. width -= 32;
  7635. } else if (!strcmp(make,"NIKON") && raw_width == 4032) {
  7636. adobe_coeff ("NIKON","COOLPIX P7700");
  7637. } else if (!strncmp(model,"COOLPIX P",9)) {
  7638. load_flags = 24;
  7639. filters = 0x94949494;
  7640. if (model[9] == '7' && iso_speed >= 400)
  7641. black = 255;
  7642. } else if (!strncmp(model,"1 ",2)) {
  7643. height -= 2;
  7644. } else if (fsize == 1581060) {
  7645. height = 963;
  7646. width = 1287;
  7647. raw_width = 1632;
  7648. maximum = 0x3f4;
  7649. colors = 4;
  7650. filters = 0x1e1e1e1e;
  7651. simple_coeff(3);
  7652. pre_mul[0] = 1.2085;
  7653. pre_mul[1] = 1.0943;
  7654. pre_mul[3] = 1.1103;
  7655. goto e900;
  7656. } else if (fsize == 2465792) {
  7657. height = 1203;
  7658. width = 1616;
  7659. raw_width = 2048;
  7660. colors = 4;
  7661. filters = 0x4b4b4b4b;
  7662. adobe_coeff ("NIKON","E950");
  7663. e900:
  7664. tiff_bps = 10;
  7665. load_raw = &CLASS packed_load_raw;
  7666. load_flags = 6;
  7667. } else if (fsize == 4771840) {
  7668. height = 1540;
  7669. width = 2064;
  7670. colors = 4;
  7671. filters = 0xe1e1e1e1;
  7672. load_raw = &CLASS packed_load_raw;
  7673. load_flags = 6;
  7674. if (!timestamp && nikon_e995())
  7675. strcpy (model, "E995");
  7676. if (strcmp(model,"E995")) {
  7677. filters = 0xb4b4b4b4;
  7678. simple_coeff(3);
  7679. pre_mul[0] = 1.196;
  7680. pre_mul[1] = 1.246;
  7681. pre_mul[2] = 1.018;
  7682. }
  7683. } else if (!strcmp(model,"E2100")) {
  7684. if (!timestamp && !nikon_e2100()) goto cp_e2500;
  7685. height = 1206;
  7686. width = 1616;
  7687. load_flags = 30;
  7688. } else if (!strcmp(model,"E2500")) {
  7689. cp_e2500:
  7690. strcpy (model, "E2500");
  7691. height = 1204;
  7692. width = 1616;
  7693. colors = 4;
  7694. filters = 0x4b4b4b4b;
  7695. } else if (fsize == 4775936) {
  7696. height = 1542;
  7697. width = 2064;
  7698. load_raw = &CLASS packed_load_raw;
  7699. load_flags = 30;
  7700. if (!timestamp) nikon_3700();
  7701. if (model[0] == 'E' && atoi(model+1) < 3700)
  7702. filters = 0x49494949;
  7703. if (!strcmp(model,"Optio 33WR")) {
  7704. flip = 1;
  7705. filters = 0x16161616;
  7706. }
  7707. if (make[0] == 'O') {
  7708. i = find_green (12, 32, 1188864, 3576832);
  7709. c = find_green (12, 32, 2383920, 2387016);
  7710. if (abs(i) < abs(c)) {
  7711. SWAP(i,c);
  7712. load_flags = 24;
  7713. }
  7714. if (i < 0) filters = 0x61616161;
  7715. }
  7716. } else if (fsize == 5869568) {
  7717. height = 1710;
  7718. width = 2288;
  7719. filters = 0x16161616;
  7720. if (!timestamp && minolta_z2()) {
  7721. strcpy (make, "Minolta");
  7722. strcpy (model,"DiMAGE Z2");
  7723. }
  7724. load_raw = &CLASS packed_load_raw;
  7725. load_flags = 6 + 24*(make[0] == 'M');
  7726. } else if (!strcmp(model,"E4500")) {
  7727. height = 1708;
  7728. width = 2288;
  7729. colors = 4;
  7730. filters = 0xb4b4b4b4;
  7731. } else if (fsize == 7438336) {
  7732. height = 1924;
  7733. width = 2576;
  7734. colors = 4;
  7735. filters = 0xb4b4b4b4;
  7736. } else if (fsize == 8998912) {
  7737. height = 2118;
  7738. width = 2832;
  7739. maximum = 0xf83;
  7740. load_raw = &CLASS packed_load_raw;
  7741. load_flags = 30;
  7742. } else if (!strcmp(make,"FUJIFILM")) {
  7743. if (!strcmp(model+7,"S2Pro")) {
  7744. strcpy (model,"S2Pro");
  7745. height = 2144;
  7746. width = 2880;
  7747. flip = 6;
  7748. } else if (load_raw != &CLASS packed_load_raw)
  7749. maximum = (is_raw == 2 && shot_select) ? 0x2f00 : 0x3e00;
  7750. top_margin = (raw_height - height) >> 2 << 1;
  7751. left_margin = (raw_width - width ) >> 2 << 1;
  7752. if (width == 2848) filters = 0x16161616;
  7753. if (width == 3328) {
  7754. width = 3262;
  7755. left_margin = 34;
  7756. }
  7757. if (width == 4952) {
  7758. left_margin = 0;
  7759. filters = 2;
  7760. }
  7761. if (fuji_layout) raw_width *= is_raw;
  7762. } else if (!strcmp(model,"RD175")) {
  7763. height = 986;
  7764. width = 1534;
  7765. data_offset = 513;
  7766. filters = 0x61616161;
  7767. load_raw = &CLASS minolta_rd175_load_raw;
  7768. } else if (!strcmp(model,"KD-400Z")) {
  7769. height = 1712;
  7770. width = 2312;
  7771. raw_width = 2336;
  7772. goto konica_400z;
  7773. } else if (!strcmp(model,"KD-510Z")) {
  7774. goto konica_510z;
  7775. } else if (!strcasecmp(make,"MINOLTA")) {
  7776. load_raw = &CLASS unpacked_load_raw;
  7777. maximum = 0xfff;
  7778. if (!strncmp(model,"DiMAGE A",8)) {
  7779. if (!strcmp(model,"DiMAGE A200"))
  7780. filters = 0x49494949;
  7781. tiff_bps = 12;
  7782. load_raw = &CLASS packed_load_raw;
  7783. } else if (!strncmp(model,"ALPHA",5) ||
  7784. !strncmp(model,"DYNAX",5) ||
  7785. !strncmp(model,"MAXXUM",6)) {
  7786. sprintf (model+20, "DYNAX %-10s", model+6+(model[0]=='M'));
  7787. adobe_coeff (make, model+20);
  7788. load_raw = &CLASS packed_load_raw;
  7789. } else if (!strncmp(model,"DiMAGE G",8)) {
  7790. if (model[8] == '4') {
  7791. height = 1716;
  7792. width = 2304;
  7793. } else if (model[8] == '5') {
  7794. konica_510z:
  7795. height = 1956;
  7796. width = 2607;
  7797. raw_width = 2624;
  7798. } else if (model[8] == '6') {
  7799. height = 2136;
  7800. width = 2848;
  7801. }
  7802. data_offset += 14;
  7803. filters = 0x61616161;
  7804. konica_400z:
  7805. load_raw = &CLASS unpacked_load_raw;
  7806. maximum = 0x3df;
  7807. order = 0x4d4d;
  7808. }
  7809. } else if (!strcmp(model,"*ist D")) {
  7810. load_raw = &CLASS unpacked_load_raw;
  7811. data_error = -1;
  7812. } else if (!strcmp(model,"*ist DS")) {
  7813. height -= 2;
  7814. } else if (!strcmp(model,"Optio S")) {
  7815. if (fsize == 3178560) {
  7816. height = 1540;
  7817. width = 2064;
  7818. load_raw = &CLASS eight_bit_load_raw;
  7819. cam_mul[0] *= 4;
  7820. cam_mul[2] *= 4;
  7821. } else {
  7822. height = 1544;
  7823. width = 2068;
  7824. raw_width = 3136;
  7825. load_raw = &CLASS packed_load_raw;
  7826. maximum = 0xf7c;
  7827. }
  7828. } else if (fsize == 6114240) {
  7829. height = 1737;
  7830. width = 2324;
  7831. raw_width = 3520;
  7832. load_raw = &CLASS packed_load_raw;
  7833. maximum = 0xf7a;
  7834. } else if (!strcmp(model,"Optio 750Z")) {
  7835. height = 2302;
  7836. width = 3072;
  7837. load_raw = &CLASS packed_load_raw;
  7838. load_flags = 30;
  7839. } else if (!strcmp(model,"DC-833m")) {
  7840. height = 2448;
  7841. width = 3264;
  7842. order = 0x4949;
  7843. filters = 0x61616161;
  7844. load_raw = &CLASS unpacked_load_raw;
  7845. maximum = 0xfc00;
  7846. } else if (!strncmp(model,"S85",3)) {
  7847. height = 2448;
  7848. width = 3264;
  7849. raw_width = fsize/height/2;
  7850. order = 0x4d4d;
  7851. load_raw = &CLASS unpacked_load_raw;
  7852. } else if (!strcmp(make,"SAMSUNG") && raw_width == 4704) {
  7853. height -= top_margin = 8;
  7854. width -= 2 * (left_margin = 8);
  7855. load_flags = 32;
  7856. } else if (!strcmp(make,"SAMSUNG") && raw_width == 5632) {
  7857. order = 0x4949;
  7858. height = 3694;
  7859. top_margin = 2;
  7860. width = 5574 - (left_margin = 32 + tiff_bps);
  7861. if (tiff_bps == 12) load_flags = 80;
  7862. } else if (!strcmp(model,"EX1")) {
  7863. order = 0x4949;
  7864. height -= 20;
  7865. top_margin = 2;
  7866. if ((width -= 6) > 3682) {
  7867. height -= 10;
  7868. width -= 46;
  7869. top_margin = 8;
  7870. }
  7871. } else if (!strcmp(model,"WB2000")) {
  7872. order = 0x4949;
  7873. height -= 3;
  7874. top_margin = 2;
  7875. if ((width -= 10) > 3718) {
  7876. height -= 28;
  7877. width -= 56;
  7878. top_margin = 8;
  7879. }
  7880. } else if (fsize == 20487168) {
  7881. height = 2808;
  7882. width = 3648;
  7883. goto wb550;
  7884. } else if (fsize == 24000000) {
  7885. height = 3000;
  7886. width = 4000;
  7887. wb550:
  7888. strcpy (model, "WB550");
  7889. order = 0x4d4d;
  7890. load_raw = &CLASS unpacked_load_raw;
  7891. load_flags = 6;
  7892. maximum = 0x3df;
  7893. } else if (!strcmp(model,"EX2F")) {
  7894. height = 3045;
  7895. width = 4070;
  7896. top_margin = 3;
  7897. order = 0x4949;
  7898. filters = 0x49494949;
  7899. load_raw = &CLASS unpacked_load_raw;
  7900. } else if (!strcmp(model,"STV680 VGA")) {
  7901. height = 484;
  7902. width = 644;
  7903. load_raw = &CLASS eight_bit_load_raw;
  7904. flip = 2;
  7905. filters = 0x16161616;
  7906. black = 16;
  7907. } else
  7908. identify2(fsize,flen,head); /* Avoid MS VS 2008/2010 bug */
  7909. if (!model[0])
  7910. sprintf (model, "%dx%d", width, height);
  7911. if (filters == UINT_MAX) filters = 0x94949494;
  7912. if (raw_color) adobe_coeff (make, model);
  7913. if (load_raw == &CLASS kodak_radc_load_raw)
  7914. if (raw_color) adobe_coeff ("Apple","Quicktake");
  7915. if (thumb_offset && !thumb_height) {
  7916. fseek (ifp, thumb_offset, SEEK_SET);
  7917. if (ljpeg_start (&jh, 1)) {
  7918. thumb_width = jh.wide;
  7919. thumb_height = jh.high;
  7920. }
  7921. }
  7922. dng_skip:
  7923. if (fuji_width) {
  7924. fuji_width = width >> !fuji_layout;
  7925. if (~fuji_width & 1) filters = 0x49494949;
  7926. width = (height >> fuji_layout) + fuji_width;
  7927. height = width - 1;
  7928. pixel_aspect = 1;
  7929. } else {
  7930. if (raw_height < height) raw_height = height;
  7931. if (raw_width < width ) raw_width = width;
  7932. }
  7933. if (!tiff_bps) tiff_bps = 12;
  7934. if (!maximum) maximum = (1 << tiff_bps) - 1;
  7935. if (!load_raw || height < 22) is_raw = 0;
  7936. #ifdef NO_JASPER
  7937. if (load_raw == &CLASS redcine_load_raw) {
  7938. #ifdef DCRAW_VERBOSE
  7939. fprintf (stderr,_("%s: You must link dcraw with %s!!\n"),
  7940. ifname, "libjasper");
  7941. #endif
  7942. is_raw = 0;
  7943. #ifdef LIBRAW_LIBRARY_BUILD
  7944. imgdata.process_warnings |= LIBRAW_WARN_NO_JASPER;
  7945. #endif
  7946. }
  7947. #endif
  7948. #ifdef NO_JPEG
  7949. if (load_raw == &CLASS kodak_jpeg_load_raw ||
  7950. load_raw == &CLASS lossy_dng_load_raw) {
  7951. #ifdef DCRAW_VERBOSE
  7952. fprintf (stderr,_("%s: You must link dcraw with %s!!\n"),
  7953. ifname, "libjpeg");
  7954. #endif
  7955. is_raw = 0;
  7956. #ifdef LIBRAW_LIBRARY_BUILD
  7957. imgdata.process_warnings |= LIBRAW_WARN_NO_JPEGLIB;
  7958. #endif
  7959. }
  7960. #endif
  7961. if (!cdesc[0])
  7962. strcpy (cdesc, colors == 3 ? "RGBG":"GMCY");
  7963. if (!raw_height) raw_height = height;
  7964. if (!raw_width ) raw_width = width;
  7965. if (filters && colors == 3)
  7966. filters |= ((filters >> 2 & 0x22222222) |
  7967. (filters << 2 & 0x88888888)) & filters << 1;
  7968. notraw:
  7969. if (flip == -1) flip = tiff_flip;
  7970. if (flip == -1) flip = 0;
  7971. #ifdef LIBRAW_LIBRARY_BUILD
  7972. RUN_CALLBACK(LIBRAW_PROGRESS_IDENTIFY,1,2);
  7973. #endif
  7974. }
  7975. void CLASS identify2(unsigned fsize, unsigned flen, char *head)
  7976. {
  7977. short pana[][6] = {
  7978. { 3130, 1743, 4, 0, -6, 0 },
  7979. { 3130, 2055, 4, 0, -6, 0 },
  7980. { 3130, 2319, 4, 0, -6, 0 },
  7981. { 3170, 2103, 18, 0,-42, 20 },
  7982. { 3170, 2367, 18, 13,-42,-21 },
  7983. { 3177, 2367, 0, 0, -1, 0 },
  7984. { 3304, 2458, 0, 0, -1, 0 },
  7985. { 3330, 2463, 9, 0, -5, 0 },
  7986. { 3330, 2479, 9, 0,-17, 4 },
  7987. { 3370, 1899, 15, 0,-44, 20 },
  7988. { 3370, 2235, 15, 0,-44, 20 },
  7989. { 3370, 2511, 15, 10,-44,-21 },
  7990. { 3690, 2751, 3, 0, -8, -3 },
  7991. { 3710, 2751, 0, 0, -3, 0 },
  7992. { 3724, 2450, 0, 0, 0, -2 },
  7993. { 3770, 2487, 17, 0,-44, 19 },
  7994. { 3770, 2799, 17, 15,-44,-19 },
  7995. { 3880, 2170, 6, 0, -6, 0 },
  7996. { 4060, 3018, 0, 0, 0, -2 },
  7997. { 4290, 2391, 3, 0, -8, -1 },
  7998. { 4330, 2439, 17, 15,-44,-19 },
  7999. { 4508, 2962, 0, 0, -3, -4 },
  8000. { 4508, 3330, 0, 0, -3, -6 } };
  8001. struct jhead jh;
  8002. int i;
  8003. if (!strcmp(model,"N95")) {
  8004. height = raw_height - (top_margin = 2);
  8005. } else if (!strcmp(model,"531C")) {
  8006. height = 1200;
  8007. width = 1600;
  8008. load_raw = &CLASS unpacked_load_raw;
  8009. filters = 0x49494949;
  8010. } else if (!strcmp(model,"640x480")) {
  8011. height = 480;
  8012. width = 640;
  8013. load_raw = &CLASS eight_bit_load_raw;
  8014. gamma_curve (0.45, 4.5, 1, 255);
  8015. } else if (!strcmp(model,"F-080C")) {
  8016. height = 768;
  8017. width = 1024;
  8018. load_raw = &CLASS eight_bit_load_raw;
  8019. } else if (!strcmp(model,"F-145C")) {
  8020. height = 1040;
  8021. width = 1392;
  8022. load_raw = &CLASS eight_bit_load_raw;
  8023. } else if (!strcmp(model,"F-201C")) {
  8024. height = 1200;
  8025. width = 1600;
  8026. load_raw = &CLASS eight_bit_load_raw;
  8027. } else if (!strcmp(model,"F-510C")) {
  8028. height = 1958;
  8029. width = 2588;
  8030. load_raw = fsize < 7500000 ?
  8031. &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
  8032. data_offset = fsize - width*height*(fsize >> 22);
  8033. maximum = 0xfff0;
  8034. } else if (!strcmp(model,"F-810C")) {
  8035. height = 2469;
  8036. width = 3272;
  8037. load_raw = &CLASS unpacked_load_raw;
  8038. maximum = 0xfff0;
  8039. } else if (!strcmp(model,"XCD-SX910CR")) {
  8040. height = 1024;
  8041. width = 1375;
  8042. raw_width = 1376;
  8043. filters = 0x49494949;
  8044. maximum = 0x3ff;
  8045. load_raw = fsize < 2000000 ?
  8046. &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
  8047. } else if (!strcmp(model,"2010")) {
  8048. height = 1207;
  8049. width = 1608;
  8050. order = 0x4949;
  8051. filters = 0x16161616;
  8052. data_offset = 3212;
  8053. maximum = 0x3ff;
  8054. load_raw = &CLASS unpacked_load_raw;
  8055. } else if (!strcmp(model,"A782")) {
  8056. height = 3000;
  8057. width = 2208;
  8058. filters = 0x61616161;
  8059. load_raw = fsize < 10000000 ?
  8060. &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
  8061. maximum = 0xffc0;
  8062. } else if (!strcmp(model,"3320AF")) {
  8063. height = 1536;
  8064. raw_width = width = 2048;
  8065. filters = 0x61616161;
  8066. load_raw = &CLASS unpacked_load_raw;
  8067. maximum = 0x3ff;
  8068. fseek (ifp, 0x300000, SEEK_SET);
  8069. if ((order = guess_byte_order(0x10000)) == 0x4d4d) {
  8070. height -= (top_margin = 16);
  8071. width -= (left_margin = 28);
  8072. maximum = 0xf5c0;
  8073. strcpy (make, "ISG");
  8074. model[0] = 0;
  8075. }
  8076. } else if (!strcmp(make,"Hasselblad")) {
  8077. if (load_raw == &CLASS lossless_jpeg_load_raw)
  8078. load_raw = &CLASS hasselblad_load_raw;
  8079. if (raw_width == 7262) {
  8080. height = 5444;
  8081. width = 7248;
  8082. top_margin = 4;
  8083. left_margin = 7;
  8084. filters = 0x61616161;
  8085. } else if (raw_width == 7410) {
  8086. height = 5502;
  8087. width = 7328;
  8088. top_margin = 4;
  8089. left_margin = 41;
  8090. filters = 0x61616161;
  8091. } else if (raw_width == 9044) {
  8092. height = 6716;
  8093. width = 8964;
  8094. top_margin = 8;
  8095. left_margin = 40;
  8096. black += load_flags = 256;
  8097. maximum = 0x8101;
  8098. } else if (raw_width == 4090) {
  8099. strcpy (model, "V96C");
  8100. height -= (top_margin = 6);
  8101. width -= (left_margin = 3) + 7;
  8102. filters = 0x61616161;
  8103. }
  8104. } else if (!strcmp(make,"Sinar")) {
  8105. if (!memcmp(head,"8BPS",4)) {
  8106. fseek (ifp, 14, SEEK_SET);
  8107. height = get4();
  8108. width = get4();
  8109. filters = 0x61616161;
  8110. data_offset = 68;
  8111. }
  8112. if (!load_raw) load_raw = &CLASS unpacked_load_raw;
  8113. maximum = 0x3fff;
  8114. } else if (!strcmp(make,"Leaf")) {
  8115. maximum = 0x3fff;
  8116. fseek (ifp, data_offset, SEEK_SET);
  8117. if (ljpeg_start (&jh, 1) && jh.bits == 15)
  8118. maximum = 0x1fff;
  8119. if (tiff_samples > 1) filters = 0;
  8120. if (tiff_samples > 1 || tile_length < raw_height) {
  8121. load_raw = &CLASS leaf_hdr_load_raw;
  8122. raw_width = tile_width;
  8123. }
  8124. if ((width | height) == 2048) {
  8125. if (tiff_samples == 1) {
  8126. filters = 1;
  8127. strcpy (cdesc, "RBTG");
  8128. strcpy (model, "CatchLight");
  8129. top_margin = 8; left_margin = 18; height = 2032; width = 2016;
  8130. } else {
  8131. strcpy (model, "DCB2");
  8132. top_margin = 10; left_margin = 16; height = 2028; width = 2022;
  8133. }
  8134. } else if (width+height == 3144+2060) {
  8135. if (!model[0]) strcpy (model, "Cantare");
  8136. if (width > height) {
  8137. top_margin = 6; left_margin = 32; height = 2048; width = 3072;
  8138. filters = 0x61616161;
  8139. } else {
  8140. left_margin = 6; top_margin = 32; width = 2048; height = 3072;
  8141. filters = 0x16161616;
  8142. }
  8143. if (!cam_mul[0] || model[0] == 'V') filters = 0;
  8144. else is_raw = tiff_samples;
  8145. } else if (width == 2116) {
  8146. strcpy (model, "Valeo 6");
  8147. height -= 2 * (top_margin = 30);
  8148. width -= 2 * (left_margin = 55);
  8149. filters = 0x49494949;
  8150. } else if (width == 3171) {
  8151. strcpy (model, "Valeo 6");
  8152. height -= 2 * (top_margin = 24);
  8153. width -= 2 * (left_margin = 24);
  8154. filters = 0x16161616;
  8155. }
  8156. } else if (!strcmp(make,"LEICA") || !strcmp(make,"Panasonic")) {
  8157. if ((flen - data_offset) / (raw_width*8/7) == raw_height)
  8158. load_raw = &CLASS panasonic_load_raw;
  8159. if (!load_raw) {
  8160. load_raw = &CLASS unpacked_load_raw;
  8161. load_flags = 4;
  8162. }
  8163. zero_is_bad = 1;
  8164. if ((height += 12) > raw_height) height = raw_height;
  8165. for (i=0; i < sizeof pana / sizeof *pana; i++)
  8166. if (raw_width == pana[i][0] && raw_height == pana[i][1]) {
  8167. left_margin = pana[i][2];
  8168. top_margin = pana[i][3];
  8169. width += pana[i][4];
  8170. height += pana[i][5];
  8171. }
  8172. filters = 0x01010101 * (uchar) "\x94\x61\x49\x16"
  8173. [((filters-1) ^ (left_margin & 1) ^ (top_margin << 1)) & 3];
  8174. } else if (!strcmp(model,"C770UZ")) {
  8175. height = 1718;
  8176. width = 2304;
  8177. filters = 0x16161616;
  8178. load_raw = &CLASS packed_load_raw;
  8179. load_flags = 30;
  8180. } else if (!strcmp(make,"OLYMPUS")) {
  8181. height += height & 1;
  8182. filters = exif_cfa;
  8183. if (width == 4100) width -= 4;
  8184. if (width == 4080) width -= 24;
  8185. if (load_raw == &CLASS unpacked_load_raw)
  8186. load_flags = 4;
  8187. tiff_bps = 12;
  8188. if (!strcmp(model,"E-300") ||
  8189. !strcmp(model,"E-500")) {
  8190. width -= 20;
  8191. if (load_raw == &CLASS unpacked_load_raw) {
  8192. maximum = 0xfc3;
  8193. memset (cblack, 0, sizeof cblack);
  8194. }
  8195. } else if (!strcmp(model,"E-330")) {
  8196. width -= 30;
  8197. if (load_raw == &CLASS unpacked_load_raw)
  8198. maximum = 0xf79;
  8199. } else if (!strcmp(model,"SP550UZ")) {
  8200. thumb_length = flen - (thumb_offset = 0xa39800);
  8201. thumb_height = 480;
  8202. thumb_width = 640;
  8203. } else if (!strcmp(model,"XZ-2")) {
  8204. load_raw = &CLASS packed_load_raw;
  8205. load_flags = 24;
  8206. }
  8207. } else if (!strcmp(model,"N Digital")) {
  8208. height = 2047;
  8209. width = 3072;
  8210. filters = 0x61616161;
  8211. data_offset = 0x1a00;
  8212. load_raw = &CLASS packed_load_raw;
  8213. } else if (!strcmp(model,"DSC-F828")) {
  8214. width = 3288;
  8215. left_margin = 5;
  8216. mask[1][3] = -17;
  8217. data_offset = 862144;
  8218. load_raw = &CLASS sony_load_raw;
  8219. filters = 0x9c9c9c9c;
  8220. colors = 4;
  8221. strcpy (cdesc, "RGBE");
  8222. } else if (!strcmp(model,"DSC-V3")) {
  8223. width = 3109;
  8224. left_margin = 59;
  8225. mask[0][1] = 9;
  8226. data_offset = 787392;
  8227. load_raw = &CLASS sony_load_raw;
  8228. } else if (!strcmp(make,"SONY") && raw_width == 3984) {
  8229. adobe_coeff ("SONY","DSC-R1");
  8230. width = 3925;
  8231. order = 0x4d4d;
  8232. } else if (!strcmp(make,"SONY") && raw_width == 5504) {
  8233. width -= 8;
  8234. } else if (!strcmp(make,"SONY") && raw_width == 6048) {
  8235. width -= 24;
  8236. } else if (!strcmp(model,"DSLR-A100")) {
  8237. if (width == 3880) {
  8238. height--;
  8239. width = ++raw_width;
  8240. } else {
  8241. order = 0x4d4d;
  8242. load_flags = 2;
  8243. }
  8244. filters = 0x61616161;
  8245. } else if (!strcmp(model,"DSLR-A350")) {
  8246. height -= 4;
  8247. } else if (!strcmp(model,"PIXL")) {
  8248. height -= top_margin = 4;
  8249. width -= left_margin = 32;
  8250. gamma_curve (0, 7, 1, 255);
  8251. } else if (!strcmp(model,"C603v")) {
  8252. height = 480;
  8253. width = 640;
  8254. if (fsize < 614400 || find_green (16, 16, 3840, 5120) < 25) goto c603v;
  8255. strcpy (model,"KAI-0340");
  8256. height -= 3;
  8257. data_offset = 3840;
  8258. order = 0x4949;
  8259. load_raw = &CLASS unpacked_load_raw;
  8260. } else if (!strcmp(model,"C603y")) {
  8261. height = 2134;
  8262. width = 2848;
  8263. c603v:
  8264. filters = 0;
  8265. load_raw = &CLASS kodak_yrgb_load_raw;
  8266. gamma_curve (0, 3.875, 1, 255);
  8267. } else if (!strcmp(model,"C603")) {
  8268. raw_height = height = 2152;
  8269. raw_width = width = 2864;
  8270. goto c603;
  8271. } else if (!strcmp(model,"C330")) {
  8272. height = 1744;
  8273. width = 2336;
  8274. raw_height = 1779;
  8275. raw_width = 2338;
  8276. top_margin = 33;
  8277. left_margin = 1;
  8278. c603:
  8279. order = 0x4949;
  8280. if ((data_offset = fsize - raw_height*raw_width)) {
  8281. fseek (ifp, 168, SEEK_SET);
  8282. read_shorts (curve, 256);
  8283. } else gamma_curve (0, 3.875, 1, 255);
  8284. load_raw = &CLASS eight_bit_load_raw;
  8285. } else if (!strncasecmp(model,"EasyShare",9)) {
  8286. data_offset = data_offset < 0x15000 ? 0x15000 : 0x17000;
  8287. load_raw = &CLASS packed_load_raw;
  8288. } else if (!strcasecmp(make,"KODAK")) {
  8289. if (filters == UINT_MAX) filters = 0x61616161;
  8290. if (!strncmp(model,"NC2000",6)) {
  8291. width -= 4;
  8292. left_margin = 2;
  8293. } else if (!strcmp(model,"EOSDCS3B")) {
  8294. width -= 4;
  8295. left_margin = 2;
  8296. } else if (!strcmp(model,"EOSDCS1")) {
  8297. width -= 4;
  8298. left_margin = 2;
  8299. } else if (!strcmp(model,"DCS420")) {
  8300. width -= 4;
  8301. left_margin = 2;
  8302. } else if (!strncmp(model,"DCS460 ",7)) {
  8303. model[6] = 0;
  8304. width -= 4;
  8305. left_margin = 2;
  8306. } else if (!strcmp(model,"DCS460A")) {
  8307. width -= 4;
  8308. left_margin = 2;
  8309. colors = 1;
  8310. filters = 0;
  8311. } else if (!strcmp(model,"DCS660M")) {
  8312. black = 214;
  8313. colors = 1;
  8314. filters = 0;
  8315. } else if (!strcmp(model,"DCS760M")) {
  8316. colors = 1;
  8317. filters = 0;
  8318. }
  8319. if (!strcmp(model+4,"20X"))
  8320. strcpy (cdesc, "MYCY");
  8321. if (strstr(model,"DC25")) {
  8322. strcpy (model, "DC25");
  8323. data_offset = 15424;
  8324. }
  8325. if (!strncmp(model,"DC2",3)) {
  8326. raw_height = height = 242;
  8327. if (flen < 100000) {
  8328. raw_width = 256; width = 249;
  8329. pixel_aspect = (4.0*height) / (3.0*width);
  8330. } else {
  8331. raw_width = 512; width = 501;
  8332. pixel_aspect = (493.0*height) / (373.0*width);
  8333. }
  8334. data_offset += raw_width + 1;
  8335. colors = 4;
  8336. filters = 0x8d8d8d8d;
  8337. simple_coeff(1);
  8338. pre_mul[1] = 1.179;
  8339. pre_mul[2] = 1.209;
  8340. pre_mul[3] = 1.036;
  8341. load_raw = &CLASS eight_bit_load_raw;
  8342. } else if (!strcmp(model,"40")) {
  8343. strcpy (model, "DC40");
  8344. height = 512;
  8345. width = 768;
  8346. data_offset = 1152;
  8347. load_raw = &CLASS kodak_radc_load_raw;
  8348. } else if (strstr(model,"DC50")) {
  8349. strcpy (model, "DC50");
  8350. height = 512;
  8351. width = 768;
  8352. data_offset = 19712;
  8353. load_raw = &CLASS kodak_radc_load_raw;
  8354. } else if (strstr(model,"DC120")) {
  8355. strcpy (model, "DC120");
  8356. height = 976;
  8357. width = 848;
  8358. pixel_aspect = height/0.75/width;
  8359. load_raw = tiff_compress == 7 ?
  8360. &CLASS kodak_jpeg_load_raw : &CLASS kodak_dc120_load_raw;
  8361. } else if (!strcmp(model,"DCS200")) {
  8362. thumb_height = 128;
  8363. thumb_width = 192;
  8364. thumb_offset = 6144;
  8365. thumb_misc = 360;
  8366. write_thumb = &CLASS layer_thumb;
  8367. height = 1024;
  8368. width = 1536;
  8369. data_offset = 79872;
  8370. load_raw = &CLASS eight_bit_load_raw;
  8371. black = 17;
  8372. }
  8373. } else if (!strcmp(model,"Fotoman Pixtura")) {
  8374. height = 512;
  8375. width = 768;
  8376. data_offset = 3632;
  8377. load_raw = &CLASS kodak_radc_load_raw;
  8378. filters = 0x61616161;
  8379. simple_coeff(2);
  8380. } else if (!strncmp(model,"QuickTake",9)) {
  8381. if (head[5]) strcpy (model+10, "200");
  8382. fseek (ifp, 544, SEEK_SET);
  8383. height = get2();
  8384. width = get2();
  8385. data_offset = (get4(),get2()) == 30 ? 738:736;
  8386. if (height > width) {
  8387. SWAP(height,width);
  8388. fseek (ifp, data_offset-6, SEEK_SET);
  8389. flip = ~get2() & 3 ? 5:6;
  8390. }
  8391. filters = 0x61616161;
  8392. } else if (!strcmp(make,"Rollei") && !load_raw) {
  8393. switch (raw_width) {
  8394. case 1316:
  8395. height = 1030;
  8396. width = 1300;
  8397. top_margin = 1;
  8398. left_margin = 6;
  8399. break;
  8400. case 2568:
  8401. height = 1960;
  8402. width = 2560;
  8403. top_margin = 2;
  8404. left_margin = 8;
  8405. }
  8406. filters = 0x16161616;
  8407. load_raw = &CLASS rollei_load_raw;
  8408. } else if (!strcmp(model,"PC-CAM 600")) {
  8409. height = 768;
  8410. data_offset = width = 1024;
  8411. filters = 0x49494949;
  8412. load_raw = &CLASS eight_bit_load_raw;
  8413. } else if (!strcmp(model,"QV-2000UX")) {
  8414. height = 1208;
  8415. width = 1632;
  8416. data_offset = width * 2;
  8417. load_raw = &CLASS eight_bit_load_raw;
  8418. } else if (fsize == 3217760) {
  8419. height = 1546;
  8420. width = 2070;
  8421. raw_width = 2080;
  8422. load_raw = &CLASS eight_bit_load_raw;
  8423. } else if (!strcmp(model,"QV-4000")) {
  8424. height = 1700;
  8425. width = 2260;
  8426. load_raw = &CLASS unpacked_load_raw;
  8427. maximum = 0xffff;
  8428. } else if (!strcmp(model,"QV-5700")) {
  8429. height = 1924;
  8430. width = 2576;
  8431. raw_width = 3232;
  8432. tiff_bps = 10;
  8433. } else if (!strcmp(model,"QV-R41")) {
  8434. height = 1720;
  8435. width = 2312;
  8436. raw_width = 3520;
  8437. left_margin = 2;
  8438. } else if (!strcmp(model,"QV-R51")) {
  8439. height = 1926;
  8440. width = 2580;
  8441. raw_width = 3904;
  8442. } else if (!strcmp(model,"EX-S20")) {
  8443. height = 1208;
  8444. width = 1620;
  8445. raw_width = 2432;
  8446. flip = 3;
  8447. } else if (!strcmp(model,"EX-S100")) {
  8448. height = 1544;
  8449. width = 2058;
  8450. raw_width = 3136;
  8451. } else if (!strcmp(model,"EX-Z50")) {
  8452. height = 1931;
  8453. width = 2570;
  8454. raw_width = 3904;
  8455. } else if (!strcmp(model,"EX-Z500")) {
  8456. height = 1937;
  8457. width = 2577;
  8458. raw_width = 3904;
  8459. filters = 0x16161616;
  8460. } else if (!strcmp(model,"EX-Z55")) {
  8461. height = 1960;
  8462. width = 2570;
  8463. raw_width = 3904;
  8464. } else if (!strcmp(model,"EX-Z60")) {
  8465. height = 2145;
  8466. width = 2833;
  8467. raw_width = 3584;
  8468. filters = 0x16161616;
  8469. tiff_bps = 10;
  8470. } else if (!strcmp(model,"EX-Z75")) {
  8471. height = 2321;
  8472. width = 3089;
  8473. raw_width = 4672;
  8474. maximum = 0xfff;
  8475. } else if (!strcmp(model,"EX-Z750")) {
  8476. height = 2319;
  8477. width = 3087;
  8478. raw_width = 4672;
  8479. maximum = 0xfff;
  8480. } else if (!strcmp(model,"EX-Z850")) {
  8481. height = 2468;
  8482. width = 3279;
  8483. raw_width = 4928;
  8484. maximum = 0xfff;
  8485. } else if (!strcmp(model,"EX-Z8")) {
  8486. height = 2467;
  8487. width = 3281;
  8488. raw_height = 2502;
  8489. raw_width = 4992;
  8490. maximum = 0xfff;
  8491. } else if (fsize == 15499264) { /* EX-Z1050 or EX-Z1080 */
  8492. height = 2752;
  8493. width = 3672;
  8494. raw_width = 5632;
  8495. } else if (!strcmp(model,"EX-ZR100")) {
  8496. height = 3044;
  8497. width = 4072;
  8498. raw_width = 4096;
  8499. load_flags = 80;
  8500. } else if (!strcmp(model,"EX-P505")) {
  8501. height = 1928;
  8502. width = 2568;
  8503. raw_width = 3852;
  8504. maximum = 0xfff;
  8505. } else if (fsize == 9313536) { /* EX-P600 or QV-R61 */
  8506. height = 2142;
  8507. width = 2844;
  8508. raw_width = 4288;
  8509. } else if (!strcmp(model,"EX-P700")) {
  8510. height = 2318;
  8511. width = 3082;
  8512. raw_width = 4672;
  8513. }
  8514. else if (!strcmp(model,"GRAS-50S5C")) {
  8515. height = 2048;
  8516. width = 2440;
  8517. load_raw = &CLASS unpacked_load_raw;
  8518. data_offset = 0;
  8519. filters = 0x49494949;
  8520. order = 0x4949;
  8521. maximum = 0xfffC;
  8522. } else if (!strcmp(model,"BB-500CL")) {
  8523. height = 2058;
  8524. width = 2448;
  8525. load_raw = &CLASS unpacked_load_raw;
  8526. data_offset = 0;
  8527. filters = 0x94949494;
  8528. order = 0x4949;
  8529. maximum = 0x3fff;
  8530. } else if (!strcmp(model,"BB-500GE")) {
  8531. height = 2058;
  8532. width = 2456;
  8533. load_raw = &CLASS unpacked_load_raw;
  8534. data_offset = 0;
  8535. filters = 0x94949494;
  8536. order = 0x4949;
  8537. maximum = 0x3fff;
  8538. } else if (!strcmp(model,"SVS625CL")) {
  8539. height = 2050;
  8540. width = 2448;
  8541. load_raw = &CLASS unpacked_load_raw;
  8542. data_offset = 0;
  8543. filters = 0x94949494;
  8544. order = 0x4949;
  8545. maximum = 0x0fff;
  8546. }
  8547. }
  8548. #line 10147 "dcraw/dcraw.c"
  8549. void CLASS convert_to_rgb()
  8550. {
  8551. #ifndef LIBRAW_LIBRARY_BUILD
  8552. int row, col, c;
  8553. #endif
  8554. int i, j, k;
  8555. #ifndef LIBRAW_LIBRARY_BUILD
  8556. ushort *img;
  8557. float out[3];
  8558. #endif
  8559. float out_cam[3][4];
  8560. double num, inverse[3][3];
  8561. static const double xyzd50_srgb[3][3] =
  8562. { { 0.436083, 0.385083, 0.143055 },
  8563. { 0.222507, 0.716888, 0.060608 },
  8564. { 0.013930, 0.097097, 0.714022 } };
  8565. static const double rgb_rgb[3][3] =
  8566. { { 1,0,0 }, { 0,1,0 }, { 0,0,1 } };
  8567. static const double adobe_rgb[3][3] =
  8568. { { 0.715146, 0.284856, 0.000000 },
  8569. { 0.000000, 1.000000, 0.000000 },
  8570. { 0.000000, 0.041166, 0.958839 } };
  8571. static const double wide_rgb[3][3] =
  8572. { { 0.593087, 0.404710, 0.002206 },
  8573. { 0.095413, 0.843149, 0.061439 },
  8574. { 0.011621, 0.069091, 0.919288 } };
  8575. static const double prophoto_rgb[3][3] =
  8576. { { 0.529317, 0.330092, 0.140588 },
  8577. { 0.098368, 0.873465, 0.028169 },
  8578. { 0.016879, 0.117663, 0.865457 } };
  8579. static const double (*out_rgb[])[3] =
  8580. { rgb_rgb, adobe_rgb, wide_rgb, prophoto_rgb, xyz_rgb };
  8581. static const char *name[] =
  8582. { "sRGB", "Adobe RGB (1998)", "WideGamut D65", "ProPhoto D65", "XYZ" };
  8583. static const unsigned phead[] =
  8584. { 1024, 0, 0x2100000, 0x6d6e7472, 0x52474220, 0x58595a20, 0, 0, 0,
  8585. 0x61637370, 0, 0, 0x6e6f6e65, 0, 0, 0, 0, 0xf6d6, 0x10000, 0xd32d };
  8586. unsigned pbody[] =
  8587. { 10, 0x63707274, 0, 36, /* cprt */
  8588. 0x64657363, 0, 40, /* desc */
  8589. 0x77747074, 0, 20, /* wtpt */
  8590. 0x626b7074, 0, 20, /* bkpt */
  8591. 0x72545243, 0, 14, /* rTRC */
  8592. 0x67545243, 0, 14, /* gTRC */
  8593. 0x62545243, 0, 14, /* bTRC */
  8594. 0x7258595a, 0, 20, /* rXYZ */
  8595. 0x6758595a, 0, 20, /* gXYZ */
  8596. 0x6258595a, 0, 20 }; /* bXYZ */
  8597. static const unsigned pwhite[] = { 0xf351, 0x10000, 0x116cc };
  8598. unsigned pcurve[] = { 0x63757276, 0, 1, 0x1000000 };
  8599. #ifdef LIBRAW_LIBRARY_BUILD
  8600. RUN_CALLBACK(LIBRAW_PROGRESS_CONVERT_RGB,0,2);
  8601. #endif
  8602. gamma_curve (gamm[0], gamm[1], 0, 0);
  8603. memcpy (out_cam, rgb_cam, sizeof out_cam);
  8604. #ifndef LIBRAW_LIBRARY_BUILD
  8605. raw_color |= colors == 1 || document_mode ||
  8606. output_color < 1 || output_color > 5;
  8607. #else
  8608. raw_color |= colors == 1 ||
  8609. output_color < 1 || output_color > 5;
  8610. #endif
  8611. if (!raw_color) {
  8612. oprof = (unsigned *) calloc (phead[0], 1);
  8613. merror (oprof, "convert_to_rgb()");
  8614. memcpy (oprof, phead, sizeof phead);
  8615. if (output_color == 5) oprof[4] = oprof[5];
  8616. oprof[0] = 132 + 12*pbody[0];
  8617. for (i=0; i < pbody[0]; i++) {
  8618. oprof[oprof[0]/4] = i ? (i > 1 ? 0x58595a20 : 0x64657363) : 0x74657874;
  8619. pbody[i*3+2] = oprof[0];
  8620. oprof[0] += (pbody[i*3+3] + 3) & -4;
  8621. }
  8622. memcpy (oprof+32, pbody, sizeof pbody);
  8623. oprof[pbody[5]/4+2] = strlen(name[output_color-1]) + 1;
  8624. memcpy ((char *)oprof+pbody[8]+8, pwhite, sizeof pwhite);
  8625. pcurve[3] = (short)(256/gamm[5]+0.5) << 16;
  8626. for (i=4; i < 7; i++)
  8627. memcpy ((char *)oprof+pbody[i*3+2], pcurve, sizeof pcurve);
  8628. pseudoinverse ((double (*)[3]) out_rgb[output_color-1], inverse, 3);
  8629. for (i=0; i < 3; i++)
  8630. for (j=0; j < 3; j++) {
  8631. for (num = k=0; k < 3; k++)
  8632. num += xyzd50_srgb[i][k] * inverse[j][k];
  8633. oprof[pbody[j*3+23]/4+i+2] = num * 0x10000 + 0.5;
  8634. }
  8635. for (i=0; i < phead[0]/4; i++)
  8636. oprof[i] = htonl(oprof[i]);
  8637. strcpy ((char *)oprof+pbody[2]+8, "auto-generated by dcraw");
  8638. strcpy ((char *)oprof+pbody[5]+12, name[output_color-1]);
  8639. for (i=0; i < 3; i++)
  8640. for (j=0; j < colors; j++)
  8641. for (out_cam[i][j] = k=0; k < 3; k++)
  8642. out_cam[i][j] += out_rgb[output_color-1][i][k] * rgb_cam[k][j];
  8643. }
  8644. #ifdef DCRAW_VERBOSE
  8645. if (verbose)
  8646. fprintf (stderr, raw_color ? _("Building histograms...\n") :
  8647. _("Converting to %s colorspace...\n"), name[output_color-1]);
  8648. #endif
  8649. #ifdef LIBRAW_LIBRARY_BUILD
  8650. convert_to_rgb_loop(out_cam);
  8651. #else
  8652. memset (histogram, 0, sizeof histogram);
  8653. for (img=image[0], row=0; row < height; row++)
  8654. for (col=0; col < width; col++, img+=4) {
  8655. if (!raw_color) {
  8656. out[0] = out[1] = out[2] = 0;
  8657. FORCC {
  8658. out[0] += out_cam[0][c] * img[c];
  8659. out[1] += out_cam[1][c] * img[c];
  8660. out[2] += out_cam[2][c] * img[c];
  8661. }
  8662. FORC3 img[c] = CLIP((int) out[c]);
  8663. }
  8664. else if (document_mode)
  8665. img[0] = img[fcol(row,col)];
  8666. FORCC histogram[c][img[c] >> 3]++;
  8667. }
  8668. #endif
  8669. if (colors == 4 && output_color) colors = 3;
  8670. #ifndef LIBRAW_LIBRARY_BUILD
  8671. if (document_mode && filters) colors = 1;
  8672. #endif
  8673. #ifdef LIBRAW_LIBRARY_BUILD
  8674. RUN_CALLBACK(LIBRAW_PROGRESS_CONVERT_RGB,1,2);
  8675. #endif
  8676. }
  8677. void CLASS fuji_rotate()
  8678. {
  8679. int i, row, col;
  8680. double step;
  8681. float r, c, fr, fc;
  8682. unsigned ur, uc;
  8683. ushort wide, high, (*img)[4], (*pix)[4];
  8684. if (!fuji_width) return;
  8685. #ifdef DCRAW_VERBOSE
  8686. if (verbose)
  8687. fprintf (stderr,_("Rotating image 45 degrees...\n"));
  8688. #endif
  8689. fuji_width = (fuji_width - 1 + shrink) >> shrink;
  8690. step = sqrt(0.5);
  8691. wide = fuji_width / step;
  8692. high = (height - fuji_width) / step;
  8693. img = (ushort (*)[4]) calloc (wide*high, sizeof *img);
  8694. merror (img, "fuji_rotate()");
  8695. #ifdef LIBRAW_LIBRARY_BUILD
  8696. RUN_CALLBACK(LIBRAW_PROGRESS_FUJI_ROTATE,0,2);
  8697. #endif
  8698. for (row=0; row < high; row++)
  8699. for (col=0; col < wide; col++) {
  8700. ur = r = fuji_width + (row-col)*step;
  8701. uc = c = (row+col)*step;
  8702. if (ur > height-2 || uc > width-2) continue;
  8703. fr = r - ur;
  8704. fc = c - uc;
  8705. pix = image + ur*width + uc;
  8706. for (i=0; i < colors; i++)
  8707. img[row*wide+col][i] =
  8708. (pix[ 0][i]*(1-fc) + pix[ 1][i]*fc) * (1-fr) +
  8709. (pix[width][i]*(1-fc) + pix[width+1][i]*fc) * fr;
  8710. }
  8711. free (image);
  8712. width = wide;
  8713. height = high;
  8714. image = img;
  8715. fuji_width = 0;
  8716. #ifdef LIBRAW_LIBRARY_BUILD
  8717. RUN_CALLBACK(LIBRAW_PROGRESS_FUJI_ROTATE,1,2);
  8718. #endif
  8719. }
  8720. void CLASS stretch()
  8721. {
  8722. ushort newdim, (*img)[4], *pix0, *pix1;
  8723. int row, col, c;
  8724. double rc, frac;
  8725. if (pixel_aspect == 1) return;
  8726. #ifdef LIBRAW_LIBRARY_BUILD
  8727. RUN_CALLBACK(LIBRAW_PROGRESS_STRETCH,0,2);
  8728. #endif
  8729. #ifdef DCRAW_VERBOSE
  8730. if (verbose) fprintf (stderr,_("Stretching the image...\n"));
  8731. #endif
  8732. if (pixel_aspect < 1) {
  8733. newdim = height / pixel_aspect + 0.5;
  8734. img = (ushort (*)[4]) calloc (width*newdim, sizeof *img);
  8735. merror (img, "stretch()");
  8736. for (rc=row=0; row < newdim; row++, rc+=pixel_aspect) {
  8737. frac = rc - (c = rc);
  8738. pix0 = pix1 = image[c*width];
  8739. if (c+1 < height) pix1 += width*4;
  8740. for (col=0; col < width; col++, pix0+=4, pix1+=4)
  8741. FORCC img[row*width+col][c] = pix0[c]*(1-frac) + pix1[c]*frac + 0.5;
  8742. }
  8743. height = newdim;
  8744. } else {
  8745. newdim = width * pixel_aspect + 0.5;
  8746. img = (ushort (*)[4]) calloc (height*newdim, sizeof *img);
  8747. merror (img, "stretch()");
  8748. for (rc=col=0; col < newdim; col++, rc+=1/pixel_aspect) {
  8749. frac = rc - (c = rc);
  8750. pix0 = pix1 = image[c];
  8751. if (c+1 < width) pix1 += 4;
  8752. for (row=0; row < height; row++, pix0+=width*4, pix1+=width*4)
  8753. FORCC img[row*newdim+col][c] = pix0[c]*(1-frac) + pix1[c]*frac + 0.5;
  8754. }
  8755. width = newdim;
  8756. }
  8757. free (image);
  8758. image = img;
  8759. #ifdef LIBRAW_LIBRARY_BUILD
  8760. RUN_CALLBACK(LIBRAW_PROGRESS_STRETCH,1,2);
  8761. #endif
  8762. }
  8763. int CLASS flip_index (int row, int col)
  8764. {
  8765. if (flip & 4) SWAP(row,col);
  8766. if (flip & 2) row = iheight - 1 - row;
  8767. if (flip & 1) col = iwidth - 1 - col;
  8768. return row * iwidth + col;
  8769. }
  8770. #line 10403 "dcraw/dcraw.c"
  8771. void CLASS tiff_set (ushort *ntag,
  8772. ushort tag, ushort type, int count, int val)
  8773. {
  8774. struct tiff_tag *tt;
  8775. int c;
  8776. tt = (struct tiff_tag *)(ntag+1) + (*ntag)++;
  8777. tt->tag = tag;
  8778. tt->type = type;
  8779. tt->count = count;
  8780. if (type < 3 && count <= 4)
  8781. FORC(4) tt->val.c[c] = val >> (c << 3);
  8782. else if (type == 3 && count <= 2)
  8783. FORC(2) tt->val.s[c] = val >> (c << 4);
  8784. else tt->val.i = val;
  8785. }
  8786. #define TOFF(ptr) ((char *)(&(ptr)) - (char *)th)
  8787. void CLASS tiff_head (struct tiff_hdr *th, int full)
  8788. {
  8789. int c, psize=0;
  8790. struct tm *t;
  8791. memset (th, 0, sizeof *th);
  8792. th->t_order = htonl(0x4d4d4949) >> 16;
  8793. th->magic = 42;
  8794. th->ifd = 10;
  8795. if (full) {
  8796. tiff_set (&th->ntag, 254, 4, 1, 0);
  8797. tiff_set (&th->ntag, 256, 4, 1, width);
  8798. tiff_set (&th->ntag, 257, 4, 1, height);
  8799. tiff_set (&th->ntag, 258, 3, colors, output_bps);
  8800. if (colors > 2)
  8801. th->tag[th->ntag-1].val.i = TOFF(th->bps);
  8802. FORC4 th->bps[c] = output_bps;
  8803. tiff_set (&th->ntag, 259, 3, 1, 1);
  8804. tiff_set (&th->ntag, 262, 3, 1, 1 + (colors > 1));
  8805. }
  8806. tiff_set (&th->ntag, 270, 2, 512, TOFF(th->t_desc));
  8807. tiff_set (&th->ntag, 271, 2, 64, TOFF(th->t_make));
  8808. tiff_set (&th->ntag, 272, 2, 64, TOFF(th->t_model));
  8809. if (full) {
  8810. if (oprof) psize = ntohl(oprof[0]);
  8811. tiff_set (&th->ntag, 273, 4, 1, sizeof *th + psize);
  8812. tiff_set (&th->ntag, 277, 3, 1, colors);
  8813. tiff_set (&th->ntag, 278, 4, 1, height);
  8814. tiff_set (&th->ntag, 279, 4, 1, height*width*colors*output_bps/8);
  8815. } else
  8816. tiff_set (&th->ntag, 274, 3, 1, "12435867"[flip]-'0');
  8817. tiff_set (&th->ntag, 282, 5, 1, TOFF(th->rat[0]));
  8818. tiff_set (&th->ntag, 283, 5, 1, TOFF(th->rat[2]));
  8819. tiff_set (&th->ntag, 284, 3, 1, 1);
  8820. tiff_set (&th->ntag, 296, 3, 1, 2);
  8821. tiff_set (&th->ntag, 305, 2, 32, TOFF(th->soft));
  8822. tiff_set (&th->ntag, 306, 2, 20, TOFF(th->date));
  8823. tiff_set (&th->ntag, 315, 2, 64, TOFF(th->t_artist));
  8824. tiff_set (&th->ntag, 34665, 4, 1, TOFF(th->nexif));
  8825. if (psize) tiff_set (&th->ntag, 34675, 7, psize, sizeof *th);
  8826. tiff_set (&th->nexif, 33434, 5, 1, TOFF(th->rat[4]));
  8827. tiff_set (&th->nexif, 33437, 5, 1, TOFF(th->rat[6]));
  8828. tiff_set (&th->nexif, 34855, 3, 1, iso_speed);
  8829. tiff_set (&th->nexif, 37386, 5, 1, TOFF(th->rat[8]));
  8830. if (gpsdata[1]) {
  8831. tiff_set (&th->ntag, 34853, 4, 1, TOFF(th->ngps));
  8832. tiff_set (&th->ngps, 0, 1, 4, 0x202);
  8833. tiff_set (&th->ngps, 1, 2, 2, gpsdata[29]);
  8834. tiff_set (&th->ngps, 2, 5, 3, TOFF(th->gps[0]));
  8835. tiff_set (&th->ngps, 3, 2, 2, gpsdata[30]);
  8836. tiff_set (&th->ngps, 4, 5, 3, TOFF(th->gps[6]));
  8837. tiff_set (&th->ngps, 5, 1, 1, gpsdata[31]);
  8838. tiff_set (&th->ngps, 6, 5, 1, TOFF(th->gps[18]));
  8839. tiff_set (&th->ngps, 7, 5, 3, TOFF(th->gps[12]));
  8840. tiff_set (&th->ngps, 18, 2, 12, TOFF(th->gps[20]));
  8841. tiff_set (&th->ngps, 29, 2, 12, TOFF(th->gps[23]));
  8842. memcpy (th->gps, gpsdata, sizeof th->gps);
  8843. }
  8844. th->rat[0] = th->rat[2] = 300;
  8845. th->rat[1] = th->rat[3] = 1;
  8846. FORC(6) th->rat[4+c] = 1000000;
  8847. th->rat[4] *= shutter;
  8848. th->rat[6] *= aperture;
  8849. th->rat[8] *= focal_len;
  8850. strncpy (th->t_desc, desc, 512);
  8851. strncpy (th->t_make, make, 64);
  8852. strncpy (th->t_model, model, 64);
  8853. strcpy (th->soft, "dcraw v"DCRAW_VERSION);
  8854. t = localtime (&timestamp);
  8855. sprintf (th->date, "%04d:%02d:%02d %02d:%02d:%02d",
  8856. t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
  8857. strncpy (th->t_artist, artist, 64);
  8858. }
  8859. #ifdef LIBRAW_LIBRARY_BUILD
  8860. void CLASS jpeg_thumb_writer (FILE *tfp,char *t_humb,int t_humb_length)
  8861. {
  8862. ushort exif[5];
  8863. struct tiff_hdr th;
  8864. fputc (0xff, tfp);
  8865. fputc (0xd8, tfp);
  8866. if (strcmp (t_humb+6, "Exif")) {
  8867. memcpy (exif, "\xff\xe1 Exif\0\0", 10);
  8868. exif[1] = htons (8 + sizeof th);
  8869. fwrite (exif, 1, sizeof exif, tfp);
  8870. tiff_head (&th, 0);
  8871. fwrite (&th, 1, sizeof th, tfp);
  8872. }
  8873. fwrite (t_humb+2, 1, t_humb_length-2, tfp);
  8874. }
  8875. void CLASS jpeg_thumb()
  8876. {
  8877. char *thumb;
  8878. thumb = (char *) malloc (thumb_length);
  8879. merror (thumb, "jpeg_thumb()");
  8880. fread (thumb, 1, thumb_length, ifp);
  8881. jpeg_thumb_writer(ofp,thumb,thumb_length);
  8882. free (thumb);
  8883. }
  8884. #else
  8885. void CLASS jpeg_thumb()
  8886. {
  8887. char *thumb;
  8888. ushort exif[5];
  8889. struct tiff_hdr th;
  8890. thumb = (char *) malloc (thumb_length);
  8891. merror (thumb, "jpeg_thumb()");
  8892. fread (thumb, 1, thumb_length, ifp);
  8893. fputc (0xff, ofp);
  8894. fputc (0xd8, ofp);
  8895. if (strcmp (thumb+6, "Exif")) {
  8896. memcpy (exif, "\xff\xe1 Exif\0\0", 10);
  8897. exif[1] = htons (8 + sizeof th);
  8898. fwrite (exif, 1, sizeof exif, ofp);
  8899. tiff_head (&th, 0);
  8900. fwrite (&th, 1, sizeof th, ofp);
  8901. }
  8902. fwrite (thumb+2, 1, thumb_length-2, ofp);
  8903. free (thumb);
  8904. }
  8905. #endif
  8906. void CLASS write_ppm_tiff()
  8907. {
  8908. struct tiff_hdr th;
  8909. uchar *ppm;
  8910. ushort *ppm2;
  8911. int c, row, col, soff, rstep, cstep;
  8912. int perc, val, total, t_white=0x2000;
  8913. perc = width * height * 0.01; /* 99th percentile white level */
  8914. if (fuji_width) perc /= 2;
  8915. if (!((highlight & ~2) || no_auto_bright))
  8916. for (t_white=c=0; c < colors; c++) {
  8917. for (val=0x2000, total=0; --val > 32; )
  8918. if ((total += histogram[c][val]) > perc) break;
  8919. if (t_white < val) t_white = val;
  8920. }
  8921. gamma_curve (gamm[0], gamm[1], 2, (t_white << 3)/bright);
  8922. iheight = height;
  8923. iwidth = width;
  8924. if (flip & 4) SWAP(height,width);
  8925. ppm = (uchar *) calloc (width, colors*output_bps/8);
  8926. ppm2 = (ushort *) ppm;
  8927. merror (ppm, "write_ppm_tiff()");
  8928. if (output_tiff) {
  8929. tiff_head (&th, 1);
  8930. fwrite (&th, sizeof th, 1, ofp);
  8931. if (oprof)
  8932. fwrite (oprof, ntohl(oprof[0]), 1, ofp);
  8933. } else if (colors > 3)
  8934. fprintf (ofp,
  8935. "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
  8936. width, height, colors, (1 << output_bps)-1, cdesc);
  8937. else
  8938. fprintf (ofp, "P%d\n%d %d\n%d\n",
  8939. colors/2+5, width, height, (1 << output_bps)-1);
  8940. soff = flip_index (0, 0);
  8941. cstep = flip_index (0, 1) - soff;
  8942. rstep = flip_index (1, 0) - flip_index (0, width);
  8943. for (row=0; row < height; row++, soff += rstep) {
  8944. for (col=0; col < width; col++, soff += cstep)
  8945. if (output_bps == 8)
  8946. FORCC ppm [col*colors+c] = curve[image[soff][c]] >> 8;
  8947. else FORCC ppm2[col*colors+c] = curve[image[soff][c]];
  8948. if (output_bps == 16 && !output_tiff && htons(0x55aa) != 0x55aa)
  8949. swab ((char*)ppm2, (char*)ppm2, width*colors*2);
  8950. fwrite (ppm, colors*output_bps/8, width, ofp);
  8951. }
  8952. free (ppm);
  8953. }