10 |
|
|
11 |
|
#include <stdio.h> |
12 |
|
|
13 |
+ |
#include <math.h> |
14 |
+ |
|
15 |
|
#include "tiffio.h" |
16 |
|
|
17 |
|
#include "color.h" |
18 |
|
|
19 |
< |
extern double atof(); |
19 |
> |
#include "resolu.h" |
20 |
|
|
21 |
+ |
|
22 |
|
extern char *malloc(), *realloc(); |
23 |
|
|
24 |
< |
double gamma = 2.2; /* gamma correction */ |
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; |
44 |
|
for (i = 1; i < argc; i++) |
45 |
|
if (argv[i][0] == '-') |
46 |
|
switch (argv[i][1]) { |
40 |
– |
/* not allowed to reset gamma... |
47 |
|
case 'g': |
48 |
< |
gamma = atof(argv[++i]); |
48 |
> |
gamcor = atof(argv[++i]); |
49 |
|
break; |
50 |
< |
*/ |
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; |
69 |
|
else |
70 |
|
break; |
71 |
|
doneopts: |
72 |
< |
setcolrgam(gamma); |
72 |
> |
setcolrgam(gamcor); |
73 |
|
|
74 |
|
if (reverse) |
75 |
|
if (i != argc-2 && i != argc-1) |
84 |
|
|
85 |
|
exit(0); |
86 |
|
userr: |
87 |
< |
fprintf(stderr, "Usage: %s [-r][-e +/-stops] input output\n", |
87 |
> |
fprintf(stderr, |
88 |
> |
"Usage: %s [-r][-b][-z][-e +/-stops][-g gamma] input output\n", |
89 |
|
progname); |
90 |
|
exit(1); |
91 |
|
} |
107 |
|
{ |
108 |
|
unsigned long xmax, ymax; |
109 |
|
TIFF *tif; |
110 |
< |
unsigned short pconfig; |
110 |
> |
unsigned short pconfig, nsamps; |
111 |
|
unsigned short hi; |
100 |
– |
char *cp; |
112 |
|
register BYTE *scanin; |
113 |
|
register COLR *scanout; |
114 |
|
register int x; |
116 |
|
/* open/check TIFF file */ |
117 |
|
if ((tif = TIFFOpen(inpf, "r")) == NULL) |
118 |
|
quiterr("cannot open TIFF input"); |
119 |
< |
if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &hi) || hi != 3) |
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) && hi != 2) |
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 != 1 && pconfig != 2)) |
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)) |
141 |
|
if (outf != NULL && strcmp(outf, "-") && |
142 |
|
freopen(outf, "w", stdout) == NULL) |
143 |
|
quiterr("cannot open Radiance output file"); |
144 |
< |
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 |
< |
} |
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 |
< |
fputresolu(YDECR|YMAJOR, xmax, ymax, stdout); |
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 |
< |
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 |
< |
} |
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; |
197 |
|
} |
198 |
|
|
199 |
|
|
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 |
– |
|
200 |
|
ra2tiff(inpf, outf) /* convert Radiance file to 24-bit TIFF */ |
201 |
|
char *inpf, *outf; |
202 |
|
{ |
219 |
– |
char buf[64]; |
220 |
– |
struct hdinfo hd; |
203 |
|
TIFF *tif; |
204 |
|
int xmax, ymax; |
205 |
|
BYTE *scanout; |
209 |
|
/* open Radiance file */ |
210 |
|
if (strcmp(inpf, "-") && freopen(inpf, "r", stdin) == NULL) |
211 |
|
quiterr("cannot open Radiance input file"); |
212 |
< |
hd.buf = NULL; hd.fmt[0] = '\0'; |
213 |
< |
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)) |
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, 3); |
220 |
> |
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, greyscale ? 1 : 3); |
221 |
|
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); |
222 |
< |
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, 2); |
223 |
< |
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, 1); |
224 |
< |
TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, hd.buf); |
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)); |
237 |
|
quiterr("error reading Radiance picture"); |
238 |
|
if (bradj) |
239 |
|
shiftcolrs(scanin, xmax, bradj); |
240 |
< |
colrs_gambs(scanin, xmax); |
241 |
< |
for (x = 0; x < xmax; x++) { |
242 |
< |
scanout[3*x] = scanin[x][RED]; |
243 |
< |
scanout[3*x+1] = scanin[x][GRN]; |
244 |
< |
scanout[3*x+2] = scanin[x][BLU]; |
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"); |
258 |
|
free((char *)scanin); |
259 |
|
free((char *)scanout); |
260 |
|
TIFFClose(tif); |
275 |
– |
free(hd.buf); |
261 |
|
} |