1 |
gregl |
2.9 |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
2 |
greg |
1.1 |
|
3 |
|
|
#ifndef lint |
4 |
gregl |
2.9 |
static char SCCSid[] = "$SunId$ SGI"; |
5 |
greg |
1.1 |
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
gregl |
2.9 |
* Program to convert between RADIANCE and TIFF files. |
9 |
|
|
* Added experimental LogLuv encodings 7/97 (GWL). |
10 |
greg |
1.1 |
*/ |
11 |
|
|
|
12 |
|
|
#include <stdio.h> |
13 |
greg |
2.3 |
#include <math.h> |
14 |
greg |
1.1 |
#include "tiffio.h" |
15 |
|
|
#include "color.h" |
16 |
greg |
1.6 |
#include "resolu.h" |
17 |
|
|
|
18 |
greg |
2.8 |
#define GAMCOR 2.2 /* default gamma */ |
19 |
|
|
|
20 |
gregl |
2.9 |
#ifndef malloc |
21 |
|
|
extern char *malloc(); |
22 |
|
|
#endif |
23 |
|
|
/* conversion flags */ |
24 |
|
|
#define C_CXFM 0x1 /* needs color transformation */ |
25 |
|
|
#define C_GAMUT 0x2 /* needs gamut mapping */ |
26 |
|
|
#define C_GAMMA 0x4 /* needs gamma correction */ |
27 |
|
|
#define C_GRY 0x8 /* TIFF is greyscale */ |
28 |
|
|
#define C_XYZE 0x10 /* Radiance is XYZE */ |
29 |
|
|
#define C_RFLT 0x20 /* Radiance data is float */ |
30 |
|
|
#define C_TFLT 0x40 /* TIFF data is float */ |
31 |
|
|
#define C_PRIM 0x80 /* has assigned primaries */ |
32 |
greg |
1.1 |
|
33 |
gregl |
2.9 |
struct { |
34 |
|
|
uint16 flags; /* conversion flags (defined above) */ |
35 |
|
|
uint16 comp; /* TIFF compression type */ |
36 |
|
|
uint16 pconf; /* TIFF planar configuration */ |
37 |
|
|
float gamcor; /* gamma correction value */ |
38 |
|
|
short bradj; /* Radiance exposure adjustment (stops) */ |
39 |
|
|
uint16 orient; /* visual orientation (TIFF spec.) */ |
40 |
|
|
float stonits; /* input conversion to nits */ |
41 |
|
|
float pixrat; /* pixel aspect ratio */ |
42 |
|
|
FILE *rfp; /* Radiance stream pointer */ |
43 |
|
|
TIFF *tif; /* TIFF pointer */ |
44 |
|
|
uint32 xmax, ymax; /* image dimensions */ |
45 |
|
|
COLORMAT cmat; /* color transformation matrix */ |
46 |
|
|
RGBPRIMS prims; /* RGB primaries */ |
47 |
|
|
union { |
48 |
|
|
COLR *colrs; /* 4-byte ???E pointer */ |
49 |
|
|
COLOR *colors; /* float array pointer */ |
50 |
|
|
char *p; /* generic pointer */ |
51 |
|
|
} r; /* Radiance scanline */ |
52 |
|
|
union { |
53 |
|
|
uint8 *bp; /* byte pointer */ |
54 |
|
|
float *fp; /* float pointer */ |
55 |
|
|
char *p; /* generic pointer */ |
56 |
|
|
} t; /* TIFF scanline */ |
57 |
|
|
int (*tf)(); /* translation procedure */ |
58 |
|
|
} cvts = { /* conversion structure */ |
59 |
|
|
0, COMPRESSION_NONE, PLANARCONFIG_CONTIG, GAMCOR, 0, |
60 |
|
|
}; |
61 |
greg |
1.4 |
|
62 |
gregl |
2.9 |
#define CHK(f) (cvts.flags & (f)) |
63 |
|
|
#define SET(f) (cvts.flags |= (f)) |
64 |
|
|
#define CLR(f) (cvts.flags &= ~(f)) |
65 |
|
|
#define TGL(f) (cvts.flags ^= (f)) |
66 |
greg |
1.5 |
|
67 |
gregl |
2.9 |
int Luv2Color(), L2Color(), RGB2Colr(), Gry2Colr(); |
68 |
|
|
int Color2Luv(), Color2L(), Colr2RGB(), Colr2Gry(); |
69 |
greg |
1.1 |
|
70 |
gregl |
2.9 |
short ortab[8] = { /* orientation conversion table */ |
71 |
|
|
YMAJOR|YDECR, |
72 |
|
|
YMAJOR|YDECR|XDECR, |
73 |
|
|
YMAJOR|XDECR, |
74 |
|
|
YMAJOR, |
75 |
|
|
YDECR, |
76 |
|
|
XDECR|YDECR, |
77 |
|
|
XDECR, |
78 |
|
|
0 |
79 |
|
|
}; |
80 |
greg |
1.1 |
|
81 |
gregl |
2.9 |
#define pixorder() ortab[cvts.orient-1] |
82 |
|
|
|
83 |
greg |
1.1 |
char *progname; |
84 |
|
|
|
85 |
|
|
|
86 |
|
|
main(argc, argv) |
87 |
|
|
int argc; |
88 |
|
|
char *argv[]; |
89 |
|
|
{ |
90 |
|
|
int reverse = 0; |
91 |
|
|
int i; |
92 |
|
|
|
93 |
|
|
progname = argv[0]; |
94 |
|
|
|
95 |
|
|
for (i = 1; i < argc; i++) |
96 |
|
|
if (argv[i][0] == '-') |
97 |
|
|
switch (argv[i][1]) { |
98 |
gregl |
2.9 |
case 'g': /* gamma correction */ |
99 |
|
|
cvts.gamcor = atof(argv[++i]); |
100 |
greg |
1.1 |
break; |
101 |
gregl |
2.9 |
case 'x': /* XYZE Radiance output */ |
102 |
|
|
TGL(C_XYZE); |
103 |
greg |
1.4 |
break; |
104 |
gregl |
2.9 |
case 'z': /* LZW compressed output */ |
105 |
|
|
cvts.comp = COMPRESSION_LZW; |
106 |
greg |
1.5 |
break; |
107 |
gregl |
2.9 |
case 'L': /* LogLuv 32-bit output */ |
108 |
|
|
cvts.comp = COMPRESSION_LOGLUV32; |
109 |
|
|
break; |
110 |
|
|
case 'l': /* LogLuv 24-bit output */ |
111 |
|
|
cvts.comp = COMPRESSION_LOGLUV24; |
112 |
|
|
break; |
113 |
|
|
case 'b': /* greyscale output? */ |
114 |
|
|
TGL(C_GRY); |
115 |
|
|
break; |
116 |
|
|
case 'e': /* exposure adjustment */ |
117 |
greg |
1.1 |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
118 |
|
|
goto userr; |
119 |
gregl |
2.9 |
cvts.bradj = atoi(argv[++i]); |
120 |
greg |
1.1 |
break; |
121 |
gregl |
2.9 |
case 'r': /* reverse conversion? */ |
122 |
greg |
1.1 |
reverse = !reverse; |
123 |
|
|
break; |
124 |
|
|
case '\0': |
125 |
|
|
goto doneopts; |
126 |
|
|
default: |
127 |
|
|
goto userr; |
128 |
|
|
} |
129 |
|
|
else |
130 |
|
|
break; |
131 |
|
|
doneopts: |
132 |
gregl |
2.9 |
if (reverse) { |
133 |
greg |
1.1 |
|
134 |
|
|
if (i != argc-2 && i != argc-1) |
135 |
|
|
goto userr; |
136 |
gregl |
2.9 |
|
137 |
|
|
tiff2ra(i, argv); |
138 |
|
|
|
139 |
|
|
} else { |
140 |
|
|
|
141 |
greg |
1.1 |
if (i != argc-2) |
142 |
|
|
goto userr; |
143 |
|
|
|
144 |
gregl |
2.9 |
if (CHK(C_GRY) && (cvts.comp == COMPRESSION_LOGLUV24 || |
145 |
|
|
cvts.comp == COMPRESSION_LOGLUV32)) |
146 |
|
|
cvts.comp = COMPRESSION_LOGL16; |
147 |
|
|
|
148 |
|
|
ra2tiff(i, argv); |
149 |
|
|
} |
150 |
|
|
|
151 |
greg |
1.1 |
exit(0); |
152 |
|
|
userr: |
153 |
greg |
1.4 |
fprintf(stderr, |
154 |
gregl |
2.9 |
"Usage: %s [-z|-L|-l][-b][-e +/-stops][-g gamma] {in.pic|-} out.tif\n", |
155 |
greg |
1.1 |
progname); |
156 |
gregl |
2.9 |
fprintf(stderr, |
157 |
|
|
" Or: %s -r [-x][-e +/-stops][-g gamma] in.tif [out.pic|-]\n", |
158 |
|
|
progname); |
159 |
greg |
1.1 |
exit(1); |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
|
163 |
|
|
quiterr(err) /* print message and exit */ |
164 |
|
|
char *err; |
165 |
|
|
{ |
166 |
|
|
if (err != NULL) { |
167 |
|
|
fprintf(stderr, "%s: %s\n", progname, err); |
168 |
|
|
exit(1); |
169 |
|
|
} |
170 |
|
|
exit(0); |
171 |
|
|
} |
172 |
|
|
|
173 |
|
|
|
174 |
gregl |
2.9 |
allocbufs() /* allocate scanline buffers */ |
175 |
greg |
1.1 |
{ |
176 |
gregl |
2.9 |
int rsiz, tsiz; |
177 |
|
|
|
178 |
|
|
rsiz = CHK(C_RFLT) ? sizeof(COLOR) : sizeof(COLR); |
179 |
|
|
tsiz = (CHK(C_TFLT) ? sizeof(float) : sizeof(uint8)) * |
180 |
|
|
(CHK(C_GRY) ? 1 : 3); |
181 |
|
|
cvts.r.p = (char *)malloc(rsiz*cvts.xmax); |
182 |
|
|
cvts.t.p = (char *)malloc(tsiz*cvts.xmax); |
183 |
|
|
if (cvts.r.p == NULL | cvts.t.p == NULL) |
184 |
|
|
quiterr("no memory to allocate scanline buffers"); |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
|
188 |
|
|
initfromtif() /* initialize conversion from TIFF input */ |
189 |
|
|
{ |
190 |
|
|
uint16 hi; |
191 |
|
|
float *fa, f1, f2; |
192 |
|
|
|
193 |
|
|
CLR(C_GRY|C_GAMMA|C_PRIM|C_RFLT|C_TFLT|C_CXFM); |
194 |
|
|
|
195 |
|
|
TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_PLANARCONFIG, &cvts.pconf); |
196 |
|
|
|
197 |
|
|
if (TIFFGetField(cvts.tif, TIFFTAG_PRIMARYCHROMATICITIES, &fa)) { |
198 |
|
|
cvts.prims[RED][CIEX] = fa[0]; |
199 |
|
|
cvts.prims[RED][CIEY] = fa[1]; |
200 |
|
|
cvts.prims[GRN][CIEX] = fa[2]; |
201 |
|
|
cvts.prims[GRN][CIEY] = fa[3]; |
202 |
|
|
cvts.prims[BLU][CIEX] = fa[4]; |
203 |
|
|
cvts.prims[BLU][CIEY] = fa[5]; |
204 |
|
|
cvts.prims[WHT][CIEX] = 1./3.; |
205 |
|
|
cvts.prims[WHT][CIEY] = 1./3.; |
206 |
|
|
if (TIFFGetField(cvts.tif, TIFFTAG_WHITEPOINT, &fa)) { |
207 |
|
|
cvts.prims[WHT][CIEX] = fa[0]; |
208 |
|
|
cvts.prims[WHT][CIEY] = fa[1]; |
209 |
|
|
} |
210 |
|
|
SET(C_PRIM); |
211 |
|
|
} |
212 |
|
|
|
213 |
|
|
if (!TIFFGetField(cvts.tif, TIFFTAG_COMPRESSION, &cvts.comp)) |
214 |
|
|
cvts.comp = COMPRESSION_NONE; |
215 |
|
|
|
216 |
|
|
if (TIFFGetField(cvts.tif, TIFFTAG_XRESOLUTION, &f1) && |
217 |
|
|
TIFFGetField(cvts.tif, TIFFTAG_YRESOLUTION, &f2)) |
218 |
|
|
cvts.pixrat = f1/f2; |
219 |
|
|
else |
220 |
|
|
cvts.pixrat = 1.; |
221 |
|
|
|
222 |
|
|
if (!TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_ORIENTATION, &cvts.orient)) |
223 |
|
|
cvts.orient = 1; |
224 |
|
|
|
225 |
|
|
switch (cvts.comp) { |
226 |
|
|
case COMPRESSION_LOGL16: |
227 |
|
|
SET(C_GRY|C_RFLT|C_TFLT|C_GAMUT); |
228 |
|
|
cvts.pconf = PLANARCONFIG_CONTIG; |
229 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_LOGLUVDATAFMT, |
230 |
|
|
LOGLUVDATAFMT_FLTY); |
231 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT, |
232 |
|
|
SAMPLEFORMAT_IEEEFP); |
233 |
|
|
cvts.tf = L2Color; |
234 |
|
|
break; |
235 |
|
|
case COMPRESSION_LOGLUV24: |
236 |
|
|
case COMPRESSION_LOGLUV32: |
237 |
|
|
SET(C_RFLT|C_TFLT); |
238 |
|
|
if (!CHK(C_XYZE)) { |
239 |
|
|
cpcolormat(cvts.cmat, xyz2rgbmat); |
240 |
|
|
SET(C_CXFM|C_GAMUT); |
241 |
|
|
} else if (cvts.comp == COMPRESSION_LOGLUV32) |
242 |
|
|
SET(C_GAMUT); |
243 |
|
|
if (cvts.pconf != PLANARCONFIG_CONTIG) |
244 |
|
|
quiterr("cannot handle separate Luv planes"); |
245 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_LOGLUVDATAFMT, |
246 |
|
|
LOGLUVDATAFMT_FLTXYZ); |
247 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT, |
248 |
|
|
SAMPLEFORMAT_IEEEFP); |
249 |
|
|
cvts.tf = Luv2Color; |
250 |
|
|
break; |
251 |
|
|
default: |
252 |
|
|
SET(C_GAMMA|C_GAMUT); |
253 |
|
|
setcolrgam(cvts.gamcor); |
254 |
|
|
if (CHK(C_XYZE)) { |
255 |
|
|
comprgb2xyzmat(cvts.cmat, |
256 |
|
|
CHK(C_PRIM) ? cvts.prims : stdprims); |
257 |
|
|
SET(C_CXFM); |
258 |
|
|
} |
259 |
|
|
if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) || |
260 |
|
|
(hi != 1 && hi != 3)) |
261 |
|
|
quiterr("unsupported samples per pixel"); |
262 |
|
|
if (hi == 1) { |
263 |
|
|
SET(C_GRY); |
264 |
|
|
cvts.pconf = PLANARCONFIG_CONTIG; |
265 |
|
|
cvts.tf = Gry2Colr; |
266 |
|
|
} else |
267 |
|
|
cvts.tf = RGB2Colr; |
268 |
|
|
if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi) || |
269 |
|
|
hi != 8) |
270 |
|
|
quiterr("unsupported bits per sample"); |
271 |
|
|
break; |
272 |
|
|
} |
273 |
|
|
|
274 |
|
|
if (TIFFGetField(cvts.tif, TIFFTAG_PHOTOMETRIC, &hi) && |
275 |
|
|
hi != (CHK(C_GRY) ? PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_RGB)) |
276 |
greg |
1.1 |
quiterr("unsupported photometric interpretation"); |
277 |
gregl |
2.9 |
|
278 |
|
|
if (!TIFFGetField(cvts.tif, TIFFTAG_IMAGEWIDTH, &cvts.xmax) || |
279 |
|
|
!TIFFGetField(cvts.tif, TIFFTAG_IMAGELENGTH, &cvts.ymax)) |
280 |
greg |
1.1 |
quiterr("unknown input image resolution"); |
281 |
gregl |
2.9 |
|
282 |
|
|
if (!TIFFGetField(cvts.tif, TIFFTAG_STONITS, &cvts.stonits)) |
283 |
|
|
cvts.stonits = 1.; |
284 |
|
|
/* add to Radiance header */ |
285 |
|
|
if (cvts.pixrat < .99 || cvts.pixrat > 1.01) |
286 |
|
|
fputaspect(cvts.pixrat, cvts.rfp); |
287 |
|
|
if (CHK(C_XYZE)) { |
288 |
|
|
fputexpos(pow(2.,(double)cvts.bradj)/cvts.stonits, cvts.rfp); |
289 |
|
|
fputformat(CIEFMT, cvts.rfp); |
290 |
|
|
} else { |
291 |
|
|
if (CHK(C_PRIM)) |
292 |
|
|
fputprims(cvts.prims, cvts.rfp); |
293 |
|
|
fputexpos(WHTEFFICACY*pow(2.,(double)cvts.bradj)/cvts.stonits, |
294 |
|
|
cvts.rfp); |
295 |
|
|
fputformat(COLRFMT, cvts.rfp); |
296 |
|
|
} |
297 |
|
|
|
298 |
|
|
allocbufs(); /* allocate scanline buffers */ |
299 |
|
|
} |
300 |
|
|
|
301 |
|
|
|
302 |
|
|
tiff2ra(ac, av) /* convert TIFF image to Radiance picture */ |
303 |
|
|
int ac; |
304 |
|
|
char *av[]; |
305 |
|
|
{ |
306 |
|
|
int32 y; |
307 |
|
|
/* open TIFF input */ |
308 |
|
|
if ((cvts.tif = TIFFOpen(av[ac], "r")) == NULL) |
309 |
|
|
quiterr("cannot open TIFF input"); |
310 |
|
|
/* open Radiance output */ |
311 |
|
|
if (av[ac+1] == NULL || !strcmp(av[ac+1], "-")) |
312 |
|
|
cvts.rfp = stdout; |
313 |
|
|
else if ((cvts.rfp = fopen(av[ac+1], "w")) == NULL) |
314 |
|
|
quiterr("cannot open Radiance output picture"); |
315 |
|
|
/* start output header */ |
316 |
|
|
newheader("RADIANCE", cvts.rfp); |
317 |
|
|
printargs(ac, av, cvts.rfp); |
318 |
|
|
|
319 |
|
|
initfromtif(); /* initialize conversion */ |
320 |
|
|
|
321 |
|
|
fputc('\n', cvts.rfp); /* finish Radiance header */ |
322 |
|
|
fputresolu(pixorder(), (int)cvts.xmax, (int)cvts.ymax, cvts.rfp); |
323 |
|
|
|
324 |
|
|
for (y = 0; y < cvts.ymax; y++) /* convert image */ |
325 |
|
|
(*cvts.tf)(y); |
326 |
|
|
/* clean up */ |
327 |
|
|
fclose(cvts.rfp); |
328 |
|
|
TIFFClose(cvts.tif); |
329 |
|
|
} |
330 |
|
|
|
331 |
|
|
|
332 |
|
|
int |
333 |
|
|
headline(s) /* process Radiance input header line */ |
334 |
|
|
char *s; |
335 |
|
|
{ |
336 |
|
|
char fmt[32]; |
337 |
|
|
|
338 |
|
|
if (formatval(fmt, s)) { |
339 |
|
|
if (!strcmp(fmt, COLRFMT)) |
340 |
|
|
CLR(C_XYZE); |
341 |
|
|
else if (!strcmp(fmt, CIEFMT)) |
342 |
|
|
SET(C_XYZE); |
343 |
|
|
else |
344 |
|
|
quiterr("unrecognized input picture format"); |
345 |
|
|
return; |
346 |
|
|
} |
347 |
|
|
if (isexpos(s)) { |
348 |
|
|
cvts.stonits /= exposval(s); |
349 |
|
|
return; |
350 |
|
|
} |
351 |
|
|
if (isaspect(s)) { |
352 |
|
|
cvts.pixrat *= aspectval(s); |
353 |
|
|
return; |
354 |
|
|
} |
355 |
|
|
if (isprims(s)) { |
356 |
|
|
primsval(cvts.prims, s); |
357 |
|
|
SET(C_PRIM); |
358 |
|
|
return; |
359 |
|
|
} |
360 |
|
|
} |
361 |
|
|
|
362 |
|
|
|
363 |
|
|
initfromrad() /* initialize input from a Radiance picture */ |
364 |
|
|
{ |
365 |
|
|
int i1, i2, po; |
366 |
|
|
/* read Radiance header */ |
367 |
|
|
CLR(C_RFLT|C_TFLT|C_XYZE|C_PRIM|C_GAMMA|C_CXFM); |
368 |
|
|
cvts.stonits = 1.; |
369 |
|
|
cvts.pixrat = 1.; |
370 |
|
|
cvts.pconf = PLANARCONFIG_CONTIG; |
371 |
|
|
getheader(cvts.rfp, headline, NULL); |
372 |
|
|
if ((po = fgetresolu(&i1, &i2, cvts.rfp)) < 0) |
373 |
|
|
quiterr("bad Radiance picture"); |
374 |
|
|
cvts.xmax = i1; cvts.ymax = i2; |
375 |
|
|
for (i1 = 0; i1 < 8; i1++) /* interpret orientation */ |
376 |
|
|
if (ortab[i1] == po) { |
377 |
|
|
cvts.orient = i1 + 1; |
378 |
|
|
break; |
379 |
greg |
1.1 |
} |
380 |
gregl |
2.9 |
if (i1 >= 8) |
381 |
|
|
quiterr("internal error 1 in initfromrad"); |
382 |
|
|
if (!(po & YMAJOR)) |
383 |
|
|
cvts.pixrat = 1./cvts.pixrat; |
384 |
|
|
if (!CHK(C_XYZE)) |
385 |
|
|
cvts.stonits *= WHTEFFICACY; |
386 |
|
|
/* set up conversion */ |
387 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_COMPRESSION, cvts.comp); |
388 |
|
|
|
389 |
|
|
switch (cvts.comp) { |
390 |
|
|
case COMPRESSION_LOGL16: |
391 |
|
|
SET(C_GRY|C_RFLT|C_TFLT); |
392 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_LOGLUVDATAFMT, |
393 |
|
|
LOGLUVDATAFMT_FLTY); |
394 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT, |
395 |
|
|
SAMPLEFORMAT_IEEEFP); |
396 |
|
|
cvts.tf = Color2L; |
397 |
|
|
break; |
398 |
|
|
case COMPRESSION_LOGLUV24: |
399 |
|
|
case COMPRESSION_LOGLUV32: |
400 |
|
|
SET(C_RFLT|C_TFLT); |
401 |
|
|
CLR(C_GRY); |
402 |
|
|
if (!CHK(C_XYZE)) { |
403 |
|
|
cpcolormat(cvts.cmat, rgb2xyzmat); |
404 |
|
|
SET(C_CXFM); |
405 |
|
|
} |
406 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_LOGLUVDATAFMT, |
407 |
|
|
LOGLUVDATAFMT_FLTXYZ); |
408 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT, |
409 |
|
|
SAMPLEFORMAT_IEEEFP); |
410 |
|
|
cvts.tf = Color2Luv; |
411 |
|
|
break; |
412 |
|
|
default: |
413 |
|
|
SET(C_GAMMA|C_GAMUT); |
414 |
|
|
setcolrgam(cvts.gamcor); |
415 |
|
|
if (CHK(C_XYZE)) { |
416 |
|
|
compxyz2rgbmat(cvts.cmat, |
417 |
|
|
CHK(C_PRIM) ? cvts.prims : stdprims); |
418 |
|
|
SET(C_CXFM); |
419 |
|
|
} |
420 |
|
|
if (CHK(C_GRY)) |
421 |
|
|
cvts.tf = Colr2Gry; |
422 |
|
|
else |
423 |
|
|
cvts.tf = Colr2RGB; |
424 |
|
|
if (CHK(C_PRIM)) { |
425 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_PRIMARYCHROMATICITIES, |
426 |
|
|
(float *)cvts.prims); |
427 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_WHITEPOINT, |
428 |
|
|
(float *)cvts.prims[WHT]); |
429 |
|
|
} |
430 |
|
|
break; |
431 |
greg |
1.1 |
} |
432 |
gregl |
2.9 |
/* set other TIFF fields */ |
433 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_IMAGEWIDTH, cvts.xmax); |
434 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_IMAGELENGTH, cvts.ymax); |
435 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, CHK(C_GRY) ? 1 : 3); |
436 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, CHK(C_TFLT) ? 32 : 8); |
437 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_PHOTOMETRIC, |
438 |
|
|
CHK(C_GRY) ? PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_RGB); |
439 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_XRESOLUTION, 72.); |
440 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_YRESOLUTION, 72./cvts.pixrat); |
441 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_ORIENTATION, cvts.orient); |
442 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_RESOLUTIONUNIT, 2); |
443 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_PLANARCONFIG, cvts.pconf); |
444 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_STONITS, |
445 |
|
|
cvts.stonits/pow(2.,(double)cvts.bradj)); |
446 |
|
|
i1 = TIFFScanlineSize(cvts.tif); |
447 |
|
|
i2 = 8192/i1; /* compute good strip size */ |
448 |
|
|
if (i2 < 1) i2 = 1; |
449 |
|
|
TIFFSetField(cvts.tif, TIFFTAG_ROWSPERSTRIP, (uint32)i2); |
450 |
|
|
|
451 |
|
|
allocbufs(); /* allocate scanline buffers */ |
452 |
|
|
} |
453 |
|
|
|
454 |
|
|
|
455 |
|
|
ra2tiff(ac, av) /* convert Radiance picture to TIFF image */ |
456 |
|
|
int ac; |
457 |
|
|
char *av[]; |
458 |
|
|
{ |
459 |
|
|
uint32 y; |
460 |
|
|
/* open Radiance file */ |
461 |
|
|
if (!strcmp(av[ac], "-")) |
462 |
|
|
cvts.rfp = stdin; |
463 |
|
|
else if ((cvts.rfp = fopen(av[ac], "r")) == NULL) |
464 |
|
|
quiterr("cannot open Radiance input picture"); |
465 |
|
|
/* open TIFF file */ |
466 |
|
|
if ((cvts.tif = TIFFOpen(av[ac+1], "w")) == NULL) |
467 |
|
|
quiterr("cannot open TIFF output"); |
468 |
|
|
|
469 |
|
|
initfromrad(); /* initialize conversion */ |
470 |
|
|
|
471 |
|
|
for (y = 0; y < cvts.ymax; y++) /* convert image */ |
472 |
|
|
(*cvts.tf)(y); |
473 |
greg |
1.1 |
/* clean up */ |
474 |
gregl |
2.9 |
TIFFClose(cvts.tif); |
475 |
|
|
fclose(cvts.rfp); |
476 |
|
|
} |
477 |
|
|
|
478 |
|
|
|
479 |
|
|
int |
480 |
|
|
Luv2Color(y) /* read/convert/write Luv->COLOR scanline */ |
481 |
|
|
uint32 y; |
482 |
|
|
{ |
483 |
|
|
register int x; |
484 |
|
|
|
485 |
|
|
if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | CHK(C_GRY)) |
486 |
|
|
quiterr("internal error 1 in Luv2Color"); |
487 |
|
|
|
488 |
|
|
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
489 |
|
|
quiterr("error reading TIFF input"); |
490 |
|
|
|
491 |
|
|
for (x = cvts.xmax; x--; ) { |
492 |
|
|
cvts.r.colors[x][RED] = cvts.t.fp[3*x]; |
493 |
|
|
cvts.r.colors[x][GRN] = cvts.t.fp[3*x + 1]; |
494 |
|
|
cvts.r.colors[x][BLU] = cvts.t.fp[3*x + 2]; |
495 |
|
|
if (CHK(C_CXFM)) |
496 |
|
|
colortrans(cvts.r.colors[x], cvts.cmat, |
497 |
|
|
cvts.r.colors[x]); |
498 |
|
|
if (CHK(C_GAMUT)) |
499 |
|
|
clipgamut(cvts.r.colors[x], cvts.t.fp[3*x + 1], |
500 |
|
|
CGAMUT_LOWER, cblack, cwhite); |
501 |
|
|
} |
502 |
|
|
if (cvts.bradj) { |
503 |
|
|
double m = pow(2.,(double)cvts.bradj); |
504 |
|
|
for (x = cvts.xmax; x--; ) |
505 |
|
|
scalecolor(cvts.r.colors[x], m); |
506 |
|
|
} |
507 |
|
|
|
508 |
|
|
if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) |
509 |
|
|
quiterr("error writing Radiance picture"); |
510 |
|
|
} |
511 |
|
|
|
512 |
|
|
|
513 |
|
|
int |
514 |
|
|
L2Color(y) /* read/convert/write L16->COLOR scanline */ |
515 |
|
|
uint32 y; |
516 |
|
|
{ |
517 |
|
|
register int x; |
518 |
|
|
|
519 |
|
|
if (CHK(C_RFLT|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY)) |
520 |
|
|
quiterr("internal error 1 in L2Color"); |
521 |
|
|
|
522 |
|
|
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
523 |
|
|
quiterr("error reading TIFF input"); |
524 |
|
|
|
525 |
|
|
for (x = cvts.xmax; x--; ) |
526 |
|
|
cvts.r.colors[x][RED] = |
527 |
|
|
cvts.r.colors[x][GRN] = |
528 |
|
|
cvts.r.colors[x][BLU] = cvts.t.fp[x] > 0. ? cvts.t.fp[x] : 0.; |
529 |
|
|
|
530 |
|
|
if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) |
531 |
|
|
quiterr("error writing Radiance picture"); |
532 |
|
|
} |
533 |
|
|
|
534 |
|
|
|
535 |
|
|
int |
536 |
|
|
RGB2Colr(y) /* read/convert/write RGB->COLR scanline */ |
537 |
|
|
uint32 y; |
538 |
|
|
{ |
539 |
|
|
COLOR ctmp; |
540 |
|
|
register int x; |
541 |
|
|
|
542 |
|
|
if (CHK(C_RFLT|C_TFLT|C_GRY)) |
543 |
|
|
quiterr("internal error 1 in RGB2Colr"); |
544 |
|
|
|
545 |
|
|
if (cvts.pconf == PLANARCONFIG_CONTIG) { |
546 |
|
|
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
547 |
|
|
goto readerr; |
548 |
|
|
for (x = cvts.xmax; x--; ) { |
549 |
|
|
cvts.r.colrs[x][RED] = cvts.t.bp[3*x]; |
550 |
|
|
cvts.r.colrs[x][GRN] = cvts.t.bp[3*x + 1]; |
551 |
|
|
cvts.r.colrs[x][BLU] = cvts.t.bp[3*x + 2]; |
552 |
|
|
} |
553 |
|
|
} else { |
554 |
|
|
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
555 |
|
|
goto readerr; |
556 |
|
|
if (TIFFReadScanline(cvts.tif, |
557 |
|
|
(tidata_t)(cvts.t.bp + cvts.xmax), y, 1) < 0) |
558 |
|
|
goto readerr; |
559 |
|
|
if (TIFFReadScanline(cvts.tif, |
560 |
|
|
(tidata_t)(cvts.t.bp + 2*cvts.xmax), y, 2) < 0) |
561 |
|
|
goto readerr; |
562 |
|
|
for (x = cvts.xmax; x--; ) { |
563 |
|
|
cvts.r.colrs[x][RED] = cvts.t.bp[x]; |
564 |
|
|
cvts.r.colrs[x][GRN] = cvts.t.bp[cvts.xmax + x]; |
565 |
|
|
cvts.r.colrs[x][BLU] = cvts.t.bp[2*cvts.xmax + x]; |
566 |
|
|
} |
567 |
|
|
} |
568 |
|
|
|
569 |
|
|
gambs_colrs(cvts.r.colrs, cvts.xmax); |
570 |
|
|
if (CHK(C_CXFM)) |
571 |
|
|
for (x = cvts.xmax; x--; ) { |
572 |
|
|
colr_color(ctmp, cvts.r.colrs[x]); |
573 |
|
|
colortrans(ctmp, cvts.cmat, ctmp); |
574 |
|
|
if (CHK(C_GAMUT)) /* !CHK(C_XYZE) */ |
575 |
|
|
clipgamut(ctmp, bright(ctmp), CGAMUT_LOWER, |
576 |
|
|
cblack, cwhite); |
577 |
|
|
setcolr(cvts.r.colrs[x], colval(ctmp,RED), |
578 |
|
|
colval(ctmp,GRN), colval(ctmp,BLU)); |
579 |
|
|
} |
580 |
|
|
if (cvts.bradj) |
581 |
|
|
shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj); |
582 |
|
|
|
583 |
|
|
if (fwritecolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0) |
584 |
|
|
quiterr("error writing Radiance picture"); |
585 |
greg |
1.1 |
return; |
586 |
|
|
readerr: |
587 |
|
|
quiterr("error reading TIFF input"); |
588 |
|
|
} |
589 |
|
|
|
590 |
|
|
|
591 |
gregl |
2.9 |
int |
592 |
|
|
Gry2Colr(y) /* read/convert/write G8->COLR scanline */ |
593 |
|
|
uint32 y; |
594 |
greg |
1.1 |
{ |
595 |
|
|
register int x; |
596 |
gregl |
2.9 |
|
597 |
|
|
if (CHK(C_RFLT|C_TFLT) | !CHK(C_GRY)) |
598 |
|
|
quiterr("internal error 1 in Gry2Colr"); |
599 |
|
|
|
600 |
|
|
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
601 |
|
|
quiterr("error reading TIFF input"); |
602 |
|
|
|
603 |
|
|
for (x = cvts.xmax; x--; ) |
604 |
|
|
cvts.r.colrs[x][RED] = |
605 |
|
|
cvts.r.colrs[x][GRN] = |
606 |
|
|
cvts.r.colrs[x][BLU] = cvts.t.bp[x]; |
607 |
|
|
|
608 |
|
|
gambs_colrs(cvts.r.colrs, cvts.xmax); |
609 |
|
|
if (cvts.bradj) |
610 |
|
|
shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj); |
611 |
|
|
|
612 |
|
|
if (fwritecolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0) |
613 |
|
|
quiterr("error writing Radiance picture"); |
614 |
|
|
} |
615 |
|
|
|
616 |
|
|
|
617 |
|
|
int |
618 |
|
|
Color2L(y) /* read/convert/write COLOR->L16 scanline */ |
619 |
|
|
uint32 y; |
620 |
|
|
{ |
621 |
|
|
double m = pow(2.,(double)cvts.bradj); |
622 |
|
|
register int x; |
623 |
|
|
|
624 |
|
|
if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | !CHK(C_GRY)) |
625 |
|
|
quiterr("internal error 1 in Color2L"); |
626 |
|
|
|
627 |
|
|
if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) |
628 |
|
|
quiterr("error reading Radiance picture"); |
629 |
|
|
|
630 |
|
|
for (x = cvts.xmax; x--; ) |
631 |
|
|
cvts.t.fp[x] = m*( CHK(C_XYZE) ? colval(cvts.r.colors[x],CIEY) |
632 |
|
|
: bright(cvts.r.colors[x]) ); |
633 |
|
|
|
634 |
|
|
if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
635 |
|
|
quiterr("error writing TIFF output"); |
636 |
|
|
} |
637 |
|
|
|
638 |
|
|
|
639 |
|
|
int |
640 |
|
|
Color2Luv(y) /* read/convert/write COLOR->Luv scanline */ |
641 |
|
|
uint32 y; |
642 |
|
|
{ |
643 |
|
|
register int x; |
644 |
|
|
|
645 |
|
|
if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | CHK(C_GRY)) |
646 |
|
|
quiterr("internal error 1 in Color2Luv"); |
647 |
|
|
|
648 |
|
|
if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) |
649 |
|
|
quiterr("error reading Radiance picture"); |
650 |
|
|
|
651 |
|
|
if (CHK(C_CXFM)) |
652 |
|
|
for (x = cvts.xmax; x--; ) |
653 |
|
|
colortrans(cvts.r.colors[x], cvts.cmat, |
654 |
|
|
cvts.r.colors[x]); |
655 |
|
|
if (cvts.bradj) { |
656 |
|
|
double m = pow(2.,(double)cvts.bradj); |
657 |
|
|
for (x = cvts.xmax; x--; ) |
658 |
|
|
scalecolor(cvts.r.colors[x], m); |
659 |
|
|
} |
660 |
|
|
|
661 |
|
|
for (x = cvts.xmax; x--; ) { |
662 |
|
|
cvts.t.fp[3*x] = colval(cvts.r.colors[x],RED); |
663 |
|
|
cvts.t.fp[3*x+1] = colval(cvts.r.colors[x],GRN); |
664 |
|
|
cvts.t.fp[3*x+2] = colval(cvts.r.colors[x],BLU); |
665 |
|
|
} |
666 |
|
|
|
667 |
|
|
if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
668 |
|
|
quiterr("error writing TIFF output"); |
669 |
|
|
} |
670 |
|
|
|
671 |
|
|
|
672 |
|
|
int |
673 |
|
|
Colr2Gry(y) /* read/convert/write COLR->RGB scanline */ |
674 |
|
|
uint32 y; |
675 |
|
|
{ |
676 |
|
|
register int x; |
677 |
|
|
|
678 |
|
|
if (CHK(C_RFLT|C_TFLT) | !CHK(C_GRY)) |
679 |
|
|
quiterr("internal error 1 in Colr2Gry"); |
680 |
|
|
|
681 |
|
|
if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0) |
682 |
|
|
quiterr("error reading Radiance picture"); |
683 |
|
|
|
684 |
|
|
if (cvts.bradj) |
685 |
|
|
shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj); |
686 |
|
|
for (x = cvts.xmax; x--; ) |
687 |
|
|
colval(cvts.r.colrs[x],CIEY) = normbright(cvts.r.colrs[x]); |
688 |
|
|
colrs_gambs(cvts.r.colrs, cvts.xmax); |
689 |
|
|
|
690 |
|
|
for (x = cvts.xmax; x--; ) |
691 |
|
|
cvts.t.bp[x] = colval(cvts.r.colrs[x],CIEY); |
692 |
|
|
|
693 |
|
|
if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
694 |
|
|
quiterr("error writing TIFF output"); |
695 |
|
|
} |
696 |
|
|
|
697 |
|
|
|
698 |
|
|
int |
699 |
|
|
Colr2RGB(y) /* read/convert/write COLR->RGB scanline */ |
700 |
|
|
uint32 y; |
701 |
|
|
{ |
702 |
|
|
COLOR ctmp; |
703 |
|
|
register int x; |
704 |
|
|
|
705 |
|
|
if (CHK(C_RFLT|C_TFLT|C_GRY)) |
706 |
|
|
quiterr("internal error 1 in Colr2RGB"); |
707 |
|
|
|
708 |
|
|
if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0) |
709 |
|
|
quiterr("error reading Radiance picture"); |
710 |
|
|
|
711 |
|
|
if (cvts.bradj) |
712 |
|
|
shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj); |
713 |
|
|
if (CHK(C_CXFM)) |
714 |
|
|
for (x = cvts.xmax; x--; ) { |
715 |
|
|
colr_color(ctmp, cvts.r.colrs[x]); |
716 |
|
|
colortrans(ctmp, cvts.cmat, ctmp); |
717 |
|
|
if (CHK(C_GAMUT)) |
718 |
|
|
clipgamut(ctmp, bright(ctmp), CGAMUT, |
719 |
|
|
cblack, cwhite); |
720 |
|
|
setcolr(cvts.r.colrs[x], colval(ctmp,RED), |
721 |
|
|
colval(ctmp,GRN), colval(ctmp,BLU)); |
722 |
greg |
1.1 |
} |
723 |
gregl |
2.9 |
colrs_gambs(cvts.r.colrs, cvts.xmax); |
724 |
|
|
|
725 |
|
|
for (x = cvts.xmax; x--; ) { |
726 |
|
|
cvts.t.bp[3*x] = cvts.r.colrs[x][RED]; |
727 |
|
|
cvts.t.bp[3*x+1] = cvts.r.colrs[x][GRN]; |
728 |
|
|
cvts.t.bp[3*x+2] = cvts.r.colrs[x][BLU]; |
729 |
greg |
1.1 |
} |
730 |
gregl |
2.9 |
|
731 |
|
|
if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
732 |
|
|
quiterr("error writing TIFF output"); |
733 |
greg |
1.1 |
} |