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.4 by greg, Fri Apr 30 17:00:29 2004 UTC vs.
Revision 2.16 by greg, Fri Jun 6 19:11:21 2025 UTC

# Line 5 | Line 5 | static const char RCSid[] = "$Id$";
5   *  program to convert between RADIANCE and Windows BMP file
6   */
7  
8 < #include  <stdio.h>
9 < #include  <string.h>
8 > #include  <math.h>
9  
10 + #include  "rtio.h"
11 + #include  "paths.h"
12   #include  "platform.h"
13   #include  "color.h"
14   #include  "tonemap.h"
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  
21 char  *progname;
22
22   static void quiterr(const char *err);
23   static void tmap2bmp(char *fnin, char *fnout, char *expec,
24                                  RGBPRIMP monpri, double gamval);
25 < static void rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry);
25 > static void rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, RGBPRIMP monpri);
26   static void bmp2rad(BMPReader *brd, FILE *rfp, int inv);
27  
28 + static RGBPRIMP rgbinp = stdprims;      /* RGB input primitives */
29 + static RGBPRIMS myinprims;              /* custom primitives holder */
30  
31 + static gethfunc headline;
32 +
33 +
34   int
35   main(int argc, char *argv[])
36   {
# Line 38 | Line 42 | main(int argc, char *argv[])
42          RESOLU          rs;
43          int             i;
44          
45 <        progname = argv[0];
45 >        fixargv0(argv[0]);              /* assigns progname */
46  
47          for (i = 1; i < argc; i++)
48 <                if (argv[i][0] == '-')
48 >                if (argv[i][0] == '-' && argv[i][1])
49                          switch (argv[i][1]) {
50                          case 'b':
51                                  rgbp = NULL;
# Line 72 | Line 76 | main(int argc, char *argv[])
76                          case 'r':
77                                  reverse = !reverse;
78                                  break;
75                        case '\0':
76                                break;
79                          default:
80                                  goto userr;
81                          }
# Line 149 | Line 151 | main(int argc, char *argv[])
151                          exit(1);
152                  }
153                                          /* get header info. */
154 <                if (checkheader(stdin, COLRFMT, NULL) < 0 ||
154 >                if (getheader(stdin, headline, NULL) < 0 ||
155                                  !fgetsresolu(&rs, stdin))
156                          quiterr("bad Radiance picture format");
157                                          /* initialize BMP header */
158                  if (rgbp == NULL) {
159                          hdr = BMPmappedHeader(scanlen(&rs),
160                                                  numscans(&rs), 0, 256);
161 +                        /*
162                          if (outfile != NULL)
163                                  hdr->compr = BI_RLE8;
164 +                        */
165                  } else
166                          hdr = BMPtruecolorHeader(scanlen(&rs),
167                                                  numscans(&rs), 0);
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[MAXFMTLEN];
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 +                if (!strcmp(fmt,SPECFMT))
222 +                        return(0);
223 +                return(-1);
224 +        }
225 +        if (isprims(s)) {               /* get input primaries */
226 +                primsval(myinprims, s);
227 +                rgbinp = myinprims;
228 +                return(0);
229 +        }
230 +        if (isncomp(s)) {
231 +                NCSAMP = ncompval(s);
232 +                return(0);
233 +        }
234 +        if (iswlsplit(s)) {
235 +                wlsplitval(WLPART, s);
236 +                return(0);
237 +        }
238 +                                        /* should I grok colcorr also? */
239 +        return(0);
240 + }
241 +
242 +
243   /* convert Radiance picture to BMP */
244   static void
245 < rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry)
245 > rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, RGBPRIMP monpri)
246   {
247 +        int     usexfm = 0;
248 +        COLORMAT        xfm;
249          COLR    *scanin;
250 +        COLOR   cval;
251          int     y, yend, ystp;
252          int     x;
253                                                  /* allocate scanline */
254          scanin = (COLR *)malloc(bwr->hdr->width*sizeof(COLR));
255          if (scanin == NULL)
256                  quiterr("out of memory in rad2bmp");
257 +                                                /* set up color conversion */
258 +        usexfm = (monpri != NULL ? rgbinp != monpri :
259 +                        rgbinp != TM_XYZPRIM && rgbinp != stdprims);
260 +        if (usexfm) {
261 +                RGBPRIMP        destpri = monpri != NULL ? monpri : stdprims;
262 +                double          expcomp = pow(2.0, (double)bradj);
263 +                if (rgbinp == TM_XYZPRIM)
264 +                        compxyz2rgbWBmat(xfm, destpri);
265 +                else
266 +                        comprgb2rgbWBmat(xfm, rgbinp, destpri);
267 +                for (y = 0; y < 3; y++)
268 +                        for (x = 0; x < 3; x++)
269 +                                xfm[y][x] *= expcomp;
270 +        }
271                                                  /* convert image */
272          if (inv) {
273                  y = bwr->hdr->height - 1;
# Line 223 | Line 278 | rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry)
278          }
279                                                  /* convert each scanline */
280          for ( ; y != yend; y += ystp) {
281 <                if (freadcolrs(scanin, bwr->hdr->width, rfp) < 0)
281 >                if (fread2colrs(scanin, bwr->hdr->width, rfp, NCSAMP, WLPART) < 0)
282                          quiterr("error reading Radiance picture");
283 <                if (bradj)
283 >                if (usexfm)
284 >                        for (x = bwr->hdr->width; x--; ) {
285 >                                colr_color(cval, scanin[x]);
286 >                                colortrans(cval, xfm, cval);
287 >                                setcolr(scanin[x], colval(cval,RED),
288 >                                                colval(cval,GRN),
289 >                                                colval(cval,BLU));
290 >                        }
291 >                else if (bradj)
292                          shiftcolrs(scanin, bwr->hdr->width, bradj);
293 <                for (x = gry ? bwr->hdr->width : 0; x--; )
294 <                        scanin[x][GRN] = normbright(scanin[x]);
293 >                if (monpri == NULL && rgbinp != TM_XYZPRIM)
294 >                        for (x = bwr->hdr->width; x--; )
295 >                                scanin[x][GRN] = normbright(scanin[x]);
296                  colrs_gambs(scanin, bwr->hdr->width);
297 <                if (gry)
297 >                if (monpri == NULL)
298                          for (x = bwr->hdr->width; x--; )
299                                  bwr->scanline[x] = scanin[x][GRN];
300                  else
# Line 295 | Line 359 | tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIM
359          int             tmflags;
360          BMPHeader       *hdr;
361          BMPWriter       *wtr;
298        RESOLU          rs;
362          FILE            *fp;
363          int             xr, yr;
364 <        BYTE            *pa;
364 >        uby8            *pa;
365          int             i;
366                                          /* check tone-mapping spec */
367          i = strlen(expec);
# Line 321 | Line 384 | tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIM
384                  fprintf(stderr, "%s: cannot open\n", fnin);
385                  exit(1);
386          }
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;
387                                          /* tone-map picture */
388          if (tmMapPicture(&pa, &xr, &yr, tmflags, monpri, gamval,
389                          0., 0., fnin, fp) != TM_E_OK)
# Line 335 | Line 391 | tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIM
391                                          /* initialize BMP header */
392          if (tmflags & TM_F_BW) {
393                  hdr = BMPmappedHeader(xr, yr, 0, 256);
394 <                hdr->compr = BI_RLE8;
394 >                if (fnout != NULL)
395 >                        hdr->compr = BI_RLE8;
396          } else
397                  hdr = BMPtruecolorHeader(xr, yr, 0);
398          if (hdr == NULL)
# Line 349 | Line 406 | tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIM
406                  quiterr("cannot allocate writer structure");
407                                          /* write to BMP file */
408          while (wtr->yscan < yr) {
409 <                BYTE    *scn = pa + xr*((tmflags & TM_F_BW) ? 1 : 3)*
410 <                                        ((rs.rt & YDECR) ?
354 <                                                (yr-1 - wtr->yscan) :
355 <                                                wtr->yscan);
409 >                uby8    *scn = pa + xr*((tmflags & TM_F_BW) ? 1 : 3)*
410 >                                                (yr-1 - wtr->yscan);
411                  if (tmflags & TM_F_BW)
412                          memcpy((void *)wtr->scanline, (void *)scn, xr);
413                  else
# Line 368 | Line 423 | tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIM
423          if (fflush((FILE *)wtr->c_data) < 0)
424                  quiterr("error writing BMP output");
425                                          /* clean up */
426 +        if (fnin != NULL)
427 +                fclose(fp);
428          free((void *)pa);
429          BMPcloseOutput(wtr);
430   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines