ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_bmp.c
Revision: 2.13
Committed: Fri Jul 19 17:37:56 2019 UTC (4 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.12: +2 -3 lines
Log Message:
Moved declarations and definitions for header.c from resolu.h to rtio.h

File Contents

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