ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tmapcolrs.c
Revision: 3.33
Committed: Fri Jul 19 17:37:56 2019 UTC (4 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.32: +2 -1 lines
Log Message:
Moved declarations and definitions for header.c from resolu.h to rtio.h

File Contents

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