PageRenderTime 36ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 3ms

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

https://bitbucket.org/cabalistic/ogredeps/
C++ | 9257 lines | 8724 code | 384 blank | 149 comment | 2770 complexity | 72935f0889776ffd03608fe38e768ba2 MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause, CPL-1.0, Unlicense, GPL-2.0, GPL-3.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, BSD-2-Clause, LGPL-2.1

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

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