ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_tiff.c
Revision: 2.6
Committed: Sun Feb 27 10:17:27 1994 UTC (30 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +1 -0 lines
Log Message:
Added new ID to first line of header and changed use of formatval

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 greg 2.6 newheader("RADIANCE", stdout);
143 greg 1.1 fputs(progname, stdout);
144     if (bradj)
145     printf(" -e %+d", bradj);
146     fputs(" -r\n", stdout);
147     fputformat(COLRFMT, stdout);
148     putchar('\n');
149 greg 2.5 fprtresolu((int)xmax, (int)ymax, stdout);
150 greg 1.1 /* convert image */
151 greg 1.5 if (nsamps == 1)
152     pconfig = 1;
153 greg 1.1 for (y = 0; y < ymax; y++) {
154     if (pconfig == 1) {
155     if (TIFFReadScanline(tif, scanin, y, 0) < 0)
156     goto readerr;
157 greg 1.5 if (nsamps == 1)
158     for (x = 0; x < xmax; x++)
159     scanout[x][RED] =
160     scanout[x][GRN] =
161     scanout[x][BLU] = scanin[x];
162     else
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     }
168 greg 1.1 } else {
169     if (TIFFReadScanline(tif, scanin, y, 0) < 0)
170     goto readerr;
171     for (x = 0; x < xmax; x++)
172     scanout[x][RED] = scanin[x];
173     if (TIFFReadScanline(tif, scanin, y, 1) < 0)
174     goto readerr;
175     for (x = 0; x < xmax; x++)
176     scanout[x][GRN] = scanin[x];
177     if (TIFFReadScanline(tif, scanin, y, 2) < 0)
178     goto readerr;
179     for (x = 0; x < xmax; x++)
180     scanout[x][BLU] = scanin[x];
181     }
182     gambs_colrs(scanout, xmax);
183     if (bradj)
184     shiftcolrs(scanout, xmax, bradj);
185     if (fwritecolrs(scanout, xmax, stdout) < 0)
186     quiterr("error writing Radiance picture");
187     }
188     /* clean up */
189     free((char *)scanin);
190     free((char *)scanout);
191     TIFFClose(tif);
192     return;
193     readerr:
194     quiterr("error reading TIFF input");
195     }
196    
197    
198     ra2tiff(inpf, outf) /* convert Radiance file to 24-bit TIFF */
199     char *inpf, *outf;
200     {
201     TIFF *tif;
202     int xmax, ymax;
203     BYTE *scanout;
204     COLR *scanin;
205     register int x;
206     int y;
207     /* open Radiance file */
208     if (strcmp(inpf, "-") && freopen(inpf, "r", stdin) == NULL)
209     quiterr("cannot open Radiance input file");
210 greg 1.2 if (checkheader(stdin, COLRFMT, NULL) < 0 ||
211 greg 1.6 fgetresolu(&xmax, &ymax, stdin) < 0)
212 greg 1.1 quiterr("bad Radiance picture");
213     /* open TIFF file */
214     if ((tif = TIFFOpen(outf, "w")) == NULL)
215     quiterr("cannot open TIFF output");
216     TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (unsigned long)xmax);
217     TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (unsigned long)ymax);
218 greg 1.5 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, greyscale ? 1 : 3);
219 greg 1.1 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
220 greg 1.5 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, greyscale ? 1 : 2);
221 greg 1.1 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, 1);
222 greg 1.4 if (lzcomp)
223     TIFFSetField(tif, TIFFTAG_COMPRESSION, (unsigned short)5);
224 greg 1.1 /* allocate scanlines */
225     scanin = (COLR *)malloc(xmax*sizeof(COLR));
226     scanout = (BYTE *)malloc(TIFFScanlineSize(tif));
227     if (scanin == NULL || scanout == NULL)
228     quiterr("out of memory in ra2tiff");
229     /* convert image */
230     for (y = 0; y < ymax; y++) {
231     if (freadcolrs(scanin, xmax, stdin) < 0)
232     quiterr("error reading Radiance picture");
233     if (bradj)
234     shiftcolrs(scanin, xmax, bradj);
235 greg 1.5 if (greyscale) {
236     for (x = 0; x < xmax; x++)
237     scanin[x][GRN] = normbright(scanin[x]);
238     colrs_gambs(scanin, xmax);
239     for (x = 0; x < xmax; x++)
240     scanout[x] = scanin[x][GRN];
241     } else {
242     colrs_gambs(scanin, xmax);
243     for (x = 0; x < xmax; x++) {
244     scanout[3*x] = scanin[x][RED];
245     scanout[3*x+1] = scanin[x][GRN];
246     scanout[3*x+2] = scanin[x][BLU];
247     }
248 greg 1.1 }
249     if (TIFFWriteScanline(tif, scanout, y, 0) < 0)
250     quiterr("error writing TIFF output");
251     }
252     /* clean up */
253     free((char *)scanin);
254     free((char *)scanout);
255     TIFFClose(tif);
256     }