--- ray/src/px/ra_t16.c 1989/05/31 17:27:18 1.2 +++ ray/src/px/ra_t16.c 1991/05/17 08:41:52 1.12 @@ -1,4 +1,4 @@ -/* Copyright (c) 1986 Regents of the University of California */ +/* Copyright (c) 1991 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -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) \ @@ -83,6 +85,8 @@ char *argv[]; sprintf(msg, "can't open output \"%s\"", argv[i+1]); quiterr(msg); } + /* set gamma */ + setcolrgam(gamma); /* convert */ if (reverse) { /* get header */ @@ -91,14 +95,15 @@ char *argv[]; if (!goodpic(&head)) quiterr("incompatible format"); /* put header */ - printargs(argc, argv, stdout); + printargs(i, argv, stdout); + fputformat(COLRFMT, 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 (checkheader(stdin, COLRFMT, NULL) < 0 || + fgetresolu(&head.x, &head.y, stdin) != (YMAJOR|YDECR)) quiterr("bad picture file"); /* assign header */ head.textSize = 0; @@ -187,7 +192,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) @@ -228,13 +233,9 @@ register FILE *fp; tg2ra(hp) /* targa file to RADIANCE file */ struct hdStruct *hp; { - float gmap[256]; - COLOR *scanline; + COLR *scanline; unsigned char *tarData; register int i, j; - /* set up gamma correction */ - for (i = 0; i < 256; i++) - gmap[i] = pow((i+.5)/256., gamma); /* skip color table */ if (hp->mapType == CM_HASMAP) fseek(stdin, (long)hp->mapLength*hp->CMapBits/8, 1); @@ -243,29 +244,30 @@ struct hdStruct *hp; /* get data */ readtarga(hp, tarData, stdin); /* allocate input scanline */ - scanline = (COLOR *)emalloc(hp->x*sizeof(COLOR)); + scanline = (COLR *)emalloc(hp->x*sizeof(COLR)); /* convert file */ for (i = hp->y-1; i >= 0; i--) { if (hp->dataBits == 16) { register unsigned short *dp; dp = (unsigned short *)tarData + i*hp->x; for (j = 0; j < hp->x; j++) { - setcolor(scanline[j], gmap[*dp>>7 & 0xf8], - gmap[*dp>>2 & 0xf8], - gmap[*dp<<3 & 0xf8]); + scanline[j][RED] = *dp>>7 & 0xf8; + scanline[j][GRN] = *dp>>2 & 0xf8; + scanline[j][BLU] = *dp<<3 & 0xf8; dp++; } } else { /* hp->dataBits == 24 */ register unsigned char *dp; dp = (unsigned char *)tarData + i*3*hp->x; for (j = 0; j < hp->x; j++) { - setcolor(scanline[j], gmap[dp[2]], - gmap[dp[1]], - gmap[dp[0]]); + scanline[j][RED] = dp[2]; + scanline[j][GRN] = dp[1]; + scanline[j][BLU] = dp[0]; dp += 3; } } - if (fwritescan(scanline, hp->x, stdout) < 0) + gambs_colrs(scanline, hp->x); + if (fwritecolrs(scanline, hp->x, stdout) < 0) quiterr("error writing RADIANCE file"); } free((char *)scanline); @@ -276,54 +278,46 @@ struct hdStruct *hp; ra2tg(hp) /* convert radiance to targa file */ struct hdStruct *hp; { - 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); + COLR *inl; /* allocate space for data */ - inline = (COLOR *)emalloc(hp->x*sizeof(COLOR)); + inl = (COLR *)emalloc(hp->x*sizeof(COLR)); tarData = taralloc(hp); /* convert file */ for (j = hp->y-1; j >= 0; j--) { - if (freadscan(inline, hp->x, stdin) < 0) + if (freadcolrs(inl, hp->x, stdin) < 0) quiterr("error reading RADIANCE file"); + colrs_gambs(inl, hp->x); if (hp->dataBits == 16) { register unsigned short *dp; + register int v; 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; + v = inl[i][RED] + (random()&7); + if (v > 255) v = 255; + *dp = (v&0xf8)<<7; + v = inl[i][GRN] + (random()&7); + if (v > 255) v = 255; + *dp |= (v&0xf8)<<2; + v = inl[i][BLU] + (random()&7); + if (v > 255) v = 255; + *dp++ |= v>>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++ = inl[i][BLU]; + *dp++ = inl[i][GRN]; + *dp++ = inl[i][RED]; } } } /* write out targa data */ writetarga(hp, tarData, stdout); - free((char *)inline); + free((char *)inl); free((char *)tarData); } @@ -334,7 +328,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 +364,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; }