18 |
|
#include "resolu.h" |
19 |
|
#include "pmap.h" |
20 |
|
|
21 |
< |
/* MacBeth colors (CIE 1931, absolute white) */ |
22 |
< |
/* computed from spectral measurements */ |
21 |
> |
/* MacBeth colors */ |
22 |
> |
#define DarkSkin 0 |
23 |
> |
#define LightSkin 1 |
24 |
> |
#define BlueSky 2 |
25 |
> |
#define Foliage 3 |
26 |
> |
#define BlueFlower 4 |
27 |
> |
#define BluishGreen 5 |
28 |
> |
#define Orange 6 |
29 |
> |
#define PurplishBlue 7 |
30 |
> |
#define ModerateRed 8 |
31 |
> |
#define Purple 9 |
32 |
> |
#define YellowGreen 10 |
33 |
> |
#define OrangeYellow 11 |
34 |
> |
#define Blue 12 |
35 |
> |
#define Green 13 |
36 |
> |
#define Red 14 |
37 |
> |
#define Yellow 15 |
38 |
> |
#define Magenta 16 |
39 |
> |
#define Cyan 17 |
40 |
> |
#define White 18 |
41 |
> |
#define Neutral8 19 |
42 |
> |
#define Neutral65 20 |
43 |
> |
#define Neutral5 21 |
44 |
> |
#define Neutral35 22 |
45 |
> |
#define Black 23 |
46 |
> |
/* computed from 5nm spectral measurements */ |
47 |
> |
/* CIE 1931 2 degree obs, equal-energy white */ |
48 |
|
float mbxyY[24][3] = { |
49 |
|
{0.462, 0.3769, 0.0932961}, /* DarkSkin */ |
50 |
|
{0.4108, 0.3542, 0.410348}, /* LightSkin */ |
75 |
|
COLOR mbRGB[24]; /* MacBeth RGB values */ |
76 |
|
|
77 |
|
#define NMBNEU 6 /* Number of MacBeth neutral colors */ |
78 |
< |
short mbneu[NMBNEU] = {23,22,21,20,19,18}; |
54 |
< |
#define NMBMOD 16 /* Number of MacBeth unsaturated colors */ |
55 |
< |
short mbmod[NMBMOD] = {0,1,2,3,4,5,6,7,8,9,10,11,19,20,21,22}; |
56 |
< |
#define NMBSAT 6 /* Number of MacBeth saturated colors */ |
57 |
< |
short mbsat[NMBSAT] = {14,12,13,15,16,17}; |
78 |
> |
short mbneu[NMBNEU] = {Black,Neutral35,Neutral5,Neutral65,Neutral8,White}; |
79 |
|
|
80 |
+ |
#define NEUFLGS (1L<<White|1L<<Neutral8|1L<<Neutral65| \ |
81 |
+ |
1L<<Neutral5|1L<<Neutral35|1L<<Black) |
82 |
+ |
|
83 |
+ |
#define SATFLGS (1L<<Red|1L<<Green|1L<<Blue|1L<<Magenta|1L<<Yellow| \ |
84 |
+ |
1L<<Cyan|1L<<Orange|1L<<Purple|1L<<PurplishBlue| \ |
85 |
+ |
1L<<YellowGreen|1<<OrangeYellow|1L<<BlueFlower) |
86 |
+ |
|
87 |
+ |
#define UNSFLGS (1L<<DarkSkin|1L<<LightSkin|1L<<BlueSky|1L<<Foliage| \ |
88 |
+ |
1L<<BluishGreen|1L<<ModerateRed) |
89 |
+ |
|
90 |
+ |
#define REQFLGS NEUFLGS /* need these colors */ |
91 |
+ |
#define MODFLGS (NEUFLGS|UNSFLGS) /* should be in gamut */ |
92 |
+ |
|
93 |
+ |
#define CENTCVG 0.3 /* measured coverage of square sample */ |
94 |
+ |
#define FULLCVG 0.9 /* coverage of entire square */ |
95 |
+ |
|
96 |
|
int xmax, ymax; /* input image dimensions */ |
97 |
|
int bounds[4][2]; /* image coordinates of chart corners */ |
98 |
|
double imgxfm[3][3]; /* coordinate transformation matrix */ |
99 |
|
|
100 |
< |
COLOR picRGB[24]; /* picture colors */ |
100 |
> |
COLOR inpRGB[24]; /* measured or scanned input colors */ |
101 |
> |
long inpflags = 0; /* flags of which colors were input */ |
102 |
> |
long gmtflags = 0; /* flags of out-of-gamut colors */ |
103 |
|
|
104 |
|
COLOR bramp[NMBNEU][2]; /* brightness ramp (per primary) */ |
105 |
|
double solmat[3][3]; /* color mapping matrix */ |
106 |
|
|
107 |
< |
FILE *debugfp = NULL; |
107 |
> |
FILE *debugfp = NULL; /* debug output picture */ |
108 |
|
char *progname; |
109 |
|
|
110 |
|
extern char *malloc(); |
114 |
|
int argc; |
115 |
|
char **argv; |
116 |
|
{ |
117 |
+ |
int inpispic = 1; |
118 |
|
int i; |
119 |
|
|
120 |
|
progname = argv[0]; |
121 |
< |
if (argc > 2 && !strcmp(argv[1], "-d")) { /* debug output */ |
122 |
< |
if ((debugfp = fopen(argv[2], "w")) == NULL) { |
123 |
< |
perror(argv[2]); |
124 |
< |
exit(1); |
125 |
< |
} |
121 |
> |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
122 |
> |
switch (argv[i][1]) { |
123 |
> |
case 'd': /* debug output */ |
124 |
> |
i++; |
125 |
> |
if (badarg(argc-i, argv+i, "s")) |
126 |
> |
goto userr; |
127 |
> |
if ((debugfp = fopen(argv[i], "w")) == NULL) { |
128 |
> |
perror(argv[i]); |
129 |
> |
exit(1); |
130 |
> |
} |
131 |
|
#ifdef MSDOS |
132 |
< |
setmode(fileno(debugfp), O_BINARY); |
132 |
> |
setmode(fileno(debugfp), O_BINARY); |
133 |
|
#endif |
134 |
< |
newheader("RADIANCE", debugfp); |
135 |
< |
printargs(argc, argv, debugfp); |
136 |
< |
argv += 2; |
137 |
< |
argc -= 2; |
138 |
< |
} |
139 |
< |
if (argc != 3 && argc != 11) |
140 |
< |
goto userr; |
141 |
< |
if (strcmp(argv[1], "-") && freopen(argv[1], "r", stdin) == NULL) { |
134 |
> |
newheader("RADIANCE", debugfp); /* start */ |
135 |
> |
printargs(argc, argv, debugfp); /* header */ |
136 |
> |
break; |
137 |
> |
case 'p': /* picture position */ |
138 |
> |
if (badarg(argc-i-1, argv+i+1, "iiiiiiii")) |
139 |
> |
goto userr; |
140 |
> |
bounds[0][0] = atoi(argv[++i]); |
141 |
> |
bounds[0][1] = atoi(argv[++i]); |
142 |
> |
bounds[1][0] = atoi(argv[++i]); |
143 |
> |
bounds[1][1] = atoi(argv[++i]); |
144 |
> |
bounds[2][0] = atoi(argv[++i]); |
145 |
> |
bounds[2][1] = atoi(argv[++i]); |
146 |
> |
bounds[3][0] = atoi(argv[++i]); |
147 |
> |
bounds[3][1] = atoi(argv[++i]); |
148 |
> |
inpispic = 2; |
149 |
> |
break; |
150 |
> |
case 'c': /* color input */ |
151 |
> |
inpispic = 0; |
152 |
> |
break; |
153 |
> |
default: |
154 |
> |
goto userr; |
155 |
> |
} |
156 |
> |
/* open files */ |
157 |
> |
if (i < argc && freopen(argv[i], "r", stdin) == NULL) { |
158 |
|
perror(argv[1]); |
159 |
|
exit(1); |
160 |
|
} |
161 |
< |
if (strcmp(argv[2], "-") && freopen(argv[2], "w", stdout) == NULL) { |
161 |
> |
if (i+1 < argc && freopen(argv[i+1], "w", stdout) == NULL) { |
162 |
|
perror(argv[2]); |
163 |
|
exit(1); |
164 |
|
} |
165 |
+ |
if (inpispic) { /* load input picture header */ |
166 |
|
#ifdef MSDOS |
167 |
< |
setmode(fileno(stdin), O_BINARY); |
167 |
> |
setmode(fileno(stdin), O_BINARY); |
168 |
|
#endif |
169 |
< |
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
170 |
< |
fgetresolu(&xmax, &ymax, stdin) < 0) { |
171 |
< |
fprintf(stderr, "%s: bad input picture\n", progname); |
172 |
< |
exit(1); |
111 |
< |
} |
112 |
< |
/* get chart boundaries */ |
113 |
< |
if (argc == 11) { |
114 |
< |
for (i = 0; i < 4; i++) { |
115 |
< |
if (!isint(argv[2*i+3]) | !isint(argv[2*i+4])) |
116 |
< |
goto userr; |
117 |
< |
bounds[i][0] = atoi(argv[2*i+3]); |
118 |
< |
bounds[i][1] = atoi(argv[2*i+4]); |
169 |
> |
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
170 |
> |
fgetresolu(&xmax, &ymax, stdin) < 0) { |
171 |
> |
fprintf(stderr, "%s: bad input picture\n", progname); |
172 |
> |
exit(1); |
173 |
|
} |
174 |
< |
} else { |
174 |
> |
} else { /* else set default xmax and ymax */ |
175 |
> |
xmax = 512; |
176 |
> |
ymax = 2*512/3; |
177 |
> |
} |
178 |
> |
if (inpispic != 2) { /* use default boundaries */ |
179 |
|
bounds[0][0] = bounds[2][0] = .029*xmax + .5; |
180 |
|
bounds[0][1] = bounds[1][1] = .956*ymax + .5; |
181 |
|
bounds[1][0] = bounds[3][0] = .971*xmax + .5; |
182 |
|
bounds[2][1] = bounds[3][1] = .056*ymax + .5; |
183 |
|
} |
184 |
|
init(); /* initialize */ |
185 |
< |
getcolors(); /* get picture colors */ |
185 |
> |
if (inpispic) /* get picture colors */ |
186 |
> |
getpicture(); |
187 |
> |
else |
188 |
> |
getcolors(); |
189 |
|
compute(); /* compute color mapping */ |
190 |
+ |
/* print comment */ |
191 |
+ |
printf("{ Color correction file computed by:\n\t"); |
192 |
+ |
printargs(argc, argv, stdout); |
193 |
+ |
printf("}\n"); |
194 |
|
putmapping(); /* put out color mapping */ |
195 |
< |
putdebug(); /* put out debug picture */ |
195 |
> |
if (debugfp != NULL) /* put out debug picture */ |
196 |
> |
if (inpispic) |
197 |
> |
picdebug(); |
198 |
> |
else |
199 |
> |
clrdebug(); |
200 |
|
exit(0); |
201 |
|
userr: |
202 |
< |
fprintf(stderr, "Usage: %s [-d dbg.pic] input.pic output.cal [xul yul xur yur xll yll xlr ylr]\n", |
202 |
> |
fprintf(stderr, |
203 |
> |
"Usage: %s [-d dbg.pic][-p xul yul xur yur xll yll xlr ylr] input.pic [output.cal]\n", |
204 |
|
progname); |
205 |
+ |
fprintf(stderr, " or: %s [-d dbg.pic] -c [xyY.dat [output.cal]]\n", |
206 |
+ |
progname); |
207 |
|
exit(1); |
208 |
|
} |
209 |
|
|
211 |
|
init() /* initialize */ |
212 |
|
{ |
213 |
|
double quad[4][2]; |
214 |
+ |
register int i; |
215 |
|
/* make coordinate transformation */ |
216 |
|
quad[0][0] = bounds[0][0]; |
217 |
|
quad[0][1] = bounds[0][1]; |
226 |
|
fprintf(stderr, "%s: bad chart boundaries\n", progname); |
227 |
|
exit(1); |
228 |
|
} |
229 |
+ |
/* map MacBeth colors to RGB space */ |
230 |
+ |
for (i = 0; i < 24; i++) |
231 |
+ |
xyY2RGB(mbRGB[i], mbxyY[i]); |
232 |
|
} |
233 |
|
|
234 |
|
|
235 |
|
int |
236 |
< |
chartndx(x, y) /* find color number for position */ |
236 |
> |
chartndx(x, y, cvg) /* find color number for position */ |
237 |
|
int x, y; |
238 |
+ |
double cvg; |
239 |
|
{ |
240 |
|
double ipos[3], cpos[3]; |
241 |
|
int ix, iy; |
242 |
|
double fx, fy; |
243 |
+ |
double cmin, cmax; |
244 |
|
|
245 |
|
ipos[0] = x; |
246 |
|
ipos[1] = y; |
254 |
|
iy = cpos[1]; |
255 |
|
fx = cpos[0] - ix; |
256 |
|
fy = cpos[1] - iy; |
257 |
< |
if (fx < .35 || fx >= .65 || fy < .35 || fy >= .65) |
257 |
> |
cmin = .5*(1.-cvg); |
258 |
> |
cmax = 1. - cmin; |
259 |
> |
if (fx < cmin || fx >= cmax || fy < cmin || fy >= cmax) |
260 |
|
return(-1); |
261 |
|
return(iy*6 + ix); |
262 |
|
} |
263 |
|
|
264 |
|
|
265 |
< |
getcolors() /* load in picture colors */ |
265 |
> |
getpicture() /* load in picture colors */ |
266 |
|
{ |
267 |
|
COLR *scanln; |
268 |
|
COLOR pval; |
277 |
|
exit(1); |
278 |
|
} |
279 |
|
for (i = 0; i < 24; i++) { |
280 |
< |
setcolor(picRGB[i], 0., 0., 0.); |
280 |
> |
setcolor(inpRGB[i], 0., 0., 0.); |
281 |
|
ccount[i] = 0; |
282 |
|
} |
283 |
|
for (y = ymax-1; y >= 0; y--) { |
287 |
|
exit(1); |
288 |
|
} |
289 |
|
for (x = 0; x < xmax; x++) { |
290 |
< |
i = chartndx(x, y); |
290 |
> |
i = chartndx(x, y, CENTCVG); |
291 |
|
if (i >= 0) { |
292 |
|
colr_color(pval, scanln[x]); |
293 |
< |
addcolor(picRGB[i], pval); |
293 |
> |
addcolor(inpRGB[i], pval); |
294 |
|
ccount[i]++; |
295 |
|
} |
296 |
|
} |
297 |
|
} |
298 |
< |
for (i = 0; i < 24; i++) { |
299 |
< |
if (ccount[i] == 0) { |
300 |
< |
fprintf(stderr, "%s: bad chart boundaries\n", |
298 |
> |
for (i = 0; i < 24; i++) { /* compute averages */ |
299 |
> |
if (ccount[i] == 0) |
300 |
> |
continue; |
301 |
> |
d = 1./ccount[i]; |
302 |
> |
scalecolor(inpRGB[i], d); |
303 |
> |
inpflags |= 1L<<i; |
304 |
> |
} |
305 |
> |
free((char *)scanln); |
306 |
> |
} |
307 |
> |
|
308 |
> |
|
309 |
> |
getcolors() /* get xyY colors from standard input */ |
310 |
> |
{ |
311 |
> |
int gotwhite = 0; |
312 |
> |
COLOR whiteclr; |
313 |
> |
int n; |
314 |
> |
float xyYin[3]; |
315 |
> |
|
316 |
> |
while (fgetval(stdin, 'i', &n) == 1) { /* read colors */ |
317 |
> |
if (n < 0 | n > 24 || |
318 |
> |
fgetval(stdin, 'f', &xyYin[0]) != 1 || |
319 |
> |
fgetval(stdin, 'f', &xyYin[1]) != 1 || |
320 |
> |
fgetval(stdin, 'f', &xyYin[2]) != 1 || |
321 |
> |
xyYin[0] < 0. | xyYin[0] > 1. | |
322 |
> |
xyYin[1] < 0. | xyYin[1] > 1.) { |
323 |
> |
fprintf(stderr, "%s: bad color input data\n", |
324 |
|
progname); |
325 |
|
exit(1); |
326 |
|
} |
327 |
< |
d = 1.0/ccount[i]; |
328 |
< |
scalecolor(picRGB[i], d); |
327 |
> |
if (n == 0) { /* calibration white */ |
328 |
> |
xyY2RGB(whiteclr, xyYin); |
329 |
> |
gotwhite++; |
330 |
> |
} else { /* standard color */ |
331 |
> |
n--; |
332 |
> |
xyY2RGB(inpRGB[n], xyYin); |
333 |
> |
inpflags |= 1L<<n; |
334 |
> |
} |
335 |
|
} |
336 |
< |
free((char *)scanln); |
336 |
> |
/* normalize colors */ |
337 |
> |
if (!gotwhite) { |
338 |
> |
if (!(inpflags & 1L<<White)) { |
339 |
> |
fprintf(stderr, "%s: missing input for White\n", |
340 |
> |
progname); |
341 |
> |
exit(1); |
342 |
> |
} |
343 |
> |
setcolor(whiteclr, |
344 |
> |
colval(inpRGB[White],RED)/colval(mbRGB[White],RED), |
345 |
> |
colval(inpRGB[White],GRN)/colval(mbRGB[White],GRN), |
346 |
> |
colval(inpRGB[White],BLU)/colval(mbRGB[White],BLU)); |
347 |
> |
} |
348 |
> |
for (n = 0; n < 24; n++) |
349 |
> |
if (inpflags & 1L<<n) |
350 |
> |
setcolor(inpRGB[n], |
351 |
> |
colval(inpRGB[n],RED)/colval(whiteclr,RED), |
352 |
> |
colval(inpRGB[n],GRN)/colval(whiteclr,GRN), |
353 |
> |
colval(inpRGB[n],BLU)/colval(whiteclr,BLU)); |
354 |
|
} |
355 |
|
|
356 |
|
|
357 |
|
bresp(y, x) /* piecewise linear interpolation of primaries */ |
358 |
|
COLOR y, x; |
359 |
|
{ |
234 |
– |
double cv[3]; |
360 |
|
register int i, n; |
361 |
|
|
362 |
|
for (i = 0; i < 3; i++) { |
363 |
< |
n = NMBNEU; |
364 |
< |
while (n > 0 && colval(x,i) < colval(bramp[--n][0],i)) |
365 |
< |
; |
366 |
< |
cv[i] = ((colval(bramp[n+1][0],i) - colval(x,i)) * |
363 |
> |
for (n = 0; n < NMBNEU-2; n++) |
364 |
> |
if (colval(x,i) < colval(bramp[n+1][0],i)) |
365 |
> |
break; |
366 |
> |
colval(y,i) = ((colval(bramp[n+1][0],i) - colval(x,i)) * |
367 |
|
colval(bramp[n][1],i) + |
368 |
|
(colval(x,i) - colval(bramp[n][0],i)) * |
369 |
|
colval(bramp[n+1][1],i)) / |
370 |
|
(colval(bramp[n+1][0],i) - colval(bramp[n][0],i)); |
246 |
– |
if (cv[i] < 0.) cv[i] = 0.; |
371 |
|
} |
248 |
– |
setcolor(y, cv[0], cv[1], cv[2]); |
372 |
|
} |
373 |
|
|
374 |
|
|
375 |
|
compute() /* compute color mapping */ |
376 |
|
{ |
377 |
< |
COLOR clrin[NMBMOD], clrout[NMBMOD]; |
377 |
> |
COLOR clrin[24], clrout[24]; |
378 |
> |
long cflags; |
379 |
|
COLOR ctmp; |
380 |
< |
double d; |
381 |
< |
register int i; |
382 |
< |
/* map MacBeth colors to RGB space */ |
383 |
< |
for (i = 0; i < 24; i++) { |
384 |
< |
d = mbxyY[i][2] / mbxyY[i][1]; |
385 |
< |
ctmp[0] = mbxyY[i][0] * d; |
262 |
< |
ctmp[1] = mbxyY[i][2]; |
263 |
< |
ctmp[2] = (1. - mbxyY[i][0] - mbxyY[i][1]) * d; |
264 |
< |
cie_rgb(mbRGB[i], ctmp); |
380 |
> |
register int i, j, n; |
381 |
> |
/* did we get what we need? */ |
382 |
> |
if ((inpflags & REQFLGS) != REQFLGS) { |
383 |
> |
fprintf(stderr, "%s: missing required input colors\n", |
384 |
> |
progname); |
385 |
> |
exit(1); |
386 |
|
} |
387 |
|
/* compute piecewise luminance curve */ |
388 |
|
for (i = 0; i < NMBNEU; i++) { |
389 |
< |
copycolor(bramp[i][0], picRGB[mbneu[i]]); |
389 |
> |
copycolor(bramp[i][0], inpRGB[mbneu[i]]); |
390 |
|
copycolor(bramp[i][1], mbRGB[mbneu[i]]); |
391 |
|
} |
392 |
< |
/* compute color matrix */ |
393 |
< |
for (i = 0; i < NMBMOD; i++) { |
394 |
< |
bresp(clrin[i], picRGB[mbmod[i]]); |
395 |
< |
copycolor(clrout[i], mbRGB[mbmod[i]]); |
396 |
< |
} |
397 |
< |
compsoln(clrin, clrout, NMBMOD); |
392 |
> |
/* compute color mapping */ |
393 |
> |
do { |
394 |
> |
cflags = inpflags & ~gmtflags; |
395 |
> |
n = 0; /* compute transform matrix */ |
396 |
> |
for (i = 0; i < 24; i++) |
397 |
> |
if (cflags & 1L<<i) { |
398 |
> |
bresp(clrin[n], inpRGB[i]); |
399 |
> |
copycolor(clrout[n], mbRGB[i]); |
400 |
> |
n++; |
401 |
> |
} |
402 |
> |
compsoln(clrin, clrout, n); |
403 |
> |
/* check out-of-gamut colors */ |
404 |
> |
for (i = 0; i < 24; i++) |
405 |
> |
if (cflags & 1L<<i) { |
406 |
> |
cresp(ctmp, mbRGB[i]); |
407 |
> |
for (j = 0; j < 3; j++) |
408 |
> |
if (colval(ctmp,j) <= 0. || |
409 |
> |
colval(ctmp,j) >= 1.) { |
410 |
> |
gmtflags |= 1L<<i; |
411 |
> |
break; |
412 |
> |
} |
413 |
> |
} |
414 |
> |
} while (cflags & gmtflags); |
415 |
> |
if (gmtflags & MODFLGS) |
416 |
> |
fprintf(stderr, |
417 |
> |
"%s: warning - some moderate colors are out of gamut\n", |
418 |
> |
progname); |
419 |
|
} |
420 |
|
|
421 |
|
|
457 |
|
} |
458 |
|
|
459 |
|
|
460 |
< |
#if NMBMOD == 3 |
319 |
< |
compsoln(cin, cout, n) /* solve 3x3 system */ |
460 |
> |
compsoln(cin, cout, n) /* solve 3xN system using least-squares */ |
461 |
|
COLOR cin[], cout[]; |
462 |
|
int n; |
463 |
|
{ |
465 |
|
double mat[3][3], invmat[3][3]; |
466 |
|
double det; |
467 |
|
double colv[3], rowv[3]; |
468 |
< |
register int i, j; |
468 |
> |
register int i, j, k; |
469 |
|
|
470 |
< |
if (n != 3) { |
471 |
< |
fprintf(stderr, "%s: inconsistent code!\n", progname); |
470 |
> |
if (n < 3) { |
471 |
> |
fprintf(stderr, "%s: too few colors to match!\n", progname); |
472 |
|
exit(1); |
473 |
|
} |
474 |
< |
for (i = 0; i < 3; i++) |
475 |
< |
for (j = 0; j < 3; j++) |
476 |
< |
mat[i][j] = colval(cin[j],i); |
474 |
> |
if (n == 3) |
475 |
> |
for (i = 0; i < 3; i++) |
476 |
> |
for (j = 0; j < 3; j++) |
477 |
> |
mat[i][j] = colval(cin[j],i); |
478 |
> |
else { /* compute A^t A */ |
479 |
> |
for (i = 0; i < 3; i++) |
480 |
> |
for (j = i; j < 3; j++) { |
481 |
> |
mat[i][j] = 0.; |
482 |
> |
for (k = 0; k < n; k++) |
483 |
> |
mat[i][j] += colval(cin[k],i) * |
484 |
> |
colval(cin[k],j); |
485 |
> |
} |
486 |
> |
for (i = 1; i < 3; i++) /* using symmetry */ |
487 |
> |
for (j = 0; j < i; j++) |
488 |
> |
mat[i][j] = mat[j][i]; |
489 |
> |
} |
490 |
|
det = mx3d_adjoint(mat, invmat); |
491 |
|
if (fabs(det) < 1e-4) { |
492 |
|
fprintf(stderr, "%s: cannot compute color mapping\n", |
500 |
|
for (j = 0; j < 3; j++) |
501 |
|
invmat[i][j] /= det; |
502 |
|
for (i = 0; i < 3; i++) { |
503 |
< |
for (j = 0; j < 3; j++) |
504 |
< |
colv[j] = colval(cout[j],i); |
503 |
> |
if (n == 3) |
504 |
> |
for (j = 0; j < 3; j++) |
505 |
> |
colv[j] = colval(cout[j],i); |
506 |
> |
else |
507 |
> |
for (j = 0; j < 3; j++) { |
508 |
> |
colv[j] = 0.; |
509 |
> |
for (k = 0; k < n; k++) |
510 |
> |
colv[j] += colval(cout[k],i) * |
511 |
> |
colval(cin[k],j); |
512 |
> |
} |
513 |
|
mx3d_transform(colv, invmat, rowv); |
514 |
|
for (j = 0; j < 3; j++) |
515 |
|
solmat[i][j] = rowv[j]; |
516 |
|
} |
517 |
|
} |
518 |
< |
#else |
519 |
< |
compsoln(cin, cout, n) /* solve 3xN system (N > 3) */ |
520 |
< |
COLOR cin[], cout[]; |
521 |
< |
int n; |
518 |
> |
|
519 |
> |
|
520 |
> |
cvtcolor(cout, cin) /* convert color according to our mapping */ |
521 |
> |
COLOR cout, cin; |
522 |
|
{ |
523 |
< |
double *au[NMBMOD], *v[3], vv[3][3], auv[NMBMOD][3], w[3]; |
362 |
< |
double b[NMBMOD]; |
363 |
< |
register int i, j; |
523 |
> |
COLOR ctmp; |
524 |
|
|
525 |
< |
if (n > NMBMOD) { |
526 |
< |
fprintf(stderr, "%s: inconsistent code!\n", progname); |
527 |
< |
exit(1); |
528 |
< |
} |
529 |
< |
for (i = 0; i < n; i++) /* assign rectangular matrix A */ |
530 |
< |
for (j = 0; j < 3; j++) |
531 |
< |
auv[i][j] = colval(cin[i],j); |
532 |
< |
/* svdcmp indexing requires pointer offsets */ |
373 |
< |
for (j = 0; j < 3; j++) |
374 |
< |
v[j] = vv[j] - 1; |
375 |
< |
for (i = 0; i < n; i++) |
376 |
< |
au[i] = auv[i] - 1; |
377 |
< |
/* compute singular value decomposition */ |
378 |
< |
fprintf(stderr, "A:\n"); |
379 |
< |
for (i = 1; i <= n; i++) |
380 |
< |
fprintf(stderr, "%g %g %g\n", (au-1)[i][1], (au-1)[i][2], (au-1)[i][3]); |
381 |
< |
svdcmp(au-1, n, 3, w-1, v-1); |
382 |
< |
fprintf(stderr, "U:\n"); |
383 |
< |
for (i = 0; i < n; i++) |
384 |
< |
fprintf(stderr, "%g %g %g\n", auv[i][0], auv[i][1], auv[i][2]); |
385 |
< |
fprintf(stderr, "V:\n"); |
386 |
< |
for (i = 0; i < 3; i++) |
387 |
< |
fprintf(stderr, "%g %g %g\n", vv[i][0], vv[i][1], vv[i][2]); |
388 |
< |
fprintf(stderr, "W: %g %g %g\n", w[0], w[1], w[2]); |
389 |
< |
/* zero out small weights */ |
390 |
< |
for (j = 0; j < 3; j++) |
391 |
< |
if (w[j] < 1e-4) |
392 |
< |
w[j] = 0.; |
393 |
< |
/* back substitution for each row vector */ |
394 |
< |
for (j = 0; j < 3; j++) { |
395 |
< |
for (i = 0; i < n; i++) |
396 |
< |
b[i] = colval(cout[i],j); |
397 |
< |
svbksb(au-1, w-1, v-1, n, 3, b-1, solmat[j]-1); |
398 |
< |
} |
525 |
> |
bresp(ctmp, cin); |
526 |
> |
cresp(cout, ctmp); |
527 |
> |
if (colval(cout,RED) < 0.) |
528 |
> |
colval(cout,RED) = 0.; |
529 |
> |
if (colval(cout,GRN) < 0.) |
530 |
> |
colval(cout,GRN) = 0.; |
531 |
> |
if (colval(cout,BLU) < 0.) |
532 |
> |
colval(cout,BLU) = 0.; |
533 |
|
} |
400 |
– |
#endif |
534 |
|
|
535 |
|
|
536 |
< |
cvtcolor(cout, cin) /* convert color according to our mapping */ |
536 |
> |
cresp(cout, cin) /* transform color according to matrix */ |
537 |
|
COLOR cout, cin; |
538 |
|
{ |
539 |
|
double r, g, b; |
540 |
|
|
541 |
< |
bresp(cout, cin); |
542 |
< |
r = colval(cout,0)*solmat[0][0] + colval(cout,1)*solmat[0][1] |
543 |
< |
+ colval(cout,2)*solmat[0][2]; |
544 |
< |
if (r < 0) r = 0; |
545 |
< |
g = colval(cout,0)*solmat[1][0] + colval(cout,1)*solmat[1][1] |
546 |
< |
+ colval(cout,2)*solmat[1][2]; |
414 |
< |
if (g < 0) g = 0; |
415 |
< |
b = colval(cout,0)*solmat[2][0] + colval(cout,1)*solmat[2][1] |
416 |
< |
+ colval(cout,2)*solmat[2][2]; |
417 |
< |
if (b < 0) b = 0; |
541 |
> |
r = colval(cin,0)*solmat[0][0] + colval(cin,1)*solmat[0][1] |
542 |
> |
+ colval(cin,2)*solmat[0][2]; |
543 |
> |
g = colval(cin,0)*solmat[1][0] + colval(cin,1)*solmat[1][1] |
544 |
> |
+ colval(cin,2)*solmat[1][2]; |
545 |
> |
b = colval(cin,0)*solmat[2][0] + colval(cin,1)*solmat[2][1] |
546 |
> |
+ colval(cin,2)*solmat[2][2]; |
547 |
|
setcolor(cout, r, g, b); |
548 |
|
} |
549 |
|
|
550 |
|
|
551 |
< |
putdebug() /* put out debugging picture */ |
551 |
> |
xyY2RGB(rgbout, xyYin) /* convert xyY to RGB */ |
552 |
> |
COLOR rgbout; |
553 |
> |
register float xyYin[3]; |
554 |
|
{ |
555 |
+ |
COLOR ctmp; |
556 |
+ |
double d; |
557 |
+ |
|
558 |
+ |
d = xyYin[2] / xyYin[1]; |
559 |
+ |
ctmp[0] = xyYin[0] * d; |
560 |
+ |
ctmp[1] = xyYin[2]; |
561 |
+ |
ctmp[2] = (1. - xyYin[0] - xyYin[1]) * d; |
562 |
+ |
cie_rgb(rgbout, ctmp); |
563 |
+ |
} |
564 |
+ |
|
565 |
+ |
|
566 |
+ |
picdebug() /* put out debugging picture */ |
567 |
+ |
{ |
568 |
+ |
static COLOR blkcol = BLKCOLOR; |
569 |
|
COLOR *scan; |
570 |
|
int y; |
571 |
|
register int x, i; |
572 |
|
|
428 |
– |
if (debugfp == NULL) |
429 |
– |
return; |
573 |
|
if (fseek(stdin, 0L, 0) == EOF) { |
574 |
|
fprintf(stderr, "%s: cannot seek on input picture\n", progname); |
575 |
|
exit(1); |
583 |
|
exit(1); |
584 |
|
} |
585 |
|
/* finish debug header */ |
586 |
+ |
fputformat(COLRFMT, debugfp); |
587 |
|
putc('\n', debugfp); |
588 |
|
fprtresolu(xmax, ymax, debugfp); |
589 |
+ |
/* write debug picture */ |
590 |
|
for (y = ymax-1; y >= 0; y--) { |
591 |
|
if (freadscan(scan, xmax, stdin) < 0) { |
592 |
|
fprintf(stderr, "%s: error rereading input picture\n", |
594 |
|
exit(1); |
595 |
|
} |
596 |
|
for (x = 0; x < xmax; x++) { |
597 |
< |
i = chartndx(x, y); |
597 |
> |
i = chartndx(x, y, CENTCVG); |
598 |
|
if (i < 0) |
599 |
|
cvtcolor(scan[x], scan[x]); |
600 |
< |
else |
600 |
> |
else if (!(1L<<i & gmtflags) || (x+y)&07) |
601 |
|
copycolor(scan[x], mbRGB[i]); |
602 |
+ |
else |
603 |
+ |
copycolor(scan[x], blkcol); |
604 |
|
} |
605 |
|
if (fwritescan(scan, xmax, debugfp) < 0) { |
606 |
|
fprintf(stderr, "%s: error writing debugging picture\n", |
608 |
|
exit(1); |
609 |
|
} |
610 |
|
} |
611 |
+ |
/* clean up */ |
612 |
+ |
fclose(debugfp); |
613 |
+ |
free((char *)scan); |
614 |
+ |
} |
615 |
+ |
|
616 |
+ |
|
617 |
+ |
clrdebug() /* put out debug picture from color input */ |
618 |
+ |
{ |
619 |
+ |
static COLR blkclr = BLKCOLR; |
620 |
+ |
COLR mbclr[24], cvclr[24]; |
621 |
+ |
COLR *scan; |
622 |
+ |
COLOR ctmp; |
623 |
+ |
int y; |
624 |
+ |
register int i, x; |
625 |
+ |
/* convert colors */ |
626 |
+ |
for (i = 0; i < 24; i++) { |
627 |
+ |
setcolr(mbclr[i], colval(mbRGB[i],RED), |
628 |
+ |
colval(mbRGB[i],GRN), colval(mbRGB[i],BLU)); |
629 |
+ |
if (inpflags & 1L<<i) { |
630 |
+ |
cvtcolor(ctmp, inpRGB[i]); |
631 |
+ |
setcolr(cvclr[i], colval(ctmp,RED), |
632 |
+ |
colval(ctmp,GRN), colval(ctmp,BLU)); |
633 |
+ |
} |
634 |
+ |
} |
635 |
+ |
/* allocate scanline */ |
636 |
+ |
scan = (COLR *)malloc(xmax*sizeof(COLR)); |
637 |
+ |
if (scan == NULL) { |
638 |
+ |
perror(progname); |
639 |
+ |
exit(1); |
640 |
+ |
} |
641 |
+ |
/* finish debug header */ |
642 |
+ |
fputformat(COLRFMT, debugfp); |
643 |
+ |
putc('\n', debugfp); |
644 |
+ |
fprtresolu(xmax, ymax, debugfp); |
645 |
+ |
/* write debug picture */ |
646 |
+ |
for (y = ymax-1; y >= 0; y--) { |
647 |
+ |
for (x = 0; x < xmax; x++) |
648 |
+ |
if ((i = chartndx(x, y, CENTCVG)) >= 0) { |
649 |
+ |
if (!(1L<<i & gmtflags) || (x+y)&07) |
650 |
+ |
copycolr(scan[x], mbclr[i]); |
651 |
+ |
else |
652 |
+ |
copycolr(scan[x], blkclr); |
653 |
+ |
} else if ((i = chartndx(x, y, FULLCVG)) >= 0 && |
654 |
+ |
inpflags & 1L<<i) |
655 |
+ |
copycolr(scan[x], cvclr[i]); |
656 |
+ |
else |
657 |
+ |
copycolr(scan[x], blkclr); |
658 |
+ |
if (fwritecolrs(scan, xmax, debugfp) < 0) { |
659 |
+ |
fprintf(stderr, "%s: error writing debugging picture\n", |
660 |
+ |
progname); |
661 |
+ |
exit(1); |
662 |
+ |
} |
663 |
+ |
} |
664 |
+ |
/* clean up */ |
665 |
+ |
fclose(debugfp); |
666 |
|
free((char *)scan); |
667 |
|
} |