ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_tiff.c
(Generate patch)

Comparing ray/src/px/ra_tiff.c (file contents):
Revision 2.6 by greg, Sun Feb 27 10:17:27 1994 UTC vs.
Revision 2.34 by greg, Sat Dec 4 00:24:43 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines