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.8 by greg, Tue Jul 20 03:08:42 2004 UTC vs.
Revision 2.13 by greg, Fri Jul 19 17:37:56 2019 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  "platform.h"
12   #include  "color.h"
13   #include  "tonemap.h"
14   #include  "resolu.h"
15   #include  "bmpfile.h"
16  
17 < int  bradj = 0;                         /* brightness adjustment */
17 > int             bradj = 0;              /* brightness adjustment */
18  
19 < double  gamcor = 2.2;                   /* gamma correction value */
19 > double          gamcor = 2.2;           /* gamma correction value */
20  
21 < char  *progname;
21 > char            *progname;
22  
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);
26 > static void rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, RGBPRIMP monpri);
27   static void bmp2rad(BMPReader *brd, FILE *rfp, int inv);
28  
29 + static RGBPRIMP rgbinp = stdprims;      /* RGB input primitives */
30 + static RGBPRIMS myinprims;              /* custom primitives holder */
31  
32 + static gethfunc headline;
33 +
34 +
35   int
36   main(int argc, char *argv[])
37   {
# Line 147 | Line 152 | main(int argc, char *argv[])
152                          exit(1);
153                  }
154                                          /* get header info. */
155 <                if (checkheader(stdin, COLRFMT, NULL) < 0 ||
155 >                if (getheader(stdin, headline, NULL) < 0 ||
156                                  !fgetsresolu(&rs, stdin))
157                          quiterr("bad Radiance picture format");
158                                          /* initialize BMP header */
159                  if (rgbp == NULL) {
160                          hdr = BMPmappedHeader(scanlen(&rs),
161                                                  numscans(&rs), 0, 256);
162 +                        /*
163                          if (outfile != NULL)
164                                  hdr->compr = BI_RLE8;
165 +                        */
166                  } else
167                          hdr = BMPtruecolorHeader(scanlen(&rs),
168                                                  numscans(&rs), 0);
# Line 171 | Line 178 | main(int argc, char *argv[])
178                  if (wtr == NULL)
179                          quiterr("cannot allocate writer structure");
180                                          /* convert file */
181 <                rad2bmp(stdin, wtr, !hdr->yIsDown, rgbp==NULL);
181 >                rad2bmp(stdin, wtr, !hdr->yIsDown, rgbp);
182                                          /* flush output */
183                  if (fflush((FILE *)wtr->c_data) < 0)
184                          quiterr("error writing BMP output");
# Line 199 | Line 206 | quiterr(const char *err)
206          exit(0);
207   }
208  
209 + /* process header line (don't echo) */
210 + static int
211 + headline(char *s, void *p)
212 + {
213 +        char    fmt[MAXFMTLEN];
214 +
215 +        if (formatval(fmt, s)) {        /* check if format string */
216 +                if (!strcmp(fmt,COLRFMT))
217 +                        return(0);
218 +                if (!strcmp(fmt,CIEFMT)) {
219 +                        rgbinp = TM_XYZPRIM;
220 +                        return(0);
221 +                }
222 +                return(-1);
223 +        }
224 +        if (isprims(s)) {               /* get input primaries */
225 +                primsval(myinprims, s);
226 +                rgbinp = myinprims;
227 +                return(0);
228 +        }
229 +                                        /* should I grok colcorr also? */
230 +        return(0);
231 + }
232 +
233 +
234   /* convert Radiance picture to BMP */
235   static void
236 < rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry)
236 > rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, RGBPRIMP monpri)
237   {
238 +        int     usexfm = 0;
239 +        COLORMAT        xfm;
240          COLR    *scanin;
241 +        COLOR   cval;
242          int     y, yend, ystp;
243          int     x;
244                                                  /* allocate scanline */
245          scanin = (COLR *)malloc(bwr->hdr->width*sizeof(COLR));
246          if (scanin == NULL)
247                  quiterr("out of memory in rad2bmp");
248 +                                                /* set up color conversion */
249 +        usexfm = (monpri != NULL ? rgbinp != monpri :
250 +                        rgbinp != TM_XYZPRIM && rgbinp != stdprims);
251 +        if (usexfm) {
252 +                double  expcomp = pow(2.0, (double)bradj);
253 +                if (rgbinp == TM_XYZPRIM)
254 +                        compxyz2rgbWBmat(xfm, monpri);
255 +                else
256 +                        comprgb2rgbWBmat(xfm, rgbinp, monpri);
257 +                for (y = 0; y < 3; y++)
258 +                        for (x = 0; x < 3; x++)
259 +                                xfm[y][x] *= expcomp;
260 +        }
261                                                  /* convert image */
262          if (inv) {
263                  y = bwr->hdr->height - 1;
# Line 222 | Line 270 | rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry)
270          for ( ; y != yend; y += ystp) {
271                  if (freadcolrs(scanin, bwr->hdr->width, rfp) < 0)
272                          quiterr("error reading Radiance picture");
273 <                if (bradj)
273 >                if (usexfm)
274 >                        for (x = bwr->hdr->width; x--; ) {
275 >                                colr_color(cval, scanin[x]);
276 >                                colortrans(cval, xfm, cval);
277 >                                setcolr(scanin[x], colval(cval,RED),
278 >                                                colval(cval,GRN),
279 >                                                colval(cval,BLU));
280 >                        }
281 >                else if (bradj)
282                          shiftcolrs(scanin, bwr->hdr->width, bradj);
283 <                for (x = gry ? bwr->hdr->width : 0; x--; )
284 <                        scanin[x][GRN] = normbright(scanin[x]);
283 >                if (monpri == NULL && rgbinp != TM_XYZPRIM)
284 >                        for (x = bwr->hdr->width; x--; )
285 >                                scanin[x][GRN] = normbright(scanin[x]);
286                  colrs_gambs(scanin, bwr->hdr->width);
287 <                if (gry)
287 >                if (monpri == NULL)
288                          for (x = bwr->hdr->width; x--; )
289                                  bwr->scanline[x] = scanin[x][GRN];
290                  else
# Line 294 | Line 351 | tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIM
351          BMPWriter       *wtr;
352          FILE            *fp;
353          int             xr, yr;
354 <        BYTE            *pa;
354 >        uby8            *pa;
355          int             i;
356                                          /* check tone-mapping spec */
357          i = strlen(expec);
# Line 339 | Line 396 | tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIM
396                  quiterr("cannot allocate writer structure");
397                                          /* write to BMP file */
398          while (wtr->yscan < yr) {
399 <                BYTE    *scn = pa + xr*((tmflags & TM_F_BW) ? 1 : 3)*
399 >                uby8    *scn = pa + xr*((tmflags & TM_F_BW) ? 1 : 3)*
400                                                  (yr-1 - wtr->yscan);
401                  if (tmflags & TM_F_BW)
402                          memcpy((void *)wtr->scanline, (void *)scn, xr);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines