| 1 |
#include <string.h> |
| 2 |
#include "mallocvar.h" |
| 3 |
#include "pm.h" |
| 4 |
#include "global_variables.h" |
| 5 |
#include "util.h" |
| 6 |
#include "decode.h" |
| 7 |
#include "bayer.h" |
| 8 |
#include "canon.h" |
| 9 |
#include "stdio_nofail.h" |
| 10 |
|
| 11 |
|
| 12 |
void |
| 13 |
canon_600_load_raw(Image const image) { |
| 14 |
unsigned char data[1120], *dp; |
| 15 |
unsigned short pixel[896], *pix; |
| 16 |
int irow, orow, col; |
| 17 |
|
| 18 |
for (irow=orow=0; irow < height; irow++) |
| 19 |
{ |
| 20 |
fread_nofail (data, 1120, 1, ifp); |
| 21 |
for (dp=data, pix=pixel; dp < data+1120; dp+=10, pix+=8) |
| 22 |
{ |
| 23 |
pix[0] = (dp[0] << 2) + (dp[1] >> 6 ); |
| 24 |
pix[1] = (dp[2] << 2) + (dp[1] >> 4 & 3); |
| 25 |
pix[2] = (dp[3] << 2) + (dp[1] >> 2 & 3); |
| 26 |
pix[3] = (dp[4] << 2) + (dp[1] & 3); |
| 27 |
pix[4] = (dp[5] << 2) + (dp[9] & 3); |
| 28 |
pix[5] = (dp[6] << 2) + (dp[9] >> 2 & 3); |
| 29 |
pix[6] = (dp[7] << 2) + (dp[9] >> 4 & 3); |
| 30 |
pix[7] = (dp[8] << 2) + (dp[9] >> 6 ); |
| 31 |
} |
| 32 |
for (col=0; col < width; col++) |
| 33 |
BAYER(orow,col) = pixel[col]; |
| 34 |
for (col=width; col < 896; col++) |
| 35 |
black += pixel[col]; |
| 36 |
if ((orow+=2) > height) |
| 37 |
orow = 1; |
| 38 |
} |
| 39 |
black /= (896 - width) * height; |
| 40 |
maximum = 0x3ff; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
void |
| 46 |
canon_a5_load_raw(Image const image) { |
| 47 |
unsigned char data[1940], *dp; |
| 48 |
unsigned short pixel[1552], *pix; |
| 49 |
int row, col; |
| 50 |
|
| 51 |
for (row=0; row < height; row++) { |
| 52 |
fread_nofail (data, raw_width * 10 / 8, 1, ifp); |
| 53 |
for (dp=data, pix=pixel; pix < pixel+raw_width; dp+=10, pix+=8) |
| 54 |
{ |
| 55 |
pix[0] = (dp[1] << 2) + (dp[0] >> 6); |
| 56 |
pix[1] = (dp[0] << 4) + (dp[3] >> 4); |
| 57 |
pix[2] = (dp[3] << 6) + (dp[2] >> 2); |
| 58 |
pix[3] = (dp[2] << 8) + (dp[5] ); |
| 59 |
pix[4] = (dp[4] << 2) + (dp[7] >> 6); |
| 60 |
pix[5] = (dp[7] << 4) + (dp[6] >> 4); |
| 61 |
pix[6] = (dp[6] << 6) + (dp[9] >> 2); |
| 62 |
pix[7] = (dp[9] << 8) + (dp[8] ); |
| 63 |
} |
| 64 |
for (col=0; col < width; col++) |
| 65 |
BAYER(row,col) = (pixel[col] & 0x3ff); |
| 66 |
for (col=width; col < raw_width; col++) |
| 67 |
black += pixel[col] & 0x3ff; |
| 68 |
} |
| 69 |
if (raw_width > width) |
| 70 |
black /= (raw_width - width) * height; |
| 71 |
maximum = 0x3ff; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
static int |
| 83 |
canon_has_lowbits() |
| 84 |
{ |
| 85 |
unsigned char test[0x4000]; |
| 86 |
int ret=1, i; |
| 87 |
|
| 88 |
fseek_nofail (ifp, 0, SEEK_SET); |
| 89 |
fread_nofail (test, 1, sizeof test, ifp); |
| 90 |
for (i=540; i < sizeof test - 1; i++) |
| 91 |
if (test[i] == 0xff) { |
| 92 |
if (test[i+1]) return 1; |
| 93 |
ret=0; |
| 94 |
} |
| 95 |
return ret; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
void |
| 101 |
canon_compressed_load_raw(Image const image) { |
| 102 |
unsigned short *pixel, *prow; |
| 103 |
int lowbits, i, row, r, col, save, val; |
| 104 |
unsigned irow, icol; |
| 105 |
struct decode *decode, *dindex; |
| 106 |
int block, diffbuf[64], leaf, len, diff, carry=0, pnum=0, base[2]; |
| 107 |
unsigned char c; |
| 108 |
|
| 109 |
MALLOCARRAY(pixel, raw_width*8); |
| 110 |
if (pixel == NULL) |
| 111 |
pm_error("Unable to allocate space for %u pixels", raw_width*8); |
| 112 |
lowbits = canon_has_lowbits(); |
| 113 |
if (!lowbits) maximum = 0x3ff; |
| 114 |
fseek_nofail (ifp, 540 + lowbits*raw_height*raw_width/4, SEEK_SET); |
| 115 |
zero_after_ff = 1; |
| 116 |
getbits(ifp, -1); |
| 117 |
for (row = 0; row < raw_height; row += 8) { |
| 118 |
for (block=0; block < raw_width >> 3; block++) { |
| 119 |
memset (diffbuf, 0, sizeof diffbuf); |
| 120 |
decode = first_decode; |
| 121 |
for (i=0; i < 64; i++ ) { |
| 122 |
for (dindex=decode; dindex->branch[0]; ) |
| 123 |
dindex = dindex->branch[getbits(ifp, 1)]; |
| 124 |
leaf = dindex->leaf; |
| 125 |
decode = second_decode; |
| 126 |
if (leaf == 0 && i) break; |
| 127 |
if (leaf == 0xff) continue; |
| 128 |
i += leaf >> 4; |
| 129 |
len = leaf & 15; |
| 130 |
if (len == 0) continue; |
| 131 |
diff = getbits(ifp, len); |
| 132 |
if ((diff & (1 << (len-1))) == 0) |
| 133 |
diff -= (1 << len) - 1; |
| 134 |
if (i < 64) diffbuf[i] = diff; |
| 135 |
} |
| 136 |
diffbuf[0] += carry; |
| 137 |
carry = diffbuf[0]; |
| 138 |
for (i=0; i < 64; i++ ) { |
| 139 |
if (pnum++ % raw_width == 0) |
| 140 |
base[0] = base[1] = 512; |
| 141 |
pixel[(block << 6) + i] = ( base[i & 1] += diffbuf[i] ); |
| 142 |
} |
| 143 |
} |
| 144 |
if (lowbits) { |
| 145 |
save = ftell_nofail(ifp); |
| 146 |
fseek_nofail (ifp, 26 + row*raw_width/4, SEEK_SET); |
| 147 |
for (prow=pixel, i=0; i < raw_width*2; i++) { |
| 148 |
c = fgetc_nofail(ifp); |
| 149 |
for (r=0; r < 8; r+=2, prow++) { |
| 150 |
val = (*prow << 2) + ((c >> r) & 3); |
| 151 |
if (raw_width == 2672 && val < 512) val += 2; |
| 152 |
*prow = val; |
| 153 |
} |
| 154 |
} |
| 155 |
fseek_nofail (ifp, save, SEEK_SET); |
| 156 |
} |
| 157 |
for (r=0; r < 8; r++) { |
| 158 |
irow = row - top_margin + r; |
| 159 |
if (irow >= height) continue; |
| 160 |
for (col = 0; col < raw_width; col++) { |
| 161 |
icol = col - left_margin; |
| 162 |
if (icol < width) |
| 163 |
BAYER(irow,icol) = pixel[r*raw_width+col]; |
| 164 |
else |
| 165 |
black += pixel[r*raw_width+col]; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
free (pixel); |
| 170 |
if (raw_width > width) |
| 171 |
black /= (raw_width - width) * height; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|