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, SPECFMT, |
22 |
> |
"float", "ascii", "double" |
23 |
|
}; |
24 |
|
|
25 |
|
const int cm_elem_size[] = { |
26 |
< |
0, 0, 3*sizeof(float), 3*sizeof(double), 4, 4 |
26 |
> |
0, 4, 4, 0, 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)); |
39 |
< |
if (cm == NULL) |
38 |
> |
sizeof(COLOR)*((size_t)nrows*ncols - 1)); |
39 |
> |
if (!cm) |
40 |
|
error(SYSTEM, "out of memory in cm_alloc()"); |
41 |
|
cm->nrows = nrows; |
42 |
|
cm->ncols = ncols; |
59 |
|
{ |
60 |
|
size_t old_size, new_size, ra_bounds[2]; |
61 |
|
|
62 |
+ |
if (!cm) |
63 |
+ |
return(NULL); |
64 |
|
if (nrows == cm->nrows) |
65 |
|
return(cm); |
66 |
|
if (nrows <= 0) { |
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); |
76 |
|
cm = (CMATRIX *)realloc(cm, ra_bounds[1]); |
77 |
< |
if (cm == NULL) |
77 |
> |
if (!cm) |
78 |
|
error(SYSTEM, "out of memory in cm_resize()"); |
79 |
|
} |
80 |
|
cm->nrows = nrows; |
83 |
|
|
84 |
|
typedef struct { |
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 |
|
|
93 |
|
get_cminfo(char *s, void *p) |
94 |
|
{ |
95 |
|
CMINFO *ip = (CMINFO *)p; |
96 |
< |
char fmt[32]; |
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 |
|
} |
108 |
|
ip->ncols = atoi(s+6); |
109 |
|
return(0); |
110 |
|
} |
111 |
+ |
if ((i = isbigendian(s)) >= 0) { |
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, 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) |
146 |
|
return(cmi.err); |
147 |
< |
if (dt != NULL) { /* get/check data type? */ |
147 |
> |
if (dt) { /* get/check data type? */ |
148 |
|
if (cmi.dtype == DTfromHeader) { |
149 |
|
if (*dt == DTfromHeader) |
150 |
|
return("missing/unknown data format in header"); |
153 |
|
else if (*dt != cmi.dtype) |
154 |
|
return("unexpected data format in header"); |
155 |
|
} |
156 |
< |
if (nr != NULL) { /* get/check #rows? */ |
156 |
> |
if (nr) { /* get/check #rows? */ |
157 |
|
if (*nr <= 0) |
158 |
|
*nr = cmi.nrows; |
159 |
|
else if ((cmi.nrows > 0) & (*nr != cmi.nrows)) |
160 |
|
return("unexpected row count in header"); |
161 |
|
} |
162 |
< |
if (nc != NULL) { /* get/check #columns? */ |
162 |
> |
if (nc) { /* get/check #columns? */ |
163 |
|
if (*nc <= 0) |
164 |
|
*nc = cmi.ncols; |
165 |
|
else if ((cmi.ncols > 0) & (*nc != cmi.ncols)) |
166 |
|
return("unexpected column count in header"); |
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) |
181 |
+ |
{ |
182 |
+ |
CMATRIX *cm; |
183 |
+ |
COLORV *mp; |
184 |
+ |
/* header already loaded */ |
185 |
+ |
cm = cm_alloc(nrows, ncols); |
186 |
+ |
if (!cm) |
187 |
+ |
return(NULL); |
188 |
+ |
mp = cm->cmem; |
189 |
+ |
while (nrows--) { |
190 |
+ |
if (freadscan((COLOR *)mp, ncols, fp) < 0) { |
191 |
+ |
error(USER, "error reading color picture as matrix"); |
192 |
+ |
cm_free(cm); |
193 |
+ |
return(NULL); |
194 |
+ |
} |
195 |
+ |
mp += 3*ncols; |
196 |
+ |
} /* caller closes stream */ |
197 |
+ |
return(cm); |
198 |
+ |
} |
199 |
+ |
|
200 |
|
/* Allocate and load a matrix from the given input (or stdin if NULL) */ |
201 |
|
CMATRIX * |
202 |
|
cm_load(const char *inspec, int nrows, int ncols, int dtype) |
203 |
|
{ |
204 |
|
const int ROWINC = 2048; |
205 |
< |
FILE *fp = stdin; |
205 |
> |
int dimsOK = (dtype == DTascii) | (nrows > 0) && ncols; |
206 |
> |
int swap = 0; |
207 |
> |
FILE *fp; |
208 |
> |
COLOR scale; |
209 |
|
CMATRIX *cm; |
210 |
|
|
211 |
< |
if (inspec == NULL) |
212 |
< |
inspec = "<stdin>"; |
213 |
< |
else if (inspec[0] == '!') { |
211 |
> |
if (!inspec) |
212 |
> |
inspec = stdin_name; |
213 |
> |
else if (!*inspec) |
214 |
> |
return(NULL); |
215 |
> |
if (inspec == stdin_name) { /* reading from stdin? */ |
216 |
> |
fp = stdin; |
217 |
> |
} else if (inspec[0] == '!') { |
218 |
|
fp = popen(inspec+1, "r"); |
219 |
< |
if (fp == NULL) { |
219 |
> |
if (!fp) { |
220 |
|
sprintf(errmsg, "cannot start command '%s'", inspec); |
221 |
|
error(SYSTEM, errmsg); |
222 |
|
} |
223 |
< |
} else if ((fp = fopen(inspec, "r")) == NULL) { |
223 |
> |
} else if (!(fp = fopen(inspec, "r"))) { |
224 |
|
sprintf(errmsg, "cannot open file '%s'", inspec); |
225 |
|
error(SYSTEM, errmsg); |
226 |
|
} |
229 |
|
#endif |
230 |
|
if (dtype != DTascii) |
231 |
|
SET_FILE_BINARY(fp); /* doesn't really work */ |
232 |
< |
if (!dtype | !ncols) { /* expecting header? */ |
233 |
< |
char *err = cm_getheader(&dtype, &nrows, &ncols, fp); |
234 |
< |
if (err != NULL) |
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 |
< |
if (ncols <= 0) |
237 |
< |
error(USER, "unspecified number of columns"); |
236 |
> |
dimsOK = ncols > 0 && ( nrows > 0 || |
237 |
> |
(dtype != DTrgbe) & (dtype != DTxyze) ); |
238 |
|
} |
239 |
+ |
if (!dimsOK && !fscnresolu(&ncols, &nrows, fp)) |
240 |
+ |
error(USER, "unspecified matrix size"); |
241 |
|
switch (dtype) { |
242 |
|
case DTascii: |
243 |
|
case DTfloat: |
244 |
|
case DTdouble: |
245 |
|
break; |
246 |
+ |
case DTrgbe: |
247 |
+ |
case DTxyze: |
248 |
+ |
cm = cm_load_rgbe(fp, nrows, ncols); |
249 |
+ |
goto cleanup; |
250 |
|
default: |
251 |
|
error(USER, "unexpected data type in cm_load()"); |
252 |
|
} |
253 |
|
if (nrows <= 0) { /* don't know length? */ |
254 |
|
int guessrows = 147; /* usually big enough */ |
255 |
< |
if ((dtype != DTascii) & (fp != stdin) & (inspec[0] != '!')) { |
255 |
> |
if (cm_elem_size[dtype] && (fp != stdin) & (inspec[0] != '!')) { |
256 |
|
long startpos = ftell(fp); |
257 |
|
if (fseek(fp, 0L, SEEK_END) == 0) { |
258 |
+ |
long rowsiz = (long)ncols*cm_elem_size[dtype]; |
259 |
|
long endpos = ftell(fp); |
194 |
– |
long elemsiz = 3*(dtype==DTfloat ? |
195 |
– |
sizeof(float) : sizeof(double)); |
260 |
|
|
261 |
< |
if ((endpos - startpos) % (ncols*elemsiz)) { |
261 |
> |
if ((endpos - startpos) % rowsiz) { |
262 |
|
sprintf(errmsg, |
263 |
|
"improper length for binary file '%s'", |
264 |
|
inspec); |
265 |
|
error(USER, errmsg); |
266 |
|
} |
267 |
< |
guessrows = (endpos - startpos)/(ncols*elemsiz); |
267 |
> |
guessrows = (endpos - startpos)/rowsiz; |
268 |
|
if (fseek(fp, startpos, SEEK_SET) < 0) { |
269 |
|
sprintf(errmsg, |
270 |
|
"fseek() error on file '%s'", |
277 |
|
cm = cm_alloc(guessrows, ncols); |
278 |
|
} else |
279 |
|
cm = cm_alloc(nrows, ncols); |
280 |
< |
if (cm == NULL) /* XXX never happens */ |
280 |
> |
if (!cm) /* XXX never happens */ |
281 |
|
return(NULL); |
282 |
|
if (dtype == DTascii) { /* read text file */ |
283 |
|
int maxrow = (nrows > 0 ? nrows : 32000); |
306 |
|
} |
307 |
|
} else { /* read binary file */ |
308 |
|
if (sizeof(COLOR) == cm_elem_size[dtype]) { |
309 |
< |
int nread = 0; |
309 |
> |
size_t nread = 0; |
310 |
|
do { /* read all we can */ |
311 |
< |
nread += fread(cm->cmem + 3*nread, |
311 |
> |
nread += getbinary(cm->cmem + 3*nread, |
312 |
|
sizeof(COLOR), |
313 |
< |
cm->nrows*cm->ncols - nread, |
313 |
> |
(size_t)cm->nrows*cm->ncols - nread, |
314 |
|
fp); |
315 |
|
if (nrows <= 0) { /* unknown length */ |
316 |
< |
if (nread == cm->nrows*cm->ncols) |
316 |
> |
if (nread == (size_t)cm->nrows*cm->ncols) |
317 |
|
/* need more space? */ |
318 |
|
cm = cm_resize(cm, cm->nrows+ROWINC); |
319 |
|
else if (nread && !(nread % cm->ncols)) |
321 |
|
cm = cm_resize(cm, nread/cm->ncols); |
322 |
|
else /* ended mid-row */ |
323 |
|
goto EOFerror; |
324 |
< |
} else if (nread < cm->nrows*cm->ncols) |
324 |
> |
} else if (nread < (size_t)cm->nrows*cm->ncols) |
325 |
|
goto EOFerror; |
326 |
< |
} while (nread < cm->nrows*cm->ncols); |
326 |
> |
} while (nread < (size_t)cm->nrows*cm->ncols); |
327 |
|
|
328 |
+ |
if (swap) { |
329 |
+ |
if (sizeof(COLORV) == 4) |
330 |
+ |
swap32((char *)cm->cmem, |
331 |
+ |
3*(size_t)cm->nrows*cm->ncols); |
332 |
+ |
else /* sizeof(COLORV) == 8 */ |
333 |
+ |
swap64((char *)cm->cmem, |
334 |
+ |
3*(size_t)cm->nrows*cm->ncols); |
335 |
+ |
} |
336 |
|
} else if (dtype == DTdouble) { |
337 |
|
double dc[3]; /* load from double */ |
338 |
|
COLORV *cvp = cm->cmem; |
339 |
< |
int n = nrows*ncols; |
339 |
> |
size_t n = (size_t)nrows*ncols; |
340 |
|
|
341 |
|
if (n <= 0) |
342 |
|
goto not_handled; |
343 |
|
while (n--) { |
344 |
< |
if (fread(dc, sizeof(double), 3, fp) != 3) |
344 |
> |
if (getbinary(dc, sizeof(double), 3, fp) != 3) |
345 |
|
goto EOFerror; |
346 |
+ |
if (swap) swap64((char *)dc, 3); |
347 |
|
copycolor(cvp, dc); |
348 |
|
cvp += 3; |
349 |
|
} |
350 |
|
} else /* dtype == DTfloat */ { |
351 |
|
float fc[3]; /* load from float */ |
352 |
|
COLORV *cvp = cm->cmem; |
353 |
< |
int n = nrows*ncols; |
353 |
> |
size_t n = (size_t)nrows*ncols; |
354 |
|
|
355 |
|
if (n <= 0) |
356 |
|
goto not_handled; |
357 |
|
while (n--) { |
358 |
< |
if (fread(fc, sizeof(float), 3, fp) != 3) |
358 |
> |
if (getbinary(fc, sizeof(float), 3, fp) != 3) |
359 |
|
goto EOFerror; |
360 |
+ |
if (swap) swap32((char *)fc, 3); |
361 |
|
copycolor(cvp, fc); |
362 |
|
cvp += 3; |
363 |
|
} |
369 |
|
error(WARNING, errmsg); |
370 |
|
} |
371 |
|
} |
372 |
+ |
cleanup: |
373 |
|
if (fp != stdin) { |
374 |
|
if (inspec[0] != '!') |
375 |
|
fclose(fp); |
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); |
399 |
|
error(USER, errmsg); |
400 |
+ |
return(NULL); |
401 |
|
not_handled: |
402 |
|
error(INTERNAL, "unhandled data size or length in cm_load()"); |
403 |
|
return(NULL); /* gratis return */ |
410 |
|
CMATRIX *cvr; |
411 |
|
int dr; |
412 |
|
|
413 |
+ |
if (!cm) |
414 |
+ |
return(NULL); |
415 |
|
if ((c < 0) | (c >= cm->ncols)) |
416 |
|
error(INTERNAL, "column requested outside matrix"); |
417 |
|
cvr = cm_alloc(cm->nrows, 1); |
418 |
< |
if (cvr == NULL) |
418 |
> |
if (!cvr) |
419 |
|
return(NULL); |
420 |
|
for (dr = 0; dr < cm->nrows; dr++) { |
421 |
|
const COLORV *sp = cm_lval(cm,dr,c); |
427 |
|
return(cvr); |
428 |
|
} |
429 |
|
|
341 |
– |
/* Scale a matrix by a single value */ |
342 |
– |
CMATRIX * |
343 |
– |
cm_scale(const CMATRIX *cm1, const COLOR sca) |
344 |
– |
{ |
345 |
– |
CMATRIX *cmr; |
346 |
– |
int dr, dc; |
347 |
– |
|
348 |
– |
cmr = cm_alloc(cm1->nrows, cm1->ncols); |
349 |
– |
if (cmr == NULL) |
350 |
– |
return(NULL); |
351 |
– |
for (dr = 0; dr < cmr->nrows; dr++) |
352 |
– |
for (dc = 0; dc < cmr->ncols; dc++) { |
353 |
– |
const COLORV *sp = cm_lval(cm1,dr,dc); |
354 |
– |
COLORV *dp = cm_lval(cmr,dr,dc); |
355 |
– |
dp[0] = sp[0] * sca[0]; |
356 |
– |
dp[1] = sp[1] * sca[1]; |
357 |
– |
dp[2] = sp[2] * sca[2]; |
358 |
– |
} |
359 |
– |
return(cmr); |
360 |
– |
} |
361 |
– |
|
430 |
|
/* Multiply two matrices (or a matrix and a vector) and allocate the result */ |
431 |
|
CMATRIX * |
432 |
|
cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2) |
435 |
|
CMATRIX *cmr; |
436 |
|
int dr, dc, i; |
437 |
|
|
438 |
+ |
if (!cm1 | !cm2) |
439 |
+ |
return(NULL); |
440 |
|
if ((cm1->ncols <= 0) | (cm1->ncols != cm2->nrows)) |
441 |
|
error(INTERNAL, "matrix dimension mismatch in cm_multiply()"); |
442 |
|
cmr = cm_alloc(cm1->nrows, cm2->ncols); |
443 |
< |
if (cmr == NULL) |
443 |
> |
if (!cmr) |
444 |
|
return(NULL); |
445 |
|
/* optimization: check for zero rows & cols */ |
446 |
|
if (((cm1->nrows > 5) | (cm2->ncols > 5)) & (cm1->ncols > 5)) { |
465 |
|
COLORV *dp = cm_lval(cmr,dr,dc); |
466 |
|
double res[3]; |
467 |
|
dp[0] = dp[1] = dp[2] = 0; |
468 |
< |
if (rowcheck != NULL && !rowcheck[dr]) |
468 |
> |
if (rowcheck && !rowcheck[dr]) |
469 |
|
continue; |
470 |
< |
if (colcheck != NULL && !colcheck[dc]) |
470 |
> |
if (colcheck && !colcheck[dc]) |
471 |
|
continue; |
472 |
|
res[0] = res[1] = res[2] = 0; |
473 |
|
for (i = 0; i < cm1->ncols; i++) { |
474 |
|
const COLORV *cp1 = cm_lval(cm1,dr,i); |
475 |
|
const COLORV *cp2 = cm_lval(cm2,i,dc); |
476 |
< |
res[0] += cp1[0] * cp2[0]; |
477 |
< |
res[1] += cp1[1] * cp2[1]; |
478 |
< |
res[2] += cp1[2] * cp2[2]; |
476 |
> |
res[0] += (double)cp1[0] * cp2[0]; |
477 |
> |
res[1] += (double)cp1[1] * cp2[1]; |
478 |
> |
res[2] += (double)cp1[2] * cp2[2]; |
479 |
|
} |
480 |
|
copycolor(dp, res); |
481 |
|
} |
482 |
< |
if (rowcheck != NULL) free(rowcheck); |
483 |
< |
if (colcheck != NULL) free(colcheck); |
482 |
> |
if (rowcheck) free(rowcheck); |
483 |
> |
if (colcheck) free(colcheck); |
484 |
|
return(cmr); |
485 |
|
} |
486 |
|
|
489 |
|
cm_write(const CMATRIX *cm, int dtype, FILE *fp) |
490 |
|
{ |
491 |
|
static const char tabEOL[2] = {'\t','\n'}; |
492 |
< |
const COLORV *mp = cm->cmem; |
492 |
> |
const COLORV *mp; |
493 |
|
int r, c; |
494 |
+ |
size_t n, rv; |
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: |
504 |
|
for (r = 0; r < cm->nrows; r++) |
510 |
|
case DTfloat: |
511 |
|
case DTdouble: |
512 |
|
if (sizeof(COLOR) == cm_elem_size[dtype]) { |
513 |
< |
r = cm->ncols*cm->nrows; |
514 |
< |
while (r > 0) { |
515 |
< |
c = fwrite(mp, sizeof(COLOR), r, fp); |
516 |
< |
if (c <= 0) |
513 |
> |
n = (size_t)cm->ncols*cm->nrows; |
514 |
> |
while (n > 0) { |
515 |
> |
rv = fwrite(mp, sizeof(COLOR), n, fp); |
516 |
> |
if (rv <= 0) |
517 |
|
return(0); |
518 |
< |
mp += 3*c; |
519 |
< |
r -= c; |
518 |
> |
mp += 3*rv; |
519 |
> |
n -= rv; |
520 |
|
} |
521 |
|
} else if (dtype == DTdouble) { |
522 |
|
double dc[3]; |
523 |
< |
r = cm->ncols*cm->nrows; |
524 |
< |
while (r--) { |
523 |
> |
n = (size_t)cm->ncols*cm->nrows; |
524 |
> |
while (n--) { |
525 |
|
copycolor(dc, mp); |
526 |
< |
if (fwrite(dc, sizeof(double), 3, fp) != 3) |
526 |
> |
if (putbinary(dc, sizeof(double), 3, fp) != 3) |
527 |
|
return(0); |
528 |
|
mp += 3; |
529 |
|
} |
530 |
|
} else /* dtype == DTfloat */ { |
531 |
|
float fc[3]; |
532 |
< |
r = cm->ncols*cm->nrows; |
533 |
< |
while (r--) { |
532 |
> |
n = (size_t)cm->ncols*cm->nrows; |
533 |
> |
while (n--) { |
534 |
|
copycolor(fc, mp); |
535 |
< |
if (fwrite(fc, sizeof(float), 3, fp) != 3) |
535 |
> |
if (putbinary(fc, sizeof(float), 3, fp) != 3) |
536 |
|
return(0); |
537 |
|
mp += 3; |
538 |
|
} |
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 |
|
} |