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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines