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