--- ray/src/px/ra_t16.c 1989/02/02 10:49:36 1.1 +++ ray/src/px/ra_t16.c 1989/09/12 13:04:36 1.4 @@ -93,12 +93,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; @@ -259,9 +259,9 @@ struct hdStruct *hp; register unsigned char *dp; dp = (unsigned char *)tarData + i*3*hp->x; for (j = 0; j < hp->x; j++) { - setcolor(scanline[j], gmap[dp[0]], + setcolor(scanline[j], gmap[dp[2]], gmap[dp[1]], - gmap[dp[2]]); + gmap[dp[0]]); dp += 3; } } @@ -276,13 +276,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 +297,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],RED); - 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],BLU); - 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 +319,7 @@ struct hdStruct *hp; free((char *)inline); free((char *)tarData); +#undef map } @@ -379,20 +374,20 @@ FILE *fp; goto readerr; cnt = (c & 0x7f) + 1; if (c & 0x80) { /* repeated pixel */ - r = getc(fp); g = getc(fp); - if ((b = getc(fp)) == EOF) + b = getc(fp); g = getc(fp); + if ((r = getc(fp)) == EOF) goto readerr; while (cnt--) { - *dp++ = r; - *dp++ = g; *dp++ = b; + *dp++ = g; + *dp++ = r; } } else /* non-repeating pixels */ while (cnt--) { *dp++ = getc(fp); *dp++ = getc(fp); - if ((b = getc(fp)) == EOF) + if ((r = getc(fp)) == EOF) goto readerr; - *dp++ = b; + *dp++ = r; } } return;