ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tmapcolrs.c
Revision: 3.7
Committed: Tue Oct 27 09:16:53 1998 UTC (25 years, 6 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.6: +4 -3 lines
Log Message:
changed getheader() to listen to return value of passed function

File Contents

# User Rev Content
1 gwlarson 3.4 /* Copyright (c) 1998 Silicon Graphics, Inc. */
2 greg 3.1
3     #ifndef lint
4 gwlarson 3.4 static char SCCSid[] = "$SunId$ SGI";
5 greg 3.1 #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 greg 3.3 #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 greg 3.1 #define LOGISZ 260
37     static TMbright logi[LOGISZ];
38     static BYTE photofact[BMESUPPER-BMESLOWER];
39    
40    
41     int
42 gwlarson 3.6 tmCvColrs(ls, cs, scan, len) /* convert RGBE/XYZE colors */
43 greg 3.1 TMbright *ls;
44     BYTE *cs;
45     COLR *scan;
46     int len;
47     {
48     static char funcName[] = "tmCvColrs";
49     COLR cmon;
50 greg 3.3 register COLRDATA *cd;
51 greg 3.1 register int i, bi, li;
52    
53     if (tmTop == NULL)
54     returnErr(TM_E_TMINVAL);
55 gwlarson 3.4 if (ls == NULL | scan == NULL | len < 0)
56 greg 3.1 returnErr(TM_E_ILLEGAL);
57 greg 3.3 if (tmNeedMatrix(tmTop)) { /* need floating point */
58 greg 3.1 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 gwlarson 3.6 if (colrReg < 0) { /* build tables if necessary */
67 greg 3.3 colrReg = tmRegPkg(&colrPkg);
68     if (colrReg < 0)
69     returnErr(TM_E_CODERR1);
70 greg 3.1 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 greg 3.3 if ((cd = (COLRDATA *)tmPkgData(tmTop,colrReg)) == NULL)
80     returnErr(TM_E_NOMEM);
81 greg 3.1 for (i = len; i--; ) {
82     copycolr(cmon, scan[i]);
83     /* world luminance */
84 greg 3.3 li = ( cd->clfb[RED]*cmon[RED] +
85     cd->clfb[GRN]*cmon[GRN] +
86     cd->clfb[BLU]*cmon[BLU] ) >> 8;
87 greg 3.1 bi = BRT2SCALE*(cmon[EXP]-COLXS) +
88 greg 3.3 logi[li] + cd->inpsfb;
89 greg 3.1 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 gwlarson 3.5 if (tmTop->flags & TM_F_BW)
97     cmon[RED] = cmon[GRN] = cmon[BLU] = li;
98 greg 3.1 /* 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 greg 3.3 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 greg 3.1 }
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 gwlarson 3.7 return(0);
150 greg 3.1 }
151     if (isexpos(s)) {
152     rh->expos *= exposval(s);
153 gwlarson 3.7 return(0);
154 greg 3.1 }
155     if (isprims(s)) {
156     primsval(rh->mypri, s);
157     rh->primp = rh->mypri;
158 gwlarson 3.7 return(0);
159 greg 3.1 }
160 gwlarson 3.7 return(0);
161 greg 3.1 }
162    
163    
164     int
165     tmLoadPicture(lpp, cpp, xp, yp, fname, fp) /* convert Radiance picture */
166     TMbright **lpp;
167     BYTE **cpp;
168     int *xp, *yp;
169     char *fname;
170     FILE *fp;
171     {
172     char *funcName = fname==NULL ? "tmLoadPicture" : fname;
173     FILE *inpf;
174     struct radhead info;
175     int err;
176     COLR *scanin = NULL;
177     int i;
178     /* check arguments */
179     if (tmTop == NULL)
180     returnErr(TM_E_TMINVAL);
181     if (lpp == NULL | xp == NULL | yp == NULL |
182     (fname == NULL & fp == TM_GETFILE))
183     returnErr(TM_E_ILLEGAL);
184     *xp = *yp = 0; /* error precaution */
185     if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "r")) == NULL)
186     returnErr(TM_E_BADFILE);
187 gwlarson 3.6 *lpp = NULL;
188     if (cpp != TM_NOCHROMP) *cpp = NULL;
189 greg 3.1 info = rhdefault; /* get our header */
190 gwlarson 3.6 getheader(inpf, headline, (MEM_PTR)&info);
191 greg 3.1 if (info.format == FMTBAD | info.expos <= 0. ||
192     fgetresolu(xp, yp, inpf) < 0) {
193     err = TM_E_BADFILE; goto done;
194     }
195     if (info.format == FMTUNK) /* assume RGBE format */
196     info.format = FMTRGB;
197     if (info.format == FMTRGB)
198     info.expos /= WHTEFFICACY;
199     else if (info.format == FMTCIE)
200     info.primp = TM_XYZPRIM;
201     /* prepare library */
202     if ((err = tmSetSpace(info.primp, 1./info.expos)) != TM_E_OK)
203     goto done;
204     err = TM_E_NOMEM; /* allocate arrays */
205     *lpp = (TMbright *)malloc(sizeof(TMbright) * *xp * *yp);
206     if (*lpp == NULL)
207     goto done;
208     if (cpp != TM_NOCHROMP) {
209     *cpp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp);
210     if (*cpp == NULL)
211     goto done;
212     }
213     scanin = (COLR *)malloc(sizeof(COLR) * *xp);
214     if (scanin == NULL)
215     goto done;
216     err = TM_E_BADFILE; /* read & convert scanlines */
217     for (i = 0; i < *yp; i++) {
218     if (freadcolrs(scanin, *xp, inpf) < 0) {
219     err = TM_E_BADFILE; break;
220     }
221     err = tmCvColrs(*lpp + (i * *xp),
222     cpp==TM_NOCHROMP ? TM_NOCHROM : *cpp + (i * 3 * *xp),
223     scanin, *xp);
224     if (err != TM_E_OK)
225     break;
226     }
227     done: /* clean up */
228     if (fp == NULL)
229     fclose(inpf);
230     if (scanin != NULL)
231 gwlarson 3.6 free((MEM_PTR)scanin);
232     if (err != TM_E_OK) {
233     if (*lpp != NULL)
234     free((MEM_PTR)*lpp);
235     if (cpp != TM_NOCHROMP && *cpp != NULL)
236     free((MEM_PTR)*cpp);
237 greg 3.1 returnErr(err);
238 gwlarson 3.6 }
239 greg 3.1 returnOK;
240     }
241    
242    
243     int /* run pcond to map picture */
244     dopcond(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname)
245     BYTE **psp;
246     int *xp, *yp;
247     int flags;
248     RGBPRIMP monpri;
249     double gamval, Lddyn, Ldmax;
250     char *fname;
251     {
252     char *funcName = fname;
253     char cmdbuf[512];
254     FILE *infp;
255     register COLR *scan;
256     register BYTE *rp;
257     int y;
258     register int x;
259     /* set up gamma correction */
260     if (setcolrcor(pow, 1./gamval) < 0)
261     returnErr(TM_E_NOMEM);
262     /* create command */
263     strcpy(cmdbuf, "pcond ");
264     if (flags & TM_F_HCONTR)
265     strcat(cmdbuf, "-s ");
266     if (flags & TM_F_MESOPIC)
267     strcat(cmdbuf, "-c ");
268     if (flags & TM_F_LINEAR)
269     strcat(cmdbuf, "-l ");
270     if (flags & TM_F_ACUITY)
271     strcat(cmdbuf, "-a ");
272     if (flags & TM_F_VEIL)
273     strcat(cmdbuf, "-v ");
274     if (flags & TM_F_CWEIGHT)
275     strcat(cmdbuf, "-w ");
276     sprintf(cmdbuf+strlen(cmdbuf),
277     "-p %f %f %f %f %f %f %f %f -d %f -u %f %s",
278     monpri[RED][CIEX], monpri[RED][CIEY],
279     monpri[GRN][CIEX], monpri[GRN][CIEY],
280     monpri[BLU][CIEX], monpri[BLU][CIEY],
281     monpri[WHT][CIEX], monpri[WHT][CIEY],
282     Lddyn, Ldmax, fname);
283     /* start pcond */
284     if ((infp = popen(cmdbuf, "r")) == NULL)
285     returnErr(TM_E_BADFILE);
286     /* check picture format and size */
287     if (checkheader(infp, COLRFMT, NULL) < 0 ||
288     fgetresolu(xp, yp, infp) < 0) {
289     pclose(infp);
290     returnErr(TM_E_BADFILE);
291     }
292     /* allocate arrays */
293 greg 3.2 scan = (COLR *)malloc(sizeof(COLR) * *xp);
294 greg 3.1 if (flags & TM_F_BW)
295     rp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp);
296     else
297     rp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp);
298     if ((*psp = rp) == NULL | scan == NULL) {
299     pclose(infp);
300     returnErr(TM_E_NOMEM);
301     }
302     /* read and gamma map file */
303     for (y = 0; y < *yp; y++) {
304     if (freadcolrs(scan, *xp, infp) < 0) {
305     pclose(infp);
306 gwlarson 3.6 free((MEM_PTR)scan);
307     free((MEM_PTR)*psp);
308 greg 3.2 *psp = NULL;
309 greg 3.1 returnErr(TM_E_BADFILE);
310     }
311     colrs_gambs(scan, *xp);
312     if (flags & TM_F_BW)
313     for (x = 0; x < *xp; x++)
314     *rp++ = normbright(scan[x]);
315     else
316     for (x = 0; x < *xp; x++) {
317     *rp++ = scan[x][RED];
318     *rp++ = scan[x][GRN];
319     *rp++ = scan[x][BLU];
320     }
321     }
322 gwlarson 3.6 free((MEM_PTR)scan);
323 greg 3.1 pclose(infp);
324     returnOK;
325     }
326    
327    
328     int /* map a Radiance picture */
329     tmMapPicture(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname, fp)
330     BYTE **psp;
331     int *xp, *yp;
332     int flags;
333     RGBPRIMP monpri;
334     double gamval, Lddyn, Ldmax;
335     char *fname;
336     FILE *fp;
337     {
338     char *funcName = fname==NULL ? "tmMapPicture" : fname;
339     BYTE *cp;
340     TMbright *lp;
341     int err;
342     /* check arguments */
343     if (psp == NULL | xp == NULL | yp == NULL | monpri == NULL |
344     (fname == NULL & fp == TM_GETFILE))
345     returnErr(TM_E_ILLEGAL);
346     /* set defaults */
347     if (gamval < MINGAM) gamval = DEFGAM;
348     if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN;
349     if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX;
350     if (flags & TM_F_BW) monpri = stdprims;
351     /* check for pcond run */
352     if (fp == TM_GETFILE && flags & TM_F_UNIMPL)
353     return( dopcond(psp, xp, yp, flags,
354     monpri, gamval, Lddyn, Ldmax, fname) );
355     /* initialize tone mapping */
356     if (tmInit(flags, monpri, gamval) == NULL)
357     returnErr(TM_E_NOMEM);
358     /* load & convert picture */
359     err = tmLoadPicture(&lp, (flags&TM_F_BW) ? TM_NOCHROMP : &cp,
360     xp, yp, fname, fp);
361     if (err != TM_E_OK) {
362     tmDone(NULL);
363     return(err);
364     }
365     /* allocate space for result */
366     if (flags & TM_F_BW) {
367     *psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp);
368 greg 3.2 if (*psp == NULL) {
369 gwlarson 3.6 free((MEM_PTR)lp);
370 greg 3.2 tmDone(NULL);
371 greg 3.1 returnErr(TM_E_NOMEM);
372 greg 3.2 }
373 greg 3.1 cp = TM_NOCHROM;
374     } else
375     *psp = cp;
376     /* compute color mapping */
377     err = tmAddHisto(lp, *xp * *yp, 1);
378     if (err != TM_E_OK)
379     goto done;
380     err = tmComputeMapping(gamval, Lddyn, Ldmax);
381     if (err != TM_E_OK)
382     goto done;
383     /* map colors */
384     err = tmMapPixels(*psp, lp, cp, *xp * *yp);
385    
386     done: /* clean up */
387 gwlarson 3.6 free((MEM_PTR)lp);
388 greg 3.1 tmDone(NULL);
389     if (err != TM_E_OK) { /* free memory on error */
390 gwlarson 3.6 free((MEM_PTR)*psp);
391 greg 3.1 *psp = NULL;
392     returnErr(err);
393     }
394     returnOK;
395 greg 3.3 }
396    
397    
398     static void
399     colrNewSpace(tms) /* color space changed for tone mapping */
400     register struct tmStruct *tms;
401     {
402     register COLRDATA *cd;
403     double d;
404    
405     cd = (COLRDATA *)tms->pd[colrReg];
406     cd->clfb[RED] = 256.*tms->clf[RED] + .5;
407     cd->clfb[GRN] = 256.*tms->clf[GRN] + .5;
408     cd->clfb[BLU] = 256.*tms->clf[BLU] + .5;
409     cd->clfb[EXP] = COLXS;
410     d = TM_BRTSCALE*log(tms->inpsf);
411     cd->inpsfb = d<0. ? d-.5 : d+.5;
412     }
413    
414    
415     static MEM_PTR
416     colrInit(tms) /* initialize private data for tone mapping */
417     register struct tmStruct *tms;
418     {
419     register COLRDATA *cd;
420     register int i;
421     /* allocate our data */
422     cd = (COLRDATA *)malloc(sizeof(COLRDATA));
423     if (cd == NULL)
424     return(NULL);
425     tms->pd[colrReg] = (MEM_PTR)cd;
426     /* compute gamma table */
427     for (i = GAMTSZ; i--; )
428     cd->gamb[i] = 256.*pow((i+.5)/GAMTSZ, 1./tms->mongam);
429     /* compute color and scale factors */
430     colrNewSpace(tms);
431     return((MEM_PTR)cd);
432 greg 3.1 }