ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tmapcolrs.c
Revision: 3.20
Committed: Wed Feb 23 00:08:08 2005 UTC (19 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R7P2, rad3R7P1
Changes since 3.19: +2 -2 lines
Log Message:
Made it so tmLoadPicture() opens files as binary under Windows

File Contents

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