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 */ |
41 |
|
for (i = 1; i < argc; i++) |
42 |
|
if (argv[i][0] == '-') |
43 |
|
switch (argv[i][1]) { |
40 |
– |
/* not allowed to reset gamma... |
44 |
|
case 'g': |
45 |
|
gamma = atof(argv[++i]); |
46 |
|
break; |
47 |
< |
*/ |
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; |
81 |
|
|
82 |
|
exit(0); |
83 |
|
userr: |
84 |
< |
fprintf(stderr, "Usage: %s [-r][-e +/-stops] input output\n", |
84 |
> |
fprintf(stderr, |
85 |
> |
"Usage: %s [-r][-b][-z][-e +/-stops][-g gamma] input output\n", |
86 |
|
progname); |
87 |
|
exit(1); |
88 |
|
} |
104 |
|
{ |
105 |
|
unsigned long xmax, ymax; |
106 |
|
TIFF *tif; |
107 |
< |
unsigned short pconfig; |
107 |
> |
unsigned short pconfig, nsamps; |
108 |
|
unsigned short hi; |
100 |
– |
char *cp; |
109 |
|
register BYTE *scanin; |
110 |
|
register COLR *scanout; |
111 |
|
register int x; |
113 |
|
/* open/check TIFF file */ |
114 |
|
if ((tif = TIFFOpen(inpf, "r")) == NULL) |
115 |
|
quiterr("cannot open TIFF input"); |
116 |
< |
if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &hi) || hi != 3) |
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) && hi != 2) |
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)) |
136 |
|
if (outf != NULL && strcmp(outf, "-") && |
137 |
|
freopen(outf, "w", stdout) == NULL) |
138 |
|
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 |
– |
} |
139 |
|
fputs(progname, stdout); |
140 |
|
if (bradj) |
141 |
|
printf(" -e %+d", bradj); |
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 |
< |
for (x = 0; x < xmax; x++) { |
154 |
< |
scanout[x][RED] = scanin[3*x]; |
155 |
< |
scanout[x][GRN] = scanin[3*x+1]; |
156 |
< |
scanout[x][BLU] = scanin[3*x+2]; |
157 |
< |
} |
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; |
191 |
|
} |
192 |
|
|
193 |
|
|
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 |
– |
|
194 |
|
ra2tiff(inpf, outf) /* convert Radiance file to 24-bit TIFF */ |
195 |
|
char *inpf, *outf; |
196 |
|
{ |
219 |
– |
char buf[64]; |
220 |
– |
struct hdinfo hd; |
197 |
|
TIFF *tif; |
198 |
|
int xmax, ymax; |
199 |
|
BYTE *scanout; |
203 |
|
/* open Radiance file */ |
204 |
|
if (strcmp(inpf, "-") && freopen(inpf, "r", stdin) == NULL) |
205 |
|
quiterr("cannot open Radiance input file"); |
206 |
< |
hd.buf = NULL; hd.fmt[0] = '\0'; |
207 |
< |
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)) |
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, 3); |
214 |
> |
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, greyscale ? 1 : 3); |
215 |
|
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); |
216 |
< |
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, 2); |
216 |
> |
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, greyscale ? 1 : 2); |
217 |
|
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, 1); |
218 |
< |
TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, hd.buf); |
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)); |
228 |
|
quiterr("error reading Radiance picture"); |
229 |
|
if (bradj) |
230 |
|
shiftcolrs(scanin, xmax, bradj); |
231 |
< |
colrs_gambs(scanin, xmax); |
232 |
< |
for (x = 0; x < xmax; x++) { |
233 |
< |
scanout[3*x] = scanin[x][RED]; |
234 |
< |
scanout[3*x+1] = scanin[x][GRN]; |
235 |
< |
scanout[3*x+2] = scanin[x][BLU]; |
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"); |
249 |
|
free((char *)scanin); |
250 |
|
free((char *)scanout); |
251 |
|
TIFFClose(tif); |
275 |
– |
free(hd.buf); |
252 |
|
} |