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.3 by schorsch, Sun Mar 28 20:33:14 2004 UTC vs.
Revision 2.6 by greg, Fri Apr 30 17:56:06 2004 UTC

# Line 10 | Line 10 | static const char RCSid[] = "$Id$";
10  
11   #include  "platform.h"
12   #include  "color.h"
13 + #include  "tonemap.h"
14   #include  "resolu.h"
15   #include  "bmpfile.h"
16  
# Line 19 | Line 20 | double gamcor = 2.2;                   /* gamma correction value */
20  
21   char  *progname;
22  
23 < static void quiterr(const char *);
23 > static void quiterr(const char *err);
24 > static void tmap2bmp(char *fnin, char *fnout, char *expec,
25 >                                RGBPRIMP monpri, double gamval);
26   static void rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry);
27   static void bmp2rad(BMPReader *brd, FILE *rfp, int inv);
28  
# Line 27 | Line 30 | static void bmp2rad(BMPReader *brd, FILE *rfp, int inv
30   int
31   main(int argc, char *argv[])
32   {
33 <        char    *inpfile=NULL, *outfile=NULL;
34 <        int     gryflag = 0;
35 <        int     reverse = 0;
36 <        RESOLU  rs;
37 <        int     i;
33 >        char            *inpfile=NULL, *outfile=NULL;
34 >        char            *expec = NULL;
35 >        int             reverse = 0;
36 >        RGBPRIMP        rgbp = stdprims;
37 >        RGBPRIMS        myprims;
38 >        RESOLU          rs;
39 >        int             i;
40          
41          progname = argv[0];
42  
43          for (i = 1; i < argc; i++)
44 <                if (argv[i][0] == '-')
44 >                if (argv[i][0] == '-' && argv[i][1])
45                          switch (argv[i][1]) {
46                          case 'b':
47 <                                gryflag = 1;
47 >                                rgbp = NULL;
48                                  break;
49                          case 'g':
50                                  gamcor = atof(argv[++i]);
51                                  break;
52                          case 'e':
53                                  if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
54 +                                        expec = argv[++i];
55 +                                else
56 +                                        bradj = atoi(argv[++i]);
57 +                                break;
58 +                        case 'p':
59 +                                if (argc-i < 9)
60                                          goto userr;
61 <                                bradj = atoi(argv[++i]);
61 >                                myprims[RED][CIEX] = atof(argv[++i]);
62 >                                myprims[RED][CIEY] = atof(argv[++i]);
63 >                                myprims[GRN][CIEX] = atof(argv[++i]);
64 >                                myprims[GRN][CIEY] = atof(argv[++i]);
65 >                                myprims[BLU][CIEX] = atof(argv[++i]);
66 >                                myprims[BLU][CIEY] = atof(argv[++i]);
67 >                                myprims[WHT][CIEX] = atof(argv[++i]);
68 >                                myprims[WHT][CIEY] = atof(argv[++i]);
69 >                                if (rgbp == stdprims)
70 >                                        rgbp = myprims;
71                                  break;
72                          case 'r':
73                                  reverse = !reverse;
74                                  break;
55                        case '\0':
56                                break;
75                          default:
76                                  goto userr;
77                          }
# Line 72 | Line 90 | main(int argc, char *argv[])
90  
91          if (i == argc-2 && strcmp(argv[i+1], "-"))
92                  outfile = argv[i+1];
93 +                                        /* check for tone-mapping */
94 +        if (expec != NULL) {
95 +                if (reverse)
96 +                        goto userr;
97 +                tmap2bmp(inpfile, outfile, expec, rgbp, gamcor);
98 +                return(0);
99 +        }
100  
101          setcolrgam(gamcor);             /* set up conversion */
102  
# Line 126 | Line 151 | main(int argc, char *argv[])
151                                  !fgetsresolu(&rs, stdin))
152                          quiterr("bad Radiance picture format");
153                                          /* initialize BMP header */
154 <                if (gryflag) {
155 <                        hdr = BMPmappedHeader(numscans(&rs),
156 <                                                scanlen(&rs), 0, 256);
154 >                if (rgbp == NULL) {
155 >                        hdr = BMPmappedHeader(scanlen(&rs),
156 >                                                numscans(&rs), 0, 256);
157                          if (outfile != NULL)
158                                  hdr->compr = BI_RLE8;
159                  } else
160 <                        hdr = BMPtruecolorHeader(numscans(&rs),
161 <                                                scanlen(&rs), 0);
160 >                        hdr = BMPtruecolorHeader(scanlen(&rs),
161 >                                                numscans(&rs), 0);
162                  if (hdr == NULL)
163                          quiterr("cannot initialize BMP header");
164                                          /* set up output direction */
# Line 147 | Line 172 | main(int argc, char *argv[])
172                  if (wtr == NULL)
173                          quiterr("cannot allocate writer structure");
174                                          /* convert file */
175 <                rad2bmp(stdin, wtr, !hdr->yIsDown && (rs.rt&YDECR), gryflag);
175 >                rad2bmp(stdin, wtr, !hdr->yIsDown && rs.rt&YDECR, rgbp==NULL);
176                                          /* flush output */
177                  if (fflush((FILE *)wtr->c_data) < 0)
178                          quiterr("error writing BMP output");
179                  BMPcloseOutput(wtr);
180          }
181 <        exit(0);                        /* success */
181 >        return(0);                      /* success */
182   userr:
183          fprintf(stderr,
184 <                "Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n",
184 > "Usage: %s [-b][-g gamma][-e spec][-p xr yr xg yg xb yb xw yw] [input|- [output]]\n",
185                          progname);
186 <        exit(1);
187 <        return(1);      /* gratis return */
186 >        fprintf(stderr,
187 >                "   or: %s -r [-g gamma][-e +/-stops] [input|- [output]]\n",
188 >                        progname);
189 >        return(1);
190   }
191  
192   /* print message and exit */
# Line 257 | Line 284 | bmp2rad(BMPReader *brd, FILE *rfp, int inv)
284          }
285                                                  /* clean up */
286          free((void *)scanout);
287 + }
288 +
289 + /* Tone-map and convert Radiance picture */
290 + static void
291 + tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIMP monpri, double gamval)
292 + {
293 +        int             tmflags;
294 +        BMPHeader       *hdr;
295 +        BMPWriter       *wtr;
296 +        RESOLU          rs;
297 +        FILE            *fp;
298 +        int             xr, yr;
299 +        BYTE            *pa;
300 +        int             i;
301 +                                        /* check tone-mapping spec */
302 +        i = strlen(expec);
303 +        if (i && !strncmp(expec, "auto", i))
304 +                tmflags = TM_F_CAMERA;
305 +        else if (i && !strncmp(expec, "human", i))
306 +                tmflags = TM_F_HUMAN & ~TM_F_UNIMPL;
307 +        else if (i && !strncmp(expec, "linear", i))
308 +                tmflags = TM_F_LINEAR;
309 +        else
310 +                quiterr("illegal exposure specification (auto|human|linear)");
311 +        if (monpri == NULL) {
312 +                tmflags |= TM_F_BW;
313 +                monpri = stdprims;
314 +        }
315 +                                        /* open Radiance input */
316 +        if (fnin == NULL)
317 +                fp = stdin;
318 +        else if ((fp = fopen(fnin, "r")) == NULL) {
319 +                fprintf(stderr, "%s: cannot open\n", fnin);
320 +                exit(1);
321 +        }
322 +                                        /* get picture orientation */
323 +        if (fnin != NULL) {
324 +                if (getheader(fp, NULL, NULL) < 0 || !fgetsresolu(&rs, fp))
325 +                        quiterr("bad Radiance picture format");
326 +                rewind(fp);
327 +        } else                          /* assume stdin has normal orient */
328 +                rs.rt = PIXSTANDARD;
329 +                                        /* tone-map picture */
330 +        if (tmMapPicture(&pa, &xr, &yr, tmflags, monpri, gamval,
331 +                        0., 0., fnin, fp) != TM_E_OK)
332 +                exit(1);
333 +                                        /* initialize BMP header */
334 +        if (tmflags & TM_F_BW) {
335 +                hdr = BMPmappedHeader(xr, yr, 0, 256);
336 +                if (fnout != NULL)
337 +                        hdr->compr = BI_RLE8;
338 +        } else
339 +                hdr = BMPtruecolorHeader(xr, yr, 0);
340 +        if (hdr == NULL)
341 +                quiterr("cannot initialize BMP header");
342 +                                        /* open BMP output */
343 +        if (fnout != NULL)
344 +                wtr = BMPopenOutputFile(fnout, hdr);
345 +        else
346 +                wtr = BMPopenOutputStream(stdout, hdr);
347 +        if (wtr == NULL)
348 +                quiterr("cannot allocate writer structure");
349 +                                        /* write to BMP file */
350 +        while (wtr->yscan < yr) {
351 +                BYTE    *scn = pa + xr*((tmflags & TM_F_BW) ? 1 : 3)*
352 +                                        ((rs.rt & YDECR) ?
353 +                                                (yr-1 - wtr->yscan) :
354 +                                                wtr->yscan);
355 +                if (tmflags & TM_F_BW)
356 +                        memcpy((void *)wtr->scanline, (void *)scn, xr);
357 +                else
358 +                        for (i = xr; i--; ) {
359 +                                wtr->scanline[3*i] = scn[3*i+BLU];
360 +                                wtr->scanline[3*i+1] = scn[3*i+GRN];
361 +                                wtr->scanline[3*i+2] = scn[3*i+RED];
362 +                        }
363 +                if ((i = BMPwriteScanline(wtr)) != BIR_OK)
364 +                        quiterr(BMPerrorMessage(i));
365 +        }
366 +                                        /* flush output */
367 +        if (fflush((FILE *)wtr->c_data) < 0)
368 +                quiterr("error writing BMP output");
369 +                                        /* clean up */
370 +        free((void *)pa);
371 +        BMPcloseOutput(wtr);
372   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines