ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tmapcolrs.c
Revision: 3.15
Committed: Fri Jan 2 11:36:26 2004 UTC (20 years, 3 months ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R6
Changes since 3.14: +9 -10 lines
Log Message:
Fixed typing/prototype of getheader() and its callback.

File Contents

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