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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines