ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tmapcolrs.c
Revision: 3.30
Committed: Tue Jun 28 21:11:04 2011 UTC (12 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad5R0, rad4R2, rad4R1, rad4R2P1
Changes since 3.29: +2 -2 lines
Log Message:
Minor compile fixes

File Contents

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