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