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