1 |
– |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
< |
* program to convert between RADIANCE and 24-bit TIFF files. |
5 |
> |
* Program to convert between RADIANCE and TIFF files. |
6 |
> |
* Added LogLuv encodings 7/97 (GWL). |
7 |
> |
* Added white-balance adjustment 10/01 (GW). |
8 |
|
*/ |
9 |
|
|
10 |
< |
#include <stdio.h> |
10 |
> |
#include <math.h> |
11 |
> |
#include <ctype.h> |
12 |
|
|
13 |
+ |
#include "rtio.h" |
14 |
+ |
#include "platform.h" |
15 |
|
#include "tiffio.h" |
14 |
– |
|
16 |
|
#include "color.h" |
17 |
+ |
#include "resolu.h" |
18 |
|
|
19 |
< |
extern double atof(); |
19 |
> |
#define GAMCOR 2.2 /* default gamma */ |
20 |
|
|
21 |
< |
extern char *malloc(), *realloc(); |
21 |
> |
/* conversion flags */ |
22 |
> |
#define C_CXFM 0x1 /* needs color transformation */ |
23 |
> |
#define C_GAMUT 0x2 /* needs gamut mapping */ |
24 |
> |
#define C_GAMMA 0x4 /* needs gamma correction */ |
25 |
> |
#define C_GRY 0x8 /* TIFF is greyscale */ |
26 |
> |
#define C_XYZE 0x10 /* Radiance is XYZE */ |
27 |
> |
#define C_RFLT 0x20 /* Radiance data is float */ |
28 |
> |
#define C_TFLT 0x40 /* TIFF data is float */ |
29 |
> |
#define C_TWRD 0x80 /* TIFF data is 16-bit */ |
30 |
> |
#define C_PRIM 0x100 /* has assigned primaries */ |
31 |
|
|
32 |
< |
double gamma = 2.2; /* gamma correction */ |
32 |
> |
typedef void colcvf_t(uint32 y); |
33 |
|
|
34 |
< |
int bradj = 0; /* brightness adjustment */ |
34 |
> |
struct { |
35 |
> |
uint16 flags; /* conversion flags (defined above) */ |
36 |
> |
char capdate[20]; /* capture date/time */ |
37 |
> |
char owner[256]; /* content owner */ |
38 |
> |
uint16 comp; /* TIFF compression type */ |
39 |
> |
uint16 phot; /* TIFF photometric type */ |
40 |
> |
uint16 pconf; /* TIFF planar configuration */ |
41 |
> |
float gamcor; /* gamma correction value */ |
42 |
> |
short bradj; /* Radiance exposure adjustment (stops) */ |
43 |
> |
uint16 orient; /* visual orientation (TIFF spec.) */ |
44 |
> |
double stonits; /* input conversion to nits */ |
45 |
> |
float pixrat; /* pixel aspect ratio */ |
46 |
> |
FILE *rfp; /* Radiance stream pointer */ |
47 |
> |
TIFF *tif; /* TIFF pointer */ |
48 |
> |
uint32 xmax, ymax; /* image dimensions */ |
49 |
> |
COLORMAT cmat; /* color transformation matrix */ |
50 |
> |
RGBPRIMS prims; /* RGB primaries */ |
51 |
> |
union { |
52 |
> |
COLR *colrs; /* 4-byte ???E pointer */ |
53 |
> |
COLOR *colors; /* float array pointer */ |
54 |
> |
char *p; /* generic pointer */ |
55 |
> |
} r; /* Radiance scanline */ |
56 |
> |
union { |
57 |
> |
uint8 *bp; /* byte pointer */ |
58 |
> |
uint16 *wp; /* word pointer */ |
59 |
> |
float *fp; /* float pointer */ |
60 |
> |
char *p; /* generic pointer */ |
61 |
> |
} t; /* TIFF scanline */ |
62 |
> |
colcvf_t *tf; /* translation procedure */ |
63 |
> |
} cvts = { /* conversion structure */ |
64 |
> |
0, "", "", COMPRESSION_NONE, PHOTOMETRIC_RGB, |
65 |
> |
PLANARCONFIG_CONTIG, GAMCOR, 0, 1, 1., 1., |
66 |
> |
}; |
67 |
|
|
68 |
+ |
#define CHK(f) (cvts.flags & (f)) |
69 |
+ |
#define SET(f) (cvts.flags |= (f)) |
70 |
+ |
#define CLR(f) (cvts.flags &= ~(f)) |
71 |
+ |
#define TGL(f) (cvts.flags ^= (f)) |
72 |
+ |
|
73 |
+ |
static colcvf_t Luv2Color, L2Color, RGB2Colr, Gry2Colr; |
74 |
+ |
static colcvf_t Color2Luv, Color2L, Colr2RGB, Colr2Gry; |
75 |
+ |
static colcvf_t RRGGBB2Color, GGry2Color, Color2RRGGBB, Color2GGry; |
76 |
+ |
|
77 |
+ |
static gethfunc headline; |
78 |
+ |
static void quiterr(char *err); |
79 |
+ |
static void allocbufs(void); |
80 |
+ |
static void initfromtif(void); |
81 |
+ |
static void tiff2ra(int ac, char *av[]); |
82 |
+ |
static void initfromrad(void); |
83 |
+ |
static void ra2tiff(int ac, char *av[]); |
84 |
+ |
|
85 |
+ |
|
86 |
+ |
|
87 |
+ |
#define RfGfBf2Color Luv2Color |
88 |
+ |
#define Gryf2Color L2Color |
89 |
+ |
#define Color2Gryf Color2L |
90 |
+ |
#define Color2RfGfBf Color2Luv |
91 |
+ |
|
92 |
+ |
short ortab[8] = { /* orientation conversion table */ |
93 |
+ |
YMAJOR|YDECR, |
94 |
+ |
YMAJOR|YDECR|XDECR, |
95 |
+ |
YMAJOR|XDECR, |
96 |
+ |
YMAJOR, |
97 |
+ |
YDECR, |
98 |
+ |
XDECR|YDECR, |
99 |
+ |
XDECR, |
100 |
+ |
0 |
101 |
+ |
}; |
102 |
+ |
|
103 |
+ |
#define pixorder() ortab[cvts.orient-1] |
104 |
+ |
|
105 |
+ |
extern char TMSTR[]; /* "CAPDATE=" from header.c */ |
106 |
+ |
char OWNSTR[] = "OWNER="; |
107 |
+ |
|
108 |
|
char *progname; |
109 |
|
|
110 |
|
|
111 |
< |
main(argc, argv) |
112 |
< |
int argc; |
30 |
< |
char *argv[]; |
111 |
> |
int |
112 |
> |
main(int argc, char *argv[]) |
113 |
|
{ |
114 |
|
int reverse = 0; |
115 |
|
int i; |
119 |
|
for (i = 1; i < argc; i++) |
120 |
|
if (argv[i][0] == '-') |
121 |
|
switch (argv[i][1]) { |
122 |
< |
/* not allowed to reset gamma... |
123 |
< |
case 'g': |
42 |
< |
gamma = atof(argv[++i]); |
122 |
> |
case 'g': /* gamma correction */ |
123 |
> |
cvts.gamcor = atof(argv[++i]); |
124 |
|
break; |
125 |
< |
*/ |
126 |
< |
case 'e': |
125 |
> |
case 'x': /* XYZE Radiance output */ |
126 |
> |
TGL(C_XYZE); |
127 |
> |
break; |
128 |
> |
case 'z': /* LZW compressed output */ |
129 |
> |
cvts.comp = COMPRESSION_LZW; |
130 |
> |
break; |
131 |
> |
case 'L': /* LogLuv 32-bit output */ |
132 |
> |
cvts.comp = COMPRESSION_SGILOG; |
133 |
> |
cvts.phot = PHOTOMETRIC_LOGLUV; |
134 |
> |
break; |
135 |
> |
case 'l': /* LogLuv 24-bit output */ |
136 |
> |
cvts.comp = COMPRESSION_SGILOG24; |
137 |
> |
cvts.phot = PHOTOMETRIC_LOGLUV; |
138 |
> |
break; |
139 |
> |
case 'w': /* 16-bit/primary output? */ |
140 |
> |
TGL(C_TWRD); |
141 |
> |
break; |
142 |
> |
case 'f': /* IEEE float output? */ |
143 |
> |
TGL(C_TFLT); |
144 |
> |
break; |
145 |
> |
case 'b': /* greyscale output? */ |
146 |
> |
TGL(C_GRY); |
147 |
> |
break; |
148 |
> |
case 'e': /* exposure adjustment */ |
149 |
|
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
150 |
|
goto userr; |
151 |
< |
bradj = atoi(argv[++i]); |
151 |
> |
cvts.bradj = atoi(argv[++i]); |
152 |
|
break; |
153 |
< |
case 'r': |
153 |
> |
case 'r': /* reverse conversion? */ |
154 |
|
reverse = !reverse; |
155 |
|
break; |
156 |
|
case '\0': |
161 |
|
else |
162 |
|
break; |
163 |
|
doneopts: |
164 |
< |
setcolrgam(gamma); |
164 |
> |
if (reverse) { |
165 |
|
|
63 |
– |
if (reverse) |
166 |
|
if (i != argc-2 && i != argc-1) |
167 |
|
goto userr; |
168 |
< |
else |
169 |
< |
tiff2ra(argv[i], argv[i+1]); |
170 |
< |
else |
168 |
> |
|
169 |
> |
tiff2ra(i, argv); |
170 |
> |
|
171 |
> |
} else { |
172 |
> |
|
173 |
|
if (i != argc-2) |
174 |
|
goto userr; |
175 |
< |
else |
176 |
< |
ra2tiff(argv[i], argv[i+1]); |
175 |
> |
/* consistency checks */ |
176 |
> |
if (CHK(C_GRY)) { |
177 |
> |
if (cvts.phot == PHOTOMETRIC_RGB) |
178 |
> |
cvts.phot = PHOTOMETRIC_MINISBLACK; |
179 |
> |
else { |
180 |
> |
cvts.phot = PHOTOMETRIC_LOGL; |
181 |
> |
cvts.comp = COMPRESSION_SGILOG; |
182 |
> |
} |
183 |
> |
} |
184 |
> |
if (CHK(C_TWRD|C_TFLT) == (C_TWRD|C_TFLT)) |
185 |
> |
goto userr; |
186 |
|
|
187 |
+ |
ra2tiff(i, argv); |
188 |
+ |
} |
189 |
+ |
|
190 |
|
exit(0); |
191 |
|
userr: |
192 |
< |
fprintf(stderr, "Usage: %s [-r][-e +/-stops] input output\n", |
192 |
> |
fprintf(stderr, |
193 |
> |
"Usage: %s [-z|-L|-l|-f|-w][-b][-e +/-stops][-g gamma] {in.hdr|-} out.tif\n", |
194 |
|
progname); |
195 |
+ |
fprintf(stderr, |
196 |
+ |
" Or: %s -r [-x][-e +/-stops][-g gamma] in.tif [out.hdr|-]\n", |
197 |
+ |
progname); |
198 |
|
exit(1); |
199 |
|
} |
200 |
|
|
201 |
|
|
202 |
< |
quiterr(err) /* print message and exit */ |
203 |
< |
char *err; |
202 |
> |
static void |
203 |
> |
quiterr( /* print message and exit */ |
204 |
> |
char *err |
205 |
> |
) |
206 |
|
{ |
207 |
|
if (err != NULL) { |
208 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
212 |
|
} |
213 |
|
|
214 |
|
|
215 |
< |
tiff2ra(inpf, outf) /* convert TIFF file to Radiance picture */ |
216 |
< |
char *inpf, *outf; |
215 |
> |
static void |
216 |
> |
allocbufs(void) /* allocate scanline buffers */ |
217 |
|
{ |
218 |
< |
unsigned long xmax, ymax; |
219 |
< |
TIFF *tif; |
220 |
< |
unsigned short pconfig; |
221 |
< |
unsigned short hi; |
222 |
< |
register BYTE *scanin; |
223 |
< |
register COLR *scanout; |
224 |
< |
register int x; |
225 |
< |
int y; |
226 |
< |
/* open/check TIFF file */ |
227 |
< |
if ((tif = TIFFOpen(inpf, "r")) == NULL) |
228 |
< |
quiterr("cannot open TIFF input"); |
229 |
< |
if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &hi) || hi != 3) |
230 |
< |
quiterr("unsupported samples per pixel"); |
231 |
< |
if (!TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &hi) || hi != 8) |
232 |
< |
quiterr("unsupported bits per sample"); |
233 |
< |
if (TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &hi) && hi != 2) |
234 |
< |
quiterr("unsupported photometric interpretation"); |
235 |
< |
if (!TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &pconfig) || |
236 |
< |
(pconfig != 1 && pconfig != 2)) |
237 |
< |
quiterr("unsupported planar configuration"); |
238 |
< |
if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &xmax) || |
239 |
< |
!TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &ymax)) |
218 |
> |
int rsiz, tsiz; |
219 |
> |
|
220 |
> |
rsiz = CHK(C_RFLT) ? sizeof(COLOR) : sizeof(COLR); |
221 |
> |
tsiz = (CHK(C_TFLT) ? sizeof(float) : |
222 |
> |
CHK(C_TWRD) ? sizeof(uint16) : sizeof(uint8)) * |
223 |
> |
(CHK(C_GRY) ? 1 : 3); |
224 |
> |
cvts.r.p = (char *)malloc(rsiz*cvts.xmax); |
225 |
> |
cvts.t.p = (char *)malloc(tsiz*cvts.xmax); |
226 |
> |
if ((cvts.r.p == NULL) | (cvts.t.p == NULL)) |
227 |
> |
quiterr("no memory to allocate scanline buffers"); |
228 |
> |
} |
229 |
> |
|
230 |
> |
|
231 |
> |
static void |
232 |
> |
initfromtif(void) /* initialize conversion from TIFF input */ |
233 |
> |
{ |
234 |
> |
uint16 hi; |
235 |
> |
char *cp; |
236 |
> |
float *fa, f1, f2; |
237 |
> |
|
238 |
> |
CLR(C_GRY|C_GAMMA|C_PRIM|C_RFLT|C_TFLT|C_TWRD|C_CXFM); |
239 |
> |
|
240 |
> |
TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_PLANARCONFIG, &cvts.pconf); |
241 |
> |
|
242 |
> |
if (TIFFGetField(cvts.tif, TIFFTAG_PRIMARYCHROMATICITIES, &fa)) { |
243 |
> |
cvts.prims[RED][CIEX] = fa[0]; |
244 |
> |
cvts.prims[RED][CIEY] = fa[1]; |
245 |
> |
cvts.prims[GRN][CIEX] = fa[2]; |
246 |
> |
cvts.prims[GRN][CIEY] = fa[3]; |
247 |
> |
cvts.prims[BLU][CIEX] = fa[4]; |
248 |
> |
cvts.prims[BLU][CIEY] = fa[5]; |
249 |
> |
cvts.prims[WHT][CIEX] = 1./3.; |
250 |
> |
cvts.prims[WHT][CIEY] = 1./3.; |
251 |
> |
if (TIFFGetField(cvts.tif, TIFFTAG_WHITEPOINT, &fa)) { |
252 |
> |
cvts.prims[WHT][CIEX] = fa[0]; |
253 |
> |
cvts.prims[WHT][CIEY] = fa[1]; |
254 |
> |
} |
255 |
> |
SET(C_PRIM); |
256 |
> |
} |
257 |
> |
|
258 |
> |
if (!TIFFGetField(cvts.tif, TIFFTAG_COMPRESSION, &cvts.comp)) |
259 |
> |
cvts.comp = COMPRESSION_NONE; |
260 |
> |
|
261 |
> |
if (TIFFGetField(cvts.tif, TIFFTAG_XRESOLUTION, &f1) && |
262 |
> |
TIFFGetField(cvts.tif, TIFFTAG_YRESOLUTION, &f2)) |
263 |
> |
cvts.pixrat = f1/f2; |
264 |
> |
|
265 |
> |
TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_ORIENTATION, &cvts.orient); |
266 |
> |
|
267 |
> |
if (!TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_PHOTOMETRIC, &cvts.phot)) |
268 |
> |
quiterr("TIFF has unspecified photometric type"); |
269 |
> |
|
270 |
> |
switch (cvts.phot) { |
271 |
> |
case PHOTOMETRIC_LOGLUV: |
272 |
> |
SET(C_RFLT|C_TFLT); |
273 |
> |
if (!CHK(C_XYZE)) { |
274 |
> |
cpcolormat(cvts.cmat, xyz2rgbmat); |
275 |
> |
SET(C_CXFM|C_GAMUT); |
276 |
> |
} else if (cvts.comp == COMPRESSION_SGILOG) |
277 |
> |
SET(C_GAMUT); /* may be outside XYZ gamut */ |
278 |
> |
if (cvts.pconf != PLANARCONFIG_CONTIG) |
279 |
> |
quiterr("cannot handle separate Luv planes"); |
280 |
> |
TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT, |
281 |
> |
SGILOGDATAFMT_FLOAT); |
282 |
> |
cvts.tf = Luv2Color; |
283 |
> |
break; |
284 |
> |
case PHOTOMETRIC_LOGL: |
285 |
> |
SET(C_GRY|C_RFLT|C_TFLT|C_GAMUT); |
286 |
> |
cvts.pconf = PLANARCONFIG_CONTIG; |
287 |
> |
TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT, |
288 |
> |
SGILOGDATAFMT_FLOAT); |
289 |
> |
cvts.tf = L2Color; |
290 |
> |
break; |
291 |
> |
case PHOTOMETRIC_YCBCR: |
292 |
> |
if (cvts.comp == COMPRESSION_JPEG && |
293 |
> |
cvts.pconf == PLANARCONFIG_CONTIG) { |
294 |
> |
TIFFSetField(cvts.tif, TIFFTAG_JPEGCOLORMODE, |
295 |
> |
JPEGCOLORMODE_RGB); |
296 |
> |
cvts.phot = PHOTOMETRIC_RGB; |
297 |
> |
} else |
298 |
> |
quiterr("unsupported photometric type"); |
299 |
> |
/* fall through */ |
300 |
> |
case PHOTOMETRIC_RGB: |
301 |
> |
SET(C_GAMMA); |
302 |
> |
setcolrgam(cvts.gamcor); |
303 |
> |
if (CHK(C_XYZE)) { |
304 |
> |
comprgb2xyzWBmat(cvts.cmat, |
305 |
> |
CHK(C_PRIM) ? cvts.prims : stdprims); |
306 |
> |
SET(C_CXFM); |
307 |
> |
} |
308 |
> |
if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) || |
309 |
> |
hi != 3) |
310 |
> |
quiterr("unsupported samples per pixel for RGB"); |
311 |
> |
if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi)) |
312 |
> |
hi = -1; |
313 |
> |
switch (hi) { |
314 |
> |
case 8: |
315 |
> |
cvts.tf = RGB2Colr; |
316 |
> |
break; |
317 |
> |
case 16: |
318 |
> |
cvts.tf = RRGGBB2Color; |
319 |
> |
SET(C_RFLT|C_TWRD); |
320 |
> |
break; |
321 |
> |
case 32: |
322 |
> |
cvts.tf = RfGfBf2Color; |
323 |
> |
SET(C_RFLT|C_TFLT); |
324 |
> |
break; |
325 |
> |
default: |
326 |
> |
quiterr("unsupported bits per sample for RGB"); |
327 |
> |
} |
328 |
> |
break; |
329 |
> |
case PHOTOMETRIC_MINISBLACK: |
330 |
> |
SET(C_GRY|C_GAMMA); |
331 |
> |
setcolrgam(cvts.gamcor); |
332 |
> |
cvts.pconf = PLANARCONFIG_CONTIG; |
333 |
> |
if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) || |
334 |
> |
hi != 1) |
335 |
> |
quiterr("unsupported samples per pixel for greyscale"); |
336 |
> |
if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi)) |
337 |
> |
hi = -1; |
338 |
> |
switch (hi) { |
339 |
> |
case 8: |
340 |
> |
cvts.tf = Gry2Colr; |
341 |
> |
break; |
342 |
> |
case 16: |
343 |
> |
cvts.tf = GGry2Color; |
344 |
> |
SET(C_RFLT|C_TWRD); |
345 |
> |
break; |
346 |
> |
case 32: |
347 |
> |
cvts.tf = Gryf2Color; |
348 |
> |
SET(C_RFLT|C_TFLT); |
349 |
> |
break; |
350 |
> |
default: |
351 |
> |
quiterr("unsupported bits per sample for Gray"); |
352 |
> |
} |
353 |
> |
break; |
354 |
> |
default: |
355 |
> |
quiterr("unsupported photometric type"); |
356 |
> |
break; |
357 |
> |
} |
358 |
> |
|
359 |
> |
if (!TIFFGetField(cvts.tif, TIFFTAG_IMAGEWIDTH, &cvts.xmax) || |
360 |
> |
!TIFFGetField(cvts.tif, TIFFTAG_IMAGELENGTH, &cvts.ymax)) |
361 |
|
quiterr("unknown input image resolution"); |
362 |
< |
/* allocate scanlines */ |
363 |
< |
scanin = (BYTE *)malloc(TIFFScanlineSize(tif)); |
364 |
< |
scanout = (COLR *)malloc(xmax*sizeof(COLR)); |
365 |
< |
if (scanin == NULL || scanout == NULL) |
366 |
< |
quiterr("out of memory in tiff2ra"); |
367 |
< |
/* open output and write header */ |
368 |
< |
if (outf != NULL && strcmp(outf, "-") && |
369 |
< |
freopen(outf, "w", stdout) == NULL) |
370 |
< |
quiterr("cannot open Radiance output file"); |
371 |
< |
fputs(progname, stdout); |
372 |
< |
if (bradj) |
373 |
< |
printf(" -e %+d", bradj); |
374 |
< |
fputs(" -r\n", stdout); |
375 |
< |
fputformat(COLRFMT, stdout); |
376 |
< |
putchar('\n'); |
377 |
< |
fputresolu(YDECR|YMAJOR, xmax, ymax, stdout); |
378 |
< |
/* convert image */ |
379 |
< |
for (y = 0; y < ymax; y++) { |
380 |
< |
if (pconfig == 1) { |
381 |
< |
if (TIFFReadScanline(tif, scanin, y, 0) < 0) |
382 |
< |
goto readerr; |
383 |
< |
for (x = 0; x < xmax; x++) { |
384 |
< |
scanout[x][RED] = scanin[3*x]; |
385 |
< |
scanout[x][GRN] = scanin[3*x+1]; |
386 |
< |
scanout[x][BLU] = scanin[3*x+2]; |
387 |
< |
} |
388 |
< |
} else { |
389 |
< |
if (TIFFReadScanline(tif, scanin, y, 0) < 0) |
390 |
< |
goto readerr; |
391 |
< |
for (x = 0; x < xmax; x++) |
392 |
< |
scanout[x][RED] = scanin[x]; |
393 |
< |
if (TIFFReadScanline(tif, scanin, y, 1) < 0) |
394 |
< |
goto readerr; |
395 |
< |
for (x = 0; x < xmax; x++) |
396 |
< |
scanout[x][GRN] = scanin[x]; |
397 |
< |
if (TIFFReadScanline(tif, scanin, y, 2) < 0) |
398 |
< |
goto readerr; |
399 |
< |
for (x = 0; x < xmax; x++) |
400 |
< |
scanout[x][BLU] = scanin[x]; |
362 |
> |
|
363 |
> |
if (!TIFFGetField(cvts.tif, TIFFTAG_STONITS, &cvts.stonits)) |
364 |
> |
cvts.stonits = -1.; |
365 |
> |
|
366 |
> |
if (!TIFFGetField(cvts.tif, TIFFTAG_DATETIME, &cp)) |
367 |
> |
cvts.capdate[0] = '\0'; |
368 |
> |
else { |
369 |
> |
strncpy(cvts.capdate, cp, 19); |
370 |
> |
cvts.capdate[19] = '\0'; |
371 |
> |
} |
372 |
> |
if (!TIFFGetField(cvts.tif, TIFFTAG_ARTIST, &cp)) |
373 |
> |
cvts.owner[0] = '\0'; |
374 |
> |
else { |
375 |
> |
strncpy(cvts.owner, cp, sizeof(cvts.owner)); |
376 |
> |
cvts.owner[sizeof(cvts.owner)-1] = '\0'; |
377 |
> |
} |
378 |
> |
/* add to Radiance header */ |
379 |
> |
if (cvts.pixrat < .99 || cvts.pixrat > 1.01) |
380 |
> |
fputaspect(cvts.pixrat, cvts.rfp); |
381 |
> |
if (CHK(C_XYZE)) { |
382 |
> |
if (cvts.stonits > .0) |
383 |
> |
fputexpos(pow(2.,(double)cvts.bradj)/cvts.stonits, cvts.rfp); |
384 |
> |
fputformat(CIEFMT, cvts.rfp); |
385 |
> |
} else { |
386 |
> |
if (CHK(C_PRIM)) |
387 |
> |
fputprims(cvts.prims, cvts.rfp); |
388 |
> |
if (cvts.stonits > .0) |
389 |
> |
fputexpos(WHTEFFICACY*pow(2.,(double)cvts.bradj)/cvts.stonits, |
390 |
> |
cvts.rfp); |
391 |
> |
fputformat(COLRFMT, cvts.rfp); |
392 |
> |
} |
393 |
> |
if (cvts.capdate[0]) |
394 |
> |
fprintf(cvts.rfp, "%s %s\n", TMSTR, cvts.capdate); |
395 |
> |
if (cvts.owner[0]) |
396 |
> |
fprintf(cvts.rfp, "%s %s\n", OWNSTR, cvts.owner); |
397 |
> |
|
398 |
> |
allocbufs(); /* allocate scanline buffers */ |
399 |
> |
} |
400 |
> |
|
401 |
> |
|
402 |
> |
static void |
403 |
> |
tiff2ra( /* convert TIFF image to Radiance picture */ |
404 |
> |
int ac, |
405 |
> |
char *av[] |
406 |
> |
) |
407 |
> |
{ |
408 |
> |
int32 y; |
409 |
> |
/* open TIFF input */ |
410 |
> |
if ((cvts.tif = TIFFOpen(av[ac], "r")) == NULL) |
411 |
> |
quiterr("cannot open TIFF input"); |
412 |
> |
/* open Radiance output */ |
413 |
> |
if (av[ac+1] == NULL || !strcmp(av[ac+1], "-")) |
414 |
> |
cvts.rfp = stdout; |
415 |
> |
else if ((cvts.rfp = fopen(av[ac+1], "w")) == NULL) |
416 |
> |
quiterr("cannot open Radiance output picture"); |
417 |
> |
SET_FILE_BINARY(cvts.rfp); |
418 |
> |
/* start output header */ |
419 |
> |
newheader("RADIANCE", cvts.rfp); |
420 |
> |
printargs(ac, av, cvts.rfp); |
421 |
> |
|
422 |
> |
initfromtif(); /* initialize conversion */ |
423 |
> |
|
424 |
> |
fputc('\n', cvts.rfp); /* finish Radiance header */ |
425 |
> |
fputresolu(pixorder(), (int)cvts.xmax, (int)cvts.ymax, cvts.rfp); |
426 |
> |
|
427 |
> |
for (y = 0; y < cvts.ymax; y++) /* convert image */ |
428 |
> |
(*cvts.tf)(y); |
429 |
> |
/* clean up */ |
430 |
> |
fclose(cvts.rfp); |
431 |
> |
TIFFClose(cvts.tif); |
432 |
> |
} |
433 |
> |
|
434 |
> |
|
435 |
> |
static int |
436 |
> |
headline( /* process Radiance input header line */ |
437 |
> |
char *s, |
438 |
> |
void *p |
439 |
> |
) |
440 |
> |
{ |
441 |
> |
static int tmstrlen = 0; |
442 |
> |
static int ownstrlen = 0; |
443 |
> |
char fmt[MAXFMTLEN]; |
444 |
> |
|
445 |
> |
if (!tmstrlen) |
446 |
> |
tmstrlen = strlen(TMSTR); |
447 |
> |
if (!ownstrlen) |
448 |
> |
ownstrlen = strlen(OWNSTR); |
449 |
> |
if (formatval(fmt, s)) { |
450 |
> |
if (!strcmp(fmt, COLRFMT)) |
451 |
> |
CLR(C_XYZE); |
452 |
> |
else if (!strcmp(fmt, CIEFMT)) |
453 |
> |
SET(C_XYZE); |
454 |
> |
else |
455 |
> |
quiterr("unrecognized input picture format"); |
456 |
> |
return(1); |
457 |
> |
} |
458 |
> |
if (isexpos(s)) { |
459 |
> |
cvts.stonits /= exposval(s); |
460 |
> |
return(1); |
461 |
> |
} |
462 |
> |
if (isaspect(s)) { |
463 |
> |
cvts.pixrat *= aspectval(s); |
464 |
> |
return(1); |
465 |
> |
} |
466 |
> |
if (isprims(s)) { |
467 |
> |
primsval(cvts.prims, s); |
468 |
> |
SET(C_PRIM); |
469 |
> |
return(1); |
470 |
> |
} |
471 |
> |
if (isdate(s)) { |
472 |
> |
if (s[tmstrlen] == ' ') |
473 |
> |
strncpy(cvts.capdate, s+tmstrlen+1, 19); |
474 |
> |
else |
475 |
> |
strncpy(cvts.capdate, s+tmstrlen, 19); |
476 |
> |
cvts.capdate[19] = '\0'; |
477 |
> |
return(1); |
478 |
> |
} |
479 |
> |
if (!strncmp(s, OWNSTR, ownstrlen)) { |
480 |
> |
register char *cp = s + ownstrlen; |
481 |
> |
|
482 |
> |
while (isspace(*cp)) |
483 |
> |
++cp; |
484 |
> |
strncpy(cvts.owner, cp, sizeof(cvts.owner)); |
485 |
> |
cvts.owner[sizeof(cvts.owner)-1] = '\0'; |
486 |
> |
for (cp = cvts.owner; *cp; cp++) |
487 |
> |
; |
488 |
> |
while (cp > cvts.owner && isspace(cp[-1])) |
489 |
> |
*--cp = '\0'; |
490 |
> |
return(1); |
491 |
> |
} |
492 |
> |
return(0); |
493 |
> |
} |
494 |
> |
|
495 |
> |
|
496 |
> |
static void |
497 |
> |
initfromrad(void) /* initialize input from a Radiance picture */ |
498 |
> |
{ |
499 |
> |
int i1, i2, po; |
500 |
> |
/* read Radiance header */ |
501 |
> |
CLR(C_RFLT|C_XYZE|C_PRIM|C_GAMMA|C_CXFM); |
502 |
> |
cvts.capdate[0] = '\0'; |
503 |
> |
cvts.owner[0] = '\0'; |
504 |
> |
cvts.stonits = 1.; |
505 |
> |
cvts.pixrat = 1.; |
506 |
> |
cvts.pconf = PLANARCONFIG_CONTIG; |
507 |
> |
getheader(cvts.rfp, headline, NULL); |
508 |
> |
if ((po = fgetresolu(&i1, &i2, cvts.rfp)) < 0) |
509 |
> |
quiterr("bad Radiance picture"); |
510 |
> |
cvts.xmax = i1; cvts.ymax = i2; |
511 |
> |
for (i1 = 0; i1 < 8; i1++) /* interpret orientation */ |
512 |
> |
if (ortab[i1] == po) { |
513 |
> |
cvts.orient = i1 + 1; |
514 |
> |
break; |
515 |
|
} |
516 |
< |
gambs_colrs(scanout, xmax); |
517 |
< |
if (bradj) |
518 |
< |
shiftcolrs(scanout, xmax, bradj); |
519 |
< |
if (fwritecolrs(scanout, xmax, stdout) < 0) |
520 |
< |
quiterr("error writing Radiance picture"); |
516 |
> |
if (i1 >= 8) |
517 |
> |
quiterr("internal error 1 in initfromrad"); |
518 |
> |
if (!(po & YMAJOR)) |
519 |
> |
cvts.pixrat = 1./cvts.pixrat; |
520 |
> |
if (!CHK(C_XYZE)) |
521 |
> |
cvts.stonits *= WHTEFFICACY; |
522 |
> |
/* set up conversion */ |
523 |
> |
TIFFSetField(cvts.tif, TIFFTAG_COMPRESSION, cvts.comp); |
524 |
> |
TIFFSetField(cvts.tif, TIFFTAG_PHOTOMETRIC, cvts.phot); |
525 |
> |
|
526 |
> |
switch (cvts.phot) { |
527 |
> |
case PHOTOMETRIC_LOGLUV: |
528 |
> |
SET(C_RFLT|C_TFLT); |
529 |
> |
CLR(C_GRY|C_TWRD); |
530 |
> |
if (!CHK(C_XYZE)) { |
531 |
> |
comprgb2xyzWBmat(cvts.cmat, |
532 |
> |
CHK(C_PRIM) ? cvts.prims : stdprims); |
533 |
> |
SET(C_CXFM); |
534 |
> |
} |
535 |
> |
if (cvts.comp != COMPRESSION_SGILOG && |
536 |
> |
cvts.comp != COMPRESSION_SGILOG24) |
537 |
> |
quiterr("internal error 2 in initfromrad"); |
538 |
> |
TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT, |
539 |
> |
SGILOGDATAFMT_FLOAT); |
540 |
> |
cvts.tf = Color2Luv; |
541 |
> |
break; |
542 |
> |
case PHOTOMETRIC_LOGL: |
543 |
> |
SET(C_GRY|C_RFLT|C_TFLT); |
544 |
> |
CLR(C_TWRD); |
545 |
> |
if (cvts.comp != COMPRESSION_SGILOG) |
546 |
> |
quiterr("internal error 3 in initfromrad"); |
547 |
> |
TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT, |
548 |
> |
SGILOGDATAFMT_FLOAT); |
549 |
> |
cvts.tf = Color2L; |
550 |
> |
break; |
551 |
> |
case PHOTOMETRIC_RGB: |
552 |
> |
SET(C_GAMMA|C_GAMUT); |
553 |
> |
CLR(C_GRY); |
554 |
> |
setcolrgam(cvts.gamcor); |
555 |
> |
if (CHK(C_XYZE)) { |
556 |
> |
compxyz2rgbWBmat(cvts.cmat, |
557 |
> |
CHK(C_PRIM) ? cvts.prims : stdprims); |
558 |
> |
SET(C_CXFM); |
559 |
> |
} |
560 |
> |
if (CHK(C_PRIM)) { |
561 |
> |
TIFFSetField(cvts.tif, TIFFTAG_PRIMARYCHROMATICITIES, |
562 |
> |
(float *)cvts.prims); |
563 |
> |
TIFFSetField(cvts.tif, TIFFTAG_WHITEPOINT, |
564 |
> |
(float *)cvts.prims[WHT]); |
565 |
> |
} |
566 |
> |
if (CHK(C_TWRD)) { |
567 |
> |
cvts.tf = Color2RRGGBB; |
568 |
> |
SET(C_RFLT); |
569 |
> |
} else if (CHK(C_TFLT)) { |
570 |
> |
TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT, |
571 |
> |
SAMPLEFORMAT_IEEEFP); |
572 |
> |
cvts.tf = Color2RfGfBf; |
573 |
> |
SET(C_RFLT); |
574 |
> |
CLR(C_GAMUT); |
575 |
> |
} else |
576 |
> |
cvts.tf = Colr2RGB; |
577 |
> |
break; |
578 |
> |
case PHOTOMETRIC_MINISBLACK: |
579 |
> |
SET(C_GRY|C_GAMMA|C_GAMUT); |
580 |
> |
setcolrgam(cvts.gamcor); |
581 |
> |
if (CHK(C_TWRD)) { |
582 |
> |
cvts.tf = Color2GGry; |
583 |
> |
SET(C_RFLT); |
584 |
> |
} else if (CHK(C_TFLT)) { |
585 |
> |
TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT, |
586 |
> |
SAMPLEFORMAT_IEEEFP); |
587 |
> |
cvts.tf = Color2Gryf; |
588 |
> |
SET(C_RFLT); |
589 |
> |
} else |
590 |
> |
cvts.tf = Colr2Gry; |
591 |
> |
break; |
592 |
> |
default: |
593 |
> |
quiterr("internal error 4 in initfromrad"); |
594 |
> |
break; |
595 |
|
} |
596 |
+ |
/* set other TIFF fields */ |
597 |
+ |
TIFFSetField(cvts.tif, TIFFTAG_IMAGEWIDTH, cvts.xmax); |
598 |
+ |
TIFFSetField(cvts.tif, TIFFTAG_IMAGELENGTH, cvts.ymax); |
599 |
+ |
TIFFSetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, CHK(C_GRY) ? 1 : 3); |
600 |
+ |
TIFFSetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, |
601 |
+ |
CHK(C_TFLT) ? 32 : CHK(C_TWRD) ? 16 : 8); |
602 |
+ |
TIFFSetField(cvts.tif, TIFFTAG_XRESOLUTION, 72.); |
603 |
+ |
TIFFSetField(cvts.tif, TIFFTAG_YRESOLUTION, 72./cvts.pixrat); |
604 |
+ |
TIFFSetField(cvts.tif, TIFFTAG_ORIENTATION, cvts.orient); |
605 |
+ |
TIFFSetField(cvts.tif, TIFFTAG_RESOLUTIONUNIT, 2); |
606 |
+ |
TIFFSetField(cvts.tif, TIFFTAG_PLANARCONFIG, cvts.pconf); |
607 |
+ |
TIFFSetField(cvts.tif, TIFFTAG_STONITS, |
608 |
+ |
cvts.stonits/pow(2.,(double)cvts.bradj)); |
609 |
+ |
if (cvts.capdate[0]) |
610 |
+ |
TIFFSetField(cvts.tif, TIFFTAG_DATETIME, cvts.capdate); |
611 |
+ |
if (cvts.owner[0]) |
612 |
+ |
TIFFSetField(cvts.tif, TIFFTAG_ARTIST, cvts.owner); |
613 |
+ |
if (cvts.comp == COMPRESSION_NONE) |
614 |
+ |
i1 = TIFFScanlineSize(cvts.tif); |
615 |
+ |
else |
616 |
+ |
i1 = 3*cvts.xmax; /* conservative guess */ |
617 |
+ |
i2 = 8192/i1; /* compute good strip size */ |
618 |
+ |
if (i2 < 1) i2 = 1; |
619 |
+ |
TIFFSetField(cvts.tif, TIFFTAG_ROWSPERSTRIP, (uint32)i2); |
620 |
+ |
|
621 |
+ |
allocbufs(); /* allocate scanline buffers */ |
622 |
+ |
} |
623 |
+ |
|
624 |
+ |
|
625 |
+ |
static void |
626 |
+ |
ra2tiff( /* convert Radiance picture to TIFF image */ |
627 |
+ |
int ac, |
628 |
+ |
char *av[] |
629 |
+ |
) |
630 |
+ |
{ |
631 |
+ |
uint32 y; |
632 |
+ |
/* open Radiance file */ |
633 |
+ |
if (!strcmp(av[ac], "-")) |
634 |
+ |
cvts.rfp = stdin; |
635 |
+ |
else if ((cvts.rfp = fopen(av[ac], "r")) == NULL) |
636 |
+ |
quiterr("cannot open Radiance input picture"); |
637 |
+ |
SET_FILE_BINARY(cvts.rfp); |
638 |
+ |
/* open TIFF file */ |
639 |
+ |
if ((cvts.tif = TIFFOpen(av[ac+1], "w")) == NULL) |
640 |
+ |
quiterr("cannot open TIFF output"); |
641 |
+ |
|
642 |
+ |
initfromrad(); /* initialize conversion */ |
643 |
+ |
|
644 |
+ |
for (y = 0; y < cvts.ymax; y++) /* convert image */ |
645 |
+ |
(*cvts.tf)(y); |
646 |
|
/* clean up */ |
647 |
< |
free((char *)scanin); |
648 |
< |
free((char *)scanout); |
649 |
< |
TIFFClose(tif); |
647 |
> |
TIFFClose(cvts.tif); |
648 |
> |
fclose(cvts.rfp); |
649 |
> |
} |
650 |
> |
|
651 |
> |
|
652 |
> |
static void |
653 |
> |
Luv2Color( /* read/convert/write Luv->COLOR scanline */ |
654 |
> |
uint32 y |
655 |
> |
) |
656 |
> |
{ |
657 |
> |
register int x; |
658 |
> |
|
659 |
> |
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT)) |
660 |
> |
quiterr("internal error 1 in Luv2Color"); |
661 |
> |
|
662 |
> |
if (cvts.pconf != PLANARCONFIG_CONTIG) |
663 |
> |
quiterr("cannot handle separate 32-bit color planes"); |
664 |
> |
|
665 |
> |
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
666 |
> |
quiterr("error reading TIFF input"); |
667 |
> |
/* also works for float RGB */ |
668 |
> |
for (x = cvts.xmax; x--; ) { |
669 |
> |
setcolor(cvts.r.colors[x], |
670 |
> |
cvts.t.fp[3*x], |
671 |
> |
cvts.t.fp[3*x + 1], |
672 |
> |
cvts.t.fp[3*x + 2]); |
673 |
> |
if (CHK(C_CXFM)) |
674 |
> |
colortrans(cvts.r.colors[x], cvts.cmat, |
675 |
> |
cvts.r.colors[x]); |
676 |
> |
if (CHK(C_GAMUT)) |
677 |
> |
clipgamut(cvts.r.colors[x], cvts.t.fp[3*x + 1], |
678 |
> |
CGAMUT_LOWER, cblack, cwhite); |
679 |
> |
} |
680 |
> |
if (cvts.bradj) { |
681 |
> |
double m = pow(2.,(double)cvts.bradj); |
682 |
> |
for (x = cvts.xmax; x--; ) |
683 |
> |
scalecolor(cvts.r.colors[x], m); |
684 |
> |
} |
685 |
> |
|
686 |
> |
if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) |
687 |
> |
quiterr("error writing Radiance picture"); |
688 |
> |
} |
689 |
> |
|
690 |
> |
|
691 |
> |
static void |
692 |
> |
RRGGBB2Color( /* read/convert/write RGB16->COLOR scanline */ |
693 |
> |
uint32 y |
694 |
> |
) |
695 |
> |
{ |
696 |
> |
int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01); |
697 |
> |
register double d; |
698 |
> |
register int x; |
699 |
> |
|
700 |
> |
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_TWRD|C_RFLT)) |
701 |
> |
quiterr("internal error 1 in RRGGBB2Color"); |
702 |
> |
|
703 |
> |
if (cvts.pconf != PLANARCONFIG_CONTIG) |
704 |
> |
quiterr("cannot handle separate 16-bit color planes"); |
705 |
> |
|
706 |
> |
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
707 |
> |
quiterr("error reading TIFF input"); |
708 |
> |
|
709 |
> |
for (x = cvts.xmax; x--; ) { |
710 |
> |
d = (cvts.t.wp[3*x] + 0.5)*(1./(1L<<16)); |
711 |
> |
if (dogamma) d = pow(d, cvts.gamcor); |
712 |
> |
colval(cvts.r.colors[x],RED) = d; |
713 |
> |
d = (cvts.t.wp[3*x + 1] + 0.5)*(1./(1L<<16)); |
714 |
> |
if (dogamma) d = pow(d, cvts.gamcor); |
715 |
> |
colval(cvts.r.colors[x],GRN) = d; |
716 |
> |
d = (cvts.t.wp[3*x + 2] + 0.5)*(1./(1L<<16)); |
717 |
> |
if (dogamma) d = pow(d, cvts.gamcor); |
718 |
> |
colval(cvts.r.colors[x],BLU) = d; |
719 |
> |
if (CHK(C_CXFM)) |
720 |
> |
colortrans(cvts.r.colors[x], cvts.cmat, |
721 |
> |
cvts.r.colors[x]); |
722 |
> |
} |
723 |
> |
if (cvts.bradj) { |
724 |
> |
d = pow(2.,(double)cvts.bradj); |
725 |
> |
for (x = cvts.xmax; x--; ) |
726 |
> |
scalecolor(cvts.r.colors[x], d); |
727 |
> |
} |
728 |
> |
|
729 |
> |
if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) |
730 |
> |
quiterr("error writing Radiance picture"); |
731 |
> |
} |
732 |
> |
|
733 |
> |
|
734 |
> |
static void |
735 |
> |
L2Color( /* read/convert/write Lfloat->COLOR scanline */ |
736 |
> |
uint32 y |
737 |
> |
) |
738 |
> |
{ |
739 |
> |
float m = pow(2., (double)cvts.bradj); |
740 |
> |
register int x; |
741 |
> |
|
742 |
> |
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY)) |
743 |
> |
quiterr("internal error 1 in L2Color"); |
744 |
> |
|
745 |
> |
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
746 |
> |
quiterr("error reading TIFF input"); |
747 |
> |
/* also works for float greyscale */ |
748 |
> |
for (x = cvts.xmax; x--; ) { |
749 |
> |
register float f = cvts.t.fp[x]; |
750 |
> |
if (cvts.bradj) f *= m; |
751 |
> |
setcolor(cvts.r.colors[x], f, f, f); |
752 |
> |
} |
753 |
> |
if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) |
754 |
> |
quiterr("error writing Radiance picture"); |
755 |
> |
} |
756 |
> |
|
757 |
> |
|
758 |
> |
static void |
759 |
> |
RGB2Colr( /* read/convert/write RGB->COLR scanline */ |
760 |
> |
uint32 y |
761 |
> |
) |
762 |
> |
{ |
763 |
> |
COLOR ctmp; |
764 |
> |
register int x; |
765 |
> |
|
766 |
> |
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY)) |
767 |
> |
quiterr("internal error 1 in RGB2Colr"); |
768 |
> |
|
769 |
> |
if (cvts.pconf == PLANARCONFIG_CONTIG) { |
770 |
> |
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
771 |
> |
goto readerr; |
772 |
> |
for (x = cvts.xmax; x--; ) { |
773 |
> |
cvts.r.colrs[x][RED] = cvts.t.bp[3*x]; |
774 |
> |
cvts.r.colrs[x][GRN] = cvts.t.bp[3*x + 1]; |
775 |
> |
cvts.r.colrs[x][BLU] = cvts.t.bp[3*x + 2]; |
776 |
> |
} |
777 |
> |
} else { |
778 |
> |
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
779 |
> |
goto readerr; |
780 |
> |
if (TIFFReadScanline(cvts.tif, |
781 |
> |
(tdata_t)(cvts.t.bp + cvts.xmax), y, 1) < 0) |
782 |
> |
goto readerr; |
783 |
> |
if (TIFFReadScanline(cvts.tif, |
784 |
> |
(tdata_t)(cvts.t.bp + 2*cvts.xmax), y, 2) < 0) |
785 |
> |
goto readerr; |
786 |
> |
for (x = cvts.xmax; x--; ) { |
787 |
> |
cvts.r.colrs[x][RED] = cvts.t.bp[x]; |
788 |
> |
cvts.r.colrs[x][GRN] = cvts.t.bp[cvts.xmax + x]; |
789 |
> |
cvts.r.colrs[x][BLU] = cvts.t.bp[2*cvts.xmax + x]; |
790 |
> |
} |
791 |
> |
} |
792 |
> |
|
793 |
> |
gambs_colrs(cvts.r.colrs, cvts.xmax); |
794 |
> |
if (CHK(C_CXFM)) |
795 |
> |
for (x = cvts.xmax; x--; ) { |
796 |
> |
colr_color(ctmp, cvts.r.colrs[x]); |
797 |
> |
colortrans(ctmp, cvts.cmat, ctmp); |
798 |
> |
if (CHK(C_GAMUT)) /* !CHK(C_XYZE) */ |
799 |
> |
clipgamut(ctmp, bright(ctmp), CGAMUT_LOWER, |
800 |
> |
cblack, cwhite); |
801 |
> |
setcolr(cvts.r.colrs[x], colval(ctmp,RED), |
802 |
> |
colval(ctmp,GRN), colval(ctmp,BLU)); |
803 |
> |
} |
804 |
> |
if (cvts.bradj) |
805 |
> |
shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj); |
806 |
> |
|
807 |
> |
if (fwritecolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0) |
808 |
> |
quiterr("error writing Radiance picture"); |
809 |
|
return; |
810 |
|
readerr: |
811 |
|
quiterr("error reading TIFF input"); |
812 |
|
} |
813 |
|
|
814 |
|
|
815 |
< |
ra2tiff(inpf, outf) /* convert Radiance file to 24-bit TIFF */ |
816 |
< |
char *inpf, *outf; |
815 |
> |
static void |
816 |
> |
Gry2Colr( /* read/convert/write G8->COLR scanline */ |
817 |
> |
uint32 y |
818 |
> |
) |
819 |
|
{ |
178 |
– |
TIFF *tif; |
179 |
– |
int xmax, ymax; |
180 |
– |
BYTE *scanout; |
181 |
– |
COLR *scanin; |
820 |
|
register int x; |
821 |
< |
int y; |
822 |
< |
/* open Radiance file */ |
823 |
< |
if (strcmp(inpf, "-") && freopen(inpf, "r", stdin) == NULL) |
824 |
< |
quiterr("cannot open Radiance input file"); |
825 |
< |
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
826 |
< |
fgetresolu(&xmax, &ymax, stdin) != (YDECR|YMAJOR)) |
827 |
< |
quiterr("bad Radiance picture"); |
828 |
< |
/* open TIFF file */ |
829 |
< |
if ((tif = TIFFOpen(outf, "w")) == NULL) |
830 |
< |
quiterr("cannot open TIFF output"); |
831 |
< |
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (unsigned long)xmax); |
832 |
< |
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (unsigned long)ymax); |
833 |
< |
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3); |
834 |
< |
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); |
835 |
< |
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, 2); |
836 |
< |
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, 1); |
837 |
< |
/* allocate scanlines */ |
838 |
< |
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
839 |
< |
scanout = (BYTE *)malloc(TIFFScanlineSize(tif)); |
840 |
< |
if (scanin == NULL || scanout == NULL) |
841 |
< |
quiterr("out of memory in ra2tiff"); |
842 |
< |
/* convert image */ |
843 |
< |
for (y = 0; y < ymax; y++) { |
844 |
< |
if (freadcolrs(scanin, xmax, stdin) < 0) |
845 |
< |
quiterr("error reading Radiance picture"); |
846 |
< |
if (bradj) |
847 |
< |
shiftcolrs(scanin, xmax, bradj); |
848 |
< |
colrs_gambs(scanin, xmax); |
849 |
< |
for (x = 0; x < xmax; x++) { |
850 |
< |
scanout[3*x] = scanin[x][RED]; |
851 |
< |
scanout[3*x+1] = scanin[x][GRN]; |
852 |
< |
scanout[3*x+2] = scanin[x][BLU]; |
821 |
> |
|
822 |
> |
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != C_GRY) |
823 |
> |
quiterr("internal error 1 in Gry2Colr"); |
824 |
> |
|
825 |
> |
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
826 |
> |
quiterr("error reading TIFF input"); |
827 |
> |
|
828 |
> |
for (x = cvts.xmax; x--; ) |
829 |
> |
cvts.r.colrs[x][RED] = |
830 |
> |
cvts.r.colrs[x][GRN] = |
831 |
> |
cvts.r.colrs[x][BLU] = cvts.t.bp[x]; |
832 |
> |
|
833 |
> |
gambs_colrs(cvts.r.colrs, cvts.xmax); |
834 |
> |
if (cvts.bradj) |
835 |
> |
shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj); |
836 |
> |
|
837 |
> |
if (fwritecolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0) |
838 |
> |
quiterr("error writing Radiance picture"); |
839 |
> |
} |
840 |
> |
|
841 |
> |
|
842 |
> |
static void |
843 |
> |
GGry2Color( /* read/convert/write G16->COLOR scanline */ |
844 |
> |
uint32 y |
845 |
> |
) |
846 |
> |
{ |
847 |
> |
int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01); |
848 |
> |
double m; |
849 |
> |
register double d; |
850 |
> |
register int x; |
851 |
> |
|
852 |
> |
if (CHK(C_TFLT|C_TWRD|C_GRY|C_RFLT) != (C_GRY|C_RFLT|C_TWRD)) |
853 |
> |
quiterr("internal error 1 in GGry2Color"); |
854 |
> |
|
855 |
> |
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
856 |
> |
quiterr("error reading TIFF input"); |
857 |
> |
|
858 |
> |
if (cvts.bradj) |
859 |
> |
m = pow(2., (double)cvts.bradj); |
860 |
> |
for (x = cvts.xmax; x--; ) { |
861 |
> |
d = (cvts.t.wp[x] + 0.5)*(1./(1L<<16)); |
862 |
> |
if (dogamma) d = pow(d, cvts.gamcor); |
863 |
> |
if (cvts.bradj) d *= m; |
864 |
> |
colval(cvts.r.colors[x],RED) = |
865 |
> |
colval(cvts.r.colors[x],GRN) = |
866 |
> |
colval(cvts.r.colors[x],BLU) = d; |
867 |
> |
} |
868 |
> |
if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) |
869 |
> |
quiterr("error writing Radiance picture"); |
870 |
> |
} |
871 |
> |
|
872 |
> |
|
873 |
> |
static void |
874 |
> |
Color2GGry( /* read/convert/write COLOR->G16 scanline */ |
875 |
> |
uint32 y |
876 |
> |
) |
877 |
> |
{ |
878 |
> |
int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01); |
879 |
> |
float m = pow(2.,(double)cvts.bradj); |
880 |
> |
register int x; |
881 |
> |
|
882 |
> |
if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TWRD|C_GRY)) |
883 |
> |
quiterr("internal error 1 in Color2GGry"); |
884 |
> |
|
885 |
> |
if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) |
886 |
> |
quiterr("error reading Radiance picture"); |
887 |
> |
|
888 |
> |
for (x = cvts.xmax; x--; ) { |
889 |
> |
register float f = m*( CHK(C_XYZE) ? |
890 |
> |
colval(cvts.r.colors[x],CIEY) |
891 |
> |
: bright(cvts.r.colors[x]) ); |
892 |
> |
if (f <= 0) |
893 |
> |
cvts.t.wp[x] = 0; |
894 |
> |
else if (f >= 1) |
895 |
> |
cvts.t.wp[x] = 0xffff; |
896 |
> |
else if (dogamma) |
897 |
> |
cvts.t.wp[x] = (int)((float)(1L<<16) * |
898 |
> |
pow(f, 1./cvts.gamcor)); |
899 |
> |
else |
900 |
> |
cvts.t.wp[x] = (int)((float)(1L<<16) * f); |
901 |
> |
} |
902 |
> |
|
903 |
> |
if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
904 |
> |
quiterr("error writing TIFF output"); |
905 |
> |
} |
906 |
> |
|
907 |
> |
|
908 |
> |
static void |
909 |
> |
Color2L( /* read/convert/write COLOR->Lfloat scanline */ |
910 |
> |
uint32 y |
911 |
> |
) |
912 |
> |
{ |
913 |
> |
float m = pow(2.,(double)cvts.bradj); |
914 |
> |
register int x; |
915 |
> |
|
916 |
> |
if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TFLT|C_GRY)) |
917 |
> |
quiterr("internal error 1 in Color2L"); |
918 |
> |
|
919 |
> |
if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) |
920 |
> |
quiterr("error reading Radiance picture"); |
921 |
> |
|
922 |
> |
for (x = cvts.xmax; x--; ) |
923 |
> |
cvts.t.fp[x] = m*( CHK(C_XYZE) ? colval(cvts.r.colors[x],CIEY) |
924 |
> |
: bright(cvts.r.colors[x]) ); |
925 |
> |
|
926 |
> |
if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
927 |
> |
quiterr("error writing TIFF output"); |
928 |
> |
} |
929 |
> |
|
930 |
> |
|
931 |
> |
static void |
932 |
> |
Color2Luv( /* read/convert/write COLOR->Luv scanline */ |
933 |
> |
uint32 y |
934 |
> |
) |
935 |
> |
{ |
936 |
> |
register int x; |
937 |
> |
|
938 |
> |
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT)) |
939 |
> |
quiterr("internal error 1 in Color2Luv"); |
940 |
> |
|
941 |
> |
if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) |
942 |
> |
quiterr("error reading Radiance picture"); |
943 |
> |
|
944 |
> |
if (CHK(C_CXFM)) |
945 |
> |
for (x = cvts.xmax; x--; ) |
946 |
> |
colortrans(cvts.r.colors[x], cvts.cmat, |
947 |
> |
cvts.r.colors[x]); |
948 |
> |
if (cvts.bradj) { |
949 |
> |
double m = pow(2.,(double)cvts.bradj); |
950 |
> |
for (x = cvts.xmax; x--; ) |
951 |
> |
scalecolor(cvts.r.colors[x], m); |
952 |
> |
} |
953 |
> |
/* also works for float RGB */ |
954 |
> |
for (x = cvts.xmax; x--; ) { |
955 |
> |
cvts.t.fp[3*x] = colval(cvts.r.colors[x],CIEX); |
956 |
> |
cvts.t.fp[3*x+1] = colval(cvts.r.colors[x],CIEY); |
957 |
> |
cvts.t.fp[3*x+2] = colval(cvts.r.colors[x],CIEZ); |
958 |
> |
} |
959 |
> |
|
960 |
> |
if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
961 |
> |
quiterr("error writing TIFF output"); |
962 |
> |
} |
963 |
> |
|
964 |
> |
|
965 |
> |
static void |
966 |
> |
Color2RRGGBB( /* read/convert/write COLOR->RGB16 scanline */ |
967 |
> |
uint32 y |
968 |
> |
) |
969 |
> |
{ |
970 |
> |
int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01); |
971 |
> |
float m = pow(2.,(double)cvts.bradj); |
972 |
> |
register int x, i; |
973 |
> |
|
974 |
> |
if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TWRD)) |
975 |
> |
quiterr("internal error 1 in Color2RRGGBB"); |
976 |
> |
|
977 |
> |
if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) |
978 |
> |
quiterr("error reading Radiance picture"); |
979 |
> |
|
980 |
> |
for (x = cvts.xmax; x--; ) { |
981 |
> |
if (CHK(C_CXFM)) { |
982 |
> |
colortrans(cvts.r.colors[x], cvts.cmat, |
983 |
> |
cvts.r.colors[x]); |
984 |
> |
if (CHK(C_GAMUT)) |
985 |
> |
clipgamut(cvts.r.colors[x], bright(cvts.r.colors[x]), |
986 |
> |
CGAMUT_LOWER, cblack, cwhite); |
987 |
> |
} |
988 |
> |
for (i = 3; i--; ) { |
989 |
> |
register float f = m*colval(cvts.r.colors[x],i); |
990 |
> |
if (f <= 0) |
991 |
> |
cvts.t.wp[3*x + i] = 0; |
992 |
> |
else if (f >= 1) |
993 |
> |
cvts.t.wp[3*x + i] = 0xffff; |
994 |
> |
else if (dogamma) |
995 |
> |
cvts.t.wp[3*x + i] = (int)((float)(1L<<16) * |
996 |
> |
pow(f, 1./cvts.gamcor)); |
997 |
> |
else |
998 |
> |
cvts.t.wp[3*x + i] = (int)((float)(1L<<16)*f); |
999 |
> |
} |
1000 |
> |
} |
1001 |
> |
|
1002 |
> |
if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
1003 |
> |
quiterr("error writing TIFF output"); |
1004 |
> |
} |
1005 |
> |
|
1006 |
> |
|
1007 |
> |
static void |
1008 |
> |
Colr2Gry( /* read/convert/write COLR->RGB scanline */ |
1009 |
> |
uint32 y |
1010 |
> |
) |
1011 |
> |
{ |
1012 |
> |
register int x; |
1013 |
> |
|
1014 |
> |
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != C_GRY) |
1015 |
> |
quiterr("internal error 1 in Colr2Gry"); |
1016 |
> |
|
1017 |
> |
if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0) |
1018 |
> |
quiterr("error reading Radiance picture"); |
1019 |
> |
|
1020 |
> |
if (cvts.bradj) |
1021 |
> |
shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj); |
1022 |
> |
for (x = cvts.xmax; x--; ) |
1023 |
> |
colval(cvts.r.colrs[x],CIEY) = normbright(cvts.r.colrs[x]); |
1024 |
> |
colrs_gambs(cvts.r.colrs, cvts.xmax); |
1025 |
> |
|
1026 |
> |
for (x = cvts.xmax; x--; ) |
1027 |
> |
cvts.t.bp[x] = colval(cvts.r.colrs[x],CIEY); |
1028 |
> |
|
1029 |
> |
if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
1030 |
> |
quiterr("error writing TIFF output"); |
1031 |
> |
} |
1032 |
> |
|
1033 |
> |
|
1034 |
> |
static void |
1035 |
> |
Colr2RGB( /* read/convert/write COLR->RGB scanline */ |
1036 |
> |
uint32 y |
1037 |
> |
) |
1038 |
> |
{ |
1039 |
> |
COLOR ctmp; |
1040 |
> |
register int x; |
1041 |
> |
|
1042 |
> |
if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY)) |
1043 |
> |
quiterr("internal error 1 in Colr2RGB"); |
1044 |
> |
|
1045 |
> |
if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0) |
1046 |
> |
quiterr("error reading Radiance picture"); |
1047 |
> |
|
1048 |
> |
if (cvts.bradj) |
1049 |
> |
shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj); |
1050 |
> |
if (CHK(C_CXFM)) |
1051 |
> |
for (x = cvts.xmax; x--; ) { |
1052 |
> |
colr_color(ctmp, cvts.r.colrs[x]); |
1053 |
> |
colortrans(ctmp, cvts.cmat, ctmp); |
1054 |
> |
if (CHK(C_GAMUT)) |
1055 |
> |
clipgamut(ctmp, bright(ctmp), CGAMUT, |
1056 |
> |
cblack, cwhite); |
1057 |
> |
setcolr(cvts.r.colrs[x], colval(ctmp,RED), |
1058 |
> |
colval(ctmp,GRN), colval(ctmp,BLU)); |
1059 |
|
} |
1060 |
< |
if (TIFFWriteScanline(tif, scanout, y, 0) < 0) |
1061 |
< |
quiterr("error writing TIFF output"); |
1060 |
> |
colrs_gambs(cvts.r.colrs, cvts.xmax); |
1061 |
> |
|
1062 |
> |
for (x = cvts.xmax; x--; ) { |
1063 |
> |
cvts.t.bp[3*x] = cvts.r.colrs[x][RED]; |
1064 |
> |
cvts.t.bp[3*x+1] = cvts.r.colrs[x][GRN]; |
1065 |
> |
cvts.t.bp[3*x+2] = cvts.r.colrs[x][BLU]; |
1066 |
|
} |
1067 |
< |
/* clean up */ |
1068 |
< |
free((char *)scanin); |
1069 |
< |
free((char *)scanout); |
222 |
< |
TIFFClose(tif); |
1067 |
> |
|
1068 |
> |
if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
1069 |
> |
quiterr("error writing TIFF output"); |
1070 |
|
} |