--- ray/src/px/ra_tiff.c 1991/08/15 13:34:58 1.1 +++ ray/src/px/ra_tiff.c 1991/12/19 14:52:25 2.2 @@ -14,10 +14,15 @@ static char SCCSid[] = "$SunId$ LBL"; #include "color.h" -extern double atof(); +#include "resolu.h" + extern char *malloc(), *realloc(); +int lzcomp = 0; /* use Lempel-Ziv compression? */ + +int greyscale = 0; /* produce greyscale image? */ + double gamma = 2.2; /* gamma correction */ int bradj = 0; /* brightness adjustment */ @@ -37,11 +42,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]); break; - */ + case 'z': + lzcomp = !lzcomp; + break; + case 'b': + greyscale = !greyscale; + break; case 'e': if (argv[i+1][0] != '+' && argv[i+1][0] != '-') goto userr; @@ -73,7 +82,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,9 +105,8 @@ char *inpf, *outf; { unsigned long xmax, ymax; TIFF *tif; - unsigned short pconfig; + unsigned short pconfig, nsamps; unsigned short hi; - char *cp; register BYTE *scanin; register COLR *scanout; register int x; @@ -105,11 +114,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)) @@ -126,32 +137,31 @@ char *inpf, *outf; if (outf != NULL && strcmp(outf, "-") && freopen(outf, "w", stdout) == NULL) quiterr("cannot open Radiance output file"); - if (TIFFGetField(tif, TIFFTAG_IMAGEDESCRIPTION, &cp)) { - while (*cp && *cp == '\n') - cp++; - for (x = 0; cp[x]; x++) - if (cp[x] != '\n' || cp[x+1] != '\n') - putchar(cp[x]); - if (x && cp[x-1] != '\n') - putchar('\n'); - } 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(xmax, 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; @@ -182,42 +192,9 @@ readerr: } -struct hdinfo { - char *buf; /* header buffer */ - char *pos; /* buffer position */ - char fmt[64]; /* format type */ -}; - - -headline(s, hd) /* add header line to buffer */ -register char *s; -register struct hdinfo *hd; -{ - register int i; - - if (isformat(s)) { - formatval(hd->fmt, s); - return; - } - if (hd->buf == NULL) - hd->pos = hd->buf = malloc(strlen(s)+1); - else { - i = hd->pos - hd->buf; - hd->buf = realloc(hd->buf, i+strlen(s)+1); - hd->pos = hd->buf + i; - } - if (hd->buf == NULL) - quiterr("out of memory in headline"); - while (*hd->pos++ = *s++) - ; -} - - ra2tiff(inpf, outf) /* convert Radiance file to 24-bit TIFF */ char *inpf, *outf; { - char buf[64]; - struct hdinfo hd; TIFF *tif; int xmax, ymax; BYTE *scanout; @@ -227,27 +204,20 @@ char *inpf, *outf; /* open Radiance file */ if (strcmp(inpf, "-") && freopen(inpf, "r", stdin) == NULL) quiterr("cannot open Radiance input file"); - hd.buf = NULL; hd.fmt[0] = '\0'; - getheader(stdin, headline, &hd); - if (bradj) - sprintf(buf, "%s -e %+d\n", progname, bradj); - else - sprintf(buf, "%s\n", progname); - headline(buf, &hd); - if (hd.fmt[0] && strcmp(hd.fmt, COLRFMT)) - quiterr("input not a Radiance picture"); - if (fgetresolu(&xmax, &ymax, stdin) != (YDECR|YMAJOR)) + if (checkheader(stdin, COLRFMT, NULL) < 0 || + 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); - TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, hd.buf); + if (lzcomp) + TIFFSetField(tif, TIFFTAG_COMPRESSION, (unsigned short)5); /* allocate scanlines */ scanin = (COLR *)malloc(xmax*sizeof(COLR)); scanout = (BYTE *)malloc(TIFFScanlineSize(tif)); @@ -259,11 +229,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"); @@ -272,5 +250,4 @@ char *inpf, *outf; free((char *)scanin); free((char *)scanout); TIFFClose(tif); - free(hd.buf); }