18 |
|
const char stdin_name[] = "<stdin>"; |
19 |
|
|
20 |
|
const char *cm_fmt_id[] = { |
21 |
< |
"unknown", COLRFMT, CIEFMT, |
21 |
> |
"unknown", COLRFMT, CIEFMT, SPECFMT, |
22 |
|
"float", "ascii", "double" |
23 |
|
}; |
24 |
|
|
25 |
|
const int cm_elem_size[] = { |
26 |
< |
0, 4, 4, 3*sizeof(float), 0, 3*sizeof(double) |
26 |
> |
0, 4, 4, 0, 3*sizeof(float), 0, 3*sizeof(double) |
27 |
|
}; |
28 |
|
|
29 |
|
/* Allocate a color coefficient matrix */ |
96 |
|
char fmt[MAXFMTLEN]; |
97 |
|
int i; |
98 |
|
|
99 |
< |
if (!strncmp(s, "NCOMP=", 6) && atoi(s+6) != 3) { |
99 |
> |
if (isncomp(s) && ncompval(s) != 3) { |
100 |
|
ip->err = "unexpected # components (must be 3)"; |
101 |
|
return(-1); |
102 |
|
} |
177 |
|
|
178 |
|
/* Allocate and load image data into matrix */ |
179 |
|
static CMATRIX * |
180 |
< |
cm_load_rgbe(FILE *fp, int nrows, int ncols, COLOR scale) |
180 |
> |
cm_load_rgbe(FILE *fp, int nrows, int ncols) |
181 |
|
{ |
182 |
– |
int doscale; |
182 |
|
CMATRIX *cm; |
183 |
|
COLORV *mp; |
184 |
|
/* header already loaded */ |
185 |
|
cm = cm_alloc(nrows, ncols); |
186 |
|
if (!cm) |
187 |
|
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) ; |
188 |
|
mp = cm->cmem; |
189 |
|
while (nrows--) { |
190 |
|
if (freadscan((COLOR *)mp, ncols, fp) < 0) { |
192 |
|
cm_free(cm); |
193 |
|
return(NULL); |
194 |
|
} |
195 |
< |
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; |
195 |
> |
mp += 3*ncols; |
196 |
|
} /* caller closes stream */ |
197 |
|
return(cm); |
198 |
|
} |
202 |
|
cm_load(const char *inspec, int nrows, int ncols, int dtype) |
203 |
|
{ |
204 |
|
const int ROWINC = 2048; |
205 |
+ |
int dimsOK = (dtype == DTascii) | (nrows > 0) && ncols; |
206 |
|
int swap = 0; |
207 |
|
FILE *fp; |
208 |
|
COLOR scale; |
229 |
|
#endif |
230 |
|
if (dtype != DTascii) |
231 |
|
SET_FILE_BINARY(fp); /* doesn't really work */ |
232 |
< |
if (!dtype | !ncols) { /* expecting header? */ |
232 |
> |
if (!dtype | !dimsOK) { /* expecting header? */ |
233 |
|
char *err = cm_getheader(&dtype, &nrows, &ncols, &swap, scale, fp); |
234 |
|
if (err) |
235 |
|
error(USER, err); |
236 |
+ |
dimsOK = ncols > 0 && ( nrows > 0 || |
237 |
+ |
(dtype != DTrgbe) & (dtype != DTxyze) ); |
238 |
|
} |
239 |
< |
if (ncols <= 0 && !fscnresolu(&ncols, &nrows, fp)) |
240 |
< |
error(USER, "unspecified number of columns"); |
239 |
> |
if (!dimsOK && !fscnresolu(&ncols, &nrows, fp)) |
240 |
> |
error(USER, "unspecified matrix size"); |
241 |
|
switch (dtype) { |
242 |
|
case DTascii: |
243 |
|
case DTfloat: |
245 |
|
break; |
246 |
|
case DTrgbe: |
247 |
|
case DTxyze: |
248 |
< |
cm = cm_load_rgbe(fp, nrows, ncols, scale); |
248 |
> |
cm = cm_load_rgbe(fp, nrows, ncols); |
249 |
|
goto cleanup; |
250 |
|
default: |
251 |
|
error(USER, "unexpected data type in cm_load()"); |
382 |
|
else |
383 |
|
funlockfile(fp); |
384 |
|
#endif |
385 |
+ |
if ((scale[0] < .99) | (scale[0] > 1.01) | |
386 |
+ |
(scale[1] < .99) | (scale[1] > 1.01) | |
387 |
+ |
(scale[2] < .99) | (scale[2] > 1.01)) { |
388 |
+ |
size_t n = (size_t)ncols*nrows; |
389 |
+ |
COLORV *mp = cm->cmem; |
390 |
+ |
while (n--) { /* apply exposure scaling */ |
391 |
+ |
*mp++ *= scale[0]; |
392 |
+ |
*mp++ *= scale[1]; |
393 |
+ |
*mp++ *= scale[2]; |
394 |
+ |
} |
395 |
+ |
} |
396 |
|
return(cm); |
397 |
|
EOFerror: |
398 |
|
sprintf(errmsg, "unexpected EOF reading %s", inspec); |
495 |
|
|
496 |
|
if (!cm) |
497 |
|
return(0); |
498 |
+ |
#ifdef getc_unlocked |
499 |
+ |
flockfile(fp); |
500 |
+ |
#endif |
501 |
|
mp = cm->cmem; |
502 |
|
switch (dtype) { |
503 |
|
case DTascii: |
549 |
|
fputs("Unsupported data type in cm_write()!\n", stderr); |
550 |
|
return(0); |
551 |
|
} |
552 |
+ |
#ifdef getc_unlocked |
553 |
+ |
funlockfile(fp); |
554 |
+ |
#endif |
555 |
|
return(fflush(fp) == 0); |
556 |
|
} |