15 |
|
#include "paths.h" |
16 |
|
#include "resolu.h" |
17 |
|
|
18 |
+ |
const char stdin_name[] = "<stdin>"; |
19 |
+ |
|
20 |
|
const char *cm_fmt_id[] = { |
21 |
< |
"unknown", "ascii", COLRFMT, CIEFMT, |
22 |
< |
"float", "double" |
21 |
> |
"unknown", COLRFMT, CIEFMT, |
22 |
> |
"float", "ascii", "double" |
23 |
|
}; |
24 |
|
|
25 |
|
const int cm_elem_size[] = { |
26 |
< |
0, 0, 4, 4, 3*sizeof(float), 3*sizeof(double) |
26 |
> |
0, 4, 4, 3*sizeof(float), 0, 3*sizeof(double) |
27 |
|
}; |
28 |
|
|
29 |
|
/* Allocate a color coefficient matrix */ |
35 |
|
if ((nrows <= 0) | (ncols <= 0)) |
36 |
|
error(USER, "attempt to create empty matrix"); |
37 |
|
cm = (CMATRIX *)malloc(sizeof(CMATRIX) + |
38 |
< |
sizeof(COLOR)*(nrows*ncols - 1)); |
38 |
> |
sizeof(COLOR)*((size_t)nrows*ncols - 1)); |
39 |
|
if (!cm) |
40 |
|
error(SYSTEM, "out of memory in cm_alloc()"); |
41 |
|
cm->nrows = nrows; |
67 |
|
cm_free(cm); |
68 |
|
return(NULL); |
69 |
|
} |
70 |
< |
old_size = sizeof(CMATRIX) + sizeof(COLOR)*(cm->nrows*cm->ncols - 1); |
70 |
> |
old_size = sizeof(CMATRIX) + sizeof(COLOR)*((size_t)cm->nrows*cm->ncols - 1); |
71 |
|
adjacent_ra_sizes(ra_bounds, old_size); |
72 |
< |
new_size = sizeof(CMATRIX) + sizeof(COLOR)*(nrows*cm->ncols - 1); |
72 |
> |
new_size = sizeof(CMATRIX) + sizeof(COLOR)*((size_t)nrows*cm->ncols - 1); |
73 |
|
if (nrows < cm->nrows ? new_size <= ra_bounds[0] : |
74 |
|
new_size > ra_bounds[1]) { |
75 |
|
adjacent_ra_sizes(ra_bounds, new_size); |
85 |
|
int dtype; /* data type */ |
86 |
|
int need2swap; /* need byte swap? */ |
87 |
|
int nrows, ncols; /* matrix size */ |
88 |
+ |
COLOR expos; /* exposure value */ |
89 |
|
char *err; /* error message */ |
90 |
|
} CMINFO; /* header info record */ |
91 |
|
|
112 |
|
ip->need2swap = (nativebigendian() != i); |
113 |
|
return(0); |
114 |
|
} |
115 |
+ |
if (isexpos(s)) { |
116 |
+ |
double d = exposval(s); |
117 |
+ |
scalecolor(ip->expos, d); |
118 |
+ |
return(0); |
119 |
+ |
} |
120 |
+ |
if (iscolcor(s)) { |
121 |
+ |
COLOR ctmp; |
122 |
+ |
colcorval(ctmp, s); |
123 |
+ |
multcolor(ip->expos, ctmp); |
124 |
+ |
return(0); |
125 |
+ |
} |
126 |
|
if (!formatval(fmt, s)) |
127 |
|
return(0); |
128 |
|
for (i = 1; i < DTend; i++) |
133 |
|
|
134 |
|
/* Load header to obtain/check data type and number of columns */ |
135 |
|
char * |
136 |
< |
cm_getheader(int *dt, int *nr, int *nc, int *swp, FILE *fp) |
136 |
> |
cm_getheader(int *dt, int *nr, int *nc, int *swp, COLOR scale, FILE *fp) |
137 |
|
{ |
138 |
|
CMINFO cmi; |
139 |
|
/* read header */ |
140 |
|
cmi.dtype = DTfromHeader; |
141 |
|
cmi.need2swap = 0; |
142 |
+ |
cmi.expos[0] = cmi.expos[1] = cmi.expos[2] = 1.f; |
143 |
|
cmi.nrows = cmi.ncols = 0; |
144 |
|
cmi.err = "unexpected EOF in header"; |
145 |
|
if (getheader(fp, get_cminfo, &cmi) < 0) |
167 |
|
} |
168 |
|
if (swp) /* get/check swap? */ |
169 |
|
*swp = cmi.need2swap; |
170 |
+ |
if (scale) { /* transfer exposure comp. */ |
171 |
+ |
scale[0] = 1.f/cmi.expos[0]; |
172 |
+ |
scale[1] = 1.f/cmi.expos[1]; |
173 |
+ |
scale[2] = 1.f/cmi.expos[2]; |
174 |
+ |
} |
175 |
|
return(NULL); |
176 |
|
} |
177 |
|
|
178 |
|
/* Allocate and load image data into matrix */ |
179 |
|
static CMATRIX * |
180 |
< |
cm_load_rgbe(FILE *fp, int nrows, int ncols) |
180 |
> |
cm_load_rgbe(FILE *fp, int nrows, int ncols, COLOR scale) |
181 |
|
{ |
182 |
+ |
int doscale; |
183 |
|
CMATRIX *cm; |
184 |
|
COLORV *mp; |
185 |
|
/* header already loaded */ |
165 |
– |
if ((nrows <= 0) | (ncols <= 0) && !fscnresolu(&ncols, &nrows, fp)) { |
166 |
– |
error(USER, "bad picture resolution string"); |
167 |
– |
return(NULL); |
168 |
– |
} |
186 |
|
cm = cm_alloc(nrows, ncols); |
187 |
|
if (!cm) |
188 |
|
return(NULL); |
189 |
+ |
doscale = (scale[0] < .99) | (scale[0] > 1.01) | |
190 |
+ |
(scale[1] < .99) | (scale[1] > 1.01) | |
191 |
+ |
(scale[2] < .99) | (scale[2] > 1.01) ; |
192 |
|
mp = cm->cmem; |
193 |
|
while (nrows--) { |
194 |
|
if (freadscan((COLOR *)mp, ncols, fp) < 0) { |
196 |
|
cm_free(cm); |
197 |
|
return(NULL); |
198 |
|
} |
199 |
< |
mp += 3*ncols; |
199 |
> |
if (doscale) { |
200 |
> |
int i = ncols; |
201 |
> |
while (i--) { |
202 |
> |
*mp++ *= scale[0]; |
203 |
> |
*mp++ *= scale[1]; |
204 |
> |
*mp++ *= scale[2]; |
205 |
> |
} |
206 |
> |
} else |
207 |
> |
mp += 3*ncols; |
208 |
|
} /* caller closes stream */ |
209 |
|
return(cm); |
210 |
|
} |
214 |
|
cm_load(const char *inspec, int nrows, int ncols, int dtype) |
215 |
|
{ |
216 |
|
const int ROWINC = 2048; |
217 |
< |
FILE *fp = stdin; |
217 |
> |
int rowsOK = (dtype == DTascii) | (nrows > 0); |
218 |
|
int swap = 0; |
219 |
+ |
FILE *fp; |
220 |
+ |
COLOR scale; |
221 |
|
CMATRIX *cm; |
222 |
|
|
223 |
|
if (!inspec) |
224 |
< |
inspec = "<stdin>"; |
225 |
< |
else if (inspec[0] == '!') { |
224 |
> |
inspec = stdin_name; |
225 |
> |
else if (!*inspec) |
226 |
> |
return(NULL); |
227 |
> |
if (inspec == stdin_name) { /* reading from stdin? */ |
228 |
> |
fp = stdin; |
229 |
> |
} else if (inspec[0] == '!') { |
230 |
|
fp = popen(inspec+1, "r"); |
231 |
|
if (!fp) { |
232 |
|
sprintf(errmsg, "cannot start command '%s'", inspec); |
241 |
|
#endif |
242 |
|
if (dtype != DTascii) |
243 |
|
SET_FILE_BINARY(fp); /* doesn't really work */ |
244 |
< |
if (!dtype | !ncols) { /* expecting header? */ |
245 |
< |
char *err = cm_getheader(&dtype, &nrows, &ncols, &swap, fp); |
244 |
> |
if (!dtype | !rowsOK | !ncols) { /* expecting header? */ |
245 |
> |
char *err = cm_getheader(&dtype, &nrows, &ncols, &swap, scale, fp); |
246 |
|
if (err) |
247 |
|
error(USER, err); |
248 |
< |
if (ncols <= 0) |
215 |
< |
error(USER, "unspecified number of columns"); |
248 |
> |
rowsOK = (nrows > 0); |
249 |
|
} |
250 |
+ |
if (!rowsOK | !ncols && !fscnresolu(&ncols, &nrows, fp)) |
251 |
+ |
error(USER, "unspecified matrix size"); |
252 |
|
switch (dtype) { |
253 |
|
case DTascii: |
254 |
|
case DTfloat: |
256 |
|
break; |
257 |
|
case DTrgbe: |
258 |
|
case DTxyze: |
259 |
< |
cm = cm_load_rgbe(fp, nrows, ncols); |
259 |
> |
cm = cm_load_rgbe(fp, nrows, ncols, scale); |
260 |
|
goto cleanup; |
261 |
|
default: |
262 |
|
error(USER, "unexpected data type in cm_load()"); |
263 |
|
} |
264 |
|
if (nrows <= 0) { /* don't know length? */ |
265 |
|
int guessrows = 147; /* usually big enough */ |
266 |
< |
if ((dtype != DTascii) & (fp != stdin) & (inspec[0] != '!')) { |
266 |
> |
if (cm_elem_size[dtype] && (fp != stdin) & (inspec[0] != '!')) { |
267 |
|
long startpos = ftell(fp); |
268 |
|
if (fseek(fp, 0L, SEEK_END) == 0) { |
269 |
+ |
long rowsiz = (long)ncols*cm_elem_size[dtype]; |
270 |
|
long endpos = ftell(fp); |
235 |
– |
long elemsiz = 3*(dtype==DTfloat ? |
236 |
– |
sizeof(float) : sizeof(double)); |
271 |
|
|
272 |
< |
if ((endpos - startpos) % (ncols*elemsiz)) { |
272 |
> |
if ((endpos - startpos) % rowsiz) { |
273 |
|
sprintf(errmsg, |
274 |
|
"improper length for binary file '%s'", |
275 |
|
inspec); |
276 |
|
error(USER, errmsg); |
277 |
|
} |
278 |
< |
guessrows = (endpos - startpos)/(ncols*elemsiz); |
278 |
> |
guessrows = (endpos - startpos)/rowsiz; |
279 |
|
if (fseek(fp, startpos, SEEK_SET) < 0) { |
280 |
|
sprintf(errmsg, |
281 |
|
"fseek() error on file '%s'", |
317 |
|
} |
318 |
|
} else { /* read binary file */ |
319 |
|
if (sizeof(COLOR) == cm_elem_size[dtype]) { |
320 |
< |
int nread = 0; |
320 |
> |
size_t nread = 0; |
321 |
|
do { /* read all we can */ |
322 |
|
nread += getbinary(cm->cmem + 3*nread, |
323 |
|
sizeof(COLOR), |
324 |
< |
cm->nrows*cm->ncols - nread, |
324 |
> |
(size_t)cm->nrows*cm->ncols - nread, |
325 |
|
fp); |
326 |
|
if (nrows <= 0) { /* unknown length */ |
327 |
< |
if (nread == cm->nrows*cm->ncols) |
327 |
> |
if (nread == (size_t)cm->nrows*cm->ncols) |
328 |
|
/* need more space? */ |
329 |
|
cm = cm_resize(cm, cm->nrows+ROWINC); |
330 |
|
else if (nread && !(nread % cm->ncols)) |
332 |
|
cm = cm_resize(cm, nread/cm->ncols); |
333 |
|
else /* ended mid-row */ |
334 |
|
goto EOFerror; |
335 |
< |
} else if (nread < cm->nrows*cm->ncols) |
335 |
> |
} else if (nread < (size_t)cm->nrows*cm->ncols) |
336 |
|
goto EOFerror; |
337 |
< |
} while (nread < cm->nrows*cm->ncols); |
337 |
> |
} while (nread < (size_t)cm->nrows*cm->ncols); |
338 |
|
|
339 |
+ |
if (swap) { |
340 |
+ |
if (sizeof(COLORV) == 4) |
341 |
+ |
swap32((char *)cm->cmem, |
342 |
+ |
3*(size_t)cm->nrows*cm->ncols); |
343 |
+ |
else /* sizeof(COLORV) == 8 */ |
344 |
+ |
swap64((char *)cm->cmem, |
345 |
+ |
3*(size_t)cm->nrows*cm->ncols); |
346 |
+ |
} |
347 |
|
} else if (dtype == DTdouble) { |
348 |
|
double dc[3]; /* load from double */ |
349 |
|
COLORV *cvp = cm->cmem; |
350 |
< |
int n = nrows*ncols; |
350 |
> |
size_t n = (size_t)nrows*ncols; |
351 |
|
|
352 |
|
if (n <= 0) |
353 |
|
goto not_handled; |
354 |
|
while (n--) { |
355 |
|
if (getbinary(dc, sizeof(double), 3, fp) != 3) |
356 |
|
goto EOFerror; |
357 |
+ |
if (swap) swap64((char *)dc, 3); |
358 |
|
copycolor(cvp, dc); |
359 |
|
cvp += 3; |
360 |
|
} |
361 |
|
} else /* dtype == DTfloat */ { |
362 |
|
float fc[3]; /* load from float */ |
363 |
|
COLORV *cvp = cm->cmem; |
364 |
< |
int n = nrows*ncols; |
364 |
> |
size_t n = (size_t)nrows*ncols; |
365 |
|
|
366 |
|
if (n <= 0) |
367 |
|
goto not_handled; |
368 |
|
while (n--) { |
369 |
|
if (getbinary(fc, sizeof(float), 3, fp) != 3) |
370 |
|
goto EOFerror; |
371 |
+ |
if (swap) swap32((char *)fc, 3); |
372 |
|
copycolor(cvp, fc); |
373 |
|
cvp += 3; |
374 |
|
} |
380 |
|
error(WARNING, errmsg); |
381 |
|
} |
382 |
|
} |
339 |
– |
if (swap) { |
340 |
– |
if (dtype == DTfloat) |
341 |
– |
swap32((char *)cm->cmem, 3*cm->nrows*cm->ncols); |
342 |
– |
else if (dtype == DTdouble) |
343 |
– |
swap64((char *)cm->cmem, 3*cm->nrows*cm->ncols); |
344 |
– |
} |
383 |
|
cleanup: |
384 |
|
if (fp != stdin) { |
385 |
|
if (inspec[0] != '!') |
491 |
|
static const char tabEOL[2] = {'\t','\n'}; |
492 |
|
const COLORV *mp; |
493 |
|
int r, c; |
494 |
+ |
size_t n, rv; |
495 |
|
|
496 |
|
if (!cm) |
497 |
|
return(0); |
507 |
|
case DTfloat: |
508 |
|
case DTdouble: |
509 |
|
if (sizeof(COLOR) == cm_elem_size[dtype]) { |
510 |
< |
r = cm->ncols*cm->nrows; |
511 |
< |
while (r > 0) { |
512 |
< |
c = putbinary(mp, sizeof(COLOR), r, fp); |
513 |
< |
if (c <= 0) |
510 |
> |
n = (size_t)cm->ncols*cm->nrows; |
511 |
> |
while (n > 0) { |
512 |
> |
rv = fwrite(mp, sizeof(COLOR), n, fp); |
513 |
> |
if (rv <= 0) |
514 |
|
return(0); |
515 |
< |
mp += 3*c; |
516 |
< |
r -= c; |
515 |
> |
mp += 3*rv; |
516 |
> |
n -= rv; |
517 |
|
} |
518 |
|
} else if (dtype == DTdouble) { |
519 |
|
double dc[3]; |
520 |
< |
r = cm->ncols*cm->nrows; |
521 |
< |
while (r--) { |
520 |
> |
n = (size_t)cm->ncols*cm->nrows; |
521 |
> |
while (n--) { |
522 |
|
copycolor(dc, mp); |
523 |
|
if (putbinary(dc, sizeof(double), 3, fp) != 3) |
524 |
|
return(0); |
526 |
|
} |
527 |
|
} else /* dtype == DTfloat */ { |
528 |
|
float fc[3]; |
529 |
< |
r = cm->ncols*cm->nrows; |
530 |
< |
while (r--) { |
529 |
> |
n = (size_t)cm->ncols*cm->nrows; |
530 |
> |
while (n--) { |
531 |
|
copycolor(fc, mp); |
532 |
|
if (putbinary(fc, sizeof(float), 3, fp) != 3) |
533 |
|
return(0); |