16 |
|
#include "resolu.h" |
17 |
|
|
18 |
|
const char *cm_fmt_id[] = { |
19 |
< |
"unknown", "ascii", COLRFMT, CIEFMT, |
20 |
< |
"float", "double" |
19 |
> |
"unknown", COLRFMT, CIEFMT, |
20 |
> |
"float", "ascii", "double" |
21 |
|
}; |
22 |
|
|
23 |
|
const int cm_elem_size[] = { |
24 |
< |
0, 0, 4, 4, 3*sizeof(float), 3*sizeof(double) |
24 |
> |
0, 4, 4, 3*sizeof(float), 0, 3*sizeof(double) |
25 |
|
}; |
26 |
|
|
27 |
|
/* Allocate a color coefficient matrix */ |
81 |
|
|
82 |
|
typedef struct { |
83 |
|
int dtype; /* data type */ |
84 |
+ |
int need2swap; /* need byte swap? */ |
85 |
|
int nrows, ncols; /* matrix size */ |
86 |
+ |
COLOR expos; /* exposure value */ |
87 |
|
char *err; /* error message */ |
88 |
|
} CMINFO; /* header info record */ |
89 |
|
|
106 |
|
ip->ncols = atoi(s+6); |
107 |
|
return(0); |
108 |
|
} |
109 |
+ |
if ((i = isbigendian(s)) >= 0) { |
110 |
+ |
ip->need2swap = (nativebigendian() != i); |
111 |
+ |
return(0); |
112 |
+ |
} |
113 |
+ |
if (isexpos(s)) { |
114 |
+ |
double d = exposval(s); |
115 |
+ |
scalecolor(ip->expos, d); |
116 |
+ |
return(0); |
117 |
+ |
} |
118 |
+ |
if (iscolcor(s)) { |
119 |
+ |
COLOR ctmp; |
120 |
+ |
colcorval(ctmp, s); |
121 |
+ |
multcolor(ip->expos, ctmp); |
122 |
+ |
return(0); |
123 |
+ |
} |
124 |
|
if (!formatval(fmt, s)) |
125 |
|
return(0); |
126 |
|
for (i = 1; i < DTend; i++) |
131 |
|
|
132 |
|
/* Load header to obtain/check data type and number of columns */ |
133 |
|
char * |
134 |
< |
cm_getheader(int *dt, int *nr, int *nc, FILE *fp) |
134 |
> |
cm_getheader(int *dt, int *nr, int *nc, int *swp, COLOR scale, FILE *fp) |
135 |
|
{ |
136 |
|
CMINFO cmi; |
137 |
|
/* read header */ |
138 |
|
cmi.dtype = DTfromHeader; |
139 |
+ |
cmi.need2swap = 0; |
140 |
+ |
cmi.expos[0] = cmi.expos[1] = cmi.expos[2] = 1.f; |
141 |
|
cmi.nrows = cmi.ncols = 0; |
142 |
|
cmi.err = "unexpected EOF in header"; |
143 |
|
if (getheader(fp, get_cminfo, &cmi) < 0) |
163 |
|
else if ((cmi.ncols > 0) & (*nc != cmi.ncols)) |
164 |
|
return("unexpected column count in header"); |
165 |
|
} |
166 |
+ |
if (swp) /* get/check swap? */ |
167 |
+ |
*swp = cmi.need2swap; |
168 |
+ |
if (scale) { /* transfer exposure comp. */ |
169 |
+ |
scale[0] = 1.f/cmi.expos[0]; |
170 |
+ |
scale[1] = 1.f/cmi.expos[1]; |
171 |
+ |
scale[2] = 1.f/cmi.expos[2]; |
172 |
+ |
} |
173 |
|
return(NULL); |
174 |
|
} |
175 |
|
|
176 |
+ |
/* Allocate and load image data into matrix */ |
177 |
+ |
static CMATRIX * |
178 |
+ |
cm_load_rgbe(FILE *fp, int nrows, int ncols, COLOR scale) |
179 |
+ |
{ |
180 |
+ |
int doscale; |
181 |
+ |
CMATRIX *cm; |
182 |
+ |
COLORV *mp; |
183 |
+ |
/* header already loaded */ |
184 |
+ |
cm = cm_alloc(nrows, ncols); |
185 |
+ |
if (!cm) |
186 |
+ |
return(NULL); |
187 |
+ |
doscale = (scale[0] < .99) | (scale[0] > 1.01) | |
188 |
+ |
(scale[1] < .99) | (scale[1] > 1.01) | |
189 |
+ |
(scale[2] < .99) | (scale[2] > 1.01) ; |
190 |
+ |
mp = cm->cmem; |
191 |
+ |
while (nrows--) { |
192 |
+ |
if (freadscan((COLOR *)mp, ncols, fp) < 0) { |
193 |
+ |
error(USER, "error reading color picture as matrix"); |
194 |
+ |
cm_free(cm); |
195 |
+ |
return(NULL); |
196 |
+ |
} |
197 |
+ |
if (doscale) { |
198 |
+ |
int i = ncols; |
199 |
+ |
while (i--) { |
200 |
+ |
*mp++ *= scale[0]; |
201 |
+ |
*mp++ *= scale[1]; |
202 |
+ |
*mp++ *= scale[2]; |
203 |
+ |
} |
204 |
+ |
} else |
205 |
+ |
mp += 3*ncols; |
206 |
+ |
} /* caller closes stream */ |
207 |
+ |
return(cm); |
208 |
+ |
} |
209 |
+ |
|
210 |
|
/* Allocate and load a matrix from the given input (or stdin if NULL) */ |
211 |
|
CMATRIX * |
212 |
|
cm_load(const char *inspec, int nrows, int ncols, int dtype) |
213 |
|
{ |
214 |
|
const int ROWINC = 2048; |
215 |
|
FILE *fp = stdin; |
216 |
+ |
int swap = 0; |
217 |
+ |
COLOR scale; |
218 |
|
CMATRIX *cm; |
219 |
|
|
220 |
|
if (!inspec) |
235 |
|
if (dtype != DTascii) |
236 |
|
SET_FILE_BINARY(fp); /* doesn't really work */ |
237 |
|
if (!dtype | !ncols) { /* expecting header? */ |
238 |
< |
char *err = cm_getheader(&dtype, &nrows, &ncols, fp); |
238 |
> |
char *err = cm_getheader(&dtype, &nrows, &ncols, &swap, scale, fp); |
239 |
|
if (err) |
240 |
|
error(USER, err); |
179 |
– |
if (ncols <= 0) |
180 |
– |
error(USER, "unspecified number of columns"); |
241 |
|
} |
242 |
+ |
if (ncols <= 0 && !fscnresolu(&ncols, &nrows, fp)) |
243 |
+ |
error(USER, "unspecified number of columns"); |
244 |
|
switch (dtype) { |
245 |
|
case DTascii: |
246 |
|
case DTfloat: |
247 |
|
case DTdouble: |
248 |
|
break; |
249 |
+ |
case DTrgbe: |
250 |
+ |
case DTxyze: |
251 |
+ |
cm = cm_load_rgbe(fp, nrows, ncols, scale); |
252 |
+ |
goto cleanup; |
253 |
|
default: |
254 |
|
error(USER, "unexpected data type in cm_load()"); |
255 |
|
} |
256 |
|
if (nrows <= 0) { /* don't know length? */ |
257 |
|
int guessrows = 147; /* usually big enough */ |
258 |
< |
if ((dtype != DTascii) & (fp != stdin) & (inspec[0] != '!')) { |
258 |
> |
if (cm_elem_size[dtype] && (fp != stdin) & (inspec[0] != '!')) { |
259 |
|
long startpos = ftell(fp); |
260 |
|
if (fseek(fp, 0L, SEEK_END) == 0) { |
261 |
+ |
long rowsiz = (long)ncols*cm_elem_size[dtype]; |
262 |
|
long endpos = ftell(fp); |
196 |
– |
long elemsiz = 3*(dtype==DTfloat ? |
197 |
– |
sizeof(float) : sizeof(double)); |
263 |
|
|
264 |
< |
if ((endpos - startpos) % (ncols*elemsiz)) { |
264 |
> |
if ((endpos - startpos) % rowsiz) { |
265 |
|
sprintf(errmsg, |
266 |
|
"improper length for binary file '%s'", |
267 |
|
inspec); |
268 |
|
error(USER, errmsg); |
269 |
|
} |
270 |
< |
guessrows = (endpos - startpos)/(ncols*elemsiz); |
270 |
> |
guessrows = (endpos - startpos)/rowsiz; |
271 |
|
if (fseek(fp, startpos, SEEK_SET) < 0) { |
272 |
|
sprintf(errmsg, |
273 |
|
"fseek() error on file '%s'", |
328 |
|
goto EOFerror; |
329 |
|
} while (nread < cm->nrows*cm->ncols); |
330 |
|
|
331 |
+ |
if (swap) { |
332 |
+ |
if (sizeof(COLORV) == 4) |
333 |
+ |
swap32((char *)cm->cmem, |
334 |
+ |
3*cm->nrows*cm->ncols); |
335 |
+ |
else /* sizeof(COLORV) == 8 */ |
336 |
+ |
swap64((char *)cm->cmem, |
337 |
+ |
3*cm->nrows*cm->ncols); |
338 |
+ |
} |
339 |
|
} else if (dtype == DTdouble) { |
340 |
|
double dc[3]; /* load from double */ |
341 |
|
COLORV *cvp = cm->cmem; |
346 |
|
while (n--) { |
347 |
|
if (getbinary(dc, sizeof(double), 3, fp) != 3) |
348 |
|
goto EOFerror; |
349 |
+ |
if (swap) swap64((char *)dc, 3); |
350 |
|
copycolor(cvp, dc); |
351 |
|
cvp += 3; |
352 |
|
} |
360 |
|
while (n--) { |
361 |
|
if (getbinary(fc, sizeof(float), 3, fp) != 3) |
362 |
|
goto EOFerror; |
363 |
+ |
if (swap) swap32((char *)fc, 3); |
364 |
|
copycolor(cvp, fc); |
365 |
|
cvp += 3; |
366 |
|
} |
372 |
|
error(WARNING, errmsg); |
373 |
|
} |
374 |
|
} |
375 |
+ |
cleanup: |
376 |
|
if (fp != stdin) { |
377 |
|
if (inspec[0] != '!') |
378 |
|
fclose(fp); |
389 |
|
EOFerror: |
390 |
|
sprintf(errmsg, "unexpected EOF reading %s", inspec); |
391 |
|
error(USER, errmsg); |
392 |
+ |
return(NULL); |
393 |
|
not_handled: |
394 |
|
error(INTERNAL, "unhandled data size or length in cm_load()"); |
395 |
|
return(NULL); /* gratis return */ |