ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_tiff.c
(Generate patch)

Comparing ray/src/px/ra_tiff.c (file contents):
Revision 1.1 by greg, Thu Aug 15 13:34:58 1991 UTC vs.
Revision 2.2 by greg, Thu Dec 19 14:52:25 1991 UTC

# Line 14 | Line 14 | static char SCCSid[] = "$SunId$ LBL";
14  
15   #include  "color.h"
16  
17 < extern double  atof();
17 > #include  "resolu.h"
18  
19 +
20   extern char  *malloc(), *realloc();
21  
22 + int  lzcomp = 0;                        /* use Lempel-Ziv compression? */
23 +
24 + int  greyscale = 0;                     /* produce greyscale image? */
25 +
26   double  gamma = 2.2;                    /* gamma correction */
27  
28   int  bradj = 0;                         /* brightness adjustment */
# Line 37 | Line 42 | char  *argv[];
42          for (i = 1; i < argc; i++)
43                  if (argv[i][0] == '-')
44                          switch (argv[i][1]) {
40                        /* not allowed to reset gamma...
45                          case 'g':
46                                  gamma = atof(argv[++i]);
47                                  break;
48 <                        */
48 >                        case 'z':
49 >                                lzcomp = !lzcomp;
50 >                                break;
51 >                        case 'b':
52 >                                greyscale = !greyscale;
53 >                                break;
54                          case 'e':
55                                  if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
56                                          goto userr;
# Line 73 | Line 82 | doneopts:
82  
83          exit(0);
84   userr:
85 <        fprintf(stderr, "Usage: %s [-r][-e +/-stops] input output\n",
85 >        fprintf(stderr,
86 >        "Usage: %s [-r][-b][-z][-e +/-stops][-g gamma] input output\n",
87                          progname);
88          exit(1);
89   }
# Line 95 | Line 105 | char   *inpf, *outf;
105   {
106          unsigned long   xmax, ymax;
107          TIFF    *tif;
108 <        unsigned short  pconfig;
108 >        unsigned short  pconfig, nsamps;
109          unsigned short  hi;
100        char    *cp;
110          register BYTE   *scanin;
111          register COLR   *scanout;
112          register int    x;
# Line 105 | Line 114 | char   *inpf, *outf;
114                                          /* open/check  TIFF file */
115          if ((tif = TIFFOpen(inpf, "r")) == NULL)
116                  quiterr("cannot open TIFF input");
117 <        if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &hi) || hi != 3)
117 >        if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &nsamps) ||
118 >                        (nsamps != 1 && nsamps != 3))
119                  quiterr("unsupported samples per pixel");
120          if (!TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &hi) || hi != 8)
121                  quiterr("unsupported bits per sample");
122 <        if (TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &hi) && hi != 2)
122 >        if (TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &hi) &&
123 >                        hi != (nsamps==1 ? 1 : 2))
124                  quiterr("unsupported photometric interpretation");
125          if (!TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &pconfig) ||
126                          (pconfig != 1 && pconfig != 2))
# Line 126 | Line 137 | char   *inpf, *outf;
137          if (outf != NULL && strcmp(outf, "-") &&
138                          freopen(outf, "w", stdout) == NULL)
139                  quiterr("cannot open Radiance output file");
129        if (TIFFGetField(tif, TIFFTAG_IMAGEDESCRIPTION, &cp)) {
130                while (*cp && *cp == '\n')
131                        cp++;
132                for (x = 0; cp[x]; x++)
133                        if (cp[x] != '\n' || cp[x+1] != '\n')
134                                putchar(cp[x]);
135                if (x && cp[x-1] != '\n')
136                        putchar('\n');
137        }
140          fputs(progname, stdout);
141          if (bradj)
142                  printf(" -e %+d", bradj);
143          fputs(" -r\n", stdout);
144          fputformat(COLRFMT, stdout);
145          putchar('\n');
146 <        fputresolu(YDECR|YMAJOR, xmax, ymax, stdout);
146 >        fprtresolu(xmax, ymax, stdout);
147                                                  /* convert image */
148 +        if (nsamps == 1)
149 +                pconfig = 1;
150          for (y = 0; y < ymax; y++) {
151                  if (pconfig == 1) {
152                          if (TIFFReadScanline(tif, scanin, y, 0) < 0)
153                                  goto readerr;
154 <                        for (x = 0; x < xmax; x++) {
155 <                                scanout[x][RED] = scanin[3*x];
156 <                                scanout[x][GRN] = scanin[3*x+1];
157 <                                scanout[x][BLU] = scanin[3*x+2];
158 <                        }
154 >                        if (nsamps == 1)
155 >                                for (x = 0; x < xmax; x++)
156 >                                        scanout[x][RED] =
157 >                                        scanout[x][GRN] =
158 >                                        scanout[x][BLU] = scanin[x];
159 >                        else
160 >                                for (x = 0; x < xmax; x++) {
161 >                                        scanout[x][RED] = scanin[3*x];
162 >                                        scanout[x][GRN] = scanin[3*x+1];
163 >                                        scanout[x][BLU] = scanin[3*x+2];
164 >                                }
165                  } else {
166                          if (TIFFReadScanline(tif, scanin, y, 0) < 0)
167                                  goto readerr;
# Line 182 | Line 192 | readerr:
192   }
193  
194  
185 struct hdinfo {
186        char    *buf;           /* header buffer */
187        char    *pos;           /* buffer position */
188        char    fmt[64];        /* format type */
189 };
190
191
192 headline(s, hd)                 /* add header line to buffer */
193 register char   *s;
194 register struct hdinfo  *hd;
195 {
196        register int    i;
197
198        if (isformat(s)) {
199                formatval(hd->fmt, s);
200                return;
201        }
202        if (hd->buf == NULL)
203                hd->pos = hd->buf = malloc(strlen(s)+1);
204        else {
205                i = hd->pos - hd->buf;
206                hd->buf = realloc(hd->buf, i+strlen(s)+1);
207                hd->pos = hd->buf + i;
208        }
209        if (hd->buf == NULL)
210                quiterr("out of memory in headline");
211        while (*hd->pos++ = *s++)
212                ;
213 }
214
215
195   ra2tiff(inpf, outf)             /* convert Radiance file to 24-bit TIFF */
196   char    *inpf, *outf;
197   {
219        char    buf[64];
220        struct hdinfo   hd;
198          TIFF    *tif;
199          int     xmax, ymax;
200          BYTE    *scanout;
# Line 227 | Line 204 | char   *inpf, *outf;
204                                                  /* open Radiance file */
205          if (strcmp(inpf, "-") && freopen(inpf, "r", stdin) == NULL)
206                  quiterr("cannot open Radiance input file");
207 <        hd.buf = NULL; hd.fmt[0] = '\0';
208 <        getheader(stdin, headline, &hd);
232 <        if (bradj)
233 <                sprintf(buf, "%s -e %+d\n", progname, bradj);
234 <        else
235 <                sprintf(buf, "%s\n", progname);
236 <        headline(buf, &hd);
237 <        if (hd.fmt[0] && strcmp(hd.fmt, COLRFMT))
238 <                quiterr("input not a Radiance picture");
239 <        if (fgetresolu(&xmax, &ymax, stdin) != (YDECR|YMAJOR))
207 >        if (checkheader(stdin, COLRFMT, NULL) < 0 ||
208 >                        fgetresolu(&xmax, &ymax, stdin) < 0)
209                  quiterr("bad Radiance picture");
210                                                  /* open TIFF file */
211          if ((tif = TIFFOpen(outf, "w")) == NULL)
212                  quiterr("cannot open TIFF output");
213          TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (unsigned long)xmax);
214          TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (unsigned long)ymax);
215 <        TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
215 >        TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, greyscale ? 1 : 3);
216          TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
217 <        TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, 2);
217 >        TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, greyscale ? 1 : 2);
218          TIFFSetField(tif, TIFFTAG_PLANARCONFIG, 1);
219 <        TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, hd.buf);
219 >        if (lzcomp)
220 >                TIFFSetField(tif, TIFFTAG_COMPRESSION, (unsigned short)5);
221                                                  /* allocate scanlines */
222          scanin = (COLR *)malloc(xmax*sizeof(COLR));
223          scanout = (BYTE *)malloc(TIFFScanlineSize(tif));
# Line 259 | Line 229 | char   *inpf, *outf;
229                          quiterr("error reading Radiance picture");
230                  if (bradj)
231                          shiftcolrs(scanin, xmax, bradj);
232 <                colrs_gambs(scanin, xmax);
233 <                for (x = 0; x < xmax; x++) {
234 <                        scanout[3*x] = scanin[x][RED];
235 <                        scanout[3*x+1] = scanin[x][GRN];
236 <                        scanout[3*x+2] = scanin[x][BLU];
232 >                if (greyscale) {
233 >                        for (x = 0; x < xmax; x++)
234 >                                scanin[x][GRN] = normbright(scanin[x]);
235 >                        colrs_gambs(scanin, xmax);
236 >                        for (x = 0; x < xmax; x++)
237 >                                scanout[x] = scanin[x][GRN];
238 >                } else {
239 >                        colrs_gambs(scanin, xmax);
240 >                        for (x = 0; x < xmax; x++) {
241 >                                scanout[3*x] = scanin[x][RED];
242 >                                scanout[3*x+1] = scanin[x][GRN];
243 >                                scanout[3*x+2] = scanin[x][BLU];
244 >                        }
245                  }
246                  if (TIFFWriteScanline(tif, scanout, y, 0) < 0)
247                          quiterr("error writing TIFF output");
# Line 272 | Line 250 | char   *inpf, *outf;
250          free((char *)scanin);
251          free((char *)scanout);
252          TIFFClose(tif);
275        free(hd.buf);
253   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines