PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 2ms

/Extensions/RawDC/libraw/internal/dcraw_common.cpp

https://bitbucket.org/kensington/opengtl
C++ | 8903 lines | 8445 code | 316 blank | 142 comment | 2748 complexity | e7cf60208f7cd0c722ad5af67d3a9c10 MD5 | raw file
Possible License(s): LGPL-2.0
  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. Look into original file (probably http://cybercom.net/~dcoffin/dcraw/dcraw.c)
  13. for copyright information.
  14. */
  15. #line 264 "dcraw/dcraw.c"
  16. #define CLASS LibRaw::
  17. #include "libraw/libraw_types.h"
  18. #define LIBRAW_LIBRARY_BUILD
  19. #define LIBRAW_IO_REDEFINED
  20. #include "libraw/libraw.h"
  21. #include "internal/defines.h"
  22. #include "internal/var_defines.h"
  23. #line 274 "dcraw/dcraw.c"
  24. #ifndef __GLIBC__
  25. char *my_memmem (char *haystack, size_t haystacklen,
  26. char *needle, size_t needlelen)
  27. {
  28. char *c;
  29. for (c = haystack; c <= haystack + haystacklen - needlelen; c++)
  30. if (!memcmp (c, needle, needlelen))
  31. return c;
  32. return 0;
  33. }
  34. #define memmem my_memmem
  35. #endif
  36. #line 308 "dcraw/dcraw.c"
  37. ushort CLASS sget2 (uchar *s)
  38. {
  39. if (order == 0x4949) /* "II" means little-endian */
  40. return s[0] | s[1] << 8;
  41. else /* "MM" means big-endian */
  42. return s[0] << 8 | s[1];
  43. }
  44. ushort CLASS get2()
  45. {
  46. uchar str[2] = { 0xff,0xff };
  47. fread (str, 1, 2, ifp);
  48. return sget2(str);
  49. }
  50. unsigned CLASS sget4 (uchar *s)
  51. {
  52. if (order == 0x4949)
  53. return s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
  54. else
  55. return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3];
  56. }
  57. #define sget4(s) sget4((uchar *)s)
  58. unsigned CLASS get4()
  59. {
  60. uchar str[4] = { 0xff,0xff,0xff,0xff };
  61. fread (str, 1, 4, ifp);
  62. return sget4(str);
  63. }
  64. unsigned CLASS getint (int type)
  65. {
  66. return type == 3 ? get2() : get4();
  67. }
  68. float CLASS int_to_float (int i)
  69. {
  70. union { int i; float f; } u;
  71. u.i = i;
  72. return u.f;
  73. }
  74. double CLASS getreal (int type)
  75. {
  76. union { char c[8]; double d; } u;
  77. int i, rev;
  78. switch (type) {
  79. case 3: return (unsigned short) get2();
  80. case 4: return (unsigned int) get4();
  81. case 5: u.d = (unsigned int) get4();
  82. return u.d / (unsigned int) get4();
  83. case 8: return (signed short) get2();
  84. case 9: return (signed int) get4();
  85. case 10: u.d = (signed int) get4();
  86. return u.d / (signed int) get4();
  87. case 11: return int_to_float (get4());
  88. case 12:
  89. rev = 7 * ((order == 0x4949) == (ntohs(0x1234) == 0x1234));
  90. for (i=0; i < 8; i++)
  91. u.c[i ^ rev] = fgetc(ifp);
  92. return u.d;
  93. default: return fgetc(ifp);
  94. }
  95. }
  96. void CLASS read_shorts (ushort *pixel, int count)
  97. {
  98. if (fread (pixel, 2, count, ifp) < count) derror();
  99. if ((order == 0x4949) == (ntohs(0x1234) == 0x1234))
  100. swab ((char*)pixel, (char*)pixel, count*2);
  101. }
  102. #line 386 "dcraw/dcraw.c"
  103. void CLASS canon_600_fixed_wb (int temp)
  104. {
  105. static const short mul[4][5] = {
  106. { 667, 358,397,565,452 },
  107. { 731, 390,367,499,517 },
  108. { 1119, 396,348,448,537 },
  109. { 1399, 485,431,508,688 } };
  110. int lo, hi, i;
  111. float frac=0;
  112. for (lo=4; --lo; )
  113. if (*mul[lo] <= temp) break;
  114. for (hi=0; hi < 3; hi++)
  115. if (*mul[hi] >= temp) break;
  116. if (lo != hi)
  117. frac = (float) (temp - *mul[lo]) / (*mul[hi] - *mul[lo]);
  118. for (i=1; i < 5; i++)
  119. pre_mul[i-1] = 1 / (frac * mul[hi][i] + (1-frac) * mul[lo][i]);
  120. #ifdef LIBRAW_LIBRARY_BUILD
  121. color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
  122. #endif
  123. }
  124. /* Return values: 0 = white 1 = near white 2 = not white */
  125. int CLASS canon_600_color (int ratio[2], int mar)
  126. {
  127. int clipped=0, target, miss;
  128. if (flash_used) {
  129. if (ratio[1] < -104)
  130. { ratio[1] = -104; clipped = 1; }
  131. if (ratio[1] > 12)
  132. { ratio[1] = 12; clipped = 1; }
  133. } else {
  134. if (ratio[1] < -264 || ratio[1] > 461) return 2;
  135. if (ratio[1] < -50)
  136. { ratio[1] = -50; clipped = 1; }
  137. if (ratio[1] > 307)
  138. { ratio[1] = 307; clipped = 1; }
  139. }
  140. target = flash_used || ratio[1] < 197
  141. ? -38 - (398 * ratio[1] >> 10)
  142. : -123 + (48 * ratio[1] >> 10);
  143. if (target - mar <= ratio[0] &&
  144. target + 20 >= ratio[0] && !clipped) return 0;
  145. miss = target - ratio[0];
  146. if (abs(miss) >= mar*4) return 2;
  147. if (miss < -20) miss = -20;
  148. if (miss > mar) miss = mar;
  149. ratio[0] = target - miss;
  150. return 1;
  151. }
  152. void CLASS canon_600_auto_wb()
  153. {
  154. int mar, row, col, i, j, st, count[] = { 0,0 };
  155. int test[8], total[2][8], ratio[2][2], stat[2];
  156. memset (&total, 0, sizeof total);
  157. i = canon_ev + 0.5;
  158. if (i < 10) mar = 150;
  159. else if (i > 12) mar = 20;
  160. else mar = 280 - 20 * i;
  161. if (flash_used) mar = 80;
  162. for (row=14; row < height-14; row+=4)
  163. for (col=10; col < width; col+=2) {
  164. for (i=0; i < 8; i++)
  165. test[(i & 4) + FC(row+(i >> 1),col+(i & 1))] =
  166. BAYER(row+(i >> 1),col+(i & 1));
  167. for (i=0; i < 8; i++)
  168. if (test[i] < 150 || test[i] > 1500) goto next;
  169. for (i=0; i < 4; i++)
  170. if (abs(test[i] - test[i+4]) > 50) goto next;
  171. for (i=0; i < 2; i++) {
  172. for (j=0; j < 4; j+=2)
  173. ratio[i][j >> 1] = ((test[i*4+j+1]-test[i*4+j]) << 10) / test[i*4+j];
  174. stat[i] = canon_600_color (ratio[i], mar);
  175. }
  176. if ((st = stat[0] | stat[1]) > 1) goto next;
  177. for (i=0; i < 2; i++)
  178. if (stat[i])
  179. for (j=0; j < 2; j++)
  180. test[i*4+j*2+1] = test[i*4+j*2] * (0x400 + ratio[i][j]) >> 10;
  181. for (i=0; i < 8; i++)
  182. total[st][i] += test[i];
  183. count[st]++;
  184. next: ;
  185. }
  186. if (count[0] | count[1]) {
  187. st = count[0]*200 < count[1];
  188. for (i=0; i < 4; i++)
  189. pre_mul[i] = 1.0 / (total[st][i] + total[st][i+4]);
  190. #ifdef LIBRAW_LIBRARY_BUILD
  191. color_flags.pre_mul_state = LIBRAW_COLORSTATE_CALCULATED;
  192. #endif
  193. }
  194. }
  195. void CLASS canon_600_coeff()
  196. {
  197. static const short table[6][12] = {
  198. { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
  199. { -1203,1715,-1136,1648, 1388,-876,267,245, -1641,2153,3921,-3409 },
  200. { -615,1127,-1563,2075, 1437,-925,509,3, -756,1268,2519,-2007 },
  201. { -190,702,-1886,2398, 2153,-1641,763,-251, -452,964,3040,-2528 },
  202. { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
  203. { -807,1319,-1785,2297, 1388,-876,769,-257, -230,742,2067,-1555 } };
  204. int t=0, i, c;
  205. float mc, yc;
  206. mc = pre_mul[1] / pre_mul[2];
  207. yc = pre_mul[3] / pre_mul[2];
  208. if (mc > 1 && mc <= 1.28 && yc < 0.8789) t=1;
  209. if (mc > 1.28 && mc <= 2) {
  210. if (yc < 0.8789) t=3;
  211. else if (yc <= 2) t=4;
  212. }
  213. if (flash_used) t=5;
  214. for (raw_color = i=0; i < 3; i++)
  215. FORCC rgb_cam[i][c] = table[t][i*4 + c] / 1024.0;
  216. #ifdef LIBRAW_LIBRARY_BUILD
  217. color_flags.rgb_cam_state = LIBRAW_COLORSTATE_CALCULATED;
  218. #endif
  219. }
  220. void CLASS canon_600_load_raw()
  221. {
  222. uchar data[1120], *dp;
  223. ushort pixel[896], *pix;
  224. int irow, row, col, val;
  225. static const short mul[4][2] =
  226. { { 1141,1145 }, { 1128,1109 }, { 1178,1149 }, { 1128,1109 } };
  227. for (irow=row=0; irow < height; irow++) {
  228. if (fread (data, 1, raw_width*5/4, ifp) < raw_width*5/4) derror();
  229. for (dp=data, pix=pixel; dp < data+1120; dp+=10, pix+=8) {
  230. pix[0] = (dp[0] << 2) + (dp[1] >> 6 );
  231. pix[1] = (dp[2] << 2) + (dp[1] >> 4 & 3);
  232. pix[2] = (dp[3] << 2) + (dp[1] >> 2 & 3);
  233. pix[3] = (dp[4] << 2) + (dp[1] & 3);
  234. pix[4] = (dp[5] << 2) + (dp[9] & 3);
  235. pix[5] = (dp[6] << 2) + (dp[9] >> 2 & 3);
  236. pix[6] = (dp[7] << 2) + (dp[9] >> 4 & 3);
  237. pix[7] = (dp[8] << 2) + (dp[9] >> 6 );
  238. }
  239. for (col=0; col < width; col++)
  240. {
  241. BAYER(row,col) = pixel[col];
  242. }
  243. for (col=width; col < raw_width; col++)
  244. {
  245. black += pixel[col];
  246. #ifdef LIBRAW_LIBRARY_BUILD
  247. ushort *dfp = get_masked_pointer(row,col);
  248. if(dfp) *dfp = pixel[col];
  249. #endif
  250. }
  251. if ((row+=2) > height) row = 1;
  252. }
  253. if (raw_width > width)
  254. black = black / ((raw_width - width) * height) - 4;
  255. for (row=0; row < height; row++)
  256. for (col=0; col < width; col++) {
  257. #ifdef LIBRAW_LIBRARY_BUILD
  258. if( filtering_mode & LIBRAW_FILTERING_NOBLACKS)
  259. val = BAYER(row,col);
  260. else
  261. #endif
  262. if ((val = BAYER(row,col) - black) < 0) val = 0;
  263. val = val * mul[row & 3][col & 1] >> 9;
  264. BAYER(row,col) = val;
  265. #ifdef LIBRAW_LIBRARY_BUILD
  266. if((int)channel_maximum[FC(row,col)] < val ) channel_maximum[FC(row,col)] = val;
  267. #endif
  268. }
  269. canon_600_fixed_wb(1311);
  270. canon_600_auto_wb();
  271. canon_600_coeff();
  272. maximum = (0x3ff - black) * 1109 >> 9;
  273. black = 0;
  274. }
  275. void CLASS remove_zeroes()
  276. {
  277. unsigned row, col, tot, n, r, c;
  278. #ifdef LIBRAW_LIBRARY_BUILD
  279. RUN_CALLBACK(LIBRAW_PROGRESS_REMOVE_ZEROES,0,2);
  280. #endif
  281. for (row=0; row < height; row++)
  282. for (col=0; col < width; col++)
  283. if (BAYER(row,col) == 0) {
  284. tot = n = 0;
  285. for (r = row-2; r <= row+2; r++)
  286. for (c = col-2; c <= col+2; c++)
  287. if (r < height && c < width &&
  288. FC(r,c) == FC(row,col) && BAYER(r,c))
  289. tot += (n++,BAYER(r,c));
  290. if (n) BAYER(row,col) = tot/n;
  291. }
  292. #ifdef LIBRAW_LIBRARY_BUILD
  293. RUN_CALLBACK(LIBRAW_PROGRESS_REMOVE_ZEROES,1,2);
  294. #endif
  295. }
  296. int CLASS canon_s2is()
  297. {
  298. unsigned row;
  299. for (row=0; row < 100; row++) {
  300. fseek (ifp, row*3340 + 3284, SEEK_SET);
  301. if (getc(ifp) > 15) return 1;
  302. }
  303. return 0;
  304. }
  305. /*
  306. getbits(-1) initializes the buffer
  307. getbits(n) where 0 <= n <= 25 returns an n-bit integer
  308. */
  309. unsigned CLASS getbithuff (int nbits, ushort *huff)
  310. {
  311. #ifdef LIBRAW_NOTHREADS
  312. static unsigned bitbuf=0;
  313. static int vbits=0, reset=0;
  314. #else
  315. #define bitbuf tls->getbits.bitbuf
  316. #define vbits tls->getbits.vbits
  317. #define reset tls->getbits.reset
  318. #endif
  319. unsigned c;
  320. if (nbits == -1)
  321. return bitbuf = vbits = reset = 0;
  322. if (nbits == 0 || vbits < 0) return 0;
  323. while (!reset && vbits < nbits && (c = fgetc(ifp)) != EOF &&
  324. !(reset = zero_after_ff && c == 0xff && fgetc(ifp))) {
  325. bitbuf = (bitbuf << 8) + (uchar) c;
  326. vbits += 8;
  327. }
  328. c = bitbuf << (32-vbits) >> (32-nbits);
  329. if (huff) {
  330. vbits -= huff[c] >> 8;
  331. c = (uchar) huff[c];
  332. } else
  333. vbits -= nbits;
  334. if (vbits < 0) derror();
  335. return c;
  336. #ifndef LIBRAW_NOTHREADS
  337. #undef bitbuf
  338. #undef vbits
  339. #undef reset
  340. #endif
  341. }
  342. #define getbits(n) getbithuff(n,0)
  343. #define gethuff(h) getbithuff(*h,h+1)
  344. /*
  345. Construct a decode tree according to the specification in *source.
  346. The first 16 bytes specify how many codes should be 1-bit, 2-bit
  347. 3-bit, etc. Bytes after that are the leaf values.
  348. For example, if the source is
  349. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
  350. 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
  351. then the code is
  352. 00 0x04
  353. 010 0x03
  354. 011 0x05
  355. 100 0x06
  356. 101 0x02
  357. 1100 0x07
  358. 1101 0x01
  359. 11100 0x08
  360. 11101 0x09
  361. 11110 0x00
  362. 111110 0x0a
  363. 1111110 0x0b
  364. 1111111 0xff
  365. */
  366. ushort * CLASS make_decoder_ref (const uchar **source)
  367. {
  368. int max, len, h, i, j;
  369. const uchar *count;
  370. ushort *huff;
  371. count = (*source += 16) - 17;
  372. for (max=16; max && !count[max]; max--);
  373. huff = (ushort *) calloc (1 + (1 << max), sizeof *huff);
  374. merror (huff, "make_decoder()");
  375. huff[0] = max;
  376. for (h=len=1; len <= max; len++)
  377. for (i=0; i < count[len]; i++, ++*source)
  378. for (j=0; j < 1 << (max-len); j++)
  379. if (h <= 1 << max)
  380. huff[h++] = len << 8 | **source;
  381. return huff;
  382. }
  383. ushort * CLASS make_decoder (const uchar *source)
  384. {
  385. return make_decoder_ref (&source);
  386. }
  387. void CLASS crw_init_tables (unsigned table, ushort *huff[2])
  388. {
  389. static const uchar first_tree[3][29] = {
  390. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
  391. 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
  392. { 0,2,2,3,1,1,1,1,2,0,0,0,0,0,0,0,
  393. 0x03,0x02,0x04,0x01,0x05,0x00,0x06,0x07,0x09,0x08,0x0a,0x0b,0xff },
  394. { 0,0,6,3,1,1,2,0,0,0,0,0,0,0,0,0,
  395. 0x06,0x05,0x07,0x04,0x08,0x03,0x09,0x02,0x00,0x0a,0x01,0x0b,0xff },
  396. };
  397. static const uchar second_tree[3][180] = {
  398. { 0,2,2,2,1,4,2,1,2,5,1,1,0,0,0,139,
  399. 0x03,0x04,0x02,0x05,0x01,0x06,0x07,0x08,
  400. 0x12,0x13,0x11,0x14,0x09,0x15,0x22,0x00,0x21,0x16,0x0a,0xf0,
  401. 0x23,0x17,0x24,0x31,0x32,0x18,0x19,0x33,0x25,0x41,0x34,0x42,
  402. 0x35,0x51,0x36,0x37,0x38,0x29,0x79,0x26,0x1a,0x39,0x56,0x57,
  403. 0x28,0x27,0x52,0x55,0x58,0x43,0x76,0x59,0x77,0x54,0x61,0xf9,
  404. 0x71,0x78,0x75,0x96,0x97,0x49,0xb7,0x53,0xd7,0x74,0xb6,0x98,
  405. 0x47,0x48,0x95,0x69,0x99,0x91,0xfa,0xb8,0x68,0xb5,0xb9,0xd6,
  406. 0xf7,0xd8,0x67,0x46,0x45,0x94,0x89,0xf8,0x81,0xd5,0xf6,0xb4,
  407. 0x88,0xb1,0x2a,0x44,0x72,0xd9,0x87,0x66,0xd4,0xf5,0x3a,0xa7,
  408. 0x73,0xa9,0xa8,0x86,0x62,0xc7,0x65,0xc8,0xc9,0xa1,0xf4,0xd1,
  409. 0xe9,0x5a,0x92,0x85,0xa6,0xe7,0x93,0xe8,0xc1,0xc6,0x7a,0x64,
  410. 0xe1,0x4a,0x6a,0xe6,0xb3,0xf1,0xd3,0xa5,0x8a,0xb2,0x9a,0xba,
  411. 0x84,0xa4,0x63,0xe5,0xc5,0xf3,0xd2,0xc4,0x82,0xaa,0xda,0xe4,
  412. 0xf2,0xca,0x83,0xa3,0xa2,0xc3,0xea,0xc2,0xe2,0xe3,0xff,0xff },
  413. { 0,2,2,1,4,1,4,1,3,3,1,0,0,0,0,140,
  414. 0x02,0x03,0x01,0x04,0x05,0x12,0x11,0x06,
  415. 0x13,0x07,0x08,0x14,0x22,0x09,0x21,0x00,0x23,0x15,0x31,0x32,
  416. 0x0a,0x16,0xf0,0x24,0x33,0x41,0x42,0x19,0x17,0x25,0x18,0x51,
  417. 0x34,0x43,0x52,0x29,0x35,0x61,0x39,0x71,0x62,0x36,0x53,0x26,
  418. 0x38,0x1a,0x37,0x81,0x27,0x91,0x79,0x55,0x45,0x28,0x72,0x59,
  419. 0xa1,0xb1,0x44,0x69,0x54,0x58,0xd1,0xfa,0x57,0xe1,0xf1,0xb9,
  420. 0x49,0x47,0x63,0x6a,0xf9,0x56,0x46,0xa8,0x2a,0x4a,0x78,0x99,
  421. 0x3a,0x75,0x74,0x86,0x65,0xc1,0x76,0xb6,0x96,0xd6,0x89,0x85,
  422. 0xc9,0xf5,0x95,0xb4,0xc7,0xf7,0x8a,0x97,0xb8,0x73,0xb7,0xd8,
  423. 0xd9,0x87,0xa7,0x7a,0x48,0x82,0x84,0xea,0xf4,0xa6,0xc5,0x5a,
  424. 0x94,0xa4,0xc6,0x92,0xc3,0x68,0xb5,0xc8,0xe4,0xe5,0xe6,0xe9,
  425. 0xa2,0xa3,0xe3,0xc2,0x66,0x67,0x93,0xaa,0xd4,0xd5,0xe7,0xf8,
  426. 0x88,0x9a,0xd7,0x77,0xc4,0x64,0xe2,0x98,0xa5,0xca,0xda,0xe8,
  427. 0xf3,0xf6,0xa9,0xb2,0xb3,0xf2,0xd2,0x83,0xba,0xd3,0xff,0xff },
  428. { 0,0,6,2,1,3,3,2,5,1,2,2,8,10,0,117,
  429. 0x04,0x05,0x03,0x06,0x02,0x07,0x01,0x08,
  430. 0x09,0x12,0x13,0x14,0x11,0x15,0x0a,0x16,0x17,0xf0,0x00,0x22,
  431. 0x21,0x18,0x23,0x19,0x24,0x32,0x31,0x25,0x33,0x38,0x37,0x34,
  432. 0x35,0x36,0x39,0x79,0x57,0x58,0x59,0x28,0x56,0x78,0x27,0x41,
  433. 0x29,0x77,0x26,0x42,0x76,0x99,0x1a,0x55,0x98,0x97,0xf9,0x48,
  434. 0x54,0x96,0x89,0x47,0xb7,0x49,0xfa,0x75,0x68,0xb6,0x67,0x69,
  435. 0xb9,0xb8,0xd8,0x52,0xd7,0x88,0xb5,0x74,0x51,0x46,0xd9,0xf8,
  436. 0x3a,0xd6,0x87,0x45,0x7a,0x95,0xd5,0xf6,0x86,0xb4,0xa9,0x94,
  437. 0x53,0x2a,0xa8,0x43,0xf5,0xf7,0xd4,0x66,0xa7,0x5a,0x44,0x8a,
  438. 0xc9,0xe8,0xc8,0xe7,0x9a,0x6a,0x73,0x4a,0x61,0xc7,0xf4,0xc6,
  439. 0x65,0xe9,0x72,0xe6,0x71,0x91,0x93,0xa6,0xda,0x92,0x85,0x62,
  440. 0xf3,0xc5,0xb2,0xa4,0x84,0xba,0x64,0xa5,0xb3,0xd2,0x81,0xe5,
  441. 0xd3,0xaa,0xc4,0xca,0xf2,0xb1,0xe4,0xd1,0x83,0x63,0xea,0xc3,
  442. 0xe2,0x82,0xf1,0xa3,0xc2,0xa1,0xc1,0xe3,0xa2,0xe1,0xff,0xff }
  443. };
  444. if (table > 2) table = 2;
  445. huff[0] = make_decoder ( first_tree[table]);
  446. huff[1] = make_decoder (second_tree[table]);
  447. }
  448. /*
  449. Return 0 if the image starts with compressed data,
  450. 1 if it starts with uncompressed low-order bits.
  451. In Canon compressed data, 0xff is always followed by 0x00.
  452. */
  453. int CLASS canon_has_lowbits()
  454. {
  455. uchar test[0x4000];
  456. int ret=1;
  457. unsigned i;
  458. fseek (ifp, 0, SEEK_SET);
  459. fread (test, 1, sizeof test, ifp);
  460. for (i=540; i < sizeof test - 1; i++)
  461. if (test[i] == 0xff) {
  462. if (test[i+1]) return 1;
  463. ret=0;
  464. }
  465. return ret;
  466. }
  467. void CLASS canon_compressed_load_raw()
  468. {
  469. ushort *pixel, *prow, *huff[2];
  470. int nblocks, lowbits, i, c, row, r, col, save, val;
  471. unsigned irow, icol;
  472. int block, diffbuf[64], leaf, len, diff, carry=0, pnum=0, base[2];
  473. crw_init_tables (tiff_compress, huff);
  474. pixel = (ushort *) calloc (raw_width*8, sizeof *pixel);
  475. merror (pixel, "canon_compressed_load_raw()");
  476. lowbits = canon_has_lowbits();
  477. if (!lowbits) maximum = 0x3ff;
  478. fseek (ifp, 540 + lowbits*raw_height*raw_width/4, SEEK_SET);
  479. zero_after_ff = 1;
  480. getbits(-1);
  481. for (row=0; row < raw_height; row+=8) {
  482. nblocks = MIN (8, raw_height-row) * raw_width >> 6;
  483. for (block=0; block < nblocks; block++) {
  484. memset (diffbuf, 0, sizeof diffbuf);
  485. for (i=0; i < 64; i++ ) {
  486. leaf = gethuff(huff[i > 0]);
  487. if (leaf == 0 && i) break;
  488. if (leaf == 0xff) continue;
  489. i += leaf >> 4;
  490. len = leaf & 15;
  491. if (len == 0) continue;
  492. diff = getbits(len);
  493. if ((diff & (1 << (len-1))) == 0)
  494. diff -= (1 << len) - 1;
  495. if (i < 64) diffbuf[i] = diff;
  496. }
  497. diffbuf[0] += carry;
  498. carry = diffbuf[0];
  499. for (i=0; i < 64; i++ ) {
  500. if (pnum++ % raw_width == 0)
  501. base[0] = base[1] = 512;
  502. if ((pixel[(block << 6) + i] = base[i & 1] += diffbuf[i]) >> 10)
  503. derror();
  504. }
  505. }
  506. if (lowbits) {
  507. save = ftell(ifp);
  508. fseek (ifp, 26 + row*raw_width/4, SEEK_SET);
  509. for (prow=pixel, i=0; i < raw_width*2; i++) {
  510. c = fgetc(ifp);
  511. for (r=0; r < 8; r+=2, prow++) {
  512. val = (*prow << 2) + ((c >> r) & 3);
  513. if (raw_width == 2672 && val < 512) val += 2;
  514. *prow = val;
  515. }
  516. }
  517. fseek (ifp, save, SEEK_SET);
  518. }
  519. for (r=0; r < 8; r++) {
  520. irow = row - top_margin + r;
  521. #ifndef LIBRAW_LIBRARY_BUILD
  522. if (irow >= height) continue;
  523. #endif
  524. for (col=0; col < raw_width; col++) {
  525. #ifdef LIBRAW_LIBRARY_BUILD
  526. ushort *dfp = get_masked_pointer(row+r,col);
  527. if(dfp) *dfp = pixel[r*raw_width+col];
  528. if (irow >= height) continue; // skip for top/bottom rows
  529. #endif
  530. icol = col - left_margin;
  531. c = FC(irow,icol);
  532. if (icol < width)
  533. {
  534. #ifdef LIBRAW_LIBRARY_BUILD
  535. ushort val = pixel[r*raw_width+col];
  536. if(channel_maximum[c] < val) channel_maximum[c]=val;
  537. #endif
  538. BAYER(irow,icol) = pixel[r*raw_width+col];
  539. }
  540. else if (col > 1 && (unsigned) (col-left_margin+2) > width+3)
  541. cblack[c] += (cblack[4+c]++,pixel[r*raw_width+col]);
  542. }
  543. }
  544. }
  545. free (pixel);
  546. FORC(2) free (huff[c]);
  547. FORC4 if (cblack[4+c]) cblack[c] /= cblack[4+c];
  548. }
  549. #line 875 "dcraw/dcraw.c"
  550. int CLASS ljpeg_start (struct jhead *jh, int info_only)
  551. {
  552. int c, tag, len;
  553. uchar data[0x10000];
  554. const uchar *dp;
  555. memset (jh, 0, sizeof *jh);
  556. jh->restart = INT_MAX;
  557. fread (data, 2, 1, ifp);
  558. if (data[1] != 0xd8) return 0;
  559. do {
  560. fread (data, 2, 2, ifp);
  561. tag = data[0] << 8 | data[1];
  562. len = (data[2] << 8 | data[3]) - 2;
  563. if (tag <= 0xff00) return 0;
  564. fread (data, 1, len, ifp);
  565. switch (tag) {
  566. case 0xffc3:
  567. jh->sraw = ((data[7] >> 4) * (data[7] & 15) - 1) & 3;
  568. case 0xffc0:
  569. jh->bits = data[0];
  570. jh->high = data[1] << 8 | data[2];
  571. jh->wide = data[3] << 8 | data[4];
  572. jh->clrs = data[5] + jh->sraw;
  573. if (len == 9 && !dng_version) getc(ifp);
  574. break;
  575. case 0xffc4:
  576. if (info_only) break;
  577. for (dp = data; dp < data+len && (c = *dp++) < 4; )
  578. jh->free[c] = jh->huff[c] = make_decoder_ref (&dp);
  579. break;
  580. case 0xffda:
  581. jh->psv = data[1+data[0]*2];
  582. jh->bits -= data[3+data[0]*2] & 15;
  583. break;
  584. case 0xffdd:
  585. jh->restart = data[0] << 8 | data[1];
  586. }
  587. } while (tag != 0xffda);
  588. if (info_only) return 1;
  589. FORC(5) if (!jh->huff[c+1]) jh->huff[c+1] = jh->huff[c];
  590. if (jh->sraw) {
  591. FORC(4) jh->huff[2+c] = jh->huff[1];
  592. FORC(jh->sraw) jh->huff[1+c] = jh->huff[0];
  593. }
  594. jh->row = (ushort *) calloc (jh->wide*jh->clrs, 4);
  595. merror (jh->row, "ljpeg_start()");
  596. return zero_after_ff = 1;
  597. }
  598. void CLASS ljpeg_end (struct jhead *jh)
  599. {
  600. int c;
  601. FORC4 if (jh->free[c]) free (jh->free[c]);
  602. free (jh->row);
  603. }
  604. int CLASS ljpeg_diff (ushort *huff)
  605. {
  606. int len, diff;
  607. len = gethuff(huff);
  608. if (len == 16 && (!dng_version || dng_version >= 0x1010000))
  609. return -32768;
  610. diff = getbits(len);
  611. if ((diff & (1 << (len-1))) == 0)
  612. diff -= (1 << len) - 1;
  613. return diff;
  614. }
  615. ushort * CLASS ljpeg_row (int jrow, struct jhead *jh)
  616. {
  617. int col, c, diff, pred, spred=0;
  618. ushort mark=0, *row[3];
  619. if (jrow * jh->wide % jh->restart == 0) {
  620. FORC(6) jh->vpred[c] = 1 << (jh->bits-1);
  621. if (jrow) {
  622. fseek (ifp, -2, SEEK_CUR);
  623. do mark = (mark << 8) + (c = fgetc(ifp));
  624. while (c != EOF && mark >> 4 != 0xffd);
  625. }
  626. getbits(-1);
  627. }
  628. FORC3 row[c] = jh->row + jh->wide*jh->clrs*((jrow+c) & 1);
  629. for (col=0; col < jh->wide; col++)
  630. FORC(jh->clrs) {
  631. diff = ljpeg_diff (jh->huff[c]);
  632. if (jh->sraw && c <= jh->sraw && (col | c))
  633. pred = spred;
  634. else if (col) pred = row[0][-jh->clrs];
  635. else pred = (jh->vpred[c] += diff) - diff;
  636. if (jrow && col) switch (jh->psv) {
  637. case 1: break;
  638. case 2: pred = row[1][0]; break;
  639. case 3: pred = row[1][-jh->clrs]; break;
  640. case 4: pred = pred + row[1][0] - row[1][-jh->clrs]; break;
  641. case 5: pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1); break;
  642. case 6: pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1); break;
  643. case 7: pred = (pred + row[1][0]) >> 1; break;
  644. default: pred = 0;
  645. }
  646. if ((**row = pred + diff) >> jh->bits) derror();
  647. if (c <= jh->sraw) spred = **row;
  648. row[0]++; row[1]++;
  649. }
  650. return row[2];
  651. }
  652. void CLASS lossless_jpeg_load_raw()
  653. {
  654. int jwide, jrow, jcol, val, jidx, c, i, j, row=0, col=0;
  655. struct jhead jh;
  656. int min=INT_MAX;
  657. ushort *rp;
  658. if (!ljpeg_start (&jh, 0)) return;
  659. jwide = jh.wide * jh.clrs;
  660. for (jrow=0; jrow < jh.high; jrow++) {
  661. rp = ljpeg_row (jrow, &jh);
  662. for (jcol=0; jcol < jwide; jcol++) {
  663. val = *rp++;
  664. if (jh.bits <= 12)
  665. #ifdef LIBRAW_LIBRARY_BUILD
  666. if( !(filtering_mode & LIBRAW_FILTERING_NORAWCURVE))
  667. #endif
  668. val = curve[val & 0xfff];
  669. if (cr2_slice[0]) {
  670. jidx = jrow*jwide + jcol;
  671. i = jidx / (cr2_slice[1]*jh.high);
  672. if ((j = i >= cr2_slice[0]))
  673. i = cr2_slice[0];
  674. jidx -= i * (cr2_slice[1]*jh.high);
  675. row = jidx / cr2_slice[1+j];
  676. col = jidx % cr2_slice[1+j] + i*cr2_slice[1];
  677. }
  678. if (raw_width == 3984 && (col -= 2) < 0)
  679. col += (row--,raw_width);
  680. #ifdef LIBRAW_LIBRARY_BUILD
  681. ushort *dfp = get_masked_pointer(row,col);
  682. if(dfp) *dfp = val;
  683. #endif
  684. if ((unsigned) (row-top_margin) < height) {
  685. c = FC(row-top_margin,col-left_margin);
  686. if ((unsigned) (col-left_margin) < width) {
  687. #ifdef LIBRAW_LIBRARY_BUILD
  688. if(channel_maximum[c] < val) channel_maximum[c] = val;
  689. #endif
  690. BAYER(row-top_margin,col-left_margin) = val;
  691. if (min > val) min = val;
  692. } else if (col > 1 && (unsigned) (col-left_margin+2) > width+3)
  693. cblack[c] += (cblack[4+c]++,val);
  694. }
  695. if (++col >= raw_width)
  696. col = (row++,0);
  697. }
  698. }
  699. ljpeg_end (&jh);
  700. FORC4 if (cblack[4+c]) cblack[c] /= cblack[4+c];
  701. if (!strcasecmp(make,"KODAK"))
  702. black = min;
  703. }
  704. void CLASS canon_sraw_load_raw()
  705. {
  706. struct jhead jh;
  707. short *rp=0, (*ip)[4];
  708. int jwide, slice, scol, ecol, row, col, jrow=0, jcol=0, pix[3], c;
  709. int v[3]={0,0,0}, ver, hue;
  710. char *cp;
  711. if (!ljpeg_start (&jh, 0)) return;
  712. jwide = (jh.wide >>= 1) * jh.clrs;
  713. for (ecol=slice=0; slice <= cr2_slice[0]; slice++) {
  714. scol = ecol;
  715. ecol += cr2_slice[1] * 2 / jh.clrs;
  716. if (!cr2_slice[0] || ecol > raw_width-1) ecol = raw_width & -2;
  717. for (row=0; row < height; row += (jh.clrs >> 1) - 1) {
  718. ip = (short (*)[4]) image + row*width;
  719. for (col=scol; col < ecol; col+=2, jcol+=jh.clrs) {
  720. if ((jcol %= jwide) == 0)
  721. rp = (short *) ljpeg_row (jrow++, &jh);
  722. if (col >= width) continue;
  723. FORC (jh.clrs-2)
  724. ip[col + (c >> 1)*width + (c & 1)][0] = rp[jcol+c];
  725. ip[col][1] = rp[jcol+jh.clrs-2] - 16384;
  726. ip[col][2] = rp[jcol+jh.clrs-1] - 16384;
  727. }
  728. }
  729. }
  730. for (cp=model2; *cp && !isdigit(*cp); cp++);
  731. sscanf (cp, "%d.%d.%d", v, v+1, v+2);
  732. ver = (v[0]*1000 + v[1])*1000 + v[2];
  733. hue = (jh.sraw+1) << 2;
  734. if (unique_id == 0x80000218 && ver > 1000006 && ver < 3000000)
  735. hue = jh.sraw << 1;
  736. ip = (short (*)[4]) image;
  737. rp = ip[0];
  738. for (row=0; row < height; row++, ip+=width) {
  739. if (row & (jh.sraw >> 1))
  740. for (col=0; col < width; col+=2)
  741. for (c=1; c < 3; c++)
  742. if (row == height-1)
  743. ip[col][c] = ip[col-width][c];
  744. else ip[col][c] = (ip[col-width][c] + ip[col+width][c] + 1) >> 1;
  745. for (col=1; col < width; col+=2)
  746. for (c=1; c < 3; c++)
  747. if (col == width-1)
  748. ip[col][c] = ip[col-1][c];
  749. else ip[col][c] = (ip[col-1][c] + ip[col+1][c] + 1) >> 1;
  750. }
  751. for ( ; rp < ip[0]; rp+=4) {
  752. if (unique_id < 0x80000218) {
  753. pix[0] = rp[0] + rp[2] - 512;
  754. pix[2] = rp[0] + rp[1] - 512;
  755. pix[1] = rp[0] + ((-778*rp[1] - (rp[2] << 11)) >> 12) - 512;
  756. } else {
  757. rp[1] = (rp[1] << 2) + hue;
  758. rp[2] = (rp[2] << 2) + hue;
  759. pix[0] = rp[0] + (( 200*rp[1] + 22929*rp[2]) >> 14);
  760. pix[1] = rp[0] + ((-5640*rp[1] - 11751*rp[2]) >> 14);
  761. pix[2] = rp[0] + ((29040*rp[1] - 101*rp[2]) >> 14);
  762. }
  763. FORC3 {
  764. #ifdef LIBRAW_LIBRARY_BUILD
  765. ushort val = CLIP(pix[c] * sraw_mul[c] >> 10);
  766. rp[c] = val;
  767. if(channel_maximum[c] < val) channel_maximum[c] = val;
  768. #else
  769. rp[c] = CLIP(pix[c] * sraw_mul[c] >> 10);
  770. #endif
  771. }
  772. }
  773. ljpeg_end (&jh);
  774. maximum = 0x3fff;
  775. }
  776. void CLASS adobe_copy_pixel (int row, int col, ushort **rp)
  777. {
  778. unsigned r, c;
  779. r = row -= top_margin;
  780. c = col -= left_margin;
  781. if (is_raw == 2 && shot_select) (*rp)++;
  782. if (filters) {
  783. #ifndef LIBRAW_LIBRARY_BUILD
  784. if (fuji_width) {
  785. r = row + fuji_width - 1 - (col >> 1);
  786. c = row + ((col+1) >> 1);
  787. }
  788. #endif
  789. #ifdef LIBRAW_LIBRARY_BUILD
  790. ushort val = **rp;
  791. if(!(filtering_mode & LIBRAW_FILTERING_NORAWCURVE))
  792. val = **rp < 0x1000 ? curve[**rp] : **rp;
  793. if (r < height && c < width)
  794. {
  795. ushort color = COLOR(r,c);
  796. image[((row) >> shrink)*iwidth + ((col) >> shrink)][color] = val;
  797. if(channel_maximum[color] < val) channel_maximum[color] = val;
  798. }
  799. else
  800. {
  801. ushort *dfp = get_masked_pointer(row+top_margin,col+left_margin);
  802. if(dfp) *dfp = val;
  803. }
  804. #else
  805. if (r < height && c < width)
  806. BAYER(r,c) = **rp < 0x1000 ? curve[**rp] : **rp;
  807. #endif
  808. *rp += is_raw;
  809. } else {
  810. if (r < height && c < width)
  811. FORC(tiff_samples)
  812. image[row*width+col][c] = (*rp)[c] < 0x1000 ? curve[(*rp)[c]]:(*rp)[c];
  813. *rp += tiff_samples;
  814. }
  815. if (is_raw == 2 && shot_select) (*rp)--;
  816. }
  817. void CLASS adobe_dng_load_raw_lj()
  818. {
  819. unsigned save, trow=0, tcol=0, jwide, jrow, jcol, row, col;
  820. struct jhead jh;
  821. ushort *rp;
  822. while (trow < raw_height) {
  823. save = ftell(ifp);
  824. if (tile_length < INT_MAX)
  825. fseek (ifp, get4(), SEEK_SET);
  826. if (!ljpeg_start (&jh, 0)) break;
  827. jwide = jh.wide;
  828. if (filters) jwide *= jh.clrs;
  829. jwide /= is_raw;
  830. for (row=col=jrow=0; jrow < jh.high; jrow++) {
  831. rp = ljpeg_row (jrow, &jh);
  832. for (jcol=0; jcol < jwide; jcol++) {
  833. adobe_copy_pixel (trow+row, tcol+col, &rp);
  834. if (++col >= tile_width || col >= raw_width)
  835. row += 1 + (col = 0);
  836. }
  837. }
  838. fseek (ifp, save+4, SEEK_SET);
  839. if ((tcol += tile_width) >= raw_width)
  840. trow += tile_length + (tcol = 0);
  841. ljpeg_end (&jh);
  842. }
  843. }
  844. void CLASS adobe_dng_load_raw_nc()
  845. {
  846. ushort *pixel, *rp;
  847. int row, col;
  848. pixel = (ushort *) calloc (raw_width * tiff_samples, sizeof *pixel);
  849. merror (pixel, "adobe_dng_load_raw_nc()");
  850. for (row=0; row < raw_height; row++) {
  851. if (tiff_bps == 16)
  852. read_shorts (pixel, raw_width * tiff_samples);
  853. else {
  854. getbits(-1);
  855. for (col=0; col < raw_width * tiff_samples; col++)
  856. pixel[col] = getbits(tiff_bps);
  857. }
  858. for (rp=pixel, col=0; col < raw_width; col++)
  859. adobe_copy_pixel (row, col, &rp);
  860. }
  861. free (pixel);
  862. }
  863. void CLASS pentax_load_raw()
  864. {
  865. ushort bit[2][13], huff[4097];
  866. int row, col, diff, c, i;
  867. ushort vpred[2][2] = {{0,0},{0,0}}, hpred[2];
  868. fseek (ifp, meta_offset, SEEK_SET);
  869. FORC(13) bit[0][c] = get2();
  870. FORC(13) bit[1][c] = fgetc(ifp);
  871. FORC(13)
  872. for (i=bit[0][c]; i <= ((bit[0][c]+(4096 >> bit[1][c])-1) & 4095); )
  873. huff[++i] = bit[1][c] << 8 | c;
  874. huff[0] = 12;
  875. fseek (ifp, data_offset, SEEK_SET);
  876. getbits(-1);
  877. for (row=0; row < raw_height; row++)
  878. {
  879. #ifndef LIBRAW_LIBRARY_BUILD
  880. if(row >= height) break;
  881. #endif
  882. for (col=0; col < raw_width; col++) {
  883. diff = ljpeg_diff (huff);
  884. if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
  885. else hpred[col & 1] += diff;
  886. if ((unsigned) (row-top_margin) < height && col < width)
  887. {
  888. #ifdef LIBRAW_LIBRARY_BUILD
  889. ushort color = FC(row-top_margin,col);
  890. if(channel_maximum[color] < hpred[col & 1]) channel_maximum[color] = hpred[col & 1];
  891. #endif
  892. BAYER(row-top_margin,col) = hpred[col & 1];
  893. }
  894. #ifdef LIBRAW_LIBRARY_BUILD
  895. else
  896. {
  897. ushort *dfp = get_masked_pointer(row,col);
  898. if(dfp) *dfp = hpred[col & 1];
  899. }
  900. if (col < width && row < height)
  901. #endif
  902. if (hpred[col & 1] >> 12) derror();
  903. }
  904. }
  905. }
  906. void CLASS nikon_compressed_load_raw()
  907. {
  908. static const uchar nikon_tree[][32] = {
  909. { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy */
  910. 5,4,3,6,2,7,1,0,8,9,11,10,12 },
  911. { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy after split */
  912. 0x39,0x5a,0x38,0x27,0x16,5,4,3,2,1,0,11,12,12 },
  913. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0, /* 12-bit lossless */
  914. 5,4,6,3,7,2,8,1,9,0,10,11,12 },
  915. { 0,1,4,3,1,1,1,1,1,2,0,0,0,0,0,0, /* 14-bit lossy */
  916. 5,6,4,7,8,3,9,2,1,0,10,11,12,13,14 },
  917. { 0,1,5,1,1,1,1,1,1,1,2,0,0,0,0,0, /* 14-bit lossy after split */
  918. 8,0x5c,0x4b,0x3a,0x29,7,6,5,4,3,2,1,0,13,14 },
  919. { 0,1,4,2,2,3,1,2,0,0,0,0,0,0,0,0, /* 14-bit lossless */
  920. 7,6,8,5,9,4,10,3,11,12,2,0,1,13,14 } };
  921. ushort *huff, ver0, ver1, vpred[2][2], hpred[2], csize;
  922. int i, min, max, step=0, tree=0, split=0, row, col, len, shl, diff;
  923. fseek (ifp, meta_offset, SEEK_SET);
  924. ver0 = fgetc(ifp);
  925. ver1 = fgetc(ifp);
  926. if (ver0 == 0x49 || ver1 == 0x58)
  927. fseek (ifp, 2110, SEEK_CUR);
  928. if (ver0 == 0x46) tree = 2;
  929. if (tiff_bps == 14) tree += 3;
  930. read_shorts (vpred[0], 4);
  931. max = 1 << tiff_bps & 0x7fff;
  932. if ((csize = get2()) > 1)
  933. step = max / (csize-1);
  934. if (ver0 == 0x44 && ver1 == 0x20 && step > 0) {
  935. for (i=0; i < csize; i++)
  936. curve[i*step] = get2();
  937. for (i=0; i < max; i++)
  938. curve[i] = ( curve[i-i%step]*(step-i%step) +
  939. curve[i-i%step+step]*(i%step) ) / step;
  940. #ifdef LIBRAW_LIBRARY_BUILD
  941. color_flags.curve_state = LIBRAW_COLORSTATE_LOADED;
  942. #endif
  943. fseek (ifp, meta_offset+562, SEEK_SET);
  944. split = get2();
  945. } else if (ver0 != 0x46 && csize <= 0x4001)
  946. {
  947. read_shorts (curve, max=csize);
  948. #ifdef LIBRAW_LIBRARY_BUILD
  949. color_flags.curve_state = LIBRAW_COLORSTATE_LOADED;
  950. #endif
  951. }
  952. while (curve[max-2] == curve[max-1]) max--;
  953. huff = make_decoder (nikon_tree[tree]);
  954. fseek (ifp, data_offset, SEEK_SET);
  955. getbits(-1);
  956. for (min=row=0; row < height; row++) {
  957. if (split && row == split) {
  958. free (huff);
  959. huff = make_decoder (nikon_tree[tree+1]);
  960. max += (min = 16) << 1;
  961. }
  962. for (col=0; col < raw_width; col++) {
  963. i = gethuff(huff);
  964. len = i & 15;
  965. shl = i >> 4;
  966. diff = ((getbits(len-shl) << 1) + 1) << shl >> 1;
  967. if ((diff & (1 << (len-1))) == 0)
  968. diff -= (1 << len) - !shl;
  969. if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
  970. else hpred[col & 1] += diff;
  971. if ((ushort)(hpred[col & 1] + min) >= max) derror();
  972. #ifndef LIBRAW_LIBRARY_BUILD
  973. if ((unsigned) (col-left_margin) < width)
  974. BAYER(row,col-left_margin) = curve[LIM((short)hpred[col & 1],0,0x3fff)];
  975. #else
  976. ushort xval = hpred[col & 1];
  977. if(!(filtering_mode & LIBRAW_FILTERING_NORAWCURVE))
  978. xval = curve[LIM((short)xval,0,0x3fff)];
  979. if ((unsigned) (col-left_margin) < width)
  980. {
  981. ushort color = FC(row,col-left_margin);
  982. if(channel_maximum[color] < xval ) channel_maximum[color] = xval;
  983. BAYER(row,col-left_margin) = xval;
  984. }
  985. else
  986. {
  987. ushort *dfp = get_masked_pointer(row,col);
  988. if(dfp) *dfp = xval;
  989. }
  990. #endif
  991. }
  992. }
  993. free (huff);
  994. }
  995. /*
  996. Figure out if a NEF file is compressed. These fancy heuristics
  997. are only needed for the D100, thanks to a bug in some cameras
  998. that tags all images as "compressed".
  999. */
  1000. int CLASS nikon_is_compressed()
  1001. {
  1002. uchar test[256];
  1003. int i;
  1004. fseek (ifp, data_offset, SEEK_SET);
  1005. fread (test, 1, 256, ifp);
  1006. for (i=15; i < 256; i+=16)
  1007. if (test[i]) return 1;
  1008. return 0;
  1009. }
  1010. /*
  1011. Returns 1 for a Coolpix 995, 0 for anything else.
  1012. */
  1013. int CLASS nikon_e995()
  1014. {
  1015. int i, histo[256];
  1016. const uchar often[] = { 0x00, 0x55, 0xaa, 0xff };
  1017. memset (histo, 0, sizeof histo);
  1018. fseek (ifp, -2000, SEEK_END);
  1019. for (i=0; i < 2000; i++)
  1020. histo[fgetc(ifp)]++;
  1021. for (i=0; i < 4; i++)
  1022. if (histo[often[i]] < 200)
  1023. return 0;
  1024. return 1;
  1025. }
  1026. /*
  1027. Returns 1 for a Coolpix 2100, 0 for anything else.
  1028. */
  1029. int CLASS nikon_e2100()
  1030. {
  1031. uchar t[12];
  1032. int i;
  1033. fseek (ifp, 0, SEEK_SET);
  1034. for (i=0; i < 1024; i++) {
  1035. fread (t, 1, 12, ifp);
  1036. if (((t[2] & t[4] & t[7] & t[9]) >> 4
  1037. & t[1] & t[6] & t[8] & t[11] & 3) != 3)
  1038. return 0;
  1039. }
  1040. return 1;
  1041. }
  1042. void CLASS nikon_3700()
  1043. {
  1044. int bits, i;
  1045. uchar dp[24];
  1046. static const struct {
  1047. int bits;
  1048. char t_make[12], t_model[15];
  1049. } table[] = {
  1050. { 0x00, "PENTAX", "Optio 33WR" },
  1051. { 0x03, "NIKON", "E3200" },
  1052. { 0x32, "NIKON", "E3700" },
  1053. { 0x33, "OLYMPUS", "C740UZ" } };
  1054. fseek (ifp, 3072, SEEK_SET);
  1055. fread (dp, 1, 24, ifp);
  1056. bits = (dp[8] & 3) << 4 | (dp[20] & 3);
  1057. for (i=0; i < sizeof table / sizeof *table; i++)
  1058. if (bits == table[i].bits) {
  1059. strcpy (make, table[i].t_make );
  1060. strcpy (model, table[i].t_model);
  1061. }
  1062. }
  1063. /*
  1064. Separates a Minolta DiMAGE Z2 from a Nikon E4300.
  1065. */
  1066. int CLASS minolta_z2()
  1067. {
  1068. int i, nz;
  1069. char tail[424];
  1070. fseek (ifp, -sizeof tail, SEEK_END);
  1071. fread (tail, 1, sizeof tail, ifp);
  1072. for (nz=i=0; i < sizeof tail; i++)
  1073. if (tail[i]) nz++;
  1074. return nz > 20;
  1075. }
  1076. /*
  1077. The Fuji Super CCD is just a Bayer grid rotated 45 degrees.
  1078. */
  1079. void CLASS fuji_load_raw()
  1080. {
  1081. ushort *pixel;
  1082. #ifndef LIBRAW_LIBRARY_BUILD
  1083. int wide, row, col, r, c;
  1084. fseek (ifp, (top_margin*raw_width + left_margin) * 2, SEEK_CUR);
  1085. wide = fuji_width << !fuji_layout;
  1086. pixel = (ushort *) calloc (wide, sizeof *pixel);
  1087. merror (pixel, "fuji_load_raw()");
  1088. for (row=0; row < raw_height; row++) {
  1089. read_shorts (pixel, wide);
  1090. fseek (ifp, 2*(raw_width - wide), SEEK_CUR);
  1091. for (col=0; col < wide; col++) {
  1092. if (fuji_layout) {
  1093. r = fuji_width - 1 - col + (row >> 1);
  1094. c = col + ((row+1) >> 1);
  1095. } else {
  1096. r = fuji_width - 1 + row - (col >> 1);
  1097. c = row + ((col+1) >> 1);
  1098. }
  1099. BAYER(r,c) = pixel[col];
  1100. }
  1101. }
  1102. free (pixel);
  1103. #else
  1104. int row,col;
  1105. int wide, r, c;
  1106. pixel = (ushort *) calloc (raw_width, sizeof *pixel);
  1107. merror (pixel, "fuji_load_raw()");
  1108. for (row=0; row < raw_height; row++) {
  1109. read_shorts (pixel, raw_width);
  1110. for (col=0; col < raw_width; col++) {
  1111. if(col >= left_margin && col < width+left_margin
  1112. && row >= top_margin && row < height+top_margin)
  1113. {
  1114. int rrow = row-top_margin;
  1115. int ccol = col-left_margin;
  1116. ushort color = FCF(rrow,ccol);
  1117. image[((rrow) >> shrink)*iwidth + ((ccol) >> shrink)][color] = pixel[col];
  1118. if(channel_maximum[color] < pixel[col] ) channel_maximum[color] = pixel[col];
  1119. }
  1120. else
  1121. {
  1122. ushort *dfp = get_masked_pointer(row,col);
  1123. if(dfp) *dfp = pixel[col];
  1124. }
  1125. }
  1126. }
  1127. free (pixel);
  1128. #endif
  1129. }
  1130. #line 1496 "dcraw/dcraw.c"
  1131. void CLASS ppm_thumb()
  1132. {
  1133. char *thumb;
  1134. thumb_length = thumb_width*thumb_height*3;
  1135. thumb = (char *) malloc (thumb_length);
  1136. merror (thumb, "ppm_thumb()");
  1137. fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
  1138. fread (thumb, 1, thumb_length, ifp);
  1139. fwrite (thumb, 1, thumb_length, ofp);
  1140. free (thumb);
  1141. }
  1142. void CLASS layer_thumb()
  1143. {
  1144. int i, c;
  1145. char *thumb, map[][4] = { "012","102" };
  1146. colors = thumb_misc >> 5 & 7;
  1147. thumb_length = thumb_width*thumb_height;
  1148. thumb = (char *) calloc (colors, thumb_length);
  1149. merror (thumb, "layer_thumb()");
  1150. fprintf (ofp, "P%d\n%d %d\n255\n",
  1151. 5 + (colors >> 1), thumb_width, thumb_height);
  1152. fread (thumb, thumb_length, colors, ifp);
  1153. for (i=0; i < thumb_length; i++)
  1154. FORCC putc (thumb[i+thumb_length*(map[thumb_misc >> 8][c]-'0')], ofp);
  1155. free (thumb);
  1156. }
  1157. void CLASS rollei_thumb()
  1158. {
  1159. unsigned i;
  1160. ushort *thumb;
  1161. thumb_length = thumb_width * thumb_height;
  1162. thumb = (ushort *) calloc (thumb_length, 2);
  1163. merror (thumb, "rollei_thumb()");
  1164. fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
  1165. read_shorts (thumb, thumb_length);
  1166. for (i=0; i < thumb_length; i++) {
  1167. putc (thumb[i] << 3, ofp);
  1168. putc (thumb[i] >> 5 << 2, ofp);
  1169. putc (thumb[i] >> 11 << 3, ofp);
  1170. }
  1171. free (thumb);
  1172. }
  1173. void CLASS rollei_load_raw()
  1174. {
  1175. uchar pixel[10];
  1176. unsigned iten=0, isix, i, buffer=0, row, col, todo[16];
  1177. isix = raw_width * raw_height * 5 / 8;
  1178. while (fread (pixel, 1, 10, ifp) == 10) {
  1179. for (i=0; i < 10; i+=2) {
  1180. todo[i] = iten++;
  1181. todo[i+1] = pixel[i] << 8 | pixel[i+1];
  1182. buffer = pixel[i] >> 2 | buffer << 6;
  1183. }
  1184. for ( ; i < 16; i+=2) {
  1185. todo[i] = isix++;
  1186. todo[i+1] = buffer >> (14-i)*5;
  1187. }
  1188. for (i=0; i < 16; i+=2) {
  1189. row = todo[i] / raw_width - top_margin;
  1190. col = todo[i] % raw_width - left_margin;
  1191. if (row < height && col < width)
  1192. {
  1193. #ifdef LIBRAW_LIBRARY_BUILD
  1194. ushort color = FC(row,col);
  1195. if(channel_maximum[color] < (todo[i+1] & 0x3ff))
  1196. channel_maximum[color] = (todo[i+1] & 0x3ff);
  1197. #endif
  1198. BAYER(row,col) = (todo[i+1] & 0x3ff);
  1199. }
  1200. #ifdef LIBRAW_LIBRARY_BUILD
  1201. else
  1202. {
  1203. ushort *dfp = get_masked_pointer(todo[i] / raw_width,todo[i] % raw_width);
  1204. if(dfp) *dfp = (todo[i+1] & 0x3ff);
  1205. }
  1206. #endif
  1207. }
  1208. }
  1209. maximum = 0x3ff;
  1210. }
  1211. int CLASS bayer (unsigned row, unsigned col)
  1212. {
  1213. return (row < height && col < width) ? BAYER(row,col) : 0;
  1214. }
  1215. void CLASS phase_one_flat_field (int is_float, int nc)
  1216. {
  1217. ushort head[8];
  1218. unsigned wide, y, x, c, rend, cend, row, col;
  1219. float *mrow, num, mult[4];
  1220. read_shorts (head, 8);
  1221. wide = head[2] / head[4];
  1222. mrow = (float *) calloc (nc*wide, sizeof *mrow);
  1223. merror (mrow, "phase_one_flat_field()");
  1224. for (y=0; y < head[3] / head[5]; y++) {
  1225. for (x=0; x < wide; x++)
  1226. for (c=0; c < nc; c+=2) {
  1227. num = is_float ? getreal(11) : get2()/32768.0;
  1228. if (y==0) mrow[c*wide+x] = num;
  1229. else mrow[(c+1)*wide+x] = (num - mrow[c*wide+x]) / head[5];
  1230. }
  1231. if (y==0) continue;
  1232. rend = head[1]-top_margin + y*head[5];
  1233. for (row = rend-head[5]; row < height && row < rend; row++) {
  1234. for (x=1; x < wide; x++) {
  1235. for (c=0; c < nc; c+=2) {
  1236. mult[c] = mrow[c*wide+x-1];
  1237. mult[c+1] = (mrow[c*wide+x] - mult[c]) / head[4];
  1238. }
  1239. cend = head[0]-left_margin + x*head[4];
  1240. for (col = cend-head[4]; col < width && col < cend; col++) {
  1241. c = nc > 2 ? FC(row,col) : 0;
  1242. if (!(c & 1)) {
  1243. c = BAYER(row,col) * mult[c];
  1244. BAYER(row,col) = LIM(c,0,65535);
  1245. }
  1246. for (c=0; c < nc; c+=2)
  1247. mult[c] += mult[c+1];
  1248. }
  1249. }
  1250. for (x=0; x < wide; x++)
  1251. for (c=0; c < nc; c+=2)
  1252. mrow[c*wide+x] += mrow[(c+1)*wide+x];
  1253. }
  1254. }
  1255. free (mrow);
  1256. }
  1257. void CLASS phase_one_correct()
  1258. {
  1259. unsigned entries, tag, data, save, col, row, type;
  1260. int len, i, j, k, cip, val[4], dev[4], sum, max;
  1261. int head[9], diff, mindiff=INT_MAX, off_412=0;
  1262. static const signed char dir[12][2] =
  1263. { {-1,-1}, {-1,1}, {1,-1}, {1,1}, {-2,0}, {0,-2}, {0,2}, {2,0},
  1264. {-2,-2}, {-2,2}, {2,-2}, {2,2} };
  1265. float poly[8], num, cfrac, frac, mult[2], *yval[2];
  1266. ushort *xval[2];
  1267. if (half_size || !meta_length) return;
  1268. #ifdef DCRAW_VERBOSE
  1269. if (verbose) fprintf (stderr,_("Phase One correction...\n"));
  1270. #endif
  1271. fseek (ifp, meta_offset, SEEK_SET);
  1272. order = get2();
  1273. fseek (ifp, 6, SEEK_CUR);
  1274. fseek (ifp, meta_offset+get4(), SEEK_SET);
  1275. entries = get4(); get4();
  1276. while (entries--) {
  1277. tag = get4();
  1278. len = get4();
  1279. data = get4();
  1280. save = ftell(ifp);
  1281. fseek (ifp, meta_offset+data, SEEK_SET);
  1282. if (tag == 0x419) { /* Polynomial curve */
  1283. for (get4(), i=0; i < 8; i++)
  1284. poly[i] = getreal(11);
  1285. poly[3] += (ph1.tag_210 - poly[7]) * poly[6] + 1;
  1286. for (i=0; i < 0x10000; i++) {
  1287. num = (poly[5]*i + poly[3])*i + poly[1];
  1288. curve[i] = LIM(num,0,65535);
  1289. } goto apply; /* apply to right half */
  1290. } else if (tag == 0x41a) { /* Polynomial curve */
  1291. for (i=0; i < 4; i++)
  1292. poly[i] = getreal(11);
  1293. for (i=0; i < 0x10000; i++) {
  1294. for (num=0, j=4; j--; )
  1295. num = num * i + poly[j];
  1296. curve[i] = LIM(num+i,0,65535);
  1297. } apply: /* apply to whole image */
  1298. for (row=0; row < height; row++)
  1299. for (col = (tag & 1)*ph1.split_col; col < width; col++)
  1300. BAYER(row,col) = curve[BAYER(row,col)];
  1301. } else if (tag == 0x400) { /* Sensor defects */
  1302. while ((len -= 8) >= 0) {
  1303. col = get2() - left_margin;
  1304. row = get2() - top_margin;
  1305. type = get2(); get2();
  1306. if (col >= width) continue;
  1307. if (type == 131) /* Bad column */
  1308. for (row=0; row < height; row++)
  1309. if (FC(row,col) == 1) {
  1310. for (sum=i=0; i < 4; i++)
  1311. sum += val[i] = bayer (row+dir[i][0], col+dir[i][1]);
  1312. for (max=i=0; i < 4; i++) {
  1313. dev[i] = abs((val[i] << 2) - sum);
  1314. if (dev[max] < dev[i]) max = i;
  1315. }
  1316. BAYER(row,col) = (sum - val[max])/3.0 + 0.5;
  1317. } else {
  1318. for (sum=0, i=8; i < 12; i++)
  1319. sum += bayer (row+dir[i][0], col+dir[i][1]);
  1320. BAYER(row,col) = 0.5 + sum * 0.0732233 +
  1321. (bayer(row,col-2) + bayer(row,col+2)) * 0.3535534;
  1322. }
  1323. else if (type == 129) { /* Bad pixel */
  1324. if (row >= height) continue;
  1325. j = (FC(row,col) != 1) * 4;
  1326. for (sum=0, i=j; i < j+8; i++)
  1327. sum += bayer (row+dir[i][0], col+dir[i][1]);
  1328. BAYER(row,col) = (sum + 4) >> 3;
  1329. }
  1330. }
  1331. } else if (tag == 0x401) { /* All-color flat fields */
  1332. phase_one_flat_field (1, 2);
  1333. } else if (tag == 0x416 || tag == 0x410) {
  1334. phase_one_flat_field (0, 2);
  1335. } else if (tag == 0x40b) { /* Red+blue flat field */
  1336. phase_one_flat_field (0, 4);
  1337. } else if (tag == 0x412) {
  1338. fseek (ifp, 36, SEEK_CUR);
  1339. diff = abs (get2() - ph1.tag_21a);
  1340. if (mindiff > diff) {
  1341. mindiff = diff;
  1342. off_412 = ftell(ifp) - 38;
  1343. }
  1344. }
  1345. fseek (ifp, save, SEEK_SET);
  1346. }
  1347. if (off_412) {
  1348. fseek (ifp, off_412, SEEK_SET);
  1349. for (i=0; i < 9; i++) head[i] = get4() & 0x7fff;
  1350. yval[0] = (float *) calloc (head[1]*head[3] + head[2]*head[4], 6);
  1351. merror (yval[0], "phase_one_correct()");
  1352. yval[1] = (float *) (yval[0] + head[1]*head[3]);
  1353. xval[0] = (ushort *) (yval[1] + head[2]*head[4]);
  1354. xval[1] = (ushort *) (xval[0] + head[1]*head[3]);
  1355. get2();
  1356. for (i=0; i < 2; i++)
  1357. for (j=0; j < head[i+1]*head[i+3]; j++)
  1358. yval[i][j] = getreal(11);
  1359. for (i=0; i < 2; i++)
  1360. for (j=0; j < head[i+1]*head[i+3]; j++)
  1361. xval[i][j] = get2();
  1362. for (row=0; row < height; row++)
  1363. for (col=0; col < width; col++) {
  1364. cfrac = (float) col * head[3] / raw_width;
  1365. cfrac -= cip = cfrac;
  1366. num = BAYER(row,col) * 0.5;
  1367. for (i=cip; i < cip+2; i++) {
  1368. for (k=j=0; j < head[1]; j++)
  1369. if (num < xval[0][k = head[1]*i+j]) break;
  1370. frac = (j == 0 || j == head[1]) ? 0 :
  1371. (xval[0][k] - num) / (xval[0][k] - xval[0][k-1]);
  1372. mult[i-cip] = yval[0][k-1] * frac + yval[0][k] * (1-frac);
  1373. }
  1374. i = ((mult[0] * (1-cfrac) + mult[1] * cfrac)
  1375. * (row + top_margin) + num) * 2;
  1376. BAYER(row,col) = LIM(i,0,65535);
  1377. }
  1378. free (yval[0]);
  1379. }
  1380. }
  1381. void CLASS phase_one_load_raw()
  1382. {
  1383. int row, col, a, b;
  1384. ushort *pixel, akey, bkey, mask;
  1385. fseek (ifp, ph1.key_off, SEEK_SET);
  1386. akey = get2();
  1387. bkey = get2();
  1388. mask = ph1.format == 1 ? 0x5555:0x1354;
  1389. #ifndef LIBRAW_LIBRARY_BUILD
  1390. fseek (ifp, data_offset + top_margin*raw_width*2, SEEK_SET);
  1391. pixel = (ushort *) calloc (raw_width, sizeof *pixel);
  1392. merror (pixel, "phase_one_load_raw()");
  1393. for (row=0; row < height; row++) {
  1394. read_shorts (pixel, raw_width);
  1395. for (col=0; col < raw_width; col+=2) {
  1396. a = pixel[col+0] ^ akey;
  1397. b = pixel[col+1] ^ bkey;
  1398. pixel[col+0] = (a & mask) | (b & ~mask);
  1399. pixel[col+1] = (b & mask) | (a & ~mask);
  1400. }
  1401. for (col=0; col < width; col++)
  1402. BAYER(row,col) = pixel[col+left_margin];
  1403. }
  1404. free (pixel);
  1405. #else
  1406. fseek (ifp, data_offset, SEEK_SET);
  1407. pixel = (ushort *) calloc (raw_width, sizeof *pixel);
  1408. merror (pixel, "phase_one_load_raw()");
  1409. for (row=0; row < raw_height; row++) {
  1410. read_shorts (pixel, raw_width);
  1411. for (col=0; col < raw_width; col+=2) {
  1412. a = pixel[col+0] ^ akey;
  1413. b = pixel[col+1] ^ bkey;
  1414. pixel[col+0] = (a & mask) | (b & ~mask);
  1415. pixel[col+1] = (b & mask) | (a & ~mask);
  1416. }
  1417. for (col=0; col < raw_width; col++)
  1418. {
  1419. ushort *dfp = get_masked_pointer(row,col);
  1420. if(dfp)
  1421. *dfp = pixel[col];
  1422. else
  1423. {
  1424. ushort color = FC(row-top_margin,col-left_margin);
  1425. if(channel_maximum[color] < pixel[col]) channel_maximum[color] = pixel[col];
  1426. BAYER(row-top_margin,col-left_margin) = pixel[col];
  1427. }
  1428. }
  1429. }
  1430. free (pixel);
  1431. if(!( filtering_mode & LIBRAW_FILTERING_NORAWCURVE) )
  1432. #endif
  1433. phase_one_correct();
  1434. }
  1435. unsigned CLASS ph1_bithuff (int nbits, ushort *huff)
  1436. {
  1437. #ifndef LIBRAW_NOTHREADS
  1438. #define bitbuf tls->ph1_bits.bitbuf
  1439. #define vbits tls->ph1_bits.vbits
  1440. #else
  1441. static UINT64 bitbuf=0;
  1442. static int vbits=0;
  1443. #endif
  1444. unsigned c;
  1445. if (nbits == -1)
  1446. return bitbuf = vbits = 0;
  1447. if (nbits == 0) return 0;
  1448. if (vbits < nbits) {
  1449. bitbuf = bitbuf << 32 | get4();
  1450. vbits += 32;
  1451. }
  1452. c = bitbuf << (64-vbits) >> (64-nbits);
  1453. if (huff) {
  1454. vbits -= huff[c] >> 8;
  1455. return (uchar) huff[c];
  1456. }
  1457. vbits -= nbits;
  1458. return c;
  1459. #ifndef LIBRAW_NOTHREADS
  1460. #undef bitbuf
  1461. #undef vbits
  1462. #endif
  1463. }
  1464. #define ph1_bits(n) ph1_bithuff(n,0)
  1465. #define ph1_huff(h) ph1_bithuff(*h,h+1)
  1466. void CLASS phase_one_load_raw_c()
  1467. {
  1468. static const int length[] = { 8,7,6,9,11,10,5,12,14,13 };
  1469. int *offset, len[2], pred[2], row, col, i, j;
  1470. ushort *pixel;
  1471. short (*t_black)[2];
  1472. pixel = (ushort *) calloc (raw_width + raw_height*4, 2);
  1473. merror (pixel, "phase_one_load_raw_c()");
  1474. offset = (int *) (pixel + raw_width);
  1475. fseek (ifp, strip_offset, SEEK_SET);
  1476. for (row=0; row < raw_height; row++)
  1477. offset[row] = get4();
  1478. t_black = (short (*)[2]) offset + raw_height;
  1479. fseek (ifp, ph1.black_off, SEEK_SET);
  1480. if (ph1.black_off)
  1481. {
  1482. read_shorts ((ushort *) t_black[0], raw_height*2);
  1483. #ifdef LIBRAW_LIBRARY_BUILD
  1484. imgdata.masked_pixels.ph1_black = (ushort (*)[2])calloc(raw_height*2,sizeof(ushort));
  1485. merror (imgdata.masked_pixels.ph1_black, "phase_one_load_raw_c()");
  1486. memmove(imgdata.masked_pixels.ph1_black,(ushort *) t_black[0],raw_height*2*sizeof(ushort));
  1487. #endif
  1488. }
  1489. for (i=0; i < 256; i++)
  1490. curve[i] = i*i / 3.969 + 0.5;
  1491. #ifdef LIBRAW_LIBRARY_BUILD
  1492. color_flags.curve_state = LIBRAW_COLORSTATE_CALCULATED;
  1493. #endif
  1494. for (row=0; row < raw_height; row++) {
  1495. fseek (ifp, data_offset + offset[row], SEEK_SET);
  1496. ph1_bits(-1);
  1497. pred[0] = pred[1] = 0;
  1498. for (col=0; col < raw_width; col++) {
  1499. if (col >= (raw_width & -8))
  1500. len[0] = len[1] = 14;
  1501. else if ((col & 7) == 0)
  1502. for (i=0; i < 2; i++) {
  1503. for (j=0; j < 5 && !ph1_bits(1); j++);
  1504. if (j--) len[i] = length[j*2 + ph1_bits(1)];
  1505. }
  1506. if ((i = len[col & 1]) == 14)
  1507. pixel[col] = pred[col & 1] = ph1_bits(16);
  1508. else
  1509. pixel[col] = pred[col & 1] += ph1_bits(i) + 1 - (1 << (i - 1));
  1510. if (pred[col & 1] >> 16) derror();
  1511. #ifdef LIBRAW_LIBRARY_BUILD
  1512. if(!( filtering_mode & LIBRAW_FILTERING_NORAWCURVE) )
  1513. #endif
  1514. if (ph1.format == 5 && pixel[col] < 256)
  1515. pixel[col] = curve[pixel[col]];
  1516. }
  1517. if ((unsigned) (row-top_margin) < height)
  1518. #ifndef LIBRAW_LIBRARY_BUILD
  1519. for (col=0; col < width; col++) {
  1520. i = (pixel[col+left_margin] << 2)
  1521. - ph1.t_black + t_black[row][col >= ph1.split_col];
  1522. if (i > 0) BAYER(row-top_margin,col) = i;
  1523. }
  1524. #else
  1525. {
  1526. for (col=0; col < raw_width; col++) {
  1527. if( filtering_mode & LIBRAW_FILTERING_NOBLACKS)
  1528. i = (pixel[col] << 2);
  1529. else
  1530. i = (pixel[col] << 2)
  1531. - ph1.t_black + t_black[row][(col /* - left_margin */) >= ph1.split_col]; // changed to fix Coffin's bug!
  1532. if(col >= left_margin && col < width+left_margin)
  1533. {
  1534. if (i > 0){
  1535. ushort color = FC(row-top_margin,col-left_margin);
  1536. BAYER(row-top_margin,col-left_margin) = i;
  1537. if(channel_maximum[color] < i) channel_maximum[color] = i ;
  1538. }
  1539. }
  1540. else
  1541. {
  1542. ushort *dfp = get_masked_pointer(row,col);
  1543. if(i>0 && dfp) *dfp = i;
  1544. }
  1545. }
  1546. }
  1547. else
  1548. {
  1549. // top-bottom fields
  1550. for (col=0; col < raw_width; col++) {
  1551. i = (pixel[col] << 2)
  1552. - ph1.t_black + t_black[row][(col+left_margin) >= ph1.split_col];
  1553. if (i > 0)
  1554. {
  1555. ushort *dfp = get_masked_pointer(row,col);
  1556. if(dfp) *dfp = i;
  1557. }
  1558. }
  1559. }
  1560. #endif
  1561. }
  1562. free (pixel);
  1563. #ifdef LIBRAW_LIBRARY_BUILD
  1564. if(!( filtering_mode & LIBRAW_FILTERING_NORAWCURVE) )
  1565. #endif
  1566. phase_one_correct();
  1567. maximum = 0xfffc - ph1.t_black;
  1568. }
  1569. void CLASS hasselblad_load_raw()
  1570. {
  1571. struct jhead jh;
  1572. int row, col, pred[2], len[2], diff, c;
  1573. if (!ljpeg_start (&jh, 0)) return;
  1574. order = 0x4949;
  1575. ph1_bits(-1);
  1576. for (row=-top_margin; row < raw_height-top_margin; row++) {
  1577. pred[0] = pred[1] = 0x8000;
  1578. for (col=-left_margin; col < raw_width-left_margin; col+=2) {
  1579. FORC(2) len[c] = ph1_huff(jh.huff[0]);
  1580. FORC(2) {
  1581. diff = ph1_bits(len[c]);
  1582. if ((diff & (1 << (len[c]-1))) == 0)
  1583. diff -= (1 << len[c]) - 1;
  1584. if (diff == 65535) diff = -32768;
  1585. pred[c] += diff;
  1586. if (row >= 0 && row < height && (unsigned)(col+c) < width)
  1587. {
  1588. #ifdef LIBRAW_LIBRARY_BUILD
  1589. ushort color = FC(row,col+c);
  1590. if(channel_maximum[color] < pred[c] ) channel_maximum[color]=pred[c];
  1591. #endif
  1592. BAYER(row,col+c) = pred[c];
  1593. }
  1594. #ifdef LIBRAW_LIBRARY_BUILD
  1595. else
  1596. {
  1597. ushort *dfp = get_masked_pointer(row+top_margin,col+left_margin+c);
  1598. if(dfp) *dfp = pred[c];
  1599. }
  1600. #endif
  1601. }
  1602. }
  1603. }
  1604. ljpeg_end (&jh);
  1605. maximum = 0xffff;
  1606. }
  1607. void CLASS leaf_hdr_load_raw()
  1608. {
  1609. ushort *pixel;
  1610. unsigned tile=0, r, c, row, col;
  1611. pixel = (ushort *) calloc (raw_width, sizeof *pixel);
  1612. merror (pixel, "leaf_hdr_load_raw()");
  1613. FORC(tiff_samples)
  1614. for (r=0; r < raw_height; r++) {
  1615. if (r % tile_length == 0) {
  1616. fseek (ifp, data_offset + 4*tile++, SEEK_SET);
  1617. fseek (ifp, get4() + 2*left_margin, SEEK_SET);
  1618. }
  1619. if (filters && c != shot_select) continue;
  1620. read_shorts (pixel, raw_width);
  1621. if ((row = r - top_margin) >= height) continue;
  1622. for (col=0; col < width; col++)
  1623. {
  1624. if (filters) BAYER(row,col) = pixel[col];
  1625. else image[row*width+col][c] = pixel[col];
  1626. #ifdef LIBRAW_LIBRARY_BUILD
  1627. if(filters)
  1628. {
  1629. ushort color = FC(row,col);
  1630. if(channel_maximum[color] < pixel[col]) channel_maximum[color] = pixel[col];
  1631. }
  1632. else
  1633. if(channel_maximum[c] < pixel[col]) channel_maximum[c] = pixel[col];
  1634. #endif
  1635. }
  1636. }
  1637. free (pixel);
  1638. if (!filters) {
  1639. maximum = 0xffff;
  1640. raw_color = 1;
  1641. }
  1642. }
  1643. #line 2034 "dcraw/dcraw.c"
  1644. void CLASS sinar_4shot_load_raw()
  1645. {
  1646. ushort *pixel;
  1647. unsigned shot, row, col, r, c;
  1648. if ((shot = shot_select) || half_size) {
  1649. if (shot) shot--;
  1650. if (shot > 3) shot = 3;
  1651. fseek (ifp, data_offset + shot*4, SEEK_SET);
  1652. fseek (ifp, get4(), SEEK_SET);
  1653. unpacked_load_raw();
  1654. return;
  1655. }
  1656. free (image);
  1657. image = (ushort (*)[4])
  1658. calloc ((iheight=height)*(iwidth=width), sizeof *image);
  1659. merror (image, "sinar_4shot_load_raw()");
  1660. pixel = (ushort *) calloc (raw_width, sizeof *pixel);
  1661. merror (pixel, "sinar_4shot_load_raw()");
  1662. for (shot=0; shot < 4; shot++) {
  1663. fseek (ifp, data_offset + shot*4, SEEK_SET);
  1664. fseek (ifp, get4(), SEEK_SET);
  1665. for (row=0; row < raw_height; row++) {
  1666. read_shorts (pixel, raw_width);
  1667. if ((r = row-top_margin - (shot >> 1 & 1)) >= height) continue;
  1668. for (col=0; col < raw_width; col++) {
  1669. if ((c = col-left_margin - (shot & 1)) >= width) continue;
  1670. image[r*width+c][FC(row,col)] = pixel[col];
  1671. }
  1672. }
  1673. }
  1674. free (pixel);
  1675. shrink = filters = 0;
  1676. }
  1677. void CLASS imacon_full_load_raw()
  1678. {
  1679. int row, col;
  1680. for (row=0; row < height; row++)
  1681. for (col=0; col < width; col++)
  1682. {
  1683. read_shorts (image[row*width+col], 3);
  1684. #ifdef LIBRAW_LIBRARY_BUILD
  1685. for(int c=0; c<3; c++)
  1686. if(image[row*width+col][c] > channel_maximum[c]) channel_maximum[c] = image[row*width+col][c];
  1687. #endif
  1688. }
  1689. }
  1690. void CLASS packed_load_raw()
  1691. {
  1692. int vbits=0, bwide, pwide, rbits, bite, half, irow, row, col, val, i;
  1693. UINT64 bitbuf=0;
  1694. if (raw_width * 8 >= width * tiff_bps) /* Is raw_width in bytes? */
  1695. pwide = (bwide = raw_width) * 8 / tiff_bps;
  1696. else bwide = (pwide = raw_width) * tiff_bps / 8;
  1697. rbits = bwide * 8 - pwide * tiff_bps;
  1698. if (load_flags & 1) bwide = bwide * 16 / 15;
  1699. fseek (ifp, top_margin*bwide, SEEK_CUR);
  1700. bite = 8 + (load_flags & 24);
  1701. half = (height+1) >> 1;
  1702. for (irow=0; irow < height; irow++) {
  1703. row = irow;
  1704. if (load_flags & 2 &&
  1705. (row = irow % half * 2 + irow / half) == 1 &&
  1706. load_flags & 4) {
  1707. if (vbits=0, tiff_compress)
  1708. fseek (ifp, data_offset - (-half*bwide & -2048), SEEK_SET);
  1709. else {
  1710. fseek (ifp, 0, SEEK_END);
  1711. fseek (ifp, ftell(ifp) >> 3 << 2, SEEK_SET);
  1712. }
  1713. }
  1714. for (col=0; col < pwide; col++) {
  1715. for (vbits -= tiff_bps; vbits < 0; vbits += bite) {
  1716. bitbuf <<= bite;
  1717. for (i=0; i < bite; i+=8)
  1718. bitbuf |= (unsigned) (fgetc(ifp) << i);
  1719. }
  1720. val = bitbuf << (64-tiff_bps-vbits) >> (64-tiff_bps);
  1721. i = (col ^ (bite == 24)) - left_margin;
  1722. if ((unsigned) i < width)
  1723. {
  1724. #ifdef LIBRAW_LIBRARY_BUILD
  1725. ushort color = FC(row,i);
  1726. if( channel_maximum[color] < val ) channel_maximum[color] = val;
  1727. BAYER(row,i) = val;
  1728. #else
  1729. BAYER(row,i) = val;
  1730. #endif
  1731. }
  1732. else if (load_flags & 32)
  1733. #ifdef LIBRAW_LIBRARY_BUILD
  1734. {
  1735. ushort *dfp = get_masked_pointer(row,col);
  1736. if(dfp) *dfp = val;
  1737. #endif
  1738. black += val;
  1739. #ifdef LIBRAW_LIBRARY_BUILD
  1740. }
  1741. #endif
  1742. if (load_flags & 1 && (col % 10) == 9 &&
  1743. fgetc(ifp) && col < width+left_margin) derror();
  1744. }
  1745. vbits -= rbits;
  1746. }
  1747. if (load_flags & 32 && pwide > width)
  1748. black /= (pwide - width) * height;
  1749. }
  1750. void CLASS unpacked_load_raw()
  1751. {
  1752. ushort *pixel;
  1753. int row, col, bits=0;
  1754. while (1 << ++bits < maximum);
  1755. #ifndef LIBRAW_LIBRARY_BUILD
  1756. fseek (ifp, (top_margin*raw_width + left_margin) * 2, SEEK_CUR);
  1757. pixel = (ushort *) calloc (width, sizeof *pixel);
  1758. merror (pixel, "unpacked_load_raw()");
  1759. for (row=0; row < height; row++) {
  1760. read_shorts (pixel, width);
  1761. fseek (ifp, 2*(raw_width - width), SEEK_CUR);
  1762. for (col=0; col < width; col++)
  1763. if ((BAYER2(row,col) = pixel[col] >> load_flags) >> bits) derror();
  1764. }
  1765. free (pixel);
  1766. #else
  1767. // fseek (ifp, (top_margin*raw_width + left_margin) * 2, SEEK_CUR);
  1768. pixel = (ushort *) calloc (raw_width, sizeof *pixel);
  1769. merror (pixel, "unpacked_load_raw()");
  1770. for (row=0; row < raw_height; row++) {
  1771. read_shorts (pixel, raw_width);
  1772. //fseek (ifp, 2*(raw_width - width), SEEK_CUR);
  1773. for (col=0; col < raw_width; col++)
  1774. {
  1775. ushort *dfp = get_masked_pointer(row,col);
  1776. if(dfp)
  1777. *dfp = pixel[col] >> load_flags;
  1778. else
  1779. {
  1780. ushort color = FC(row-top_margin,col-left_margin);
  1781. if ((BAYER2(row-top_margin,col-left_margin) = pixel[col] >> load_flags) >> bits) derror();
  1782. if (channel_maximum[color] < pixel[col]>>load_flags)channel_maximum[color]= pixel[col]>>load_flags;
  1783. }
  1784. }
  1785. }
  1786. free (pixel);
  1787. #endif
  1788. }
  1789. void CLASS nokia_load_raw()
  1790. {
  1791. uchar *data, *dp;
  1792. ushort *pixel, *pix;
  1793. int dwide, row, c;
  1794. dwide = raw_width * 5 / 4;
  1795. data = (uchar *) malloc (dwide + raw_width*2);
  1796. merror (data, "nokia_load_raw()");
  1797. pixel = (ushort *) (data + dwide);
  1798. for (row=0; row < raw_height; row++) {
  1799. if (fread (data, 1, dwide, ifp) < dwide) derror();
  1800. for (dp=data, pix=pixel; pix < pixel+raw_width; dp+=5, pix+=4)
  1801. FORC4 pix[c] = (dp[c] << 2) | (dp[4] >> (c << 1) & 3);
  1802. if (row < top_margin)
  1803. #ifdef LIBRAW_LIBRARY_BUILD
  1804. {
  1805. int col;
  1806. for(col=0;col<width;col++)
  1807. {
  1808. ushort *dfp = get_masked_pointer(row,col);
  1809. if(dfp)
  1810. *dfp = pixel[col];
  1811. }
  1812. FORC(width) black += pixel[c];
  1813. }
  1814. #else
  1815. FORC(width) black += pixel[c];
  1816. #endif
  1817. else
  1818. FORC(width){
  1819. #ifdef LIBRAW_LIBRARY_BUILD
  1820. ushort color = FC(row-top_margin,c);
  1821. if(channel_maximum[color] < pixel[c]) channel_maximum[color] = pixel[c];
  1822. #endif
  1823. BAYER(row-top_margin,c) = pixel[c];
  1824. }
  1825. }
  1826. free (data);
  1827. if (top_margin) black /= top_margin * width;
  1828. maximum = 0x3ff;
  1829. }
  1830. unsigned CLASS pana_bits (int nbits)
  1831. {
  1832. #ifndef LIBRAW_NOTHREADS
  1833. #define buf tls->pana_bits.buf
  1834. #define vbits tls->pana_bits.vbits
  1835. #else
  1836. static uchar buf[0x4000];
  1837. static int vbits;
  1838. #endif
  1839. int byte;
  1840. if (!nbits) return vbits=0;
  1841. if (!vbits) {
  1842. fread (buf+load_flags, 1, 0x4000-load_flags, ifp);
  1843. fread (buf, 1, load_flags, ifp);
  1844. }
  1845. vbits = (vbits - nbits) & 0x1ffff;
  1846. byte = vbits >> 3 ^ 0x3ff0;
  1847. return (buf[byte] | buf[byte+1] << 8) >> (vbits & 7) & ~(-1 << nbits);
  1848. #ifndef LIBRAW_NOTHREADS
  1849. #undef buf
  1850. #undef vbits
  1851. #endif
  1852. }
  1853. void CLASS panasonic_load_raw()
  1854. {
  1855. int row, col, i, j, sh=0, pred[2], nonz[2];
  1856. pana_bits(0);
  1857. for (row=0; row < height; row++)
  1858. for (col=0; col < raw_width; col++) {
  1859. if ((i = col % 14) == 0)
  1860. pred[0] = pred[1] = nonz[0] = nonz[1] = 0;
  1861. if (i % 3 == 2) sh = 4 >> (3 - pana_bits(2));
  1862. if (nonz[i & 1]) {
  1863. if ((j = pana_bits(8))) {
  1864. if ((pred[i & 1] -= 0x80 << sh) < 0 || sh == 4)
  1865. pred[i & 1] &= ~(-1 << sh);
  1866. pred[i & 1] += j << sh;
  1867. }
  1868. } else if ((nonz[i & 1] = pana_bits(8)) || i > 11)
  1869. pred[i & 1] = nonz[i & 1] << 4 | pana_bits(4);
  1870. if (col < width)
  1871. {
  1872. #ifdef LIBRAW_LIBRARY_BUILD
  1873. ushort color = FC(row,col);
  1874. #endif
  1875. if ((BAYER(row,col) = pred[col & 1]) > 4098) derror();
  1876. #ifdef LIBRAW_LIBRARY_BUILD
  1877. if( channel_maximum[color] < pred[col &1] ) channel_maximum[color] = pred[col &1];
  1878. #endif
  1879. }
  1880. #ifdef LIBRAW_LIBRARY_BUILD
  1881. if(col>=width)
  1882. {
  1883. ushort *dfp = get_masked_pointer(row,col);
  1884. if(dfp)*dfp = pred[col & 1];
  1885. }
  1886. #endif
  1887. }
  1888. }
  1889. void CLASS olympus_load_raw()
  1890. {
  1891. ushort huff[4096];
  1892. int row, col, nbits, sign, low, high, i, c, w, n, nw;
  1893. int acarry[2][3], *carry, pred, diff;
  1894. huff[n=0] = 0xc0c;
  1895. for (i=12; i--; )
  1896. FORC(2048 >> i) huff[++n] = (i+1) << 8 | i;
  1897. fseek (ifp, 7, SEEK_CUR);
  1898. getbits(-1);
  1899. for (row=0; row < height; row++) {
  1900. memset (acarry, 0, sizeof acarry);
  1901. for (col=0; col < raw_width; col++) {
  1902. carry = acarry[col & 1];
  1903. i = 2 * (carry[2] < 3);
  1904. for (nbits=2+i; (ushort) carry[0] >> (nbits+i); nbits++);
  1905. low = (sign = getbits(3)) & 3;
  1906. sign = sign << 29 >> 31;
  1907. if ((high = getbithuff(12,huff)) == 12)
  1908. high = getbits(16-nbits) >> 1;
  1909. carry[0] = (high << nbits) | getbits(nbits);
  1910. diff = (carry[0] ^ sign) + carry[1];
  1911. carry[1] = (diff*3 + carry[1]) >> 5;
  1912. carry[2] = carry[0] > 16 ? 0 : carry[2]+1;
  1913. if (col >= width) continue;
  1914. if (row < 2 && col < 2) pred = 0;
  1915. else if (row < 2) pred = BAYER(row,col-2);
  1916. else if (col < 2) pred = BAYER(row-2,col);
  1917. else {
  1918. w = BAYER(row,col-2);
  1919. n = BAYER(row-2,col);
  1920. nw = BAYER(row-2,col-2);
  1921. if ((w < nw && nw < n) || (n < nw && nw < w)) {
  1922. if (ABS(w-nw) > 32 || ABS(n-nw) > 32)
  1923. pred = w + n - nw;
  1924. else pred = (w + n) >> 1;
  1925. } else pred = ABS(w-nw) > ABS(n-nw) ? w : n;
  1926. }
  1927. #ifdef LIBRAW_LIBRARY_BUILD
  1928. {
  1929. ushort val = pred + ((diff << 2) | low);
  1930. ushort color = FC(row,col);
  1931. if(val >> 12) derror();
  1932. BAYER(row,col)=val;
  1933. if(channel_maximum[color] < val ) channel_maximum[color] = val;
  1934. }
  1935. #else
  1936. if ((BAYER(row,col) = pred + ((diff << 2) | low)) >> 12) derror();
  1937. #endif
  1938. }
  1939. }
  1940. }
  1941. void CLASS minolta_rd175_load_raw()
  1942. {
  1943. uchar pixel[768];
  1944. unsigned irow, box, row, col;
  1945. for (irow=0; irow < 1481; irow++) {
  1946. if (fread (pixel, 1, 768, ifp) < 768) derror();
  1947. box = irow / 82;
  1948. row = irow % 82 * 12 + ((box < 12) ? box | 1 : (box-12)*2);
  1949. switch (irow) {
  1950. case 1477: case 1479: continue;
  1951. case 1476: row = 984; break;
  1952. case 1480: row = 985; break;
  1953. case 1478: row = 985; box = 1;
  1954. }
  1955. if ((box < 12) && (box & 1)) {
  1956. for (col=0; col < 1533; col++, row ^= 1)
  1957. if (col != 1) BAYER(row,col) = (col+1) & 2 ?
  1958. pixel[col/2-1] + pixel[col/2+1] : pixel[col/2] << 1;
  1959. BAYER(row,1) = pixel[1] << 1;
  1960. BAYER(row,1533) = pixel[765] << 1;
  1961. } else
  1962. for (col=row & 1; col < 1534; col+=2)
  1963. {
  1964. #ifdef LIBRAW_LIBRARY_BUILD
  1965. ushort color = FC(row,col);
  1966. if(channel_maximum[color] < pixel[col/2] << 1)
  1967. channel_maximum[color] = pixel[col/2] << 1;
  1968. #endif
  1969. BAYER(row,col) = pixel[col/2] << 1;
  1970. }
  1971. }
  1972. maximum = 0xff << 1;
  1973. }
  1974. void CLASS quicktake_100_load_raw()
  1975. {
  1976. uchar pixel[484][644];
  1977. static const short gstep[16] =
  1978. { -89,-60,-44,-32,-22,-15,-8,-2,2,8,15,22,32,44,60,89 };
  1979. static const short rstep[6][4] =
  1980. { { -3,-1,1,3 }, { -5,-1,1,5 }, { -8,-2,2,8 },
  1981. { -13,-3,3,13 }, { -19,-4,4,19 }, { -28,-6,6,28 } };
  1982. static const short t_curve[256] =
  1983. { 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,
  1984. 28,29,30,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,53,
  1985. 54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,74,75,76,77,78,
  1986. 79,80,81,82,83,84,86,88,90,92,94,97,99,101,103,105,107,110,112,114,116,
  1987. 118,120,123,125,127,129,131,134,136,138,140,142,144,147,149,151,153,155,
  1988. 158,160,162,164,166,168,171,173,175,177,179,181,184,186,188,190,192,195,
  1989. 197,199,201,203,205,208,210,212,214,216,218,221,223,226,230,235,239,244,
  1990. 248,252,257,261,265,270,274,278,283,287,291,296,300,305,309,313,318,322,
  1991. 326,331,335,339,344,348,352,357,361,365,370,374,379,383,387,392,396,400,
  1992. 405,409,413,418,422,426,431,435,440,444,448,453,457,461,466,470,474,479,
  1993. 483,487,492,496,500,508,519,531,542,553,564,575,587,598,609,620,631,643,
  1994. 654,665,676,687,698,710,721,732,743,754,766,777,788,799,810,822,833,844,
  1995. 855,866,878,889,900,911,922,933,945,956,967,978,989,1001,1012,1023 };
  1996. int rb, row, col, sharp, val=0;
  1997. getbits(-1);
  1998. memset (pixel, 0x80, sizeof pixel);
  1999. for (row=2; row < height+2; row++) {
  2000. for (col=2+(row & 1); col < width+2; col+=2) {
  2001. val = ((pixel[row-1][col-1] + 2*pixel[row-1][col+1] +
  2002. pixel[row][col-2]) >> 2) + gstep[getbits(4)];
  2003. pixel[row][col] = val = LIM(val,0,255);
  2004. if (col < 4)
  2005. pixel[row][col-2] = pixel[row+1][~row & 1] = val;
  2006. if (row == 2)
  2007. pixel[row-1][col+1] = pixel[row-1][col+3] = val;
  2008. }
  2009. pixel[row][col] = val;
  2010. }
  2011. for (rb=0; rb < 2; rb++)
  2012. for (row=2+rb; row < height+2; row+=2)
  2013. for (col=3-(row & 1); col < width+2; col+=2) {
  2014. if (row < 4 || col < 4) sharp = 2;
  2015. else {
  2016. val = ABS(pixel[row-2][col] - pixel[row][col-2])
  2017. + ABS(pixel[row-2][col] - pixel[row-2][col-2])
  2018. + ABS(pixel[row][col-2] - pixel[row-2][col-2]);
  2019. sharp = val < 4 ? 0 : val < 8 ? 1 : val < 16 ? 2 :
  2020. val < 32 ? 3 : val < 48 ? 4 : 5;
  2021. }
  2022. val = ((pixel[row-2][col] + pixel[row][col-2]) >> 1)
  2023. + rstep[sharp][getbits(2)];
  2024. pixel[row][col] = val = LIM(val,0,255);
  2025. if (row < 4) pixel[row-2][col+2] = val;
  2026. if (col < 4) pixel[row+2][col-2] = val;
  2027. }
  2028. for (row=2; row < height+2; row++)
  2029. for (col=3-(row & 1); col < width+2; col+=2) {
  2030. val = ((pixel[row][col-1] + (pixel[row][col] << 2) +
  2031. pixel[row][col+1]) >> 1) - 0x100;
  2032. pixel[row][col] = LIM(val,0,255);
  2033. }
  2034. for (row=0; row < height; row++)
  2035. for (col=0; col < width; col++)
  2036. {
  2037. #ifdef LIBRAW_LIBRARY_BUILD
  2038. ushort color = FC(row,col);
  2039. if(channel_maximum[color] < t_curve[pixel[row+2][col+2]])
  2040. channel_maximum[color] = t_curve[pixel[row+2][col+2]];
  2041. #endif
  2042. BAYER(row,col) = t_curve[pixel[row+2][col+2]];
  2043. }
  2044. maximum = 0x3ff;
  2045. }
  2046. #define radc_token(tree) ((signed char) getbithuff(8,huff[tree]))
  2047. #define FORYX for (y=1; y < 3; y++) for (x=col+1; x >= col; x--)
  2048. #define PREDICTOR (c ? (buf[c][y-1][x] + buf[c][y][x+1]) / 2 \
  2049. : (buf[c][y-1][x+1] + 2*buf[c][y-1][x] + buf[c][y][x+1]) / 4)
  2050. void CLASS kodak_radc_load_raw()
  2051. {
  2052. static const char src[] = {
  2053. 1,1, 2,3, 3,4, 4,2, 5,7, 6,5, 7,6, 7,8,
  2054. 1,0, 2,1, 3,3, 4,4, 5,2, 6,7, 7,6, 8,5, 8,8,
  2055. 2,1, 2,3, 3,0, 3,2, 3,4, 4,6, 5,5, 6,7, 6,8,
  2056. 2,0, 2,1, 2,3, 3,2, 4,4, 5,6, 6,7, 7,5, 7,8,
  2057. 2,1, 2,4, 3,0, 3,2, 3,3, 4,7, 5,5, 6,6, 6,8,
  2058. 2,3, 3,1, 3,2, 3,4, 3,5, 3,6, 4,7, 5,0, 5,8,
  2059. 2,3, 2,6, 3,0, 3,1, 4,4, 4,5, 4,7, 5,2, 5,8,
  2060. 2,4, 2,7, 3,3, 3,6, 4,1, 4,2, 4,5, 5,0, 5,8,
  2061. 2,6, 3,1, 3,3, 3,5, 3,7, 3,8, 4,0, 5,2, 5,4,
  2062. 2,0, 2,1, 3,2, 3,3, 4,4, 4,5, 5,6, 5,7, 4,8,
  2063. 1,0, 2,2, 2,-2,
  2064. 1,-3, 1,3,
  2065. 2,-17, 2,-5, 2,5, 2,17,
  2066. 2,-7, 2,2, 2,9, 2,18,
  2067. 2,-18, 2,-9, 2,-2, 2,7,
  2068. 2,-28, 2,28, 3,-49, 3,-9, 3,9, 4,49, 5,-79, 5,79,
  2069. 2,-1, 2,13, 2,26, 3,39, 4,-16, 5,55, 6,-37, 6,76,
  2070. 2,-26, 2,-13, 2,1, 3,-39, 4,16, 5,-55, 6,-76, 6,37
  2071. };
  2072. ushort huff[19][256];
  2073. int row, col, tree, nreps, rep, step, i, c, s, r, x, y, val;
  2074. short last[3] = { 16,16,16 }, mul[3], buf[3][3][386];
  2075. static const ushort pt[] =
  2076. { 0,0, 1280,1344, 2320,3616, 3328,8000, 4095,16383, 65535,16383 };
  2077. for (i=2; i < 12; i+=2)
  2078. for (c=pt[i-2]; c <= pt[i]; c++)
  2079. curve[c] = (float)
  2080. (c-pt[i-2]) / (pt[i]-pt[i-2]) * (pt[i+1]-pt[i-1]) + pt[i-1] + 0.5;
  2081. for (s=i=0; i < sizeof src; i+=2)
  2082. FORC(256 >> src[i])
  2083. huff[0][s++] = src[i] << 8 | (uchar) src[i+1];
  2084. s = kodak_cbpp == 243 ? 2 : 3;
  2085. FORC(256) huff[18][c] = (8-s) << 8 | c >> s << s | 1 << (s-1);
  2086. getbits(-1);
  2087. for (i=0; i < sizeof(buf)/sizeof(short); i++)
  2088. buf[0][0][i] = 2048;
  2089. for (row=0; row < height; row+=4) {
  2090. FORC3 mul[c] = getbits(6);
  2091. FORC3 {
  2092. val = ((0x1000000/last[c] + 0x7ff) >> 12) * mul[c];
  2093. s = val > 65564 ? 10:12;
  2094. x = ~(-1 << (s-1));
  2095. val <<= 12-s;
  2096. for (i=0; i < sizeof(buf[0])/sizeof(short); i++)
  2097. buf[c][0][i] = (buf[c][0][i] * val + x) >> s;
  2098. last[c] = mul[c];
  2099. for (r=0; r <= !c; r++) {
  2100. buf[c][1][width/2] = buf[c][2][width/2] = mul[c] << 7;
  2101. for (tree=1, col=width/2; col > 0; ) {
  2102. if ((tree = radc_token(tree))) {
  2103. col -= 2;
  2104. if (tree == 8)
  2105. FORYX buf[c][y][x] = (uchar) radc_token(18) * mul[c];
  2106. else
  2107. FORYX buf[c][y][x] = radc_token(tree+10) * 16 + PREDICTOR;
  2108. } else
  2109. do {
  2110. nreps = (col > 2) ? radc_token(9) + 1 : 1;
  2111. for (rep=0; rep < 8 && rep < nreps && col > 0; rep++) {
  2112. col -= 2;
  2113. FORYX buf[c][y][x] = PREDICTOR;
  2114. if (rep & 1) {
  2115. step = radc_token(10) << 4;
  2116. FORYX buf[c][y][x] += step;
  2117. }
  2118. }
  2119. } while (nreps == 9);
  2120. }
  2121. for (y=0; y < 2; y++)
  2122. for (x=0; x < width/2; x++) {
  2123. val = (buf[c][y+1][x] << 4) / mul[c];
  2124. if (val < 0) val = 0;
  2125. if (c) BAYER(row+y*2+c-1,x*2+2-c) = val;
  2126. else BAYER(row+r*2+y,x*2+y) = val;
  2127. }
  2128. memcpy (buf[c][0]+!c, buf[c][2], sizeof buf[c][0]-2*!c);
  2129. }
  2130. }
  2131. for (y=row; y < row+4; y++)
  2132. for (x=0; x < width; x++)
  2133. if ((x+y) & 1) {
  2134. r = x ? x-1 : x+1;
  2135. s = x+1 < width ? x+1 : x-1;
  2136. val = (BAYER(y,x)-2048)*2 + (BAYER(y,r)+BAYER(y,s))/2;
  2137. if (val < 0) val = 0;
  2138. BAYER(y,x) = val;
  2139. }
  2140. }
  2141. for (i=0; i < iheight*iwidth*4; i++)
  2142. #ifdef LIBRAW_LIBRARY_BUILD
  2143. {
  2144. ushort c = i%4;
  2145. image[0][i] = curve[image[0][i]];
  2146. if(channel_maximum[c] < image[0][i]) channel_maximum[c] = image[0][i];
  2147. }
  2148. #else
  2149. image[0][i] = curve[image[0][i]];
  2150. #endif
  2151. maximum = 0x3fff;
  2152. }
  2153. #undef FORYX
  2154. #undef PREDICTOR
  2155. #ifdef NO_JPEG
  2156. void CLASS kodak_jpeg_load_raw() {}
  2157. #else
  2158. METHODDEF(boolean)
  2159. fill_input_buffer (j_decompress_ptr cinfo)
  2160. {
  2161. #ifndef LIBRAW_NOTHREADS
  2162. #define jpeg_buffer tls->jpeg_buffer
  2163. #else
  2164. static uchar jpeg_buffer[4096];
  2165. #endif
  2166. size_t nbytes;
  2167. nbytes = fread (jpeg_buffer, 1, 4096, ifp);
  2168. swab (jpeg_buffer, jpeg_buffer, nbytes);
  2169. cinfo->src->next_input_byte = jpeg_buffer;
  2170. cinfo->src->bytes_in_buffer = nbytes;
  2171. return TRUE;
  2172. #ifndef LIBRAW_NOTHREADS
  2173. #undef jpeg_buffer
  2174. #endif
  2175. }
  2176. void CLASS kodak_jpeg_load_raw()
  2177. {
  2178. struct jpeg_decompress_struct cinfo;
  2179. struct jpeg_error_mgr jerr;
  2180. JSAMPARRAY buf;
  2181. JSAMPLE (*pixel)[3];
  2182. int row, col;
  2183. cinfo.err = jpeg_std_error (&jerr);
  2184. jpeg_create_decompress (&cinfo);
  2185. jpeg_stdio_src (&cinfo, ifp);
  2186. cinfo.src->fill_input_buffer = fill_input_buffer;
  2187. jpeg_read_header (&cinfo, TRUE);
  2188. jpeg_start_decompress (&cinfo);
  2189. if ((cinfo.output_width != width ) ||
  2190. (cinfo.output_height*2 != height ) ||
  2191. (cinfo.output_components != 3 )) {
  2192. #ifdef DCRAW_VERBOSE
  2193. fprintf (stderr,_("%s: incorrect JPEG dimensions\n"), ifname);
  2194. #endif
  2195. jpeg_destroy_decompress (&cinfo);
  2196. #ifdef LIBRAW_LIBRARY_BUILD
  2197. throw LIBRAW_EXCEPTION_DECODE_JPEG;
  2198. #else
  2199. longjmp (failure, 3);
  2200. #endif
  2201. }
  2202. buf = (*cinfo.mem->alloc_sarray)
  2203. ((j_common_ptr) &cinfo, JPOOL_IMAGE, width*3, 1);
  2204. while (cinfo.output_scanline < cinfo.output_height) {
  2205. row = cinfo.output_scanline * 2;
  2206. jpeg_read_scanlines (&cinfo, buf, 1);
  2207. pixel = (JSAMPLE (*)[3]) buf[0];
  2208. for (col=0; col < width; col+=2) {
  2209. BAYER(row+0,col+0) = pixel[col+0][1] << 1;
  2210. BAYER(row+1,col+1) = pixel[col+1][1] << 1;
  2211. BAYER(row+0,col+1) = pixel[col][0] + pixel[col+1][0];
  2212. BAYER(row+1,col+0) = pixel[col][2] + pixel[col+1][2];
  2213. #ifdef LIBRAW_LIBRARY_BUILD
  2214. if(channel_maximum[FC(row+0,col+0)] < pixel[col+0][1] << 1) channel_maximum[FC(row+0,col+0)]=pixel[col+0][1]<<1;
  2215. if(channel_maximum[FC(row+1,col+1)] < pixel[col+1][1] << 1) channel_maximum[FC(row+1,col+1)]=pixel[col+1][1]<<1;
  2216. if(channel_maximum[FC(row+0,col+1)] < pixel[col][0] + pixel[col+1][0])
  2217. channel_maximum[FC(row+0,col+1)] = pixel[col][0] + pixel[col+1][0];
  2218. if(channel_maximum[FC(row+1,col+0)] < pixel[col][2] + pixel[col+1][2])
  2219. channel_maximum[FC(row+1,col+0)] = pixel[col][2] + pixel[col+1][2];
  2220. #endif
  2221. }
  2222. }
  2223. jpeg_finish_decompress (&cinfo);
  2224. jpeg_destroy_decompress (&cinfo);
  2225. maximum = 0xff << 1;
  2226. }
  2227. #endif
  2228. void CLASS kodak_dc120_load_raw()
  2229. {
  2230. static const int mul[4] = { 162, 192, 187, 92 };
  2231. static const int add[4] = { 0, 636, 424, 212 };
  2232. uchar pixel[848];
  2233. int row, shift, col;
  2234. for (row=0; row < height; row++) {
  2235. if (fread (pixel, 1, 848, ifp) < 848) derror();
  2236. shift = row * mul[row & 3] + add[row & 3];
  2237. for (col=0; col < width; col++)
  2238. #ifdef LIBRAW_LIBRARY_BUILD
  2239. {
  2240. ushort val = pixel[(col + shift) % 848];
  2241. ushort color = FC(row,col);
  2242. BAYER(row,col) = val;
  2243. if(channel_maximum[color] < val) channel_maximum[color] = val;
  2244. }
  2245. #else
  2246. BAYER(row,col) = (ushort) pixel[(col + shift) % 848];
  2247. #endif
  2248. }
  2249. maximum = 0xff;
  2250. }
  2251. void CLASS eight_bit_load_raw()
  2252. {
  2253. uchar *pixel;
  2254. unsigned row, col, val, lblack=0;
  2255. pixel = (uchar *) calloc (raw_width, sizeof *pixel);
  2256. merror (pixel, "eight_bit_load_raw()");
  2257. #ifndef LIBRAW_LIBRARY_BUILD
  2258. fseek (ifp, top_margin*raw_width, SEEK_CUR);
  2259. for (row=0; row < height; row++) {
  2260. if (fread (pixel, 1, raw_width, ifp) < raw_width) derror();
  2261. for (col=0; col < raw_width; col++) {
  2262. val = curve[pixel[col]];
  2263. if ((unsigned) (col-left_margin) < width)
  2264. BAYER(row,col-left_margin) = val;
  2265. else lblack += val;
  2266. }
  2267. }
  2268. #else
  2269. for (row=0; row < raw_height; row++) {
  2270. if (fread (pixel, 1, raw_width, ifp) < raw_width) derror();
  2271. for (col=0; col < raw_width; col++) {
  2272. if(filtering_mode & LIBRAW_FILTERING_NORAWCURVE)
  2273. {
  2274. val = pixel[col];
  2275. if(val>maximum) maximum = val;
  2276. }
  2277. else
  2278. val = curve[pixel[col]];
  2279. if((unsigned) (row-top_margin)< height)
  2280. {
  2281. if ((unsigned) (col-left_margin) < width)
  2282. {
  2283. ushort color=FC(row-top_margin,col-left_margin);
  2284. if(channel_maximum[color] < val) channel_maximum[color] = val;
  2285. BAYER(row-top_margin,col-left_margin) = val;
  2286. }
  2287. else
  2288. {
  2289. ushort *dfp = get_masked_pointer(row,col);
  2290. if(dfp) *dfp = val;
  2291. lblack += val;
  2292. }
  2293. }
  2294. else // top/bottom margins
  2295. {
  2296. ushort *dfp = get_masked_pointer(row,col);
  2297. if(dfp) *dfp = val;
  2298. }
  2299. }
  2300. }
  2301. #endif
  2302. free (pixel);
  2303. if (raw_width > width+1)
  2304. black = lblack / ((raw_width - width) * height);
  2305. if (!strncmp(model,"DC2",3))
  2306. black = 0;
  2307. #ifdef LIBRAW_LIBRARY_BUILD
  2308. if(!(filtering_mode & LIBRAW_FILTERING_NORAWCURVE))
  2309. #endif
  2310. maximum = curve[0xff];
  2311. }
  2312. void CLASS kodak_yrgb_load_raw()
  2313. {
  2314. uchar *pixel;
  2315. int row, col, y, cb, cr, rgb[3], c;
  2316. pixel = (uchar *) calloc (raw_width, 3*sizeof *pixel);
  2317. merror (pixel, "kodak_yrgb_load_raw()");
  2318. for (row=0; row < height; row++) {
  2319. if (~row & 1)
  2320. if (fread (pixel, raw_width, 3, ifp) < 3) derror();
  2321. for (col=0; col < raw_width; col++) {
  2322. y = pixel[width*2*(row & 1) + col];
  2323. cb = pixel[width + (col & -2)] - 128;
  2324. cr = pixel[width + (col & -2)+1] - 128;
  2325. rgb[1] = y-((cb + cr + 2) >> 2);
  2326. rgb[2] = rgb[1] + cb;
  2327. rgb[0] = rgb[1] + cr;
  2328. FORC3{
  2329. image[row*width+col][c] = curve[LIM(rgb[c],0,255)];
  2330. #ifdef LIBRAW_LIBRARY_BUILD
  2331. if(channel_maximum[c] < image[row*width+col][c]) channel_maximum[c] = image[row*width+col][c];
  2332. #endif
  2333. }
  2334. }
  2335. }
  2336. free (pixel);
  2337. maximum = curve[0xff];
  2338. }
  2339. void CLASS kodak_262_load_raw()
  2340. {
  2341. static const uchar kodak_tree[2][26] =
  2342. { { 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 },
  2343. { 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 } };
  2344. ushort *huff[2];
  2345. uchar *pixel;
  2346. int *strip, ns, c, row, col, chess, pi=0, pi1, pi2, pred, val;
  2347. FORC(2) huff[c] = make_decoder (kodak_tree[c]);
  2348. ns = (raw_height+63) >> 5;
  2349. pixel = (uchar *) malloc (raw_width*32 + ns*4);
  2350. merror (pixel, "kodak_262_load_raw()");
  2351. strip = (int *) (pixel + raw_width*32);
  2352. order = 0x4d4d;
  2353. FORC(ns) strip[c] = get4();
  2354. for (row=0; row < raw_height; row++) {
  2355. if ((row & 31) == 0) {
  2356. fseek (ifp, strip[row >> 5], SEEK_SET);
  2357. getbits(-1);
  2358. pi = 0;
  2359. }
  2360. for (col=0; col < raw_width; col++) {
  2361. chess = (row + col) & 1;
  2362. pi1 = chess ? pi-2 : pi-raw_width-1;
  2363. pi2 = chess ? pi-2*raw_width : pi-raw_width+1;
  2364. if (col <= chess) pi1 = -1;
  2365. if (pi1 < 0) pi1 = pi2;
  2366. if (pi2 < 0) pi2 = pi1;
  2367. if (pi1 < 0 && col > 1) pi1 = pi2 = pi-2;
  2368. pred = (pi1 < 0) ? 0 : (pixel[pi1] + pixel[pi2]) >> 1;
  2369. pixel[pi] = val = pred + ljpeg_diff (huff[chess]);
  2370. if (val >> 8) derror();
  2371. #ifdef LIBRAW_LIBRARY_BUILD
  2372. if(filtering_mode & LIBRAW_FILTERING_NORAWCURVE)
  2373. val = pixel[pi++];
  2374. else
  2375. val = curve[pixel[pi++]];
  2376. #else
  2377. val = curve[pixel[pi++]];
  2378. #endif
  2379. if ((unsigned) (col-left_margin) < width)
  2380. {
  2381. #ifdef LIBRAW_LIBRARY_BUILD
  2382. ushort color = FC(row,col-left_margin);
  2383. if(channel_maximum[color] < val ) channel_maximum[color]=val;
  2384. #endif
  2385. BAYER(row,col-left_margin) = val;
  2386. }
  2387. else
  2388. #ifndef LIBRAW_LIBRARY_BUILD
  2389. black += val;
  2390. #else
  2391. {
  2392. ushort *dfp = get_masked_pointer(row,col);
  2393. if(dfp) *dfp = val;
  2394. black += val;
  2395. }
  2396. #endif
  2397. }
  2398. }
  2399. free (pixel);
  2400. FORC(2) free (huff[c]);
  2401. if (raw_width > width)
  2402. black /= (raw_width - width) * height;
  2403. }
  2404. int CLASS kodak_65000_decode (short *out, int bsize)
  2405. {
  2406. uchar c, blen[768];
  2407. ushort raw[6];
  2408. INT64 bitbuf=0;
  2409. int save, bits=0, i, j, len, diff;
  2410. save = ftell(ifp);
  2411. bsize = (bsize + 3) & -4;
  2412. for (i=0; i < bsize; i+=2) {
  2413. c = fgetc(ifp);
  2414. if ((blen[i ] = c & 15) > 12 ||
  2415. (blen[i+1] = c >> 4) > 12 ) {
  2416. fseek (ifp, save, SEEK_SET);
  2417. for (i=0; i < bsize; i+=8) {
  2418. read_shorts (raw, 6);
  2419. out[i ] = raw[0] >> 12 << 8 | raw[2] >> 12 << 4 | raw[4] >> 12;
  2420. out[i+1] = raw[1] >> 12 << 8 | raw[3] >> 12 << 4 | raw[5] >> 12;
  2421. for (j=0; j < 6; j++)
  2422. out[i+2+j] = raw[j] & 0xfff;
  2423. }
  2424. return 1;
  2425. }
  2426. }
  2427. if ((bsize & 7) == 4) {
  2428. bitbuf = fgetc(ifp) << 8;
  2429. bitbuf += fgetc(ifp);
  2430. bits = 16;
  2431. }
  2432. for (i=0; i < bsize; i++) {
  2433. len = blen[i];
  2434. if (bits < len) {
  2435. for (j=0; j < 32; j+=8)
  2436. bitbuf += (INT64) fgetc(ifp) << (bits+(j^8));
  2437. bits += 32;
  2438. }
  2439. diff = bitbuf & (0xffff >> (16-len));
  2440. bitbuf >>= len;
  2441. bits -= len;
  2442. if ((diff & (1 << (len-1))) == 0)
  2443. diff -= (1 << len) - 1;
  2444. out[i] = diff;
  2445. }
  2446. return 0;
  2447. }
  2448. void CLASS kodak_65000_load_raw()
  2449. {
  2450. short buf[256];
  2451. int row, col, len, pred[2], ret, i;
  2452. for (row=0; row < height; row++)
  2453. for (col=0; col < width; col+=256) {
  2454. pred[0] = pred[1] = 0;
  2455. len = MIN (256, width-col);
  2456. ret = kodak_65000_decode (buf, len);
  2457. for (i=0; i < len; i++)
  2458. #ifndef LIBRAW_LIBRARY_BUILD
  2459. if ((BAYER(row,col+i) = curve[ret ? buf[i] :
  2460. (pred[i & 1] += buf[i])]) >> 12) derror();
  2461. #else
  2462. {
  2463. ushort val = ret ? buf[i] : (pred[i & 1] += buf[i]);
  2464. ushort color = FC(row,col);
  2465. if(!(filtering_mode & LIBRAW_FILTERING_NORAWCURVE))
  2466. val = curve[val];
  2467. BAYER(row,col+i)=val;
  2468. if(channel_maximum[color] < val ) channel_maximum[color]=val;
  2469. if(curve[val]>>12) derror();
  2470. }
  2471. #endif
  2472. }
  2473. }
  2474. void CLASS kodak_ycbcr_load_raw()
  2475. {
  2476. short buf[384], *bp;
  2477. int row, col, len, c, i, j, k, y[2][2], cb, cr, rgb[3];
  2478. ushort *ip;
  2479. for (row=0; row < height; row+=2)
  2480. for (col=0; col < width; col+=128) {
  2481. len = MIN (128, width-col);
  2482. kodak_65000_decode (buf, len*3);
  2483. y[0][1] = y[1][1] = cb = cr = 0;
  2484. for (bp=buf, i=0; i < len; i+=2, bp+=2) {
  2485. cb += bp[4];
  2486. cr += bp[5];
  2487. rgb[1] = -((cb + cr + 2) >> 2);
  2488. rgb[2] = rgb[1] + cb;
  2489. rgb[0] = rgb[1] + cr;
  2490. for (j=0; j < 2; j++)
  2491. for (k=0; k < 2; k++) {
  2492. if ((y[j][k] = y[j][k^1] + *bp++) >> 10) derror();
  2493. ip = image[(row+j)*width + col+i+k];
  2494. #ifndef LIBRAW_LIBRARY_BUILD
  2495. FORC3 ip[c] = curve[LIM(y[j][k]+rgb[c], 0, 0xfff)];
  2496. #else
  2497. if(!(filtering_mode & LIBRAW_FILTERING_NORAWCURVE))
  2498. FORC3 ip[c] = curve[LIM(y[j][k]+rgb[c], 0, 0xfff)];
  2499. else
  2500. FORC3 ip[c] = y[j][k]+rgb[c];;
  2501. FORC3
  2502. if(channel_maximum[c] < ip[c])
  2503. channel_maximum[c] = ip[c];
  2504. #endif
  2505. }
  2506. }
  2507. }
  2508. }
  2509. void CLASS kodak_rgb_load_raw()
  2510. {
  2511. short buf[768], *bp;
  2512. int row, col, len, c, i, rgb[3];
  2513. ushort *ip=image[0];
  2514. for (row=0; row < height; row++)
  2515. for (col=0; col < width; col+=256) {
  2516. len = MIN (256, width-col);
  2517. kodak_65000_decode (buf, len*3);
  2518. memset (rgb, 0, sizeof rgb);
  2519. for (bp=buf, i=0; i < len; i++, ip+=4)
  2520. FORC3{
  2521. if ((ip[c] = rgb[c] += *bp++) >> 12) derror();
  2522. #ifdef LIBRAW_LIBRARY_BUILD
  2523. if(channel_maximum[c] < ip[c]) channel_maximum[c] = ip[c];
  2524. #endif
  2525. }
  2526. }
  2527. }
  2528. void CLASS kodak_thumb_load_raw()
  2529. {
  2530. int row, col;
  2531. colors = thumb_misc >> 5;
  2532. for (row=0; row < height; row++)
  2533. for (col=0; col < width; col++)
  2534. read_shorts (image[row*width+col], colors);
  2535. maximum = (1 << (thumb_misc & 31)) - 1;
  2536. }
  2537. void CLASS sony_decrypt (unsigned *data, int len, int start, int key)
  2538. {
  2539. #ifndef LIBRAW_NOTHREADS
  2540. #define pad tls->sony_decrypt.pad
  2541. #define p tls->sony_decrypt.p
  2542. #else
  2543. static unsigned pad[128], p;
  2544. #endif
  2545. if (start) {
  2546. for (p=0; p < 4; p++)
  2547. pad[p] = key = key * 48828125 + 1;
  2548. pad[3] = pad[3] << 1 | (pad[0]^pad[2]) >> 31;
  2549. for (p=4; p < 127; p++)
  2550. pad[p] = (pad[p-4]^pad[p-2]) << 1 | (pad[p-3]^pad[p-1]) >> 31;
  2551. for (p=0; p < 127; p++)
  2552. pad[p] = htonl(pad[p]);
  2553. }
  2554. while (len--)
  2555. *data++ ^= pad[p++ & 127] = pad[(p+1) & 127] ^ pad[(p+65) & 127];
  2556. #ifndef LIBRAW_NOTHREADS
  2557. #undef pad
  2558. #undef p
  2559. #endif
  2560. }
  2561. void CLASS sony_load_raw()
  2562. {
  2563. uchar head[40];
  2564. ushort *pixel;
  2565. unsigned i, key, row, col;
  2566. fseek (ifp, 200896, SEEK_SET);
  2567. fseek (ifp, (unsigned) fgetc(ifp)*4 - 1, SEEK_CUR);
  2568. order = 0x4d4d;
  2569. key = get4();
  2570. fseek (ifp, 164600, SEEK_SET);
  2571. fread (head, 1, 40, ifp);
  2572. sony_decrypt ((unsigned int *) head, 10, 1, key);
  2573. for (i=26; i-- > 22; )
  2574. key = key << 8 | head[i];
  2575. fseek (ifp, data_offset, SEEK_SET);
  2576. pixel = (ushort *) calloc (raw_width, sizeof *pixel);
  2577. merror (pixel, "sony_load_raw()");
  2578. for (row=0; row < height; row++) {
  2579. if (fread (pixel, 2, raw_width, ifp) < raw_width) derror();
  2580. sony_decrypt ((unsigned int *) pixel, raw_width/2, !row, key);
  2581. #ifdef LIBRAW_LIBRARY_BUILD
  2582. for (col=0; col < left_margin; col++)
  2583. {
  2584. ushort *dfp = get_masked_pointer(row,col);
  2585. if(dfp) *dfp = ntohs(pixel[col]);
  2586. }
  2587. for (col=left_margin+width; col < raw_width; col++)
  2588. {
  2589. ushort *dfp = get_masked_pointer(row,col);
  2590. if(dfp) *dfp = ntohs(pixel[col]);
  2591. }
  2592. #endif
  2593. for (col=9; col < left_margin; col++)
  2594. black += ntohs(pixel[col]);
  2595. for (col=0; col < width; col++)
  2596. #ifdef LIBRAW_LIBRARY_BUILD
  2597. {
  2598. ushort color = FC(row,col);
  2599. ushort val = ntohs(pixel[col+left_margin]);
  2600. if(val>>14)
  2601. derror();
  2602. BAYER(row,col)=val;
  2603. if(channel_maximum[color] < val ) channel_maximum[color] = val;
  2604. }
  2605. #else
  2606. if ((BAYER(row,col) = ntohs(pixel[col+left_margin])) >> 14)
  2607. derror();
  2608. #endif
  2609. }
  2610. free (pixel);
  2611. if (left_margin > 9)
  2612. black /= (left_margin-9) * height;
  2613. maximum = 0x3ff0;
  2614. }
  2615. void CLASS sony_arw_load_raw()
  2616. {
  2617. ushort huff[32768];
  2618. static const ushort tab[18] =
  2619. { 0xf11,0xf10,0xe0f,0xd0e,0xc0d,0xb0c,0xa0b,0x90a,0x809,
  2620. 0x708,0x607,0x506,0x405,0x304,0x303,0x300,0x202,0x201 };
  2621. int i, c, n, col, row, len, diff, sum=0;
  2622. for (n=i=0; i < 18; i++)
  2623. FORC(32768 >> (tab[i] >> 8)) huff[n++] = tab[i];
  2624. getbits(-1);
  2625. for (col = raw_width; col--; )
  2626. for (row=0; row < raw_height+1; row+=2) {
  2627. if (row == raw_height) row = 1;
  2628. len = getbithuff(15,huff);
  2629. diff = getbits(len);
  2630. if ((diff & (1 << (len-1))) == 0)
  2631. diff -= (1 << len) - 1;
  2632. if ((sum += diff) >> 12) derror();
  2633. if (row < height)
  2634. {
  2635. BAYER(row,col) = sum;
  2636. #ifdef LIBRAW_LIBRARY_BUILD
  2637. ushort color = FC(row,col);
  2638. if(channel_maximum[color] < sum ) channel_maximum[color] = sum;
  2639. #endif
  2640. }
  2641. #ifdef LIBRAW_LIBRARY_BUILD
  2642. else
  2643. {
  2644. ushort *dfp = get_masked_pointer(row,col);
  2645. if(dfp) *dfp = sum;
  2646. }
  2647. #endif
  2648. }
  2649. }
  2650. void CLASS sony_arw2_load_raw()
  2651. {
  2652. uchar *data, *dp;
  2653. ushort pix[16];
  2654. int row, col, val, max, min, imax, imin, sh, bit, i;
  2655. data = (uchar *) malloc (raw_width+1);
  2656. merror (data, "sony_arw2_load_raw()");
  2657. for (row=0; row < height; row++) {
  2658. fread (data, 1, raw_width, ifp);
  2659. for (dp=data, col=0; col < width-30; dp+=16) {
  2660. max = 0x7ff & (val = sget4(dp));
  2661. min = 0x7ff & val >> 11;
  2662. imax = 0x0f & val >> 22;
  2663. imin = 0x0f & val >> 26;
  2664. for (sh=0; sh < 4 && 0x80 << sh <= max-min; sh++);
  2665. for (bit=30, i=0; i < 16; i++)
  2666. if (i == imax) pix[i] = max;
  2667. else if (i == imin) pix[i] = min;
  2668. else {
  2669. pix[i] = ((sget2(dp+(bit >> 3)) >> (bit & 7) & 0x7f) << sh) + min;
  2670. if (pix[i] > 0x7ff) pix[i] = 0x7ff;
  2671. bit += 7;
  2672. }
  2673. for (i=0; i < 16; i++, col+=2)
  2674. #ifndef LIBRAW_LIBRARY_BUILD
  2675. BAYER(row,col) = curve[pix[i] << 1] >> 2;
  2676. #else
  2677. {
  2678. ushort val = pix[i];
  2679. ushort color = FC(row,col);
  2680. if(!(filtering_mode & LIBRAW_FILTERING_NORAWCURVE))
  2681. val = curve[val<<1]>>2;
  2682. BAYER(row,col)=val;
  2683. if(channel_maximum[color] < val ) channel_maximum[color] = val;
  2684. }
  2685. #endif
  2686. col -= col & 1 ? 1:31;
  2687. }
  2688. }
  2689. free (data);
  2690. }
  2691. #define HOLE(row) ((holes >> (((row) - raw_height) & 7)) & 1)
  2692. /* Kudos to Rich Taylor for figuring out SMaL's compression algorithm. */
  2693. void CLASS smal_decode_segment (unsigned seg[2][2], int holes)
  2694. {
  2695. uchar hist[3][13] = {
  2696. { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
  2697. { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
  2698. { 3, 3, 0, 0, 63, 47, 31, 15, 0 } };
  2699. int low, high=0xff, carry=0, nbits=8;
  2700. int s, count, bin, next, i, sym[3];
  2701. uchar diff, pred[]={0,0};
  2702. ushort data=0, range=0;
  2703. unsigned pix, row, col;
  2704. fseek (ifp, seg[0][1]+1, SEEK_SET);
  2705. getbits(-1);
  2706. for (pix=seg[0][0]; pix < seg[1][0]; pix++) {
  2707. for (s=0; s < 3; s++) {
  2708. data = data << nbits | getbits(nbits);
  2709. if (carry < 0)
  2710. carry = (nbits += carry+1) < 1 ? nbits-1 : 0;
  2711. while (--nbits >= 0)
  2712. if ((data >> nbits & 0xff) == 0xff) break;
  2713. if (nbits > 0)
  2714. data = ((data & ((1 << (nbits-1)) - 1)) << 1) |
  2715. ((data + (((data & (1 << (nbits-1)))) << 1)) & (-1 << nbits));
  2716. if (nbits >= 0) {
  2717. data += getbits(1);
  2718. carry = nbits - 8;
  2719. }
  2720. count = ((((data-range+1) & 0xffff) << 2) - 1) / (high >> 4);
  2721. for (bin=0; hist[s][bin+5] > count; bin++);
  2722. low = hist[s][bin+5] * (high >> 4) >> 2;
  2723. if (bin) high = hist[s][bin+4] * (high >> 4) >> 2;
  2724. high -= low;
  2725. for (nbits=0; high << nbits < 128; nbits++);
  2726. range = (range+low) << nbits;
  2727. high <<= nbits;
  2728. next = hist[s][1];
  2729. if (++hist[s][2] > hist[s][3]) {
  2730. next = (next+1) & hist[s][0];
  2731. hist[s][3] = (hist[s][next+4] - hist[s][next+5]) >> 2;
  2732. hist[s][2] = 1;
  2733. }
  2734. if (hist[s][hist[s][1]+4] - hist[s][hist[s][1]+5] > 1) {
  2735. if (bin < hist[s][1])
  2736. for (i=bin; i < hist[s][1]; i++) hist[s][i+5]--;
  2737. else if (next <= bin)
  2738. for (i=hist[s][1]; i < bin; i++) hist[s][i+5]++;
  2739. }
  2740. hist[s][1] = next;
  2741. sym[s] = bin;
  2742. }
  2743. diff = sym[2] << 5 | sym[1] << 2 | (sym[0] & 3);
  2744. if (sym[0] & 4)
  2745. diff = diff ? -diff : 0x80;
  2746. if (ftell(ifp) + 12 >= seg[1][1])
  2747. diff = 0;
  2748. pred[pix & 1] += diff;
  2749. row = pix / raw_width - top_margin;
  2750. col = pix % raw_width - left_margin;
  2751. if (row < height && col < width)
  2752. {
  2753. #ifdef LIBRAW_LIBRARY_BUILD
  2754. ushort color = FC(row,col);
  2755. if(channel_maximum[color] < pred[pix & 1])
  2756. channel_maximum[color] = pred[pix & 1];
  2757. #endif
  2758. BAYER(row,col) = pred[pix & 1];
  2759. }
  2760. #ifdef LIBRAW_LIBRARY_BUILD
  2761. else
  2762. {
  2763. ushort *dfp = get_masked_pointer(row+top_margin,col+left_margin);
  2764. if(dfp) *dfp = pred[pix &1];
  2765. }
  2766. #endif
  2767. if (!(pix & 1) && HOLE(row)) pix += 2;
  2768. }
  2769. maximum = 0xff;
  2770. }
  2771. void CLASS smal_v6_load_raw()
  2772. {
  2773. unsigned seg[2][2];
  2774. fseek (ifp, 16, SEEK_SET);
  2775. seg[0][0] = 0;
  2776. seg[0][1] = get2();
  2777. seg[1][0] = raw_width * raw_height;
  2778. seg[1][1] = INT_MAX;
  2779. smal_decode_segment (seg, 0);
  2780. }
  2781. int CLASS median4 (int *p)
  2782. {
  2783. int min, max, sum, i;
  2784. min = max = sum = p[0];
  2785. for (i=1; i < 4; i++) {
  2786. sum += p[i];
  2787. if (min > p[i]) min = p[i];
  2788. if (max < p[i]) max = p[i];
  2789. }
  2790. return (sum - min - max) >> 1;
  2791. }
  2792. void CLASS fill_holes (int holes)
  2793. {
  2794. int row, col, val[4];
  2795. for (row=2; row < height-2; row++) {
  2796. if (!HOLE(row)) continue;
  2797. for (col=1; col < width-1; col+=4) {
  2798. val[0] = BAYER(row-1,col-1);
  2799. val[1] = BAYER(row-1,col+1);
  2800. val[2] = BAYER(row+1,col-1);
  2801. val[3] = BAYER(row+1,col+1);
  2802. BAYER(row,col) = median4(val);
  2803. }
  2804. for (col=2; col < width-2; col+=4)
  2805. if (HOLE(row-2) || HOLE(row+2))
  2806. BAYER(row,col) = (BAYER(row,col-2) + BAYER(row,col+2)) >> 1;
  2807. else {
  2808. val[0] = BAYER(row,col-2);
  2809. val[1] = BAYER(row,col+2);
  2810. val[2] = BAYER(row-2,col);
  2811. val[3] = BAYER(row+2,col);
  2812. BAYER(row,col) = median4(val);
  2813. }
  2814. }
  2815. }
  2816. void CLASS smal_v9_load_raw()
  2817. {
  2818. unsigned seg[256][2], offset, nseg, holes, i;
  2819. fseek (ifp, 67, SEEK_SET);
  2820. offset = get4();
  2821. nseg = fgetc(ifp);
  2822. fseek (ifp, offset, SEEK_SET);
  2823. for (i=0; i < nseg*2; i++)
  2824. seg[0][i] = get4() + data_offset*(i & 1);
  2825. fseek (ifp, 78, SEEK_SET);
  2826. holes = fgetc(ifp);
  2827. fseek (ifp, 88, SEEK_SET);
  2828. seg[nseg][0] = raw_height * raw_width;
  2829. seg[nseg][1] = get4() + data_offset;
  2830. for (i=0; i < nseg; i++)
  2831. smal_decode_segment (seg+i, holes);
  2832. if (holes) fill_holes (holes);
  2833. }
  2834. #line 4107 "dcraw/dcraw.c"
  2835. void CLASS gamma_curve (double pwr, double ts, int mode, int imax)
  2836. {
  2837. int i;
  2838. double g[6], bnd[2]={0,0}, r;
  2839. g[0] = pwr;
  2840. g[1] = ts;
  2841. g[2] = g[3] = g[4] = 0;
  2842. bnd[g[1] >= 1] = 1;
  2843. if (g[1] && (g[1]-1)*(g[0]-1) <= 0) {
  2844. for (i=0; i < 48; i++) {
  2845. g[2] = (bnd[0] + bnd[1])/2;
  2846. if (g[0]) bnd[(pow(g[2]/g[1],-g[0]) - 1)/g[0] - 1/g[2] > -1] = g[2];
  2847. else bnd[g[2]/exp(1-1/g[2]) < g[1]] = g[2];
  2848. }
  2849. g[3] = g[2] / g[1];
  2850. if (g[0]) g[4] = g[2] * (1/g[0] - 1);
  2851. }
  2852. if (g[0]) g[5] = 1 / (g[1]*SQR(g[3])/2 - g[4]*(1 - g[3]) +
  2853. (1 - pow(g[3],1+g[0]))*(1 + g[4])/(1 + g[0])) - 1;
  2854. else g[5] = 1 / (g[1]*SQR(g[3])/2 + 1
  2855. - g[2] - g[3] - g[2]*g[3]*(log(g[3]) - 1)) - 1;
  2856. if (!mode--) {
  2857. memcpy (gamm, g, sizeof gamm);
  2858. return;
  2859. }
  2860. for (i=0; i < 0x10000; i++) {
  2861. curve[i] = 0xffff;
  2862. if ((r = (double) i / imax) < 1)
  2863. curve[i] = 0x10000 * ( mode
  2864. ? (r < g[3] ? r*g[1] : (g[0] ? pow( r,g[0])*(1+g[4])-g[4] : log(r)*g[2]+1))
  2865. : (r < g[2] ? r/g[1] : (g[0] ? pow((r+g[4])/(1+g[4]),1/g[0]) : exp((r-1)/g[2]))));
  2866. }
  2867. }
  2868. void CLASS pseudoinverse (double (*in)[3], double (*out)[3], int size)
  2869. {
  2870. double work[3][6], num;
  2871. int i, j, k;
  2872. for (i=0; i < 3; i++) {
  2873. for (j=0; j < 6; j++)
  2874. work[i][j] = j == i+3;
  2875. for (j=0; j < 3; j++)
  2876. for (k=0; k < size; k++)
  2877. work[i][j] += in[k][i] * in[k][j];
  2878. }
  2879. for (i=0; i < 3; i++) {
  2880. num = work[i][i];
  2881. for (j=0; j < 6; j++)
  2882. work[i][j] /= num;
  2883. for (k=0; k < 3; k++) {
  2884. if (k==i) continue;
  2885. num = work[k][i];
  2886. for (j=0; j < 6; j++)
  2887. work[k][j] -= work[i][j] * num;
  2888. }
  2889. }
  2890. for (i=0; i < size; i++)
  2891. for (j=0; j < 3; j++)
  2892. for (out[i][j]=k=0; k < 3; k++)
  2893. out[i][j] += work[j][k+3] * in[i][k];
  2894. }
  2895. void CLASS cam_xyz_coeff (double cam_xyz[4][3])
  2896. {
  2897. double cam_rgb[4][3], inverse[4][3], num;
  2898. int i, j, k;
  2899. for (i=0; i < colors; i++) /* Multiply out XYZ colorspace */
  2900. for (j=0; j < 3; j++)
  2901. for (cam_rgb[i][j] = k=0; k < 3; k++)
  2902. cam_rgb[i][j] += cam_xyz[i][k] * xyz_rgb[k][j];
  2903. for (i=0; i < colors; i++) { /* Normalize cam_rgb so that */
  2904. for (num=j=0; j < 3; j++) /* cam_rgb * (1,1,1) is (1,1,1,1) */
  2905. num += cam_rgb[i][j];
  2906. for (j=0; j < 3; j++)
  2907. cam_rgb[i][j] /= num;
  2908. pre_mul[i] = 1 / num;
  2909. }
  2910. pseudoinverse (cam_rgb, inverse, colors);
  2911. for (raw_color = i=0; i < 3; i++)
  2912. for (j=0; j < colors; j++)
  2913. rgb_cam[i][j] = inverse[j][i];
  2914. #ifdef LIBRAW_LIBRARY_BUILD
  2915. color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
  2916. color_flags.rgb_cam_state = LIBRAW_COLORSTATE_CONST;
  2917. #endif
  2918. }
  2919. #ifdef COLORCHECK
  2920. void CLASS colorcheck()
  2921. {
  2922. #define NSQ 24
  2923. // Coordinates of the GretagMacbeth ColorChecker squares
  2924. // width, height, 1st_column, 1st_row
  2925. int cut[NSQ][4]; // you must set these
  2926. // ColorChecker Chart under 6500-kelvin illumination
  2927. static const double gmb_xyY[NSQ][3] = {
  2928. { 0.400, 0.350, 10.1 }, // Dark Skin
  2929. { 0.377, 0.345, 35.8 }, // Light Skin
  2930. { 0.247, 0.251, 19.3 }, // Blue Sky
  2931. { 0.337, 0.422, 13.3 }, // Foliage
  2932. { 0.265, 0.240, 24.3 }, // Blue Flower
  2933. { 0.261, 0.343, 43.1 }, // Bluish Green
  2934. { 0.506, 0.407, 30.1 }, // Orange
  2935. { 0.211, 0.175, 12.0 }, // Purplish Blue
  2936. { 0.453, 0.306, 19.8 }, // Moderate Red
  2937. { 0.285, 0.202, 6.6 }, // Purple
  2938. { 0.380, 0.489, 44.3 }, // Yellow Green
  2939. { 0.473, 0.438, 43.1 }, // Orange Yellow
  2940. { 0.187, 0.129, 6.1 }, // Blue
  2941. { 0.305, 0.478, 23.4 }, // Green
  2942. { 0.539, 0.313, 12.0 }, // Red
  2943. { 0.448, 0.470, 59.1 }, // Yellow
  2944. { 0.364, 0.233, 19.8 }, // Magenta
  2945. { 0.196, 0.252, 19.8 }, // Cyan
  2946. { 0.310, 0.316, 90.0 }, // White
  2947. { 0.310, 0.316, 59.1 }, // Neutral 8
  2948. { 0.310, 0.316, 36.2 }, // Neutral 6.5
  2949. { 0.310, 0.316, 19.8 }, // Neutral 5
  2950. { 0.310, 0.316, 9.0 }, // Neutral 3.5
  2951. { 0.310, 0.316, 3.1 } }; // Black
  2952. double gmb_cam[NSQ][4], gmb_xyz[NSQ][3];
  2953. double inverse[NSQ][3], cam_xyz[4][3], num;
  2954. int c, i, j, k, sq, row, col, count[4];
  2955. memset (gmb_cam, 0, sizeof gmb_cam);
  2956. for (sq=0; sq < NSQ; sq++) {
  2957. FORCC count[c] = 0;
  2958. for (row=cut[sq][3]; row < cut[sq][3]+cut[sq][1]; row++)
  2959. for (col=cut[sq][2]; col < cut[sq][2]+cut[sq][0]; col++) {
  2960. c = FC(row,col);
  2961. if (c >= colors) c -= 2;
  2962. gmb_cam[sq][c] += BAYER(row,col);
  2963. count[c]++;
  2964. }
  2965. FORCC gmb_cam[sq][c] = gmb_cam[sq][c]/count[c] - black;
  2966. gmb_xyz[sq][0] = gmb_xyY[sq][2] * gmb_xyY[sq][0] / gmb_xyY[sq][1];
  2967. gmb_xyz[sq][1] = gmb_xyY[sq][2];
  2968. gmb_xyz[sq][2] = gmb_xyY[sq][2] *
  2969. (1 - gmb_xyY[sq][0] - gmb_xyY[sq][1]) / gmb_xyY[sq][1];
  2970. }
  2971. pseudoinverse (gmb_xyz, inverse, NSQ);
  2972. for (i=0; i < colors; i++)
  2973. for (j=0; j < 3; j++)
  2974. for (cam_xyz[i][j] = k=0; k < NSQ; k++)
  2975. cam_xyz[i][j] += gmb_cam[k][i] * inverse[k][j];
  2976. cam_xyz_coeff (cam_xyz);
  2977. #ifdef DCRAW_VERBOSE
  2978. if (verbose) {
  2979. printf (" { \"%s %s\", %d,\n\t{", make, model, black);
  2980. num = 10000 / (cam_xyz[1][0] + cam_xyz[1][1] + cam_xyz[1][2]);
  2981. FORCC for (j=0; j < 3; j++)
  2982. printf ("%c%d", (c | j) ? ',':' ', (int) (cam_xyz[c][j] * num + 0.5));
  2983. puts (" } },");
  2984. }
  2985. #endif
  2986. #undef NSQ
  2987. }
  2988. #endif
  2989. void CLASS hat_transform (float *temp, float *base, int st, int size, int sc)
  2990. {
  2991. int i;
  2992. for (i=0; i < sc; i++)
  2993. temp[i] = 2*base[st*i] + base[st*(sc-i)] + base[st*(i+sc)];
  2994. for (; i+sc < size; i++)
  2995. temp[i] = 2*base[st*i] + base[st*(i-sc)] + base[st*(i+sc)];
  2996. for (; i < size; i++)
  2997. temp[i] = 2*base[st*i] + base[st*(i-sc)] + base[st*(2*size-2-(i+sc))];
  2998. }
  2999. #ifndef _OPENMP
  3000. void CLASS wavelet_denoise()
  3001. {
  3002. float *fimg=0, *temp, thold, mul[2], avg, diff;
  3003. int scale=1, size, lev, hpass, lpass, row, col, nc, c, i, wlast, blk[2];
  3004. ushort *window[4];
  3005. static const float noise[] =
  3006. { 0.8002,0.2735,0.1202,0.0585,0.0291,0.0152,0.0080,0.0044 };
  3007. #ifdef DCRAW_VERBOSE
  3008. if (verbose) fprintf (stderr,_("Wavelet denoising...\n"));
  3009. #endif
  3010. while (maximum << scale < 0x10000) scale++;
  3011. maximum <<= --scale;
  3012. black <<= scale;
  3013. FORC4 cblack[c] <<= scale;
  3014. if ((size = iheight*iwidth) < 0x15550000)
  3015. fimg = (float *) malloc ((size*3 + iheight + iwidth) * sizeof *fimg);
  3016. merror (fimg, "wavelet_denoise()");
  3017. temp = fimg + size*3;
  3018. if ((nc = colors) == 3 && filters) nc++;
  3019. FORC(nc) { /* denoise R,G1,B,G3 individually */
  3020. for (i=0; i < size; i++)
  3021. fimg[i] = 256 * sqrt((double)(image[i][c] << scale));
  3022. for (hpass=lev=0; lev < 5; lev++) {
  3023. lpass = size*((lev & 1)+1);
  3024. for (row=0; row < iheight; row++) {
  3025. hat_transform (temp, fimg+hpass+row*iwidth, 1, iwidth, 1 << lev);
  3026. for (col=0; col < iwidth; col++)
  3027. fimg[lpass + row*iwidth + col] = temp[col] * 0.25;
  3028. }
  3029. for (col=0; col < iwidth; col++) {
  3030. hat_transform (temp, fimg+lpass+col, iwidth, iheight, 1 << lev);
  3031. for (row=0; row < iheight; row++)
  3032. fimg[lpass + row*iwidth + col] = temp[row] * 0.25;
  3033. }
  3034. thold = threshold * noise[lev];
  3035. for (i=0; i < size; i++) {
  3036. fimg[hpass+i] -= fimg[lpass+i];
  3037. if (fimg[hpass+i] < -thold) fimg[hpass+i] += thold;
  3038. else if (fimg[hpass+i] > thold) fimg[hpass+i] -= thold;
  3039. else fimg[hpass+i] = 0;
  3040. if (hpass) fimg[i] += fimg[hpass+i];
  3041. }
  3042. hpass = lpass;
  3043. }
  3044. for (i=0; i < size; i++)
  3045. image[i][c] = CLIP(SQR(fimg[i]+fimg[lpass+i])/0x10000);
  3046. }
  3047. if (filters && colors == 3) { /* pull G1 and G3 closer together */
  3048. for (row=0; row < 2; row++) {
  3049. mul[row] = 0.125 * pre_mul[FC(row+1,0) | 1] / pre_mul[FC(row,0) | 1];
  3050. blk[row] = cblack[FC(row,0) | 1];
  3051. }
  3052. for (i=0; i < 4; i++)
  3053. window[i] = (ushort *) fimg + width*i;
  3054. for (wlast=-1, row=1; row < height-1; row++) {
  3055. while (wlast < row+1) {
  3056. for (wlast++, i=0; i < 4; i++)
  3057. window[(i+3) & 3] = window[i];
  3058. for (col = FC(wlast,1) & 1; col < width; col+=2)
  3059. window[2][col] = BAYER(wlast,col);
  3060. }
  3061. thold = threshold/512;
  3062. for (col = (FC(row,0) & 1)+1; col < width-1; col+=2) {
  3063. avg = ( window[0][col-1] + window[0][col+1] +
  3064. window[2][col-1] + window[2][col+1] - blk[~row & 1]*4 )
  3065. * mul[row & 1] + (window[1][col] + blk[row & 1]) * 0.5;
  3066. avg = avg < 0 ? 0 : sqrt(avg);
  3067. diff = sqrt((double)(BAYER(row,col))) - avg;
  3068. if (diff < -thold) diff += thold;
  3069. else if (diff > thold) diff -= thold;
  3070. else diff = 0;
  3071. BAYER(row,col) = CLIP(SQR(avg+diff) + 0.5);
  3072. }
  3073. }
  3074. }
  3075. free (fimg);
  3076. }
  3077. #else
  3078. void CLASS wavelet_denoise()
  3079. {
  3080. float *fimg=0, *temp, thold, mul[2], avg, diff;
  3081. int scale=1, size, lev, hpass, lpass, row, col, nc, c, i, wlast, blk[2];
  3082. ushort *window[4];
  3083. static const float noise[] =
  3084. { 0.8002,0.2735,0.1202,0.0585,0.0291,0.0152,0.0080,0.0044 };
  3085. #ifdef DCRAW_VERBOSE
  3086. if (verbose) fprintf (stderr,_("Wavelet denoising...\n"));
  3087. #endif
  3088. while (maximum << scale < 0x10000) scale++;
  3089. maximum <<= --scale;
  3090. black <<= scale;
  3091. FORC4 cblack[c] <<= scale;
  3092. if ((size = iheight*iwidth) < 0x15550000)
  3093. fimg = (float *) malloc ((size*3 + iheight + iwidth) * sizeof *fimg);
  3094. merror (fimg, "wavelet_denoise()");
  3095. temp = fimg + size*3;
  3096. if ((nc = colors) == 3 && filters) nc++;
  3097. #ifdef LIBRAW_LIBRARY_BUILD
  3098. #pragma omp parallel default(shared) private(i,col,row,thold,lev,lpass,hpass,temp,c) firstprivate(scale,size)
  3099. #endif
  3100. {
  3101. temp = (float*)malloc( (iheight + iwidth) * sizeof *fimg);
  3102. FORC(nc) { /* denoise R,G1,B,G3 individually */
  3103. #ifdef LIBRAW_LIBRARY_BUILD
  3104. #pragma omp for
  3105. #endif
  3106. for (i=0; i < size; i++)
  3107. fimg[i] = 256 * sqrt((double)(image[i][c] << scale));
  3108. for (hpass=lev=0; lev < 5; lev++) {
  3109. lpass = size*((lev & 1)+1);
  3110. #ifdef LIBRAW_LIBRARY_BUILD
  3111. #pragma omp for
  3112. #endif
  3113. for (row=0; row < iheight; row++) {
  3114. hat_transform (temp, fimg+hpass+row*iwidth, 1, iwidth, 1 << lev);
  3115. for (col=0; col < iwidth; col++)
  3116. fimg[lpass + row*iwidth + col] = temp[col] * 0.25;
  3117. }
  3118. #ifdef LIBRAW_LIBRARY_BUILD
  3119. #pragma omp for
  3120. #endif
  3121. for (col=0; col < iwidth; col++) {
  3122. hat_transform (temp, fimg+lpass+col, iwidth, iheight, 1 << lev);
  3123. for (row=0; row < iheight; row++)
  3124. fimg[lpass + row*iwidth + col] = temp[row] * 0.25;
  3125. }
  3126. thold = threshold * noise[lev];
  3127. #ifdef LIBRAW_LIBRARY_BUILD
  3128. #pragma omp for
  3129. #endif
  3130. for (i=0; i < size; i++) {
  3131. fimg[hpass+i] -= fimg[lpass+i];
  3132. if (fimg[hpass+i] < -thold) fimg[hpass+i] += thold;
  3133. else if (fimg[hpass+i] > thold) fimg[hpass+i] -= thold;
  3134. else fimg[hpass+i] = 0;
  3135. if (hpass) fimg[i] += fimg[hpass+i];
  3136. }
  3137. hpass = lpass;
  3138. }
  3139. #ifdef LIBRAW_LIBRARY_BUILD
  3140. #pragma omp for
  3141. #endif
  3142. for (i=0; i < size; i++)
  3143. image[i][c] = CLIP(SQR(fimg[i]+fimg[lpass+i])/0x10000);
  3144. }
  3145. free(temp);
  3146. } /* end omp parallel */
  3147. /* the following loops are hard to parallize, no idea yes,
  3148. * problem is wlast which is carrying dependency
  3149. * second part should be easyer, but did not yet get it right.
  3150. */
  3151. if (filters && colors == 3) { /* pull G1 and G3 closer together */
  3152. for (row=0; row < 2; row++){
  3153. mul[row] = 0.125 * pre_mul[FC(row+1,0) | 1] / pre_mul[FC(row,0) | 1];
  3154. blk[row] = cblack[FC(row,0) | 1];
  3155. }
  3156. for (i=0; i < 4; i++)
  3157. window[i] = (ushort *) fimg + width*i;
  3158. for (wlast=-1, row=1; row < height-1; row++) {
  3159. while (wlast < row+1) {
  3160. for (wlast++, i=0; i < 4; i++)
  3161. window[(i+3) & 3] = window[i];
  3162. for (col = FC(wlast,1) & 1; col < width; col+=2)
  3163. window[2][col] = BAYER(wlast,col);
  3164. }
  3165. thold = threshold/512;
  3166. for (col = (FC(row,0) & 1)+1; col < width-1; col+=2) {
  3167. avg = ( window[0][col-1] + window[0][col+1] +
  3168. window[2][col-1] + window[2][col+1] - blk[~row & 1]*4 )
  3169. * mul[row & 1] + (window[1][col] + blk[row & 1]) * 0.5;
  3170. avg = avg < 0 ? 0 : sqrt(avg);
  3171. diff = sqrt((double)BAYER(row,col)) - avg;
  3172. if (diff < -thold) diff += thold;
  3173. else if (diff > thold) diff -= thold;
  3174. else diff = 0;
  3175. BAYER(row,col) = CLIP(SQR(avg+diff) + 0.5);
  3176. }
  3177. }
  3178. }
  3179. free (fimg);
  3180. }
  3181. #endif
  3182. // green equilibration
  3183. void CLASS green_matching()
  3184. {
  3185. int i,j;
  3186. double m1,m2,c1,c2;
  3187. int o1_1,o1_2,o1_3,o1_4;
  3188. int o2_1,o2_2,o2_3,o2_4;
  3189. ushort (*img)[4];
  3190. const int margin = 3;
  3191. int oj = 2, oi = 2;
  3192. float f;
  3193. const float thr = 0.01f;
  3194. if(FC(oj, oi) != 3) oj++;
  3195. if(FC(oj, oi) != 3) oi++;
  3196. img = (ushort (*)[4]) calloc (height*width, sizeof *image);
  3197. merror (img, "green_matching()");
  3198. memcpy(img,image,height*width*sizeof *image);
  3199. for(j=oj;j<height-margin;j+=2)
  3200. for(i=oi;i<width-margin;i+=2){
  3201. o1_1=img[(j-1)*width+i-1][1];
  3202. o1_2=img[(j-1)*width+i+1][1];
  3203. o1_3=img[(j+1)*width+i-1][1];
  3204. o1_4=img[(j+1)*width+i+1][1];
  3205. o2_1=img[(j-2)*width+i][3];
  3206. o2_2=img[(j+2)*width+i][3];
  3207. o2_3=img[j*width+i-2][3];
  3208. o2_4=img[j*width+i+2][3];
  3209. m1=(o1_1+o1_2+o1_3+o1_4)/4.0;
  3210. m2=(o2_1+o2_2+o2_3+o2_4)/4.0;
  3211. 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;
  3212. 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;
  3213. if((img[j*width+i][3]<maximum*0.95)&&(c1<maximum*thr)&&(c2<maximum*thr))
  3214. {
  3215. f = image[j*width+i][3]*m1/m2;
  3216. image[j*width+i][3]=f>0xffff?0xffff:f;
  3217. }
  3218. }
  3219. free(img);
  3220. }
  3221. void CLASS scale_colors()
  3222. {
  3223. unsigned bottom, right, size, row, col, ur, uc, i, x, y, c, sum[8];
  3224. int val, dark, sat;
  3225. double dsum[8], dmin, dmax;
  3226. float scale_mul[4], fr, fc;
  3227. ushort *img=0, *pix;
  3228. #ifdef LIBRAW_LIBRARY_BUILD
  3229. RUN_CALLBACK(LIBRAW_PROGRESS_SCALE_COLORS,0,2);
  3230. #endif
  3231. FORC4 cblack[c] += black;
  3232. if (user_mul[0])
  3233. memcpy (pre_mul, user_mul, sizeof pre_mul);
  3234. if (use_auto_wb || (use_camera_wb && cam_mul[0] == -1)) {
  3235. memset (dsum, 0, sizeof dsum);
  3236. bottom = MIN (greybox[1]+greybox[3], height);
  3237. right = MIN (greybox[0]+greybox[2], width);
  3238. for (row=greybox[1]; row < bottom; row += 8)
  3239. for (col=greybox[0]; col < right; col += 8) {
  3240. memset (sum, 0, sizeof sum);
  3241. for (y=row; y < row+8 && y < bottom; y++)
  3242. for (x=col; x < col+8 && x < right; x++)
  3243. FORC4 {
  3244. if (filters) {
  3245. c = FC(y,x);
  3246. val = BAYER(y,x);
  3247. } else
  3248. val = image[y*width+x][c];
  3249. if (val > maximum-25) goto skip_block;
  3250. if ((val -= cblack[c]) < 0) val = 0;
  3251. sum[c] += val;
  3252. sum[c+4]++;
  3253. if (filters) break;
  3254. }
  3255. FORC(8) dsum[c] += sum[c];
  3256. skip_block: ;
  3257. }
  3258. FORC4 if (dsum[c]) pre_mul[c] = dsum[c+4] / dsum[c];
  3259. #ifdef LIBRAW_LIBRARY_BUILD
  3260. color_flags.pre_mul_state = LIBRAW_COLORSTATE_CALCULATED;
  3261. #endif
  3262. }
  3263. if (use_camera_wb && cam_mul[0] != -1) {
  3264. memset (sum, 0, sizeof sum);
  3265. for (row=0; row < 8; row++)
  3266. for (col=0; col < 8; col++) {
  3267. c = FC(row,col);
  3268. if ((val = white[row][col] - cblack[c]) > 0)
  3269. sum[c] += val;
  3270. sum[c+4]++;
  3271. }
  3272. if (sum[0] && sum[1] && sum[2] && sum[3])
  3273. {
  3274. FORC4 pre_mul[c] = (float) sum[c+4] / sum[c];
  3275. #ifdef LIBRAW_LIBRARY_BUILD
  3276. color_flags.pre_mul_state = LIBRAW_COLORSTATE_CALCULATED;
  3277. #endif
  3278. }
  3279. else if (cam_mul[0] && cam_mul[2])
  3280. {
  3281. memcpy (pre_mul, cam_mul, sizeof pre_mul);
  3282. #ifdef LIBRAW_LIBRARY_BUILD
  3283. color_flags.pre_mul_state =color_flags.pre_mul_state;
  3284. #endif
  3285. }
  3286. else
  3287. {
  3288. #ifdef LIBRAW_LIBRARY_BUILD
  3289. imgdata.process_warnings |= LIBRAW_WARN_BAD_CAMERA_WB;
  3290. #endif
  3291. #ifdef DCRAW_VERBOSE
  3292. fprintf (stderr,_("%s: Cannot use camera white balance.\n"), ifname);
  3293. #endif
  3294. }
  3295. }
  3296. if (pre_mul[3] == 0) pre_mul[3] = colors < 4 ? pre_mul[1] : 1;
  3297. dark = black;
  3298. sat = maximum;
  3299. if (threshold) wavelet_denoise();
  3300. maximum -= black;
  3301. #ifdef LIBRAW_LIBRARY_BUILD
  3302. for(i=0;i<4;i++)
  3303. if(channel_maximum[i]>=cblack[i])
  3304. channel_maximum[i]-=cblack[i];
  3305. else
  3306. channel_maximum[i] = 0;
  3307. #endif
  3308. for (dmin=DBL_MAX, dmax=c=0; c < 4; c++) {
  3309. if (dmin > pre_mul[c])
  3310. dmin = pre_mul[c];
  3311. if (dmax < pre_mul[c])
  3312. dmax = pre_mul[c];
  3313. }
  3314. if (!highlight) dmax = dmin;
  3315. FORC4 scale_mul[c] = (pre_mul[c] /= dmax) * 65535.0 / maximum;
  3316. #ifdef DCRAW_VERBOSE
  3317. if (verbose) {
  3318. fprintf (stderr,
  3319. _("Scaling with darkness %d, saturation %d, and\nmultipliers"), dark, sat);
  3320. FORC4 fprintf (stderr, " %f", pre_mul[c]);
  3321. fputc ('\n', stderr);
  3322. }
  3323. #endif
  3324. size = iheight*iwidth;
  3325. for (i=0; i < size*4; i++) {
  3326. val = image[0][i];
  3327. if (!val) continue;
  3328. val -= cblack[i & 3];
  3329. val *= scale_mul[i & 3];
  3330. image[0][i] = CLIP(val);
  3331. }
  3332. if ((aber[0] != 1 || aber[2] != 1) && colors == 3) {
  3333. #ifdef DCRAW_VERBOSE
  3334. if (verbose)
  3335. fprintf (stderr,_("Correcting chromatic aberration...\n"));
  3336. #endif
  3337. for (c=0; c < 4; c+=2) {
  3338. if (aber[c] == 1) continue;
  3339. img = (ushort *) malloc (size * sizeof *img);
  3340. merror (img, "scale_colors()");
  3341. for (i=0; i < size; i++)
  3342. img[i] = image[i][c];
  3343. for (row=0; row < iheight; row++) {
  3344. ur = fr = (row - iheight*0.5) * aber[c] + iheight*0.5;
  3345. if (ur > iheight-2) continue;
  3346. fr -= ur;
  3347. for (col=0; col < iwidth; col++) {
  3348. uc = fc = (col - iwidth*0.5) * aber[c] + iwidth*0.5;
  3349. if (uc > iwidth-2) continue;
  3350. fc -= uc;
  3351. pix = img + ur*iwidth + uc;
  3352. image[row*iwidth+col][c] =
  3353. (pix[ 0]*(1-fc) + pix[ 1]*fc) * (1-fr) +
  3354. (pix[iwidth]*(1-fc) + pix[iwidth+1]*fc) * fr;
  3355. }
  3356. }
  3357. free(img);
  3358. }
  3359. }
  3360. #ifdef LIBRAW_LIBRARY_BUILD
  3361. RUN_CALLBACK(LIBRAW_PROGRESS_SCALE_COLORS,1,2);
  3362. #endif
  3363. }
  3364. void CLASS pre_interpolate()
  3365. {
  3366. ushort (*img)[4];
  3367. int row, col, c;
  3368. #ifdef LIBRAW_LIBRARY_BUILD
  3369. RUN_CALLBACK(LIBRAW_PROGRESS_PRE_INTERPOLATE,0,2);
  3370. #endif
  3371. if (shrink) {
  3372. if (half_size) {
  3373. height = iheight;
  3374. width = iwidth;
  3375. } else {
  3376. img = (ushort (*)[4]) calloc (height*width, sizeof *img);
  3377. merror (img, "pre_interpolate()");
  3378. for (row=0; row < height; row++)
  3379. for (col=0; col < width; col++) {
  3380. c = fc(row,col);
  3381. img[row*width+col][c] = image[(row >> 1)*iwidth+(col >> 1)][c];
  3382. }
  3383. free (image);
  3384. image = img;
  3385. shrink = 0;
  3386. }
  3387. }
  3388. if (filters && colors == 3) {
  3389. if ((mix_green = four_color_rgb)) colors++;
  3390. else {
  3391. for (row = FC(1,0) >> 1; row < height; row+=2)
  3392. for (col = FC(row,1) & 1; col < width; col+=2)
  3393. image[row*width+col][1] = image[row*width+col][3];
  3394. filters &= ~((filters & 0x55555555) << 1);
  3395. }
  3396. }
  3397. if (half_size) filters = 0;
  3398. #ifdef LIBRAW_LIBRARY_BUILD
  3399. RUN_CALLBACK(LIBRAW_PROGRESS_PRE_INTERPOLATE,1,2);
  3400. #endif
  3401. }
  3402. void CLASS border_interpolate (int border)
  3403. {
  3404. unsigned row, col, y, x, f, c, sum[8];
  3405. for (row=0; row < height; row++)
  3406. for (col=0; col < width; col++) {
  3407. if (col==border && row >= border && row < height-border)
  3408. col = width-border;
  3409. memset (sum, 0, sizeof sum);
  3410. for (y=row-1; y != row+2; y++)
  3411. for (x=col-1; x != col+2; x++)
  3412. if (y < height && x < width) {
  3413. f = fc(y,x);
  3414. sum[f] += image[y*width+x][f];
  3415. sum[f+4]++;
  3416. }
  3417. f = fc(row,col);
  3418. FORCC if (c != f && sum[c+4])
  3419. image[row*width+col][c] = sum[c] / sum[c+4];
  3420. }
  3421. }
  3422. void CLASS lin_interpolate()
  3423. {
  3424. int code[16][16][32], *ip, sum[4];
  3425. int c, i, x, y, row, col, shift, color;
  3426. ushort *pix;
  3427. #ifdef DCRAW_VERBOSE
  3428. if (verbose) fprintf (stderr,_("Bilinear interpolation...\n"));
  3429. #endif
  3430. #ifdef LIBRAW_LIBRARY_BUILD
  3431. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,0,3);
  3432. #endif
  3433. border_interpolate(1);
  3434. for (row=0; row < 16; row++)
  3435. for (col=0; col < 16; col++) {
  3436. ip = code[row][col];
  3437. memset (sum, 0, sizeof sum);
  3438. for (y=-1; y <= 1; y++)
  3439. for (x=-1; x <= 1; x++) {
  3440. shift = (y==0) + (x==0);
  3441. if (shift == 2) continue;
  3442. color = fc(row+y,col+x);
  3443. *ip++ = (width*y + x)*4 + color;
  3444. *ip++ = shift;
  3445. *ip++ = color;
  3446. sum[color] += 1 << shift;
  3447. }
  3448. FORCC
  3449. if (c != fc(row,col)) {
  3450. *ip++ = c;
  3451. *ip++ = 256 / sum[c];
  3452. }
  3453. }
  3454. #ifdef LIBRAW_LIBRARY_BUILD
  3455. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,1,3);
  3456. #endif
  3457. for (row=1; row < height-1; row++)
  3458. for (col=1; col < width-1; col++) {
  3459. pix = image[row*width+col];
  3460. ip = code[row & 15][col & 15];
  3461. memset (sum, 0, sizeof sum);
  3462. for (i=8; i--; ip+=3)
  3463. sum[ip[2]] += pix[ip[0]] << ip[1];
  3464. for (i=colors; --i; ip+=2)
  3465. pix[ip[0]] = sum[ip[0]] * ip[1] >> 8;
  3466. }
  3467. #ifdef LIBRAW_LIBRARY_BUILD
  3468. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,2,3);
  3469. #endif
  3470. }
  3471. /*
  3472. This algorithm is officially called:
  3473. "Interpolation using a Threshold-based variable number of gradients"
  3474. described in http://scien.stanford.edu/pages/labsite/1999/psych221/projects/99/tingchen/algodep/vargra.html
  3475. I've extended the basic idea to work with non-Bayer filter arrays.
  3476. Gradients are numbered clockwise from NW=0 to W=7.
  3477. */
  3478. void CLASS vng_interpolate()
  3479. {
  3480. static const signed char *cp, terms[] = {
  3481. -2,-2,+0,-1,0,0x01, -2,-2,+0,+0,1,0x01, -2,-1,-1,+0,0,0x01,
  3482. -2,-1,+0,-1,0,0x02, -2,-1,+0,+0,0,0x03, -2,-1,+0,+1,1,0x01,
  3483. -2,+0,+0,-1,0,0x06, -2,+0,+0,+0,1,0x02, -2,+0,+0,+1,0,0x03,
  3484. -2,+1,-1,+0,0,0x04, -2,+1,+0,-1,1,0x04, -2,+1,+0,+0,0,0x06,
  3485. -2,+1,+0,+1,0,0x02, -2,+2,+0,+0,1,0x04, -2,+2,+0,+1,0,0x04,
  3486. -1,-2,-1,+0,0,0x80, -1,-2,+0,-1,0,0x01, -1,-2,+1,-1,0,0x01,
  3487. -1,-2,+1,+0,1,0x01, -1,-1,-1,+1,0,0x88, -1,-1,+1,-2,0,0x40,
  3488. -1,-1,+1,-1,0,0x22, -1,-1,+1,+0,0,0x33, -1,-1,+1,+1,1,0x11,
  3489. -1,+0,-1,+2,0,0x08, -1,+0,+0,-1,0,0x44, -1,+0,+0,+1,0,0x11,
  3490. -1,+0,+1,-2,1,0x40, -1,+0,+1,-1,0,0x66, -1,+0,+1,+0,1,0x22,
  3491. -1,+0,+1,+1,0,0x33, -1,+0,+1,+2,1,0x10, -1,+1,+1,-1,1,0x44,
  3492. -1,+1,+1,+0,0,0x66, -1,+1,+1,+1,0,0x22, -1,+1,+1,+2,0,0x10,
  3493. -1,+2,+0,+1,0,0x04, -1,+2,+1,+0,1,0x04, -1,+2,+1,+1,0,0x04,
  3494. +0,-2,+0,+0,1,0x80, +0,-1,+0,+1,1,0x88, +0,-1,+1,-2,0,0x40,
  3495. +0,-1,+1,+0,0,0x11, +0,-1,+2,-2,0,0x40, +0,-1,+2,-1,0,0x20,
  3496. +0,-1,+2,+0,0,0x30, +0,-1,+2,+1,1,0x10, +0,+0,+0,+2,1,0x08,
  3497. +0,+0,+2,-2,1,0x40, +0,+0,+2,-1,0,0x60, +0,+0,+2,+0,1,0x20,
  3498. +0,+0,+2,+1,0,0x30, +0,+0,+2,+2,1,0x10, +0,+1,+1,+0,0,0x44,
  3499. +0,+1,+1,+2,0,0x10, +0,+1,+2,-1,1,0x40, +0,+1,+2,+0,0,0x60,
  3500. +0,+1,+2,+1,0,0x20, +0,+1,+2,+2,0,0x10, +1,-2,+1,+0,0,0x80,
  3501. +1,-1,+1,+1,0,0x88, +1,+0,+1,+2,0,0x08, +1,+0,+2,-1,0,0x40,
  3502. +1,+0,+2,+1,0,0x10
  3503. }, chood[] = { -1,-1, -1,0, -1,+1, 0,+1, +1,+1, +1,0, +1,-1, 0,-1 };
  3504. ushort (*brow[5])[4], *pix;
  3505. int prow=7, pcol=1, *ip, *code[16][16], gval[8], gmin, gmax, sum[4];
  3506. int row, col, x, y, x1, x2, y1, y2, t, weight, grads, color, diag;
  3507. int g, diff, thold, num, c;
  3508. lin_interpolate();
  3509. #ifdef DCRAW_VERBOSE
  3510. if (verbose) fprintf (stderr,_("VNG interpolation...\n"));
  3511. #endif
  3512. if (filters == 1) prow = pcol = 15;
  3513. ip = (int *) calloc ((prow+1)*(pcol+1), 1280);
  3514. merror (ip, "vng_interpolate()");
  3515. for (row=0; row <= prow; row++) /* Precalculate for VNG */
  3516. for (col=0; col <= pcol; col++) {
  3517. code[row][col] = ip;
  3518. for (cp=terms, t=0; t < 64; t++) {
  3519. y1 = *cp++; x1 = *cp++;
  3520. y2 = *cp++; x2 = *cp++;
  3521. weight = *cp++;
  3522. grads = *cp++;
  3523. color = fc(row+y1,col+x1);
  3524. if (fc(row+y2,col+x2) != color) continue;
  3525. diag = (fc(row,col+1) == color && fc(row+1,col) == color) ? 2:1;
  3526. if (abs(y1-y2) == diag && abs(x1-x2) == diag) continue;
  3527. *ip++ = (y1*width + x1)*4 + color;
  3528. *ip++ = (y2*width + x2)*4 + color;
  3529. *ip++ = weight;
  3530. for (g=0; g < 8; g++)
  3531. if (grads & 1<<g) *ip++ = g;
  3532. *ip++ = -1;
  3533. }
  3534. *ip++ = INT_MAX;
  3535. for (cp=chood, g=0; g < 8; g++) {
  3536. y = *cp++; x = *cp++;
  3537. *ip++ = (y*width + x) * 4;
  3538. color = fc(row,col);
  3539. if (fc(row+y,col+x) != color && fc(row+y*2,col+x*2) == color)
  3540. *ip++ = (y*width + x) * 8 + color;
  3541. else
  3542. *ip++ = 0;
  3543. }
  3544. }
  3545. brow[4] = (ushort (*)[4]) calloc (width*3, sizeof **brow);
  3546. merror (brow[4], "vng_interpolate()");
  3547. for (row=0; row < 3; row++)
  3548. brow[row] = brow[4] + row*width;
  3549. for (row=2; row < height-2; row++) { /* Do VNG interpolation */
  3550. #ifdef LIBRAW_LIBRARY_BUILD
  3551. if(!((row-2)%256))RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,(row-2)/256+1,((height-3)/256)+1);
  3552. #endif
  3553. for (col=2; col < width-2; col++) {
  3554. pix = image[row*width+col];
  3555. ip = code[row & prow][col & pcol];
  3556. memset (gval, 0, sizeof gval);
  3557. while ((g = ip[0]) != INT_MAX) { /* Calculate gradients */
  3558. diff = ABS(pix[g] - pix[ip[1]]) << ip[2];
  3559. gval[ip[3]] += diff;
  3560. ip += 5;
  3561. if ((g = ip[-1]) == -1) continue;
  3562. gval[g] += diff;
  3563. while ((g = *ip++) != -1)
  3564. gval[g] += diff;
  3565. }
  3566. ip++;
  3567. gmin = gmax = gval[0]; /* Choose a threshold */
  3568. for (g=1; g < 8; g++) {
  3569. if (gmin > gval[g]) gmin = gval[g];
  3570. if (gmax < gval[g]) gmax = gval[g];
  3571. }
  3572. if (gmax == 0) {
  3573. memcpy (brow[2][col], pix, sizeof *image);
  3574. continue;
  3575. }
  3576. thold = gmin + (gmax >> 1);
  3577. memset (sum, 0, sizeof sum);
  3578. color = fc(row,col);
  3579. for (num=g=0; g < 8; g++,ip+=2) { /* Average the neighbors */
  3580. if (gval[g] <= thold) {
  3581. FORCC
  3582. if (c == color && ip[1])
  3583. sum[c] += (pix[c] + pix[ip[1]]) >> 1;
  3584. else
  3585. sum[c] += pix[ip[0] + c];
  3586. num++;
  3587. }
  3588. }
  3589. FORCC { /* Save to buffer */
  3590. t = pix[color];
  3591. if (c != color)
  3592. t += (sum[c] - sum[color]) / num;
  3593. brow[2][col][c] = CLIP(t);
  3594. }
  3595. }
  3596. if (row > 3) /* Write buffer to image */
  3597. memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image);
  3598. for (g=0; g < 4; g++)
  3599. brow[(g-1) & 3] = brow[g];
  3600. }
  3601. memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image);
  3602. memcpy (image[(row-1)*width+2], brow[1]+2, (width-4)*sizeof *image);
  3603. free (brow[4]);
  3604. free (code[0][0]);
  3605. }
  3606. /*
  3607. Patterned Pixel Grouping Interpolation by Alain Desbiolles
  3608. */
  3609. void CLASS ppg_interpolate()
  3610. {
  3611. int dir[5] = { 1, width, -1, -width, 1 };
  3612. int row, col, diff[2], guess[2], c, d, i;
  3613. ushort (*pix)[4];
  3614. border_interpolate(3);
  3615. #ifdef DCRAW_VERBOSE
  3616. if (verbose) fprintf (stderr,_("PPG interpolation...\n"));
  3617. #endif
  3618. /* Fill in the green layer with gradients and pattern recognition: */
  3619. #ifdef LIBRAW_LIBRARY_BUILD
  3620. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,0,3);
  3621. #ifdef _OPENMP
  3622. #pragma omp parallel for default(shared) private(guess, diff, row, col, d, c, i, pix) schedule(static)
  3623. #endif
  3624. #endif
  3625. for (row=3; row < height-3; row++)
  3626. for (col=3+(FC(row,3) & 1), c=FC(row,col); col < width-3; col+=2) {
  3627. pix = image + row*width+col;
  3628. for (i=0; (d=dir[i]) > 0; i++) {
  3629. guess[i] = (pix[-d][1] + pix[0][c] + pix[d][1]) * 2
  3630. - pix[-2*d][c] - pix[2*d][c];
  3631. diff[i] = ( ABS(pix[-2*d][c] - pix[ 0][c]) +
  3632. ABS(pix[ 2*d][c] - pix[ 0][c]) +
  3633. ABS(pix[ -d][1] - pix[ d][1]) ) * 3 +
  3634. ( ABS(pix[ 3*d][1] - pix[ d][1]) +
  3635. ABS(pix[-3*d][1] - pix[-d][1]) ) * 2;
  3636. }
  3637. d = dir[i = diff[0] > diff[1]];
  3638. pix[0][1] = ULIM(guess[i] >> 2, pix[d][1], pix[-d][1]);
  3639. }
  3640. /* Calculate red and blue for each green pixel: */
  3641. #ifdef LIBRAW_LIBRARY_BUILD
  3642. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,1,3);
  3643. #ifdef _OPENMP
  3644. #pragma omp parallel for default(shared) private(guess, diff, row, col, d, c, i, pix) schedule(static)
  3645. #endif
  3646. #endif
  3647. for (row=1; row < height-1; row++)
  3648. for (col=1+(FC(row,2) & 1), c=FC(row,col+1); col < width-1; col+=2) {
  3649. pix = image + row*width+col;
  3650. for (i=0; (d=dir[i]) > 0; c=2-c, i++)
  3651. pix[0][c] = CLIP((pix[-d][c] + pix[d][c] + 2*pix[0][1]
  3652. - pix[-d][1] - pix[d][1]) >> 1);
  3653. }
  3654. /* Calculate blue for red pixels and vice versa: */
  3655. #ifdef LIBRAW_LIBRARY_BUILD
  3656. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,2,3);
  3657. #ifdef _OPENMP
  3658. #pragma omp parallel for default(shared) private(guess, diff, row, col, d, c, i, pix) schedule(static)
  3659. #endif
  3660. #endif
  3661. for (row=1; row < height-1; row++)
  3662. for (col=1+(FC(row,1) & 1), c=2-FC(row,col); col < width-1; col+=2) {
  3663. pix = image + row*width+col;
  3664. for (i=0; (d=dir[i]+dir[i+1]) > 0; i++) {
  3665. diff[i] = ABS(pix[-d][c] - pix[d][c]) +
  3666. ABS(pix[-d][1] - pix[0][1]) +
  3667. ABS(pix[ d][1] - pix[0][1]);
  3668. guess[i] = pix[-d][c] + pix[d][c] + 2*pix[0][1]
  3669. - pix[-d][1] - pix[d][1];
  3670. }
  3671. if (diff[0] != diff[1])
  3672. pix[0][c] = CLIP(guess[diff[0] > diff[1]] >> 1);
  3673. else
  3674. pix[0][c] = CLIP((guess[0]+guess[1]) >> 2);
  3675. }
  3676. }
  3677. /*
  3678. Adaptive Homogeneity-Directed interpolation is based on
  3679. the work of Keigo Hirakawa, Thomas Parks, and Paul Lee.
  3680. */
  3681. #define TS 256 /* Tile Size */
  3682. #ifndef _OPENMP
  3683. void CLASS ahd_interpolate()
  3684. {
  3685. int i, j, k, top, left, row, col, tr, tc, c, d, val, hm[2];
  3686. ushort (*pix)[4], (*rix)[3];
  3687. static const int dir[4] = { -1, 1, -TS, TS };
  3688. unsigned ldiff[2][4], abdiff[2][4], leps, abeps;
  3689. float r, cbrt[0x10000], xyz[3], xyz_cam[3][4];
  3690. ushort (*rgb)[TS][TS][3];
  3691. short (*lab)[TS][TS][3], (*lix)[3];
  3692. char (*homo)[TS][TS], *buffer;
  3693. #ifdef DCRAW_VERBOSE
  3694. if (verbose) fprintf (stderr,_("AHD interpolation...\n"));
  3695. #endif
  3696. for (i=0; i < 0x10000; i++) {
  3697. r = i / 65535.0;
  3698. cbrt[i] = r > 0.008856 ? pow((double)r,1/3.0) : 7.787*r + 16/116.0;
  3699. }
  3700. for (i=0; i < 3; i++)
  3701. for (j=0; j < colors; j++)
  3702. for (xyz_cam[i][j] = k=0; k < 3; k++)
  3703. xyz_cam[i][j] += xyz_rgb[i][k] * rgb_cam[k][j] / d65_white[i];
  3704. border_interpolate(5);
  3705. buffer = (char *) malloc (26*TS*TS); /* 1664 kB */
  3706. merror (buffer, "ahd_interpolate()");
  3707. rgb = (ushort(*)[TS][TS][3]) buffer;
  3708. lab = (short (*)[TS][TS][3])(buffer + 12*TS*TS);
  3709. homo = (char (*)[TS][TS]) (buffer + 24*TS*TS);
  3710. for (top=2; top < height-5; top += TS-6)
  3711. {
  3712. #ifdef LIBRAW_LIBRARY_BUILD
  3713. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,(top-2)/(TS-6),(height-7)/(TS-6)+1);
  3714. #endif
  3715. for (left=2; left < width-5; left += TS-6) {
  3716. /* Interpolate green horizontally and vertically: */
  3717. for (row = top; row < top+TS && row < height-2; row++) {
  3718. col = left + (FC(row,left) & 1);
  3719. for (c = FC(row,col); col < left+TS && col < width-2; col+=2) {
  3720. pix = image + row*width+col;
  3721. val = ((pix[-1][1] + pix[0][c] + pix[1][1]) * 2
  3722. - pix[-2][c] - pix[2][c]) >> 2;
  3723. rgb[0][row-top][col-left][1] = ULIM(val,pix[-1][1],pix[1][1]);
  3724. val = ((pix[-width][1] + pix[0][c] + pix[width][1]) * 2
  3725. - pix[-2*width][c] - pix[2*width][c]) >> 2;
  3726. rgb[1][row-top][col-left][1] = ULIM(val,pix[-width][1],pix[width][1]);
  3727. }
  3728. }
  3729. /* Interpolate red and blue, and convert to CIELab: */
  3730. for (d=0; d < 2; d++)
  3731. for (row=top+1; row < top+TS-1 && row < height-3; row++)
  3732. for (col=left+1; col < left+TS-1 && col < width-3; col++) {
  3733. pix = image + row*width+col;
  3734. rix = &rgb[d][row-top][col-left];
  3735. lix = &lab[d][row-top][col-left];
  3736. if ((c = 2 - FC(row,col)) == 1) {
  3737. c = FC(row+1,col);
  3738. val = pix[0][1] + (( pix[-1][2-c] + pix[1][2-c]
  3739. - rix[-1][1] - rix[1][1] ) >> 1);
  3740. rix[0][2-c] = CLIP(val);
  3741. val = pix[0][1] + (( pix[-width][c] + pix[width][c]
  3742. - rix[-TS][1] - rix[TS][1] ) >> 1);
  3743. } else
  3744. val = rix[0][1] + (( pix[-width-1][c] + pix[-width+1][c]
  3745. + pix[+width-1][c] + pix[+width+1][c]
  3746. - rix[-TS-1][1] - rix[-TS+1][1]
  3747. - rix[+TS-1][1] - rix[+TS+1][1] + 1) >> 2);
  3748. rix[0][c] = CLIP(val);
  3749. c = FC(row,col);
  3750. rix[0][c] = pix[0][c];
  3751. xyz[0] = xyz[1] = xyz[2] = 0.5;
  3752. FORCC {
  3753. xyz[0] += xyz_cam[0][c] * rix[0][c];
  3754. xyz[1] += xyz_cam[1][c] * rix[0][c];
  3755. xyz[2] += xyz_cam[2][c] * rix[0][c];
  3756. }
  3757. xyz[0] = cbrt[CLIP((int) xyz[0])];
  3758. xyz[1] = cbrt[CLIP((int) xyz[1])];
  3759. xyz[2] = cbrt[CLIP((int) xyz[2])];
  3760. lix[0][0] = 64 * (116 * xyz[1] - 16);
  3761. lix[0][1] = 64 * 500 * (xyz[0] - xyz[1]);
  3762. lix[0][2] = 64 * 200 * (xyz[1] - xyz[2]);
  3763. }
  3764. /* Build homogeneity maps from the CIELab images: */
  3765. memset (homo, 0, 2*TS*TS);
  3766. for (row=top+2; row < top+TS-2 && row < height-4; row++) {
  3767. tr = row-top;
  3768. for (col=left+2; col < left+TS-2 && col < width-4; col++) {
  3769. tc = col-left;
  3770. for (d=0; d < 2; d++) {
  3771. lix = &lab[d][tr][tc];
  3772. for (i=0; i < 4; i++) {
  3773. ldiff[d][i] = ABS(lix[0][0]-lix[dir[i]][0]);
  3774. abdiff[d][i] = SQR(lix[0][1]-lix[dir[i]][1])
  3775. + SQR(lix[0][2]-lix[dir[i]][2]);
  3776. }
  3777. }
  3778. leps = MIN(MAX(ldiff[0][0],ldiff[0][1]),
  3779. MAX(ldiff[1][2],ldiff[1][3]));
  3780. abeps = MIN(MAX(abdiff[0][0],abdiff[0][1]),
  3781. MAX(abdiff[1][2],abdiff[1][3]));
  3782. for (d=0; d < 2; d++)
  3783. for (i=0; i < 4; i++)
  3784. if (ldiff[d][i] <= leps && abdiff[d][i] <= abeps)
  3785. homo[d][tr][tc]++;
  3786. }
  3787. }
  3788. /* Combine the most homogenous pixels for the final result: */
  3789. for (row=top+3; row < top+TS-3 && row < height-5; row++) {
  3790. tr = row-top;
  3791. for (col=left+3; col < left+TS-3 && col < width-5; col++) {
  3792. tc = col-left;
  3793. for (d=0; d < 2; d++)
  3794. for (hm[d]=0, i=tr-1; i <= tr+1; i++)
  3795. for (j=tc-1; j <= tc+1; j++)
  3796. hm[d] += homo[d][i][j];
  3797. if (hm[0] != hm[1])
  3798. FORC3 image[row*width+col][c] = rgb[hm[1] > hm[0]][tr][tc][c];
  3799. else
  3800. FORC3 image[row*width+col][c] =
  3801. (rgb[0][tr][tc][c] + rgb[1][tr][tc][c]) >> 1;
  3802. }
  3803. }
  3804. }
  3805. }
  3806. free (buffer);
  3807. }
  3808. #else
  3809. void CLASS ahd_interpolate()
  3810. {
  3811. int i, j, k, top, left, row, col, tr, tc, c, d, val, hm[2];
  3812. ushort (*pix)[4], (*rix)[3];
  3813. static const int dir[4] = { -1, 1, -TS, TS };
  3814. unsigned ldiff[2][4], abdiff[2][4], leps, abeps;
  3815. float r, cbrt[0x10000], xyz[3], xyz_cam[3][4];
  3816. ushort (*rgb)[TS][TS][3];
  3817. short (*lab)[TS][TS][3], (*lix)[3];
  3818. char (*homo)[TS][TS], *buffer;
  3819. #ifdef DCRAW_VERBOSE
  3820. if (verbose) fprintf (stderr,_("AHD interpolation...\n"));
  3821. #endif
  3822. for (i=0; i < 0x10000; i++) {
  3823. r = i / 65535.0;
  3824. cbrt[i] = r > 0.008856 ? pow((double)r,1/3.0) : 7.787*r + 16/116.0;
  3825. }
  3826. for (i=0; i < 3; i++)
  3827. for (j=0; j < colors; j++)
  3828. for (xyz_cam[i][j] = k=0; k < 3; k++)
  3829. xyz_cam[i][j] += xyz_rgb[i][k] * rgb_cam[k][j] / d65_white[i];
  3830. border_interpolate(5);
  3831. #ifdef LIBRAW_LIBRARY_BUILD
  3832. #pragma omp parallel default(shared) private(buffer,rgb,lab,homo,top,left,row,c,col,pix,val,d,rix,xyz,lix,tc,tr,ldiff,abdiff,leps,abeps,hm,i,j) firstprivate(cbrt) shared(xyz_cam)
  3833. #endif
  3834. {
  3835. buffer = (char *) malloc (26*TS*TS); /* 1664 kB */
  3836. merror (buffer, "ahd_interpolate()");
  3837. rgb = (ushort(*)[TS][TS][3]) buffer;
  3838. lab = (short (*)[TS][TS][3])(buffer + 12*TS*TS);
  3839. homo = (char (*)[TS][TS]) (buffer + 24*TS*TS);
  3840. #pragma omp for schedule(dynamic)
  3841. for (top=2; top < height-5; top += TS-6){
  3842. #ifdef LIBRAW_LIBRARY_BUILD
  3843. RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,(top-2)/(TS-6)+1,(height-7)/(TS-6)+1);
  3844. #endif
  3845. for (left=2; left < width-5; left += TS-6) {
  3846. /* Interpolate green horizontally and vertically: */
  3847. for (row = top; row < top+TS && row < height-2; row++) {
  3848. col = left + (FC(row,left) & 1);
  3849. for (c = FC(row,col); col < left+TS && col < width-2; col+=2) {
  3850. pix = image + row*width+col;
  3851. val = ((pix[-1][1] + pix[0][c] + pix[1][1]) * 2
  3852. - pix[-2][c] - pix[2][c]) >> 2;
  3853. rgb[0][row-top][col-left][1] = ULIM(val,pix[-1][1],pix[1][1]);
  3854. val = ((pix[-width][1] + pix[0][c] + pix[width][1]) * 2
  3855. - pix[-2*width][c] - pix[2*width][c]) >> 2;
  3856. rgb[1][row-top][col-left][1] = ULIM(val,pix[-width][1],pix[width][1]);
  3857. }
  3858. }
  3859. /* Interpolate red and blue, and convert to CIELab: */
  3860. for (d=0; d < 2; d++)
  3861. for (row=top+1; row < top+TS-1 && row < height-3; row++)
  3862. for (col=left+1; col < left+TS-1 && col < width-3; col++) {
  3863. pix = image + row*width+col;
  3864. rix = &rgb[d][row-top][col-left];
  3865. lix = &lab[d][row-top][col-left];
  3866. if ((c = 2 - FC(row,col)) == 1) {
  3867. c = FC(row+1,col);
  3868. val = pix[0][1] + (( pix[-1][2-c] + pix[1][2-c]
  3869. - rix[-1][1] - rix[1][1] ) >> 1);
  3870. rix[0][2-c] = CLIP(val);
  3871. val = pix[0][1] + (( pix[-width][c] + pix[width][c]
  3872. - rix[-TS][1] - rix[TS][1] ) >> 1);
  3873. } else
  3874. val = rix[0][1] + (( pix[-width-1][c] + pix[-width+1][c]
  3875. + pix[+width-1][c] + pix[+width+1][c]
  3876. - rix[-TS-1][1] - rix[-TS+1][1]
  3877. - rix[+TS-1][1] - rix[+TS+1][1] + 1) >> 2);
  3878. rix[0][c] = CLIP(val);
  3879. c = FC(row,col);
  3880. rix[0][c] = pix[0][c];
  3881. xyz[0] = xyz[1] = xyz[2] = 0.5;
  3882. FORCC {
  3883. xyz[0] += xyz_cam[0][c] * rix[0][c];
  3884. xyz[1] += xyz_cam[1][c] * rix[0][c];
  3885. xyz[2] += xyz_cam[2][c] * rix[0][c];
  3886. }
  3887. xyz[0] = cbrt[CLIP((int) xyz[0])];
  3888. xyz[1] = cbrt[CLIP((int) xyz[1])];
  3889. xyz[2] = cbrt[CLIP((int) xyz[2])];
  3890. lix[0][0] = 64 * (116 * xyz[1] - 16);
  3891. lix[0][1] = 64 * 500 * (xyz[0] - xyz[1]);
  3892. lix[0][2] = 64 * 200 * (xyz[1] - xyz[2]);
  3893. }
  3894. /* Build homogeneity maps from the CIELab images: */
  3895. memset (homo, 0, 2*TS*TS);
  3896. for (row=top+2; row < top+TS-2 && row < height-4; row++) {
  3897. tr = row-top;
  3898. for (col=left+2; col < left+TS-2 && col < width-4; col++) {
  3899. tc = col-left;
  3900. for (d=0; d < 2; d++) {
  3901. lix = &lab[d][tr][tc];
  3902. for (i=0; i < 4; i++) {
  3903. ldiff[d][i] = ABS(lix[0][0]-lix[dir[i]][0]);
  3904. abdiff[d][i] = SQR(lix[0][1]-lix[dir[i]][1])
  3905. + SQR(lix[0][2]-lix[dir[i]][2]);
  3906. }
  3907. }
  3908. leps = MIN(MAX(ldiff[0][0],ldiff[0][1]),
  3909. MAX(ldiff[1][2],ldiff[1][3]));
  3910. abeps = MIN(MAX(abdiff[0][0],abdiff[0][1]),
  3911. MAX(abdiff[1][2],abdiff[1][3]));
  3912. for (d=0; d < 2; d++)
  3913. for (i=0; i < 4; i++)
  3914. if (ldiff[d][i] <= leps && abdiff[d][i] <= abeps)
  3915. homo[d][tr][tc]++;
  3916. }
  3917. }
  3918. /* Combine the most homogenous pixels for the final result: */
  3919. for (row=top+3; row < top+TS-3 && row < height-5; row++) {
  3920. tr = row-top;
  3921. for (col=left+3; col < left+TS-3 && col < width-5; col++) {
  3922. tc = col-left;
  3923. for (d=0; d < 2; d++)
  3924. for (hm[d]=0, i=tr-1; i <= tr+1; i++)
  3925. for (j=tc-1; j <= tc+1; j++)
  3926. hm[d] += homo[d][i][j];
  3927. if (hm[0] != hm[1])
  3928. FORC3 image[row*width+col][c] = rgb[hm[1] > hm[0]][tr][tc][c];
  3929. else
  3930. FORC3 image[row*width+col][c] =
  3931. (rgb[0][tr][tc][c] + rgb[1][tr][tc][c]) >> 1;
  3932. }
  3933. }
  3934. }
  3935. }
  3936. free (buffer);
  3937. }
  3938. }
  3939. #endif
  3940. #undef TS
  3941. void CLASS median_filter()
  3942. {
  3943. ushort (*pix)[4];
  3944. int pass, c, i, j, k, med[9];
  3945. static const uchar opt[] = /* Optimal 9-element median search */
  3946. { 1,2, 4,5, 7,8, 0,1, 3,4, 6,7, 1,2, 4,5, 7,8,
  3947. 0,3, 5,8, 4,7, 3,6, 1,4, 2,5, 4,7, 4,2, 6,4, 4,2 };
  3948. for (pass=1; pass <= med_passes; pass++) {
  3949. #ifdef LIBRAW_LIBRARY_BUILD
  3950. RUN_CALLBACK(LIBRAW_PROGRESS_MEDIAN_FILTER,pass-1,med_passes);
  3951. #endif
  3952. #ifdef DCRAW_VERBOSE
  3953. if (verbose)
  3954. fprintf (stderr,_("Median filter pass %d...\n"), pass);
  3955. #endif
  3956. for (c=0; c < 3; c+=2) {
  3957. for (pix = image; pix < image+width*height; pix++)
  3958. pix[0][3] = pix[0][c];
  3959. for (pix = image+width; pix < image+width*(height-1); pix++) {
  3960. if ((pix-image+1) % width < 2) continue;
  3961. for (k=0, i = -width; i <= width; i += width)
  3962. for (j = i-1; j <= i+1; j++)
  3963. med[k++] = pix[j][3] - pix[j][1];
  3964. for (i=0; i < sizeof opt; i+=2)
  3965. if (med[opt[i]] > med[opt[i+1]])
  3966. SWAP (med[opt[i]] , med[opt[i+1]]);
  3967. pix[0][c] = CLIP(med[4] + pix[0][1]);
  3968. }
  3969. }
  3970. }
  3971. }
  3972. void CLASS blend_highlights()
  3973. {
  3974. int clip=INT_MAX, row, col, c, i, j;
  3975. static const float trans[2][4][4] =
  3976. { { { 1,1,1 }, { 1.7320508,-1.7320508,0 }, { -1,-1,2 } },
  3977. { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } };
  3978. static const float itrans[2][4][4] =
  3979. { { { 1,0.8660254,-0.5 }, { 1,-0.8660254,-0.5 }, { 1,0,1 } },
  3980. { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } };
  3981. float cam[2][4], lab[2][4], sum[2], chratio;
  3982. if ((unsigned) (colors-3) > 1) return;
  3983. #ifdef DCRAW_VERBOSE
  3984. if (verbose) fprintf (stderr,_("Blending highlights...\n"));
  3985. #endif
  3986. FORCC if (clip > (i = 65535*pre_mul[c])) clip = i;
  3987. #ifdef LIBRAW_LIBRARY_BUILD
  3988. RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS,0,2);
  3989. #endif
  3990. for (row=0; row < height; row++)
  3991. for (col=0; col < width; col++) {
  3992. FORCC if (image[row*width+col][c] > clip) break;
  3993. if (c == colors) continue;
  3994. FORCC {
  3995. cam[0][c] = image[row*width+col][c];
  3996. cam[1][c] = MIN(cam[0][c],clip);
  3997. }
  3998. for (i=0; i < 2; i++) {
  3999. FORCC for (lab[i][c]=j=0; j < colors; j++)
  4000. lab[i][c] += trans[colors-3][c][j] * cam[i][j];
  4001. for (sum[i]=0,c=1; c < colors; c++)
  4002. sum[i] += SQR(lab[i][c]);
  4003. }
  4004. chratio = sqrt(sum[1]/sum[0]);
  4005. for (c=1; c < colors; c++)
  4006. lab[0][c] *= chratio;
  4007. FORCC for (cam[0][c]=j=0; j < colors; j++)
  4008. cam[0][c] += itrans[colors-3][c][j] * lab[0][j];
  4009. FORCC image[row*width+col][c] = cam[0][c] / colors;
  4010. }
  4011. #ifdef LIBRAW_LIBRARY_BUILD
  4012. RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS,1,2);
  4013. #endif
  4014. }
  4015. #define SCALE (4 >> shrink)
  4016. void CLASS recover_highlights()
  4017. {
  4018. float *map, sum, wgt, grow;
  4019. int hsat[4], count, spread, change, val, i;
  4020. unsigned high, wide, mrow, mcol, row, col, kc, c, d, y, x;
  4021. ushort *pixel;
  4022. static const signed char dir[8][2] =
  4023. { {-1,-1}, {-1,0}, {-1,1}, {0,1}, {1,1}, {1,0}, {1,-1}, {0,-1} };
  4024. #ifdef DCRAW_VERBOSE
  4025. if (verbose) fprintf (stderr,_("Rebuilding highlights...\n"));
  4026. #endif
  4027. grow = pow (2.0, 4.0-highlight);
  4028. FORCC hsat[c] = 32000 * pre_mul[c];
  4029. for (kc=0, c=1; c < colors; c++)
  4030. if (pre_mul[kc] < pre_mul[c]) kc = c;
  4031. high = height / SCALE;
  4032. wide = width / SCALE;
  4033. map = (float *) calloc (high*wide, sizeof *map);
  4034. merror (map, "recover_highlights()");
  4035. FORCC if (c != kc) {
  4036. #ifdef LIBRAW_LIBRARY_BUILD
  4037. RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS,c-1,colors-1);
  4038. #endif
  4039. memset (map, 0, high*wide*sizeof *map);
  4040. for (mrow=0; mrow < high; mrow++)
  4041. for (mcol=0; mcol < wide; mcol++) {
  4042. sum = wgt = count = 0;
  4043. for (row = mrow*SCALE; row < (mrow+1)*SCALE; row++)
  4044. for (col = mcol*SCALE; col < (mcol+1)*SCALE; col++) {
  4045. pixel = image[row*width+col];
  4046. if (pixel[c] / hsat[c] == 1 && pixel[kc] > 24000) {
  4047. sum += pixel[c];
  4048. wgt += pixel[kc];
  4049. count++;
  4050. }
  4051. }
  4052. if (count == SCALE*SCALE)
  4053. map[mrow*wide+mcol] = sum / wgt;
  4054. }
  4055. for (spread = 32/grow; spread--; ) {
  4056. for (mrow=0; mrow < high; mrow++)
  4057. for (mcol=0; mcol < wide; mcol++) {
  4058. if (map[mrow*wide+mcol]) continue;
  4059. sum = count = 0;
  4060. for (d=0; d < 8; d++) {
  4061. y = mrow + dir[d][0];
  4062. x = mcol + dir[d][1];
  4063. if (y < high && x < wide && map[y*wide+x] > 0) {
  4064. sum += (1 + (d & 1)) * map[y*wide+x];
  4065. count += 1 + (d & 1);
  4066. }
  4067. }
  4068. if (count > 3)
  4069. map[mrow*wide+mcol] = - (sum+grow) / (count+grow);
  4070. }
  4071. for (change=i=0; i < high*wide; i++)
  4072. if (map[i] < 0) {
  4073. map[i] = -map[i];
  4074. change = 1;
  4075. }
  4076. if (!change) break;
  4077. }
  4078. for (i=0; i < high*wide; i++)
  4079. if (map[i] == 0) map[i] = 1;
  4080. for (mrow=0; mrow < high; mrow++)
  4081. for (mcol=0; mcol < wide; mcol++) {
  4082. for (row = mrow*SCALE; row < (mrow+1)*SCALE; row++)
  4083. for (col = mcol*SCALE; col < (mcol+1)*SCALE; col++) {
  4084. pixel = image[row*width+col];
  4085. if (pixel[c] / hsat[c] > 1) {
  4086. val = pixel[kc] * map[mrow*wide+mcol];
  4087. if (pixel[c] < val) pixel[c] = CLIP(val);
  4088. }
  4089. }
  4090. }
  4091. }
  4092. free (map);
  4093. }
  4094. #undef SCALE
  4095. void CLASS tiff_get (unsigned base,
  4096. unsigned *tag, unsigned *type, unsigned *len, unsigned *save)
  4097. {
  4098. *tag = get2();
  4099. *type = get2();
  4100. *len = get4();
  4101. *save = ftell(ifp) + 4;
  4102. if (*len * ("11124811248488"[*type < 14 ? *type:0]-'0') > 4)
  4103. fseek (ifp, get4()+base, SEEK_SET);
  4104. }
  4105. void CLASS parse_thumb_note (int base, unsigned toff, unsigned tlen)
  4106. {
  4107. unsigned entries, tag, type, len, save;
  4108. entries = get2();
  4109. while (entries--) {
  4110. tiff_get (base, &tag, &type, &len, &save);
  4111. if (tag == toff) thumb_offset = get4()+base;
  4112. if (tag == tlen) thumb_length = get4();
  4113. fseek (ifp, save, SEEK_SET);
  4114. }
  4115. }
  4116. #line 5459 "dcraw/dcraw.c"
  4117. void CLASS parse_makernote (int base, int uptag)
  4118. {
  4119. static const uchar xlat[2][256] = {
  4120. { 0xc1,0xbf,0x6d,0x0d,0x59,0xc5,0x13,0x9d,0x83,0x61,0x6b,0x4f,0xc7,0x7f,0x3d,0x3d,
  4121. 0x53,0x59,0xe3,0xc7,0xe9,0x2f,0x95,0xa7,0x95,0x1f,0xdf,0x7f,0x2b,0x29,0xc7,0x0d,
  4122. 0xdf,0x07,0xef,0x71,0x89,0x3d,0x13,0x3d,0x3b,0x13,0xfb,0x0d,0x89,0xc1,0x65,0x1f,
  4123. 0xb3,0x0d,0x6b,0x29,0xe3,0xfb,0xef,0xa3,0x6b,0x47,0x7f,0x95,0x35,0xa7,0x47,0x4f,
  4124. 0xc7,0xf1,0x59,0x95,0x35,0x11,0x29,0x61,0xf1,0x3d,0xb3,0x2b,0x0d,0x43,0x89,0xc1,
  4125. 0x9d,0x9d,0x89,0x65,0xf1,0xe9,0xdf,0xbf,0x3d,0x7f,0x53,0x97,0xe5,0xe9,0x95,0x17,
  4126. 0x1d,0x3d,0x8b,0xfb,0xc7,0xe3,0x67,0xa7,0x07,0xf1,0x71,0xa7,0x53,0xb5,0x29,0x89,
  4127. 0xe5,0x2b,0xa7,0x17,0x29,0xe9,0x4f,0xc5,0x65,0x6d,0x6b,0xef,0x0d,0x89,0x49,0x2f,
  4128. 0xb3,0x43,0x53,0x65,0x1d,0x49,0xa3,0x13,0x89,0x59,0xef,0x6b,0xef,0x65,0x1d,0x0b,
  4129. 0x59,0x13,0xe3,0x4f,0x9d,0xb3,0x29,0x43,0x2b,0x07,0x1d,0x95,0x59,0x59,0x47,0xfb,
  4130. 0xe5,0xe9,0x61,0x47,0x2f,0x35,0x7f,0x17,0x7f,0xef,0x7f,0x95,0x95,0x71,0xd3,0xa3,
  4131. 0x0b,0x71,0xa3,0xad,0x0b,0x3b,0xb5,0xfb,0xa3,0xbf,0x4f,0x83,0x1d,0xad,0xe9,0x2f,
  4132. 0x71,0x65,0xa3,0xe5,0x07,0x35,0x3d,0x0d,0xb5,0xe9,0xe5,0x47,0x3b,0x9d,0xef,0x35,
  4133. 0xa3,0xbf,0xb3,0xdf,0x53,0xd3,0x97,0x53,0x49,0x71,0x07,0x35,0x61,0x71,0x2f,0x43,
  4134. 0x2f,0x11,0xdf,0x17,0x97,0xfb,0x95,0x3b,0x7f,0x6b,0xd3,0x25,0xbf,0xad,0xc7,0xc5,
  4135. 0xc5,0xb5,0x8b,0xef,0x2f,0xd3,0x07,0x6b,0x25,0x49,0x95,0x25,0x49,0x6d,0x71,0xc7 },
  4136. { 0xa7,0xbc,0xc9,0xad,0x91,0xdf,0x85,0xe5,0xd4,0x78,0xd5,0x17,0x46,0x7c,0x29,0x4c,
  4137. 0x4d,0x03,0xe9,0x25,0x68,0x11,0x86,0xb3,0xbd,0xf7,0x6f,0x61,0x22,0xa2,0x26,0x34,
  4138. 0x2a,0xbe,0x1e,0x46,0x14,0x68,0x9d,0x44,0x18,0xc2,0x40,0xf4,0x7e,0x5f,0x1b,0xad,
  4139. 0x0b,0x94,0xb6,0x67,0xb4,0x0b,0xe1,0xea,0x95,0x9c,0x66,0xdc,0xe7,0x5d,0x6c,0x05,
  4140. 0xda,0xd5,0xdf,0x7a,0xef,0xf6,0xdb,0x1f,0x82,0x4c,0xc0,0x68,0x47,0xa1,0xbd,0xee,
  4141. 0x39,0x50,0x56,0x4a,0xdd,0xdf,0xa5,0xf8,0xc6,0xda,0xca,0x90,0xca,0x01,0x42,0x9d,
  4142. 0x8b,0x0c,0x73,0x43,0x75,0x05,0x94,0xde,0x24,0xb3,0x80,0x34,0xe5,0x2c,0xdc,0x9b,
  4143. 0x3f,0xca,0x33,0x45,0xd0,0xdb,0x5f,0xf5,0x52,0xc3,0x21,0xda,0xe2,0x22,0x72,0x6b,
  4144. 0x3e,0xd0,0x5b,0xa8,0x87,0x8c,0x06,0x5d,0x0f,0xdd,0x09,0x19,0x93,0xd0,0xb9,0xfc,
  4145. 0x8b,0x0f,0x84,0x60,0x33,0x1c,0x9b,0x45,0xf1,0xf0,0xa3,0x94,0x3a,0x12,0x77,0x33,
  4146. 0x4d,0x44,0x78,0x28,0x3c,0x9e,0xfd,0x65,0x57,0x16,0x94,0x6b,0xfb,0x59,0xd0,0xc8,
  4147. 0x22,0x36,0xdb,0xd2,0x63,0x98,0x43,0xa1,0x04,0x87,0x86,0xf7,0xa6,0x26,0xbb,0xd6,
  4148. 0x59,0x4d,0xbf,0x6a,0x2e,0xaa,0x2b,0xef,0xe6,0x78,0xb6,0x4e,0xe0,0x2f,0xdc,0x7c,
  4149. 0xbe,0x57,0x19,0x32,0x7e,0x2a,0xd0,0xb8,0xba,0x29,0x00,0x3c,0x52,0x7d,0xa8,0x49,
  4150. 0x3b,0x2d,0xeb,0x25,0x49,0xfa,0xa3,0xaa,0x39,0xa7,0xc5,0xa7,0x50,0x11,0x36,0xfb,
  4151. 0xc6,0x67,0x4a,0xf5,0xa5,0x12,0x65,0x7e,0xb0,0xdf,0xaf,0x4e,0xb3,0x61,0x7f,0x2f } };
  4152. unsigned offset=0, entries, tag, type, len, save, c;
  4153. unsigned ver97=0, serial=0, i, wbi=0, wb[4]={0,0,0,0};
  4154. uchar buf97[324], ci, cj, ck;
  4155. short sorder=order;
  4156. char buf[10];
  4157. /*
  4158. The MakerNote might have its own TIFF header (possibly with
  4159. its own byte-order!), or it might just be a table.
  4160. */
  4161. fread (buf, 1, 10, ifp);
  4162. if (!strncmp (buf,"KDK" ,3) || /* these aren't TIFF tables */
  4163. !strncmp (buf,"VER" ,3) ||
  4164. !strncmp (buf,"IIII",4) ||
  4165. !strncmp (buf,"MMMM",4)) return;
  4166. if (!strncmp (buf,"KC" ,2) || /* Konica KD-400Z, KD-510Z */
  4167. !strncmp (buf,"MLY" ,3)) { /* Minolta DiMAGE G series */
  4168. order = 0x4d4d;
  4169. while ((i=ftell(ifp)) < data_offset && i < 16384) {
  4170. wb[0] = wb[2]; wb[2] = wb[1]; wb[1] = wb[3];
  4171. wb[3] = get2();
  4172. if (wb[1] == 256 && wb[3] == 256 &&
  4173. wb[0] > 256 && wb[0] < 640 && wb[2] > 256 && wb[2] < 640)
  4174. FORC4 cam_mul[c] = wb[c];
  4175. #ifdef LIBRAW_LIBRARY_BUILD
  4176. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4177. #endif
  4178. }
  4179. goto quit;
  4180. }
  4181. if (!strcmp (buf,"Nikon")) {
  4182. base = ftell(ifp);
  4183. order = get2();
  4184. if (get2() != 42) goto quit;
  4185. offset = get4();
  4186. fseek (ifp, offset-8, SEEK_CUR);
  4187. } else if (!strcmp (buf,"OLYMPUS")) {
  4188. base = ftell(ifp)-10;
  4189. fseek (ifp, -2, SEEK_CUR);
  4190. order = get2(); get2();
  4191. } else if (!strncmp (buf,"FUJIFILM",8) ||
  4192. !strncmp (buf,"SONY",4) ||
  4193. !strcmp (buf,"Panasonic")) {
  4194. order = 0x4949;
  4195. fseek (ifp, 2, SEEK_CUR);
  4196. } else if (!strcmp (buf,"OLYMP") ||
  4197. !strcmp (buf,"LEICA") ||
  4198. !strcmp (buf,"Ricoh") ||
  4199. !strcmp (buf,"EPSON"))
  4200. fseek (ifp, -2, SEEK_CUR);
  4201. else if (!strcmp (buf,"AOC") ||
  4202. !strcmp (buf,"QVC"))
  4203. fseek (ifp, -4, SEEK_CUR);
  4204. else fseek (ifp, -10, SEEK_CUR);
  4205. entries = get2();
  4206. if (entries > 1000) return;
  4207. while (entries--) {
  4208. tiff_get (base, &tag, &type, &len, &save);
  4209. tag |= uptag << 16;
  4210. if (tag == 2 && strstr(make,"NIKON"))
  4211. iso_speed = (get2(),get2());
  4212. if (tag == 4 && len > 26 && len < 35) {
  4213. if ((i=(get4(),get2())) != 0x7fff && !iso_speed)
  4214. iso_speed = 50 * pow (2, i/32.0 - 4);
  4215. if ((i=(get2(),get2())) != 0x7fff && !aperture)
  4216. aperture = pow (2, i/64.0);
  4217. if ((i=get2()) != 0xffff && !shutter)
  4218. shutter = pow (2, (short) i/-32.0);
  4219. wbi = (get2(),get2());
  4220. shot_order = (get2(),get2());
  4221. }
  4222. if ((tag == 4 || tag == 0x114) && !strncmp(make,"KONICA",6)) {
  4223. fseek (ifp, tag == 4 ? 140:160, SEEK_CUR);
  4224. switch (get2()) {
  4225. case 72: flip = 0; break;
  4226. case 76: flip = 6; break;
  4227. case 82: flip = 5; break;
  4228. }
  4229. }
  4230. if (tag == 7 && type == 2 && len > 20)
  4231. fgets (model2, 64, ifp);
  4232. if (tag == 8 && type == 4)
  4233. shot_order = get4();
  4234. if (tag == 9 && !strcmp(make,"Canon"))
  4235. fread (artist, 64, 1, ifp);
  4236. if (tag == 0xc && len == 4) {
  4237. cam_mul[0] = getreal(type);
  4238. cam_mul[2] = getreal(type);
  4239. #ifdef LIBRAW_LIBRARY_BUILD
  4240. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4241. #endif
  4242. }
  4243. if (tag == 0xd && type == 7 && get2() == 0xaaaa) {
  4244. fread (buf97, 1, sizeof buf97, ifp);
  4245. char *pp = (char*)memmem ((char*)buf97, sizeof buf97,(char*)"\xbb\xbb",2);
  4246. i = ((uchar*)pp - buf97) + 10;
  4247. if (i < 70 && buf97[i] < 3)
  4248. flip = "065"[buf97[i]]-'0';
  4249. }
  4250. if (tag == 0x10 && type == 4)
  4251. unique_id = get4();
  4252. if (tag == 0x11 && is_raw && !strncmp(make,"NIKON",5)) {
  4253. fseek (ifp, get4()+base, SEEK_SET);
  4254. parse_tiff_ifd (base);
  4255. }
  4256. if (tag == 0x14 && len == 2560 && type == 7) {
  4257. fseek (ifp, 1248, SEEK_CUR);
  4258. goto get2_256;
  4259. }
  4260. if (tag == 0x15 && type == 2 && is_raw)
  4261. fread (model, 64, 1, ifp);
  4262. if (strstr(make,"PENTAX")) {
  4263. if (tag == 0x1b) tag = 0x1018;
  4264. if (tag == 0x1c) tag = 0x1017;
  4265. }
  4266. if (tag == 0x1d)
  4267. while ((c = fgetc(ifp)) && c != EOF)
  4268. serial = serial*10 + (isdigit(c) ? c - '0' : c % 10);
  4269. if (tag == 0x81 && type == 4) {
  4270. data_offset = get4();
  4271. fseek (ifp, data_offset + 41, SEEK_SET);
  4272. raw_height = get2() * 2;
  4273. raw_width = get2();
  4274. filters = 0x61616161;
  4275. }
  4276. if (tag == 0x29 && type == 1) {
  4277. c = wbi < 18 ? "012347800000005896"[wbi]-'0' : 0;
  4278. fseek (ifp, 8 + c*32, SEEK_CUR);
  4279. FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get4();
  4280. #ifdef LIBRAW_LIBRARY_BUILD
  4281. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4282. #endif
  4283. }
  4284. if ((tag == 0x81 && type == 7) ||
  4285. (tag == 0x100 && type == 7) ||
  4286. (tag == 0x280 && type == 1)) {
  4287. thumb_offset = ftell(ifp);
  4288. thumb_length = len;
  4289. }
  4290. if (tag == 0x88 && type == 4 && (thumb_offset = get4()))
  4291. thumb_offset += base;
  4292. if (tag == 0x89 && type == 4)
  4293. thumb_length = get4();
  4294. if (tag == 0x8c || tag == 0x96)
  4295. meta_offset = ftell(ifp);
  4296. if (tag == 0x97) {
  4297. for (i=0; i < 4; i++)
  4298. ver97 = ver97 * 10 + fgetc(ifp)-'0';
  4299. switch (ver97) {
  4300. case 100:
  4301. fseek (ifp, 68, SEEK_CUR);
  4302. FORC4 cam_mul[(c >> 1) | ((c & 1) << 1)] = get2();
  4303. #ifdef LIBRAW_LIBRARY_BUILD
  4304. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4305. #endif
  4306. break;
  4307. case 102:
  4308. fseek (ifp, 6, SEEK_CUR);
  4309. goto get2_rggb;
  4310. case 103:
  4311. fseek (ifp, 16, SEEK_CUR);
  4312. FORC4 cam_mul[c] = get2();
  4313. #ifdef LIBRAW_LIBRARY_BUILD
  4314. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4315. #endif
  4316. }
  4317. if (ver97 >= 200) {
  4318. if (ver97 != 205) fseek (ifp, 280, SEEK_CUR);
  4319. fread (buf97, 324, 1, ifp);
  4320. }
  4321. }
  4322. if (tag == 0xa4 && type == 3) {
  4323. fseek (ifp, wbi*48, SEEK_CUR);
  4324. FORC3 cam_mul[c] = get2();
  4325. #ifdef LIBRAW_LIBRARY_BUILD
  4326. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4327. #endif
  4328. }
  4329. if (tag == 0xa7 && (unsigned) (ver97-200) < 12 && !cam_mul[0]) {
  4330. ci = xlat[0][serial & 0xff];
  4331. cj = xlat[1][fgetc(ifp)^fgetc(ifp)^fgetc(ifp)^fgetc(ifp)];
  4332. ck = 0x60;
  4333. for (i=0; i < 324; i++)
  4334. buf97[i] ^= (cj += ci * ck++);
  4335. i = "66666>666;6A"[ver97-200] - '0';
  4336. FORC4 cam_mul[c ^ (c >> 1) ^ (i & 1)] =
  4337. sget2 (buf97 + (i & -2) + c*2);
  4338. #ifdef LIBRAW_LIBRARY_BUILD
  4339. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4340. #endif
  4341. }
  4342. if (tag == 0x200 && len == 3)
  4343. shot_order = (get4(),get4());
  4344. if (tag == 0x200 && len == 4)
  4345. FORC4 cblack[c ^ c >> 1] = get2();
  4346. if (tag == 0x201 && len == 4)
  4347. goto get2_rggb;
  4348. if (tag == 0x220 && len == 53)
  4349. meta_offset = ftell(ifp) + 14;
  4350. if (tag == 0x401 && type == 4 && len == 4)
  4351. FORC4 cblack[c ^ c >> 1] = get4();
  4352. if (tag == 0xe01) { /* Nikon Capture Note */
  4353. type = order;
  4354. order = 0x4949;
  4355. fseek (ifp, 22, SEEK_CUR);
  4356. for (offset=22; offset+22 < len; offset += 22+i) {
  4357. tag = get4();
  4358. fseek (ifp, 14, SEEK_CUR);
  4359. i = get4()-4;
  4360. if (tag == 0x76a43207) flip = get2();
  4361. else fseek (ifp, i, SEEK_CUR);
  4362. }
  4363. order = type;
  4364. }
  4365. if (tag == 0xe80 && len == 256 && type == 7) {
  4366. fseek (ifp, 48, SEEK_CUR);
  4367. cam_mul[0] = get2() * 508 * 1.078 / 0x10000;
  4368. cam_mul[2] = get2() * 382 * 1.173 / 0x10000;
  4369. #ifdef LIBRAW_LIBRARY_BUILD
  4370. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4371. #endif
  4372. }
  4373. if (tag == 0xf00 && type == 7) {
  4374. if (len == 614)
  4375. fseek (ifp, 176, SEEK_CUR);
  4376. else if (len == 734 || len == 1502)
  4377. fseek (ifp, 148, SEEK_CUR);
  4378. else goto next;
  4379. goto get2_256;
  4380. }
  4381. if ((tag == 0x1011 && len == 9) || tag == 0x20400200)
  4382. {
  4383. for (i=0; i < 3; i++)
  4384. FORC3 cmatrix[i][c] = ((short) get2()) / 256.0;
  4385. #ifdef LIBRAW_LIBRARY_BUILD
  4386. color_flags.cmatrix_state = LIBRAW_COLORSTATE_LOADED;
  4387. #endif
  4388. }
  4389. if ((tag == 0x1012 || tag == 0x20400600) && len == 4)
  4390. FORC4 cblack[c ^ c >> 1] = get2();
  4391. if (tag == 0x1017 || tag == 0x20400100)
  4392. {
  4393. cam_mul[0] = get2() / 256.0;
  4394. #ifdef LIBRAW_LIBRARY_BUILD
  4395. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4396. #endif
  4397. }
  4398. if (tag == 0x1018 || tag == 0x20400100)
  4399. {
  4400. cam_mul[2] = get2() / 256.0;
  4401. #ifdef LIBRAW_LIBRARY_BUILD
  4402. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4403. #endif
  4404. }
  4405. if (tag == 0x2011 && len == 2) {
  4406. get2_256:
  4407. order = 0x4d4d;
  4408. cam_mul[0] = get2() / 256.0;
  4409. cam_mul[2] = get2() / 256.0;
  4410. #ifdef LIBRAW_LIBRARY_BUILD
  4411. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4412. #endif
  4413. }
  4414. if ((tag | 0x70) == 0x2070 && type == 4)
  4415. fseek (ifp, get4()+base, SEEK_SET);
  4416. if (tag == 0x2010 && type != 7)
  4417. load_raw = &CLASS olympus_load_raw;
  4418. if (tag == 0x2020)
  4419. parse_thumb_note (base, 257, 258);
  4420. if (tag == 0x2040)
  4421. parse_makernote (base, 0x2040);
  4422. if (tag == 0xb028) {
  4423. fseek (ifp, get4()+base, SEEK_SET);
  4424. parse_thumb_note (base, 136, 137);
  4425. }
  4426. if (tag == 0x4001 && len > 500) {
  4427. i = len == 582 ? 50 : len == 653 ? 68 : len == 5120 ? 142 : 126;
  4428. fseek (ifp, i, SEEK_CUR);
  4429. get2_rggb:
  4430. FORC4 cam_mul[c ^ (c >> 1)] = get2();
  4431. #ifdef LIBRAW_LIBRARY_BUILD
  4432. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4433. #endif
  4434. fseek (ifp, 22, SEEK_CUR);
  4435. FORC4 sraw_mul[c ^ (c >> 1)] = get2();
  4436. }
  4437. next:
  4438. fseek (ifp, save, SEEK_SET);
  4439. }
  4440. quit:
  4441. order = sorder;
  4442. }
  4443. /*
  4444. Since the TIFF DateTime string has no timezone information,
  4445. assume that the camera's clock was set to Universal Time.
  4446. */
  4447. void CLASS get_timestamp (int reversed)
  4448. {
  4449. struct tm t;
  4450. char str[20];
  4451. int i;
  4452. str[19] = 0;
  4453. if (reversed)
  4454. for (i=19; i--; ) str[i] = fgetc(ifp);
  4455. else
  4456. fread (str, 19, 1, ifp);
  4457. memset (&t, 0, sizeof t);
  4458. if (sscanf (str, "%d:%d:%d %d:%d:%d", &t.tm_year, &t.tm_mon,
  4459. &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec) != 6)
  4460. return;
  4461. t.tm_year -= 1900;
  4462. t.tm_mon -= 1;
  4463. if (mktime(&t) > 0)
  4464. timestamp = mktime(&t);
  4465. }
  4466. void CLASS parse_exif (int base)
  4467. {
  4468. unsigned kodak, entries, tag, type, len, save, c;
  4469. double expo;
  4470. kodak = !strncmp(make,"EASTMAN",7) && tiff_nifds < 3;
  4471. entries = get2();
  4472. while (entries--) {
  4473. tiff_get (base, &tag, &type, &len, &save);
  4474. switch (tag) {
  4475. case 33434: shutter = getreal(type); break;
  4476. case 33437: aperture = getreal(type); break;
  4477. case 34855: iso_speed = get2(); break;
  4478. case 36867:
  4479. case 36868: get_timestamp(0); break;
  4480. case 37377: if ((expo = -getreal(type)) < 128)
  4481. shutter = pow (2, expo); break;
  4482. case 37378: aperture = pow (2, getreal(type)/2); break;
  4483. case 37386: focal_len = getreal(type); break;
  4484. case 37500: parse_makernote (base, 0); break;
  4485. case 40962: if (kodak) raw_width = get4(); break;
  4486. case 40963: if (kodak) raw_height = get4(); break;
  4487. case 41730:
  4488. if (get4() == 0x20002)
  4489. for (exif_cfa=c=0; c < 8; c+=2)
  4490. exif_cfa |= fgetc(ifp) * 0x01010101 << c;
  4491. }
  4492. fseek (ifp, save, SEEK_SET);
  4493. }
  4494. }
  4495. void CLASS parse_gps (int base)
  4496. {
  4497. unsigned entries, tag, type, len, save, c;
  4498. entries = get2();
  4499. while (entries--) {
  4500. tiff_get (base, &tag, &type, &len, &save);
  4501. switch (tag) {
  4502. case 1: case 3: case 5:
  4503. gpsdata[29+tag/2] = getc(ifp); break;
  4504. case 2: case 4: case 7:
  4505. FORC(6) gpsdata[tag/3*6+c] = get4(); break;
  4506. case 6:
  4507. FORC(2) gpsdata[18+c] = get4(); break;
  4508. case 18: case 29:
  4509. fgets ((char *) (gpsdata+14+tag/3), MIN(len,12), ifp);
  4510. }
  4511. fseek (ifp, save, SEEK_SET);
  4512. }
  4513. }
  4514. void CLASS romm_coeff (float romm_cam[3][3])
  4515. {
  4516. static const float rgb_romm[3][3] = /* ROMM == Kodak ProPhoto */
  4517. { { 2.034193, -0.727420, -0.306766 },
  4518. { -0.228811, 1.231729, -0.002922 },
  4519. { -0.008565, -0.153273, 1.161839 } };
  4520. int i, j, k;
  4521. for (i=0; i < 3; i++)
  4522. for (j=0; j < 3; j++)
  4523. for (cmatrix[i][j] = k=0; k < 3; k++)
  4524. cmatrix[i][j] += rgb_romm[i][k] * romm_cam[k][j];
  4525. #ifdef LIBRAW_LIBRARY_BUILD
  4526. color_flags.cmatrix_state = LIBRAW_COLORSTATE_CALCULATED;
  4527. #endif
  4528. }
  4529. void CLASS parse_mos (int offset)
  4530. {
  4531. char data[40];
  4532. int skip, from, i, c, neut[4], planes=0, frot=0;
  4533. static const char *mod[] =
  4534. { "","DCB2","Volare","Cantare","CMost","Valeo 6","Valeo 11","Valeo 22",
  4535. "Valeo 11p","Valeo 17","","Aptus 17","Aptus 22","Aptus 75","Aptus 65",
  4536. "Aptus 54S","Aptus 65S","Aptus 75S","AFi 5","AFi 6","AFi 7" };
  4537. float romm_cam[3][3];
  4538. fseek (ifp, offset, SEEK_SET);
  4539. while (1) {
  4540. if (get4() != 0x504b5453) break;
  4541. get4();
  4542. fread (data, 1, 40, ifp);
  4543. skip = get4();
  4544. from = ftell(ifp);
  4545. if (!strcmp(data,"JPEG_preview_data")) {
  4546. thumb_offset = from;
  4547. thumb_length = skip;
  4548. }
  4549. if (!strcmp(data,"icc_camera_profile")) {
  4550. profile_offset = from;
  4551. profile_length = skip;
  4552. }
  4553. if (!strcmp(data,"ShootObj_back_type")) {
  4554. fscanf (ifp, "%d", &i);
  4555. if ((unsigned) i < sizeof mod / sizeof (*mod))
  4556. strcpy (model, mod[i]);
  4557. }
  4558. if (!strcmp(data,"icc_camera_to_tone_matrix")) {
  4559. for (i=0; i < 9; i++)
  4560. romm_cam[0][i] = int_to_float(get4());
  4561. romm_coeff (romm_cam);
  4562. }
  4563. if (!strcmp(data,"CaptProf_color_matrix")) {
  4564. for (i=0; i < 9; i++)
  4565. fscanf (ifp, "%f", &romm_cam[0][i]);
  4566. romm_coeff (romm_cam);
  4567. }
  4568. if (!strcmp(data,"CaptProf_number_of_planes"))
  4569. fscanf (ifp, "%d", &planes);
  4570. if (!strcmp(data,"CaptProf_raw_data_rotation"))
  4571. fscanf (ifp, "%d", &flip);
  4572. if (!strcmp(data,"CaptProf_mosaic_pattern"))
  4573. FORC4 {
  4574. fscanf (ifp, "%d", &i);
  4575. if (i == 1) frot = c ^ (c >> 1);
  4576. }
  4577. if (!strcmp(data,"ImgProf_rotation_angle")) {
  4578. fscanf (ifp, "%d", &i);
  4579. flip = i - flip;
  4580. }
  4581. if (!strcmp(data,"NeutObj_neutrals") && !cam_mul[0]) {
  4582. FORC4 fscanf (ifp, "%d", neut+c);
  4583. FORC3 cam_mul[c] = (float) neut[0] / neut[c+1];
  4584. #ifdef LIBRAW_LIBRARY_BUILD
  4585. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4586. #endif
  4587. }
  4588. parse_mos (from);
  4589. fseek (ifp, skip+from, SEEK_SET);
  4590. }
  4591. if (planes)
  4592. filters = (planes == 1) * 0x01010101 *
  4593. (uchar) "\x94\x61\x16\x49"[(flip/90 + frot) & 3];
  4594. }
  4595. void CLASS linear_table (unsigned len)
  4596. {
  4597. int i;
  4598. if (len > 0x1000) len = 0x1000;
  4599. read_shorts (curve, len);
  4600. #ifdef LIBRAW_LIBRARY_BUILD
  4601. color_flags.curve_state = LIBRAW_COLORSTATE_LOADED;
  4602. #endif
  4603. for (i=len; i < 0x1000; i++)
  4604. curve[i] = curve[i-1];
  4605. maximum = curve[0xfff];
  4606. }
  4607. void CLASS parse_kodak_ifd (int base)
  4608. {
  4609. unsigned entries, tag, type, len, save;
  4610. int i, c, wbi=-2, wbtemp=6500;
  4611. float mul[3]={1,1,1}, num;
  4612. static const int wbtag[] = { 64037,64040,64039,64041,-1,-1,64042 };
  4613. entries = get2();
  4614. if (entries > 1024) return;
  4615. while (entries--) {
  4616. tiff_get (base, &tag, &type, &len, &save);
  4617. if (tag == 1020) wbi = getint(type);
  4618. if (tag == 1021 && len == 72) { /* WB set in software */
  4619. fseek (ifp, 40, SEEK_CUR);
  4620. FORC3 cam_mul[c] = 2048.0 / get2();
  4621. #ifdef LIBRAW_LIBRARY_BUILD
  4622. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4623. #endif
  4624. wbi = -2;
  4625. }
  4626. if (tag == 2118) wbtemp = getint(type);
  4627. if (tag == 2130 + wbi)
  4628. FORC3 mul[c] = getreal(type);
  4629. if (tag == 2140 + wbi && wbi >= 0)
  4630. {
  4631. FORC3 {
  4632. for (num=i=0; i < 4; i++)
  4633. num += getreal(type) * pow (wbtemp/100.0, i);
  4634. cam_mul[c] = 2048 / (num * mul[c]);
  4635. }
  4636. #ifdef LIBRAW_LIBRARY_BUILD
  4637. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4638. #endif
  4639. }
  4640. if (tag == 2317) linear_table (len);
  4641. if (tag == 6020) iso_speed = getint(type);
  4642. if (tag == 64013) wbi = fgetc(ifp);
  4643. if ((unsigned) wbi < 7 && tag == wbtag[wbi])
  4644. FORC3 cam_mul[c] = get4();
  4645. if (tag == 64019) width = getint(type);
  4646. if (tag == 64020) height = (getint(type)+1) & -2;
  4647. fseek (ifp, save, SEEK_SET);
  4648. }
  4649. }
  4650. #line 6012 "dcraw/dcraw.c"
  4651. int CLASS parse_tiff_ifd (int base)
  4652. {
  4653. unsigned entries, tag, type, len, plen=16, save;
  4654. int ifd, use_cm=0, cfa, i, j, c, ima_len=0;
  4655. int blrr=1, blrc=1, dblack[] = { 0,0,0,0 };
  4656. char software[64], *cbuf, *cp;
  4657. uchar cfa_pat[16], cfa_pc[] = { 0,1,2,3 }, tab[256];
  4658. double cc[4][4], cm[4][3], cam_xyz[4][3], num;
  4659. double ab[]={ 1,1,1,1 }, asn[] = { 0,0,0,0 }, xyz[] = { 1,1,1 };
  4660. unsigned sony_curve[] = { 0,0,0,0,0,4095 };
  4661. unsigned *buf, sony_offset=0, sony_length=0, sony_key=0;
  4662. struct jhead jh;
  4663. FILE *sfp;
  4664. if (tiff_nifds >= sizeof tiff_ifd / sizeof tiff_ifd[0])
  4665. return 1;
  4666. ifd = tiff_nifds++;
  4667. for (j=0; j < 4; j++)
  4668. for (i=0; i < 4; i++)
  4669. cc[j][i] = i == j;
  4670. entries = get2();
  4671. if (entries > 512) return 1;
  4672. while (entries--) {
  4673. tiff_get (base, &tag, &type, &len, &save);
  4674. switch (tag) {
  4675. case 17: case 18:
  4676. if (type == 3 && len == 1)
  4677. {
  4678. cam_mul[(tag-17)*2] = get2() / 256.0;
  4679. #ifdef LIBRAW_LIBRARY_BUILD
  4680. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4681. #endif
  4682. }
  4683. break;
  4684. case 23:
  4685. if (type == 3) iso_speed = get2();
  4686. break;
  4687. case 36: case 37: case 38:
  4688. cam_mul[tag-0x24] = get2();
  4689. break;
  4690. case 39:
  4691. if (len < 50 || cam_mul[0]) break;
  4692. fseek (ifp, 12, SEEK_CUR);
  4693. FORC3 cam_mul[c] = get2();
  4694. #ifdef LIBRAW_LIBRARY_BUILD
  4695. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4696. #endif
  4697. break;
  4698. case 46:
  4699. if (type != 7 || fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8) break;
  4700. thumb_offset = ftell(ifp) - 2;
  4701. thumb_length = len;
  4702. break;
  4703. case 61440: /* Fuji HS10 table */
  4704. parse_tiff_ifd (base);
  4705. break;
  4706. case 2: case 256: case 61441: /* ImageWidth */
  4707. tiff_ifd[ifd].t_width = getint(type);
  4708. break;
  4709. case 3: case 257: case 61442: /* ImageHeight */
  4710. tiff_ifd[ifd].t_height = getint(type);
  4711. break;
  4712. case 258: /* BitsPerSample */
  4713. case 61443:
  4714. tiff_ifd[ifd].samples = len & 7;
  4715. tiff_ifd[ifd].bps = getint(type);
  4716. break;
  4717. case 259: /* Compression */
  4718. tiff_ifd[ifd].comp = get2();
  4719. break;
  4720. case 262: /* PhotometricInterpretation */
  4721. tiff_ifd[ifd].phint = get2();
  4722. break;
  4723. case 270: /* ImageDescription */
  4724. fread (desc, 512, 1, ifp);
  4725. break;
  4726. case 271: /* Make */
  4727. fgets (make, 64, ifp);
  4728. break;
  4729. case 272: /* Model */
  4730. fgets (model, 64, ifp);
  4731. break;
  4732. case 280: /* Panasonic RW2 offset */
  4733. if (type != 4) break;
  4734. load_raw = &CLASS panasonic_load_raw;
  4735. load_flags = 0x2008;
  4736. case 273: /* StripOffset */
  4737. case 513: /* JpegIFOffset */
  4738. case 61447:
  4739. tiff_ifd[ifd].offset = get4()+base;
  4740. if (!tiff_ifd[ifd].bps) {
  4741. fseek (ifp, tiff_ifd[ifd].offset, SEEK_SET);
  4742. if (ljpeg_start (&jh, 1)) {
  4743. tiff_ifd[ifd].comp = 6;
  4744. tiff_ifd[ifd].t_width = jh.wide;
  4745. tiff_ifd[ifd].t_height = jh.high;
  4746. tiff_ifd[ifd].bps = jh.bits;
  4747. tiff_ifd[ifd].samples = jh.clrs;
  4748. if (!(jh.sraw || (jh.clrs & 1)))
  4749. tiff_ifd[ifd].t_width *= jh.clrs;
  4750. i = order;
  4751. parse_tiff (tiff_ifd[ifd].offset + 12);
  4752. order = i;
  4753. }
  4754. }
  4755. break;
  4756. case 274: /* Orientation */
  4757. tiff_ifd[ifd].t_flip = "50132467"[get2() & 7]-'0';
  4758. break;
  4759. case 277: /* SamplesPerPixel */
  4760. tiff_ifd[ifd].samples = getint(type) & 7;
  4761. break;
  4762. case 279: /* StripByteCounts */
  4763. case 514:
  4764. case 61448:
  4765. tiff_ifd[ifd].bytes = get4();
  4766. break;
  4767. case 61454:
  4768. FORC3 cam_mul[(4-c) % 3] = getint(type);
  4769. break;
  4770. case 305: case 11: /* Software */
  4771. fgets (software, 64, ifp);
  4772. if (!strncmp(software,"Adobe",5) ||
  4773. !strncmp(software,"dcraw",5) ||
  4774. !strncmp(software,"UFRaw",5) ||
  4775. !strncmp(software,"Bibble",6) ||
  4776. !strncmp(software,"Nikon Scan",10) ||
  4777. !strcmp (software,"Digital Photo Professional"))
  4778. is_raw = 0;
  4779. break;
  4780. case 306: /* DateTime */
  4781. get_timestamp(0);
  4782. break;
  4783. case 315: /* Artist */
  4784. fread (artist, 64, 1, ifp);
  4785. break;
  4786. case 322: /* TileWidth */
  4787. tile_width = getint(type);
  4788. break;
  4789. case 323: /* TileLength */
  4790. tile_length = getint(type);
  4791. break;
  4792. case 324: /* TileOffsets */
  4793. tiff_ifd[ifd].offset = len > 1 ? ftell(ifp) : get4();
  4794. if (len == 4) {
  4795. load_raw = &CLASS sinar_4shot_load_raw;
  4796. is_raw = 5;
  4797. }
  4798. break;
  4799. case 330: /* SubIFDs */
  4800. if (!strcmp(model,"DSLR-A100") && tiff_ifd[ifd].t_width == 3872) {
  4801. load_raw = &CLASS sony_arw_load_raw;
  4802. data_offset = get4()+base;
  4803. ifd++; break;
  4804. }
  4805. while (len--) {
  4806. i = ftell(ifp);
  4807. fseek (ifp, get4()+base, SEEK_SET);
  4808. if (parse_tiff_ifd (base)) break;
  4809. fseek (ifp, i+4, SEEK_SET);
  4810. }
  4811. break;
  4812. case 400:
  4813. strcpy (make, "Sarnoff");
  4814. maximum = 0xfff;
  4815. break;
  4816. case 28688:
  4817. FORC4 sony_curve[c+1] = get2() >> 2 & 0xfff;
  4818. for (i=0; i < 5; i++)
  4819. for (j = sony_curve[i]+1; j <= sony_curve[i+1]; j++)
  4820. curve[j] = curve[j-1] + (1 << i);
  4821. #ifdef LIBRAW_LIBRARY_BUILD
  4822. color_flags.curve_state = LIBRAW_COLORSTATE_LOADED;
  4823. #endif
  4824. break;
  4825. case 29184: sony_offset = get4(); break;
  4826. case 29185: sony_length = get4(); break;
  4827. case 29217: sony_key = get4(); break;
  4828. case 29264:
  4829. parse_minolta (ftell(ifp));
  4830. raw_width = 0;
  4831. break;
  4832. case 29443:
  4833. FORC4 cam_mul[c ^ (c < 2)] = get2();
  4834. #ifdef LIBRAW_LIBRARY_BUILD
  4835. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4836. #endif
  4837. break;
  4838. case 29459:
  4839. FORC4 cam_mul[c] = get2();
  4840. i = (cam_mul[1] == 1024 && cam_mul[2] == 1024) << 1;
  4841. SWAP (cam_mul[i],cam_mul[i+1])
  4842. #ifdef LIBRAW_LIBRARY_BUILD
  4843. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4844. #endif
  4845. break;
  4846. case 33405: /* Model2 */
  4847. fgets (model2, 64, ifp);
  4848. break;
  4849. case 33422: /* CFAPattern */
  4850. case 64777: /* Kodak P-series */
  4851. if ((plen=len) > 16) plen = 16;
  4852. fread (cfa_pat, 1, plen, ifp);
  4853. for (colors=cfa=i=0; i < plen; i++) {
  4854. colors += !(cfa & (1 << cfa_pat[i]));
  4855. cfa |= 1 << cfa_pat[i];
  4856. }
  4857. if (cfa == 070) memcpy (cfa_pc,"\003\004\005",3); /* CMY */
  4858. if (cfa == 072) memcpy (cfa_pc,"\005\003\004\001",4); /* GMCY */
  4859. goto guess_cfa_pc;
  4860. case 33424:
  4861. case 65024:
  4862. fseek (ifp, get4()+base, SEEK_SET);
  4863. parse_kodak_ifd (base);
  4864. break;
  4865. case 33434: /* ExposureTime */
  4866. shutter = getreal(type);
  4867. break;
  4868. case 33437: /* FNumber */
  4869. aperture = getreal(type);
  4870. break;
  4871. case 34306: /* Leaf white balance */
  4872. FORC4 cam_mul[c ^ 1] = 4096.0 / get2();
  4873. #ifdef LIBRAW_LIBRARY_BUILD
  4874. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4875. #endif
  4876. break;
  4877. case 34307: /* Leaf CatchLight color matrix */
  4878. fread (software, 1, 7, ifp);
  4879. if (strncmp(software,"MATRIX",6)) break;
  4880. colors = 4;
  4881. for (raw_color = i=0; i < 3; i++) {
  4882. FORC4 fscanf (ifp, "%f", &rgb_cam[i][c^1]);
  4883. if (!use_camera_wb) continue;
  4884. num = 0;
  4885. FORC4 num += rgb_cam[i][c];
  4886. FORC4 rgb_cam[i][c] /= num;
  4887. #ifdef LIBRAW_LIBRARY_BUILD
  4888. color_flags.rgb_cam_state = LIBRAW_COLORSTATE_LOADED;
  4889. #endif
  4890. }
  4891. break;
  4892. case 34310: /* Leaf metadata */
  4893. parse_mos (ftell(ifp));
  4894. case 34303:
  4895. strcpy (make, "Leaf");
  4896. break;
  4897. case 34665: /* EXIF tag */
  4898. fseek (ifp, get4()+base, SEEK_SET);
  4899. parse_exif (base);
  4900. break;
  4901. case 34853: /* GPSInfo tag */
  4902. fseek (ifp, get4()+base, SEEK_SET);
  4903. parse_gps (base);
  4904. break;
  4905. case 34675: /* InterColorProfile */
  4906. case 50831: /* AsShotICCProfile */
  4907. profile_offset = ftell(ifp);
  4908. profile_length = len;
  4909. break;
  4910. case 37122: /* CompressedBitsPerPixel */
  4911. kodak_cbpp = get4();
  4912. break;
  4913. case 37386: /* FocalLength */
  4914. focal_len = getreal(type);
  4915. break;
  4916. case 37393: /* ImageNumber */
  4917. shot_order = getint(type);
  4918. break;
  4919. case 37400: /* old Kodak KDC tag */
  4920. for (raw_color = i=0; i < 3; i++) {
  4921. getreal(type);
  4922. FORC3 rgb_cam[i][c] = getreal(type);
  4923. }
  4924. #ifdef LIBRAW_LIBRARY_BUILD
  4925. color_flags.rgb_cam_state = LIBRAW_COLORSTATE_LOADED;
  4926. #endif
  4927. break;
  4928. case 46275: /* Imacon tags */
  4929. strcpy (make, "Imacon");
  4930. data_offset = ftell(ifp);
  4931. ima_len = len;
  4932. break;
  4933. case 46279:
  4934. if (!ima_len) break;
  4935. fseek (ifp, 78, SEEK_CUR);
  4936. raw_width = get4();
  4937. raw_height = get4();
  4938. left_margin = get4() & 7;
  4939. width = raw_width - left_margin - (get4() & 7);
  4940. top_margin = get4() & 7;
  4941. height = raw_height - top_margin - (get4() & 7);
  4942. if (raw_width == 7262) {
  4943. height = 5444;
  4944. width = 7244;
  4945. left_margin = 7;
  4946. }
  4947. fseek (ifp, 52, SEEK_CUR);
  4948. FORC3 cam_mul[c] = getreal(11);
  4949. #ifdef LIBRAW_LIBRARY_BUILD
  4950. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4951. #endif
  4952. fseek (ifp, 114, SEEK_CUR);
  4953. flip = (get2() >> 7) * 90;
  4954. if (width * height * 6 == ima_len) {
  4955. if (flip % 180 == 90) SWAP(width,height);
  4956. filters = flip = 0;
  4957. }
  4958. sprintf (model, "Ixpress %d-Mp", height*width/1000000);
  4959. load_raw = &CLASS imacon_full_load_raw;
  4960. if (filters) {
  4961. if (left_margin & 1) filters = 0x61616161;
  4962. load_raw = &CLASS unpacked_load_raw;
  4963. }
  4964. maximum = 0xffff;
  4965. break;
  4966. case 50454: /* Sinar tag */
  4967. case 50455:
  4968. if (!(cbuf = (char *) malloc(len))) break;
  4969. fread (cbuf, 1, len, ifp);
  4970. for (cp = cbuf-1; cp && cp < cbuf+len; cp = strchr(cp,'\n'))
  4971. if (!strncmp (++cp,"Neutral ",8))
  4972. {
  4973. sscanf (cp+8, "%f %f %f", cam_mul, cam_mul+1, cam_mul+2);
  4974. #ifdef LIBRAW_LIBRARY_BUILD
  4975. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  4976. #endif
  4977. }
  4978. free (cbuf);
  4979. break;
  4980. case 50458:
  4981. if (!make[0]) strcpy (make, "Hasselblad");
  4982. break;
  4983. case 50459: /* Hasselblad tag */
  4984. i = order;
  4985. j = ftell(ifp);
  4986. c = tiff_nifds;
  4987. order = get2();
  4988. fseek (ifp, j+(get2(),get4()), SEEK_SET);
  4989. parse_tiff_ifd (j);
  4990. maximum = 0xffff;
  4991. tiff_nifds = c;
  4992. order = i;
  4993. break;
  4994. case 50706: /* DNGVersion */
  4995. FORC4 dng_version = (dng_version << 8) + fgetc(ifp);
  4996. if (!make[0]) strcpy (make, "DNG");
  4997. is_raw = 1;
  4998. break;
  4999. case 50710: /* CFAPlaneColor */
  5000. if (len > 4) len = 4;
  5001. colors = len;
  5002. fread (cfa_pc, 1, colors, ifp);
  5003. guess_cfa_pc:
  5004. FORCC tab[cfa_pc[c]] = c;
  5005. cdesc[c] = 0;
  5006. for (i=16; i--; )
  5007. filters = filters << 2 | tab[cfa_pat[i % plen]];
  5008. break;
  5009. case 50711: /* CFALayout */
  5010. if (get2() == 2) {
  5011. fuji_width = 1;
  5012. filters = 0x49494949;
  5013. }
  5014. break;
  5015. case 291:
  5016. case 50712: /* LinearizationTable */
  5017. linear_table (len);
  5018. break;
  5019. case 50713: /* BlackLevelRepeatDim */
  5020. blrr = get2();
  5021. blrc = get2();
  5022. break;
  5023. case 61450:
  5024. blrr = blrc = 2;
  5025. case 50714: /* BlackLevel */
  5026. black = getreal(type);
  5027. if (!filters || !~filters) break;
  5028. dblack[0] = black;
  5029. dblack[1] = (blrc == 2) ? getreal(type):dblack[0];
  5030. dblack[2] = (blrr == 2) ? getreal(type):dblack[0];
  5031. dblack[3] = (blrc == 2 && blrr == 2) ? getreal(type):dblack[1];
  5032. if (colors == 3)
  5033. filters |= ((filters >> 2 & 0x22222222) |
  5034. (filters << 2 & 0x88888888)) & filters << 1;
  5035. FORC4 cblack[filters >> (c << 1) & 3] = dblack[c];
  5036. black = 0;
  5037. break;
  5038. case 50715: /* BlackLevelDeltaH */
  5039. case 50716: /* BlackLevelDeltaV */
  5040. for (num=i=0; i < len; i++)
  5041. num += getreal(type);
  5042. black += num/len + 0.5;
  5043. break;
  5044. case 50717: /* WhiteLevel */
  5045. maximum = getint(type);
  5046. break;
  5047. case 50718: /* DefaultScale */
  5048. pixel_aspect = getreal(type);
  5049. pixel_aspect /= getreal(type);
  5050. break;
  5051. case 50721: /* ColorMatrix1 */
  5052. case 50722: /* ColorMatrix2 */
  5053. FORCC for (j=0; j < 3; j++)
  5054. cm[c][j] = getreal(type);
  5055. use_cm = 1;
  5056. break;
  5057. case 50723: /* CameraCalibration1 */
  5058. case 50724: /* CameraCalibration2 */
  5059. for (i=0; i < colors; i++)
  5060. FORCC cc[i][c] = getreal(type);
  5061. break;
  5062. case 50727: /* AnalogBalance */
  5063. FORCC ab[c] = getreal(type);
  5064. break;
  5065. case 50728: /* AsShotNeutral */
  5066. FORCC asn[c] = getreal(type);
  5067. break;
  5068. case 50729: /* AsShotWhiteXY */
  5069. xyz[0] = getreal(type);
  5070. xyz[1] = getreal(type);
  5071. xyz[2] = 1 - xyz[0] - xyz[1];
  5072. FORC3 xyz[c] /= d65_white[c];
  5073. break;
  5074. case 50740: /* DNGPrivateData */
  5075. if (dng_version) break;
  5076. parse_minolta (j = get4()+base);
  5077. fseek (ifp, j, SEEK_SET);
  5078. parse_tiff_ifd (base);
  5079. break;
  5080. case 50752:
  5081. read_shorts (cr2_slice, 3);
  5082. break;
  5083. case 50829: /* ActiveArea */
  5084. top_margin = getint(type);
  5085. left_margin = getint(type);
  5086. height = getint(type) - top_margin;
  5087. width = getint(type) - left_margin;
  5088. break;
  5089. case 64772: /* Kodak P-series */
  5090. if (len < 13) break;
  5091. fseek (ifp, 16, SEEK_CUR);
  5092. data_offset = get4();
  5093. fseek (ifp, 28, SEEK_CUR);
  5094. data_offset += get4();
  5095. load_raw = &CLASS packed_load_raw;
  5096. break;
  5097. case 65026:
  5098. if (type == 2) fgets (model2, 64, ifp);
  5099. }
  5100. fseek (ifp, save, SEEK_SET);
  5101. }
  5102. if (sony_length && (buf = (unsigned *) malloc(sony_length))) {
  5103. fseek (ifp, sony_offset, SEEK_SET);
  5104. fread (buf, sony_length, 1, ifp);
  5105. sony_decrypt (buf, sony_length/4, 1, sony_key);
  5106. #ifndef LIBRAW_LIBRARY_BUILD
  5107. sfp = ifp;
  5108. if ((ifp = tmpfile())) {
  5109. fwrite (buf, sony_length, 1, ifp);
  5110. fseek (ifp, 0, SEEK_SET);
  5111. parse_tiff_ifd (-sony_offset);
  5112. fclose (ifp);
  5113. }
  5114. ifp = sfp;
  5115. #else
  5116. if( !ifp->tempbuffer_open(buf,sony_length))
  5117. {
  5118. parse_tiff_ifd(-sony_offset);
  5119. ifp->tempbuffer_close();
  5120. }
  5121. #endif
  5122. free (buf);
  5123. }
  5124. for (i=0; i < colors; i++)
  5125. FORCC cc[i][c] *= ab[i];
  5126. if (use_cm) {
  5127. FORCC for (i=0; i < 3; i++)
  5128. for (cam_xyz[c][i]=j=0; j < colors; j++)
  5129. cam_xyz[c][i] += cc[c][j] * cm[j][i] * xyz[i];
  5130. cam_xyz_coeff (cam_xyz);
  5131. }
  5132. if (asn[0]) {
  5133. cam_mul[3] = 0;
  5134. FORCC cam_mul[c] = 1 / asn[c];
  5135. #ifdef LIBRAW_LIBRARY_BUILD
  5136. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5137. #endif
  5138. }
  5139. if (!use_cm)
  5140. {
  5141. FORCC pre_mul[c] /= cc[c][c];
  5142. #ifdef LIBRAW_LIBRARY_BUILD
  5143. color_flags.pre_mul_state = LIBRAW_COLORSTATE_LOADED;
  5144. #endif
  5145. }
  5146. return 0;
  5147. }
  5148. int CLASS parse_tiff (int base)
  5149. {
  5150. int doff;
  5151. fseek (ifp, base, SEEK_SET);
  5152. order = get2();
  5153. if (order != 0x4949 && order != 0x4d4d) return 0;
  5154. get2();
  5155. while ((doff = get4())) {
  5156. fseek (ifp, doff+base, SEEK_SET);
  5157. if (parse_tiff_ifd (base)) break;
  5158. }
  5159. return 1;
  5160. }
  5161. void CLASS apply_tiff()
  5162. {
  5163. int max_samp=0, raw=-1, thm=-1, i;
  5164. struct jhead jh;
  5165. thumb_misc = 16;
  5166. if (thumb_offset) {
  5167. fseek (ifp, thumb_offset, SEEK_SET);
  5168. if (ljpeg_start (&jh, 1)) {
  5169. thumb_misc = jh.bits;
  5170. thumb_width = jh.wide;
  5171. thumb_height = jh.high;
  5172. }
  5173. }
  5174. for (i=0; i < tiff_nifds; i++) {
  5175. if (max_samp < tiff_ifd[i].samples)
  5176. max_samp = tiff_ifd[i].samples;
  5177. if (max_samp > 3) max_samp = 3;
  5178. if ((tiff_ifd[i].comp != 6 || tiff_ifd[i].samples != 3) &&
  5179. (tiff_ifd[i].t_width | tiff_ifd[i].t_height) < 0x10000 &&
  5180. tiff_ifd[i].t_width*tiff_ifd[i].t_height > raw_width*raw_height) {
  5181. raw_width = tiff_ifd[i].t_width;
  5182. raw_height = tiff_ifd[i].t_height;
  5183. tiff_bps = tiff_ifd[i].bps;
  5184. tiff_compress = tiff_ifd[i].comp;
  5185. data_offset = tiff_ifd[i].offset;
  5186. tiff_flip = tiff_ifd[i].t_flip;
  5187. tiff_samples = tiff_ifd[i].samples;
  5188. raw = i;
  5189. }
  5190. }
  5191. for (i=tiff_nifds; i--; )
  5192. if (tiff_ifd[i].t_flip) tiff_flip = tiff_ifd[i].t_flip;
  5193. if (raw >= 0 && !load_raw)
  5194. switch (tiff_compress) {
  5195. case 0: case 1:
  5196. switch (tiff_bps) {
  5197. case 8: load_raw = &CLASS eight_bit_load_raw; break;
  5198. case 12: load_raw = &CLASS packed_load_raw;
  5199. if (tiff_ifd[raw].phint == 2)
  5200. load_flags = 6;
  5201. if (strncmp(make,"PENTAX",6)) break;
  5202. case 14:
  5203. case 16: load_raw = &CLASS unpacked_load_raw; break;
  5204. }
  5205. if (tiff_ifd[raw].bytes*5 == raw_width*raw_height*8) {
  5206. tiff_bps = 12;
  5207. load_raw = &CLASS packed_load_raw;
  5208. load_flags = 17;
  5209. }
  5210. break;
  5211. case 6: case 7: case 99:
  5212. load_raw = &CLASS lossless_jpeg_load_raw; break;
  5213. case 262:
  5214. load_raw = &CLASS kodak_262_load_raw; break;
  5215. case 32767:
  5216. if (tiff_ifd[raw].bytes == raw_width*raw_height) {
  5217. tiff_bps = 12;
  5218. load_raw = &CLASS sony_arw2_load_raw; break;
  5219. }
  5220. if (tiff_ifd[raw].bytes*8 != raw_width*raw_height*tiff_bps) {
  5221. raw_height += 8;
  5222. load_raw = &CLASS sony_arw_load_raw; break;
  5223. }
  5224. load_flags = 15;
  5225. case 32769:
  5226. load_flags++;
  5227. case 32770:
  5228. case 32773:
  5229. load_raw = &CLASS packed_load_raw; break;
  5230. case 34713:
  5231. load_raw = &CLASS nikon_compressed_load_raw; break;
  5232. case 65535:
  5233. load_raw = &CLASS pentax_load_raw; break;
  5234. case 65000:
  5235. switch (tiff_ifd[raw].phint) {
  5236. case 2: load_raw = &CLASS kodak_rgb_load_raw; filters = 0; break;
  5237. case 6: load_raw = &CLASS kodak_ycbcr_load_raw; filters = 0; break;
  5238. case 32803: load_raw = &CLASS kodak_65000_load_raw;
  5239. }
  5240. case 32867: break;
  5241. default: is_raw = 0;
  5242. }
  5243. if (!dng_version)
  5244. if ( (tiff_samples == 3 && tiff_ifd[raw].bytes &&
  5245. tiff_bps != 14 && tiff_bps != 2048)
  5246. || (tiff_bps == 8 && !strstr(make,"KODAK") && !strstr(make,"Kodak") &&
  5247. !strstr(model2,"DEBUG RAW")))
  5248. is_raw = 0;
  5249. for (i=0; i < tiff_nifds; i++)
  5250. if (i != raw && tiff_ifd[i].samples == max_samp &&
  5251. tiff_ifd[i].t_width * tiff_ifd[i].t_height / SQR(tiff_ifd[i].bps+1) >
  5252. thumb_width * thumb_height / SQR(thumb_misc+1)) {
  5253. thumb_width = tiff_ifd[i].t_width;
  5254. thumb_height = tiff_ifd[i].t_height;
  5255. thumb_offset = tiff_ifd[i].offset;
  5256. thumb_length = tiff_ifd[i].bytes;
  5257. thumb_misc = tiff_ifd[i].bps;
  5258. thm = i;
  5259. }
  5260. if (thm >= 0) {
  5261. thumb_misc |= tiff_ifd[thm].samples << 5;
  5262. switch (tiff_ifd[thm].comp) {
  5263. case 0:
  5264. write_thumb = &CLASS layer_thumb;
  5265. break;
  5266. case 1:
  5267. if (tiff_ifd[thm].bps > 8)
  5268. thumb_load_raw = &CLASS kodak_thumb_load_raw;
  5269. else
  5270. write_thumb = &CLASS ppm_thumb;
  5271. break;
  5272. case 65000:
  5273. thumb_load_raw = tiff_ifd[thm].phint == 6 ?
  5274. &CLASS kodak_ycbcr_load_raw : &CLASS kodak_rgb_load_raw;
  5275. }
  5276. }
  5277. }
  5278. void CLASS parse_minolta (int base)
  5279. {
  5280. int save, tag, len, offset, high=0, wide=0, i, c;
  5281. short sorder=order;
  5282. fseek (ifp, base, SEEK_SET);
  5283. if (fgetc(ifp) || fgetc(ifp)-'M' || fgetc(ifp)-'R') return;
  5284. order = fgetc(ifp) * 0x101;
  5285. offset = base + get4() + 8;
  5286. while ((save=ftell(ifp)) < offset) {
  5287. for (tag=i=0; i < 4; i++)
  5288. tag = tag << 8 | fgetc(ifp);
  5289. len = get4();
  5290. switch (tag) {
  5291. case 0x505244: /* PRD */
  5292. fseek (ifp, 8, SEEK_CUR);
  5293. high = get2();
  5294. wide = get2();
  5295. break;
  5296. case 0x574247: /* WBG */
  5297. get4();
  5298. i = strcmp(model,"DiMAGE A200") ? 0:3;
  5299. FORC4 cam_mul[c ^ (c >> 1) ^ i] = get2();
  5300. #ifdef LIBRAW_LIBRARY_BUILD
  5301. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5302. #endif
  5303. break;
  5304. case 0x545457: /* TTW */
  5305. parse_tiff (ftell(ifp));
  5306. data_offset = offset;
  5307. }
  5308. fseek (ifp, save+len+8, SEEK_SET);
  5309. }
  5310. raw_height = high;
  5311. raw_width = wide;
  5312. order = sorder;
  5313. }
  5314. /*
  5315. Many cameras have a "debug mode" that writes JPEG and raw
  5316. at the same time. The raw file has no header, so try to
  5317. to open the matching JPEG file and read its metadata.
  5318. */
  5319. void CLASS parse_external_jpeg()
  5320. {
  5321. const char *file, *ext;
  5322. char *jname, *jfile, *jext;
  5323. #ifndef LIBRAW_LIBRARY_BUILD
  5324. FILE *save=ifp;
  5325. #else
  5326. if(!ifp->fname())
  5327. {
  5328. imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA ;
  5329. return;
  5330. }
  5331. #endif
  5332. ext = strrchr (ifname, '.');
  5333. file = strrchr (ifname, '/');
  5334. if (!file) file = strrchr (ifname, '\\');
  5335. #ifndef LIBRAW_LIBRARY_BUILD
  5336. if (!file) file = ifname-1;
  5337. #else
  5338. if (!file) file = (char*)ifname-1;
  5339. #endif
  5340. file++;
  5341. if (!ext || strlen(ext) != 4 || ext-file != 8) return;
  5342. jname = (char *) malloc (strlen(ifname) + 1);
  5343. merror (jname, "parse_external_jpeg()");
  5344. strcpy (jname, ifname);
  5345. jfile = file - ifname + jname;
  5346. jext = ext - ifname + jname;
  5347. if (strcasecmp (ext, ".jpg")) {
  5348. strcpy (jext, isupper(ext[1]) ? ".JPG":".jpg");
  5349. if (isdigit(*file)) {
  5350. memcpy (jfile, file+4, 4);
  5351. memcpy (jfile+4, file, 4);
  5352. }
  5353. } else
  5354. while (isdigit(*--jext)) {
  5355. if (*jext != '9') {
  5356. (*jext)++;
  5357. break;
  5358. }
  5359. *jext = '0';
  5360. }
  5361. #ifndef LIBRAW_LIBRARY_BUILD
  5362. if (strcmp (jname, ifname)) {
  5363. if ((ifp = fopen (jname, "rb"))) {
  5364. #ifdef DCRAW_VERBOSE
  5365. if (verbose)
  5366. fprintf (stderr,_("Reading metadata from %s ...\n"), jname);
  5367. #endif
  5368. parse_tiff (12);
  5369. thumb_offset = 0;
  5370. is_raw = 1;
  5371. fclose (ifp);
  5372. }
  5373. }
  5374. #else
  5375. if (strcmp (jname, ifname))
  5376. {
  5377. if(!ifp->subfile_open(jname))
  5378. {
  5379. parse_tiff (12);
  5380. thumb_offset = 0;
  5381. is_raw = 1;
  5382. ifp->subfile_close();
  5383. }
  5384. else
  5385. imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA ;
  5386. }
  5387. #endif
  5388. if (!timestamp)
  5389. {
  5390. #ifdef LIBRAW_LIBRARY_BUILD
  5391. imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA ;
  5392. #endif
  5393. #ifdef DCRAW_VERBOSE
  5394. fprintf (stderr,_("Failed to read metadata from %s\n"), jname);
  5395. #endif
  5396. }
  5397. free (jname);
  5398. #ifndef LIBRAW_LIBRARY_BUILD
  5399. ifp = save;
  5400. #endif
  5401. }
  5402. /*
  5403. CIFF block 0x1030 contains an 8x8 white sample.
  5404. Load this into white[][] for use in scale_colors().
  5405. */
  5406. void CLASS ciff_block_1030()
  5407. {
  5408. static const ushort key[] = { 0x410, 0x45f3 };
  5409. int i, bpp, row, col, vbits=0;
  5410. unsigned long bitbuf=0;
  5411. if ((get2(),get4()) != 0x80008 || !get4()) return;
  5412. bpp = get2();
  5413. if (bpp != 10 && bpp != 12) return;
  5414. for (i=row=0; row < 8; row++)
  5415. for (col=0; col < 8; col++) {
  5416. if (vbits < bpp) {
  5417. bitbuf = bitbuf << 16 | (get2() ^ key[i++ & 1]);
  5418. vbits += 16;
  5419. }
  5420. white[row][col] =
  5421. bitbuf << (LONG_BIT - vbits) >> (LONG_BIT - bpp);
  5422. vbits -= bpp;
  5423. }
  5424. }
  5425. /*
  5426. Parse a CIFF file, better known as Canon CRW format.
  5427. */
  5428. void CLASS parse_ciff (int offset, int length)
  5429. {
  5430. int tboff, nrecs, c, type, len, save, wbi=-1;
  5431. ushort key[] = { 0x410, 0x45f3 };
  5432. fseek (ifp, offset+length-4, SEEK_SET);
  5433. tboff = get4() + offset;
  5434. fseek (ifp, tboff, SEEK_SET);
  5435. nrecs = get2();
  5436. if (nrecs > 100) return;
  5437. while (nrecs--) {
  5438. type = get2();
  5439. len = get4();
  5440. save = ftell(ifp) + 4;
  5441. fseek (ifp, offset+get4(), SEEK_SET);
  5442. if ((((type >> 8) + 8) | 8) == 0x38)
  5443. parse_ciff (ftell(ifp), len); /* Parse a sub-table */
  5444. if (type == 0x0810)
  5445. fread (artist, 64, 1, ifp);
  5446. if (type == 0x080a) {
  5447. fread (make, 64, 1, ifp);
  5448. fseek (ifp, ((INT64)strlen(make)) - 63, SEEK_CUR);
  5449. fread (model, 64, 1, ifp);
  5450. }
  5451. if (type == 0x1810) {
  5452. fseek (ifp, 12, SEEK_CUR);
  5453. flip = get4();
  5454. }
  5455. if (type == 0x1835) /* Get the decoder table */
  5456. tiff_compress = get4();
  5457. if (type == 0x2007) {
  5458. thumb_offset = ftell(ifp);
  5459. thumb_length = len;
  5460. }
  5461. if (type == 0x1818) {
  5462. shutter = pow (2.0f, -int_to_float((get4(),get4())));
  5463. aperture = pow (2.0f, int_to_float(get4())/2);
  5464. }
  5465. if (type == 0x102a) {
  5466. iso_speed = pow (2, (get4(),get2())/32.0 - 4) * 50;
  5467. aperture = pow (2, (get2(),(short)get2())/64.0);
  5468. shutter = pow (2,-((short)get2())/32.0);
  5469. wbi = (get2(),get2());
  5470. if (wbi > 17) wbi = 0;
  5471. fseek (ifp, 32, SEEK_CUR);
  5472. if (shutter > 1e6) shutter = get2()/10.0;
  5473. }
  5474. if (type == 0x102c) {
  5475. if (get2() > 512) { /* Pro90, G1 */
  5476. fseek (ifp, 118, SEEK_CUR);
  5477. FORC4 cam_mul[c ^ 2] = get2();
  5478. #ifdef LIBRAW_LIBRARY_BUILD
  5479. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5480. #endif
  5481. } else { /* G2, S30, S40 */
  5482. fseek (ifp, 98, SEEK_CUR);
  5483. FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get2();
  5484. #ifdef LIBRAW_LIBRARY_BUILD
  5485. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5486. #endif
  5487. }
  5488. }
  5489. if (type == 0x0032) {
  5490. if (len == 768) { /* EOS D30 */
  5491. fseek (ifp, 72, SEEK_CUR);
  5492. FORC4 cam_mul[c ^ (c >> 1)] = 1024.0 / get2();
  5493. #ifdef LIBRAW_LIBRARY_BUILD
  5494. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5495. #endif
  5496. if (!wbi) cam_mul[0] = -1; /* use my auto white balance */
  5497. } else if (!cam_mul[0]) {
  5498. if (get2() == key[0]) /* Pro1, G6, S60, S70 */
  5499. c = (strstr(model,"Pro1") ?
  5500. "012346000000000000":"01345:000000006008")[wbi]-'0'+ 2;
  5501. else { /* G3, G5, S45, S50 */
  5502. c = "023457000000006000"[wbi]-'0';
  5503. key[0] = key[1] = 0;
  5504. }
  5505. fseek (ifp, 78 + c*8, SEEK_CUR);
  5506. FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get2() ^ key[c & 1];
  5507. #ifdef LIBRAW_LIBRARY_BUILD
  5508. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5509. #endif
  5510. if (!wbi) cam_mul[0] = -1;
  5511. }
  5512. }
  5513. if (type == 0x10a9) { /* D60, 10D, 300D, and clones */
  5514. if (len > 66) wbi = "0134567028"[wbi]-'0';
  5515. fseek (ifp, 2 + wbi*8, SEEK_CUR);
  5516. FORC4 cam_mul[c ^ (c >> 1)] = get2();
  5517. #ifdef LIBRAW_LIBRARY_BUILD
  5518. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5519. #endif
  5520. }
  5521. if (type == 0x1030 && (0x18040 >> wbi & 1))
  5522. ciff_block_1030(); /* all that don't have 0x10a9 */
  5523. if (type == 0x1031) {
  5524. raw_width = (get2(),get2());
  5525. raw_height = get2();
  5526. }
  5527. if (type == 0x5029) {
  5528. focal_len = len >> 16;
  5529. if ((len & 0xffff) == 2) focal_len /= 32;
  5530. }
  5531. if (type == 0x5813) flash_used = int_to_float(len);
  5532. if (type == 0x5814) canon_ev = int_to_float(len);
  5533. if (type == 0x5817) shot_order = len;
  5534. if (type == 0x5834) unique_id = len;
  5535. if (type == 0x580e) timestamp = len;
  5536. if (type == 0x180e) timestamp = get4();
  5537. #ifdef LOCALTIME
  5538. if ((type | 0x4000) == 0x580e)
  5539. timestamp = mktime (gmtime (&timestamp));
  5540. #endif
  5541. fseek (ifp, save, SEEK_SET);
  5542. }
  5543. }
  5544. void CLASS parse_rollei()
  5545. {
  5546. char line[128], *val;
  5547. struct tm t;
  5548. fseek (ifp, 0, SEEK_SET);
  5549. memset (&t, 0, sizeof t);
  5550. do {
  5551. fgets (line, 128, ifp);
  5552. if ((val = strchr(line,'=')))
  5553. *val++ = 0;
  5554. else
  5555. val = line + strlen(line);
  5556. if (!strcmp(line,"DAT"))
  5557. sscanf (val, "%d.%d.%d", &t.tm_mday, &t.tm_mon, &t.tm_year);
  5558. if (!strcmp(line,"TIM"))
  5559. sscanf (val, "%d:%d:%d", &t.tm_hour, &t.tm_min, &t.tm_sec);
  5560. if (!strcmp(line,"HDR"))
  5561. thumb_offset = atoi(val);
  5562. if (!strcmp(line,"X "))
  5563. raw_width = atoi(val);
  5564. if (!strcmp(line,"Y "))
  5565. raw_height = atoi(val);
  5566. if (!strcmp(line,"TX "))
  5567. thumb_width = atoi(val);
  5568. if (!strcmp(line,"TY "))
  5569. thumb_height = atoi(val);
  5570. } while (strncmp(line,"EOHD",4));
  5571. data_offset = thumb_offset + thumb_width * thumb_height * 2;
  5572. t.tm_year -= 1900;
  5573. t.tm_mon -= 1;
  5574. if (mktime(&t) > 0)
  5575. timestamp = mktime(&t);
  5576. strcpy (make, "Rollei");
  5577. strcpy (model,"d530flex");
  5578. write_thumb = &CLASS rollei_thumb;
  5579. }
  5580. void CLASS parse_sinar_ia()
  5581. {
  5582. int entries, off;
  5583. char str[8], *cp;
  5584. order = 0x4949;
  5585. fseek (ifp, 4, SEEK_SET);
  5586. entries = get4();
  5587. fseek (ifp, get4(), SEEK_SET);
  5588. while (entries--) {
  5589. off = get4(); get4();
  5590. fread (str, 8, 1, ifp);
  5591. if (!strcmp(str,"META")) meta_offset = off;
  5592. if (!strcmp(str,"THUMB")) thumb_offset = off;
  5593. if (!strcmp(str,"RAW0")) data_offset = off;
  5594. }
  5595. fseek (ifp, meta_offset+20, SEEK_SET);
  5596. fread (make, 64, 1, ifp);
  5597. make[63] = 0;
  5598. if ((cp = strchr(make,' '))) {
  5599. strcpy (model, cp+1);
  5600. *cp = 0;
  5601. }
  5602. raw_width = get2();
  5603. raw_height = get2();
  5604. load_raw = &CLASS unpacked_load_raw;
  5605. thumb_width = (get4(),get2());
  5606. thumb_height = get2();
  5607. write_thumb = &CLASS ppm_thumb;
  5608. maximum = 0x3fff;
  5609. }
  5610. void CLASS parse_phase_one (int base)
  5611. {
  5612. unsigned entries, tag, type, len, data, save, i, c;
  5613. float romm_cam[3][3];
  5614. char *cp;
  5615. memset (&ph1, 0, sizeof ph1);
  5616. fseek (ifp, base, SEEK_SET);
  5617. order = get4() & 0xffff;
  5618. if (get4() >> 8 != 0x526177) return; /* "Raw" */
  5619. fseek (ifp, get4()+base, SEEK_SET);
  5620. entries = get4();
  5621. get4();
  5622. while (entries--) {
  5623. tag = get4();
  5624. type = get4();
  5625. len = get4();
  5626. data = get4();
  5627. save = ftell(ifp);
  5628. fseek (ifp, base+data, SEEK_SET);
  5629. switch (tag) {
  5630. case 0x100: flip = "0653"[data & 3]-'0'; break;
  5631. case 0x106:
  5632. for (i=0; i < 9; i++)
  5633. romm_cam[0][i] = getreal(11);
  5634. romm_coeff (romm_cam);
  5635. break;
  5636. case 0x107:
  5637. FORC3 cam_mul[c] = getreal(11);
  5638. #ifdef LIBRAW_LIBRARY_BUILD
  5639. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5640. #endif
  5641. break;
  5642. case 0x108: raw_width = data; break;
  5643. case 0x109: raw_height = data; break;
  5644. case 0x10a: left_margin = data; break;
  5645. case 0x10b: top_margin = data; break;
  5646. case 0x10c: width = data; break;
  5647. case 0x10d: height = data; break;
  5648. case 0x10e: ph1.format = data; break;
  5649. case 0x10f: data_offset = data+base; break;
  5650. case 0x110: meta_offset = data+base;
  5651. meta_length = len; break;
  5652. case 0x112: ph1.key_off = save - 4; break;
  5653. case 0x210: ph1.tag_210 = int_to_float(data); break;
  5654. case 0x21a: ph1.tag_21a = data; break;
  5655. case 0x21c: strip_offset = data+base; break;
  5656. case 0x21d: ph1.t_black = data; break;
  5657. case 0x222: ph1.split_col = data - left_margin; break;
  5658. case 0x223: ph1.black_off = data+base; break;
  5659. case 0x301:
  5660. model[63] = 0;
  5661. fread (model, 1, 63, ifp);
  5662. if ((cp = strstr(model," camera"))) *cp = 0;
  5663. }
  5664. fseek (ifp, save, SEEK_SET);
  5665. }
  5666. load_raw = ph1.format < 3 ?
  5667. &CLASS phase_one_load_raw : &CLASS phase_one_load_raw_c;
  5668. maximum = 0xffff;
  5669. strcpy (make, "Phase One");
  5670. if (model[0]) return;
  5671. switch (raw_height) {
  5672. case 2060: strcpy (model,"LightPhase"); break;
  5673. case 2682: strcpy (model,"H 10"); break;
  5674. case 4128: strcpy (model,"H 20"); break;
  5675. case 5488: strcpy (model,"H 25"); break;
  5676. }
  5677. }
  5678. void CLASS parse_fuji (int offset)
  5679. {
  5680. unsigned entries, tag, len, save, c;
  5681. fseek (ifp, offset, SEEK_SET);
  5682. entries = get4();
  5683. if (entries > 255) return;
  5684. while (entries--) {
  5685. tag = get2();
  5686. len = get2();
  5687. save = ftell(ifp);
  5688. if (tag == 0x100) {
  5689. raw_height = get2();
  5690. raw_width = get2();
  5691. } else if (tag == 0x121) {
  5692. height = get2();
  5693. if ((width = get2()) == 4284) width += 3;
  5694. } else if (tag == 0x130) {
  5695. fuji_layout = fgetc(ifp) >> 7;
  5696. load_raw = fgetc(ifp) & 8 ?
  5697. &CLASS unpacked_load_raw : &CLASS fuji_load_raw;
  5698. }
  5699. if (tag == 0x2ff0)
  5700. {
  5701. FORC4 cam_mul[c ^ 1] = get2();
  5702. #ifdef LIBRAW_LIBRARY_BUILD
  5703. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5704. #endif
  5705. }
  5706. fseek (ifp, save+len, SEEK_SET);
  5707. }
  5708. if (!raw_height) {
  5709. filters = 0x16161616;
  5710. load_raw = &CLASS packed_load_raw;
  5711. load_flags = 24;
  5712. }
  5713. height <<= fuji_layout;
  5714. width >>= fuji_layout;
  5715. }
  5716. int CLASS parse_jpeg (int offset)
  5717. {
  5718. int len, save, hlen, mark;
  5719. fseek (ifp, offset, SEEK_SET);
  5720. if (fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8) return 0;
  5721. while (fgetc(ifp) == 0xff && (mark = fgetc(ifp)) != 0xda) {
  5722. order = 0x4d4d;
  5723. len = get2() - 2;
  5724. save = ftell(ifp);
  5725. if (mark == 0xc0 || mark == 0xc3) {
  5726. fgetc(ifp);
  5727. raw_height = get2();
  5728. raw_width = get2();
  5729. }
  5730. order = get2();
  5731. hlen = get4();
  5732. if (get4() == 0x48454150) /* "HEAP" */
  5733. parse_ciff (save+hlen, len-hlen);
  5734. if (parse_tiff (save+6)) apply_tiff();
  5735. fseek (ifp, save+len, SEEK_SET);
  5736. }
  5737. return 1;
  5738. }
  5739. void CLASS parse_riff()
  5740. {
  5741. unsigned i, size, end;
  5742. char tag[4], date[64], month[64];
  5743. static const char mon[12][4] =
  5744. { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
  5745. struct tm t;
  5746. order = 0x4949;
  5747. fread (tag, 4, 1, ifp);
  5748. size = get4();
  5749. end = ftell(ifp) + size;
  5750. if (!memcmp(tag,"RIFF",4) || !memcmp(tag,"LIST",4)) {
  5751. int cnt=0;
  5752. get4();
  5753. while (ftell(ifp)+7 < end)
  5754. {
  5755. parse_riff();
  5756. if(cnt++>10000) break; // no more than 10k times
  5757. }
  5758. } else if (!memcmp(tag,"nctg",4)) {
  5759. while (ftell(ifp)+7 < end) {
  5760. i = get2();
  5761. size = get2();
  5762. if ((i+1) >> 1 == 10 && size == 20)
  5763. get_timestamp(0);
  5764. else fseek (ifp, size, SEEK_CUR);
  5765. }
  5766. } else if (!memcmp(tag,"IDIT",4) && size < 64) {
  5767. fread (date, 64, 1, ifp);
  5768. date[size] = 0;
  5769. memset (&t, 0, sizeof t);
  5770. if (sscanf (date, "%*s %s %d %d:%d:%d %d", month, &t.tm_mday,
  5771. &t.tm_hour, &t.tm_min, &t.tm_sec, &t.tm_year) == 6) {
  5772. for (i=0; i < 12 && strcasecmp(mon[i],month); i++);
  5773. t.tm_mon = i;
  5774. t.tm_year -= 1900;
  5775. if (mktime(&t) > 0)
  5776. timestamp = mktime(&t);
  5777. }
  5778. } else
  5779. fseek (ifp, size, SEEK_CUR);
  5780. }
  5781. void CLASS parse_smal (int offset, int fsize)
  5782. {
  5783. int ver;
  5784. fseek (ifp, offset+2, SEEK_SET);
  5785. order = 0x4949;
  5786. ver = fgetc(ifp);
  5787. if (ver == 6)
  5788. fseek (ifp, 5, SEEK_CUR);
  5789. if (get4() != fsize) return;
  5790. if (ver > 6) data_offset = get4();
  5791. raw_height = height = get2();
  5792. raw_width = width = get2();
  5793. strcpy (make, "SMaL");
  5794. sprintf (model, "v%d %dx%d", ver, width, height);
  5795. if (ver == 6) load_raw = &CLASS smal_v6_load_raw;
  5796. if (ver == 9) load_raw = &CLASS smal_v9_load_raw;
  5797. }
  5798. void CLASS parse_cine()
  5799. {
  5800. unsigned off_head, off_setup, off_image, i;
  5801. order = 0x4949;
  5802. fseek (ifp, 4, SEEK_SET);
  5803. is_raw = get2() == 2;
  5804. fseek (ifp, 14, SEEK_CUR);
  5805. is_raw *= get4();
  5806. off_head = get4();
  5807. off_setup = get4();
  5808. off_image = get4();
  5809. timestamp = get4();
  5810. if ((i = get4())) timestamp = i;
  5811. fseek (ifp, off_head+4, SEEK_SET);
  5812. raw_width = get4();
  5813. raw_height = get4();
  5814. switch (get2(),get2()) {
  5815. case 8: load_raw = &CLASS eight_bit_load_raw; break;
  5816. case 16: load_raw = &CLASS unpacked_load_raw;
  5817. }
  5818. fseek (ifp, off_setup+792, SEEK_SET);
  5819. strcpy (make, "CINE");
  5820. sprintf (model, "%d", get4());
  5821. fseek (ifp, 12, SEEK_CUR);
  5822. switch ((i=get4()) & 0xffffff) {
  5823. case 3: filters = 0x94949494; break;
  5824. case 4: filters = 0x49494949; break;
  5825. default: is_raw = 0;
  5826. }
  5827. fseek (ifp, 72, SEEK_CUR);
  5828. switch ((get4()+3600) % 360) {
  5829. case 270: flip = 4; break;
  5830. case 180: flip = 1; break;
  5831. case 90: flip = 7; break;
  5832. case 0: flip = 2;
  5833. }
  5834. cam_mul[0] = getreal(11);
  5835. cam_mul[2] = getreal(11);
  5836. #ifdef LIBRAW_LIBRARY_BUILD
  5837. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  5838. #endif
  5839. maximum = ~(-1 << get4());
  5840. fseek (ifp, 668, SEEK_CUR);
  5841. shutter = get4()/1000000000.0;
  5842. fseek (ifp, off_image, SEEK_SET);
  5843. if (shot_select < is_raw)
  5844. fseek (ifp, shot_select*8, SEEK_CUR);
  5845. data_offset = (INT64) get4() + 8;
  5846. data_offset += (INT64) get4() << 32;
  5847. }
  5848. #line 7343 "dcraw/dcraw.c"
  5849. void CLASS adobe_coeff (const char *p_make, const char *p_model)
  5850. {
  5851. static const struct {
  5852. const char *prefix;
  5853. short t_black, t_maximum, trans[12];
  5854. } table[] = {
  5855. { "AGFAPHOTO DC-833m", 0, 0, /* DJC */
  5856. { 11438,-3762,-1115,-2409,9914,2497,-1227,2295,5300 } },
  5857. { "Apple QuickTake", 0, 0, /* DJC */
  5858. { 21392,-5653,-3353,2406,8010,-415,7166,1427,2078 } },
  5859. { "Canon EOS D2000", 0, 0,
  5860. { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
  5861. { "Canon EOS D6000", 0, 0,
  5862. { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
  5863. { "Canon EOS D30", 0, 0,
  5864. { 9805,-2689,-1312,-5803,13064,3068,-2438,3075,8775 } },
  5865. { "Canon EOS D60", 0, 0xfa0,
  5866. { 6188,-1341,-890,-7168,14489,2937,-2640,3228,8483 } },
  5867. { "Canon EOS 5D Mark II", 0, 0x3cf0,
  5868. { 4716,603,-830,-7798,15474,2480,-1496,1937,6651 } },
  5869. { "Canon EOS 5D", 0, 0xe6c,
  5870. { 6347,-479,-972,-8297,15954,2480,-1968,2131,7649 } },
  5871. { "Canon EOS 7D", 0, 0x3510,
  5872. { 6844,-996,-856,-3876,11761,2396,-593,1772,6198 } },
  5873. { "Canon EOS 10D", 0, 0xfa0,
  5874. { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
  5875. { "Canon EOS 20Da", 0, 0,
  5876. { 14155,-5065,-1382,-6550,14633,2039,-1623,1824,6561 } },
  5877. { "Canon EOS 20D", 0, 0xfff,
  5878. { 6599,-537,-891,-8071,15783,2424,-1983,2234,7462 } },
  5879. { "Canon EOS 30D", 0, 0,
  5880. { 6257,-303,-1000,-7880,15621,2396,-1714,1904,7046 } },
  5881. { "Canon EOS 40D", 0, 0x3f60,
  5882. { 6071,-747,-856,-7653,15365,2441,-2025,2553,7315 } },
  5883. { "Canon EOS 50D", 0, 0x3d93,
  5884. { 4920,616,-593,-6493,13964,2784,-1774,3178,7005 } },
  5885. { "Canon EOS 300D", 0, 0xfa0,
  5886. { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
  5887. { "Canon EOS 350D", 0, 0xfff,
  5888. { 6018,-617,-965,-8645,15881,2975,-1530,1719,7642 } },
  5889. { "Canon EOS 400D", 0, 0xe8e,
  5890. { 7054,-1501,-990,-8156,15544,2812,-1278,1414,7796 } },
  5891. { "Canon EOS 450D", 0, 0x390d,
  5892. { 5784,-262,-821,-7539,15064,2672,-1982,2681,7427 } },
  5893. { "Canon EOS 500D", 0, 0x3479,
  5894. { 4763,712,-646,-6821,14399,2640,-1921,3276,6561 } },
  5895. { "Canon EOS 550D", 0, 0x3dd7,
  5896. { 6941,-1164,-857,-3825,11597,2534,-416,1540,6039 } },
  5897. { "Canon EOS 1000D", 0, 0xe43,
  5898. { 6771,-1139,-977,-7818,15123,2928,-1244,1437,7533 } },
  5899. { "Canon EOS-1Ds Mark III", 0, 0x3bb0,
  5900. { 5859,-211,-930,-8255,16017,2353,-1732,1887,7448 } },
  5901. { "Canon EOS-1Ds Mark II", 0, 0xe80,
  5902. { 6517,-602,-867,-8180,15926,2378,-1618,1771,7633 } },
  5903. { "Canon EOS-1D Mark IV", 0, 0x3bb0,
  5904. { 6014,-220,-795,-4109,12014,2361,-561,1824,5787 } },
  5905. { "Canon EOS-1D Mark III", 0, 0x3bb0,
  5906. { 6291,-540,-976,-8350,16145,2311,-1714,1858,7326 } },
  5907. { "Canon EOS-1D Mark II N", 0, 0xe80,
  5908. { 6240,-466,-822,-8180,15825,2500,-1801,1938,8042 } },
  5909. { "Canon EOS-1D Mark II", 0, 0xe80,
  5910. { 6264,-582,-724,-8312,15948,2504,-1744,1919,8664 } },
  5911. { "Canon EOS-1DS", 0, 0xe20,
  5912. { 4374,3631,-1743,-7520,15212,2472,-2892,3632,8161 } },
  5913. { "Canon EOS-1D", 0, 0xe20,
  5914. { 6806,-179,-1020,-8097,16415,1687,-3267,4236,7690 } },
  5915. { "Canon EOS", 0, 0,
  5916. { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
  5917. { "Canon PowerShot A530", 0, 0,
  5918. { 0 } }, /* don't want the A5 matrix */
  5919. { "Canon PowerShot A50", 0, 0,
  5920. { -5300,9846,1776,3436,684,3939,-5540,9879,6200,-1404,11175,217 } },
  5921. { "Canon PowerShot A5", 0, 0,
  5922. { -4801,9475,1952,2926,1611,4094,-5259,10164,5947,-1554,10883,547 } },
  5923. { "Canon PowerShot G10", 0, 0,
  5924. { 11093,-3906,-1028,-5047,12492,2879,-1003,1750,5561 } },
  5925. { "Canon PowerShot G11", 0, 0,
  5926. { 12177,-4817,-1069,-1612,9864,2049,-98,850,4471 } },
  5927. { "Canon PowerShot G1", 0, 0,
  5928. { -4778,9467,2172,4743,-1141,4344,-5146,9908,6077,-1566,11051,557 } },
  5929. { "Canon PowerShot G2", 0, 0,
  5930. { 9087,-2693,-1049,-6715,14382,2537,-2291,2819,7790 } },
  5931. { "Canon PowerShot G3", 0, 0,
  5932. { 9212,-2781,-1073,-6573,14189,2605,-2300,2844,7664 } },
  5933. { "Canon PowerShot G5", 0, 0,
  5934. { 9757,-2872,-933,-5972,13861,2301,-1622,2328,7212 } },
  5935. { "Canon PowerShot G6", 0, 0,
  5936. { 9877,-3775,-871,-7613,14807,3072,-1448,1305,7485 } },
  5937. { "Canon PowerShot G9", 0, 0,
  5938. { 7368,-2141,-598,-5621,13254,2625,-1418,1696,5743 } },
  5939. { "Canon PowerShot Pro1", 0, 0,
  5940. { 10062,-3522,-999,-7643,15117,2730,-765,817,7323 } },
  5941. { "Canon PowerShot Pro70", 34, 0,
  5942. { -4155,9818,1529,3939,-25,4522,-5521,9870,6610,-2238,10873,1342 } },
  5943. { "Canon PowerShot Pro90", 0, 0,
  5944. { -4963,9896,2235,4642,-987,4294,-5162,10011,5859,-1770,11230,577 } },
  5945. { "Canon PowerShot S30", 0, 0,
  5946. { 10566,-3652,-1129,-6552,14662,2006,-2197,2581,7670 } },
  5947. { "Canon PowerShot S40", 0, 0,
  5948. { 8510,-2487,-940,-6869,14231,2900,-2318,2829,9013 } },
  5949. { "Canon PowerShot S45", 0, 0,
  5950. { 8163,-2333,-955,-6682,14174,2751,-2077,2597,8041 } },
  5951. { "Canon PowerShot S50", 0, 0,
  5952. { 8882,-2571,-863,-6348,14234,2288,-1516,2172,6569 } },
  5953. { "Canon PowerShot S60", 0, 0,
  5954. { 8795,-2482,-797,-7804,15403,2573,-1422,1996,7082 } },
  5955. { "Canon PowerShot S70", 0, 0,
  5956. { 9976,-3810,-832,-7115,14463,2906,-901,989,7889 } },
  5957. { "Canon PowerShot S90", 0, 0,
  5958. { 12374,-5016,-1049,-1677,9902,2078,-83,852,4683 } },
  5959. { "Canon PowerShot A470", 0, 0, /* DJC */
  5960. { 12513,-4407,-1242,-2680,10276,2405,-878,2215,4734 } },
  5961. { "Canon PowerShot A610", 0, 0, /* DJC */
  5962. { 15591,-6402,-1592,-5365,13198,2168,-1300,1824,5075 } },
  5963. { "Canon PowerShot A620", 0, 0, /* DJC */
  5964. { 15265,-6193,-1558,-4125,12116,2010,-888,1639,5220 } },
  5965. { "Canon PowerShot A630", 0, 0, /* DJC */
  5966. { 14201,-5308,-1757,-6087,14472,1617,-2191,3105,5348 } },
  5967. { "Canon PowerShot A640", 0, 0, /* DJC */
  5968. { 13124,-5329,-1390,-3602,11658,1944,-1612,2863,4885 } },
  5969. { "Canon PowerShot A650", 0, 0, /* DJC */
  5970. { 9427,-3036,-959,-2581,10671,1911,-1039,1982,4430 } },
  5971. { "Canon PowerShot A720", 0, 0, /* DJC */
  5972. { 14573,-5482,-1546,-1266,9799,1468,-1040,1912,3810 } },
  5973. { "Canon PowerShot S3 IS", 0, 0, /* DJC */
  5974. { 14062,-5199,-1446,-4712,12470,2243,-1286,2028,4836 } },
  5975. { "Canon PowerShot SX1 IS", 0, 0,
  5976. { 6578,-259,-502,-5974,13030,3309,-308,1058,4970 } },
  5977. { "Canon PowerShot SX110 IS", 0, 0, /* DJC */
  5978. { 14134,-5576,-1527,-1991,10719,1273,-1158,1929,3581 } },
  5979. { "CASIO EX-S20", 0, 0, /* DJC */
  5980. { 11634,-3924,-1128,-4968,12954,2015,-1588,2648,7206 } },
  5981. { "CASIO EX-Z750", 0, 0, /* DJC */
  5982. { 10819,-3873,-1099,-4903,13730,1175,-1755,3751,4632 } },
  5983. { "CINE 650", 0, 0,
  5984. { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
  5985. { "CINE 660", 0, 0,
  5986. { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
  5987. { "CINE", 0, 0,
  5988. { 20183,-4295,-423,-3940,15330,3985,-280,4870,9800 } },
  5989. { "Contax N Digital", 0, 0xf1e,
  5990. { 7777,1285,-1053,-9280,16543,2916,-3677,5679,7060 } },
  5991. { "EPSON R-D1", 0, 0,
  5992. { 6827,-1878,-732,-8429,16012,2564,-704,592,7145 } },
  5993. { "FUJIFILM FinePix E550", 0, 0,
  5994. { 11044,-3888,-1120,-7248,15168,2208,-1531,2277,8069 } },
  5995. { "FUJIFILM FinePix E900", 0, 0,
  5996. { 9183,-2526,-1078,-7461,15071,2574,-2022,2440,8639 } },
  5997. { "FUJIFILM FinePix F8", 0, 0,
  5998. { 11044,-3888,-1120,-7248,15168,2208,-1531,2277,8069 } },
  5999. { "FUJIFILM FinePix F7", 0, 0,
  6000. { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
  6001. { "FUJIFILM FinePix S100FS", 514, 0,
  6002. { 11521,-4355,-1065,-6524,13767,3058,-1466,1984,6045 } },
  6003. { "FUJIFILM FinePix S200EXR", 512, 0x3fff,
  6004. { 0 } },
  6005. { "FUJIFILM FinePix S20Pro", 0, 0,
  6006. { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
  6007. { "FUJIFILM FinePix S2Pro", 128, 0,
  6008. { 12492,-4690,-1402,-7033,15423,1647,-1507,2111,7697 } },
  6009. { "FUJIFILM FinePix S3Pro", 0, 0,
  6010. { 11807,-4612,-1294,-8927,16968,1988,-2120,2741,8006 } },
  6011. { "FUJIFILM FinePix S5Pro", 0, 0,
  6012. { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
  6013. { "FUJIFILM FinePix S5000", 0, 0,
  6014. { 8754,-2732,-1019,-7204,15069,2276,-1702,2334,6982 } },
  6015. { "FUJIFILM FinePix S5100", 0, 0,
  6016. { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
  6017. { "FUJIFILM FinePix S5500", 0, 0,
  6018. { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
  6019. { "FUJIFILM FinePix S5200", 0, 0,
  6020. { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
  6021. { "FUJIFILM FinePix S5600", 0, 0,
  6022. { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
  6023. { "FUJIFILM FinePix S6", 0, 0,
  6024. { 12628,-4887,-1401,-6861,14996,1962,-2198,2782,7091 } },
  6025. { "FUJIFILM FinePix S7000", 0, 0,
  6026. { 10190,-3506,-1312,-7153,15051,2238,-2003,2399,7505 } },
  6027. { "FUJIFILM FinePix S9000", 0, 0,
  6028. { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
  6029. { "FUJIFILM FinePix S9500", 0, 0,
  6030. { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
  6031. { "FUJIFILM FinePix S9100", 0, 0,
  6032. { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
  6033. { "FUJIFILM FinePix S9600", 0, 0,
  6034. { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
  6035. { "FUJIFILM IS-1", 0, 0,
  6036. { 21461,-10807,-1441,-2332,10599,1999,289,875,7703 } },
  6037. { "FUJIFILM IS Pro", 0, 0,
  6038. { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
  6039. { "FUJIFILM FinePix HS10 HS11", 0, 0xf68, /* DJC */
  6040. { 12164,-3169,-1662,-1020,10358,662,-224,2108,3106 } },
  6041. { "Imacon Ixpress", 0, 0, /* DJC */
  6042. { 7025,-1415,-704,-5188,13765,1424,-1248,2742,6038 } },
  6043. { "KODAK NC2000", 0, 0,
  6044. { 13891,-6055,-803,-465,9919,642,2121,82,1291 } },
  6045. { "Kodak DCS315C", 8, 0,
  6046. { 17523,-4827,-2510,756,8546,-137,6113,1649,2250 } },
  6047. { "Kodak DCS330C", 8, 0,
  6048. { 20620,-7572,-2801,-103,10073,-396,3551,-233,2220 } },
  6049. { "KODAK DCS420", 0, 0,
  6050. { 10868,-1852,-644,-1537,11083,484,2343,628,2216 } },
  6051. { "KODAK DCS460", 0, 0,
  6052. { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
  6053. { "KODAK EOSDCS1", 0, 0,
  6054. { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
  6055. { "KODAK EOSDCS3B", 0, 0,
  6056. { 9898,-2700,-940,-2478,12219,206,1985,634,1031 } },
  6057. { "Kodak DCS520C", 180, 0,
  6058. { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
  6059. { "Kodak DCS560C", 188, 0,
  6060. { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
  6061. { "Kodak DCS620C", 180, 0,
  6062. { 23617,-10175,-3149,-2054,11749,-272,2586,-489,3453 } },
  6063. { "Kodak DCS620X", 185, 0,
  6064. { 13095,-6231,154,12221,-21,-2137,895,4602,2258 } },
  6065. { "Kodak DCS660C", 214, 0,
  6066. { 18244,-6351,-2739,-791,11193,-521,3711,-129,2802 } },
  6067. { "Kodak DCS720X", 0, 0,
  6068. { 11775,-5884,950,9556,1846,-1286,-1019,6221,2728 } },
  6069. { "Kodak DCS760C", 0, 0,
  6070. { 16623,-6309,-1411,-4344,13923,323,2285,274,2926 } },
  6071. { "Kodak DCS Pro SLR", 0, 0,
  6072. { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
  6073. { "Kodak DCS Pro 14nx", 0, 0,
  6074. { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
  6075. { "Kodak DCS Pro 14", 0, 0,
  6076. { 7791,3128,-776,-8588,16458,2039,-2455,4006,6198 } },
  6077. { "Kodak ProBack645", 0, 0,
  6078. { 16414,-6060,-1470,-3555,13037,473,2545,122,4948 } },
  6079. { "Kodak ProBack", 0, 0,
  6080. { 21179,-8316,-2918,-915,11019,-165,3477,-180,4210 } },
  6081. { "KODAK P712", 0, 0,
  6082. { 9658,-3314,-823,-5163,12695,2768,-1342,1843,6044 } },
  6083. { "KODAK P850", 0, 0xf7c,
  6084. { 10511,-3836,-1102,-6946,14587,2558,-1481,1792,6246 } },
  6085. { "KODAK P880", 0, 0xfff,
  6086. { 12805,-4662,-1376,-7480,15267,2360,-1626,2194,7904 } },
  6087. { "KODAK EasyShare Z980", 0, 0,
  6088. { 11313,-3559,-1101,-3893,11891,2257,-1214,2398,4908 } },
  6089. { "KODAK EasyShare Z981", 0, 0,
  6090. { 12729,-4717,-1188,-1367,9187,2582,274,860,4411 } },
  6091. { "KODAK EASYSHARE Z1015", 0, 0xef1,
  6092. { 11265,-4286,-992,-4694,12343,2647,-1090,1523,5447 } },
  6093. { "Leaf CMost", 0, 0,
  6094. { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
  6095. { "Leaf Valeo 6", 0, 0,
  6096. { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
  6097. { "Leaf Aptus 54S", 0, 0,
  6098. { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
  6099. { "Leaf Aptus 65", 0, 0,
  6100. { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
  6101. { "Leaf Aptus 75", 0, 0,
  6102. { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
  6103. { "Leaf", 0, 0,
  6104. { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
  6105. { "Mamiya ZD", 0, 0,
  6106. { 7645,2579,-1363,-8689,16717,2015,-3712,5941,5961 } },
  6107. { "Micron 2010", 110, 0, /* DJC */
  6108. { 16695,-3761,-2151,155,9682,163,3433,951,4904 } },
  6109. { "Minolta DiMAGE 5", 0, 0xf7d,
  6110. { 8983,-2942,-963,-6556,14476,2237,-2426,2887,8014 } },
  6111. { "Minolta DiMAGE 7Hi", 0, 0xf7d,
  6112. { 11368,-3894,-1242,-6521,14358,2339,-2475,3056,7285 } },
  6113. { "Minolta DiMAGE 7", 0, 0xf7d,
  6114. { 9144,-2777,-998,-6676,14556,2281,-2470,3019,7744 } },
  6115. { "Minolta DiMAGE A1", 0, 0xf8b,
  6116. { 9274,-2547,-1167,-8220,16323,1943,-2273,2720,8340 } },
  6117. { "MINOLTA DiMAGE A200", 0, 0,
  6118. { 8560,-2487,-986,-8112,15535,2771,-1209,1324,7743 } },
  6119. { "Minolta DiMAGE A2", 0, 0xf8f,
  6120. { 9097,-2726,-1053,-8073,15506,2762,-966,981,7763 } },
  6121. { "Minolta DiMAGE Z2", 0, 0, /* DJC */
  6122. { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
  6123. { "MINOLTA DYNAX 5", 0, 0xffb,
  6124. { 10284,-3283,-1086,-7957,15762,2316,-829,882,6644 } },
  6125. { "MINOLTA DYNAX 7", 0, 0xffb,
  6126. { 10239,-3104,-1099,-8037,15727,2451,-927,925,6871 } },
  6127. { "MOTOROLA PIXL", 0, 0, /* DJC */
  6128. { 8898,-989,-1033,-3292,11619,1674,-661,3178,5216 } },
  6129. { "NIKON D100", 0, 0,
  6130. { 5902,-933,-782,-8983,16719,2354,-1402,1455,6464 } },
  6131. { "NIKON D1H", 0, 0,
  6132. { 7577,-2166,-926,-7454,15592,1934,-2377,2808,8606 } },
  6133. { "NIKON D1X", 0, 0,
  6134. { 7702,-2245,-975,-9114,17242,1875,-2679,3055,8521 } },
  6135. { "NIKON D1", 0, 0, /* multiplied by 2.218750, 1.0, 1.148438 */
  6136. { 16772,-4726,-2141,-7611,15713,1972,-2846,3494,9521 } },
  6137. { "NIKON D200", 0, 0xfbc,
  6138. { 8367,-2248,-763,-8758,16447,2422,-1527,1550,8053 } },
  6139. { "NIKON D2H", 0, 0,
  6140. { 5710,-901,-615,-8594,16617,2024,-2975,4120,6830 } },
  6141. { "NIKON D2X", 0, 0,
  6142. { 10231,-2769,-1255,-8301,15900,2552,-797,680,7148 } },
  6143. { "NIKON D3000", 0, 0,
  6144. { 8736,-2458,-935,-9075,16894,2251,-1354,1242,8263 } },
  6145. { "NIKON D300", 0, 0,
  6146. { 9030,-1992,-715,-8465,16302,2255,-2689,3217,8069 } },
  6147. { "NIKON D3X", 0, 0,
  6148. { 7171,-1986,-648,-8085,15555,2718,-2170,2512,7457 } },
  6149. { "NIKON D3S", 0, 0,
  6150. { 8828,-2406,-694,-4874,12603,2541,-660,1509,7587 } },
  6151. { "NIKON D3", 0, 0,
  6152. { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
  6153. { "NIKON D40X", 0, 0,
  6154. { 8819,-2543,-911,-9025,16928,2151,-1329,1213,8449 } },
  6155. { "NIKON D40", 0, 0,
  6156. { 6992,-1668,-806,-8138,15748,2543,-874,850,7897 } },
  6157. { "NIKON D5000", 0, 0xf00,
  6158. { 7309,-1403,-519,-8474,16008,2622,-2433,2826,8064 } },
  6159. { "NIKON D50", 0, 0,
  6160. { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
  6161. { "NIKON D60", 0, 0,
  6162. { 8736,-2458,-935,-9075,16894,2251,-1354,1242,8263 } },
  6163. { "NIKON D700", 0, 0,
  6164. { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
  6165. { "NIKON D70", 0, 0,
  6166. { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
  6167. { "NIKON D80", 0, 0,
  6168. { 8629,-2410,-883,-9055,16940,2171,-1490,1363,8520 } },
  6169. { "NIKON D90", 0, 0xf00,
  6170. { 7309,-1403,-519,-8474,16008,2622,-2434,2826,8064 } },
  6171. { "NIKON E950", 0, 0x3dd, /* DJC */
  6172. { -3746,10611,1665,9621,-1734,2114,-2389,7082,3064,3406,6116,-244 } },
  6173. { "NIKON E995", 0, 0, /* copied from E5000 */
  6174. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  6175. { "NIKON E2100", 0, 0, /* copied from Z2, new white balance */
  6176. { 13142,-4152,-1596,-4655,12374,2282,-1769,2696,6711} },
  6177. { "NIKON E2500", 0, 0,
  6178. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  6179. { "NIKON E4300", 0, 0, /* copied from Minolta DiMAGE Z2 */
  6180. { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
  6181. { "NIKON E4500", 0, 0,
  6182. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  6183. { "NIKON E5000", 0, 0,
  6184. { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
  6185. { "NIKON E5400", 0, 0,
  6186. { 9349,-2987,-1001,-7919,15766,2266,-2098,2680,6839 } },
  6187. { "NIKON E5700", 0, 0,
  6188. { -5368,11478,2368,5537,-113,3148,-4969,10021,5782,778,9028,211 } },
  6189. { "NIKON E8400", 0, 0,
  6190. { 7842,-2320,-992,-8154,15718,2599,-1098,1342,7560 } },
  6191. { "NIKON E8700", 0, 0,
  6192. { 8489,-2583,-1036,-8051,15583,2643,-1307,1407,7354 } },
  6193. { "NIKON E8800", 0, 0,
  6194. { 7971,-2314,-913,-8451,15762,2894,-1442,1520,7610 } },
  6195. { "NIKON COOLPIX P6000", 0, 0,
  6196. { 9698,-3367,-914,-4706,12584,2368,-837,968,5801 } },
  6197. { "OLYMPUS C5050", 0, 0,
  6198. { 10508,-3124,-1273,-6079,14294,1901,-1653,2306,6237 } },
  6199. { "OLYMPUS C5060", 0, 0,
  6200. { 10445,-3362,-1307,-7662,15690,2058,-1135,1176,7602 } },
  6201. { "OLYMPUS C7070", 0, 0,
  6202. { 10252,-3531,-1095,-7114,14850,2436,-1451,1723,6365 } },
  6203. { "OLYMPUS C70", 0, 0,
  6204. { 10793,-3791,-1146,-7498,15177,2488,-1390,1577,7321 } },
  6205. { "OLYMPUS C80", 0, 0,
  6206. { 8606,-2509,-1014,-8238,15714,2703,-942,979,7760 } },
  6207. { "OLYMPUS E-10", 0, 0xffc,
  6208. { 12745,-4500,-1416,-6062,14542,1580,-1934,2256,6603 } },
  6209. { "OLYMPUS E-1", 0, 0,
  6210. { 11846,-4767,-945,-7027,15878,1089,-2699,4122,8311 } },
  6211. { "OLYMPUS E-20", 0, 0xffc,
  6212. { 13173,-4732,-1499,-5807,14036,1895,-2045,2452,7142 } },
  6213. { "OLYMPUS E-300", 0, 0,
  6214. { 7828,-1761,-348,-5788,14071,1830,-2853,4518,6557 } },
  6215. { "OLYMPUS E-330", 0, 0,
  6216. { 8961,-2473,-1084,-7979,15990,2067,-2319,3035,8249 } },
  6217. { "OLYMPUS E-30", 0, 0xfbc,
  6218. { 8144,-1861,-1111,-7763,15894,1929,-1865,2542,7607 } },
  6219. { "OLYMPUS E-3", 0, 0xf99,
  6220. { 9487,-2875,-1115,-7533,15606,2010,-1618,2100,7389 } },
  6221. { "OLYMPUS E-400", 0, 0,
  6222. { 6169,-1483,-21,-7107,14761,2536,-2904,3580,8568 } },
  6223. { "OLYMPUS E-410", 0, 0xf6a,
  6224. { 8856,-2582,-1026,-7761,15766,2082,-2009,2575,7469 } },
  6225. { "OLYMPUS E-420", 0, 0xfd7,
  6226. { 8746,-2425,-1095,-7594,15612,2073,-1780,2309,7416 } },
  6227. { "OLYMPUS E-450", 0, 0xfd2,
  6228. { 8745,-2425,-1095,-7594,15613,2073,-1780,2309,7416 } },
  6229. { "OLYMPUS E-500", 0, 0,
  6230. { 8136,-1968,-299,-5481,13742,1871,-2556,4205,6630 } },
  6231. { "OLYMPUS E-510", 0, 0xf6a,
  6232. { 8785,-2529,-1033,-7639,15624,2112,-1783,2300,7817 } },
  6233. { "OLYMPUS E-520", 0, 0xfd2,
  6234. { 8344,-2322,-1020,-7596,15635,2048,-1748,2269,7287 } },
  6235. { "OLYMPUS E-600", 0, 0xfaf,
  6236. { 8453,-2198,-1092,-7609,15681,2008,-1725,2337,7824 } },
  6237. { "OLYMPUS E-620", 0, 0xfaf,
  6238. { 8453,-2198,-1092,-7609,15681,2008,-1725,2337,7824 } },
  6239. { "OLYMPUS E-P1", 0, 0xffd,
  6240. { 8343,-2050,-1021,-7715,15705,2103,-1831,2380,8235 } },
  6241. { "OLYMPUS E-P2", 0, 0xffd,
  6242. { 8343,-2050,-1021,-7715,15705,2103,-1831,2380,8235 } },
  6243. { "OLYMPUS E-PL1", 0, 0,
  6244. { 11408,-4289,-1215,-4286,12385,2118,-387,1467,7787 } },
  6245. { "OLYMPUS SP350", 0, 0,
  6246. { 12078,-4836,-1069,-6671,14306,2578,-786,939,7418 } },
  6247. { "OLYMPUS SP3", 0, 0,
  6248. { 11766,-4445,-1067,-6901,14421,2707,-1029,1217,7572 } },
  6249. { "OLYMPUS SP500UZ", 0, 0xfff,
  6250. { 9493,-3415,-666,-5211,12334,3260,-1548,2262,6482 } },
  6251. { "OLYMPUS SP510UZ", 0, 0xffe,
  6252. { 10593,-3607,-1010,-5881,13127,3084,-1200,1805,6721 } },
  6253. { "OLYMPUS SP550UZ", 0, 0xffe,
  6254. { 11597,-4006,-1049,-5432,12799,2957,-1029,1750,6516 } },
  6255. { "OLYMPUS SP560UZ", 0, 0xff9,
  6256. { 10915,-3677,-982,-5587,12986,2911,-1168,1968,6223 } },
  6257. { "OLYMPUS SP570UZ", 0, 0,
  6258. { 11522,-4044,-1146,-4736,12172,2904,-988,1829,6039 } },
  6259. { "PENTAX *ist DL2", 0, 0,
  6260. { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
  6261. { "PENTAX *ist DL", 0, 0,
  6262. { 10829,-2838,-1115,-8339,15817,2696,-837,680,11939 } },
  6263. { "PENTAX *ist DS2", 0, 0,
  6264. { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
  6265. { "PENTAX *ist DS", 0, 0,
  6266. { 10371,-2333,-1206,-8688,16231,2602,-1230,1116,11282 } },
  6267. { "PENTAX *ist D", 0, 0,
  6268. { 9651,-2059,-1189,-8881,16512,2487,-1460,1345,10687 } },
  6269. { "PENTAX K10D", 0, 0,
  6270. { 9566,-2863,-803,-7170,15172,2112,-818,803,9705 } },
  6271. { "PENTAX K1", 0, 0,
  6272. { 11095,-3157,-1324,-8377,15834,2720,-1108,947,11688 } },
  6273. { "PENTAX K20D", 0, 0,
  6274. { 9427,-2714,-868,-7493,16092,1373,-2199,3264,7180 } },
  6275. { "PENTAX K200D", 0, 0,
  6276. { 9186,-2678,-907,-8693,16517,2260,-1129,1094,8524 } },
  6277. { "PENTAX K2000", 0, 0,
  6278. { 11057,-3604,-1155,-5152,13046,2329,-282,375,8104 } },
  6279. { "PENTAX K-m", 0, 0,
  6280. { 11057,-3604,-1155,-5152,13046,2329,-282,375,8104 } },
  6281. { "PENTAX K-x", 0, 0,
  6282. { 8843,-2837,-625,-5025,12644,2668,-411,1234,7410 } },
  6283. { "PENTAX K-7", 0, 0,
  6284. { 9142,-2947,-678,-8648,16967,1663,-2224,2898,8615 } },
  6285. { "Panasonic DMC-FZ8", 0, 0xf7f,
  6286. { 8986,-2755,-802,-6341,13575,3077,-1476,2144,6379 } },
  6287. { "Panasonic DMC-FZ18", 0, 0,
  6288. { 9932,-3060,-935,-5809,13331,2753,-1267,2155,5575 } },
  6289. { "Panasonic DMC-FZ28", 15, 0xfff,
  6290. { 10109,-3488,-993,-5412,12812,2916,-1305,2140,5543 } },
  6291. { "Panasonic DMC-FZ30", 0, 0xf94,
  6292. { 10976,-4029,-1141,-7918,15491,2600,-1670,2071,8246 } },
  6293. { "Panasonic DMC-FZ35", 147, 0xfff,
  6294. { 9938,-2780,-890,-4604,12393,2480,-1117,2304,4620 } },
  6295. { "Panasonic DMC-FZ50", 0, 0xfff, /* aka "LEICA V-LUX1" */
  6296. { 7906,-2709,-594,-6231,13351,3220,-1922,2631,6537 } },
  6297. { "Panasonic DMC-L10", 15, 0xf96,
  6298. { 8025,-1942,-1050,-7920,15904,2100,-2456,3005,7039 } },
  6299. { "Panasonic DMC-L1", 0, 0x3dff, /* aka "LEICA DIGILUX 3" */
  6300. { 8054,-1885,-1025,-8349,16367,2040,-2805,3542,7629 } },
  6301. { "Panasonic DMC-LC1", 0, 0, /* aka "LEICA DIGILUX 2" */
  6302. { 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
  6303. { "Panasonic DMC-LX1", 0, 0x3dfc, /* aka "LEICA D-LUX2" */
  6304. { 10704,-4187,-1230,-8314,15952,2501,-920,945,8927 } },
  6305. { "Panasonic DMC-LX2", 0, 0, /* aka "LEICA D-LUX3" */
  6306. { 8048,-2810,-623,-6450,13519,3272,-1700,2146,7049 } },
  6307. { "Panasonic DMC-LX3", 15, 0xfff, /* aka "LEICA D-LUX4" */
  6308. { 8128,-2668,-655,-6134,13307,3161,-1782,2568,6083 } },
  6309. { "Panasonic DMC-FX150", 15, 0xfff,
  6310. { 9082,-2907,-925,-6119,13377,3058,-1797,2641,5609 } },
  6311. { "Panasonic DMC-G10", 0, 0,
  6312. { 10113,-3400,-1114,-4765,12683,2317,-377,1437,6710 } },
  6313. { "Panasonic DMC-G1", 15, 0xfff,
  6314. { 8199,-2065,-1056,-8124,16156,2033,-2458,3022,7220 } },
  6315. { "Panasonic DMC-G2", 15, 0xf3c,
  6316. { 10113,-3400,-1114,-4765,12683,2317,-377,1437,6710 } },
  6317. { "Panasonic DMC-GF1", 15, 0xf92,
  6318. { 7888,-1902,-1011,-8106,16085,2099,-2353,2866,7330 } },
  6319. { "Panasonic DMC-GH1", 15, 0xf92,
  6320. { 6299,-1466,-532,-6535,13852,2969,-2331,3112,5984 } },
  6321. { "Phase One H 20", 0, 0, /* DJC */
  6322. { 1313,1855,-109,-6715,15908,808,-327,1840,6020 } },
  6323. { "Phase One P 2", 0, 0,
  6324. { 2905,732,-237,-8134,16626,1476,-3038,4253,7517 } },
  6325. { "Phase One P 30", 0, 0,
  6326. { 4516,-245,-37,-7020,14976,2173,-3206,4671,7087 } },
  6327. { "Phase One P 45", 0, 0,
  6328. { 5053,-24,-117,-5684,14076,1702,-2619,4492,5849 } },
  6329. { "Phase One P65", 0, 0,
  6330. { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
  6331. { "SAMSUNG GX-1", 0, 0,
  6332. { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
  6333. { "SAMSUNG S85", 0, 0, /* DJC */
  6334. { 11885,-3968,-1473,-4214,12299,1916,-835,1655,5549 } },
  6335. { "Sinar", 0, 0, /* DJC */
  6336. { 16442,-2956,-2422,-2877,12128,750,-1136,6066,4559 } },
  6337. { "SONY DSC-F828", 491, 0,
  6338. { 7924,-1910,-777,-8226,15459,2998,-1517,2199,6818,-7242,11401,3481 } },
  6339. { "SONY DSC-R1", 512, 0,
  6340. { 8512,-2641,-694,-8042,15670,2526,-1821,2117,7414 } },
  6341. { "SONY DSC-V3", 0, 0,
  6342. { 7511,-2571,-692,-7894,15088,3060,-948,1111,8128 } },
  6343. { "SONY DSLR-A100", 0, 0xfeb,
  6344. { 9437,-2811,-774,-8405,16215,2290,-710,596,7181 } },
  6345. { "SONY DSLR-A2", 0, 0,
  6346. { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
  6347. { "SONY DSLR-A300", 0, 0,
  6348. { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
  6349. { "SONY DSLR-A330", 0, 0,
  6350. { 9847,-3091,-929,-8485,16346,2225,-714,595,7103 } },
  6351. { "SONY DSLR-A350", 0, 0xffc,
  6352. { 6038,-1484,-578,-9146,16746,2513,-875,746,7217 } },
  6353. { "SONY DSLR-A380", 0, 0,
  6354. { 6038,-1484,-579,-9145,16746,2512,-875,746,7218 } },
  6355. { "SONY DSLR-A450", 128, 0xfeb,
  6356. { 4950,-580,-103,-5228,12542,3029,-709,1435,7371 } },
  6357. { "SONY DSLR-A5", 128, 0xfeb,
  6358. { 4950,-580,-103,-5228,12542,3029,-709,1435,7371 } },
  6359. { "SONY DSLR-A700", 126, 0,
  6360. { 5775,-805,-359,-8574,16295,2391,-1943,2341,7249 } },
  6361. { "SONY DSLR-A850", 128, 0,
  6362. { 5413,-1162,-365,-5665,13098,2866,-608,1179,8440 } },
  6363. { "SONY DSLR-A900", 128, 0,
  6364. { 5209,-1072,-397,-8845,16120,2919,-1618,1803,8654 } },
  6365. { "SONY NEX-3", 138, 0, /* DJC */
  6366. { 6907,-1256,-645,-4940,12621,2320,-1710,2581,6230 } },
  6367. { "SONY NEX-5", 116, 0, /* DJC */
  6368. { 6807,-1350,-342,-4216,11649,2567,-1089,2001,6420 } }
  6369. };
  6370. double cam_xyz[4][3];
  6371. char name[130];
  6372. int i, j;
  6373. sprintf (name, "%s %s", p_make, p_model);
  6374. for (i=0; i < sizeof table / sizeof *table; i++)
  6375. if (!strncmp (name, table[i].prefix, strlen(table[i].prefix))) {
  6376. if (table[i].t_black) black = (ushort) table[i].t_black;
  6377. if (table[i].t_maximum) maximum = (ushort) table[i].t_maximum;
  6378. if (table[i].trans[0]) {
  6379. for (j=0; j < 12; j++)
  6380. #ifdef LIBRAW_LIBRARY_BUILD
  6381. imgdata.color.cam_xyz[0][j] =
  6382. #endif
  6383. cam_xyz[0][j] = table[i].trans[j] / 10000.0;
  6384. cam_xyz_coeff (cam_xyz);
  6385. }
  6386. break;
  6387. }
  6388. }
  6389. void CLASS simple_coeff (int index)
  6390. {
  6391. static const float table[][12] = {
  6392. /* index 0 -- all Foveon cameras */
  6393. { 1.4032,-0.2231,-0.1016,-0.5263,1.4816,0.017,-0.0112,0.0183,0.9113 },
  6394. /* index 1 -- Kodak DC20 and DC25 */
  6395. { 2.25,0.75,-1.75,-0.25,-0.25,0.75,0.75,-0.25,-0.25,-1.75,0.75,2.25 },
  6396. /* index 2 -- Logitech Fotoman Pixtura */
  6397. { 1.893,-0.418,-0.476,-0.495,1.773,-0.278,-1.017,-0.655,2.672 },
  6398. /* index 3 -- Nikon E880, E900, and E990 */
  6399. { -1.936280, 1.800443, -1.448486, 2.584324,
  6400. 1.405365, -0.524955, -0.289090, 0.408680,
  6401. -1.204965, 1.082304, 2.941367, -1.818705 }
  6402. };
  6403. int i, c;
  6404. for (raw_color = i=0; i < 3; i++)
  6405. FORCC rgb_cam[i][c] = table[index][i*colors+c];
  6406. #ifdef LIBRAW_LIBRARY_BUILD
  6407. color_flags.rgb_cam_state = LIBRAW_COLORSTATE_CALCULATED;
  6408. #endif
  6409. }
  6410. short CLASS guess_byte_order (int words)
  6411. {
  6412. uchar test[4][2];
  6413. int t=2, msb;
  6414. double diff, sum[2] = {0,0};
  6415. fread (test[0], 2, 2, ifp);
  6416. for (words-=2; words--; ) {
  6417. fread (test[t], 2, 1, ifp);
  6418. for (msb=0; msb < 2; msb++) {
  6419. diff = (test[t^2][msb] << 8 | test[t^2][!msb])
  6420. - (test[t ][msb] << 8 | test[t ][!msb]);
  6421. sum[msb] += diff*diff;
  6422. }
  6423. t = (t+1) & 3;
  6424. }
  6425. return sum[0] < sum[1] ? 0x4d4d : 0x4949;
  6426. }
  6427. #line 7931 "dcraw/dcraw.c"
  6428. float CLASS find_green (int bps, int bite, int off0, int off1)
  6429. {
  6430. UINT64 bitbuf=0;
  6431. int vbits, col, i, c;
  6432. ushort img[2][2064];
  6433. double sum[]={0,0};
  6434. FORC(2) {
  6435. fseek (ifp, c ? off1:off0, SEEK_SET);
  6436. for (vbits=col=0; col < width; col++) {
  6437. for (vbits -= bps; vbits < 0; vbits += bite) {
  6438. bitbuf <<= bite;
  6439. for (i=0; i < bite; i+=8)
  6440. bitbuf |= (unsigned) (fgetc(ifp) << i);
  6441. }
  6442. img[c][col] = bitbuf << (64-bps-vbits) >> (64-bps);
  6443. }
  6444. }
  6445. FORC(width-1) {
  6446. sum[ c & 1] += ABS(img[0][c]-img[1][c+1]);
  6447. sum[~c & 1] += ABS(img[1][c]-img[0][c+1]);
  6448. }
  6449. return 100 * log(sum[0]/sum[1]);
  6450. }
  6451. /*
  6452. Identify which camera created this file, and set global variables
  6453. accordingly.
  6454. */
  6455. void CLASS identify()
  6456. {
  6457. char head[32], *cp;
  6458. int hlen, flen, fsize, zero_fsize=1, i, c, is_canon;
  6459. struct jhead jh;
  6460. static const struct {
  6461. int fsize;
  6462. char t_make[12], t_model[19], withjpeg;
  6463. } table[] = {
  6464. { 62464, "Kodak", "DC20" ,0 },
  6465. { 124928, "Kodak", "DC20" ,0 },
  6466. { 1652736, "Kodak", "DCS200" ,0 },
  6467. { 4159302, "Kodak", "C330" ,0 },
  6468. { 4162462, "Kodak", "C330" ,0 },
  6469. { 460800, "Kodak", "C603v" ,0 },
  6470. { 614400, "Kodak", "C603v" ,0 },
  6471. { 6163328, "Kodak", "C603" ,0 },
  6472. { 6166488, "Kodak", "C603" ,0 },
  6473. { 9116448, "Kodak", "C603y" ,0 },
  6474. { 311696, "ST Micro", "STV680 VGA" ,0 }, /* SPYz */
  6475. { 787456, "Creative", "PC-CAM 600" ,0 },
  6476. { 1138688, "Minolta", "RD175" ,0 },
  6477. { 3840000, "Foculus", "531C" ,0 },
  6478. { 786432, "AVT", "F-080C" ,0 },
  6479. { 1447680, "AVT", "F-145C" ,0 },
  6480. { 1920000, "AVT", "F-201C" ,0 },
  6481. { 5067304, "AVT", "F-510C" ,0 },
  6482. { 5067316, "AVT", "F-510C" ,0 },
  6483. { 10134608, "AVT", "F-510C" ,0 },
  6484. { 10134620, "AVT", "F-510C" ,0 },
  6485. { 16157136, "AVT", "F-810C" ,0 },
  6486. { 1409024, "Sony", "XCD-SX910CR" ,0 },
  6487. { 2818048, "Sony", "XCD-SX910CR" ,0 },
  6488. { 3884928, "Micron", "2010" ,0 },
  6489. { 6624000, "Pixelink", "A782" ,0 },
  6490. { 13248000, "Pixelink", "A782" ,0 },
  6491. { 6291456, "RoverShot","3320AF" ,0 },
  6492. { 6553440, "Canon", "PowerShot A460" ,0 },
  6493. { 6653280, "Canon", "PowerShot A530" ,0 },
  6494. { 6573120, "Canon", "PowerShot A610" ,0 },
  6495. { 9219600, "Canon", "PowerShot A620" ,0 },
  6496. { 9243240, "Canon", "PowerShot A470" ,0 },
  6497. { 10341600, "Canon", "PowerShot A720 IS",0 },
  6498. { 10383120, "Canon", "PowerShot A630" ,0 },
  6499. { 12945240, "Canon", "PowerShot A640" ,0 },
  6500. { 15636240, "Canon", "PowerShot A650" ,0 },
  6501. { 5298000, "Canon", "PowerShot SD300" ,0 },
  6502. { 7710960, "Canon", "PowerShot S3 IS" ,0 },
  6503. { 15467760, "Canon", "PowerShot SX110 IS",0 },
  6504. { 18653760, "Canon", "PowerShot SX20 IS",0 },
  6505. { 5939200, "OLYMPUS", "C770UZ" ,0 },
  6506. { 1581060, "NIKON", "E900" ,1 }, /* or E900s,E910 */
  6507. { 2465792, "NIKON", "E950" ,1 }, /* or E800,E700 */
  6508. { 2940928, "NIKON", "E2100" ,1 }, /* or E2500 */
  6509. { 4771840, "NIKON", "E990" ,1 }, /* or E995, Oly C3030Z */
  6510. { 4775936, "NIKON", "E3700" ,1 }, /* or Optio 33WR */
  6511. { 5869568, "NIKON", "E4300" ,1 }, /* or DiMAGE Z2 */
  6512. { 5865472, "NIKON", "E4500" ,1 },
  6513. { 7438336, "NIKON", "E5000" ,1 }, /* or E5700 */
  6514. { 8998912, "NIKON", "COOLPIX S6" ,1 },
  6515. { 1976352, "CASIO", "QV-2000UX" ,1 },
  6516. { 3217760, "CASIO", "QV-3*00EX" ,1 },
  6517. { 6218368, "CASIO", "QV-5700" ,1 },
  6518. { 6054400, "CASIO", "QV-R41" ,1 },
  6519. { 7530816, "CASIO", "QV-R51" ,1 },
  6520. { 7684000, "CASIO", "QV-4000" ,1 },
  6521. { 2937856, "CASIO", "EX-S20" ,1 },
  6522. { 4948608, "CASIO", "EX-S100" ,1 },
  6523. { 7542528, "CASIO", "EX-Z50" ,1 },
  6524. { 7753344, "CASIO", "EX-Z55" ,1 },
  6525. { 7816704, "CASIO", "EX-Z60" ,1 },
  6526. { 10843712, "CASIO", "EX-Z75" ,1 },
  6527. { 10834368, "CASIO", "EX-Z750" ,1 },
  6528. { 12310144, "CASIO", "EX-Z850" ,1 },
  6529. { 15499264, "CASIO", "EX-Z1050" ,1 },
  6530. { 7426656, "CASIO", "EX-P505" ,1 },
  6531. { 9313536, "CASIO", "EX-P600" ,1 },
  6532. { 10979200, "CASIO", "EX-P700" ,1 },
  6533. { 3178560, "PENTAX", "Optio S" ,1 },
  6534. { 4841984, "PENTAX", "Optio S" ,1 },
  6535. { 6114240, "PENTAX", "Optio S4" ,1 }, /* or S4i, CASIO EX-Z4 */
  6536. { 10702848, "PENTAX", "Optio 750Z" ,1 },
  6537. { 15980544, "AGFAPHOTO","DC-833m" ,1 },
  6538. { 16098048, "SAMSUNG", "S85" ,1 },
  6539. { 16215552, "SAMSUNG", "S85" ,1 },
  6540. { 20487168, "SAMSUNG", "WB550" ,1 },
  6541. { 24000000, "SAMSUNG", "WB550" ,1 },
  6542. { 12582980, "Sinar", "" ,0 },
  6543. { 33292868, "Sinar", "" ,0 },
  6544. { 44390468, "Sinar", "" ,0 } };
  6545. static const char *corp[] =
  6546. { "Canon", "NIKON", "EPSON", "KODAK", "Kodak", "OLYMPUS", "PENTAX",
  6547. "MINOLTA", "Minolta", "Konica", "CASIO", "Sinar", "Phase One",
  6548. "SAMSUNG", "Mamiya", "MOTOROLA" };
  6549. #ifdef LIBRAW_LIBRARY_BUILD
  6550. RUN_CALLBACK(LIBRAW_PROGRESS_IDENTIFY,0,2);
  6551. #endif
  6552. tiff_flip = flip = filters = -1; /* 0 is valid, so -1 is unknown */
  6553. raw_height = raw_width = fuji_width = fuji_layout = cr2_slice[0] = 0;
  6554. maximum = height = width = top_margin = left_margin = 0;
  6555. cdesc[0] = desc[0] = artist[0] = make[0] = model[0] = model2[0] = 0;
  6556. iso_speed = shutter = aperture = focal_len = unique_id = 0;
  6557. tiff_nifds = 0;
  6558. memset (tiff_ifd, 0, sizeof tiff_ifd);
  6559. memset (gpsdata, 0, sizeof gpsdata);
  6560. memset (cblack, 0, sizeof cblack);
  6561. memset (white, 0, sizeof white);
  6562. thumb_offset = thumb_length = thumb_width = thumb_height = 0;
  6563. load_raw = thumb_load_raw = 0;
  6564. write_thumb = &CLASS jpeg_thumb;
  6565. data_offset = meta_length = tiff_bps = tiff_compress = 0;
  6566. kodak_cbpp = zero_after_ff = dng_version = load_flags = 0;
  6567. timestamp = shot_order = tiff_samples = black =
  6568. #ifndef LIBRAW_LIBRARY_BUILD
  6569. is_foveon =
  6570. #endif
  6571. 0;
  6572. mix_green = profile_length = data_error = zero_is_bad = 0;
  6573. pixel_aspect = is_raw = raw_color = 1;
  6574. tile_width = tile_length = INT_MAX;
  6575. for (i=0; i < 4; i++) {
  6576. cam_mul[i] = i == 1;
  6577. pre_mul[i] = i < 3;
  6578. FORC3 cmatrix[c][i] = 0;
  6579. FORC3 rgb_cam[c][i] = c == i;
  6580. }
  6581. #ifdef LIBRAW_LIBRARY_BUILD
  6582. color_flags.cmatrix_state = LIBRAW_COLORSTATE_INIT;
  6583. color_flags.rgb_cam_state = LIBRAW_COLORSTATE_INIT;
  6584. color_flags.pre_mul_state = LIBRAW_COLORSTATE_INIT;
  6585. color_flags.cam_mul_state = LIBRAW_COLORSTATE_INIT;
  6586. #endif
  6587. colors = 3;
  6588. for (i=0; i < 0x4000; i++) curve[i] = i;
  6589. #ifdef LIBRAW_LIBRARY_BUILD
  6590. color_flags.curve_state = LIBRAW_COLORSTATE_INIT;
  6591. #endif
  6592. order = get2();
  6593. hlen = get4();
  6594. fseek (ifp, 0, SEEK_SET);
  6595. fread (head, 1, 32, ifp);
  6596. fseek (ifp, 0, SEEK_END);
  6597. flen = fsize = ftell(ifp);
  6598. if ((cp = (char *) memmem (head, 32, (char*)"MMMM", 4)) ||
  6599. (cp = (char *) memmem (head, 32, (char*)"IIII", 4))) {
  6600. parse_phase_one (cp-head);
  6601. if (cp-head && parse_tiff(0)) apply_tiff();
  6602. } else if (order == 0x4949 || order == 0x4d4d) {
  6603. if (!memcmp (head+6,"HEAPCCDR",8)) {
  6604. data_offset = hlen;
  6605. parse_ciff (hlen, flen - hlen);
  6606. } else if (parse_tiff(0)) apply_tiff();
  6607. } else if (!memcmp (head,"\xff\xd8\xff\xe1",4) &&
  6608. !memcmp (head+6,"Exif",4)) {
  6609. fseek (ifp, 4, SEEK_SET);
  6610. data_offset = 4 + get2();
  6611. fseek (ifp, data_offset, SEEK_SET);
  6612. if (fgetc(ifp) != 0xff)
  6613. parse_tiff(12);
  6614. thumb_offset = 0;
  6615. } else if (!memcmp (head+25,"ARECOYK",7)) {
  6616. strcpy (make, "Contax");
  6617. strcpy (model,"N Digital");
  6618. fseek (ifp, 33, SEEK_SET);
  6619. get_timestamp(1);
  6620. fseek (ifp, 60, SEEK_SET);
  6621. FORC4 cam_mul[c ^ (c >> 1)] = get4();
  6622. #ifdef LIBRAW_LIBRARY_BUILD
  6623. color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
  6624. #endif
  6625. } else if (!strcmp (head, "PXN")) {
  6626. strcpy (make, "Logitech");
  6627. strcpy (model,"Fotoman Pixtura");
  6628. } else if (!strcmp (head, "qktk")) {
  6629. strcpy (make, "Apple");
  6630. strcpy (model,"QuickTake 100");
  6631. } else if (!strcmp (head, "qktn")) {
  6632. strcpy (make, "Apple");
  6633. strcpy (model,"QuickTake 150");
  6634. } else if (!memcmp (head,"FUJIFILM",8)) {
  6635. fseek (ifp, 84, SEEK_SET);
  6636. thumb_offset = get4();
  6637. thumb_length = get4();
  6638. fseek (ifp, 92, SEEK_SET);
  6639. parse_fuji (get4());
  6640. if (thumb_offset > 120) {
  6641. fseek (ifp, 120, SEEK_SET);
  6642. is_raw += (i = get4()) && 1;
  6643. if (is_raw == 2 && shot_select)
  6644. parse_fuji (i);
  6645. }
  6646. fseek (ifp, 100, SEEK_SET);
  6647. parse_tiff (data_offset = get4());
  6648. parse_tiff (thumb_offset+12);
  6649. apply_tiff();
  6650. } else if (!memcmp (head,"RIFF",4)) {
  6651. fseek (ifp, 0, SEEK_SET);
  6652. parse_riff();
  6653. } else if (!memcmp (head,"\0\001\0\001\0@",6)) {
  6654. fseek (ifp, 6, SEEK_SET);
  6655. fread (make, 1, 8, ifp);
  6656. fread (model, 1, 8, ifp);
  6657. fread (model2, 1, 16, ifp);
  6658. data_offset = get2();
  6659. get2();
  6660. raw_width = get2();
  6661. raw_height = get2();
  6662. load_raw = &CLASS nokia_load_raw;
  6663. filters = 0x61616161;
  6664. } else if (!memcmp (head,"DSC-Image",9))
  6665. parse_rollei();
  6666. else if (!memcmp (head,"PWAD",4))
  6667. parse_sinar_ia();
  6668. else if (!memcmp (head,"\0MRM",4))
  6669. parse_minolta(0);
  6670. #ifndef LIBRAW_LIBRARY_BUILD
  6671. else if (!memcmp (head,"FOVb",4))
  6672. parse_foveon();
  6673. #endif
  6674. else if (!memcmp (head,"CI",2))
  6675. parse_cine();
  6676. else
  6677. for (zero_fsize=i=0; i < sizeof table / sizeof *table; i++)
  6678. if (fsize == table[i].fsize) {
  6679. strcpy (make, table[i].t_make );
  6680. strcpy (model, table[i].t_model);
  6681. if (table[i].withjpeg)
  6682. parse_external_jpeg();
  6683. }
  6684. if (zero_fsize) fsize = 0;
  6685. if (make[0] == 0) parse_smal (0, flen);
  6686. if (make[0] == 0) parse_jpeg (is_raw = 0);
  6687. for (i=0; i < sizeof corp / sizeof *corp; i++)
  6688. if (strstr (make, corp[i])) /* Simplify company names */
  6689. strcpy (make, corp[i]);
  6690. if (!strncmp (make,"KODAK",5) &&
  6691. ((cp = strstr(model," DIGITAL CAMERA")) ||
  6692. (cp = strstr(model," Digital Camera")) ||
  6693. (cp = strstr(model,"FILE VERSION"))))
  6694. *cp = 0;
  6695. cp = make + strlen(make); /* Remove trailing spaces */
  6696. while (*--cp == ' ') *cp = 0;
  6697. cp = model + strlen(model);
  6698. while (*--cp == ' ') *cp = 0;
  6699. i = strlen(make); /* Remove make from model */
  6700. if (!strncasecmp (model, make, i) && model[i++] == ' ')
  6701. memmove (model, model+i, 64-i);
  6702. if (!strncmp (model,"Digital Camera ",15))
  6703. strcpy (model, model+15);
  6704. desc[511] = artist[63] = make[63] = model[63] = model2[63] = 0;
  6705. if (!is_raw) goto notraw;
  6706. if (!height) height = raw_height;
  6707. if (!width) width = raw_width;
  6708. if (fuji_width) {
  6709. fuji_width = (raw_width+1)/2;
  6710. width = height + fuji_width;
  6711. height = width - 1;
  6712. pixel_aspect = 1;
  6713. }
  6714. if (height == 2624 && width == 3936) /* Pentax K10D and Samsung GX10 */
  6715. { height = 2616; width = 3896; }
  6716. if (height == 3136 && width == 4864) /* Pentax K20D */
  6717. { height = 3124; width = 4688; }
  6718. if (height == 3136 && width == 4736) /* Pentax K-7 */
  6719. { height = 3122; width = 4684;
  6720. top_margin = 2; filters = 0x16161616; }
  6721. if (height == 3014 && width == 4096) /* Ricoh GX200 */
  6722. width = 4014;
  6723. if (dng_version) {
  6724. if (filters == UINT_MAX) filters = 0;
  6725. if (filters) is_raw = tiff_samples;
  6726. else colors = tiff_samples;
  6727. if (tiff_compress == 1)
  6728. load_raw = &CLASS adobe_dng_load_raw_nc;
  6729. if (tiff_compress == 7)
  6730. load_raw = &CLASS adobe_dng_load_raw_lj;
  6731. goto dng_skip;
  6732. }
  6733. if ((is_canon = !strcmp(make,"Canon")))
  6734. load_raw = memcmp (head+6,"HEAPCCDR",8) ?
  6735. &CLASS lossless_jpeg_load_raw : &CLASS canon_compressed_load_raw;
  6736. if (!strcmp(make,"NIKON")) {
  6737. if (!load_raw)
  6738. load_raw = &CLASS packed_load_raw;
  6739. if (model[0] == 'E')
  6740. load_flags |= !data_offset << 2 | 2;
  6741. }
  6742. if (!strcmp(make,"CASIO")) {
  6743. load_raw = &CLASS packed_load_raw;
  6744. maximum = 0xf7f;
  6745. }
  6746. /* Set parameters based on camera name (for non-DNG files). */
  6747. #ifndef LIBRAW_LIBRARY_BUILD
  6748. if (is_foveon) {
  6749. if (height*2 < width) pixel_aspect = 0.5;
  6750. if (height > width) pixel_aspect = 2;
  6751. filters = 0;
  6752. load_raw = &CLASS foveon_load_raw;
  6753. simple_coeff(0);
  6754. } else
  6755. #endif
  6756. if (is_canon && tiff_bps == 15) {
  6757. switch (width) {
  6758. case 3344: width -= 66;
  6759. case 3872: width -= 6;
  6760. }
  6761. filters = 0;
  6762. load_raw = &CLASS canon_sraw_load_raw;
  6763. } else if (!strcmp(model,"PowerShot 600")) {
  6764. height = 613;
  6765. width = 854;
  6766. raw_width = 896;
  6767. pixel_aspect = 607/628.0;
  6768. colors = 4;
  6769. filters = 0xe1e4e1e4;
  6770. load_raw = &CLASS canon_600_load_raw;
  6771. } else if (!strcmp(model,"PowerShot A5") ||
  6772. !strcmp(model,"PowerShot A5 Zoom")) {
  6773. height = 773;
  6774. width = 960;
  6775. raw_width = 992;
  6776. pixel_aspect = 256/235.0;
  6777. colors = 4;
  6778. filters = 0x1e4e1e4e;
  6779. goto canon_a5;
  6780. } else if (!strcmp(model,"PowerShot A50")) {
  6781. height = 968;
  6782. width = 1290;
  6783. raw_width = 1320;
  6784. colors = 4;
  6785. filters = 0x1b4e4b1e;
  6786. goto canon_a5;
  6787. } else if (!strcmp(model,"PowerShot Pro70")) {
  6788. height = 1024;
  6789. width = 1552;
  6790. colors = 4;
  6791. filters = 0x1e4b4e1b;
  6792. goto canon_a5;
  6793. } else if (!strcmp(model,"PowerShot SD300")) {
  6794. height = 1752;
  6795. width = 2344;
  6796. raw_height = 1766;
  6797. raw_width = 2400;
  6798. top_margin = 12;
  6799. left_margin = 12;
  6800. goto canon_a5;
  6801. } else if (!strcmp(model,"PowerShot A460")) {
  6802. height = 1960;
  6803. width = 2616;
  6804. raw_height = 1968;
  6805. raw_width = 2664;
  6806. top_margin = 4;
  6807. left_margin = 4;
  6808. goto canon_a5;
  6809. } else if (!strcmp(model,"PowerShot A530")) {
  6810. height = 1984;
  6811. width = 2620;
  6812. raw_height = 1992;
  6813. raw_width = 2672;
  6814. top_margin = 6;
  6815. left_margin = 10;
  6816. goto canon_a5;
  6817. } else if (!strcmp(model,"PowerShot A610")) {
  6818. if (canon_s2is()) strcpy (model+10, "S2 IS");
  6819. height = 1960;
  6820. width = 2616;
  6821. raw_height = 1968;
  6822. raw_width = 2672;
  6823. top_margin = 8;
  6824. left_margin = 12;
  6825. goto canon_a5;
  6826. } else if (!strcmp(model,"PowerShot A620")) {
  6827. height = 2328;
  6828. width = 3112;
  6829. raw_height = 2340;
  6830. raw_width = 3152;
  6831. top_margin = 12;
  6832. left_margin = 36;
  6833. goto canon_a5;
  6834. } else if (!strcmp(model,"PowerShot A470")) {
  6835. height = 2328;
  6836. width = 3096;
  6837. raw_height = 2346;
  6838. raw_width = 3152;
  6839. top_margin = 6;
  6840. left_margin = 12;
  6841. goto canon_a5;
  6842. } else if (!strcmp(model,"PowerShot A720 IS")) {
  6843. height = 2472;
  6844. width = 3298;
  6845. raw_height = 2480;
  6846. raw_width = 3336;
  6847. top_margin = 5;
  6848. left_margin = 6;
  6849. goto canon_a5;
  6850. } else if (!strcmp(model,"PowerShot A630")) {
  6851. height = 2472;
  6852. width = 3288;
  6853. raw_height = 2484;
  6854. raw_width = 3344;
  6855. top_margin = 6;
  6856. left_margin = 12;
  6857. goto canon_a5;
  6858. } else if (!strcmp(model,"PowerShot A640")) {
  6859. height = 2760;
  6860. width = 3672;
  6861. raw_height = 2772;
  6862. raw_width = 3736;
  6863. top_margin = 6;
  6864. left_margin = 12;
  6865. goto canon_a5;
  6866. } else if (!strcmp(model,"PowerShot A650")) {
  6867. height = 3024;
  6868. width = 4032;
  6869. raw_height = 3048;
  6870. raw_width = 4104;
  6871. top_margin = 12;
  6872. left_margin = 48;
  6873. goto canon_a5;
  6874. } else if (!strcmp(model,"PowerShot S3 IS")) {
  6875. height = 2128;
  6876. width = 2840;
  6877. raw_height = 2136;
  6878. raw_width = 2888;
  6879. top_margin = 8;
  6880. left_margin = 44;
  6881. canon_a5:
  6882. tiff_bps = 10;
  6883. load_raw = &CLASS packed_load_raw;
  6884. load_flags = 40;
  6885. if (raw_width > 1600) zero_is_bad = 1;
  6886. } else if (!strcmp(model,"PowerShot SX110 IS")) {
  6887. height = 2760;
  6888. width = 3684;
  6889. raw_height = 2772;
  6890. raw_width = 3720;
  6891. top_margin = 12;
  6892. left_margin = 6;
  6893. load_raw = &CLASS packed_load_raw;
  6894. load_flags = 40;
  6895. zero_is_bad = 1;
  6896. } else if (!strcmp(model,"PowerShot SX20 IS")) {
  6897. height = 3024;
  6898. width = 4032;
  6899. raw_height = 3048;
  6900. raw_width = 4080;
  6901. top_margin = 12;
  6902. left_margin = 24;
  6903. load_raw = &CLASS packed_load_raw;
  6904. load_flags = 40;
  6905. zero_is_bad = 1;
  6906. } else if (!strcmp(model,"PowerShot Pro90 IS")) {
  6907. width = 1896;
  6908. colors = 4;
  6909. filters = 0xb4b4b4b4;
  6910. } else if (is_canon && raw_width == 2144) {
  6911. height = 1550;
  6912. width = 2088;
  6913. top_margin = 8;
  6914. left_margin = 4;
  6915. if (!strcmp(model,"PowerShot G1")) {
  6916. colors = 4;
  6917. filters = 0xb4b4b4b4;
  6918. }
  6919. } else if (is_canon && raw_width == 2224) {
  6920. height = 1448;
  6921. width = 2176;
  6922. top_margin = 6;
  6923. left_margin = 48;
  6924. } else if (is_canon && raw_width == 2376) {
  6925. height = 1720;
  6926. width = 2312;
  6927. top_margin = 6;
  6928. left_margin = 12;
  6929. } else if (is_canon && raw_width == 2672) {
  6930. height = 1960;
  6931. width = 2616;
  6932. top_margin = 6;
  6933. left_margin = 12;
  6934. } else if (is_canon && raw_width == 3152) {
  6935. height = 2056;
  6936. width = 3088;
  6937. top_margin = 12;
  6938. left_margin = 64;
  6939. if (unique_id == 0x80000170)
  6940. adobe_coeff ("Canon","EOS 300D");
  6941. } else if (is_canon && raw_width == 3160) {
  6942. height = 2328;
  6943. width = 3112;
  6944. top_margin = 12;
  6945. left_margin = 44;
  6946. } else if (is_canon && raw_width == 3344) {
  6947. height = 2472;
  6948. width = 3288;
  6949. top_margin = 6;
  6950. left_margin = 4;
  6951. } else if (!strcmp(model,"EOS D2000C")) {
  6952. filters = 0x61616161;
  6953. black = curve[200];
  6954. } else if (is_canon && raw_width == 3516) {
  6955. top_margin = 14;
  6956. left_margin = 42;
  6957. if (unique_id == 0x80000189)
  6958. adobe_coeff ("Canon","EOS 350D");
  6959. goto canon_cr2;
  6960. } else if (is_canon && raw_width == 3596) {
  6961. top_margin = 12;
  6962. left_margin = 74;
  6963. goto canon_cr2;
  6964. } else if (is_canon && raw_width == 3744) {
  6965. height = 2760;
  6966. width = 3684;
  6967. top_margin = 16;
  6968. left_margin = 8;
  6969. } else if (is_canon && raw_width == 3944) {
  6970. height = 2602;
  6971. width = 3908;
  6972. top_margin = 18;
  6973. left_margin = 30;
  6974. } else if (is_canon && raw_width == 3948) {
  6975. top_margin = 18;
  6976. left_margin = 42;
  6977. height -= 2;
  6978. if (unique_id == 0x80000236)
  6979. adobe_coeff ("Canon","EOS 400D");
  6980. if (unique_id == 0x80000254)
  6981. adobe_coeff ("Canon","EOS 1000D");
  6982. goto canon_cr2;
  6983. } else if (is_canon && raw_width == 3984) {
  6984. top_margin = 20;
  6985. left_margin = 76;
  6986. height -= 2;
  6987. goto canon_cr2;
  6988. } else if (is_canon && raw_width == 4104) {
  6989. height = 3024;
  6990. width = 4032;
  6991. top_margin = 12;
  6992. left_margin = 48;
  6993. } else if (is_canon && raw_width == 4152) {
  6994. top_margin = 12;
  6995. left_margin = 192;
  6996. goto canon_cr2;
  6997. } else if (is_canon && raw_width == 4312) {
  6998. top_margin = 18;
  6999. left_margin = 22;
  7000. height -= 2;
  7001. if (unique_id == 0x80000176)
  7002. adobe_coeff ("Canon","EOS 450D");
  7003. goto canon_cr2;
  7004. } else if (is_canon && raw_width == 4476) {
  7005. top_margin = 34;
  7006. left_margin = 90;
  7007. goto canon_cr2;
  7008. } else if (is_canon && raw_width == 4480) {
  7009. height = 3326;
  7010. width = 4432;
  7011. top_margin = 10;
  7012. left_margin = 12;
  7013. filters = 0x49494949;
  7014. } else if (is_canon && raw_width == 4832) {
  7015. top_margin = unique_id == 0x80000261 ? 51:26;
  7016. left_margin = 62;
  7017. if (unique_id == 0x80000252)
  7018. adobe_coeff ("Canon","EOS 500D");
  7019. goto canon_cr2;
  7020. } else if (is_canon && raw_width == 5120) {
  7021. height -= top_margin = 45;
  7022. left_margin = 142;
  7023. width = 4916;
  7024. } else if (is_canon && raw_width == 5344) {
  7025. top_margin = 51;
  7026. left_margin = 142;
  7027. if (unique_id == 0x80000270)
  7028. adobe_coeff ("Canon","EOS 550D");
  7029. goto canon_cr2;
  7030. } else if (is_canon && raw_width == 5360) {
  7031. top_margin = 51;
  7032. left_margin = 158;
  7033. goto canon_cr2;
  7034. } else if (is_canon && raw_width == 5792) {
  7035. top_margin = 51;
  7036. left_margin = 158;
  7037. goto canon_cr2;
  7038. } else if (is_canon && raw_width == 5108) {
  7039. top_margin = 13;
  7040. left_margin = 98;
  7041. canon_cr2:
  7042. height -= top_margin;
  7043. width -= left_margin;
  7044. } else if (is_canon && raw_width == 5712) {
  7045. height = 3752;
  7046. width = 5640;
  7047. top_margin = 20;
  7048. left_margin = 62;
  7049. } else if (!strcmp(model,"D1")) {
  7050. cam_mul[0] *= 256/527.0;
  7051. cam_mul[2] *= 256/317.0;
  7052. } else if (!strcmp(model,"D1X")) {
  7053. width -= 4;
  7054. pixel_aspect = 0.5;
  7055. } else if (!strcmp(model,"D40X") ||
  7056. !strcmp(model,"D60") ||
  7057. !strcmp(model,"D80") ||
  7058. !strcmp(model,"D3000")) {
  7059. height -= 3;
  7060. width -= 4;
  7061. } else if (!strcmp(model,"D3") ||
  7062. !strcmp(model,"D3S") ||
  7063. !strcmp(model,"D700")) {
  7064. width -= 4;
  7065. left_margin = 2;
  7066. } else if (!strcmp(model,"D5000")) {
  7067. width -= 42;
  7068. } else if (!strncmp(model,"D40",3) ||
  7069. !strncmp(model,"D50",3) ||
  7070. !strncmp(model,"D70",3)) {
  7071. width--;
  7072. } else if (!strcmp(model,"D90")) {
  7073. width -= 42;
  7074. } else if (!strcmp(model,"D100")) {
  7075. if (tiff_compress == 34713 && !nikon_is_compressed()) {
  7076. load_raw = &CLASS packed_load_raw;
  7077. load_flags |= 1;
  7078. raw_width = (width += 3) + 3;
  7079. }
  7080. } else if (!strcmp(model,"D200")) {
  7081. left_margin = 1;
  7082. width -= 4;
  7083. filters = 0x94949494;
  7084. } else if (!strncmp(model,"D2H",3)) {
  7085. left_margin = 6;
  7086. width -= 14;
  7087. } else if (!strncmp(model,"D2X",3)) {
  7088. if (width == 3264) width -= 32;
  7089. else width -= 8;
  7090. } else if (!strncmp(model,"D300",4)) {
  7091. width -= 32;
  7092. } else if (!strcmp(model,"COOLPIX P6000")) {
  7093. load_flags = 24;
  7094. filters = 0x94949494;
  7095. } else if (fsize == 1581060) {
  7096. height = 963;
  7097. width = 1287;
  7098. raw_width = 1632;
  7099. maximum = 0x3f4;
  7100. colors = 4;
  7101. filters = 0x1e1e1e1e;
  7102. simple_coeff(3);
  7103. pre_mul[0] = 1.2085;
  7104. pre_mul[1] = 1.0943;
  7105. pre_mul[3] = 1.1103;
  7106. #ifdef LIBRAW_LIBRARY_BUILD
  7107. color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
  7108. #endif
  7109. goto e900;
  7110. } else if (fsize == 2465792) {
  7111. height = 1203;
  7112. width = 1616;
  7113. raw_width = 2048;
  7114. colors = 4;
  7115. filters = 0x4b4b4b4b;
  7116. adobe_coeff ("NIKON","E950");
  7117. e900:
  7118. tiff_bps = 10;
  7119. load_raw = &CLASS packed_load_raw;
  7120. load_flags = 6;
  7121. } else if (fsize == 4771840) {
  7122. height = 1540;
  7123. width = 2064;
  7124. colors = 4;
  7125. filters = 0xe1e1e1e1;
  7126. load_raw = &CLASS packed_load_raw;
  7127. load_flags = 6;
  7128. if (!timestamp && nikon_e995())
  7129. strcpy (model, "E995");
  7130. if (strcmp(model,"E995")) {
  7131. filters = 0xb4b4b4b4;
  7132. simple_coeff(3);
  7133. pre_mul[0] = 1.196;
  7134. pre_mul[1] = 1.246;
  7135. pre_mul[2] = 1.018;
  7136. #ifdef LIBRAW_LIBRARY_BUILD
  7137. color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
  7138. #endif
  7139. }
  7140. } else if (!strcmp(model,"E2100")) {
  7141. if (!timestamp && !nikon_e2100()) goto cp_e2500;
  7142. height = 1206;
  7143. width = 1616;
  7144. load_flags = 30;
  7145. } else if (!strcmp(model,"E2500")) {
  7146. cp_e2500:
  7147. strcpy (model, "E2500");
  7148. height = 1204;
  7149. width = 1616;
  7150. colors = 4;
  7151. filters = 0x4b4b4b4b;
  7152. } else if (fsize == 4775936) {
  7153. height = 1542;
  7154. width = 2064;
  7155. load_raw = &CLASS packed_load_raw;
  7156. load_flags = 30;
  7157. if (!timestamp) nikon_3700();
  7158. if (model[0] == 'E' && atoi(model+1) < 3700)
  7159. filters = 0x49494949;
  7160. if (!strcmp(model,"Optio 33WR")) {
  7161. flip = 1;
  7162. filters = 0x16161616;
  7163. }
  7164. if (make[0] == 'O') {
  7165. i = find_green (12, 32, 1188864, 3576832);
  7166. c = find_green (12, 32, 2383920, 2387016);
  7167. if (abs(i) < abs(c)) {
  7168. SWAP(i,c);
  7169. load_flags = 24;
  7170. }
  7171. if (i < 0) filters = 0x61616161;
  7172. }
  7173. } else if (fsize == 5869568) {
  7174. height = 1710;
  7175. width = 2288;
  7176. filters = 0x16161616;
  7177. if (!timestamp && minolta_z2()) {
  7178. strcpy (make, "Minolta");
  7179. strcpy (model,"DiMAGE Z2");
  7180. }
  7181. load_raw = &CLASS packed_load_raw;
  7182. load_flags = 6 + 24*(make[0] == 'M');
  7183. } else if (!strcmp(model,"E4500")) {
  7184. height = 1708;
  7185. width = 2288;
  7186. colors = 4;
  7187. filters = 0xb4b4b4b4;
  7188. } else if (fsize == 7438336) {
  7189. height = 1924;
  7190. width = 2576;
  7191. colors = 4;
  7192. filters = 0xb4b4b4b4;
  7193. } else if (fsize == 8998912) {
  7194. height = 2118;
  7195. width = 2832;
  7196. maximum = 0xf83;
  7197. load_raw = &CLASS packed_load_raw;
  7198. load_flags = 30;
  7199. } else if (!strcmp(make,"FUJIFILM")) {
  7200. if (!strcmp(model+7,"S2Pro")) {
  7201. strcpy (model+7," S2Pro");
  7202. height = 2144;
  7203. width = 2880;
  7204. flip = 6;
  7205. } else if (load_raw != &CLASS packed_load_raw)
  7206. maximum = 0x3e00;
  7207. if (is_raw == 2 && shot_select)
  7208. maximum = 0x2f00;
  7209. top_margin = (raw_height - height)/2;
  7210. left_margin = (raw_width - width )/2;
  7211. if (is_raw == 2)
  7212. data_offset += (shot_select > 0) * ( fuji_layout ?
  7213. (raw_width *= 2) : raw_height*raw_width*2 );
  7214. if (load_raw == &CLASS fuji_load_raw) {
  7215. fuji_width = width >> !fuji_layout;
  7216. width = (height >> fuji_layout) + fuji_width;
  7217. raw_height = height;
  7218. height = width - 1;
  7219. if (~fuji_width & 1) filters = 0x49494949;
  7220. }
  7221. } else if (!strcmp(model,"RD175")) {
  7222. height = 986;
  7223. width = 1534;
  7224. data_offset = 513;
  7225. filters = 0x61616161;
  7226. load_raw = &CLASS minolta_rd175_load_raw;
  7227. } else if (!strcmp(model,"KD-400Z")) {
  7228. height = 1712;
  7229. width = 2312;
  7230. raw_width = 2336;
  7231. goto konica_400z;
  7232. } else if (!strcmp(model,"KD-510Z")) {
  7233. goto konica_510z;
  7234. } else if (!strcasecmp(make,"MINOLTA")) {
  7235. load_raw = &CLASS unpacked_load_raw;
  7236. maximum = 0xfff;
  7237. if (!strncmp(model,"DiMAGE A",8)) {
  7238. if (!strcmp(model,"DiMAGE A200"))
  7239. filters = 0x49494949;
  7240. tiff_bps = 12;
  7241. load_raw = &CLASS packed_load_raw;
  7242. } else if (!strncmp(model,"ALPHA",5) ||
  7243. !strncmp(model,"DYNAX",5) ||
  7244. !strncmp(model,"MAXXUM",6)) {
  7245. sprintf (model+20, "DYNAX %-10s", model+6+(model[0]=='M'));
  7246. adobe_coeff (make, model+20);
  7247. load_raw = &CLASS packed_load_raw;
  7248. } else if (!strncmp(model,"DiMAGE G",8)) {
  7249. if (model[8] == '4') {
  7250. height = 1716;
  7251. width = 2304;
  7252. } else if (model[8] == '5') {
  7253. konica_510z:
  7254. height = 1956;
  7255. width = 2607;
  7256. raw_width = 2624;
  7257. } else if (model[8] == '6') {
  7258. height = 2136;
  7259. width = 2848;
  7260. }
  7261. data_offset += 14;
  7262. filters = 0x61616161;
  7263. konica_400z:
  7264. load_raw = &CLASS unpacked_load_raw;
  7265. maximum = 0x3df;
  7266. order = 0x4d4d;
  7267. }
  7268. } else if (!strcmp(model,"*ist D")) {
  7269. data_error = -1;
  7270. } else if (!strcmp(model,"*ist DS")) {
  7271. height -= 2;
  7272. } else if (!strcmp(model,"K20D")) {
  7273. filters = 0x16161616;
  7274. } else if (!strcmp(model,"K-x")) {
  7275. width = 4309;
  7276. filters = 0x16161616;
  7277. } else if (!strcmp(model,"Optio S")) {
  7278. if (fsize == 3178560) {
  7279. height = 1540;
  7280. width = 2064;
  7281. load_raw = &CLASS eight_bit_load_raw;
  7282. cam_mul[0] *= 4;
  7283. cam_mul[2] *= 4;
  7284. } else {
  7285. height = 1544;
  7286. width = 2068;
  7287. raw_width = 3136;
  7288. load_raw = &CLASS packed_load_raw;
  7289. maximum = 0xf7c;
  7290. }
  7291. } else if (fsize == 6114240) {
  7292. height = 1737;
  7293. width = 2324;
  7294. raw_width = 3520;
  7295. load_raw = &CLASS packed_load_raw;
  7296. maximum = 0xf7a;
  7297. } else if (!strcmp(model,"Optio 750Z")) {
  7298. height = 2302;
  7299. width = 3072;
  7300. load_raw = &CLASS packed_load_raw;
  7301. load_flags = 30;
  7302. } else if (!strcmp(model,"DC-833m")) {
  7303. height = 2448;
  7304. width = 3264;
  7305. order = 0x4949;
  7306. filters = 0x61616161;
  7307. load_raw = &CLASS unpacked_load_raw;
  7308. maximum = 0xfc00;
  7309. } else if (!strncmp(model,"S85",3)) {
  7310. height = 2448;
  7311. width = 3264;
  7312. raw_width = fsize/height/2;
  7313. order = 0x4d4d;
  7314. load_raw = &CLASS unpacked_load_raw;
  7315. maximum = 0xffff;
  7316. } else if (!strcmp(model,"NX10")) {
  7317. height -= top_margin = 4;
  7318. width -= 2 * (left_margin = 8);
  7319. } else if (!strcmp(model,"EX1")) {
  7320. order = 0x4949;
  7321. height = 2760;
  7322. top_margin = 2;
  7323. if ((width -= 6) > 3682) {
  7324. height = 2750;
  7325. width = 3668;
  7326. top_margin = 8;
  7327. }
  7328. maximum = 0x3e00;
  7329. } else if (fsize == 20487168) {
  7330. height = 2808;
  7331. width = 3648;
  7332. goto wb550;
  7333. } else if (fsize == 24000000) {
  7334. height = 3000;
  7335. width = 4000;
  7336. wb550:
  7337. strcpy (model, "WB550");
  7338. order = 0x4d4d;
  7339. load_raw = &CLASS unpacked_load_raw;
  7340. load_flags = 6;
  7341. maximum = 0x3df;
  7342. } else if (!strcmp(model,"STV680 VGA")) {
  7343. height = 484;
  7344. width = 644;
  7345. load_raw = &CLASS eight_bit_load_raw;
  7346. flip = 2;
  7347. filters = 0x16161616;
  7348. black = 16;
  7349. } else if (!strcmp(model,"N95")) {
  7350. height = raw_height - (top_margin = 2);
  7351. } else if (!strcmp(model,"531C")) {
  7352. height = 1200;
  7353. width = 1600;
  7354. load_raw = &CLASS unpacked_load_raw;
  7355. filters = 0x49494949;
  7356. } else if (!strcmp(model,"F-080C")) {
  7357. height = 768;
  7358. width = 1024;
  7359. load_raw = &CLASS eight_bit_load_raw;
  7360. } else if (!strcmp(model,"F-145C")) {
  7361. height = 1040;
  7362. width = 1392;
  7363. load_raw = &CLASS eight_bit_load_raw;
  7364. } else if (!strcmp(model,"F-201C")) {
  7365. height = 1200;
  7366. width = 1600;
  7367. load_raw = &CLASS eight_bit_load_raw;
  7368. } else if (!strcmp(model,"F-510C")) {
  7369. height = 1958;
  7370. width = 2588;
  7371. load_raw = fsize < 7500000 ?
  7372. &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
  7373. data_offset = fsize - width*height*(fsize >> 22);
  7374. maximum = 0xfff0;
  7375. } else if (!strcmp(model,"F-810C")) {
  7376. height = 2469;
  7377. width = 3272;
  7378. load_raw = &CLASS unpacked_load_raw;
  7379. maximum = 0xfff0;
  7380. } else if (!strcmp(model,"XCD-SX910CR")) {
  7381. height = 1024;
  7382. width = 1375;
  7383. raw_width = 1376;
  7384. filters = 0x49494949;
  7385. maximum = 0x3ff;
  7386. load_raw = fsize < 2000000 ?
  7387. &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
  7388. } else if (!strcmp(model,"2010")) {
  7389. height = 1207;
  7390. width = 1608;
  7391. order = 0x4949;
  7392. filters = 0x16161616;
  7393. data_offset = 3212;
  7394. maximum = 0x3ff;
  7395. load_raw = &CLASS unpacked_load_raw;
  7396. } else if (!strcmp(model,"A782")) {
  7397. height = 3000;
  7398. width = 2208;
  7399. filters = 0x61616161;
  7400. load_raw = fsize < 10000000 ?
  7401. &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
  7402. maximum = 0xffc0;
  7403. } else if (!strcmp(model,"3320AF")) {
  7404. height = 1536;
  7405. raw_width = width = 2048;
  7406. filters = 0x61616161;
  7407. load_raw = &CLASS unpacked_load_raw;
  7408. maximum = 0x3ff;
  7409. fseek (ifp, 0x300000, SEEK_SET);
  7410. if ((order = guess_byte_order(0x10000)) == 0x4d4d) {
  7411. height -= (top_margin = 16);
  7412. width -= (left_margin = 28);
  7413. maximum = 0xf5c0;
  7414. strcpy (make, "ISG");
  7415. model[0] = 0;
  7416. }
  7417. } else if (!strcmp(make,"Hasselblad")) {
  7418. if (load_raw == &CLASS lossless_jpeg_load_raw)
  7419. load_raw = &CLASS hasselblad_load_raw;
  7420. if (raw_width == 7262) {
  7421. height = 5444;
  7422. width = 7248;
  7423. top_margin = 4;
  7424. left_margin = 7;
  7425. filters = 0x61616161;
  7426. } else if (raw_width == 4090) {
  7427. strcpy (model, "V96C");
  7428. height -= (top_margin = 6);
  7429. width -= (left_margin = 3) + 7;
  7430. filters = 0x61616161;
  7431. }
  7432. } else if (!strcmp(make,"Sinar")) {
  7433. if (!memcmp(head,"8BPS",4)) {
  7434. fseek (ifp, 14, SEEK_SET);
  7435. height = get4();
  7436. width = get4();
  7437. filters = 0x61616161;
  7438. data_offset = 68;
  7439. }
  7440. if (!load_raw) load_raw = &CLASS unpacked_load_raw;
  7441. maximum = 0x3fff;
  7442. } else if (!strcmp(make,"Leaf")) {
  7443. maximum = 0x3fff;
  7444. fseek (ifp, data_offset, SEEK_SET);
  7445. if (ljpeg_start (&jh, 1) && jh.bits == 15)
  7446. maximum = 0x1fff;
  7447. if (tiff_samples > 1) filters = 0;
  7448. if (tiff_samples > 1 || tile_length < raw_height)
  7449. load_raw = &CLASS leaf_hdr_load_raw;
  7450. if ((width | height) == 2048) {
  7451. if (tiff_samples == 1) {
  7452. filters = 1;
  7453. strcpy (cdesc, "RBTG");
  7454. strcpy (model, "CatchLight");
  7455. top_margin = 8; left_margin = 18; height = 2032; width = 2016;
  7456. } else {
  7457. strcpy (model, "DCB2");
  7458. top_margin = 10; left_margin = 16; height = 2028; width = 2022;
  7459. }
  7460. } else if (width+height == 3144+2060) {
  7461. if (!model[0]) strcpy (model, "Cantare");
  7462. if (width > height) {
  7463. top_margin = 6; left_margin = 32; height = 2048; width = 3072;
  7464. filters = 0x61616161;
  7465. } else {
  7466. left_margin = 6; top_margin = 32; width = 2048; height = 3072;
  7467. filters = 0x16161616;
  7468. }
  7469. if (!cam_mul[0] || model[0] == 'V') filters = 0;
  7470. else is_raw = tiff_samples;
  7471. } else if (width == 2116) {
  7472. strcpy (model, "Valeo 6");
  7473. height -= 2 * (top_margin = 30);
  7474. width -= 2 * (left_margin = 55);
  7475. filters = 0x49494949;
  7476. } else if (width == 3171) {
  7477. strcpy (model, "Valeo 6");
  7478. height -= 2 * (top_margin = 24);
  7479. width -= 2 * (left_margin = 24);
  7480. filters = 0x16161616;
  7481. }
  7482. } else if (!strcmp(make,"LEICA") || !strcmp(make,"Panasonic")) {
  7483. if ((flen - data_offset) / (width*8/7) == height)
  7484. load_raw = &CLASS panasonic_load_raw;
  7485. if (!load_raw) {
  7486. load_raw = &CLASS unpacked_load_raw;
  7487. load_flags = 4;
  7488. }
  7489. zero_is_bad = 1;
  7490. switch (width) {
  7491. case 2568:
  7492. adobe_coeff ("Panasonic","DMC-LC1"); break;
  7493. case 3130:
  7494. left_margin = -14;
  7495. case 3170:
  7496. left_margin += 18;
  7497. width = 3096;
  7498. if (height > 2326) {
  7499. height = 2326;
  7500. top_margin = 13;
  7501. filters = 0x49494949;
  7502. }
  7503. adobe_coeff ("Panasonic","DMC-FZ8"); break;
  7504. case 3213:
  7505. width -= 27;
  7506. case 3177:
  7507. width -= 10;
  7508. load_flags = 2;
  7509. filters = 0x49494949;
  7510. adobe_coeff ("Panasonic","DMC-L1"); break;
  7511. case 3304:
  7512. width -= 17;
  7513. adobe_coeff ("Panasonic","DMC-FZ30"); break;
  7514. case 3330:
  7515. width += 43;
  7516. left_margin = -6;
  7517. maximum = 0xf7f;
  7518. case 3370:
  7519. width -= 82;
  7520. left_margin += 15;
  7521. if (height > 2480)
  7522. height = 2480 - (top_margin = 10);
  7523. filters = 0x49494949;
  7524. adobe_coeff ("Panasonic","DMC-FZ18"); break;
  7525. case 3690:
  7526. height -= 2;
  7527. left_margin = -14;
  7528. maximum = 0xf7f0;
  7529. case 3770:
  7530. width = 3672;
  7531. if (--height == 2798 && (height = 2760))
  7532. top_margin = 15;
  7533. else filters = 0x49494949;
  7534. left_margin += 17;
  7535. adobe_coeff ("Panasonic","DMC-FZ50"); break;
  7536. case 3710:
  7537. width = 3682;
  7538. filters = 0x49494949;
  7539. adobe_coeff ("Panasonic","DMC-L10"); break;
  7540. case 3724:
  7541. width -= 14;
  7542. if (height == 2450) height -= 2;
  7543. case 3836:
  7544. width -= 42;
  7545. lx3: filters = 0x16161616;
  7546. if (make[0] != 'P')
  7547. adobe_coeff ("Panasonic","DMC-LX3");
  7548. break;
  7549. case 3880:
  7550. width -= 22;
  7551. left_margin = 6;
  7552. load_flags = 2;
  7553. adobe_coeff ("Panasonic","DMC-LX1"); break;
  7554. case 4060:
  7555. width = 3982;
  7556. if (height == 2250) goto lx3;
  7557. width = 4018;
  7558. filters = 0x16161616;
  7559. if (!strncmp(model,"DMC-FZ3",7)) {
  7560. height -= 2;
  7561. adobe_coeff ("Panasonic","DMC-FZ35"); break;
  7562. }
  7563. filters = 0x49494949;
  7564. break;
  7565. case 4172:
  7566. case 4396:
  7567. width -= 28;
  7568. filters = 0x49494949;
  7569. adobe_coeff ("Panasonic","DMC-GH1"); break;
  7570. case 4290:
  7571. height += 38;
  7572. left_margin = -14;
  7573. filters = 0x49494949;
  7574. case 4330:
  7575. width = 4248;
  7576. if ((height -= 39) == 2400)
  7577. top_margin = 15;
  7578. left_margin += 17;
  7579. adobe_coeff ("Panasonic","DMC-LX2"); break;
  7580. case 4508:
  7581. height -= 6;
  7582. width = 4429;
  7583. filters = 0x16161616;
  7584. adobe_coeff ("Panasonic","DMC-FX150"); break;
  7585. }
  7586. } else if (!strcmp(model,"C770UZ")) {
  7587. height = 1718;
  7588. width = 2304;
  7589. filters = 0x16161616;
  7590. load_raw = &CLASS packed_load_raw;
  7591. load_flags = 30;
  7592. } else if (!strcmp(make,"OLYMPUS")) {
  7593. height += height & 1;
  7594. filters = exif_cfa;
  7595. if (width == 4100) width -= 4;
  7596. if (load_raw == &CLASS unpacked_load_raw)
  7597. load_flags = 4;
  7598. tiff_bps = 12;
  7599. if (!strcmp(model,"E-300") ||
  7600. !strcmp(model,"E-500")) {
  7601. width -= 20;
  7602. if (load_raw == &CLASS unpacked_load_raw) {
  7603. maximum = 0xfc3;
  7604. memset (cblack, 0, sizeof cblack);
  7605. }
  7606. } else if (!strcmp(model,"E-330")) {
  7607. width -= 30;
  7608. if (load_raw == &CLASS unpacked_load_raw)
  7609. maximum = 0xf79;
  7610. } else if (!strcmp(model,"SP550UZ")) {
  7611. thumb_length = flen - (thumb_offset = 0xa39800);
  7612. thumb_height = 480;
  7613. thumb_width = 640;
  7614. }
  7615. } else if (!strcmp(model,"N Digital")) {
  7616. height = 2047;
  7617. width = 3072;
  7618. filters = 0x61616161;
  7619. data_offset = 0x1a00;
  7620. load_raw = &CLASS packed_load_raw;
  7621. } else if (!strcmp(model,"DSC-F828")) {
  7622. width = 3288;
  7623. left_margin = 5;
  7624. data_offset = 862144;
  7625. load_raw = &CLASS sony_load_raw;
  7626. filters = 0x9c9c9c9c;
  7627. colors = 4;
  7628. strcpy (cdesc, "RGBE");
  7629. } else if (!strcmp(model,"DSC-V3")) {
  7630. width = 3109;
  7631. left_margin = 59;
  7632. data_offset = 787392;
  7633. load_raw = &CLASS sony_load_raw;
  7634. } else if (!strcmp(make,"SONY") && raw_width == 3984) {
  7635. adobe_coeff ("SONY","DSC-R1");
  7636. width = 3925;
  7637. order = 0x4d4d;
  7638. } else if (!strcmp(model,"DSLR-A100")) {
  7639. if (width == 3880) {
  7640. height--;
  7641. width = ++raw_width;
  7642. } else {
  7643. order = 0x4d4d;
  7644. load_flags = 2;
  7645. }
  7646. filters = 0x61616161;
  7647. } else if (!strcmp(model,"DSLR-A350")) {
  7648. height -= 4;
  7649. } else if (!strcmp(model,"PIXL")) {
  7650. height -= top_margin = 4;
  7651. width -= left_margin = 32;
  7652. gamma_curve (0, 7, 1, 255);
  7653. } else if (!strcmp(model,"C603v")) {
  7654. height = 480;
  7655. width = 640;
  7656. if (fsize < 614400 || find_green (16, 16, 3840, 5120) < 25) goto c603v;
  7657. strcpy (model,"KAI-0340");
  7658. height -= 3;
  7659. data_offset = 3840;
  7660. order = 0x4949;
  7661. load_raw = &CLASS unpacked_load_raw;
  7662. } else if (!strcmp(model,"C603y")) {
  7663. height = 2134;
  7664. width = 2848;
  7665. c603v:
  7666. filters = 0;
  7667. load_raw = &CLASS kodak_yrgb_load_raw;
  7668. gamma_curve (0, 3.875, 1, 255);
  7669. } else if (!strcmp(model,"C603")) {
  7670. raw_height = height = 2152;
  7671. raw_width = width = 2864;
  7672. goto c603;
  7673. } else if (!strcmp(model,"C330")) {
  7674. height = 1744;
  7675. width = 2336;
  7676. raw_height = 1779;
  7677. raw_width = 2338;
  7678. top_margin = 33;
  7679. left_margin = 1;
  7680. c603:
  7681. order = 0x4949;
  7682. if ((data_offset = fsize - raw_height*raw_width)) {
  7683. fseek (ifp, 168, SEEK_SET);
  7684. read_shorts (curve, 256);
  7685. #ifdef LIBRAW_LIBRARY_BUILD
  7686. color_flags.curve_state = LIBRAW_COLORSTATE_LOADED;
  7687. #endif
  7688. } else gamma_curve (0, 3.875, 1, 255);
  7689. load_raw = &CLASS eight_bit_load_raw;
  7690. }
  7691. #if 1
  7692. else
  7693. identify2(fsize,flen,head); /* Avoid MS VS 2008 bug */
  7694. #else
  7695. } else if (!strncasecmp(model,"EasyShare",9)) {
  7696. data_offset = 0x15000;
  7697. load_raw = &CLASS packed_load_raw;
  7698. } else if (!strcasecmp(make,"KODAK")) {
  7699. if (filters == UINT_MAX) filters = 0x61616161;
  7700. if (!strncmp(model,"NC2000",6)) {
  7701. width -= 4;
  7702. left_margin = 2;
  7703. } else if (!strcmp(model,"EOSDCS3B")) {
  7704. width -= 4;
  7705. left_margin = 2;
  7706. } else if (!strcmp(model,"EOSDCS1")) {
  7707. width -= 4;
  7708. left_margin = 2;
  7709. } else if (!strcmp(model,"DCS420")) {
  7710. width -= 4;
  7711. left_margin = 2;
  7712. } else if (!strncmp(model,"DCS460 ",7)) {
  7713. model[6] = 0;
  7714. width -= 4;
  7715. left_margin = 2;
  7716. } else if (!strcmp(model,"DCS460A")) {
  7717. width -= 4;
  7718. left_margin = 2;
  7719. colors = 1;
  7720. filters = 0;
  7721. } else if (!strcmp(model,"DCS660M")) {
  7722. black = 214;
  7723. colors = 1;
  7724. filters = 0;
  7725. } else if (!strcmp(model,"DCS760M")) {
  7726. colors = 1;
  7727. filters = 0;
  7728. }
  7729. if (!strcmp(model+4,"20X"))
  7730. strcpy (cdesc, "MYCY");
  7731. if (strstr(model,"DC25")) {
  7732. strcpy (model, "DC25");
  7733. data_offset = 15424;
  7734. }
  7735. if (!strncmp(model,"DC2",3)) {
  7736. height = 242;
  7737. if (flen < 100000) {
  7738. raw_width = 256; width = 249;
  7739. pixel_aspect = (4.0*height) / (3.0*width);
  7740. } else {
  7741. raw_width = 512; width = 501;
  7742. pixel_aspect = (493.0*height) / (373.0*width);
  7743. }
  7744. data_offset += raw_width + 1;
  7745. colors = 4;
  7746. filters = 0x8d8d8d8d;
  7747. simple_coeff(1);
  7748. pre_mul[1] = 1.179;
  7749. pre_mul[2] = 1.209;
  7750. pre_mul[3] = 1.036;
  7751. #ifdef LIBRAW_LIBRARY_BUILD
  7752. color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
  7753. #endif
  7754. load_raw = &CLASS eight_bit_load_raw;
  7755. } else if (!strcmp(model,"40")) {
  7756. strcpy (model, "DC40");
  7757. height = 512;
  7758. width = 768;
  7759. data_offset = 1152;
  7760. load_raw = &CLASS kodak_radc_load_raw;
  7761. } else if (strstr(model,"DC50")) {
  7762. strcpy (model, "DC50");
  7763. height = 512;
  7764. width = 768;
  7765. data_offset = 19712;
  7766. load_raw = &CLASS kodak_radc_load_raw;
  7767. } else if (strstr(model,"DC120")) {
  7768. strcpy (model, "DC120");
  7769. height = 976;
  7770. width = 848;
  7771. pixel_aspect = height/0.75/width;
  7772. load_raw = tiff_compress == 7 ?
  7773. &CLASS kodak_jpeg_load_raw : &CLASS kodak_dc120_load_raw;
  7774. } else if (!strcmp(model,"DCS200")) {
  7775. thumb_height = 128;
  7776. thumb_width = 192;
  7777. thumb_offset = 6144;
  7778. thumb_misc = 360;
  7779. write_thumb = &CLASS layer_thumb;
  7780. height = 1024;
  7781. width = 1536;
  7782. data_offset = 79872;
  7783. load_raw = &CLASS eight_bit_load_raw;
  7784. black = 17;
  7785. }
  7786. } else if (!strcmp(model,"Fotoman Pixtura")) {
  7787. height = 512;
  7788. width = 768;
  7789. data_offset = 3632;
  7790. load_raw = &CLASS kodak_radc_load_raw;
  7791. filters = 0x61616161;
  7792. simple_coeff(2);
  7793. } else if (!strcmp(model,"QuickTake 100")) {
  7794. fseek (ifp, 544, SEEK_SET);
  7795. height = get2();
  7796. width = get2();
  7797. data_offset = (get4(),get2()) == 30 ? 738:736;
  7798. if (height > width) {
  7799. SWAP(height,width);
  7800. fseek (ifp, data_offset-6, SEEK_SET);
  7801. flip = ~get2() & 3 ? 5:6;
  7802. }
  7803. load_raw = &CLASS quicktake_100_load_raw;
  7804. filters = 0x61616161;
  7805. } else if (!strcmp(model,"QuickTake 150")) {
  7806. data_offset = 738 - head[5];
  7807. if (head[5]) strcpy (model+10, "200");
  7808. load_raw = &CLASS kodak_radc_load_raw;
  7809. height = 480;
  7810. width = 640;
  7811. filters = 0x61616161;
  7812. } else if (!strcmp(make,"Rollei") && !load_raw) {
  7813. switch (raw_width) {
  7814. case 1316:
  7815. height = 1030;
  7816. width = 1300;
  7817. top_margin = 1;
  7818. left_margin = 6;
  7819. break;
  7820. case 2568:
  7821. height = 1960;
  7822. width = 2560;
  7823. top_margin = 2;
  7824. left_margin = 8;
  7825. }
  7826. filters = 0x16161616;
  7827. load_raw = &CLASS rollei_load_raw;
  7828. } else if (!strcmp(model,"PC-CAM 600")) {
  7829. height = 768;
  7830. data_offset = width = 1024;
  7831. filters = 0x49494949;
  7832. load_raw = &CLASS eight_bit_load_raw;
  7833. }
  7834. else if (!strcmp(model,"QV-2000UX")) {
  7835. height = 1208;
  7836. width = 1632;
  7837. data_offset = width * 2;
  7838. load_raw = &CLASS eight_bit_load_raw;
  7839. } else if (fsize == 3217760) {
  7840. height = 1546;
  7841. width = 2070;
  7842. raw_width = 2080;
  7843. load_raw = &CLASS eight_bit_load_raw;
  7844. } else if (!strcmp(model,"QV-4000")) {
  7845. height = 1700;
  7846. width = 2260;
  7847. load_raw = &CLASS unpacked_load_raw;
  7848. maximum = 0xffff;
  7849. } else if (!strcmp(model,"QV-5700")) {
  7850. height = 1924;
  7851. width = 2576;
  7852. raw_width = 3232;
  7853. tiff_bps = 10;
  7854. } else if (!strcmp(model,"QV-R41")) {
  7855. height = 1720;
  7856. width = 2312;
  7857. raw_width = 3520;
  7858. left_margin = 2;
  7859. } else if (!strcmp(model,"QV-R51")) {
  7860. height = 1926;
  7861. width = 2580;
  7862. raw_width = 3904;
  7863. } else if (!strcmp(model,"EX-S20")) {
  7864. height = 1208;
  7865. width = 1620;
  7866. raw_width = 2432;
  7867. flip = 3;
  7868. } else if (!strcmp(model,"EX-S100")) {
  7869. height = 1544;
  7870. width = 2058;
  7871. raw_width = 3136;
  7872. } else if (!strcmp(model,"EX-Z50")) {
  7873. height = 1931;
  7874. width = 2570;
  7875. raw_width = 3904;
  7876. } else if (!strcmp(model,"EX-Z55")) {
  7877. height = 1960;
  7878. width = 2570;
  7879. raw_width = 3904;
  7880. } else if (!strcmp(model,"EX-Z60")) {
  7881. height = 2145;
  7882. width = 2833;
  7883. raw_width = 3584;
  7884. filters = 0x16161616;
  7885. tiff_bps = 10;
  7886. } else if (!strcmp(model,"EX-Z75")) {
  7887. height = 2321;
  7888. width = 3089;
  7889. raw_width = 4672;
  7890. maximum = 0xfff;
  7891. } else if (!strcmp(model,"EX-Z750")) {
  7892. height = 2319;
  7893. width = 3087;
  7894. raw_width = 4672;
  7895. maximum = 0xfff;
  7896. } else if (!strcmp(model,"EX-Z850")) {
  7897. height = 2468;
  7898. width = 3279;
  7899. raw_width = 4928;
  7900. maximum = 0xfff;
  7901. } else if (!strcmp(model,"EX-Z1050")) {
  7902. height = 2752;
  7903. width = 3672;
  7904. raw_width = 5632;
  7905. maximum = 0xffc;
  7906. } else if (!strcmp(model,"EX-P505")) {
  7907. height = 1928;
  7908. width = 2568;
  7909. raw_width = 3852;
  7910. maximum = 0xfff;
  7911. } else if (fsize == 9313536) { /* EX-P600 or QV-R61 */
  7912. height = 2142;
  7913. width = 2844;
  7914. raw_width = 4288;
  7915. } else if (!strcmp(model,"EX-P700")) {
  7916. height = 2318;
  7917. width = 3082;
  7918. raw_width = 4672;
  7919. }
  7920. #endif
  7921. if (!model[0])
  7922. sprintf (model, "%dx%d", width, height);
  7923. if (filters == UINT_MAX) filters = 0x94949494;
  7924. if (raw_color) adobe_coeff (make, model);
  7925. if (load_raw == &CLASS kodak_radc_load_raw)
  7926. if (raw_color) adobe_coeff ("Apple","Quicktake");
  7927. if (thumb_offset && !thumb_height) {
  7928. fseek (ifp, thumb_offset, SEEK_SET);
  7929. if (ljpeg_start (&jh, 1)) {
  7930. thumb_width = jh.wide;
  7931. thumb_height = jh.high;
  7932. }
  7933. }
  7934. dng_skip:
  7935. if (!tiff_bps) tiff_bps = 12;
  7936. if (!maximum) maximum = (1 << tiff_bps) - 1;
  7937. if (!load_raw || height < 22) is_raw = 0;
  7938. #ifdef NO_JPEG
  7939. if (load_raw == &CLASS kodak_jpeg_load_raw) {
  7940. #ifdef DCRAW_VERBOSE
  7941. fprintf (stderr,_("%s: You must link dcraw with libjpeg!!\n"), ifname);
  7942. #endif
  7943. is_raw = 0;
  7944. #ifdef LIBRAW_LIBRARY_BUILD
  7945. imgdata.process_warnings |= LIBRAW_WARN_NO_JPEGLIB;
  7946. #endif
  7947. }
  7948. #endif
  7949. if (!cdesc[0])
  7950. strcpy (cdesc, colors == 3 ? "RGB":"GMCY");
  7951. if (!raw_height) raw_height = height;
  7952. if (!raw_width ) raw_width = width;
  7953. if (filters && colors == 3)
  7954. filters |= ((filters >> 2 & 0x22222222) |
  7955. (filters << 2 & 0x88888888)) & filters << 1;
  7956. notraw:
  7957. if (flip == -1) flip = tiff_flip;
  7958. if (flip == -1) flip = 0;
  7959. #ifdef LIBRAW_LIBRARY_BUILD
  7960. RUN_CALLBACK(LIBRAW_PROGRESS_IDENTIFY,1,2);
  7961. #endif
  7962. }
  7963. void CLASS identify2(unsigned fsize, unsigned flen, char *head)
  7964. {
  7965. if (!strncasecmp(model,"EasyShare",9)) {
  7966. data_offset = 0x15000;
  7967. load_raw = &CLASS packed_load_raw;
  7968. } else if (!strcasecmp(make,"KODAK")) {
  7969. if (filters == UINT_MAX) filters = 0x61616161;
  7970. if (!strncmp(model,"NC2000",6)) {
  7971. width -= 4;
  7972. left_margin = 2;
  7973. } else if (!strcmp(model,"EOSDCS3B")) {
  7974. width -= 4;
  7975. left_margin = 2;
  7976. } else if (!strcmp(model,"EOSDCS1")) {
  7977. width -= 4;
  7978. left_margin = 2;
  7979. } else if (!strcmp(model,"DCS420")) {
  7980. width -= 4;
  7981. left_margin = 2;
  7982. } else if (!strncmp(model,"DCS460 ",7)) {
  7983. model[6] = 0;
  7984. width -= 4;
  7985. left_margin = 2;
  7986. } else if (!strcmp(model,"DCS460A")) {
  7987. width -= 4;
  7988. left_margin = 2;
  7989. colors = 1;
  7990. filters = 0;
  7991. } else if (!strcmp(model,"DCS660M")) {
  7992. black = 214;
  7993. colors = 1;
  7994. filters = 0;
  7995. } else if (!strcmp(model,"DCS760M")) {
  7996. colors = 1;
  7997. filters = 0;
  7998. }
  7999. if (!strcmp(model+4,"20X"))
  8000. strcpy (cdesc, "MYCY");
  8001. if (strstr(model,"DC25")) {
  8002. strcpy (model, "DC25");
  8003. data_offset = 15424;
  8004. }
  8005. if (!strncmp(model,"DC2",3)) {
  8006. height = 242;
  8007. if (flen < 100000) {
  8008. raw_width = 256; width = 249;
  8009. pixel_aspect = (4.0*height) / (3.0*width);
  8010. } else {
  8011. raw_width = 512; width = 501;
  8012. pixel_aspect = (493.0*height) / (373.0*width);
  8013. }
  8014. data_offset += raw_width + 1;
  8015. colors = 4;
  8016. filters = 0x8d8d8d8d;
  8017. simple_coeff(1);
  8018. pre_mul[1] = 1.179;
  8019. pre_mul[2] = 1.209;
  8020. pre_mul[3] = 1.036;
  8021. #ifdef LIBRAW_LIBRARY_BUILD
  8022. color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
  8023. #endif
  8024. load_raw = &CLASS eight_bit_load_raw;
  8025. } else if (!strcmp(model,"40")) {
  8026. strcpy (model, "DC40");
  8027. height = 512;
  8028. width = 768;
  8029. data_offset = 1152;
  8030. load_raw = &CLASS kodak_radc_load_raw;
  8031. } else if (strstr(model,"DC50")) {
  8032. strcpy (model, "DC50");
  8033. height = 512;
  8034. width = 768;
  8035. data_offset = 19712;
  8036. load_raw = &CLASS kodak_radc_load_raw;
  8037. } else if (strstr(model,"DC120")) {
  8038. strcpy (model, "DC120");
  8039. height = 976;
  8040. width = 848;
  8041. pixel_aspect = height/0.75/width;
  8042. load_raw = tiff_compress == 7 ?
  8043. &CLASS kodak_jpeg_load_raw : &CLASS kodak_dc120_load_raw;
  8044. } else if (!strcmp(model,"DCS200")) {
  8045. thumb_height = 128;
  8046. thumb_width = 192;
  8047. thumb_offset = 6144;
  8048. thumb_misc = 360;
  8049. write_thumb = &CLASS layer_thumb;
  8050. height = 1024;
  8051. width = 1536;
  8052. data_offset = 79872;
  8053. load_raw = &CLASS eight_bit_load_raw;
  8054. black = 17;
  8055. }
  8056. } else if (!strcmp(model,"Fotoman Pixtura")) {
  8057. height = 512;
  8058. width = 768;
  8059. data_offset = 3632;
  8060. load_raw = &CLASS kodak_radc_load_raw;
  8061. filters = 0x61616161;
  8062. simple_coeff(2);
  8063. } else if (!strcmp(model,"QuickTake 100")) {
  8064. fseek (ifp, 544, SEEK_SET);
  8065. height = get2();
  8066. width = get2();
  8067. data_offset = (get4(),get2()) == 30 ? 738:736;
  8068. if (height > width) {
  8069. SWAP(height,width);
  8070. fseek (ifp, data_offset-6, SEEK_SET);
  8071. flip = ~get2() & 3 ? 5:6;
  8072. }
  8073. load_raw = &CLASS quicktake_100_load_raw;
  8074. filters = 0x61616161;
  8075. } else if (!strcmp(model,"QuickTake 150")) {
  8076. data_offset = 738 - head[5];
  8077. if (head[5]) strcpy (model+10, "200");
  8078. load_raw = &CLASS kodak_radc_load_raw;
  8079. height = 480;
  8080. width = 640;
  8081. filters = 0x61616161;
  8082. } else if (!strcmp(make,"Rollei") && !load_raw) {
  8083. switch (raw_width) {
  8084. case 1316:
  8085. height = 1030;
  8086. width = 1300;
  8087. top_margin = 1;
  8088. left_margin = 6;
  8089. break;
  8090. case 2568:
  8091. height = 1960;
  8092. width = 2560;
  8093. top_margin = 2;
  8094. left_margin = 8;
  8095. }
  8096. filters = 0x16161616;
  8097. load_raw = &CLASS rollei_load_raw;
  8098. } else if (!strcmp(model,"PC-CAM 600")) {
  8099. height = 768;
  8100. data_offset = width = 1024;
  8101. filters = 0x49494949;
  8102. load_raw = &CLASS eight_bit_load_raw;
  8103. }
  8104. else if (!strcmp(model,"QV-2000UX")) {
  8105. height = 1208;
  8106. width = 1632;
  8107. data_offset = width * 2;
  8108. load_raw = &CLASS eight_bit_load_raw;
  8109. } else if (fsize == 3217760) {
  8110. height = 1546;
  8111. width = 2070;
  8112. raw_width = 2080;
  8113. load_raw = &CLASS eight_bit_load_raw;
  8114. } else if (!strcmp(model,"QV-4000")) {
  8115. height = 1700;
  8116. width = 2260;
  8117. load_raw = &CLASS unpacked_load_raw;
  8118. maximum = 0xffff;
  8119. } else if (!strcmp(model,"QV-5700")) {
  8120. height = 1924;
  8121. width = 2576;
  8122. raw_width = 3232;
  8123. tiff_bps = 10;
  8124. } else if (!strcmp(model,"QV-R41")) {
  8125. height = 1720;
  8126. width = 2312;
  8127. raw_width = 3520;
  8128. left_margin = 2;
  8129. } else if (!strcmp(model,"QV-R51")) {
  8130. height = 1926;
  8131. width = 2580;
  8132. raw_width = 3904;
  8133. } else if (!strcmp(model,"EX-S20")) {
  8134. height = 1208;
  8135. width = 1620;
  8136. raw_width = 2432;
  8137. flip = 3;
  8138. } else if (!strcmp(model,"EX-S100")) {
  8139. height = 1544;
  8140. width = 2058;
  8141. raw_width = 3136;
  8142. } else if (!strcmp(model,"EX-Z50")) {
  8143. height = 1931;
  8144. width = 2570;
  8145. raw_width = 3904;
  8146. } else if (!strcmp(model,"EX-Z55")) {
  8147. height = 1960;
  8148. width = 2570;
  8149. raw_width = 3904;
  8150. } else if (!strcmp(model,"EX-Z60")) {
  8151. height = 2145;
  8152. width = 2833;
  8153. raw_width = 3584;
  8154. filters = 0x16161616;
  8155. tiff_bps = 10;
  8156. } else if (!strcmp(model,"EX-Z75")) {
  8157. height = 2321;
  8158. width = 3089;
  8159. raw_width = 4672;
  8160. } else if (!strcmp(model,"EX-Z750")) {
  8161. height = 2319;
  8162. width = 3087;
  8163. raw_width = 4672;
  8164. maximum = 0xfff;
  8165. } else if (!strcmp(model,"EX-Z850")) {
  8166. height = 2468;
  8167. width = 3279;
  8168. raw_width = 4928;
  8169. maximum = 0xfff;
  8170. } else if (!strcmp(model,"EX-Z1050")) {
  8171. height = 2752;
  8172. width = 3672;
  8173. raw_width = 5632;
  8174. maximum = 0xffc;
  8175. } else if (!strcmp(model,"EX-P505")) {
  8176. height = 1928;
  8177. width = 2568;
  8178. raw_width = 3852;
  8179. maximum = 0xfff;
  8180. } else if (fsize == 9313536) { /* EX-P600 or QV-R61 */
  8181. height = 2142;
  8182. width = 2844;
  8183. raw_width = 4288;
  8184. } else if (!strcmp(model,"EX-P700")) {
  8185. height = 2318;
  8186. width = 3082;
  8187. raw_width = 4672;
  8188. }
  8189. }
  8190. #line 9797 "dcraw/dcraw.c"
  8191. void CLASS convert_to_rgb()
  8192. {
  8193. int row, col, c, i, j, k;
  8194. ushort *img;
  8195. float out[3], out_cam[3][4];
  8196. double num, inverse[3][3];
  8197. static const double xyzd50_srgb[3][3] =
  8198. { { 0.436083, 0.385083, 0.143055 },
  8199. { 0.222507, 0.716888, 0.060608 },
  8200. { 0.013930, 0.097097, 0.714022 } };
  8201. static const double rgb_rgb[3][3] =
  8202. { { 1,0,0 }, { 0,1,0 }, { 0,0,1 } };
  8203. static const double adobe_rgb[3][3] =
  8204. { { 0.715146, 0.284856, 0.000000 },
  8205. { 0.000000, 1.000000, 0.000000 },
  8206. { 0.000000, 0.041166, 0.958839 } };
  8207. static const double wide_rgb[3][3] =
  8208. { { 0.593087, 0.404710, 0.002206 },
  8209. { 0.095413, 0.843149, 0.061439 },
  8210. { 0.011621, 0.069091, 0.919288 } };
  8211. static const double prophoto_rgb[3][3] =
  8212. { { 0.529317, 0.330092, 0.140588 },
  8213. { 0.098368, 0.873465, 0.028169 },
  8214. { 0.016879, 0.117663, 0.865457 } };
  8215. static const double (*out_rgb[])[3] =
  8216. { rgb_rgb, adobe_rgb, wide_rgb, prophoto_rgb, xyz_rgb };
  8217. static const char *name[] =
  8218. { "sRGB", "Adobe RGB (1998)", "WideGamut D65", "ProPhoto D65", "XYZ" };
  8219. static const unsigned phead[] =
  8220. { 1024, 0, 0x2100000, 0x6d6e7472, 0x52474220, 0x58595a20, 0, 0, 0,
  8221. 0x61637370, 0, 0, 0x6e6f6e65, 0, 0, 0, 0, 0xf6d6, 0x10000, 0xd32d };
  8222. unsigned pbody[] =
  8223. { 10, 0x63707274, 0, 36, /* cprt */
  8224. 0x64657363, 0, 40, /* desc */
  8225. 0x77747074, 0, 20, /* wtpt */
  8226. 0x626b7074, 0, 20, /* bkpt */
  8227. 0x72545243, 0, 14, /* rTRC */
  8228. 0x67545243, 0, 14, /* gTRC */
  8229. 0x62545243, 0, 14, /* bTRC */
  8230. 0x7258595a, 0, 20, /* rXYZ */
  8231. 0x6758595a, 0, 20, /* gXYZ */
  8232. 0x6258595a, 0, 20 }; /* bXYZ */
  8233. static const unsigned pwhite[] = { 0xf351, 0x10000, 0x116cc };
  8234. unsigned pcurve[] = { 0x63757276, 0, 1, 0x1000000 };
  8235. #ifdef LIBRAW_LIBRARY_BUILD
  8236. RUN_CALLBACK(LIBRAW_PROGRESS_CONVERT_RGB,0,2);
  8237. #endif
  8238. gamma_curve (gamm[0], gamm[1], 0, 0);
  8239. memcpy (out_cam, rgb_cam, sizeof out_cam);
  8240. raw_color |= colors == 1 || document_mode ||
  8241. output_color < 1 || output_color > 5;
  8242. if (!raw_color) {
  8243. oprof = (unsigned *) calloc (phead[0], 1);
  8244. merror (oprof, "convert_to_rgb()");
  8245. memcpy (oprof, phead, sizeof phead);
  8246. if (output_color == 5) oprof[4] = oprof[5];
  8247. oprof[0] = 132 + 12*pbody[0];
  8248. for (i=0; i < pbody[0]; i++) {
  8249. oprof[oprof[0]/4] = i ? (i > 1 ? 0x58595a20 : 0x64657363) : 0x74657874;
  8250. pbody[i*3+2] = oprof[0];
  8251. oprof[0] += (pbody[i*3+3] + 3) & -4;
  8252. }
  8253. memcpy (oprof+32, pbody, sizeof pbody);
  8254. oprof[pbody[5]/4+2] = strlen(name[output_color-1]) + 1;
  8255. memcpy ((char *)oprof+pbody[8]+8, pwhite, sizeof pwhite);
  8256. pcurve[3] = (short)(256/gamm[5]+0.5) << 16;
  8257. for (i=4; i < 7; i++)
  8258. memcpy ((char *)oprof+pbody[i*3+2], pcurve, sizeof pcurve);
  8259. pseudoinverse ((double (*)[3]) out_rgb[output_color-1], inverse, 3);
  8260. for (i=0; i < 3; i++)
  8261. for (j=0; j < 3; j++) {
  8262. for (num = k=0; k < 3; k++)
  8263. num += xyzd50_srgb[i][k] * inverse[j][k];
  8264. oprof[pbody[j*3+23]/4+i+2] = num * 0x10000 + 0.5;
  8265. }
  8266. for (i=0; i < phead[0]/4; i++)
  8267. oprof[i] = htonl(oprof[i]);
  8268. strcpy ((char *)oprof+pbody[2]+8, "auto-generated by dcraw");
  8269. strcpy ((char *)oprof+pbody[5]+12, name[output_color-1]);
  8270. for (i=0; i < 3; i++)
  8271. for (j=0; j < colors; j++)
  8272. for (out_cam[i][j] = k=0; k < 3; k++)
  8273. out_cam[i][j] += out_rgb[output_color-1][i][k] * rgb_cam[k][j];
  8274. }
  8275. #ifdef DCRAW_VERBOSE
  8276. if (verbose)
  8277. fprintf (stderr, raw_color ? _("Building histograms...\n") :
  8278. _("Converting to %s colorspace...\n"), name[output_color-1]);
  8279. #endif
  8280. #ifdef LIBRAW_LIBRARY_BUILD
  8281. memset(histogram,0,sizeof(int)*LIBRAW_HISTOGRAM_SIZE*4);
  8282. #else
  8283. memset (histogram, 0, sizeof histogram);
  8284. #endif
  8285. for (img=image[0], row=0; row < height; row++)
  8286. for (col=0; col < width; col++, img+=4) {
  8287. if (!raw_color) {
  8288. out[0] = out[1] = out[2] = 0;
  8289. FORCC {
  8290. out[0] += out_cam[0][c] * img[c];
  8291. out[1] += out_cam[1][c] * img[c];
  8292. out[2] += out_cam[2][c] * img[c];
  8293. }
  8294. FORC3 img[c] = CLIP((int) out[c]);
  8295. }
  8296. else if (document_mode)
  8297. img[0] = img[FC(row,col)];
  8298. FORCC histogram[c][img[c] >> 3]++;
  8299. }
  8300. if (colors == 4 && output_color) colors = 3;
  8301. if (document_mode && filters) colors = 1;
  8302. #ifdef LIBRAW_LIBRARY_BUILD
  8303. RUN_CALLBACK(LIBRAW_PROGRESS_CONVERT_RGB,1,2);
  8304. #endif
  8305. }
  8306. void CLASS fuji_rotate()
  8307. {
  8308. int i, row, col;
  8309. double step;
  8310. float r, c, fr, fc;
  8311. unsigned ur, uc;
  8312. ushort wide, high, (*img)[4], (*pix)[4];
  8313. if (!fuji_width) return;
  8314. #ifdef DCRAW_VERBOSE
  8315. if (verbose)
  8316. fprintf (stderr,_("Rotating image 45 degrees...\n"));
  8317. #endif
  8318. fuji_width = (fuji_width - 1 + shrink) >> shrink;
  8319. step = sqrt(0.5);
  8320. wide = fuji_width / step;
  8321. high = (height - fuji_width) / step;
  8322. img = (ushort (*)[4]) calloc (wide*high, sizeof *img);
  8323. merror (img, "fuji_rotate()");
  8324. #ifdef LIBRAW_LIBRARY_BUILD
  8325. RUN_CALLBACK(LIBRAW_PROGRESS_FUJI_ROTATE,0,2);
  8326. #endif
  8327. for (row=0; row < high; row++)
  8328. for (col=0; col < wide; col++) {
  8329. ur = r = fuji_width + (row-col)*step;
  8330. uc = c = (row+col)*step;
  8331. if (ur > height-2 || uc > width-2) continue;
  8332. fr = r - ur;
  8333. fc = c - uc;
  8334. pix = image + ur*width + uc;
  8335. for (i=0; i < colors; i++)
  8336. img[row*wide+col][i] =
  8337. (pix[ 0][i]*(1-fc) + pix[ 1][i]*fc) * (1-fr) +
  8338. (pix[width][i]*(1-fc) + pix[width+1][i]*fc) * fr;
  8339. }
  8340. free (image);
  8341. width = wide;
  8342. height = high;
  8343. image = img;
  8344. fuji_width = 0;
  8345. #ifdef LIBRAW_LIBRARY_BUILD
  8346. RUN_CALLBACK(LIBRAW_PROGRESS_FUJI_ROTATE,1,2);
  8347. #endif
  8348. }
  8349. void CLASS stretch()
  8350. {
  8351. ushort newdim, (*img)[4], *pix0, *pix1;
  8352. int row, col, c;
  8353. double rc, frac;
  8354. if (pixel_aspect == 1) return;
  8355. #ifdef LIBRAW_LIBRARY_BUILD
  8356. RUN_CALLBACK(LIBRAW_PROGRESS_STRETCH,0,2);
  8357. #endif
  8358. #ifdef DCRAW_VERBOSE
  8359. if (verbose) fprintf (stderr,_("Stretching the image...\n"));
  8360. #endif
  8361. if (pixel_aspect < 1) {
  8362. newdim = height / pixel_aspect + 0.5;
  8363. img = (ushort (*)[4]) calloc (width*newdim, sizeof *img);
  8364. merror (img, "stretch()");
  8365. for (rc=row=0; row < newdim; row++, rc+=pixel_aspect) {
  8366. frac = rc - (c = rc);
  8367. pix0 = pix1 = image[c*width];
  8368. if (c+1 < height) pix1 += width*4;
  8369. for (col=0; col < width; col++, pix0+=4, pix1+=4)
  8370. FORCC img[row*width+col][c] = pix0[c]*(1-frac) + pix1[c]*frac + 0.5;
  8371. }
  8372. height = newdim;
  8373. } else {
  8374. newdim = width * pixel_aspect + 0.5;
  8375. img = (ushort (*)[4]) calloc (height*newdim, sizeof *img);
  8376. merror (img, "stretch()");
  8377. for (rc=col=0; col < newdim; col++, rc+=1/pixel_aspect) {
  8378. frac = rc - (c = rc);
  8379. pix0 = pix1 = image[c];
  8380. if (c+1 < width) pix1 += 4;
  8381. for (row=0; row < height; row++, pix0+=width*4, pix1+=width*4)
  8382. FORCC img[row*newdim+col][c] = pix0[c]*(1-frac) + pix1[c]*frac + 0.5;
  8383. }
  8384. width = newdim;
  8385. }
  8386. free (image);
  8387. image = img;
  8388. #ifdef LIBRAW_LIBRARY_BUILD
  8389. RUN_CALLBACK(LIBRAW_PROGRESS_STRETCH,1,2);
  8390. #endif
  8391. }
  8392. int CLASS flip_index (int row, int col)
  8393. {
  8394. if (flip & 4) SWAP(row,col);
  8395. if (flip & 2) row = iheight - 1 - row;
  8396. if (flip & 1) col = iwidth - 1 - col;
  8397. return row * iwidth + col;
  8398. }
  8399. #line 10040 "dcraw/dcraw.c"
  8400. void CLASS tiff_set (ushort *ntag,
  8401. ushort tag, ushort type, int count, int val)
  8402. {
  8403. struct tiff_tag *tt;
  8404. int c;
  8405. tt = (struct tiff_tag *)(ntag+1) + (*ntag)++;
  8406. tt->tag = tag;
  8407. tt->type = type;
  8408. tt->count = count;
  8409. if (type < 3 && count <= 4)
  8410. FORC(4) tt->val.c[c] = val >> (c << 3);
  8411. else if (type == 3 && count <= 2)
  8412. FORC(2) tt->val.s[c] = val >> (c << 4);
  8413. else tt->val.i = val;
  8414. }
  8415. #define TOFF(ptr) ((char *)(&(ptr)) - (char *)th)
  8416. void CLASS tiff_head (struct tiff_hdr *th, int full)
  8417. {
  8418. int c, psize=0;
  8419. struct tm *t;
  8420. memset (th, 0, sizeof *th);
  8421. th->t_order = htonl(0x4d4d4949) >> 16;
  8422. th->magic = 42;
  8423. th->ifd = 10;
  8424. if (full) {
  8425. tiff_set (&th->ntag, 254, 4, 1, 0);
  8426. tiff_set (&th->ntag, 256, 4, 1, width);
  8427. tiff_set (&th->ntag, 257, 4, 1, height);
  8428. tiff_set (&th->ntag, 258, 3, colors, output_bps);
  8429. if (colors > 2)
  8430. th->tag[th->ntag-1].val.i = TOFF(th->bps);
  8431. FORC4 th->bps[c] = output_bps;
  8432. tiff_set (&th->ntag, 259, 3, 1, 1);
  8433. tiff_set (&th->ntag, 262, 3, 1, 1 + (colors > 1));
  8434. }
  8435. tiff_set (&th->ntag, 270, 2, 512, TOFF(th->t_desc));
  8436. tiff_set (&th->ntag, 271, 2, 64, TOFF(th->t_make));
  8437. tiff_set (&th->ntag, 272, 2, 64, TOFF(th->t_model));
  8438. if (full) {
  8439. if (oprof) psize = ntohl(oprof[0]);
  8440. tiff_set (&th->ntag, 273, 4, 1, sizeof *th + psize);
  8441. tiff_set (&th->ntag, 277, 3, 1, colors);
  8442. tiff_set (&th->ntag, 278, 4, 1, height);
  8443. tiff_set (&th->ntag, 279, 4, 1, height*width*colors*output_bps/8);
  8444. } else
  8445. tiff_set (&th->ntag, 274, 3, 1, "12435867"[flip]-'0');
  8446. tiff_set (&th->ntag, 282, 5, 1, TOFF(th->rat[0]));
  8447. tiff_set (&th->ntag, 283, 5, 1, TOFF(th->rat[2]));
  8448. tiff_set (&th->ntag, 284, 3, 1, 1);
  8449. tiff_set (&th->ntag, 296, 3, 1, 2);
  8450. tiff_set (&th->ntag, 305, 2, 32, TOFF(th->soft));
  8451. tiff_set (&th->ntag, 306, 2, 20, TOFF(th->date));
  8452. tiff_set (&th->ntag, 315, 2, 64, TOFF(th->t_artist));
  8453. tiff_set (&th->ntag, 34665, 4, 1, TOFF(th->nexif));
  8454. if (psize) tiff_set (&th->ntag, 34675, 7, psize, sizeof *th);
  8455. tiff_set (&th->nexif, 33434, 5, 1, TOFF(th->rat[4]));
  8456. tiff_set (&th->nexif, 33437, 5, 1, TOFF(th->rat[6]));
  8457. tiff_set (&th->nexif, 34855, 3, 1, iso_speed);
  8458. tiff_set (&th->nexif, 37386, 5, 1, TOFF(th->rat[8]));
  8459. if (gpsdata[1]) {
  8460. tiff_set (&th->ntag, 34853, 4, 1, TOFF(th->ngps));
  8461. tiff_set (&th->ngps, 0, 1, 4, 0x202);
  8462. tiff_set (&th->ngps, 1, 2, 2, gpsdata[29]);
  8463. tiff_set (&th->ngps, 2, 5, 3, TOFF(th->gps[0]));
  8464. tiff_set (&th->ngps, 3, 2, 2, gpsdata[30]);
  8465. tiff_set (&th->ngps, 4, 5, 3, TOFF(th->gps[6]));
  8466. tiff_set (&th->ngps, 5, 1, 1, gpsdata[31]);
  8467. tiff_set (&th->ngps, 6, 5, 1, TOFF(th->gps[18]));
  8468. tiff_set (&th->ngps, 7, 5, 3, TOFF(th->gps[12]));
  8469. tiff_set (&th->ngps, 18, 2, 12, TOFF(th->gps[20]));
  8470. tiff_set (&th->ngps, 29, 2, 12, TOFF(th->gps[23]));
  8471. memcpy (th->gps, gpsdata, sizeof th->gps);
  8472. }
  8473. th->rat[0] = th->rat[2] = 300;
  8474. th->rat[1] = th->rat[3] = 1;
  8475. FORC(6) th->rat[4+c] = 1000000;
  8476. th->rat[4] *= shutter;
  8477. th->rat[6] *= aperture;
  8478. th->rat[8] *= focal_len;
  8479. strncpy (th->t_desc, desc, 512);
  8480. strncpy (th->t_make, make, 64);
  8481. strncpy (th->t_model, model, 64);
  8482. strcpy (th->soft, "dcraw v"VERSION);
  8483. t = gmtime (&timestamp);
  8484. sprintf (th->date, "%04d:%02d:%02d %02d:%02d:%02d",
  8485. t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
  8486. strncpy (th->t_artist, artist, 64);
  8487. }
  8488. void CLASS jpeg_thumb_writer (FILE *tfp,char *t_humb,int t_humb_length)
  8489. {
  8490. ushort exif[5];
  8491. struct tiff_hdr th;
  8492. fputc (0xff, tfp);
  8493. fputc (0xd8, tfp);
  8494. if (strcmp (t_humb+6, "Exif")) {
  8495. memcpy (exif, "\xff\xe1 Exif\0\0", 10);
  8496. exif[1] = htons (8 + sizeof th);
  8497. fwrite (exif, 1, sizeof exif, tfp);
  8498. tiff_head (&th, 0);
  8499. fwrite (&th, 1, sizeof th, tfp);
  8500. }
  8501. fwrite (t_humb+2, 1, t_humb_length-2, tfp);
  8502. }
  8503. void CLASS jpeg_thumb()
  8504. {
  8505. char *thumb;
  8506. ushort exif[5];
  8507. struct tiff_hdr th;
  8508. thumb = (char *) malloc (thumb_length);
  8509. merror (thumb, "jpeg_thumb()");
  8510. fread (thumb, 1, thumb_length, ifp);
  8511. #if 0
  8512. fputc (0xff, ofp);
  8513. fputc (0xd8, ofp);
  8514. if (strcmp (thumb+6, "Exif")) {
  8515. memcpy (exif, "\xff\xe1 Exif\0\0", 10);
  8516. exif[1] = htons (8 + sizeof th);
  8517. fwrite (exif, 1, sizeof exif, ofp);
  8518. tiff_head (&th, 0);
  8519. fwrite (&th, 1, sizeof th, ofp);
  8520. }
  8521. fwrite (thumb+2, 1, thumb_length-2, ofp);
  8522. #else
  8523. jpeg_thumb_writer(ofp,thumb,thumb_length);
  8524. #endif
  8525. free (thumb);
  8526. }
  8527. void CLASS write_ppm_tiff()
  8528. {
  8529. struct tiff_hdr th;
  8530. uchar *ppm;
  8531. ushort *ppm2;
  8532. int c, row, col, soff, rstep, cstep;
  8533. int perc, val, total, t_white=0x2000;
  8534. perc = width * height * 0.01; /* 99th percentile white level */
  8535. if (fuji_width) perc /= 2;
  8536. if (!((highlight & ~2) || no_auto_bright))
  8537. for (t_white=c=0; c < colors; c++) {
  8538. for (val=0x2000, total=0; --val > 32; )
  8539. if ((total += histogram[c][val]) > perc) break;
  8540. if (t_white < val) t_white = val;
  8541. }
  8542. gamma_curve (gamm[0], gamm[1], 2, (t_white << 3)/bright);
  8543. iheight = height;
  8544. iwidth = width;
  8545. if (flip & 4) SWAP(height,width);
  8546. ppm = (uchar *) calloc (width, colors*output_bps/8);
  8547. ppm2 = (ushort *) ppm;
  8548. merror (ppm, "write_ppm_tiff()");
  8549. if (output_tiff) {
  8550. tiff_head (&th, 1);
  8551. fwrite (&th, sizeof th, 1, ofp);
  8552. if (oprof)
  8553. fwrite (oprof, ntohl(oprof[0]), 1, ofp);
  8554. } else if (colors > 3)
  8555. fprintf (ofp,
  8556. "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
  8557. width, height, colors, (1 << output_bps)-1, cdesc);
  8558. else
  8559. fprintf (ofp, "P%d\n%d %d\n%d\n",
  8560. colors/2+5, width, height, (1 << output_bps)-1);
  8561. soff = flip_index (0, 0);
  8562. cstep = flip_index (0, 1) - soff;
  8563. rstep = flip_index (1, 0) - flip_index (0, width);
  8564. for (row=0; row < height; row++, soff += rstep) {
  8565. for (col=0; col < width; col++, soff += cstep)
  8566. if (output_bps == 8)
  8567. FORCC ppm [col*colors+c] = curve[image[soff][c]] >> 8;
  8568. else FORCC ppm2[col*colors+c] = curve[image[soff][c]];
  8569. if (output_bps == 16 && !output_tiff && htons(0x55aa) != 0x55aa)
  8570. swab ((char*)ppm2, (char*)ppm2, width*colors*2);
  8571. fwrite (ppm, colors*output_bps/8, width, ofp);
  8572. }
  8573. free (ppm);
  8574. }