ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_tiff.c
Revision: 2.7
Committed: Fri Nov 11 11:26:20 1994 UTC (29 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.6: +10 -5 lines
Log Message:
replaced some values with macros for improved readability

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