--- ray/src/px/ra_t16.c 1990/02/09 13:59:21 1.7 +++ ray/src/px/ra_t16.c 1991/08/23 13:40:04 1.14 @@ -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"; @@ -34,8 +34,10 @@ extern char *ecalloc(), *emalloc(); extern double atof(), pow(); -double gamma = 2.0; /* gamma correction */ +double gamma = 2.2; /* gamma correction */ +int bradj = 0; /* brightness adjustment */ + char *progname; char msg[128]; @@ -67,6 +69,11 @@ char *argv[]; case '3': head.dataBits = 24; break; + case 'e': + if (argv[i+1][0] != '+' && argv[i+1][0] != '-') + goto userr; + bradj = atoi(argv[++i]); + break; default: goto userr; } @@ -85,6 +92,8 @@ char *argv[]; sprintf(msg, "can't open output \"%s\"", argv[i+1]); quiterr(msg); } + /* set gamma */ + setcolrgam(gamma); /* convert */ if (reverse) { /* get header */ @@ -94,13 +103,14 @@ char *argv[]; quiterr("incompatible format"); /* put header */ 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; @@ -116,7 +126,7 @@ char *argv[]; } exit(0); userr: - fprintf(stderr, "Usage: %s [-2|-3|-r][-g gamma] [input [output]]\n", + fprintf(stderr, "Usage: %s [-2|-3|-r][-g gamma][-e +/-stops] [input [output]]\n", progname); exit(1); } @@ -230,13 +240,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 +251,32 @@ 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 (bradj) + shiftcolrs(scanline, hp->x, bradj); + if (fwritecolrs(scanline, hp->x, stdout) < 0) quiterr("error writing RADIANCE file"); } free((char *)scanline); @@ -278,50 +287,49 @@ 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"); + if (bradj) + shiftcolrs(inl, hp->x, bradj); + 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++) { - *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; + 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++) { - *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 }