--- ray/src/px/ra_t16.c 1989/05/31 17:27:18 1.2 +++ ray/src/px/ra_t16.c 1990/03/12 15:14:42 1.8 @@ -15,6 +15,8 @@ static char SCCSid[] = "$SunId$ LBL"; #include "color.h" +#include "random.h" + #include "targa.h" #define goodpic(h) (((h)->dataType==IM_RGB || (h)->dataType==IM_CRGB) \ @@ -91,14 +93,14 @@ char *argv[]; if (!goodpic(&head)) quiterr("incompatible format"); /* put header */ - printargs(argc, argv, stdout); + printargs(i, argv, stdout); putchar('\n'); - printf("-Y %d +X %d\n", head.y, head.x); + fputresolu(YMAJOR|YDECR, head.x, head.y, stdout); /* convert file */ tg2ra(&head); } else { getheader(stdin, NULL); - if (scanf("-Y %d +X %d\n", &head.y, &head.x) != 2) + if (fgetresolu(&head.x, &head.y, stdin) != (YMAJOR|YDECR)) quiterr("bad picture file"); /* assign header */ head.textSize = 0; @@ -187,7 +189,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) @@ -276,55 +278,50 @@ struct hdStruct *hp; ra2tg(hp) /* convert radiance to targa file */ struct hdStruct *hp; { +#define map(v) (v >= 1.0 ? 1023 : (int)(v*1023.+.5)) unsigned char gmap[1024]; - register int i, j, c; + register int i, j; unsigned char *tarData; - COLOR *inline; + COLOR *inl; /* set up gamma correction */ - for (i = 0; i < 1024; i++) - gmap[i] = 256.*pow((i+.5)/1024., 1./gamma); + for (i = 0; i < 1024; i++) { + j = 256.*pow((i+.5)/1024., 1./gamma); + gmap[i] = hp->dataBits == 16 && j > 248 ? 248 : j; + } /* allocate space for data */ - inline = (COLOR *)emalloc(hp->x*sizeof(COLOR)); + inl = (COLOR *)emalloc(hp->x*sizeof(COLOR)); tarData = taralloc(hp); /* convert file */ for (j = hp->y-1; j >= 0; j--) { - if (freadscan(inline, hp->x, stdin) < 0) + if (freadscan(inl, hp->x, stdin) < 0) quiterr("error reading RADIANCE file"); if (hp->dataBits == 16) { register unsigned short *dp; dp = (unsigned short *)tarData + j*hp->x; for (i = 0; i < hp->x; i++) { - c = 1024.*colval(inline[i],RED); - if (c > 1023) c = 1023; - *dp = (gmap[c] & 0xf8)<<7; - c = 1024.*colval(inline[i],GRN); - if (c > 1023) c = 1023; - *dp |= (gmap[c] & 0xf8)<<2; - c = 1024.*colval(inline[i],BLU); - if (c > 1023) c = 1023; - *dp++ |= gmap[c]>>3; + *dp = ((gmap[map(colval(inl[i],RED))] + +(random()&7)) & 0xf8)<<7; + *dp |= ((gmap[map(colval(inl[i],GRN))] + +(random()&7)) & 0xf8)<<2; + *dp++ |= (gmap[map(colval(inl[i],BLU))] + +(random()&7))>>3; } } else { /* hp->dataBits == 24 */ register unsigned char *dp; dp = (unsigned char *)tarData + j*3*hp->x; for (i = 0; i < hp->x; i++) { - c = 1024.*colval(inline[i],BLU); - if (c > 1023) c = 1023; - *dp++ = gmap[c]; - c = 1024.*colval(inline[i],GRN); - if (c > 1023) c = 1023; - *dp++ = gmap[c]; - c = 1024.*colval(inline[i],RED); - if (c > 1023) c = 1023; - *dp++ = gmap[c]; + *dp++ = gmap[map(colval(inl[i],BLU))]; + *dp++ = gmap[map(colval(inl[i],GRN))]; + *dp++ = gmap[map(colval(inl[i],RED))]; } } } /* write out targa data */ writetarga(hp, tarData, stdout); - free((char *)inline); + free((char *)inl); free((char *)tarData); +#undef map } @@ -334,7 +331,7 @@ unsigned char *d; FILE *fp; { if (h->dataType == IM_RGB) { /* uncompressed */ - if (fwrite(d, 3*h->x, h->y, fp) != h->y) + if (fwrite((char *)d, 3*h->x, h->y, fp) != h->y) quiterr("error writing targa file"); return; } @@ -370,7 +367,7 @@ FILE *fp; int r, g, b; if (h->dataType == IM_RGB) { /* uncompressed */ - if (fread(data, 3*h->x, h->y, fp) != h->y) + if (fread((char *)data, 3*h->x, h->y, fp) != h->y) goto readerr; return; }