| 1 |
#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */ |
| 2 |
#define _BSD_SOURCE /* Make sure string.h containst strcasecmp() */ |
| 3 |
#include <stdlib.h> |
| 4 |
#include <string.h> |
| 5 |
#include <limits.h> |
| 6 |
|
| 7 |
#include "pm.h" |
| 8 |
|
| 9 |
#include "global_variables.h" |
| 10 |
#include "util.h" |
| 11 |
#include "decode.h" |
| 12 |
#include "bayer.h" |
| 13 |
#include "stdio_nofail.h" |
| 14 |
|
| 15 |
#include "ljpeg.h" |
| 16 |
|
| 17 |
|
| 18 |
/* |
| 19 |
Not a full implementation of Lossless JPEG, just |
| 20 |
enough to decode Canon, Kodak and Adobe DNG images. |
| 21 |
*/ |
| 22 |
|
| 23 |
int |
| 24 |
ljpeg_start(FILE * const ifP, |
| 25 |
struct jhead * const jhP) { |
| 26 |
|
| 27 |
int i, tag; |
| 28 |
unsigned char data[256], *dp; |
| 29 |
|
| 30 |
init_decoder(); |
| 31 |
for (i=0; i < 4; i++) |
| 32 |
jhP->huff[i] = free_decode; |
| 33 |
fread_nofail (data, 2, 1, ifP); |
| 34 |
if (data[0] != 0xff || data[1] != 0xd8) return 0; |
| 35 |
do { |
| 36 |
unsigned int len; |
| 37 |
|
| 38 |
fread_nofail (data, 2, 2, ifP); |
| 39 |
tag = data[0] << 8 | data[1]; |
| 40 |
len = data[2] << 8 | data[3]; |
| 41 |
|
| 42 |
if (len < 2) |
| 43 |
pm_error("Length field is %u; must be at least 2", len); |
| 44 |
else { |
| 45 |
unsigned int const dataLen = len - 2; |
| 46 |
|
| 47 |
if (tag <= 0xff00 || dataLen > 255) return 0; |
| 48 |
fread_nofail (data, 1, dataLen, ifP); |
| 49 |
switch (tag) { |
| 50 |
case 0xffc3: |
| 51 |
jhP->bits = data[0]; |
| 52 |
jhP->high = data[1] << 8 | data[2]; |
| 53 |
jhP->wide = data[3] << 8 | data[4]; |
| 54 |
jhP->clrs = data[5]; |
| 55 |
break; |
| 56 |
case 0xffc4: |
| 57 |
for (dp = data; dp < data + dataLen && *dp < 4; ) { |
| 58 |
jhP->huff[*dp] = free_decode; |
| 59 |
dp = make_decoder (++dp, 0); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
} while (tag != 0xffda); |
| 64 |
jhP->row = calloc (jhP->wide*jhP->clrs, 2); |
| 65 |
if (jhP->row == NULL) |
| 66 |
pm_error("Out of memory in ljpeg_start()"); |
| 67 |
for (i=0; i < 4; i++) |
| 68 |
jhP->vpred[i] = 1 << (jhP->bits-1); |
| 69 |
zero_after_ff = 1; |
| 70 |
getbits(ifP, -1); |
| 71 |
return 1; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
int |
| 77 |
ljpeg_diff(FILE * const ifP, |
| 78 |
struct decode * const dindexHeadP) { |
| 79 |
|
| 80 |
int len; |
| 81 |
int diff; |
| 82 |
struct decode * dindexP; |
| 83 |
|
| 84 |
for (dindexP = dindexHeadP; dindexP->branch[0]; ) |
| 85 |
dindexP = dindexP->branch[getbits(ifP, 1)]; |
| 86 |
|
| 87 |
diff = getbits(ifP, len = dindexP->leaf); |
| 88 |
|
| 89 |
if ((diff & (1 << (len-1))) == 0) |
| 90 |
diff -= (1 << len) - 1; |
| 91 |
|
| 92 |
return diff; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
void |
| 98 |
ljpeg_row(FILE * const ifP, |
| 99 |
struct jhead * const jhP) { |
| 100 |
|
| 101 |
int col, c, diff; |
| 102 |
unsigned short *outp=jhP->row; |
| 103 |
|
| 104 |
for (col=0; col < jhP->wide; col++) |
| 105 |
for (c=0; c < jhP->clrs; c++) { |
| 106 |
diff = ljpeg_diff(ifP, jhP->huff[c]); |
| 107 |
*outp = col ? outp[-jhP->clrs]+diff : (jhP->vpred[c] += diff); |
| 108 |
outp++; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
void |
| 115 |
lossless_jpeg_load_raw(Image const image) { |
| 116 |
|
| 117 |
int jwide, jrow, jcol, val, jidx, i, row, col; |
| 118 |
struct jhead jh; |
| 119 |
int min=INT_MAX; |
| 120 |
|
| 121 |
if (!ljpeg_start (ifp, &jh)) return; |
| 122 |
jwide = jh.wide * jh.clrs; |
| 123 |
|
| 124 |
for (jrow=0; jrow < jh.high; jrow++) { |
| 125 |
ljpeg_row (ifp, &jh); |
| 126 |
for (jcol=0; jcol < jwide; jcol++) { |
| 127 |
val = curve[jh.row[jcol]]; |
| 128 |
jidx = jrow*jwide + jcol; |
| 129 |
if (raw_width == 5108) { |
| 130 |
i = jidx / (1680*jh.high); |
| 131 |
if (i < 2) { |
| 132 |
row = jidx / 1680 % jh.high; |
| 133 |
col = jidx % 1680 + i*1680; |
| 134 |
} else { |
| 135 |
jidx -= 2*1680*jh.high; |
| 136 |
row = jidx / 1748; |
| 137 |
col = jidx % 1748 + 2*1680; |
| 138 |
} |
| 139 |
} else if (raw_width == 3516) { |
| 140 |
row = jidx / 1758; |
| 141 |
col = jidx % 1758; |
| 142 |
if (row >= raw_height) { |
| 143 |
row -= raw_height; |
| 144 |
col += 1758; |
| 145 |
} |
| 146 |
} else { |
| 147 |
row = jidx / raw_width; |
| 148 |
col = jidx % raw_width; |
| 149 |
} |
| 150 |
if ((unsigned) (row-top_margin) >= height) continue; |
| 151 |
if ((unsigned) (col-left_margin) < width) { |
| 152 |
BAYER(row-top_margin,col-left_margin) = val; |
| 153 |
if (min > val) min = val; |
| 154 |
} else |
| 155 |
black += val; |
| 156 |
} |
| 157 |
} |
| 158 |
free (jh.row); |
| 159 |
if (raw_width > width) |
| 160 |
black /= (raw_width - width) * height; |
| 161 |
if (!strcasecmp(make,"KODAK")) |
| 162 |
black = min; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|