ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_xyze.c
(Generate patch)

Comparing ray/src/px/ra_xyze.c (file contents):
Revision 2.8 by schorsch, Fri Jan 2 12:47:01 2004 UTC vs.
Revision 2.15 by greg, Tue Oct 1 20:04:44 2024 UTC

# Line 6 | Line 6 | static const char      RCSid[] = "$Id$";
6   *  Added white-balance adjustment 10/01 (GW).
7   */
8  
9 #include  <stdio.h>
10 #include  <string.h>
9   #include  <math.h>
12 #include  <time.h>
10  
11   #include  "platform.h"
12   #include  "color.h"
13   #include  "resolu.h"
14 + #include  "rtio.h"
15  
16 < int  rgbinp = -1;                       /* input is RGBE? */
17 <
16 > enum {InpUNK, InpRGB, InpXYZ, InpSPEC}; /* input format */
17 > int  infmt = InpUNK;
18   int  rgbout = 0;                        /* output should be RGBE? */
21
19   RGBPRIMS  inprims = STDPRIMS;           /* input primaries */
23
20   RGBPRIMS  outprims = STDPRIMS;          /* output primaries */
25
21   double  expcomp = 1.0;                  /* exposure compensation */
27
22   int  doflat = -1;                       /* produce flat file? */
23 <
23 > double  origexp = -1.0;                 /* original exposure */
24   char  *progname;
25  
26   static gethfunc headline;
27 + static void quiterr(char *err);
28 + static void myreadscan(COLOR *scn, int len);
29 + static void convert(void);
30  
31  
32 +
33   static int
34   headline(                               /* process header line */
35          char    *s,
36          void    *p
37   )
38   {
39 <        char    fmt[32];
39 >        char    fmt[MAXFMTLEN];
40  
41          if (formatval(fmt, s)) {        /* check if format string */
42                  if (!strcmp(fmt,COLRFMT))
43 <                        rgbinp = 1;
43 >                        infmt = InpRGB;
44                  else if (!strcmp(fmt,CIEFMT))
45 <                        rgbinp = 0;
45 >                        infmt = InpXYZ;
46 >                else if (!strcmp(fmt,SPECFMT))
47 >                        infmt = InpSPEC;
48                  else
49 <                        rgbinp = -2;
49 >                        infmt = InpUNK;
50                  return(0);              /* don't echo */
51          }
52 +        if (origexp > 0.0 && isexpos(s)) {
53 +                origexp *= exposval(s);
54 +                return(0);              /* don't echo */
55 +        }
56          if (isprims(s)) {               /* get input primaries */
57                  primsval(inprims, s);
58                  return(0);              /* don't echo */
59          }
60 +        if (iswlsplit(s)) {             /* get wavelength limits */
61 +                wlsplitval(WLPART, s);
62 +                return(0);              /* don't echo */
63 +        }
64 +        if (isncomp(s)) {               /* number of color samples */
65 +                NCSAMP = ncompval(s);
66 +                return(0);              /* don't echo */
67 +        }
68                                          /* should I grok colcorr also? */
69          return(fputs(s, stdout));
70   }
71  
72  
73 < main(argc, argv)
74 < int  argc;
63 < char  *argv[];
73 > int
74 > main(int  argc, char  *argv[])
75   {
76          int  i;
77          SET_DEFAULT_BINARY();
# Line 92 | Line 103 | char  *argv[];
103                                  outprims[WHT][CIEX] = atof(argv[++i]);
104                                  outprims[WHT][CIEY] = atof(argv[++i]);
105                                  break;
106 +                        case 'o':               /* original exposure */
107 +                                origexp = 1.0;
108 +                                break;
109                          case 'e':               /* exposure compensation */
110                                  expcomp = atof(argv[++i]);
111                                  if (argv[i][0] == '+' || argv[i][0] == '-')
# Line 118 | Line 132 | char  *argv[];
132                  exit(1);
133          }
134          getheader(stdin, headline, NULL);
135 <        if (rgbinp == -2)
136 <                quiterr("unrecognized input file format");
137 <        if (rgbinp == -1)
138 <                rgbinp = !rgbout;
135 >        if (infmt == InpUNK)
136 >                quiterr("unrecognized/missing input file format");
137 >        if (infmt == InpSPEC ? (NCSAMP <= 3) | (NCSAMP > MAXCSAMP) :
138 >                        NCSAMP != 3)
139 >                quiterr("bad number of color components");
140          printargs(argc, argv, stdout);          /* add to header */
141          convert();                              /* convert picture */
142          exit(0);
143   userr:
144 <        fprintf(stderr, "Usage: %s [-r][-e exp][-c|-u]", progname);
144 >        fprintf(stderr, "Usage: %s [-r][-o][-e exp][-c|-u]", progname);
145          fprintf(stderr, "[-p rx ry gx gy bx by wx wy] [input [output]]\n");
146          exit(1);
147   }
148  
149  
150 < quiterr(err)            /* print message and exit */
151 < char  *err;
150 > static void
151 > quiterr(                /* print message and exit */
152 >        char  *err
153 > )
154   {
155          if (err != NULL) {
156                  fprintf(stderr, "%s: %s\n", progname, err);
# Line 143 | Line 160 | char  *err;
160   }
161  
162  
163 < convert()                               /* convert to XYZE or RGBE picture */
163 > static void
164 > myreadscan(COLOR *scn, int len)
165   {
166 +        if (infmt == InpSPEC) {         /* read & convert to XYZ */
167 +                static COLOR    *scomp = NULL;
168 +                SCOLR           sclr;
169 +                SCOLOR          scol;
170 +                COLOR           xyz;
171 +                int             n;
172 +                if (scomp == NULL) {    /* initialize conversion */
173 +                        scomp = (COLOR *)malloc(sizeof(COLOR)*NCSAMP);
174 +                        if (scomp == NULL)
175 +                                quiterr("out of memory in myreadscan");
176 +                        for (n = NCSAMP; n--; )
177 +                                spec_cie(scomp[n],
178 +                                        WLPART[0] + (WLPART[3] - WLPART[0])*(n+1)/NCSAMP,
179 +                                        WLPART[0] + (WLPART[3] - WLPART[0])*n/NCSAMP);
180 +                }
181 +                while (len-- > 0) {
182 +                        if (getbinary(sclr, LSCOLR, 1, stdin) != 1)
183 +                                goto readerr;
184 +                        scolr_scolor(scol, sclr);
185 +                        setcolor(*scn, 0, 0, 0);
186 +                        for (n = NCSAMP; n--; ) {
187 +                                copycolor(xyz, scomp[n]);
188 +                                scalecolor(xyz, scol[n]);
189 +                                addcolor(*scn, xyz);
190 +                        }
191 +                        scn++;
192 +                }
193 +                return;
194 +        }                               /* else read as RGBE/XYZE */
195 +        if (freadscan(scn, len, stdin) >= 0)
196 +                return;
197 + readerr:
198 +        quiterr("error reading input picture");
199 + }
200 +
201 +
202 + static void
203 + convert(void)                           /* convert to XYZE or RGBE picture */
204 + {
205          int     order;
206          int     xmax, ymax;
207          COLORMAT        xfm;
208 <        register COLOR  *scanin;
209 <        register COLR   *scanout;
210 <        double  ourexp = expcomp;
208 >        COLOR   *scanin;
209 >        COLR    *scanout;
210 >        double  exp2do = expcomp;
211 >        double  exp2report = expcomp;
212          int     y;
213 <        register int    x;
213 >        int     x;
214 >                                                /* recover original? */
215 >        if (origexp > 0.0)
216 >                exp2do /= origexp;
217                                                  /* compute transform */
218          if (rgbout) {
219 <                if (rgbinp) {                   /* RGBE -> RGBE */
219 >                if (infmt == InpRGB) {          /* RGBE -> RGBE */
220                          comprgb2rgbWBmat(xfm, inprims, outprims);
221 <                } else {                        /* XYZE -> RGBE */
221 >                } else {                        /* XYZE/Spectral -> RGBE */
222                          compxyz2rgbWBmat(xfm, outprims);
162                        ourexp *= WHTEFFICACY;
223                  }
224 +                if (infmt == InpXYZ) {
225 +                        if (origexp > 0.0)
226 +                                exp2do /= WHTEFFICACY;
227 +                        else
228 +                                exp2report *= WHTEFFICACY;
229 +                }
230          } else {
231 <                if (rgbinp) {                   /* RGBE -> XYZE */
231 >                if (infmt == InpRGB) {          /* RGBE -> XYZE */
232                          comprgb2xyzWBmat(xfm, inprims);
233 <                        ourexp /= WHTEFFICACY;
234 <                } else {                        /* XYZE -> XYZE */
235 <                        for (y = 0; y < 3; y++)
236 <                                for (x = 0; x < 3; x++)
171 <                                        xfm[y][x] = x==y ? 1. : 0.;
233 >                } else {                        /* XYZE/Spectral -> XYZE */
234 >                        memset(xfm, 0, sizeof(xfm));
235 >                        for (x = 3; x--; )
236 >                                xfm[x][x] = 1.;
237                  }
238 +                if (infmt != InpXYZ) {
239 +                        if (origexp > 0)
240 +                                exp2do *= WHTEFFICACY;
241 +                        else
242 +                                exp2report /= WHTEFFICACY;
243 +                }
244          }
245          for (y = 0; y < 3; y++)
246                  for (x = 0; x < 3; x++)
247 <                        xfm[y][x] *= expcomp;
247 >                        xfm[y][x] *= exp2do;
248                                                  /* get input resolution */
249          if ((order = fgetresolu(&xmax, &ymax, stdin)) < 0)
250                  quiterr("bad picture format");
251                                                  /* complete output header */
252 <        if (ourexp < 0.99 || ourexp > 1.01)
253 <                fputexpos(ourexp, stdout);
252 >        if ((exp2report < 0.99) | (exp2report > 1.01))
253 >                fputexpos(exp2report, stdout);
254          if (rgbout) {
255                  fputprims(outprims, stdout);
256                  fputformat(COLRFMT, stdout);
# Line 191 | Line 262 | convert()                              /* convert to XYZE or RGBE picture */
262          scanin = (COLOR *)malloc(xmax*sizeof(COLOR));
263          if (scanin == NULL)
264                  quiterr("out of memory in convert");
265 <        scanout = doflat ? (COLR *)malloc(xmax*sizeof(COLR)) : NULL;
265 >        scanout = doflat ? (COLR *)malloc(xmax*sizeof(COLR)) : (COLR *)NULL;
266                                                  /* convert image */
267          for (y = 0; y < ymax; y++) {
268 <                if (freadscan(scanin, xmax, stdin) < 0)
198 <                        quiterr("error reading input picture");
268 >                myreadscan(scanin, xmax);
269                  for (x = 0; x < xmax; x++) {
270                          colortrans(scanin[x], xfm, scanin[x]);
271                          if (rgbout)
# Line 207 | Line 277 | convert()                              /* convert to XYZE or RGBE picture */
277                                  setcolr(scanout[x], colval(scanin[x],RED),
278                                                  colval(scanin[x],GRN),
279                                                  colval(scanin[x],BLU));
280 <                        fwrite((char *)scanout, sizeof(COLR), xmax, stdout);
280 >                        putbinary(scanout, sizeof(COLR), xmax, stdout);
281                  } else
282                          fwritescan(scanin, xmax, stdout);
283                  if (ferror(stdout))
284                          quiterr("error writing output picture");
285          }
286                                                  /* free scanline */
287 <        free((void *)scanin);
287 >        free(scanin);
288          if (scanout != NULL)
289 <                free((void *)scanout);
289 >                free(scanout);
290   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines