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

# Content
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 #include <math.h>
14
15 #include "tiffio.h"
16
17 #include "color.h"
18
19 #include "resolu.h"
20
21
22 extern char *malloc(), *realloc();
23
24 int lzcomp = 0; /* use Lempel-Ziv compression? */
25
26 int greyscale = 0; /* produce greyscale image? */
27
28 double gamcor = 2.2; /* gamma correction */
29
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 gamcor = atof(argv[++i]);
49 break;
50 case 'z':
51 lzcomp = !lzcomp;
52 break;
53 case 'b':
54 greyscale = !greyscale;
55 break;
56 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 setcolrgam(gamcor);
73
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 fprintf(stderr,
88 "Usage: %s [-r][-b][-z][-e +/-stops][-g gamma] input output\n",
89 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 unsigned short pconfig, nsamps;
111 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 if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &nsamps) ||
120 (nsamps != 1 && nsamps != 3))
121 quiterr("unsupported samples per pixel");
122 if (!TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &hi) || hi != 8)
123 quiterr("unsupported bits per sample");
124 if (TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &hi) &&
125 hi != (nsamps==1 ? PHOTOMETRIC_MINISBLACK :
126 PHOTOMETRIC_RGB))
127 quiterr("unsupported photometric interpretation");
128 if (!TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &pconfig) ||
129 (pconfig != PLANARCONFIG_CONTIG &&
130 pconfig != PLANARCONFIG_SEPARATE))
131 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 newheader("RADIANCE", stdout);
145 fputs(progname, stdout);
146 if (bradj)
147 printf(" -e %+d", bradj);
148 fputs(" -r\n", stdout);
149 fputformat(COLRFMT, stdout);
150 putchar('\n');
151 fprtresolu((int)xmax, (int)ymax, stdout);
152 /* convert image */
153 if (nsamps == 1)
154 pconfig = 1;
155 for (y = 0; y < ymax; y++) {
156 if (pconfig == 1) {
157 if (TIFFReadScanline(tif, scanin, y, 0) < 0)
158 goto readerr;
159 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 } 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 if (checkheader(stdin, COLRFMT, NULL) < 0 ||
213 fgetresolu(&xmax, &ymax, stdin) < 0)
214 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 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, greyscale ? 1 : 3);
221 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
222 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC,
223 greyscale ? PHOTOMETRIC_MINISBLACK :
224 PHOTOMETRIC_RGB);
225 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
226 if (lzcomp)
227 TIFFSetField(tif, TIFFTAG_COMPRESSION,
228 (unsigned short)COMPRESSION_LZW);
229 /* 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 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 }
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 }