PageRenderTime 75ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 1ms

/lib/cximage-6.0/raw/dcraw.c

http://github.com/xbmc/xbmc
C | 8643 lines | 8049 code | 383 blank | 211 comment | 2768 complexity | f88f30a89921a61886cc38ebb87aa935 MD5 | raw file
Possible License(s): GPL-3.0, CC-BY-SA-3.0, LGPL-2.0, 0BSD, Unlicense, GPL-2.0, AGPL-1.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0

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

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

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