--- ray/src/px/ra_t8.c 1989/09/12 13:04:38 1.2 +++ ray/src/px/ra_t8.c 1992/10/09 15:24:21 2.3 @@ -1,4 +1,4 @@ -/* Copyright (c) 1986 Regents of the University of California */ +/* Copyright (c) 1992 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -15,10 +15,22 @@ static char SCCSid[] = "$SunId$ LBL"; #include "color.h" +#include "resolu.h" + #include "pic.h" #include "targa.h" +#ifdef MSDOS +#include +#endif + +#include + +#ifndef BSD +#define bcopy(s,d,n) (void)memcpy(d,s,n) +extern char *memcpy(); +#endif /* descriptor for a picture file or frame buffer */ typedef struct { char *name; /* file name */ @@ -31,13 +43,13 @@ typedef struct { } pos; /* position(s) */ } pic; -#define goodpic(h) (my_imType(h) && my_mapType(h)) -#define my_imType(h) (((h)->dataType==IM_CMAP || (h)->dataType==IM_CCMAP) \ +#define goodpic(h) (my_imType(h) && my_mapType(h)) +#define my_imType(h) (((h)->dataType==IM_CMAP || (h)->dataType==IM_CCMAP) \ && (h)->dataBits==8 && (h)->imType==0) -#define my_mapType(h) ((h)->mapType==CM_HASMAP && \ +#define my_mapType(h) ((h)->mapType==CM_HASMAP && \ ((h)->CMapBits==24 || (h)->CMapBits==32)) -#define taralloc(h) (pixel *)emalloc((h)->x*(h)->y*sizeof(pixel)) +#define taralloc(h) (pixel *)emalloc((h)->x*(h)->y*sizeof(pixel)) extern pic *openinput(); @@ -45,9 +57,9 @@ extern char *ecalloc(), *emalloc(); extern long ftell(); -extern double atof(), pow(); +double gamma = 2.2; /* gamma correction */ -double gamma = 2.0; /* gamma correction */ +int bradj = 0; /* brightness adjustment */ pic *inpic; @@ -55,7 +67,7 @@ char *progname; char errmsg[128]; -COLR *inline; +COLR *inl; pixel *tarData; @@ -67,13 +79,18 @@ int argc; char *argv[]; { colormap rasmap; - struct hdStruct head; + struct hdStruct head; int dither = 1; int reverse = 0; int ncolors = 256; int greyscale = 0; int i; - +#ifdef MSDOS + extern int _fmode; + _fmode = O_BINARY; + setmode(fileno(stdin), O_BINARY); + setmode(fileno(stdout), O_BINARY); +#endif progname = argv[0]; for (i = 1; i < argc; i++) @@ -91,6 +108,11 @@ char *argv[]; case 'b': greyscale = 1; break; + case 'e': + if (argv[i+1][0] != '+' && argv[i+1][0] != '-') + goto userr; + bradj = atoi(argv[++i]); + break; case 'c': ncolors = atoi(argv[++i]); break; @@ -120,13 +142,14 @@ char *argv[]; quiterr(errmsg); } /* put header */ - printargs(argc, argv, stdout); + printargs(i, argv, stdout); + fputformat(COLRFMT, stdout); putchar('\n'); - fputresolu(YMAJOR|YDECR, xmax, ymax, stdout); + fprtresolu(xmax, ymax, stdout); /* convert file */ tg2ra(&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]); @@ -149,9 +172,9 @@ 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); } @@ -205,7 +228,7 @@ int code; getthead(hp, ip, fp) /* read header from input */ -struct hdStruct *hp; +struct hdStruct *hp; char *ip; register FILE *fp; { @@ -227,7 +250,7 @@ register FILE *fp; if (ip != NULL) if (nidbytes) - fread(ip, nidbytes, 1, fp); + fread((char *)ip, nidbytes, 1, fp); else *ip = '\0'; else if (nidbytes) @@ -238,7 +261,7 @@ register FILE *fp; putthead(hp, ip, fp) /* write header to output */ -struct hdStruct *hp; +struct hdStruct *hp; char *ip; register FILE *fp; { @@ -278,10 +301,10 @@ register struct hdStruct *h; p->fp = stdin; else if ((p->fp = fopen(fname, "r")) == NULL) return(NULL); - /* discard header */ - getheader(p->fp, NULL); - if (fgetresolu(&xmax, &ymax, p->fp) != (YMAJOR|YDECR)) - quiterr("bad picture size"); + /* get header info. */ + 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)); @@ -300,7 +323,7 @@ register struct hdStruct *h; h->dataBits = 8; h->imType = 0; /* allocate scanline */ - inline = (COLR *)emalloc(xmax*sizeof(COLR)); + inl = (COLR *)emalloc(xmax*sizeof(COLR)); /* allocate targa data */ tarData = taralloc(h); @@ -309,7 +332,7 @@ register struct hdStruct *h; tg2ra(hp) /* targa file to RADIANCE file */ -struct hdStruct *hp; +struct hdStruct *hp; { union { BYTE c3[256][3]; @@ -320,8 +343,10 @@ struct hdStruct *hp; register int i, j; /* get color table */ - if ((hp->CMapBits==24 ? fread(map.c3, sizeof(map.c3), 1, stdin) : - fread(map.c4, sizeof(map.c4), 1, stdin)) != 1) + if ((hp->CMapBits==24 ? fread((char *)(map.c3+hp->mapOrig), + 3*hp->mapLength,1,stdin) : + fread((char *)(map.c4+hp->mapOrig), + 4*hp->mapLength,1,stdin)) != 1) quiterr("error reading color table"); /* convert table */ for (i = hp->mapOrig; i < hp->mapOrig+hp->mapLength; i++) @@ -335,7 +360,8 @@ struct hdStruct *hp; pow((map.c4[i][3]+.5)/256.,gamma), pow((map.c4[i][2]+.5)/256.,gamma), pow((map.c4[i][1]+.5)/256.,gamma)); - + if (bradj) + shiftcolrs(ctab, 256, bradj); /* allocate targa data */ tarData = taralloc(hp); /* get data */ @@ -358,15 +384,13 @@ picreadline3(y, l3) /* 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); } @@ -376,32 +400,15 @@ 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]; } } @@ -410,17 +417,17 @@ picwriteline(y, l) /* save output scanline */ int y; pixel *l; { - bcopy(l, &tarData[(ymax-1-y)*xmax], xmax*sizeof(pixel)); + bcopy((char *)l, (char *)&tarData[(ymax-1-y)*xmax], xmax*sizeof(pixel)); } writetarga(h, d, fp) /* write out targa data */ -struct hdStruct *h; +struct hdStruct *h; pixel *d; FILE *fp; { if (h->dataType == IM_CMAP) { /* uncompressed */ - if (fwrite(d, h->x*sizeof(pixel), h->y, fp) != h->y) + if (fwrite((char *)d,h->x*sizeof(pixel),h->y,fp) != h->y) quiterr("error writing targa file"); return; } @@ -429,7 +436,7 @@ FILE *fp; readtarga(h, data, fp) /* read in targa data */ -struct hdStruct *h; +struct hdStruct *h; pixel *data; FILE *fp; { @@ -437,7 +444,7 @@ FILE *fp; register pixel *dp; if (h->dataType == IM_CMAP) { /* uncompressed */ - if (fread(data, h->x*sizeof(pixel), h->y, fp) != h->y) + if (fread((char *)data,h->x*sizeof(pixel),h->y,fp) != h->y) goto readerr; return; } @@ -480,7 +487,7 @@ 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/gamma) * 256.0; map[0][i] = map[1][i] = map[2][i] = val; } }