--- ray/src/px/ra_tiff.c 1991/08/15 13:54:08 1.2 +++ ray/src/px/ra_tiff.c 1993/11/18 09:55:26 2.5 @@ -10,16 +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(); -double gamma = 2.2; /* gamma correction */ +int lzcomp = 0; /* use Lempel-Ziv compression? */ +int greyscale = 0; /* produce greyscale image? */ + +double gamcor = 2.2; /* gamma correction */ + int bradj = 0; /* brightness adjustment */ char *progname; @@ -37,11 +44,15 @@ char *argv[]; for (i = 1; i < argc; i++) if (argv[i][0] == '-') switch (argv[i][1]) { - /* not allowed to reset gamma... 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; @@ -58,7 +69,7 @@ char *argv[]; else break; doneopts: - setcolrgam(gamma); + setcolrgam(gamcor); if (reverse) if (i != argc-2 && i != argc-1) @@ -73,7 +84,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); } @@ -95,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; @@ -104,11 +116,13 @@ 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 ? 1 : 2)) quiterr("unsupported photometric interpretation"); if (!TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &pconfig) || (pconfig != 1 && pconfig != 2)) @@ -131,17 +145,25 @@ char *inpf, *outf; 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; @@ -185,17 +207,19 @@ 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_PHOTOMETRIC, greyscale ? 1 : 2); TIFFSetField(tif, TIFFTAG_PLANARCONFIG, 1); + if (lzcomp) + TIFFSetField(tif, TIFFTAG_COMPRESSION, (unsigned short)5); /* allocate scanlines */ scanin = (COLR *)malloc(xmax*sizeof(COLR)); scanout = (BYTE *)malloc(TIFFScanlineSize(tif)); @@ -207,11 +231,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");