ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_tiff.c
Revision: 1.3
Committed: Thu Aug 15 14:04:21 1991 UTC (32 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.2: +0 -2 lines
Log Message:
added back gamma control (though not properly registered)

File Contents

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