ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_tiff.c
Revision: 1.5
Committed: Thu Aug 15 15:47:35 1991 UTC (32 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.4: +39 -16 lines
Log Message:
added -b option for greyscale images

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