ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tmapcolrs.c
Revision: 3.8
Committed: Tue Oct 27 09:32:54 1998 UTC (25 years, 6 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.7: +4 -2 lines
Log Message:
fixed tone mapping for TM_F_BW back the way it was in 3.4

File Contents

# Content
1 /* Copyright (c) 1998 Silicon Graphics, Inc. */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ SGI";
5 #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 #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 #define LOGISZ 260
37 static TMbright logi[LOGISZ];
38 static BYTE photofact[BMESUPPER-BMESLOWER];
39
40
41 int
42 tmCvColrs(ls, cs, scan, len) /* convert 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 register COLRDATA *cd;
51 register int i, bi, li;
52
53 if (tmTop == NULL)
54 returnErr(TM_E_TMINVAL);
55 if (ls == NULL | scan == NULL | len < 0)
56 returnErr(TM_E_ILLEGAL);
57 if (tmNeedMatrix(tmTop)) { /* need floating point */
58 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 if (colrReg < 0) { /* build tables if necessary */
67 colrReg = tmRegPkg(&colrPkg);
68 if (colrReg < 0)
69 returnErr(TM_E_CODERR1);
70 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 if ((cd = (COLRDATA *)tmPkgData(tmTop,colrReg)) == NULL)
80 returnErr(TM_E_NOMEM);
81 for (i = len; i--; ) {
82 copycolr(cmon, scan[i]);
83 /* world luminance */
84 li = ( cd->clfb[RED]*cmon[RED] +
85 cd->clfb[GRN]*cmon[GRN] +
86 cd->clfb[BLU]*cmon[BLU] ) >> 8;
87 bi = BRT2SCALE*(cmon[EXP]-COLXS) +
88 logi[li] + cd->inpsfb;
89 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 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 }
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(0);
152 }
153 if (isexpos(s)) {
154 rh->expos *= exposval(s);
155 return(0);
156 }
157 if (isprims(s)) {
158 primsval(rh->mypri, s);
159 rh->primp = rh->mypri;
160 return(0);
161 }
162 return(0);
163 }
164
165
166 int
167 tmLoadPicture(lpp, cpp, xp, yp, fname, fp) /* convert Radiance picture */
168 TMbright **lpp;
169 BYTE **cpp;
170 int *xp, *yp;
171 char *fname;
172 FILE *fp;
173 {
174 char *funcName = fname==NULL ? "tmLoadPicture" : fname;
175 FILE *inpf;
176 struct radhead info;
177 int err;
178 COLR *scanin = NULL;
179 int i;
180 /* check arguments */
181 if (tmTop == NULL)
182 returnErr(TM_E_TMINVAL);
183 if (lpp == NULL | xp == NULL | yp == NULL |
184 (fname == NULL & fp == TM_GETFILE))
185 returnErr(TM_E_ILLEGAL);
186 *xp = *yp = 0; /* error precaution */
187 if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "r")) == NULL)
188 returnErr(TM_E_BADFILE);
189 *lpp = NULL;
190 if (cpp != TM_NOCHROMP) *cpp = NULL;
191 info = rhdefault; /* get our header */
192 getheader(inpf, headline, (MEM_PTR)&info);
193 if (info.format == FMTBAD | info.expos <= 0. ||
194 fgetresolu(xp, yp, inpf) < 0) {
195 err = TM_E_BADFILE; goto done;
196 }
197 if (info.format == FMTUNK) /* assume RGBE format */
198 info.format = FMTRGB;
199 if (info.format == FMTRGB)
200 info.expos /= WHTEFFICACY;
201 else if (info.format == FMTCIE)
202 info.primp = TM_XYZPRIM;
203 /* prepare library */
204 if ((err = tmSetSpace(info.primp, 1./info.expos)) != TM_E_OK)
205 goto done;
206 err = TM_E_NOMEM; /* allocate arrays */
207 *lpp = (TMbright *)malloc(sizeof(TMbright) * *xp * *yp);
208 if (*lpp == NULL)
209 goto done;
210 if (cpp != TM_NOCHROMP) {
211 *cpp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp);
212 if (*cpp == NULL)
213 goto done;
214 }
215 scanin = (COLR *)malloc(sizeof(COLR) * *xp);
216 if (scanin == NULL)
217 goto done;
218 err = TM_E_BADFILE; /* read & convert scanlines */
219 for (i = 0; i < *yp; i++) {
220 if (freadcolrs(scanin, *xp, inpf) < 0) {
221 err = TM_E_BADFILE; break;
222 }
223 err = tmCvColrs(*lpp + (i * *xp),
224 cpp==TM_NOCHROMP ? TM_NOCHROM : *cpp + (i * 3 * *xp),
225 scanin, *xp);
226 if (err != TM_E_OK)
227 break;
228 }
229 done: /* clean up */
230 if (fp == NULL)
231 fclose(inpf);
232 if (scanin != NULL)
233 free((MEM_PTR)scanin);
234 if (err != TM_E_OK) {
235 if (*lpp != NULL)
236 free((MEM_PTR)*lpp);
237 if (cpp != TM_NOCHROMP && *cpp != NULL)
238 free((MEM_PTR)*cpp);
239 returnErr(err);
240 }
241 returnOK;
242 }
243
244
245 int /* run pcond to map picture */
246 dopcond(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname)
247 BYTE **psp;
248 int *xp, *yp;
249 int flags;
250 RGBPRIMP monpri;
251 double gamval, Lddyn, Ldmax;
252 char *fname;
253 {
254 char *funcName = fname;
255 char cmdbuf[512];
256 FILE *infp;
257 register COLR *scan;
258 register BYTE *rp;
259 int y;
260 register int x;
261 /* set up gamma correction */
262 if (setcolrcor(pow, 1./gamval) < 0)
263 returnErr(TM_E_NOMEM);
264 /* create command */
265 strcpy(cmdbuf, "pcond ");
266 if (flags & TM_F_HCONTR)
267 strcat(cmdbuf, "-s ");
268 if (flags & TM_F_MESOPIC)
269 strcat(cmdbuf, "-c ");
270 if (flags & TM_F_LINEAR)
271 strcat(cmdbuf, "-l ");
272 if (flags & TM_F_ACUITY)
273 strcat(cmdbuf, "-a ");
274 if (flags & TM_F_VEIL)
275 strcat(cmdbuf, "-v ");
276 if (flags & TM_F_CWEIGHT)
277 strcat(cmdbuf, "-w ");
278 sprintf(cmdbuf+strlen(cmdbuf),
279 "-p %f %f %f %f %f %f %f %f -d %f -u %f %s",
280 monpri[RED][CIEX], monpri[RED][CIEY],
281 monpri[GRN][CIEX], monpri[GRN][CIEY],
282 monpri[BLU][CIEX], monpri[BLU][CIEY],
283 monpri[WHT][CIEX], monpri[WHT][CIEY],
284 Lddyn, Ldmax, fname);
285 /* start pcond */
286 if ((infp = popen(cmdbuf, "r")) == NULL)
287 returnErr(TM_E_BADFILE);
288 /* check picture format and size */
289 if (checkheader(infp, COLRFMT, NULL) < 0 ||
290 fgetresolu(xp, yp, infp) < 0) {
291 pclose(infp);
292 returnErr(TM_E_BADFILE);
293 }
294 /* allocate arrays */
295 scan = (COLR *)malloc(sizeof(COLR) * *xp);
296 if (flags & TM_F_BW)
297 rp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp);
298 else
299 rp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp);
300 if ((*psp = rp) == NULL | scan == NULL) {
301 pclose(infp);
302 returnErr(TM_E_NOMEM);
303 }
304 /* read and gamma map file */
305 for (y = 0; y < *yp; y++) {
306 if (freadcolrs(scan, *xp, infp) < 0) {
307 pclose(infp);
308 free((MEM_PTR)scan);
309 free((MEM_PTR)*psp);
310 *psp = NULL;
311 returnErr(TM_E_BADFILE);
312 }
313 colrs_gambs(scan, *xp);
314 if (flags & TM_F_BW)
315 for (x = 0; x < *xp; x++)
316 *rp++ = normbright(scan[x]);
317 else
318 for (x = 0; x < *xp; x++) {
319 *rp++ = scan[x][RED];
320 *rp++ = scan[x][GRN];
321 *rp++ = scan[x][BLU];
322 }
323 }
324 free((MEM_PTR)scan);
325 pclose(infp);
326 returnOK;
327 }
328
329
330 int /* map a Radiance picture */
331 tmMapPicture(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname, fp)
332 BYTE **psp;
333 int *xp, *yp;
334 int flags;
335 RGBPRIMP monpri;
336 double gamval, Lddyn, Ldmax;
337 char *fname;
338 FILE *fp;
339 {
340 char *funcName = fname==NULL ? "tmMapPicture" : fname;
341 BYTE *cp;
342 TMbright *lp;
343 int err;
344 /* check arguments */
345 if (psp == NULL | xp == NULL | yp == NULL | monpri == NULL |
346 (fname == NULL & fp == TM_GETFILE))
347 returnErr(TM_E_ILLEGAL);
348 /* set defaults */
349 if (gamval < MINGAM) gamval = DEFGAM;
350 if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN;
351 if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX;
352 if (flags & TM_F_BW) monpri = stdprims;
353 /* check for pcond run */
354 if (fp == TM_GETFILE && flags & TM_F_UNIMPL)
355 return( dopcond(psp, xp, yp, flags,
356 monpri, gamval, Lddyn, Ldmax, fname) );
357 /* initialize tone mapping */
358 if (tmInit(flags, monpri, gamval) == NULL)
359 returnErr(TM_E_NOMEM);
360 /* load & convert picture */
361 err = tmLoadPicture(&lp, (flags&TM_F_BW) ? TM_NOCHROMP : &cp,
362 xp, yp, fname, fp);
363 if (err != TM_E_OK) {
364 tmDone(NULL);
365 return(err);
366 }
367 /* allocate space for result */
368 if (flags & TM_F_BW) {
369 *psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp);
370 if (*psp == NULL) {
371 free((MEM_PTR)lp);
372 tmDone(NULL);
373 returnErr(TM_E_NOMEM);
374 }
375 cp = TM_NOCHROM;
376 } else
377 *psp = cp;
378 /* compute color mapping */
379 err = tmAddHisto(lp, *xp * *yp, 1);
380 if (err != TM_E_OK)
381 goto done;
382 err = tmComputeMapping(gamval, Lddyn, Ldmax);
383 if (err != TM_E_OK)
384 goto done;
385 /* map colors */
386 err = tmMapPixels(*psp, lp, cp, *xp * *yp);
387
388 done: /* clean up */
389 free((MEM_PTR)lp);
390 tmDone(NULL);
391 if (err != TM_E_OK) { /* free memory on error */
392 free((MEM_PTR)*psp);
393 *psp = NULL;
394 returnErr(err);
395 }
396 returnOK;
397 }
398
399
400 static void
401 colrNewSpace(tms) /* color space changed for tone mapping */
402 register struct tmStruct *tms;
403 {
404 register COLRDATA *cd;
405 double d;
406
407 cd = (COLRDATA *)tms->pd[colrReg];
408 cd->clfb[RED] = 256.*tms->clf[RED] + .5;
409 cd->clfb[GRN] = 256.*tms->clf[GRN] + .5;
410 cd->clfb[BLU] = 256.*tms->clf[BLU] + .5;
411 cd->clfb[EXP] = COLXS;
412 d = TM_BRTSCALE*log(tms->inpsf);
413 cd->inpsfb = d<0. ? d-.5 : d+.5;
414 }
415
416
417 static MEM_PTR
418 colrInit(tms) /* initialize private data for tone mapping */
419 register struct tmStruct *tms;
420 {
421 register COLRDATA *cd;
422 register int i;
423 /* allocate our data */
424 cd = (COLRDATA *)malloc(sizeof(COLRDATA));
425 if (cd == NULL)
426 return(NULL);
427 tms->pd[colrReg] = (MEM_PTR)cd;
428 /* compute gamma table */
429 for (i = GAMTSZ; i--; )
430 cd->gamb[i] = 256.*pow((i+.5)/GAMTSZ, 1./tms->mongam);
431 /* compute color and scale factors */
432 colrNewSpace(tms);
433 return((MEM_PTR)cd);
434 }