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