--- ray/src/px/ra_ppm.c 2003/02/22 02:07:28 2.9 +++ ray/src/px/ra_ppm.c 2006/01/06 06:40:53 2.13 @@ -1,48 +1,50 @@ #ifndef lint -static const char RCSid[] = "$Id: ra_ppm.c,v 2.9 2003/02/22 02:07:28 greg Exp $"; +static const char RCSid[] = "$Id: ra_ppm.c,v 2.13 2006/01/06 06:40:53 greg Exp $"; #endif /* * program to convert between RADIANCE and Poskanzer Pixmaps */ #include - -#ifdef MSDOS -#include -#endif - #include - #include - #include +#include "platform.h" #include "color.h" - #include "resolu.h" - -int agryscan(), bgryscan(), aclrscan(), bclrscan(); -int agryscan2(), bgryscan2(), aclrscan2(), bclrscan2(); -int normval(); -unsigned int scanint(), intv(), getby2(); - #define fltv(i) (((i)+.5)/(maxval+1.)) - int bradj = 0; /* brightness adjustment */ - double gamcor = 2.2; /* gamma correction value */ - int maxval = 255; /* maximum primary value */ - char *progname; - int xmax, ymax; +typedef int colrscanf_t(COLR *scan, int len, FILE *fp); +typedef int colorscanf_t(COLOR *scan, int len, FILE *fp); -main(argc, argv) -int argc; -char *argv[]; +static colrscanf_t agryscan, bgryscan, aclrscan, bclrscan; +static void ppm2ra(colrscanf_t *getscan); +static void ra2ppm(int binary, int grey); + +static colorscanf_t agryscan2, bgryscan2, aclrscan2, bclrscan2; +static void ppm2ra2(colorscanf_t *getscan); +static void ra2ppm2(int binary, int grey); + +static int normval(unsigned int v); +static unsigned int scanint(FILE *fp); +static unsigned int intv(double v); +static unsigned int getby2(FILE *fp); +static void putby2(unsigned int w, FILE *fp); +static void quiterr(char *err); + + +int +main( + int argc, + char *argv[] +) { char inpbuf[2]; int gryflag = 0; @@ -101,10 +103,10 @@ char *argv[]; if (read(fileno(stdin), inpbuf, 2) != 2 || inpbuf[0] != 'P') quiterr("input not a Poskanzer Pixmap"); ptype = inpbuf[1]; -#ifdef MSDOS +#ifdef _WIN32 if (ptype > 4) - setmode(fileno(stdin), O_BINARY); - setmode(fileno(stdout), O_BINARY); + SET_FILE_BINARY(stdin); + SET_FILE_BINARY(stdout); #endif xmax = scanint(stdin); ymax = scanint(stdin); @@ -151,10 +153,10 @@ char *argv[]; quiterr("unsupported Pixmap type"); } } else { -#ifdef MSDOS - setmode(fileno(stdin), O_BINARY); +#ifdef _WIN32 + SET_FILE_BINARY(stdin); if (binflag) - setmode(fileno(stdout), O_BINARY); + SET_FILE_BINARY(stdout); #endif /* get header info. */ if (checkheader(stdin, COLRFMT, NULL) < 0 || @@ -178,8 +180,10 @@ userr: } -quiterr(err) /* print message and exit */ -char *err; +static void +quiterr( /* print message and exit */ + char *err +) { if (err != NULL) { fprintf(stderr, "%s: %s\n", progname, err); @@ -189,8 +193,10 @@ char *err; } -ppm2ra(getscan) /* convert 1-byte Pixmap to Radiance picture */ -int (*getscan)(); +static void +ppm2ra( /* convert 1-byte Pixmap to Radiance picture */ + colrscanf_t *getscan +) { COLR *scanout; int y; @@ -213,8 +219,11 @@ int (*getscan)(); } -ra2ppm(binary, grey) /* convert Radiance picture to Pixmap */ -int binary, grey; +static void +ra2ppm( /* convert Radiance picture to 1-byte/sample Pixmap */ + int binary, + int grey +) { COLR *scanin; register int x; @@ -259,8 +268,10 @@ int binary, grey; } -ppm2ra2(getscan) /* convert 2-byte Pixmap to Radiance picture */ -int (*getscan)(); +static void +ppm2ra2( /* convert 2-byte Pixmap to Radiance picture */ + colorscanf_t *getscan +) { COLOR *scanout; double mult; @@ -276,7 +287,7 @@ int (*getscan)(); for (y = ymax-1; y >= 0; y--) { if ((*getscan)(scanout, xmax, stdin) < 0) quiterr("error reading Pixmap"); - for (x = gamcor>1.01|gamcor<0.99?xmax:0; x--; ) { + for (x = (gamcor>1.01)|(gamcor<0.99)?xmax:0; x--; ) { colval(scanout[x],RED) = pow(colval(scanout[x],RED), gamcor); colval(scanout[x],GRN) = @@ -294,8 +305,11 @@ int (*getscan)(); } -ra2ppm2(binary, grey) /* convert Radiance picture to Pixmap (2-byte) */ -int binary, grey; +static void +ra2ppm2( /* convert Radiance picture to Pixmap (2-byte) */ + int binary, + int grey +) { COLOR *scanin; double mult, d; @@ -316,7 +330,7 @@ int binary, grey; for (x = grey?xmax:0; x--; ) colval(scanin[x],GRN) = bright(scanin[x]); d = 1./gamcor; - for (x = d>1.01|d<0.99?xmax:0; x--; ) { + for (x = (d>1.01)|(d<0.99)?xmax:0; x--; ) { colval(scanin[x],GRN) = pow(colval(scanin[x],GRN), d); if (!grey) { colval(scanin[x],RED) = @@ -358,10 +372,12 @@ int binary, grey; } -agryscan(scan, len, fp) /* get an ASCII greyscale scanline */ -register COLR *scan; -register int len; -FILE *fp; +static int +agryscan( /* get an ASCII greyscale scanline */ + register COLR *scan, + register int len, + FILE *fp +) { while (len-- > 0) { scan[0][RED] = @@ -373,10 +389,12 @@ FILE *fp; } -bgryscan(scan, len, fp) /* get a binary greyscale scanline */ -register COLR *scan; -int len; -register FILE *fp; +static int +bgryscan( /* get a binary greyscale scanline */ + register COLR *scan, + int len, + register FILE *fp +) { register int c; @@ -394,10 +412,12 @@ register FILE *fp; } -aclrscan(scan, len, fp) /* get an ASCII color scanline */ -register COLR *scan; -register int len; -FILE *fp; +static int +aclrscan( /* get an ASCII color scanline */ + register COLR *scan, + register int len, + FILE *fp +) { while (len-- > 0) { scan[0][RED] = normval(scanint(fp)); @@ -409,10 +429,12 @@ FILE *fp; } -bclrscan(scan, len, fp) /* get a binary color scanline */ -register COLR *scan; -int len; -register FILE *fp; +static int +bclrscan( /* get a binary color scanline */ + register COLR *scan, + int len, + register FILE *fp +) { int r, g, b; @@ -436,10 +458,12 @@ register FILE *fp; } -agryscan2(scan, len, fp) /* get an ASCII greyscale scanline */ -register COLOR *scan; -register int len; -FILE *fp; +static int +agryscan2( /* get an ASCII greyscale scanline */ + register COLOR *scan, + register int len, + FILE *fp +) { while (len-- > 0) { colval(scan[0],RED) = @@ -451,10 +475,12 @@ FILE *fp; } -bgryscan2(scan, len, fp) /* get a binary greyscale scanline */ -register COLOR *scan; -int len; -register FILE *fp; +static int +bgryscan2( /* get a binary greyscale scanline */ + register COLOR *scan, + int len, + register FILE *fp +) { register int c; @@ -470,10 +496,12 @@ register FILE *fp; } -aclrscan2(scan, len, fp) /* get an ASCII color scanline */ -register COLOR *scan; -register int len; -FILE *fp; +static int +aclrscan2( /* get an ASCII color scanline */ + register COLOR *scan, + register int len, + FILE *fp +) { while (len-- > 0) { colval(scan[0],RED) = fltv(scanint(fp)); @@ -485,10 +513,12 @@ FILE *fp; } -bclrscan2(scan, len, fp) /* get a binary color scanline */ -register COLOR *scan; -int len; -register FILE *fp; +static int +bclrscan2( /* get a binary color scanline */ + register COLOR *scan, + int len, + register FILE *fp +) { int r, g, b; @@ -506,9 +536,10 @@ register FILE *fp; } -unsigned int -scanint(fp) /* scan the next positive integer value */ -register FILE *fp; +static unsigned int +scanint( /* scan the next positive integer value */ + register FILE *fp +) { register int c; register unsigned int i; @@ -534,9 +565,10 @@ tryagain: } -int -normval(v) /* normalize a value to [0,255] */ -register unsigned int v; +static int +normval( /* normalize a value to [0,255] */ + register unsigned int v +) { if (v >= maxval) return(255); @@ -546,26 +578,29 @@ register unsigned int v; } -unsigned int -getby2(fp) /* return 2-byte quantity from fp */ -register FILE *fp; +static unsigned int +getby2( /* return 2-byte quantity from fp */ + register FILE *fp +) { register int lowerb, upperb; - lowerb = getc(fp); upperb = getc(fp); + lowerb = getc(fp); if (upperb == EOF) return(EOF); return(upperb<<8 | lowerb); } -putby2(w, fp) /* put 2-byte quantity to fp */ -register unsigned int w; -register FILE *fp; +static void +putby2( /* put 2-byte quantity to fp */ + register unsigned int w, + register FILE *fp +) { - putc(w & 0xff, fp); putc(w>>8 & 0xff, fp); + putc(w & 0xff, fp); if (ferror(fp)) { fprintf(stderr, "%s: write error on PPM output\n", progname); exit(1); @@ -573,9 +608,10 @@ register FILE *fp; } -unsigned int -intv(v) /* return integer quantity for v */ -register double v; +static unsigned int +intv( /* return integer quantity for v */ + register double v +) { if (v >= 0.99999) return(maxval);