PageRenderTime 71ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 2ms

/Source/LibRawLite/internal/dcraw_common.cpp

https://bitbucket.org/tiran/freeimage
C++ | 9385 lines | 8885 code | 356 blank | 144 comment | 2909 complexity | 62d53896f8a38f0d2f613bfbf11e9212 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, GPL-2.0, GPL-3.0, LGPL-3.0, BSD-2-Clause, LGPL-2.1, BSD-3-Clause

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

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

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