ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_tiff.c
Revision: 2.2
Committed: Thu Dec 19 14:52:25 1991 UTC (32 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +0 -1 lines
Log Message:
eliminated atof declarations for NeXT

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