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

Comparing ray/src/px/ra_tiff.c (file contents):
Revision 2.10 by gregl, Thu Jul 31 21:26:45 1997 UTC vs.
Revision 2.40 by greg, Sat Jun 7 05:09:46 2025 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  Program to convert between RADIANCE and TIFF files.
6 < *  Added experimental LogLuv encodings 7/97 (GWL).
6 > *  Added LogLuv encodings 7/97 (GWL).
7 > *  Added white-balance adjustment 10/01 (GW).
8   */
9  
12 #include  <stdio.h>
10   #include  <math.h>
11 + #include  <ctype.h>
12 + #include  "rtio.h"
13 + #include  "platform.h"
14   #include  "tiffio.h"
15   #include  "color.h"
16   #include  "resolu.h"
17  
18   #define  GAMCOR         2.2             /* default gamma */
19  
20 #ifndef malloc
21 extern char  *malloc();
22 #endif
20                                  /* conversion flags */
21   #define C_CXFM          0x1             /* needs color transformation */
22   #define C_GAMUT         0x2             /* needs gamut mapping */
23   #define C_GAMMA         0x4             /* needs gamma correction */
24   #define C_GRY           0x8             /* TIFF is greyscale */
25   #define C_XYZE          0x10            /* Radiance is XYZE */
26 < #define C_RFLT          0x20            /* Radiance data is float */
27 < #define C_TFLT          0x40            /* TIFF data is float */
28 < #define C_PRIM          0x80            /* has assigned primaries */
26 > #define C_SPEC          0x20            /* Radiance is spectral */
27 > #define C_RFLT          0x40            /* Radiance data is float */
28 > #define C_TFLT          0x80            /* TIFF data is float */
29 > #define C_TWRD          0x100           /* TIFF data is 16-bit */
30 > #define C_PRIM          0x200           /* has assigned primaries */
31  
32 < struct {
32 > typedef void colcvf_t(uint32 y);
33 >
34 > struct CONVST {
35          uint16  flags;          /* conversion flags (defined above) */
36 +        char    capdate[20];    /* capture date/time */
37 +        char    owner[256];     /* content owner */
38          uint16  comp;           /* TIFF compression type */
39          uint16  phot;           /* TIFF photometric type */
40          uint16  pconf;          /* TIFF planar configuration */
41          float   gamcor;         /* gamma correction value */
42          short   bradj;          /* Radiance exposure adjustment (stops) */
43          uint16  orient;         /* visual orientation (TIFF spec.) */
44 <        float   stonits;        /* input conversion to nits */
44 >        double  stonits;        /* input conversion to nits */
45          float   pixrat;         /* pixel aspect ratio */
46 +        int     ncc;            /* # color components for spectral */
47 +        float   wpt[4];         /* wavelength partitions */
48          FILE    *rfp;           /* Radiance stream pointer */
49          TIFF    *tif;           /* TIFF pointer */
50          uint32  xmax, ymax;     /* image dimensions */
# Line 48 | Line 53 | struct {
53          union {
54                  COLR    *colrs;         /* 4-byte ???E pointer */
55                  COLOR   *colors;        /* float array pointer */
56 <                char    *p;             /* generic pointer */
56 >                void    *p;             /* generic pointer */
57          } r;                    /* Radiance scanline */
58          union {
59                  uint8   *bp;            /* byte pointer */
60 +                uint16  *wp;            /* word pointer */
61                  float   *fp;            /* float pointer */
62 <                char    *p;             /* generic pointer */
62 >                void    *p;             /* generic pointer */
63          } t;                    /* TIFF scanline */
64 <        int     (*tf)();        /* translation procedure */
64 >        colcvf_t *tf;   /* translation procedure */
65   }       cvts = {        /* conversion structure */
66 <        0, COMPRESSION_NONE, PHOTOMETRIC_RGB,
67 <        PLANARCONFIG_CONTIG, GAMCOR, 0, 1, 1., 1.,
66 >        0, "", "", COMPRESSION_NONE, PHOTOMETRIC_RGB,
67 >        PLANARCONFIG_CONTIG, GAMCOR, 0, 1, 1., 1., 3,
68   };
69  
70   #define CHK(f)          (cvts.flags & (f))
# Line 66 | Line 72 | struct {
72   #define CLR(f)          (cvts.flags &= ~(f))
73   #define TGL(f)          (cvts.flags ^= (f))
74  
75 < int     Luv2Color(), L2Color(), RGB2Colr(), Gry2Colr();
76 < int     Color2Luv(), Color2L(), Colr2RGB(), Colr2Gry();
75 > static colcvf_t Luv2Color, L2Color, RGB2Colr, Gry2Colr;
76 > static colcvf_t Color2Luv, Color2L, Colr2RGB, Colr2Gry;
77 > static colcvf_t RRGGBB2Color, GGry2Color, Color2RRGGBB, Color2GGry;
78  
79 + static gethfunc headline;
80 + static void quiterr(char *err);
81 + static void allocbufs(void);
82 + static void initfromtif(void);
83 + static void tiff2ra(int  ac, char  *av[]);
84 + static void initfromrad(void);
85 + static void ra2tiff(int  ac, char  *av[]);
86 + static void ReadRadScan(struct CONVST *cvp);
87 +
88 +
89 + #define RfGfBf2Color    Luv2Color
90 + #define Gryf2Color      L2Color
91 + #define Color2Gryf      Color2L
92 + #define Color2RfGfBf    Color2Luv
93 +
94   short   ortab[8] = {            /* orientation conversion table */
95          YMAJOR|YDECR,
96          YMAJOR|YDECR|XDECR,
# Line 82 | Line 104 | short  ortab[8] = {            /* orientation conversion table */
104  
105   #define pixorder()      ortab[cvts.orient-1]
106  
107 < char  *progname;
107 > extern char     TMSTR[];        /* "CAPDATE=" from header.c */
108 > char            OWNSTR[] = "OWNER=";
109  
110  
111 < main(argc, argv)
112 < int  argc;
90 < char  *argv[];
111 > int
112 > main(int  argc, char  *argv[])
113   {
114          int  reverse = 0;
115          int  i;
116 <        
117 <        progname = argv[0];
116 >                                        /* set global progname */
117 >        fixargv0(argv[0]);
118  
119          for (i = 1; i < argc; i++)
120                  if (argv[i][0] == '-')
# Line 114 | Line 136 | char  *argv[];
136                                  cvts.comp = COMPRESSION_SGILOG24;
137                                  cvts.phot = PHOTOMETRIC_LOGLUV;
138                                  break;
139 +                        case 'w':               /* 16-bit/primary output? */
140 +                                TGL(C_TWRD);
141 +                                break;
142 +                        case 'f':               /* IEEE float output? */
143 +                                TGL(C_TFLT);
144 +                                break;
145                          case 'b':               /* greyscale output? */
146                                  TGL(C_GRY);
147                                  break;
# Line 144 | Line 172 | doneopts:
172  
173                  if (i != argc-2)
174                          goto userr;
175 <
176 <                if (CHK(C_GRY))         /* consistency corrections */
175 >                                                /* consistency checks */
176 >                if (CHK(C_GRY)) {
177                          if (cvts.phot == PHOTOMETRIC_RGB)
178                                  cvts.phot = PHOTOMETRIC_MINISBLACK;
179                          else {
180                                  cvts.phot = PHOTOMETRIC_LOGL;
181                                  cvts.comp = COMPRESSION_SGILOG;
182                          }
183 +                }
184 +                if (CHK(C_TWRD|C_TFLT) == (C_TWRD|C_TFLT))
185 +                        goto userr;
186  
187                  ra2tiff(i, argv);
188          }
# Line 159 | Line 190 | doneopts:
190          exit(0);
191   userr:
192          fprintf(stderr,
193 <        "Usage: %s [-z|-L|-l][-b][-e +/-stops][-g gamma] {in.pic|-} out.tif\n",
193 >        "Usage: %s [-z|-L|-l|-f|-w][-b][-e +/-stops][-g gamma] {in.hdr|-} out.tif\n",
194                          progname);
195          fprintf(stderr,
196 <        "   Or: %s -r [-x][-e +/-stops][-g gamma] in.tif [out.pic|-]\n",
196 >        "   Or: %s -r [-x][-e +/-stops][-g gamma] in.tif [out.hdr|-]\n",
197                          progname);
198          exit(1);
199   }
200  
201  
202 < quiterr(err)            /* print message and exit */
203 < char  *err;
202 > static void
203 > quiterr(                /* print message and exit */
204 >        char  *err
205 > )
206   {
207          if (err != NULL) {
208                  fprintf(stderr, "%s: %s\n", progname, err);
# Line 179 | Line 212 | char  *err;
212   }
213  
214  
215 < allocbufs()                     /* allocate scanline buffers */
215 > static void
216 > allocbufs(void)                 /* allocate scanline buffers */
217   {
218          int     rsiz, tsiz;
219  
220          rsiz = CHK(C_RFLT) ? sizeof(COLOR) : sizeof(COLR);
221 <        tsiz = (CHK(C_TFLT) ? sizeof(float) : sizeof(uint8)) *
221 >        tsiz = (CHK(C_TFLT) ? sizeof(float) :
222 >                        CHK(C_TWRD) ? sizeof(uint16) : sizeof(uint8)) *
223                          (CHK(C_GRY) ? 1 : 3);
224 <        cvts.r.p = (char *)malloc(rsiz*cvts.xmax);
225 <        cvts.t.p = (char *)malloc(tsiz*cvts.xmax);
226 <        if (cvts.r.p == NULL | cvts.t.p == NULL)
224 >        cvts.r.p = malloc(rsiz*cvts.xmax);
225 >        cvts.t.p = malloc(tsiz*cvts.xmax);
226 >        if ((cvts.r.p == NULL) | (cvts.t.p == NULL))
227                  quiterr("no memory to allocate scanline buffers");
228   }
229  
230  
231 < initfromtif()           /* initialize conversion from TIFF input */
231 > static void
232 > initfromtif(void)               /* initialize conversion from TIFF input */
233   {
234          uint16  hi;
235 +        char    *cp;
236          float   *fa, f1, f2;
237  
201        CLR(C_GRY|C_GAMMA|C_PRIM|C_RFLT|C_TFLT|C_CXFM);
202
238          TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_PLANARCONFIG, &cvts.pconf);
239  
240          if (TIFFGetField(cvts.tif, TIFFTAG_PRIMARYCHROMATICITIES, &fa)) {
# Line 237 | Line 272 | initfromtif()          /* initialize conversion from TIFF inpu
272                          cpcolormat(cvts.cmat, xyz2rgbmat);
273                          SET(C_CXFM|C_GAMUT);
274                  } else if (cvts.comp == COMPRESSION_SGILOG)
275 <                        SET(C_GAMUT);
275 >                        SET(C_GAMUT);           /* may be outside XYZ gamut */
276                  if (cvts.pconf != PLANARCONFIG_CONTIG)
277                          quiterr("cannot handle separate Luv planes");
278                  TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
279 <                                SGILOGDATAFMT_FLTXYZ);
279 >                                SGILOGDATAFMT_FLOAT);
280                  cvts.tf = Luv2Color;
281                  break;
282          case PHOTOMETRIC_LOGL:
283                  SET(C_GRY|C_RFLT|C_TFLT|C_GAMUT);
284                  cvts.pconf = PLANARCONFIG_CONTIG;
285                  TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
286 <                                SGILOGDATAFMT_FLTY);
286 >                                SGILOGDATAFMT_FLOAT);
287                  cvts.tf = L2Color;
288                  break;
289 +        case PHOTOMETRIC_YCBCR:
290 +                if (cvts.comp == COMPRESSION_JPEG &&
291 +                                cvts.pconf == PLANARCONFIG_CONTIG) {
292 +                        TIFFSetField(cvts.tif, TIFFTAG_JPEGCOLORMODE,
293 +                                        JPEGCOLORMODE_RGB);
294 +                        cvts.phot = PHOTOMETRIC_RGB;
295 +                } else
296 +                        quiterr("unsupported photometric type");
297 +                /* fall through */
298          case PHOTOMETRIC_RGB:
299 <                SET(C_GAMMA|C_GAMUT);
299 >                SET(C_GAMMA);
300                  setcolrgam(cvts.gamcor);
301                  if (CHK(C_XYZE)) {
302 <                        comprgb2xyzmat(cvts.cmat,
302 >                        comprgb2xyzWBmat(cvts.cmat,
303                                          CHK(C_PRIM) ? cvts.prims : stdprims);
304                          SET(C_CXFM);
305                  }
306                  if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) ||
307                                  hi != 3)
308                          quiterr("unsupported samples per pixel for RGB");
309 <                if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi) ||
310 <                                hi != 8)
309 >                if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi))
310 >                        hi = -1;
311 >                switch (hi) {
312 >                case 8:
313 >                        cvts.tf = RGB2Colr;
314 >                        break;
315 >                case 16:
316 >                        cvts.tf = RRGGBB2Color;
317 >                        SET(C_RFLT|C_TWRD);
318 >                        break;
319 >                case 32:
320 >                        cvts.tf = RfGfBf2Color;
321 >                        SET(C_RFLT|C_TFLT);
322 >                        break;
323 >                default:
324                          quiterr("unsupported bits per sample for RGB");
325 <                cvts.tf = RGB2Colr;
325 >                }
326                  break;
327          case PHOTOMETRIC_MINISBLACK:
328 <                SET(C_GRY|C_GAMMA|C_GAMUT);
328 >                SET(C_GRY|C_GAMMA);
329 >                setcolrgam(cvts.gamcor);
330                  cvts.pconf = PLANARCONFIG_CONTIG;
331                  if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) ||
332                                  hi != 1)
333                          quiterr("unsupported samples per pixel for greyscale");
334 <                if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi) ||
335 <                                hi != 8)
336 <                        quiterr("unsupported bits per sample for greyscale");
337 <                cvts.tf = Gry2Colr;
334 >                if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi))
335 >                        hi = -1;
336 >                switch (hi) {
337 >                case 8:
338 >                        cvts.tf = Gry2Colr;
339 >                        break;
340 >                case 16:
341 >                        cvts.tf = GGry2Color;
342 >                        SET(C_RFLT|C_TWRD);
343 >                        break;
344 >                case 32:
345 >                        cvts.tf = Gryf2Color;
346 >                        SET(C_RFLT|C_TFLT);
347 >                        break;
348 >                default:
349 >                        quiterr("unsupported bits per sample for Gray");
350 >                }
351                  break;
352          default:
353                  quiterr("unsupported photometric type");
# Line 288 | Line 359 | initfromtif()          /* initialize conversion from TIFF inpu
359                  quiterr("unknown input image resolution");
360  
361          if (!TIFFGetField(cvts.tif, TIFFTAG_STONITS, &cvts.stonits))
362 <                cvts.stonits = 1.;
362 >                cvts.stonits = -1.;
363 >
364 >        if (!TIFFGetField(cvts.tif, TIFFTAG_DATETIME, &cp))
365 >                cvts.capdate[0] = '\0';
366 >        else {
367 >                strncpy(cvts.capdate, cp, 19);
368 >                cvts.capdate[19] = '\0';
369 >        }
370 >        if (!TIFFGetField(cvts.tif, TIFFTAG_ARTIST, &cp))
371 >                cvts.owner[0] = '\0';
372 >        else {
373 >                strncpy(cvts.owner, cp, sizeof(cvts.owner));
374 >                cvts.owner[sizeof(cvts.owner)-1] = '\0';
375 >        }
376                                          /* add to Radiance header */
377          if (cvts.pixrat < .99 || cvts.pixrat > 1.01)
378                  fputaspect(cvts.pixrat, cvts.rfp);
379          if (CHK(C_XYZE)) {
380 <                fputexpos(pow(2.,(double)cvts.bradj)/cvts.stonits, cvts.rfp);
380 >                if (cvts.stonits > .0)
381 >                        fputexpos(pow(2.,(double)cvts.bradj)/cvts.stonits, cvts.rfp);
382                  fputformat(CIEFMT, cvts.rfp);
383          } else {
384                  if (CHK(C_PRIM))
385                          fputprims(cvts.prims, cvts.rfp);
386 <                fputexpos(WHTEFFICACY*pow(2.,(double)cvts.bradj)/cvts.stonits,
387 <                                cvts.rfp);
386 >                if (cvts.stonits > .0)
387 >                        fputexpos(WHTEFFICACY*pow(2.,(double)cvts.bradj)/cvts.stonits,
388 >                                        cvts.rfp);
389                  fputformat(COLRFMT, cvts.rfp);
390          }
391 +        if (cvts.capdate[0])
392 +                fprintf(cvts.rfp, "%s %s\n", TMSTR, cvts.capdate);
393 +        if (cvts.owner[0])
394 +                fprintf(cvts.rfp, "%s %s\n", OWNSTR, cvts.owner);
395  
396          allocbufs();                    /* allocate scanline buffers */
397   }
398  
399  
400 < tiff2ra(ac, av)         /* convert TIFF image to Radiance picture */
401 < int  ac;
402 < char  *av[];
400 > static void
401 > tiff2ra(                /* convert TIFF image to Radiance picture */
402 >        int  ac,
403 >        char  *av[]
404 > )
405   {
406          int32   y;
407                                          /* open TIFF input */
# Line 320 | Line 412 | char  *av[];
412                  cvts.rfp = stdout;
413          else if ((cvts.rfp = fopen(av[ac+1], "w")) == NULL)
414                  quiterr("cannot open Radiance output picture");
415 +        SET_FILE_BINARY(cvts.rfp);
416                                          /* start output header */
417          newheader("RADIANCE", cvts.rfp);
418          printargs(ac, av, cvts.rfp);
# Line 337 | Line 430 | char  *av[];
430   }
431  
432  
433 < int
434 < headline(s)                     /* process Radiance input header line */
435 < char    *s;
433 > static int
434 > headline(                       /* process Radiance input header line */
435 >        char    *s,
436 >        void    *p
437 > )
438   {
439 <        char    fmt[32];
439 >        static int      tmstrlen = 0;
440 >        static int      ownstrlen = 0;
441 >        struct CONVST   *cvp = (struct CONVST *)p;
442 >        char            fmt[MAXFMTLEN];
443  
444 +        if (!tmstrlen) {
445 +                tmstrlen = strlen(TMSTR);
446 +                ownstrlen = strlen(OWNSTR);
447 +        }
448          if (formatval(fmt, s)) {
449 <                if (!strcmp(fmt, COLRFMT))
450 <                        CLR(C_XYZE);
349 <                else if (!strcmp(fmt, CIEFMT))
449 >                CLR(C_XYZE|C_SPEC);
450 >                if (!strcmp(fmt, CIEFMT))
451                          SET(C_XYZE);
452 <                else
453 <                        quiterr("unrecognized input picture format");
454 <                return;
452 >                else if (!strcmp(fmt, SPECFMT))
453 >                        SET(C_SPEC);
454 >                else if (strcmp(fmt, COLRFMT))
455 >                        return(-1);
456 >                return(1);
457          }
458          if (isexpos(s)) {
459 <                cvts.stonits /= exposval(s);
460 <                return;
459 >                cvp->stonits /= exposval(s);
460 >                return(1);
461          }
462          if (isaspect(s)) {
463 <                cvts.pixrat *= aspectval(s);
464 <                return;
463 >                cvp->pixrat *= aspectval(s);
464 >                return(1);
465          }
466          if (isprims(s)) {
467 <                primsval(cvts.prims, s);
467 >                primsval(cvp->prims, s);
468                  SET(C_PRIM);
469 <                return;
469 >                return(1);
470          }
471 +        if (isdate(s)) {
472 +                if (s[tmstrlen] == ' ')
473 +                        strncpy(cvp->capdate, s+tmstrlen+1, 19);
474 +                else
475 +                        strncpy(cvp->capdate, s+tmstrlen, 19);
476 +                cvp->capdate[19] = '\0';
477 +                return(1);
478 +        }
479 +        if (!strncmp(s, OWNSTR, ownstrlen)) {
480 +                char    *cp = s + ownstrlen;
481 +
482 +                while (isspace(*cp))
483 +                        ++cp;
484 +                strncpy(cvp->owner, cp, sizeof(cvp->owner));
485 +                cvp->owner[sizeof(cvp->owner)-1] = '\0';
486 +                for (cp = cvp->owner; *cp; cp++)
487 +                        ;
488 +                while (cp > cvp->owner && isspace(cp[-1]))
489 +                        *--cp = '\0';
490 +                return(1);
491 +        }
492 +        if (isncomp(s)) {
493 +                cvp->ncc = ncompval(s);
494 +                return((3 <= cvp->ncc) & (cvp->ncc < MAXCSAMP) ? 1 : -1);
495 +        }
496 +        if (iswlsplit(s))
497 +                return(wlsplitval(cvp->wpt, s) ? 1 : -1);
498 +        return(0);
499   }
500  
501  
502 < initfromrad()                   /* initialize input from a Radiance picture */
502 > static void
503 > initfromrad(void)                       /* initialize input from a Radiance picture */
504   {
505          int     i1, i2, po;
506                                                  /* read Radiance header */
507 <        CLR(C_RFLT|C_TFLT|C_XYZE|C_PRIM|C_GAMMA|C_CXFM);
508 <        cvts.stonits = 1.;
509 <        cvts.pixrat = 1.;
378 <        cvts.pconf = PLANARCONFIG_CONTIG;
379 <        getheader(cvts.rfp, headline, NULL);
380 <        if ((po = fgetresolu(&i1, &i2, cvts.rfp)) < 0)
507 >        memcpy(cvts.wpt, WLPART, sizeof(cvts.wpt));
508 >        if (getheader(cvts.rfp, headline, &cvts) < 0 ||
509 >                        (po = fgetresolu(&i1, &i2, cvts.rfp)) < 0)
510                  quiterr("bad Radiance picture");
511          cvts.xmax = i1; cvts.ymax = i2;
512          for (i1 = 0; i1 < 8; i1++)              /* interpret orientation */
# Line 398 | Line 527 | initfromrad()                  /* initialize input from a Radiance pi
527          switch (cvts.phot) {
528          case PHOTOMETRIC_LOGLUV:
529                  SET(C_RFLT|C_TFLT);
530 <                CLR(C_GRY);
531 <                if (!CHK(C_XYZE)) {
532 <                        cpcolormat(cvts.cmat, rgb2xyzmat);
530 >                CLR(C_GRY|C_TWRD);
531 >                if (!CHK(C_XYZE|C_SPEC)) {
532 >                        comprgb2xyzWBmat(cvts.cmat,
533 >                                        CHK(C_PRIM) ? cvts.prims : stdprims);
534                          SET(C_CXFM);
535                  }
536                  if (cvts.comp != COMPRESSION_SGILOG &&
537                                  cvts.comp != COMPRESSION_SGILOG24)
538                          quiterr("internal error 2 in initfromrad");
539                  TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
540 <                                SGILOGDATAFMT_FLTXYZ);
540 >                                SGILOGDATAFMT_FLOAT);
541                  cvts.tf = Color2Luv;
542                  break;
543          case PHOTOMETRIC_LOGL:
544                  SET(C_GRY|C_RFLT|C_TFLT);
545 +                CLR(C_TWRD);
546                  if (cvts.comp != COMPRESSION_SGILOG)    
547                          quiterr("internal error 3 in initfromrad");
548                  TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
549 <                                SGILOGDATAFMT_FLTY);
549 >                                SGILOGDATAFMT_FLOAT);
550                  cvts.tf = Color2L;
551                  break;
552          case PHOTOMETRIC_RGB:
553                  SET(C_GAMMA|C_GAMUT);
554                  CLR(C_GRY);
555                  setcolrgam(cvts.gamcor);
556 <                if (CHK(C_XYZE)) {
557 <                        compxyz2rgbmat(cvts.cmat,
556 >                if (CHK(C_TWRD|C_TFLT) ? CHK(C_XYZE|C_SPEC) : CHK(C_XYZE)) {
557 >                        compxyz2rgbWBmat(cvts.cmat,
558                                          CHK(C_PRIM) ? cvts.prims : stdprims);
559                          SET(C_CXFM);
560                  }
# Line 433 | Line 564 | initfromrad()                  /* initialize input from a Radiance pi
564                          TIFFSetField(cvts.tif, TIFFTAG_WHITEPOINT,
565                                          (float *)cvts.prims[WHT]);
566                  }
567 <                cvts.tf = Colr2RGB;
567 >                if (CHK(C_TWRD)) {
568 >                        cvts.tf = Color2RRGGBB;
569 >                        SET(C_RFLT);
570 >                } else if (CHK(C_TFLT)) {
571 >                        TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT,
572 >                                        SAMPLEFORMAT_IEEEFP);
573 >                        cvts.tf = Color2RfGfBf;
574 >                        SET(C_RFLT);
575 >                        CLR(C_GAMUT);
576 >                } else
577 >                        cvts.tf = Colr2RGB;
578                  break;
579          case PHOTOMETRIC_MINISBLACK:
580                  SET(C_GRY|C_GAMMA|C_GAMUT);
581                  setcolrgam(cvts.gamcor);
582 <                cvts.tf = Colr2Gry;
582 >                if (CHK(C_TWRD)) {
583 >                        cvts.tf = Color2GGry;
584 >                        SET(C_RFLT);
585 >                } else if (CHK(C_TFLT)) {
586 >                        TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT,
587 >                                        SAMPLEFORMAT_IEEEFP);
588 >                        cvts.tf = Color2Gryf;
589 >                        SET(C_RFLT);
590 >                } else
591 >                        cvts.tf = Colr2Gry;
592                  break;
593          default:
594                  quiterr("internal error 4 in initfromrad");
# Line 448 | Line 598 | initfromrad()                  /* initialize input from a Radiance pi
598          TIFFSetField(cvts.tif, TIFFTAG_IMAGEWIDTH, cvts.xmax);
599          TIFFSetField(cvts.tif, TIFFTAG_IMAGELENGTH, cvts.ymax);
600          TIFFSetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, CHK(C_GRY) ? 1 : 3);
601 <        TIFFSetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, CHK(C_TFLT) ? 32 : 8);
601 >        TIFFSetField(cvts.tif, TIFFTAG_BITSPERSAMPLE,
602 >                        CHK(C_TFLT) ? 32 : CHK(C_TWRD) ? 16 : 8);
603          TIFFSetField(cvts.tif, TIFFTAG_XRESOLUTION, 72.);
604          TIFFSetField(cvts.tif, TIFFTAG_YRESOLUTION, 72./cvts.pixrat);
605          TIFFSetField(cvts.tif, TIFFTAG_ORIENTATION, cvts.orient);
# Line 456 | Line 607 | initfromrad()                  /* initialize input from a Radiance pi
607          TIFFSetField(cvts.tif, TIFFTAG_PLANARCONFIG, cvts.pconf);
608          TIFFSetField(cvts.tif, TIFFTAG_STONITS,
609                          cvts.stonits/pow(2.,(double)cvts.bradj));
610 +        if (cvts.capdate[0])
611 +                TIFFSetField(cvts.tif, TIFFTAG_DATETIME, cvts.capdate);
612 +        if (cvts.owner[0])
613 +                TIFFSetField(cvts.tif, TIFFTAG_ARTIST, cvts.owner);
614          if (cvts.comp == COMPRESSION_NONE)
615                  i1 = TIFFScanlineSize(cvts.tif);
616          else
# Line 468 | Line 623 | initfromrad()                  /* initialize input from a Radiance pi
623   }
624  
625  
626 < ra2tiff(ac, av)         /* convert Radiance picture to TIFF image */
627 < int  ac;
628 < char  *av[];
626 > static void
627 > ra2tiff(                /* convert Radiance picture to TIFF image */
628 >        int  ac,
629 >        char  *av[]
630 > )
631   {
632          uint32  y;
633                                                  /* open Radiance file */
# Line 478 | Line 635 | char  *av[];
635                  cvts.rfp = stdin;
636          else if ((cvts.rfp = fopen(av[ac], "r")) == NULL)
637                  quiterr("cannot open Radiance input picture");
638 +        SET_FILE_BINARY(cvts.rfp);
639                                                  /* open TIFF file */
640          if ((cvts.tif = TIFFOpen(av[ac+1], "w")) == NULL)
641                  quiterr("cannot open TIFF output");
# Line 492 | Line 650 | char  *av[];
650   }
651  
652  
653 < int
654 < Luv2Color(y)                    /* read/convert/write Luv->COLOR scanline */
497 < uint32  y;
653 > static void
654 > ReadRadScan(struct CONVST *cvp)
655   {
656 <        register int    x;
656 >        static struct CONVST    *lastcvp = NULL;
657 >        static COLOR            scomp[MAXCSAMP];
658  
659 <        if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | CHK(C_GRY))
659 >        if (cvp->ncc > 3) {                     /* convert spectral pixels */
660 >                SCOLR           sclr;
661 >                SCOLOR          scol;
662 >                COLOR           xyz;
663 >                int             x, n;
664 >                if (lastcvp != cvp) {
665 >                    for (n = cvp->ncc; n--; )
666 >                        spec_cie(scomp[n],
667 >                                cvp->wpt[0] + (cvp->wpt[3] - cvp->wpt[0])*(n+1)/cvp->ncc,
668 >                                cvp->wpt[0] + (cvp->wpt[3] - cvp->wpt[0])*n/cvp->ncc);
669 >                    lastcvp = cvp;
670 >                }
671 >                for (x = 0; x < cvp->xmax; x++) {
672 >                        if (getbinary(sclr, cvp->ncc+1, 1, cvp->rfp) != 1)
673 >                                goto readerr;
674 >                        scolr2scolor(scol, sclr, cvp->ncc);
675 >                        setcolor(cvp->r.colors[x], 0, 0, 0);
676 >                        for (n = cvp->ncc; n--; ) {
677 >                                copycolor(xyz, scomp[n]);
678 >                                scalecolor(xyz, scol[n]);
679 >                                addcolor(cvp->r.colors[x], xyz);
680 >                        }
681 >                }
682 >                return;
683 >        }
684 >        if (freadscan(cvp->r.colors, cvp->xmax, cvp->rfp) >= 0)
685 >                return;
686 > readerr:
687 >        quiterr("error reading Radiance picture");
688 > }
689 >
690 >
691 > static void
692 > Luv2Color(                      /* read/convert/write Luv->COLOR scanline */
693 >        uint32  y
694 > )
695 > {
696 >        int     x;
697 >
698 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT))
699                  quiterr("internal error 1 in Luv2Color");
700  
701 +        if (cvts.pconf != PLANARCONFIG_CONTIG)
702 +                quiterr("cannot handle separate 32-bit color planes");
703 +
704          if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
705                  quiterr("error reading TIFF input");
706 <        
706 >                                        /* also works for float RGB */
707          for (x = cvts.xmax; x--; ) {
708 <                cvts.r.colors[x][RED] = cvts.t.fp[3*x];
709 <                cvts.r.colors[x][GRN] = cvts.t.fp[3*x + 1];
710 <                cvts.r.colors[x][BLU] = cvts.t.fp[3*x + 2];
708 >                setcolor(cvts.r.colors[x],
709 >                                cvts.t.fp[3*x],
710 >                                cvts.t.fp[3*x + 1],
711 >                                cvts.t.fp[3*x + 2]);
712                  if (CHK(C_CXFM))
713                          colortrans(cvts.r.colors[x], cvts.cmat,
714                                          cvts.r.colors[x]);
# Line 526 | Line 727 | uint32 y;
727   }
728  
729  
730 < int
731 < L2Color(y)                      /* read/convert/write L16->COLOR scanline */
732 < uint32  y;
730 > static void
731 > RRGGBB2Color(                   /* read/convert/write RGB16->COLOR scanline */
732 >        uint32  y
733 > )
734   {
735 <        register int    x;
735 >        int     dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
736 >        double  d;
737 >        int     x;
738  
739 <        if (CHK(C_RFLT|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY))
740 <                quiterr("internal error 1 in L2Color");
739 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_TWRD|C_RFLT))
740 >                quiterr("internal error 1 in RRGGBB2Color");
741  
742 +        if (cvts.pconf != PLANARCONFIG_CONTIG)
743 +                quiterr("cannot handle separate 16-bit color planes");
744 +
745          if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
746                  quiterr("error reading TIFF input");
747          
748 <        for (x = cvts.xmax; x--; )
749 <                cvts.r.colors[x][RED] =
750 <                cvts.r.colors[x][GRN] =
751 <                cvts.r.colors[x][BLU] = cvts.t.fp[x] > 0. ? cvts.t.fp[x] : 0.;
748 >        for (x = cvts.xmax; x--; ) {
749 >                d = (cvts.t.wp[3*x] + 0.5)*(1./(1L<<16));
750 >                if (dogamma) d = pow(d, cvts.gamcor);
751 >                colval(cvts.r.colors[x],RED) = d;
752 >                d = (cvts.t.wp[3*x + 1] + 0.5)*(1./(1L<<16));
753 >                if (dogamma) d = pow(d, cvts.gamcor);
754 >                colval(cvts.r.colors[x],GRN) = d;
755 >                d = (cvts.t.wp[3*x + 2] + 0.5)*(1./(1L<<16));
756 >                if (dogamma) d = pow(d, cvts.gamcor);
757 >                colval(cvts.r.colors[x],BLU) = d;
758 >                if (CHK(C_CXFM))
759 >                        colortrans(cvts.r.colors[x], cvts.cmat,
760 >                                        cvts.r.colors[x]);
761 >        }
762 >        if (cvts.bradj) {
763 >                d = pow(2.,(double)cvts.bradj);
764 >                for (x = cvts.xmax; x--; )
765 >                        scalecolor(cvts.r.colors[x], d);
766 >        }
767  
768          if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
769                  quiterr("error writing Radiance picture");
770   }
771  
772  
773 < int
774 < RGB2Colr(y)                     /* read/convert/write RGB->COLR scanline */
775 < uint32  y;
773 > static void
774 > L2Color(                        /* read/convert/write Lfloat->COLOR scanline */
775 >        uint32  y
776 > )
777   {
778 +        float   m = pow(2., (double)cvts.bradj);
779 +        int     x;
780 +
781 +        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY))
782 +                quiterr("internal error 1 in L2Color");
783 +
784 +        if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
785 +                quiterr("error reading TIFF input");
786 +                                        /* also works for float greyscale */
787 +        for (x = cvts.xmax; x--; ) {
788 +                float   f = cvts.t.fp[x];
789 +                if (cvts.bradj) f *= m;
790 +                setcolor(cvts.r.colors[x], f, f, f);
791 +        }
792 +        if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
793 +                quiterr("error writing Radiance picture");
794 + }
795 +
796 +
797 + static void
798 + RGB2Colr(                       /* read/convert/write RGB->COLR scanline */
799 +        uint32  y
800 + )
801 + {
802          COLOR   ctmp;
803 <        register int    x;
803 >        int     x;
804  
805 <        if (CHK(C_RFLT|C_TFLT|C_GRY))
805 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY))
806                  quiterr("internal error 1 in RGB2Colr");
807  
808          if (cvts.pconf == PLANARCONFIG_CONTIG) {
# Line 570 | Line 817 | uint32 y;
817                  if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
818                          goto readerr;
819                  if (TIFFReadScanline(cvts.tif,
820 <                                (tidata_t)(cvts.t.bp + cvts.xmax), y, 1) < 0)
820 >                                (tdata_t)(cvts.t.bp + cvts.xmax), y, 1) < 0)
821                          goto readerr;
822                  if (TIFFReadScanline(cvts.tif,
823 <                                (tidata_t)(cvts.t.bp + 2*cvts.xmax), y, 2) < 0)
823 >                                (tdata_t)(cvts.t.bp + 2*cvts.xmax), y, 2) < 0)
824                          goto readerr;
825                  for (x = cvts.xmax; x--; ) {
826                          cvts.r.colrs[x][RED] = cvts.t.bp[x];
# Line 604 | Line 851 | readerr:
851   }
852  
853  
854 < int
855 < Gry2Colr(y)                     /* read/convert/write G8->COLR scanline */
856 < uint32  y;
854 > static void
855 > Gry2Colr(                       /* read/convert/write G8->COLR scanline */
856 >        uint32  y
857 > )
858   {
859 <        register int    x;
859 >        int     x;
860  
861 <        if (CHK(C_RFLT|C_TFLT) | !CHK(C_GRY))
861 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != C_GRY)
862                  quiterr("internal error 1 in Gry2Colr");
863  
864          if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
# Line 630 | Line 878 | uint32 y;
878   }
879  
880  
881 < int
882 < Color2L(y)                      /* read/convert/write COLOR->L16 scanline */
883 < uint32  y;
881 > static void
882 > GGry2Color(                     /* read/convert/write G16->COLOR scanline */
883 >        uint32  y
884 > )
885   {
886 <        double  m = pow(2.,(double)cvts.bradj);
887 <        register int    x;
886 >        int     dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
887 >        double  m;
888 >        double  d;
889 >        int     x;
890  
891 <        if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | !CHK(C_GRY))
891 >        if (CHK(C_TFLT|C_TWRD|C_GRY|C_RFLT) != (C_GRY|C_RFLT|C_TWRD))
892 >                quiterr("internal error 1 in GGry2Color");
893 >
894 >        if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
895 >                quiterr("error reading TIFF input");
896 >
897 >        if (cvts.bradj)
898 >                m = pow(2., (double)cvts.bradj);
899 >        for (x = cvts.xmax; x--; ) {
900 >                d = (cvts.t.wp[x] + 0.5)*(1./(1L<<16));
901 >                if (dogamma) d = pow(d, cvts.gamcor);
902 >                if (cvts.bradj) d *= m;
903 >                colval(cvts.r.colors[x],RED) =
904 >                colval(cvts.r.colors[x],GRN) =
905 >                colval(cvts.r.colors[x],BLU) = d;
906 >        }
907 >        if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
908 >                quiterr("error writing Radiance picture");
909 > }
910 >
911 >
912 > static void
913 > Color2GGry(                     /* read/convert/write COLOR->G16 scanline */
914 >        uint32  y
915 > )
916 > {
917 >        int     dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
918 >        float   m = pow(2.,(double)cvts.bradj);
919 >        int     x;
920 >
921 >        if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TWRD|C_GRY))
922 >                quiterr("internal error 1 in Color2GGry");
923 >
924 >        ReadRadScan(&cvts);
925 >
926 >        for (x = cvts.xmax; x--; ) {
927 >                float   f = m*( CHK(C_XYZE) ?
928 >                                                colval(cvts.r.colors[x],CIEY)
929 >                                                : bright(cvts.r.colors[x]) );
930 >                if (f <= 0)
931 >                        cvts.t.wp[x] = 0;
932 >                else if (f >= 1)
933 >                        cvts.t.wp[x] = 0xffff;
934 >                else if (dogamma)
935 >                        cvts.t.wp[x] = (int)((float)(1L<<16) *
936 >                                                pow(f, 1./cvts.gamcor));
937 >                else
938 >                        cvts.t.wp[x] = (int)((float)(1L<<16) * f);
939 >        }
940 >
941 >        if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
942 >                quiterr("error writing TIFF output");
943 > }
944 >
945 >
946 > static void
947 > Color2L(                        /* read/convert/write COLOR->Lfloat scanline */
948 >        uint32  y
949 > )
950 > {
951 >        float   m = pow(2.,(double)cvts.bradj);
952 >        int     x;
953 >
954 >        if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TFLT|C_GRY))
955                  quiterr("internal error 1 in Color2L");
956  
957 <        if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
644 <                quiterr("error reading Radiance picture");
957 >        ReadRadScan(&cvts);
958  
959          for (x = cvts.xmax; x--; )
960                  cvts.t.fp[x] = m*( CHK(C_XYZE) ? colval(cvts.r.colors[x],CIEY)
# Line 652 | Line 965 | uint32 y;
965   }
966  
967  
968 < int
969 < Color2Luv(y)                    /* read/convert/write COLOR->Luv scanline */
970 < uint32  y;
968 > static void
969 > Color2Luv(                      /* read/convert/write COLOR->Luv scanline */
970 >        uint32  y
971 > )
972   {
973 <        register int    x;
973 >        int     x;
974  
975 <        if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | CHK(C_GRY))
975 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT))
976                  quiterr("internal error 1 in Color2Luv");
977  
978 <        if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
665 <                quiterr("error reading Radiance picture");
978 >        ReadRadScan(&cvts);
979  
980          if (CHK(C_CXFM))
981                  for (x = cvts.xmax; x--; )
# Line 673 | Line 986 | uint32 y;
986                  for (x = cvts.xmax; x--; )
987                          scalecolor(cvts.r.colors[x], m);
988          }
989 +                                        /* also works for float RGB */
990 +        for (x = cvts.xmax; x--; ) {
991 +                cvts.t.fp[3*x] = colval(cvts.r.colors[x],CIEX);
992 +                cvts.t.fp[3*x+1] = colval(cvts.r.colors[x],CIEY);
993 +                cvts.t.fp[3*x+2] = colval(cvts.r.colors[x],CIEZ);
994 +        }
995  
996 +        if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
997 +                quiterr("error writing TIFF output");
998 + }
999 +
1000 +
1001 + static void
1002 + Color2RRGGBB(                   /* read/convert/write COLOR->RGB16 scanline */
1003 +        uint32  y
1004 + )
1005 + {
1006 +        int     dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
1007 +        float   m = pow(2.,(double)cvts.bradj);
1008 +        int     x, i;
1009 +
1010 +        if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TWRD))
1011 +                quiterr("internal error 1 in Color2RRGGBB");
1012 +
1013 +        ReadRadScan(&cvts);
1014 +
1015          for (x = cvts.xmax; x--; ) {
1016 <                cvts.t.fp[3*x] = colval(cvts.r.colors[x],RED);
1017 <                cvts.t.fp[3*x+1] = colval(cvts.r.colors[x],GRN);
1018 <                cvts.t.fp[3*x+2] = colval(cvts.r.colors[x],BLU);
1016 >            if (CHK(C_CXFM)) {
1017 >                        colortrans(cvts.r.colors[x], cvts.cmat,
1018 >                                        cvts.r.colors[x]);
1019 >                if (CHK(C_GAMUT))
1020 >                        clipgamut(cvts.r.colors[x], bright(cvts.r.colors[x]),
1021 >                                        CGAMUT_LOWER, cblack, cwhite);
1022 >            }
1023 >            for (i = 3; i--; ) {
1024 >                float   f = m*colval(cvts.r.colors[x],i);
1025 >                if (f <= 0)
1026 >                        cvts.t.wp[3*x + i] = 0;
1027 >                else if (f >= 1)
1028 >                        cvts.t.wp[3*x + i] = 0xffff;
1029 >                else if (dogamma)
1030 >                        cvts.t.wp[3*x + i] = (int)((float)(1L<<16) *
1031 >                                                pow(f, 1./cvts.gamcor));
1032 >                else
1033 >                        cvts.t.wp[3*x + i] = (int)((float)(1L<<16)*f);
1034 >            }
1035          }
1036  
1037          if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
# Line 685 | Line 1039 | uint32 y;
1039   }
1040  
1041  
1042 < int
1043 < Colr2Gry(y)                     /* read/convert/write COLR->RGB scanline */
1044 < uint32  y;
1042 > static void
1043 > Colr2Gry(                       /* read/convert/write COLR->RGB scanline */
1044 >        uint32  y
1045 > )
1046   {
1047 <        register int    x;
1047 >        int     x;
1048  
1049 <        if (CHK(C_RFLT|C_TFLT) | !CHK(C_GRY))
1049 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != C_GRY)
1050                  quiterr("internal error 1 in Colr2Gry");
1051  
1052 <        if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
1052 >        if (fread2colrs(cvts.r.colrs, cvts.xmax, cvts.rfp,
1053 >                                cvts.ncc, cvts.wpt) < 0)
1054                  quiterr("error reading Radiance picture");
1055  
1056          if (cvts.bradj)
# Line 711 | Line 1067 | uint32 y;
1067   }
1068  
1069  
1070 < int
1071 < Colr2RGB(y)                     /* read/convert/write COLR->RGB scanline */
1072 < uint32  y;
1070 > static void
1071 > Colr2RGB(                       /* read/convert/write COLR->RGB scanline */
1072 >        uint32  y
1073 > )
1074   {
1075          COLOR   ctmp;
1076 <        register int    x;
1076 >        int     x;
1077  
1078 <        if (CHK(C_RFLT|C_TFLT|C_GRY))
1078 >        if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY))
1079                  quiterr("internal error 1 in Colr2RGB");
1080  
1081 <        if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
1081 >        if (fread2colrs(cvts.r.colrs, cvts.xmax, cvts.rfp,
1082 >                                cvts.ncc, cvts.wpt) < 0)
1083                  quiterr("error reading Radiance picture");
1084  
1085          if (cvts.bradj)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines