| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#define _DEFAULT_SOURCE 1 |
| 11 |
#define _BSD_SOURCE 1 |
| 12 |
#define _XOPEN_SOURCE 500 |
| 13 |
|
| 14 |
|
| 15 |
#include "pm_config.h" |
| 16 |
|
| 17 |
#include <ctype.h> |
| 18 |
#include <unistd.h> |
| 19 |
#include <errno.h> |
| 20 |
#include <assert.h> |
| 21 |
#include <fcntl.h> |
| 22 |
#include <float.h> |
| 23 |
#include <limits.h> |
| 24 |
#include <math.h> |
| 25 |
#include <time.h> |
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
|
| 30 |
#ifdef HAVE_IO_H |
| 31 |
#include <io.h> |
| 32 |
#endif |
| 33 |
#if !MSVCRT |
| 34 |
#include <unistd.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
#include "pm_c_util.h" |
| 38 |
#include "mallocvar.h" |
| 39 |
#include "shhopt.h" |
| 40 |
#include "pam.h" |
| 41 |
|
| 42 |
#include "global_variables.h" |
| 43 |
#include "cameratopam.h" |
| 44 |
#include "util.h" |
| 45 |
#include "decode.h" |
| 46 |
#include "identify.h" |
| 47 |
#include "bayer.h" |
| 48 |
#include "foveon.h" |
| 49 |
#include "dng.h" |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
FILE * ifp; |
| 57 |
short order; |
| 58 |
char make[64], model[70], model2[64], *meta_data; |
| 59 |
time_t timestamp; |
| 60 |
int data_offset, meta_offset, meta_length; |
| 61 |
int tiff_data_compression, kodak_data_compression; |
| 62 |
int raw_height, raw_width, top_margin, left_margin; |
| 63 |
int height, width, fuji_width, colors, tiff_samples; |
| 64 |
int black, maximum, clip_max; |
| 65 |
int iheight, iwidth, shrink; |
| 66 |
int is_dng, is_canon, is_foveon, use_coeff, use_gamma; |
| 67 |
int flip, xmag, ymag; |
| 68 |
int zero_after_ff; |
| 69 |
unsigned filters; |
| 70 |
unsigned short white[8][8]; |
| 71 |
unsigned short curve[0x1000]; |
| 72 |
int fuji_secondary; |
| 73 |
float cam_mul[4], coeff[3][4]; |
| 74 |
float pre_mul[4]; |
| 75 |
int histogram[3][0x2000]; |
| 76 |
jmp_buf failure; |
| 77 |
int use_secondary; |
| 78 |
bool verbose; |
| 79 |
|
| 80 |
#define CLASS |
| 81 |
|
| 82 |
#define FORC3 for (c=0; c < 3; c++) |
| 83 |
#define FORC4 for (c=0; c < colors; c++) |
| 84 |
|
| 85 |
static void CLASS merror (const void *ptr, const char *where) |
| 86 |
{ |
| 87 |
if (ptr == NULL) |
| 88 |
pm_error ("Out of memory in %s", where); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
struct CmdlineInfo { |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
const char * inputFileName; |
| 98 |
float bright; |
| 99 |
float red_scale; |
| 100 |
float blue_scale; |
| 101 |
const char * profile; |
| 102 |
unsigned int identify_only; |
| 103 |
unsigned int verbose; |
| 104 |
unsigned int half_size; |
| 105 |
unsigned int four_color_rgb; |
| 106 |
unsigned int document_mode; |
| 107 |
unsigned int quick_interpolate; |
| 108 |
unsigned int use_auto_wb; |
| 109 |
unsigned int use_camera_wb; |
| 110 |
unsigned int use_camera_rgb; |
| 111 |
unsigned int use_secondary; |
| 112 |
unsigned int no_clip_color; |
| 113 |
unsigned int linear; |
| 114 |
}; |
| 115 |
|
| 116 |
|
| 117 |
static struct CmdlineInfo cmdline; |
| 118 |
|
| 119 |
static void |
| 120 |
parseCommandLine(int argc, char ** argv, |
| 121 |
struct CmdlineInfo *cmdlineP) { |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
optStruct3 opt; |
| 129 |
optEntry *option_def; |
| 130 |
unsigned int option_def_index; |
| 131 |
unsigned int brightSpec, red_scaleSpec, blue_scaleSpec, |
| 132 |
profileSpec; |
| 133 |
|
| 134 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 135 |
|
| 136 |
opt.opt_table = option_def; |
| 137 |
opt.short_allowed = FALSE; |
| 138 |
opt.allowNegNum = FALSE; |
| 139 |
|
| 140 |
option_def_index = 0; |
| 141 |
OPTENT3(0, "bright", |
| 142 |
OPT_FLOAT, &cmdlineP->bright, &brightSpec, 0); |
| 143 |
OPTENT3(0, "red_scale", |
| 144 |
OPT_FLOAT, &cmdlineP->red_scale, &red_scaleSpec, 0); |
| 145 |
OPTENT3(0, "blue_scale", |
| 146 |
OPT_FLOAT, &cmdlineP->blue_scale, &blue_scaleSpec, 0); |
| 147 |
OPTENT3(0, "profile", |
| 148 |
OPT_STRING, &cmdlineP->profile, &profileSpec, 0); |
| 149 |
OPTENT3(0, "identify_only", |
| 150 |
OPT_FLAG, NULL, &cmdlineP->identify_only, 0); |
| 151 |
OPTENT3(0, "verbose", |
| 152 |
OPT_FLAG, NULL, &cmdlineP->verbose, 0); |
| 153 |
OPTENT3(0, "half_size", |
| 154 |
OPT_FLAG, NULL, &cmdlineP->half_size, 0); |
| 155 |
OPTENT3(0, "four_color_rgb", |
| 156 |
OPT_FLAG, NULL, &cmdlineP->four_color_rgb, 0); |
| 157 |
OPTENT3(0, "document_mode", |
| 158 |
OPT_FLAG, NULL, &cmdlineP->document_mode, 0); |
| 159 |
OPTENT3(0, "quick_interpolate", |
| 160 |
OPT_FLAG, NULL, &cmdlineP->quick_interpolate, 0); |
| 161 |
OPTENT3(0, "balance_auto", |
| 162 |
OPT_FLAG, NULL, &cmdlineP->use_auto_wb, 0); |
| 163 |
OPTENT3(0, "balance_camera", |
| 164 |
OPT_FLAG, NULL, &cmdlineP->use_camera_wb, 0); |
| 165 |
OPTENT3(0, "use_secondary", |
| 166 |
OPT_FLAG, NULL, &cmdlineP->use_secondary, 0); |
| 167 |
OPTENT3(0, "no_clip_color", |
| 168 |
OPT_FLAG, NULL, &cmdlineP->no_clip_color, 0); |
| 169 |
OPTENT3(0, "rgb", |
| 170 |
OPT_FLAG, NULL, &cmdlineP->use_camera_rgb, 0); |
| 171 |
OPTENT3(0, "linear", |
| 172 |
OPT_FLAG, NULL, &cmdlineP->linear, 0); |
| 173 |
|
| 174 |
pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0); |
| 175 |
|
| 176 |
if (!brightSpec) |
| 177 |
cmdlineP->bright = 1.0; |
| 178 |
if (!red_scaleSpec) |
| 179 |
cmdlineP->red_scale = 1.0; |
| 180 |
if (!blue_scaleSpec) |
| 181 |
cmdlineP->blue_scale = 1.0; |
| 182 |
if (!profileSpec) |
| 183 |
cmdlineP->profile = NULL; |
| 184 |
|
| 185 |
|
| 186 |
if (argc - 1 == 0) |
| 187 |
cmdlineP->inputFileName = strdup("-"); |
| 188 |
else if (argc - 1 == 1) |
| 189 |
cmdlineP->inputFileName = strdup(argv[1]); |
| 190 |
else |
| 191 |
pm_error("Too many arguments. The only argument accepted " |
| 192 |
"is the input file name"); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
static void CLASS |
| 198 |
fixBadPixels(Image const image) { |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
if (filters) { |
| 204 |
FILE *fp; |
| 205 |
char *fname, *cp, line[128]; |
| 206 |
int len, time, row, col, rad, tot, n, fixed=0; |
| 207 |
|
| 208 |
for (len=16 ; ; len *= 2) { |
| 209 |
fname = malloc (len); |
| 210 |
if (!fname) return; |
| 211 |
if (getcwd (fname, len-12)) |
| 212 |
break; |
| 213 |
free (fname); |
| 214 |
if (errno != ERANGE) |
| 215 |
return; |
| 216 |
} |
| 217 |
#if MSVCRT |
| 218 |
if (fname[1] == ':') |
| 219 |
memmove (fname, fname+2, len-2); |
| 220 |
for (cp=fname; *cp; cp++) |
| 221 |
if (*cp == '\\') *cp = '/'; |
| 222 |
#endif |
| 223 |
cp = fname + strlen(fname); |
| 224 |
if (cp[-1] == '/') |
| 225 |
--cp; |
| 226 |
fp = NULL; |
| 227 |
while (*fname == '/') { |
| 228 |
strcpy (cp, "/.badpixels"); |
| 229 |
fp = fopen (fname, "r"); |
| 230 |
if (fp) |
| 231 |
break; |
| 232 |
if (cp == fname) |
| 233 |
break; |
| 234 |
while (*--cp != '/'); |
| 235 |
} |
| 236 |
free (fname); |
| 237 |
if (fp) { |
| 238 |
while (fgets (line, 128, fp)) { |
| 239 |
char * cp; |
| 240 |
cp = strchr (line, '#'); |
| 241 |
if (cp) *cp = 0; |
| 242 |
if (sscanf (line, "%d %d %d", &col, &row, &time) != 3) |
| 243 |
continue; |
| 244 |
if ((unsigned) col >= width || (unsigned) row >= height) |
| 245 |
continue; |
| 246 |
if (time > timestamp) continue; |
| 247 |
for (tot=n=0, rad=1; rad < 3 && n==0; rad++) { |
| 248 |
unsigned int r; |
| 249 |
for (r = row-rad; r <= row+rad; ++r) { |
| 250 |
unsigned int c; |
| 251 |
for (c = col-rad; c <= col+rad; ++c) { |
| 252 |
if ((unsigned) r < height && |
| 253 |
(unsigned) c < width && |
| 254 |
(r != row || c != col) && |
| 255 |
FC(r,c) == FC(row,col)) { |
| 256 |
tot += BAYER(r,c); |
| 257 |
++n; |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
BAYER(row,col) = tot/n; |
| 263 |
if (cmdline.verbose) { |
| 264 |
if (!fixed++) |
| 265 |
pm_message ("Fixed bad pixels at: %d,%d", col, row); |
| 266 |
} |
| 267 |
} |
| 268 |
fclose (fp); |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
static void CLASS |
| 276 |
scaleColors(Image const image) { |
| 277 |
|
| 278 |
int row; |
| 279 |
int c; |
| 280 |
int val; |
| 281 |
int shift; |
| 282 |
int min[4], max[4], count[4]; |
| 283 |
double sum[4], dmin; |
| 284 |
int scaleMax; |
| 285 |
|
| 286 |
scaleMax = maximum - black; |
| 287 |
if (cmdline.use_auto_wb || (cmdline.use_camera_wb && camera_red == -1)) { |
| 288 |
unsigned int row; |
| 289 |
FORC4 min [c] = INT_MAX; |
| 290 |
FORC4 max [c] = 0; |
| 291 |
FORC4 count[c] = 0; |
| 292 |
FORC4 sum [c] = 0; |
| 293 |
for (row = 0; row < height; ++row) { |
| 294 |
unsigned int col; |
| 295 |
for (col = 0; col < width; ++col) { |
| 296 |
FORC4 { |
| 297 |
int val; |
| 298 |
val = image[row*width+col][c]; |
| 299 |
if (val != 0) { |
| 300 |
if (min[c] > val) |
| 301 |
min[c] = val; |
| 302 |
if (max[c] < val) |
| 303 |
max[c] = val; |
| 304 |
val -= black; |
| 305 |
if (val <= scaleMax-25) { |
| 306 |
sum [c] += MAX(0, val); |
| 307 |
count[c] += 1; |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
FORC4 pre_mul[c] = count[c] / sum[c]; |
| 314 |
} |
| 315 |
if (cmdline.use_camera_wb && camera_red != -1) { |
| 316 |
unsigned int row; |
| 317 |
FORC4 count[c] = sum[c] = 0; |
| 318 |
for (row = 0; row < 8; ++row) { |
| 319 |
unsigned int col; |
| 320 |
for (col = 0; col < 8; ++col) { |
| 321 |
c = FC(row,col); |
| 322 |
if ((val = white[row][col] - black) > 0) |
| 323 |
sum[c] += val; |
| 324 |
++count[c]; |
| 325 |
} |
| 326 |
} |
| 327 |
val = 1; |
| 328 |
FORC4 if (sum[c] == 0) val = 0; |
| 329 |
if (val) |
| 330 |
FORC4 pre_mul[c] = count[c] / sum[c]; |
| 331 |
else if (camera_red && camera_blue) |
| 332 |
memcpy(pre_mul, cam_mul, sizeof pre_mul); |
| 333 |
else |
| 334 |
pm_message ("Cannot use camera white balance."); |
| 335 |
} |
| 336 |
if (!use_coeff) { |
| 337 |
pre_mul[0] *= cmdline.red_scale; |
| 338 |
pre_mul[2] *= cmdline.blue_scale; |
| 339 |
} |
| 340 |
dmin = DBL_MAX; |
| 341 |
FORC4 if (dmin > pre_mul[c]) |
| 342 |
dmin = pre_mul[c]; |
| 343 |
FORC4 pre_mul[c] /= dmin; |
| 344 |
|
| 345 |
for (shift = 0; scaleMax << shift < 0x8000; ++shift); |
| 346 |
|
| 347 |
FORC4 pre_mul[c] *= 1 << shift; |
| 348 |
scaleMax <<= shift; |
| 349 |
|
| 350 |
if (cmdline.linear || cmdline.bright < 1) { |
| 351 |
scaleMax = MIN(0xffff, scaleMax * cmdline.bright); |
| 352 |
FORC4 pre_mul[c] *= cmdline.bright; |
| 353 |
} |
| 354 |
if (cmdline.verbose) { |
| 355 |
fprintf(stderr, "Scaling with black=%d, ", black); |
| 356 |
fprintf(stderr, "pre_mul[] = "); |
| 357 |
FORC4 fprintf (stderr, " %f", pre_mul[c]); |
| 358 |
fprintf(stderr, "\n"); |
| 359 |
} |
| 360 |
clip_max = cmdline.no_clip_color ? 0xffff : scaleMax; |
| 361 |
for (row = 0; row < height; ++row) { |
| 362 |
unsigned int col; |
| 363 |
for (col = 0; col < width; ++col) { |
| 364 |
unsigned int c; |
| 365 |
for (c = 0; c < colors; ++c) { |
| 366 |
int val; |
| 367 |
val = image[row*width+col][c]; |
| 368 |
if (val != 0) { |
| 369 |
val -= black; |
| 370 |
val *= pre_mul[c]; |
| 371 |
image[row*width+col][c] = MAX(0, MIN(clip_max, val)); |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
static void CLASS |
| 381 |
vngInterpolate(Image const image) { |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
static const signed char *cp, terms[] = { |
| 393 |
-2,-2,+0,-1,0,(char)0x01, -2,-2,+0,+0,1,(char)0x01, -2,-1,-1,+0,0,(char)0x01, |
| 394 |
-2,-1,+0,-1,0,(char)0x02, -2,-1,+0,+0,0,(char)0x03, -2,-1,+0,+1,1,(char)0x01, |
| 395 |
-2,+0,+0,-1,0,(char)0x06, -2,+0,+0,+0,1,(char)0x02, -2,+0,+0,+1,0,(char)0x03, |
| 396 |
-2,+1,-1,+0,0,(char)0x04, -2,+1,+0,-1,1,(char)0x04, -2,+1,+0,+0,0,(char)0x06, |
| 397 |
-2,+1,+0,+1,0,(char)0x02, -2,+2,+0,+0,1,(char)0x04, -2,+2,+0,+1,0,(char)0x04, |
| 398 |
-1,-2,-1,+0,0,(char)0x80, -1,-2,+0,-1,0,(char)0x01, -1,-2,+1,-1,0,(char)0x01, |
| 399 |
-1,-2,+1,+0,1,(char)0x01, -1,-1,-1,+1,0,(char)0x88, -1,-1,+1,-2,0,(char)0x40, |
| 400 |
-1,-1,+1,-1,0,(char)0x22, -1,-1,+1,+0,0,(char)0x33, -1,-1,+1,+1,1,(char)0x11, |
| 401 |
-1,+0,-1,+2,0,(char)0x08, -1,+0,+0,-1,0,(char)0x44, -1,+0,+0,+1,0,(char)0x11, |
| 402 |
-1,+0,+1,-2,1,(char)0x40, -1,+0,+1,-1,0,(char)0x66, -1,+0,+1,+0,1,(char)0x22, |
| 403 |
-1,+0,+1,+1,0,(char)0x33, -1,+0,+1,+2,1,(char)0x10, -1,+1,+1,-1,1,(char)0x44, |
| 404 |
-1,+1,+1,+0,0,(char)0x66, -1,+1,+1,+1,0,(char)0x22, -1,+1,+1,+2,0,(char)0x10, |
| 405 |
-1,+2,+0,+1,0,(char)0x04, -1,+2,+1,+0,1,(char)0x04, -1,+2,+1,+1,0,(char)0x04, |
| 406 |
+0,-2,+0,+0,1,(char)0x80, +0,-1,+0,+1,1,(char)0x88, +0,-1,+1,-2,0,(char)0x40, |
| 407 |
+0,-1,+1,+0,0,(char)0x11, +0,-1,+2,-2,0,(char)0x40, +0,-1,+2,-1,0,(char)0x20, |
| 408 |
+0,-1,+2,+0,0,(char)0x30, +0,-1,+2,+1,1,(char)0x10, +0,+0,+0,+2,1,(char)0x08, |
| 409 |
+0,+0,+2,-2,1,(char)0x40, +0,+0,+2,-1,0,(char)0x60, +0,+0,+2,+0,1,(char)0x20, |
| 410 |
+0,+0,+2,+1,0,(char)0x30, +0,+0,+2,+2,1,(char)0x10, +0,+1,+1,+0,0,(char)0x44, |
| 411 |
+0,+1,+1,+2,0,(char)0x10, +0,+1,+2,-1,1,(char)0x40, +0,+1,+2,+0,0,(char)0x60, |
| 412 |
+0,+1,+2,+1,0,(char)0x20, +0,+1,+2,+2,0,(char)0x10, +1,-2,+1,+0,0,(char)0x80, |
| 413 |
+1,-1,+1,+1,0,(char)0x88, +1,+0,+1,+2,0,(char)0x08, +1,+0,+2,-1,0,(char)0x40, |
| 414 |
+1,+0,+2,+1,0,(char)0x10 |
| 415 |
}, chood[] = { -1,-1, -1,0, -1,+1, 0,+1, +1,+1, +1,0, +1,-1, 0,-1 }; |
| 416 |
unsigned short (*brow[5])[4], *pix; |
| 417 |
int code[8][2][320], *ip, gval[8], gmin, gmax, sum[4]; |
| 418 |
int row, col, shift, x, y, x1, x2, y1, y2, t, weight, grads, color, diag; |
| 419 |
int g, diff, thold, num, c; |
| 420 |
|
| 421 |
for (row=0; row < 8; row++) { |
| 422 |
for (col=1; col < 3; col++) { |
| 423 |
ip = code[row][col & 1]; |
| 424 |
memset (sum, 0, sizeof sum); |
| 425 |
for (y=-1; y <= 1; y++) |
| 426 |
for (x=-1; x <= 1; x++) { |
| 427 |
shift = (y==0) + (x==0); |
| 428 |
if (shift == 2) continue; |
| 429 |
color = FC(row+y,col+x); |
| 430 |
*ip++ = (width*y + x)*4 + color; |
| 431 |
*ip++ = shift; |
| 432 |
*ip++ = color; |
| 433 |
sum[color] += 1 << shift; |
| 434 |
} |
| 435 |
FORC4 |
| 436 |
if (c != FC(row,col)) { |
| 437 |
*ip++ = c; |
| 438 |
*ip++ = sum[c]; |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
for (row=1; row < height-1; row++) { |
| 443 |
for (col=1; col < width-1; col++) { |
| 444 |
pix = image[row*width+col]; |
| 445 |
ip = code[row & 7][col & 1]; |
| 446 |
memset (sum, 0, sizeof sum); |
| 447 |
for (g=8; g--; ) { |
| 448 |
diff = pix[*ip++]; |
| 449 |
diff <<= *ip++; |
| 450 |
sum[*ip++] += diff; |
| 451 |
} |
| 452 |
for (g=colors; --g; ) { |
| 453 |
c = *ip++; |
| 454 |
pix[c] = sum[c] / *ip++; |
| 455 |
} |
| 456 |
} |
| 457 |
} |
| 458 |
if (cmdline.quick_interpolate) |
| 459 |
return; |
| 460 |
|
| 461 |
for (row=0; row < 8; row++) { |
| 462 |
for (col=0; col < 2; col++) { |
| 463 |
ip = code[row][col]; |
| 464 |
for (cp=terms, t=0; t < 64; t++) { |
| 465 |
y1 = *cp++; x1 = *cp++; |
| 466 |
y2 = *cp++; x2 = *cp++; |
| 467 |
weight = *cp++; |
| 468 |
grads = *cp++; |
| 469 |
color = FC(row+y1,col+x1); |
| 470 |
if (FC(row+y2,col+x2) != color) continue; |
| 471 |
diag = |
| 472 |
(FC(row,col+1) == color && FC(row+1,col) == color) ? 2:1; |
| 473 |
if (abs(y1-y2) == diag && abs(x1-x2) == diag) continue; |
| 474 |
*ip++ = (y1*width + x1)*4 + color; |
| 475 |
*ip++ = (y2*width + x2)*4 + color; |
| 476 |
*ip++ = weight; |
| 477 |
for (g=0; g < 8; g++) |
| 478 |
if (grads & 1<<g) *ip++ = g; |
| 479 |
*ip++ = -1; |
| 480 |
} |
| 481 |
*ip++ = INT_MAX; |
| 482 |
for (cp=chood, g=0; g < 8; g++) { |
| 483 |
y = *cp++; x = *cp++; |
| 484 |
*ip++ = (y*width + x) * 4; |
| 485 |
color = FC(row,col); |
| 486 |
if (FC(row+y,col+x) != color && FC(row+y*2,col+x*2) == color) |
| 487 |
*ip++ = (y*width + x) * 8 + color; |
| 488 |
else |
| 489 |
*ip++ = 0; |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
brow[4] = calloc (width*3, sizeof **brow); |
| 494 |
merror (brow[4], "vngInterpolate()"); |
| 495 |
for (row=0; row < 3; row++) |
| 496 |
brow[row] = brow[4] + row*width; |
| 497 |
for (row=2; row < height-2; row++) { |
| 498 |
for (col=2; col < width-2; col++) { |
| 499 |
pix = image[row*width+col]; |
| 500 |
ip = code[row & 7][col & 1]; |
| 501 |
memset (gval, 0, sizeof gval); |
| 502 |
while ((g = ip[0]) != INT_MAX) { |
| 503 |
num = (diff = pix[g] - pix[ip[1]]) >> 31; |
| 504 |
gval[ip[3]] += (diff = ((diff ^ num) - num) << ip[2]); |
| 505 |
ip += 5; |
| 506 |
if ((g = ip[-1]) == -1) continue; |
| 507 |
gval[g] += diff; |
| 508 |
while ((g = *ip++) != -1) |
| 509 |
gval[g] += diff; |
| 510 |
} |
| 511 |
ip++; |
| 512 |
gmin = gmax = gval[0]; |
| 513 |
for (g=1; g < 8; g++) { |
| 514 |
if (gmin > gval[g]) gmin = gval[g]; |
| 515 |
if (gmax < gval[g]) gmax = gval[g]; |
| 516 |
} |
| 517 |
if (gmax == 0) { |
| 518 |
memcpy (brow[2][col], pix, sizeof *image); |
| 519 |
continue; |
| 520 |
} |
| 521 |
thold = gmin + (gmax >> 1); |
| 522 |
memset (sum, 0, sizeof sum); |
| 523 |
color = FC(row,col); |
| 524 |
for (num=g=0; g < 8; g++,ip+=2) { |
| 525 |
if (gval[g] <= thold) { |
| 526 |
FORC4 |
| 527 |
if (c == color && ip[1]) |
| 528 |
sum[c] += (pix[c] + pix[ip[1]]) >> 1; |
| 529 |
else |
| 530 |
sum[c] += pix[ip[0] + c]; |
| 531 |
num++; |
| 532 |
} |
| 533 |
} |
| 534 |
FORC4 { |
| 535 |
t = pix[color]; |
| 536 |
if (c != color) { |
| 537 |
t += (sum[c] - sum[color])/num; |
| 538 |
if (t < 0) t = 0; |
| 539 |
if (t > clip_max) t = clip_max; |
| 540 |
} |
| 541 |
brow[2][col][c] = t; |
| 542 |
} |
| 543 |
} |
| 544 |
if (row > 3) |
| 545 |
memcpy(image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image); |
| 546 |
for (g=0; g < 4; g++) |
| 547 |
brow[(g-1) & 3] = brow[g]; |
| 548 |
} |
| 549 |
memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image); |
| 550 |
memcpy (image[(row-1)*width+2], brow[1]+2, (width-4)*sizeof *image); |
| 551 |
free (brow[4]); |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
static void CLASS |
| 557 |
convertToRgb(Image const image, |
| 558 |
unsigned int const trim) { |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
unsigned int row; |
| 565 |
unsigned int c; |
| 566 |
float rgb[3]; |
| 567 |
|
| 568 |
c = 0; |
| 569 |
|
| 570 |
if (cmdline.document_mode) |
| 571 |
colors = 1; |
| 572 |
|
| 573 |
memset(histogram, 0, sizeof histogram); |
| 574 |
|
| 575 |
for (row = 0 + trim; row < height - trim; ++row) { |
| 576 |
unsigned int col; |
| 577 |
for (col = 0 + trim; col < width - trim; ++col) { |
| 578 |
unsigned short * const img = image[row*width+col]; |
| 579 |
|
| 580 |
if (cmdline.document_mode) |
| 581 |
c = FC(row,col); |
| 582 |
|
| 583 |
if (colors == 4 && !use_coeff) |
| 584 |
|
| 585 |
img[1] = (img[1] + img[3]) / 2; |
| 586 |
|
| 587 |
if (colors == 1) { |
| 588 |
|
| 589 |
unsigned int i; |
| 590 |
for (i = 0; i < 3; ++i) |
| 591 |
rgb[i] = img[c]; |
| 592 |
} else if (use_coeff) { |
| 593 |
|
| 594 |
unsigned int i; |
| 595 |
for (i = 0; i < 3; ++i) { |
| 596 |
unsigned int j; |
| 597 |
for (j = 0, rgb[i]= 0; j < colors; ++j) |
| 598 |
rgb[i] += img[j] * coeff[i][j]; |
| 599 |
} |
| 600 |
} else { |
| 601 |
|
| 602 |
unsigned int i; |
| 603 |
for (i = 0; i < 3; ++i) |
| 604 |
rgb[i] = img[i]; |
| 605 |
} |
| 606 |
{ |
| 607 |
unsigned int i; |
| 608 |
for (i = 0; i < 3; ++i) |
| 609 |
img[i] = MIN(clip_max, MAX(0, rgb[i])); |
| 610 |
} |
| 611 |
{ |
| 612 |
unsigned int i; |
| 613 |
for (i = 0; i < 3; ++i) |
| 614 |
++histogram[i][img[i] >> 3]; |
| 615 |
} |
| 616 |
} |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
static void CLASS |
| 623 |
fujiRotate(Image * const imageP) { |
| 624 |
|
| 625 |
int wide; |
| 626 |
int high; |
| 627 |
unsigned int row; |
| 628 |
double step; |
| 629 |
float r, c, fr, fc; |
| 630 |
unsigned short (*newImage)[4]; |
| 631 |
unsigned short (*pix)[4]; |
| 632 |
|
| 633 |
if (fuji_width > 0) { |
| 634 |
if (cmdline.verbose) |
| 635 |
pm_message ("Rotating image 45 degrees..."); |
| 636 |
|
| 637 |
fuji_width = (fuji_width + shrink) >> shrink; |
| 638 |
step = sqrt(0.5); |
| 639 |
wide = fuji_width / step; |
| 640 |
high = (height - fuji_width) / step; |
| 641 |
newImage = calloc (wide*high, sizeof *newImage); |
| 642 |
merror (newImage, "fujiRotate()"); |
| 643 |
|
| 644 |
for (row = 0; row < high; ++row) { |
| 645 |
unsigned int col; |
| 646 |
for (col = 0; col < wide; ++col) { |
| 647 |
unsigned int ur = r = fuji_width + (row-col)*step; |
| 648 |
unsigned int uc = c = (row+col)*step; |
| 649 |
|
| 650 |
unsigned int i; |
| 651 |
|
| 652 |
if (ur > height-2 || uc > width-2) |
| 653 |
continue; |
| 654 |
|
| 655 |
fr = r - ur; |
| 656 |
fc = c - uc; |
| 657 |
pix = (*imageP) + ur * width + uc; |
| 658 |
|
| 659 |
for (i = 0; i < colors; ++i) { |
| 660 |
newImage[row*wide+col][i] = |
| 661 |
(pix[ 0][i]*(1-fc) + pix[ 1][i]*fc) * (1-fr) + |
| 662 |
(pix[width][i]*(1-fc) + pix[width+1][i]*fc) * fr; |
| 663 |
} |
| 664 |
} |
| 665 |
} |
| 666 |
free(*imageP); |
| 667 |
width = wide; |
| 668 |
height = high; |
| 669 |
*imageP = newImage; |
| 670 |
fuji_width = 0; |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
static void CLASS |
| 677 |
flipImage(Image const image) { |
| 678 |
|
| 679 |
unsigned *flag; |
| 680 |
int size, base, dest; |
| 681 |
|
| 682 |
struct imageCell { |
| 683 |
unsigned char contents[8]; |
| 684 |
}; |
| 685 |
struct imageCell * img; |
| 686 |
|
| 687 |
switch ((flip+3600) % 360) { |
| 688 |
case 270: flip = 0x5; break; |
| 689 |
case 180: flip = 0x3; break; |
| 690 |
case 90: flip = 0x6; |
| 691 |
} |
| 692 |
img = (struct imageCell *) image; |
| 693 |
size = height * width; |
| 694 |
flag = calloc ((size+31) >> 5, sizeof *flag); |
| 695 |
merror (flag, "flipImage()"); |
| 696 |
for (base = 0; base < size; ++base) { |
| 697 |
if (flag[base >> 5] & (1 << (base & 31))) { |
| 698 |
|
| 699 |
} else { |
| 700 |
struct imageCell const hold = img[base]; |
| 701 |
dest = base; |
| 702 |
while (true) { |
| 703 |
unsigned int col; |
| 704 |
unsigned int row; |
| 705 |
int next; |
| 706 |
if (flip & 0x4) { |
| 707 |
row = dest % height; |
| 708 |
col = dest / height; |
| 709 |
} else { |
| 710 |
row = dest / width; |
| 711 |
col = dest % width; |
| 712 |
} |
| 713 |
if (flip & 0x2) |
| 714 |
row = height - 1 - row; |
| 715 |
if (flip & 1) |
| 716 |
col = width - 1 - col; |
| 717 |
next = row * width + col; |
| 718 |
if (next == base) |
| 719 |
break; |
| 720 |
flag[next >> 5] |= 1 << (next & 31); |
| 721 |
img[dest] = img[next]; |
| 722 |
dest = next; |
| 723 |
} |
| 724 |
img[dest] = hold; |
| 725 |
} |
| 726 |
} |
| 727 |
free (flag); |
| 728 |
if (flip & 0x4) { |
| 729 |
int const oldHeight = height; |
| 730 |
int const oldYmag = ymag; |
| 731 |
|
| 732 |
height = width; |
| 733 |
width = oldHeight; |
| 734 |
ymag = xmag; |
| 735 |
xmag = oldYmag; |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
static void CLASS |
| 742 |
writePamLinear(FILE * const ofP, |
| 743 |
Image const image, |
| 744 |
unsigned int const trim) { |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
unsigned int row; |
| 749 |
|
| 750 |
struct pam pam; |
| 751 |
tuple * tuplerow; |
| 752 |
|
| 753 |
pam.size = sizeof(pam); |
| 754 |
pam.len = PAM_STRUCT_SIZE(tuple_type); |
| 755 |
pam.file = ofP; |
| 756 |
pam.width = width - trim - trim; |
| 757 |
pam.height = height - trim - trim; |
| 758 |
pam.depth = 3; |
| 759 |
pam.format = PAM_FORMAT; |
| 760 |
pam.maxval = MAX(maximum, 256); |
| 761 |
strcpy(pam.tuple_type, "RGB"); |
| 762 |
|
| 763 |
pnm_writepaminit(&pam); |
| 764 |
|
| 765 |
tuplerow = pnm_allocpamrow(&pam); |
| 766 |
|
| 767 |
for (row = 0 + trim; row < height - trim; ++row) { |
| 768 |
unsigned int col; |
| 769 |
for (col = 0 + trim; col < width - trim; ++col) { |
| 770 |
unsigned int const pamCol = col - trim; |
| 771 |
unsigned int plane; |
| 772 |
for (plane = 0; plane < 3; ++plane) |
| 773 |
tuplerow[pamCol][plane] = image[row*width+col][plane]; |
| 774 |
} |
| 775 |
pnm_writepamrow(&pam, tuplerow); |
| 776 |
} |
| 777 |
pnm_freepamrow(tuplerow); |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
static void CLASS |
| 783 |
writePamNonlinear(FILE * const ofP, |
| 784 |
Image const image, |
| 785 |
unsigned int const trim) { |
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
unsigned char lut[0x10000]; |
| 790 |
int perc; |
| 791 |
int c; |
| 792 |
int total; |
| 793 |
int i; |
| 794 |
unsigned int row; |
| 795 |
float white; |
| 796 |
float r; |
| 797 |
struct pam pam; |
| 798 |
tuple * tuplerow; |
| 799 |
|
| 800 |
white = 0; |
| 801 |
|
| 802 |
pam.size = sizeof(pam); |
| 803 |
pam.len = PAM_STRUCT_SIZE(tuple_type); |
| 804 |
pam.file = ofP; |
| 805 |
pam.width = xmag*(width-trim*2); |
| 806 |
pam.height = ymag*(height-trim*2); |
| 807 |
pam.depth = 3; |
| 808 |
pam.format = PAM_FORMAT; |
| 809 |
pam.maxval = 255; |
| 810 |
strcpy(pam.tuple_type, "RGB"); |
| 811 |
|
| 812 |
pnm_writepaminit(&pam); |
| 813 |
|
| 814 |
tuplerow = pnm_allocpamrow(&pam); |
| 815 |
|
| 816 |
perc = width * height * 0.01; |
| 817 |
if (fuji_width) perc /= 2; |
| 818 |
FORC3 { |
| 819 |
int val; |
| 820 |
for (val=0x2000, total=0; --val > 32; ) |
| 821 |
if ((total += histogram[c][val]) > perc) break; |
| 822 |
if (white < val) |
| 823 |
white = val; |
| 824 |
} |
| 825 |
white *= 8 / cmdline.bright; |
| 826 |
for (i=0; i < 0x10000; ++i) { |
| 827 |
int val; |
| 828 |
r = i / white; |
| 829 |
val = 256 * ( !use_gamma ? r : |
| 830 |
r <= 0.018 ? r*4.5 : pow(r,0.45)*1.099-0.099 ); |
| 831 |
lut[i] = MIN(255, val); |
| 832 |
} |
| 833 |
|
| 834 |
for (row = 0 + trim; row < height - trim; ++row) { |
| 835 |
unsigned int col; |
| 836 |
for (col = 0 + trim; col < width - trim; ++col) { |
| 837 |
unsigned int plane; |
| 838 |
for (plane = 0; plane < pam.depth; ++plane) { |
| 839 |
sample const value = lut[image[row*width+col][plane]]; |
| 840 |
unsigned int copy; |
| 841 |
for (copy = 0; copy < xmag; ++copy) { |
| 842 |
unsigned int const pamcol = xmag*(col-trim)+copy; |
| 843 |
tuplerow[pamcol][plane] = value; |
| 844 |
} |
| 845 |
} |
| 846 |
} |
| 847 |
{ |
| 848 |
unsigned int copy; |
| 849 |
for (copy = 0; copy < ymag; ++copy) |
| 850 |
pnm_writepamrow(&pam, tuplerow); |
| 851 |
} |
| 852 |
} |
| 853 |
pnm_freepamrow(tuplerow); |
| 854 |
} |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
static void CLASS |
| 859 |
writePam(FILE * const ofP, |
| 860 |
Image const image, |
| 861 |
bool const linear, |
| 862 |
unsigned int const trim) { |
| 863 |
|
| 864 |
if (linear) |
| 865 |
writePamLinear(ofP, image, trim); |
| 866 |
else |
| 867 |
writePamNonlinear(ofP, image, trim); |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
static void CLASS |
| 873 |
convertIt(FILE * const ifP, |
| 874 |
FILE * const ofP, |
| 875 |
LoadRawFn * const load_raw) { |
| 876 |
|
| 877 |
Image image; |
| 878 |
unsigned int trim; |
| 879 |
|
| 880 |
shrink = cmdline.half_size && filters; |
| 881 |
iheight = (height + shrink) >> shrink; |
| 882 |
iwidth = (width + shrink) >> shrink; |
| 883 |
image = calloc (iheight*iwidth*sizeof(*image) + meta_length, 1); |
| 884 |
merror (image, "main()"); |
| 885 |
meta_data = (char *) (image + iheight*iwidth); |
| 886 |
if (cmdline.verbose) |
| 887 |
pm_message ("Loading %s %s image ...", make, model); |
| 888 |
|
| 889 |
use_secondary = cmdline.use_secondary; |
| 890 |
|
| 891 |
ifp = ifP; |
| 892 |
|
| 893 |
load_raw(image); |
| 894 |
fixBadPixels(image); |
| 895 |
height = iheight; |
| 896 |
width = iwidth; |
| 897 |
if (is_foveon) { |
| 898 |
if (cmdline.verbose) |
| 899 |
pm_message ("Foveon interpolation..."); |
| 900 |
foveon_interpolate(image, coeff); |
| 901 |
} else { |
| 902 |
scaleColors(image); |
| 903 |
} |
| 904 |
if (shrink) |
| 905 |
filters = 0; |
| 906 |
|
| 907 |
if (filters && !cmdline.document_mode) { |
| 908 |
trim = 1; |
| 909 |
if (cmdline.verbose) |
| 910 |
pm_message ("%s interpolation...", |
| 911 |
cmdline.quick_interpolate ? "Bilinear":"VNG"); |
| 912 |
vngInterpolate(image); |
| 913 |
} else |
| 914 |
trim = 0; |
| 915 |
|
| 916 |
fujiRotate(&image); |
| 917 |
|
| 918 |
if (cmdline.verbose) |
| 919 |
pm_message ("Converting to RGB colorspace..."); |
| 920 |
convertToRgb(image, trim); |
| 921 |
|
| 922 |
if (flip) { |
| 923 |
if (cmdline.verbose) |
| 924 |
pm_message ("Flipping image %c:%c:%c...", |
| 925 |
flip & 1 ? 'H':'0', flip & 2 ? 'V':'0', |
| 926 |
flip & 4 ? 'T':'0'); |
| 927 |
flipImage(image); |
| 928 |
} |
| 929 |
writePam(ofP, image, cmdline.linear, trim); |
| 930 |
|
| 931 |
free(image); |
| 932 |
} |
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
int |
| 937 |
main (int argc, char **argv) { |
| 938 |
|
| 939 |
FILE * const ofP = stdout; |
| 940 |
|
| 941 |
FILE * ifP; |
| 942 |
int rc; |
| 943 |
LoadRawFn * load_raw; |
| 944 |
|
| 945 |
pnm_init(&argc, argv); |
| 946 |
|
| 947 |
parseCommandLine(argc, argv, &cmdline); |
| 948 |
|
| 949 |
verbose = cmdline.verbose; |
| 950 |
|
| 951 |
ifP = pm_openr(cmdline.inputFileName); |
| 952 |
|
| 953 |
rc = identify(ifP, |
| 954 |
cmdline.use_secondary, cmdline.use_camera_rgb, |
| 955 |
cmdline.red_scale, cmdline.blue_scale, |
| 956 |
cmdline.four_color_rgb, cmdline.inputFileName, |
| 957 |
&load_raw); |
| 958 |
if (rc != 0) |
| 959 |
pm_error("Unable to identify the format of the input image"); |
| 960 |
else { |
| 961 |
if (cmdline.identify_only) { |
| 962 |
pm_message ("Input is a %s %s image.", make, model); |
| 963 |
} else { |
| 964 |
if (cmdline.verbose) |
| 965 |
pm_message ("Input is a %s %s image.", make, model); |
| 966 |
convertIt(ifP, ofP, load_raw); |
| 967 |
} |
| 968 |
} |
| 969 |
pm_close(ifP); |
| 970 |
pm_close(ofP); |
| 971 |
|
| 972 |
return 0; |
| 973 |
} |
| 974 |
|
| 975 |
|
| 976 |
|