--- ray/src/px/ra_t16.c 1990/01/18 23:58:21 1.6 +++ ray/src/px/ra_t16.c 1991/04/18 14:35:42 1.11 @@ -85,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 */ @@ -93,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'); fputresolu(YMAJOR|YDECR, head.x, head.y, stdout); /* convert file */ tg2ra(&head); } else { - getheader(stdin, NULL); - if (fgetresolu(&head.x, &head.y, stdin) != (YMAJOR|YDECR)) + if (checkheader(stdin, COLRFMT, NULL) < 0 || + fgetresolu(&head.x, &head.y, stdin) != (YMAJOR|YDECR)) quiterr("bad picture file"); /* assign header */ head.textSize = 0; @@ -230,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); @@ -245,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); @@ -278,50 +278,40 @@ 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; unsigned char *tarData; - COLOR *inline; - /* set up gamma correction */ - for (i = 0; i < 1024; i++) { - j = 256.*pow((i+.5)/1024., 1./gamma); - gmap[i] = hp->dataBits == 16 && j > 248 ? 248 : j; - } + 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; dp = (unsigned short *)tarData + j*hp->x; for (i = 0; i < hp->x; i++) { - *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; + *dp = ((inl[i][RED]+(random()&7)) & 0xf8)<<7; + *dp |= ((inl[i][GRN]+(random()&7)) & 0xf8)<<2; + *dp++ |= (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++) { - *dp++ = gmap[map(colval(inline[i],BLU))]; - *dp++ = gmap[map(colval(inline[i],GRN))]; - *dp++ = gmap[map(colval(inline[i],RED))]; + *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); -#undef map }