--- ray/src/px/ra_tiff.c 1991/08/15 14:04:21 1.3 +++ ray/src/px/ra_tiff.c 1995/10/30 10:56:57 2.8 @@ -10,16 +10,25 @@ static char SCCSid[] = "$SunId$ LBL"; #include +#include + #include "tiffio.h" #include "color.h" -extern double atof(); +#include "resolu.h" + +#define GAMCOR 2.2 /* default gamma */ + extern char *malloc(), *realloc(); -double gamma = 2.2; /* gamma correction */ +int lzcomp = 0; /* use Lempel-Ziv compression? */ +int greyscale = 0; /* produce greyscale image? */ + +double gamcor = GAMCOR; /* gamma correction */ + int bradj = 0; /* brightness adjustment */ char *progname; @@ -38,8 +47,14 @@ char *argv[]; if (argv[i][0] == '-') switch (argv[i][1]) { case 'g': - gamma = atof(argv[++i]); + gamcor = atof(argv[++i]); break; + case 'z': + lzcomp = !lzcomp; + break; + case 'b': + greyscale = !greyscale; + break; case 'e': if (argv[i+1][0] != '+' && argv[i+1][0] != '-') goto userr; @@ -56,7 +71,7 @@ char *argv[]; else break; doneopts: - setcolrgam(gamma); + setcolrgam(gamcor); if (reverse) if (i != argc-2 && i != argc-1) @@ -71,7 +86,8 @@ doneopts: exit(0); userr: - fprintf(stderr, "Usage: %s [-r][-e +/-stops] input output\n", + fprintf(stderr, + "Usage: %s [-r][-b][-z][-e +/-stops][-g gamma] input output\n", progname); exit(1); } @@ -93,7 +109,7 @@ char *inpf, *outf; { unsigned long xmax, ymax; TIFF *tif; - unsigned short pconfig; + unsigned short pconfig, nsamps; unsigned short hi; register BYTE *scanin; register COLR *scanout; @@ -102,14 +118,18 @@ char *inpf, *outf; /* open/check TIFF file */ if ((tif = TIFFOpen(inpf, "r")) == NULL) quiterr("cannot open TIFF input"); - if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &hi) || hi != 3) + if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &nsamps) || + (nsamps != 1 && nsamps != 3)) quiterr("unsupported samples per pixel"); if (!TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &hi) || hi != 8) quiterr("unsupported bits per sample"); - if (TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &hi) && hi != 2) + if (TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &hi) && + hi != (nsamps==1 ? PHOTOMETRIC_MINISBLACK : + PHOTOMETRIC_RGB)) quiterr("unsupported photometric interpretation"); if (!TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &pconfig) || - (pconfig != 1 && pconfig != 2)) + (pconfig != PLANARCONFIG_CONTIG && + pconfig != PLANARCONFIG_SEPARATE)) quiterr("unsupported planar configuration"); if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &xmax) || !TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &ymax)) @@ -123,23 +143,34 @@ char *inpf, *outf; if (outf != NULL && strcmp(outf, "-") && freopen(outf, "w", stdout) == NULL) quiterr("cannot open Radiance output file"); + newheader("RADIANCE", stdout); fputs(progname, stdout); if (bradj) printf(" -e %+d", bradj); + if (gamcor != GAMCOR) + printf(" -g %f", gamcor); fputs(" -r\n", stdout); fputformat(COLRFMT, stdout); putchar('\n'); - fputresolu(YDECR|YMAJOR, xmax, ymax, stdout); + fprtresolu((int)xmax, (int)ymax, stdout); /* convert image */ + if (nsamps == 1) + pconfig = 1; for (y = 0; y < ymax; y++) { if (pconfig == 1) { if (TIFFReadScanline(tif, scanin, y, 0) < 0) goto readerr; - for (x = 0; x < xmax; x++) { - scanout[x][RED] = scanin[3*x]; - scanout[x][GRN] = scanin[3*x+1]; - scanout[x][BLU] = scanin[3*x+2]; - } + if (nsamps == 1) + for (x = 0; x < xmax; x++) + scanout[x][RED] = + scanout[x][GRN] = + scanout[x][BLU] = scanin[x]; + else + for (x = 0; x < xmax; x++) { + scanout[x][RED] = scanin[3*x]; + scanout[x][GRN] = scanin[3*x+1]; + scanout[x][BLU] = scanin[3*x+2]; + } } else { if (TIFFReadScanline(tif, scanin, y, 0) < 0) goto readerr; @@ -183,17 +214,22 @@ char *inpf, *outf; if (strcmp(inpf, "-") && freopen(inpf, "r", stdin) == NULL) quiterr("cannot open Radiance input file"); if (checkheader(stdin, COLRFMT, NULL) < 0 || - fgetresolu(&xmax, &ymax, stdin) != (YDECR|YMAJOR)) + fgetresolu(&xmax, &ymax, stdin) < 0) quiterr("bad Radiance picture"); /* open TIFF file */ if ((tif = TIFFOpen(outf, "w")) == NULL) quiterr("cannot open TIFF output"); TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (unsigned long)xmax); TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (unsigned long)ymax); - TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3); + TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, greyscale ? 1 : 3); TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); - TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, 2); - TIFFSetField(tif, TIFFTAG_PLANARCONFIG, 1); + TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, + greyscale ? PHOTOMETRIC_MINISBLACK : + PHOTOMETRIC_RGB); + TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); + if (lzcomp) + TIFFSetField(tif, TIFFTAG_COMPRESSION, + (unsigned short)COMPRESSION_LZW); /* allocate scanlines */ scanin = (COLR *)malloc(xmax*sizeof(COLR)); scanout = (BYTE *)malloc(TIFFScanlineSize(tif)); @@ -205,11 +241,19 @@ char *inpf, *outf; quiterr("error reading Radiance picture"); if (bradj) shiftcolrs(scanin, xmax, bradj); - colrs_gambs(scanin, xmax); - for (x = 0; x < xmax; x++) { - scanout[3*x] = scanin[x][RED]; - scanout[3*x+1] = scanin[x][GRN]; - scanout[3*x+2] = scanin[x][BLU]; + if (greyscale) { + for (x = 0; x < xmax; x++) + scanin[x][GRN] = normbright(scanin[x]); + colrs_gambs(scanin, xmax); + for (x = 0; x < xmax; x++) + scanout[x] = scanin[x][GRN]; + } else { + colrs_gambs(scanin, xmax); + for (x = 0; x < xmax; x++) { + scanout[3*x] = scanin[x][RED]; + scanout[3*x+1] = scanin[x][GRN]; + scanout[3*x+2] = scanin[x][BLU]; + } } if (TIFFWriteScanline(tif, scanout, y, 0) < 0) quiterr("error writing TIFF output");