Back to index

radiance  4R0+20100331
Classes | Defines | Functions | Variables
tmapcolrs.c File Reference
#include "copyright.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include "tmprivat.h"
#include "resolu.h"

Go to the source code of this file.

Classes

struct  COLRDATA
struct  radhead

Defines

#define GAMTSZ   1024
#define LOGISZ   260
#define FMTRGB   1 /* Input is RGBE */
#define FMTCIE   2 /* Input is CIE XYZE */
#define FMTUNK   3 /* Input format is unspecified */
#define FMTBAD   4 /* Input is not a recognized format */

Functions

static MEM_PTR colrInit (TMstruct *)
static void colrNewSpace (TMstruct *)
int tmCvColrs (TMstruct *tms, TMbright *ls, BYTE *cs, COLR *scan, int len)
static int headline (register char *s, void *vrh)
int tmLoadPicture (TMstruct *tms, TMbright **lpp, BYTE **cpp, int *xp, int *yp, char *fname, FILE *fp)
int tmMapPicture (BYTE **psp, int *xp, int *yp, int flags, RGBPRIMP monpri, double gamval, double Lddyn, double Ldmax, char *fname, FILE *fp)

Variables

static const char RCSid [] = "$Id: tmapcolrs.c,v 3.26 2009/02/09 20:48:08 greg Exp $"
static gethfunc headline
static struct tmPackage
static int colrReg = -1
static TMbright logi [LOGISZ]
static struct radhead rhdefault

Class Documentation

struct COLRDATA

Definition at line 26 of file tmapcolrs.c.

Class Members
int clfb
int32 cmatb
BYTE gamb
TMbright inpsfb
struct radhead

Definition at line 143 of file tmapcolrs.c.

Class Members
double expos
int format
RGBPRIMS mypri
RGBPRIMP primp

Define Documentation

#define FMTBAD   4 /* Input is not a recognized format */

Definition at line 141 of file tmapcolrs.c.

#define FMTCIE   2 /* Input is CIE XYZE */

Definition at line 139 of file tmapcolrs.c.

#define FMTRGB   1 /* Input is RGBE */

Definition at line 138 of file tmapcolrs.c.

#define FMTUNK   3 /* Input format is unspecified */

Definition at line 140 of file tmapcolrs.c.

#define GAMTSZ   1024

Definition at line 24 of file tmapcolrs.c.

#define LOGISZ   260

Definition at line 42 of file tmapcolrs.c.


Function Documentation

static MEM_PTR colrInit ( TMstruct tms) [static]

Definition at line 446 of file tmapcolrs.c.

{
       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);
}

Here is the call graph for this function:

static void colrNewSpace ( TMstruct tms) [static]

Definition at line 426 of file tmapcolrs.c.

