ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_tiff.c
Revision: 2.11
Committed: Fri Aug 15 18:30:14 1997 UTC (26 years, 8 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 2.10: +10 -1 lines
Log Message:
changed StoNits to double and added JPEG reading capability

File Contents

# User Rev Content
1 gregl 2.9 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2 greg 1.1
3     #ifndef lint
4 gregl 2.9 static char SCCSid[] = "$SunId$ SGI";
5 greg 1.1 #endif
6    
7     /*
8 gregl 2.9 * Program to convert between RADIANCE and TIFF files.
9     * Added experimental LogLuv encodings 7/97 (GWL).
10 greg 1.1 */
11    
12     #include <stdio.h>
13 greg 2.3 #include <math.h>
14 greg 1.1 #include "tiffio.h"
15     #include "color.h"
16 greg 1.6 #include "resolu.h"
17    
18 greg 2.8 #define GAMCOR 2.2 /* default gamma */
19    
20 gregl 2.9 #ifndef malloc
21     extern char *malloc();
22     #endif
23     /* conversion flags */
24     #define C_CXFM 0x1 /* needs color transformation */
25     #define C_GAMUT 0x2 /* needs gamut mapping */
26     #define C_GAMMA 0x4 /* needs gamma correction */
27     #define C_GRY 0x8 /* TIFF is greyscale */
28     #define C_XYZE 0x10 /* Radiance is XYZE */
29     #define C_RFLT 0x20 /* Radiance data is float */
30     #define C_TFLT 0x40 /* TIFF data is float */
31     #define C_PRIM 0x80 /* has assigned primaries */
32 greg 1.1
33 gregl 2.9 struct {
34     uint16 flags; /* conversion flags (defined above) */
35     uint16 comp; /* TIFF compression type */
36 gregl 2.10 uint16 phot; /* TIFF photometric type */
37 gregl 2.9 uint16 pconf; /* TIFF planar configuration */
38     float gamcor; /* gamma correction value */
39     short bradj; /* Radiance exposure adjustment (stops) */
40     uint16 orient; /* visual orientation (TIFF spec.) */
41 gregl 2.11 double stonits; /* input conversion to nits */
42 gregl 2.9 float pixrat; /* pixel aspect ratio */
43     FILE *rfp; /* Radiance stream pointer */
44     TIFF *tif; /* TIFF pointer */
45     uint32 xmax, ymax; /* image dimensions */
46     COLORMAT cmat; /* color transformation matrix */
47     RGBPRIMS prims; /* RGB primaries */
48     union {
49     COLR *colrs; /* 4-byte ???E pointer */
50     COLOR *colors; /* float array pointer */
51     char *p; /* generic pointer */
52     } r; /* Radiance scanline */
53     union {
54     uint8 *bp; /* byte pointer */
55     float *fp; /* float pointer */
56     char *p; /* generic pointer */
57     } t; /* TIFF scanline */
58     int (*tf)(); /* translation procedure */
59     } cvts = { /* conversion structure */
60 gregl 2.10 0, COMPRESSION_NONE, PHOTOMETRIC_RGB,
61     PLANARCONFIG_CONTIG, GAMCOR, 0, 1, 1., 1.,
62 gregl 2.9 };
63 greg 1.4
64 gregl 2.9 #define CHK(f) (cvts.flags & (f))
65     #define SET(f) (cvts.flags |= (f))
66     #define CLR(f) (cvts.flags &= ~(f))
67     #define TGL(f) (cvts.flags ^= (f))
68 greg 1.5
69 gregl 2.9 int Luv2Color(), L2Color(), RGB2Colr(), Gry2Colr();
70     int Color2Luv(), Color2L(), Colr2RGB(), Colr2Gry();
71 greg 1.1
72 gregl 2.9 short ortab[8] = { /* orientation conversion table */
73     YMAJOR|YDECR,
74     YMAJOR|YDECR|XDECR,
75     YMAJOR|XDECR,
76     YMAJOR,
77     YDECR,
78     XDECR|YDECR,
79     XDECR,
80     0
81     };
82 greg 1.1
83 gregl 2.9 #define pixorder() ortab[cvts.orient-1]
84    
85 greg 1.1 char *progname;
86    
87    
88     main(argc, argv)
89     int argc;
90     char *argv[];
91     {
92     int reverse = 0;
93     int i;
94    
95     progname = argv[0];
96    
97     for (i = 1; i < argc; i++)
98     if (argv[i][0] == '-')
99     switch (argv[i][1]) {
100 gregl 2.9 case 'g': /* gamma correction */
101     cvts.gamcor = atof(argv[++i]);
102 greg 1.1 break;
103 gregl 2.9 case 'x': /* XYZE Radiance output */
104     TGL(C_XYZE);
105 greg 1.4 break;
106 gregl 2.9 case 'z': /* LZW compressed output */
107     cvts.comp = COMPRESSION_LZW;
108 greg 1.5 break;
109 gregl 2.9 case 'L': /* LogLuv 32-bit output */
110 gregl 2.10 cvts.comp = COMPRESSION_SGILOG;
111     cvts.phot = PHOTOMETRIC_LOGLUV;
112 gregl 2.9 break;
113     case 'l': /* LogLuv 24-bit output */
114 gregl 2.10 cvts.comp = COMPRESSION_SGILOG24;
115     cvts.phot = PHOTOMETRIC_LOGLUV;
116 gregl 2.9 break;
117     case 'b': /* greyscale output? */
118     TGL(C_GRY);
119     break;
120     case 'e': /* exposure adjustment */
121 greg 1.1 if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
122     goto userr;
123 gregl 2.9 cvts.bradj = atoi(argv[++i]);
124 greg 1.1 break;
125 gregl 2.9 case 'r': /* reverse conversion? */
126 greg 1.1 reverse = !reverse;
127     break;
128     case '\0':
129     goto doneopts;
130     default:
131     goto userr;
132     }
133     else
134     break;
135     doneopts:
136 gregl 2.9 if (reverse) {
137 greg 1.1
138     if (i != argc-2 && i != argc-1)
139     goto userr;
140 gregl 2.9
141     tiff2ra(i, argv);
142    
143     } else {
144    
145 greg 1.1 if (i != argc-2)
146     goto userr;
147    
148 gregl 2.10 if (CHK(C_GRY)) /* consistency corrections */
149     if (cvts.phot == PHOTOMETRIC_RGB)
150     cvts.phot = PHOTOMETRIC_MINISBLACK;
151     else {
152     cvts.phot = PHOTOMETRIC_LOGL;
153     cvts.comp = COMPRESSION_SGILOG;
154     }
155 gregl 2.9
156     ra2tiff(i, argv);
157     }
158    
159 greg 1.1 exit(0);
160     userr:
161 greg 1.4 fprintf(stderr,
162 gregl 2.9 "Usage: %s [-z|-L|-l][-b][-e +/-stops][-g gamma] {in.pic|-} out.tif\n",
163 greg 1.1 progname);
164 gregl 2.9 fprintf(stderr,
165     " Or: %s -r [-x][-e +/-stops][-g gamma] in.tif [out.pic|-]\n",
166     progname);
167 greg 1.1 exit(1);
168     }
169    
170    
171     quiterr(err) /* print message and exit */
172     char *err;
173     {
174     if (err != NULL) {
175     fprintf(stderr, "%s: %s\n", progname, err);
176     exit(1);
177     }
178     exit(0);
179     }
180    
181    
182 gregl 2.9 allocbufs() /* allocate scanline buffers */
183 greg 1.1 {
184 gregl 2.9 int rsiz, tsiz;
185    
186     rsiz = CHK(C_RFLT) ? sizeof(COLOR) : sizeof(COLR);
187     tsiz = (CHK(C_TFLT) ? sizeof(float) : sizeof(uint8)) *
188     (CHK(C_GRY) ? 1 : 3);
189     cvts.r.p = (char *)malloc(rsiz*cvts.xmax);
190     cvts.t.p = (char *)malloc(tsiz*cvts.xmax);
191     if (cvts.r.p == NULL | cvts.t.p == NULL)
192     quiterr("no memory to allocate scanline buffers");
193     }
194    
195    
196     initfromtif() /* initialize conversion from TIFF input */
197     {
198     uint16 hi;
199     float *fa, f1, f2;
200    
201     CLR(C_GRY|C_GAMMA|C_PRIM|C_RFLT|C_TFLT|C_CXFM);
202    
203     TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_PLANARCONFIG, &cvts.pconf);
204    
205     if (TIFFGetField(cvts.tif, TIFFTAG_PRIMARYCHROMATICITIES, &fa)) {
206     cvts.prims[RED][CIEX] = fa[0];
207     cvts.prims[RED][CIEY] = fa[1];
208     cvts.prims[GRN][CIEX] = fa[2];
209     cvts.prims[GRN][CIEY] = fa[3];
210     cvts.prims[BLU][CIEX] = fa[4];
211     cvts.prims[BLU][CIEY] = fa[5];
212     cvts.prims[WHT][CIEX] = 1./3.;
213     cvts.prims[WHT][CIEY] = 1./3.;
214     if (TIFFGetField(cvts.tif, TIFFTAG_WHITEPOINT, &fa)) {
215     cvts.prims[WHT][CIEX] = fa[0];
216     cvts.prims[WHT][CIEY] = fa[1];
217     }
218     SET(C_PRIM);
219     }
220    
221     if (!TIFFGetField(cvts.tif, TIFFTAG_COMPRESSION, &cvts.comp))
222     cvts.comp = COMPRESSION_NONE;
223    
224     if (TIFFGetField(cvts.tif, TIFFTAG_XRESOLUTION, &f1) &&
225     TIFFGetField(cvts.tif, TIFFTAG_YRESOLUTION, &f2))
226     cvts.pixrat = f1/f2;
227    
228 gregl 2.10 TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_ORIENTATION, &cvts.orient);
229 gregl 2.9
230 gregl 2.10 if (!TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_PHOTOMETRIC, &cvts.phot))
231     quiterr("TIFF has unspecified photometric type");
232    
233     switch (cvts.phot) {
234     case PHOTOMETRIC_LOGLUV:
235 gregl 2.9 SET(C_RFLT|C_TFLT);
236     if (!CHK(C_XYZE)) {
237     cpcolormat(cvts.cmat, xyz2rgbmat);
238     SET(C_CXFM|C_GAMUT);
239 gregl 2.10 } else if (cvts.comp == COMPRESSION_SGILOG)
240 gregl 2.9 SET(C_GAMUT);
241     if (cvts.pconf != PLANARCONFIG_CONTIG)
242     quiterr("cannot handle separate Luv planes");
243 gregl 2.10 TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
244     SGILOGDATAFMT_FLTXYZ);
245 gregl 2.9 cvts.tf = Luv2Color;
246     break;
247 gregl 2.10 case PHOTOMETRIC_LOGL:
248     SET(C_GRY|C_RFLT|C_TFLT|C_GAMUT);
249     cvts.pconf = PLANARCONFIG_CONTIG;
250     TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
251     SGILOGDATAFMT_FLTY);
252     cvts.tf = L2Color;
253     break;
254 gregl 2.11 case PHOTOMETRIC_YCBCR:
255     if (cvts.comp == COMPRESSION_JPEG &&
256     cvts.pconf == PLANARCONFIG_CONTIG) {
257     TIFFSetField(cvts.tif, TIFFTAG_JPEGCOLORMODE,
258     JPEGCOLORMODE_RGB);
259     cvts.phot = PHOTOMETRIC_RGB;
260     } else
261     quiterr("unsupported photometric type");
262     /* fall through */
263 gregl 2.10 case PHOTOMETRIC_RGB:
264 gregl 2.9 SET(C_GAMMA|C_GAMUT);
265     setcolrgam(cvts.gamcor);
266     if (CHK(C_XYZE)) {
267     comprgb2xyzmat(cvts.cmat,
268     CHK(C_PRIM) ? cvts.prims : stdprims);
269     SET(C_CXFM);
270     }
271     if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) ||
272 gregl 2.10 hi != 3)
273     quiterr("unsupported samples per pixel for RGB");
274 gregl 2.9 if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi) ||
275     hi != 8)
276 gregl 2.10 quiterr("unsupported bits per sample for RGB");
277     cvts.tf = RGB2Colr;
278 gregl 2.9 break;
279 gregl 2.10 case PHOTOMETRIC_MINISBLACK:
280     SET(C_GRY|C_GAMMA|C_GAMUT);
281     cvts.pconf = PLANARCONFIG_CONTIG;
282     if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) ||
283     hi != 1)
284     quiterr("unsupported samples per pixel for greyscale");
285     if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi) ||
286     hi != 8)
287     quiterr("unsupported bits per sample for greyscale");
288     cvts.tf = Gry2Colr;
289     break;
290     default:
291     quiterr("unsupported photometric type");
292     break;
293 gregl 2.9 }
294    
295     if (!TIFFGetField(cvts.tif, TIFFTAG_IMAGEWIDTH, &cvts.xmax) ||
296     !TIFFGetField(cvts.tif, TIFFTAG_IMAGELENGTH, &cvts.ymax))
297 greg 1.1 quiterr("unknown input image resolution");
298 gregl 2.9
299     if (!TIFFGetField(cvts.tif, TIFFTAG_STONITS, &cvts.stonits))
300     cvts.stonits = 1.;
301     /* add to Radiance header */
302     if (cvts.pixrat < .99 || cvts.pixrat > 1.01)
303     fputaspect(cvts.pixrat, cvts.rfp);
304     if (CHK(C_XYZE)) {
305     fputexpos(pow(2.,(double)cvts.bradj)/cvts.stonits, cvts.rfp);
306     fputformat(CIEFMT, cvts.rfp);
307     } else {
308     if (CHK(C_PRIM))
309     fputprims(cvts.prims, cvts.rfp);
310     fputexpos(WHTEFFICACY*pow(2.,(double)cvts.bradj)/cvts.stonits,
311     cvts.rfp);
312     fputformat(COLRFMT, cvts.rfp);
313     }
314    
315     allocbufs(); /* allocate scanline buffers */
316     }
317    
318    
319     tiff2ra(ac, av) /* convert TIFF image to Radiance picture */
320     int ac;
321     char *av[];
322     {
323     int32 y;
324     /* open TIFF input */
325     if ((cvts.tif = TIFFOpen(av[ac], "r")) == NULL)
326     quiterr("cannot open TIFF input");
327     /* open Radiance output */
328     if (av[ac+1] == NULL || !strcmp(av[ac+1], "-"))
329     cvts.rfp = stdout;
330     else if ((cvts.rfp = fopen(av[ac+1], "w")) == NULL)
331     quiterr("cannot open Radiance output picture");
332     /* start output header */
333     newheader("RADIANCE", cvts.rfp);
334     printargs(ac, av, cvts.rfp);
335    
336     initfromtif(); /* initialize conversion */
337    
338     fputc('\n', cvts.rfp); /* finish Radiance header */
339     fputresolu(pixorder(), (int)cvts.xmax, (int)cvts.ymax, cvts.rfp);
340    
341     for (y = 0; y < cvts.ymax; y++) /* convert image */
342     (*cvts.tf)(y);
343     /* clean up */
344     fclose(cvts.rfp);
345     TIFFClose(cvts.tif);
346     }
347    
348    
349     int
350     headline(s) /* process Radiance input header line */
351     char *s;
352     {
353     char fmt[32];
354    
355     if (formatval(fmt, s)) {
356     if (!strcmp(fmt, COLRFMT))
357     CLR(C_XYZE);
358     else if (!strcmp(fmt, CIEFMT))
359     SET(C_XYZE);
360     else
361     quiterr("unrecognized input picture format");
362     return;
363     }
364     if (isexpos(s)) {
365     cvts.stonits /= exposval(s);
366     return;
367     }
368     if (isaspect(s)) {
369     cvts.pixrat *= aspectval(s);
370     return;
371     }
372     if (isprims(s)) {
373     primsval(cvts.prims, s);
374     SET(C_PRIM);
375     return;
376     }
377     }
378    
379    
380     initfromrad() /* initialize input from a Radiance picture */
381     {
382     int i1, i2, po;
383     /* read Radiance header */
384     CLR(C_RFLT|C_TFLT|C_XYZE|C_PRIM|C_GAMMA|C_CXFM);
385     cvts.stonits = 1.;
386     cvts.pixrat = 1.;
387     cvts.pconf = PLANARCONFIG_CONTIG;
388     getheader(cvts.rfp, headline, NULL);
389     if ((po = fgetresolu(&i1, &i2, cvts.rfp)) < 0)
390     quiterr("bad Radiance picture");
391     cvts.xmax = i1; cvts.ymax = i2;
392     for (i1 = 0; i1 < 8; i1++) /* interpret orientation */
393     if (ortab[i1] == po) {
394     cvts.orient = i1 + 1;
395     break;
396 greg 1.1 }
397 gregl 2.9 if (i1 >= 8)
398     quiterr("internal error 1 in initfromrad");
399     if (!(po & YMAJOR))
400     cvts.pixrat = 1./cvts.pixrat;
401     if (!CHK(C_XYZE))
402     cvts.stonits *= WHTEFFICACY;
403     /* set up conversion */
404     TIFFSetField(cvts.tif, TIFFTAG_COMPRESSION, cvts.comp);
405 gregl 2.10 TIFFSetField(cvts.tif, TIFFTAG_PHOTOMETRIC, cvts.phot);
406 gregl 2.9
407 gregl 2.10 switch (cvts.phot) {
408     case PHOTOMETRIC_LOGLUV:
409 gregl 2.9 SET(C_RFLT|C_TFLT);
410     CLR(C_GRY);
411     if (!CHK(C_XYZE)) {
412     cpcolormat(cvts.cmat, rgb2xyzmat);
413     SET(C_CXFM);
414     }
415 gregl 2.10 if (cvts.comp != COMPRESSION_SGILOG &&
416     cvts.comp != COMPRESSION_SGILOG24)
417     quiterr("internal error 2 in initfromrad");
418     TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
419     SGILOGDATAFMT_FLTXYZ);
420 gregl 2.9 cvts.tf = Color2Luv;
421     break;
422 gregl 2.10 case PHOTOMETRIC_LOGL:
423     SET(C_GRY|C_RFLT|C_TFLT);
424     if (cvts.comp != COMPRESSION_SGILOG)
425     quiterr("internal error 3 in initfromrad");
426     TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
427     SGILOGDATAFMT_FLTY);
428     cvts.tf = Color2L;
429     break;
430     case PHOTOMETRIC_RGB:
431 gregl 2.9 SET(C_GAMMA|C_GAMUT);
432 gregl 2.10 CLR(C_GRY);
433 gregl 2.9 setcolrgam(cvts.gamcor);
434     if (CHK(C_XYZE)) {
435     compxyz2rgbmat(cvts.cmat,
436     CHK(C_PRIM) ? cvts.prims : stdprims);
437     SET(C_CXFM);
438     }
439     if (CHK(C_PRIM)) {
440     TIFFSetField(cvts.tif, TIFFTAG_PRIMARYCHROMATICITIES,
441     (float *)cvts.prims);
442     TIFFSetField(cvts.tif, TIFFTAG_WHITEPOINT,
443     (float *)cvts.prims[WHT]);
444     }
445 gregl 2.10 cvts.tf = Colr2RGB;
446 gregl 2.9 break;
447 gregl 2.10 case PHOTOMETRIC_MINISBLACK:
448     SET(C_GRY|C_GAMMA|C_GAMUT);
449     setcolrgam(cvts.gamcor);
450     cvts.tf = Colr2Gry;
451     break;
452     default:
453     quiterr("internal error 4 in initfromrad");
454     break;
455 greg 1.1 }
456 gregl 2.9 /* set other TIFF fields */
457     TIFFSetField(cvts.tif, TIFFTAG_IMAGEWIDTH, cvts.xmax);
458     TIFFSetField(cvts.tif, TIFFTAG_IMAGELENGTH, cvts.ymax);
459     TIFFSetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, CHK(C_GRY) ? 1 : 3);
460     TIFFSetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, CHK(C_TFLT) ? 32 : 8);
461     TIFFSetField(cvts.tif, TIFFTAG_XRESOLUTION, 72.);
462     TIFFSetField(cvts.tif, TIFFTAG_YRESOLUTION, 72./cvts.pixrat);
463     TIFFSetField(cvts.tif, TIFFTAG_ORIENTATION, cvts.orient);
464     TIFFSetField(cvts.tif, TIFFTAG_RESOLUTIONUNIT, 2);
465     TIFFSetField(cvts.tif, TIFFTAG_PLANARCONFIG, cvts.pconf);
466     TIFFSetField(cvts.tif, TIFFTAG_STONITS,
467     cvts.stonits/pow(2.,(double)cvts.bradj));
468 gregl 2.10 if (cvts.comp == COMPRESSION_NONE)
469     i1 = TIFFScanlineSize(cvts.tif);
470     else
471     i1 = 3*cvts.xmax; /* conservative guess */
472 gregl 2.9 i2 = 8192/i1; /* compute good strip size */
473     if (i2 < 1) i2 = 1;
474     TIFFSetField(cvts.tif, TIFFTAG_ROWSPERSTRIP, (uint32)i2);
475    
476     allocbufs(); /* allocate scanline buffers */
477     }
478    
479    
480     ra2tiff(ac, av) /* convert Radiance picture to TIFF image */
481     int ac;
482     char *av[];
483     {
484     uint32 y;
485     /* open Radiance file */
486     if (!strcmp(av[ac], "-"))
487     cvts.rfp = stdin;
488     else if ((cvts.rfp = fopen(av[ac], "r")) == NULL)
489     quiterr("cannot open Radiance input picture");
490     /* open TIFF file */
491     if ((cvts.tif = TIFFOpen(av[ac+1], "w")) == NULL)
492     quiterr("cannot open TIFF output");
493    
494     initfromrad(); /* initialize conversion */
495    
496     for (y = 0; y < cvts.ymax; y++) /* convert image */
497     (*cvts.tf)(y);
498 greg 1.1 /* clean up */
499 gregl 2.9 TIFFClose(cvts.tif);
500     fclose(cvts.rfp);
501     }
502    
503    
504     int
505     Luv2Color(y) /* read/convert/write Luv->COLOR scanline */
506     uint32 y;
507     {
508     register int x;
509    
510     if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | CHK(C_GRY))
511     quiterr("internal error 1 in Luv2Color");
512    
513     if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
514     quiterr("error reading TIFF input");
515    
516     for (x = cvts.xmax; x--; ) {
517     cvts.r.colors[x][RED] = cvts.t.fp[3*x];
518     cvts.r.colors[x][GRN] = cvts.t.fp[3*x + 1];
519     cvts.r.colors[x][BLU] = cvts.t.fp[3*x + 2];
520     if (CHK(C_CXFM))
521     colortrans(cvts.r.colors[x], cvts.cmat,
522     cvts.r.colors[x]);
523     if (CHK(C_GAMUT))
524     clipgamut(cvts.r.colors[x], cvts.t.fp[3*x + 1],
525     CGAMUT_LOWER, cblack, cwhite);
526     }
527     if (cvts.bradj) {
528     double m = pow(2.,(double)cvts.bradj);
529     for (x = cvts.xmax; x--; )
530     scalecolor(cvts.r.colors[x], m);
531     }
532    
533     if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
534     quiterr("error writing Radiance picture");
535     }
536    
537    
538     int
539     L2Color(y) /* read/convert/write L16->COLOR scanline */
540     uint32 y;
541     {
542     register int x;
543    
544     if (CHK(C_RFLT|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY))
545     quiterr("internal error 1 in L2Color");
546    
547     if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
548     quiterr("error reading TIFF input");
549    
550     for (x = cvts.xmax; x--; )
551     cvts.r.colors[x][RED] =
552     cvts.r.colors[x][GRN] =
553     cvts.r.colors[x][BLU] = cvts.t.fp[x] > 0. ? cvts.t.fp[x] : 0.;
554    
555     if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
556     quiterr("error writing Radiance picture");
557     }
558    
559    
560     int
561     RGB2Colr(y) /* read/convert/write RGB->COLR scanline */
562     uint32 y;
563     {
564     COLOR ctmp;
565     register int x;
566    
567     if (CHK(C_RFLT|C_TFLT|C_GRY))
568     quiterr("internal error 1 in RGB2Colr");
569    
570     if (cvts.pconf == PLANARCONFIG_CONTIG) {
571     if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
572     goto readerr;
573     for (x = cvts.xmax; x--; ) {
574     cvts.r.colrs[x][RED] = cvts.t.bp[3*x];
575     cvts.r.colrs[x][GRN] = cvts.t.bp[3*x + 1];
576     cvts.r.colrs[x][BLU] = cvts.t.bp[3*x + 2];
577     }
578     } else {
579     if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
580     goto readerr;
581     if (TIFFReadScanline(cvts.tif,
582     (tidata_t)(cvts.t.bp + cvts.xmax), y, 1) < 0)
583     goto readerr;
584     if (TIFFReadScanline(cvts.tif,
585     (tidata_t)(cvts.t.bp + 2*cvts.xmax), y, 2) < 0)
586     goto readerr;
587     for (x = cvts.xmax; x--; ) {
588     cvts.r.colrs[x][RED] = cvts.t.bp[x];
589     cvts.r.colrs[x][GRN] = cvts.t.bp[cvts.xmax + x];
590     cvts.r.colrs[x][BLU] = cvts.t.bp[2*cvts.xmax + x];
591     }
592     }
593    
594     gambs_colrs(cvts.r.colrs, cvts.xmax);
595     if (CHK(C_CXFM))
596     for (x = cvts.xmax; x--; ) {
597     colr_color(ctmp, cvts.r.colrs[x]);
598     colortrans(ctmp, cvts.cmat, ctmp);
599     if (CHK(C_GAMUT)) /* !CHK(C_XYZE) */
600     clipgamut(ctmp, bright(ctmp), CGAMUT_LOWER,
601     cblack, cwhite);
602     setcolr(cvts.r.colrs[x], colval(ctmp,RED),
603     colval(ctmp,GRN), colval(ctmp,BLU));
604     }
605     if (cvts.bradj)
606     shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
607    
608     if (fwritecolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
609     quiterr("error writing Radiance picture");
610 greg 1.1 return;
611     readerr:
612     quiterr("error reading TIFF input");
613     }
614    
615    
616 gregl 2.9 int
617     Gry2Colr(y) /* read/convert/write G8->COLR scanline */
618     uint32 y;
619 greg 1.1 {
620     register int x;
621 gregl 2.9
622     if (CHK(C_RFLT|C_TFLT) | !CHK(C_GRY))
623     quiterr("internal error 1 in Gry2Colr");
624    
625     if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
626     quiterr("error reading TIFF input");
627    
628     for (x = cvts.xmax; x--; )
629     cvts.r.colrs[x][RED] =
630     cvts.r.colrs[x][GRN] =
631     cvts.r.colrs[x][BLU] = cvts.t.bp[x];
632    
633     gambs_colrs(cvts.r.colrs, cvts.xmax);
634     if (cvts.bradj)
635     shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
636    
637     if (fwritecolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
638     quiterr("error writing Radiance picture");
639     }
640    
641    
642     int
643     Color2L(y) /* read/convert/write COLOR->L16 scanline */
644     uint32 y;
645     {
646     double m = pow(2.,(double)cvts.bradj);
647     register int x;
648    
649     if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | !CHK(C_GRY))
650     quiterr("internal error 1 in Color2L");
651    
652     if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
653     quiterr("error reading Radiance picture");
654    
655     for (x = cvts.xmax; x--; )
656     cvts.t.fp[x] = m*( CHK(C_XYZE) ? colval(cvts.r.colors[x],CIEY)
657     : bright(cvts.r.colors[x]) );
658    
659     if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
660     quiterr("error writing TIFF output");
661     }
662    
663    
664     int
665     Color2Luv(y) /* read/convert/write COLOR->Luv scanline */
666     uint32 y;
667     {
668     register int x;
669    
670     if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | CHK(C_GRY))
671     quiterr("internal error 1 in Color2Luv");
672    
673     if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
674     quiterr("error reading Radiance picture");
675    
676     if (CHK(C_CXFM))
677     for (x = cvts.xmax; x--; )
678     colortrans(cvts.r.colors[x], cvts.cmat,
679     cvts.r.colors[x]);
680     if (cvts.bradj) {
681     double m = pow(2.,(double)cvts.bradj);
682     for (x = cvts.xmax; x--; )
683     scalecolor(cvts.r.colors[x], m);
684     }
685    
686     for (x = cvts.xmax; x--; ) {
687     cvts.t.fp[3*x] = colval(cvts.r.colors[x],RED);
688     cvts.t.fp[3*x+1] = colval(cvts.r.colors[x],GRN);
689     cvts.t.fp[3*x+2] = colval(cvts.r.colors[x],BLU);
690     }
691    
692     if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
693     quiterr("error writing TIFF output");
694     }
695    
696    
697     int
698     Colr2Gry(y) /* read/convert/write COLR->RGB scanline */
699     uint32 y;
700     {
701     register int x;
702    
703     if (CHK(C_RFLT|C_TFLT) | !CHK(C_GRY))
704     quiterr("internal error 1 in Colr2Gry");
705    
706     if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
707     quiterr("error reading Radiance picture");
708    
709     if (cvts.bradj)
710     shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
711     for (x = cvts.xmax; x--; )
712     colval(cvts.r.colrs[x],CIEY) = normbright(cvts.r.colrs[x]);
713     colrs_gambs(cvts.r.colrs, cvts.xmax);
714    
715     for (x = cvts.xmax; x--; )
716     cvts.t.bp[x] = colval(cvts.r.colrs[x],CIEY);
717    
718     if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
719     quiterr("error writing TIFF output");
720     }
721    
722    
723     int
724     Colr2RGB(y) /* read/convert/write COLR->RGB scanline */
725     uint32 y;
726     {
727     COLOR ctmp;
728     register int x;
729    
730     if (CHK(C_RFLT|C_TFLT|C_GRY))
731     quiterr("internal error 1 in Colr2RGB");
732    
733     if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
734     quiterr("error reading Radiance picture");
735    
736     if (cvts.bradj)
737     shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
738     if (CHK(C_CXFM))
739     for (x = cvts.xmax; x--; ) {
740     colr_color(ctmp, cvts.r.colrs[x]);
741     colortrans(ctmp, cvts.cmat, ctmp);
742     if (CHK(C_GAMUT))
743     clipgamut(ctmp, bright(ctmp), CGAMUT,
744     cblack, cwhite);
745     setcolr(cvts.r.colrs[x], colval(ctmp,RED),
746     colval(ctmp,GRN), colval(ctmp,BLU));
747 greg 1.1 }
748 gregl 2.9 colrs_gambs(cvts.r.colrs, cvts.xmax);
749    
750     for (x = cvts.xmax; x--; ) {
751     cvts.t.bp[3*x] = cvts.r.colrs[x][RED];
752     cvts.t.bp[3*x+1] = cvts.r.colrs[x][GRN];
753     cvts.t.bp[3*x+2] = cvts.r.colrs[x][BLU];
754 greg 1.1 }
755 gregl 2.9
756     if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
757     quiterr("error writing TIFF output");
758 greg 1.1 }