ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_tiff.c
Revision: 1.2
Committed: Thu Aug 15 13:54:08 1991 UTC (32 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.1: +2 -55 lines
Log Message:
took out use of image description (sniff!)

File Contents

# User Rev Content
1 greg 1.1 /* Copyright (c) 1991 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * program to convert between RADIANCE and 24-bit TIFF files.
9     */
10    
11     #include <stdio.h>
12    
13     #include "tiffio.h"
14    
15     #include "color.h"
16    
17     extern double atof();
18    
19     extern char *malloc(), *realloc();
20    
21     double gamma = 2.2; /* gamma correction */
22    
23     int bradj = 0; /* brightness adjustment */
24    
25     char *progname;
26    
27    
28     main(argc, argv)
29     int argc;
30     char *argv[];
31     {
32     int reverse = 0;
33     int i;
34    
35     progname = argv[0];
36    
37     for (i = 1; i < argc; i++)
38     if (argv[i][0] == '-')
39     switch (argv[i][1]) {
40     /* not allowed to reset gamma...
41     case 'g':
42     gamma = atof(argv[++i]);
43     break;
44     */
45     case 'e':
46     if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
47     goto userr;
48     bradj = atoi(argv[++i]);
49     break;
50     case 'r':
51     reverse = !reverse;
52     break;
53     case '\0':
54     goto doneopts;
55     default:
56     goto userr;
57     }
58     else
59     break;
60     doneopts:
61     setcolrgam(gamma);
62    
63     if (reverse)
64     if (i != argc-2 && i != argc-1)
65     goto userr;
66     else
67     tiff2ra(argv[i], argv[i+1]);
68     else
69     if (i != argc-2)
70     goto userr;
71     else
72     ra2tiff(argv[i], argv[i+1]);
73    
74     exit(0);
75     userr:
76     fprintf(stderr, "Usage: %s [-r][-e +/-stops] input output\n",
77     progname);
78     exit(1);
79     }
80    
81    
82     quiterr(err) /* print message and exit */
83     char *err;
84     {
85     if (err != NULL) {
86     fprintf(stderr, "%s: %s\n", progname, err);
87     exit(1);
88     }
89     exit(0);
90     }
91    
92    
93     tiff2ra(inpf, outf) /* convert TIFF file to Radiance picture */
94     char *inpf, *outf;
95     {
96     unsigned long xmax, ymax;
97     TIFF *tif;
98     unsigned short pconfig;
99     unsigned short hi;
100     register BYTE *scanin;
101     register COLR *scanout;
102     register int x;
103     int y;
104     /* open/check TIFF file */
105     if ((tif = TIFFOpen(inpf, "r")) == NULL)
106     quiterr("cannot open TIFF input");
107     if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &hi) || hi != 3)
108     quiterr("unsupported samples per pixel");
109     if (!TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &hi) || hi != 8)
110     quiterr("unsupported bits per sample");
111     if (TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &hi) && hi != 2)
112     quiterr("unsupported photometric interpretation");
113     if (!TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &pconfig) ||
114     (pconfig != 1 && pconfig != 2))
115     quiterr("unsupported planar configuration");
116     if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &xmax) ||
117     !TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &ymax))
118     quiterr("unknown input image resolution");
119     /* allocate scanlines */
120     scanin = (BYTE *)malloc(TIFFScanlineSize(tif));
121     scanout = (COLR *)malloc(xmax*sizeof(COLR));
122     if (scanin == NULL || scanout == NULL)
123     quiterr("out of memory in tiff2ra");
124     /* open output and write header */
125     if (outf != NULL && strcmp(outf, "-") &&
126     freopen(outf, "w", stdout) == NULL)
127     quiterr("cannot open Radiance output file");
128     fputs(progname, stdout);
129     if (bradj)
130     printf(" -e %+d", bradj);
131     fputs(" -r\n", stdout);
132     fputformat(COLRFMT, stdout);
133     putchar('\n');
134     fputresolu(YDECR|YMAJOR, xmax, ymax, stdout);
135     /* convert image */
136     for (y = 0; y < ymax; y++) {
137     if (pconfig == 1) {
138     if (TIFFReadScanline(tif, scanin, y, 0) < 0)
139     goto readerr;
140     for (x = 0; x < xmax; x++) {
141     scanout[x][RED] = scanin[3*x];
142     scanout[x][GRN] = scanin[3*x+1];
143     scanout[x][BLU] = scanin[3*x+2];
144     }
145     } else {
146     if (TIFFReadScanline(tif, scanin, y, 0) < 0)
147     goto readerr;
148     for (x = 0; x < xmax; x++)
149     scanout[x][RED] = scanin[x];
150     if (TIFFReadScanline(tif, scanin, y, 1) < 0)
151     goto readerr;
152     for (x = 0; x < xmax; x++)
153     scanout[x][GRN] = scanin[x];
154     if (TIFFReadScanline(tif, scanin, y, 2) < 0)
155     goto readerr;
156     for (x = 0; x < xmax; x++)
157     scanout[x][BLU] = scanin[x];
158     }
159     gambs_colrs(scanout, xmax);
160     if (bradj)
161     shiftcolrs(scanout, xmax, bradj);
162     if (fwritecolrs(scanout, xmax, stdout) < 0)
163     quiterr("error writing Radiance picture");
164     }
165     /* clean up */
166     free((char *)scanin);
167     free((char *)scanout);
168     TIFFClose(tif);
169     return;
170     readerr:
171     quiterr("error reading TIFF input");
172     }
173    
174    
175     ra2tiff(inpf, outf) /* convert Radiance file to 24-bit TIFF */
176     char *inpf, *outf;
177     {
178     TIFF *tif;
179     int xmax, ymax;
180     BYTE *scanout;
181     COLR *scanin;
182     register int x;
183     int y;
184     /* open Radiance file */
185     if (strcmp(inpf, "-") && freopen(inpf, "r", stdin) == NULL)
186     quiterr("cannot open Radiance input file");
187 greg 1.2 if (checkheader(stdin, COLRFMT, NULL) < 0 ||
188     fgetresolu(&xmax, &ymax, stdin) != (YDECR|YMAJOR))
189 greg 1.1 quiterr("bad Radiance picture");
190     /* open TIFF file */
191     if ((tif = TIFFOpen(outf, "w")) == NULL)
192     quiterr("cannot open TIFF output");
193     TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (unsigned long)xmax);
194     TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (unsigned long)ymax);
195     TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
196     TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
197     TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, 2);
198     TIFFSetField(tif, TIFFTAG_PLANARCONFIG, 1);
199     /* allocate scanlines */
200     scanin = (COLR *)malloc(xmax*sizeof(COLR));
201     scanout = (BYTE *)malloc(TIFFScanlineSize(tif));
202     if (scanin == NULL || scanout == NULL)
203     quiterr("out of memory in ra2tiff");
204     /* convert image */
205     for (y = 0; y < ymax; y++) {
206     if (freadcolrs(scanin, xmax, stdin) < 0)
207     quiterr("error reading Radiance picture");
208     if (bradj)
209     shiftcolrs(scanin, xmax, bradj);
210     colrs_gambs(scanin, xmax);
211     for (x = 0; x < xmax; x++) {
212     scanout[3*x] = scanin[x][RED];
213     scanout[3*x+1] = scanin[x][GRN];
214     scanout[3*x+2] = scanin[x][BLU];
215     }
216     if (TIFFWriteScanline(tif, scanout, y, 0) < 0)
217     quiterr("error writing TIFF output");
218     }
219     /* clean up */
220     free((char *)scanin);
221     free((char *)scanout);
222     TIFFClose(tif);
223     }