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.8 by greg, Mon Oct 30 10:56:57 1995 UTC vs.
Revision 2.9 by gregl, Thu Jul 24 11:47:05 1997 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines