5 |
|
* program to convert between RADIANCE and Poskanzer Pixmaps |
6 |
|
*/ |
7 |
|
|
8 |
– |
#include <stdio.h> |
8 |
|
#include <math.h> |
9 |
|
#include <ctype.h> |
11 |
– |
#include <time.h> |
12 |
– |
|
10 |
|
#include "platform.h" |
11 |
+ |
#include "rtio.h" |
12 |
|
#include "color.h" |
13 |
|
#include "resolu.h" |
14 |
|
|
17 |
– |
|
18 |
– |
int agryscan(), bgryscan(), aclrscan(), bclrscan(); |
19 |
– |
int agryscan2(), bgryscan2(), aclrscan2(), bclrscan2(); |
20 |
– |
int normval(); |
21 |
– |
unsigned int scanint(), intv(), getby2(); |
22 |
– |
|
15 |
|
#define fltv(i) (((i)+.5)/(maxval+1.)) |
24 |
– |
|
16 |
|
int bradj = 0; /* brightness adjustment */ |
26 |
– |
|
17 |
|
double gamcor = 2.2; /* gamma correction value */ |
28 |
– |
|
18 |
|
int maxval = 255; /* maximum primary value */ |
19 |
+ |
int xmax, ymax; |
20 |
|
|
21 |
< |
char *progname; |
21 |
> |
typedef int colrscanf_t(COLR *scan, int len, FILE *fp); |
22 |
> |
typedef int colorscanf_t(COLOR *scan, int len, FILE *fp); |
23 |
|
|
24 |
< |
int xmax, ymax; |
24 |
> |
static colrscanf_t agryscan, bgryscan, aclrscan, bclrscan; |
25 |
> |
static void ppm2ra(colrscanf_t *getscan); |
26 |
> |
static void ra2ppm(int binary, int grey); |
27 |
|
|
28 |
+ |
static colorscanf_t agryscan2, bgryscan2, aclrscan2, bclrscan2; |
29 |
+ |
static void ppm2ra2(colorscanf_t *getscan); |
30 |
+ |
static void ra2ppm2(int binary, int grey); |
31 |
|
|
32 |
< |
main(argc, argv) |
33 |
< |
int argc; |
34 |
< |
char *argv[]; |
32 |
> |
static int normval(unsigned int v); |
33 |
> |
static unsigned int scanint(FILE *fp); |
34 |
> |
static unsigned int intv(double v); |
35 |
> |
static unsigned int getby2(FILE *fp); |
36 |
> |
static void putby2(unsigned int w, FILE *fp); |
37 |
> |
static void quiterr(char *err); |
38 |
> |
|
39 |
> |
|
40 |
> |
int |
41 |
> |
main( |
42 |
> |
int argc, |
43 |
> |
char *argv[] |
44 |
> |
) |
45 |
|
{ |
46 |
|
char inpbuf[2]; |
47 |
|
int gryflag = 0; |
50 |
|
int ptype; |
51 |
|
int i; |
52 |
|
|
53 |
< |
progname = argv[0]; |
53 |
> |
fixargv0(argv[0]); |
54 |
|
|
55 |
|
for (i = 1; i < argc; i++) |
56 |
|
if (argv[i][0] == '-') |
100 |
|
if (read(fileno(stdin), inpbuf, 2) != 2 || inpbuf[0] != 'P') |
101 |
|
quiterr("input not a Poskanzer Pixmap"); |
102 |
|
ptype = inpbuf[1]; |
103 |
< |
#ifdef _WIN32 |
103 |
> |
#if defined(_WIN32) || defined(_WIN64) |
104 |
|
if (ptype > 4) |
105 |
|
SET_FILE_BINARY(stdin); |
106 |
|
SET_FILE_BINARY(stdout); |
150 |
|
quiterr("unsupported Pixmap type"); |
151 |
|
} |
152 |
|
} else { |
153 |
< |
#ifdef _WIN32 |
153 |
> |
#if defined(_WIN32) || defined(_WIN64) |
154 |
|
SET_FILE_BINARY(stdin); |
155 |
|
if (binflag) |
156 |
|
SET_FILE_BINARY(stdout); |
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
< |
quiterr(err) /* print message and exit */ |
181 |
< |
char *err; |
180 |
> |
static void |
181 |
> |
quiterr( /* print message and exit */ |
182 |
> |
char *err |
183 |
> |
) |
184 |
|
{ |
185 |
|
if (err != NULL) { |
186 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
190 |
|
} |
191 |
|
|
192 |
|
|
193 |
< |
ppm2ra(getscan) /* convert 1-byte Pixmap to Radiance picture */ |
194 |
< |
int (*getscan)(); |
193 |
> |
static void |
194 |
> |
ppm2ra( /* convert 1-byte Pixmap to Radiance picture */ |
195 |
> |
colrscanf_t *getscan |
196 |
> |
) |
197 |
|
{ |
198 |
|
COLR *scanout; |
199 |
|
int y; |
216 |
|
} |
217 |
|
|
218 |
|
|
219 |
< |
ra2ppm(binary, grey) /* convert Radiance picture to Pixmap */ |
220 |
< |
int binary, grey; |
219 |
> |
static void |
220 |
> |
ra2ppm( /* convert Radiance picture to 1-byte/sample Pixmap */ |
221 |
> |
int binary, |
222 |
> |
int grey |
223 |
> |
) |
224 |
|
{ |
225 |
|
COLR *scanin; |
226 |
|
register int x; |
265 |
|
} |
266 |
|
|
267 |
|
|
268 |
< |
ppm2ra2(getscan) /* convert 2-byte Pixmap to Radiance picture */ |
269 |
< |
int (*getscan)(); |
268 |
> |
static void |
269 |
> |
ppm2ra2( /* convert 2-byte Pixmap to Radiance picture */ |
270 |
> |
colorscanf_t *getscan |
271 |
> |
) |
272 |
|
{ |
273 |
|
COLOR *scanout; |
274 |
|
double mult; |
284 |
|
for (y = ymax-1; y >= 0; y--) { |
285 |
|
if ((*getscan)(scanout, xmax, stdin) < 0) |
286 |
|
quiterr("error reading Pixmap"); |
287 |
< |
for (x = gamcor>1.01|gamcor<0.99?xmax:0; x--; ) { |
287 |
> |
for (x = (gamcor>1.01)|(gamcor<0.99)?xmax:0; x--; ) { |
288 |
|
colval(scanout[x],RED) = |
289 |
|
pow(colval(scanout[x],RED), gamcor); |
290 |
|
colval(scanout[x],GRN) = |
302 |
|
} |
303 |
|
|
304 |
|
|
305 |
< |
ra2ppm2(binary, grey) /* convert Radiance picture to Pixmap (2-byte) */ |
306 |
< |
int binary, grey; |
305 |
> |
static void |
306 |
> |
ra2ppm2( /* convert Radiance picture to Pixmap (2-byte) */ |
307 |
> |
int binary, |
308 |
> |
int grey |
309 |
> |
) |
310 |
|
{ |
311 |
|
COLOR *scanin; |
312 |
|
double mult, d; |
327 |
|
for (x = grey?xmax:0; x--; ) |
328 |
|
colval(scanin[x],GRN) = bright(scanin[x]); |
329 |
|
d = 1./gamcor; |
330 |
< |
for (x = d>1.01|d<0.99?xmax:0; x--; ) { |
330 |
> |
for (x = (d>1.01)|(d<0.99)?xmax:0; x--; ) { |
331 |
|
colval(scanin[x],GRN) = pow(colval(scanin[x],GRN), d); |
332 |
|
if (!grey) { |
333 |
|
colval(scanin[x],RED) = |
369 |
|
} |
370 |
|
|
371 |
|
|
372 |
< |
agryscan(scan, len, fp) /* get an ASCII greyscale scanline */ |
373 |
< |
register COLR *scan; |
374 |
< |
register int len; |
375 |
< |
FILE *fp; |
372 |
> |
static int |
373 |
> |
agryscan( /* get an ASCII greyscale scanline */ |
374 |
> |
register COLR *scan, |
375 |
> |
register int len, |
376 |
> |
FILE *fp |
377 |
> |
) |
378 |
|
{ |
379 |
|
while (len-- > 0) { |
380 |
|
scan[0][RED] = |
386 |
|
} |
387 |
|
|
388 |
|
|
389 |
< |
bgryscan(scan, len, fp) /* get a binary greyscale scanline */ |
390 |
< |
register COLR *scan; |
391 |
< |
int len; |
392 |
< |
register FILE *fp; |
389 |
> |
static int |
390 |
> |
bgryscan( /* get a binary greyscale scanline */ |
391 |
> |
register COLR *scan, |
392 |
> |
int len, |
393 |
> |
register FILE *fp |
394 |
> |
) |
395 |
|
{ |
396 |
|
register int c; |
397 |
|
|
409 |
|
} |
410 |
|
|
411 |
|
|
412 |
< |
aclrscan(scan, len, fp) /* get an ASCII color scanline */ |
413 |
< |
register COLR *scan; |
414 |
< |
register int len; |
415 |
< |
FILE *fp; |
412 |
> |
static int |
413 |
> |
aclrscan( /* get an ASCII color scanline */ |
414 |
> |
register COLR *scan, |
415 |
> |
register int len, |
416 |
> |
FILE *fp |
417 |
> |
) |
418 |
|
{ |
419 |
|
while (len-- > 0) { |
420 |
|
scan[0][RED] = normval(scanint(fp)); |
426 |
|
} |
427 |
|
|
428 |
|
|
429 |
< |
bclrscan(scan, len, fp) /* get a binary color scanline */ |
430 |
< |
register COLR *scan; |
431 |
< |
int len; |
432 |
< |
register FILE *fp; |
429 |
> |
static int |
430 |
> |
bclrscan( /* get a binary color scanline */ |
431 |
> |
register COLR *scan, |
432 |
> |
int len, |
433 |
> |
register FILE *fp |
434 |
> |
) |
435 |
|
{ |
436 |
|
int r, g, b; |
437 |
|
|
455 |
|
} |
456 |
|
|
457 |
|
|
458 |
< |
agryscan2(scan, len, fp) /* get an ASCII greyscale scanline */ |
459 |
< |
register COLOR *scan; |
460 |
< |
register int len; |
461 |
< |
FILE *fp; |
458 |
> |
static int |
459 |
> |
agryscan2( /* get an ASCII greyscale scanline */ |
460 |
> |
register COLOR *scan, |
461 |
> |
register int len, |
462 |
> |
FILE *fp |
463 |
> |
) |
464 |
|
{ |
465 |
|
while (len-- > 0) { |
466 |
|
colval(scan[0],RED) = |
472 |
|
} |
473 |
|
|
474 |
|
|
475 |
< |
bgryscan2(scan, len, fp) /* get a binary greyscale scanline */ |
476 |
< |
register COLOR *scan; |
477 |
< |
int len; |
478 |
< |
register FILE *fp; |
475 |
> |
static int |
476 |
> |
bgryscan2( /* get a binary greyscale scanline */ |
477 |
> |
register COLOR *scan, |
478 |
> |
int len, |
479 |
> |
register FILE *fp |
480 |
> |
) |
481 |
|
{ |
482 |
|
register int c; |
483 |
|
|
493 |
|
} |
494 |
|
|
495 |
|
|
496 |
< |
aclrscan2(scan, len, fp) /* get an ASCII color scanline */ |
497 |
< |
register COLOR *scan; |
498 |
< |
register int len; |
499 |
< |
FILE *fp; |
496 |
> |
static int |
497 |
> |
aclrscan2( /* get an ASCII color scanline */ |
498 |
> |
register COLOR *scan, |
499 |
> |
register int len, |
500 |
> |
FILE *fp |
501 |
> |
) |
502 |
|
{ |
503 |
|
while (len-- > 0) { |
504 |
|
colval(scan[0],RED) = fltv(scanint(fp)); |
510 |
|
} |
511 |
|
|
512 |
|
|
513 |
< |
bclrscan2(scan, len, fp) /* get a binary color scanline */ |
514 |
< |
register COLOR *scan; |
515 |
< |
int len; |
516 |
< |
register FILE *fp; |
513 |
> |
static int |
514 |
> |
bclrscan2( /* get a binary color scanline */ |
515 |
> |
register COLOR *scan, |
516 |
> |
int len, |
517 |
> |
register FILE *fp |
518 |
> |
) |
519 |
|
{ |
520 |
|
int r, g, b; |
521 |
|
|
533 |
|
} |
534 |
|
|
535 |
|
|
536 |
< |
unsigned int |
537 |
< |
scanint(fp) /* scan the next positive integer value */ |
538 |
< |
register FILE *fp; |
536 |
> |
static unsigned int |
537 |
> |
scanint( /* scan the next positive integer value */ |
538 |
> |
register FILE *fp |
539 |
> |
) |
540 |
|
{ |
541 |
|
register int c; |
542 |
|
register unsigned int i; |
562 |
|
} |
563 |
|
|
564 |
|
|
565 |
< |
int |
566 |
< |
normval(v) /* normalize a value to [0,255] */ |
567 |
< |
register unsigned int v; |
565 |
> |
static int |
566 |
> |
normval( /* normalize a value to [0,255] */ |
567 |
> |
register unsigned int v |
568 |
> |
) |
569 |
|
{ |
570 |
|
if (v >= maxval) |
571 |
|
return(255); |
575 |
|
} |
576 |
|
|
577 |
|
|
578 |
< |
unsigned int |
579 |
< |
getby2(fp) /* return 2-byte quantity from fp */ |
580 |
< |
register FILE *fp; |
578 |
> |
static unsigned int |
579 |
> |
getby2( /* return 2-byte quantity from fp */ |
580 |
> |
register FILE *fp |
581 |
> |
) |
582 |
|
{ |
583 |
|
register int lowerb, upperb; |
584 |
|
|
548 |
– |
lowerb = getc(fp); |
585 |
|
upperb = getc(fp); |
586 |
< |
if (upperb == EOF) |
586 |
> |
lowerb = getc(fp); |
587 |
> |
if (lowerb == EOF) |
588 |
|
return(EOF); |
589 |
|
return(upperb<<8 | lowerb); |
590 |
|
} |
591 |
|
|
592 |
|
|
593 |
< |
putby2(w, fp) /* put 2-byte quantity to fp */ |
594 |
< |
register unsigned int w; |
595 |
< |
register FILE *fp; |
593 |
> |
static void |
594 |
> |
putby2( /* put 2-byte quantity to fp */ |
595 |
> |
register unsigned int w, |
596 |
> |
register FILE *fp |
597 |
> |
) |
598 |
|
{ |
560 |
– |
putc(w & 0xff, fp); |
599 |
|
putc(w>>8 & 0xff, fp); |
600 |
+ |
putc(w & 0xff, fp); |
601 |
|
if (ferror(fp)) { |
602 |
|
fprintf(stderr, "%s: write error on PPM output\n", progname); |
603 |
|
exit(1); |
605 |
|
} |
606 |
|
|
607 |
|
|
608 |
< |
unsigned int |
609 |
< |
intv(v) /* return integer quantity for v */ |
610 |
< |
register double v; |
608 |
> |
static unsigned int |
609 |
> |
intv( /* return integer quantity for v */ |
610 |
> |
register double v |
611 |
> |
) |
612 |
|
{ |
613 |
|
if (v >= 0.99999) |
614 |
|
return(maxval); |