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 1.1 by greg, Thu Aug 15 13:34:58 1991 UTC vs.
Revision 2.16 by gwlarson, Thu Oct 8 11:21:52 1998 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>
13 <
13 > #include  <math.h>
14   #include  "tiffio.h"
14
15   #include  "color.h"
16 + #include  "resolu.h"
17  
18 < extern double  atof();
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 < double  gamma = 2.2;                    /* gamma correction */
33 > struct {
34 >        uint16  flags;          /* conversion flags (defined above) */
35 >        uint16  comp;           /* TIFF compression type */
36 >        uint16  phot;           /* TIFF photometric type */
37 >        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 >        double  stonits;        /* input conversion to nits */
42 >        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 >        0, COMPRESSION_NONE, PHOTOMETRIC_RGB,
61 >        PLANARCONFIG_CONTIG, GAMCOR, 0, 1, 1., 1.,
62 > };
63  
64 < int  bradj = 0;                         /* brightness adjustment */
64 > #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  
69 + int     Luv2Color(), L2Color(), RGB2Colr(), Gry2Colr();
70 + int     Color2Luv(), Color2L(), Colr2RGB(), Colr2Gry();
71 +
72 + 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 +
83 + #define pixorder()      ortab[cvts.orient-1]
84 +
85   char  *progname;
86  
87  
# Line 37 | Line 97 | char  *argv[];
97          for (i = 1; i < argc; i++)
98                  if (argv[i][0] == '-')
99                          switch (argv[i][1]) {
100 <                        /* not allowed to reset gamma...
101 <                        case 'g':
42 <                                gamma = atof(argv[++i]);
100 >                        case 'g':               /* gamma correction */
101 >                                cvts.gamcor = atof(argv[++i]);
102                                  break;
103 <                        */
104 <                        case 'e':
103 >                        case 'x':               /* XYZE Radiance output */
104 >                                TGL(C_XYZE);
105 >                                break;
106 >                        case 'z':               /* LZW compressed output */
107 >                                cvts.comp = COMPRESSION_LZW;
108 >                                break;
109 >                        case 'L':               /* LogLuv 32-bit output */
110 >                                cvts.comp = COMPRESSION_SGILOG;
111 >                                cvts.phot = PHOTOMETRIC_LOGLUV;
112 >                                break;
113 >                        case 'l':               /* LogLuv 24-bit output */
114 >                                cvts.comp = COMPRESSION_SGILOG24;
115 >                                cvts.phot = PHOTOMETRIC_LOGLUV;
116 >                                break;
117 >                        case 'b':               /* greyscale output? */
118 >                                TGL(C_GRY);
119 >                                break;
120 >                        case 'e':               /* exposure adjustment */
121                                  if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
122                                          goto userr;
123 <                                bradj = atoi(argv[++i]);
123 >                                cvts.bradj = atoi(argv[++i]);
124                                  break;
125 <                        case 'r':
125 >                        case 'r':               /* reverse conversion? */
126                                  reverse = !reverse;
127                                  break;
128                          case '\0':
# Line 58 | Line 133 | char  *argv[];
133                  else
134                          break;
135   doneopts:
136 <        setcolrgam(gamma);
136 >        if (reverse) {
137  
63        if (reverse)
138                  if (i != argc-2 && i != argc-1)
139                          goto userr;
140 <                else
141 <                        tiff2ra(argv[i], argv[i+1]);
142 <        else
140 >
141 >                tiff2ra(i, argv);
142 >
143 >        } else {
144 >
145                  if (i != argc-2)
146                          goto userr;
71                else
72                        ra2tiff(argv[i], argv[i+1]);
147  
148 +                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 +
156 +                ra2tiff(i, argv);
157 +        }
158 +
159          exit(0);
160   userr:
161 <        fprintf(stderr, "Usage: %s [-r][-e +/-stops] input output\n",
161 >        fprintf(stderr,
162 >        "Usage: %s [-z|-L|-l][-b][-e +/-stops][-g gamma] {in.pic|-} out.tif\n",
163                          progname);
164 +        fprintf(stderr,
165 +        "   Or: %s -r [-x][-e +/-stops][-g gamma] in.tif [out.pic|-]\n",
166 +                        progname);
167          exit(1);
168   }
169  
# Line 90 | Line 179 | char  *err;
179   }
180  
181  
182 < tiff2ra(inpf, outf)     /* convert TIFF file to Radiance picture */
94 < char    *inpf, *outf;
182 > allocbufs()                     /* allocate scanline buffers */
183   {
184 <        unsigned long   xmax, ymax;
185 <        TIFF    *tif;
186 <        unsigned short  pconfig;
187 <        unsigned short  hi;
188 <        char    *cp;
189 <        register BYTE   *scanin;
190 <        register COLR   *scanout;
191 <        register int    x;
192 <        int     y;
193 <                                        /* open/check  TIFF file */
194 <        if ((tif = TIFFOpen(inpf, "r")) == NULL)
195 <                quiterr("cannot open TIFF input");
196 <        if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &hi) || hi != 3)
197 <                quiterr("unsupported samples per pixel");
198 <        if (!TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &hi) || hi != 8)
199 <                quiterr("unsupported bits per sample");
200 <        if (TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &hi) && hi != 2)
201 <                quiterr("unsupported photometric interpretation");
202 <        if (!TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &pconfig) ||
203 <                        (pconfig != 1 && pconfig != 2))
204 <                quiterr("unsupported planar configuration");
205 <        if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &xmax) ||
206 <                        !TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &ymax))
184 >        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 >        TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_ORIENTATION, &cvts.orient);
229 >
230 >        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 >                SET(C_RFLT|C_TFLT);
236 >                if (!CHK(C_XYZE)) {
237 >                        cpcolormat(cvts.cmat, xyz2rgbmat);
238 >                        SET(C_CXFM|C_GAMUT);
239 >                } else if (cvts.comp == COMPRESSION_SGILOG)
240 >                        SET(C_GAMUT);
241 >                if (cvts.pconf != PLANARCONFIG_CONTIG)
242 >                        quiterr("cannot handle separate Luv planes");
243 >                TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
244 >                                SGILOGDATAFMT_FLOAT);
245 >                cvts.tf = Luv2Color;
246 >                break;
247 >        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_FLOAT);
252 >                cvts.tf = L2Color;
253 >                break;
254 >        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 >        case PHOTOMETRIC_RGB:
264 >                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 >                                hi != 3)
273 >                        quiterr("unsupported samples per pixel for RGB");
274 >                if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi) ||
275 >                                hi != 8)
276 >                        quiterr("unsupported bits per sample for RGB");
277 >                cvts.tf = RGB2Colr;
278 >                break;
279 >        case PHOTOMETRIC_MINISBLACK:
280 >                SET(C_GRY|C_GAMMA|C_GAMUT);
281 >                setcolrgam(cvts.gamcor);
282 >                cvts.pconf = PLANARCONFIG_CONTIG;
283 >                if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) ||
284 >                                hi != 1)
285 >                        quiterr("unsupported samples per pixel for greyscale");
286 >                if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi) ||
287 >                                hi != 8)
288 >                        quiterr("unsupported bits per sample for greyscale");
289 >                cvts.tf = Gry2Colr;
290 >                break;
291 >        default:
292 >                quiterr("unsupported photometric type");
293 >                break;
294 >        }
295 >
296 >        if (!TIFFGetField(cvts.tif, TIFFTAG_IMAGEWIDTH, &cvts.xmax) ||
297 >                !TIFFGetField(cvts.tif, TIFFTAG_IMAGELENGTH, &cvts.ymax))
298                  quiterr("unknown input image resolution");
299 <                                                /* allocate scanlines */
300 <        scanin = (BYTE *)malloc(TIFFScanlineSize(tif));
301 <        scanout = (COLR *)malloc(xmax*sizeof(COLR));
302 <        if (scanin == NULL || scanout == NULL)
303 <                quiterr("out of memory in tiff2ra");
304 <                                        /* open output and write header */
305 <        if (outf != NULL && strcmp(outf, "-") &&
306 <                        freopen(outf, "w", stdout) == NULL)
307 <                quiterr("cannot open Radiance output file");
308 <        if (TIFFGetField(tif, TIFFTAG_IMAGEDESCRIPTION, &cp)) {
309 <                while (*cp && *cp == '\n')
310 <                        cp++;
311 <                for (x = 0; cp[x]; x++)
312 <                        if (cp[x] != '\n' || cp[x+1] != '\n')
313 <                                putchar(cp[x]);
135 <                if (x && cp[x-1] != '\n')
136 <                        putchar('\n');
299 >
300 >        if (!TIFFGetField(cvts.tif, TIFFTAG_STONITS, &cvts.stonits))
301 >                cvts.stonits = 1.;
302 >                                        /* add to Radiance header */
303 >        if (cvts.pixrat < .99 || cvts.pixrat > 1.01)
304 >                fputaspect(cvts.pixrat, cvts.rfp);
305 >        if (CHK(C_XYZE)) {
306 >                fputexpos(pow(2.,(double)cvts.bradj)/cvts.stonits, cvts.rfp);
307 >                fputformat(CIEFMT, cvts.rfp);
308 >        } else {
309 >                if (CHK(C_PRIM))
310 >                        fputprims(cvts.prims, cvts.rfp);
311 >                fputexpos(WHTEFFICACY*pow(2.,(double)cvts.bradj)/cvts.stonits,
312 >                                cvts.rfp);
313 >                fputformat(COLRFMT, cvts.rfp);
314          }
315 <        fputs(progname, stdout);
316 <        if (bradj)
317 <                printf(" -e %+d", bradj);
318 <        fputs(" -r\n", stdout);
319 <        fputformat(COLRFMT, stdout);
320 <        putchar('\n');
321 <        fputresolu(YDECR|YMAJOR, xmax, ymax, stdout);
322 <                                                /* convert image */
323 <        for (y = 0; y < ymax; y++) {
324 <                if (pconfig == 1) {
325 <                        if (TIFFReadScanline(tif, scanin, y, 0) < 0)
326 <                                goto readerr;
327 <                        for (x = 0; x < xmax; x++) {
328 <                                scanout[x][RED] = scanin[3*x];
329 <                                scanout[x][GRN] = scanin[3*x+1];
330 <                                scanout[x][BLU] = scanin[3*x+2];
331 <                        }
332 <                } else {
333 <                        if (TIFFReadScanline(tif, scanin, y, 0) < 0)
334 <                                goto readerr;
335 <                        for (x = 0; x < xmax; x++)
336 <                                scanout[x][RED] = scanin[x];
337 <                        if (TIFFReadScanline(tif, scanin, y, 1) < 0)
338 <                                goto readerr;
339 <                        for (x = 0; x < xmax; x++)
340 <                                scanout[x][GRN] = scanin[x];
341 <                        if (TIFFReadScanline(tif, scanin, y, 2) < 0)
342 <                                goto readerr;
343 <                        for (x = 0; x < xmax; x++)
344 <                                scanout[x][BLU] = scanin[x];
315 >
316 >        allocbufs();                    /* allocate scanline buffers */
317 > }
318 >
319 >
320 > tiff2ra(ac, av)         /* convert TIFF image to Radiance picture */
321 > int  ac;
322 > char  *av[];
323 > {
324 >        int32   y;
325 >                                        /* open TIFF input */
326 >        if ((cvts.tif = TIFFOpen(av[ac], "r")) == NULL)
327 >                quiterr("cannot open TIFF input");
328 >                                        /* open Radiance output */
329 >        if (av[ac+1] == NULL || !strcmp(av[ac+1], "-"))
330 >                cvts.rfp = stdout;
331 >        else if ((cvts.rfp = fopen(av[ac+1], "w")) == NULL)
332 >                quiterr("cannot open Radiance output picture");
333 >                                        /* start output header */
334 >        newheader("RADIANCE", cvts.rfp);
335 >        printargs(ac, av, cvts.rfp);
336 >
337 >        initfromtif();                  /* initialize conversion */
338 >
339 >        fputc('\n', cvts.rfp);          /* finish Radiance header */
340 >        fputresolu(pixorder(), (int)cvts.xmax, (int)cvts.ymax, cvts.rfp);
341 >
342 >        for (y = 0; y < cvts.ymax; y++)         /* convert image */
343 >                (*cvts.tf)(y);
344 >                                                /* clean up */
345 >        fclose(cvts.rfp);
346 >        TIFFClose(cvts.tif);
347 > }
348 >
349 >
350 > int
351 > headline(s)                     /* process Radiance input header line */
352 > char    *s;
353 > {
354 >        char    fmt[32];
355 >
356 >        if (formatval(fmt, s)) {
357 >                if (!strcmp(fmt, COLRFMT))
358 >                        CLR(C_XYZE);
359 >                else if (!strcmp(fmt, CIEFMT))
360 >                        SET(C_XYZE);
361 >                else
362 >                        quiterr("unrecognized input picture format");
363 >                return;
364 >        }
365 >        if (isexpos(s)) {
366 >                cvts.stonits /= exposval(s);
367 >                return;
368 >        }
369 >        if (isaspect(s)) {
370 >                cvts.pixrat *= aspectval(s);
371 >                return;
372 >        }
373 >        if (isprims(s)) {
374 >                primsval(cvts.prims, s);
375 >                SET(C_PRIM);
376 >                return;
377 >        }
378 > }
379 >
380 >
381 > initfromrad()                   /* initialize input from a Radiance picture */
382 > {
383 >        int     i1, i2, po;
384 >                                                /* read Radiance header */
385 >        CLR(C_RFLT|C_TFLT|C_XYZE|C_PRIM|C_GAMMA|C_CXFM);
386 >        cvts.stonits = 1.;
387 >        cvts.pixrat = 1.;
388 >        cvts.pconf = PLANARCONFIG_CONTIG;
389 >        getheader(cvts.rfp, headline, NULL);
390 >        if ((po = fgetresolu(&i1, &i2, cvts.rfp)) < 0)
391 >                quiterr("bad Radiance picture");
392 >        cvts.xmax = i1; cvts.ymax = i2;
393 >        for (i1 = 0; i1 < 8; i1++)              /* interpret orientation */
394 >                if (ortab[i1] == po) {
395 >                        cvts.orient = i1 + 1;
396 >                        break;
397                  }
398 <                gambs_colrs(scanout, xmax);
399 <                if (bradj)
400 <                        shiftcolrs(scanout, xmax, bradj);
401 <                if (fwritecolrs(scanout, xmax, stdout) < 0)
402 <                        quiterr("error writing Radiance picture");
398 >        if (i1 >= 8)
399 >                quiterr("internal error 1 in initfromrad");
400 >        if (!(po & YMAJOR))
401 >                cvts.pixrat = 1./cvts.pixrat;
402 >        if (!CHK(C_XYZE))
403 >                cvts.stonits *= WHTEFFICACY;
404 >                                                /* set up conversion */
405 >        TIFFSetField(cvts.tif, TIFFTAG_COMPRESSION, cvts.comp);
406 >        TIFFSetField(cvts.tif, TIFFTAG_PHOTOMETRIC, cvts.phot);
407 >
408 >        switch (cvts.phot) {
409 >        case PHOTOMETRIC_LOGLUV:
410 >                SET(C_RFLT|C_TFLT);
411 >                CLR(C_GRY);
412 >                if (!CHK(C_XYZE)) {
413 >                        cpcolormat(cvts.cmat, rgb2xyzmat);
414 >                        SET(C_CXFM);
415 >                }
416 >                if (cvts.comp != COMPRESSION_SGILOG &&
417 >                                cvts.comp != COMPRESSION_SGILOG24)
418 >                        quiterr("internal error 2 in initfromrad");
419 >                TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
420 >                                SGILOGDATAFMT_FLOAT);
421 >                cvts.tf = Color2Luv;
422 >                break;
423 >        case PHOTOMETRIC_LOGL:
424 >                SET(C_GRY|C_RFLT|C_TFLT);
425 >                if (cvts.comp != COMPRESSION_SGILOG)    
426 >                        quiterr("internal error 3 in initfromrad");
427 >                TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
428 >                                SGILOGDATAFMT_FLOAT);
429 >                cvts.tf = Color2L;
430 >                break;
431 >        case PHOTOMETRIC_RGB:
432 >                SET(C_GAMMA|C_GAMUT);
433 >                CLR(C_GRY);
434 >                setcolrgam(cvts.gamcor);
435 >                if (CHK(C_XYZE)) {
436 >                        compxyz2rgbmat(cvts.cmat,
437 >                                        CHK(C_PRIM) ? cvts.prims : stdprims);
438 >                        SET(C_CXFM);
439 >                }
440 >                if (CHK(C_PRIM)) {
441 >                        TIFFSetField(cvts.tif, TIFFTAG_PRIMARYCHROMATICITIES,
442 >                                        (float *)cvts.prims);
443 >                        TIFFSetField(cvts.tif, TIFFTAG_WHITEPOINT,
444 >                                        (float *)cvts.prims[WHT]);
445 >                }
446 >                cvts.tf = Colr2RGB;
447 >                break;
448 >        case PHOTOMETRIC_MINISBLACK:
449 >                SET(C_GRY|C_GAMMA|C_GAMUT);
450 >                setcolrgam(cvts.gamcor);
451 >                cvts.tf = Colr2Gry;
452 >                break;
453 >        default:
454 >                quiterr("internal error 4 in initfromrad");
455 >                break;
456          }
457 +                                                /* set other TIFF fields */
458 +        TIFFSetField(cvts.tif, TIFFTAG_IMAGEWIDTH, cvts.xmax);
459 +        TIFFSetField(cvts.tif, TIFFTAG_IMAGELENGTH, cvts.ymax);
460 +        TIFFSetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, CHK(C_GRY) ? 1 : 3);
461 +        TIFFSetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, CHK(C_TFLT) ? 32 : 8);
462 +        TIFFSetField(cvts.tif, TIFFTAG_XRESOLUTION, 72.);
463 +        TIFFSetField(cvts.tif, TIFFTAG_YRESOLUTION, 72./cvts.pixrat);
464 +        TIFFSetField(cvts.tif, TIFFTAG_ORIENTATION, cvts.orient);
465 +        TIFFSetField(cvts.tif, TIFFTAG_RESOLUTIONUNIT, 2);
466 +        TIFFSetField(cvts.tif, TIFFTAG_PLANARCONFIG, cvts.pconf);
467 +        TIFFSetField(cvts.tif, TIFFTAG_STONITS,
468 +                        cvts.stonits/pow(2.,(double)cvts.bradj));
469 +        if (cvts.comp == COMPRESSION_NONE)
470 +                i1 = TIFFScanlineSize(cvts.tif);
471 +        else
472 +                i1 = 3*cvts.xmax;       /* conservative guess */
473 +        i2 = 8192/i1;                           /* compute good strip size */
474 +        if (i2 < 1) i2 = 1;
475 +        TIFFSetField(cvts.tif, TIFFTAG_ROWSPERSTRIP, (uint32)i2);
476 +
477 +        allocbufs();                            /* allocate scanline buffers */
478 + }
479 +
480 +
481 + ra2tiff(ac, av)         /* convert Radiance picture to TIFF image */
482 + int  ac;
483 + char  *av[];
484 + {
485 +        uint32  y;
486 +                                                /* open Radiance file */
487 +        if (!strcmp(av[ac], "-"))
488 +                cvts.rfp = stdin;
489 +        else if ((cvts.rfp = fopen(av[ac], "r")) == NULL)
490 +                quiterr("cannot open Radiance input picture");
491 +                                                /* open TIFF file */
492 +        if ((cvts.tif = TIFFOpen(av[ac+1], "w")) == NULL)
493 +                quiterr("cannot open TIFF output");
494 +
495 +        initfromrad();                          /* initialize conversion */
496 +
497 +        for (y = 0; y < cvts.ymax; y++)         /* convert image */
498 +                (*cvts.tf)(y);
499                                                  /* clean up */
500 <        free((char *)scanin);
501 <        free((char *)scanout);
502 <        TIFFClose(tif);
500 >        TIFFClose(cvts.tif);
501 >        fclose(cvts.rfp);
502 > }
503 >
504 >
505 > int
506 > Luv2Color(y)                    /* read/convert/write Luv->COLOR scanline */
507 > uint32  y;
508 > {
509 >        register int    x;
510 >
511 >        if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | CHK(C_GRY))
512 >                quiterr("internal error 1 in Luv2Color");
513 >
514 >        if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
515 >                quiterr("error reading TIFF input");
516 >        
517 >        for (x = cvts.xmax; x--; ) {
518 >                colval(cvts.r.colors[x],CIEX) = cvts.t.fp[3*x];
519 >                colval(cvts.r.colors[x],CIEY) = cvts.t.fp[3*x + 1];
520 >                colval(cvts.r.colors[x],CIEZ) = cvts.t.fp[3*x + 2];
521 >                if (CHK(C_CXFM))
522 >                        colortrans(cvts.r.colors[x], cvts.cmat,
523 >                                        cvts.r.colors[x]);
524 >                if (CHK(C_GAMUT))
525 >                        clipgamut(cvts.r.colors[x], cvts.t.fp[3*x + 1],
526 >                                        CGAMUT_LOWER, cblack, cwhite);
527 >        }
528 >        if (cvts.bradj) {
529 >                double  m = pow(2.,(double)cvts.bradj);
530 >                for (x = cvts.xmax; x--; )
531 >                        scalecolor(cvts.r.colors[x], m);
532 >        }
533 >
534 >        if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
535 >                quiterr("error writing Radiance picture");
536 > }
537 >
538 >
539 > int
540 > L2Color(y)                      /* read/convert/write L16->COLOR scanline */
541 > uint32  y;
542 > {
543 >        register int    x;
544 >
545 >        if (CHK(C_RFLT|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY))
546 >                quiterr("internal error 1 in L2Color");
547 >
548 >        if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
549 >                quiterr("error reading TIFF input");
550 >        
551 >        for (x = cvts.xmax; x--; )
552 >                colval(cvts.r.colors[x],RED) =
553 >                colval(cvts.r.colors[x],GRN) =
554 >                colval(cvts.r.colors[x],BLU) =
555 >                                cvts.t.fp[x] > 0. ? cvts.t.fp[x] : 0.;
556 >
557 >        if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
558 >                quiterr("error writing Radiance picture");
559 > }
560 >
561 >
562 > int
563 > RGB2Colr(y)                     /* read/convert/write RGB->COLR scanline */
564 > uint32  y;
565 > {
566 >        COLOR   ctmp;
567 >        register int    x;
568 >
569 >        if (CHK(C_RFLT|C_TFLT|C_GRY))
570 >                quiterr("internal error 1 in RGB2Colr");
571 >
572 >        if (cvts.pconf == PLANARCONFIG_CONTIG) {
573 >                if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
574 >                        goto readerr;
575 >                for (x = cvts.xmax; x--; ) {
576 >                        cvts.r.colrs[x][RED] = cvts.t.bp[3*x];
577 >                        cvts.r.colrs[x][GRN] = cvts.t.bp[3*x + 1];
578 >                        cvts.r.colrs[x][BLU] = cvts.t.bp[3*x + 2];
579 >                }
580 >        } else {
581 >                if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
582 >                        goto readerr;
583 >                if (TIFFReadScanline(cvts.tif,
584 >                                (tdata_t)(cvts.t.bp + cvts.xmax), y, 1) < 0)
585 >                        goto readerr;
586 >                if (TIFFReadScanline(cvts.tif,
587 >                                (tdata_t)(cvts.t.bp + 2*cvts.xmax), y, 2) < 0)
588 >                        goto readerr;
589 >                for (x = cvts.xmax; x--; ) {
590 >                        cvts.r.colrs[x][RED] = cvts.t.bp[x];
591 >                        cvts.r.colrs[x][GRN] = cvts.t.bp[cvts.xmax + x];
592 >                        cvts.r.colrs[x][BLU] = cvts.t.bp[2*cvts.xmax + x];
593 >                }
594 >        }
595 >
596 >        gambs_colrs(cvts.r.colrs, cvts.xmax);
597 >        if (CHK(C_CXFM))
598 >                for (x = cvts.xmax; x--; ) {
599 >                        colr_color(ctmp, cvts.r.colrs[x]);
600 >                        colortrans(ctmp, cvts.cmat, ctmp);
601 >                        if (CHK(C_GAMUT))       /* !CHK(C_XYZE) */
602 >                                clipgamut(ctmp, bright(ctmp), CGAMUT_LOWER,
603 >                                                cblack, cwhite);
604 >                        setcolr(cvts.r.colrs[x], colval(ctmp,RED),
605 >                                        colval(ctmp,GRN), colval(ctmp,BLU));
606 >                }
607 >        if (cvts.bradj)
608 >                shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
609 >
610 >        if (fwritecolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
611 >                quiterr("error writing Radiance picture");
612          return;
613   readerr:
614          quiterr("error reading TIFF input");
615   }
616  
617  
618 < struct hdinfo {
619 <        char    *buf;           /* header buffer */
620 <        char    *pos;           /* buffer position */
621 <        char    fmt[64];        /* format type */
622 < };
618 > int
619 > Gry2Colr(y)                     /* read/convert/write G8->COLR scanline */
620 > uint32  y;
621 > {
622 >        register int    x;
623  
624 +        if (CHK(C_RFLT|C_TFLT) | !CHK(C_GRY))
625 +                quiterr("internal error 1 in Gry2Colr");
626  
627 < headline(s, hd)                 /* add header line to buffer */
628 < register char   *s;
629 < register struct hdinfo  *hd;
627 >        if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
628 >                quiterr("error reading TIFF input");
629 >
630 >        for (x = cvts.xmax; x--; )
631 >                cvts.r.colrs[x][RED] =
632 >                cvts.r.colrs[x][GRN] =
633 >                cvts.r.colrs[x][BLU] = cvts.t.bp[x];
634 >
635 >        gambs_colrs(cvts.r.colrs, cvts.xmax);
636 >        if (cvts.bradj)
637 >                shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
638 >
639 >        if (fwritecolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
640 >                quiterr("error writing Radiance picture");
641 > }
642 >
643 >
644 > int
645 > Color2L(y)                      /* read/convert/write COLOR->L16 scanline */
646 > uint32  y;
647   {
648 <        register int    i;
648 >        double  m = pow(2.,(double)cvts.bradj);
649 >        register int    x;
650  
651 <        if (isformat(s)) {
652 <                formatval(hd->fmt, s);
653 <                return;
651 >        if (CHK(C_RFLT|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY))
652 >                quiterr("internal error 1 in Color2L");
653 >
654 >        if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
655 >                quiterr("error reading Radiance picture");
656 >
657 >        for (x = cvts.xmax; x--; )
658 >                cvts.t.fp[x] = m*( CHK(C_XYZE) ? colval(cvts.r.colors[x],CIEY)
659 >                                                : bright(cvts.r.colors[x]) );
660 >
661 >        if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
662 >                quiterr("error writing TIFF output");
663 > }
664 >
665 >
666 > int
667 > Color2Luv(y)                    /* read/convert/write COLOR->Luv scanline */
668 > uint32  y;
669 > {
670 >        register int    x;
671 >
672 >        if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | CHK(C_GRY))
673 >                quiterr("internal error 1 in Color2Luv");
674 >
675 >        if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
676 >                quiterr("error reading Radiance picture");
677 >
678 >        if (CHK(C_CXFM))
679 >                for (x = cvts.xmax; x--; )
680 >                        colortrans(cvts.r.colors[x], cvts.cmat,
681 >                                        cvts.r.colors[x]);
682 >        if (cvts.bradj) {
683 >                double  m = pow(2.,(double)cvts.bradj);
684 >                for (x = cvts.xmax; x--; )
685 >                        scalecolor(cvts.r.colors[x], m);
686          }
687 <        if (hd->buf == NULL)
688 <                hd->pos = hd->buf = malloc(strlen(s)+1);
689 <        else {
690 <                i = hd->pos - hd->buf;
691 <                hd->buf = realloc(hd->buf, i+strlen(s)+1);
207 <                hd->pos = hd->buf + i;
687 >
688 >        for (x = cvts.xmax; x--; ) {
689 >                cvts.t.fp[3*x] = colval(cvts.r.colors[x],CIEX);
690 >                cvts.t.fp[3*x+1] = colval(cvts.r.colors[x],CIEY);
691 >                cvts.t.fp[3*x+2] = colval(cvts.r.colors[x],CIEZ);
692          }
693 <        if (hd->buf == NULL)
694 <                quiterr("out of memory in headline");
695 <        while (*hd->pos++ = *s++)
212 <                ;
693 >
694 >        if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
695 >                quiterr("error writing TIFF output");
696   }
697  
698  
699 < ra2tiff(inpf, outf)             /* convert Radiance file to 24-bit TIFF */
700 < char    *inpf, *outf;
699 > int
700 > Colr2Gry(y)                     /* read/convert/write COLR->RGB scanline */
701 > uint32  y;
702   {
219        char    buf[64];
220        struct hdinfo   hd;
221        TIFF    *tif;
222        int     xmax, ymax;
223        BYTE    *scanout;
224        COLR    *scanin;
703          register int    x;
704 <        int     y;
705 <                                                /* open Radiance file */
706 <        if (strcmp(inpf, "-") && freopen(inpf, "r", stdin) == NULL)
707 <                quiterr("cannot open Radiance input file");
708 <        hd.buf = NULL; hd.fmt[0] = '\0';
709 <        getheader(stdin, headline, &hd);
710 <        if (bradj)
711 <                sprintf(buf, "%s -e %+d\n", progname, bradj);
712 <        else
713 <                sprintf(buf, "%s\n", progname);
714 <        headline(buf, &hd);
715 <        if (hd.fmt[0] && strcmp(hd.fmt, COLRFMT))
716 <                quiterr("input not a Radiance picture");
717 <        if (fgetresolu(&xmax, &ymax, stdin) != (YDECR|YMAJOR))
718 <                quiterr("bad Radiance picture");
719 <                                                /* open TIFF file */
720 <        if ((tif = TIFFOpen(outf, "w")) == NULL)
721 <                quiterr("cannot open TIFF output");
722 <        TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (unsigned long)xmax);
723 <        TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (unsigned long)ymax);
724 <        TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
725 <        TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
726 <        TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, 2);
727 <        TIFFSetField(tif, TIFFTAG_PLANARCONFIG, 1);
728 <        TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, hd.buf);
729 <                                                /* allocate scanlines */
730 <        scanin = (COLR *)malloc(xmax*sizeof(COLR));
731 <        scanout = (BYTE *)malloc(TIFFScanlineSize(tif));
732 <        if (scanin == NULL || scanout == NULL)
733 <                quiterr("out of memory in ra2tiff");
734 <                                                /* convert image */
735 <        for (y = 0; y < ymax; y++) {
736 <                if (freadcolrs(scanin, xmax, stdin) < 0)
737 <                        quiterr("error reading Radiance picture");
738 <                if (bradj)
739 <                        shiftcolrs(scanin, xmax, bradj);
740 <                colrs_gambs(scanin, xmax);
741 <                for (x = 0; x < xmax; x++) {
742 <                        scanout[3*x] = scanin[x][RED];
743 <                        scanout[3*x+1] = scanin[x][GRN];
744 <                        scanout[3*x+2] = scanin[x][BLU];
704 >
705 >        if (CHK(C_RFLT|C_TFLT) | !CHK(C_GRY))
706 >                quiterr("internal error 1 in Colr2Gry");
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 >        for (x = cvts.xmax; x--; )
714 >                colval(cvts.r.colrs[x],CIEY) = normbright(cvts.r.colrs[x]);
715 >        colrs_gambs(cvts.r.colrs, cvts.xmax);
716 >
717 >        for (x = cvts.xmax; x--; )
718 >                cvts.t.bp[x] = colval(cvts.r.colrs[x],CIEY);
719 >
720 >        if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
721 >                quiterr("error writing TIFF output");
722 > }
723 >
724 >
725 > int
726 > Colr2RGB(y)                     /* read/convert/write COLR->RGB scanline */
727 > uint32  y;
728 > {
729 >        COLOR   ctmp;
730 >        register int    x;
731 >
732 >        if (CHK(C_RFLT|C_TFLT|C_GRY))
733 >                quiterr("internal error 1 in Colr2RGB");
734 >
735 >        if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
736 >                quiterr("error reading Radiance picture");
737 >
738 >        if (cvts.bradj)
739 >                shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
740 >        if (CHK(C_CXFM))
741 >                for (x = cvts.xmax; x--; ) {
742 >                        colr_color(ctmp, cvts.r.colrs[x]);
743 >                        colortrans(ctmp, cvts.cmat, ctmp);
744 >                        if (CHK(C_GAMUT))
745 >                                clipgamut(ctmp, bright(ctmp), CGAMUT,
746 >                                                cblack, cwhite);
747 >                        setcolr(cvts.r.colrs[x], colval(ctmp,RED),
748 >                                        colval(ctmp,GRN), colval(ctmp,BLU));
749                  }
750 <                if (TIFFWriteScanline(tif, scanout, y, 0) < 0)
751 <                        quiterr("error writing TIFF output");
750 >        colrs_gambs(cvts.r.colrs, cvts.xmax);
751 >
752 >        for (x = cvts.xmax; x--; ) {
753 >                cvts.t.bp[3*x] = cvts.r.colrs[x][RED];
754 >                cvts.t.bp[3*x+1] = cvts.r.colrs[x][GRN];
755 >                cvts.t.bp[3*x+2] = cvts.r.colrs[x][BLU];
756          }
757 <                                                /* clean up */
758 <        free((char *)scanin);
759 <        free((char *)scanout);
274 <        TIFFClose(tif);
275 <        free(hd.buf);
757 >
758 >        if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
759 >                quiterr("error writing TIFF output");
760   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines