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