--- ray/src/px/ra_pict.c 1991/10/23 09:33:11 1.2 +++ ray/src/px/ra_pict.c 2004/03/28 20:33:14 2.10 @@ -1,25 +1,56 @@ #ifndef lint -static char SCCSid[] = "$SunId$ AU"; +static const char RCSid[] = "$Id: ra_pict.c,v 2.10 2004/03/28 20:33:14 schorsch Exp $"; #endif -/* - rad2pict - Convert an Radiance image to APPLE pict format. - - School of Architecture, Auckland University, Private Bag - Auckland, New Zealand -*/ +/* Convert an Radiance image to APPLE pict format. + * + * Orginally Iris to PICT by Paul Haeberli - 1990 + * Hacked into Rad to PICT by Russell Street 1990 + * + * History: + * V 1 -- Does basic conversion + * V 1.1 (2/11/91) -- Added options for Gamma + * -- verbose option + * -- man page + * -- better about allocating buffers + * V 1.2 (19/11/91) -- Revised to handle opening "The Radiance Way" + * -- Added exposure level adjustment + */ + #include -#include +#include +#include + +#include "platform.h" #include "pict.h" #include "color.h" +#include "resolu.h" -char cbuf[8192*5]; -char pbuf[8192]; -int outbytes; -FILE *outf, *inf; -char **gargv; +int outbytes; /* This had better be 32 bits! */ +char *progname; +int verbose = 0; +float gamcor = 2.0; +int bradj = 0; -putrect(xorg,yorg,xsize,ysize) -int xorg, yorg, xsize, ysize; +static void putrect(int xorg, int yorg, int xsize, int ysize); +static void putfprect(int xorg, int yorg, int xsize, int ysize); +static void putalong(long l); +static void putashort(short s); +static void putbyte(int b); +static void putbytes(unsigned char *buf, int n ); +static void putpict(int xsize, int ysize); +static void getrow(FILE *in, char *cbuf, int xsize); +static int packbits(unsigned char *ibits, unsigned char *pbits, int nbits); +static void usage(void); + + + /* First some utility routines */ +static void +putrect( + int xorg, + int yorg, + int xsize, + int ysize +) { putashort(yorg); putashort(xorg); @@ -27,8 +58,13 @@ int xorg, yorg, xsize, ysize; putashort(xsize); } -putfprect(xorg,yorg,xsize,ysize) -int xorg, yorg, xsize, ysize; +static void +putfprect( + int xorg, + int yorg, + int xsize, + int ysize +) { putalong(yorg<<16); putalong(xorg<<16); @@ -36,8 +72,10 @@ int xorg, yorg, xsize, ysize; putalong(xsize<<16); } -putalong(l) -long l; +static void +putalong( + long l +) { putbyte((l>>24)&0xff); putbyte((l>>16)&0xff); @@ -45,116 +83,194 @@ long l; putbyte((l>>0)&0xff); } -putashort(s) -short s; +static void +putashort( + short s +) { putbyte((s>>8)&0xff); putbyte((s>>0)&0xff); } -putbyte(b) -unsigned char b; +static void +putbyte( + int b +) { - char c[1]; - - c[0] = b; - if(!fwrite(c,1,1,outf)) { - fprintf(stderr,"%s: error on write\n", gargv[0]); + if (putc(b,stdout) == EOF && ferror(stdout)) { + fprintf(stderr,"%s: error on write\n", progname); exit(1); } outbytes++; } -putbytes(buf,n) -unsigned char *buf; -int n; +static void +putbytes( + unsigned char *buf, + int n +) { - if(!fwrite(buf,n,1,outf)) { - fprintf(stderr,"topict: error on write\n"); + if(!fwrite(buf,n,1,stdout)) { + fprintf(stderr,"%s: error on write\n", progname); exit(1); } outbytes+=n; } -main(argc,argv) -int argc; -char **argv; +int +main( + int argc, + char **argv +) { int xsize, ysize; int i, picsize; int ssizepos, lsizepos; + SET_DEFAULT_BINARY(); + SET_FILE_BINARY(stdin); + SET_FILE_BINARY(stdout); + progname = argv[0]; - gargv = argv; + for (i = 1; i < argc ; i++) + if (argv[i][0] == '-') + switch (argv[i][1]) { + case 'g': gamcor = atof(argv[++i]); + break; - if( argc<3 ) { - fprintf(stderr, "Usage: %s inimage out.pict\n", gargv[0]); - exit(1); - } + case 'e': if (argv[i+1][0] != '+' && argv[i+1][0] != '-') + usage(); + else + bradj = atoi(argv[++i]); + break; - if( (inf=fopen(gargv[1],"rb")) == NULL ) { - fprintf(stderr,"%s: can't open input file %s\n",gargv[0], gargv[1]); + case 'v': ; + verbose = 1; + break; + + case 'r': fprintf(stderr, "Sorry. Get a Macintosh :-)\n"); + exit(1); + + case '-': i++; + goto outofparse; + break; /* NOTREACHED */ + + default: + usage(); + break; + } + else + break; + +outofparse: + + if (i < argc - 2) + usage(); + + if (i <= argc - 1 && freopen(argv[i], "r", stdin) == NULL) { + fprintf(stderr, "%s: can not open input \"%s\"\n", + progname, argv[i]); exit(1); } - if( (outf=fopen(gargv[2],"wb")) == NULL ) { - fprintf(stderr,"%s: can't open output file %s\n", gargv[0], gargv[1]); + if (i <= argc - 2 && freopen(argv[i+1], "w", stdout) == NULL) { + fprintf(stderr, "%s: can not open input \"%s\"\n", + progname, argv[i+1]); exit(1); } + +#ifdef DEBUG + fprintf(stderr, "Input file: %s\n", i <= argc - 1 ? argv[i] : "stdin"); + fprintf(stderr, "Outut file: %s\n", i <= argc - 2 ? argv[i+1] : "stdout" ); + fprintf(stderr, "Gamma: %f\n", gamcor); + fprintf(stderr, "Brightness adjust: %d\n", bradj); + fprintf(stderr, "Verbose: %s\n", verbose ? "on" : "off"); +#endif - if (checkheader(inf, COLRFMT, NULL) < 0 || - fgetresolu(&xsize, &ysize, inf) != (YMAJOR|YDECR)) { - fprintf(stderr, "%s: not a radiance picture\n", argv[1]); - exit(1); - } - setcolrgam(2.0); + /* OK. Now we read the size of the Radiance picture */ + if (checkheader(stdin, COLRFMT, NULL) < 0 || + fgetresolu(&xsize, &ysize, stdin) < 0 /* != (YMAJOR|YDECR) */ ) { + fprintf(stderr, "%s: not a radiance picture\n", progname); + exit(1); + } + /* Set the gamma correction */ + + setcolrgam(gamcor); + for(i=0; i250) @@ -185,48 +305,58 @@ int ysize; else putbyte(nbytes); putbytes(pbuf,nbytes); - } + } if(outbytes&1) putbyte(0); + + free(cbuf); + free(pbuf); } -int -getrow(in, mybuff, xsize) -FILE *in; -char *mybuff; -int xsize; +static void +getrow( + FILE *in, + char *cbuf, + int xsize +) { - COLOR color; - COLR *scanin = (COLR*) malloc(xsize * sizeof(COLR)); + extern char *tempbuffer(); /* defined in color.c */ + COLR *scanin = NULL; int x; - if (scanin == NULL) { - printf("scanin null"); + if ((scanin = (COLR *)tempbuffer(xsize*sizeof(COLR))) == NULL) { + fprintf(stderr, "%s: not enough memory\n", progname); + exit(1); } - if (freadcolrs(scanin, xsize, in) < 0) { - fprintf(stderr, " read error\n"); - exit(1); - } + fprintf(stderr, "%s: read error\n", progname); + exit(1); + } - colrs_gambs(scanin, xsize); + if (bradj) /* Adjust exposure level */ + shiftcolrs(scanin, xsize, bradj); + + + colrs_gambs(scanin, xsize); /* Gamma correct it */ for (x = 0; x < xsize; x++) { - colr_color(color, scanin[x]); - cbuf[xsize * 0 + x] = color[RED] * 255; - cbuf[xsize * 1 + x] = color[GRN] * 255; - cbuf[xsize * 2 + x] = color[BLU] * 255; - } - free(scanin); + cbuf[x] = scanin[x][RED]; + cbuf[xsize + x] = scanin[x][GRN]; + cbuf[2*xsize + x] = scanin[x][BLU]; + } + } -packbits(ibits,pbits,nbits) -unsigned char *ibits, *pbits; -int nbits; + +static int +packbits( + unsigned char *ibits, + unsigned char *pbits, + int nbits +) { - int bytes; unsigned char *sptr; unsigned char *ibitsend; unsigned char *optr = pbits; @@ -234,12 +364,12 @@ int nbits; nbytes = ((nbits-1)/8)+1; ibitsend = ibits+nbytes; - while (ibits