1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: ra_tiff.c,v 2.38 2024/10/04 19:15:19 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Program to convert between RADIANCE and TIFF files. |
6 |
* Added LogLuv encodings 7/97 (GWL). |
7 |
* Added white-balance adjustment 10/01 (GW). |
8 |
*/ |
9 |
|
10 |
#include <math.h> |
11 |
#include <ctype.h> |
12 |
#include "paths.h" |
13 |
#include "rtio.h" |
14 |
#include "platform.h" |
15 |
#include "tiffio.h" |
16 |
#include "color.h" |
17 |
#include "resolu.h" |
18 |
|
19 |
#define GAMCOR 2.2 /* default gamma */ |
20 |
|
21 |
/* conversion flags */ |
22 |
#define C_CXFM 0x1 /* needs color transformation */ |
23 |
#define C_GAMUT 0x2 /* needs gamut mapping */ |
24 |
#define C_GAMMA 0x4 /* needs gamma correction */ |
25 |
#define C_GRY 0x8 /* TIFF is greyscale */ |
26 |
#define C_XYZE 0x10 /* Radiance is XYZE */ |
27 |
#define C_SPEC 0x20 /* Radiance is spectral */ |
28 |
#define C_RFLT 0x40 /* Radiance data is float */ |
29 |
#define C_TFLT 0x80 /* TIFF data is float */ |
30 |
#define C_TWRD 0x100 /* TIFF data is 16-bit */ |
31 |
#define C_PRIM 0x200 /* has assigned primaries */ |
32 |
|
33 |
typedef void colcvf_t(uint32 y); |
34 |
|
35 |
struct CONVST { |
36 |
uint16 flags; /* conversion flags (defined above) */ |
37 |
char capdate[20]; /* capture date/time */ |
38 |
char owner[256]; /* content owner */ |
39 |
uint16 comp; /* TIFF compression type */ |
40 |
uint16 phot; /* TIFF photometric type */ |
41 |
uint16 pconf; /* TIFF planar configuration */ |
42 |
float gamcor; /* gamma correction value */ |
43 |
short bradj; /* Radiance exposure adjustment (stops) */ |
44 |
uint16 orient; /* visual orientation (TIFF spec.) */ |
45 |
double stonits; /* input conversion to nits */ |
46 |
float pixrat; /* pixel aspect ratio */ |
47 |
int ncc; /* # color components for spectral */ |
48 |
float wpt[4]; /* wavelength partitions */ |
49 |
FILE *rfp; /* Radiance stream pointer */ |
50 |
TIFF *tif; /* TIFF pointer */ |
51 |
uint32 xmax, ymax; /* image dimensions */ |
52 |
COLORMAT cmat; /* color transformation matrix */ |
53 |
RGBPRIMS prims; /* RGB primaries */ |
54 |
union { |
55 |
COLR *colrs; /* 4-byte ???E pointer */ |
56 |
COLOR *colors; /* float array pointer */ |
57 |
void *p; /* generic pointer */ |
58 |
} r; /* Radiance scanline */ |
59 |
union { |
60 |
uint8 *bp; /* byte pointer */ |
61 |
uint16 *wp; /* word pointer */ |
62 |
float *fp; /* float pointer */ |
63 |
void *p; /* generic pointer */ |
64 |
} t; /* TIFF scanline */ |
65 |
colcvf_t *tf; /* translation procedure */ |
66 |
} cvts = { /* conversion structure */ |
67 |
0, "", "", COMPRESSION_NONE, PHOTOMETRIC_RGB, |
68 |
PLANARCONFIG_CONTIG, GAMCOR, 0, 1, 1., 1., 3, |
69 |
}; |
70 |
|
71 |
#define CHK(f) (cvts.flags & (f)) |
72 |
#define SET(f) (cvts.flags |= (f)) |
73 |
#define CLR(f) (cvts.flags &= ~(f)) |
74 |
#define TGL(f) (cvts.flags ^= (f)) |
75 |
|
76 |
static colcvf_t Luv2Color, L2Color, RGB2Colr, Gry2Colr; |
77 |
static colcvf_t Color2Luv, Color2L, Colr2RGB, Colr2Gry; |
78 |
static colcvf_t RRGGBB2Color, GGry2Color, Color2RRGGBB, Color2GGry; |
79 |
|
80 |
static gethfunc headline; |
81 |
static void quiterr(char *err); |
82 |
static void allocbufs(void); |
83 |
static void initfromtif(void); |
84 |
static void tiff2ra(int ac, char *av[]); |
85 |
static void initfromrad(void); |
86 |
static void ra2tiff(int ac, char *av[]); |
87 |
static void ReadRadScan(struct CONVST *cvp); |
88 |
|
89 |
|
90 |
#define RfGfBf2Color Luv2Color |
91 |
#define Gryf2Color L2Color |
92 |
#define Color2Gryf Color2L |
93 |
#define Color2RfGfBf Color2Luv |
94 |
|
95 |
short ortab[8] = { /* orientation conversion table */ |
96 |
YMAJOR|YDECR, |
97 |
YMAJOR|YDECR|XDECR, |
98 |
YMAJOR|XDECR, |
99 |
YMAJOR, |
100 |
YDECR, |
101 |
XDECR|YDECR, |
102 |
XDECR, |
103 |
0 |
104 |
}; |
105 |
|
106 |
#define pixorder() ortab[cvts.orient-1] |
107 |
|
108 |
extern char TMSTR[]; /* "CAPDATE=" from header.c */ |
109 |
char OWNSTR[] = "OWNER="; |
110 |
|
111 |
|
112 |
int |
113 |
main(int argc, char *argv[]) |
114 |
{ |
115 |
int reverse = 0; |
116 |
int i; |
117 |
/* set global progname */ |
118 |
fixargv0(argv[0]); |
119 |
|
120 |
for (i = 1; i < argc; i++) |
121 |
if (argv[i][0] == '-') |
122 |
switch (argv[i][1]) { |
123 |
case 'g': /* gamma correction */ |
124 |
cvts.gamcor = atof(argv[++i]); |
125 |
break; |
126 |
case 'x': /* XYZE Radiance output */ |
127 |
TGL(C_XYZE); |
128 |
break; |
129 |
case 'z': /* LZW compressed output */ |
130 |
cvts.comp = COMPRESSION_LZW; |
131 |
break; |
132 |
case 'L': /* LogLuv 32-bit output */ |
133 |
cvts.comp = COMPRESSION_SGILOG; |
134 |
cvts.phot = PHOTOMETRIC_LOGLUV; |
135 |
break; |
136 |
case 'l': /* LogLuv 24-bit output */ |
137 |
cvts.comp = COMPRESSION_SGILOG24; |
138 |
cvts.phot = PHOTOMETRIC_LOGLUV; |
139 |
break; |
140 |
case 'w': /* 16-bit/primary output? */ |
141 |
TGL(C_TWRD); |
142 |
break; |
143 |
case 'f': /* IEEE float output? */ |
144 |
TGL(C_TFLT); |
145 |
break; |
146 |
case 'b': /* greyscale output? */ |
147 |
TGL(C_GRY); |
148 |
break; |
149 |
case 'e': /* exposure adjustment */ |
150 |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
151 |
goto userr; |
152 |
cvts.bradj = atoi(argv[++i]); |
153 |
break; |
154 |
case 'r': /* reverse conversion? */ |
155 |
reverse = !reverse; |
156 |
break; |
157 |
case '\0': |
158 |
goto doneopts; |
159 |
default: |
160 |
goto userr; |
161 |
} |
162 |
else |
163 |
break; |
164 |
doneopts: |
165 |
if (reverse) { |
166 |
|
167 |
if (i != argc-2 && i != argc-1) |
168 |
goto userr; |
169 |
|
170 |
tiff2ra(i, argv); |
171 |
|
172 |
} else { |
173 |
|
174 |
if (i != argc-2) |
175 |
goto userr; |
176 |
/* consistency checks */ |
177 |
if (CHK(C_GRY)) { |
178 |
if (cvts.phot == PHOTOMETRIC_RGB) |
179 |
cvts.phot = PHOTOMETRIC_MINISBLACK; |
180 |
else { |
181 |
cvts.phot = PHOTOMETRIC_LOGL; |
182 |
cvts.comp = COMPRESSION_SGILOG; |
183 |
} |
184 |
} |
185 |
if (CHK(C_TWRD|C_TFLT) == (C_TWRD|C_TFLT)) |
186 |
goto userr; |
187 |
|
188 |
ra2tiff(i, argv); |
189 |
} |
190 |
|
191 |
exit(0); |
192 |
userr: |
193 |
fprintf(stderr, |
194 |
"Usage: %s [-z|-L|-l|-f|-w][-b][-e +/-stops][-g gamma] {in.hdr|-} out.tif\n", |
195 |
progname); |
196 |
fprintf(stderr, |
197 |
" Or: %s -r [-x][-e +/-stops][-g gamma] in.tif [out.hdr|-]\n", |
198 |
progname); |
199 |
exit(1); |
200 |
} |
201 |
|
202 |
|
203 |
static void |
204 |
quiterr( /* print message and exit */ |
205 |
char *err |
206 |
) |
207 |
{ |
208 |
if (err != NULL) { |
209 |
fprintf(stderr, "%s: %s\n", progname, err); |
210 |
exit(1); |
211 |
} |
212 |
exit(0); |
213 |
} |
214 |
|
215 |
|
216 |
static void |
217 |
allocbufs(void) /* allocate scanline buffers */ |
218 |
{ |
219 |
int rsiz, tsiz; |
220 |
|
221 |
rsiz = CHK(C_RFLT) ? sizeof(COLOR) : sizeof(COLR); |
222 |
tsiz = (CHK(C_TFLT) ? sizeof(float) : |
223 |
CHK(C_TWRD) ? sizeof(uint16) : sizeof(uint8)) * |
224 |
(CHK(C_GRY) ? 1 : 3); |
225 |
cvts.r.p = malloc(rsiz*cvts.xmax); |
226 |
cvts.t.p = malloc(tsiz*cvts.xmax); |
227 |
if ((cvts.r.p == NULL) | (cvts.t.p == NULL)) |
228 |
quiterr("no memory to allocate scanline buffers"); |
229 |
} |
230 |
|
231 |
|
232 |
static void |
233 |
initfromtif(void) /* initialize conversion from TIFF input */ |
234 |
{ |
235 |
uint16 hi; |
236 |
char *cp; |
237 |
float *fa, f1, f2; |
238 |
|
239 |
TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_PLANARCONFIG, &cvts.pconf); |
240 |
|
241 |
if (TIFFGetField(cvts.tif, TIFFTAG_PRIMARYCHROMATICITIES, &fa)) { |
242 |
cvts.prims[RED][CIEX] = fa[0]; |
243 |
cvts.prims[RED][CIEY] = fa[1]; |
244 |
cvts.prims[GRN][CIEX] = fa[2]; |
245 |
cvts.prims[GRN][CIEY] = fa[3]; |
246 |
cvts.prims[BLU][CIEX] = fa[4]; |
247 |
cvts.prims[BLU][CIEY] = fa[5]; |
248 |
cvts.prims[WHT][CIEX] = 1./3.; |
249 |
cvts.prims[WHT][CIEY] = 1./3.; |
250 |
if (TIFFGetField(cvts.tif, TIFFTAG_WHITEPOINT, &fa)) { |
251 |
cvts.prims[WHT][CIEX] = fa[0]; |
252 |
cvts.prims[WHT][CIEY] = fa[1]; |
253 |
} |
254 |
SET(C_PRIM); |
255 |
} |
256 |
|
257 |
if (!TIFFGetField(cvts.tif, TIFFTAG_COMPRESSION, &cvts.comp)) |
258 |
cvts.comp = COMPRESSION_NONE; |
259 |
|
260 |
if (TIFFGetField(cvts.tif, TIFFTAG_XRESOLUTION, &f1) && |
261 |
TIFFGetField(cvts.tif, TIFFTAG_YRESOLUTION, &f2)) |
262 |
cvts.pixrat = f1/f2; |
263 |
|
264 |
TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_ORIENTATION, &cvts.orient); |
265 |
|
266 |
if (!TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_PHOTOMETRIC, &cvts.phot)) |
267 |
quiterr("TIFF has unspecified photometric type"); |
268 |
|
269 |
switch (cvts.phot) { |
270 |
case PHOTOMETRIC_LOGLUV: |
271 |
SET(C_RFLT|C_TFLT); |
272 |
if (!CHK(C_XYZE)) { |
273 |
cpcolormat(cvts.cmat, xyz2rgbmat); |
274 |
SET(C_CXFM|C_GAMUT); |
275 |
} else if (cvts.comp == COMPRESSION_SGILOG) |
276 |
SET(C_GAMUT); /* may be outside XYZ gamut */ |
277 |
if (cvts.pconf != PLANARCONFIG_CONTIG) |
278 |
quiterr("cannot handle separate Luv planes"); |
279 |
TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT, |
280 |
SGILOGDATAFMT_FLOAT); |
281 |
cvts.tf = Luv2Color; |
282 |
break; |
283 |
case PHOTOMETRIC_LOGL: |
284 |
SET(C_GRY|C_RFLT|C_TFLT|C_GAMUT); |
285 |
cvts.pconf = PLANARCONFIG_CONTIG; |
286 |
TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT, |
287 |
SGILOGDATAFMT_FLOAT); |
288 |
cvts.tf = L2Color; |
289 |
break; |
290 |
case PHOTOMETRIC_YCBCR: |
291 |
if (cvts.comp == COMPRESSION_JPEG && |
292 |
cvts.pconf == PLANARCONFIG_CONTIG) { |
293 |
TIFFSetField(cvts.tif, TIFFTAG_JPEGCOLORMODE, |
294 |
JPEGCOLORMODE_RGB); |
295 |
cvts.phot = PHOTOMETRIC_RGB; |
296 |
} else |
297 |
quiterr("unsupported photometric type"); |
298 |
/* fall through */ |
299 |
case PHOTOMETRIC_RGB: |
300 |
SET(C_GAMMA); |
301 |
setcolrgam(cvts.gamcor); |
302 |
if (CHK(C_XYZE)) { |
303 |
comprgb2xyzWBmat(cvts.cmat, |
304 |
CHK(C_PRIM) ? cvts.prims : stdprims); |
305 |
SET(C_CXFM); |
306 |
} |
307 |
if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) || |
308 |
hi != 3) |
309 |
quiterr("unsupported samples per pixel for RGB"); |
310 |
if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi)) |
311 |
hi = -1; |
312 |
switch (hi) { |
313 |
case 8: |
314 |
cvts.tf = RGB2Colr; |
315 |
break; |
316 |
case 16: |
317 |
cvts.tf = RRGGBB2Color; |
318 |
SET(C_RFLT|C_TWRD); |
319 |
break; |
320 |
case 32: |
321 |
cvts.tf = RfGfBf2Color; |
322 |
SET(C_RFLT|C_TFLT); |
323 |
break; |
324 |
default: |
325 |
quiterr("unsupported bits per sample for RGB"); |
326 |
} |
327 |
break; |
328 |
case PHOTOMETRIC_MINISBLACK: |
329 |
SET(C_GRY|C_GAMMA); |
330 |
setcolrgam(cvts.gamcor); |
331 |
cvts.pconf = PLANARCONFIG_CONTIG; |
332 |
if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) || |
333 |
hi != 1) |
334 |
quiterr("unsupported samples per pixel for greyscale"); |
335 |
if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi)) |
336 |
hi = -1; |
337 |
switch (hi) { |
338 |
case 8: |
339 |
cvts.tf = Gry2Colr; |
340 |
break; |
341 |
case 16: |
342 |
cvts.tf = GGry2Color; |
343 |
SET(C_RFLT|C_TWRD); |
344 |
break; |
345 |
case 32: |
346 |
cvts.tf = Gryf2Color; |
347 |
SET(C_RFLT|C_TFLT); |
348 |
break; |
349 |
default: |
350 |
quiterr("unsupported bits per sample for Gray"); |
351 |
} |
352 |
break; |
353 |
default: |
354 |
quiterr("unsupported photometric type"); |
355 |
break; |
356 |
} |
357 |
|
358 |
if (!TIFFGetField(cvts.tif, TIFFTAG_IMAGEWIDTH, &cvts.xmax) || |
359 |
!TIFFGetField(cvts.tif, TIFFTAG_IMAGELENGTH, &cvts.ymax)) |
360 |
quiterr("unknown input image resolution"); |
361 |
|
362 |
if (!TIFFGetField(cvts.tif, TIFFTAG_STONITS, &cvts.stonits)) |
363 |
cvts.stonits = -1.; |
364 |
|
365 |
if (!TIFFGetField(cvts.tif, TIFFTAG_DATETIME, &cp)) |
366 |
cvts.capdate[0] = '\0'; |
367 |
else { |
368 |
strncpy(cvts.capdate, cp, 19); |
369 |
cvts.capdate[19] = '\0'; |
370 |
} |
371 |
if (!TIFFGetField(cvts.tif, TIFFTAG_ARTIST, &cp)) |
372 |
cvts.owner[0] = '\0'; |
373 |
else { |
374 |
strncpy(cvts.owner, cp, sizeof(cvts.owner)); |
375 |
cvts.owner[sizeof(cvts.owner)-1] = '\0'; |
376 |
} |
377 |
/* add to Radiance header */ |
378 |
if (cvts.pixrat < .99 || cvts.pixrat > 1.01) |
379 |
fputaspect(cvts.pixrat, cvts.rfp); |
380 |
if (CHK(C_XYZE)) { |
381 |
if (cvts.stonits > .0) |
382 |
fputexpos(pow(2.,(double)cvts.bradj)/cvts.stonits, cvts.rfp); |
383 |
fputformat(CIEFMT, cvts.rfp); |
384 |
} else { |
385 |
if (CHK(C_PRIM)) |
386 |
fputprims(cvts.prims, cvts.rfp); |
387 |
if (cvts.stonits > .0) |
388 |
fputexpos(WHTEFFICACY*pow(2.,(double)cvts.bradj)/cvts.stonits, |
389 |
cvts.rfp); |
390 |
fputformat(COLRFMT, cvts.rfp); |
391 |
} |
392 |
if (cvts.capdate[0]) |
393 |
fprintf(cvts.rfp, "%s %s\n", TMSTR, cvts.capdate); |
394 |
if (cvts.owner[0]) |
395 |
fprintf(cvts.rfp, "%s %s\n", OWNSTR, cvts.owner); |
396 |
|
397 |
allocbufs(); /* allocate scanline buffers */ |
398 |
} |
399 |
|
400 |
|
401 |
static void |
402 |
tiff2ra( /* convert TIFF image to Radiance picture */ |
403 |
int ac, |
404 |
char *av[] |
405 |
) |
406 |
{ |
407 |
int32 y; |
408 |
/* open TIFF input */ |
409 |
if ((cvts.tif = TIFFOpen(av[ac], "r")) == NULL) |
410 |
quiterr("cannot open TIFF input"); |
411 |
/* open Radiance output */ |
412 |
if (av[ac+1] == NULL || !strcmp(av[ac+1], "-")) |
413 |
cvts.rfp = stdout; |
414 |
else if ((cvts.rfp = fopen(av[ac+1], "w")) == NULL) |
415 |
quiterr("cannot open Radiance output picture"); |
416 |
SET_FILE_BINARY(cvts.rfp); |
417 |
/* start output header */ |
418 |
newheader("RADIANCE", cvts.rfp); |
419 |
printargs(ac, av, cvts.rfp); |
420 |
|
421 |
initfromtif(); /* initialize conversion */ |
422 |
|
423 |
fputc('\n', cvts.rfp); /* finish Radiance header */ |
424 |
fputresolu(pixorder(), (int)cvts.xmax, (int)cvts.ymax, cvts.rfp); |
425 |
|
426 |
for (y = 0; y < cvts.ymax; y++) /* convert image */ |
427 |
(*cvts.tf)(y); |
428 |
/* clean up */ |
429 |
fclose(cvts.rfp); |
430 |
TIFFClose(cvts.tif); |
431 |
} |
432 |
|
433 |
|
434 |
static int |
435 |
headline( /* process Radiance input header line */ |
436 |
char *s, |
437 |
void *p |
438 |
) |
439 |
{ |
440 |
static int tmstrlen = 0; |
441 |
static int ownstrlen = 0; |
442 |
struct CONVST *cvp = (struct CONVST *)p; |
443 |
char fmt[MAXFMTLEN]; |
444 |
|
445 |
if (!tmstrlen) { |
446 |
tmstrlen = strlen(TMSTR); |
447 |
ownstrlen = strlen(OWNSTR); |
448 |
} |
449 |
if (formatval(fmt, s)) { |
450 |
CLR(C_XYZE|C_SPEC); |
451 |
if (!strcmp(fmt, CIEFMT)) |
452 |
SET(C_XYZE); |
453 |
else if (!strcmp(fmt, SPECFMT)) |
454 |
SET(C_SPEC); |
455 |
else if (strcmp(fmt, COLRFMT)) |
456 |
return(-1); |
457 |
return(1); |
458 |
} |
459 |
if (isexpos(s)) { |
460 |
cvp->stonits /= exposval(s); |
461 |
return(1); |
462 |
} |
463 |
if (isaspect(s)) { |
464 |
cvp->pixrat *= aspectval(s); |
465 |
return(1); |
466 |
} |
467 |
if (isprims(s)) { |
468 |
primsval(cvp->prims, s); |
469 |
SET(C_PRIM); |
470 |
return(1); |
471 |
} |
472 |
if (isdate(s)) { |
473 |
if (s[tmstrlen] == ' ') |
474 |
strncpy(cvp->capdate, s+tmstrlen+1, 19); |
475 |
else |
476 |
strncpy(cvp->capdate, s+tmstrlen, 19); |
477 |
cvp->capdate[19] = '\0'; |
478 |
return(1); |
479 |
} |
480 |
if (!strncmp(s, OWNSTR, ownstrlen)) { |
481 |
char *cp = s + ownstrlen; |
482 |
|
483 |
while (isspace(*cp)) |
484 |
++cp; |
485 |
strncpy(cvp->owner, cp, sizeof(cvp->owner)); |
486 |
cvp->owner[sizeof(cvp->owner)-1] = '\0'; |
487 |
for (cp = cvp->owner; *cp; cp++) |
488 |
; |
489 |
while (cp > cvp->owner && isspace(cp[-1])) |
490 |
*--cp = '\0'; |
491 |
return(1); |
492 |
} |
493 |
if (isncomp(s)) { |
494 |
cvp->ncc = ncompval(s); |
495 |
return((3 <= cvp->ncc) & (cvp->ncc < MAXCSAMP) ? 1 : -1); |
496 |
} |
497 |
if (iswlsplit(s)) |
498 |
return(wlsplitval(cvp->wpt, s) ? 1 : -1); |
499 |
return(0); |
500 |
} |
501 |
|
502 |
|
503 |
static void |
504 |
initfromrad(void) /* initialize input from a Radiance picture */ |
505 |
{ |
506 |
int i1, i2, po; |
507 |
/* read Radiance header */ |
508 |
memcpy(cvts.wpt, WLPART, sizeof(cvts.wpt)); |
509 |
if (getheader(cvts.rfp, headline, &cvts) < 0 || |
510 |
(po = fgetresolu(&i1, &i2, cvts.rfp)) < 0) |
511 |
quiterr("bad Radiance picture"); |
512 |
cvts.xmax = i1; cvts.ymax = i2; |
513 |
for (i1 = 0; i1 < 8; i1++) /* interpret orientation */ |
514 |
if (ortab[i1] == po) { |
515 |
cvts.orient = i1 + 1; |
516 |
break; |
517 |
} |
518 |
if (i1 >= 8) |
519 |
quiterr("internal error 1 in initfromrad"); |
520 |
if (!(po & YMAJOR)) |
521 |
cvts.pixrat = 1./cvts.pixrat; |
522 |
if (!CHK(C_XYZE)) |
523 |
cvts.stonits *= WHTEFFICACY; |
524 |
/* set up conversion */ |
525 |
TIFFSetField(cvts.tif, TIFFTAG_COMPRESSION, cvts.comp); |
526 |
TIFFSetField(cvts.tif, TIFFTAG_PHOTOMETRIC, cvts.phot); |
527 |
|
528 |
switch (cvts.phot) { |
529 |
case PHOTOMETRIC_LOGLUV: |
530 |
SET(C_RFLT|C_TFLT); |
531 |
CLR(C_GRY|C_TWRD); |
532 |
if (!CHK(C_XYZE|C_SPEC)) { |
533 |
comprgb2xyzWBmat(cvts.cmat, |
534 |
CHK(C_PRIM) ? cvts.prims : stdprims); |
535 |
SET(C_CXFM); |
536 |
} |
537 |
if (cvts.comp != COMPRESSION_SGILOG && |
538 |
cvts.comp != COMPRESSION_SGILOG24) |
539 |
quiterr("internal error 2 in initfromrad"); |
540 |
TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT, |
541 |
SGILOGDATAFMT_FLOAT); |
542 |
cvts.tf = Color2Luv; |
543 |
break; |
544 |
case PHOTOMETRIC_LOGL: |
545 |
SET(C_GRY|C_RFLT|C_TFLT); |
546 |
CLR(C_TWRD); |
547 |
if (cvts.comp != COMPRESSION_SGILOG) |
548 |
quiterr("internal error 3 in initfromrad"); |
549 |
TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT, |
550 |
SGILOGDATAFMT_FLOAT); |
551 |
cvts.tf = Color2L; |
552 |
break; |
553 |
case PHOTOMETRIC_RGB: |
554 |
SET(C_GAMMA|C_GAMUT); |
555 |
CLR(C_GRY); |
556 |
setcolrgam(cvts.gamcor); |
557 |
if (CHK(C_TWRD|C_TFLT) ? CHK(C_XYZE|C_SPEC) : CHK(C_XYZE)) { |
558 |
compxyz2rgbWBmat(cvts.cmat, |
559 |
CHK(C_PRIM) ? cvts.prims : stdprims); |
560 |
SET(C_CXFM); |
561 |
} |
562 |
if (CHK(C_PRIM)) { |
563 |
TIFFSetField(cvts.tif, TIFFTAG_PRIMARYCHROMATICITIES, |
564 |
(float *)cvts.prims); |
565 |
TIFFSetField(cvts.tif, TIFFTAG_WHITEPOINT, |
566 |
(float *)cvts.prims[WHT]); |
567 |
} |
568 |
if (CHK(C_TWRD)) { |
569 |
cvts.tf = Color2RRGGBB; |
570 |
SET(C_RFLT); |
571 |
} else if (CHK(C_TFLT)) { |
572 |
TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT, |
573 |
SAMPLEFORMAT_IEEEFP); |
574 |
cvts.tf = Color2RfGfBf; |
575 |
SET(C_RFLT); |
576 |
CLR(C_GAMUT); |
577 |
} else |
578 |
cvts.tf = Colr2RGB; |
579 |
break; |
580 |
case PHOTOMETRIC_MINISBLACK: |
581 |
SET(C_GRY|C_GAMMA|C_GAMUT); |
582 |
setcolrgam(cvts.gamcor); |
583 |
if (CHK(C_TWRD)) { |
584 |
cvts.tf = Color2GGry; |
585 |
SET(C_RFLT); |
586 |
} else if (CHK(C_TFLT)) { |
587 |
TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT, |
588 |
SAMPLEFORMAT_IEEEFP); |
589 |
cvts.tf = Color2Gryf; |
590 |
SET(C_RFLT); |
591 |
} else |
592 |
cvts.tf = Colr2Gry; |
593 |
break; |
594 |
default: |
595 |
quiterr("internal error 4 in initfromrad"); |
596 |
break; |
597 |
} |
598 |
/* set other TIFF fields */ |
599 |
TIFFSetField(cvts.tif, TIFFTAG_IMAGEWIDTH, cvts.xmax); |
600 |
TIFFSetField(cvts.tif, TIFFTAG_IMAGELENGTH, cvts.ymax); |
601 |
TIFFSetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, CHK(C_GRY) ? 1 : 3); |
602 |
TIFFSetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, |
603 |
CHK(C_TFLT) ? 32 : CHK(C_TWRD) ? 16 : 8); |
604 |
TIFFSetField(cvts.tif, TIFFTAG_XRESOLUTION, 72.); |
605 |
TIFFSetField(cvts.tif, TIFFTAG_YRESOLUTION, 72./cvts.pixrat); |
606 |
TIFFSetField(cvts.tif, TIFFTAG_ORIENTATION, cvts.orient); |
607 |
TIFFSetField(cvts.tif, TIFFTAG_RESOLUTIONUNIT, 2); |
608 |
TIFFSetField(cvts.tif, TIFFTAG_PLANARCONFIG, cvts.pconf); |
609 |
TIFFSetField(cvts.tif, TIFFTAG_STONITS, |
610 |
cvts.stonits/pow(2.,(double)cvts.bradj)); |
611 |
if (cvts.capdate[0]) |
612 |
TIFFSetField(cvts.tif, TIFFTAG_DATETIME, cvts.capdate); |
613 |
if (cvts.owner[0]) |
614 |
TIFFSetField(cvts.tif, TIFFTAG_ARTIST, cvts.owner); |
615 |
if (cvts.comp == COMPRESSION_NONE) |
616 |
i1 = TIFFScanlineSize(cvts.tif); |
617 |
else |
618 |
i1 = 3*cvts.xmax; /* conservative guess */ |
619 |
i2 = 8192/i1; /* compute good strip size */ |
620 |
if (i2 < 1) i2 = 1; |
621 |
TIFFSetField(cvts.tif, TIFFTAG_ROWSPERSTRIP, (uint32)i2); |
622 |
|
623 |
allocbufs(); /* allocate scanline buffers */ |
624 |
} |
625 |
|
626 |
|
627 |
static void |
628 |
ra2tiff( /* convert Radiance picture to TIFF image */ |
629 |
int ac, |
630 |
char *av[] |
631 |
) |
632 |
{ |
633 |
uint32 y; |
634 |
/* open Radiance file */ |
635 |
if (!strcmp(av[ac], "-")) |
636 |
cvts.rfp = stdin; |
637 |
else if ((cvts.rfp = fopen(av[ac], "r")) == NULL) |
638 |
quiterr("cannot open Radiance input picture"); |
639 |
SET_FILE_BINARY(cvts.rfp); |
640 |
/* open TIFF file */ |
641 |
if ((cvts.tif = TIFFOpen(av[ac+1], "w")) == NULL) |
642 |
quiterr("cannot open TIFF output"); |
643 |
|
644 |
initfromrad(); /* initialize conversion */ |
645 |
|
646 |
for (y = 0; y < cvts.ymax; y++) /* convert image */ |
647 |
(*cvts.tf)(y); |
648 |
/* clean up */ |
649 |
TIFFClose(cvts.tif); |
650 |
fclose(cvts.rfp); |
651 |
} |
652 |
|
653 |
|
654 |
static void |
655 |
ReadRadScan(struct CONVST *cvp) |
656 |
{ |
657 |
static struct CONVST *lastcvp = NULL; |
658 |
static COLOR scomp[MAXCSAMP]; |
659 |
|
660 |
if (cvp->ncc > 3) { /* convert spectral pixels */ |
661 |
SCOLR sclr; |
662 |
SCOLOR scol; |
663 |
COLOR xyz; |
664 |
int x, n; |
665 |
if (lastcvp != cvp) { |
666 |
for (n = cvp->ncc; n--; ) |
667 |
spec_cie(scomp[n], |
668 |
cvp->wpt[0] + (cvp->wpt[3] - cvp->wpt[0])*(n+1)/cvp->ncc, |
669 |
cvp->wpt[0] + (cvp->wpt[3] - cvp->wpt[0])*n/cvp->ncc); |
670 |
lastcvp = cvp; |
671 |
} |
672 |
for (x = 0; x < cvp->xmax; x++) { |
673 |
if (getbinary(sclr, cvp->ncc+1, 1, cvp->rfp) != 1) |
674 |
goto readerr; |
675 |
scolr2scolor(scol, sclr, cvp->ncc); |
676 |
setcolor(cvp->r.colors[x], 0, 0, 0); |
677 |
for (n = cvp->ncc; n--; ) { |
678 |
copycolor(xyz, scomp[n]); |
679 |
scalecolor(xyz, scol[n]); |
680 |
addcolor(cvp->r.colors[x], xyz); |
681 |
} |
682 |
} |
683 |
return; |
684 |
} |
685 |
if (freadscan(cvp->r.colors, cvp->xmax, cvp->rfp) >= 0) |
686 |
return; |
687 |
readerr: |
688 |
quiterr("error reading Radiance picture"); |
689 |
} |
690 |
|
691 |
|
692 |
static void |
693 |
Luv2Color( /* read/convert/write Luv->COLOR scanline */ |
694 |
uint32 y |
695 |
) |
696 |
{ |
697 |
int x; |
698 |
|
699 |
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT)) |
700 |
quiterr("internal error 1 in Luv2Color"); |
701 |
|
702 |
if (cvts.pconf != PLANARCONFIG_CONTIG) |
703 |
quiterr("cannot handle separate 32-bit color planes"); |
704 |
|
705 |
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
706 |
quiterr("error reading TIFF input"); |
707 |
/* also works for float RGB */ |
708 |
for (x = cvts.xmax; x--; ) { |
709 |
setcolor(cvts.r.colors[x], |
710 |
cvts.t.fp[3*x], |
711 |
cvts.t.fp[3*x + 1], |
712 |
cvts.t.fp[3*x + 2]); |
713 |
if (CHK(C_CXFM)) |
714 |
colortrans(cvts.r.colors[x], cvts.cmat, |
715 |
cvts.r.colors[x]); |
716 |
if (CHK(C_GAMUT)) |
717 |
clipgamut(cvts.r.colors[x], cvts.t.fp[3*x + 1], |
718 |
CGAMUT_LOWER, cblack, cwhite); |
719 |
} |
720 |
if (cvts.bradj) { |
721 |
double m = pow(2.,(double)cvts.bradj); |
722 |
for (x = cvts.xmax; x--; ) |
723 |
scalecolor(cvts.r.colors[x], m); |
724 |
} |
725 |
|
726 |
if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) |
727 |
quiterr("error writing Radiance picture"); |
728 |
} |
729 |
|
730 |
|
731 |
static void |
732 |
RRGGBB2Color( /* read/convert/write RGB16->COLOR scanline */ |
733 |
uint32 y |
734 |
) |
735 |
{ |
736 |
int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01); |
737 |
double d; |
738 |
int x; |
739 |
|
740 |
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_TWRD|C_RFLT)) |
741 |
quiterr("internal error 1 in RRGGBB2Color"); |
742 |
|
743 |
if (cvts.pconf != PLANARCONFIG_CONTIG) |
744 |
quiterr("cannot handle separate 16-bit color planes"); |
745 |
|
746 |
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
747 |
quiterr("error reading TIFF input"); |
748 |
|
749 |
for (x = cvts.xmax; x--; ) { |
750 |
d = (cvts.t.wp[3*x] + 0.5)*(1./(1L<<16)); |
751 |
if (dogamma) d = pow(d, cvts.gamcor); |
752 |
colval(cvts.r.colors[x],RED) = d; |
753 |
d = (cvts.t.wp[3*x + 1] + 0.5)*(1./(1L<<16)); |
754 |
if (dogamma) d = pow(d, cvts.gamcor); |
755 |
colval(cvts.r.colors[x],GRN) = d; |
756 |
d = (cvts.t.wp[3*x + 2] + 0.5)*(1./(1L<<16)); |
757 |
if (dogamma) d = pow(d, cvts.gamcor); |
758 |
colval(cvts.r.colors[x],BLU) = d; |
759 |
if (CHK(C_CXFM)) |
760 |
colortrans(cvts.r.colors[x], cvts.cmat, |
761 |
cvts.r.colors[x]); |
762 |
} |
763 |
if (cvts.bradj) { |
764 |
d = pow(2.,(double)cvts.bradj); |
765 |
for (x = cvts.xmax; x--; ) |
766 |
scalecolor(cvts.r.colors[x], d); |
767 |
} |
768 |
|
769 |
if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) |
770 |
quiterr("error writing Radiance picture"); |
771 |
} |
772 |
|
773 |
|
774 |
static void |
775 |
L2Color( /* read/convert/write Lfloat->COLOR scanline */ |
776 |
uint32 y |
777 |
) |
778 |
{ |
779 |
float m = pow(2., (double)cvts.bradj); |
780 |
int x; |
781 |
|
782 |
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY)) |
783 |
quiterr("internal error 1 in L2Color"); |
784 |
|
785 |
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
786 |
quiterr("error reading TIFF input"); |
787 |
/* also works for float greyscale */ |
788 |
for (x = cvts.xmax; x--; ) { |
789 |
float f = cvts.t.fp[x]; |
790 |
if (cvts.bradj) f *= m; |
791 |
setcolor(cvts.r.colors[x], f, f, f); |
792 |
} |
793 |
if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) |
794 |
quiterr("error writing Radiance picture"); |
795 |
} |
796 |
|
797 |
|
798 |
static void |
799 |
RGB2Colr( /* read/convert/write RGB->COLR scanline */ |
800 |
uint32 y |
801 |
) |
802 |
{ |
803 |
COLOR ctmp; |
804 |
int x; |
805 |
|
806 |
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY)) |
807 |
quiterr("internal error 1 in RGB2Colr"); |
808 |
|
809 |
if (cvts.pconf == PLANARCONFIG_CONTIG) { |
810 |
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
811 |
goto readerr; |
812 |
for (x = cvts.xmax; x--; ) { |
813 |
cvts.r.colrs[x][RED] = cvts.t.bp[3*x]; |
814 |
cvts.r.colrs[x][GRN] = cvts.t.bp[3*x + 1]; |
815 |
cvts.r.colrs[x][BLU] = cvts.t.bp[3*x + 2]; |
816 |
} |
817 |
} else { |
818 |
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
819 |
goto readerr; |
820 |
if (TIFFReadScanline(cvts.tif, |
821 |
(tdata_t)(cvts.t.bp + cvts.xmax), y, 1) < 0) |
822 |
goto readerr; |
823 |
if (TIFFReadScanline(cvts.tif, |
824 |
(tdata_t)(cvts.t.bp + 2*cvts.xmax), y, 2) < 0) |
825 |
goto readerr; |
826 |
for (x = cvts.xmax; x--; ) { |
827 |
cvts.r.colrs[x][RED] = cvts.t.bp[x]; |
828 |
cvts.r.colrs[x][GRN] = cvts.t.bp[cvts.xmax + x]; |
829 |
cvts.r.colrs[x][BLU] = cvts.t.bp[2*cvts.xmax + x]; |
830 |
} |
831 |
} |
832 |
|
833 |
gambs_colrs(cvts.r.colrs, cvts.xmax); |
834 |
if (CHK(C_CXFM)) |
835 |
for (x = cvts.xmax; x--; ) { |
836 |
colr_color(ctmp, cvts.r.colrs[x]); |
837 |
colortrans(ctmp, cvts.cmat, ctmp); |
838 |
if (CHK(C_GAMUT)) /* !CHK(C_XYZE) */ |
839 |
clipgamut(ctmp, bright(ctmp), CGAMUT_LOWER, |
840 |
cblack, cwhite); |
841 |
setcolr(cvts.r.colrs[x], colval(ctmp,RED), |
842 |
colval(ctmp,GRN), colval(ctmp,BLU)); |
843 |
} |
844 |
if (cvts.bradj) |
845 |
shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj); |
846 |
|
847 |
if (fwritecolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0) |
848 |
quiterr("error writing Radiance picture"); |
849 |
return; |
850 |
readerr: |
851 |
quiterr("error reading TIFF input"); |
852 |
} |
853 |
|
854 |
|
855 |
static void |
856 |
Gry2Colr( /* read/convert/write G8->COLR scanline */ |
857 |
uint32 y |
858 |
) |
859 |
{ |
860 |
int x; |
861 |
|
862 |
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != C_GRY) |
863 |
quiterr("internal error 1 in Gry2Colr"); |
864 |
|
865 |
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
866 |
quiterr("error reading TIFF input"); |
867 |
|
868 |
for (x = cvts.xmax; x--; ) |
869 |
cvts.r.colrs[x][RED] = |
870 |
cvts.r.colrs[x][GRN] = |
871 |
cvts.r.colrs[x][BLU] = cvts.t.bp[x]; |
872 |
|
873 |
gambs_colrs(cvts.r.colrs, cvts.xmax); |
874 |
if (cvts.bradj) |
875 |
shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj); |
876 |
|
877 |
if (fwritecolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0) |
878 |
quiterr("error writing Radiance picture"); |
879 |
} |
880 |
|
881 |
|
882 |
static void |
883 |
GGry2Color( /* read/convert/write G16->COLOR scanline */ |
884 |
uint32 y |
885 |
) |
886 |
{ |
887 |
int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01); |
888 |
double m; |
889 |
double d; |
890 |
int x; |
891 |
|
892 |
if (CHK(C_TFLT|C_TWRD|C_GRY|C_RFLT) != (C_GRY|C_RFLT|C_TWRD)) |
893 |
quiterr("internal error 1 in GGry2Color"); |
894 |
|
895 |
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
896 |
quiterr("error reading TIFF input"); |
897 |
|
898 |
if (cvts.bradj) |
899 |
m = pow(2., (double)cvts.bradj); |
900 |
for (x = cvts.xmax; x--; ) { |
901 |
d = (cvts.t.wp[x] + 0.5)*(1./(1L<<16)); |
902 |
if (dogamma) d = pow(d, cvts.gamcor); |
903 |
if (cvts.bradj) d *= m; |
904 |
colval(cvts.r.colors[x],RED) = |
905 |
colval(cvts.r.colors[x],GRN) = |
906 |
colval(cvts.r.colors[x],BLU) = d; |
907 |
} |
908 |
if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) |
909 |
quiterr("error writing Radiance picture"); |
910 |
} |
911 |
|
912 |
|
913 |
static void |
914 |
Color2GGry( /* read/convert/write COLOR->G16 scanline */ |
915 |
uint32 y |
916 |
) |
917 |
{ |
918 |
int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01); |
919 |
float m = pow(2.,(double)cvts.bradj); |
920 |
int x; |
921 |
|
922 |
if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TWRD|C_GRY)) |
923 |
quiterr("internal error 1 in Color2GGry"); |
924 |
|
925 |
ReadRadScan(&cvts); |
926 |
|
927 |
for (x = cvts.xmax; x--; ) { |
928 |
float f = m*( CHK(C_XYZE) ? |
929 |
colval(cvts.r.colors[x],CIEY) |
930 |
: bright(cvts.r.colors[x]) ); |
931 |
if (f <= 0) |
932 |
cvts.t.wp[x] = 0; |
933 |
else if (f >= 1) |
934 |
cvts.t.wp[x] = 0xffff; |
935 |
else if (dogamma) |
936 |
cvts.t.wp[x] = (int)((float)(1L<<16) * |
937 |
pow(f, 1./cvts.gamcor)); |
938 |
else |
939 |
cvts.t.wp[x] = (int)((float)(1L<<16) * f); |
940 |
} |
941 |
|
942 |
if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
943 |
quiterr("error writing TIFF output"); |
944 |
} |
945 |
|
946 |
|
947 |
static void |
948 |
Color2L( /* read/convert/write COLOR->Lfloat scanline */ |
949 |
uint32 y |
950 |
) |
951 |
{ |
952 |
float m = pow(2.,(double)cvts.bradj); |
953 |
int x; |
954 |
|
955 |
if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TFLT|C_GRY)) |
956 |
quiterr("internal error 1 in Color2L"); |
957 |
|
958 |
ReadRadScan(&cvts); |
959 |
|
960 |
for (x = cvts.xmax; x--; ) |
961 |
cvts.t.fp[x] = m*( CHK(C_XYZE) ? colval(cvts.r.colors[x],CIEY) |
962 |
: bright(cvts.r.colors[x]) ); |
963 |
|
964 |
if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
965 |
quiterr("error writing TIFF output"); |
966 |
} |
967 |
|
968 |
|
969 |
static void |
970 |
Color2Luv( /* read/convert/write COLOR->Luv scanline */ |
971 |
uint32 y |
972 |
) |
973 |
{ |
974 |
int x; |
975 |
|
976 |
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT)) |
977 |
quiterr("internal error 1 in Color2Luv"); |
978 |
|
979 |
ReadRadScan(&cvts); |
980 |
|
981 |
if (CHK(C_CXFM)) |
982 |
for (x = cvts.xmax; x--; ) |
983 |
colortrans(cvts.r.colors[x], cvts.cmat, |
984 |
cvts.r.colors[x]); |
985 |
if (cvts.bradj) { |
986 |
double m = pow(2.,(double)cvts.bradj); |
987 |
for (x = cvts.xmax; x--; ) |
988 |
scalecolor(cvts.r.colors[x], m); |
989 |
} |
990 |
/* also works for float RGB */ |
991 |
for (x = cvts.xmax; x--; ) { |
992 |
cvts.t.fp[3*x] = colval(cvts.r.colors[x],CIEX); |
993 |
cvts.t.fp[3*x+1] = colval(cvts.r.colors[x],CIEY); |
994 |
cvts.t.fp[3*x+2] = colval(cvts.r.colors[x],CIEZ); |
995 |
} |
996 |
|
997 |
if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
998 |
quiterr("error writing TIFF output"); |
999 |
} |
1000 |
|
1001 |
|
1002 |
static void |
1003 |
Color2RRGGBB( /* read/convert/write COLOR->RGB16 scanline */ |
1004 |
uint32 y |
1005 |
) |
1006 |
{ |
1007 |
int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01); |
1008 |
float m = pow(2.,(double)cvts.bradj); |
1009 |
int x, i; |
1010 |
|
1011 |
if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TWRD)) |
1012 |
quiterr("internal error 1 in Color2RRGGBB"); |
1013 |
|
1014 |
ReadRadScan(&cvts); |
1015 |
|
1016 |
for (x = cvts.xmax; x--; ) { |
1017 |
if (CHK(C_CXFM)) { |
1018 |
colortrans(cvts.r.colors[x], cvts.cmat, |
1019 |
cvts.r.colors[x]); |
1020 |
if (CHK(C_GAMUT)) |
1021 |
clipgamut(cvts.r.colors[x], bright(cvts.r.colors[x]), |
1022 |
CGAMUT_LOWER, cblack, cwhite); |
1023 |
} |
1024 |
for (i = 3; i--; ) { |
1025 |
float f = m*colval(cvts.r.colors[x],i); |
1026 |
if (f <= 0) |
1027 |
cvts.t.wp[3*x + i] = 0; |
1028 |
else if (f >= 1) |
1029 |
cvts.t.wp[3*x + i] = 0xffff; |
1030 |
else if (dogamma) |
1031 |
cvts.t.wp[3*x + i] = (int)((float)(1L<<16) * |
1032 |
pow(f, 1./cvts.gamcor)); |
1033 |
else |
1034 |
cvts.t.wp[3*x + i] = (int)((float)(1L<<16)*f); |
1035 |
} |
1036 |
} |
1037 |
|
1038 |
if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
1039 |
quiterr("error writing TIFF output"); |
1040 |
} |
1041 |
|
1042 |
|
1043 |
static void |
1044 |
Colr2Gry( /* read/convert/write COLR->RGB scanline */ |
1045 |
uint32 y |
1046 |
) |
1047 |
{ |
1048 |
int x; |
1049 |
|
1050 |
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != C_GRY) |
1051 |
quiterr("internal error 1 in Colr2Gry"); |
1052 |
|
1053 |
if (fread2colrs(cvts.r.colrs, cvts.xmax, cvts.rfp, |
1054 |
cvts.ncc, cvts.wpt) < 0) |
1055 |
quiterr("error reading Radiance picture"); |
1056 |
|
1057 |
if (cvts.bradj) |
1058 |
shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj); |
1059 |
for (x = cvts.xmax; x--; ) |
1060 |
colval(cvts.r.colrs[x],CIEY) = normbright(cvts.r.colrs[x]); |
1061 |
colrs_gambs(cvts.r.colrs, cvts.xmax); |
1062 |
|
1063 |
for (x = cvts.xmax; x--; ) |
1064 |
cvts.t.bp[x] = colval(cvts.r.colrs[x],CIEY); |
1065 |
|
1066 |
if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
1067 |
quiterr("error writing TIFF output"); |
1068 |
} |
1069 |
|
1070 |
|
1071 |
static void |
1072 |
Colr2RGB( /* read/convert/write COLR->RGB scanline */ |
1073 |
uint32 y |
1074 |
) |
1075 |
{ |
1076 |
COLOR ctmp; |
1077 |
int x; |
1078 |
|
1079 |
if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY)) |
1080 |
quiterr("internal error 1 in Colr2RGB"); |
1081 |
|
1082 |
if (fread2colrs(cvts.r.colrs, cvts.xmax, cvts.rfp, |
1083 |
cvts.ncc, cvts.wpt) < 0) |
1084 |
quiterr("error reading Radiance picture"); |
1085 |
|
1086 |
if (cvts.bradj) |
1087 |
shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj); |
1088 |
if (CHK(C_CXFM)) |
1089 |
for (x = cvts.xmax; x--; ) { |
1090 |
colr_color(ctmp, cvts.r.colrs[x]); |
1091 |
colortrans(ctmp, cvts.cmat, ctmp); |
1092 |
if (CHK(C_GAMUT)) |
1093 |
clipgamut(ctmp, bright(ctmp), CGAMUT, |
1094 |
cblack, cwhite); |
1095 |
setcolr(cvts.r.colrs[x], colval(ctmp,RED), |
1096 |
colval(ctmp,GRN), colval(ctmp,BLU)); |
1097 |
} |
1098 |
colrs_gambs(cvts.r.colrs, cvts.xmax); |
1099 |
|
1100 |
for (x = cvts.xmax; x--; ) { |
1101 |
cvts.t.bp[3*x] = cvts.r.colrs[x][RED]; |
1102 |
cvts.t.bp[3*x+1] = cvts.r.colrs[x][GRN]; |
1103 |
cvts.t.bp[3*x+2] = cvts.r.colrs[x][BLU]; |
1104 |
} |
1105 |
|
1106 |
if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
1107 |
quiterr("error writing TIFF output"); |
1108 |
} |