--- ray/src/px/ra_t16.c 1989/05/31 17:27:18 1.2 +++ ray/src/px/ra_t16.c 1989/10/20 16:42:28 1.5 @@ -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) \ @@ -93,12 +95,12 @@ char *argv[]; /* put header */ printargs(argc, 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; @@ -276,13 +278,16 @@ 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; /* 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)); tarData = taralloc(hp); @@ -294,29 +299,20 @@ struct hdStruct *hp; 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(inline[i],RED))] + +(random()&7)) & 0xf8)<<7; + *dp |= ((gmap[map(colval(inline[i],GRN))] + +(random()&7)) & 0xf8)<<2; + *dp++ |= (gmap[map(colval(inline[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(inline[i],BLU))]; + *dp++ = gmap[map(colval(inline[i],GRN))]; + *dp++ = gmap[map(colval(inline[i],RED))]; } } } @@ -325,6 +321,7 @@ struct hdStruct *hp; free((char *)inline); free((char *)tarData); +#undef map }