ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tmapcolrs.c
Revision: 3.4
Committed: Mon Aug 17 17:58:46 1998 UTC (25 years, 8 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.3: +3 -3 lines
Log Message:
took "color.h" include out of tonemap.h and put in tmprivat.h
changed routines so zero-length arrays don't return error

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     /* mesopic adj. */
97     if (tmTop->flags & TM_F_MESOPIC && bi < BMESUPPER) {
98     register int pf, sli = normscot(cmon);
99     if (bi < BMESLOWER)
100     cmon[RED] = cmon[GRN] = cmon[BLU] = sli;
101     else {
102     if (tmTop->flags & TM_F_BW)
103     cmon[RED] = cmon[GRN] = cmon[BLU] = li;
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     } else if (tmTop->flags & TM_F_BW) {
111     cmon[RED] = cmon[GRN] = cmon[BLU] = li;
112     }
113 greg 3.3 bi = ( (int4)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 8;
114     cs[3*i ] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
115     bi = ( (int4)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 8;
116     cs[3*i+1] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
117     bi = ( (int4)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 8;
118     cs[3*i+2] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
119 greg 3.1 }
120     returnOK;
121     }
122    
123    
124     #define FMTRGB 1 /* Input is RGBE */
125     #define FMTCIE 2 /* Input is CIE XYZE */
126     #define FMTUNK 3 /* Input format is unspecified */
127     #define FMTBAD 4 /* Input is not a recognized format */
128    
129     static struct radhead {
130     int format; /* FMTRGB, FMTCIE, FMTUNK, FMTBAD */
131     double expos; /* input exposure value */
132     RGBPRIMP primp; /* input primaries */
133     RGBPRIMS mypri; /* custom primaries */
134     } rhdefault = {FMTUNK, 1., stdprims, STDPRIMS};
135    
136    
137     static int
138     headline(s, rh) /* grok a header line */
139     register char *s;
140     register struct radhead *rh;
141     {
142     char fmt[32];
143    
144     if (formatval(fmt, s)) {
145     if (!strcmp(fmt, COLRFMT))
146     rh->format = FMTRGB;
147     else if (!strcmp(fmt, CIEFMT))
148     rh->format = FMTCIE;
149     else
150     rh->format = FMTBAD;
151     return;
152     }
153     if (isexpos(s)) {
154     rh->expos *= exposval(s);
155     return;
156     }
157     if (isprims(s)) {
158     primsval(rh->mypri, s);
159     rh->primp = rh->mypri;
160     return;
161     }
162     }
163    
164    
165     int
166     tmLoadPicture(lpp, cpp, xp, yp, fname, fp) /* convert Radiance picture */
167     TMbright **lpp;
168     BYTE **cpp;
169     int *xp, *yp;
170     char *fname;
171     FILE *fp;
172     {
173     char *funcName = fname==NULL ? "tmLoadPicture" : fname;
174     FILE *inpf;
175     struct radhead info;
176     int err;
177     COLR *scanin = NULL;
178     int i;
179     /* check arguments */
180     if (tmTop == NULL)
181     returnErr(TM_E_TMINVAL);
182     if (lpp == NULL | xp == NULL | yp == NULL |
183     (fname == NULL & fp == TM_GETFILE))
184     returnErr(TM_E_ILLEGAL);
185     *xp = *yp = 0; /* error precaution */
186     if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "r")) == NULL)
187     returnErr(TM_E_BADFILE);
188     info = rhdefault; /* get our header */
189     getheader(inpf, headline, (char *)&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((char *)scanin);
231     if (err != TM_E_OK)
232     returnErr(err);
233     returnOK;
234     }
235    
236    
237     int /* run pcond to map picture */
238     dopcond(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname)
239     BYTE **psp;
240     int *xp, *yp;
241     int flags;
242     RGBPRIMP monpri;
243     double gamval, Lddyn, Ldmax;
244     char *fname;
245     {
246     char *funcName = fname;
247     char cmdbuf[512];
248     FILE *infp;
249     register COLR *scan;
250     register BYTE *rp;
251     int y;
252     register int x;
253     /* set up gamma correction */
254     if (setcolrcor(pow, 1./gamval) < 0)
255     returnErr(TM_E_NOMEM);
256     /* create command */
257     strcpy(cmdbuf, "pcond ");
258     if (flags & TM_F_HCONTR)
259     strcat(cmdbuf, "-s ");
260     if (flags & TM_F_MESOPIC)
261     strcat(cmdbuf, "-c ");
262     if (flags & TM_F_LINEAR)
263     strcat(cmdbuf, "-l ");
264     if (flags & TM_F_ACUITY)
265     strcat(cmdbuf, "-a ");
266     if (flags & TM_F_VEIL)
267     strcat(cmdbuf, "-v ");
268     if (flags & TM_F_CWEIGHT)
269     strcat(cmdbuf, "-w ");
270     sprintf(cmdbuf+strlen(cmdbuf),
271     "-p %f %f %f %f %f %f %f %f -d %f -u %f %s",
272     monpri[RED][CIEX], monpri[RED][CIEY],
273     monpri[GRN][CIEX], monpri[GRN][CIEY],
274     monpri[BLU][CIEX], monpri[BLU][CIEY],
275     monpri[WHT][CIEX], monpri[WHT][CIEY],
276     Lddyn, Ldmax, fname);
277     /* start pcond */
278     if ((infp = popen(cmdbuf, "r")) == NULL)
279     returnErr(TM_E_BADFILE);
280     /* check picture format and size */
281     if (checkheader(infp, COLRFMT, NULL) < 0 ||
282     fgetresolu(xp, yp, infp) < 0) {
283     pclose(infp);
284     returnErr(TM_E_BADFILE);
285     }
286     /* allocate arrays */
287 greg 3.2 scan = (COLR *)malloc(sizeof(COLR) * *xp);
288 greg 3.1 if (flags & TM_F_BW)
289     rp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp);
290     else
291     rp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp);
292     if ((*psp = rp) == NULL | scan == NULL) {
293     pclose(infp);
294     returnErr(TM_E_NOMEM);
295     }
296     /* read and gamma map file */
297     for (y = 0; y < *yp; y++) {
298     if (freadcolrs(scan, *xp, infp) < 0) {
299     pclose(infp);
300 greg 3.2 free((char *)scan);
301     free((char *)*psp);
302     *psp = NULL;
303 greg 3.1 returnErr(TM_E_BADFILE);
304     }
305     colrs_gambs(scan, *xp);
306     if (flags & TM_F_BW)
307     for (x = 0; x < *xp; x++)
308     *rp++ = normbright(scan[x]);
309     else
310     for (x = 0; x < *xp; x++) {
311     *rp++ = scan[x][RED];
312     *rp++ = scan[x][GRN];
313     *rp++ = scan[x][BLU];
314     }
315     }
316     free((char *)scan);
317     pclose(infp);
318     returnOK;
319     }
320    
321    
322     int /* map a Radiance picture */
323     tmMapPicture(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname, fp)
324     BYTE **psp;
325     int *xp, *yp;
326     int flags;
327     RGBPRIMP monpri;
328     double gamval, Lddyn, Ldmax;
329     char *fname;
330     FILE *fp;
331     {
332     char *funcName = fname==NULL ? "tmMapPicture" : fname;
333     FILE *inpf;
334     BYTE *cp;
335     TMbright *lp;
336     int err;
337     /* check arguments */
338     if (psp == NULL | xp == NULL | yp == NULL | monpri == NULL |
339     (fname == NULL & fp == TM_GETFILE))
340     returnErr(TM_E_ILLEGAL);
341     /* set defaults */
342     if (gamval < MINGAM) gamval = DEFGAM;
343     if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN;
344     if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX;
345     if (flags & TM_F_BW) monpri = stdprims;
346     /* check for pcond run */
347     if (fp == TM_GETFILE && flags & TM_F_UNIMPL)
348     return( dopcond(psp, xp, yp, flags,
349     monpri, gamval, Lddyn, Ldmax, fname) );
350     /* initialize tone mapping */
351     if (tmInit(flags, monpri, gamval) == NULL)
352     returnErr(TM_E_NOMEM);
353     /* load & convert picture */
354     err = tmLoadPicture(&lp, (flags&TM_F_BW) ? TM_NOCHROMP : &cp,
355     xp, yp, fname, fp);
356     if (err != TM_E_OK) {
357     tmDone(NULL);
358     return(err);
359     }
360     /* allocate space for result */
361     if (flags & TM_F_BW) {
362     *psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp);
363 greg 3.2 if (*psp == NULL) {
364     free((char *)lp);
365     tmDone(NULL);
366 greg 3.1 returnErr(TM_E_NOMEM);
367 greg 3.2 }
368 greg 3.1 cp = TM_NOCHROM;
369     } else
370     *psp = cp;
371     /* compute color mapping */
372     err = tmAddHisto(lp, *xp * *yp, 1);
373     if (err != TM_E_OK)
374     goto done;
375     err = tmComputeMapping(gamval, Lddyn, Ldmax);
376     if (err != TM_E_OK)
377     goto done;
378     /* map colors */
379     err = tmMapPixels(*psp, lp, cp, *xp * *yp);
380    
381     done: /* clean up */
382     free((char *)lp);
383     tmDone(NULL);
384     if (err != TM_E_OK) { /* free memory on error */
385     free((char *)*psp);
386     *psp = NULL;
387     returnErr(err);
388     }
389     returnOK;
390 greg 3.3 }
391    
392    
393     static void
394     colrNewSpace(tms) /* color space changed for tone mapping */
395     register struct tmStruct *tms;
396     {
397     register COLRDATA *cd;
398     double d;
399    
400     cd = (COLRDATA *)tms->pd[colrReg];
401     cd->clfb[RED] = 256.*tms->clf[RED] + .5;
402     cd->clfb[GRN] = 256.*tms->clf[GRN] + .5;
403     cd->clfb[BLU] = 256.*tms->clf[BLU] + .5;
404     cd->clfb[EXP] = COLXS;
405     d = TM_BRTSCALE*log(tms->inpsf);
406     cd->inpsfb = d<0. ? d-.5 : d+.5;
407     }
408    
409    
410     static MEM_PTR
411     colrInit(tms) /* initialize private data for tone mapping */
412     register struct tmStruct *tms;
413     {
414     register COLRDATA *cd;
415     register int i;
416     /* allocate our data */
417     cd = (COLRDATA *)malloc(sizeof(COLRDATA));
418     if (cd == NULL)
419     return(NULL);
420     tms->pd[colrReg] = (MEM_PTR)cd;
421     /* compute gamma table */
422     for (i = GAMTSZ; i--; )
423     cd->gamb[i] = 256.*pow((i+.5)/GAMTSZ, 1./tms->mongam);
424     /* compute color and scale factors */
425     colrNewSpace(tms);
426     return((MEM_PTR)cd);
427 greg 3.1 }