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.35 by greg, Thu Aug 2 18:33:47 2018 UTC vs.
Revision 2.40 by greg, Sat Jun 7 05:09:46 2025 UTC

# Line 7 | Line 7 | static const char      RCSid[] = "$Id$";
7   *  Added white-balance adjustment 10/01 (GW).
8   */
9  
10 #include  <stdio.h>
10   #include  <math.h>
11   #include  <ctype.h>
12 < #include  <time.h>
14 < #include  <string.h>
15 <
12 > #include  "rtio.h"
13   #include  "platform.h"
14   #include  "tiffio.h"
15   #include  "color.h"
# Line 26 | Line 23 | static const char      RCSid[] = "$Id$";
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_TWRD          0x80            /* TIFF data is 16-bit */
29 < #define C_PRIM          0x100           /* 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   typedef void colcvf_t(uint32 y);
33  
34 < struct {
34 > struct CONVST {
35          uint16  flags;          /* conversion flags (defined above) */
36          char    capdate[20];    /* capture date/time */
37          char    owner[256];     /* content owner */
# Line 45 | Line 43 | struct {
43          uint16  orient;         /* visual orientation (TIFF spec.) */
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 53 | 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          colcvf_t *tf;   /* translation procedure */
65   }       cvts = {        /* conversion structure */
66          0, "", "", COMPRESSION_NONE, PHOTOMETRIC_RGB,
67 <        PLANARCONFIG_CONTIG, GAMCOR, 0, 1, 1., 1.,
67 >        PLANARCONFIG_CONTIG, GAMCOR, 0, 1, 1., 1., 3,
68   };
69  
70   #define CHK(f)          (cvts.flags & (f))
# Line 83 | Line 83 | 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  
88
89   #define RfGfBf2Color    Luv2Color
90   #define Gryf2Color      L2Color
91   #define Color2Gryf      Color2L
# Line 107 | Line 107 | short  ortab[8] = {            /* orientation conversion table */
107   extern char     TMSTR[];        /* "CAPDATE=" from header.c */
108   char            OWNSTR[] = "OWNER=";
109  
110 char  *progname;
110  
112
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 223 | Line 221 | allocbufs(void)                        /* allocate scanline buffers */
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);
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   }
# Line 237 | Line 235 | initfromtif(void)              /* initialize conversion from TIFF
235          char    *cp;
236          float   *fa, f1, f2;
237  
240        CLR(C_GRY|C_GAMMA|C_PRIM|C_RFLT|C_TFLT|C_TWRD|C_CXFM);
241
238          TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_PLANARCONFIG, &cvts.pconf);
239  
240          if (TIFFGetField(cvts.tif, TIFFTAG_PRIMARYCHROMATICITIES, &fa)) {
# Line 442 | Line 438 | headline(                      /* process Radiance input header line */
438   {
439          static int      tmstrlen = 0;
440          static int      ownstrlen = 0;
441 <        char    fmt[MAXFMTLEN];
441 >        struct CONVST   *cvp = (struct CONVST *)p;
442 >        char            fmt[MAXFMTLEN];
443  
444 <        if (!tmstrlen)
444 >        if (!tmstrlen) {
445                  tmstrlen = strlen(TMSTR);
449        if (!ownstrlen)
446                  ownstrlen = strlen(OWNSTR);
447 +        }
448          if (formatval(fmt, s)) {
449 <                if (!strcmp(fmt, COLRFMT))
450 <                        CLR(C_XYZE);
454 <                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");
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);
459 >                cvp->stonits /= exposval(s);
460                  return(1);
461          }
462          if (isaspect(s)) {
463 <                cvts.pixrat *= aspectval(s);
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(1);
470          }
471          if (isdate(s)) {
472                  if (s[tmstrlen] == ' ')
473 <                        strncpy(cvts.capdate, s+tmstrlen+1, 19);
473 >                        strncpy(cvp->capdate, s+tmstrlen+1, 19);
474                  else
475 <                        strncpy(cvts.capdate, s+tmstrlen, 19);
476 <                cvts.capdate[19] = '\0';
475 >                        strncpy(cvp->capdate, s+tmstrlen, 19);
476 >                cvp->capdate[19] = '\0';
477                  return(1);
478          }
479          if (!strncmp(s, OWNSTR, ownstrlen)) {
480 <                register char   *cp = s + ownstrlen;
480 >                char    *cp = s + ownstrlen;
481  
482                  while (isspace(*cp))
483                          ++cp;
484 <                strncpy(cvts.owner, cp, sizeof(cvts.owner));
485 <                cvts.owner[sizeof(cvts.owner)-1] = '\0';
486 <                for (cp = cvts.owner; *cp; 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 > cvts.owner && isspace(cp[-1]))
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  
# Line 500 | Line 504 | initfromrad(void)                      /* initialize input from a Radianc
504   {
505          int     i1, i2, po;
506                                                  /* read Radiance header */
507 <        CLR(C_RFLT|C_XYZE|C_PRIM|C_GAMMA|C_CXFM);
508 <        cvts.capdate[0] = '\0';
509 <        cvts.owner[0] = '\0';
506 <        cvts.stonits = 1.;
507 <        cvts.pixrat = 1.;
508 <        cvts.pconf = PLANARCONFIG_CONTIG;
509 <        getheader(cvts.rfp, headline, NULL);
510 <        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 529 | Line 528 | initfromrad(void)                      /* initialize input from a Radianc
528          case PHOTOMETRIC_LOGLUV:
529                  SET(C_RFLT|C_TFLT);
530                  CLR(C_GRY|C_TWRD);
531 <                if (!CHK(C_XYZE)) {
531 >                if (!CHK(C_XYZE|C_SPEC)) {
532                          comprgb2xyzWBmat(cvts.cmat,
533                                          CHK(C_PRIM) ? cvts.prims : stdprims);
534                          SET(C_CXFM);
# Line 554 | Line 553 | initfromrad(void)                      /* initialize input from a Radianc
553                  SET(C_GAMMA|C_GAMUT);
554                  CLR(C_GRY);
555                  setcolrgam(cvts.gamcor);
556 <                if (CHK(C_XYZE)) {
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);
# Line 652 | Line 651 | ra2tiff(               /* convert Radiance picture to TIFF image */
651  
652  
653   static void
654 + ReadRadScan(struct CONVST *cvp)
655 + {
656 +        static struct CONVST    *lastcvp = NULL;
657 +        static COLOR            scomp[MAXCSAMP];
658 +
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 <        register int    x;
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");
# Line 696 | Line 733 | RRGGBB2Color(                  /* read/convert/write RGB16->COLOR sca
733   )
734   {
735          int     dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
736 <        register double d;
737 <        register int    x;
736 >        double  d;
737 >        int     x;
738  
739          if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_TWRD|C_RFLT))
740                  quiterr("internal error 1 in RRGGBB2Color");
# Line 739 | Line 776 | L2Color(                       /* read/convert/write Lfloat->COLOR scanlin
776   )
777   {
778          float   m = pow(2., (double)cvts.bradj);
779 <        register int    x;
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");
# Line 748 | Line 785 | L2Color(                       /* read/convert/write Lfloat->COLOR scanlin
785                  quiterr("error reading TIFF input");
786                                          /* also works for float greyscale */
787          for (x = cvts.xmax; x--; ) {
788 <                register float  f = cvts.t.fp[x];
788 >                float   f = cvts.t.fp[x];
789                  if (cvts.bradj) f *= m;
790                  setcolor(cvts.r.colors[x], f, f, f);
791          }
# Line 763 | Line 800 | RGB2Colr(                      /* read/convert/write RGB->COLR scanline *
800   )
801   {
802          COLOR   ctmp;
803 <        register int    x;
803 >        int     x;
804  
805          if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY))
806                  quiterr("internal error 1 in RGB2Colr");
# Line 819 | Line 856 | 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_TWRD|C_TFLT|C_GRY) != C_GRY)
862                  quiterr("internal error 1 in Gry2Colr");
# Line 848 | Line 885 | GGry2Color(                    /* read/convert/write G16->COLOR scanlin
885   {
886          int     dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
887          double  m;
888 <        register double d;
889 <        register int    x;
888 >        double  d;
889 >        int     x;
890  
891          if (CHK(C_TFLT|C_TWRD|C_GRY|C_RFLT) != (C_GRY|C_RFLT|C_TWRD))
892                  quiterr("internal error 1 in GGry2Color");
# Line 879 | Line 916 | Color2GGry(                    /* read/convert/write COLOR->G16 scanlin
916   {
917          int     dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
918          float   m = pow(2.,(double)cvts.bradj);
919 <        register int    x;
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 <        if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
888 <                quiterr("error reading Radiance picture");
924 >        ReadRadScan(&cvts);
925  
926          for (x = cvts.xmax; x--; ) {
927 <                register float  f = m*( CHK(C_XYZE) ?
927 >                float   f = m*( CHK(C_XYZE) ?
928                                                  colval(cvts.r.colors[x],CIEY)
929                                                  : bright(cvts.r.colors[x]) );
930                  if (f <= 0)
# Line 913 | Line 949 | Color2L(                       /* read/convert/write COLOR->Lfloat scanlin
949   )
950   {
951          float   m = pow(2.,(double)cvts.bradj);
952 <        register int    x;
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)
922 <                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 935 | Line 970 | 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_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)
944 <                quiterr("error reading Radiance picture");
978 >        ReadRadScan(&cvts);
979  
980          if (CHK(C_CXFM))
981                  for (x = cvts.xmax; x--; )
# Line 971 | Line 1005 | Color2RRGGBB(                  /* read/convert/write COLOR->RGB16 sca
1005   {
1006          int     dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
1007          float   m = pow(2.,(double)cvts.bradj);
1008 <        register int    x, i;
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 <        if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
980 <                quiterr("error reading Radiance picture");
1013 >        ReadRadScan(&cvts);
1014  
1015          for (x = cvts.xmax; x--; ) {
1016              if (CHK(C_CXFM)) {
# Line 988 | Line 1021 | Color2RRGGBB(                  /* read/convert/write COLOR->RGB16 sca
1021                                          CGAMUT_LOWER, cblack, cwhite);
1022              }
1023              for (i = 3; i--; ) {
1024 <                register float  f = m*colval(cvts.r.colors[x],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)
# Line 1011 | Line 1044 | 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_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 1039 | Line 1073 | Colr2RGB(                      /* read/convert/write COLR->RGB scanline *
1073   )
1074   {
1075          COLOR   ctmp;
1076 <        register int    x;
1076 >        int     x;
1077  
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