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

Comparing ray/src/px/ra_bmp.c (file contents):
Revision 2.5 by greg, Fri Apr 30 17:30:33 2004 UTC vs.
Revision 2.9 by greg, Thu Sep 23 18:00:54 2004 UTC

# Line 6 | Line 6 | static const char RCSid[] = "$Id$";
6   */
7  
8   #include  <stdio.h>
9 + #include  <math.h>
10   #include  <string.h>
11  
12   #include  "platform.h"
# Line 14 | Line 15 | static const char RCSid[] = "$Id$";
15   #include  "resolu.h"
16   #include  "bmpfile.h"
17  
18 < int  bradj = 0;                         /* brightness adjustment */
18 > int             bradj = 0;              /* brightness adjustment */
19  
20 < double  gamcor = 2.2;                   /* gamma correction value */
20 > double          gamcor = 2.2;           /* gamma correction value */
21  
22 < char  *progname;
22 > char            *progname;
23  
24   static void quiterr(const char *err);
25   static void tmap2bmp(char *fnin, char *fnout, char *expec,
26                                  RGBPRIMP monpri, double gamval);
27 < static void rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry);
27 > static void rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, RGBPRIMP monpri);
28   static void bmp2rad(BMPReader *brd, FILE *rfp, int inv);
29  
30 + static RGBPRIMP rgbinp = stdprims;      /* RGB input primitives */
31 + static RGBPRIMS myinprims;              /* custom primitives holder */
32  
33 + static gethfunc headline;
34 +
35 +
36   int
37   main(int argc, char *argv[])
38   {
# Line 41 | Line 47 | main(int argc, char *argv[])
47          progname = argv[0];
48  
49          for (i = 1; i < argc; i++)
50 <                if (argv[i][0] == '-')
50 >                if (argv[i][0] == '-' && argv[i][1])
51                          switch (argv[i][1]) {
52                          case 'b':
53                                  rgbp = NULL;
# Line 72 | Line 78 | main(int argc, char *argv[])
78                          case 'r':
79                                  reverse = !reverse;
80                                  break;
75                        case '\0':
76                                break;
81                          default:
82                                  goto userr;
83                          }
# Line 149 | Line 153 | main(int argc, char *argv[])
153                          exit(1);
154                  }
155                                          /* get header info. */
156 <                if (checkheader(stdin, COLRFMT, NULL) < 0 ||
156 >                if (getheader(stdin, headline, NULL) < 0 ||
157                                  !fgetsresolu(&rs, stdin))
158                          quiterr("bad Radiance picture format");
159                                          /* initialize BMP header */
# Line 164 | Line 168 | main(int argc, char *argv[])
168                  if (hdr == NULL)
169                          quiterr("cannot initialize BMP header");
170                                          /* set up output direction */
171 <                hdr->yIsDown = (rs.rt & YDECR) &&
168 <                                ((outfile == NULL) | (hdr->compr == BI_RLE8));
171 >                hdr->yIsDown = ((outfile == NULL) | (hdr->compr == BI_RLE8));
172                                          /* open BMP output */
173                  if (outfile != NULL)
174                          wtr = BMPopenOutputFile(outfile, hdr);
# Line 174 | Line 177 | main(int argc, char *argv[])
177                  if (wtr == NULL)
178                          quiterr("cannot allocate writer structure");
179                                          /* convert file */
180 <                rad2bmp(stdin, wtr, !hdr->yIsDown && rs.rt&YDECR, rgbp==NULL);
180 >                rad2bmp(stdin, wtr, !hdr->yIsDown, rgbp);
181                                          /* flush output */
182                  if (fflush((FILE *)wtr->c_data) < 0)
183                          quiterr("error writing BMP output");
# Line 202 | Line 205 | quiterr(const char *err)
205          exit(0);
206   }
207  
208 + /* process header line (don't echo) */
209 + static int
210 + headline(char *s, void *p)
211 + {
212 +        char    fmt[32];
213 +
214 +        if (formatval(fmt, s)) {        /* check if format string */
215 +                if (!strcmp(fmt,COLRFMT))
216 +                        return(0);
217 +                if (!strcmp(fmt,CIEFMT)) {
218 +                        rgbinp = TM_XYZPRIM;
219 +                        return(0);
220 +                }
221 +                return(-1);
222 +        }
223 +        if (isprims(s)) {               /* get input primaries */
224 +                primsval(myinprims, s);
225 +                rgbinp = myinprims;
226 +                return(0);
227 +        }
228 +                                        /* should I grok colcorr also? */
229 +        return(0);
230 + }
231 +
232 +
233   /* convert Radiance picture to BMP */
234   static void
235 < rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry)
235 > rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, RGBPRIMP monpri)
236   {
237 +        int     usexfm = 0;
238 +        COLORMAT        xfm;
239          COLR    *scanin;
240 +        COLOR   cval;
241          int     y, yend, ystp;
242          int     x;
243                                                  /* allocate scanline */
244          scanin = (COLR *)malloc(bwr->hdr->width*sizeof(COLR));
245          if (scanin == NULL)
246                  quiterr("out of memory in rad2bmp");
247 +                                                /* set up color conversion */
248 +        usexfm = (monpri != NULL ? rgbinp != monpri :
249 +                        rgbinp != TM_XYZPRIM && rgbinp != stdprims);
250 +        if (usexfm) {
251 +                double  expcomp = pow(2.0, (double)bradj);
252 +                if (rgbinp == TM_XYZPRIM)
253 +                        compxyz2rgbWBmat(xfm, monpri);
254 +                else
255 +                        comprgb2rgbWBmat(xfm, rgbinp, monpri);
256 +                for (y = 0; y < 3; y++)
257 +                        for (x = 0; x < 3; x++)
258 +                                xfm[y][x] *= expcomp;
259 +        }
260                                                  /* convert image */
261          if (inv) {
262                  y = bwr->hdr->height - 1;
# Line 225 | Line 269 | rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry)
269          for ( ; y != yend; y += ystp) {
270                  if (freadcolrs(scanin, bwr->hdr->width, rfp) < 0)
271                          quiterr("error reading Radiance picture");
272 <                if (bradj)
272 >                if (usexfm)
273 >                        for (x = bwr->hdr->width; x--; ) {
274 >                                colr_color(cval, scanin[x]);
275 >                                colortrans(cval, xfm, cval);
276 >                                setcolr(scanin[x], colval(cval,RED),
277 >                                                colval(cval,GRN),
278 >                                                colval(cval,BLU));
279 >                        }
280 >                else if (bradj)
281                          shiftcolrs(scanin, bwr->hdr->width, bradj);
282 <                for (x = gry ? bwr->hdr->width : 0; x--; )
283 <                        scanin[x][GRN] = normbright(scanin[x]);
282 >                if (monpri == NULL && rgbinp != TM_XYZPRIM)
283 >                        for (x = bwr->hdr->width; x--; )
284 >                                scanin[x][GRN] = normbright(scanin[x]);
285                  colrs_gambs(scanin, bwr->hdr->width);
286 <                if (gry)
286 >                if (monpri == NULL)
287                          for (x = bwr->hdr->width; x--; )
288                                  bwr->scanline[x] = scanin[x][GRN];
289                  else
# Line 295 | Line 348 | tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIM
348          int             tmflags;
349          BMPHeader       *hdr;
350          BMPWriter       *wtr;
298        RESOLU          rs;
351          FILE            *fp;
352          int             xr, yr;
353          BYTE            *pa;
# Line 321 | Line 373 | tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIM
373                  fprintf(stderr, "%s: cannot open\n", fnin);
374                  exit(1);
375          }
324                                        /* get picture orientation */
325        if (fnin != NULL) {
326                if (getheader(fp, NULL, NULL) < 0 || !fgetsresolu(&rs, fp))
327                        quiterr("bad Radiance picture format");
328                rewind(fp);
329        } else                          /* assume stdin has normal orient */
330                rs.rt = PIXSTANDARD;
376                                          /* tone-map picture */
377          if (tmMapPicture(&pa, &xr, &yr, tmflags, monpri, gamval,
378                          0., 0., fnin, fp) != TM_E_OK)
# Line 351 | Line 396 | tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIM
396                                          /* write to BMP file */
397          while (wtr->yscan < yr) {
398                  BYTE    *scn = pa + xr*((tmflags & TM_F_BW) ? 1 : 3)*
399 <                                        ((rs.rt & YDECR) ?
355 <                                                (yr-1 - wtr->yscan) :
356 <                                                wtr->yscan);
399 >                                                (yr-1 - wtr->yscan);
400                  if (tmflags & TM_F_BW)
401                          memcpy((void *)wtr->scanline, (void *)scn, xr);
402                  else
# Line 369 | Line 412 | tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIM
412          if (fflush((FILE *)wtr->c_data) < 0)
413                  quiterr("error writing BMP output");
414                                          /* clean up */
415 +        if (fnin != NULL)
416 +                fclose(fp);
417          free((void *)pa);
418          BMPcloseOutput(wtr);
419   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines