ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_tiff.c
Revision: 2.5
Committed: Thu Nov 18 09:55:26 1993 UTC (30 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +1 -1 lines
Log Message:
minor compiler warning fixes

File Contents

# User Rev Content
1 greg 1.1 /* Copyright (c) 1991 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * program to convert between RADIANCE and 24-bit TIFF files.
9     */
10    
11     #include <stdio.h>
12    
13 greg 2.3 #include <math.h>
14    
15 greg 1.1 #include "tiffio.h"
16    
17     #include "color.h"
18    
19 greg 1.6 #include "resolu.h"
20    
21 greg 1.1
22     extern char *malloc(), *realloc();
23    
24 greg 1.4 int lzcomp = 0; /* use Lempel-Ziv compression? */
25    
26 greg 1.5 int greyscale = 0; /* produce greyscale image? */
27    
28 greg 2.4 double gamcor = 2.2; /* gamma correction */
29 greg 1.1
30     int bradj = 0; /* brightness adjustment */
31    
32     char *progname;
33    
34    
35     main(argc, argv)
36     int argc;
37     char *argv[];
38     {
39     int reverse = 0;
40     int i;
41    
42     progname = argv[0];
43    
44     for (i = 1; i < argc; i++)
45     if (argv[i][0] == '-')
46     switch (argv[i][1]) {
47     case 'g':
48 greg 2.4 gamcor = atof(argv[++i]);
49 greg 1.1 break;
50 greg 1.4 case 'z':
51     lzcomp = !lzcomp;
52     break;
53 greg 1.5 case 'b':
54     greyscale = !greyscale;
55     break;
56 greg 1.1 case 'e':
57     if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
58     goto userr;
59     bradj = atoi(argv[++i]);
60     break;
61     case 'r':
62     reverse = !reverse;
63     break;
64     case '\0':
65     goto doneopts;
66     default:
67     goto userr;
68     }
69     else
70     break;
71     doneopts:
72 greg 2.4 setcolrgam(gamcor);
73 greg 1.1
74     if (reverse)
75     if (i != argc-2 && i != argc-1)
76     goto userr;
77     else
78     tiff2ra(argv[i], argv[i+1]);
79     else
80     if (i != argc-2)
81     goto userr;
82     else
83     ra2tiff(argv[i], argv[i+1]);
84    
85     exit(0);
86     userr:
87 greg 1.4 fprintf(stderr,
88 greg 1.5 "Usage: %s [-r][-b][-z][-e +/-stops][-g gamma] input output\n",
89 greg 1.1 progname);
90     exit(1);
91     }
92    
93    
94     quiterr(err) /* print message and exit */
95     char *err;
96     {
97     if (err != NULL) {
98     fprintf(stderr, "%s: %s\n", progname, err);
99     exit(1);
100     }
101     exit(0);
102     }
103    
104    
105     tiff2ra(inpf, outf) /* convert TIFF file to Radiance picture */
106     char *inpf, *outf;
107     {
108     unsigned long xmax, ymax;
109     TIFF *tif;
110 greg 1.5 unsigned short pconfig, nsamps;
111 greg 1.1 unsigned short hi;
112     register BYTE *scanin;
113     register COLR *scanout;
114     register int x;
115     int y;
116     /* open/check TIFF file */
117     if ((tif = TIFFOpen(inpf, "r")) == NULL)
118     quiterr("cannot open TIFF input");
119 greg 1.5 if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &nsamps) ||
120     (nsamps != 1 && nsamps != 3))
121 greg 1.1 quiterr("unsupported samples per pixel");
122     if (!TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &hi) || hi != 8)
123     quiterr("unsupported bits per sample");
124 greg 1.5 if (TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &hi) &&
125     hi != (nsamps==1 ? 1 : 2))
126 greg 1.1 quiterr("unsupported photometric interpretation");
127     if (!TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &pconfig) ||
128     (pconfig != 1 && pconfig != 2))
129     quiterr("unsupported planar configuration");
130     if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &xmax) ||
131     !TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &ymax))
132     quiterr("unknown input image resolution");
133     /* allocate scanlines */
134     scanin = (BYTE *)malloc(TIFFScanlineSize(tif));
135     scanout = (COLR *)malloc(xmax*sizeof(COLR));
136     if (scanin == NULL || scanout == NULL)
137     quiterr("out of memory in tiff2ra");
138     /* open output and write header */
139     if (outf != NULL && strcmp(outf, "-") &&
140     freopen(outf, "w", stdout) == NULL)
141     quiterr("cannot open Radiance output file");
142     fputs(progname, stdout);
143     if (bradj)
144     printf(" -e %+d", bradj);
145     fputs(" -r\n", stdout);
146     fputformat(COLRFMT, stdout);
147     putchar('\n');
148 greg 2.5 fprtresolu((int)xmax, (int)ymax, stdout);
149 greg 1.1 /* convert image */
150 greg 1.5 if (nsamps == 1)
151     pconfig = 1;
152 greg 1.1 for (y = 0; y < ymax; y++) {
153     if (pconfig == 1) {
154     if (TIFFReadScanline(tif, scanin, y, 0) < 0)
155     goto readerr;
156 greg 1.5 if (nsamps == 1)
157     for (x = 0; x < xmax; x++)
158     scanout[x][RED] =
159     scanout[x][GRN] =
160     scanout[x][BLU] = scanin[x];
161     else
162     for (x = 0; x < xmax; x++) {
163     scanout[x][RED] = scanin[3*x];
164     scanout[x][GRN] = scanin[3*x+1];
165     scanout[x][BLU] = scanin[3*x+2];
166     }
167 greg 1.1 } else {
168     if (TIFFReadScanline(tif, scanin, y, 0) < 0)
169     goto readerr;
170     for (x = 0; x < xmax; x++)
171     scanout[x][RED] = scanin[x];
172     if (TIFFReadScanline(tif, scanin, y, 1) < 0)
173     goto readerr;
174     for (x = 0; x < xmax; x++)
175     scanout[x][GRN] = scanin[x];
176     if (TIFFReadScanline(tif, scanin, y, 2) < 0)
177     goto readerr;
178     for (x = 0; x < xmax; x++)
179     scanout[x][BLU] = scanin[x];
180     }
181     gambs_colrs(scanout, xmax);
182     if (bradj)
183     shiftcolrs(scanout, xmax, bradj);
184     if (fwritecolrs(scanout, xmax, stdout) < 0)
185     quiterr("error writing Radiance picture");
186     }
187     /* clean up */
188     free((char *)scanin);
189     free((char *)scanout);
190     TIFFClose(tif);
191     return;
192     readerr:
193     quiterr("error reading TIFF input");
194     }
195    
196    
197     ra2tiff(inpf, outf) /* convert Radiance file to 24-bit TIFF */
198     char *inpf, *outf;
199     {
200     TIFF *tif;
201     int xmax, ymax;
202     BYTE *scanout;
203     COLR *scanin;
204     register int x;
205     int y;
206     /* open Radiance file */
207     if (strcmp(inpf, "-") && freopen(inpf, "r", stdin) == NULL)
208     quiterr("cannot open Radiance input file");
209 greg 1.2 if (checkheader(stdin, COLRFMT, NULL) < 0 ||
210 greg 1.6 fgetresolu(&xmax, &ymax, stdin) < 0)
211 greg 1.1 quiterr("bad Radiance picture");
212     /* open TIFF file */
213     if ((tif = TIFFOpen(outf, "w")) == NULL)
214     quiterr("cannot open TIFF output");
215     TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (unsigned long)xmax);
216     TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (unsigned long)ymax);
217 greg 1.5 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, greyscale ? 1 : 3);
218 greg 1.1 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
219 greg 1.5 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, greyscale ? 1 : 2);
220 greg 1.1 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, 1);
221 greg 1.4 if (lzcomp)
222     TIFFSetField(tif, TIFFTAG_COMPRESSION, (unsigned short)5);
223 greg 1.1 /* allocate scanlines */
224     scanin = (COLR *)malloc(xmax*sizeof(COLR));
225     scanout = (BYTE *)malloc(TIFFScanlineSize(tif));
226     if (scanin == NULL || scanout == NULL)
227     quiterr("out of memory in ra2tiff");
228     /* convert image */
229     for (y = 0; y < ymax; y++) {
230     if (freadcolrs(scanin, xmax, stdin) < 0)
231     quiterr("error reading Radiance picture");
232     if (bradj)
233     shiftcolrs(scanin, xmax, bradj);
234 greg 1.5 if (greyscale) {
235     for (x = 0; x < xmax; x++)
236     scanin[x][GRN] = normbright(scanin[x]);
237     colrs_gambs(scanin, xmax);
238     for (x = 0; x < xmax; x++)
239     scanout[x] = scanin[x][GRN];
240     } else {
241     colrs_gambs(scanin, xmax);
242     for (x = 0; x < xmax; x++) {
243     scanout[3*x] = scanin[x][RED];
244     scanout[3*x+1] = scanin[x][GRN];
245     scanout[3*x+2] = scanin[x][BLU];
246     }
247 greg 1.1 }
248     if (TIFFWriteScanline(tif, scanout, y, 0) < 0)
249     quiterr("error writing TIFF output");
250     }
251     /* clean up */
252     free((char *)scanin);
253     free((char *)scanout);
254     TIFFClose(tif);
255     }