PageRenderTime 90ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 2ms

/Source/LibRawLite/internal/dcraw_common.cpp

https://bitbucket.org/tiran/freeimageturbo
C++ | 9243 lines | 8712 code | 382 blank | 149 comment | 2768 complexity | 5e99ac5ef38224ec4d56240b56b1ab94 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0, BSD-2-Clause, LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-2.0, GPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. Copyright 2008-2010 LibRaw LLC (info@libraw.org)
  3. LibRaw is free software; you can redistribute it and/or modify
  4. it under the terms of the one of three licenses as you choose:
  5. 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
  6. (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
  7. 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  8. (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
  9. 3. LibRaw Software License 27032010
  10. (See file LICENSE.LibRaw.pdf provided in LibRaw distribution archive for details).
  11. This file is generated from Dave Coffin's dcraw.c
  12. dcraw.c -- Dave Coffin's raw photo decoder
  13. Copyright 1997-2010 by Dave Coffin, dcoffin a cybercom o net
  14. Look into dcraw homepage (probably http://cybercom.net/~dcoffin/dcraw/)
  15. for more information
  16. */
  17. #include <math.h>
  18. #define CLASS LibRaw::
  19. #include "libraw/libraw_types.h"
  20. #define LIBRAW_LIBRARY_BUILD
  21. #define LIBRAW_IO_REDEFINED
  22. #include "libraw/libraw.h"
  23. #include "internal/defines.h"
  24. #include "internal/var_defines.h"
  25. #include "internal/libraw_bytebuffer.h"
  26. #ifndef __GLIBC__
  27. char *my_memmem (char *haystack, size_t haystacklen,
  28. char *needle, size_t needlelen)
  29. {
  30. char *c;
  31. for (c = haystack; c <= haystack + haystacklen - needlelen; c++)
  32. if (!memcmp (c, needle, needlelen))
  33. return c;
  34. return 0;
  35. }
  36. #define memmem my_memmem
  37. #endif
  38. ushort CLASS sget2 (uchar *s)
  39. {
  40. if (order == 0x4949) /* "II" means little-endian */
  41. return s[0] | s[1] << 8;
  42. else /* "MM" means big-endian */
  43. return s[0] << 8 | s[1];
  44. }
  45. ushort CLASS get2()
  46. {
  47. uchar str[2] = { 0xff,0xff };
  48. fread (str, 1, 2, ifp);
  49. return sget2(str);
  50. }
  51. unsigned CLASS sget4 (uchar *s)
  52. {
  53. if (order == 0x4949)
  54. return s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
  55. else
  56. return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3];
  57. }
  58. #define sget4(s) sget4((uchar *)s)
  59. unsigned CLASS get4()
  60. {
  61. uchar str[4] = { 0xff,0xff,0xff,0xff };
  62. fread (str, 1, 4, ifp);
  63. return sget4(str);
  64. }
  65. unsigned CLASS getint (int type)
  66. {
  67. return type == 3 ? get2() : get4();
  68. }
  69. float CLASS int_to_float (int i)
  70. {
  71. union { int i; float f; } u;
  72. u.i = i;
  73. return u.f;
  74. }
  75. double CLASS getreal (int type)
  76. {
  77. union { char c[8]; double d; } u;
  78. int i, rev;
  79. switch (type) {
  80. case 3: return (unsigned short) get2();
  81. case 4: return (unsigned int) get4();
  82. case 5: u.d = (unsigned int) get4();
  83. return u.d / (unsigned int) get4();
  84. case 8: return (signed short) get2();
  85. case 9: return (signed int) get4();
  86. case 10: u.d = (signed int) get4();
  87. return u.d / (signed int) get4();
  88. case 11: return int_to_float (get4());
  89. case 12:
  90. rev = 7 * ((order == 0x4949) == (ntohs(0x1234) == 0x1234));
  91. for (i=0; i < 8; i++)
  92. u.c[i ^ rev] = fgetc(ifp);
  93. return u.d;
  94. default: return fgetc(ifp);
  95. }
  96. }
  97. void CLASS read_shorts (ushort *pixel, int count)
  98. {
  99. if (fread (pixel, 2, count, ifp) < count) derror();
  100. if ((order == 0x4949) == (ntohs(0x1234) == 0x1234))
  101. swab ((char*)pixel, (char*)pixel, count*2);
  102. }
  103. #ifdef LIBRAW_LIBRARY_BUILD
  104. #define RBAYER(x,y) raw_image[(x)*raw_width+(y)]
  105. #define RRBAYER(x,y) raw_image[((x)+top_margin)*raw_width+(y)+left_margin]
  106. #define CBAYER(x,y) color_image[((x)+top_margin)*raw_width+(y)+left_margin][FC((x),(y))]
  107. #else
  108. #define CBAYER(x,y) BAYER((x),(y))
  109. #define RBAYER(x,y) BAYER((x),(y))
  110. #define RRBAYER(x,y) BAYER((x),(y))
  111. #endif
  112. void CLASS canon_600_fixed_wb (int temp)
  113. {
  114. static const short mul[4][5] = {
  115. { 667, 358,397,565,452 },
  116. { 731, 390,367,499,517 },
  117. { 1119, 396,348,448,537 },
  118. { 1399, 485,431,508,688 } };
  119. int lo, hi, i;
  120. float frac=0;
  121. for (lo=4; --lo; )
  122. if (*mul[lo] <= temp) break;
  123. for (hi=0; hi < 3; hi++)
  124. if (*mul[hi] >= temp) break;
  125. if (lo != hi)
  126. frac = (float) (temp - *mul[lo]) / (*mul[hi] - *mul[lo]);
  127. for (i=1; i < 5; i++)
  128. pre_mul[i-1] = 1 / (frac * mul[hi][i] + (1-frac) * mul[lo][i]);
  129. #ifdef LIBRAW_LIBRARY_BUILD
  130. color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
  131. #endif
  132. }
  133. /* Return values: 0 = white 1 = near white 2 = not white */
  134. int CLASS canon_600_color (int ratio[2], int mar)
  135. {
  136. int clipped=0, target, miss;
  137. if (flash_used) {
  138. if (ratio[1] < -104)
  139. { ratio[1] = -104; clipped = 1; }
  140. if (ratio[1] > 12)
  141. { ratio[1] = 12; clipped = 1; }
  142. } else {
  143. if (ratio[1] < -264 || ratio[1] > 461) return 2;
  144. if (ratio[1] < -50)
  145. { ratio[1] = -50; clipped = 1; }
  146. if (ratio[1] > 307)
  147. { ratio[1] = 307; clipped = 1; }
  148. }
  149. target = flash_used || ratio[1] < 197
  150. ? -38 - (398 * ratio[1] >> 10)
  151. : -123 + (48 * ratio[1] >> 10);
  152. if (target - mar <= ratio[0] &&
  153. target + 20 >= ratio[0] && !clipped) return 0;
  154. miss = target - ratio[0];
  155. if (abs(miss) >= mar*4) return 2;
  156. if (miss < -20) miss = -20;
  157. if (miss > mar) miss = mar;
  158. ratio[0] = target - miss;
  159. return 1;
  160. }
  161. void CLASS canon_600_auto_wb()
  162. {
  163. int mar, row, col, i, j, st, count[] = { 0,0 };
  164. int test[8], total[2][8], ratio[2][2], stat[2];
  165. memset (&total, 0, sizeof total);
  166. i = canon_ev + 0.5;
  167. if (i < 10) mar = 150;
  168. else if (i > 12) mar = 20;
  169. else mar = 280 - 20 * i;
  170. if (flash_used) mar = 80;
  171. for (row=14; row < height-14; row+=4)
  172. for (col=10; col < width; col+=2) {
  173. for (i=0; i < 8; i++)
  174. test[(i & 4) + FC(row+(i >> 1),col+(i & 1))] =
  175. BAYER(row+(i >> 1),col+(i & 1));
  176. for (i=0; i < 8; i++)
  177. if (test[i] < 150 || test[i] > 1500) goto next;
  178. for (i=0; i < 4; i++)
  179. if (abs(test[i] - test[i+4]) > 50) goto next;
  180. for (i=0; i < 2; i++) {
  181. for (j=0; j < 4; j+=2)
  182. ratio[i][j >> 1] = ((test[i*4+j+1]-test[i*4+j]) << 10) / test[i*4+j];
  183. stat[i] = canon_600_color (ratio[i], mar);
  184. }
  185. if ((st = stat[0] | stat[1]) > 1) goto next;
  186. for (i=0; i < 2; i++)
  187. if (stat[i])
  188. for (j=0; j < 2; j++)
  189. test[i*4+j*2+1] = test[i*4+j*2] * (0x400 + ratio[i][j]) >> 10;
  190. for (i=0; i < 8; i++)
  191. total[st][i] += test[i];
  192. count[st]++;
  193. next: ;
  194. }
  195. if (count[0] | count[1]) {
  196. st = count[0]*200 < count[1];
  197. for (i=0; i < 4; i++)
  198. pre_mul[i] = 1.0 / (total[st][i] + total[st][i+4]);
  199. #ifdef LIBRAW_LIBRARY_BUILD
  200. color_flags.pre_mul_state = LIBRAW_COLORSTATE_CALCULATED;
  201. #endif
  202. }
  203. }
  204. void CLASS canon_600_coeff()
  205. {
  206. static const short table[6][12] = {
  207. { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
  208. { -1203,1715,-1136,1648, 1388,-876,267,245, -1641,2153,3921,-3409 },
  209. { -615,1127,-1563,2075, 1437,-925,509,3, -756,1268,2519,-2007 },
  210. { -190,702,-1886,2398, 2153,-1641,763,-251, -452,964,3040,-2528 },
  211. { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
  212. { -807,1319,-1785,2297, 1388,-876,769,-257, -230,742,2067,-1555 } };
  213. int t=0, i, c;
  214. float mc, yc;
  215. mc = pre_mul[1] / pre_mul[2];
  216. yc = pre_mul[3] / pre_mul[2];
  217. if (mc > 1 && mc <= 1.28 && yc < 0.8789) t=1;
  218. if (mc > 1.28 && mc <= 2) {
  219. if (yc < 0.8789) t=3;
  220. else if (yc <= 2) t=4;
  221. }
  222. if (flash_used) t=5;
  223. for (raw_color = i=0; i < 3; i++)
  224. FORCC rgb_cam[i][c] = table[t][i*4 + c] / 1024.0;
  225. #ifdef LIBRAW_LIBRARY_BUILD
  226. color_flags.rgb_cam_state = LIBRAW_COLORSTATE_CALCULATED;
  227. #endif
  228. }
  229. void CLASS canon_600_load_raw()
  230. {
  231. uchar data[1120], *dp;
  232. ushort pixel[896], *pix;
  233. int irow, row, col, val;
  234. static const short mul[4][2] =
  235. { { 1141,1145 }, { 1128,1109 }, { 1178,1149 }, { 1128,1109 } };
  236. for (irow=row=0; irow < height; irow++) {
  237. if (fread (data, 1, raw_width*5/4, ifp) < raw_width*5/4) derror();
  238. for (dp=data, pix=pixel; dp < data+1120; dp+=10, pix+=8) {
  239. pix[0] = (dp[0] << 2) + (dp[1] >> 6 );
  240. pix[1] = (dp[2] << 2) + (dp[1] >> 4 & 3);
  241. pix[2] = (dp[3] << 2) + (dp[1] >> 2 & 3);
  242. pix[3] = (dp[4] << 2) + (dp[1] & 3);
  243. pix[4] = (dp[5] << 2) + (dp[9] & 3);
  244. pix[5] = (dp[6] << 2) + (dp[9] >> 2 & 3);
  245. pix[6] = (dp[7] << 2) + (dp[9] >> 4 & 3);
  246. pix[7] = (dp[8] << 2) + (dp[9] >> 6 );
  247. }
  248. #ifndef LIBRAW_LIBRARY_BUILD
  249. for (col=0; col < width; col++)
  250. {
  251. BAYER(row,col) = pixel[col];
  252. }
  253. #else
  254. for (col=0; col < raw_width; col++)
  255. RBAYER(row,col) = pixel[col];
  256. #endif
  257. for (col=width; col < raw_width; col++)
  258. {
  259. black += pixel[col];
  260. }
  261. if ((row+=2) > height) row = 1;
  262. }
  263. if (raw_width > width)
  264. black = black / ((raw_width - width) * height) - 4;
  265. #ifndef LIBRAW_LIBRARY_BUILD
  266. for (row=0; row < height; row++)
  267. for (col=0; col < width; col++) {
  268. if ((val = BAYER(row,col) - black) < 0) val = 0;
  269. val = val * mul[row & 3][col & 1] >> 9;
  270. BAYER(row,col) = val;
  271. }
  272. #else
  273. for (row=0; row < height; row++)
  274. for (col=0; col < raw_width; col++) {
  275. if ((val = RBAYER(row,col) - black) < 0) val = 0;
  276. val = val * mul[row & 3][col & 1] >> 9;
  277. RBAYER(row,col) = val;
  278. }
  279. #endif
  280. canon_600_fixed_wb(1311);
  281. canon_600_auto_wb();
  282. canon_600_coeff();
  283. maximum = (0x3ff - black) * 1109 >> 9;
  284. black = 0;
  285. }
  286. void CLASS remove_zeroes()
  287. {
  288. unsigned row, col, tot, n, r, c;
  289. #ifdef LIBRAW_LIBRARY_BUILD
  290. RUN_CALLBACK(LIBRAW_PROGRESS_REMOVE_ZEROES,0,2);
  291. #endif
  292. for (row=0; row < height; row++)
  293. for (col=0; col < width; col++)
  294. if (BAYER(row,col) == 0) {
  295. tot = n = 0;
  296. for (r = row-2; r <= row+2; r++)
  297. for (c = col-2; c <= col+2; c++)
  298. if (r < height && c < width &&
  299. FC(r,c) == FC(row,col) && BAYER(r,c))
  300. tot += (n++,BAYER(r,c));
  301. if (n) BAYER(row,col) = tot/n;
  302. }
  303. #ifdef LIBRAW_LIBRARY_BUILD
  304. RUN_CALLBACK(LIBRAW_PROGRESS_REMOVE_ZEROES,1,2);
  305. #endif
  306. }
  307. int CLASS canon_s2is()
  308. {
  309. unsigned row;
  310. for (row=0; row < 100; row++) {
  311. fseek (ifp, row*3340 + 3284, SEEK_SET);
  312. if (getc(ifp) > 15) return 1;
  313. }
  314. return 0;
  315. }
  316. /*
  317. getbits(-1) initializes the buffer
  318. getbits(n) where 0 <= n <= 25 returns an n-bit integer
  319. */
  320. unsigned CLASS getbithuff (int nbits, ushort *huff)
  321. {
  322. #ifdef LIBRAW_NOTHREADS
  323. static unsigned bitbuf=0;
  324. static int vbits=0, reset=0;
  325. #else
  326. #define bitbuf tls->getbits.bitbuf
  327. #define vbits tls->getbits.vbits
  328. #define reset tls->getbits.reset
  329. #endif
  330. unsigned c;
  331. if (nbits == -1)
  332. return bitbuf = vbits = reset = 0;
  333. if (nbits == 0 || vbits < 0) return 0;
  334. while (!reset && vbits < nbits && (c = fgetc(ifp)) != EOF &&
  335. !(reset = zero_after_ff && c == 0xff && fgetc(ifp))) {
  336. bitbuf = (bitbuf << 8) + (uchar) c;
  337. vbits += 8;
  338. }
  339. c = bitbuf << (32-vbits) >> (32-nbits);
  340. if (huff) {
  341. vbits -= huff[c] >> 8;
  342. c = (uchar) huff[c];
  343. } else
  344. vbits -= nbits;
  345. if (vbits < 0) derror();
  346. return c;
  347. #ifndef LIBRAW_NOTHREADS
  348. #undef bitbuf
  349. #undef vbits
  350. #undef reset
  351. #endif
  352. }
  353. #define getbits(n) getbithuff(n,0)
  354. #define gethuff(h) getbithuff(*h,h+1)
  355. /*
  356. Construct a decode tree according to the specification in *source.
  357. The first 16 bytes specify how many codes should be 1-bit, 2-bit
  358. 3-bit, etc. Bytes after that are the leaf values.
  359. For example, if the source is
  360. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
  361. 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
  362. then the code is
  363. 00 0x04
  364. 010 0x03
  365. 011 0x05
  366. 100 0x06
  367. 101 0x02
  368. 1100 0x07
  369. 1101 0x01
  370. 11100 0x08
  371. 11101 0x09
  372. 11110 0x00
  373. 111110 0x0a
  374. 1111110 0x0b
  375. 1111111 0xff
  376. */
  377. ushort * CLASS make_decoder_ref (const uchar **source)
  378. {
  379. int max, len, h, i, j;
  380. const uchar *count;
  381. ushort *huff;
  382. count = (*source += 16) - 17;
  383. for (max=16; max && !count[max]; max--);
  384. huff = (ushort *) calloc (1 + (1 << max), sizeof *huff);
  385. merror (huff, "make_decoder()");
  386. huff[0] = max;
  387. for (h=len=1; len <= max; len++)
  388. for (i=0; i < count[len]; i++, ++*source)
  389. for (j=0; j < 1 << (max-len); j++)
  390. if (h <= 1 << max)
  391. huff[h++] = len << 8 | **source;
  392. return huff;
  393. }
  394. ushort * CLASS make_decoder (const uchar *source)
  395. {
  396. return make_decoder_ref (&source);
  397. }
  398. void CLASS crw_init_tables (unsigned table, ushort *huff[2])
  399. {
  400. static const uchar first_tree[3][29] = {
  401. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
  402. 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
  403. { 0,2,2,3,1,1,1,1,2,0,0,0,0,0,0,0,
  404. 0x03,0x02,0x04,0x01,0x05,0x00,0x06,0x07,0x09,0x08,0x0a,0x0b,0xff },
  405. { 0,0,6,3,1,1,2,0,0,0,0,0,0,0,0,0,
  406. 0x06,0x05,0x07,0x04,0x08,0x03,0x09,0x02,0x00,0x0a,0x01,0x0b,0xff },
  407. };
  408. static const uchar second_tree[3][180] = {
  409. { 0,2,2,2,1,4,2,1,2,5,1,1,0,0,0,139,
  410. 0x03,0x04,0x02,0x05,0x01,0x06,0x07,0x08,
  411. 0x12,0x13,0x11,0x14,0x09,0x15,0x22,0x00,0x21,0x16,0x0a,0xf0,
  412. 0x23,0x17,0x24,0x31,0x32,0x18,0x19,0x33,0x25,0x41,0x34,0x42,
  413. 0x35,0x51,0x36,0x37,0x38,0x29,0x79,0x26,0x1a,0x39,0x56,0x57,
  414. 0x28,0x27,0x52,0x55,0x58,0x43,0x76,0x59,0x77,0x54,0x61,0xf9,
  415. 0x71,0x78,0x75,0x96,0x97,0x49,0xb7,0x53,0xd7,0x74,0xb6,0x98,
  416. 0x47,0x48,0x95,0x69,0x99,0x91,0xfa,0xb8,0x68,0xb5,0xb9,0xd6,
  417. 0xf7,0xd8,0x67,0x46,0x45,0x94,0x89,0xf8,0x81,0xd5,0xf6,0xb4,
  418. 0x88,0xb1,0x2a,0x44,0x72,0xd9,0x87,0x66,0xd4,0xf5,0x3a,0xa7,
  419. 0x73,0xa9,0xa8,0x86,0x62,0xc7,0x65,0xc8,0xc9,0xa1,0xf4,0xd1,
  420. 0xe9,0x5a,0x92,0x85,0xa6,0xe7,0x93,0xe8,0xc1,0xc6,0x7a,0x64,
  421. 0xe1,0x4a,0x6a,0xe6,0xb3,0xf1,0xd3,0xa5,0x8a,0xb2,0x9a,0xba,
  422. 0x84,0xa4,0x63,0xe5,0xc5,0xf3,0xd2,0xc4,0x82,0xaa,0xda,0xe4,
  423. 0xf2,0xca,0x83,0xa3,0xa2,0xc3,0xea,0xc2,0xe2,0xe3,0xff,0xff },
  424. { 0,2,2,1,4,1,4,1,3,3,1,0,0,0,0,140,
  425. 0x02,0x03,0x01,0x04,0x05,0x12,0x11,0x06,
  426. 0x13,0x07,0x08,0x14,0x22,0x09,0x21,0x00,0x23,0x15,0x31,0x32,
  427. 0x0a,0x16,0xf0,0x24,0x33,0x41,0x42,0x19,0x17,0x25,0x18,0x51,
  428. 0x34,0x43,0x52,0x29,0x35,0x61,0x39,0x71,0x62,0x36,0x53,0x26,
  429. 0x38,0x1a,0x37,0x81,0x27,0x91,0x79,0x55,0x45,0x28,0x72,0x59,
  430. 0xa1,0xb1,0x44,0x69,0x54,0x58,0xd1,0xfa,0x57,0xe1,0xf1,0xb9,
  431. 0x49,0x47,0x63,0x6a,0xf9,0x56,0x46,0xa8,0x2a,0x4a,0x78,0x99,
  432. 0x3a,0x75,0x74,0x86,0x65,0xc1,0x76,0xb6,0x96,0xd6,0x89,0x85,
  433. 0xc9,0xf5,0x95,0xb4,0xc7,0xf7,0x8a,0x97,0xb8,0x73,0xb7,0xd8,
  434. 0xd9,0x87,0xa7,0x7a,0x48,0x82,0x84,0xea,0xf4,0xa6,0xc5,0x5a,
  435. 0x94,0xa4,0xc6,0x92,0xc3,0x68,0xb5,0xc8,0xe4,0xe5,0xe6,0xe9,
  436. 0xa2,0xa3,0xe3,0xc2,0x66,0x67,0x93,0xaa,0xd4,0xd5,0xe7,0xf8,
  437. 0x88,0x9a,0xd7,0x77,0xc4,0x64,0xe2,0x98,0xa5,0xca,0xda,0xe8,
  438. 0xf3,0xf6,0xa9,0xb2,0xb3,0xf2,0xd2,0x83,0xba,0xd3,0xff,0xff },
  439. { 0,0,6,2,1,3,3,2,5,1,2,2,8,10,0,117,
  440. 0x04,0x05,0x03,0x06,0x02,0x07,0x01,0x08,
  441. 0x09,0x12,0x13,0x14,0x11,0x15,0x0a,0x16,0x17,0xf0,0x00,0x22,
  442. 0x21,0x18,0x23,0x19,0x24,0x32,0x31,0x25,0x33,0x38,0x37,0x34,
  443. 0x35,0x36,0x39,0x79,0x57,0x58,0x59,0x28,0x56,0x78,0x27,0x41,
  444. 0x29,0x77,0x26,0x42,0x76,0x99,0x1a,0x55,0x98,0x97,0xf9,0x48,
  445. 0x54,0x96,0x89,0x47,0xb7,0x49,0xfa,0x75,0x68,0xb6,0x67,0x69,
  446. 0xb9,0xb8,0xd8,0x52,0xd7,0x88,0xb5,0x74,0x51,0x46,0xd9,0xf8,
  447. 0x3a,0xd6,0x87,0x45,0x7a,0x95,0xd5,0xf6,0x86,0xb4,0xa9,0x94,
  448. 0x53,0x2a,0xa8,0x43,0xf5,0xf7,0xd4,0x66,0xa7,0x5a,0x44,0x8a,
  449. 0xc9,0xe8,0xc8,0xe7,0x9a,0x6a,0x73,0x4a,0x61,0xc7,0xf4,0xc6,
  450. 0x65,0xe9,0x72,0xe6,0x71,0x91,0x93,0xa6,0xda,0x92,0x85,0x62,
  451. 0xf3,0xc5,0xb2,0xa4,0x84,0xba,0x64,0xa5,0xb3,0xd2,0x81,0xe5,
  452. 0xd3,0xaa,0xc4,0xca,0xf2,0xb1,0xe4,0xd1,0x83,0x63,0xea,0xc3,
  453. 0xe2,0x82,0xf1,0xa3,0xc2,0xa1,0xc1,0xe3,0xa2,0xe1,0xff,0xff }
  454. };
  455. if (table > 2) table = 2;
  456. huff[0] = make_decoder ( first_tree[table]);
  457. huff[1] = make_decoder (second_tree[table]);
  458. }
  459. /*
  460. Return 0 if the image starts with compressed data,
  461. 1 if it starts with uncompressed low-order bits.
  462. In Canon compressed data, 0xff is always followed by 0x00.
  463. */
  464. int CLASS canon_has_lowbits()
  465. {
  466. uchar test[0x4000];
  467. int ret=1;
  468. unsigned i;
  469. fseek (ifp, 0, SEEK_SET);
  470. fread (test, 1, sizeof test, ifp);
  471. for (i=540; i < sizeof test - 1; i++)
  472. if (test[i] == 0xff) {
  473. if (test[i+1]) return 1;
  474. ret=0;
  475. }
  476. return ret;
  477. }
  478. void CLASS canon_compressed_load_raw()
  479. {
  480. ushort *pixel, *prow, *huff[2];
  481. int nblocks, lowbits, i, c, row, r, col, save, val;
  482. unsigned irow, icol;
  483. int block, diffbuf[64], leaf, len, diff, carry=0, pnum=0, base[2];
  484. crw_init_tables (tiff_compress, huff);
  485. pixel = (ushort *) calloc (raw_width*8, sizeof *pixel);
  486. merror (pixel, "canon_compressed_load_raw()");
  487. lowbits = canon_has_lowbits();
  488. if (!lowbits) maximum = 0x3ff;
  489. fseek (ifp, 540 + lowbits*raw_height*raw_width/4, SEEK_SET);
  490. zero_after_ff = 1;
  491. getbits(-1);
  492. for (row=0; row < raw_height; row+=8) {
  493. nblocks = MIN (8, raw_height-row) * raw_width >> 6;
  494. for (block=0; block < nblocks; block++) {
  495. memset (diffbuf, 0, sizeof diffbuf);
  496. for (i=0; i < 64; i++ ) {
  497. leaf = gethuff(huff[i > 0]);
  498. if (leaf == 0 && i) break;
  499. if (leaf == 0xff) continue;
  500. i += leaf >> 4;
  501. len = leaf & 15;
  502. if (len == 0) continue;
  503. diff = getbits(len);
  504. if ((diff & (1 << (len-1))) == 0)
  505. diff -= (1 << len) - 1;
  506. if (i < 64) diffbuf[i] = diff;
  507. }
  508. diffbuf[0] += carry;
  509. carry = diffbuf[0];
  510. for (i=0; i < 64; i++ ) {
  511. if (pnum++ % raw_width == 0)
  512. base[0] = base[1] = 512;
  513. if ((pixel[(block << 6) + i] = base[i & 1] += diffbuf[i]) >> 10)
  514. derror();
  515. }
  516. }
  517. if (lowbits) {
  518. save = ftell(ifp);
  519. fseek (ifp, 26 + row*raw_width/4, SEEK_SET);
  520. for (prow=pixel, i=0; i < raw_width*2; i++) {
  521. c = fgetc(ifp);
  522. for (r=0; r < 8; r+=2, prow++) {
  523. val = (*prow << 2) + ((c >> r) & 3);
  524. if (raw_width == 2672 && val < 512) val += 2;
  525. *prow = val;
  526. }
  527. }
  528. fseek (ifp, save, SEEK_SET);
  529. }
  530. #ifdef LIBRAW_LIBRARY_BUILD
  531. for (r=0; r < 8; r++) {
  532. if(row+r>=raw_height) break; // Not sure that raw_height is always N*8
  533. // MOVE entire row into place
  534. memmove(&raw_image[(row+r)*raw_width],&pixel[r*raw_width],raw_width*sizeof(pixel[0]));
  535. irow = row - top_margin + r;
  536. if (irow >= height) continue; // if row above image area than irow is VERY positive :)
  537. // only margins!
  538. for (col=0; col < left_margin; col++)
  539. {
  540. icol = col - left_margin;
  541. c = FC(irow,icol);
  542. if (icol >= width && col > 1 && (unsigned) (col-left_margin+2) > width+3)
  543. cblack[c] += (cblack[4+c]++,pixel[r*raw_width+col]);
  544. }
  545. for (col=width+left_margin; col < raw_width; col++)
  546. {
  547. icol = col - left_margin;
  548. c = FC(irow,icol);
  549. if (icol >= width && col > 1 && (unsigned) (col-left_margin+2) > width+3)
  550. cblack[c] += (cblack[4+c]++,pixel[r*raw_width+col]);
  551. }
  552. }
  553. #else
  554. // dcraw original code
  555. for (r=0; r < 8; r++) {
  556. irow = row - top_margin + r;
  557. if (irow >= height) continue;
  558. for (col=0; col < raw_width; col++) {
  559. icol = col - left_margin;
  560. c = FC(irow,icol);
  561. if (icol < width)
  562. BAYER(irow,icol) = pixel[r*raw_width+col];
  563. else if (col > 1 && (unsigned) (col-left_margin+2) > width+3)
  564. cblack[c] += (cblack[4+c]++,pixel[r*raw_width+col]);
  565. }
  566. }
  567. #endif
  568. }
  569. free (pixel);
  570. FORC(2) free (huff[c]);
  571. FORC4 if (cblack[4+c]) cblack[c] /= cblack[4+c];
  572. }
  573. int CLASS ljpeg_start (struct jhead *jh, int info_only)
  574. {
  575. int c, tag, len;
  576. uchar data[0x10000];
  577. const uchar *dp;
  578. memset (jh, 0, sizeof *jh);
  579. jh->restart = INT_MAX;
  580. fread (data, 2, 1, ifp);
  581. if (data[1] != 0xd8) return 0;
  582. do {
  583. fread (data, 2, 2, ifp);
  584. tag = data[0] << 8 | data[1];
  585. len = (data[2] << 8 | data[3]) - 2;
  586. if (tag <= 0xff00) return 0;
  587. fread (data, 1, len, ifp);
  588. switch (tag) {
  589. case 0xffc3:
  590. jh->sraw = ((data[7] >> 4) * (data[7] & 15) - 1) & 3;
  591. case 0xffc0:
  592. jh->bits = data[0];
  593. jh->high = data[1] << 8 | data[2];
  594. jh->wide = data[3] << 8 | data[4];
  595. jh->clrs = data[5] + jh->sraw;
  596. if (len == 9 && !dng_version) getc(ifp);
  597. break;
  598. case 0xffc4:
  599. if (info_only) break;
  600. for (dp = data; dp < data+len && (c = *dp++) < 4; )
  601. jh->free[c] = jh->huff[c] = make_decoder_ref (&dp);
  602. break;
  603. case 0xffda:
  604. jh->psv = data[1+data[0]*2];
  605. jh->bits -= data[3+data[0]*2] & 15;
  606. break;
  607. case 0xffdd:
  608. jh->restart = data[0] << 8 | data[1];
  609. }
  610. } while (tag != 0xffda);
  611. if (info_only) return 1;
  612. FORC(5) if (!jh->huff[c+1]) jh->huff[c+1] = jh->huff[c];
  613. if (jh->sraw) {
  614. FORC(4) jh->huff[2+c] = jh->huff[1];
  615. FORC(jh->sraw) jh->huff[1+c] = jh->huff[0];
  616. }
  617. jh->row = (ushort *) calloc (jh->wide*jh->clrs, 4);
  618. merror (jh->row, "ljpeg_start()");
  619. return zero_after_ff = 1;
  620. }
  621. void CLASS ljpeg_end (struct jhead *jh)
  622. {
  623. int c;
  624. FORC4 if (jh->free[c]) free (jh->free[c]);
  625. free (jh->row);
  626. }
  627. // used for kodak-262 decoder
  628. int CLASS ljpeg_diff (ushort *huff)
  629. {
  630. int len, diff;
  631. len = gethuff(huff);
  632. if (len == 16 && (!dng_version || dng_version >= 0x1010000))
  633. return -32768;
  634. diff = getbits(len);
  635. if ((diff & (1 << (len-1))) == 0)
  636. diff -= (1 << len) - 1;
  637. return diff;
  638. }
  639. #ifdef LIBRAW_LIBRARY_BUILD
  640. int CLASS ljpeg_diff_new (LibRaw_bit_buffer& bits, LibRaw_byte_buffer* buf,ushort *huff)
  641. {
  642. int len, diff;
  643. len = bits._gethuff_lj(buf,*huff,huff+1);
  644. if (len == 16 && (!dng_version || dng_version >= 0x1010000))
  645. return -32768;
  646. diff = bits._getbits_lj(buf,len);
  647. if ((diff & (1 << (len-1))) == 0)
  648. diff -= (1 << len) - 1;
  649. return diff;
  650. }
  651. int CLASS ljpeg_diff_pef (LibRaw_bit_buffer& bits, LibRaw_byte_buffer* buf,ushort *huff)
  652. {
  653. int len, diff;
  654. len = bits._gethuff(buf,*huff,huff+1,zero_after_ff);
  655. if (len == 16 && (!dng_version || dng_version >= 0x1010000))
  656. return -32768;
  657. diff = bits._getbits(buf,len,zero_after_ff);
  658. if ((diff & (1 << (len-1))) == 0)
  659. diff -= (1 << len) - 1;
  660. return diff;
  661. }
  662. #endif
  663. ushort * CLASS ljpeg_row (int jrow, struct jhead *jh)
  664. {
  665. int col, c, diff, pred, spred=0;
  666. ushort mark=0, *row[3];
  667. if (jrow * jh->wide % jh->restart == 0) {
  668. FORC(6) jh->vpred[c] = 1 << (jh->bits-1);
  669. if (jrow) {
  670. fseek (ifp, -2, SEEK_CUR);
  671. do mark = (mark << 8) + (c = fgetc(ifp));
  672. while (c != EOF && mark >> 4 != 0xffd);
  673. }
  674. getbits(-1);
  675. }
  676. FORC3 row[c] = jh->row + jh->wide*jh->clrs*((jrow+c) & 1);
  677. for (col=0; col < jh->wide; col++)
  678. FORC(jh->clrs) {
  679. diff = ljpeg_diff (jh->huff[c]);
  680. if (jh->sraw && c <= jh->sraw && (col | c))
  681. pred = spred;
  682. else if (col) pred = row[0][-jh->clrs];
  683. else pred = (jh->vpred[c] += diff) - diff;
  684. if (jrow && col) switch (jh->psv) {
  685. case 1: break;
  686. case 2: pred = row[1][0]; break;
  687. case 3: pred = row[1][-jh->clrs]; break;
  688. case 4: pred = pred + row[1][0] - row[1][-jh->clrs]; break;
  689. case 5: pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1); break;
  690. case 6: pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1); break;
  691. case 7: pred = (pred + row[1][0]) >> 1; break;
  692. default: pred = 0;
  693. }
  694. if ((**row = pred + diff) >> jh->bits) derror();
  695. if (c <= jh->sraw) spred = **row;
  696. row[0]++; row[1]++;
  697. }
  698. return row[2];
  699. }
  700. #ifdef LIBRAW_LIBRARY_BUILD
  701. ushort * CLASS ljpeg_row_new (int jrow, struct jhead *jh, LibRaw_bit_buffer& bits,LibRaw_byte_buffer* bytes)
  702. {
  703. int col, c, diff, pred, spred=0;
  704. ushort mark=0, *row[3];
  705. if (jrow * jh->wide % jh->restart == 0) {
  706. FORC(6) jh->vpred[c] = 1 << (jh->bits-1);
  707. if (jrow) {
  708. bytes->unseek2();
  709. do mark = (mark << 8) + (c = bytes->get_byte());
  710. while (c != EOF && mark >> 4 != 0xffd);
  711. }
  712. bits.reset();
  713. }
  714. FORC3 row[c] = jh->row + jh->wide*jh->clrs*((jrow+c) & 1);
  715. for (col=0; col < jh->wide; col++)
  716. FORC(jh->clrs) {
  717. diff = ljpeg_diff_new (bits,bytes,jh->huff[c]);
  718. if (jh->sraw && c <= jh->sraw && (col | c))
  719. pred = spred;
  720. else if (col) pred = row[0][-jh->clrs];
  721. else pred = (jh->vpred[c] += diff) - diff;
  722. if (jrow && col) switch (jh->psv) {
  723. case 1: break;
  724. case 2: pred = row[1][0]; break;
  725. case 3: pred = row[1][-jh->clrs]; break;
  726. case 4: pred = pred + row[1][0] - row[1][-jh->clrs]; break;
  727. case 5: pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1); break;
  728. case 6: pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1); break;
  729. case 7: pred = (pred + row[1][0]) >> 1; break;
  730. default: pred = 0;
  731. }
  732. if ((**row = pred + diff) >> jh->bits) derror();
  733. if (c <= jh->sraw) spred = **row;
  734. row[0]++; row[1]++;
  735. }
  736. return row[2];
  737. }
  738. #endif
  739. void CLASS lossless_jpeg_load_raw()
  740. {
  741. int jwide, jrow, jcol, val, c, i, row=0, col=0;
  742. #ifndef LIBRAW_LIBRARY_BUILD
  743. int jidx,j;
  744. #endif
  745. struct jhead jh;
  746. int min=INT_MAX;
  747. ushort *rp;
  748. #ifdef LIBRAW_LIBRARY_BUILD
  749. int save_min = 0;
  750. unsigned slicesW[16],slicesWcnt=0,slices;
  751. unsigned *offset;
  752. unsigned t_y=0,t_x=0,t_s=0,slice=0,pixelsInSlice,pixno;
  753. if (!strcasecmp(make,"KODAK"))
  754. save_min = 1;
  755. #endif
  756. #ifdef LIBRAW_LIBRARY_BUILD
  757. if (cr2_slice[0]>15)
  758. throw LIBRAW_EXCEPTION_IO_EOF; // change many slices
  759. #else
  760. fprintf(stderr,"Too many CR2 slices: %d\n",cr2_slice[0]+1);
  761. return;
  762. #endif
  763. if (!ljpeg_start (&jh, 0)) return;
  764. jwide = jh.wide * jh.clrs;
  765. #ifdef LIBRAW_LIBRARY_BUILD
  766. if(cr2_slice[0])
  767. {
  768. for(i=0;i<cr2_slice[0];i++)
  769. slicesW[slicesWcnt++] = cr2_slice[1];
  770. slicesW[slicesWcnt++] = cr2_slice[2];
  771. }
  772. else
  773. {
  774. // not sliced
  775. slicesW[slicesWcnt++] = raw_width; // safe fallback
  776. }
  777. slices = slicesWcnt * jh.high;
  778. offset = (unsigned*)calloc(slices+1,sizeof(offset[0]));
  779. for(slice=0;slice<slices;slice++)
  780. {
  781. offset[slice] = (t_x + t_y * raw_width)| (t_s<<28);
  782. if(offset[slice] & 0x0fffffff >= raw_width * raw_height)
  783. throw LIBRAW_EXCEPTION_IO_BADFILE;
  784. t_y++;
  785. if(t_y == jh.high)
  786. {
  787. t_y = 0;
  788. t_x += slicesW[t_s++];
  789. }
  790. }
  791. offset[slices] = offset[slices-1];
  792. slice = 1; // next slice
  793. pixno = offset[0];
  794. pixelsInSlice = slicesW[0];
  795. #endif
  796. #ifdef LIBRAW_LIBRARY_BUILD
  797. LibRaw_byte_buffer *buf=NULL;
  798. if(data_size)
  799. buf = ifp->make_byte_buffer(data_size);
  800. LibRaw_bit_buffer bits;
  801. #endif
  802. for (jrow=0; jrow < jh.high; jrow++) {
  803. #ifdef LIBRAW_LIBRARY_BUILD
  804. if (data_size)
  805. rp = ljpeg_row_new (jrow, &jh,bits,buf);
  806. else
  807. #endif
  808. rp = ljpeg_row (jrow, &jh);
  809. if (load_flags & 1)
  810. row = jrow & 1 ? height-1-jrow/2 : jrow/2;
  811. for (jcol=0; jcol < jwide; jcol++) {
  812. val = *rp++;
  813. if (jh.bits <= 12)
  814. val = curve[val & 0xfff];
  815. #ifndef LIBRAW_LIBRARY_BUILD
  816. // slow dcraw way to calculate row/col
  817. if (cr2_slice[0]) {
  818. jidx = jrow*jwide + jcol;
  819. i = jidx / (cr2_slice[1]*jh.high);
  820. if ((j = i >= cr2_slice[0]))
  821. i = cr2_slice[0];
  822. jidx -= i * (cr2_slice[1]*jh.high);
  823. row = jidx / cr2_slice[1+j];
  824. col = jidx % cr2_slice[1+j] + i*cr2_slice[1];
  825. }
  826. #else
  827. // new fast one, but for data_size defined only (i.e. new CR2 format, not 1D/1Ds)
  828. if(data_size)
  829. {
  830. row = pixno/raw_width;
  831. col = pixno % raw_width;
  832. pixno++;
  833. if (0 == --pixelsInSlice)
  834. {
  835. unsigned o = offset[slice++];
  836. pixno = o & 0x0fffffff;
  837. pixelsInSlice = slicesW[o>>28];
  838. }
  839. }
  840. #endif
  841. #ifndef LIBRAW_LIBRARY_BUILD
  842. if (raw_width == 3984 && (col -= 2) < 0)
  843. col += (row--,raw_width);
  844. if ((unsigned) (row-top_margin) < height) {
  845. c = FC(row-top_margin,col-left_margin);
  846. if ((unsigned) (col-left_margin) < width) {
  847. BAYER(row-top_margin,col-left_margin) = val;
  848. if (min > val) min = val;
  849. } else if (col > 1 && (unsigned) (col-left_margin+2) > width+3)
  850. cblack[c] += (cblack[4+c]++,val);
  851. }
  852. #else
  853. if (raw_width == 3984)
  854. {
  855. if ( (col -= 2) < 0)
  856. col += (row--,raw_width);
  857. if(row >= 0 && row < raw_height && col >= 0 && col < raw_width)
  858. RBAYER(row,col) = val;
  859. }
  860. else
  861. RBAYER(row,col) = val;
  862. if ((unsigned) (row-top_margin) < height)
  863. {
  864. // within image height
  865. if ((unsigned) (col-left_margin) < width)
  866. {
  867. // within image area, save min
  868. if(save_min)
  869. if (min > val) min = val;
  870. }
  871. else if (col > 1 && (unsigned) (col-left_margin+2) > width+3)
  872. {
  873. c = FC(row-top_margin,col-left_margin);
  874. cblack[c] += (cblack[4+c]++,val);
  875. }
  876. }
  877. #endif
  878. #ifndef LIBRAW_LIBRARY_BUILD
  879. if (++col >= raw_width)
  880. col = (row++,0);
  881. #else
  882. if(!data_size) // 1D or 1Ds case
  883. if (++col >= raw_width)
  884. col = (row++,0);
  885. #endif
  886. }
  887. }
  888. ljpeg_end (&jh);
  889. FORC4 if (cblack[4+c]) cblack[c] /= cblack[4+c];
  890. if (!strcasecmp(make,"KODAK"))
  891. black = min;
  892. #ifdef LIBRAW_LIBRARY_BUILD
  893. if(buf)
  894. delete buf;
  895. free(offset);
  896. #endif
  897. }
  898. void CLASS canon_sraw_load_raw()
  899. {
  900. struct jhead jh;
  901. short *rp=0, (*ip)[4];
  902. int jwide, slice, scol, ecol, row, col, jrow=0, jcol=0, pix[3], c;
  903. int v[3]={0,0,0}, ver, hue;
  904. char *cp;
  905. if (!ljpeg_start (&jh, 0)) return;
  906. jwide = (jh.wide >>= 1) * jh.clrs;
  907. #ifdef LIBRAW_LIBRARY_BUILD
  908. if(!data_size)
  909. throw LIBRAW_EXCEPTION_IO_BADFILE;
  910. LibRaw_byte_buffer *buf = ifp->make_byte_buffer(data_size);
  911. LibRaw_bit_buffer bits;
  912. #endif
  913. for (ecol=slice=0; slice <= cr2_slice[0]; slice++) {
  914. scol = ecol;
  915. ecol += cr2_slice[1] * 2 / jh.clrs;
  916. if (!cr2_slice[0] || ecol > raw_width-1) ecol = raw_width & -2;
  917. for (row=0; row < height; row += (jh.clrs >> 1) - 1) {
  918. ip = (short (*)[4]) image + row*width;
  919. for (col=scol; col < ecol; col+=2, jcol+=jh.clrs) {
  920. if ((jcol %= jwide) == 0)
  921. #ifdef LIBRAW_LIBRARY_BUILD
  922. rp = (short*) ljpeg_row_new (jrow++, &jh,bits,buf);
  923. #else
  924. rp = (short *) ljpeg_row (jrow++, &jh);
  925. #endif
  926. if (col >= width) continue;
  927. FORC (jh.clrs-2)
  928. ip[col + (c >> 1)*width + (c & 1)][0] = rp[jcol+c];
  929. ip[col][1] = rp[jcol+jh.clrs-2] - 16384;
  930. ip[col][2] = rp[jcol+jh.clrs-1] - 16384;
  931. }
  932. }
  933. }
  934. for (cp=model2; *cp && !isdigit(*cp); cp++);
  935. sscanf (cp, "%d.%d.%d", v, v+1, v+2);
  936. ver = (v[0]*1000 + v[1])*1000 + v[2];
  937. hue = (jh.sraw+1) << 2;
  938. if (unique_id >= 0x80000281 || (unique_id == 0x80000218 && ver > 1000006))
  939. hue = jh.sraw << 1;
  940. ip = (short (*)[4]) image;
  941. rp = ip[0];
  942. for (row=0; row < height; row++, ip+=width) {
  943. if (row & (jh.sraw >> 1))
  944. for (col=0; col < width; col+=2)
  945. for (c=1; c < 3; c++)
  946. if (row == height-1)
  947. ip[col][c] = ip[col-width][c];
  948. else ip[col][c] = (ip[col-width][c] + ip[col+width][c] + 1) >> 1;
  949. for (col=1; col < width; col+=2)
  950. for (c=1; c < 3; c++)
  951. if (col == width-1)
  952. ip[col][c] = ip[col-1][c];
  953. else ip[col][c] = (ip[col-1][c] + ip[col+1][c] + 1) >> 1;
  954. }
  955. for ( ; rp < ip[0]; rp+=4) {
  956. if (unique_id < 0x80000218) {
  957. pix[0] = rp[0] + rp[2] - 512;
  958. pix[2] = rp[0] + rp[1] - 512;
  959. pix[1] = rp[0] + ((-778*rp[1] - (rp[2] << 11)) >> 12) - 512;
  960. } else {
  961. rp[1] = (rp[1] << 2) + hue;
  962. rp[2] = (rp[2] << 2) + hue;
  963. pix[0] = rp[0] + (( 50*rp[1] + 22929*rp[2]) >> 14);
  964. pix[1] = rp[0] + ((-5640*rp[1] - 11751*rp[2]) >> 14);
  965. pix[2] = rp[0] + ((29040*rp[1] - 101*rp[2]) >> 14);
  966. }
  967. FORC3
  968. rp[c] = CLIP(pix[c] * sraw_mul[c] >> 10);
  969. }
  970. #ifdef LIBRAW_LIBRARY_BUILD
  971. delete buf;
  972. #endif
  973. ljpeg_end (&jh);
  974. maximum = 0x3fff;
  975. }
  976. void CLASS adobe_copy_pixel (int row, int col, ushort **rp)
  977. {
  978. unsigned r, c;
  979. #ifndef LIBRAW_LIBRARY_BUILD
  980. r = row -= top_margin;
  981. c = col -= left_margin;
  982. if (is_raw == 2 && shot_select) (*rp)++;
  983. if (filters) {
  984. if (fuji_width) {
  985. r = row + fuji_width - 1 - (col >> 1);
  986. c = row + ((col+1) >> 1);
  987. }
  988. if (r < height && c < width)
  989. BAYER(r,c) = **rp < 0x1000 ? curve[**rp] : **rp;
  990. *rp += is_raw;
  991. } else {
  992. if (r < height && c < width)
  993. FORC(tiff_samples)
  994. image[row*width+col][c] = (*rp)[c] < 0x1000 ? curve[(*rp)[c]]:(*rp)[c];
  995. *rp += tiff_samples;
  996. }
  997. #else
  998. if (is_raw == 2 && shot_select) (*rp)++;
  999. if (filters) {
  1000. if(row < raw_height && col < raw_width)
  1001. RBAYER(row,col) = **rp < 0x1000 ? curve[**rp] : **rp;
  1002. *rp += is_raw;
  1003. } else {
  1004. if (row < raw_height && col < raw_width)
  1005. FORC(tiff_samples)
  1006. color_image[row*raw_width+col][c] = (*rp)[c]<0x1000 ? curve[(*rp)[c]]:(*rp)[c];
  1007. *rp += tiff_samples;
  1008. }
  1009. #endif
  1010. if (is_raw == 2 && shot_select) (*rp)--;
  1011. }
  1012. void CLASS adobe_dng_load_raw_lj()
  1013. {
  1014. unsigned save, trow=0, tcol=0, jwide, jrow, jcol, row, col;
  1015. struct jhead jh;
  1016. ushort *rp;
  1017. while (trow < raw_height) {
  1018. save = ftell(ifp);
  1019. if (tile_length < INT_MAX)
  1020. fseek (ifp, get4(), SEEK_SET);
  1021. if (!ljpeg_start (&jh, 0)) break;
  1022. jwide = jh.wide;
  1023. if (filters) jwide *= jh.clrs;
  1024. jwide /= is_raw;
  1025. #ifdef LIBRAW_LIBRARY_BUILD
  1026. if(!data_size)
  1027. throw LIBRAW_EXCEPTION_IO_BADFILE;
  1028. LibRaw_byte_buffer *buf = ifp->make_byte_buffer(data_size);
  1029. LibRaw_bit_buffer bits;
  1030. #endif
  1031. for (row=col=jrow=0; jrow < jh.high; jrow++) {
  1032. #ifdef LIBRAW_LIBRARY_BUILD
  1033. rp = ljpeg_row_new (jrow, &jh,bits,buf);
  1034. #else
  1035. rp = ljpeg_row (jrow, &jh);
  1036. #endif
  1037. for (jcol=0; jcol < jwide; jcol++) {
  1038. adobe_copy_pixel (trow+row, tcol+col, &rp);
  1039. if (++col >= tile_width || col >= raw_width)
  1040. row += 1 + (col = 0);
  1041. }
  1042. }
  1043. fseek (ifp, save+4, SEEK_SET);
  1044. if ((tcol += tile_width) >= raw_width)
  1045. trow += tile_length + (tcol = 0);
  1046. ljpeg_end (&jh);
  1047. #ifdef LIBRAW_LIBRARY_BUILD
  1048. delete buf;
  1049. #endif
  1050. }
  1051. }
  1052. void CLASS adobe_dng_load_raw_nc()
  1053. {
  1054. ushort *pixel, *rp;
  1055. int row, col;
  1056. pixel = (ushort *) calloc (raw_width * tiff_samples, sizeof *pixel);
  1057. merror (pixel, "adobe_dng_load_raw_nc()");
  1058. #ifdef LIBRAW_LIBRARY_BUILD
  1059. int dsz= raw_height*raw_width * tiff_samples * tiff_bps/8;
  1060. LibRaw_byte_buffer *buf = NULL;
  1061. if (tiff_bps != 16)
  1062. {
  1063. buf = ifp->make_byte_buffer(dsz);
  1064. }
  1065. LibRaw_bit_buffer bits;
  1066. #endif
  1067. for (row=0; row < raw_height; row++) {
  1068. if (tiff_bps == 16)
  1069. read_shorts (pixel, raw_width * tiff_samples);
  1070. else {
  1071. #ifdef LIBRAW_LIBRARY_BUILD
  1072. bits.reset();
  1073. for (col=0; col < raw_width * tiff_samples; col++)
  1074. pixel[col] = bits._getbits(buf,tiff_bps,zero_after_ff);
  1075. #else
  1076. getbits(-1);
  1077. for (col=0; col < raw_width * tiff_samples; col++)
  1078. pixel[col] = getbits(tiff_bps);
  1079. #endif
  1080. }
  1081. for (rp=pixel, col=0; col < raw_width; col++)
  1082. adobe_copy_pixel (row, col, &rp);
  1083. }
  1084. free (pixel);
  1085. #ifdef LIBRAW_LIBRARY_BUILD
  1086. if(buf)
  1087. delete buf;
  1088. #endif
  1089. }
  1090. void CLASS pentax_load_raw()
  1091. {
  1092. ushort bit[2][15], huff[4097];
  1093. int dep, row, col, diff, c, i;
  1094. ushort vpred[2][2] = {{0,0},{0,0}}, hpred[2];
  1095. fseek (ifp, meta_offset, SEEK_SET);
  1096. dep = (get2() + 12) & 15;
  1097. fseek (ifp, 12, SEEK_CUR);
  1098. FORC(dep) bit[0][c] = get2();
  1099. FORC(dep) bit[1][c] = fgetc(ifp);
  1100. FORC(dep)
  1101. for (i=bit[0][c]; i <= ((bit[0][c]+(4096 >> bit[1][c])-1) & 4095); )
  1102. huff[++i] = bit[1][c] << 8 | c;
  1103. huff[0] = 12;
  1104. fseek (ifp, data_offset, SEEK_SET);
  1105. #ifdef LIBRAW_LIBRARY_BUILD
  1106. if(!data_size)
  1107. throw LIBRAW_EXCEPTION_IO_BADFILE;
  1108. LibRaw_byte_buffer *buf = ifp->make_byte_buffer(data_size);
  1109. LibRaw_bit_buffer bits;
  1110. bits.reset();
  1111. #else
  1112. getbits(-1);
  1113. #endif
  1114. for (row=0; row < raw_height; row++)
  1115. {
  1116. for (col=0; col < raw_width; col++) {
  1117. #ifdef LIBRAW_LIBRARY_BUILD
  1118. diff = ljpeg_diff_pef(bits,buf,huff);
  1119. #else
  1120. diff = ljpeg_diff (huff);
  1121. #endif
  1122. if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
  1123. else hpred[col & 1] += diff;
  1124. unsigned val = hpred[col & 1];
  1125. #ifndef LIBRAW_LIBRARY_BUILD
  1126. if ((unsigned) (row-top_margin) < height &&
  1127. (unsigned)(col-left_margin) < width)
  1128. BAYER(row-top_margin,col-left_margin) = val;
  1129. #else
  1130. RBAYER(row,col) = val;
  1131. #endif
  1132. if (val >> tiff_bps) derror();
  1133. }
  1134. }
  1135. #ifdef LIBRAW_LIBRARY_BUILD
  1136. delete buf;
  1137. #endif
  1138. }
  1139. void CLASS nikon_compressed_load_raw()
  1140. {
  1141. static const uchar nikon_tree[][32] = {
  1142. { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy */
  1143. 5,4,3,6,2,7,1,0,8,9,11,10,12 },
  1144. { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy after split */
  1145. 0x39,0x5a,0x38,0x27,0x16,5,4,3,2,1,0,11,12,12 },
  1146. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0, /* 12-bit lossless */
  1147. 5,4,6,3,7,2,8,1,9,0,10,11,12 },
  1148. { 0,1,4,3,1,1,1,1,1,2,0,0,0,0,0,0, /* 14-bit lossy */
  1149. 5,6,4,7,8,3,9,2,1,0,10,11,12,13,14 },
  1150. { 0,1,5,1,1,1,1,1,1,1,2,0,0,0,0,0, /* 14-bit lossy after split */
  1151. 8,0x5c,0x4b,0x3a,0x29,7,6,5,4,3,2,1,0,13,14 },
  1152. { 0,1,4,2,2,3,1,2,0,0,0,0,0,0,0,0, /* 14-bit lossless */
  1153. 7,6,8,5,9,4,10,3,11,12,2,0,1,13,14 } };
  1154. ushort *huff, ver0, ver1, vpred[2][2], hpred[2], csize;
  1155. int i, min, max, step=0, tree=0, split=0, row, col, len, shl, diff;
  1156. fseek (ifp, meta_offset, SEEK_SET);
  1157. ver0 = fgetc(ifp);
  1158. ver1 = fgetc(ifp);
  1159. if (ver0 == 0x49 || ver1 == 0x58)
  1160. fseek (ifp, 2110, SEEK_CUR);
  1161. if (ver0 == 0x46) tree = 2;
  1162. if (tiff_bps == 14) tree += 3;
  1163. read_shorts (vpred[0], 4);
  1164. max = 1 << tiff_bps & 0x7fff;
  1165. if ((csize = get2()) > 1)
  1166. step = max / (csize-1);
  1167. if (ver0 == 0x44 && ver1 == 0x20 && step > 0) {
  1168. for (i=0; i < csize; i++)
  1169. curve[i*step] = get2();
  1170. for (i=0; i < max; i++)
  1171. curve[i] = ( curve[i-i%step]*(step-i%step) +
  1172. curve[i-i%step+step]*(i%step) ) / step;
  1173. #ifdef LIBRAW_LIBRARY_BUILD
  1174. color_flags.curve_state = LIBRAW_COLORSTATE_LOADED;
  1175. #endif
  1176. fseek (ifp, meta_offset+562, SEEK_SET);
  1177. split = get2();
  1178. } else if (ver0 != 0x46 && csize <= 0x4001)
  1179. {
  1180. read_shorts (curve, max=csize);
  1181. #ifdef LIBRAW_LIBRARY_BUILD
  1182. color_flags.curve_state = LIBRAW_COLORSTATE_LOADED;
  1183. #endif
  1184. }
  1185. while (curve[max-2] == curve[max-1]) max--;
  1186. huff = make_decoder (nikon_tree[tree]);
  1187. fseek (ifp, data_offset, SEEK_SET);
  1188. #ifdef LIBRAW_LIBRARY_BUILD
  1189. if(!data_size)
  1190. throw LIBRAW_EXCEPTION_IO_BADFILE;
  1191. LibRaw_byte_buffer *buf = ifp->make_byte_buffer(data_size);
  1192. LibRaw_bit_buffer bits;
  1193. bits.reset();
  1194. #else
  1195. getbits(-1);
  1196. #endif
  1197. for (min=row=0; row < height; row++) {
  1198. if (split && row == split) {
  1199. free (huff);
  1200. huff = make_decoder (nikon_tree[tree+1]);
  1201. max += (min = 16) << 1;
  1202. }
  1203. for (col=0; col < raw_width; col++) {
  1204. #ifdef LIBRAW_LIBRARY_BUILD
  1205. i = bits._gethuff(buf,*huff,huff+1,zero_after_ff);
  1206. #else
  1207. i = gethuff(huff);
  1208. #endif
  1209. len = i & 15;
  1210. shl = i >> 4;
  1211. #ifdef LIBRAW_LIBRARY_BUILD
  1212. diff = ((bits._getbits(buf,len-shl,zero_after_ff) << 1) + 1) << shl >> 1;
  1213. #else
  1214. diff = ((getbits(len-shl) << 1) + 1) << shl >> 1;
  1215. #endif
  1216. if ((diff & (1 << (len-1))) == 0)
  1217. diff -= (1 << len) - !shl;
  1218. if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
  1219. else hpred[col & 1] += diff;
  1220. if ((ushort)(hpred[col & 1] + min) >= max) derror();
  1221. #ifndef LIBRAW_LIBRARY_BUILD
  1222. if ((unsigned) (col-left_margin) < width)
  1223. BAYER(row,col-left_margin) = curve[LIM((short)hpred[col & 1],0,0x3fff)];
  1224. #else
  1225. ushort xval = hpred[col & 1];
  1226. xval = curve[LIM((short)xval,0,0x3fff)];
  1227. RBAYER(row,col) = xval;
  1228. #endif
  1229. }
  1230. }
  1231. #ifdef LIBRAW_LIBRARY_BUILD
  1232. delete buf;
  1233. #endif
  1234. free (huff);
  1235. }
  1236. /*
  1237. Figure out if a NEF file is compressed. These fancy heuristics
  1238. are only needed for the D100, thanks to a bug in some cameras
  1239. that tags all images as "compressed".
  1240. */
  1241. int CLASS nikon_is_compressed()
  1242. {
  1243. uchar test[256];
  1244. int i;
  1245. fseek (ifp, data_offset, SEEK_SET);
  1246. fread (test, 1, 256, ifp);
  1247. for (i=15; i < 256; i+=16)
  1248. if (test[i]) return 1;
  1249. return 0;
  1250. }
  1251. /*
  1252. Returns 1 for a Coolpix 995, 0 for anything else.
  1253. */
  1254. int CLASS nikon_e995()
  1255. {
  1256. int i, histo[256];
  1257. const uchar often[] = { 0x00, 0x55, 0xaa, 0xff };
  1258. memset (histo, 0, sizeof histo);
  1259. fseek (ifp, -2000, SEEK_END);
  1260. for (i=0; i < 2000; i++)
  1261. histo[fgetc(ifp)]++;
  1262. for (i=0; i < 4; i++)
  1263. if (histo[often[i]] < 200)
  1264. return 0;
  1265. return 1;
  1266. }
  1267. /*
  1268. Returns 1 for a Coolpix 2100, 0 for anything else.
  1269. */
  1270. int CLASS nikon_e2100()
  1271. {
  1272. uchar t[12];
  1273. int i;
  1274. fseek (ifp, 0, SEEK_SET);
  1275. for (i=0; i < 1024; i++) {
  1276. fread (t, 1, 12, ifp);
  1277. if (((t[2] & t[4] & t[7] & t[9]) >> 4
  1278. & t[1] & t[6] & t[8] & t[11] & 3) != 3)
  1279. return 0;
  1280. }
  1281. return 1;
  1282. }
  1283. void CLASS nikon_3700()
  1284. {
  1285. int bits, i;
  1286. uchar dp[24];
  1287. static const struct {
  1288. int bits;
  1289. char t_make[12], t_model[15];
  1290. } table[] = {
  1291. { 0x00, "PENTAX", "Optio 33WR" },
  1292. { 0x03, "NIKON", "E3200" },
  1293. { 0x32, "NIKON", "E3700" },
  1294. { 0x33, "OLYMPUS", "C740UZ" } };
  1295. fseek (ifp, 3072, SEEK_SET);
  1296. fread (dp, 1, 24, ifp);
  1297. bits = (dp[8] & 3) << 4 | (dp[20] & 3);
  1298. for (i=0; i < sizeof table / sizeof *table; i++)
  1299. if (bits == table[i].bits) {
  1300. strcpy (make, table[i].t_make );
  1301. strcpy (model, table[i].t_model);
  1302. }
  1303. }
  1304. /*
  1305. Separates a Minolta DiMAGE Z2 from a Nikon E4300.
  1306. */
  1307. int CLASS minolta_z2()
  1308. {
  1309. int i, nz;
  1310. char tail[424];
  1311. fseek (ifp, -sizeof tail, SEEK_END);
  1312. fread (tail, 1, sizeof tail, ifp);
  1313. for (nz=i=0; i < sizeof tail; i++)
  1314. if (tail[i]) nz++;
  1315. return nz > 20;
  1316. }
  1317. /*
  1318. The Fuji Super CCD is just a Bayer grid rotated 45 degrees.
  1319. */
  1320. void CLASS fuji_load_raw()
  1321. {
  1322. #ifndef LIBRAW_LIBRARY_BUILD
  1323. ushort *pixel;
  1324. int wide, row, col, r, c;
  1325. fseek (ifp, (top_margin*raw_width + left_margin) * 2, SEEK_CUR);
  1326. wide = fuji_width << !fuji_layout;
  1327. pixel = (ushort *) calloc (wide, sizeof *pixel);
  1328. merror (pixel, "fuji_load_raw()");
  1329. for (row=0; row < raw_height; row++) {
  1330. read_shorts (pixel, wide);
  1331. fseek (ifp, 2*(raw_width - wide), SEEK_CUR);
  1332. for (col=0; col < wide; col++) {
  1333. if (fuji_layout) {
  1334. r = fuji_width - 1 - col + (row >> 1);
  1335. c = col + ((row+1) >> 1);
  1336. } else {
  1337. r = fuji_width - 1 + row - (col >> 1);
  1338. c = row + ((col+1) >> 1);
  1339. }
  1340. BAYER(r,c) = pixel[col];
  1341. }
  1342. }
  1343. free (pixel);
  1344. #else
  1345. read_shorts(raw_image,raw_width*raw_height);
  1346. #endif
  1347. }
  1348. void CLASS ppm_thumb()
  1349. {
  1350. char *thumb;
  1351. thumb_length = thumb_width*thumb_height*3;
  1352. thumb = (char *) malloc (thumb_length);
  1353. merror (thumb, "ppm_thumb()");
  1354. fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
  1355. fread (thumb, 1, thumb_length, ifp);
  1356. fwrite (thumb, 1, thumb_length, ofp);
  1357. free (thumb);
  1358. }
  1359. void CLASS layer_thumb()
  1360. {
  1361. int i, c;
  1362. char *thumb, map[][4] = { "012","102" };
  1363. colors = thumb_misc >> 5 & 7;
  1364. thumb_length = thumb_width*thumb_height;
  1365. thumb = (char *) calloc (colors, thumb_length);
  1366. merror (thumb, "layer_thumb()");
  1367. fprintf (ofp, "P%d\n%d %d\n255\n",
  1368. 5 + (colors >> 1), thumb_width, thumb_height);
  1369. fread (thumb, thumb_length, colors, ifp);
  1370. for (i=0; i < thumb_length; i++)
  1371. FORCC putc (thumb[i+thumb_length*(map[thumb_misc >> 8][c]-'0')], ofp);
  1372. free (thumb);
  1373. }
  1374. void CLASS rollei_thumb()
  1375. {
  1376. unsigned i;
  1377. ushort *thumb;
  1378. thumb_length = thumb_width * thumb_height;
  1379. thumb = (ushort *) calloc (thumb_length, 2);
  1380. merror (thumb, "rollei_thumb()");
  1381. fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
  1382. read_shorts (thumb, thumb_length);
  1383. for (i=0; i < thumb_length; i++) {
  1384. putc (thumb[i] << 3, ofp);
  1385. putc (thumb[i] >> 5 << 2, ofp);
  1386. putc (thumb[i] >> 11 << 3, ofp);
  1387. }
  1388. free (thumb);
  1389. }
  1390. void CLASS rollei_load_raw()
  1391. {
  1392. uchar pixel[10];
  1393. unsigned iten=0, isix, i, buffer=0, row, col, todo[16];
  1394. isix = raw_width * raw_height * 5 / 8;
  1395. while (fread (pixel, 1, 10, ifp) == 10) {
  1396. for (i=0; i < 10; i+=2) {
  1397. todo[i] = iten++;
  1398. todo[i+1] = pixel[i] << 8 | pixel[i+1];
  1399. buffer = pixel[i] >> 2 | buffer << 6;
  1400. }
  1401. for ( ; i < 16; i+=2) {
  1402. todo[i] = isix++;
  1403. todo[i+1] = buffer >> (14-i)*5;
  1404. }
  1405. for (i=0; i < 16; i+=2) {
  1406. #ifndef LIBRAW_LIBRARY_BUILD
  1407. row = todo[i] / raw_width - top_margin;
  1408. col = todo[i] % raw_width - left_margin;
  1409. if (row < height && col < width)
  1410. BAYER(row,col) = (todo[i+1] & 0x3ff);
  1411. #else
  1412. RBAYER(todo[i] / raw_width,todo[i] % raw_width) = (todo[i+1] & 0x3ff);
  1413. #endif
  1414. }
  1415. }
  1416. maximum = 0x3ff;
  1417. }
  1418. int CLASS bayer (unsigned row, unsigned col)
  1419. {
  1420. return (row < height && col < width) ? BAYER(row,col) : 0;
  1421. }
  1422. void CLASS phase_one_flat_field (int is_float, int nc)
  1423. {
  1424. ushort head[8];
  1425. unsigned wide, y, x, c, rend, cend, row, col;
  1426. float *mrow, num, mult[4];
  1427. read_shorts (head, 8);
  1428. wide = head[2] / head[4];
  1429. mrow = (float *) calloc (nc*wide, sizeof *mrow);
  1430. merror (mrow, "phase_one_flat_field()");
  1431. for (y=0; y < head[3] / head[5]; y++) {
  1432. for (x=0; x < wide; x++)
  1433. for (c=0; c < nc; c+=2) {
  1434. num = is_float ? getreal(11) : get2()/32768.0;
  1435. if (y==0) mrow[c*wide+x] = num;
  1436. else mrow[(c+1)*wide+x] = (num - mrow[c*wide+x]) / head[5];
  1437. }
  1438. if (y==0) continue;
  1439. rend = head[1]-top_margin + y*head[5];
  1440. for (row = rend-head[5]; row < height && row < rend; row++) {
  1441. for (x=1; x < wide; x++) {
  1442. for (c=0; c < nc; c+=2) {
  1443. mult[c] = mrow[c*wide+x-1];
  1444. mult[c+1] = (mrow[c*wide+x] - mult[c]) / head[4];
  1445. }
  1446. cend = head[0]-left_margin + x*head[4];
  1447. for (col = cend-head[4]; col < width && col < cend; col++) {
  1448. c = nc > 2 ? FC(row,col) : 0;
  1449. if (!(c & 1)) {
  1450. c = BAYER(row,col) * mult[c];
  1451. BAYER(row,col) = LIM(c,0,65535);
  1452. }
  1453. for (c=0; c < nc; c+=2)
  1454. mult[c] += mult[c+1];
  1455. }
  1456. }
  1457. for (x=0; x < wide; x++)
  1458. for (c=0; c < nc; c+=2)
  1459. mrow[c*wide+x] += mrow[(c+1)*wide+x];
  1460. }
  1461. }
  1462. free (mrow);
  1463. }
  1464. void CLASS phase_one_correct()
  1465. {
  1466. unsigned entries, tag, data, save, col, row, type;
  1467. int len, i, j, k, cip, val[4], dev[4], sum, max;
  1468. int head[9], diff, mindiff=INT_MAX, off_412=0;
  1469. static const signed char dir[12][2] =
  1470. { {-1,-1}, {-1,1}, {1,-1}, {1,1}, {-2,0}, {0,-2}, {0,2}, {2,0},
  1471. {-2,-2}, {-2,2}, {2,-2}, {2,2} };
  1472. float poly[8], num, cfrac, frac, mult[2], *yval[2];
  1473. ushort *xval[2];
  1474. if (half_size || !meta_length) return;
  1475. #ifdef DCRAW_VERBOSE
  1476. if (verbose) fprintf (stderr,_("Phase One correction...\n"));
  1477. #endif
  1478. fseek (ifp, meta_offset, SEEK_SET);
  1479. order = get2();
  1480. fseek (ifp, 6, SEEK_CUR);
  1481. fseek (ifp, meta_offset+get4(), SEEK_SET);
  1482. entries = get4(); get4();
  1483. while (entries--) {
  1484. tag = get4();
  1485. len = get4();
  1486. data = get4();
  1487. save = ftell(ifp);
  1488. fseek (ifp, meta_offset+data, SEEK_SET);
  1489. if (tag == 0x419) { /* Polynomial curve */
  1490. for (get4(), i=0; i < 8; i++)
  1491. poly[i] = getreal(11);
  1492. poly[3] += (ph1.tag_210 - poly[7]) * poly[6] + 1;
  1493. for (i=0; i < 0x10000; i++) {
  1494. num = (poly[5]*i + poly[3])*i + poly[1];
  1495. curve[i] = LIM(num,0,65535);
  1496. } goto apply; /* apply to right half */
  1497. } else if (tag == 0x41a) { /* Polynomial curve */
  1498. for (i=0; i < 4; i++)
  1499. poly[i] = getreal(11);
  1500. for (i=0; i < 0x10000; i++) {
  1501. for (num=0, j=4; j--; )
  1502. num = num * i + poly[j];
  1503. curve[i] = LIM(num+i,0,65535);
  1504. } apply: /* apply to whole image */
  1505. for (row=0; row < height; row++)
  1506. for (col = (tag & 1)*ph1.split_col; col < width; col++)
  1507. BAYER(row,col) = curve[BAYER(row,col)];
  1508. } else if (tag == 0x400) { /* Sensor defects */
  1509. while ((len -= 8) >= 0) {
  1510. col = get2() - left_margin;
  1511. row = get2() - top_margin;
  1512. type = get2(); get2();
  1513. if (col >= width) continue;
  1514. if (type == 131) /* Bad column */
  1515. for (row=0; row < height; row++)
  1516. if (FC(row,col) == 1) {
  1517. for (sum=i=0; i < 4; i++)
  1518. sum += val[i] = bayer (row+dir[i][0], col+dir[i][1]);
  1519. for (max=i=0; i < 4; i++) {
  1520. dev[i] = abs((val[i] << 2) - sum);
  1521. if (dev[max] < dev[i]) max = i;
  1522. }
  1523. BAYER(row,col) = (sum - val[max])/3.0 + 0.5;
  1524. } else {
  1525. for (sum=0, i=8; i < 12; i++)
  1526. sum += bayer (row+dir[i][0], col+dir[i][1]);
  1527. BAYER(row,col) = 0.5 + sum * 0.0732233 +
  1528. (bayer(row,col-2) + bayer(row,col+2)) * 0.3535534;
  1529. }
  1530. else if (type == 129) { /* Bad pixel */
  1531. if (row >= height) continue;
  1532. j = (FC(row,col) != 1) * 4;
  1533. for (sum=0, i=j; i < j+8; i++)
  1534. sum += bayer (row+dir[i][0], col+dir[i][1]);
  1535. BAYER(row,col) = (sum + 4) >> 3;
  1536. }
  1537. }
  1538. } else if (tag == 0x401) { /* All-color flat fields */
  1539. phase_one_flat_field (1, 2);
  1540. } else if (tag == 0x416 || tag == 0x410) {
  1541. phase_one_flat_field (0, 2);
  1542. } else if (tag == 0x40b) { /* Red+blue flat field */
  1543. phase_one_flat_field (0, 4);
  1544. } else if (tag == 0x412) {
  1545. fseek (ifp, 36, SEEK_CUR);
  1546. diff = abs (get2() - ph1.tag_21a);
  1547. if (mindiff > diff) {
  1548. mindiff = diff;
  1549. off_412 = ftell(ifp) - 38;
  1550. }
  1551. }
  1552. fseek (ifp, save, SEEK_SET);
  1553. }
  1554. if (off_412) {
  1555. fseek (ifp, off_412, SEEK_SET);
  1556. for (i=0; i < 9; i++) head[i] = get4() & 0x7fff;
  1557. yval[0] = (float *) calloc (head[1]*head[3] + head[2]*head[4], 6);
  1558. merror (yval[0], "phase_one_correct()");
  1559. yval[1] = (float *) (yval[0] + head[1]*head[3]);
  1560. xval[0] = (ushort *) (yval[1] + head[2]*head[4]);
  1561. xval[1] = (ushort *) (xval[0] + head[1]*head[3]);
  1562. get2();
  1563. for (i=0; i < 2; i++)
  1564. for (j=0; j < head[i+1]*head[i+3]; j++)
  1565. yval[i][j] = getreal(11);
  1566. for (i=0; i < 2; i++)
  1567. for (j=0; j < head[i+1]*head[i+3]; j++)
  1568. xval[i][j] = get2();
  1569. for (row=0; row < height; row++)
  1570. for (col=0; col < width; col++) {
  1571. cfrac = (float) col * head[3] / raw_width;
  1572. cfrac -= cip = cfrac;
  1573. num = BAYER(row,col) * 0.5;
  1574. for (i=cip; i < cip+2; i++) {
  1575. for (k=j=0; j < head[1]; j++)
  1576. if (num < xval[0][k = head[1]*i+j]) break;
  1577. frac = (j == 0 || j == head[1]) ? 0 :
  1578. (xval[0][k] - num) / (xval[0][k] - xval[0][k-1]);
  1579. mult[i-cip] = yval[0][k-1] * frac + yval[0][k] * (1-frac);
  1580. }
  1581. i = ((mult[0] * (1-cfrac) + mult[1] * cfrac)
  1582. * (row + top_margin) + num) * 2;
  1583. BAYER(row,col) = LIM(i,0,65535);
  1584. }
  1585. free (yval[0]);
  1586. }
  1587. }
  1588. void CLASS phase_one_load_raw()
  1589. {
  1590. int row, col, a, b;
  1591. ushort *pixel, a

Large files files are truncated, but you can click here to view the full file