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