{
       register COLRDATA    *cd;
       double d;
       int    i, j;

       cd = (COLRDATA *)tms->pd[colrReg];
       for (i = 3; i--; )
              cd->clfb[i] = 0x1000*tms->clf[i] + .5;
       cd->inpsfb = tmCvLuminance(tms->inpsf);
       for (i = 3; i--; )
              for (j = 3; j--; ) {
                     d = tms->cmat[i][j] / tms->inpsf;
                     cd->cmatb[i][j] = 0x10000*d + (d<0. ? -.5 : .5);
              }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int headline ( register char *  s,
void *  vrh 
) [static]

Definition at line 152 of file tmapcolrs.c.

{
       char   fmt[32];
       register struct radhead     *rh = vrh;

       if (formatval(fmt, s)) {
              if (!strcmp(fmt, COLRFMT))
                     rh->format = FMTRGB;
              else if (!strcmp(fmt, CIEFMT))
                     rh->format = FMTCIE;
              else
                     rh->format = FMTBAD;
              return(0);
       }
       if (isexpos(s)) {
              rh->expos *= exposval(s);
              return(0);
       }
       if (isprims(s)) {
              primsval(rh->mypri, s);
              rh->primp = rh->mypri;
              return(0);
       }
       return(0);
}

Here is the call graph for this function:

int tmCvColrs ( TMstruct tms,
TMbright ls,
BYTE cs,
COLR scan,
int  len 
)

Definition at line 47 of file tmapcolrs.c.

{
       static const char    funcName[] = "tmCvColrs";
       int    cmon[4];
       register COLRDATA    *cd;
       register int  i, j, li, bi;
       int32  vl;

       if (tms == NULL)
              returnErr(TM_E_TMINVAL);
       if ((ls == NULL) | (scan == NULL) | (len < 0))
              returnErr(TM_E_ILLEGAL);
       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] = 0;
              tmMkMesofact();
       }
       if ((cd = (COLRDATA *)tmPkgData(tms,colrReg)) == NULL)
              returnErr(TM_E_NOMEM);
       for (i = len; i--; ) {
              if (tmNeedMatrix(tms)) {           /* apply color xform */
                     for (j = 3; j--; ) {
                            vl =   cd->cmatb[j][RED]*(int32)scan[i][RED] +
                                   cd->cmatb[j][GRN]*(int32)scan[i][GRN] +
                                   cd->cmatb[j][BLU]*(int32)scan[i][BLU] ;
                            if (vl < 0) cmon[j] = vl/0x10000;
                            else cmon[j] = vl>>16;
                     }
                     cmon[EXP] = scan[i][EXP];
              } else
                     copycolr(cmon, scan[i]);
                                                 /* world luminance */
              li =   cd->clfb[RED]*cmon[RED] +
                     cd->clfb[GRN]*cmon[GRN] +
                     cd->clfb[BLU]*cmon[BLU] ;
              if (li >= 0xfff00) li = 255;
              else li >>= 12;
              bi = BRT2SCALE(cmon[EXP]-COLXS) + cd->inpsfb;
              if (li > 0)
                     bi += logi[li];
              else {
                     bi += logi[0];
                     li = 1;                            /* avoid /0 */
              }
              ls[i] = bi;
              if (cs == TM_NOCHROM)                     /* no color? */
                     continue;
                                                 /* mesopic adj. */
              if (tms->flags & TM_F_MESOPIC && bi < BMESUPPER) {
                     int    pf, sli = normscot(cmon);
                     if (bi < BMESLOWER) {
                            cmon[RED] = cmon[GRN] = cmon[BLU] = sli;
                     } else {
                            if (tms->flags & TM_F_BW)
                                   cmon[RED] = cmon[GRN] = cmon[BLU] = li;
                            pf = tmMesofact[bi-BMESLOWER];
                            sli *= 256 - pf;
                            for (j = 3; j--; ) {
                                   cmon[j] = sli + pf*cmon[j];
                                   if (cmon[j] <= 0) cmon[j] = 0;
                                   else cmon[j] >>= 8;
                            }
                     }
              } else if (tms->flags & TM_F_BW) {
                     cmon[RED] = cmon[GRN] = cmon[BLU] = li;
              } else {
                     for (j = 3; j--; )
                            if (cmon[j] < 0) cmon[j] = 0;
              }
              bi = ( (int32)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 12;
              cs[3*i  ] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
              bi = ( (int32)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 12;
              cs[3*i+1] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
              bi = ( (int32)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 12;
              cs[3*i+2] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
       }
       returnOK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int tmLoadPicture ( TMstruct tms,
TMbright **  lpp,
BYTE **  cpp,
int *  xp,
int *  yp,
char *  fname,
FILE *  fp 
)

Definition at line 183 of file tmapcolrs.c.

{
       char   *funcName = fname==NULL ? "tmLoadPicture" : fname;
       FILE   *inpf;
       struct radhead       info;
       int    err;
       COLR   *scanin = NULL;
       int    i;
                                          /* check arguments */
       if (tms == NULL)
              returnErr(TM_E_TMINVAL);
       if ((lpp == NULL) | (xp == NULL) | (yp == NULL) |
                     ((fname == NULL) & (fp == TM_GETFILE)))
              returnErr(TM_E_ILLEGAL);
       *xp = *yp = 0;                            /* error precaution */
       if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "rb")) == NULL)
              returnErr(TM_E_BADFILE);
       *lpp = NULL;
       if (cpp != TM_NOCHROMP) *cpp = NULL;
       info = rhdefault;                  /* get our header */
       getheader(inpf, headline, &info);
       if ((info.format == FMTBAD) | (info.expos <= 0.) ||
                     fgetresolu(xp, yp, inpf) < 0) {
              err = TM_E_BADFILE; goto done;
       }
       if (info.format == FMTUNK)         /* assume RGBE format */
              info.format = FMTRGB;
       if (info.format == FMTRGB)
              info.expos /= WHTEFFICACY;
       else if (info.format == FMTCIE)
              info.primp = TM_XYZPRIM;
                                          /* prepare library */
       if ((err = tmSetSpace(tms, info.primp, 1./info.expos, NULL)) != TM_E_OK)
              goto done;
       err = TM_E_NOMEM;                  /* allocate arrays */
       *lpp = (TMbright *)malloc(sizeof(TMbright) * *xp * *yp);
       if (*lpp == NULL)
              goto done;
       if (cpp != TM_NOCHROMP) {
              *cpp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp);
              if (*cpp == NULL)
                     goto done;
       }
       scanin = (COLR *)malloc(sizeof(COLR) * *xp);
       if (scanin == NULL)
              goto done;
       err = TM_E_BADFILE;                /* read & convert scanlines */
       for (i = 0; i < *yp; i++) {
              if (freadcolrs(scanin, *xp, inpf) < 0) {
                     err = TM_E_BADFILE; break;
              }
              err = tmCvColrs(tms, *lpp + (i * *xp),
                     cpp==TM_NOCHROMP ? TM_NOCHROM : *cpp + (i * 3 * *xp),
                            scanin, *xp);
              if (err != TM_E_OK)
                     break;
       }
