ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_bmp.c
Revision: 2.10
Committed: Mon Oct 4 17:44:22 2004 UTC (19 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R7P2, rad3R7P1, rad4R0, rad3R6, rad3R6P1, rad3R8, rad3R9
Changes since 2.9: +3 -1 lines
Log Message:
Altered ra_bmp so it doesn't reverse scanline order on grayscale output

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.10 static const char RCSid[] = "$Id: ra_bmp.c,v 2.9 2004/09/23 18:00:54 greg Exp $";
3 greg 2.1 #endif
4     /*
5     * program to convert between RADIANCE and Windows BMP file
6     */
7    
8     #include <stdio.h>
9 greg 2.9 #include <math.h>
10 schorsch 2.3 #include <string.h>
11    
12 greg 2.1 #include "platform.h"
13     #include "color.h"
14 greg 2.4 #include "tonemap.h"
15 greg 2.1 #include "resolu.h"
16     #include "bmpfile.h"
17    
18 greg 2.9 int bradj = 0; /* brightness adjustment */
19 greg 2.1
20 greg 2.9 double gamcor = 2.2; /* gamma correction value */
21 greg 2.1
22 greg 2.9 char *progname;
23 greg 2.1
24 greg 2.4 static void quiterr(const char *err);
25     static void tmap2bmp(char *fnin, char *fnout, char *expec,
26     RGBPRIMP monpri, double gamval);
27 greg 2.9 static void rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, RGBPRIMP monpri);
28 schorsch 2.3 static void bmp2rad(BMPReader *brd, FILE *rfp, int inv);
29 greg 2.1
30 greg 2.9 static RGBPRIMP rgbinp = stdprims; /* RGB input primitives */
31     static RGBPRIMS myinprims; /* custom primitives holder */
32    
33     static gethfunc headline;
34    
35 greg 2.1
36     int
37     main(int argc, char *argv[])
38     {
39 greg 2.4 char *inpfile=NULL, *outfile=NULL;
40     char *expec = NULL;
41     int reverse = 0;
42     RGBPRIMP rgbp = stdprims;
43     RGBPRIMS myprims;
44     RESOLU rs;
45     int i;
46 greg 2.1
47     progname = argv[0];
48    
49     for (i = 1; i < argc; i++)
50 greg 2.6 if (argv[i][0] == '-' && argv[i][1])
51 greg 2.1 switch (argv[i][1]) {
52     case 'b':
53 greg 2.4 rgbp = NULL;
54 greg 2.1 break;
55     case 'g':
56     gamcor = atof(argv[++i]);
57     break;
58     case 'e':
59     if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
60 greg 2.4 expec = argv[++i];
61     else
62     bradj = atoi(argv[++i]);
63     break;
64     case 'p':
65     if (argc-i < 9)
66 greg 2.1 goto userr;
67 greg 2.4 myprims[RED][CIEX] = atof(argv[++i]);
68     myprims[RED][CIEY] = atof(argv[++i]);
69     myprims[GRN][CIEX] = atof(argv[++i]);
70     myprims[GRN][CIEY] = atof(argv[++i]);
71     myprims[BLU][CIEX] = atof(argv[++i]);
72     myprims[BLU][CIEY] = atof(argv[++i]);
73     myprims[WHT][CIEX] = atof(argv[++i]);
74     myprims[WHT][CIEY] = atof(argv[++i]);
75     if (rgbp == stdprims)
76     rgbp = myprims;
77 greg 2.1 break;
78     case 'r':
79     reverse = !reverse;
80     break;
81     default:
82     goto userr;
83     }
84     else
85     break;
86    
87     if (i < argc-2)
88     goto userr;
89    
90     SET_FILE_BINARY(stdin);
91     SET_FILE_BINARY(stdout);
92     SET_DEFAULT_BINARY();
93    
94     if (i <= argc-1 && strcmp(argv[i], "-"))
95     inpfile = argv[i];
96    
97     if (i == argc-2 && strcmp(argv[i+1], "-"))
98     outfile = argv[i+1];
99 greg 2.4 /* check for tone-mapping */
100     if (expec != NULL) {
101     if (reverse)
102     goto userr;
103     tmap2bmp(inpfile, outfile, expec, rgbp, gamcor);
104     return(0);
105     }
106 greg 2.1
107     setcolrgam(gamcor); /* set up conversion */
108    
109     if (reverse) {
110     BMPReader *rdr;
111     /* open BMP file or stream */
112     if (inpfile != NULL)
113     rdr = BMPopenInputFile(inpfile);
114     else
115     rdr = BMPopenInputStream(stdin);
116    
117     if (rdr == NULL) {
118     fprintf(stderr, "%s: cannot open or recognize BMP\n",
119     inpfile != NULL ? inpfile : "<stdin>");
120     exit(1);
121     }
122     /* open Radiance output */
123     if (outfile != NULL && freopen(outfile, "w", stdout) == NULL) {
124     fprintf(stderr, "%s: cannot open for output\n",
125     outfile);
126     exit(1);
127     }
128     /* put Radiance header */
129     newheader("RADIANCE", stdout);
130     printargs(i, argv, stdout);
131     fputformat(COLRFMT, stdout);
132     putchar('\n');
133     rs.xr = rdr->hdr->width;
134     rs.yr = rdr->hdr->height;
135     rs.rt = YMAJOR;
136 greg 2.2 /* write scans downward if we can */
137 greg 2.1 if (rdr->hdr->yIsDown || inpfile != NULL)
138     rs.rt |= YDECR;
139     fputsresolu(&rs, stdout);
140     /* convert file */
141     bmp2rad(rdr, stdout, !rdr->hdr->yIsDown && inpfile!=NULL);
142     /* flush output */
143 greg 2.2 BMPcloseInput(rdr);
144 greg 2.1 if (fflush(stdout) < 0)
145     quiterr("error writing Radiance output");
146     } else {
147     BMPHeader *hdr;
148     BMPWriter *wtr;
149     /* open Radiance input */
150     if (inpfile != NULL && freopen(inpfile, "r", stdin) == NULL) {
151     fprintf(stderr, "%s: cannot open input file\n",
152     inpfile);
153     exit(1);
154     }
155     /* get header info. */
156 greg 2.9 if (getheader(stdin, headline, NULL) < 0 ||
157 greg 2.1 !fgetsresolu(&rs, stdin))
158     quiterr("bad Radiance picture format");
159     /* initialize BMP header */
160 greg 2.4 if (rgbp == NULL) {
161     hdr = BMPmappedHeader(scanlen(&rs),
162     numscans(&rs), 0, 256);
163 greg 2.10 /*
164 greg 2.2 if (outfile != NULL)
165     hdr->compr = BI_RLE8;
166 greg 2.10 */
167 greg 2.2 } else
168 greg 2.4 hdr = BMPtruecolorHeader(scanlen(&rs),
169     numscans(&rs), 0);
170 greg 2.1 if (hdr == NULL)
171     quiterr("cannot initialize BMP header");
172 greg 2.2 /* set up output direction */
173 greg 2.7 hdr->yIsDown = ((outfile == NULL) | (hdr->compr == BI_RLE8));
174 greg 2.1 /* open BMP output */
175     if (outfile != NULL)
176     wtr = BMPopenOutputFile(outfile, hdr);
177     else
178     wtr = BMPopenOutputStream(stdout, hdr);
179 greg 2.2 if (wtr == NULL)
180     quiterr("cannot allocate writer structure");
181 greg 2.1 /* convert file */
182 greg 2.9 rad2bmp(stdin, wtr, !hdr->yIsDown, rgbp);
183 greg 2.1 /* flush output */
184     if (fflush((FILE *)wtr->c_data) < 0)
185     quiterr("error writing BMP output");
186 greg 2.2 BMPcloseOutput(wtr);
187 greg 2.1 }
188 greg 2.4 return(0); /* success */
189 greg 2.1 userr:
190     fprintf(stderr,
191 greg 2.4 "Usage: %s [-b][-g gamma][-e spec][-p xr yr xg yg xb yb xw yw] [input|- [output]]\n",
192 greg 2.1 progname);
193 greg 2.4 fprintf(stderr,
194     " or: %s -r [-g gamma][-e +/-stops] [input|- [output]]\n",
195     progname);
196     return(1);
197 greg 2.1 }
198    
199     /* print message and exit */
200 schorsch 2.3 static void
201 greg 2.1 quiterr(const char *err)
202     {
203     if (err != NULL) {
204     fprintf(stderr, "%s: %s\n", progname, err);
205     exit(1);
206     }
207     exit(0);
208     }
209    
210 greg 2.9 /* 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 greg 2.1 /* convert Radiance picture to BMP */
236 schorsch 2.3 static void
237 greg 2.9 rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, RGBPRIMP monpri)
238 greg 2.1 {
239 greg 2.9 int usexfm = 0;
240     COLORMAT xfm;
241 greg 2.1 COLR *scanin;
242 greg 2.9 COLOR cval;
243 greg 2.1 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 greg 2.9 /* 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 greg 2.1 /* convert image */
263     if (inv) {
264     y = bwr->hdr->height - 1;
265     ystp = -1; yend = -1;
266     } else {
267     y = 0;
268     ystp = 1; yend = bwr->hdr->height;
269     }
270     /* convert each scanline */
271     for ( ; y != yend; y += ystp) {
272     if (freadcolrs(scanin, bwr->hdr->width, rfp) < 0)
273     quiterr("error reading Radiance picture");
274 greg 2.9 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 greg 2.1 shiftcolrs(scanin, bwr->hdr->width, bradj);
284 greg 2.9 if (monpri == NULL && rgbinp != TM_XYZPRIM)
285     for (x = bwr->hdr->width; x--; )
286     scanin[x][GRN] = normbright(scanin[x]);
287 greg 2.1 colrs_gambs(scanin, bwr->hdr->width);
288 greg 2.9 if (monpri == NULL)
289 greg 2.1 for (x = bwr->hdr->width; x--; )
290     bwr->scanline[x] = scanin[x][GRN];
291     else
292     for (x = bwr->hdr->width; x--; ) {
293     bwr->scanline[3*x] = scanin[x][BLU];
294     bwr->scanline[3*x+1] = scanin[x][GRN];
295     bwr->scanline[3*x+2] = scanin[x][RED];
296     }
297     bwr->yscan = y;
298     x = BMPwriteScanline(bwr);
299     if (x != BIR_OK)
300     quiterr(BMPerrorMessage(x));
301     }
302     /* free scanline */
303     free((void *)scanin);
304     }
305    
306     /* convert BMP file to Radiance */
307 schorsch 2.3 static void
308 greg 2.1 bmp2rad(BMPReader *brd, FILE *rfp, int inv)
309     {
310     COLR *scanout;
311     int y, yend, ystp;
312     int x;
313     /* allocate scanline */
314     scanout = (COLR *)malloc(brd->hdr->width*sizeof(COLR));
315     if (scanout == NULL)
316     quiterr("out of memory in bmp2rad");
317     /* convert image */
318     if (inv) {
319     y = brd->hdr->height - 1;
320     ystp = -1; yend = -1;
321     } else {
322     y = 0;
323     ystp = 1; yend = brd->hdr->height;
324     }
325     /* convert each scanline */
326     for ( ; y != yend; y += ystp) {
327     x = BMPseekScanline(y, brd);
328     if (x != BIR_OK)
329     quiterr(BMPerrorMessage(x));
330     for (x = brd->hdr->width; x--; ) {
331     RGBquad rgbq = BMPdecodePixel(x, brd);
332     scanout[x][RED] = rgbq.r;
333     scanout[x][GRN] = rgbq.g;
334     scanout[x][BLU] = rgbq.b;
335     }
336     gambs_colrs(scanout, brd->hdr->width);
337     if (bradj)
338     shiftcolrs(scanout, brd->hdr->width, bradj);
339     if (fwritecolrs(scanout, brd->hdr->width, rfp) < 0)
340     quiterr("error writing Radiance picture");
341     }
342     /* clean up */
343     free((void *)scanout);
344     }
345 greg 2.4
346     /* Tone-map and convert Radiance picture */
347     static void
348     tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIMP monpri, double gamval)
349     {
350     int tmflags;
351     BMPHeader *hdr;
352     BMPWriter *wtr;
353     FILE *fp;
354     int xr, yr;
355     BYTE *pa;
356     int i;
357     /* check tone-mapping spec */
358     i = strlen(expec);
359     if (i && !strncmp(expec, "auto", i))
360     tmflags = TM_F_CAMERA;
361     else if (i && !strncmp(expec, "human", i))
362     tmflags = TM_F_HUMAN & ~TM_F_UNIMPL;
363     else if (i && !strncmp(expec, "linear", i))
364     tmflags = TM_F_LINEAR;
365     else
366     quiterr("illegal exposure specification (auto|human|linear)");
367     if (monpri == NULL) {
368     tmflags |= TM_F_BW;
369     monpri = stdprims;
370     }
371     /* open Radiance input */
372     if (fnin == NULL)
373     fp = stdin;
374     else if ((fp = fopen(fnin, "r")) == NULL) {
375     fprintf(stderr, "%s: cannot open\n", fnin);
376     exit(1);
377     }
378     /* tone-map picture */
379     if (tmMapPicture(&pa, &xr, &yr, tmflags, monpri, gamval,
380     0., 0., fnin, fp) != TM_E_OK)
381     exit(1);
382     /* initialize BMP header */
383     if (tmflags & TM_F_BW) {
384     hdr = BMPmappedHeader(xr, yr, 0, 256);
385 greg 2.5 if (fnout != NULL)
386     hdr->compr = BI_RLE8;
387 greg 2.4 } else
388     hdr = BMPtruecolorHeader(xr, yr, 0);
389     if (hdr == NULL)
390     quiterr("cannot initialize BMP header");
391     /* open BMP output */
392     if (fnout != NULL)
393     wtr = BMPopenOutputFile(fnout, hdr);
394     else
395     wtr = BMPopenOutputStream(stdout, hdr);
396     if (wtr == NULL)
397     quiterr("cannot allocate writer structure");
398     /* write to BMP file */
399     while (wtr->yscan < yr) {
400     BYTE *scn = pa + xr*((tmflags & TM_F_BW) ? 1 : 3)*
401 greg 2.7 (yr-1 - wtr->yscan);
402 greg 2.4 if (tmflags & TM_F_BW)
403     memcpy((void *)wtr->scanline, (void *)scn, xr);
404     else
405     for (i = xr; i--; ) {
406     wtr->scanline[3*i] = scn[3*i+BLU];
407     wtr->scanline[3*i+1] = scn[3*i+GRN];
408     wtr->scanline[3*i+2] = scn[3*i+RED];
409     }
410     if ((i = BMPwriteScanline(wtr)) != BIR_OK)
411     quiterr(BMPerrorMessage(i));
412     }
413     /* flush output */
414     if (fflush((FILE *)wtr->c_data) < 0)
415     quiterr("error writing BMP output");
416     /* clean up */
417 greg 2.8 if (fnin != NULL)
418     fclose(fp);
419 greg 2.4 free((void *)pa);
420     BMPcloseOutput(wtr);
421     }