PageRenderTime 75ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 3ms

/internal/dcraw_common.cpp

https://github.com/jabbervorx/LibRaw
C++ | 9324 lines | 8823 code | 357 blank | 144 comment | 2893 complexity | c4d4d41542e4407f7663f13c8f9812c0 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1

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

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