ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tmapcolrs.c
Revision: 3.38
Committed: Tue Sep 10 20:24:42 2024 UTC (7 months, 3 weeks ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.37: +29 -9 lines
Log Message:
feat(ra_bmp,ra_rgbe,ximage): Added quick conversion of spectral inputs to RGB

File Contents

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