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.8 by greg, Mon Oct 30 10:56:57 1995 UTC

# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
10  
11   #include  <stdio.h>
12  
13 + #include  <math.h>
14 +
15   #include  "tiffio.h"
16  
17   #include  "color.h"
18  
19 < extern double  atof();
19 > #include  "resolu.h"
20  
21 +
22 + #define  GAMCOR         2.2             /* default gamma */
23 +
24   extern char  *malloc(), *realloc();
25  
26 < double  gamma = 2.2;                    /* gamma correction */
26 > int  lzcomp = 0;                        /* use Lempel-Ziv compression? */
27  
28 + int  greyscale = 0;                     /* produce greyscale image? */
29 +
30 + double  gamcor = GAMCOR;                /* gamma correction */
31 +
32   int  bradj = 0;                         /* brightness adjustment */
33  
34   char  *progname;
# Line 37 | Line 46 | char  *argv[];
46          for (i = 1; i < argc; i++)
47                  if (argv[i][0] == '-')
48                          switch (argv[i][1]) {
40                        /* not allowed to reset gamma...
49                          case 'g':
50 <                                gamma = atof(argv[++i]);
50 >                                gamcor = atof(argv[++i]);
51                                  break;
52 <                        */
52 >                        case 'z':
53 >                                lzcomp = !lzcomp;
54 >                                break;
55 >                        case 'b':
56 >                                greyscale = !greyscale;
57 >                                break;
58                          case 'e':
59                                  if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
60                                          goto userr;
# Line 58 | Line 71 | char  *argv[];
71                  else
72                          break;
73   doneopts:
74 <        setcolrgam(gamma);
74 >        setcolrgam(gamcor);
75  
76          if (reverse)
77                  if (i != argc-2 && i != argc-1)
# Line 73 | Line 86 | doneopts:
86  
87          exit(0);
88   userr:
89 <        fprintf(stderr, "Usage: %s [-r][-e +/-stops] input output\n",
89 >        fprintf(stderr,
90 >        "Usage: %s [-r][-b][-z][-e +/-stops][-g gamma] input output\n",
91                          progname);
92          exit(1);
93   }
# Line 95 | Line 109 | char   *inpf, *outf;
109   {
110          unsigned long   xmax, ymax;
111          TIFF    *tif;
112 <        unsigned short  pconfig;
112 >        unsigned short  pconfig, nsamps;
113          unsigned short  hi;
100        char    *cp;
114          register BYTE   *scanin;
115          register COLR   *scanout;
116          register int    x;
# Line 105 | Line 118 | char   *inpf, *outf;
118                                          /* open/check  TIFF file */
119          if ((tif = TIFFOpen(inpf, "r")) == NULL)
120                  quiterr("cannot open TIFF input");
121 <        if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &hi) || hi != 3)
121 >        if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &nsamps) ||
122 >                        (nsamps != 1 && nsamps != 3))
123                  quiterr("unsupported samples per pixel");
124          if (!TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &hi) || hi != 8)
125                  quiterr("unsupported bits per sample");
126 <        if (TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &hi) && hi != 2)
126 >        if (TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &hi) &&
127 >                        hi != (nsamps==1 ? PHOTOMETRIC_MINISBLACK :
128 >                                        PHOTOMETRIC_RGB))
129                  quiterr("unsupported photometric interpretation");
130          if (!TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &pconfig) ||
131 <                        (pconfig != 1 && pconfig != 2))
131 >                        (pconfig != PLANARCONFIG_CONTIG &&
132 >                                pconfig != PLANARCONFIG_SEPARATE))
133                  quiterr("unsupported planar configuration");
134          if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &xmax) ||
135                          !TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &ymax))
# Line 126 | Line 143 | char   *inpf, *outf;
143          if (outf != NULL && strcmp(outf, "-") &&
144                          freopen(outf, "w", stdout) == NULL)
145                  quiterr("cannot open Radiance output file");
146 <        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 <        }
146 >        newheader("RADIANCE", stdout);
147          fputs(progname, stdout);
148          if (bradj)
149                  printf(" -e %+d", bradj);
150 +        if (gamcor != GAMCOR)
151 +                printf(" -g %f", gamcor);
152          fputs(" -r\n", stdout);
153          fputformat(COLRFMT, stdout);
154          putchar('\n');
155 <        fputresolu(YDECR|YMAJOR, xmax, ymax, stdout);
155 >        fprtresolu((int)xmax, (int)ymax, stdout);
156                                                  /* convert image */
157 +        if (nsamps == 1)
158 +                pconfig = 1;
159          for (y = 0; y < ymax; y++) {
160                  if (pconfig == 1) {
161                          if (TIFFReadScanline(tif, scanin, y, 0) < 0)
162                                  goto readerr;
163 <                        for (x = 0; x < xmax; x++) {
164 <                                scanout[x][RED] = scanin[3*x];
165 <                                scanout[x][GRN] = scanin[3*x+1];
166 <                                scanout[x][BLU] = scanin[3*x+2];
167 <                        }
163 >                        if (nsamps == 1)
164 >                                for (x = 0; x < xmax; x++)
165 >                                        scanout[x][RED] =
166 >                                        scanout[x][GRN] =
167 >                                        scanout[x][BLU] = scanin[x];
168 >                        else
169 >                                for (x = 0; x < xmax; x++) {
170 >                                        scanout[x][RED] = scanin[3*x];
171 >                                        scanout[x][GRN] = scanin[3*x+1];
172 >                                        scanout[x][BLU] = scanin[3*x+2];
173 >                                }
174                  } else {
175                          if (TIFFReadScanline(tif, scanin, y, 0) < 0)
176                                  goto readerr;
# Line 182 | Line 201 | readerr:
201   }
202  
203  
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
204   ra2tiff(inpf, outf)             /* convert Radiance file to 24-bit TIFF */
205   char    *inpf, *outf;
206   {
219        char    buf[64];
220        struct hdinfo   hd;
207          TIFF    *tif;
208          int     xmax, ymax;
209          BYTE    *scanout;
# Line 227 | Line 213 | char   *inpf, *outf;
213                                                  /* open Radiance file */
214          if (strcmp(inpf, "-") && freopen(inpf, "r", stdin) == NULL)
215                  quiterr("cannot open Radiance input file");
216 <        hd.buf = NULL; hd.fmt[0] = '\0';
217 <        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))
216 >        if (checkheader(stdin, COLRFMT, NULL) < 0 ||
217 >                        fgetresolu(&xmax, &ymax, stdin) < 0)
218                  quiterr("bad Radiance picture");
219                                                  /* open TIFF file */
220          if ((tif = TIFFOpen(outf, "w")) == NULL)
221                  quiterr("cannot open TIFF output");
222          TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (unsigned long)xmax);
223          TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (unsigned long)ymax);
224 <        TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
224 >        TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, greyscale ? 1 : 3);
225          TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
226 <        TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, 2);
227 <        TIFFSetField(tif, TIFFTAG_PLANARCONFIG, 1);
228 <        TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, hd.buf);
226 >        TIFFSetField(tif, TIFFTAG_PHOTOMETRIC,
227 >                        greyscale ? PHOTOMETRIC_MINISBLACK :
228 >                                PHOTOMETRIC_RGB);
229 >        TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
230 >        if (lzcomp)
231 >                TIFFSetField(tif, TIFFTAG_COMPRESSION,
232 >                                (unsigned short)COMPRESSION_LZW);
233                                                  /* allocate scanlines */
234          scanin = (COLR *)malloc(xmax*sizeof(COLR));
235          scanout = (BYTE *)malloc(TIFFScanlineSize(tif));
# Line 259 | Line 241 | char   *inpf, *outf;
241                          quiterr("error reading Radiance picture");
242                  if (bradj)
243                          shiftcolrs(scanin, xmax, bradj);
244 <                colrs_gambs(scanin, xmax);
245 <                for (x = 0; x < xmax; x++) {
246 <                        scanout[3*x] = scanin[x][RED];
247 <                        scanout[3*x+1] = scanin[x][GRN];
248 <                        scanout[3*x+2] = scanin[x][BLU];
244 >                if (greyscale) {
245 >                        for (x = 0; x < xmax; x++)
246 >                                scanin[x][GRN] = normbright(scanin[x]);
247 >                        colrs_gambs(scanin, xmax);
248 >                        for (x = 0; x < xmax; x++)
249 >                                scanout[x] = scanin[x][GRN];
250 >                } else {
251 >                        colrs_gambs(scanin, xmax);
252 >                        for (x = 0; x < xmax; x++) {
253 >                                scanout[3*x] = scanin[x][RED];
254 >                                scanout[3*x+1] = scanin[x][GRN];
255 >                                scanout[3*x+2] = scanin[x][BLU];
256 >                        }
257                  }
258                  if (TIFFWriteScanline(tif, scanout, y, 0) < 0)
259                          quiterr("error writing TIFF output");
# Line 272 | Line 262 | char   *inpf, *outf;
262          free((char *)scanin);
263          free((char *)scanout);
264          TIFFClose(tif);
275        free(hd.buf);
265   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines