9 |
|
|
10 |
|
#include <stdio.h> |
11 |
|
#include <math.h> |
12 |
+ |
#include <ctype.h> |
13 |
|
#include <time.h> |
14 |
+ |
#include <string.h> |
15 |
+ |
|
16 |
|
#include "tiffio.h" |
17 |
|
#include "color.h" |
18 |
|
#include "resolu.h" |
30 |
|
#define C_TWRD 0x80 /* TIFF data is 16-bit */ |
31 |
|
#define C_PRIM 0x100 /* has assigned primaries */ |
32 |
|
|
33 |
+ |
typedef void colcvf_t(uint32 y); |
34 |
+ |
|
35 |
|
struct { |
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 */ |
60 |
|
float *fp; /* float pointer */ |
61 |
|
char *p; /* generic pointer */ |
62 |
|
} t; /* TIFF scanline */ |
63 |
< |
void (*tf)(); /* translation procedure */ |
63 |
> |
colcvf_t *tf; /* translation procedure */ |
64 |
|
} cvts = { /* conversion structure */ |
65 |
< |
0, COMPRESSION_NONE, PHOTOMETRIC_RGB, |
65 |
> |
0, "", "", COMPRESSION_NONE, PHOTOMETRIC_RGB, |
66 |
|
PLANARCONFIG_CONTIG, GAMCOR, 0, 1, 1., 1., |
67 |
|
}; |
68 |
|
|
71 |
|
#define CLR(f) (cvts.flags &= ~(f)) |
72 |
|
#define TGL(f) (cvts.flags ^= (f)) |
73 |
|
|
74 |
< |
void Luv2Color(), L2Color(), RGB2Colr(), Gry2Colr(); |
75 |
< |
void Color2Luv(), Color2L(), Colr2RGB(), Colr2Gry(); |
76 |
< |
void RRGGBB2Color(), GGry2Color(), Color2RRGGBB(), Color2GGry(); |
74 |
> |
static colcvf_t Luv2Color, L2Color, RGB2Colr, Gry2Colr; |
75 |
> |
static colcvf_t Color2Luv, Color2L, Colr2RGB, Colr2Gry; |
76 |
> |
static colcvf_t RRGGBB2Color, GGry2Color, Color2RRGGBB, Color2GGry; |
77 |
|
|
78 |
+ |
static gethfunc headline; |
79 |
+ |
static void quiterr(char *err); |
80 |
+ |
static void allocbufs(void); |
81 |
+ |
static void initfromtif(void); |
82 |
+ |
static void tiff2ra(int ac, char *av[]); |
83 |
+ |
static void initfromrad(void); |
84 |
+ |
static void ra2tiff(int ac, char *av[]); |
85 |
+ |
|
86 |
+ |
|
87 |
+ |
|
88 |
|
#define RfGfBf2Color Luv2Color |
89 |
|
#define Gryf2Color L2Color |
90 |
|
#define Color2Gryf Color2L |
103 |
|
|
104 |
|
#define pixorder() ortab[cvts.orient-1] |
105 |
|
|
106 |
+ |
extern char TMSTR[]; /* "CAPDATE=" from header.c */ |
107 |
+ |
char OWNSTR[] = "OWNER="; |
108 |
+ |
|
109 |
|
char *progname; |
110 |
|
|
111 |
|
|
112 |
< |
main(argc, argv) |
113 |
< |
int argc; |
94 |
< |
char *argv[]; |
112 |
> |
int |
113 |
> |
main(int argc, char *argv[]) |
114 |
|
{ |
115 |
|
int reverse = 0; |
116 |
|
int i; |
174 |
|
if (i != argc-2) |
175 |
|
goto userr; |
176 |
|
/* consistency checks */ |
177 |
< |
if (CHK(C_GRY)) |
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 |
|
|
200 |
|
} |
201 |
|
|
202 |
|
|
203 |
< |
quiterr(err) /* print message and exit */ |
204 |
< |
char *err; |
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); |
213 |
|
} |
214 |
|
|
215 |
|
|
216 |
< |
allocbufs() /* allocate scanline buffers */ |
216 |
> |
static void |
217 |
> |
allocbufs(void) /* allocate scanline buffers */ |
218 |
|
{ |
219 |
|
int rsiz, tsiz; |
220 |
|
|
224 |
|
(CHK(C_GRY) ? 1 : 3); |
225 |
|
cvts.r.p = (char *)malloc(rsiz*cvts.xmax); |
226 |
|
cvts.t.p = (char *)malloc(tsiz*cvts.xmax); |
227 |
< |
if (cvts.r.p == NULL | cvts.t.p == NULL) |
227 |
> |
if ((cvts.r.p == NULL) | (cvts.t.p == NULL)) |
228 |
|
quiterr("no memory to allocate scanline buffers"); |
229 |
|
} |
230 |
|
|
231 |
|
|
232 |
< |
initfromtif() /* initialize conversion from TIFF input */ |
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 |
|
CLR(C_GRY|C_GAMMA|C_PRIM|C_RFLT|C_TFLT|C_TWRD|C_CXFM); |
275 |
|
cpcolormat(cvts.cmat, xyz2rgbmat); |
276 |
|
SET(C_CXFM|C_GAMUT); |
277 |
|
} else if (cvts.comp == COMPRESSION_SGILOG) |
278 |
< |
SET(C_GAMUT); |
278 |
> |
SET(C_GAMUT); /* may be outside XYZ gamut */ |
279 |
|
if (cvts.pconf != PLANARCONFIG_CONTIG) |
280 |
|
quiterr("cannot handle separate Luv planes"); |
281 |
|
TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT, |
363 |
|
|
364 |
|
if (!TIFFGetField(cvts.tif, TIFFTAG_STONITS, &cvts.stonits)) |
365 |
|
cvts.stonits = 1.; |
366 |
+ |
|
367 |
+ |
if (!TIFFGetField(cvts.tif, TIFFTAG_DATETIME, &cp)) |
368 |
+ |
cvts.capdate[0] = '\0'; |
369 |
+ |
else { |
370 |
+ |
strncpy(cvts.capdate, cp, 19); |
371 |
+ |
cvts.capdate[19] = '\0'; |
372 |
+ |
} |
373 |
+ |
if (!TIFFGetField(cvts.tif, TIFFTAG_ARTIST, &cp)) |
374 |
+ |
cvts.owner[0] = '\0'; |
375 |
+ |
else { |
376 |
+ |
strncpy(cvts.owner, cp, sizeof(cvts.owner)); |
377 |
+ |
cvts.owner[sizeof(cvts.owner)-1] = '\0'; |
378 |
+ |
} |
379 |
|
/* add to Radiance header */ |
380 |
|
if (cvts.pixrat < .99 || cvts.pixrat > 1.01) |
381 |
|
fputaspect(cvts.pixrat, cvts.rfp); |
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 |
< |
tiff2ra(ac, av) /* convert TIFF image to Radiance picture */ |
402 |
< |
int ac; |
403 |
< |
char *av[]; |
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 */ |
430 |
|
} |
431 |
|
|
432 |
|
|
433 |
< |
int |
434 |
< |
headline(s) /* process Radiance input header line */ |
435 |
< |
char *s; |
433 |
> |
static int |
434 |
> |
headline( /* process Radiance input header line */ |
435 |
> |
char *s, |
436 |
> |
void *p |
437 |
> |
) |
438 |
|
{ |
439 |
+ |
static int tmstrlen = 0; |
440 |
+ |
static int ownstrlen = 0; |
441 |
|
char fmt[32]; |
442 |
|
|
443 |
+ |
if (!tmstrlen) |
444 |
+ |
tmstrlen = strlen(TMSTR); |
445 |
+ |
if (!ownstrlen) |
446 |
+ |
ownstrlen = strlen(OWNSTR); |
447 |
|
if (formatval(fmt, s)) { |
448 |
|
if (!strcmp(fmt, COLRFMT)) |
449 |
|
CLR(C_XYZE); |
466 |
|
SET(C_PRIM); |
467 |
|
return(1); |
468 |
|
} |
469 |
+ |
if (isdate(s)) { |
470 |
+ |
if (s[tmstrlen] == ' ') |
471 |
+ |
strncpy(cvts.capdate, s+tmstrlen+1, 19); |
472 |
+ |
else |
473 |
+ |
strncpy(cvts.capdate, s+tmstrlen, 19); |
474 |
+ |
cvts.capdate[19] = '\0'; |
475 |
+ |
return(1); |
476 |
+ |
} |
477 |
+ |
if (!strncmp(s, OWNSTR, ownstrlen)) { |
478 |
+ |
register char *cp = s + ownstrlen; |
479 |
+ |
|
480 |
+ |
while (isspace(*cp)) |
481 |
+ |
++cp; |
482 |
+ |
strncpy(cvts.owner, cp, sizeof(cvts.owner)); |
483 |
+ |
cvts.owner[sizeof(cvts.owner)-1] = '\0'; |
484 |
+ |
for (cp = cvts.owner; *cp; cp++) |
485 |
+ |
; |
486 |
+ |
while (cp > cvts.owner && isspace(cp[-1])) |
487 |
+ |
*--cp = '\0'; |
488 |
+ |
return(1); |
489 |
+ |
} |
490 |
|
return(0); |
491 |
|
} |
492 |
|
|
493 |
|
|
494 |
< |
initfromrad() /* initialize input from a Radiance picture */ |
494 |
> |
static void |
495 |
> |
initfromrad(void) /* initialize input from a Radiance picture */ |
496 |
|
{ |
497 |
|
int i1, i2, po; |
498 |
|
/* read Radiance header */ |
499 |
|
CLR(C_RFLT|C_XYZE|C_PRIM|C_GAMMA|C_CXFM); |
500 |
+ |
cvts.capdate[0] = '\0'; |
501 |
+ |
cvts.owner[0] = '\0'; |
502 |
|
cvts.stonits = 1.; |
503 |
|
cvts.pixrat = 1.; |
504 |
|
cvts.pconf = PLANARCONFIG_CONTIG; |
565 |
|
cvts.tf = Color2RRGGBB; |
566 |
|
SET(C_RFLT); |
567 |
|
} else if (CHK(C_TFLT)) { |
568 |
+ |
TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT, |
569 |
+ |
SAMPLEFORMAT_IEEEFP); |
570 |
|
cvts.tf = Color2RfGfBf; |
571 |
|
SET(C_RFLT); |
572 |
+ |
CLR(C_GAMUT); |
573 |
|
} else |
574 |
|
cvts.tf = Colr2RGB; |
575 |
|
break; |
580 |
|
cvts.tf = Color2GGry; |
581 |
|
SET(C_RFLT); |
582 |
|
} else if (CHK(C_TFLT)) { |
583 |
+ |
TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT, |
584 |
+ |
SAMPLEFORMAT_IEEEFP); |
585 |
|
cvts.tf = Color2Gryf; |
586 |
|
SET(C_RFLT); |
587 |
|
} else |
604 |
|
TIFFSetField(cvts.tif, TIFFTAG_PLANARCONFIG, cvts.pconf); |
605 |
|
TIFFSetField(cvts.tif, TIFFTAG_STONITS, |
606 |
|
cvts.stonits/pow(2.,(double)cvts.bradj)); |
607 |
+ |
if (cvts.capdate[0]) |
608 |
+ |
TIFFSetField(cvts.tif, TIFFTAG_DATETIME, cvts.capdate); |
609 |
+ |
if (cvts.owner[0]) |
610 |
+ |
TIFFSetField(cvts.tif, TIFFTAG_ARTIST, cvts.owner); |
611 |
|
if (cvts.comp == COMPRESSION_NONE) |
612 |
|
i1 = TIFFScanlineSize(cvts.tif); |
613 |
|
else |
620 |
|
} |
621 |
|
|
622 |
|
|
623 |
< |
ra2tiff(ac, av) /* convert Radiance picture to TIFF image */ |
624 |
< |
int ac; |
625 |
< |
char *av[]; |
623 |
> |
static void |
624 |
> |
ra2tiff( /* convert Radiance picture to TIFF image */ |
625 |
> |
int ac, |
626 |
> |
char *av[] |
627 |
> |
) |
628 |
|
{ |
629 |
|
uint32 y; |
630 |
|
/* open Radiance file */ |
646 |
|
} |
647 |
|
|
648 |
|
|
649 |
< |
void |
650 |
< |
Luv2Color(y) /* read/convert/write Luv->COLOR scanline */ |
651 |
< |
uint32 y; |
649 |
> |
static void |
650 |
> |
Luv2Color( /* read/convert/write Luv->COLOR scanline */ |
651 |
> |
uint32 y |
652 |
> |
) |
653 |
|
{ |
654 |
|
register int x; |
655 |
|
|
656 |
|
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT)) |
657 |
|
quiterr("internal error 1 in Luv2Color"); |
658 |
|
|
659 |
+ |
if (cvts.pconf != PLANARCONFIG_CONTIG) |
660 |
+ |
quiterr("cannot handle separate 32-bit color planes"); |
661 |
+ |
|
662 |
|
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
663 |
|
quiterr("error reading TIFF input"); |
664 |
|
/* also works for float RGB */ |
685 |
|
} |
686 |
|
|
687 |
|
|
688 |
< |
void |
689 |
< |
RRGGBB2Color(y) /* read/convert/write RGB16->COLOR scanline */ |
690 |
< |
uint32 y; |
688 |
> |
static void |
689 |
> |
RRGGBB2Color( /* read/convert/write RGB16->COLOR scanline */ |
690 |
> |
uint32 y |
691 |
> |
) |
692 |
|
{ |
693 |
< |
int dogamma = cvts.gamcor < 0.99 | cvts.gamcor > 1.01; |
693 |
> |
int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01); |
694 |
|
register double d; |
695 |
|
register int x; |
696 |
|
|
697 |
|
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_TWRD|C_RFLT)) |
698 |
|
quiterr("internal error 1 in RRGGBB2Color"); |
699 |
|
|
700 |
+ |
if (cvts.pconf != PLANARCONFIG_CONTIG) |
701 |
+ |
quiterr("cannot handle separate 16-bit color planes"); |
702 |
+ |
|
703 |
|
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
704 |
|
quiterr("error reading TIFF input"); |
705 |
|
|
716 |
|
if (CHK(C_CXFM)) |
717 |
|
colortrans(cvts.r.colors[x], cvts.cmat, |
718 |
|
cvts.r.colors[x]); |
624 |
– |
if (CHK(C_GAMUT)) |
625 |
– |
clipgamut(cvts.r.colors[x], cvts.t.fp[3*x + 1], |
626 |
– |
CGAMUT_LOWER, cblack, cwhite); |
719 |
|
} |
720 |
|
if (cvts.bradj) { |
721 |
|
d = pow(2.,(double)cvts.bradj); |
728 |
|
} |
729 |
|
|
730 |
|
|
731 |
< |
void |
732 |
< |
L2Color(y) /* read/convert/write Lfloat->COLOR scanline */ |
733 |
< |
uint32 y; |
731 |
> |
static void |
732 |
> |
L2Color( /* read/convert/write Lfloat->COLOR scanline */ |
733 |
> |
uint32 y |
734 |
> |
) |
735 |
|
{ |
736 |
|
float m = pow(2., (double)cvts.bradj); |
737 |
|
register int x; |
752 |
|
} |
753 |
|
|
754 |
|
|
755 |
< |
void |
756 |
< |
RGB2Colr(y) /* read/convert/write RGB->COLR scanline */ |
757 |
< |
uint32 y; |
755 |
> |
static void |
756 |
> |
RGB2Colr( /* read/convert/write RGB->COLR scanline */ |
757 |
> |
uint32 y |
758 |
> |
) |
759 |
|
{ |
760 |
|
COLOR ctmp; |
761 |
|
register int x; |
809 |
|
} |
810 |
|
|
811 |
|
|
812 |
< |
void |
813 |
< |
Gry2Colr(y) /* read/convert/write G8->COLR scanline */ |
814 |
< |
uint32 y; |
812 |
> |
static void |
813 |
> |
Gry2Colr( /* read/convert/write G8->COLR scanline */ |
814 |
> |
uint32 y |
815 |
> |
) |
816 |
|
{ |
817 |
|
register int x; |
818 |
|
|
836 |
|
} |
837 |
|
|
838 |
|
|
839 |
< |
void |
840 |
< |
GGry2Color(y) /* read/convert/write G16->COLOR scanline */ |
841 |
< |
uint32 y; |
839 |
> |
static void |
840 |
> |
GGry2Color( /* read/convert/write G16->COLOR scanline */ |
841 |
> |
uint32 y |
842 |
> |
) |
843 |
|
{ |
844 |
< |
int dogamma = cvts.gamcor < 0.99 | cvts.gamcor > 1.01; |
844 |
> |
int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01); |
845 |
|
double m; |
846 |
|
register double d; |
847 |
|
register int x; |
867 |
|
} |
868 |
|
|
869 |
|
|
870 |
< |
void |
871 |
< |
Color2GGry(y) /* read/convert/write COLOR->G16 scanline */ |
872 |
< |
uint32 y; |
870 |
> |
static void |
871 |
> |
Color2GGry( /* read/convert/write COLOR->G16 scanline */ |
872 |
> |
uint32 y |
873 |
> |
) |
874 |
|
{ |
875 |
< |
int dogamma = cvts.gamcor < 0.99 | cvts.gamcor > 1.01; |
875 |
> |
int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01); |
876 |
|
float m = pow(2.,(double)cvts.bradj); |
877 |
|
register int x; |
878 |
|
|
902 |
|
} |
903 |
|
|
904 |
|
|
905 |
< |
void |
906 |
< |
Color2L(y) /* read/convert/write COLOR->Lfloat scanline */ |
907 |
< |
uint32 y; |
905 |
> |
static void |
906 |
> |
Color2L( /* read/convert/write COLOR->Lfloat scanline */ |
907 |
> |
uint32 y |
908 |
> |
) |
909 |
|
{ |
910 |
|
float m = pow(2.,(double)cvts.bradj); |
911 |
|
register int x; |
925 |
|
} |
926 |
|
|
927 |
|
|
928 |
< |
void |
929 |
< |
Color2Luv(y) /* read/convert/write COLOR->Luv scanline */ |
930 |
< |
uint32 y; |
928 |
> |
static void |
929 |
> |
Color2Luv( /* read/convert/write COLOR->Luv scanline */ |
930 |
> |
uint32 y |
931 |
> |
) |
932 |
|
{ |
933 |
|
register int x; |
934 |
|
|
959 |
|
} |
960 |
|
|
961 |
|
|
962 |
< |
void |
963 |
< |
Color2RRGGBB(y) /* read/convert/write COLOR->RGB16 scanline */ |
964 |
< |
uint32 y; |
962 |
> |
static void |
963 |
> |
Color2RRGGBB( /* read/convert/write COLOR->RGB16 scanline */ |
964 |
> |
uint32 y |
965 |
> |
) |
966 |
|
{ |
967 |
< |
int dogamma = cvts.gamcor < 0.99 | cvts.gamcor > 1.01; |
967 |
> |
int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01); |
968 |
|
float m = pow(2.,(double)cvts.bradj); |
969 |
|
register int x, i; |
970 |
|
|
974 |
|
if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) |
975 |
|
quiterr("error reading Radiance picture"); |
976 |
|
|
977 |
< |
for (x = cvts.xmax; x--; ) |
977 |
> |
for (x = cvts.xmax; x--; ) { |
978 |
> |
if (CHK(C_CXFM)) { |
979 |
> |
colortrans(cvts.r.colors[x], cvts.cmat, |
980 |
> |
cvts.r.colors[x]); |
981 |
> |
if (CHK(C_GAMUT)) |
982 |
> |
clipgamut(cvts.r.colors[x], bright(cvts.r.colors[x]), |
983 |
> |
CGAMUT_LOWER, cblack, cwhite); |
984 |
> |
} |
985 |
|
for (i = 3; i--; ) { |
986 |
|
register float f = m*colval(cvts.r.colors[x],i); |
987 |
|
if (f <= 0) |
994 |
|
else |
995 |
|
cvts.t.wp[3*x + i] = (int)((float)(1L<<16)*f); |
996 |
|
} |
997 |
+ |
} |
998 |
|
|
999 |
|
if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
1000 |
|
quiterr("error writing TIFF output"); |
1001 |
|
} |
1002 |
|
|
1003 |
|
|
1004 |
< |
void |
1005 |
< |
Colr2Gry(y) /* read/convert/write COLR->RGB scanline */ |
1006 |
< |
uint32 y; |
1004 |
> |
static void |
1005 |
> |
Colr2Gry( /* read/convert/write COLR->RGB scanline */ |
1006 |
> |
uint32 y |
1007 |
> |
) |
1008 |
|
{ |
1009 |
|
register int x; |
1010 |
|
|
1028 |
|
} |
1029 |
|
|
1030 |
|
|
1031 |
< |
void |
1032 |
< |
Colr2RGB(y) /* read/convert/write COLR->RGB scanline */ |
1033 |
< |
uint32 y; |
1031 |
> |
static void |
1032 |
> |
Colr2RGB( /* read/convert/write COLR->RGB scanline */ |
1033 |
> |
uint32 y |
1034 |
> |
) |
1035 |
|
{ |
1036 |
|
COLOR ctmp; |
1037 |
|
register int x; |