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.9 by gregl, Thu Jul 24 11:47:05 1997 UTC vs.
Revision 2.21 by greg, Tue Jul 1 19:04:08 2003 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  
10   #include  <stdio.h>
11   #include  <math.h>
12 + #include  <time.h>
13   #include  "tiffio.h"
14   #include  "color.h"
15   #include  "resolu.h"
16  
17   #define  GAMCOR         2.2             /* default gamma */
18  
20 #ifndef malloc
21 extern char  *malloc();
22 #endif
19                                  /* conversion flags */
20   #define C_CXFM          0x1             /* needs color transformation */
21   #define C_GAMUT         0x2             /* needs gamut mapping */
# Line 28 | Line 24 | extern char  *malloc();
24   #define C_XYZE          0x10            /* Radiance is XYZE */
25   #define C_RFLT          0x20            /* Radiance data is float */
26   #define C_TFLT          0x40            /* TIFF data is float */
27 < #define C_PRIM          0x80            /* has assigned primaries */
27 > #define C_TWRD          0x80            /* TIFF data is 16-bit */
28 > #define C_PRIM          0x100           /* has assigned primaries */
29  
30   struct {
31          uint16  flags;          /* conversion flags (defined above) */
32          uint16  comp;           /* TIFF compression type */
33 +        uint16  phot;           /* TIFF photometric type */
34          uint16  pconf;          /* TIFF planar configuration */
35          float   gamcor;         /* gamma correction value */
36          short   bradj;          /* Radiance exposure adjustment (stops) */
37          uint16  orient;         /* visual orientation (TIFF spec.) */
38 <        float   stonits;        /* input conversion to nits */
38 >        double  stonits;        /* input conversion to nits */
39          float   pixrat;         /* pixel aspect ratio */
40          FILE    *rfp;           /* Radiance stream pointer */
41          TIFF    *tif;           /* TIFF pointer */
# Line 51 | Line 49 | struct {
49          } r;                    /* Radiance scanline */
50          union {
51                  uint8   *bp;            /* byte pointer */
52 +                uint16  *wp;            /* word pointer */
53                  float   *fp;            /* float pointer */
54                  char    *p;             /* generic pointer */
55          } t;                    /* TIFF scanline */
56 <        int     (*tf)();        /* translation procedure */
56 >        void    (*tf)();        /* translation procedure */
57   }       cvts = {        /* conversion structure */
58 <        0, COMPRESSION_NONE, PLANARCONFIG_CONTIG, GAMCOR, 0,
58 >        0, COMPRESSION_NONE, PHOTOMETRIC_RGB,
59 >        PLANARCONFIG_CONTIG, GAMCOR, 0, 1, 1., 1.,
60   };
61  
62   #define CHK(f)          (cvts.flags & (f))
# Line 64 | Line 64 | struct {
64   #define CLR(f)          (cvts.flags &= ~(f))
65   #define TGL(f)          (cvts.flags ^= (f))
66  
67 < int     Luv2Color(), L2Color(), RGB2Colr(), Gry2Colr();
68 < int     Color2Luv(), Color2L(), Colr2RGB(), Colr2Gry();
67 > void    Luv2Color(), L2Color(), RGB2Colr(), Gry2Colr();
68 > void    Color2Luv(), Color2L(), Colr2RGB(), Colr2Gry();
69 > void    RRGGBB2Color(), GGry2Color(), Color2RRGGBB(), Color2GGry();
70  
71 + #define RfGfBf2Color    Luv2Color
72 + #define Gryf2Color      L2Color
73 + #define Color2Gryf      Color2L
74 + #define Color2RfGfBf    Color2Luv
75 +
76   short   ortab[8] = {            /* orientation conversion table */
77          YMAJOR|YDECR,
78          YMAJOR|YDECR|XDECR,
# Line 105 | Line 111 | char  *argv[];
111                                  cvts.comp = COMPRESSION_LZW;
112                                  break;
113                          case 'L':               /* LogLuv 32-bit output */
114 <                                cvts.comp = COMPRESSION_LOGLUV32;
114 >                                cvts.comp = COMPRESSION_SGILOG;
115 >                                cvts.phot = PHOTOMETRIC_LOGLUV;
116                                  break;
117                          case 'l':               /* LogLuv 24-bit output */
118 <                                cvts.comp = COMPRESSION_LOGLUV24;
118 >                                cvts.comp = COMPRESSION_SGILOG24;
119 >                                cvts.phot = PHOTOMETRIC_LOGLUV;
120                                  break;
121 +                        case 'w':               /* 16-bit/primary output? */
122 +                                TGL(C_TWRD);
123 +                                break;
124 +                        case 'f':               /* IEEE float output? */
125 +                                TGL(C_TFLT);
126 +                                break;
127                          case 'b':               /* greyscale output? */
128                                  TGL(C_GRY);
129                                  break;
# Line 140 | Line 154 | doneopts:
154  
155                  if (i != argc-2)
156                          goto userr;
157 +                                                /* consistency checks */
158 +                if (CHK(C_GRY))
159 +                        if (cvts.phot == PHOTOMETRIC_RGB)
160 +                                cvts.phot = PHOTOMETRIC_MINISBLACK;
161 +                        else {
162 +                                cvts.phot = PHOTOMETRIC_LOGL;
163 +                                cvts.comp = COMPRESSION_SGILOG;
164 +                        }
165 +                if (CHK(C_TWRD|C_TFLT) == (C_TWRD|C_TFLT))
166 +                        goto userr;
167  
144                if (CHK(C_GRY) && (cvts.comp == COMPRESSION_LOGLUV24 ||
145                                        cvts.comp == COMPRESSION_LOGLUV32))
146                        cvts.comp = COMPRESSION_LOGL16;
147
168                  ra2tiff(i, argv);
169          }
170  
171          exit(0);
172   userr:
173          fprintf(stderr,
174 <        "Usage: %s [-z|-L|-l][-b][-e +/-stops][-g gamma] {in.pic|-} out.tif\n",
174 >        "Usage: %s [-z|-L|-l|-f|-w][-b][-e +/-stops][-g gamma] {in.pic|-} out.tif\n",
175                          progname);
176          fprintf(stderr,
177          "   Or: %s -r [-x][-e +/-stops][-g gamma] in.tif [out.pic|-]\n",
# Line 176 | Line 196 | allocbufs()                    /* allocate scanline buffers */
196          int     rsiz, tsiz;
197  
198          rsiz = CHK(C_RFLT) ? sizeof(COLOR) : sizeof(COLR);
199 <        tsiz = (CHK(C_TFLT) ? sizeof(float) : sizeof(uint8)) *
199 >        tsiz = (CHK(C_TFLT) ? sizeof(float) :
200 >                        CHK(C_TWRD) ? sizeof(uint16) : sizeof(uint8)) *
201                          (CHK(C_GRY) ? 1 : 3);
202          cvts.r.p = (char *)malloc(rsiz*cvts.xmax);
203          cvts.t.p = (char *)malloc(tsiz*cvts.xmax);
# Line 190 | Line 211 | initfromtif()          /* initialize conversion from TIFF inpu
211          uint16  hi;
212          float   *fa, f1, f2;
213  
214 <        CLR(C_GRY|C_GAMMA|C_PRIM|C_RFLT|C_TFLT|C_CXFM);
214 >        CLR(C_GRY|C_GAMMA|C_PRIM|C_RFLT|C_TFLT|C_TWRD|C_CXFM);
215  
216          TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_PLANARCONFIG, &cvts.pconf);
217  
# Line 216 | Line 237 | initfromtif()          /* initialize conversion from TIFF inpu
237          if (TIFFGetField(cvts.tif, TIFFTAG_XRESOLUTION, &f1) &&
238                          TIFFGetField(cvts.tif, TIFFTAG_YRESOLUTION, &f2))
239                  cvts.pixrat = f1/f2;
219        else
220                cvts.pixrat = 1.;
240  
241 <        if (!TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_ORIENTATION, &cvts.orient))
223 <                cvts.orient = 1;
241 >        TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_ORIENTATION, &cvts.orient);
242  
243 <        switch (cvts.comp) {
244 <        case COMPRESSION_LOGL16:
245 <                SET(C_GRY|C_RFLT|C_TFLT|C_GAMUT);
246 <                cvts.pconf = PLANARCONFIG_CONTIG;
247 <                TIFFSetField(cvts.tif, TIFFTAG_LOGLUVDATAFMT,
230 <                                LOGLUVDATAFMT_FLTY);
231 <                TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT,
232 <                                SAMPLEFORMAT_IEEEFP);
233 <                cvts.tf = L2Color;
234 <                break;
235 <        case COMPRESSION_LOGLUV24:
236 <        case COMPRESSION_LOGLUV32:
243 >        if (!TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_PHOTOMETRIC, &cvts.phot))
244 >                quiterr("TIFF has unspecified photometric type");
245 >
246 >        switch (cvts.phot) {
247 >        case PHOTOMETRIC_LOGLUV:
248                  SET(C_RFLT|C_TFLT);
249                  if (!CHK(C_XYZE)) {
250                          cpcolormat(cvts.cmat, xyz2rgbmat);
251                          SET(C_CXFM|C_GAMUT);
252 <                } else if (cvts.comp == COMPRESSION_LOGLUV32)
252 >                } else if (cvts.comp == COMPRESSION_SGILOG)
253                          SET(C_GAMUT);
254                  if (cvts.pconf != PLANARCONFIG_CONTIG)
255                          quiterr("cannot handle separate Luv planes");
256 <                TIFFSetField(cvts.tif, TIFFTAG_LOGLUVDATAFMT,
257 <                                LOGLUVDATAFMT_FLTXYZ);
247 <                TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT,
248 <                                SAMPLEFORMAT_IEEEFP);
256 >                TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
257 >                                SGILOGDATAFMT_FLOAT);
258                  cvts.tf = Luv2Color;
259                  break;
260 <        default:
261 <                SET(C_GAMMA|C_GAMUT);
260 >        case PHOTOMETRIC_LOGL:
261 >                SET(C_GRY|C_RFLT|C_TFLT|C_GAMUT);
262 >                cvts.pconf = PLANARCONFIG_CONTIG;
263 >                TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
264 >                                SGILOGDATAFMT_FLOAT);
265 >                cvts.tf = L2Color;
266 >                break;
267 >        case PHOTOMETRIC_YCBCR:
268 >                if (cvts.comp == COMPRESSION_JPEG &&
269 >                                cvts.pconf == PLANARCONFIG_CONTIG) {
270 >                        TIFFSetField(cvts.tif, TIFFTAG_JPEGCOLORMODE,
271 >                                        JPEGCOLORMODE_RGB);
272 >                        cvts.phot = PHOTOMETRIC_RGB;
273 >                } else
274 >                        quiterr("unsupported photometric type");
275 >                /* fall through */
276 >        case PHOTOMETRIC_RGB:
277 >                SET(C_GAMMA);
278                  setcolrgam(cvts.gamcor);
279                  if (CHK(C_XYZE)) {
280 <                        comprgb2xyzmat(cvts.cmat,
280 >                        comprgb2xyzWBmat(cvts.cmat,
281                                          CHK(C_PRIM) ? cvts.prims : stdprims);
282                          SET(C_CXFM);
283                  }
284                  if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) ||
285 <                                (hi != 1 && hi != 3))
286 <                        quiterr("unsupported samples per pixel");
287 <                if (hi == 1) {
288 <                        SET(C_GRY);
289 <                        cvts.pconf = PLANARCONFIG_CONTIG;
290 <                        cvts.tf = Gry2Colr;
266 <                } else
285 >                                hi != 3)
286 >                        quiterr("unsupported samples per pixel for RGB");
287 >                if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi))
288 >                        hi = -1;
289 >                switch (hi) {
290 >                case 8:
291                          cvts.tf = RGB2Colr;
292 <                if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi) ||
293 <                                hi != 8)
294 <                        quiterr("unsupported bits per sample");
292 >                        break;
293 >                case 16:
294 >                        cvts.tf = RRGGBB2Color;
295 >                        SET(C_RFLT|C_TWRD);
296 >                        break;
297 >                case 32:
298 >                        cvts.tf = RfGfBf2Color;
299 >                        SET(C_RFLT|C_TFLT);
300 >                        break;
301 >                default:
302 >                        quiterr("unsupported bits per sample for RGB");
303 >                }
304                  break;
305 +        case PHOTOMETRIC_MINISBLACK:
306 +                SET(C_GRY|C_GAMMA);
307 +                setcolrgam(cvts.gamcor);
308 +                cvts.pconf = PLANARCONFIG_CONTIG;
309 +                if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) ||
310 +                                hi != 1)
311 +                        quiterr("unsupported samples per pixel for greyscale");
312 +                if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi))
313 +                        hi = -1;
314 +                switch (hi) {
315 +                case 8:
316 +                        cvts.tf = Gry2Colr;
317 +                        break;
318 +                case 16:
319 +                        cvts.tf = GGry2Color;
320 +                        SET(C_RFLT|C_TWRD);
321 +                        break;
322 +                case 32:
323 +                        cvts.tf = Gryf2Color;
324 +                        SET(C_RFLT|C_TFLT);
325 +                        break;
326 +                default:
327 +                        quiterr("unsupported bits per sample for Gray");
328 +                }
329 +                break;
330 +        default:
331 +                quiterr("unsupported photometric type");
332 +                break;
333          }
334  
274        if (TIFFGetField(cvts.tif, TIFFTAG_PHOTOMETRIC, &hi) &&
275                hi != (CHK(C_GRY) ? PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_RGB))
276                quiterr("unsupported photometric interpretation");
277
335          if (!TIFFGetField(cvts.tif, TIFFTAG_IMAGEWIDTH, &cvts.xmax) ||
336                  !TIFFGetField(cvts.tif, TIFFTAG_IMAGELENGTH, &cvts.ymax))
337                  quiterr("unknown input image resolution");
# Line 342 | Line 399 | char   *s;
399                          SET(C_XYZE);
400                  else
401                          quiterr("unrecognized input picture format");
402 <                return;
402 >                return(1);
403          }
404          if (isexpos(s)) {
405                  cvts.stonits /= exposval(s);
406 <                return;
406 >                return(1);
407          }
408          if (isaspect(s)) {
409                  cvts.pixrat *= aspectval(s);
410 <                return;
410 >                return(1);
411          }
412          if (isprims(s)) {
413                  primsval(cvts.prims, s);
414                  SET(C_PRIM);
415 <                return;
415 >                return(1);
416          }
417 +        return(0);
418   }
419  
420  
# Line 364 | Line 422 | initfromrad()                  /* initialize input from a Radiance pi
422   {
423          int     i1, i2, po;
424                                                  /* read Radiance header */
425 <        CLR(C_RFLT|C_TFLT|C_XYZE|C_PRIM|C_GAMMA|C_CXFM);
425 >        CLR(C_RFLT|C_XYZE|C_PRIM|C_GAMMA|C_CXFM);
426          cvts.stonits = 1.;
427          cvts.pixrat = 1.;
428          cvts.pconf = PLANARCONFIG_CONTIG;
# Line 385 | Line 443 | initfromrad()                  /* initialize input from a Radiance pi
443                  cvts.stonits *= WHTEFFICACY;
444                                                  /* set up conversion */
445          TIFFSetField(cvts.tif, TIFFTAG_COMPRESSION, cvts.comp);
446 +        TIFFSetField(cvts.tif, TIFFTAG_PHOTOMETRIC, cvts.phot);
447  
448 <        switch (cvts.comp) {
449 <        case COMPRESSION_LOGL16:
391 <                SET(C_GRY|C_RFLT|C_TFLT);
392 <                TIFFSetField(cvts.tif, TIFFTAG_LOGLUVDATAFMT,
393 <                                LOGLUVDATAFMT_FLTY);
394 <                TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT,
395 <                                SAMPLEFORMAT_IEEEFP);
396 <                cvts.tf = Color2L;
397 <                break;
398 <        case COMPRESSION_LOGLUV24:
399 <        case COMPRESSION_LOGLUV32:
448 >        switch (cvts.phot) {
449 >        case PHOTOMETRIC_LOGLUV:
450                  SET(C_RFLT|C_TFLT);
451 <                CLR(C_GRY);
451 >                CLR(C_GRY|C_TWRD);
452                  if (!CHK(C_XYZE)) {
453 <                        cpcolormat(cvts.cmat, rgb2xyzmat);
453 >                        comprgb2xyzWBmat(cvts.cmat,
454 >                                        CHK(C_PRIM) ? cvts.prims : stdprims);
455                          SET(C_CXFM);
456                  }
457 <                TIFFSetField(cvts.tif, TIFFTAG_LOGLUVDATAFMT,
458 <                                LOGLUVDATAFMT_FLTXYZ);
459 <                TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT,
460 <                                SAMPLEFORMAT_IEEEFP);
457 >                if (cvts.comp != COMPRESSION_SGILOG &&
458 >                                cvts.comp != COMPRESSION_SGILOG24)
459 >                        quiterr("internal error 2 in initfromrad");
460 >                TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
461 >                                SGILOGDATAFMT_FLOAT);
462                  cvts.tf = Color2Luv;
463                  break;
464 <        default:
464 >        case PHOTOMETRIC_LOGL:
465 >                SET(C_GRY|C_RFLT|C_TFLT);
466 >                CLR(C_TWRD);
467 >                if (cvts.comp != COMPRESSION_SGILOG)    
468 >                        quiterr("internal error 3 in initfromrad");
469 >                TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
470 >                                SGILOGDATAFMT_FLOAT);
471 >                cvts.tf = Color2L;
472 >                break;
473 >        case PHOTOMETRIC_RGB:
474                  SET(C_GAMMA|C_GAMUT);
475 +                CLR(C_GRY);
476                  setcolrgam(cvts.gamcor);
477                  if (CHK(C_XYZE)) {
478 <                        compxyz2rgbmat(cvts.cmat,
478 >                        compxyz2rgbWBmat(cvts.cmat,
479                                          CHK(C_PRIM) ? cvts.prims : stdprims);
480                          SET(C_CXFM);
481                  }
420                if (CHK(C_GRY))
421                        cvts.tf = Colr2Gry;
422                else
423                        cvts.tf = Colr2RGB;
482                  if (CHK(C_PRIM)) {
483                          TIFFSetField(cvts.tif, TIFFTAG_PRIMARYCHROMATICITIES,
484                                          (float *)cvts.prims);
485                          TIFFSetField(cvts.tif, TIFFTAG_WHITEPOINT,
486                                          (float *)cvts.prims[WHT]);
487                  }
488 +                if (CHK(C_TWRD)) {
489 +                        cvts.tf = Color2RRGGBB;
490 +                        SET(C_RFLT);
491 +                } else if (CHK(C_TFLT)) {
492 +                        cvts.tf = Color2RfGfBf;
493 +                        SET(C_RFLT);
494 +                } else
495 +                        cvts.tf = Colr2RGB;
496                  break;
497 +        case PHOTOMETRIC_MINISBLACK:
498 +                SET(C_GRY|C_GAMMA|C_GAMUT);
499 +                setcolrgam(cvts.gamcor);
500 +                if (CHK(C_TWRD)) {
501 +                        cvts.tf = Color2GGry;
502 +                        SET(C_RFLT);
503 +                } else if (CHK(C_TFLT)) {
504 +                        cvts.tf = Color2Gryf;
505 +                        SET(C_RFLT);
506 +                } else
507 +                        cvts.tf = Colr2Gry;
508 +                break;
509 +        default:
510 +                quiterr("internal error 4 in initfromrad");
511 +                break;
512          }
513                                                  /* set other TIFF fields */
514          TIFFSetField(cvts.tif, TIFFTAG_IMAGEWIDTH, cvts.xmax);
515          TIFFSetField(cvts.tif, TIFFTAG_IMAGELENGTH, cvts.ymax);
516          TIFFSetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, CHK(C_GRY) ? 1 : 3);
517 <        TIFFSetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, CHK(C_TFLT) ? 32 : 8);
518 <        TIFFSetField(cvts.tif, TIFFTAG_PHOTOMETRIC,
438 <                        CHK(C_GRY) ? PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_RGB);
517 >        TIFFSetField(cvts.tif, TIFFTAG_BITSPERSAMPLE,
518 >                        CHK(C_TFLT) ? 32 : CHK(C_TWRD) ? 16 : 8);
519          TIFFSetField(cvts.tif, TIFFTAG_XRESOLUTION, 72.);
520          TIFFSetField(cvts.tif, TIFFTAG_YRESOLUTION, 72./cvts.pixrat);
521          TIFFSetField(cvts.tif, TIFFTAG_ORIENTATION, cvts.orient);
# Line 443 | Line 523 | initfromrad()                  /* initialize input from a Radiance pi
523          TIFFSetField(cvts.tif, TIFFTAG_PLANARCONFIG, cvts.pconf);
524          TIFFSetField(cvts.tif, TIFFTAG_STONITS,
525                          cvts.stonits/pow(2.,(double)cvts.bradj));
526 <        i1 = TIFFScanlineSize(cvts.tif);
526 >        if (cvts.comp == COMPRESSION_NONE)
527 >                i1 = TIFFScanlineSize(cvts.tif);
528 >        else
529 >                i1 = 3*cvts.xmax;       /* conservative guess */
530          i2 = 8192/i1;                           /* compute good strip size */
531          if (i2 < 1) i2 = 1;
532          TIFFSetField(cvts.tif, TIFFTAG_ROWSPERSTRIP, (uint32)i2);
# Line 476 | Line 559 | char  *av[];
559   }
560  
561  
562 < int
562 > void
563   Luv2Color(y)                    /* read/convert/write Luv->COLOR scanline */
564   uint32  y;
565   {
566          register int    x;
567  
568 <        if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | CHK(C_GRY))
568 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT))
569                  quiterr("internal error 1 in Luv2Color");
570  
571          if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
572                  quiterr("error reading TIFF input");
573 <        
573 >                                        /* also works for float RGB */
574          for (x = cvts.xmax; x--; ) {
575 <                cvts.r.colors[x][RED] = cvts.t.fp[3*x];
576 <                cvts.r.colors[x][GRN] = cvts.t.fp[3*x + 1];
577 <                cvts.r.colors[x][BLU] = cvts.t.fp[3*x + 2];
575 >                setcolor(cvts.r.colors[x],
576 >                                cvts.t.fp[3*x],
577 >                                cvts.t.fp[3*x + 1],
578 >                                cvts.t.fp[3*x + 2]);
579                  if (CHK(C_CXFM))
580                          colortrans(cvts.r.colors[x], cvts.cmat,
581                                          cvts.r.colors[x]);
# Line 510 | Line 594 | uint32 y;
594   }
595  
596  
597 < int
598 < L2Color(y)                      /* read/convert/write L16->COLOR scanline */
597 > void
598 > RRGGBB2Color(y)                 /* read/convert/write RGB16->COLOR scanline */
599   uint32  y;
600   {
601 +        int     dogamma = cvts.gamcor < 0.99 | cvts.gamcor > 1.01;
602 +        register double d;
603          register int    x;
604  
605 <        if (CHK(C_RFLT|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY))
606 <                quiterr("internal error 1 in L2Color");
605 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_TWRD|C_RFLT))
606 >                quiterr("internal error 1 in RRGGBB2Color");
607  
608          if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
609                  quiterr("error reading TIFF input");
610          
611 <        for (x = cvts.xmax; x--; )
612 <                cvts.r.colors[x][RED] =
613 <                cvts.r.colors[x][GRN] =
614 <                cvts.r.colors[x][BLU] = cvts.t.fp[x] > 0. ? cvts.t.fp[x] : 0.;
611 >        for (x = cvts.xmax; x--; ) {
612 >                d = (cvts.t.wp[3*x] + 0.5)*(1./(1L<<16));
613 >                if (dogamma) d = pow(d, cvts.gamcor);
614 >                colval(cvts.r.colors[x],RED) = d;
615 >                d = (cvts.t.wp[3*x + 1] + 0.5)*(1./(1L<<16));
616 >                if (dogamma) d = pow(d, cvts.gamcor);
617 >                colval(cvts.r.colors[x],GRN) = d;
618 >                d = (cvts.t.wp[3*x + 2] + 0.5)*(1./(1L<<16));
619 >                if (dogamma) d = pow(d, cvts.gamcor);
620 >                colval(cvts.r.colors[x],BLU) = d;
621 >                if (CHK(C_CXFM))
622 >                        colortrans(cvts.r.colors[x], cvts.cmat,
623 >                                        cvts.r.colors[x]);
624 >                if (CHK(C_GAMUT))
625 >                        clipgamut(cvts.r.colors[x], cvts.t.fp[3*x + 1],
626 >                                        CGAMUT_LOWER, cblack, cwhite);
627 >        }
628 >        if (cvts.bradj) {
629 >                d = pow(2.,(double)cvts.bradj);
630 >                for (x = cvts.xmax; x--; )
631 >                        scalecolor(cvts.r.colors[x], d);
632 >        }
633  
634          if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
635                  quiterr("error writing Radiance picture");
636   }
637  
638  
639 < int
639 > void
640 > L2Color(y)                      /* read/convert/write Lfloat->COLOR scanline */
641 > uint32  y;
642 > {
643 >        float   m = pow(2., (double)cvts.bradj);
644 >        register int    x;
645 >
646 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY))
647 >                quiterr("internal error 1 in L2Color");
648 >
649 >        if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
650 >                quiterr("error reading TIFF input");
651 >                                        /* also works for float greyscale */
652 >        for (x = cvts.xmax; x--; ) {
653 >                register float  f = cvts.t.fp[x];
654 >                if (cvts.bradj) f *= m;
655 >                setcolor(cvts.r.colors[x], f, f, f);
656 >        }
657 >        if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
658 >                quiterr("error writing Radiance picture");
659 > }
660 >
661 >
662 > void
663   RGB2Colr(y)                     /* read/convert/write RGB->COLR scanline */
664   uint32  y;
665   {
666          COLOR   ctmp;
667          register int    x;
668  
669 <        if (CHK(C_RFLT|C_TFLT|C_GRY))
669 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY))
670                  quiterr("internal error 1 in RGB2Colr");
671  
672          if (cvts.pconf == PLANARCONFIG_CONTIG) {
# Line 554 | Line 681 | uint32 y;
681                  if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
682                          goto readerr;
683                  if (TIFFReadScanline(cvts.tif,
684 <                                (tidata_t)(cvts.t.bp + cvts.xmax), y, 1) < 0)
684 >                                (tdata_t)(cvts.t.bp + cvts.xmax), y, 1) < 0)
685                          goto readerr;
686                  if (TIFFReadScanline(cvts.tif,
687 <                                (tidata_t)(cvts.t.bp + 2*cvts.xmax), y, 2) < 0)
687 >                                (tdata_t)(cvts.t.bp + 2*cvts.xmax), y, 2) < 0)
688                          goto readerr;
689                  for (x = cvts.xmax; x--; ) {
690                          cvts.r.colrs[x][RED] = cvts.t.bp[x];
# Line 588 | Line 715 | readerr:
715   }
716  
717  
718 < int
718 > void
719   Gry2Colr(y)                     /* read/convert/write G8->COLR scanline */
720   uint32  y;
721   {
722          register int    x;
723  
724 <        if (CHK(C_RFLT|C_TFLT) | !CHK(C_GRY))
724 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != C_GRY)
725                  quiterr("internal error 1 in Gry2Colr");
726  
727          if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
# Line 614 | Line 741 | uint32 y;
741   }
742  
743  
744 < int
745 < Color2L(y)                      /* read/convert/write COLOR->L16 scanline */
744 > void
745 > GGry2Color(y)                   /* read/convert/write G16->COLOR scanline */
746   uint32  y;
747   {
748 <        double  m = pow(2.,(double)cvts.bradj);
748 >        int     dogamma = cvts.gamcor < 0.99 | cvts.gamcor > 1.01;
749 >        double  m;
750 >        register double d;
751          register int    x;
752  
753 <        if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | !CHK(C_GRY))
753 >        if (CHK(C_TFLT|C_TWRD|C_GRY|C_RFLT) != (C_GRY|C_RFLT|C_TWRD))
754 >                quiterr("internal error 1 in GGry2Color");
755 >
756 >        if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
757 >                quiterr("error reading TIFF input");
758 >
759 >        if (cvts.bradj)
760 >                m = pow(2., (double)cvts.bradj);
761 >        for (x = cvts.xmax; x--; ) {
762 >                d = (cvts.t.wp[x] + 0.5)*(1./(1L<<16));
763 >                if (dogamma) d = pow(d, cvts.gamcor);
764 >                if (cvts.bradj) d *= m;
765 >                colval(cvts.r.colors[x],RED) =
766 >                colval(cvts.r.colors[x],GRN) =
767 >                colval(cvts.r.colors[x],BLU) = d;
768 >        }
769 >        if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
770 >                quiterr("error writing Radiance picture");
771 > }
772 >
773 >
774 > void
775 > Color2GGry(y)                   /* read/convert/write COLOR->G16 scanline */
776 > uint32  y;
777 > {
778 >        int     dogamma = cvts.gamcor < 0.99 | cvts.gamcor > 1.01;
779 >        float   m = pow(2.,(double)cvts.bradj);
780 >        register int    x;
781 >
782 >        if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TWRD|C_GRY))
783 >                quiterr("internal error 1 in Color2GGry");
784 >
785 >        if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
786 >                quiterr("error reading Radiance picture");
787 >
788 >        for (x = cvts.xmax; x--; ) {
789 >                register float  f = m*( CHK(C_XYZE) ?
790 >                                                colval(cvts.r.colors[x],CIEY)
791 >                                                : bright(cvts.r.colors[x]) );
792 >                if (f <= 0)
793 >                        cvts.t.wp[x] = 0;
794 >                else if (f >= 1)
795 >                        cvts.t.wp[x] = 0xffff;
796 >                else if (dogamma)
797 >                        cvts.t.wp[x] = (int)((float)(1L<<16) *
798 >                                                pow(f, 1./cvts.gamcor));
799 >                else
800 >                        cvts.t.wp[x] = (int)((float)(1L<<16) * f);
801 >        }
802 >
803 >        if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
804 >                quiterr("error writing TIFF output");
805 > }
806 >
807 >
808 > void
809 > Color2L(y)                      /* read/convert/write COLOR->Lfloat scanline */
810 > uint32  y;
811 > {
812 >        float   m = pow(2.,(double)cvts.bradj);
813 >        register int    x;
814 >
815 >        if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TFLT|C_GRY))
816                  quiterr("internal error 1 in Color2L");
817  
818          if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
# Line 636 | Line 827 | uint32 y;
827   }
828  
829  
830 < int
830 > void
831   Color2Luv(y)                    /* read/convert/write COLOR->Luv scanline */
832   uint32  y;
833   {
834          register int    x;
835  
836 <        if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | CHK(C_GRY))
836 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT))
837                  quiterr("internal error 1 in Color2Luv");
838  
839          if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
# Line 657 | Line 848 | uint32 y;
848                  for (x = cvts.xmax; x--; )
849                          scalecolor(cvts.r.colors[x], m);
850          }
851 <
851 >                                        /* also works for float RGB */
852          for (x = cvts.xmax; x--; ) {
853 <                cvts.t.fp[3*x] = colval(cvts.r.colors[x],RED);
854 <                cvts.t.fp[3*x+1] = colval(cvts.r.colors[x],GRN);
855 <                cvts.t.fp[3*x+2] = colval(cvts.r.colors[x],BLU);
853 >                cvts.t.fp[3*x] = colval(cvts.r.colors[x],CIEX);
854 >                cvts.t.fp[3*x+1] = colval(cvts.r.colors[x],CIEY);
855 >                cvts.t.fp[3*x+2] = colval(cvts.r.colors[x],CIEZ);
856          }
857  
858          if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
# Line 669 | Line 860 | uint32 y;
860   }
861  
862  
863 < int
863 > void
864 > Color2RRGGBB(y)                 /* read/convert/write COLOR->RGB16 scanline */
865 > uint32  y;
866 > {
867 >        int     dogamma = cvts.gamcor < 0.99 | cvts.gamcor > 1.01;
868 >        float   m = pow(2.,(double)cvts.bradj);
869 >        register int    x, i;
870 >
871 >        if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TWRD))
872 >                quiterr("internal error 1 in Color2RRGGBB");
873 >
874 >        if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
875 >                quiterr("error reading Radiance picture");
876 >
877 >        for (x = cvts.xmax; x--; )
878 >            for (i = 3; i--; ) {
879 >                register float  f = m*colval(cvts.r.colors[x],i);
880 >                if (f <= 0)
881 >                        cvts.t.wp[3*x + i] = 0;
882 >                else if (f >= 1)
883 >                        cvts.t.wp[3*x + i] = 0xffff;
884 >                else if (dogamma)
885 >                        cvts.t.wp[3*x + i] = (int)((float)(1L<<16) *
886 >                                                pow(f, 1./cvts.gamcor));
887 >                else
888 >                        cvts.t.wp[3*x + i] = (int)((float)(1L<<16)*f);
889 >            }
890 >
891 >        if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
892 >                quiterr("error writing TIFF output");
893 > }
894 >
895 >
896 > void
897   Colr2Gry(y)                     /* read/convert/write COLR->RGB scanline */
898   uint32  y;
899   {
900          register int    x;
901  
902 <        if (CHK(C_RFLT|C_TFLT) | !CHK(C_GRY))
902 >        if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != C_GRY)
903                  quiterr("internal error 1 in Colr2Gry");
904  
905          if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
# Line 695 | Line 919 | uint32 y;
919   }
920  
921  
922 < int
922 > void
923   Colr2RGB(y)                     /* read/convert/write COLR->RGB scanline */
924   uint32  y;
925   {
926          COLOR   ctmp;
927          register int    x;
928  
929 <        if (CHK(C_RFLT|C_TFLT|C_GRY))
929 >        if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY))
930                  quiterr("internal error 1 in Colr2RGB");
931  
932          if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines