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 |
+ |
#define GAMCOR 2.2 /* default gamma */ |
23 |
+ |
|
24 |
|
extern char *malloc(), *realloc(); |
25 |
|
|
26 |
< |
double gamma = 2.2; /* gamma correction */ |
26 |
> |
int lzcomp = 0; /* use Lempel-Ziv compression? */ |
27 |
|
|
28 |
+ |
int greyscale = 0; /* produce greyscale image? */ |
29 |
+ |
|
30 |
+ |
double gamcor = GAMCOR; /* gamma correction */ |
31 |
+ |
|
32 |
|
int bradj = 0; /* brightness adjustment */ |
33 |
|
|
34 |
|
char *progname; |
46 |
|
for (i = 1; i < argc; i++) |
47 |
|
if (argv[i][0] == '-') |
48 |
|
switch (argv[i][1]) { |
40 |
– |
/* not allowed to reset gamma... |
49 |
|
case 'g': |
50 |
< |
gamma = atof(argv[++i]); |
50 |
> |
gamcor = atof(argv[++i]); |
51 |
|
break; |
52 |
< |
*/ |
52 |
> |
case 'z': |
53 |
> |
lzcomp = !lzcomp; |
54 |
> |
break; |
55 |
> |
case 'b': |
56 |
> |
greyscale = !greyscale; |
57 |
> |
break; |
58 |
|
case 'e': |
59 |
|
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
60 |
|
goto userr; |
71 |
|
else |
72 |
|
break; |
73 |
|
doneopts: |
74 |
< |
setcolrgam(gamma); |
74 |
> |
setcolrgam(gamcor); |
75 |
|
|
76 |
|
if (reverse) |
77 |
|
if (i != argc-2 && i != argc-1) |
86 |
|
|
87 |
|
exit(0); |
88 |
|
userr: |
89 |
< |
fprintf(stderr, "Usage: %s [-r][-e +/-stops] input output\n", |
89 |
> |
fprintf(stderr, |
90 |
> |
"Usage: %s [-r][-b][-z][-e +/-stops][-g gamma] input output\n", |
91 |
|
progname); |
92 |
|
exit(1); |
93 |
|
} |
109 |
|
{ |
110 |
|
unsigned long xmax, ymax; |
111 |
|
TIFF *tif; |
112 |
< |
unsigned short pconfig; |
112 |
> |
unsigned short pconfig, nsamps; |
113 |
|
unsigned short hi; |
114 |
|
register BYTE *scanin; |
115 |
|
register COLR *scanout; |
118 |
|
/* open/check TIFF file */ |
119 |
|
if ((tif = TIFFOpen(inpf, "r")) == NULL) |
120 |
|
quiterr("cannot open TIFF input"); |
121 |
< |
if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &hi) || hi != 3) |
121 |
> |
if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &nsamps) || |
122 |
> |
(nsamps != 1 && nsamps != 3)) |
123 |
|
quiterr("unsupported samples per pixel"); |
124 |
|
if (!TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &hi) || hi != 8) |
125 |
|
quiterr("unsupported bits per sample"); |
126 |
< |
if (TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &hi) && hi != 2) |
126 |
> |
if (TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &hi) && |
127 |
> |
hi != (nsamps==1 ? PHOTOMETRIC_MINISBLACK : |
128 |
> |
PHOTOMETRIC_RGB)) |
129 |
|
quiterr("unsupported photometric interpretation"); |
130 |
|
if (!TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &pconfig) || |
131 |
< |
(pconfig != 1 && pconfig != 2)) |
131 |
> |
(pconfig != PLANARCONFIG_CONTIG && |
132 |
> |
pconfig != PLANARCONFIG_SEPARATE)) |
133 |
|
quiterr("unsupported planar configuration"); |
134 |
|
if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &xmax) || |
135 |
|
!TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &ymax)) |
143 |
|
if (outf != NULL && strcmp(outf, "-") && |
144 |
|
freopen(outf, "w", stdout) == NULL) |
145 |
|
quiterr("cannot open Radiance output file"); |
146 |
+ |
newheader("RADIANCE", stdout); |
147 |
|
fputs(progname, stdout); |
148 |
|
if (bradj) |
149 |
|
printf(" -e %+d", bradj); |
150 |
+ |
if (gamcor != GAMCOR) |
151 |
+ |
printf(" -g %f", gamcor); |
152 |
|
fputs(" -r\n", stdout); |
153 |
|
fputformat(COLRFMT, stdout); |
154 |
|
putchar('\n'); |
155 |
< |
fputresolu(YDECR|YMAJOR, xmax, ymax, stdout); |
155 |
> |
fprtresolu((int)xmax, (int)ymax, stdout); |
156 |
|
/* convert image */ |
157 |
+ |
if (nsamps == 1) |
158 |
+ |
pconfig = 1; |
159 |
|
for (y = 0; y < ymax; y++) { |
160 |
|
if (pconfig == 1) { |
161 |
|
if (TIFFReadScanline(tif, scanin, y, 0) < 0) |
162 |
|
goto readerr; |
163 |
< |
for (x = 0; x < xmax; x++) { |
164 |
< |
scanout[x][RED] = scanin[3*x]; |
165 |
< |
scanout[x][GRN] = scanin[3*x+1]; |
166 |
< |
scanout[x][BLU] = scanin[3*x+2]; |
167 |
< |
} |
163 |
> |
if (nsamps == 1) |
164 |
> |
for (x = 0; x < xmax; x++) |
165 |
> |
scanout[x][RED] = |
166 |
> |
scanout[x][GRN] = |
167 |
> |
scanout[x][BLU] = scanin[x]; |
168 |
> |
else |
169 |
> |
for (x = 0; x < xmax; x++) { |
170 |
> |
scanout[x][RED] = scanin[3*x]; |
171 |
> |
scanout[x][GRN] = scanin[3*x+1]; |
172 |
> |
scanout[x][BLU] = scanin[3*x+2]; |
173 |
> |
} |
174 |
|
} else { |
175 |
|
if (TIFFReadScanline(tif, scanin, y, 0) < 0) |
176 |
|
goto readerr; |
214 |
|
if (strcmp(inpf, "-") && freopen(inpf, "r", stdin) == NULL) |
215 |
|
quiterr("cannot open Radiance input file"); |
216 |
|
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
217 |
< |
fgetresolu(&xmax, &ymax, stdin) != (YDECR|YMAJOR)) |
217 |
> |
fgetresolu(&xmax, &ymax, stdin) < 0) |
218 |
|
quiterr("bad Radiance picture"); |
219 |
|
/* open TIFF file */ |
220 |
|
if ((tif = TIFFOpen(outf, "w")) == NULL) |
221 |
|
quiterr("cannot open TIFF output"); |
222 |
|
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (unsigned long)xmax); |
223 |
|
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (unsigned long)ymax); |
224 |
< |
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3); |
224 |
> |
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, greyscale ? 1 : 3); |
225 |
|
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); |
226 |
< |
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, 2); |
227 |
< |
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, 1); |
226 |
> |
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, |
227 |
> |
greyscale ? PHOTOMETRIC_MINISBLACK : |
228 |
> |
PHOTOMETRIC_RGB); |
229 |
> |
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |
230 |
> |
if (lzcomp) |
231 |
> |
TIFFSetField(tif, TIFFTAG_COMPRESSION, |
232 |
> |
(unsigned short)COMPRESSION_LZW); |
233 |
|
/* allocate scanlines */ |
234 |
|
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
235 |
|
scanout = (BYTE *)malloc(TIFFScanlineSize(tif)); |
241 |
|
quiterr("error reading Radiance picture"); |
242 |
|
if (bradj) |
243 |
|
shiftcolrs(scanin, xmax, bradj); |
244 |
< |
colrs_gambs(scanin, xmax); |
245 |
< |
for (x = 0; x < xmax; x++) { |
246 |
< |
scanout[3*x] = scanin[x][RED]; |
247 |
< |
scanout[3*x+1] = scanin[x][GRN]; |
248 |
< |
scanout[3*x+2] = scanin[x][BLU]; |
244 |
> |
if (greyscale) { |
245 |
> |
for (x = 0; x < xmax; x++) |
246 |
> |
scanin[x][GRN] = normbright(scanin[x]); |
247 |
> |
colrs_gambs(scanin, xmax); |
248 |
> |
for (x = 0; x < xmax; x++) |
249 |
> |
scanout[x] = scanin[x][GRN]; |
250 |
> |
} else { |
251 |
> |
colrs_gambs(scanin, xmax); |
252 |
> |
for (x = 0; x < xmax; x++) { |
253 |
> |
scanout[3*x] = scanin[x][RED]; |
254 |
> |
scanout[3*x+1] = scanin[x][GRN]; |
255 |
> |
scanout[3*x+2] = scanin[x][BLU]; |
256 |
> |
} |
257 |
|
} |
258 |
|
if (TIFFWriteScanline(tif, scanout, y, 0) < 0) |
259 |
|
quiterr("error writing TIFF output"); |