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.37 by greg, Fri Oct 4 18:49:06 2024 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 +
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 + extern char     TMSTR[];        /* "CAPDATE=" from header.c */
109 + char            OWNSTR[] = "OWNER=";
110 +
111   char  *progname;
112  
113  
114 < main(argc, argv)
115 < int  argc;
90 < char  *argv[];
114 > int
115 > main(int  argc, char  *argv[])
116   {
117          int  reverse = 0;
118          int  i;
# Line 114 | Line 139 | char  *argv[];
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;
# Line 144 | Line 175 | doneopts:
175  
176                  if (i != argc-2)
177                          goto userr;
178 <
179 <                if (CHK(C_GRY))         /* consistency corrections */
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          }
# Line 159 | Line 193 | doneopts:
193          exit(0);
194   userr:
195          fprintf(stderr,
196 <        "Usage: %s [-z|-L|-l][-b][-e +/-stops][-g gamma] {in.pic|-} out.tif\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.pic|-]\n",
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 179 | Line 215 | char  *err;
215   }
216  
217  
218 < allocbufs()                     /* allocate scanline buffers */
218 > static void
219 > allocbufs(void)                 /* allocate scanline buffers */
220   {
221          int     rsiz, tsiz;
222  
223          rsiz = CHK(C_RFLT) ? sizeof(COLOR) : sizeof(COLR);
224 <        tsiz = (CHK(C_TFLT) ? sizeof(float) : sizeof(uint8)) *
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 = (char *)malloc(rsiz*cvts.xmax);
228 <        cvts.t.p = (char *)malloc(tsiz*cvts.xmax);
229 <        if (cvts.r.p == NULL | cvts.t.p == NULL)
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 < initfromtif()           /* initialize conversion from TIFF input */
234 > static void
235 > initfromtif(void)               /* initialize conversion from TIFF input */
236   {
237          uint16  hi;
238 +        char    *cp;
239          float   *fa, f1, f2;
240  
201        CLR(C_GRY|C_GAMMA|C_PRIM|C_RFLT|C_TFLT|C_CXFM);
202
241          TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_PLANARCONFIG, &cvts.pconf);
242  
243          if (TIFFGetField(cvts.tif, TIFFTAG_PRIMARYCHROMATICITIES, &fa)) {
# Line 237 | Line 275 | initfromtif()          /* initialize conversion from TIFF inpu
275                          cpcolormat(cvts.cmat, xyz2rgbmat);
276                          SET(C_CXFM|C_GAMUT);
277                  } else if (cvts.comp == COMPRESSION_SGILOG)
278 <                        SET(C_GAMUT);
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_FLTXYZ);
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_FLTY);
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|C_GAMUT);
302 >                SET(C_GAMMA);
303                  setcolrgam(cvts.gamcor);
304                  if (CHK(C_XYZE)) {
305 <                        comprgb2xyzmat(cvts.cmat,
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 != 8)
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 <                cvts.tf = RGB2Colr;
328 >                }
329                  break;
330          case PHOTOMETRIC_MINISBLACK:
331 <                SET(C_GRY|C_GAMMA|C_GAMUT);
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 != 8)
339 <                        quiterr("unsupported bits per sample for greyscale");
340 <                cvts.tf = Gry2Colr;
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");
# Line 288 | Line 362 | initfromtif()          /* initialize conversion from TIFF inpu
362                  quiterr("unknown input image resolution");
363  
364          if (!TIFFGetField(cvts.tif, TIFFTAG_STONITS, &cvts.stonits))
365 <                cvts.stonits = 1.;
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 <                fputexpos(pow(2.,(double)cvts.bradj)/cvts.stonits, cvts.rfp);
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 <                fputexpos(WHTEFFICACY*pow(2.,(double)cvts.bradj)/cvts.stonits,
390 <                                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 < tiff2ra(ac, av)         /* convert TIFF image to Radiance picture */
404 < int  ac;
405 < char  *av[];
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 */
# Line 320 | Line 415 | char  *av[];
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);
# Line 337 | Line 433 | char  *av[];
433   }
434  
435  
436 < int
437 < headline(s)                     /* process Radiance input header line */
438 < char    *s;
436 > static int
437 > headline(                       /* process Radiance input header line */
438 >        char    *s,
439 >        void    *p
440 > )
441   {
442 <        char    fmt[32];
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 <                if (!strcmp(fmt, COLRFMT))
453 <                        CLR(C_XYZE);
349 <                else if (!strcmp(fmt, CIEFMT))
452 >                CLR(C_XYZE|C_SPEC);
453 >                if (!strcmp(fmt, CIEFMT))
454                          SET(C_XYZE);
455 <                else
456 <                        quiterr("unrecognized input picture format");
457 <                return;
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 <                cvts.stonits /= exposval(s);
463 <                return;
462 >                cvp->stonits /= exposval(s);
463 >                return(1);
464          }
465          if (isaspect(s)) {
466 <                cvts.pixrat *= aspectval(s);
467 <                return;
466 >                cvp->pixrat *= aspectval(s);
467 >                return(1);
468          }
469          if (isprims(s)) {
470 <                primsval(cvts.prims, s);
470 >                primsval(cvp->prims, s);
471                  SET(C_PRIM);
472 <                return;
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 < initfromrad()                   /* initialize input from a Radiance picture */
505 > static void
506 > initfromrad(void)                       /* initialize input from a Radiance picture */
507   {
508          int     i1, i2, po;
509                                                  /* read Radiance header */
510 <        CLR(C_RFLT|C_TFLT|C_XYZE|C_PRIM|C_GAMMA|C_CXFM);
511 <        cvts.stonits = 1.;
512 <        cvts.pixrat = 1.;
378 <        cvts.pconf = PLANARCONFIG_CONTIG;
379 <        getheader(cvts.rfp, headline, NULL);
380 <        if ((po = fgetresolu(&i1, &i2, cvts.rfp)) < 0)
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 */
# Line 398 | Line 530 | initfromrad()                  /* initialize input from a Radiance pi
530          switch (cvts.phot) {
531          case PHOTOMETRIC_LOGLUV:
532                  SET(C_RFLT|C_TFLT);
533 <                CLR(C_GRY);
534 <                if (!CHK(C_XYZE)) {
535 <                        cpcolormat(cvts.cmat, rgb2xyzmat);
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_FLTXYZ);
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_FLTY);
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)) {
560 <                        compxyz2rgbmat(cvts.cmat,
559 >                if (CHK(C_XYZE|C_SPEC)) {
560 >                        compxyz2rgbWBmat(cvts.cmat,
561                                          CHK(C_PRIM) ? cvts.prims : stdprims);
562                          SET(C_CXFM);
563                  }
# Line 433 | Line 567 | initfromrad()                  /* initialize input from a Radiance pi
567                          TIFFSetField(cvts.tif, TIFFTAG_WHITEPOINT,
568                                          (float *)cvts.prims[WHT]);
569                  }
570 <                cvts.tf = Colr2RGB;
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 <                cvts.tf = Colr2Gry;
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");
# Line 448 | Line 601 | initfromrad()                  /* initialize input from a Radiance pi
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, CHK(C_TFLT) ? 32 : 8);
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);
# Line 456 | Line 610 | initfromrad()                  /* initialize input from a Radiance pi
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
# Line 468 | Line 626 | initfromrad()                  /* initialize input from a Radiance pi
626   }
627  
628  
629 < ra2tiff(ac, av)         /* convert Radiance picture to TIFF image */
630 < int  ac;
631 < char  *av[];
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 */
# Line 478 | Line 638 | char  *av[];
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 ((cvts.tif = TIFFOpen(av[ac+1], "w")) == NULL)
644                  quiterr("cannot open TIFF output");
# Line 492 | Line 653 | char  *av[];
653   }
654  
655  
656 < int
657 < Luv2Color(y)                    /* read/convert/write Luv->COLOR scanline */
497 < uint32  y;
656 > static void
657 > ReadRadScan(struct CONVST *cvp)
658   {
659 <        register int    x;
659 >        static struct CONVST    *lastcvp = NULL;
660 >        static COLOR            scomp[MAXCSAMP];
661  
662 <        if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | CHK(C_GRY))
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 >                return;
686 >        }
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 <        
709 >                                        /* also works for float RGB */
710          for (x = cvts.xmax; x--; ) {
711 <                cvts.r.colors[x][RED] = cvts.t.fp[3*x];
712 <                cvts.r.colors[x][GRN] = cvts.t.fp[3*x + 1];
713 <                cvts.r.colors[x][BLU] = cvts.t.fp[3*x + 2];
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]);
# Line 526 | Line 730 | uint32 y;
730   }
731  
732  
733 < int
734 < L2Color(y)                      /* read/convert/write L16->COLOR scanline */
735 < uint32  y;
733 > static void
734 > RRGGBB2Color(                   /* read/convert/write RGB16->COLOR scanline */
735 >        uint32  y
736 > )
737   {
738 <        register int    x;
738 >        int     dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
739 >        double  d;
740 >        int     x;
741  
742 <        if (CHK(C_RFLT|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY))
743 <                quiterr("internal error 1 in L2Color");
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 <                cvts.r.colors[x][RED] =
753 <                cvts.r.colors[x][GRN] =
754 <                cvts.r.colors[x][BLU] = cvts.t.fp[x] > 0. ? cvts.t.fp[x] : 0.;
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 < int
777 < RGB2Colr(y)                     /* read/convert/write RGB->COLR scanline */
778 < uint32  y;
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 <        register int    x;
806 >        int     x;
807  
808 <        if (CHK(C_RFLT|C_TFLT|C_GRY))
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) {
# Line 570 | Line 820 | uint32 y;
820                  if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
821                          goto readerr;
822                  if (TIFFReadScanline(cvts.tif,
823 <                                (tidata_t)(cvts.t.bp + cvts.xmax), y, 1) < 0)
823 >                                (tdata_t)(cvts.t.bp + cvts.xmax), y, 1) < 0)
824                          goto readerr;
825                  if (TIFFReadScanline(cvts.tif,
826 <                                (tidata_t)(cvts.t.bp + 2*cvts.xmax), y, 2) < 0)
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];
# Line 604 | Line 854 | readerr:
854   }
855  
856  
857 < int
858 < Gry2Colr(y)                     /* read/convert/write G8->COLR scanline */
859 < uint32  y;
857 > static void
858 > Gry2Colr(                       /* read/convert/write G8->COLR scanline */
859 >        uint32  y
860 > )
861   {
862 <        register int    x;
862 >        int     x;
863  
864 <        if (CHK(C_RFLT|C_TFLT) | !CHK(C_GRY))
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)
# Line 630 | Line 881 | uint32 y;
881   }
882  
883  
884 < int
885 < Color2L(y)                      /* read/convert/write COLOR->L16 scanline */
886 < uint32  y;
884 > static void
885 > GGry2Color(                     /* read/convert/write G16->COLOR scanline */
886 >        uint32  y
887 > )
888   {
889 <        double  m = pow(2.,(double)cvts.bradj);
890 <        register int    x;
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_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | !CHK(C_GRY))
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 <        if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
644 <                quiterr("error reading Radiance picture");
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)
# Line 652 | Line 968 | uint32 y;
968   }
969  
970  
971 < int
972 < Color2Luv(y)                    /* read/convert/write COLOR->Luv scanline */
973 < uint32  y;
971 > static void
972 > Color2Luv(                      /* read/convert/write COLOR->Luv scanline */
973 >        uint32  y
974 > )
975   {
976 <        register int    x;
976 >        int     x;
977  
978 <        if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | CHK(C_GRY))
978 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT))
979                  quiterr("internal error 1 in Color2Luv");
980  
981 <        if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
665 <                quiterr("error reading Radiance picture");
981 >        ReadRadScan(&cvts);
982  
983          if (CHK(C_CXFM))
984                  for (x = cvts.xmax; x--; )
# Line 673 | Line 989 | uint32 y;
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 <                cvts.t.fp[3*x] = colval(cvts.r.colors[x],RED);
1020 <                cvts.t.fp[3*x+1] = colval(cvts.r.colors[x],GRN);
1021 <                cvts.t.fp[3*x+2] = colval(cvts.r.colors[x],BLU);
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)
# Line 685 | Line 1042 | uint32 y;
1042   }
1043  
1044  
1045 < int
1046 < Colr2Gry(y)                     /* read/convert/write COLR->RGB scanline */
1047 < uint32  y;
1045 > static void
1046 > Colr2Gry(                       /* read/convert/write COLR->RGB scanline */
1047 >        uint32  y
1048 > )
1049   {
1050 <        register int    x;
1050 >        int     x;
1051  
1052 <        if (CHK(C_RFLT|C_TFLT) | !CHK(C_GRY))
1052 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != C_GRY)
1053                  quiterr("internal error 1 in Colr2Gry");
1054  
1055 <        if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
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)
# Line 711 | Line 1070 | uint32 y;
1070   }
1071  
1072  
1073 < int
1074 < Colr2RGB(y)                     /* read/convert/write COLR->RGB scanline */
1075 < uint32  y;
1073 > static void
1074 > Colr2RGB(                       /* read/convert/write COLR->RGB scanline */
1075 >        uint32  y
1076 > )
1077   {
1078          COLOR   ctmp;
1079 <        register int    x;
1079 >        int     x;
1080  
1081 <        if (CHK(C_RFLT|C_TFLT|C_GRY))
1081 >        if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY))
1082                  quiterr("internal error 1 in Colr2RGB");
1083  
1084 <        if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
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)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines