ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tmapcolrs.c
Revision: 3.6
Committed: Thu Oct 22 13:53:19 1998 UTC (25 years, 6 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.5: +18 -12 lines
Log Message:
minor fixes

File Contents

# Content
1 /* Copyright (c) 1998 Silicon Graphics, Inc. */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ SGI";
5 #endif
6
7 /*
8 * Routines for tone mapping on Radiance RGBE and XYZE pictures.
9 * See tonemap.h for detailed function descriptions.
10 */
11
12 #include <stdio.h>
13 #include <math.h>
14 #include "tmprivat.h"
15 #include "resolu.h"
16
17
18 extern char *tempbuffer();
19
20 #define GAMTSZ 1024
21
22 typedef struct {
23 BYTE gamb[GAMTSZ]; /* gamma lookup table */
24 COLR clfb; /* encoded tm->clf */
25 TMbright inpsfb; /* encoded tm->inpsf */
26 } COLRDATA;
27
28 static MEM_PTR colrInit();
29 static void colrNewSpace();
30 extern void free();
31 static struct tmPackage colrPkg = { /* our package functions */
32 colrInit, colrNewSpace, free
33 };
34 static int colrReg = -1; /* our package registration number */
35
36 #define LOGISZ 260
37 static TMbright logi[LOGISZ];
38 static BYTE photofact[BMESUPPER-BMESLOWER];
39
40
41 int
42 tmCvColrs(ls, cs, scan, len) /* convert RGBE/XYZE colors */
43 TMbright *ls;
44 BYTE *cs;
45 COLR *scan;
46 int len;
47 {
48 static char funcName[] = "tmCvColrs";
49 COLR cmon;
50 register COLRDATA *cd;
51 register int i, bi, li;
52
53 if (tmTop == NULL)
54 returnErr(TM_E_TMINVAL);
55 if (ls == NULL | scan == NULL | len < 0)
56 returnErr(TM_E_ILLEGAL);
57 if (tmNeedMatrix(tmTop)) { /* need floating point */
58 register COLOR *newscan;
59 newscan = (COLOR *)tempbuffer(len*sizeof(COLOR));
60 if (newscan == NULL)
61 returnErr(TM_E_NOMEM);
62 for (i = len; i--; )
63 colr_color(newscan[i], scan[i]);
64 return(tmCvColors(ls, cs, newscan, len));
65 }
66 if (colrReg < 0) { /* build tables if necessary */
67 colrReg = tmRegPkg(&colrPkg);
68 if (colrReg < 0)
69 returnErr(TM_E_CODERR1);
70 for (i = 256; i--; )
71 logi[i] = TM_BRTSCALE*log((i+.5)/256.) - .5;
72 for (i = 256; i < LOGISZ; i++)
73 logi[i] = logi[255];
74 for (i = BMESLOWER; i < BMESUPPER; i++)
75 photofact[i-BMESLOWER] = 256. *
76 (tmLuminance(i) - LMESLOWER) /
77 (LMESUPPER - LMESLOWER);
78 }
79 if ((cd = (COLRDATA *)tmPkgData(tmTop,colrReg)) == NULL)
80 returnErr(TM_E_NOMEM);
81 for (i = len; i--; ) {
82 copycolr(cmon, scan[i]);
83 /* world luminance */
84 li = ( cd->clfb[RED]*cmon[RED] +
85 cd->clfb[GRN]*cmon[GRN] +
86 cd->clfb[BLU]*cmon[BLU] ) >> 8;
87 bi = BRT2SCALE*(cmon[EXP]-COLXS) +
88 logi[li] + cd->inpsfb;
89 if (bi < MINBRT) {
90 bi = MINBRT-1; /* bogus value */
91 li++; /* avoid li==0 */
92 }
93 ls[i] = bi;
94 if (cs == TM_NOCHROM) /* no color? */
95 continue;
96 if (tmTop->flags & TM_F_BW)
97 cmon[RED] = cmon[GRN] = cmon[BLU] = li;
98 /* mesopic adj. */
99 if (tmTop->flags & TM_F_MESOPIC && bi < BMESUPPER) {
100 register int pf, sli = normscot(cmon);
101 if (bi < BMESLOWER)
102 cmon[RED] = cmon[GRN] = cmon[BLU] = sli;
103 else {
104 pf = photofact[bi-BMESLOWER];
105 sli *= 256 - pf;
106 cmon[RED] = ( sli + pf*cmon[RED] ) >> 8;
107 cmon[GRN] = ( sli + pf*cmon[GRN] ) >> 8;
108 cmon[BLU] = ( sli + pf*cmon[BLU] ) >> 8;
109 }
110 }
111 bi = ( (int4)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 8;
112 cs[3*i ] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
113 bi = ( (int4)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 8;
114 cs[3*i+1] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
115 bi = ( (int4)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 8;
116 cs[3*i+2] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
117 }
118 returnOK;
119 }
120
121
122 #define FMTRGB 1 /* Input is RGBE */
123 #define FMTCIE 2 /* Input is CIE XYZE */
124 #define FMTUNK 3 /* Input format is unspecified */
125 #define FMTBAD 4 /* Input is not a recognized format */
126
127 static struct radhead {
128 int format; /* FMTRGB, FMTCIE, FMTUNK, FMTBAD */
129 double expos; /* input exposure value */
130 RGBPRIMP primp; /* input primaries */
131 RGBPRIMS mypri; /* custom primaries */
132 } rhdefault = {FMTUNK, 1., stdprims, STDPRIMS};
133
134
135 static int
136 headline(s, rh) /* grok a header line */
137 register char *s;
138 register struct radhead *rh;
139 {
140 char fmt[32];
141
142 if (formatval(fmt, s)) {
143 if (!strcmp(fmt, COLRFMT))
144 rh->format = FMTRGB;
145 else if (!strcmp(fmt, CIEFMT))
146 rh->format = FMTCIE;
147 else
148 rh->format = FMTBAD;
149 return;
150 }
151 if (isexpos(s)) {
152 rh->expos *= exposval(s);
153 return;
154 }
155 if (isprims(s)) {
156 primsval(rh->mypri, s);
157 rh->primp = rh->mypri;
158 return;
159 }
160 }
161
162
163 int
164 tmLoadPicture(lpp, cpp, xp, yp, fname, fp) /* convert Radiance picture */
165 TMbright **lpp;
166 BYTE **cpp;
167 int *xp, *yp;
168 char *fname;
169 FILE *fp;
170 {
171 char *funcName = fname==NULL ? "tmLoadPicture" : fname;
172 FILE *inpf;
173 struct radhead info;
174 int err;
175 COLR *scanin = NULL;
176 int i;
177 /* check arguments */
178 if (tmTop == NULL)
179 returnErr(TM_E_TMINVAL);
180 if (lpp == NULL | xp == NULL | yp == NULL |
181 (fname == NULL & fp == TM_GETFILE))
182 returnErr(TM_E_ILLEGAL);
183 *xp = *yp = 0; /* error precaution */
184 if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "r")) == NULL)
185 returnErr(TM_E_BADFILE);
186 *lpp = NULL;
187 if (cpp != TM_NOCHROMP) *cpp = NULL;
188 info = rhdefault; /* get our header */
189 getheader(inpf, headline, (MEM_PTR)&info);
190 if (info.format == FMTBAD | info.expos <= 0. ||
191 fgetresolu(xp, yp, inpf) < 0) {
192 err = TM_E_BADFILE; goto done;
193 }
194 if (info.format == FMTUNK) /* assume RGBE format */
195 info.format = FMTRGB;
196 if (info.format == FMTRGB)
197 info.expos /= WHTEFFICACY;
198 else if (info.format == FMTCIE)
199 info.primp = TM_XYZPRIM;
200 /* prepare library */
201 if ((err = tmSetSpace(info.primp, 1./info.expos)) != TM_E_OK)
202 goto done;
203 err = TM_E_NOMEM; /* allocate arrays */
204 *lpp = (TMbright *)malloc(sizeof(TMbright) * *xp * *yp);
205 if (*lpp == NULL)
206 goto done;
207 if (cpp != TM_NOCHROMP) {
208 *cpp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp);
209 if (*cpp == NULL)
210 goto done;
211 }
212 scanin = (COLR *)malloc(sizeof(COLR) * *xp);
213 if (scanin == NULL)
214 goto done;
215 err = TM_E_BADFILE; /* read & convert scanlines */
216 for (i = 0; i < *yp; i++) {
217 if (freadcolrs(scanin, *xp, inpf) < 0) {
218 err = TM_E_BADFILE; break;
219 }
220 err = tmCvColrs(*lpp + (i * *xp),
221 cpp==TM_NOCHROMP ? TM_NOCHROM : *cpp + (i * 3 * *xp),
222 scanin, *xp);
223 if (err != TM_E_OK)
224 break;
225 }
226 done: /* clean up */
227 if (fp == NULL)
228 fclose(inpf);
229 if (scanin != NULL)
230 free((MEM_PTR)scanin);
231 if (err != TM_E_OK) {
232 if (*lpp != NULL)
233 free((MEM_PTR)*lpp);
234 if (cpp != TM_NOCHROMP && *cpp != NULL)
235 free((MEM_PTR)*cpp);
236 returnErr(err);
237 }
238 returnOK;
239 }
240
241
242 int /* run pcond to map picture */
243 dopcond(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname)
244 BYTE **psp;
245 int *xp, *yp;
246 int flags;
247 RGBPRIMP monpri;
248 double gamval, Lddyn, Ldmax;
249 char *fname;
250 {
251 char *funcName = fname;
252 char cmdbuf[512];
253 FILE *infp;
254 register COLR *scan;
255 register BYTE *rp;
256 int y;
257 register int x;
258 /* set up gamma correction */
259 if (setcolrcor(pow, 1./gamval) < 0)
260 returnErr(TM_E_NOMEM);
261 /* create command */
262 strcpy(cmdbuf, "pcond ");
263 if (flags & TM_F_HCONTR)
264 strcat(cmdbuf, "-s ");
265 if (flags & TM_F_MESOPIC)
266 strcat(cmdbuf, "-c ");
267 if (flags & TM_F_LINEAR)
268 strcat(cmdbuf, "-l ");
269 if (flags & TM_F_ACUITY)
270 strcat(cmdbuf, "-a ");
271 if (flags & TM_F_VEIL)
272 strcat(cmdbuf, "-v ");
273 if (flags & TM_F_CWEIGHT)
274 strcat(cmdbuf, "-w ");
275 sprintf(cmdbuf+strlen(cmdbuf),
276 "-p %f %f %f %f %f %f %f %f -d %f -u %f %s",
277 monpri[RED][CIEX], monpri[RED][CIEY],
278 monpri[GRN][CIEX], monpri[GRN][CIEY],
279 monpri[BLU][CIEX], monpri[BLU][CIEY],
280 monpri[WHT][CIEX], monpri[WHT][CIEY],
281 Lddyn, Ldmax, fname);
282 /* start pcond */
283 if ((infp = popen(cmdbuf, "r")) == NULL)
284 returnErr(TM_E_BADFILE);
285 /* check picture format and size */
286 if (checkheader(infp, COLRFMT, NULL) < 0 ||
287 fgetresolu(xp, yp, infp) < 0) {
288 pclose(infp);
289 returnErr(TM_E_BADFILE);
290 }
291 /* allocate arrays */
292 scan = (COLR *)malloc(sizeof(COLR) * *xp);
293 if (flags & TM_F_BW)
294 rp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp);
295 else
296 rp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp);
297 if ((*psp = rp) == NULL | scan == NULL) {
298 pclose(infp);
299 returnErr(TM_E_NOMEM);
300 }
301 /* read and gamma map file */
302 for (y = 0; y < *yp; y++) {
303 if (freadcolrs(scan, *xp, infp) < 0) {
304 pclose(infp);
305 free((MEM_PTR)scan);
306 free((MEM_PTR)*psp);
307 *psp = NULL;
308 returnErr(TM_E_BADFILE);
309 }
310 colrs_gambs(scan, *xp);
311 if (flags & TM_F_BW)
312 for (x = 0; x < *xp; x++)
313 *rp++ = normbright(scan[x]);
314 else
315 for (x = 0; x < *xp; x++) {
316 *rp++ = scan[x][RED];
317 *rp++ = scan[x][GRN];
318 *rp++ = scan[x][BLU];
319 }
320 }
321 free((MEM_PTR)scan);
322 pclose(infp);
323 returnOK;
324 }
325
326
327 int /* map a Radiance picture */
328 tmMapPicture(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname, fp)
329 BYTE **psp;
330 int *xp, *yp;
331 int flags;
332 RGBPRIMP monpri;
333 double gamval, Lddyn, Ldmax;
334 char *fname;
335 FILE *fp;
336 {
337 char *funcName = fname==NULL ? "tmMapPicture" : fname;
338 BYTE *cp;
339 TMbright *lp;
340 int err;
341 /* check arguments */
342 if (psp == NULL | xp == NULL | yp == NULL | monpri == NULL |
343 (fname == NULL & fp == TM_GETFILE))
344 returnErr(TM_E_ILLEGAL);
345 /* set defaults */
346 if (gamval < MINGAM) gamval = DEFGAM;
347 if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN;
348 if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX;
349 if (flags & TM_F_BW) monpri = stdprims;
350 /* check for pcond run */
351 if (fp == TM_GETFILE && flags & TM_F_UNIMPL)
352 return( dopcond(psp, xp, yp, flags,
353 monpri, gamval, Lddyn, Ldmax, fname) );
354 /* initialize tone mapping */
355 if (tmInit(flags, monpri, gamval) == NULL)
356 returnErr(TM_E_NOMEM);
357 /* load & convert picture */
358 err = tmLoadPicture(&lp, (flags&TM_F_BW) ? TM_NOCHROMP : &cp,
359 xp, yp, fname, fp);
360 if (err != TM_E_OK) {
361 tmDone(NULL);
362 return(err);
363 }
364 /* allocate space for result */
365 if (flags & TM_F_BW) {
366 *psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp);
367 if (*psp == NULL) {
368 free((MEM_PTR)lp);
369 tmDone(NULL);
370 returnErr(TM_E_NOMEM);
371 }
372 cp = TM_NOCHROM;
373 } else
374 *psp = cp;
375 /* compute color mapping */
376 err = tmAddHisto(lp, *xp * *yp, 1);
377 if (err != TM_E_OK)
378 goto done;
379 err = tmComputeMapping(gamval, Lddyn, Ldmax);
380 if (err != TM_E_OK)
381 goto done;
382 /* map colors */
383 err = tmMapPixels(*psp, lp, cp, *xp * *yp);
384
385 done: /* clean up */
386 free((MEM_PTR)lp);
387 tmDone(NULL);
388 if (err != TM_E_OK) { /* free memory on error */
389 free((MEM_PTR)*psp);
390 *psp = NULL;
391 returnErr(err);
392 }
393 returnOK;
394 }
395
396
397 static void
398 colrNewSpace(tms) /* color space changed for tone mapping */
399 register struct tmStruct *tms;
400 {
401 register COLRDATA *cd;
402 double d;
403
404 cd = (COLRDATA *)tms->pd[colrReg];
405 cd->clfb[RED] = 256.*tms->clf[RED] + .5;
406 cd->clfb[GRN] = 256.*tms->clf[GRN] + .5;
407 cd->clfb[BLU] = 256.*tms->clf[BLU] + .5;
408 cd->clfb[EXP] = COLXS;
409 d = TM_BRTSCALE*log(tms->inpsf);
410 cd->inpsfb = d<0. ? d-.5 : d+.5;
411 }
412
413
414 static MEM_PTR
415 colrInit(tms) /* initialize private data for tone mapping */
416 register struct tmStruct *tms;
417 {
418 register COLRDATA *cd;
419 register int i;
420 /* allocate our data */
421 cd = (COLRDATA *)malloc(sizeof(COLRDATA));
422 if (cd == NULL)
423 return(NULL);
424 tms->pd[colrReg] = (MEM_PTR)cd;
425 /* compute gamma table */
426 for (i = GAMTSZ; i--; )
427 cd->gamb[i] = 256.*pow((i+.5)/GAMTSZ, 1./tms->mongam);
428 /* compute color and scale factors */
429 colrNewSpace(tms);
430 return((MEM_PTR)cd);
431 }