| 1 |
/* Copyright (c) 1997 Regents of the University of California */ |
| 2 |
|
| 3 |
#ifndef lint |
| 4 |
static char SCCSid[] = "$SunId$ LBL"; |
| 5 |
#endif |
| 6 |
|
| 7 |
/* |
| 8 |
* Warp colors in Radiance picture to correct for input/output changes. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <stdio.h> |
| 12 |
#include "color.h" |
| 13 |
#include "warp3d.h" |
| 14 |
|
| 15 |
char *progname; /* global argv[0] */ |
| 16 |
|
| 17 |
FILE *infp = NULL; /* input stream */ |
| 18 |
int xres, yres; /* input picture resolution */ |
| 19 |
|
| 20 |
WARP3D *cwarp; /* our warp map */ |
| 21 |
int iclip = CGAMUT_UPPER; /* input value gamut clipping */ |
| 22 |
int oclip = CGAMUT_LOWER; /* output value gamut clipping */ |
| 23 |
|
| 24 |
|
| 25 |
main(argc, argv) |
| 26 |
int argc; |
| 27 |
char *argv[]; |
| 28 |
{ |
| 29 |
static char picfmt[LPICFMT+1] = PICFMT; |
| 30 |
int cwflags = 0; |
| 31 |
int rval; |
| 32 |
int i; |
| 33 |
|
| 34 |
progname = argv[0]; |
| 35 |
infp = stdin; |
| 36 |
/* get options */ |
| 37 |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
| 38 |
switch (argv[i][1]) { |
| 39 |
case 'e': |
| 40 |
cwflags = W3EXACT; |
| 41 |
break; |
| 42 |
case 'f': |
| 43 |
cwflags = W3FAST; |
| 44 |
break; |
| 45 |
case 'i': |
| 46 |
iclip = 0; |
| 47 |
break; |
| 48 |
case 'o': |
| 49 |
oclip = CGAMUT; |
| 50 |
break; |
| 51 |
default: |
| 52 |
goto userr; |
| 53 |
} |
| 54 |
/* load warp map */ |
| 55 |
if (i >= argc) |
| 56 |
goto userr; |
| 57 |
if ((cwarp = load3dw(argv[i++], NULL)) == NULL) |
| 58 |
syserror("load3dw"); |
| 59 |
set3dwfl(cwarp, cwflags); |
| 60 |
/* open input and output pictures */ |
| 61 |
if (i < argc && (infp = fopen(argv[i], "r")) == NULL) |
| 62 |
syserror(argv[i]); |
| 63 |
if (i < argc-1 && freopen(argv[i+1], "w", stdout) == NULL) |
| 64 |
syserror(argv[i+1]); |
| 65 |
/* transfer header */ |
| 66 |
if ((rval = checkheader(infp, picfmt, stdout)) < 0) { |
| 67 |
fprintf(stderr, "%s: input not a Radiance picture\n", |
| 68 |
progname); |
| 69 |
exit(1); |
| 70 |
} |
| 71 |
if (rval) |
| 72 |
fputformat(picfmt, stdout); |
| 73 |
/* add new header info. */ |
| 74 |
printargs(i, argv, stdout); |
| 75 |
putchar('\n'); |
| 76 |
/* get picture size */ |
| 77 |
if ((rval = fgetresolu(&xres, &yres, infp)) < 0) { |
| 78 |
fprintf(stderr, "%s: bad picture size\n", progname); |
| 79 |
exit(1); |
| 80 |
} |
| 81 |
/* new picture size the same */ |
| 82 |
fputresolu(rval, xres, yres, stdout); |
| 83 |
/* warp those colors! */ |
| 84 |
picwarp(); |
| 85 |
exit(0); |
| 86 |
userr: |
| 87 |
fprintf(stderr, |
| 88 |
"Usage: %s [-i][-o][-e|-f] map.cwp [input.pic [output.pic]]\n", |
| 89 |
progname); |
| 90 |
exit(1); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
syserror(s) /* print system error and exit */ |
| 95 |
char *s; |
| 96 |
{ |
| 97 |
fprintf(stderr, "%s: ", progname); |
| 98 |
perror(s); |
| 99 |
exit(2); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
picwarp() /* warp our picture scanlines */ |
| 104 |
{ |
| 105 |
register COLOR *scan; |
| 106 |
long ngamut = 0; |
| 107 |
int rval; |
| 108 |
int y; |
| 109 |
register int x; |
| 110 |
|
| 111 |
scan = (COLOR *)malloc(xres*sizeof(COLOR)); |
| 112 |
if (scan == NULL) |
| 113 |
syserror("picwarp"); |
| 114 |
for (y = 0; y < yres; y++) { |
| 115 |
if (freadscan(scan, xres, infp) < 0) { |
| 116 |
fprintf(stderr, "%s: error reading input picture\n", |
| 117 |
progname); |
| 118 |
exit(1); |
| 119 |
} |
| 120 |
for (x = 0; x < xres; x++) { |
| 121 |
if (iclip) |
| 122 |
clipgamut(scan[x], bright(scan[x]), iclip, |
| 123 |
cblack, cwhite); |
| 124 |
rval = warp3d(scan[x], scan[x], cwarp); |
| 125 |
if (rval & W3ERROR) |
| 126 |
syserror("warp3d"); |
| 127 |
if (rval & W3BADMAP) { |
| 128 |
fprintf(stderr, "%s: singular color mapping\n", |
| 129 |
progname); |
| 130 |
exit(1); |
| 131 |
} |
| 132 |
if (rval & W3GAMUT) |
| 133 |
ngamut++; |
| 134 |
if (oclip) |
| 135 |
clipgamut(scan[x], bright(scan[x]), oclip, |
| 136 |
cblack, cwhite); |
| 137 |
} |
| 138 |
if (fwritescan(scan, xres, stdout) < 0) { |
| 139 |
fprintf(stderr, "%s: error writing output picture\n", |
| 140 |
progname); |
| 141 |
exit(1); |
| 142 |
} |
| 143 |
} |
| 144 |
if (ngamut >= (long)xres*yres/100) |
| 145 |
fprintf(stderr, "%s: warning - %d%% of pixels out of gamut\n", |
| 146 |
progname, 100*ngamut/((long)xres*yres)); |
| 147 |
free((char *)scan); |
| 148 |
} |