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