| 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 */ |
| 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, |
| 66 |
|
PLANARCONFIG_CONTIG, GAMCOR, 0, 1, 1., 1., |
| 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 |
| 109 |
|
char *progname; |
| 110 |
|
|
| 111 |
|
|
| 112 |
< |
main(argc, argv) |
| 113 |
< |
int argc; |
| 99 |
< |
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; |
| 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, |
| 362 |
|
quiterr("unknown input image resolution"); |
| 363 |
|
|
| 364 |
|
if (!TIFFGetField(cvts.tif, TIFFTAG_STONITS, &cvts.stonits)) |
| 365 |
< |
cvts.stonits = 1.; |
| 365 |
> |
cvts.stonits = -1.; |
| 366 |
|
|
| 367 |
|
if (!TIFFGetField(cvts.tif, TIFFTAG_DATETIME, &cp)) |
| 368 |
|
cvts.capdate[0] = '\0'; |
| 380 |
|
if (cvts.pixrat < .99 || cvts.pixrat > 1.01) |
| 381 |
|
fputaspect(cvts.pixrat, cvts.rfp); |
| 382 |
|
if (CHK(C_XYZE)) { |
| 383 |
< |
fputexpos(pow(2.,(double)cvts.bradj)/cvts.stonits, cvts.rfp); |
| 383 |
> |
if (cvts.stonits > .0) |
| 384 |
> |
fputexpos(pow(2.,(double)cvts.bradj)/cvts.stonits, cvts.rfp); |
| 385 |
|
fputformat(CIEFMT, cvts.rfp); |
| 386 |
|
} else { |
| 387 |
|
if (CHK(C_PRIM)) |
| 388 |
|
fputprims(cvts.prims, cvts.rfp); |
| 389 |
< |
fputexpos(WHTEFFICACY*pow(2.,(double)cvts.bradj)/cvts.stonits, |
| 390 |
< |
cvts.rfp); |
| 389 |
> |
if (cvts.stonits > .0) |
| 390 |
> |
fputexpos(WHTEFFICACY*pow(2.,(double)cvts.bradj)/cvts.stonits, |
| 391 |
> |
cvts.rfp); |
| 392 |
|
fputformat(COLRFMT, cvts.rfp); |
| 393 |
|
} |
| 394 |
|
if (cvts.capdate[0]) |
| 400 |
|
} |
| 401 |
|
|
| 402 |
|
|
| 403 |
< |
tiff2ra(ac, av) /* convert TIFF image to Radiance picture */ |
| 404 |
< |
int ac; |
| 405 |
< |
char *av[]; |
| 403 |
> |
static void |
| 404 |
> |
tiff2ra( /* convert TIFF image to Radiance picture */ |
| 405 |
> |
int ac, |
| 406 |
> |
char *av[] |
| 407 |
> |
) |
| 408 |
|
{ |
| 409 |
|
int32 y; |
| 410 |
|
/* open TIFF input */ |
| 432 |
|
} |
| 433 |
|
|
| 434 |
|
|
| 435 |
< |
int |
| 436 |
< |
headline(s) /* process Radiance input header line */ |
| 437 |
< |
char *s; |
| 435 |
> |
static int |
| 436 |
> |
headline( /* process Radiance input header line */ |
| 437 |
> |
char *s, |
| 438 |
> |
void *p |
| 439 |
> |
) |
| 440 |
|
{ |
| 441 |
|
static int tmstrlen = 0; |
| 442 |
|
static int ownstrlen = 0; |
| 493 |
|
} |
| 494 |
|
|
| 495 |
|
|
| 496 |
< |
initfromrad() /* initialize input from a Radiance picture */ |
| 496 |
> |
static void |
| 497 |
> |
initfromrad(void) /* initialize input from a Radiance picture */ |
| 498 |
|
{ |
| 499 |
|
int i1, i2, po; |
| 500 |
|
/* read Radiance header */ |
| 571 |
|
SAMPLEFORMAT_IEEEFP); |
| 572 |
|
cvts.tf = Color2RfGfBf; |
| 573 |
|
SET(C_RFLT); |
| 574 |
+ |
CLR(C_GAMUT); |
| 575 |
|
} else |
| 576 |
|
cvts.tf = Colr2RGB; |
| 577 |
|
break; |
| 622 |
|
} |
| 623 |
|
|
| 624 |
|
|
| 625 |
< |
ra2tiff(ac, av) /* convert Radiance picture to TIFF image */ |
| 626 |
< |
int ac; |
| 627 |
< |
char *av[]; |
| 625 |
> |
static void |
| 626 |
> |
ra2tiff( /* convert Radiance picture to TIFF image */ |
| 627 |
> |
int ac, |
| 628 |
> |
char *av[] |
| 629 |
> |
) |
| 630 |
|
{ |
| 631 |
|
uint32 y; |
| 632 |
|
/* open Radiance file */ |
| 648 |
|
} |
| 649 |
|
|
| 650 |
|
|
| 651 |
< |
void |
| 652 |
< |
Luv2Color(y) /* read/convert/write Luv->COLOR scanline */ |
| 653 |
< |
uint32 y; |
| 651 |
> |
static void |
| 652 |
> |
Luv2Color( /* read/convert/write Luv->COLOR scanline */ |
| 653 |
> |
uint32 y |
| 654 |
> |
) |
| 655 |
|
{ |
| 656 |
|
register int x; |
| 657 |
|
|
| 658 |
|
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT)) |
| 659 |
|
quiterr("internal error 1 in Luv2Color"); |
| 660 |
|
|
| 661 |
+ |
if (cvts.pconf != PLANARCONFIG_CONTIG) |
| 662 |
+ |
quiterr("cannot handle separate 32-bit color planes"); |
| 663 |
+ |
|
| 664 |
|
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
| 665 |
|
quiterr("error reading TIFF input"); |
| 666 |
|
/* also works for float RGB */ |
| 687 |
|
} |
| 688 |
|
|
| 689 |
|
|
| 690 |
< |
void |
| 691 |
< |
RRGGBB2Color(y) /* read/convert/write RGB16->COLOR scanline */ |
| 692 |
< |
uint32 y; |
| 690 |
> |
static void |
| 691 |
> |
RRGGBB2Color( /* read/convert/write RGB16->COLOR scanline */ |
| 692 |
> |
uint32 y |
| 693 |
> |
) |
| 694 |
|
{ |
| 695 |
< |
int dogamma = cvts.gamcor < 0.99 | cvts.gamcor > 1.01; |
| 695 |
> |
int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01); |
| 696 |
|
register double d; |
| 697 |
|
register int x; |
| 698 |
|
|
| 699 |
|
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_TWRD|C_RFLT)) |
| 700 |
|
quiterr("internal error 1 in RRGGBB2Color"); |
| 701 |
|
|
| 702 |
+ |
if (cvts.pconf != PLANARCONFIG_CONTIG) |
| 703 |
+ |
quiterr("cannot handle separate 16-bit color planes"); |
| 704 |
+ |
|
| 705 |
|
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
| 706 |
|
quiterr("error reading TIFF input"); |
| 707 |
|
|
| 718 |
|
if (CHK(C_CXFM)) |
| 719 |
|
colortrans(cvts.r.colors[x], cvts.cmat, |
| 720 |
|
cvts.r.colors[x]); |
| 684 |
– |
if (CHK(C_GAMUT)) |
| 685 |
– |
clipgamut(cvts.r.colors[x], cvts.t.fp[3*x + 1], |
| 686 |
– |
CGAMUT_LOWER, cblack, cwhite); |
| 721 |
|
} |
| 722 |
|
if (cvts.bradj) { |
| 723 |
|
d = pow(2.,(double)cvts.bradj); |
| 730 |
|
} |
| 731 |
|
|
| 732 |
|
|
| 733 |
< |
void |
| 734 |
< |
L2Color(y) /* read/convert/write Lfloat->COLOR scanline */ |
| 735 |
< |
uint32 y; |
| 733 |
> |
static void |
| 734 |
> |
L2Color( /* read/convert/write Lfloat->COLOR scanline */ |
| 735 |
> |
uint32 y |
| 736 |
> |
) |
| 737 |
|
{ |
| 738 |
|
float m = pow(2., (double)cvts.bradj); |
| 739 |
|
register int x; |
| 754 |
|
} |
| 755 |
|
|
| 756 |
|
|
| 757 |
< |
void |
| 758 |
< |
RGB2Colr(y) /* read/convert/write RGB->COLR scanline */ |
| 759 |
< |
uint32 y; |
| 757 |
> |
static void |
| 758 |
> |
RGB2Colr( /* read/convert/write RGB->COLR scanline */ |
| 759 |
> |
uint32 y |
| 760 |
> |
) |
| 761 |
|
{ |
| 762 |
|
COLOR ctmp; |
| 763 |
|
register int x; |
| 811 |
|
} |
| 812 |
|
|
| 813 |
|
|
| 814 |
< |
void |
| 815 |
< |
Gry2Colr(y) /* read/convert/write G8->COLR scanline */ |
| 816 |
< |
uint32 y; |
| 814 |
> |
static void |
| 815 |
> |
Gry2Colr( /* read/convert/write G8->COLR scanline */ |
| 816 |
> |
uint32 y |
| 817 |
> |
) |
| 818 |
|
{ |
| 819 |
|
register int x; |
| 820 |
|
|
| 838 |
|
} |
| 839 |
|
|
| 840 |
|
|
| 841 |
< |
void |
| 842 |
< |
GGry2Color(y) /* read/convert/write G16->COLOR scanline */ |
| 843 |
< |
uint32 y; |
| 841 |
> |
static void |
| 842 |
> |
GGry2Color( /* read/convert/write G16->COLOR scanline */ |
| 843 |
> |
uint32 y |
| 844 |
> |
) |
| 845 |
|
{ |
| 846 |
< |
int dogamma = cvts.gamcor < 0.99 | cvts.gamcor > 1.01; |
| 846 |
> |
int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01); |
| 847 |
|
double m; |
| 848 |
|
register double d; |
| 849 |
|
register int x; |
| 869 |
|
} |
| 870 |
|
|
| 871 |
|
|
| 872 |
< |
void |
| 873 |
< |
Color2GGry(y) /* read/convert/write COLOR->G16 scanline */ |
| 874 |
< |
uint32 y; |
| 872 |
> |
static void |
| 873 |
> |
Color2GGry( /* read/convert/write COLOR->G16 scanline */ |
| 874 |
> |
uint32 y |
| 875 |
> |
) |
| 876 |
|
{ |
| 877 |
< |
int dogamma = cvts.gamcor < 0.99 | cvts.gamcor > 1.01; |
| 877 |
> |
int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01); |
| 878 |
|
float m = pow(2.,(double)cvts.bradj); |
| 879 |
|
register int x; |
| 880 |
|
|
| 904 |
|
} |
| 905 |
|
|
| 906 |
|
|
| 907 |
< |
void |
| 908 |
< |
Color2L(y) /* read/convert/write COLOR->Lfloat scanline */ |
| 909 |
< |
uint32 y; |
| 907 |
> |
static void |
| 908 |
> |
Color2L( /* read/convert/write COLOR->Lfloat scanline */ |
| 909 |
> |
uint32 y |
| 910 |
> |
) |
| 911 |
|
{ |
| 912 |
|
float m = pow(2.,(double)cvts.bradj); |
| 913 |
|
register int x; |
| 927 |
|
} |
| 928 |
|
|
| 929 |
|
|
| 930 |
< |
void |
| 931 |
< |
Color2Luv(y) /* read/convert/write COLOR->Luv scanline */ |
| 932 |
< |
uint32 y; |
| 930 |
> |
static void |
| 931 |
> |
Color2Luv( /* read/convert/write COLOR->Luv scanline */ |
| 932 |
> |
uint32 y |
| 933 |
> |
) |
| 934 |
|
{ |
| 935 |
|
register int x; |
| 936 |
|
|
| 961 |
|
} |
| 962 |
|
|
| 963 |
|
|
| 964 |
< |
void |
| 965 |
< |
Color2RRGGBB(y) /* read/convert/write COLOR->RGB16 scanline */ |
| 966 |
< |
uint32 y; |
| 964 |
> |
static void |
| 965 |
> |
Color2RRGGBB( /* read/convert/write COLOR->RGB16 scanline */ |
| 966 |
> |
uint32 y |
| 967 |
> |
) |
| 968 |
|
{ |
| 969 |
< |
int dogamma = cvts.gamcor < 0.99 | cvts.gamcor > 1.01; |
| 969 |
> |
int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01); |
| 970 |
|
float m = pow(2.,(double)cvts.bradj); |
| 971 |
|
register int x, i; |
| 972 |
|
|
| 976 |
|
if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) |
| 977 |
|
quiterr("error reading Radiance picture"); |
| 978 |
|
|
| 979 |
< |
for (x = cvts.xmax; x--; ) |
| 979 |
> |
for (x = cvts.xmax; x--; ) { |
| 980 |
> |
if (CHK(C_CXFM)) { |
| 981 |
> |
colortrans(cvts.r.colors[x], cvts.cmat, |
| 982 |
> |
cvts.r.colors[x]); |
| 983 |
> |
if (CHK(C_GAMUT)) |
| 984 |
> |
clipgamut(cvts.r.colors[x], bright(cvts.r.colors[x]), |
| 985 |
> |
CGAMUT_LOWER, cblack, cwhite); |
| 986 |
> |
} |
| 987 |
|
for (i = 3; i--; ) { |
| 988 |
|
register float f = m*colval(cvts.r.colors[x],i); |
| 989 |
|
if (f <= 0) |
| 996 |
|
else |
| 997 |
|
cvts.t.wp[3*x + i] = (int)((float)(1L<<16)*f); |
| 998 |
|
} |
| 999 |
+ |
} |
| 1000 |
|
|
| 1001 |
|
if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
| 1002 |
|
quiterr("error writing TIFF output"); |
| 1003 |
|
} |
| 1004 |
|
|
| 1005 |
|
|
| 1006 |
< |
void |
| 1007 |
< |
Colr2Gry(y) /* read/convert/write COLR->RGB scanline */ |
| 1008 |
< |
uint32 y; |
| 1006 |
> |
static void |
| 1007 |
> |
Colr2Gry( /* read/convert/write COLR->RGB scanline */ |
| 1008 |
> |
uint32 y |
| 1009 |
> |
) |
| 1010 |
|
{ |
| 1011 |
|
register int x; |
| 1012 |
|
|
| 1030 |
|
} |
| 1031 |
|
|
| 1032 |
|
|
| 1033 |
< |
void |
| 1034 |
< |
Colr2RGB(y) /* read/convert/write COLR->RGB scanline */ |
| 1035 |
< |
uint32 y; |
| 1033 |
> |
static void |
| 1034 |
> |
Colr2RGB( /* read/convert/write COLR->RGB scanline */ |
| 1035 |
> |
uint32 y |
| 1036 |
> |
) |
| 1037 |
|
{ |
| 1038 |
|
COLOR ctmp; |
| 1039 |
|
register int x; |