--- ray/src/px/ra_pr.c 1989/02/02 10:49:35 1.1 +++ ray/src/px/ra_pr.c 2004/03/28 20:33:14 2.7 @@ -1,9 +1,6 @@ -/* Copyright (c) 1986 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: ra_pr.c,v 2.7 2004/03/28 20:33:14 schorsch Exp $"; #endif - /* * ra_pr.c - program to convert between RADIANCE and pixrect picture format. * @@ -12,11 +9,13 @@ static char SCCSid[] = "$SunId$ LBL"; */ #include +#include +#include -#include - +#include "rtmisc.h" +#include "rasterfile.h" #include "color.h" - +#include "resolu.h" #include "pic.h" /* descriptor for a picture file or frame buffer */ @@ -31,30 +30,23 @@ typedef struct { } pos; /* position(s) */ } pic; -extern pic *openinput(), *openoutput(); -extern char *ecalloc(), *emalloc(); - -extern long ftell(); - -extern double atof(), pow(); - -double gamma = 2.0; /* gamma correction */ - +double gamcor = 2.2; /* gamma correction */ +int bradj = 0; /* brightness adjustment */ pic *inpic, *outpic; - char *progname; - char errmsg[128]; - -COLR *inline; - +COLR *inl; int xmax, ymax; +static void quiterr(char *err); +static pic * openinput(char *fname, struct rasterfile *h); +static pic * openoutput(char *fname, struct rasterfile *h); +static void pr2ra(struct rasterfile *h); -main(argc, argv) -int argc; -char *argv[]; + +int +main(int argc, char *argv[]) { colormap rasmap; struct rasterfile head; @@ -73,11 +65,16 @@ char *argv[]; dither = !dither; break; case 'g': - gamma = atof(argv[++i]); + gamcor = atof(argv[++i]); break; case 'b': greyscale = !greyscale; break; + case 'e': + if (argv[i+1][0] != '+' && argv[i+1][0] != '-') + goto userr; + bradj = atoi(argv[++i]); + break; case 'r': reverse = !reverse; break; @@ -102,7 +99,7 @@ char *argv[]; quiterr(errmsg); } /* get header */ - if (fread(&head, sizeof(head), 1, stdin) != 1) + if (fread((char *)&head, sizeof(head), 1, stdin) != 1) quiterr("missing header"); if (head.ras_magic != RAS_MAGIC) quiterr("bad raster format"); @@ -113,13 +110,15 @@ char *argv[]; head.ras_depth != 8) quiterr("incompatible format"); /* put header */ - printargs(argc, argv, stdout); + newheader("RADIANCE", stdout); + printargs(i, argv, stdout); + fputformat(COLRFMT, stdout); putchar('\n'); - printf("-Y %d +X %d\n", ymax, xmax); + fprtresolu(xmax, ymax, stdout); /* convert file */ pr2ra(&head); } else { - if (i > argc-1 || i < argc-2) + if (i < argc-2 || (!greyscale && i > argc-1)) goto userr; if ((inpic = openinput(argv[i], &head)) == NULL) { sprintf(errmsg, "can't open input \"%s\"", argv[i]); @@ -139,16 +138,18 @@ char *argv[]; quiterr(NULL); userr: fprintf(stderr, - "Usage: %s [-d][-c ncolors][-b][-g gamma] input [output]\n", + "Usage: %s [-d][-c ncolors][-b][-g gamma][-e +/-stops] input [output]\n", progname); - fprintf(stderr, " Or: %s -r [-g gamma] [input [output]]\n", + fprintf(stderr, " Or: %s -r [-g gamma][-e +/-stops] [input [output]]\n", progname); exit(1); } -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); @@ -158,6 +159,7 @@ char *err; } +void eputs(s) char *s; { @@ -165,6 +167,7 @@ char *s; } +void quit(code) int code; { @@ -172,10 +175,11 @@ int code; } -pic * -openinput(fname, h) /* open RADIANCE input file */ -char *fname; -register struct rasterfile *h; +static pic * +openinput( /* open RADIANCE input file */ + char *fname, + register struct rasterfile *h +) { register pic *p; @@ -185,10 +189,10 @@ register struct rasterfile *h; p->fp = stdin; else if ((p->fp = fopen(fname, "r")) == NULL) return(NULL); - /* discard header */ - getheader(p->fp, NULL); - if (fscanf(p->fp, "-Y %d +X %d\n", &ymax, &xmax) != 2) - quiterr("bad picture size"); + /* check header */ + if (checkheader(p->fp, COLRFMT, NULL) < 0 || + fgetresolu(&xmax, &ymax, p->fp) < 0) + quiterr("bad picture format"); p->nexty = 0; p->bytes_line = 0; /* variable length lines */ p->pos.y = (long *)ecalloc(ymax, sizeof(long)); @@ -203,16 +207,17 @@ register struct rasterfile *h; h->ras_maptype = RMT_EQUAL_RGB; h->ras_maplength = 256*3; /* allocate scanline */ - inline = (COLR *)emalloc(xmax*sizeof(COLR)); + inl = (COLR *)emalloc(xmax*sizeof(COLR)); return(p); } -pic * -openoutput(fname, h) /* open output rasterfile */ -char *fname; -register struct rasterfile *h; +static pic * +openoutput( /* open output rasterfile */ + char *fname, + register struct rasterfile *h +) { register pic *p; @@ -223,7 +228,7 @@ register struct rasterfile *h; else if ((p->fp = fopen(fname, "w")) == NULL) return(NULL); /* write header */ - fwrite(h, sizeof(*h), 1, p->fp); + fwrite((char *)h, sizeof(*h), 1, p->fp); p->nexty = -1; /* needs color map */ p->bytes_line = h->ras_width; p->pos.b = 0; @@ -232,8 +237,10 @@ register struct rasterfile *h; } -pr2ra(h) /* pixrect file to RADIANCE file */ -struct rasterfile *h; +static void +pr2ra( /* pixrect file to RADIANCE file */ + struct rasterfile *h +) { BYTE cmap[3][256]; COLR ctab[256]; @@ -243,14 +250,16 @@ struct rasterfile *h; scanline = (COLR *)emalloc(xmax*sizeof(COLR)); /* get color table */ for (i = 0; i < 3; i ++) - if (fread(cmap[i], h->ras_maplength/3, 1, stdin) != 1) + if (fread((char *)cmap[i], h->ras_maplength/3, 1, stdin) != 1) quiterr("error reading color table"); /* convert table */ for (i = 0; i < h->ras_maplength/3; i++) setcolr(ctab[i], - pow((cmap[0][i]+.5)/256.,gamma), - pow((cmap[1][i]+.5)/256.,gamma), - pow((cmap[2][i]+.5)/256.,gamma)); + pow((cmap[0][i]+.5)/256.,gamcor), + pow((cmap[1][i]+.5)/256.,gamcor), + pow((cmap[2][i]+.5)/256.,gamcor)); + if (bradj) + shiftcolrs(ctab, 256, bradj); /* convert file */ for (i = 0; i < ymax; i++) { for (j = 0; j < xmax; j++) { @@ -263,23 +272,23 @@ struct rasterfile *h; if (fwritecolrs(scanline, xmax, stdout) < 0) quiterr("error writing RADIANCE file"); } - free((char *)scanline); + free((void *)scanline); } -picreadline3(y, l3) /* read in 3-byte scanline */ -int y; -register rgbpixel *l3; +extern void +picreadline3( /* read in 3-byte scanline */ + int y, + register rgbpixel *l3 +) { - register BYTE *l4; - register int shift, c; - int i; + register int i; - if (inpic->nexty != y) { /* find scanline */ + if (inpic->nexty != y) { /* find scanline */ if (inpic->bytes_line == 0) { if (inpic->pos.y[y] == 0) { while (inpic->nexty < y) { - if (freadcolrs(inline, xmax, inpic->fp) < 0) + if (freadcolrs(inl, xmax, inpic->fp) < 0) quiterr("read error in picreadline3"); inpic->pos.y[++inpic->nexty] = ftell(inpic->fp); } @@ -289,39 +298,24 @@ register rgbpixel *l3; quiterr("seek error in picreadline3"); } else if (inpic->bytes_line == 0 && inpic->pos.y[inpic->nexty] == 0) inpic->pos.y[inpic->nexty] = ftell(inpic->fp); - if (freadcolrs(inline, xmax, inpic->fp) < 0) /* read scanline */ + if (freadcolrs(inl, xmax, inpic->fp) < 0) /* read scanline */ quiterr("read error in picreadline3"); inpic->nexty = y+1; /* convert scanline */ - for (l4=inline[0], i=xmax; i--; l4+=4, l3++) { - shift = l4[EXP] - COLXS; - if (shift >= 8) { - l3->r = l3->g = l3->b = 255; - } else if (shift <= -8) { - l3->r = l3->g = l3->b = 0; - } else if (shift > 0) { - c = l4[RED] << shift; - l3->r = c > 255 ? 255 : c; - c = l4[GRN] << shift; - l3->g = c > 255 ? 255 : c; - c = l4[BLU] << shift; - l3->b = c > 255 ? 255 : c; - } else if (shift < 0) { - l3->r = l4[RED] >> -shift; - l3->g = l4[GRN] >> -shift; - l3->b = l4[BLU] >> -shift; - } else { - l3->r = l4[RED]; - l3->g = l4[GRN]; - l3->b = l4[BLU]; - } + normcolrs(inl, xmax, bradj); + for (i = 0; i < xmax; i++) { + l3[i].r = inl[i][RED]; + l3[i].g = inl[i][GRN]; + l3[i].b = inl[i][BLU]; } } -picwriteline(y, l) /* write out scanline */ -int y; -register pixel *l; +extern void +picwriteline( /* write out scanline */ + int y, + register pixel *l +) { if (outpic->nexty != y) { /* seek to scanline */ if (outpic->bytes_line == 0) { @@ -333,7 +327,7 @@ register pixel *l; quiterr("seek error in picwriteline"); } /* write scanline */ - if (fwrite(l, sizeof(pixel), xmax, outpic->fp) != xmax) + if (fwrite((char *)l, sizeof(pixel), xmax, outpic->fp) != xmax) quiterr("write error in picwriteline"); if (xmax&1) /* on 16-bit boundary */ putc(l[xmax-1], outpic->fp); @@ -343,8 +337,10 @@ register pixel *l; } -picwritecm(cm) /* write out color map */ -colormap cm; +extern void +picwritecm( /* write out color map */ + colormap cm +) { register int i, j; @@ -362,13 +358,15 @@ colormap cm; } -picreadcm(map) /* do gamma correction if requested */ -colormap map; +extern void +picreadcm( /* do gamma correction if requested */ + colormap map +) { register int i, val; for (i = 0; i < 256; i++) { - val = pow(i/256.0, 1.0/gamma) * 256.0; + val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; map[0][i] = map[1][i] = map[2][i] = val; } }