14 |
|
|
15 |
|
#include "color.h" |
16 |
|
|
17 |
+ |
#include "resolu.h" |
18 |
+ |
|
19 |
|
extern double atof(); |
20 |
|
|
21 |
|
extern char *malloc(), *realloc(); |
22 |
|
|
23 |
+ |
int lzcomp = 0; /* use Lempel-Ziv compression? */ |
24 |
+ |
|
25 |
+ |
int greyscale = 0; /* produce greyscale image? */ |
26 |
+ |
|
27 |
|
double gamma = 2.2; /* gamma correction */ |
28 |
|
|
29 |
|
int bradj = 0; /* brightness adjustment */ |
43 |
|
for (i = 1; i < argc; i++) |
44 |
|
if (argv[i][0] == '-') |
45 |
|
switch (argv[i][1]) { |
40 |
– |
/* not allowed to reset gamma... |
46 |
|
case 'g': |
47 |
|
gamma = atof(argv[++i]); |
48 |
|
break; |
49 |
< |
*/ |
49 |
> |
case 'z': |
50 |
> |
lzcomp = !lzcomp; |
51 |
> |
break; |
52 |
> |
case 'b': |
53 |
> |
greyscale = !greyscale; |
54 |
> |
break; |
55 |
|
case 'e': |
56 |
|
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
57 |
|
goto userr; |
83 |
|
|
84 |
|
exit(0); |
85 |
|
userr: |
86 |
< |
fprintf(stderr, "Usage: %s [-r][-e +/-stops] input output\n", |
86 |
> |
fprintf(stderr, |
87 |
> |
"Usage: %s [-r][-b][-z][-e +/-stops][-g gamma] input output\n", |
88 |
|
progname); |
89 |
|
exit(1); |
90 |
|
} |
106 |
|
{ |
107 |
|
unsigned long xmax, ymax; |
108 |
|
TIFF *tif; |
109 |
< |
unsigned short pconfig; |
109 |
> |
unsigned short pconfig, nsamps; |
110 |
|
unsigned short hi; |
100 |
– |
char *cp; |
111 |
|
register BYTE *scanin; |
112 |
|
register COLR *scanout; |
113 |
|
register int x; |
115 |
|
/* open/check TIFF file */ |
116 |
|
if ((tif = TIFFOpen(inpf, "r")) == NULL) |
117 |
|
quiterr("cannot open TIFF input"); |
118 |
< |
if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &hi) || hi != 3) |
118 |
> |
if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &nsamps) || |
119 |
> |
(nsamps != 1 && nsamps != 3)) |
120 |
|
quiterr("unsupported samples per pixel"); |
121 |
|
if (!TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &hi) || hi != 8) |
122 |
|
quiterr("unsupported bits per sample"); |
123 |
< |
if (TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &hi) && hi != 2) |
123 |
> |
if (TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &hi) && |
124 |
> |
hi != (nsamps==1 ? 1 : 2)) |
125 |
|
quiterr("unsupported photometric interpretation"); |
126 |
|
if (!TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &pconfig) || |
127 |
|
(pconfig != 1 && pconfig != 2)) |
138 |
|
if (outf != NULL && strcmp(outf, "-") && |
139 |
|
freopen(outf, "w", stdout) == NULL) |
140 |
|
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 |
– |
} |
141 |
|
fputs(progname, stdout); |
142 |
|
if (bradj) |
143 |
|
printf(" -e %+d", bradj); |
144 |
|
fputs(" -r\n", stdout); |
145 |
|
fputformat(COLRFMT, stdout); |
146 |
|
putchar('\n'); |
147 |
< |
fputresolu(YDECR|YMAJOR, xmax, ymax, stdout); |
147 |
> |
fprtresolu(xmax, ymax, stdout); |
148 |
|
/* convert image */ |
149 |
+ |
if (nsamps == 1) |
150 |
+ |
pconfig = 1; |
151 |
|
for (y = 0; y < ymax; y++) { |
152 |
|
if (pconfig == 1) { |
153 |
|
if (TIFFReadScanline(tif, scanin, y, 0) < 0) |
154 |
|
goto readerr; |
155 |
< |
for (x = 0; x < xmax; x++) { |
156 |
< |
scanout[x][RED] = scanin[3*x]; |
157 |
< |
scanout[x][GRN] = scanin[3*x+1]; |
158 |
< |
scanout[x][BLU] = scanin[3*x+2]; |
159 |
< |
} |
155 |
> |
if (nsamps == 1) |
156 |
> |
for (x = 0; x < xmax; x++) |
157 |
> |
scanout[x][RED] = |
158 |
> |
scanout[x][GRN] = |
159 |
> |
scanout[x][BLU] = scanin[x]; |
160 |
> |
else |
161 |
> |
for (x = 0; x < xmax; x++) { |
162 |
> |
scanout[x][RED] = scanin[3*x]; |
163 |
> |
scanout[x][GRN] = scanin[3*x+1]; |
164 |
> |
scanout[x][BLU] = scanin[3*x+2]; |
165 |
> |
} |
166 |
|
} else { |
167 |
|
if (TIFFReadScanline(tif, scanin, y, 0) < 0) |
168 |
|
goto readerr; |
193 |
|
} |
194 |
|
|
195 |
|
|
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 |
– |
|
196 |
|
ra2tiff(inpf, outf) /* convert Radiance file to 24-bit TIFF */ |
197 |
|
char *inpf, *outf; |
198 |
|
{ |
219 |
– |
char buf[64]; |
220 |
– |
struct hdinfo hd; |
199 |
|
TIFF *tif; |
200 |
|
int xmax, ymax; |
201 |
|
BYTE *scanout; |
205 |
|
/* open Radiance file */ |
206 |
|
if (strcmp(inpf, "-") && freopen(inpf, "r", stdin) == NULL) |
207 |
|
quiterr("cannot open Radiance input file"); |
208 |
< |
hd.buf = NULL; hd.fmt[0] = '\0'; |
209 |
< |
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)) |
208 |
> |
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
209 |
> |
fgetresolu(&xmax, &ymax, stdin) < 0) |
210 |
|
quiterr("bad Radiance picture"); |
211 |
|
/* open TIFF file */ |
212 |
|
if ((tif = TIFFOpen(outf, "w")) == NULL) |
213 |
|
quiterr("cannot open TIFF output"); |
214 |
|
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (unsigned long)xmax); |
215 |
|
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (unsigned long)ymax); |
216 |
< |
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3); |
216 |
> |
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, greyscale ? 1 : 3); |
217 |
|
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); |
218 |
< |
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, 2); |
218 |
> |
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, greyscale ? 1 : 2); |
219 |
|
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, 1); |
220 |
< |
TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, hd.buf); |
220 |
> |
if (lzcomp) |
221 |
> |
TIFFSetField(tif, TIFFTAG_COMPRESSION, (unsigned short)5); |
222 |
|
/* allocate scanlines */ |
223 |
|
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
224 |
|
scanout = (BYTE *)malloc(TIFFScanlineSize(tif)); |
230 |
|
quiterr("error reading Radiance picture"); |
231 |
|
if (bradj) |
232 |
|
shiftcolrs(scanin, xmax, bradj); |
233 |
< |
colrs_gambs(scanin, xmax); |
234 |
< |
for (x = 0; x < xmax; x++) { |
235 |
< |
scanout[3*x] = scanin[x][RED]; |
236 |
< |
scanout[3*x+1] = scanin[x][GRN]; |
237 |
< |
scanout[3*x+2] = scanin[x][BLU]; |
233 |
> |
if (greyscale) { |
234 |
> |
for (x = 0; x < xmax; x++) |
235 |
> |
scanin[x][GRN] = normbright(scanin[x]); |
236 |
> |
colrs_gambs(scanin, xmax); |
237 |
> |
for (x = 0; x < xmax; x++) |
238 |
> |
scanout[x] = scanin[x][GRN]; |
239 |
> |
} else { |
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]; |
245 |
> |
} |
246 |
|
} |
247 |
|
if (TIFFWriteScanline(tif, scanout, y, 0) < 0) |
248 |
|
quiterr("error writing TIFF output"); |
251 |
|
free((char *)scanin); |
252 |
|
free((char *)scanout); |
253 |
|
TIFFClose(tif); |
275 |
– |
free(hd.buf); |
254 |
|
} |