done:                                     /* clean up */
       if (fp == NULL)
              fclose(inpf);
       if (scanin != NULL)
              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;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int tmMapPicture ( BYTE **  psp,
int *  xp,
int *  yp,
int  flags,
RGBPRIMP  monpri,
double  gamval,
double  Lddyn,
double  Ldmax,
char *  fname,
FILE *  fp 
)

Definition at line 353 of file tmapcolrs.c.

{
       char   *funcName = fname==NULL ? "tmMapPicture" : fname;
       TMstruct      *tms;
       BYTE   *cp;
       TMbright      *lp;
       int    err;
                                          /* check arguments */
       if ((psp == NULL) | (xp == NULL) | (yp == NULL) | (monpri == NULL) |
                     ((fname == NULL) & (fp == TM_GETFILE)))
              returnErr(TM_E_ILLEGAL);
                                          /* set defaults */
       if (gamval < MINGAM) gamval = DEFGAM;
       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 ((tms = tmInit(flags, monpri, gamval)) == NULL)
              returnErr(TM_E_NOMEM);
                                          /* load & convert picture */
       err = tmLoadPicture(tms, &lp, (flags&TM_F_BW) ? TM_NOCHROMP : &cp,
                     xp, yp, fname, fp);
       if (err != TM_E_OK) {
              tmDone(tms);
              return(err);
       }
                                          /* allocate space for result */
       if (flags & TM_F_BW) {
              *psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp);
              if (*psp == NULL) {
                     free((MEM_PTR)lp);
                     tmDone(tms);
                     returnErr(TM_E_NOMEM);
              }
              cp = TM_NOCHROM;
       } else
              *psp = cp;
                                          /* compute color mapping */
       err = tmAddHisto(tms, lp, *xp * *yp, 1);
       if (err != TM_E_OK)
              goto done;
       err = tmComputeMapping(tms, gamval, Lddyn, Ldmax);
       if (err != TM_E_OK)
              goto done;
                                          /* map colors */
       err = tmMapPixels(tms, *psp, lp, cp, *xp * *yp);

done:                                     /* clean up */
       free((MEM_PTR)lp);
       tmDone(tms);
       if (err != TM_E_OK) {                     /* free memory on error */
              free((MEM_PTR)*psp);
              *psp = NULL;
              returnErr(err);
       }
       returnOK;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

int colrReg = -1 [static]

Definition at line 40 of file tmapcolrs.c.

gethfunc headline [static]

Definition at line 35 of file tmapcolrs.c.

TMbright logi[LOGISZ] [static]

Definition at line 43 of file tmapcolrs.c.

const char RCSid[] = "$Id: tmapcolrs.c,v 3.26 2009/02/09 20:48:08 greg Exp $" [static]

Definition at line 2 of file tmapcolrs.c.

struct radhead rhdefault [static]
struct tmPackage [static]
Initial value:
 {     
       colrInit, colrNewSpace, free
}

Definition at line 37 of file tmapcolrs.c.