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