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 1.1 by greg, Thu Aug 15 13:34:58 1991 UTC vs.
Revision 2.31 by greg, Tue Jun 7 16:54:23 2005 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"
18 + #include  "resolu.h"
19  
20 < extern double  atof();
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 < double  gamma = 2.2;                    /* gamma correction */
33 > typedef void colcvf_t(uint32 y);
34  
35 < int  bradj = 0;                         /* brightness adjustment */
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 + #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 + 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;
30 < char  *argv[];
112 > int
113 > main(int  argc, char  *argv[])
114   {
115          int  reverse = 0;
116          int  i;
# Line 37 | Line 120 | char  *argv[];
120          for (i = 1; i < argc; i++)
121                  if (argv[i][0] == '-')
122                          switch (argv[i][1]) {
123 <                        /* not allowed to reset gamma...
124 <                        case 'g':
42 <                                gamma = atof(argv[++i]);
123 >                        case 'g':               /* gamma correction */
124 >                                cvts.gamcor = atof(argv[++i]);
125                                  break;
126 <                        */
127 <                        case 'e':
126 >                        case 'x':               /* XYZE Radiance output */
127 >                                TGL(C_XYZE);
128 >                                break;
129 >                        case 'z':               /* LZW compressed output */
130 >                                cvts.comp = COMPRESSION_LZW;
131 >                                break;
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 58 | Line 162 | char  *argv[];
162                  else
163                          break;
164   doneopts:
165 <        setcolrgam(gamma);
165 >        if (reverse) {
166  
63        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, "Usage: %s [-r][-e +/-stops] input output\n",
193 >        fprintf(stderr,
194 >        "Usage: %s [-z|-L|-l|-f|-w][-b][-e +/-stops][-g gamma] {in.pic|-} out.tif\n",
195                          progname);
196 +        fprintf(stderr,
197 +        "   Or: %s -r [-x][-e +/-stops][-g gamma] in.tif [out.pic|-]\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 90 | 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;
222 <        unsigned short  hi;
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 <        register BYTE   *scanin;
238 <        register COLR   *scanout;
239 <        register int    x;
240 <        int     y;
241 <                                        /* open/check  TIFF file */
242 <        if ((tif = TIFFOpen(inpf, "r")) == NULL)
243 <                quiterr("cannot open TIFF input");
244 <        if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &hi) || hi != 3)
245 <                quiterr("unsupported samples per pixel");
246 <        if (!TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &hi) || hi != 8)
247 <                quiterr("unsupported bits per sample");
248 <        if (TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &hi) && hi != 2)
249 <                quiterr("unsupported photometric interpretation");
250 <        if (!TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &pconfig) ||
251 <                        (pconfig != 1 && pconfig != 2))
252 <                quiterr("unsupported planar configuration");
253 <        if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &xmax) ||
254 <                        !TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &ymax))
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");
129 <        if (TIFFGetField(tif, TIFFTAG_IMAGEDESCRIPTION, &cp)) {
130 <                while (*cp && *cp == '\n')
131 <                        cp++;
132 <                for (x = 0; cp[x]; x++)
133 <                        if (cp[x] != '\n' || cp[x+1] != '\n')
134 <                                putchar(cp[x]);
135 <                if (x && cp[x-1] != '\n')
136 <                        putchar('\n');
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 <        fputs(progname, stdout);
374 <        if (bradj)
375 <                printf(" -e %+d", bradj);
376 <        fputs(" -r\n", stdout);
377 <        fputformat(COLRFMT, stdout);
378 <        putchar('\n');
379 <        fputresolu(YDECR|YMAJOR, xmax, ymax, stdout);
380 <                                                /* convert image */
381 <        for (y = 0; y < ymax; y++) {
382 <                if (pconfig == 1) {
383 <                        if (TIFFReadScanline(tif, scanin, y, 0) < 0)
384 <                                goto readerr;
385 <                        for (x = 0; x < xmax; x++) {
386 <                                scanout[x][RED] = scanin[3*x];
387 <                                scanout[x][GRN] = scanin[3*x+1];
388 <                                scanout[x][BLU] = scanin[3*x+2];
389 <                        }
390 <                } else {
391 <                        if (TIFFReadScanline(tif, scanin, y, 0) < 0)
392 <                                goto readerr;
393 <                        for (x = 0; x < xmax; x++)
394 <                                scanout[x][RED] = scanin[x];
395 <                        if (TIFFReadScanline(tif, scanin, y, 1) < 0)
396 <                                goto readerr;
397 <                        for (x = 0; x < xmax; x++)
398 <                                scanout[x][GRN] = scanin[x];
399 <                        if (TIFFReadScanline(tif, scanin, y, 2) < 0)
400 <                                goto readerr;
401 <                        for (x = 0; x < xmax; x++)
402 <                                scanout[x][BLU] = scanin[x];
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 >                fputexpos(pow(2.,(double)cvts.bradj)/cvts.stonits, cvts.rfp);
384 >                fputformat(CIEFMT, cvts.rfp);
385 >        } else {
386 >                if (CHK(C_PRIM))
387 >                        fputprims(cvts.prims, cvts.rfp);
388 >                fputexpos(WHTEFFICACY*pow(2.,(double)cvts.bradj)/cvts.stonits,
389 >                                cvts.rfp);
390 >                fputformat(COLRFMT, cvts.rfp);
391 >        }
392 >        if (cvts.capdate[0])
393 >                fprintf(cvts.rfp, "%s %s\n", TMSTR, cvts.capdate);
394 >        if (cvts.owner[0])
395 >                fprintf(cvts.rfp, "%s %s\n", OWNSTR, cvts.owner);
396 >
397 >        allocbufs();                    /* allocate scanline buffers */
398 > }
399 >
400 >
401 > static void
402 > tiff2ra(                /* convert TIFF image to Radiance picture */
403 >        int  ac,
404 >        char  *av[]
405 > )
406 > {
407 >        int32   y;
408 >                                        /* open TIFF input */
409 >        if ((cvts.tif = TIFFOpen(av[ac], "r")) == NULL)
410 >                quiterr("cannot open TIFF input");
411 >                                        /* open Radiance output */
412 >        if (av[ac+1] == NULL || !strcmp(av[ac+1], "-"))
413 >                cvts.rfp = stdout;
414 >        else if ((cvts.rfp = fopen(av[ac+1], "w")) == NULL)
415 >                quiterr("cannot open Radiance output picture");
416 >                                        /* start output header */
417 >        newheader("RADIANCE", cvts.rfp);
418 >        printargs(ac, av, cvts.rfp);
419 >
420 >        initfromtif();                  /* initialize conversion */
421 >
422 >        fputc('\n', cvts.rfp);          /* finish Radiance header */
423 >        fputresolu(pixorder(), (int)cvts.xmax, (int)cvts.ymax, cvts.rfp);
424 >
425 >        for (y = 0; y < cvts.ymax; y++)         /* convert image */
426 >                (*cvts.tf)(y);
427 >                                                /* clean up */
428 >        fclose(cvts.rfp);
429 >        TIFFClose(cvts.tif);
430 > }
431 >
432 >
433 > static int
434 > headline(                       /* process Radiance input header line */
435 >        char    *s,
436 >        void    *p
437 > )
438 > {
439 >        static int      tmstrlen = 0;
440 >        static int      ownstrlen = 0;
441 >        char    fmt[32];
442 >
443 >        if (!tmstrlen)
444 >                tmstrlen = strlen(TMSTR);
445 >        if (!ownstrlen)
446 >                ownstrlen = strlen(OWNSTR);
447 >        if (formatval(fmt, s)) {
448 >                if (!strcmp(fmt, COLRFMT))
449 >                        CLR(C_XYZE);
450 >                else if (!strcmp(fmt, CIEFMT))
451 >                        SET(C_XYZE);
452 >                else
453 >                        quiterr("unrecognized input picture format");
454 >                return(1);
455 >        }
456 >        if (isexpos(s)) {
457 >                cvts.stonits /= exposval(s);
458 >                return(1);
459 >        }
460 >        if (isaspect(s)) {
461 >                cvts.pixrat *= aspectval(s);
462 >                return(1);
463 >        }
464 >        if (isprims(s)) {
465 >                primsval(cvts.prims, s);
466 >                SET(C_PRIM);
467 >                return(1);
468 >        }
469 >        if (isdate(s)) {
470 >                if (s[tmstrlen] == ' ')
471 >                        strncpy(cvts.capdate, s+tmstrlen+1, 19);
472 >                else
473 >                        strncpy(cvts.capdate, s+tmstrlen, 19);
474 >                cvts.capdate[19] = '\0';
475 >                return(1);
476 >        }
477 >        if (!strncmp(s, OWNSTR, ownstrlen)) {
478 >                register char   *cp = s + ownstrlen;
479 >
480 >                while (isspace(*cp))
481 >                        ++cp;
482 >                strncpy(cvts.owner, cp, sizeof(cvts.owner));
483 >                cvts.owner[sizeof(cvts.owner)-1] = '\0';
484 >                for (cp = cvts.owner; *cp; cp++)
485 >                        ;
486 >                while (cp > cvts.owner && isspace(cp[-1]))
487 >                        *--cp = '\0';
488 >                return(1);
489 >        }
490 >        return(0);
491 > }
492 >
493 >
494 > static void
495 > initfromrad(void)                       /* initialize input from a Radiance picture */
496 > {
497 >        int     i1, i2, po;
498 >                                                /* read Radiance header */
499 >        CLR(C_RFLT|C_XYZE|C_PRIM|C_GAMMA|C_CXFM);
500 >        cvts.capdate[0] = '\0';
501 >        cvts.owner[0] = '\0';
502 >        cvts.stonits = 1.;
503 >        cvts.pixrat = 1.;
504 >        cvts.pconf = PLANARCONFIG_CONTIG;
505 >        getheader(cvts.rfp, headline, NULL);
506 >        if ((po = fgetresolu(&i1, &i2, cvts.rfp)) < 0)
507 >                quiterr("bad Radiance picture");
508 >        cvts.xmax = i1; cvts.ymax = i2;
509 >        for (i1 = 0; i1 < 8; i1++)              /* interpret orientation */
510 >                if (ortab[i1] == po) {
511 >                        cvts.orient = i1 + 1;
512 >                        break;
513                  }
514 <                gambs_colrs(scanout, xmax);
515 <                if (bradj)
516 <                        shiftcolrs(scanout, xmax, bradj);
517 <                if (fwritecolrs(scanout, xmax, stdout) < 0)
518 <                        quiterr("error writing Radiance picture");
514 >        if (i1 >= 8)
515 >                quiterr("internal error 1 in initfromrad");
516 >        if (!(po & YMAJOR))
517 >                cvts.pixrat = 1./cvts.pixrat;
518 >        if (!CHK(C_XYZE))
519 >                cvts.stonits *= WHTEFFICACY;
520 >                                                /* set up conversion */
521 >        TIFFSetField(cvts.tif, TIFFTAG_COMPRESSION, cvts.comp);
522 >        TIFFSetField(cvts.tif, TIFFTAG_PHOTOMETRIC, cvts.phot);
523 >
524 >        switch (cvts.phot) {
525 >        case PHOTOMETRIC_LOGLUV:
526 >                SET(C_RFLT|C_TFLT);
527 >                CLR(C_GRY|C_TWRD);
528 >                if (!CHK(C_XYZE)) {
529 >                        comprgb2xyzWBmat(cvts.cmat,
530 >                                        CHK(C_PRIM) ? cvts.prims : stdprims);
531 >                        SET(C_CXFM);
532 >                }
533 >                if (cvts.comp != COMPRESSION_SGILOG &&
534 >                                cvts.comp != COMPRESSION_SGILOG24)
535 >                        quiterr("internal error 2 in initfromrad");
536 >                TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
537 >                                SGILOGDATAFMT_FLOAT);
538 >                cvts.tf = Color2Luv;
539 >                break;
540 >        case PHOTOMETRIC_LOGL:
541 >                SET(C_GRY|C_RFLT|C_TFLT);
542 >                CLR(C_TWRD);
543 >                if (cvts.comp != COMPRESSION_SGILOG)    
544 >                        quiterr("internal error 3 in initfromrad");
545 >                TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
546 >                                SGILOGDATAFMT_FLOAT);
547 >                cvts.tf = Color2L;
548 >                break;
549 >        case PHOTOMETRIC_RGB:
550 >                SET(C_GAMMA|C_GAMUT);
551 >                CLR(C_GRY);
552 >                setcolrgam(cvts.gamcor);
553 >                if (CHK(C_XYZE)) {
554 >                        compxyz2rgbWBmat(cvts.cmat,
555 >                                        CHK(C_PRIM) ? cvts.prims : stdprims);
556 >                        SET(C_CXFM);
557 >                }
558 >                if (CHK(C_PRIM)) {
559 >                        TIFFSetField(cvts.tif, TIFFTAG_PRIMARYCHROMATICITIES,
560 >                                        (float *)cvts.prims);
561 >                        TIFFSetField(cvts.tif, TIFFTAG_WHITEPOINT,
562 >                                        (float *)cvts.prims[WHT]);
563 >                }
564 >                if (CHK(C_TWRD)) {
565 >                        cvts.tf = Color2RRGGBB;
566 >                        SET(C_RFLT);
567 >                } else if (CHK(C_TFLT)) {
568 >                        TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT,
569 >                                        SAMPLEFORMAT_IEEEFP);
570 >                        cvts.tf = Color2RfGfBf;
571 >                        SET(C_RFLT);
572 >                        CLR(C_GAMUT);
573 >                } else
574 >                        cvts.tf = Colr2RGB;
575 >                break;
576 >        case PHOTOMETRIC_MINISBLACK:
577 >                SET(C_GRY|C_GAMMA|C_GAMUT);
578 >                setcolrgam(cvts.gamcor);
579 >                if (CHK(C_TWRD)) {
580 >                        cvts.tf = Color2GGry;
581 >                        SET(C_RFLT);
582 >                } else if (CHK(C_TFLT)) {
583 >                        TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT,
584 >                                        SAMPLEFORMAT_IEEEFP);
585 >                        cvts.tf = Color2Gryf;
586 >                        SET(C_RFLT);
587 >                } else
588 >                        cvts.tf = Colr2Gry;
589 >                break;
590 >        default:
591 >                quiterr("internal error 4 in initfromrad");
592 >                break;
593          }
594 +                                                /* set other TIFF fields */
595 +        TIFFSetField(cvts.tif, TIFFTAG_IMAGEWIDTH, cvts.xmax);
596 +        TIFFSetField(cvts.tif, TIFFTAG_IMAGELENGTH, cvts.ymax);
597 +        TIFFSetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, CHK(C_GRY) ? 1 : 3);
598 +        TIFFSetField(cvts.tif, TIFFTAG_BITSPERSAMPLE,
599 +                        CHK(C_TFLT) ? 32 : CHK(C_TWRD) ? 16 : 8);
600 +        TIFFSetField(cvts.tif, TIFFTAG_XRESOLUTION, 72.);
601 +        TIFFSetField(cvts.tif, TIFFTAG_YRESOLUTION, 72./cvts.pixrat);
602 +        TIFFSetField(cvts.tif, TIFFTAG_ORIENTATION, cvts.orient);
603 +        TIFFSetField(cvts.tif, TIFFTAG_RESOLUTIONUNIT, 2);
604 +        TIFFSetField(cvts.tif, TIFFTAG_PLANARCONFIG, cvts.pconf);
605 +        TIFFSetField(cvts.tif, TIFFTAG_STONITS,
606 +                        cvts.stonits/pow(2.,(double)cvts.bradj));
607 +        if (cvts.capdate[0])
608 +                TIFFSetField(cvts.tif, TIFFTAG_DATETIME, cvts.capdate);
609 +        if (cvts.owner[0])
610 +                TIFFSetField(cvts.tif, TIFFTAG_ARTIST, cvts.owner);
611 +        if (cvts.comp == COMPRESSION_NONE)
612 +                i1 = TIFFScanlineSize(cvts.tif);
613 +        else
614 +                i1 = 3*cvts.xmax;       /* conservative guess */
615 +        i2 = 8192/i1;                           /* compute good strip size */
616 +        if (i2 < 1) i2 = 1;
617 +        TIFFSetField(cvts.tif, TIFFTAG_ROWSPERSTRIP, (uint32)i2);
618 +
619 +        allocbufs();                            /* allocate scanline buffers */
620 + }
621 +
622 +
623 + static void
624 + ra2tiff(                /* convert Radiance picture to TIFF image */
625 +        int  ac,
626 +        char  *av[]
627 + )
628 + {
629 +        uint32  y;
630 +                                                /* open Radiance file */
631 +        if (!strcmp(av[ac], "-"))
632 +                cvts.rfp = stdin;
633 +        else if ((cvts.rfp = fopen(av[ac], "r")) == NULL)
634 +                quiterr("cannot open Radiance input picture");
635 +                                                /* open TIFF file */
636 +        if ((cvts.tif = TIFFOpen(av[ac+1], "w")) == NULL)
637 +                quiterr("cannot open TIFF output");
638 +
639 +        initfromrad();                          /* initialize conversion */
640 +
641 +        for (y = 0; y < cvts.ymax; y++)         /* convert image */
642 +                (*cvts.tf)(y);
643                                                  /* clean up */
644 <        free((char *)scanin);
645 <        free((char *)scanout);
646 <        TIFFClose(tif);
644 >        TIFFClose(cvts.tif);
645 >        fclose(cvts.rfp);
646 > }
647 >
648 >
649 > static void
650 > Luv2Color(                      /* read/convert/write Luv->COLOR scanline */
651 >        uint32  y
652 > )
653 > {
654 >        register int    x;
655 >
656 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT))
657 >                quiterr("internal error 1 in Luv2Color");
658 >
659 >        if (cvts.pconf != PLANARCONFIG_CONTIG)
660 >                quiterr("cannot handle separate 32-bit color planes");
661 >
662 >        if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
663 >                quiterr("error reading TIFF input");
664 >                                        /* also works for float RGB */
665 >        for (x = cvts.xmax; x--; ) {
666 >                setcolor(cvts.r.colors[x],
667 >                                cvts.t.fp[3*x],
668 >                                cvts.t.fp[3*x + 1],
669 >                                cvts.t.fp[3*x + 2]);
670 >                if (CHK(C_CXFM))
671 >                        colortrans(cvts.r.colors[x], cvts.cmat,
672 >                                        cvts.r.colors[x]);
673 >                if (CHK(C_GAMUT))
674 >                        clipgamut(cvts.r.colors[x], cvts.t.fp[3*x + 1],
675 >                                        CGAMUT_LOWER, cblack, cwhite);
676 >        }
677 >        if (cvts.bradj) {
678 >                double  m = pow(2.,(double)cvts.bradj);
679 >                for (x = cvts.xmax; x--; )
680 >                        scalecolor(cvts.r.colors[x], m);
681 >        }
682 >
683 >        if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
684 >                quiterr("error writing Radiance picture");
685 > }
686 >
687 >
688 > static void
689 > RRGGBB2Color(                   /* read/convert/write RGB16->COLOR scanline */
690 >        uint32  y
691 > )
692 > {
693 >        int     dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
694 >        register double d;
695 >        register int    x;
696 >
697 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_TWRD|C_RFLT))
698 >                quiterr("internal error 1 in RRGGBB2Color");
699 >
700 >        if (cvts.pconf != PLANARCONFIG_CONTIG)
701 >                quiterr("cannot handle separate 16-bit color planes");
702 >
703 >        if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
704 >                quiterr("error reading TIFF input");
705 >        
706 >        for (x = cvts.xmax; x--; ) {
707 >                d = (cvts.t.wp[3*x] + 0.5)*(1./(1L<<16));
708 >                if (dogamma) d = pow(d, cvts.gamcor);
709 >                colval(cvts.r.colors[x],RED) = d;
710 >                d = (cvts.t.wp[3*x + 1] + 0.5)*(1./(1L<<16));
711 >                if (dogamma) d = pow(d, cvts.gamcor);
712 >                colval(cvts.r.colors[x],GRN) = d;
713 >                d = (cvts.t.wp[3*x + 2] + 0.5)*(1./(1L<<16));
714 >                if (dogamma) d = pow(d, cvts.gamcor);
715 >                colval(cvts.r.colors[x],BLU) = d;
716 >                if (CHK(C_CXFM))
717 >                        colortrans(cvts.r.colors[x], cvts.cmat,
718 >                                        cvts.r.colors[x]);
719 >        }
720 >        if (cvts.bradj) {
721 >                d = pow(2.,(double)cvts.bradj);
722 >                for (x = cvts.xmax; x--; )
723 >                        scalecolor(cvts.r.colors[x], d);
724 >        }
725 >
726 >        if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
727 >                quiterr("error writing Radiance picture");
728 > }
729 >
730 >
731 > static void
732 > L2Color(                        /* read/convert/write Lfloat->COLOR scanline */
733 >        uint32  y
734 > )
735 > {
736 >        float   m = pow(2., (double)cvts.bradj);
737 >        register int    x;
738 >
739 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY))
740 >                quiterr("internal error 1 in L2Color");
741 >
742 >        if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
743 >                quiterr("error reading TIFF input");
744 >                                        /* also works for float greyscale */
745 >        for (x = cvts.xmax; x--; ) {
746 >                register float  f = cvts.t.fp[x];
747 >                if (cvts.bradj) f *= m;
748 >                setcolor(cvts.r.colors[x], f, f, f);
749 >        }
750 >        if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
751 >                quiterr("error writing Radiance picture");
752 > }
753 >
754 >
755 > static void
756 > RGB2Colr(                       /* read/convert/write RGB->COLR scanline */
757 >        uint32  y
758 > )
759 > {
760 >        COLOR   ctmp;
761 >        register int    x;
762 >
763 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY))
764 >                quiterr("internal error 1 in RGB2Colr");
765 >
766 >        if (cvts.pconf == PLANARCONFIG_CONTIG) {
767 >                if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
768 >                        goto readerr;
769 >                for (x = cvts.xmax; x--; ) {
770 >                        cvts.r.colrs[x][RED] = cvts.t.bp[3*x];
771 >                        cvts.r.colrs[x][GRN] = cvts.t.bp[3*x + 1];
772 >                        cvts.r.colrs[x][BLU] = cvts.t.bp[3*x + 2];
773 >                }
774 >        } else {
775 >                if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
776 >                        goto readerr;
777 >                if (TIFFReadScanline(cvts.tif,
778 >                                (tdata_t)(cvts.t.bp + cvts.xmax), y, 1) < 0)
779 >                        goto readerr;
780 >                if (TIFFReadScanline(cvts.tif,
781 >                                (tdata_t)(cvts.t.bp + 2*cvts.xmax), y, 2) < 0)
782 >                        goto readerr;
783 >                for (x = cvts.xmax; x--; ) {
784 >                        cvts.r.colrs[x][RED] = cvts.t.bp[x];
785 >                        cvts.r.colrs[x][GRN] = cvts.t.bp[cvts.xmax + x];
786 >                        cvts.r.colrs[x][BLU] = cvts.t.bp[2*cvts.xmax + x];
787 >                }
788 >        }
789 >
790 >        gambs_colrs(cvts.r.colrs, cvts.xmax);
791 >        if (CHK(C_CXFM))
792 >                for (x = cvts.xmax; x--; ) {
793 >                        colr_color(ctmp, cvts.r.colrs[x]);
794 >                        colortrans(ctmp, cvts.cmat, ctmp);
795 >                        if (CHK(C_GAMUT))       /* !CHK(C_XYZE) */
796 >                                clipgamut(ctmp, bright(ctmp), CGAMUT_LOWER,
797 >                                                cblack, cwhite);
798 >                        setcolr(cvts.r.colrs[x], colval(ctmp,RED),
799 >                                        colval(ctmp,GRN), colval(ctmp,BLU));
800 >                }
801 >        if (cvts.bradj)
802 >                shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
803 >
804 >        if (fwritecolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
805 >                quiterr("error writing Radiance picture");
806          return;
807   readerr:
808          quiterr("error reading TIFF input");
809   }
810  
811  
812 < struct hdinfo {
813 <        char    *buf;           /* header buffer */
814 <        char    *pos;           /* buffer position */
815 <        char    fmt[64];        /* format type */
816 < };
812 > static void
813 > Gry2Colr(                       /* read/convert/write G8->COLR scanline */
814 >        uint32  y
815 > )
816 > {
817 >        register int    x;
818  
819 +        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != C_GRY)
820 +                quiterr("internal error 1 in Gry2Colr");
821  
822 < headline(s, hd)                 /* add header line to buffer */
823 < register char   *s;
824 < register struct hdinfo  *hd;
822 >        if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
823 >                quiterr("error reading TIFF input");
824 >
825 >        for (x = cvts.xmax; x--; )
826 >                cvts.r.colrs[x][RED] =
827 >                cvts.r.colrs[x][GRN] =
828 >                cvts.r.colrs[x][BLU] = cvts.t.bp[x];
829 >
830 >        gambs_colrs(cvts.r.colrs, cvts.xmax);
831 >        if (cvts.bradj)
832 >                shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
833 >
834 >        if (fwritecolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
835 >                quiterr("error writing Radiance picture");
836 > }
837 >
838 >
839 > static void
840 > GGry2Color(                     /* read/convert/write G16->COLOR scanline */
841 >        uint32  y
842 > )
843   {
844 <        register int    i;
844 >        int     dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
845 >        double  m;
846 >        register double d;
847 >        register int    x;
848  
849 <        if (isformat(s)) {
850 <                formatval(hd->fmt, s);
851 <                return;
849 >        if (CHK(C_TFLT|C_TWRD|C_GRY|C_RFLT) != (C_GRY|C_RFLT|C_TWRD))
850 >                quiterr("internal error 1 in GGry2Color");
851 >
852 >        if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
853 >                quiterr("error reading TIFF input");
854 >
855 >        if (cvts.bradj)
856 >                m = pow(2., (double)cvts.bradj);
857 >        for (x = cvts.xmax; x--; ) {
858 >                d = (cvts.t.wp[x] + 0.5)*(1./(1L<<16));
859 >                if (dogamma) d = pow(d, cvts.gamcor);
860 >                if (cvts.bradj) d *= m;
861 >                colval(cvts.r.colors[x],RED) =
862 >                colval(cvts.r.colors[x],GRN) =
863 >                colval(cvts.r.colors[x],BLU) = d;
864          }
865 <        if (hd->buf == NULL)
866 <                hd->pos = hd->buf = malloc(strlen(s)+1);
867 <        else {
868 <                i = hd->pos - hd->buf;
869 <                hd->buf = realloc(hd->buf, i+strlen(s)+1);
870 <                hd->pos = hd->buf + i;
865 >        if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
866 >                quiterr("error writing Radiance picture");
867 > }
868 >
869 >
870 > static void
871 > Color2GGry(                     /* read/convert/write COLOR->G16 scanline */
872 >        uint32  y
873 > )
874 > {
875 >        int     dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
876 >        float   m = pow(2.,(double)cvts.bradj);
877 >        register int    x;
878 >
879 >        if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TWRD|C_GRY))
880 >                quiterr("internal error 1 in Color2GGry");
881 >
882 >        if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
883 >                quiterr("error reading Radiance picture");
884 >
885 >        for (x = cvts.xmax; x--; ) {
886 >                register float  f = m*( CHK(C_XYZE) ?
887 >                                                colval(cvts.r.colors[x],CIEY)
888 >                                                : bright(cvts.r.colors[x]) );
889 >                if (f <= 0)
890 >                        cvts.t.wp[x] = 0;
891 >                else if (f >= 1)
892 >                        cvts.t.wp[x] = 0xffff;
893 >                else if (dogamma)
894 >                        cvts.t.wp[x] = (int)((float)(1L<<16) *
895 >                                                pow(f, 1./cvts.gamcor));
896 >                else
897 >                        cvts.t.wp[x] = (int)((float)(1L<<16) * f);
898          }
899 <        if (hd->buf == NULL)
900 <                quiterr("out of memory in headline");
901 <        while (*hd->pos++ = *s++)
212 <                ;
899 >
900 >        if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
901 >                quiterr("error writing TIFF output");
902   }
903  
904  
905 < ra2tiff(inpf, outf)             /* convert Radiance file to 24-bit TIFF */
906 < char    *inpf, *outf;
905 > static void
906 > Color2L(                        /* read/convert/write COLOR->Lfloat scanline */
907 >        uint32  y
908 > )
909   {
910 <        char    buf[64];
220 <        struct hdinfo   hd;
221 <        TIFF    *tif;
222 <        int     xmax, ymax;
223 <        BYTE    *scanout;
224 <        COLR    *scanin;
910 >        float   m = pow(2.,(double)cvts.bradj);
911          register int    x;
912 <        int     y;
913 <                                                /* open Radiance file */
914 <        if (strcmp(inpf, "-") && freopen(inpf, "r", stdin) == NULL)
915 <                quiterr("cannot open Radiance input file");
916 <        hd.buf = NULL; hd.fmt[0] = '\0';
917 <        getheader(stdin, headline, &hd);
918 <        if (bradj)
919 <                sprintf(buf, "%s -e %+d\n", progname, bradj);
920 <        else
921 <                sprintf(buf, "%s\n", progname);
922 <        headline(buf, &hd);
923 <        if (hd.fmt[0] && strcmp(hd.fmt, COLRFMT))
924 <                quiterr("input not a Radiance picture");
925 <        if (fgetresolu(&xmax, &ymax, stdin) != (YDECR|YMAJOR))
926 <                quiterr("bad Radiance picture");
927 <                                                /* open TIFF file */
928 <        if ((tif = TIFFOpen(outf, "w")) == NULL)
929 <                quiterr("cannot open TIFF output");
930 <        TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (unsigned long)xmax);
931 <        TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (unsigned long)ymax);
932 <        TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
933 <        TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
934 <        TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, 2);
935 <        TIFFSetField(tif, TIFFTAG_PLANARCONFIG, 1);
936 <        TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, hd.buf);
937 <                                                /* allocate scanlines */
938 <        scanin = (COLR *)malloc(xmax*sizeof(COLR));
939 <        scanout = (BYTE *)malloc(TIFFScanlineSize(tif));
940 <        if (scanin == NULL || scanout == NULL)
941 <                quiterr("out of memory in ra2tiff");
942 <                                                /* convert image */
943 <        for (y = 0; y < ymax; y++) {
944 <                if (freadcolrs(scanin, xmax, stdin) < 0)
945 <                        quiterr("error reading Radiance picture");
946 <                if (bradj)
947 <                        shiftcolrs(scanin, xmax, bradj);
948 <                colrs_gambs(scanin, xmax);
949 <                for (x = 0; x < xmax; x++) {
950 <                        scanout[3*x] = scanin[x][RED];
951 <                        scanout[3*x+1] = scanin[x][GRN];
952 <                        scanout[3*x+2] = scanin[x][BLU];
912 >
913 >        if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TFLT|C_GRY))
914 >                quiterr("internal error 1 in Color2L");
915 >
916 >        if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
917 >                quiterr("error reading Radiance picture");
918 >
919 >        for (x = cvts.xmax; x--; )
920 >                cvts.t.fp[x] = m*( CHK(C_XYZE) ? colval(cvts.r.colors[x],CIEY)
921 >                                                : bright(cvts.r.colors[x]) );
922 >
923 >        if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
924 >                quiterr("error writing TIFF output");
925 > }
926 >
927 >
928 > static void
929 > Color2Luv(                      /* read/convert/write COLOR->Luv scanline */
930 >        uint32  y
931 > )
932 > {
933 >        register int    x;
934 >
935 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT))
936 >                quiterr("internal error 1 in Color2Luv");
937 >
938 >        if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
939 >                quiterr("error reading Radiance picture");
940 >
941 >        if (CHK(C_CXFM))
942 >                for (x = cvts.xmax; x--; )
943 >                        colortrans(cvts.r.colors[x], cvts.cmat,
944 >                                        cvts.r.colors[x]);
945 >        if (cvts.bradj) {
946 >                double  m = pow(2.,(double)cvts.bradj);
947 >                for (x = cvts.xmax; x--; )
948 >                        scalecolor(cvts.r.colors[x], m);
949 >        }
950 >                                        /* also works for float RGB */
951 >        for (x = cvts.xmax; x--; ) {
952 >                cvts.t.fp[3*x] = colval(cvts.r.colors[x],CIEX);
953 >                cvts.t.fp[3*x+1] = colval(cvts.r.colors[x],CIEY);
954 >                cvts.t.fp[3*x+2] = colval(cvts.r.colors[x],CIEZ);
955 >        }
956 >
957 >        if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
958 >                quiterr("error writing TIFF output");
959 > }
960 >
961 >
962 > static void
963 > Color2RRGGBB(                   /* read/convert/write COLOR->RGB16 scanline */
964 >        uint32  y
965 > )
966 > {
967 >        int     dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
968 >        float   m = pow(2.,(double)cvts.bradj);
969 >        register int    x, i;
970 >
971 >        if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TWRD))
972 >                quiterr("internal error 1 in Color2RRGGBB");
973 >
974 >        if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
975 >                quiterr("error reading Radiance picture");
976 >
977 >        for (x = cvts.xmax; x--; ) {
978 >            if (CHK(C_CXFM)) {
979 >                        colortrans(cvts.r.colors[x], cvts.cmat,
980 >                                        cvts.r.colors[x]);
981 >                if (CHK(C_GAMUT))
982 >                        clipgamut(cvts.r.colors[x], bright(cvts.r.colors[x]),
983 >                                        CGAMUT_LOWER, cblack, cwhite);
984 >            }
985 >            for (i = 3; i--; ) {
986 >                register float  f = m*colval(cvts.r.colors[x],i);
987 >                if (f <= 0)
988 >                        cvts.t.wp[3*x + i] = 0;
989 >                else if (f >= 1)
990 >                        cvts.t.wp[3*x + i] = 0xffff;
991 >                else if (dogamma)
992 >                        cvts.t.wp[3*x + i] = (int)((float)(1L<<16) *
993 >                                                pow(f, 1./cvts.gamcor));
994 >                else
995 >                        cvts.t.wp[3*x + i] = (int)((float)(1L<<16)*f);
996 >            }
997 >        }
998 >
999 >        if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
1000 >                quiterr("error writing TIFF output");
1001 > }
1002 >
1003 >
1004 > static void
1005 > Colr2Gry(                       /* read/convert/write COLR->RGB scanline */
1006 >        uint32  y
1007 > )
1008 > {
1009 >        register int    x;
1010 >
1011 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != C_GRY)
1012 >                quiterr("internal error 1 in Colr2Gry");
1013 >
1014 >        if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
1015 >                quiterr("error reading Radiance picture");
1016 >
1017 >        if (cvts.bradj)
1018 >                shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
1019 >        for (x = cvts.xmax; x--; )
1020 >                colval(cvts.r.colrs[x],CIEY) = normbright(cvts.r.colrs[x]);
1021 >        colrs_gambs(cvts.r.colrs, cvts.xmax);
1022 >
1023 >        for (x = cvts.xmax; x--; )
1024 >                cvts.t.bp[x] = colval(cvts.r.colrs[x],CIEY);
1025 >
1026 >        if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
1027 >                quiterr("error writing TIFF output");
1028 > }
1029 >
1030 >
1031 > static void
1032 > Colr2RGB(                       /* read/convert/write COLR->RGB scanline */
1033 >        uint32  y
1034 > )
1035 > {
1036 >        COLOR   ctmp;
1037 >        register int    x;
1038 >
1039 >        if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY))
1040 >                quiterr("internal error 1 in Colr2RGB");
1041 >
1042 >        if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
1043 >                quiterr("error reading Radiance picture");
1044 >
1045 >        if (cvts.bradj)
1046 >                shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
1047 >        if (CHK(C_CXFM))
1048 >                for (x = cvts.xmax; x--; ) {
1049 >                        colr_color(ctmp, cvts.r.colrs[x]);
1050 >                        colortrans(ctmp, cvts.cmat, ctmp);
1051 >                        if (CHK(C_GAMUT))
1052 >                                clipgamut(ctmp, bright(ctmp), CGAMUT,
1053 >                                                cblack, cwhite);
1054 >                        setcolr(cvts.r.colrs[x], colval(ctmp,RED),
1055 >                                        colval(ctmp,GRN), colval(ctmp,BLU));
1056                  }
1057 <                if (TIFFWriteScanline(tif, scanout, y, 0) < 0)
1058 <                        quiterr("error writing TIFF output");
1057 >        colrs_gambs(cvts.r.colrs, cvts.xmax);
1058 >
1059 >        for (x = cvts.xmax; x--; ) {
1060 >                cvts.t.bp[3*x] = cvts.r.colrs[x][RED];
1061 >                cvts.t.bp[3*x+1] = cvts.r.colrs[x][GRN];
1062 >                cvts.t.bp[3*x+2] = cvts.r.colrs[x][BLU];
1063          }
1064 <                                                /* clean up */
1065 <        free((char *)scanin);
1066 <        free((char *)scanout);
274 <        TIFFClose(tif);
275 <        free(hd.buf);
1064 >
1065 >        if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
1066 >                quiterr("error writing TIFF output");
1067   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines