PageRenderTime 64ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 2ms

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

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

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