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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines