ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tmapcolrs.c
Revision: 3.5
Committed: Thu Oct 8 16:31:02 1998 UTC (25 years, 6 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.4: +2 -4 lines
Log Message:
minor adjustment in tmCvColrs() when TM_F_BW is set

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     tmCvColrs(ls, cs, scan, len) /* tone map 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 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 greg 3.3 if (colrReg == -1) { /* build tables if necessary */
67     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     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     info = rhdefault; /* get our header */
187     getheader(inpf, headline, (char *)&info);
188     if (info.format == FMTBAD | info.expos <= 0. ||
189     fgetresolu(xp, yp, inpf) < 0) {
190     err = TM_E_BADFILE; goto done;
191     }
192     if (info.format == FMTUNK) /* assume RGBE format */
193     info.format = FMTRGB;
194     if (info.format == FMTRGB)
195     info.expos /= WHTEFFICACY;
196     else if (info.format == FMTCIE)
197     info.primp = TM_XYZPRIM;
198     /* prepare library */
199     if ((err = tmSetSpace(info.primp, 1./info.expos)) != TM_E_OK)
200     goto done;
201     err = TM_E_NOMEM; /* allocate arrays */
202     *lpp = (TMbright *)malloc(sizeof(TMbright) * *xp * *yp);
203     if (*lpp == NULL)
204     goto done;
205     if (cpp != TM_NOCHROMP) {
206     *cpp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp);
207     if (*cpp == NULL)
208     goto done;
209     }
210     scanin = (COLR *)malloc(sizeof(COLR) * *xp);
211     if (scanin == NULL)
212     goto done;
213     err = TM_E_BADFILE; /* read & convert scanlines */
214     for (i = 0; i < *yp; i++) {
215     if (freadcolrs(scanin, *xp, inpf) < 0) {
216     err = TM_E_BADFILE; break;
217     }
218     err = tmCvColrs(*lpp + (i * *xp),
219     cpp==TM_NOCHROMP ? TM_NOCHROM : *cpp + (i * 3 * *xp),
220     scanin, *xp);
221     if (err != TM_E_OK)
222     break;
223     }
224     done: /* clean up */
225     if (fp == NULL)
226     fclose(inpf);
227     if (scanin != NULL)
228     free((char *)scanin);
229     if (err != TM_E_OK)
230     returnErr(err);
231     returnOK;
232     }
233    
234    
235     int /* run pcond to map picture */
236     dopcond(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname)
237     BYTE **psp;
238     int *xp, *yp;
239     int flags;
240     RGBPRIMP monpri;
241     double gamval, Lddyn, Ldmax;
242     char *fname;
243     {
244     char *funcName = fname;
245     char cmdbuf[512];
246     FILE *infp;
247     register COLR *scan;
248     register BYTE *rp;
249     int y;
250     register int x;
251     /* set up gamma correction */
252     if (setcolrcor(pow, 1./gamval) < 0)
253     returnErr(TM_E_NOMEM);
254     /* create command */
255     strcpy(cmdbuf, "pcond ");
256     if (flags & TM_F_HCONTR)
257     strcat(cmdbuf, "-s ");
258     if (flags & TM_F_MESOPIC)
259     strcat(cmdbuf, "-c ");
260     if (flags & TM_F_LINEAR)
261     strcat(cmdbuf, "-l ");
262     if (flags & TM_F_ACUITY)
263     strcat(cmdbuf, "-a ");
264     if (flags & TM_F_VEIL)
265     strcat(cmdbuf, "-v ");
266     if (flags & TM_F_CWEIGHT)
267     strcat(cmdbuf, "-w ");
268     sprintf(cmdbuf+strlen(cmdbuf),
269     "-p %f %f %f %f %f %f %f %f -d %f -u %f %s",
270     monpri[RED][CIEX], monpri[RED][CIEY],
271     monpri[GRN][CIEX], monpri[GRN][CIEY],
272     monpri[BLU][CIEX], monpri[BLU][CIEY],
273     monpri[WHT][CIEX], monpri[WHT][CIEY],
274     Lddyn, Ldmax, fname);
275     /* start pcond */
276     if ((infp = popen(cmdbuf, "r")) == NULL)
277     returnErr(TM_E_BADFILE);
278     /* check picture format and size */
279     if (checkheader(infp, COLRFMT, NULL) < 0 ||
280     fgetresolu(xp, yp, infp) < 0) {
281     pclose(infp);
282     returnErr(TM_E_BADFILE);
283     }
284     /* allocate arrays */
285 greg 3.2 scan = (COLR *)malloc(sizeof(COLR) * *xp);
286 greg 3.1 if (flags & TM_F_BW)
287     rp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp);
288     else
289     rp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp);
290     if ((*psp = rp) == NULL | scan == NULL) {
291     pclose(infp);
292     returnErr(TM_E_NOMEM);
293     }
294     /* read and gamma map file */
295     for (y = 0; y < *yp; y++) {
296     if (freadcolrs(scan, *xp, infp) < 0) {
297     pclose(infp);
298 greg 3.2 free((char *)scan);
299     free((char *)*psp);
300     *psp = NULL;
301 greg 3.1 returnErr(TM_E_BADFILE);
302     }
303     colrs_gambs(scan, *xp);
304     if (flags & TM_F_BW)
305     for (x = 0; x < *xp; x++)
306     *rp++ = normbright(scan[x]);
307     else
308     for (x = 0; x < *xp; x++) {
309     *rp++ = scan[x][RED];
310     *rp++ = scan[x][GRN];
311     *rp++ = scan[x][BLU];
312     }
313     }
314     free((char *)scan);
315     pclose(infp);
316     returnOK;
317     }
318    
319    
320     int /* map a Radiance picture */
321     tmMapPicture(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname, fp)
322     BYTE **psp;
323     int *xp, *yp;
324     int flags;
325     RGBPRIMP monpri;
326     double gamval, Lddyn, Ldmax;
327     char *fname;
328     FILE *fp;
329     {
330     char *funcName = fname==NULL ? "tmMapPicture" : fname;
331     FILE *inpf;
332     BYTE *cp;
333     TMbright *lp;
334     int err;
335     /* check arguments */
336     if (psp == NULL | xp == NULL | yp == NULL | monpri == NULL |
337     (fname == NULL & fp == TM_GETFILE))
338     returnErr(TM_E_ILLEGAL);
339     /* set defaults */
340     if (gamval < MINGAM) gamval = DEFGAM;
341     if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN;
342     if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX;
343     if (flags & TM_F_BW) monpri = stdprims;
344     /* check for pcond run */
345     if (fp == TM_GETFILE && flags & TM_F_UNIMPL)
346     return( dopcond(psp, xp, yp, flags,
347     monpri, gamval, Lddyn, Ldmax, fname) );
348     /* initialize tone mapping */
349     if (tmInit(flags, monpri, gamval) == NULL)
350     returnErr(TM_E_NOMEM);
351     /* load & convert picture */
352     err = tmLoadPicture(&lp, (flags&TM_F_BW) ? TM_NOCHROMP : &cp,
353     xp, yp, fname, fp);
354     if (err != TM_E_OK) {
355     tmDone(NULL);
356     return(err);
357     }
358     /* allocate space for result */
359     if (flags & TM_F_BW) {
360     *psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp);
361 greg 3.2 if (*psp == NULL) {
362     free((char *)lp);
363     tmDone(NULL);
364 greg 3.1 returnErr(TM_E_NOMEM);
365 greg 3.2 }
366 greg 3.1 cp = TM_NOCHROM;
367     } else
368     *psp = cp;
369     /* compute color mapping */
370     err = tmAddHisto(lp, *xp * *yp, 1);
371     if (err != TM_E_OK)
372     goto done;
373     err = tmComputeMapping(gamval, Lddyn, Ldmax);
374     if (err != TM_E_OK)
375     goto done;
376     /* map colors */
377     err = tmMapPixels(*psp, lp, cp, *xp * *yp);
378    
379     done: /* clean up */
380     free((char *)lp);
381     tmDone(NULL);
382     if (err != TM_E_OK) { /* free memory on error */
383     free((char *)*psp);
384     *psp = NULL;
385     returnErr(err);
386     }
387     returnOK;
388 greg 3.3 }
389    
390    
391     static void
392     colrNewSpace(tms) /* color space changed for tone mapping */
393     register struct tmStruct *tms;
394     {
395     register COLRDATA *cd;
396     double d;
397    
398     cd = (COLRDATA *)tms->pd[colrReg];
399     cd->clfb[RED] = 256.*tms->clf[RED] + .5;
400     cd->clfb[GRN] = 256.*tms->clf[GRN] + .5;
401     cd->clfb[BLU] = 256.*tms->clf[BLU] + .5;
402     cd->clfb[EXP] = COLXS;
403     d = TM_BRTSCALE*log(tms->inpsf);
404     cd->inpsfb = d<0. ? d-.5 : d+.5;
405     }
406    
407    
408     static MEM_PTR
409     colrInit(tms) /* initialize private data for tone mapping */
410     register struct tmStruct *tms;
411     {
412     register COLRDATA *cd;
413     register int i;
414     /* allocate our data */
415     cd = (COLRDATA *)malloc(sizeof(COLRDATA));
416     if (cd == NULL)
417     return(NULL);
418     tms->pd[colrReg] = (MEM_PTR)cd;
419     /* compute gamma table */
420     for (i = GAMTSZ; i--; )
421     cd->gamb[i] = 256.*pow((i+.5)/GAMTSZ, 1./tms->mongam);
422     /* compute color and scale factors */
423     colrNewSpace(tms);
424     return((MEM_PTR)cd);
425 greg 3.1 }