/src/FreeImage/Source/LibRawLite/internal/dcraw_common.cpp
C++ | 9257 lines | 8724 code | 384 blank | 149 comment | 2770 complexity | 72935f0889776ffd03608fe38e768ba2 MD5 | raw file
Possible License(s): LGPL-2.0, Unlicense, MPL-2.0-no-copyleft-exception, BSD-2-Clause, LGPL-3.0, LGPL-2.1, BSD-3-Clause, GPL-2.0
Large files files are truncated, but you can click here to view the full file
- /*
- Copyright 2008-2010 LibRaw LLC (info@libraw.org)
- LibRaw is free software; you can redistribute it and/or modify
- it under the terms of the one of three licenses as you choose:
- 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
- (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
- 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
- (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
- 3. LibRaw Software License 27032010
- (See file LICENSE.LibRaw.pdf provided in LibRaw distribution archive for details).
- This file is generated from Dave Coffin's dcraw.c
- dcraw.c -- Dave Coffin's raw photo decoder
- Copyright 1997-2010 by Dave Coffin, dcoffin a cybercom o net
- Look into dcraw homepage (probably http://cybercom.net/~dcoffin/dcraw/)
- for more information
- */
- #include <math.h>
- #define CLASS LibRaw::
- #include "libraw/libraw_types.h"
- #define LIBRAW_LIBRARY_BUILD
- #define LIBRAW_IO_REDEFINED
- #include "libraw/libraw.h"
- #include "internal/defines.h"
- #include "internal/var_defines.h"
- #include "internal/libraw_bytebuffer.h"
- #ifdef ANDROID
- #include <asm/byteorder.h>
- void swab(const void *from, void*to, ssize_t n)
- {
- if (n < 0)
- return;
-
- for (ssize_t i = 0; i < (n/2)*2; i += 2)
- {
- *((uint16_t*)to+i) = __arch__swab16(*((uint16_t*)from+i));
- }
- }
- #endif
- #ifndef __GLIBC__
- char *my_memmem (char *haystack, size_t haystacklen,
- char *needle, size_t needlelen)
- {
- char *c;
- for (c = haystack; c <= haystack + haystacklen - needlelen; c++)
- if (!memcmp (c, needle, needlelen))
- return c;
- return 0;
- }
- #define memmem my_memmem
- #endif
- ushort CLASS sget2 (uchar *s)
- {
- if (order == 0x4949) /* "II" means little-endian */
- return s[0] | s[1] << 8;
- else /* "MM" means big-endian */
- return s[0] << 8 | s[1];
- }
- ushort CLASS get2()
- {
- uchar str[2] = { 0xff,0xff };
- fread (str, 1, 2, ifp);
- return sget2(str);
- }
- unsigned CLASS sget4 (uchar *s)
- {
- if (order == 0x4949)
- return s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
- else
- return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3];
- }
- #define sget4(s) sget4((uchar *)s)
- unsigned CLASS get4()
- {
- uchar str[4] = { 0xff,0xff,0xff,0xff };
- fread (str, 1, 4, ifp);
- return sget4(str);
- }
- unsigned CLASS getint (int type)
- {
- return type == 3 ? get2() : get4();
- }
- float CLASS int_to_float (int i)
- {
- union { int i; float f; } u;
- u.i = i;
- return u.f;
- }
- double CLASS getreal (int type)
- {
- union { char c[8]; double d; } u;
- int i, rev;
- switch (type) {
- case 3: return (unsigned short) get2();
- case 4: return (unsigned int) get4();
- case 5: u.d = (unsigned int) get4();
- return u.d / (unsigned int) get4();
- case 8: return (signed short) get2();
- case 9: return (signed int) get4();
- case 10: u.d = (signed int) get4();
- return u.d / (signed int) get4();
- case 11: return int_to_float (get4());
- case 12:
- rev = 7 * ((order == 0x4949) == (ntohs(0x1234) == 0x1234));
- for (i=0; i < 8; i++)
- u.c[i ^ rev] = fgetc(ifp);
- return u.d;
- default: return fgetc(ifp);
- }
- }
- void CLASS read_shorts (ushort *pixel, int count)
- {
- if (fread (pixel, 2, count, ifp) < count) derror();
- if ((order == 0x4949) == (ntohs(0x1234) == 0x1234))
- swab ((char*)pixel, (char*)pixel, count*2);
- }
- #ifdef LIBRAW_LIBRARY_BUILD
- #define RBAYER(x,y) raw_image[(x)*raw_width+(y)]
- #define RRBAYER(x,y) raw_image[((x)+top_margin)*raw_width+(y)+left_margin]
- #define CBAYER(x,y) color_image[((x)+top_margin)*raw_width+(y)+left_margin][FC((x),(y))]
- #else
- #define CBAYER(x,y) BAYER((x),(y))
- #define RBAYER(x,y) BAYER((x),(y))
- #define RRBAYER(x,y) BAYER((x),(y))
- #endif
- void CLASS canon_600_fixed_wb (int temp)
- {
- static const short mul[4][5] = {
- { 667, 358,397,565,452 },
- { 731, 390,367,499,517 },
- { 1119, 396,348,448,537 },
- { 1399, 485,431,508,688 } };
- int lo, hi, i;
- float frac=0;
- for (lo=4; --lo; )
- if (*mul[lo] <= temp) break;
- for (hi=0; hi < 3; hi++)
- if (*mul[hi] >= temp) break;
- if (lo != hi)
- frac = (float) (temp - *mul[lo]) / (*mul[hi] - *mul[lo]);
- for (i=1; i < 5; i++)
- pre_mul[i-1] = 1 / (frac * mul[hi][i] + (1-frac) * mul[lo][i]);
- #ifdef LIBRAW_LIBRARY_BUILD
- color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
- #endif
- }
- /* Return values: 0 = white 1 = near white 2 = not white */
- int CLASS canon_600_color (int ratio[2], int mar)
- {
- int clipped=0, target, miss;
- if (flash_used) {
- if (ratio[1] < -104)
- { ratio[1] = -104; clipped = 1; }
- if (ratio[1] > 12)
- { ratio[1] = 12; clipped = 1; }
- } else {
- if (ratio[1] < -264 || ratio[1] > 461) return 2;
- if (ratio[1] < -50)
- { ratio[1] = -50; clipped = 1; }
- if (ratio[1] > 307)
- { ratio[1] = 307; clipped = 1; }
- }
- target = flash_used || ratio[1] < 197
- ? -38 - (398 * ratio[1] >> 10)
- : -123 + (48 * ratio[1] >> 10);
- if (target - mar <= ratio[0] &&
- target + 20 >= ratio[0] && !clipped) return 0;
- miss = target - ratio[0];
- if (abs(miss) >= mar*4) return 2;
- if (miss < -20) miss = -20;
- if (miss > mar) miss = mar;
- ratio[0] = target - miss;
- return 1;
- }
- void CLASS canon_600_auto_wb()
- {
- int mar, row, col, i, j, st, count[] = { 0,0 };
- int test[8], total[2][8], ratio[2][2], stat[2];
- memset (&total, 0, sizeof total);
- i = canon_ev + 0.5;
- if (i < 10) mar = 150;
- else if (i > 12) mar = 20;
- else mar = 280 - 20 * i;
- if (flash_used) mar = 80;
- for (row=14; row < height-14; row+=4)
- for (col=10; col < width; col+=2) {
- for (i=0; i < 8; i++)
- test[(i & 4) + FC(row+(i >> 1),col+(i & 1))] =
- BAYER(row+(i >> 1),col+(i & 1));
- for (i=0; i < 8; i++)
- if (test[i] < 150 || test[i] > 1500) goto next;
- for (i=0; i < 4; i++)
- if (abs(test[i] - test[i+4]) > 50) goto next;
- for (i=0; i < 2; i++) {
- for (j=0; j < 4; j+=2)
- ratio[i][j >> 1] = ((test[i*4+j+1]-test[i*4+j]) << 10) / test[i*4+j];
- stat[i] = canon_600_color (ratio[i], mar);
- }
- if ((st = stat[0] | stat[1]) > 1) goto next;
- for (i=0; i < 2; i++)
- if (stat[i])
- for (j=0; j < 2; j++)
- test[i*4+j*2+1] = test[i*4+j*2] * (0x400 + ratio[i][j]) >> 10;
- for (i=0; i < 8; i++)
- total[st][i] += test[i];
- count[st]++;
- next: ;
- }
- if (count[0] | count[1]) {
- st = count[0]*200 < count[1];
- for (i=0; i < 4; i++)
- pre_mul[i] = 1.0 / (total[st][i] + total[st][i+4]);
- #ifdef LIBRAW_LIBRARY_BUILD
- color_flags.pre_mul_state = LIBRAW_COLORSTATE_CALCULATED;
- #endif
- }
- }
- void CLASS canon_600_coeff()
- {
- static const short table[6][12] = {
- { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
- { -1203,1715,-1136,1648, 1388,-876,267,245, -1641,2153,3921,-3409 },
- { -615,1127,-1563,2075, 1437,-925,509,3, -756,1268,2519,-2007 },
- { -190,702,-1886,2398, 2153,-1641,763,-251, -452,964,3040,-2528 },
- { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
- { -807,1319,-1785,2297, 1388,-876,769,-257, -230,742,2067,-1555 } };
- int t=0, i, c;
- float mc, yc;
- mc = pre_mul[1] / pre_mul[2];
- yc = pre_mul[3] / pre_mul[2];
- if (mc > 1 && mc <= 1.28 && yc < 0.8789) t=1;
- if (mc > 1.28 && mc <= 2) {
- if (yc < 0.8789) t=3;
- else if (yc <= 2) t=4;
- }
- if (flash_used) t=5;
- for (raw_color = i=0; i < 3; i++)
- FORCC rgb_cam[i][c] = table[t][i*4 + c] / 1024.0;
- #ifdef LIBRAW_LIBRARY_BUILD
- color_flags.rgb_cam_state = LIBRAW_COLORSTATE_CALCULATED;
- #endif
- }
- void CLASS canon_600_load_raw()
- {
- uchar data[1120], *dp;
- ushort pixel[896], *pix;
- int irow, row, col, val;
- static const short mul[4][2] =
- { { 1141,1145 }, { 1128,1109 }, { 1178,1149 }, { 1128,1109 } };
- for (irow=row=0; irow < height; irow++) {
- if (fread (data, 1, raw_width*5/4, ifp) < raw_width*5/4) derror();
- for (dp=data, pix=pixel; dp < data+1120; dp+=10, pix+=8) {
- pix[0] = (dp[0] << 2) + (dp[1] >> 6 );
- pix[1] = (dp[2] << 2) + (dp[1] >> 4 & 3);
- pix[2] = (dp[3] << 2) + (dp[1] >> 2 & 3);
- pix[3] = (dp[4] << 2) + (dp[1] & 3);
- pix[4] = (dp[5] << 2) + (dp[9] & 3);
- pix[5] = (dp[6] << 2) + (dp[9] >> 2 & 3);
- pix[6] = (dp[7] << 2) + (dp[9] >> 4 & 3);
- pix[7] = (dp[8] << 2) + (dp[9] >> 6 );
- }
- #ifndef LIBRAW_LIBRARY_BUILD
- for (col=0; col < width; col++)
- {
- BAYER(row,col) = pixel[col];
- }
- #else
- for (col=0; col < raw_width; col++)
- RBAYER(row,col) = pixel[col];
- #endif
- for (col=width; col < raw_width; col++)
- {
- black += pixel[col];
- }
- if ((row+=2) > height) row = 1;
- }
- if (raw_width > width)
- black = black / ((raw_width - width) * height) - 4;
- #ifndef LIBRAW_LIBRARY_BUILD
- for (row=0; row < height; row++)
- for (col=0; col < width; col++) {
- if ((val = BAYER(row,col) - black) < 0) val = 0;
- val = val * mul[row & 3][col & 1] >> 9;
- BAYER(row,col) = val;
- }
- #else
- for (row=0; row < height; row++)
- for (col=0; col < raw_width; col++) {
- if ((val = RBAYER(row,col) - black) < 0) val = 0;
- val = val * mul[row & 3][col & 1] >> 9;
- RBAYER(row,col) = val;
- }
- #endif
- canon_600_fixed_wb(1311);
- canon_600_auto_wb();
- canon_600_coeff();
- maximum = (0x3ff - black) * 1109 >> 9;
- black = 0;
- }
- void CLASS remove_zeroes()
- {
- unsigned row, col, tot, n, r, c;
- #ifdef LIBRAW_LIBRARY_BUILD
- RUN_CALLBACK(LIBRAW_PROGRESS_REMOVE_ZEROES,0,2);
- #endif
- for (row=0; row < height; row++)
- for (col=0; col < width; col++)
- if (BAYER(row,col) == 0) {
- tot = n = 0;
- for (r = row-2; r <= row+2; r++)
- for (c = col-2; c <= col+2; c++)
- if (r < height && c < width &&
- FC(r,c) == FC(row,col) && BAYER(r,c))
- tot += (n++,BAYER(r,c));
- if (n) BAYER(row,col) = tot/n;
- }
- #ifdef LIBRAW_LIBRARY_BUILD
- RUN_CALLBACK(LIBRAW_PROGRESS_REMOVE_ZEROES,1,2);
- #endif
- }
- int CLASS canon_s2is()
- {
- unsigned row;
- for (row=0; row < 100; row++) {
- fseek (ifp, row*3340 + 3284, SEEK_SET);
- if (getc(ifp) > 15) return 1;
- }
- return 0;
- }
- /*
- getbits(-1) initializes the buffer
- getbits(n) where 0 <= n <= 25 returns an n-bit integer
- */
- unsigned CLASS getbithuff (int nbits, ushort *huff)
- {
- #ifdef LIBRAW_NOTHREADS
- static unsigned bitbuf=0;
- static int vbits=0, reset=0;
- #else
- #define bitbuf tls->getbits.bitbuf
- #define vbits tls->getbits.vbits
- #define reset tls->getbits.reset
- #endif
- unsigned c;
- if (nbits == -1)
- return bitbuf = vbits = reset = 0;
- if (nbits == 0 || vbits < 0) return 0;
- while (!reset && vbits < nbits && (c = fgetc(ifp)) != EOF &&
- !(reset = zero_after_ff && c == 0xff && fgetc(ifp))) {
- bitbuf = (bitbuf << 8) + (uchar) c;
- vbits += 8;
- }
- c = bitbuf << (32-vbits) >> (32-nbits);
- if (huff) {
- vbits -= huff[c] >> 8;
- c = (uchar) huff[c];
- } else
- vbits -= nbits;
- if (vbits < 0) derror();
- return c;
- #ifndef LIBRAW_NOTHREADS
- #undef bitbuf
- #undef vbits
- #undef reset
- #endif
- }
- #define getbits(n) getbithuff(n,0)
- #define gethuff(h) getbithuff(*h,h+1)
- /*
- Construct a decode tree according to the specification in *source.
- The first 16 bytes specify how many codes should be 1-bit, 2-bit
- 3-bit, etc. Bytes after that are the leaf values.
- For example, if the source is
- { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
- 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
- then the code is
- 00 0x04
- 010 0x03
- 011 0x05
- 100 0x06
- 101 0x02
- 1100 0x07
- 1101 0x01
- 11100 0x08
- 11101 0x09
- 11110 0x00
- 111110 0x0a
- 1111110 0x0b
- 1111111 0xff
- */
- ushort * CLASS make_decoder_ref (const uchar **source)
- {
- int max, len, h, i, j;
- const uchar *count;
- ushort *huff;
- count = (*source += 16) - 17;
- for (max=16; max && !count[max]; max--);
- huff = (ushort *) calloc (1 + (1 << max), sizeof *huff);
- merror (huff, "make_decoder()");
- huff[0] = max;
- for (h=len=1; len <= max; len++)
- for (i=0; i < count[len]; i++, ++*source)
- for (j=0; j < 1 << (max-len); j++)
- if (h <= 1 << max)
- huff[h++] = len << 8 | **source;
- return huff;
- }
- ushort * CLASS make_decoder (const uchar *source)
- {
- return make_decoder_ref (&source);
- }
- void CLASS crw_init_tables (unsigned table, ushort *huff[2])
- {
- static const uchar first_tree[3][29] = {
- { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
- 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
- { 0,2,2,3,1,1,1,1,2,0,0,0,0,0,0,0,
- 0x03,0x02,0x04,0x01,0x05,0x00,0x06,0x07,0x09,0x08,0x0a,0x0b,0xff },
- { 0,0,6,3,1,1,2,0,0,0,0,0,0,0,0,0,
- 0x06,0x05,0x07,0x04,0x08,0x03,0x09,0x02,0x00,0x0a,0x01,0x0b,0xff },
- };
- static const uchar second_tree[3][180] = {
- { 0,2,2,2,1,4,2,1,2,5,1,1,0,0,0,139,
- 0x03,0x04,0x02,0x05,0x01,0x06,0x07,0x08,
- 0x12,0x13,0x11,0x14,0x09,0x15,0x22,0x00,0x21,0x16,0x0a,0xf0,
- 0x23,0x17,0x24,0x31,0x32,0x18,0x19,0x33,0x25,0x41,0x34,0x42,
- 0x35,0x51,0x36,0x37,0x38,0x29,0x79,0x26,0x1a,0x39,0x56,0x57,
- 0x28,0x27,0x52,0x55,0x58,0x43,0x76,0x59,0x77,0x54,0x61,0xf9,
- 0x71,0x78,0x75,0x96,0x97,0x49,0xb7,0x53,0xd7,0x74,0xb6,0x98,
- 0x47,0x48,0x95,0x69,0x99,0x91,0xfa,0xb8,0x68,0xb5,0xb9,0xd6,
- 0xf7,0xd8,0x67,0x46,0x45,0x94,0x89,0xf8,0x81,0xd5,0xf6,0xb4,
- 0x88,0xb1,0x2a,0x44,0x72,0xd9,0x87,0x66,0xd4,0xf5,0x3a,0xa7,
- 0x73,0xa9,0xa8,0x86,0x62,0xc7,0x65,0xc8,0xc9,0xa1,0xf4,0xd1,
- 0xe9,0x5a,0x92,0x85,0xa6,0xe7,0x93,0xe8,0xc1,0xc6,0x7a,0x64,
- 0xe1,0x4a,0x6a,0xe6,0xb3,0xf1,0xd3,0xa5,0x8a,0xb2,0x9a,0xba,
- 0x84,0xa4,0x63,0xe5,0xc5,0xf3,0xd2,0xc4,0x82,0xaa,0xda,0xe4,
- 0xf2,0xca,0x83,0xa3,0xa2,0xc3,0xea,0xc2,0xe2,0xe3,0xff,0xff },
- { 0,2,2,1,4,1,4,1,3,3,1,0,0,0,0,140,
- 0x02,0x03,0x01,0x04,0x05,0x12,0x11,0x06,
- 0x13,0x07,0x08,0x14,0x22,0x09,0x21,0x00,0x23,0x15,0x31,0x32,
- 0x0a,0x16,0xf0,0x24,0x33,0x41,0x42,0x19,0x17,0x25,0x18,0x51,
- 0x34,0x43,0x52,0x29,0x35,0x61,0x39,0x71,0x62,0x36,0x53,0x26,
- 0x38,0x1a,0x37,0x81,0x27,0x91,0x79,0x55,0x45,0x28,0x72,0x59,
- 0xa1,0xb1,0x44,0x69,0x54,0x58,0xd1,0xfa,0x57,0xe1,0xf1,0xb9,
- 0x49,0x47,0x63,0x6a,0xf9,0x56,0x46,0xa8,0x2a,0x4a,0x78,0x99,
- 0x3a,0x75,0x74,0x86,0x65,0xc1,0x76,0xb6,0x96,0xd6,0x89,0x85,
- 0xc9,0xf5,0x95,0xb4,0xc7,0xf7,0x8a,0x97,0xb8,0x73,0xb7,0xd8,
- 0xd9,0x87,0xa7,0x7a,0x48,0x82,0x84,0xea,0xf4,0xa6,0xc5,0x5a,
- 0x94,0xa4,0xc6,0x92,0xc3,0x68,0xb5,0xc8,0xe4,0xe5,0xe6,0xe9,
- 0xa2,0xa3,0xe3,0xc2,0x66,0x67,0x93,0xaa,0xd4,0xd5,0xe7,0xf8,
- 0x88,0x9a,0xd7,0x77,0xc4,0x64,0xe2,0x98,0xa5,0xca,0xda,0xe8,
- 0xf3,0xf6,0xa9,0xb2,0xb3,0xf2,0xd2,0x83,0xba,0xd3,0xff,0xff },
- { 0,0,6,2,1,3,3,2,5,1,2,2,8,10,0,117,
- 0x04,0x05,0x03,0x06,0x02,0x07,0x01,0x08,
- 0x09,0x12,0x13,0x14,0x11,0x15,0x0a,0x16,0x17,0xf0,0x00,0x22,
- 0x21,0x18,0x23,0x19,0x24,0x32,0x31,0x25,0x33,0x38,0x37,0x34,
- 0x35,0x36,0x39,0x79,0x57,0x58,0x59,0x28,0x56,0x78,0x27,0x41,
- 0x29,0x77,0x26,0x42,0x76,0x99,0x1a,0x55,0x98,0x97,0xf9,0x48,
- 0x54,0x96,0x89,0x47,0xb7,0x49,0xfa,0x75,0x68,0xb6,0x67,0x69,
- 0xb9,0xb8,0xd8,0x52,0xd7,0x88,0xb5,0x74,0x51,0x46,0xd9,0xf8,
- 0x3a,0xd6,0x87,0x45,0x7a,0x95,0xd5,0xf6,0x86,0xb4,0xa9,0x94,
- 0x53,0x2a,0xa8,0x43,0xf5,0xf7,0xd4,0x66,0xa7,0x5a,0x44,0x8a,
- 0xc9,0xe8,0xc8,0xe7,0x9a,0x6a,0x73,0x4a,0x61,0xc7,0xf4,0xc6,
- 0x65,0xe9,0x72,0xe6,0x71,0x91,0x93,0xa6,0xda,0x92,0x85,0x62,
- 0xf3,0xc5,0xb2,0xa4,0x84,0xba,0x64,0xa5,0xb3,0xd2,0x81,0xe5,
- 0xd3,0xaa,0xc4,0xca,0xf2,0xb1,0xe4,0xd1,0x83,0x63,0xea,0xc3,
- 0xe2,0x82,0xf1,0xa3,0xc2,0xa1,0xc1,0xe3,0xa2,0xe1,0xff,0xff }
- };
- if (table > 2) table = 2;
- huff[0] = make_decoder ( first_tree[table]);
- huff[1] = make_decoder (second_tree[table]);
- }
- /*
- Return 0 if the image starts with compressed data,
- 1 if it starts with uncompressed low-order bits.
- In Canon compressed data, 0xff is always followed by 0x00.
- */
- int CLASS canon_has_lowbits()
- {
- uchar test[0x4000];
- int ret=1;
- unsigned i;
- fseek (ifp, 0, SEEK_SET);
- fread (test, 1, sizeof test, ifp);
- for (i=540; i < sizeof test - 1; i++)
- if (test[i] == 0xff) {
- if (test[i+1]) return 1;
- ret=0;
- }
- return ret;
- }
- void CLASS canon_compressed_load_raw()
- {
- ushort *pixel, *prow, *huff[2];
- int nblocks, lowbits, i, c, row, r, col, save, val;
- unsigned irow, icol;
- int block, diffbuf[64], leaf, len, diff, carry=0, pnum=0, base[2];
- crw_init_tables (tiff_compress, huff);
- pixel = (ushort *) calloc (raw_width*8, sizeof *pixel);
- merror (pixel, "canon_compressed_load_raw()");
- lowbits = canon_has_lowbits();
- if (!lowbits) maximum = 0x3ff;
- fseek (ifp, 540 + lowbits*raw_height*raw_width/4, SEEK_SET);
- zero_after_ff = 1;
- getbits(-1);
- for (row=0; row < raw_height; row+=8) {
- nblocks = MIN (8, raw_height-row) * raw_width >> 6;
- for (block=0; block < nblocks; block++) {
- memset (diffbuf, 0, sizeof diffbuf);
- for (i=0; i < 64; i++ ) {
- leaf = gethuff(huff[i > 0]);
- if (leaf == 0 && i) break;
- if (leaf == 0xff) continue;
- i += leaf >> 4;
- len = leaf & 15;
- if (len == 0) continue;
- diff = getbits(len);
- if ((diff & (1 << (len-1))) == 0)
- diff -= (1 << len) - 1;
- if (i < 64) diffbuf[i] = diff;
- }
- diffbuf[0] += carry;
- carry = diffbuf[0];
- for (i=0; i < 64; i++ ) {
- if (pnum++ % raw_width == 0)
- base[0] = base[1] = 512;
- if ((pixel[(block << 6) + i] = base[i & 1] += diffbuf[i]) >> 10)
- derror();
- }
- }
- if (lowbits) {
- save = ftell(ifp);
- fseek (ifp, 26 + row*raw_width/4, SEEK_SET);
- for (prow=pixel, i=0; i < raw_width*2; i++) {
- c = fgetc(ifp);
- for (r=0; r < 8; r+=2, prow++) {
- val = (*prow << 2) + ((c >> r) & 3);
- if (raw_width == 2672 && val < 512) val += 2;
- *prow = val;
- }
- }
- fseek (ifp, save, SEEK_SET);
- }
- #ifdef LIBRAW_LIBRARY_BUILD
- for (r=0; r < 8; r++) {
- if(row+r>=raw_height) break; // Not sure that raw_height is always N*8
- // MOVE entire row into place
- memmove(&raw_image[(row+r)*raw_width],&pixel[r*raw_width],raw_width*sizeof(pixel[0]));
- irow = row - top_margin + r;
- if (irow >= height) continue; // if row above image area than irow is VERY positive :)
- // only margins!
- for (col=0; col < left_margin; col++)
- {
- icol = col - left_margin;
- c = FC(irow,icol);
- if (icol >= width && col > 1 && (unsigned) (col-left_margin+2) > width+3)
- cblack[c] += (cblack[4+c]++,pixel[r*raw_width+col]);
- }
- for (col=width+left_margin; col < raw_width; col++)
- {
- icol = col - left_margin;
- c = FC(irow,icol);
- if (icol >= width && col > 1 && (unsigned) (col-left_margin+2) > width+3)
- cblack[c] += (cblack[4+c]++,pixel[r*raw_width+col]);
- }
- }
- #else
- // dcraw original code
- for (r=0; r < 8; r++) {
- irow = row - top_margin + r;
- if (irow >= height) continue;
- for (col=0; col < raw_width; col++) {
- icol = col - left_margin;
- c = FC(irow,icol);
- if (icol < width)
- BAYER(irow,icol) = pixel[r*raw_width+col];
- else if (col > 1 && (unsigned) (col-left_margin+2) > width+3)
- cblack[c] += (cblack[4+c]++,pixel[r*raw_width+col]);
- }
- }
- #endif
- }
- free (pixel);
- FORC(2) free (huff[c]);
- FORC4 if (cblack[4+c]) cblack[c] /= cblack[4+c];
- }
- int CLASS ljpeg_start (struct jhead *jh, int info_only)
- {
- int c, tag, len;
- uchar data[0x10000];
- const uchar *dp;
- memset (jh, 0, sizeof *jh);
- jh->restart = INT_MAX;
- fread (data, 2, 1, ifp);
- if (data[1] != 0xd8) return 0;
- do {
- fread (data, 2, 2, ifp);
- tag = data[0] << 8 | data[1];
- len = (data[2] << 8 | data[3]) - 2;
- if (tag <= 0xff00) return 0;
- fread (data, 1, len, ifp);
- switch (tag) {
- case 0xffc3:
- jh->sraw = ((data[7] >> 4) * (data[7] & 15) - 1) & 3;
- case 0xffc0:
- jh->bits = data[0];
- jh->high = data[1] << 8 | data[2];
- jh->wide = data[3] << 8 | data[4];
- jh->clrs = data[5] + jh->sraw;
- if (len == 9 && !dng_version) getc(ifp);
- break;
- case 0xffc4:
- if (info_only) break;
- for (dp = data; dp < data+len && (c = *dp++) < 4; )
- jh->free[c] = jh->huff[c] = make_decoder_ref (&dp);
- break;
- case 0xffda:
- jh->psv = data[1+data[0]*2];
- jh->bits -= data[3+data[0]*2] & 15;
- break;
- case 0xffdd:
- jh->restart = data[0] << 8 | data[1];
- }
- } while (tag != 0xffda);
- if (info_only) return 1;
- FORC(5) if (!jh->huff[c+1]) jh->huff[c+1] = jh->huff[c];
- if (jh->sraw) {
- FORC(4) jh->huff[2+c] = jh->huff[1];
- FORC(jh->sraw) jh->huff[1+c] = jh->huff[0];
- }
- jh->row = (ushort *) calloc (jh->wide*jh->clrs, 4);
- merror (jh->row, "ljpeg_start()");
- return zero_after_ff = 1;
- }
- void CLASS ljpeg_end (struct jhead *jh)
- {
- int c;
- FORC4 if (jh->free[c]) free (jh->free[c]);
- free (jh->row);
- }
- // used for kodak-262 decoder
- int CLASS ljpeg_diff (ushort *huff)
- {
- int len, diff;
- len = gethuff(huff);
- if (len == 16 && (!dng_version || dng_version >= 0x1010000))
- return -32768;
- diff = getbits(len);
- if ((diff & (1 << (len-1))) == 0)
- diff -= (1 << len) - 1;
- return diff;
- }
- #ifdef LIBRAW_LIBRARY_BUILD
- int CLASS ljpeg_diff_new (LibRaw_bit_buffer& bits, LibRaw_byte_buffer* buf,ushort *huff)
- {
- int len, diff;
- len = bits._gethuff_lj(buf,*huff,huff+1);
- if (len == 16 && (!dng_version || dng_version >= 0x1010000))
- return -32768;
- diff = bits._getbits_lj(buf,len);
- if ((diff & (1 << (len-1))) == 0)
- diff -= (1 << len) - 1;
- return diff;
- }
- int CLASS ljpeg_diff_pef (LibRaw_bit_buffer& bits, LibRaw_byte_buffer* buf,ushort *huff)
- {
- int len, diff;
- len = bits._gethuff(buf,*huff,huff+1,zero_after_ff);
- if (len == 16 && (!dng_version || dng_version >= 0x1010000))
- return -32768;
- diff = bits._getbits(buf,len,zero_after_ff);
- if ((diff & (1 << (len-1))) == 0)
- diff -= (1 << len) - 1;
- return diff;
- }
- #endif
- ushort * CLASS ljpeg_row (int jrow, struct jhead *jh)
- {
- int col, c, diff, pred, spred=0;
- ushort mark=0, *row[3];
- if (jrow * jh->wide % jh->restart == 0) {
- FORC(6) jh->vpred[c] = 1 << (jh->bits-1);
- if (jrow) {
- fseek (ifp, -2, SEEK_CUR);
- do mark = (mark << 8) + (c = fgetc(ifp));
- while (c != EOF && mark >> 4 != 0xffd);
- }
- getbits(-1);
- }
- FORC3 row[c] = jh->row + jh->wide*jh->clrs*((jrow+c) & 1);
- for (col=0; col < jh->wide; col++)
- FORC(jh->clrs) {
- diff = ljpeg_diff (jh->huff[c]);
- if (jh->sraw && c <= jh->sraw && (col | c))
- pred = spred;
- else if (col) pred = row[0][-jh->clrs];
- else pred = (jh->vpred[c] += diff) - diff;
- if (jrow && col) switch (jh->psv) {
- case 1: break;
- case 2: pred = row[1][0]; break;
- case 3: pred = row[1][-jh->clrs]; break;
- case 4: pred = pred + row[1][0] - row[1][-jh->clrs]; break;
- case 5: pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1); break;
- case 6: pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1); break;
- case 7: pred = (pred + row[1][0]) >> 1; break;
- default: pred = 0;
- }
- if ((**row = pred + diff) >> jh->bits) derror();
- if (c <= jh->sraw) spred = **row;
- row[0]++; row[1]++;
- }
- return row[2];
- }
- #ifdef LIBRAW_LIBRARY_BUILD
- ushort * CLASS ljpeg_row_new (int jrow, struct jhead *jh, LibRaw_bit_buffer& bits,LibRaw_byte_buffer* bytes)
- {
- int col, c, diff, pred, spred=0;
- ushort mark=0, *row[3];
- if (jrow * jh->wide % jh->restart == 0) {
- FORC(6) jh->vpred[c] = 1 << (jh->bits-1);
- if (jrow) {
- bytes->unseek2();
- do mark = (mark << 8) + (c = bytes->get_byte());
- while (c != EOF && mark >> 4 != 0xffd);
- }
- bits.reset();
- }
- FORC3 row[c] = jh->row + jh->wide*jh->clrs*((jrow+c) & 1);
- for (col=0; col < jh->wide; col++)
- FORC(jh->clrs) {
- diff = ljpeg_diff_new (bits,bytes,jh->huff[c]);
- if (jh->sraw && c <= jh->sraw && (col | c))
- pred = spred;
- else if (col) pred = row[0][-jh->clrs];
- else pred = (jh->vpred[c] += diff) - diff;
- if (jrow && col) switch (jh->psv) {
- case 1: break;
- case 2: pred = row[1][0]; break;
- case 3: pred = row[1][-jh->clrs]; break;
- case 4: pred = pred + row[1][0] - row[1][-jh->clrs]; break;
- case 5: pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1); break;
- case 6: pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1); break;
- case 7: pred = (pred + row[1][0]) >> 1; break;
- default: pred = 0;
- }
- if ((**row = pred + diff) >> jh->bits) derror();
- if (c <= jh->sraw) spred = **row;
- row[0]++; row[1]++;
- }
- return row[2];
- }
- #endif
- void CLASS lossless_jpeg_load_raw()
- {
- int jwide, jrow, jcol, val, c, i, row=0, col=0;
- #ifndef LIBRAW_LIBRARY_BUILD
- int jidx,j;
- #endif
- struct jhead jh;
- int min=INT_MAX;
- ushort *rp;
- #ifdef LIBRAW_LIBRARY_BUILD
- int save_min = 0;
- unsigned slicesW[16],slicesWcnt=0,slices;
- unsigned *offset;
- unsigned t_y=0,t_x=0,t_s=0,slice=0,pixelsInSlice,pixno;
- if (!strcasecmp(make,"KODAK"))
- save_min = 1;
- #endif
- #ifdef LIBRAW_LIBRARY_BUILD
- if (cr2_slice[0]>15)
- throw LIBRAW_EXCEPTION_IO_EOF; // change many slices
- #else
- fprintf(stderr,"Too many CR2 slices: %d\n",cr2_slice[0]+1);
- return;
- #endif
- if (!ljpeg_start (&jh, 0)) return;
- jwide = jh.wide * jh.clrs;
- #ifdef LIBRAW_LIBRARY_BUILD
- if(cr2_slice[0])
- {
- for(i=0;i<cr2_slice[0];i++)
- slicesW[slicesWcnt++] = cr2_slice[1];
- slicesW[slicesWcnt++] = cr2_slice[2];
- }
- else
- {
- // not sliced
- slicesW[slicesWcnt++] = raw_width; // safe fallback
- }
-
- slices = slicesWcnt * jh.high;
- offset = (unsigned*)calloc(slices+1,sizeof(offset[0]));
-
- for(slice=0;slice<slices;slice++)
- {
- offset[slice] = (t_x + t_y * raw_width)| (t_s<<28);
- if(offset[slice] & 0x0fffffff >= raw_width * raw_height)
- throw LIBRAW_EXCEPTION_IO_BADFILE;
- t_y++;
- if(t_y == jh.high)
- {
- t_y = 0;
- t_x += slicesW[t_s++];
- }
- }
- offset[slices] = offset[slices-1];
- slice = 1; // next slice
- pixno = offset[0];
- pixelsInSlice = slicesW[0];
- #endif
- #ifdef LIBRAW_LIBRARY_BUILD
- LibRaw_byte_buffer *buf=NULL;
- if(data_size)
- buf = ifp->make_byte_buffer(data_size);
- LibRaw_bit_buffer bits;
- #endif
- for (jrow=0; jrow < jh.high; jrow++) {
- #ifdef LIBRAW_LIBRARY_BUILD
- if (data_size)
- rp = ljpeg_row_new (jrow, &jh,bits,buf);
- else
- #endif
- rp = ljpeg_row (jrow, &jh);
- if (load_flags & 1)
- row = jrow & 1 ? height-1-jrow/2 : jrow/2;
- for (jcol=0; jcol < jwide; jcol++) {
- val = *rp++;
- if (jh.bits <= 12)
- val = curve[val & 0xfff];
- #ifndef LIBRAW_LIBRARY_BUILD
- // slow dcraw way to calculate row/col
- if (cr2_slice[0]) {
- jidx = jrow*jwide + jcol;
- i = jidx / (cr2_slice[1]*jh.high);
- if ((j = i >= cr2_slice[0]))
- i = cr2_slice[0];
- jidx -= i * (cr2_slice[1]*jh.high);
- row = jidx / cr2_slice[1+j];
- col = jidx % cr2_slice[1+j] + i*cr2_slice[1];
- }
- #else
- // new fast one, but for data_size defined only (i.e. new CR2 format, not 1D/1Ds)
- if(data_size)
- {
- row = pixno/raw_width;
- col = pixno % raw_width;
- pixno++;
- if (0 == --pixelsInSlice)
- {
- unsigned o = offset[slice++];
- pixno = o & 0x0fffffff;
- pixelsInSlice = slicesW[o>>28];
- }
- }
- #endif
- #ifndef LIBRAW_LIBRARY_BUILD
- if (raw_width == 3984 && (col -= 2) < 0)
- col += (row--,raw_width);
- if ((unsigned) (row-top_margin) < height) {
- c = FC(row-top_margin,col-left_margin);
- if ((unsigned) (col-left_margin) < width) {
- BAYER(row-top_margin,col-left_margin) = val;
- if (min > val) min = val;
- } else if (col > 1 && (unsigned) (col-left_margin+2) > width+3)
- cblack[c] += (cblack[4+c]++,val);
- }
- #else
- if (raw_width == 3984)
- {
- if ( (col -= 2) < 0)
- col += (row--,raw_width);
- if(row >= 0 && row < raw_height && col >= 0 && col < raw_width)
- RBAYER(row,col) = val;
- }
- else
- RBAYER(row,col) = val;
- if ((unsigned) (row-top_margin) < height)
- {
- // within image height
- if ((unsigned) (col-left_margin) < width)
- {
- // within image area, save min
- if(save_min)
- if (min > val) min = val;
- }
- else if (col > 1 && (unsigned) (col-left_margin+2) > width+3)
- {
- c = FC(row-top_margin,col-left_margin);
- cblack[c] += (cblack[4+c]++,val);
- }
- }
- #endif
- #ifndef LIBRAW_LIBRARY_BUILD
- if (++col >= raw_width)
- col = (row++,0);
- #else
- if(!data_size) // 1D or 1Ds case
- if (++col >= raw_width)
- col = (row++,0);
- #endif
- }
- }
- ljpeg_end (&jh);
- FORC4 if (cblack[4+c]) cblack[c] /= cblack[4+c];
- if (!strcasecmp(make,"KODAK"))
- black = min;
- #ifdef LIBRAW_LIBRARY_BUILD
- if(buf)
- delete buf;
- free(offset);
- #endif
- }
- void CLASS canon_sraw_load_raw()
- {
- struct jhead jh;
- short *rp=0, (*ip)[4];
- int jwide, slice, scol, ecol, row, col, jrow=0, jcol=0, pix[3], c;
- int v[3]={0,0,0}, ver, hue;
- char *cp;
- if (!ljpeg_start (&jh, 0)) return;
- jwide = (jh.wide >>= 1) * jh.clrs;
- #ifdef LIBRAW_LIBRARY_BUILD
- if(!data_size)
- throw LIBRAW_EXCEPTION_IO_BADFILE;
- LibRaw_byte_buffer *buf = ifp->make_byte_buffer(data_size);
- LibRaw_bit_buffer bits;
- #endif
- for (ecol=slice=0; slice <= cr2_slice[0]; slice++) {
- scol = ecol;
- ecol += cr2_slice[1] * 2 / jh.clrs;
- if (!cr2_slice[0] || ecol > raw_width-1) ecol = raw_width & -2;
- for (row=0; row < height; row += (jh.clrs >> 1) - 1) {
- ip = (short (*)[4]) image + row*width;
- for (col=scol; col < ecol; col+=2, jcol+=jh.clrs) {
- if ((jcol %= jwide) == 0)
- #ifdef LIBRAW_LIBRARY_BUILD
- rp = (short*) ljpeg_row_new (jrow++, &jh,bits,buf);
- #else
- rp = (short *) ljpeg_row (jrow++, &jh);
- #endif
- if (col >= width) continue;
- FORC (jh.clrs-2)
- ip[col + (c >> 1)*width + (c & 1)][0] = rp[jcol+c];
- ip[col][1] = rp[jcol+jh.clrs-2] - 16384;
- ip[col][2] = rp[jcol+jh.clrs-1] - 16384;
- }
- }
- }
- for (cp=model2; *cp && !isdigit(*cp); cp++);
- sscanf (cp, "%d.%d.%d", v, v+1, v+2);
- ver = (v[0]*1000 + v[1])*1000 + v[2];
- hue = (jh.sraw+1) << 2;
- if (unique_id >= 0x80000281 || (unique_id == 0x80000218 && ver > 1000006))
- hue = jh.sraw << 1;
- ip = (short (*)[4]) image;
- rp = ip[0];
- for (row=0; row < height; row++, ip+=width) {
- if (row & (jh.sraw >> 1))
- for (col=0; col < width; col+=2)
- for (c=1; c < 3; c++)
- if (row == height-1)
- ip[col][c] = ip[col-width][c];
- else ip[col][c] = (ip[col-width][c] + ip[col+width][c] + 1) >> 1;
- for (col=1; col < width; col+=2)
- for (c=1; c < 3; c++)
- if (col == width-1)
- ip[col][c] = ip[col-1][c];
- else ip[col][c] = (ip[col-1][c] + ip[col+1][c] + 1) >> 1;
- }
- for ( ; rp < ip[0]; rp+=4) {
- if (unique_id < 0x80000218) {
- pix[0] = rp[0] + rp[2] - 512;
- pix[2] = rp[0] + rp[1] - 512;
- pix[1] = rp[0] + ((-778*rp[1] - (rp[2] << 11)) >> 12) - 512;
- } else {
- rp[1] = (rp[1] << 2) + hue;
- rp[2] = (rp[2] << 2) + hue;
- pix[0] = rp[0] + (( 50*rp[1] + 22929*rp[2]) >> 14);
- pix[1] = rp[0] + ((-5640*rp[1] - 11751*rp[2]) >> 14);
- pix[2] = rp[0] + ((29040*rp[1] - 101*rp[2]) >> 14);
- }
- FORC3
- rp[c] = CLIP(pix[c] * sraw_mul[c] >> 10);
- }
- #ifdef LIBRAW_LIBRARY_BUILD
- delete buf;
- #endif
- ljpeg_end (&jh);
- maximum = 0x3fff;
- }
- void CLASS adobe_copy_pixel (int row, int col, ushort **rp)
- {
- unsigned r, c;
- #ifndef LIBRAW_LIBRARY_BUILD
- r = row -= top_margin;
- c = col -= left_margin;
- if (is_raw == 2 && shot_select) (*rp)++;
- if (filters) {
- if (fuji_width) {
- r = row + fuji_width - 1 - (col >> 1);
- c = row + ((col+1) >> 1);
- }
- if (r < height && c < width)
- BAYER(r,c) = **rp < 0x1000 ? curve[**rp] : **rp;
- *rp += is_raw;
- } else {
- if (r < height && c < width)
- FORC(tiff_samples)
- image[row*width+col][c] = (*rp)[c] < 0x1000 ? curve[(*rp)[c]]:(*rp)[c];
- *rp += tiff_samples;
- }
- #else
- if (is_raw == 2 && shot_select) (*rp)++;
- if (filters) {
- if(row < raw_height && col < raw_width)
- RBAYER(row,col) = **rp < 0x1000 ? curve[**rp] : **rp;
- *rp += is_raw;
- } else {
- if (row < raw_height && col < raw_width)
- FORC(tiff_samples)
- color_image[row*raw_width+col][c] = (*rp)[c]<0x1000 ? curve[(*rp)[c]]:(*rp)[c];
- *rp += tiff_samples;
- }
- #endif
- if (is_raw == 2 && shot_select) (*rp)--;
- }
- void CLASS adobe_dng_load_raw_lj()
- {
- unsigned save, trow=0, tcol=0, jwide, jrow, jcol, row, col;
- struct jhead jh;
- ushort *rp;
- while (trow < raw_height) {
- save = ftell(ifp);
- if (tile_length < INT_MAX)
- fseek (ifp, get4(), SEEK_SET);
- if (!ljpeg_start (&jh, 0)) break;
- jwide = jh.wide;
- if (filters) jwide *= jh.clrs;
- jwide /= is_raw;
- #ifdef LIBRAW_LIBRARY_BUILD
- if(!data_size)
- throw LIBRAW_EXCEPTION_IO_BADFILE;
- LibRaw_byte_buffer *buf = ifp->make_byte_buffer(data_size);
- LibRaw_bit_buffer bits;
- #endif
- for (row=col=jrow=0; jrow < jh.high; jrow++) {
- #ifdef LIBRAW_LIBRARY_BUILD
- rp = ljpeg_row_new (jrow, &jh,bits,buf);
- #else
- rp = ljpeg_row (jrow, &jh);
- #endif
- for (jcol=0; jcol < jwide; jcol++) {
- adobe_copy_pixel (trow+row, tcol+col, &rp);
- if (++col >= tile_width || col >= raw_width)
- row += 1 + (col = 0);
- }
- }
- fseek (ifp, save+4, SEEK_SET);
- if ((tcol += tile_width) >= raw_width)
- trow += tile_length + (tcol = 0);
- ljpeg_end (&jh);
- #ifdef LIBRAW_LIBRARY_BUILD
- delete buf;
- #endif
- }
- }
- void CLASS adobe_dng_load_raw_nc()
- {
- ushort *pixel, *rp;
- int row, col;
- pixel = (ushort *) calloc (raw_width * tiff_samples, sizeof *pixel);
- merror (pixel, "adobe_dng_load_raw_nc()");
- #ifdef LIBRAW_LIBRARY_BUILD
- int dsz= raw_height*raw_width * tiff_samples * tiff_bps/8;
- LibRaw_byte_buffer *buf = NULL;
- if (tiff_bps != 16)
- {
- buf = ifp->make_byte_buffer(dsz);
- }
- LibRaw_bit_buffer bits;
- #endif
- for (row=0; row < raw_height; row++) {
- if (tiff_bps == 16)
- read_shorts (pixel, raw_width * tiff_samples);
- else {
- #ifdef LIBRAW_LIBRARY_BUILD
- bits.reset();
- for (col=0; col < raw_width * tiff_samples; col++)
- pixel[col] = bits._getbits(buf,tiff_bps,zero_after_ff);
- #else
- getbits(-1);
- for (col=0; col < raw_width * tiff_samples; col++)
- pixel[col] = getbits(tiff_bps);
- #endif
- }
- for (rp=pixel, col=0; col < raw_width; col++)
- adobe_copy_pixel (row, col, &rp);
- }
- free (pixel);
- #ifdef LIBRAW_LIBRARY_BUILD
- if(buf)
- delete buf;
- #endif
- }
- void CLASS pentax_load_raw()
- {
- ushort bit[2][15], huff[4097];
- int dep, row, col, diff, c, i;
- ushort vpred[2][2] = {{0,0},{0,0}}, hpred[2];
- fseek (ifp, meta_offset, SEEK_SET);
- dep = (get2() + 12) & 15;
- fseek (ifp, 12, SEEK_CUR);
- FORC(dep) bit[0][c] = get2();
- FORC(dep) bit[1][c] = fgetc(ifp);
- FORC(dep)
- for (i=bit[0][c]; i <= ((bit[0][c]+(4096 >> bit[1][c])-1) & 4095); )
- huff[++i] = bit[1][c] << 8 | c;
- huff[0] = 12;
- fseek (ifp, data_offset, SEEK_SET);
- #ifdef LIBRAW_LIBRARY_BUILD
- if(!data_size)
- throw LIBRAW_EXCEPTION_IO_BADFILE;
- LibRaw_byte_buffer *buf = ifp->make_byte_buffer(data_size);
- LibRaw_bit_buffer bits;
- bits.reset();
- #else
- getbits(-1);
- #endif
- for (row=0; row < raw_height; row++)
- {
- for (col=0; col < raw_width; col++) {
- #ifdef LIBRAW_LIBRARY_BUILD
- diff = ljpeg_diff_pef(bits,buf,huff);
- #else
- diff = ljpeg_diff (huff);
- #endif
- if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
- else hpred[col & 1] += diff;
- unsigned val = hpred[col & 1];
- #ifndef LIBRAW_LIBRARY_BUILD
- if ((unsigned) (row-top_margin) < height &&
- (unsigned)(col-left_margin) < width)
- BAYER(row-top_margin,col-left_margin) = val;
- #else
- RBAYER(row,col) = val;
- #endif
- if (val >> tiff_bps) derror();
- }
- }
- #ifdef LIBRAW_LIBRARY_BUILD
- delete buf;
- #endif
- }
- void CLASS nikon_compressed_load_raw()
- {
- static const uchar nikon_tree[][32] = {
- { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy */
- 5,4,3,6,2,7,1,0,8,9,11,10,12 },
- { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy after split */
- 0x39,0x5a,0x38,0x27,0x16,5,4,3,2,1,0,11,12,12 },
- { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0, /* 12-bit lossless */
- 5,4,6,3,7,2,8,1,9,0,10,11,12 },
- { 0,1,4,3,1,1,1,1,1,2,0,0,0,0,0,0, /* 14-bit lossy */
- 5,6,4,7,8,3,9,2,1,0,10,11,12,13,14 },
- { 0,1,5,1,1,1,1,1,1,1,2,0,0,0,0,0, /* 14-bit lossy after split */
- 8,0x5c,0x4b,0x3a,0x29,7,6,5,4,3,2,1,0,13,14 },
- { 0,1,4,2,2,3,1,2,0,0,0,0,0,0,0,0, /* 14-bit lossless */
- 7,6,8,5,9,4,10,3,11,12,2,0,1,13,14 } };
- ushort *huff, ver0, ver1, vpred[2][2], hpred[2], csize;
- int i, min, max, step=0, tree=0, split=0, row, col, len, shl, diff;
- fseek (ifp, meta_offset, SEEK_SET);
- ver0 = fgetc(ifp);
- ver1 = fgetc(ifp);
- if (ver0 == 0x49 || ver1 == 0x58)
- fseek (ifp, 2110, SEEK_CUR);
- if (ver0 == 0x46) tree = 2;
- if (tiff_bps == 14) tree += 3;
- read_shorts (vpred[0], 4);
- max = 1 << tiff_bps & 0x7fff;
- if ((csize = get2()) > 1)
- step = max / (csize-1);
- if (ver0 == 0x44 && ver1 == 0x20 && step > 0) {
- for (i=0; i < csize; i++)
- curve[i*step] = get2();
- for (i=0; i < max; i++)
- curve[i] = ( curve[i-i%step]*(step-i%step) +
- curve[i-i%step+step]*(i%step) ) / step;
- #ifdef LIBRAW_LIBRARY_BUILD
- color_flags.curve_state = LIBRAW_COLORSTATE_LOADED;
- #endif
- fseek (ifp, meta_offset+562, SEEK_SET);
- split = get2();
- } else if (ver0 != 0x46 && csize <= 0x4001)
- {
- read_shorts (curve, max=csize);
- #ifdef LIBRAW_LIBRARY_BUILD
- color_flags.curve_state = LIBRAW_COLORSTATE_LOADED;
- #endif
- }
- while (curve[max-2] == curve[max-1]) max--;
- huff = make_decoder (nikon_tree[tree]);
- fseek (ifp, data_offset, SEEK_SET);
- #ifdef LIBRAW_LIBRARY_BUILD
- if(!data_size)
- throw LIBRAW_EXCEPTION_IO_BADFILE;
- LibRaw_byte_buffer *buf = ifp->make_byte_buffer(data_size);
- LibRaw_bit_buffer bits;
- bits.reset();
- #else
- getbits(-1);
- #endif
- for (min=row=0; row < height; row++) {
- if (split && row == split) {
- free (huff);
- huff = make_decoder (nikon_tree[tree+1]);
- max += (min = 16) << 1;
- }
- for (col=0; col < raw_width; col++) {
- #ifdef LIBRAW_LIBRARY_BUILD
- i = bits._gethuff(buf,*huff,huff+1,zero_after_ff);
- #else
- i = gethuff(huff);
- #endif
- len = i & 15;
- shl = i >> 4;
- #ifdef LIBRAW_LIBRARY_BUILD
- diff = ((bits._getbits(buf,len-shl,zero_after_ff) << 1) + 1) << shl >> 1;
- #else
- diff = ((getbits(len-shl) << 1) + 1) << shl >> 1;
- #endif
- if ((diff & (1 << (len-1))) == 0)
- diff -= (1 << len) - !shl;
- if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
- else hpred[col & 1] += diff;
- if ((ushort)(hpred[col & 1] + min) >= max) derror();
- #ifndef LIBRAW_LIBRARY_BUILD
- if ((unsigned) (col-left_margin) < width)
- BAYER(row,col-left_margin) = curve[LIM((short)hpred[col & 1],0,0x3fff)];
- #else
- ushort xval = hpred[col & 1];
- xval = curve[LIM((short)xval,0,0x3fff)];
- RBAYER(row,col) = xval;
- #endif
- }
- }
- #ifdef LIBRAW_LIBRARY_BUILD
- delete buf;
- #endif
- free (huff);
- }
- /*
- Figure out if a NEF file is compressed. These fancy heuristics
- are only needed for the D100, thanks to a bug in some cameras
- that tags all images as "compressed".
- */
- int CLASS nikon_is_compressed()
- {
- uchar test[256];
- int i;
- fseek (ifp, data_offset, SEEK_SET);
- fread (test, 1, 256, ifp);
- for (i=15; i < 256; i+=16)
- if (test[i]) return 1;
- return 0;
- }
- /*
- Returns 1 for a Coolpix 995, 0 for anything else.
- */
- int CLASS nikon_e995()
- {
- int i, histo[256];
- const uchar often[] = { 0x00, 0x55, 0xaa, 0xff };
- memset (histo, 0, sizeof histo);
- fseek (ifp, -2000, SEEK_END);
- for (i=0; i < 2000; i++)
- histo[fgetc(ifp)]++;
- for (i=0; i < 4; i++)
- if (histo[often[i]] < 200)
- return 0;
- return 1;
- }
- /*
- Returns 1 for a Coolpix 2100, 0 for anything else.
- */
- int CLASS nikon_e2100()
- {
- uchar t[12];
- int i;
- fseek (ifp, 0, SEEK_SET);
- for (i=0; i < 1024; i++) {
- fread (t, 1, 12, ifp);
- if (((t[2] & t[4] & t[7] & t[9]) >> 4
- & t[1] & t[6] & t[8] & t[11] & 3) != 3)
- return 0;
- }
- return 1;
- }
- void CLASS nikon_3700()
- {
- int bits, i;
- uchar dp[24];
- static const struct {
- int bits;
- char t_make[12], t_model[15];
- } table[] = {
- { 0x00, "PENTAX", "Optio 33WR" },
- { 0x03, "NIKON", "E3200" },
- { 0x32, "NIKON", "E3700" },
- { 0x33, "OLYMPUS", "C740UZ" } };
- fseek (ifp, 3072, SEEK_SET);
- fread (dp, 1, 24, ifp);
- bits = (dp[8] & 3) << 4 | (dp[20] & 3);
- for (i=0; i < sizeof table / sizeof *table; i++)
- if (bits == table[i].bits) {
- strcpy (make, table[i].t_make );
- strcpy (model, table[i].t_model);
- }
- }
- /*
- Separates a Minolta DiMAGE Z2 from a Nikon E4300.
- */
- int CLASS minolta_z2()
- {
- int i, nz;
- char tail[424];
- fseek (ifp, -sizeof tail, SEEK_END);
- fread (tail, 1, sizeof tail, ifp);
- for (nz=i=0; i < sizeof tail; i++)
- if (tail[i]) nz++;
- return nz > 20;
- }
- /*
- The Fuji Super CCD is just a Bayer grid rotated 45 degrees.
- */
- void CLASS fuji_load_raw()
- {
- #ifndef LIBRAW_LIBRARY_BUILD
- ushort *pixel;
- int wide, row, col, r, c;
- fseek (ifp, (top_margin*raw_width + left_margin) * 2, SEEK_CUR);
- wide = fuji_width << !fuji_layout;
- pixel = (ushort *) calloc (wide, sizeof *pixel);
- merror (pixel, "fuji_load_raw()");
- for (row=0; row < raw_height; row++) {
- read_shorts (pixel, wide);
- fseek (ifp, 2*(raw_width - wide), SEEK_CUR);
- for (col=0; col < wide; col++) {
- if (fuji_layout) {
- r = fuji_width - 1 - col + (row >> 1);
- c = col + ((row+1) >> 1);
- } else {
- r = fuji_width - 1 + row - (col >> 1);
- c = row + ((col+1) >> 1);
- }
- BAYER(r,c) = pixel[col];
- }
- }
- free (pixel);
- #else
- read_shorts(raw_image,raw_width*raw_height);
- #endif
- }
- void CLASS ppm_thumb()
- {
- char *thumb;
- thumb_length = thumb_width*thumb_height*3;
- thumb = (char *) malloc (thumb_length);
- merror (thumb, "ppm_thumb()");
- fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
- fread (thumb, 1, thumb_length, ifp);
- fwrite (thumb, 1, thumb_length, ofp);
- free (thumb);
- }
- void CLASS layer_thumb()
- {
- int i, c;
- char *thumb, map[][4] = { "012","102" };
- colors = thumb_misc >> 5 & 7;
- thumb_length = thumb_width*thumb_height;
- thumb = (char *) calloc (colors, thumb_length);
- merror (thumb, "layer_thumb()");
- fprintf (ofp, "P%d\n%d %d\n255\n",
- 5 + (colors >> 1), thumb_width, thumb_height);
- fread (thumb, thumb_length, colors, ifp);
- for (i=0; i < thumb_length; i++)
- FORCC putc (thumb[i+thumb_length*(map[thumb_misc >> 8][c]-'0')], ofp);
- free (thumb);
- }
- void CLASS rollei_thumb()
- {
- unsigned i;
- ushort *thumb;
- thumb_length = thumb_width * thumb_height;
- thumb = (ushort *) calloc (thumb_length, 2);
- merror (thumb, "rollei_thumb()");
- fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
- read_shorts (thumb, thumb_length);
- for (i=0; i < thumb_length; i++) {
- putc (thumb[i] << 3, ofp);
- putc (thumb[i] >> 5 << 2, ofp);
- putc (thumb[i] >> 11 << 3, ofp);
- }
- free (thumb);
- }
- void CLASS rollei_load_raw()
- {
- uchar pixel[10];
- unsigned iten=0, isix, i, buffer=0, row, col, todo[16];
- isix = raw_width * raw_height * 5 / 8;
- while (fread (pixel, 1, 10, ifp) == 10) {
- for (i=0; i < 10; i+=2) {
- todo[i] = iten++;
- todo[i+1] = pixel[i] << 8 | pixel[i+1];
- buffer = pixel[i] >> 2 | buffer << 6;
- }
- for ( ; i < 16; i+=2) {
- todo[i] = isix++;
- todo[i+1] = buffer >> (14-i)*5;
- }
- for (i=0; i < 16; i+=2) {
- #ifndef LIBRAW_LIBRARY_BUILD
- row = todo[i] / raw_width - top_margin;
- col = todo[i] % raw_width - left_margin;
- if (row < height && col < width)
- BAYER(row,col) = (todo[i+1] & 0x3ff);
- #else
- RBAYER(todo[i] / raw_width,todo[i] % raw_width) = (todo[i+1] & 0x3ff);
- #endif
- }
- }
- maximum = 0x3ff;
- }
- int CLASS bayer (unsigned row, unsigned col)
- {
- return (row < height && col < width) ? BAYER(row,col) : 0;
- }
- void CLASS phase_one_flat_field (int is_float, int nc)
- {
- ushort head[8];
- unsigned wide, y, x, c, rend, cend, row, col;
- float *mrow, num, mult[4];
- read_shorts (head, 8);
- wide = head[2] / head[4];
- mrow = (float *) calloc (nc*wide, sizeof *mrow);
- merror (mrow, "phase_one_flat_field()");
- for (y=0; y < head[3] / head[5]; y++) {
- for (x=0; x < wide; x++)
- for (c=0; c < nc; c+=2) {
- num = is_float ? getreal(11) : get2()/32768.0;
- if (y==0) mrow[c*wide+x] = num;
- else mrow[(c+1)*wide+x] = (num - mrow[c*wide+x]) / head[5];
- }
- if (y==0) continue;
- rend = head[1]-top_margin + y*head[5];
- for (row = rend-head[5]; row < height && row < rend; row++) {
- for (x=1; x < wide; x++) {
- for (c=0; c < nc; c+=2) {
- mult[c] = mrow[c*wide+x-1];
- mult[c+1] = (mrow[c*wide+x] - mult[c]) / head[4];
- }
- cend = head[0]-left_margin + x*head[4];
- for (col = cend-head[4]; col < width && col < cend; col++) {
- c = nc > 2 ? FC(row,col) : 0;
- if (!(c & 1)) {
- c = BAYER(row,col) * mult[c];
- BAYER(row,col) = LIM(c,0,65535);
- }
- for (c=0; c < nc; c+=2)
- mult[c] += mult[c+1];
- }
- }
- for (x=0; x < wide; x++)
- for (c=0; c < nc; c+=2)
- mrow[c*wide+x] += mrow[(c+1)*wide+x];
- }
- }
- free (mrow);
- }
- void CLASS phase_one_correct()
- {
- unsigned entries, tag, data, save, col, row, type;
- int len, i, j, k, cip, val[4], dev[4], sum, max;
- int head[9], diff, mindiff=INT_MAX, off_412=0;
- static const signed char dir[12][2] =
- { {-1,-1}, {-1,1}, {1,-1}, {1,1}, {-2,0}, {0,-2}, {0,2}, {2,0},
- {-2,-2}, {-2,2}, {2,-2}, {2,2} };
- float poly[8], num, cfrac, frac, mult[2], *yval[2];
- ushort *xval[2];
- if (half_size || !meta_length) return;
- #ifdef DCRAW_VERBOSE
- if (verbose) fprintf (stderr,_("Phase One correction...\n"));
- #endif
- fseek (ifp, meta_offset, SEEK_SET);
- order = get2();
- fseek (ifp, 6, SEEK_CUR);
- fseek (ifp, meta_offset+get4(), SEEK_SET);
- entries = get4(); get4();
- while (entries--) {
- tag = get4();
- len = get4();
- data = get4();
- save = ftell(ifp);
- fseek (ifp, meta_offset+data, SEEK_SET);
- if (tag == 0x419) { /* Polynomial curve */
- for (get4(), i=0; i < 8; i++)
- poly[i] = getreal(11);
- poly[3] += (ph1.tag_210 - poly[7]) * poly[6] + 1;
- for (i=0; i < 0x10000; i++) {
- num = (poly[5]*i + poly[3])*i + poly[1];
- curve[i] = LIM(num,0,65535);
- } goto apply; /* apply to right half */
- } else if (tag == 0x41a) { /* Polynomial curve */
- for (i=0; i < 4; i++)
- poly[i] = getreal(11);
- for (i=0; i < 0x10000; i++) {
- for (num=0, j=4; j--; )
- num = num * i + poly[j];
- curve[i] = LIM(num+i,0,65535);
- } apply: /* apply to whole image */
- for (row=0; row < height; row++)
- for (col = (tag & 1)*ph1.split_col; col < width; col++)
- BAYER(row,col) = curve[BAYER(row,col)];
- } else if (tag == 0x400) { /* Sensor defects */
- while ((len -= 8) >= 0) {
- col = get2() - left_margin;
- row = get2() - top_margin;
- type = get2(); get2();
- if (col >= width) continue;
- if (type == 131) /* Bad column */
- for (row=0; row < height; row++)
- if (FC(row,col) == 1) {
- for (sum=i=0; i < 4; i++)
- sum += val[i] = bayer (row+dir[i][0], col+dir[i][1]);
- for (max=i=0; i < 4; i++) {
- dev[i] = abs((val[i] << 2) - sum);
- if (dev[max] < dev[i]) max = i;
- }
- BAYER(row,col) = (sum - val[max])/3.0 + 0.5;
- } else {
- for (sum=0, i=8; i < 12; i++)
- sum += bayer (row+dir[i][0], col+dir[i][1]);
- BAYER(row,col) = 0.5 + sum * 0.0732233 +
- (bayer(row,col-2) + bayer(row,col+2)) * 0.3535534;
- }
- else if (type == 129) { /* Bad pixel */
- if (row >= height) continue;
- j = (FC(row,col) != 1) * 4;
- for (sum=0, i=j; i < j+8; i++)
- sum += bayer (row+dir[i][0], col+dir[i][1]);
- BAYER(row,col) = (sum + 4) >> 3;
- }
- }
- } else if (tag == 0x401) { /* All-color flat fields */
- phase_one_flat_field (1, 2);
- } else if (tag == 0x416 || tag == 0x410) {
- phase_one_flat_field (0, 2);
- } else if (tag == 0x40b) { /* Red+blue flat field */
- phase_one_flat_field (0, 4);
- } else if (tag == 0x412) {
- fseek (ifp, 36, SEEK_CUR);
- diff = abs (get2() - ph1.tag_21a);
- if (mindiff > diff) {
- mindiff = diff;
- off_412 = ftell(ifp) - 38;
- }
- }
- fseek (ifp, save, SEEK_SET);
- }
- if (off_412) {
- fseek (ifp, off_412, SEEK_SET);
- for (i=0; i < 9; i++) head[i] = get4() & 0x7fff;
- yval[0] = (float *) calloc (head[1]*head[3] + head[2]*head[4], 6);
- merror (yval[0], "phase_one_correct()");
- yval[1] = (float *) (yval[0] + head[1]*head[3]);
- xval[0] = (ushort *) (yval[1] + head[2]*head[4]);
- xval[1] = (ushort *) (xval[0] + head[1]*head[3]);
- get2();
- for (i=0; i < 2; i++)
- for (j=0; j < head[i+1]*head[i+3]; j++)
- yval[i][j] = getreal(11);
- for (i=0; i < 2; i++)
- for (j=0; j < head[i+1]*head[i+3]; j++)
- xval[i][j] = get2();
- for (row=0; row < height; row++)
- for (col=0; col < width; col++) {
- cfrac = (float) col * head[3] / raw_width;
- cfrac -= cip = cfrac;
- num = BAYER(row,col) * 0.5;
- for (i=cip; i < cip+2; i++) {
- for (k=j=0; j < head[1]; j++)
- if (num < xval[0][k = head[1]*i+j]) break;
- frac = (j == 0 || j == head[1]) ? 0 :
- (xval[0][k] - num) / (xval[0][k] - xval[0][k-1]);
- mult[i-cip] = yval[0][k-1] * frac + yval…
Large files files are truncated, but you can click here to view the full file