PageRenderTime 70ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 2ms

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

https://bitbucket.org/kensington/opengtl
C++ | 8903 lines | 8445 code | 316 blank | 142 comment | 2748 complexity | e7cf60208f7cd0c722ad5af67d3a9c10 MD5 | raw file
Possible License(s): LGPL-2.0

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

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