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.10 by greg, Mon Oct 4 17:44:22 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 */
160                  if (rgbp == NULL) {
161                          hdr = BMPmappedHeader(scanlen(&rs),
162                                                  numscans(&rs), 0, 256);
163 +                        /*
164                          if (outfile != NULL)
165                                  hdr->compr = BI_RLE8;
166 +                        */
167                  } else
168                          hdr = BMPtruecolorHeader(scanlen(&rs),
169                                                  numscans(&rs), 0);
170                  if (hdr == NULL)
171                          quiterr("cannot initialize BMP header");
172                                          /* set up output direction */
173 <                hdr->yIsDown = (rs.rt & YDECR) &&
168 <                                ((outfile == NULL) | (hdr->compr == BI_RLE8));
173 >                hdr->yIsDown = ((outfile == NULL) | (hdr->compr == BI_RLE8));
174                                          /* open BMP output */
175                  if (outfile != NULL)
176                          wtr = BMPopenOutputFile(outfile, hdr);
# Line 174 | Line 179 | main(int argc, char *argv[])
179                  if (wtr == NULL)
180                          quiterr("cannot allocate writer structure");
181                                          /* convert file */
182 <                rad2bmp(stdin, wtr, !hdr->yIsDown && rs.rt&YDECR, rgbp==NULL);
182 >                rad2bmp(stdin, wtr, !hdr->yIsDown, rgbp);
183                                          /* flush output */
184                  if (fflush((FILE *)wtr->c_data) < 0)
185                          quiterr("error writing BMP output");
# Line 202 | Line 207 | quiterr(const char *err)
207          exit(0);
208   }
209  
210 + /* process header line (don't echo) */
211 + static int
212 + headline(char *s, void *p)
213 + {
214 +        char    fmt[32];
215 +
216 +        if (formatval(fmt, s)) {        /* check if format string */
217 +                if (!strcmp(fmt,COLRFMT))
218 +                        return(0);
219 +                if (!strcmp(fmt,CIEFMT)) {
220 +                        rgbinp = TM_XYZPRIM;
221 +                        return(0);
222 +                }
223 +                return(-1);
224 +        }
225 +        if (isprims(s)) {               /* get input primaries */
226 +                primsval(myinprims, s);
227 +                rgbinp = myinprims;
228 +                return(0);
229 +        }
230 +                                        /* should I grok colcorr also? */
231 +        return(0);
232 + }
233 +
234 +
235   /* convert Radiance picture to BMP */
236   static void
237 < rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry)
237 > rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, RGBPRIMP monpri)
238   {
239 +        int     usexfm = 0;
240 +        COLORMAT        xfm;
241          COLR    *scanin;
242 +        COLOR   cval;
243          int     y, yend, ystp;
244          int     x;
245                                                  /* allocate scanline */
246          scanin = (COLR *)malloc(bwr->hdr->width*sizeof(COLR));
247          if (scanin == NULL)
248                  quiterr("out of memory in rad2bmp");
249 +                                                /* set up color conversion */
250 +        usexfm = (monpri != NULL ? rgbinp != monpri :
251 +                        rgbinp != TM_XYZPRIM && rgbinp != stdprims);
252 +        if (usexfm) {
253 +                double  expcomp = pow(2.0, (double)bradj);
254 +                if (rgbinp == TM_XYZPRIM)
255 +                        compxyz2rgbWBmat(xfm, monpri);
256 +                else
257 +                        comprgb2rgbWBmat(xfm, rgbinp, monpri);
258 +                for (y = 0; y < 3; y++)
259 +                        for (x = 0; x < 3; x++)
260 +                                xfm[y][x] *= expcomp;
261 +        }
262                                                  /* convert image */
263          if (inv) {
264                  y = bwr->hdr->height - 1;
# Line 225 | Line 271 | rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry)
271          for ( ; y != yend; y += ystp) {
272                  if (freadcolrs(scanin, bwr->hdr->width, rfp) < 0)
273                          quiterr("error reading Radiance picture");
274 <                if (bradj)
274 >                if (usexfm)
275 >                        for (x = bwr->hdr->width; x--; ) {
276 >                                colr_color(cval, scanin[x]);
277 >                                colortrans(cval, xfm, cval);
278 >                                setcolr(scanin[x], colval(cval,RED),
279 >                                                colval(cval,GRN),
280 >                                                colval(cval,BLU));
281 >                        }
282 >                else if (bradj)
283                          shiftcolrs(scanin, bwr->hdr->width, bradj);
284 <                for (x = gry ? bwr->hdr->width : 0; x--; )
285 <                        scanin[x][GRN] = normbright(scanin[x]);
284 >                if (monpri == NULL && rgbinp != TM_XYZPRIM)
285 >                        for (x = bwr->hdr->width; x--; )
286 >                                scanin[x][GRN] = normbright(scanin[x]);
287                  colrs_gambs(scanin, bwr->hdr->width);
288 <                if (gry)
288 >                if (monpri == NULL)
289                          for (x = bwr->hdr->width; x--; )
290                                  bwr->scanline[x] = scanin[x][GRN];
291                  else
# Line 295 | Line 350 | tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIM
350          int             tmflags;
351          BMPHeader       *hdr;
352          BMPWriter       *wtr;
298        RESOLU          rs;
353          FILE            *fp;
354          int             xr, yr;
355          BYTE            *pa;
# Line 321 | Line 375 | tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIM
375                  fprintf(stderr, "%s: cannot open\n", fnin);
376                  exit(1);
377          }
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;
378                                          /* tone-map picture */
379          if (tmMapPicture(&pa, &xr, &yr, tmflags, monpri, gamval,
380                          0., 0., fnin, fp) != TM_E_OK)
# Line 351 | Line 398 | tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIM
398                                          /* write to BMP file */
399          while (wtr->yscan < yr) {
400                  BYTE    *scn = pa + xr*((tmflags & TM_F_BW) ? 1 : 3)*
401 <                                        ((rs.rt & YDECR) ?
355 <                                                (yr-1 - wtr->yscan) :
356 <                                                wtr->yscan);
401 >                                                (yr-1 - wtr->yscan);
402                  if (tmflags & TM_F_BW)
403                          memcpy((void *)wtr->scanline, (void *)scn, xr);
404                  else
# Line 369 | Line 414 | tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIM
414          if (fflush((FILE *)wtr->c_data) < 0)
415                  quiterr("error writing BMP output");
416                                          /* clean up */
417 +        if (fnin != NULL)
418 +                fclose(fp);
419          free((void *)pa);
420          BMPcloseOutput(wtr);
421   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines