--- ray/src/common/tmapcolrs.c 1997/04/16 20:28:05 3.2
+++ ray/src/common/tmapcolrs.c 2003/02/22 02:07:22 3.10
@@ -1,29 +1,106 @@
-/* Copyright (c) 1997 Regents of the University of California */
-
#ifndef lint
-static char SCCSid[] = "$SunId$ LBL";
+static const char RCSid[] = "$Id: tmapcolrs.c,v 3.10 2003/02/22 02:07:22 greg Exp $";
#endif
-
/*
* Routines for tone mapping on Radiance RGBE and XYZE pictures.
- * See tonemap.h for detailed function descriptions.
+ *
+ * Externals declared in tonemap.h
*/
+/* ====================================================================
+ * The Radiance Software License, Version 1.0
+ *
+ * Copyright (c) 1990 - 2002 The Regents of the University of California,
+ * through Lawrence Berkeley National Laboratory. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes Radiance software
+ * (http://radsite.lbl.gov/)
+ * developed by the Lawrence Berkeley National Laboratory
+ * (http://www.lbl.gov/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
+ * and "The Regents of the University of California" must
+ * not be used to endorse or promote products derived from this
+ * software without prior written permission. For written
+ * permission, please contact radiance@radsite.lbl.gov.
+ *
+ * 5. Products derived from this software may not be called "Radiance",
+ * nor may "Radiance" appear in their name, without prior written
+ * permission of Lawrence Berkeley National Laboratory.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of Lawrence Berkeley National Laboratory. For more
+ * information on Lawrence Berkeley National Laboratory, please see
+ * .
+ */
+
#include
+#include
#include
+#include
#include "tmprivat.h"
#include "resolu.h"
+#ifndef TM_PIC_CTRANS
+#define TM_PIC_CTRANS 1 /* transform colors? (expensive) */
+#endif
-extern char *tempbuffer();
+#define GAMTSZ 1024
+typedef struct {
+ BYTE gamb[GAMTSZ]; /* gamma lookup table */
+ COLR clfb; /* encoded tm->clf */
+ TMbright inpsfb; /* encoded tm->inpsf */
+} COLRDATA;
+
+#ifdef NOPROTO
+static MEM_PTR colrInit();
+static void colrNewSpace();
+#else
+static MEM_PTR colrInit(struct tmStruct *);
+static void colrNewSpace(struct tmStruct *);
+#endif
+static struct tmPackage colrPkg = { /* our package functions */
+ colrInit, colrNewSpace, free
+};
+static int colrReg = -1; /* our package registration number */
+
#define LOGISZ 260
static TMbright logi[LOGISZ];
-static BYTE photofact[BMESUPPER-BMESLOWER];
int
-tmCvColrs(ls, cs, scan, len) /* tone map RGBE/XYZE colors */
+tmCvColrs(ls, cs, scan, len) /* convert RGBE/XYZE colors */
TMbright *ls;
BYTE *cs;
COLR *scan;
@@ -31,13 +108,18 @@ int len;
{
static char funcName[] = "tmCvColrs";
COLR cmon;
+ register COLRDATA *cd;
register int i, bi, li;
if (tmTop == NULL)
returnErr(TM_E_TMINVAL);
- if (ls == NULL | scan == NULL | len <= 0)
+ if (ls == NULL | scan == NULL | len < 0)
returnErr(TM_E_ILLEGAL);
- if (tmTop->flags & TM_F_NEEDMAT) { /* need floating point */
+#if TM_PIC_CTRANS
+ if (tmNeedMatrix(tmTop)) { /* need floating point */
+#else
+ if (tmTop->inppri == TM_XYZPRIM) { /* no way around this */
+#endif
register COLOR *newscan;
newscan = (COLOR *)tempbuffer(len*sizeof(COLOR));
if (newscan == NULL)
@@ -46,27 +128,29 @@ int len;
colr_color(newscan[i], scan[i]);
return(tmCvColors(ls, cs, newscan, len));
}
- if (logi[0] == 0) { /* build tables if necessary */
+ if (colrReg < 0) { /* build tables if necessary */
+ colrReg = tmRegPkg(&colrPkg);
+ if (colrReg < 0)
+ returnErr(TM_E_CODERR1);
for (i = 256; i--; )
logi[i] = TM_BRTSCALE*log((i+.5)/256.) - .5;
for (i = 256; i < LOGISZ; i++)
- logi[i] = logi[255];
- for (i = BMESLOWER; i < BMESUPPER; i++)
- photofact[i-BMESLOWER] = 256. *
- (tmLuminance(i) - LMESLOWER) /
- (LMESUPPER - LMESLOWER);
+ logi[i] = 0;
+ tmMkMesofact();
}
+ if ((cd = (COLRDATA *)tmPkgData(tmTop,colrReg)) == NULL)
+ returnErr(TM_E_NOMEM);
for (i = len; i--; ) {
copycolr(cmon, scan[i]);
/* world luminance */
- li = ( tmTop->clfb[RED]*cmon[RED] +
- tmTop->clfb[GRN]*cmon[GRN] +
- tmTop->clfb[BLU]*cmon[BLU] ) >> 8;
- bi = BRT2SCALE*(cmon[EXP]-COLXS) +
- logi[li] + tmTop->inpsfb;
- if (bi < MINBRT) {
- bi = MINBRT-1; /* bogus value */
- li++; /* avoid li==0 */
+ li = ( cd->clfb[RED]*cmon[RED] +
+ cd->clfb[GRN]*cmon[GRN] +
+ cd->clfb[BLU]*cmon[BLU] ) >> 8;
+ bi = BRT2SCALE(cmon[EXP]-COLXS) +
+ logi[li] + cd->inpsfb;
+ if (li <= 0) {
+ bi = TM_NOBRT; /* bogus value */
+ li = 1; /* avoid li==0 */
}
ls[i] = bi;
if (cs == TM_NOCHROM) /* no color? */
@@ -79,7 +163,7 @@ int len;
else {
if (tmTop->flags & TM_F_BW)
cmon[RED] = cmon[GRN] = cmon[BLU] = li;
- pf = photofact[bi-BMESLOWER];
+ pf = tmMesofact[bi-BMESLOWER];
sli *= 256 - pf;
cmon[RED] = ( sli + pf*cmon[RED] ) >> 8;
cmon[GRN] = ( sli + pf*cmon[GRN] ) >> 8;
@@ -88,12 +172,12 @@ int len;
} else if (tmTop->flags & TM_F_BW) {
cmon[RED] = cmon[GRN] = cmon[BLU] = li;
}
- bi = ( (int4)TM_GAMTSZ*tmTop->clfb[RED]*cmon[RED]/li ) >> 8;
- cs[3*i ] = bi>=TM_GAMTSZ ? 255 : tmTop->gamb[bi];
- bi = ( (int4)TM_GAMTSZ*tmTop->clfb[GRN]*cmon[GRN]/li ) >> 8;
- cs[3*i+1] = bi>=TM_GAMTSZ ? 255 : tmTop->gamb[bi];
- bi = ( (int4)TM_GAMTSZ*tmTop->clfb[BLU]*cmon[BLU]/li ) >> 8;
- cs[3*i+2] = bi>=TM_GAMTSZ ? 255 : tmTop->gamb[bi];
+ bi = ( (int4)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 8;
+ cs[3*i ] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
+ bi = ( (int4)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 8;
+ cs[3*i+1] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
+ bi = ( (int4)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 8;
+ cs[3*i+2] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
}
returnOK;
}
@@ -126,17 +210,18 @@ register struct radhead *rh;
rh->format = FMTCIE;
else
rh->format = FMTBAD;
- return;
+ return(0);
}
if (isexpos(s)) {
rh->expos *= exposval(s);
- return;
+ return(0);
}
if (isprims(s)) {
primsval(rh->mypri, s);
rh->primp = rh->mypri;
- return;
+ return(0);
}
+ return(0);
}
@@ -163,6 +248,8 @@ FILE *fp;
*xp = *yp = 0; /* error precaution */
if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "r")) == NULL)
returnErr(TM_E_BADFILE);
+ *lpp = NULL;
+ if (cpp != TM_NOCHROMP) *cpp = NULL;
info = rhdefault; /* get our header */
getheader(inpf, headline, (char *)&info);
if (info.format == FMTBAD | info.expos <= 0. ||
@@ -205,13 +292,19 @@ done: /* clean up */
if (fp == NULL)
fclose(inpf);
if (scanin != NULL)
- free((char *)scanin);
- if (err != TM_E_OK)
+ free((MEM_PTR)scanin);
+ if (err != TM_E_OK) {
+ if (*lpp != NULL)
+ free((MEM_PTR)*lpp);
+ if (cpp != TM_NOCHROMP && *cpp != NULL)
+ free((MEM_PTR)*cpp);
returnErr(err);
+ }
returnOK;
}
+#ifdef PCOND
int /* run pcond to map picture */
dopcond(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname)
BYTE **psp;
@@ -222,7 +315,7 @@ double gamval, Lddyn, Ldmax;
char *fname;
{
char *funcName = fname;
- char cmdbuf[512];
+ char cmdbuf[1024];
FILE *infp;
register COLR *scan;
register BYTE *rp;
@@ -232,26 +325,26 @@ char *fname;
if (setcolrcor(pow, 1./gamval) < 0)
returnErr(TM_E_NOMEM);
/* create command */
- strcpy(cmdbuf, "pcond ");
+ strcpy(cmdbuf, PCOND);
if (flags & TM_F_HCONTR)
- strcat(cmdbuf, "-s ");
+ strcat(cmdbuf, " -s");
if (flags & TM_F_MESOPIC)
- strcat(cmdbuf, "-c ");
+ strcat(cmdbuf, " -c");
if (flags & TM_F_LINEAR)
- strcat(cmdbuf, "-l ");
+ strcat(cmdbuf, " -l");
if (flags & TM_F_ACUITY)
- strcat(cmdbuf, "-a ");
+ strcat(cmdbuf, " -a");
if (flags & TM_F_VEIL)
- strcat(cmdbuf, "-v ");
+ strcat(cmdbuf, " -v");
if (flags & TM_F_CWEIGHT)
- strcat(cmdbuf, "-w ");
- sprintf(cmdbuf+strlen(cmdbuf),
- "-p %f %f %f %f %f %f %f %f -d %f -u %f %s",
- monpri[RED][CIEX], monpri[RED][CIEY],
- monpri[GRN][CIEX], monpri[GRN][CIEY],
- monpri[BLU][CIEX], monpri[BLU][CIEY],
- monpri[WHT][CIEX], monpri[WHT][CIEY],
- Lddyn, Ldmax, fname);
+ strcat(cmdbuf, " -w");
+ if (monpri != stdprims)
+ sprintf(cmdbuf+strlen(cmdbuf), " -p %f %f %f %f %f %f %f %f",
+ monpri[RED][CIEX], monpri[RED][CIEY],
+ monpri[GRN][CIEX], monpri[GRN][CIEY],
+ monpri[BLU][CIEX], monpri[BLU][CIEY],
+ monpri[WHT][CIEX], monpri[WHT][CIEY]);
+ sprintf(cmdbuf+strlen(cmdbuf), " -d %f -u %f %s", Lddyn, Ldmax, fname);
/* start pcond */
if ((infp = popen(cmdbuf, "r")) == NULL)
returnErr(TM_E_BADFILE);
@@ -275,8 +368,8 @@ char *fname;
for (y = 0; y < *yp; y++) {
if (freadcolrs(scan, *xp, infp) < 0) {
pclose(infp);
- free((char *)scan);
- free((char *)*psp);
+ free((MEM_PTR)scan);
+ free((MEM_PTR)*psp);
*psp = NULL;
returnErr(TM_E_BADFILE);
}
@@ -291,10 +384,11 @@ char *fname;
*rp++ = scan[x][BLU];
}
}
- free((char *)scan);
+ free((MEM_PTR)scan);
pclose(infp);
returnOK;
}
+#endif
int /* map a Radiance picture */
@@ -308,7 +402,6 @@ char *fname;
FILE *fp;
{
char *funcName = fname==NULL ? "tmMapPicture" : fname;
- FILE *inpf;
BYTE *cp;
TMbright *lp;
int err;
@@ -321,10 +414,12 @@ FILE *fp;
if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN;
if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX;
if (flags & TM_F_BW) monpri = stdprims;
+#ifdef PCOND
/* check for pcond run */
if (fp == TM_GETFILE && flags & TM_F_UNIMPL)
return( dopcond(psp, xp, yp, flags,
monpri, gamval, Lddyn, Ldmax, fname) );
+#endif
/* initialize tone mapping */
if (tmInit(flags, monpri, gamval) == NULL)
returnErr(TM_E_NOMEM);
@@ -339,7 +434,7 @@ FILE *fp;
if (flags & TM_F_BW) {
*psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp);
if (*psp == NULL) {
- free((char *)lp);
+ free((MEM_PTR)lp);
tmDone(NULL);
returnErr(TM_E_NOMEM);
}
@@ -357,12 +452,49 @@ FILE *fp;
err = tmMapPixels(*psp, lp, cp, *xp * *yp);
done: /* clean up */
- free((char *)lp);
+ free((MEM_PTR)lp);
tmDone(NULL);
if (err != TM_E_OK) { /* free memory on error */
- free((char *)*psp);
+ free((MEM_PTR)*psp);
*psp = NULL;
returnErr(err);
}
returnOK;
+}
+
+
+static void
+colrNewSpace(tms) /* color space changed for tone mapping */
+register struct tmStruct *tms;
+{
+ register COLRDATA *cd;
+ double d;
+
+ cd = (COLRDATA *)tms->pd[colrReg];
+ cd->clfb[RED] = 256.*tms->clf[RED] + .5;
+ cd->clfb[GRN] = 256.*tms->clf[GRN] + .5;
+ cd->clfb[BLU] = 256.*tms->clf[BLU] + .5;
+ cd->clfb[EXP] = COLXS;
+ d = TM_BRTSCALE*log(tms->inpsf);
+ cd->inpsfb = d<0. ? d-.5 : d+.5;
+}
+
+
+static MEM_PTR
+colrInit(tms) /* initialize private data for tone mapping */
+register struct tmStruct *tms;
+{
+ register COLRDATA *cd;
+ register int i;
+ /* allocate our data */
+ cd = (COLRDATA *)malloc(sizeof(COLRDATA));
+ if (cd == NULL)
+ return(NULL);
+ tms->pd[colrReg] = (MEM_PTR)cd;
+ /* compute gamma table */
+ for (i = GAMTSZ; i--; )
+ cd->gamb[i] = 256.*pow((i+.5)/GAMTSZ, 1./tms->mongam);
+ /* compute color and scale factors */
+ colrNewSpace(tms);
+ return((MEM_PTR)cd);
}