/src/FreeImage/Source/LibRawLite/dcraw/dcraw.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 9232 lines · 8646 code · 380 blank · 206 comment · 2970 complexity · ffa87ec7f6c5592582e974c5b4acb627 MD5 · raw file

Large files are truncated click here to view the full file

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