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