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> |
10 |
|
|
11 |
|
#include "platform.h" |
12 |
+ |
#include "rtio.h" |
13 |
|
#include "color.h" |
14 |
|
#include "resolu.h" |
15 |
|
|
17 |
– |
|
18 |
– |
int agryscan(), bgryscan(), aclrscan(), bclrscan(); |
19 |
– |
int agryscan2(), bgryscan2(), aclrscan2(), bclrscan2(); |
20 |
– |
int normval(); |
21 |
– |
unsigned int scanint(), intv(), getby2(); |
22 |
– |
|
16 |
|
#define fltv(i) (((i)+.5)/(maxval+1.)) |
24 |
– |
|
17 |
|
int bradj = 0; /* brightness adjustment */ |
26 |
– |
|
18 |
|
double gamcor = 2.2; /* gamma correction value */ |
28 |
– |
|
19 |
|
int maxval = 255; /* maximum primary value */ |
30 |
– |
|
20 |
|
char *progname; |
32 |
– |
|
21 |
|
int xmax, ymax; |
22 |
|
|
23 |
+ |
typedef int colrscanf_t(COLR *scan, int len, FILE *fp); |
24 |
+ |
typedef int colorscanf_t(COLOR *scan, int len, FILE *fp); |
25 |
|
|
26 |
< |
main(argc, argv) |
27 |
< |
int argc; |
28 |
< |
char *argv[]; |
26 |
> |
static colrscanf_t agryscan, bgryscan, aclrscan, bclrscan; |
27 |
> |
static void ppm2ra(colrscanf_t *getscan); |
28 |
> |
static void ra2ppm(int binary, int grey); |
29 |
> |
|
30 |
> |
static colorscanf_t agryscan2, bgryscan2, aclrscan2, bclrscan2; |
31 |
> |
static void ppm2ra2(colorscanf_t *getscan); |
32 |
> |
static void ra2ppm2(int binary, int grey); |
33 |
> |
|
34 |
> |
static int normval(unsigned int v); |
35 |
> |
static unsigned int scanint(FILE *fp); |
36 |
> |
static unsigned int intv(double v); |
37 |
> |
static unsigned int getby2(FILE *fp); |
38 |
> |
static void putby2(unsigned int w, FILE *fp); |
39 |
> |
static void quiterr(char *err); |
40 |
> |
|
41 |
> |
|
42 |
> |
int |
43 |
> |
main( |
44 |
> |
int argc, |
45 |
> |
char *argv[] |
46 |
> |
) |
47 |
|
{ |
48 |
|
char inpbuf[2]; |
49 |
|
int gryflag = 0; |
102 |
|
if (read(fileno(stdin), inpbuf, 2) != 2 || inpbuf[0] != 'P') |
103 |
|
quiterr("input not a Poskanzer Pixmap"); |
104 |
|
ptype = inpbuf[1]; |
105 |
< |
#ifdef _WIN32 |
105 |
> |
#if defined(_WIN32) || defined(_WIN64) |
106 |
|
if (ptype > 4) |
107 |
|
SET_FILE_BINARY(stdin); |
108 |
|
SET_FILE_BINARY(stdout); |
152 |
|
quiterr("unsupported Pixmap type"); |
153 |
|
} |
154 |
|
} else { |
155 |
< |
#ifdef _WIN32 |
155 |
> |
#if defined(_WIN32) || defined(_WIN64) |
156 |
|
SET_FILE_BINARY(stdin); |
157 |
|
if (binflag) |
158 |
|
SET_FILE_BINARY(stdout); |
179 |
|
} |
180 |
|
|
181 |
|
|
182 |
< |
quiterr(err) /* print message and exit */ |
183 |
< |
char *err; |
182 |
> |
static void |
183 |
> |
quiterr( /* print message and exit */ |
184 |
> |
char *err |
185 |
> |
) |
186 |
|
{ |
187 |
|
if (err != NULL) { |
188 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
192 |
|
} |
193 |
|
|
194 |
|
|
195 |
< |
ppm2ra(getscan) /* convert 1-byte Pixmap to Radiance picture */ |
196 |
< |
int (*getscan)(); |
195 |
> |
static void |
196 |
> |
ppm2ra( /* convert 1-byte Pixmap to Radiance picture */ |
197 |
> |
colrscanf_t *getscan |
198 |
> |
) |
199 |
|
{ |
200 |
|
COLR *scanout; |
201 |
|
int y; |
218 |
|
} |
219 |
|
|
220 |
|
|
221 |
< |
ra2ppm(binary, grey) /* convert Radiance picture to Pixmap */ |
222 |
< |
int binary, grey; |
221 |
> |
static void |
222 |
> |
ra2ppm( /* convert Radiance picture to 1-byte/sample Pixmap */ |
223 |
> |
int binary, |
224 |
> |
int grey |
225 |
> |
) |
226 |
|
{ |
227 |
|
COLR *scanin; |
228 |
|
register int x; |
267 |
|
} |
268 |
|
|
269 |
|
|
270 |
< |
ppm2ra2(getscan) /* convert 2-byte Pixmap to Radiance picture */ |
271 |
< |
int (*getscan)(); |
270 |
> |
static void |
271 |
> |
ppm2ra2( /* convert 2-byte Pixmap to Radiance picture */ |
272 |
> |
colorscanf_t *getscan |
273 |
> |
) |
274 |
|
{ |
275 |
|
COLOR *scanout; |
276 |
|
double mult; |
304 |
|
} |
305 |
|
|
306 |
|
|
307 |
< |
ra2ppm2(binary, grey) /* convert Radiance picture to Pixmap (2-byte) */ |
308 |
< |
int binary, grey; |
307 |
> |
static void |
308 |
> |
ra2ppm2( /* convert Radiance picture to Pixmap (2-byte) */ |
309 |
> |
int binary, |
310 |
> |
int grey |
311 |
> |
) |
312 |
|
{ |
313 |
|
COLOR *scanin; |
314 |
|
double mult, d; |
371 |
|
} |
372 |
|
|
373 |
|
|
374 |
< |
agryscan(scan, len, fp) /* get an ASCII greyscale scanline */ |
375 |
< |
register COLR *scan; |
376 |
< |
register int len; |
377 |
< |
FILE *fp; |
374 |
> |
static int |
375 |
> |
agryscan( /* get an ASCII greyscale scanline */ |
376 |
> |
register COLR *scan, |
377 |
> |
register int len, |
378 |
> |
FILE *fp |
379 |
> |
) |
380 |
|
{ |
381 |
|
while (len-- > 0) { |
382 |
|
scan[0][RED] = |
388 |
|
} |
389 |
|
|
390 |
|
|
391 |
< |
bgryscan(scan, len, fp) /* get a binary greyscale scanline */ |
392 |
< |
register COLR *scan; |
393 |
< |
int len; |
394 |
< |
register FILE *fp; |
391 |
> |
static int |
392 |
> |
bgryscan( /* get a binary greyscale scanline */ |
393 |
> |
register COLR *scan, |
394 |
> |
int len, |
395 |
> |
register FILE *fp |
396 |
> |
) |
397 |
|
{ |
398 |
|
register int c; |
399 |
|
|
411 |
|
} |
412 |
|
|
413 |
|
|
414 |
< |
aclrscan(scan, len, fp) /* get an ASCII color scanline */ |
415 |
< |
register COLR *scan; |
416 |
< |
register int len; |
417 |
< |
FILE *fp; |
414 |
> |
static int |
415 |
> |
aclrscan( /* get an ASCII color scanline */ |
416 |
> |
register COLR *scan, |
417 |
> |
register int len, |
418 |
> |
FILE *fp |
419 |
> |
) |
420 |
|
{ |
421 |
|
while (len-- > 0) { |
422 |
|
scan[0][RED] = normval(scanint(fp)); |
428 |
|
} |
429 |
|
|
430 |
|
|
431 |
< |
bclrscan(scan, len, fp) /* get a binary color scanline */ |
432 |
< |
register COLR *scan; |
433 |
< |
int len; |
434 |
< |
register FILE *fp; |
431 |
> |
static int |
432 |
> |
bclrscan( /* get a binary color scanline */ |
433 |
> |
register COLR *scan, |
434 |
> |
int len, |
435 |
> |
register FILE *fp |
436 |
> |
) |
437 |
|
{ |
438 |
|
int r, g, b; |
439 |
|
|
457 |
|
} |
458 |
|
|
459 |
|
|
460 |
< |
agryscan2(scan, len, fp) /* get an ASCII greyscale scanline */ |
461 |
< |
register COLOR *scan; |
462 |
< |
register int len; |
463 |
< |
FILE *fp; |
460 |
> |
static int |
461 |
> |
agryscan2( /* get an ASCII greyscale scanline */ |
462 |
> |
register COLOR *scan, |
463 |
> |
register int len, |
464 |
> |
FILE *fp |
465 |
> |
) |
466 |
|
{ |
467 |
|
while (len-- > 0) { |
468 |
|
colval(scan[0],RED) = |
474 |
|
} |
475 |
|
|
476 |
|
|
477 |
< |
bgryscan2(scan, len, fp) /* get a binary greyscale scanline */ |
478 |
< |
register COLOR *scan; |
479 |
< |
int len; |
480 |
< |
register FILE *fp; |
477 |
> |
static int |
478 |
> |
bgryscan2( /* get a binary greyscale scanline */ |
479 |
> |
register COLOR *scan, |
480 |
> |
int len, |
481 |
> |
register FILE *fp |
482 |
> |
) |
483 |
|
{ |
484 |
|
register int c; |
485 |
|
|
495 |
|
} |
496 |
|
|
497 |
|
|
498 |
< |
aclrscan2(scan, len, fp) /* get an ASCII color scanline */ |
499 |
< |
register COLOR *scan; |
500 |
< |
register int len; |
501 |
< |
FILE *fp; |
498 |
> |
static int |
499 |
> |
aclrscan2( /* get an ASCII color scanline */ |
500 |
> |
register COLOR *scan, |
501 |
> |
register int len, |
502 |
> |
FILE *fp |
503 |
> |
) |
504 |
|
{ |
505 |
|
while (len-- > 0) { |
506 |
|
colval(scan[0],RED) = fltv(scanint(fp)); |
512 |
|
} |
513 |
|
|
514 |
|
|
515 |
< |
bclrscan2(scan, len, fp) /* get a binary color scanline */ |
516 |
< |
register COLOR *scan; |
517 |
< |
int len; |
518 |
< |
register FILE *fp; |
515 |
> |
static int |
516 |
> |
bclrscan2( /* get a binary color scanline */ |
517 |
> |
register COLOR *scan, |
518 |
> |
int len, |
519 |
> |
register FILE *fp |
520 |
> |
) |
521 |
|
{ |
522 |
|
int r, g, b; |
523 |
|
|
535 |
|
} |
536 |
|
|
537 |
|
|
538 |
< |
unsigned int |
539 |
< |
scanint(fp) /* scan the next positive integer value */ |
540 |
< |
register FILE *fp; |
538 |
> |
static unsigned int |
539 |
> |
scanint( /* scan the next positive integer value */ |
540 |
> |
register FILE *fp |
541 |
> |
) |
542 |
|
{ |
543 |
|
register int c; |
544 |
|
register unsigned int i; |
564 |
|
} |
565 |
|
|
566 |
|
|
567 |
< |
int |
568 |
< |
normval(v) /* normalize a value to [0,255] */ |
569 |
< |
register unsigned int v; |
567 |
> |
static int |
568 |
> |
normval( /* normalize a value to [0,255] */ |
569 |
> |
register unsigned int v |
570 |
> |
) |
571 |
|
{ |
572 |
|
if (v >= maxval) |
573 |
|
return(255); |
577 |
|
} |
578 |
|
|
579 |
|
|
580 |
< |
unsigned int |
581 |
< |
getby2(fp) /* return 2-byte quantity from fp */ |
582 |
< |
register FILE *fp; |
580 |
> |
static unsigned int |
581 |
> |
getby2( /* return 2-byte quantity from fp */ |
582 |
> |
register FILE *fp |
583 |
> |
) |
584 |
|
{ |
585 |
|
register int lowerb, upperb; |
586 |
|
|
548 |
– |
lowerb = getc(fp); |
587 |
|
upperb = getc(fp); |
588 |
< |
if (upperb == EOF) |
588 |
> |
lowerb = getc(fp); |
589 |
> |
if (lowerb == EOF) |
590 |
|
return(EOF); |
591 |
|
return(upperb<<8 | lowerb); |
592 |
|
} |
593 |
|
|
594 |
|
|
595 |
< |
putby2(w, fp) /* put 2-byte quantity to fp */ |
596 |
< |
register unsigned int w; |
597 |
< |
register FILE *fp; |
595 |
> |
static void |
596 |
> |
putby2( /* put 2-byte quantity to fp */ |
597 |
> |
register unsigned int w, |
598 |
> |
register FILE *fp |
599 |
> |
) |
600 |
|
{ |
560 |
– |
putc(w & 0xff, fp); |
601 |
|
putc(w>>8 & 0xff, fp); |
602 |
+ |
putc(w & 0xff, fp); |
603 |
|
if (ferror(fp)) { |
604 |
|
fprintf(stderr, "%s: write error on PPM output\n", progname); |
605 |
|
exit(1); |
607 |
|
} |
608 |
|
|
609 |
|
|
610 |
< |
unsigned int |
611 |
< |
intv(v) /* return integer quantity for v */ |
612 |
< |
register double v; |
610 |
> |
static unsigned int |
611 |
> |
intv( /* return integer quantity for v */ |
612 |
> |
register double v |
613 |
> |
) |
614 |
|
{ |
615 |
|
if (v >= 0.99999) |
616 |
|
return(maxval); |