52 |
|
#define NMBNEU 6 /* Number of MacBeth neutral colors */ |
53 |
|
short mbneu[NMBNEU] = {23,22,21,20,19,18}; |
54 |
|
#define NMBMOD 3 /* Number of MacBeth moderate colors */ |
55 |
< |
short mbmod[NMBMOD] = {1,3,2}; |
55 |
> |
short mbmod[NMBMOD] = {1,2,21}; |
56 |
|
#define NMBSAT 6 /* Number of MacBeth saturated colors */ |
57 |
|
short mbsat[NMBSAT] = {14,12,13,15,16,17}; |
58 |
|
|
62 |
|
|
63 |
|
COLOR picRGB[24]; /* picture colors */ |
64 |
|
|
65 |
< |
double bramp[NMBNEU][2]; /* brightness ramp */ |
65 |
> |
COLOR bramp[NMBNEU][2]; /* brightness ramp (per primary) */ |
66 |
|
double solmat[3][3]; /* color mapping matrix */ |
67 |
|
|
68 |
|
FILE *debugfp = NULL; |
125 |
|
} |
126 |
|
init(); /* initialize */ |
127 |
|
getcolors(); /* get picture colors */ |
128 |
+ |
compute(); /* compute color mapping */ |
129 |
|
putmapping(); /* put out color mapping */ |
130 |
|
putdebug(); /* put out debug picture */ |
131 |
|
exit(0); |
139 |
|
init() /* initialize */ |
140 |
|
{ |
141 |
|
double quad[4][2]; |
141 |
– |
COLOR ctmp; |
142 |
– |
double d; |
143 |
– |
int i; |
142 |
|
/* make coordinate transformation */ |
143 |
|
quad[0][0] = bounds[0][0]; |
144 |
|
quad[0][1] = bounds[0][1]; |
153 |
|
fprintf(stderr, "%s: bad chart boundaries\n", progname); |
154 |
|
exit(1); |
155 |
|
} |
158 |
– |
/* map MacBeth colors to RGB space */ |
159 |
– |
for (i = 0; i < 24; i++) { |
160 |
– |
d = mbxyY[i][2] / mbxyY[i][1]; |
161 |
– |
ctmp[0] = mbxyY[i][0] * d; |
162 |
– |
ctmp[1] = mbxyY[i][2]; |
163 |
– |
ctmp[2] = (1. - mbxyY[i][0] - mbxyY[i][1]) * d; |
164 |
– |
cie_rgb(mbRGB[i], ctmp); |
165 |
– |
} |
156 |
|
} |
157 |
|
|
158 |
|
|
228 |
|
} |
229 |
|
|
230 |
|
|
231 |
< |
double |
232 |
< |
bresp(x) /* piecewise linear interpolation of brightness */ |
243 |
< |
double x; |
231 |
> |
bresp(y, x) /* piecewise linear interpolation of primaries */ |
232 |
> |
COLOR y, x; |
233 |
|
{ |
234 |
< |
register int n = NMBNEU; |
234 |
> |
register int i, n; |
235 |
|
|
236 |
< |
while (n > 0 && x < bramp[--n][0]) |
237 |
< |
; |
238 |
< |
return( ((bramp[n+1][0] - x)*bramp[n][1] + |
239 |
< |
(x - bramp[n][0])*bramp[n+1][1]) / |
240 |
< |
(bramp[n+1][0] - bramp[n][0]) ); |
236 |
> |
for (i = 0; i < 3; i++) { |
237 |
> |
n = NMBNEU; |
238 |
> |
while (n > 0 && colval(x,i) < colval(bramp[--n][0],i)) |
239 |
> |
; |
240 |
> |
colval(y,i) = ((colval(bramp[n+1][0],i) - colval(x,i)) * |
241 |
> |
colval(bramp[n][1],i) + |
242 |
> |
(colval(x,i) - colval(bramp[n][0],i)) * |
243 |
> |
colval(bramp[n+1][1],i)) / |
244 |
> |
(colval(bramp[n+1][0],i) - colval(bramp[n][0],i)); |
245 |
> |
} |
246 |
|
} |
247 |
|
|
248 |
|
|
249 |
< |
putmapping() /* compute and print mapping for pcomb -f */ |
249 |
> |
compute() /* compute color mapping */ |
250 |
|
{ |
251 |
< |
float clrin[NMBMOD][3], clrout[NMBMOD][3]; |
252 |
< |
register int i, j; |
251 |
> |
COLOR clrin[NMBMOD], clrout[NMBMOD]; |
252 |
> |
COLOR ctmp; |
253 |
> |
double d; |
254 |
> |
register int i; |
255 |
> |
/* map MacBeth colors to RGB space */ |
256 |
> |
for (i = 0; i < 24; i++) { |
257 |
> |
d = mbxyY[i][2] / mbxyY[i][1]; |
258 |
> |
ctmp[0] = mbxyY[i][0] * d; |
259 |
> |
ctmp[1] = mbxyY[i][2]; |
260 |
> |
ctmp[2] = (1. - mbxyY[i][0] - mbxyY[i][1]) * d; |
261 |
> |
cie_rgb(mbRGB[i], ctmp); |
262 |
> |
} |
263 |
|
/* compute piecewise luminance curve */ |
264 |
|
for (i = 0; i < NMBNEU; i++) { |
265 |
< |
bramp[i][0] = bright(picRGB[mbneu[i]]); |
266 |
< |
bramp[i][1] = bright(mbRGB[mbneu[i]]); |
265 |
> |
copycolor(bramp[i][0], picRGB[mbneu[i]]); |
266 |
> |
copycolor(bramp[i][1], mbRGB[mbneu[i]]); |
267 |
|
} |
268 |
|
/* compute color matrix */ |
269 |
< |
for (i = 0; i < NMBMOD; i++) |
270 |
< |
for (j = 0; j < 3; j++) { |
271 |
< |
clrin[i][j] = bresp(picRGB[mbmod[i]][j]); |
272 |
< |
clrout[i][j] = mbRGB[mbmod[i]][j]; |
269 |
< |
} |
269 |
> |
for (i = 0; i < NMBMOD; i++) { |
270 |
> |
bresp(clrin[i], picRGB[mbmod[i]]); |
271 |
> |
copycolor(clrout[i], mbRGB[mbmod[i]]); |
272 |
> |
} |
273 |
|
compsoln(clrin, clrout, NMBMOD); |
274 |
+ |
} |
275 |
+ |
|
276 |
+ |
|
277 |
+ |
putmapping() /* put out color mapping for pcomb -f */ |
278 |
+ |
{ |
279 |
+ |
static char cchar[3] = {'r', 'g', 'b'}; |
280 |
+ |
register int i, j; |
281 |
|
/* print brightness mapping */ |
282 |
< |
printf("xval(i) : select(i, 0"); |
283 |
< |
for (i = 0; i < NMBNEU; i++) |
284 |
< |
printf(", %g", bramp[i][0]); |
285 |
< |
printf(");\n"); |
286 |
< |
printf("yval(i) : select(i, 0"); |
287 |
< |
for (i = 0; i < NMBNEU; i++) |
288 |
< |
printf(", %g", bramp[i][1]); |
289 |
< |
printf(");\n"); |
290 |
< |
printf("ifind(x,f,n) : if(1.5-n, 1, if(x-f(n), n, ifind(x,f,n-1)));\n"); |
291 |
< |
printf("binterp(i,x) : ((xval(i+1)-x)*yval(i) + (x-xval(i))*yval(i+1))/\n"); |
292 |
< |
printf("\t\t(xval(i+1) - xval(i));\n"); |
293 |
< |
printf("bresp(x) : binterp(ifind(x,xval,xval(0)-1), x);\n"); |
294 |
< |
printf("nred = bresp(ri(1));\n"); |
295 |
< |
printf("ngrn = bresp(gi(1));\n"); |
296 |
< |
printf("nblu = bresp(bi(1));\n"); |
282 |
> |
for (j = 0; j < 3; j++) { |
283 |
> |
printf("%cxa(i) : select(i", cchar[j]); |
284 |
> |
for (i = 0; i < NMBNEU; i++) |
285 |
> |
printf(",%g", colval(bramp[i][0],j)); |
286 |
> |
printf(");\n"); |
287 |
> |
printf("%cya(i) : select(i", cchar[j]); |
288 |
> |
for (i = 0; i < NMBNEU; i++) |
289 |
> |
printf(",%g", colval(bramp[i][1],j)); |
290 |
> |
printf(");\n"); |
291 |
> |
printf("%c = %ci(1);\n", cchar[j], cchar[j]); |
292 |
> |
printf("%cfi(n) = if(n-%g, %d, if(%cxa(n+1)-%c, n, %cfi(n+1)));\n", |
293 |
> |
cchar[j], NMBNEU-1.5, NMBNEU-1, cchar[j], |
294 |
> |
cchar[j], cchar[j]); |
295 |
> |
printf("%cndx = %cfi(1);\n", cchar[j], cchar[j]); |
296 |
> |
printf("%cn = ((%cxa(%cndx+1)-%c)*%cya(%cndx) + ", |
297 |
> |
cchar[j], cchar[j], cchar[j], |
298 |
> |
cchar[j], cchar[j], cchar[j]); |
299 |
> |
printf("(%c-%cxa(%cndx))*%cya(%cndx+1)) /\n", |
300 |
> |
cchar[j], cchar[j], cchar[j], |
301 |
> |
cchar[j], cchar[j]); |
302 |
> |
printf("\t\t(%cxa(%cndx+1) - %cxa(%cndx)) ;\n", |
303 |
> |
cchar[j], cchar[j], cchar[j], cchar[j]); |
304 |
> |
} |
305 |
|
/* print color mapping */ |
306 |
< |
printf("ro = %g*nred + %g*ngrn + %g*nblu\n", |
306 |
> |
printf("ro = %g*rn + %g*gn + %g*bn ;\n", |
307 |
|
solmat[0][0], solmat[1][0], solmat[2][0]); |
308 |
< |
printf("go = %g*nred + %g*ngrn + %g*nblu\n", |
308 |
> |
printf("go = %g*rn + %g*gn + %g*bn ;\n", |
309 |
|
solmat[0][1], solmat[1][1], solmat[2][1]); |
310 |
< |
printf("bo = %g*nred + %g*ngrn + %g*nblu\n", |
310 |
> |
printf("bo = %g*rn + %g*gn + %g*bn ;\n", |
311 |
|
solmat[0][2], solmat[1][2], solmat[2][2]); |
312 |
|
} |
313 |
|
|
314 |
|
|
315 |
|
compsoln(cin, cout, n) /* solve 3x3 system */ |
316 |
< |
float cin[][3], cout[][3]; |
316 |
> |
COLOR cin[], cout[]; |
317 |
|
int n; |
318 |
|
{ |
319 |
|
extern double mx3d_adjoint(), fabs(); |
328 |
|
} |
329 |
|
for (i = 0; i < 3; i++) |
330 |
|
for (j = 0; j < 3; j++) |
331 |
< |
mat[i][j] = cin[j][i]; |
331 |
> |
mat[i][j] = colval(cin[j],i); |
332 |
|
det = mx3d_adjoint(mat, invmat); |
333 |
|
if (fabs(det) < 1e-4) { |
334 |
|
fprintf(stderr, "%s: cannot compute color mapping\n", |
343 |
|
invmat[i][j] /= det; |
344 |
|
for (i = 0; i < 3; i++) { |
345 |
|
for (j = 0; j < 3; j++) |
346 |
< |
rowv[j] = cout[j][i]; |
346 |
> |
rowv[j] = colval(cout[j],i); |
347 |
|
mx3d_transform(rowv, invmat, colv); |
348 |
|
for (j = 0; j < 3; j++) |
349 |
|
solmat[j][i] = colv[j]; |
355 |
|
COLOR cout, cin; |
356 |
|
{ |
357 |
|
double r, g, b; |
340 |
– |
double r1, g1, b1; |
358 |
|
|
359 |
< |
r = bresp(colval(cin,RED)); |
360 |
< |
g = bresp(colval(cin,GRN)); |
361 |
< |
b = bresp(colval(cin,BLU)); |
362 |
< |
r1 = r*solmat[0][0] + g*solmat[1][0] + b*solmat[2][0]; |
363 |
< |
if (r1 < 0) r1 = 0; |
364 |
< |
g1 = r*solmat[0][1] + g*solmat[1][1] + b*solmat[2][1]; |
365 |
< |
if (g1 < 0) g1 = 0; |
366 |
< |
b1 = r*solmat[0][2] + g*solmat[1][2] + b*solmat[2][2]; |
367 |
< |
if (b1 < 0) b1 = 0; |
368 |
< |
setcolor(cout, r1, g1, b1); |
359 |
> |
bresp(cout, cin); |
360 |
> |
r = colval(cout,0)*solmat[0][0] + colval(cout,1)*solmat[1][0] |
361 |
> |
+ colval(cout,2)*solmat[2][0]; |
362 |
> |
if (r < 0) r = 0; |
363 |
> |
g = colval(cout,0)*solmat[0][1] + colval(cout,1)*solmat[1][1] |
364 |
> |
+ colval(cout,2)*solmat[2][1]; |
365 |
> |
if (g < 0) g = 0; |
366 |
> |
b = colval(cout,0)*solmat[0][2] + colval(cout,1)*solmat[1][2] |
367 |
> |
+ colval(cout,2)*solmat[2][2]; |
368 |
> |
if (b < 0) b = 0; |
369 |
> |
setcolor(cout, r, g, b); |
370 |
|
} |
371 |
|
|
372 |
|
|