--- ray/src/px/ra_tiff.c 1991/08/15 14:48:55 1.4 +++ ray/src/px/ra_tiff.c 1994/11/11 11:26:20 2.7 @@ -10,18 +10,23 @@ static char SCCSid[] = "$SunId$ LBL"; #include +#include + #include "tiffio.h" #include "color.h" -extern double atof(); +#include "resolu.h" + extern char *malloc(), *realloc(); int lzcomp = 0; /* use Lempel-Ziv compression? */ -double gamma = 2.2; /* gamma correction */ +int greyscale = 0; /* produce greyscale image? */ +double gamcor = 2.2; /* gamma correction */ + int bradj = 0; /* brightness adjustment */ char *progname; @@ -40,11 +45,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; @@ -61,7 +69,7 @@ char *argv[]; else break; doneopts: - setcolrgam(gamma); + setcolrgam(gamcor); if (reverse) if (i != argc-2 && i != argc-1) @@ -77,7 +85,7 @@ doneopts: exit(0); userr: fprintf(stderr, - "Usage: %s [-r][-z][-e +/-stops][-g gamma] input output\n", + "Usage: %s [-r][-b][-z][-e +/-stops][-g gamma] input output\n", progname); exit(1); } @@ -99,7 +107,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; @@ -108,14 +116,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)) @@ -129,23 +141,32 @@ 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); 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; @@ -189,19 +210,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)5); + TIFFSetField(tif, TIFFTAG_COMPRESSION, + (unsigned short)COMPRESSION_LZW); /* allocate scanlines */ scanin = (COLR *)malloc(xmax*sizeof(COLR)); scanout = (BYTE *)malloc(TIFFScanlineSize(tif)); @@ -213,11 +237,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");