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.5 by greg, Thu Aug 15 15:47:35 1991 UTC vs.
Revision 2.37 by greg, Fri Oct 4 18:49:06 2024 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>
10 > #include  <math.h>
11 > #include  <ctype.h>
12  
13 + #include  "rtio.h"
14 + #include  "platform.h"
15   #include  "tiffio.h"
14
16   #include  "color.h"
17 + #include  "resolu.h"
18  
19 < extern double  atof();
19 > #define  GAMCOR         2.2             /* default gamma */
20  
21 < extern char  *malloc(), *realloc();
21 >                                /* conversion flags */
22 > #define C_CXFM          0x1             /* needs color transformation */
23 > #define C_GAMUT         0x2             /* needs gamut mapping */
24 > #define C_GAMMA         0x4             /* needs gamma correction */
25 > #define C_GRY           0x8             /* TIFF is greyscale */
26 > #define C_XYZE          0x10            /* Radiance is XYZE */
27 > #define C_SPEC          0x20            /* Radiance is spectral */
28 > #define C_RFLT          0x40            /* Radiance data is float */
29 > #define C_TFLT          0x80            /* TIFF data is float */
30 > #define C_TWRD          0x100           /* TIFF data is 16-bit */
31 > #define C_PRIM          0x200           /* 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 CONVST {
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 >        int     ncc;            /* # color components for spectral */
48 >        float   wpt[4];         /* wavelength partitions */
49 >        FILE    *rfp;           /* Radiance stream pointer */
50 >        TIFF    *tif;           /* TIFF pointer */
51 >        uint32  xmax, ymax;     /* image dimensions */
52 >        COLORMAT        cmat;   /* color transformation matrix */
53 >        RGBPRIMS        prims;  /* RGB primaries */
54 >        union {
55 >                COLR    *colrs;         /* 4-byte ???E pointer */
56 >                COLOR   *colors;        /* float array pointer */
57 >                void    *p;             /* generic pointer */
58 >        } r;                    /* Radiance scanline */
59 >        union {
60 >                uint8   *bp;            /* byte pointer */
61 >                uint16  *wp;            /* word pointer */
62 >                float   *fp;            /* float pointer */
63 >                void    *p;             /* generic pointer */
64 >        } t;                    /* TIFF scanline */
65 >        colcvf_t *tf;   /* translation procedure */
66 > }       cvts = {        /* conversion structure */
67 >        0, "", "", COMPRESSION_NONE, PHOTOMETRIC_RGB,
68 >        PLANARCONFIG_CONTIG, GAMCOR, 0, 1, 1., 1., 3,
69 > };
70  
71 < double  gamma = 2.2;                    /* gamma correction */
71 > #define CHK(f)          (cvts.flags & (f))
72 > #define SET(f)          (cvts.flags |= (f))
73 > #define CLR(f)          (cvts.flags &= ~(f))
74 > #define TGL(f)          (cvts.flags ^= (f))
75  
76 < int  bradj = 0;                         /* brightness adjustment */
76 > static colcvf_t Luv2Color, L2Color, RGB2Colr, Gry2Colr;
77 > static colcvf_t Color2Luv, Color2L, Colr2RGB, Colr2Gry;
78 > static colcvf_t RRGGBB2Color, GGry2Color, Color2RRGGBB, Color2GGry;
79  
80 + static gethfunc headline;
81 + static void quiterr(char *err);
82 + static void allocbufs(void);
83 + static void initfromtif(void);
84 + static void tiff2ra(int  ac, char  *av[]);
85 + static void initfromrad(void);
86 + static void ra2tiff(int  ac, char  *av[]);
87 + static void ReadRadScan(struct CONVST *cvp);
88 +
89 +
90 + #define RfGfBf2Color    Luv2Color
91 + #define Gryf2Color      L2Color
92 + #define Color2Gryf      Color2L
93 + #define Color2RfGfBf    Color2Luv
94 +
95 + short   ortab[8] = {            /* orientation conversion table */
96 +        YMAJOR|YDECR,
97 +        YMAJOR|YDECR|XDECR,
98 +        YMAJOR|XDECR,
99 +        YMAJOR,
100 +        YDECR,
101 +        XDECR|YDECR,
102 +        XDECR,
103 +        0
104 + };
105 +
106 + #define pixorder()      ortab[cvts.orient-1]
107 +
108 + extern char     TMSTR[];        /* "CAPDATE=" from header.c */
109 + char            OWNSTR[] = "OWNER=";
110 +
111   char  *progname;
112  
113  
114 < main(argc, argv)
115 < int  argc;
34 < char  *argv[];
114 > int
115 > main(int  argc, char  *argv[])
116   {
117          int  reverse = 0;
118          int  i;
# Line 41 | Line 122 | char  *argv[];
122          for (i = 1; i < argc; i++)
123                  if (argv[i][0] == '-')
124                          switch (argv[i][1]) {
125 <                        case 'g':
126 <                                gamma = atof(argv[++i]);
125 >                        case 'g':               /* gamma correction */
126 >                                cvts.gamcor = atof(argv[++i]);
127                                  break;
128 <                        case 'z':
129 <                                lzcomp = !lzcomp;
128 >                        case 'x':               /* XYZE Radiance output */
129 >                                TGL(C_XYZE);
130                                  break;
131 <                        case 'b':
132 <                                greyscale = !greyscale;
131 >                        case 'z':               /* LZW compressed output */
132 >                                cvts.comp = COMPRESSION_LZW;
133                                  break;
134 <                        case 'e':
134 >                        case 'L':               /* LogLuv 32-bit output */
135 >                                cvts.comp = COMPRESSION_SGILOG;
136 >                                cvts.phot = PHOTOMETRIC_LOGLUV;
137 >                                break;
138 >                        case 'l':               /* LogLuv 24-bit output */
139 >                                cvts.comp = COMPRESSION_SGILOG24;
140 >                                cvts.phot = PHOTOMETRIC_LOGLUV;
141 >                                break;
142 >                        case 'w':               /* 16-bit/primary output? */
143 >                                TGL(C_TWRD);
144 >                                break;
145 >                        case 'f':               /* IEEE float output? */
146 >                                TGL(C_TFLT);
147 >                                break;
148 >                        case 'b':               /* greyscale output? */
149 >                                TGL(C_GRY);
150 >                                break;
151 >                        case 'e':               /* exposure adjustment */
152                                  if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
153                                          goto userr;
154 <                                bradj = atoi(argv[++i]);
154 >                                cvts.bradj = atoi(argv[++i]);
155                                  break;
156 <                        case 'r':
156 >                        case 'r':               /* reverse conversion? */
157                                  reverse = !reverse;
158                                  break;
159                          case '\0':
# Line 66 | Line 164 | char  *argv[];
164                  else
165                          break;
166   doneopts:
167 <        setcolrgam(gamma);
167 >        if (reverse) {
168  
71        if (reverse)
169                  if (i != argc-2 && i != argc-1)
170                          goto userr;
171 <                else
172 <                        tiff2ra(argv[i], argv[i+1]);
173 <        else
171 >
172 >                tiff2ra(i, argv);
173 >
174 >        } else {
175 >
176                  if (i != argc-2)
177                          goto userr;
178 <                else
179 <                        ra2tiff(argv[i], argv[i+1]);
178 >                                                /* consistency checks */
179 >                if (CHK(C_GRY)) {
180 >                        if (cvts.phot == PHOTOMETRIC_RGB)
181 >                                cvts.phot = PHOTOMETRIC_MINISBLACK;
182 >                        else {
183 >                                cvts.phot = PHOTOMETRIC_LOGL;
184 >                                cvts.comp = COMPRESSION_SGILOG;
185 >                        }
186 >                }
187 >                if (CHK(C_TWRD|C_TFLT) == (C_TWRD|C_TFLT))
188 >                        goto userr;
189  
190 +                ra2tiff(i, argv);
191 +        }
192 +
193          exit(0);
194   userr:
195          fprintf(stderr,
196 <        "Usage: %s [-r][-b][-z][-e +/-stops][-g gamma] input output\n",
196 >        "Usage: %s [-z|-L|-l|-f|-w][-b][-e +/-stops][-g gamma] {in.hdr|-} out.tif\n",
197                          progname);
198 +        fprintf(stderr,
199 +        "   Or: %s -r [-x][-e +/-stops][-g gamma] in.tif [out.hdr|-]\n",
200 +                        progname);
201          exit(1);
202   }
203  
204  
205 < quiterr(err)            /* print message and exit */
206 < char  *err;
205 > static void
206 > quiterr(                /* print message and exit */
207 >        char  *err
208 > )
209   {
210          if (err != NULL) {
211                  fprintf(stderr, "%s: %s\n", progname, err);
# Line 99 | Line 215 | char  *err;
215   }
216  
217  
218 < tiff2ra(inpf, outf)     /* convert TIFF file to Radiance picture */
219 < char    *inpf, *outf;
218 > static void
219 > allocbufs(void)                 /* allocate scanline buffers */
220   {
221 <        unsigned long   xmax, ymax;
222 <        TIFF    *tif;
223 <        unsigned short  pconfig, nsamps;
224 <        unsigned short  hi;
225 <        register BYTE   *scanin;
226 <        register COLR   *scanout;
227 <        register int    x;
228 <        int     y;
229 <                                        /* open/check  TIFF file */
230 <        if ((tif = TIFFOpen(inpf, "r")) == NULL)
231 <                quiterr("cannot open TIFF input");
232 <        if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &nsamps) ||
233 <                        (nsamps != 1 && nsamps != 3))
234 <                quiterr("unsupported samples per pixel");
235 <        if (!TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &hi) || hi != 8)
236 <                quiterr("unsupported bits per sample");
237 <        if (TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &hi) &&
238 <                        hi != (nsamps==1 ? 1 : 2))
239 <                quiterr("unsupported photometric interpretation");
240 <        if (!TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &pconfig) ||
241 <                        (pconfig != 1 && pconfig != 2))
242 <                quiterr("unsupported planar configuration");
243 <        if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &xmax) ||
244 <                        !TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &ymax))
245 <                quiterr("unknown input image resolution");
246 <                                                /* allocate scanlines */
247 <        scanin = (BYTE *)malloc(TIFFScanlineSize(tif));
248 <        scanout = (COLR *)malloc(xmax*sizeof(COLR));
249 <        if (scanin == NULL || scanout == NULL)
250 <                quiterr("out of memory in tiff2ra");
251 <                                        /* open output and write header */
252 <        if (outf != NULL && strcmp(outf, "-") &&
253 <                        freopen(outf, "w", stdout) == NULL)
254 <                quiterr("cannot open Radiance output file");
139 <        fputs(progname, stdout);
140 <        if (bradj)
141 <                printf(" -e %+d", bradj);
142 <        fputs(" -r\n", stdout);
143 <        fputformat(COLRFMT, stdout);
144 <        putchar('\n');
145 <        fputresolu(YDECR|YMAJOR, xmax, ymax, stdout);
146 <                                                /* convert image */
147 <        if (nsamps == 1)
148 <                pconfig = 1;
149 <        for (y = 0; y < ymax; y++) {
150 <                if (pconfig == 1) {
151 <                        if (TIFFReadScanline(tif, scanin, y, 0) < 0)
152 <                                goto readerr;
153 <                        if (nsamps == 1)
154 <                                for (x = 0; x < xmax; x++)
155 <                                        scanout[x][RED] =
156 <                                        scanout[x][GRN] =
157 <                                        scanout[x][BLU] = scanin[x];
158 <                        else
159 <                                for (x = 0; x < xmax; x++) {
160 <                                        scanout[x][RED] = scanin[3*x];
161 <                                        scanout[x][GRN] = scanin[3*x+1];
162 <                                        scanout[x][BLU] = scanin[3*x+2];
163 <                                }
164 <                } else {
165 <                        if (TIFFReadScanline(tif, scanin, y, 0) < 0)
166 <                                goto readerr;
167 <                        for (x = 0; x < xmax; x++)
168 <                                scanout[x][RED] = scanin[x];
169 <                        if (TIFFReadScanline(tif, scanin, y, 1) < 0)
170 <                                goto readerr;
171 <                        for (x = 0; x < xmax; x++)
172 <                                scanout[x][GRN] = scanin[x];
173 <                        if (TIFFReadScanline(tif, scanin, y, 2) < 0)
174 <                                goto readerr;
175 <                        for (x = 0; x < xmax; x++)
176 <                                scanout[x][BLU] = scanin[x];
221 >        int     rsiz, tsiz;
222 >
223 >        rsiz = CHK(C_RFLT) ? sizeof(COLOR) : sizeof(COLR);
224 >        tsiz = (CHK(C_TFLT) ? sizeof(float) :
225 >                        CHK(C_TWRD) ? sizeof(uint16) : sizeof(uint8)) *
226 >                        (CHK(C_GRY) ? 1 : 3);
227 >        cvts.r.p = malloc(rsiz*cvts.xmax);
228 >        cvts.t.p = malloc(tsiz*cvts.xmax);
229 >        if ((cvts.r.p == NULL) | (cvts.t.p == NULL))
230 >                quiterr("no memory to allocate scanline buffers");
231 > }
232 >
233 >
234 > static void
235 > initfromtif(void)               /* initialize conversion from TIFF input */
236 > {
237 >        uint16  hi;
238 >        char    *cp;
239 >        float   *fa, f1, f2;
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 <                gambs_colrs(scanout, xmax);
179 <                if (bradj)
180 <                        shiftcolrs(scanout, xmax, bradj);
181 <                if (fwritecolrs(scanout, xmax, stdout) < 0)
182 <                        quiterr("error writing Radiance picture");
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 +
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 +        SET_FILE_BINARY(cvts.rfp);
419 +                                        /* start output header */
420 +        newheader("RADIANCE", cvts.rfp);
421 +        printargs(ac, av, cvts.rfp);
422 +
423 +        initfromtif();                  /* initialize conversion */
424 +
425 +        fputc('\n', cvts.rfp);          /* finish Radiance header */
426 +        fputresolu(pixorder(), (int)cvts.xmax, (int)cvts.ymax, cvts.rfp);
427 +
428 +        for (y = 0; y < cvts.ymax; y++)         /* convert image */
429 +                (*cvts.tf)(y);
430                                                  /* clean up */
431 <        free((char *)scanin);
432 <        free((char *)scanout);
187 <        TIFFClose(tif);
188 <        return;
189 < readerr:
190 <        quiterr("error reading TIFF input");
431 >        fclose(cvts.rfp);
432 >        TIFFClose(cvts.tif);
433   }
434  
435  
436 < ra2tiff(inpf, outf)             /* convert Radiance file to 24-bit TIFF */
437 < char    *inpf, *outf;
436 > static int
437 > headline(                       /* process Radiance input header line */
438 >        char    *s,
439 >        void    *p
440 > )
441   {
442 <        TIFF    *tif;
443 <        int     xmax, ymax;
444 <        BYTE    *scanout;
445 <        COLR    *scanin;
446 <        register int    x;
447 <        int     y;
448 <                                                /* open Radiance file */
449 <        if (strcmp(inpf, "-") && freopen(inpf, "r", stdin) == NULL)
450 <                quiterr("cannot open Radiance input file");
451 <        if (checkheader(stdin, COLRFMT, NULL) < 0 ||
452 <                        fgetresolu(&xmax, &ymax, stdin) != (YDECR|YMAJOR))
442 >        static int      tmstrlen = 0;
443 >        static int      ownstrlen = 0;
444 >        struct CONVST   *cvp = (struct CONVST *)p;
445 >        char            fmt[MAXFMTLEN];
446 >
447 >        if (!tmstrlen) {
448 >                tmstrlen = strlen(TMSTR);
449 >                ownstrlen = strlen(OWNSTR);
450 >        }
451 >        if (formatval(fmt, s)) {
452 >                CLR(C_XYZE|C_SPEC);
453 >                if (!strcmp(fmt, CIEFMT))
454 >                        SET(C_XYZE);
455 >                else if (!strcmp(fmt, SPECFMT))
456 >                        SET(C_SPEC);
457 >                else if (strcmp(fmt, COLRFMT))
458 >                        return(-1);
459 >                return(1);
460 >        }
461 >        if (isexpos(s)) {
462 >                cvp->stonits /= exposval(s);
463 >                return(1);
464 >        }
465 >        if (isaspect(s)) {
466 >                cvp->pixrat *= aspectval(s);
467 >                return(1);
468 >        }
469 >        if (isprims(s)) {
470 >                primsval(cvp->prims, s);
471 >                SET(C_PRIM);
472 >                return(1);
473 >        }
474 >        if (isdate(s)) {
475 >                if (s[tmstrlen] == ' ')
476 >                        strncpy(cvp->capdate, s+tmstrlen+1, 19);
477 >                else
478 >                        strncpy(cvp->capdate, s+tmstrlen, 19);
479 >                cvp->capdate[19] = '\0';
480 >                return(1);
481 >        }
482 >        if (!strncmp(s, OWNSTR, ownstrlen)) {
483 >                char    *cp = s + ownstrlen;
484 >
485 >                while (isspace(*cp))
486 >                        ++cp;
487 >                strncpy(cvp->owner, cp, sizeof(cvp->owner));
488 >                cvp->owner[sizeof(cvp->owner)-1] = '\0';
489 >                for (cp = cvp->owner; *cp; cp++)
490 >                        ;
491 >                while (cp > cvp->owner && isspace(cp[-1]))
492 >                        *--cp = '\0';
493 >                return(1);
494 >        }
495 >        if (isncomp(s)) {
496 >                cvp->ncc = ncompval(s);
497 >                return((3 <= cvp->ncc) & (cvp->ncc < MAXCSAMP) ? 1 : -1);
498 >        }
499 >        if (iswlsplit(s))
500 >                return(wlsplitval(cvp->wpt, s) ? 1 : -1);
501 >        return(0);
502 > }
503 >
504 >
505 > static void
506 > initfromrad(void)                       /* initialize input from a Radiance picture */
507 > {
508 >        int     i1, i2, po;
509 >                                                /* read Radiance header */
510 >        memcpy(cvts.wpt, WLPART, sizeof(cvts.wpt));
511 >        if (getheader(cvts.rfp, headline, &cvts) < 0 ||
512 >                        (po = fgetresolu(&i1, &i2, cvts.rfp)) < 0)
513                  quiterr("bad Radiance picture");
514 +        cvts.xmax = i1; cvts.ymax = i2;
515 +        for (i1 = 0; i1 < 8; i1++)              /* interpret orientation */
516 +                if (ortab[i1] == po) {
517 +                        cvts.orient = i1 + 1;
518 +                        break;
519 +                }
520 +        if (i1 >= 8)
521 +                quiterr("internal error 1 in initfromrad");
522 +        if (!(po & YMAJOR))
523 +                cvts.pixrat = 1./cvts.pixrat;
524 +        if (!CHK(C_XYZE))
525 +                cvts.stonits *= WHTEFFICACY;
526 +                                                /* set up conversion */
527 +        TIFFSetField(cvts.tif, TIFFTAG_COMPRESSION, cvts.comp);
528 +        TIFFSetField(cvts.tif, TIFFTAG_PHOTOMETRIC, cvts.phot);
529 +
530 +        switch (cvts.phot) {
531 +        case PHOTOMETRIC_LOGLUV:
532 +                SET(C_RFLT|C_TFLT);
533 +                CLR(C_GRY|C_TWRD);
534 +                if (!CHK(C_XYZE|C_SPEC)) {
535 +                        comprgb2xyzWBmat(cvts.cmat,
536 +                                        CHK(C_PRIM) ? cvts.prims : stdprims);
537 +                        SET(C_CXFM);
538 +                }
539 +                if (cvts.comp != COMPRESSION_SGILOG &&
540 +                                cvts.comp != COMPRESSION_SGILOG24)
541 +                        quiterr("internal error 2 in initfromrad");
542 +                TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
543 +                                SGILOGDATAFMT_FLOAT);
544 +                cvts.tf = Color2Luv;
545 +                break;
546 +        case PHOTOMETRIC_LOGL:
547 +                SET(C_GRY|C_RFLT|C_TFLT);
548 +                CLR(C_TWRD);
549 +                if (cvts.comp != COMPRESSION_SGILOG)    
550 +                        quiterr("internal error 3 in initfromrad");
551 +                TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
552 +                                SGILOGDATAFMT_FLOAT);
553 +                cvts.tf = Color2L;
554 +                break;
555 +        case PHOTOMETRIC_RGB:
556 +                SET(C_GAMMA|C_GAMUT);
557 +                CLR(C_GRY);
558 +                setcolrgam(cvts.gamcor);
559 +                if (CHK(C_XYZE|C_SPEC)) {
560 +                        compxyz2rgbWBmat(cvts.cmat,
561 +                                        CHK(C_PRIM) ? cvts.prims : stdprims);
562 +                        SET(C_CXFM);
563 +                }
564 +                if (CHK(C_PRIM)) {
565 +                        TIFFSetField(cvts.tif, TIFFTAG_PRIMARYCHROMATICITIES,
566 +                                        (float *)cvts.prims);
567 +                        TIFFSetField(cvts.tif, TIFFTAG_WHITEPOINT,
568 +                                        (float *)cvts.prims[WHT]);
569 +                }
570 +                if (CHK(C_TWRD)) {
571 +                        cvts.tf = Color2RRGGBB;
572 +                        SET(C_RFLT);
573 +                } else if (CHK(C_TFLT)) {
574 +                        TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT,
575 +                                        SAMPLEFORMAT_IEEEFP);
576 +                        cvts.tf = Color2RfGfBf;
577 +                        SET(C_RFLT);
578 +                        CLR(C_GAMUT);
579 +                } else
580 +                        cvts.tf = Colr2RGB;
581 +                break;
582 +        case PHOTOMETRIC_MINISBLACK:
583 +                SET(C_GRY|C_GAMMA|C_GAMUT);
584 +                setcolrgam(cvts.gamcor);
585 +                if (CHK(C_TWRD)) {
586 +                        cvts.tf = Color2GGry;
587 +                        SET(C_RFLT);
588 +                } else if (CHK(C_TFLT)) {
589 +                        TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT,
590 +                                        SAMPLEFORMAT_IEEEFP);
591 +                        cvts.tf = Color2Gryf;
592 +                        SET(C_RFLT);
593 +                } else
594 +                        cvts.tf = Colr2Gry;
595 +                break;
596 +        default:
597 +                quiterr("internal error 4 in initfromrad");
598 +                break;
599 +        }
600 +                                                /* set other TIFF fields */
601 +        TIFFSetField(cvts.tif, TIFFTAG_IMAGEWIDTH, cvts.xmax);
602 +        TIFFSetField(cvts.tif, TIFFTAG_IMAGELENGTH, cvts.ymax);
603 +        TIFFSetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, CHK(C_GRY) ? 1 : 3);
604 +        TIFFSetField(cvts.tif, TIFFTAG_BITSPERSAMPLE,
605 +                        CHK(C_TFLT) ? 32 : CHK(C_TWRD) ? 16 : 8);
606 +        TIFFSetField(cvts.tif, TIFFTAG_XRESOLUTION, 72.);
607 +        TIFFSetField(cvts.tif, TIFFTAG_YRESOLUTION, 72./cvts.pixrat);
608 +        TIFFSetField(cvts.tif, TIFFTAG_ORIENTATION, cvts.orient);
609 +        TIFFSetField(cvts.tif, TIFFTAG_RESOLUTIONUNIT, 2);
610 +        TIFFSetField(cvts.tif, TIFFTAG_PLANARCONFIG, cvts.pconf);
611 +        TIFFSetField(cvts.tif, TIFFTAG_STONITS,
612 +                        cvts.stonits/pow(2.,(double)cvts.bradj));
613 +        if (cvts.capdate[0])
614 +                TIFFSetField(cvts.tif, TIFFTAG_DATETIME, cvts.capdate);
615 +        if (cvts.owner[0])
616 +                TIFFSetField(cvts.tif, TIFFTAG_ARTIST, cvts.owner);
617 +        if (cvts.comp == COMPRESSION_NONE)
618 +                i1 = TIFFScanlineSize(cvts.tif);
619 +        else
620 +                i1 = 3*cvts.xmax;       /* conservative guess */
621 +        i2 = 8192/i1;                           /* compute good strip size */
622 +        if (i2 < 1) i2 = 1;
623 +        TIFFSetField(cvts.tif, TIFFTAG_ROWSPERSTRIP, (uint32)i2);
624 +
625 +        allocbufs();                            /* allocate scanline buffers */
626 + }
627 +
628 +
629 + static void
630 + ra2tiff(                /* convert Radiance picture to TIFF image */
631 +        int  ac,
632 +        char  *av[]
633 + )
634 + {
635 +        uint32  y;
636 +                                                /* open Radiance file */
637 +        if (!strcmp(av[ac], "-"))
638 +                cvts.rfp = stdin;
639 +        else if ((cvts.rfp = fopen(av[ac], "r")) == NULL)
640 +                quiterr("cannot open Radiance input picture");
641 +        SET_FILE_BINARY(cvts.rfp);
642                                                  /* open TIFF file */
643 <        if ((tif = TIFFOpen(outf, "w")) == NULL)
643 >        if ((cvts.tif = TIFFOpen(av[ac+1], "w")) == NULL)
644                  quiterr("cannot open TIFF output");
645 <        TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (unsigned long)xmax);
646 <        TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (unsigned long)ymax);
647 <        TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, greyscale ? 1 : 3);
648 <        TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
649 <        TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, greyscale ? 1 : 2);
650 <        TIFFSetField(tif, TIFFTAG_PLANARCONFIG, 1);
651 <        if (lzcomp)
652 <                TIFFSetField(tif, TIFFTAG_COMPRESSION, (unsigned short)5);
653 <                                                /* allocate scanlines */
654 <        scanin = (COLR *)malloc(xmax*sizeof(COLR));
655 <        scanout = (BYTE *)malloc(TIFFScanlineSize(tif));
656 <        if (scanin == NULL || scanout == NULL)
657 <                quiterr("out of memory in ra2tiff");
658 <                                                /* convert image */
659 <        for (y = 0; y < ymax; y++) {
660 <                if (freadcolrs(scanin, xmax, stdin) < 0)
661 <                        quiterr("error reading Radiance picture");
662 <                if (bradj)
663 <                        shiftcolrs(scanin, xmax, bradj);
664 <                if (greyscale) {
665 <                        for (x = 0; x < xmax; x++)
666 <                                scanin[x][GRN] = normbright(scanin[x]);
667 <                        colrs_gambs(scanin, xmax);
668 <                        for (x = 0; x < xmax; x++)
669 <                                scanout[x] = scanin[x][GRN];
670 <                } else {
671 <                        colrs_gambs(scanin, xmax);
672 <                        for (x = 0; x < xmax; x++) {
673 <                                scanout[3*x] = scanin[x][RED];
674 <                                scanout[3*x+1] = scanin[x][GRN];
675 <                                scanout[3*x+2] = scanin[x][BLU];
645 >
646 >        initfromrad();                          /* initialize conversion */
647 >
648 >        for (y = 0; y < cvts.ymax; y++)         /* convert image */
649 >                (*cvts.tf)(y);
650 >                                                /* clean up */
651 >        TIFFClose(cvts.tif);
652 >        fclose(cvts.rfp);
653 > }
654 >
655 >
656 > static void
657 > ReadRadScan(struct CONVST *cvp)
658 > {
659 >        static struct CONVST    *lastcvp = NULL;
660 >        static COLOR            scomp[MAXCSAMP];
661 >
662 >        if (cvp->ncc > 3) {                     /* convert spectral pixels */
663 >                SCOLR           sclr;
664 >                SCOLOR          scol;
665 >                COLOR           xyz;
666 >                int             x, n;
667 >                if (lastcvp != cvp) {
668 >                    for (n = cvp->ncc; n--; )
669 >                        spec_cie(scomp[n],
670 >                                cvp->wpt[0] + (cvp->wpt[3] - cvp->wpt[0])*(n+1)/cvp->ncc,
671 >                                cvp->wpt[0] + (cvp->wpt[3] - cvp->wpt[0])*n/cvp->ncc);
672 >                    lastcvp = cvp;
673 >                }
674 >                for (x = 0; x < cvp->xmax; x++) {
675 >                        if (getbinary(sclr, cvp->ncc+1, 1, cvp->rfp) != 1)
676 >                                goto readerr;
677 >                        scolr2scolor(scol, sclr, cvp->ncc);
678 >                        setcolor(cvp->r.colors[x], 0, 0, 0);
679 >                        for (n = cvp->ncc; n--; ) {
680 >                                copycolor(xyz, scomp[n]);
681 >                                scalecolor(xyz, scol[n]);
682 >                                addcolor(cvp->r.colors[x], xyz);
683                          }
684                  }
685 <                if (TIFFWriteScanline(tif, scanout, y, 0) < 0)
246 <                        quiterr("error writing TIFF output");
685 >                return;
686          }
687 <                                                /* clean up */
688 <        free((char *)scanin);
689 <        free((char *)scanout);
690 <        TIFFClose(tif);
687 >        if (freadscan(cvp->r.colors, cvp->xmax, cvp->rfp) >= 0)
688 >                return;
689 > readerr:
690 >        quiterr("error reading Radiance picture");
691 > }
692 >
693 >
694 > static void
695 > Luv2Color(                      /* read/convert/write Luv->COLOR scanline */
696 >        uint32  y
697 > )
698 > {
699 >        int     x;
700 >
701 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT))
702 >                quiterr("internal error 1 in Luv2Color");
703 >
704 >        if (cvts.pconf != PLANARCONFIG_CONTIG)
705 >                quiterr("cannot handle separate 32-bit color planes");
706 >
707 >        if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
708 >                quiterr("error reading TIFF input");
709 >                                        /* also works for float RGB */
710 >        for (x = cvts.xmax; x--; ) {
711 >                setcolor(cvts.r.colors[x],
712 >                                cvts.t.fp[3*x],
713 >                                cvts.t.fp[3*x + 1],
714 >                                cvts.t.fp[3*x + 2]);
715 >                if (CHK(C_CXFM))
716 >                        colortrans(cvts.r.colors[x], cvts.cmat,
717 >                                        cvts.r.colors[x]);
718 >                if (CHK(C_GAMUT))
719 >                        clipgamut(cvts.r.colors[x], cvts.t.fp[3*x + 1],
720 >                                        CGAMUT_LOWER, cblack, cwhite);
721 >        }
722 >        if (cvts.bradj) {
723 >                double  m = pow(2.,(double)cvts.bradj);
724 >                for (x = cvts.xmax; x--; )
725 >                        scalecolor(cvts.r.colors[x], m);
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 > RRGGBB2Color(                   /* read/convert/write RGB16->COLOR scanline */
735 >        uint32  y
736 > )
737 > {
738 >        int     dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
739 >        double  d;
740 >        int     x;
741 >
742 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_TWRD|C_RFLT))
743 >                quiterr("internal error 1 in RRGGBB2Color");
744 >
745 >        if (cvts.pconf != PLANARCONFIG_CONTIG)
746 >                quiterr("cannot handle separate 16-bit color planes");
747 >
748 >        if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
749 >                quiterr("error reading TIFF input");
750 >        
751 >        for (x = cvts.xmax; x--; ) {
752 >                d = (cvts.t.wp[3*x] + 0.5)*(1./(1L<<16));
753 >                if (dogamma) d = pow(d, cvts.gamcor);
754 >                colval(cvts.r.colors[x],RED) = d;
755 >                d = (cvts.t.wp[3*x + 1] + 0.5)*(1./(1L<<16));
756 >                if (dogamma) d = pow(d, cvts.gamcor);
757 >                colval(cvts.r.colors[x],GRN) = d;
758 >                d = (cvts.t.wp[3*x + 2] + 0.5)*(1./(1L<<16));
759 >                if (dogamma) d = pow(d, cvts.gamcor);
760 >                colval(cvts.r.colors[x],BLU) = d;
761 >                if (CHK(C_CXFM))
762 >                        colortrans(cvts.r.colors[x], cvts.cmat,
763 >                                        cvts.r.colors[x]);
764 >        }
765 >        if (cvts.bradj) {
766 >                d = pow(2.,(double)cvts.bradj);
767 >                for (x = cvts.xmax; x--; )
768 >                        scalecolor(cvts.r.colors[x], d);
769 >        }
770 >
771 >        if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
772 >                quiterr("error writing Radiance picture");
773 > }
774 >
775 >
776 > static void
777 > L2Color(                        /* read/convert/write Lfloat->COLOR scanline */
778 >        uint32  y
779 > )
780 > {
781 >        float   m = pow(2., (double)cvts.bradj);
782 >        int     x;
783 >
784 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY))
785 >                quiterr("internal error 1 in L2Color");
786 >
787 >        if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
788 >                quiterr("error reading TIFF input");
789 >                                        /* also works for float greyscale */
790 >        for (x = cvts.xmax; x--; ) {
791 >                float   f = cvts.t.fp[x];
792 >                if (cvts.bradj) f *= m;
793 >                setcolor(cvts.r.colors[x], f, f, f);
794 >        }
795 >        if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
796 >                quiterr("error writing Radiance picture");
797 > }
798 >
799 >
800 > static void
801 > RGB2Colr(                       /* read/convert/write RGB->COLR scanline */
802 >        uint32  y
803 > )
804 > {
805 >        COLOR   ctmp;
806 >        int     x;
807 >
808 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY))
809 >                quiterr("internal error 1 in RGB2Colr");
810 >
811 >        if (cvts.pconf == PLANARCONFIG_CONTIG) {
812 >                if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
813 >                        goto readerr;
814 >                for (x = cvts.xmax; x--; ) {
815 >                        cvts.r.colrs[x][RED] = cvts.t.bp[3*x];
816 >                        cvts.r.colrs[x][GRN] = cvts.t.bp[3*x + 1];
817 >                        cvts.r.colrs[x][BLU] = cvts.t.bp[3*x + 2];
818 >                }
819 >        } else {
820 >                if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
821 >                        goto readerr;
822 >                if (TIFFReadScanline(cvts.tif,
823 >                                (tdata_t)(cvts.t.bp + cvts.xmax), y, 1) < 0)
824 >                        goto readerr;
825 >                if (TIFFReadScanline(cvts.tif,
826 >                                (tdata_t)(cvts.t.bp + 2*cvts.xmax), y, 2) < 0)
827 >                        goto readerr;
828 >                for (x = cvts.xmax; x--; ) {
829 >                        cvts.r.colrs[x][RED] = cvts.t.bp[x];
830 >                        cvts.r.colrs[x][GRN] = cvts.t.bp[cvts.xmax + x];
831 >                        cvts.r.colrs[x][BLU] = cvts.t.bp[2*cvts.xmax + x];
832 >                }
833 >        }
834 >
835 >        gambs_colrs(cvts.r.colrs, cvts.xmax);
836 >        if (CHK(C_CXFM))
837 >                for (x = cvts.xmax; x--; ) {
838 >                        colr_color(ctmp, cvts.r.colrs[x]);
839 >                        colortrans(ctmp, cvts.cmat, ctmp);
840 >                        if (CHK(C_GAMUT))       /* !CHK(C_XYZE) */
841 >                                clipgamut(ctmp, bright(ctmp), CGAMUT_LOWER,
842 >                                                cblack, cwhite);
843 >                        setcolr(cvts.r.colrs[x], colval(ctmp,RED),
844 >                                        colval(ctmp,GRN), colval(ctmp,BLU));
845 >                }
846 >        if (cvts.bradj)
847 >                shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
848 >
849 >        if (fwritecolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
850 >                quiterr("error writing Radiance picture");
851 >        return;
852 > readerr:
853 >        quiterr("error reading TIFF input");
854 > }
855 >
856 >
857 > static void
858 > Gry2Colr(                       /* read/convert/write G8->COLR scanline */
859 >        uint32  y
860 > )
861 > {
862 >        int     x;
863 >
864 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != C_GRY)
865 >                quiterr("internal error 1 in Gry2Colr");
866 >
867 >        if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
868 >                quiterr("error reading TIFF input");
869 >
870 >        for (x = cvts.xmax; x--; )
871 >                cvts.r.colrs[x][RED] =
872 >                cvts.r.colrs[x][GRN] =
873 >                cvts.r.colrs[x][BLU] = cvts.t.bp[x];
874 >
875 >        gambs_colrs(cvts.r.colrs, cvts.xmax);
876 >        if (cvts.bradj)
877 >                shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
878 >
879 >        if (fwritecolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
880 >                quiterr("error writing Radiance picture");
881 > }
882 >
883 >
884 > static void
885 > GGry2Color(                     /* read/convert/write G16->COLOR scanline */
886 >        uint32  y
887 > )
888 > {
889 >        int     dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
890 >        double  m;
891 >        double  d;
892 >        int     x;
893 >
894 >        if (CHK(C_TFLT|C_TWRD|C_GRY|C_RFLT) != (C_GRY|C_RFLT|C_TWRD))
895 >                quiterr("internal error 1 in GGry2Color");
896 >
897 >        if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
898 >                quiterr("error reading TIFF input");
899 >
900 >        if (cvts.bradj)
901 >                m = pow(2., (double)cvts.bradj);
902 >        for (x = cvts.xmax; x--; ) {
903 >                d = (cvts.t.wp[x] + 0.5)*(1./(1L<<16));
904 >                if (dogamma) d = pow(d, cvts.gamcor);
905 >                if (cvts.bradj) d *= m;
906 >                colval(cvts.r.colors[x],RED) =
907 >                colval(cvts.r.colors[x],GRN) =
908 >                colval(cvts.r.colors[x],BLU) = d;
909 >        }
910 >        if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
911 >                quiterr("error writing Radiance picture");
912 > }
913 >
914 >
915 > static void
916 > Color2GGry(                     /* read/convert/write COLOR->G16 scanline */
917 >        uint32  y
918 > )
919 > {
920 >        int     dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
921 >        float   m = pow(2.,(double)cvts.bradj);
922 >        int     x;
923 >
924 >        if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TWRD|C_GRY))
925 >                quiterr("internal error 1 in Color2GGry");
926 >
927 >        ReadRadScan(&cvts);
928 >
929 >        for (x = cvts.xmax; x--; ) {
930 >                float   f = m*( CHK(C_XYZE) ?
931 >                                                colval(cvts.r.colors[x],CIEY)
932 >                                                : bright(cvts.r.colors[x]) );
933 >                if (f <= 0)
934 >                        cvts.t.wp[x] = 0;
935 >                else if (f >= 1)
936 >                        cvts.t.wp[x] = 0xffff;
937 >                else if (dogamma)
938 >                        cvts.t.wp[x] = (int)((float)(1L<<16) *
939 >                                                pow(f, 1./cvts.gamcor));
940 >                else
941 >                        cvts.t.wp[x] = (int)((float)(1L<<16) * f);
942 >        }
943 >
944 >        if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
945 >                quiterr("error writing TIFF output");
946 > }
947 >
948 >
949 > static void
950 > Color2L(                        /* read/convert/write COLOR->Lfloat scanline */
951 >        uint32  y
952 > )
953 > {
954 >        float   m = pow(2.,(double)cvts.bradj);
955 >        int     x;
956 >
957 >        if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TFLT|C_GRY))
958 >                quiterr("internal error 1 in Color2L");
959 >
960 >        ReadRadScan(&cvts);
961 >
962 >        for (x = cvts.xmax; x--; )
963 >                cvts.t.fp[x] = m*( CHK(C_XYZE) ? colval(cvts.r.colors[x],CIEY)
964 >                                                : bright(cvts.r.colors[x]) );
965 >
966 >        if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
967 >                quiterr("error writing TIFF output");
968 > }
969 >
970 >
971 > static void
972 > Color2Luv(                      /* read/convert/write COLOR->Luv scanline */
973 >        uint32  y
974 > )
975 > {
976 >        int     x;
977 >
978 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT))
979 >                quiterr("internal error 1 in Color2Luv");
980 >
981 >        ReadRadScan(&cvts);
982 >
983 >        if (CHK(C_CXFM))
984 >                for (x = cvts.xmax; x--; )
985 >                        colortrans(cvts.r.colors[x], cvts.cmat,
986 >                                        cvts.r.colors[x]);
987 >        if (cvts.bradj) {
988 >                double  m = pow(2.,(double)cvts.bradj);
989 >                for (x = cvts.xmax; x--; )
990 >                        scalecolor(cvts.r.colors[x], m);
991 >        }
992 >                                        /* also works for float RGB */
993 >        for (x = cvts.xmax; x--; ) {
994 >                cvts.t.fp[3*x] = colval(cvts.r.colors[x],CIEX);
995 >                cvts.t.fp[3*x+1] = colval(cvts.r.colors[x],CIEY);
996 >                cvts.t.fp[3*x+2] = colval(cvts.r.colors[x],CIEZ);
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 > Color2RRGGBB(                   /* read/convert/write COLOR->RGB16 scanline */
1006 >        uint32  y
1007 > )
1008 > {
1009 >        int     dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
1010 >        float   m = pow(2.,(double)cvts.bradj);
1011 >        int     x, i;
1012 >
1013 >        if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TWRD))
1014 >                quiterr("internal error 1 in Color2RRGGBB");
1015 >
1016 >        ReadRadScan(&cvts);
1017 >
1018 >        for (x = cvts.xmax; x--; ) {
1019 >            if (CHK(C_CXFM)) {
1020 >                        colortrans(cvts.r.colors[x], cvts.cmat,
1021 >                                        cvts.r.colors[x]);
1022 >                if (CHK(C_GAMUT))
1023 >                        clipgamut(cvts.r.colors[x], bright(cvts.r.colors[x]),
1024 >                                        CGAMUT_LOWER, cblack, cwhite);
1025 >            }
1026 >            for (i = 3; i--; ) {
1027 >                float   f = m*colval(cvts.r.colors[x],i);
1028 >                if (f <= 0)
1029 >                        cvts.t.wp[3*x + i] = 0;
1030 >                else if (f >= 1)
1031 >                        cvts.t.wp[3*x + i] = 0xffff;
1032 >                else if (dogamma)
1033 >                        cvts.t.wp[3*x + i] = (int)((float)(1L<<16) *
1034 >                                                pow(f, 1./cvts.gamcor));
1035 >                else
1036 >                        cvts.t.wp[3*x + i] = (int)((float)(1L<<16)*f);
1037 >            }
1038 >        }
1039 >
1040 >        if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
1041 >                quiterr("error writing TIFF output");
1042 > }
1043 >
1044 >
1045 > static void
1046 > Colr2Gry(                       /* read/convert/write COLR->RGB scanline */
1047 >        uint32  y
1048 > )
1049 > {
1050 >        int     x;
1051 >
1052 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != C_GRY)
1053 >                quiterr("internal error 1 in Colr2Gry");
1054 >
1055 >        if (fread2colrs(cvts.r.colrs, cvts.xmax, cvts.rfp,
1056 >                                cvts.ncc, cvts.wpt) < 0)
1057 >                quiterr("error reading Radiance picture");
1058 >
1059 >        if (cvts.bradj)
1060 >                shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
1061 >        for (x = cvts.xmax; x--; )
1062 >                colval(cvts.r.colrs[x],CIEY) = normbright(cvts.r.colrs[x]);
1063 >        colrs_gambs(cvts.r.colrs, cvts.xmax);
1064 >
1065 >        for (x = cvts.xmax; x--; )
1066 >                cvts.t.bp[x] = colval(cvts.r.colrs[x],CIEY);
1067 >
1068 >        if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
1069 >                quiterr("error writing TIFF output");
1070 > }
1071 >
1072 >
1073 > static void
1074 > Colr2RGB(                       /* read/convert/write COLR->RGB scanline */
1075 >        uint32  y
1076 > )
1077 > {
1078 >        COLOR   ctmp;
1079 >        int     x;
1080 >
1081 >        if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY))
1082 >                quiterr("internal error 1 in Colr2RGB");
1083 >
1084 >        if (fread2colrs(cvts.r.colrs, cvts.xmax, cvts.rfp,
1085 >                                cvts.ncc, cvts.wpt) < 0)
1086 >                quiterr("error reading Radiance picture");
1087 >
1088 >        if (cvts.bradj)
1089 >                shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
1090 >        if (CHK(C_CXFM))
1091 >                for (x = cvts.xmax; x--; ) {
1092 >                        colr_color(ctmp, cvts.r.colrs[x]);
1093 >                        colortrans(ctmp, cvts.cmat, ctmp);
1094 >                        if (CHK(C_GAMUT))
1095 >                                clipgamut(ctmp, bright(ctmp), CGAMUT,
1096 >                                                cblack, cwhite);
1097 >                        setcolr(cvts.r.colrs[x], colval(ctmp,RED),
1098 >                                        colval(ctmp,GRN), colval(ctmp,BLU));
1099 >                }
1100 >        colrs_gambs(cvts.r.colrs, cvts.xmax);
1101 >
1102 >        for (x = cvts.xmax; x--; ) {
1103 >                cvts.t.bp[3*x] = cvts.r.colrs[x][RED];
1104 >                cvts.t.bp[3*x+1] = cvts.r.colrs[x][GRN];
1105 >                cvts.t.bp[3*x+2] = cvts.r.colrs[x][BLU];
1106 >        }
1107 >
1108 >        if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
1109 >                quiterr("error writing TIFF output");
1110   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines