8 |
|
*/ |
9 |
|
|
10 |
|
#include <ctype.h> |
11 |
+ |
#include "platform.h" |
12 |
|
#include "standard.h" |
13 |
|
#include "cmatrix.h" |
14 |
|
#include "platform.h" |
15 |
+ |
#include "paths.h" |
16 |
|
#include "resolu.h" |
17 |
|
|
16 |
– |
const char *cm_fmt_id[] = { |
17 |
– |
"unknown", "ascii", "float", "double", |
18 |
– |
COLRFMT, CIEFMT |
19 |
– |
}; |
20 |
– |
|
21 |
– |
const int cm_elem_size[] = { |
22 |
– |
0, 0, 3*sizeof(float), 3*sizeof(double), 4, 4 |
23 |
– |
}; |
24 |
– |
|
18 |
|
/* Allocate a color coefficient matrix */ |
19 |
|
CMATRIX * |
20 |
|
cm_alloc(int nrows, int ncols) |
24 |
|
if ((nrows <= 0) | (ncols <= 0)) |
25 |
|
error(USER, "attempt to create empty matrix"); |
26 |
|
cm = (CMATRIX *)malloc(sizeof(CMATRIX) + |
27 |
< |
3*sizeof(COLORV)*(nrows*ncols - 1)); |
28 |
< |
if (cm == NULL) |
27 |
> |
sizeof(COLOR)*((size_t)nrows*ncols - 1)); |
28 |
> |
if (!cm) |
29 |
|
error(SYSTEM, "out of memory in cm_alloc()"); |
30 |
|
cm->nrows = nrows; |
31 |
|
cm->ncols = ncols; |
32 |
|
return(cm); |
33 |
|
} |
34 |
|
|
35 |
+ |
static void |
36 |
+ |
adjacent_ra_sizes(size_t bounds[2], size_t target) |
37 |
+ |
{ |
38 |
+ |
bounds[0] = 0; bounds[1] = 2048; |
39 |
+ |
while (bounds[1] < target) { |
40 |
+ |
bounds[0] = bounds[1]; |
41 |
+ |
bounds[1] += bounds[1]>>1; |
42 |
+ |
} |
43 |
+ |
} |
44 |
+ |
|
45 |
|
/* Resize color coefficient matrix */ |
46 |
|
CMATRIX * |
47 |
|
cm_resize(CMATRIX *cm, int nrows) |
48 |
|
{ |
49 |
+ |
size_t old_size, new_size, ra_bounds[2]; |
50 |
+ |
|
51 |
+ |
if (!cm) |
52 |
+ |
return(NULL); |
53 |
|
if (nrows == cm->nrows) |
54 |
|
return(cm); |
55 |
|
if (nrows <= 0) { |
56 |
|
cm_free(cm); |
57 |
|
return(NULL); |
58 |
|
} |
59 |
< |
cm = (CMATRIX *)realloc(cm, sizeof(CMATRIX) + |
60 |
< |
3*sizeof(COLORV)*(nrows*cm->ncols - 1)); |
61 |
< |
if (cm == NULL) |
62 |
< |
error(SYSTEM, "out of memory in cm_resize()"); |
59 |
> |
old_size = sizeof(CMATRIX) + sizeof(COLOR)*((size_t)cm->nrows*cm->ncols - 1); |
60 |
> |
adjacent_ra_sizes(ra_bounds, old_size); |
61 |
> |
new_size = sizeof(CMATRIX) + sizeof(COLOR)*((size_t)nrows*cm->ncols - 1); |
62 |
> |
if (nrows < cm->nrows ? new_size <= ra_bounds[0] : |
63 |
> |
new_size > ra_bounds[1]) { |
64 |
> |
adjacent_ra_sizes(ra_bounds, new_size); |
65 |
> |
cm = (CMATRIX *)realloc(cm, ra_bounds[1]); |
66 |
> |
if (!cm) |
67 |
> |
error(SYSTEM, "out of memory in cm_resize()"); |
68 |
> |
} |
69 |
|
cm->nrows = nrows; |
70 |
|
return(cm); |
71 |
|
} |
72 |
|
|
73 |
+ |
typedef struct { |
74 |
+ |
int dtype; /* data type */ |
75 |
+ |
int need2swap; /* need byte swap? */ |
76 |
+ |
int nrows, ncols; /* matrix size */ |
77 |
+ |
COLOR expos; /* exposure value */ |
78 |
+ |
char *err; /* error message */ |
79 |
+ |
} CMINFO; /* header info record */ |
80 |
+ |
|
81 |
|
static int |
82 |
< |
getDT(char *s, void *p) |
82 |
> |
get_cminfo(char *s, void *p) |
83 |
|
{ |
84 |
< |
char fmt[32]; |
84 |
> |
CMINFO *ip = (CMINFO *)p; |
85 |
> |
char fmt[MAXFMTLEN]; |
86 |
|
int i; |
87 |
< |
|
87 |
> |
|
88 |
> |
if (isncomp(s) && ncompval(s) != 3) { |
89 |
> |
ip->err = "unexpected # components (must be 3)"; |
90 |
> |
return(-1); |
91 |
> |
} |
92 |
> |
if (!strncmp(s, "NROWS=", 6)) { |
93 |
> |
ip->nrows = atoi(s+6); |
94 |
> |
return(0); |
95 |
> |
} |
96 |
> |
if (!strncmp(s, "NCOLS=", 6)) { |
97 |
> |
ip->ncols = atoi(s+6); |
98 |
> |
return(0); |
99 |
> |
} |
100 |
> |
if ((i = isbigendian(s)) >= 0) { |
101 |
> |
ip->need2swap = (nativebigendian() != i); |
102 |
> |
return(0); |
103 |
> |
} |
104 |
> |
if (isexpos(s)) { |
105 |
> |
double d = exposval(s); |
106 |
> |
scalecolor(ip->expos, d); |
107 |
> |
return(0); |
108 |
> |
} |
109 |
> |
if (iscolcor(s)) { |
110 |
> |
COLOR ctmp; |
111 |
> |
colcorval(ctmp, s); |
112 |
> |
multcolor(ip->expos, ctmp); |
113 |
> |
return(0); |
114 |
> |
} |
115 |
|
if (!formatval(fmt, s)) |
116 |
|
return(0); |
117 |
|
for (i = 1; i < DTend; i++) |
118 |
|
if (!strcmp(fmt, cm_fmt_id[i])) |
119 |
< |
*((int *)p) = i; |
119 |
> |
ip->dtype = i; |
120 |
|
return(0); |
121 |
|
} |
122 |
|
|
123 |
< |
/* Load header to obtain data type */ |
124 |
< |
int |
125 |
< |
getDTfromHeader(FILE *fp) |
123 |
> |
/* Load header to obtain/check data type and number of columns */ |
124 |
> |
char * |
125 |
> |
cm_getheader(int *dt, int *nr, int *nc, int *swp, COLOR scale, FILE *fp) |
126 |
|
{ |
127 |
< |
int dt = DTfromHeader; |
128 |
< |
|
129 |
< |
if (getheader(fp, getDT, &dt) < 0) |
130 |
< |
error(SYSTEM, "header read error"); |
131 |
< |
if (dt == DTfromHeader) |
132 |
< |
error(USER, "missing data format in header"); |
133 |
< |
return(dt); |
127 |
> |
CMINFO cmi; |
128 |
> |
/* read header */ |
129 |
> |
cmi.dtype = DTfromHeader; |
130 |
> |
cmi.need2swap = 0; |
131 |
> |
cmi.expos[0] = cmi.expos[1] = cmi.expos[2] = 1.f; |
132 |
> |
cmi.nrows = cmi.ncols = 0; |
133 |
> |
cmi.err = "unexpected EOF in header"; |
134 |
> |
if (getheader(fp, get_cminfo, &cmi) < 0) |
135 |
> |
return(cmi.err); |
136 |
> |
if (dt) { /* get/check data type? */ |
137 |
> |
if (cmi.dtype == DTfromHeader) { |
138 |
> |
if (*dt == DTfromHeader) |
139 |
> |
return("missing/unknown data format in header"); |
140 |
> |
} else if (*dt == DTfromHeader) |
141 |
> |
*dt = cmi.dtype; |
142 |
> |
else if (*dt != cmi.dtype) |
143 |
> |
return("unexpected data format in header"); |
144 |
> |
} |
145 |
> |
if (nr) { /* get/check #rows? */ |
146 |
> |
if (*nr <= 0) |
147 |
> |
*nr = cmi.nrows; |
148 |
> |
else if ((cmi.nrows > 0) & (*nr != cmi.nrows)) |
149 |
> |
return("unexpected row count in header"); |
150 |
> |
} |
151 |
> |
if (nc) { /* get/check #columns? */ |
152 |
> |
if (*nc <= 0) |
153 |
> |
*nc = cmi.ncols; |
154 |
> |
else if ((cmi.ncols > 0) & (*nc != cmi.ncols)) |
155 |
> |
return("unexpected column count in header"); |
156 |
> |
} |
157 |
> |
if (swp) /* get/check swap? */ |
158 |
> |
*swp = cmi.need2swap; |
159 |
> |
if (scale) { /* transfer exposure comp. */ |
160 |
> |
scale[0] = 1.f/cmi.expos[0]; |
161 |
> |
scale[1] = 1.f/cmi.expos[1]; |
162 |
> |
scale[2] = 1.f/cmi.expos[2]; |
163 |
> |
} |
164 |
> |
return(NULL); |
165 |
|
} |
166 |
|
|
167 |
< |
/* Allocate and load a matrix from the given file (or stdin if NULL) */ |
168 |
< |
CMATRIX * |
169 |
< |
cm_load(const char *fname, int nrows, int ncols, int dtype) |
167 |
> |
/* Allocate and load image data into matrix */ |
168 |
> |
static CMATRIX * |
169 |
> |
cm_load_rgbe(FILE *fp, int nrows, int ncols) |
170 |
|
{ |
91 |
– |
FILE *fp = stdin; |
171 |
|
CMATRIX *cm; |
172 |
+ |
COLORV *mp; |
173 |
+ |
/* header already loaded */ |
174 |
+ |
cm = cm_alloc(nrows, ncols); |
175 |
+ |
if (!cm) |
176 |
+ |
return(NULL); |
177 |
+ |
mp = cm->cmem; |
178 |
+ |
while (nrows--) { |
179 |
+ |
if (freadscan((COLOR *)mp, ncols, fp) < 0) { |
180 |
+ |
error(USER, "error reading color picture as matrix"); |
181 |
+ |
cm_free(cm); |
182 |
+ |
return(NULL); |
183 |
+ |
} |
184 |
+ |
mp += 3*ncols; |
185 |
+ |
} /* caller closes stream */ |
186 |
+ |
return(cm); |
187 |
+ |
} |
188 |
|
|
189 |
< |
if (ncols <= 0) |
190 |
< |
error(USER, "Non-positive number of columns"); |
191 |
< |
if (fname == NULL) |
192 |
< |
fname = "<stdin>"; |
193 |
< |
else if ((fp = fopen(fname, "r")) == NULL) { |
194 |
< |
sprintf(errmsg, "cannot open file '%s'", fname); |
189 |
> |
/* Allocate and load a matrix from the given input (or stdin if NULL) */ |
190 |
> |
CMATRIX * |
191 |
> |
cm_load(const char *inspec, int nrows, int ncols, int dtype) |
192 |
> |
{ |
193 |
> |
const int ROWINC = 2048; |
194 |
> |
int dimsOK = (dtype == DTascii) | (nrows > 0) && ncols; |
195 |
> |
int swap = 0; |
196 |
> |
FILE *fp; |
197 |
> |
COLOR scale; |
198 |
> |
CMATRIX *cm; |
199 |
> |
|
200 |
> |
if (!inspec) |
201 |
> |
inspec = stdin_name; |
202 |
> |
else if (!*inspec) |
203 |
> |
return(NULL); |
204 |
> |
if (inspec == stdin_name) { /* reading from stdin? */ |
205 |
> |
fp = stdin; |
206 |
> |
} else if (inspec[0] == '!') { |
207 |
> |
fp = popen(inspec+1, "r"); |
208 |
> |
if (!fp) { |
209 |
> |
sprintf(errmsg, "cannot start command '%s'", inspec); |
210 |
> |
error(SYSTEM, errmsg); |
211 |
> |
} |
212 |
> |
} else if (!(fp = fopen(inspec, "r"))) { |
213 |
> |
sprintf(errmsg, "cannot open file '%s'", inspec); |
214 |
|
error(SYSTEM, errmsg); |
215 |
|
} |
216 |
|
#ifdef getc_unlocked |
218 |
|
#endif |
219 |
|
if (dtype != DTascii) |
220 |
|
SET_FILE_BINARY(fp); /* doesn't really work */ |
221 |
< |
if (dtype == DTfromHeader) |
222 |
< |
dtype = getDTfromHeader(fp); |
221 |
> |
if (!dtype | !dimsOK) { /* expecting header? */ |
222 |
> |
char *err = cm_getheader(&dtype, &nrows, &ncols, &swap, scale, fp); |
223 |
> |
if (err) |
224 |
> |
error(USER, err); |
225 |
> |
dimsOK = ncols > 0 && ( nrows > 0 || |
226 |
> |
(dtype != DTrgbe) & (dtype != DTxyze) ); |
227 |
> |
} |
228 |
> |
if (!dimsOK && !fscnresolu(&ncols, &nrows, fp)) |
229 |
> |
error(USER, "unspecified matrix size"); |
230 |
|
switch (dtype) { |
231 |
|
case DTascii: |
232 |
|
case DTfloat: |
233 |
|
case DTdouble: |
234 |
|
break; |
235 |
+ |
case DTrgbe: |
236 |
+ |
case DTxyze: |
237 |
+ |
cm = cm_load_rgbe(fp, nrows, ncols); |
238 |
+ |
goto cleanup; |
239 |
|
default: |
240 |
|
error(USER, "unexpected data type in cm_load()"); |
241 |
|
} |
242 |
|
if (nrows <= 0) { /* don't know length? */ |
243 |
|
int guessrows = 147; /* usually big enough */ |
244 |
< |
if ((dtype != DTascii) & (fp != stdin)) { |
244 |
> |
if (cm_elem_size[dtype] && (fp != stdin) & (inspec[0] != '!')) { |
245 |
|
long startpos = ftell(fp); |
246 |
|
if (fseek(fp, 0L, SEEK_END) == 0) { |
247 |
+ |
long rowsiz = (long)ncols*cm_elem_size[dtype]; |
248 |
|
long endpos = ftell(fp); |
123 |
– |
long elemsiz = 3*(dtype==DTfloat ? |
124 |
– |
sizeof(float) : sizeof(double)); |
249 |
|
|
250 |
< |
if ((endpos - startpos) % (ncols*elemsiz)) { |
250 |
> |
if ((endpos - startpos) % rowsiz) { |
251 |
|
sprintf(errmsg, |
252 |
|
"improper length for binary file '%s'", |
253 |
< |
fname); |
253 |
> |
inspec); |
254 |
|
error(USER, errmsg); |
255 |
|
} |
256 |
< |
guessrows = (endpos - startpos)/(ncols*elemsiz); |
256 |
> |
guessrows = (endpos - startpos)/rowsiz; |
257 |
|
if (fseek(fp, startpos, SEEK_SET) < 0) { |
258 |
|
sprintf(errmsg, |
259 |
|
"fseek() error on file '%s'", |
260 |
< |
fname); |
260 |
> |
inspec); |
261 |
|
error(SYSTEM, errmsg); |
262 |
|
} |
263 |
|
nrows = guessrows; /* we're confident */ |
266 |
|
cm = cm_alloc(guessrows, ncols); |
267 |
|
} else |
268 |
|
cm = cm_alloc(nrows, ncols); |
269 |
< |
if (cm == NULL) /* XXX never happens */ |
269 |
> |
if (!cm) /* XXX never happens */ |
270 |
|
return(NULL); |
271 |
|
if (dtype == DTascii) { /* read text file */ |
272 |
|
int maxrow = (nrows > 0 ? nrows : 32000); |
273 |
|
int r, c; |
274 |
|
for (r = 0; r < maxrow; r++) { |
275 |
|
if (r >= cm->nrows) /* need more space? */ |
276 |
< |
cm = cm_resize(cm, 2*cm->nrows); |
276 |
> |
cm = cm_resize(cm, cm->nrows+ROWINC); |
277 |
|
for (c = 0; c < ncols; c++) { |
278 |
|
COLORV *cv = cm_lval(cm,r,c); |
279 |
< |
if (fscanf(fp, COLSPEC, cv, cv+1, cv+2) != 3) |
279 |
> |
if (fscanf(fp, COLSPEC, cv, cv+1, cv+2) != 3) { |
280 |
|
if ((nrows <= 0) & (r > 0) & !c) { |
281 |
|
cm = cm_resize(cm, maxrow=r); |
282 |
|
break; |
283 |
|
} else |
284 |
|
goto EOFerror; |
285 |
+ |
} |
286 |
|
} |
287 |
|
} |
288 |
|
while ((c = getc(fp)) != EOF) |
289 |
|
if (!isspace(c)) { |
290 |
|
sprintf(errmsg, |
291 |
< |
"unexpected data at end of ascii file %s", |
292 |
< |
fname); |
291 |
> |
"unexpected data at end of ascii input '%s'", |
292 |
> |
inspec); |
293 |
|
error(WARNING, errmsg); |
294 |
|
break; |
295 |
|
} |
296 |
|
} else { /* read binary file */ |
297 |
|
if (sizeof(COLOR) == cm_elem_size[dtype]) { |
298 |
< |
int nread = 0; |
298 |
> |
size_t nread = 0; |
299 |
|
do { /* read all we can */ |
300 |
< |
nread += fread(cm->cmem + 3*nread, |
301 |
< |
3*sizeof(COLORV), |
302 |
< |
cm->nrows*cm->ncols - nread, |
300 |
> |
nread += getbinary(cm->cmem + 3*nread, |
301 |
> |
sizeof(COLOR), |
302 |
> |
(size_t)cm->nrows*cm->ncols - nread, |
303 |
|
fp); |
304 |
|
if (nrows <= 0) { /* unknown length */ |
305 |
< |
if (nread == cm->nrows*cm->ncols) |
305 |
> |
if (nread == (size_t)cm->nrows*cm->ncols) |
306 |
|
/* need more space? */ |
307 |
< |
cm = cm_resize(cm, 2*cm->nrows); |
307 |
> |
cm = cm_resize(cm, cm->nrows+ROWINC); |
308 |
|
else if (nread && !(nread % cm->ncols)) |
309 |
|
/* seem to be done */ |
310 |
|
cm = cm_resize(cm, nread/cm->ncols); |
311 |
|
else /* ended mid-row */ |
312 |
|
goto EOFerror; |
313 |
< |
} else if (nread < cm->nrows*cm->ncols) |
313 |
> |
} else if (nread < (size_t)cm->nrows*cm->ncols) |
314 |
|
goto EOFerror; |
315 |
< |
} while (nread < cm->nrows*cm->ncols); |
315 |
> |
} while (nread < (size_t)cm->nrows*cm->ncols); |
316 |
|
|
317 |
+ |
if (swap) { |
318 |
+ |
if (sizeof(COLORV) == 4) |
319 |
+ |
swap32((char *)cm->cmem, |
320 |
+ |
3*(size_t)cm->nrows*cm->ncols); |
321 |
+ |
else /* sizeof(COLORV) == 8 */ |
322 |
+ |
swap64((char *)cm->cmem, |
323 |
+ |
3*(size_t)cm->nrows*cm->ncols); |
324 |
+ |
} |
325 |
|
} else if (dtype == DTdouble) { |
326 |
|
double dc[3]; /* load from double */ |
327 |
|
COLORV *cvp = cm->cmem; |
328 |
< |
int n = nrows*ncols; |
328 |
> |
size_t n = (size_t)nrows*ncols; |
329 |
|
|
330 |
|
if (n <= 0) |
331 |
|
goto not_handled; |
332 |
|
while (n--) { |
333 |
< |
if (fread(dc, sizeof(double), 3, fp) != 3) |
333 |
> |
if (getbinary(dc, sizeof(double), 3, fp) != 3) |
334 |
|
goto EOFerror; |
335 |
+ |
if (swap) swap64((char *)dc, 3); |
336 |
|
copycolor(cvp, dc); |
337 |
|
cvp += 3; |
338 |
|
} |
339 |
|
} else /* dtype == DTfloat */ { |
340 |
|
float fc[3]; /* load from float */ |
341 |
|
COLORV *cvp = cm->cmem; |
342 |
< |
int n = nrows*ncols; |
342 |
> |
size_t n = (size_t)nrows*ncols; |
343 |
|
|
344 |
|
if (n <= 0) |
345 |
|
goto not_handled; |
346 |
|
while (n--) { |
347 |
< |
if (fread(fc, sizeof(float), 3, fp) != 3) |
347 |
> |
if (getbinary(fc, sizeof(float), 3, fp) != 3) |
348 |
|
goto EOFerror; |
349 |
+ |
if (swap) swap32((char *)fc, 3); |
350 |
|
copycolor(cvp, fc); |
351 |
|
cvp += 3; |
352 |
|
} |
353 |
|
} |
354 |
|
if (fgetc(fp) != EOF) { |
355 |
|
sprintf(errmsg, |
356 |
< |
"unexpected data at end of binary file %s", |
357 |
< |
fname); |
356 |
> |
"unexpected data at end of binary input '%s'", |
357 |
> |
inspec); |
358 |
|
error(WARNING, errmsg); |
359 |
|
} |
360 |
|
} |
361 |
< |
if (fp != stdin) |
362 |
< |
fclose(fp); |
361 |
> |
cleanup: |
362 |
> |
if (fp != stdin) { |
363 |
> |
if (inspec[0] != '!') |
364 |
> |
fclose(fp); |
365 |
> |
else if (pclose(fp)) { |
366 |
> |
sprintf(errmsg, "error running command '%s'", inspec); |
367 |
> |
error(WARNING, errmsg); |
368 |
> |
} |
369 |
> |
} |
370 |
|
#ifdef getc_unlocked |
371 |
|
else |
372 |
|
funlockfile(fp); |
373 |
|
#endif |
374 |
+ |
if ((scale[0] < .99) | (scale[0] > 1.01) | |
375 |
+ |
(scale[1] < .99) | (scale[1] > 1.01) | |
376 |
+ |
(scale[2] < .99) | (scale[2] > 1.01)) { |
377 |
+ |
size_t n = (size_t)ncols*nrows; |
378 |
+ |
COLORV *mp = cm->cmem; |
379 |
+ |
while (n--) { /* apply exposure scaling */ |
380 |
+ |
*mp++ *= scale[0]; |
381 |
+ |
*mp++ *= scale[1]; |
382 |
+ |
*mp++ *= scale[2]; |
383 |
+ |
} |
384 |
+ |
} |
385 |
|
return(cm); |
386 |
|
EOFerror: |
387 |
< |
sprintf(errmsg, "unexpected EOF reading %s", fname); |
387 |
> |
sprintf(errmsg, "unexpected EOF reading %s", inspec); |
388 |
|
error(USER, errmsg); |
389 |
+ |
return(NULL); |
390 |
|
not_handled: |
391 |
|
error(INTERNAL, "unhandled data size or length in cm_load()"); |
392 |
|
return(NULL); /* gratis return */ |
399 |
|
CMATRIX *cvr; |
400 |
|
int dr; |
401 |
|
|
402 |
+ |
if (!cm) |
403 |
+ |
return(NULL); |
404 |
|
if ((c < 0) | (c >= cm->ncols)) |
405 |
|
error(INTERNAL, "column requested outside matrix"); |
406 |
|
cvr = cm_alloc(cm->nrows, 1); |
407 |
< |
if (cvr == NULL) |
407 |
> |
if (!cvr) |
408 |
|
return(NULL); |
409 |
|
for (dr = 0; dr < cm->nrows; dr++) { |
410 |
|
const COLORV *sp = cm_lval(cm,dr,c); |
416 |
|
return(cvr); |
417 |
|
} |
418 |
|
|
263 |
– |
/* Scale a matrix by a single value */ |
264 |
– |
CMATRIX * |
265 |
– |
cm_scale(const CMATRIX *cm1, const COLOR sca) |
266 |
– |
{ |
267 |
– |
CMATRIX *cmr; |
268 |
– |
int dr, dc; |
269 |
– |
|
270 |
– |
cmr = cm_alloc(cm1->nrows, cm1->ncols); |
271 |
– |
if (cmr == NULL) |
272 |
– |
return(NULL); |
273 |
– |
for (dr = 0; dr < cmr->nrows; dr++) |
274 |
– |
for (dc = 0; dc < cmr->ncols; dc++) { |
275 |
– |
const COLORV *sp = cm_lval(cm1,dr,dc); |
276 |
– |
COLORV *dp = cm_lval(cmr,dr,dc); |
277 |
– |
dp[0] = sp[0] * sca[0]; |
278 |
– |
dp[1] = sp[1] * sca[1]; |
279 |
– |
dp[2] = sp[2] * sca[2]; |
280 |
– |
} |
281 |
– |
return(cmr); |
282 |
– |
} |
283 |
– |
|
419 |
|
/* Multiply two matrices (or a matrix and a vector) and allocate the result */ |
420 |
|
CMATRIX * |
421 |
|
cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2) |
424 |
|
CMATRIX *cmr; |
425 |
|
int dr, dc, i; |
426 |
|
|
427 |
+ |
if (!cm1 | !cm2) |
428 |
+ |
return(NULL); |
429 |
|
if ((cm1->ncols <= 0) | (cm1->ncols != cm2->nrows)) |
430 |
|
error(INTERNAL, "matrix dimension mismatch in cm_multiply()"); |
431 |
|
cmr = cm_alloc(cm1->nrows, cm2->ncols); |
432 |
< |
if (cmr == NULL) |
432 |
> |
if (!cmr) |
433 |
|
return(NULL); |
434 |
|
/* optimization: check for zero rows & cols */ |
435 |
|
if (((cm1->nrows > 5) | (cm2->ncols > 5)) & (cm1->ncols > 5)) { |
452 |
|
for (dr = 0; dr < cmr->nrows; dr++) |
453 |
|
for (dc = 0; dc < cmr->ncols; dc++) { |
454 |
|
COLORV *dp = cm_lval(cmr,dr,dc); |
455 |
+ |
double res[3]; |
456 |
|
dp[0] = dp[1] = dp[2] = 0; |
457 |
< |
if (rowcheck != NULL && !rowcheck[dr]) |
457 |
> |
if (rowcheck && !rowcheck[dr]) |
458 |
|
continue; |
459 |
< |
if (colcheck != NULL && !colcheck[dc]) |
459 |
> |
if (colcheck && !colcheck[dc]) |
460 |
|
continue; |
461 |
+ |
res[0] = res[1] = res[2] = 0; |
462 |
|
for (i = 0; i < cm1->ncols; i++) { |
463 |
|
const COLORV *cp1 = cm_lval(cm1,dr,i); |
464 |
|
const COLORV *cp2 = cm_lval(cm2,i,dc); |
465 |
< |
dp[0] += cp1[0] * cp2[0]; |
466 |
< |
dp[1] += cp1[1] * cp2[1]; |
467 |
< |
dp[2] += cp1[2] * cp2[2]; |
465 |
> |
res[0] += (double)cp1[0] * cp2[0]; |
466 |
> |
res[1] += (double)cp1[1] * cp2[1]; |
467 |
> |
res[2] += (double)cp1[2] * cp2[2]; |
468 |
|
} |
469 |
+ |
copycolor(dp, res); |
470 |
|
} |
471 |
< |
if (rowcheck != NULL) free(rowcheck); |
472 |
< |
if (colcheck != NULL) free(colcheck); |
471 |
> |
if (rowcheck) free(rowcheck); |
472 |
> |
if (colcheck) free(colcheck); |
473 |
|
return(cmr); |
474 |
|
} |
475 |
|
|
477 |
|
int |
478 |
|
cm_write(const CMATRIX *cm, int dtype, FILE *fp) |
479 |
|
{ |
480 |
< |
const COLORV *mp = cm->cmem; |
481 |
< |
int r, c; |
480 |
> |
static const char tabEOL[2] = {'\t','\n'}; |
481 |
> |
const COLORV *mp; |
482 |
> |
int r, c; |
483 |
> |
size_t n, rv; |
484 |
|
|
485 |
+ |
if (!cm) |
486 |
+ |
return(0); |
487 |
+ |
#ifdef getc_unlocked |
488 |
+ |
flockfile(fp); |
489 |
+ |
#endif |
490 |
+ |
mp = cm->cmem; |
491 |
|
switch (dtype) { |
492 |
|
case DTascii: |
493 |
< |
for (r = 0; r < cm->nrows; r++) { |
493 |
> |
for (r = 0; r < cm->nrows; r++) |
494 |
|
for (c = 0; c < cm->ncols; c++, mp += 3) |
495 |
< |
fprintf(fp, "\t%.6e %.6e %.6e", |
496 |
< |
mp[0], mp[1], mp[2]); |
497 |
< |
fputc('\n', fp); |
350 |
< |
} |
495 |
> |
fprintf(fp, "%.6e %.6e %.6e%c", |
496 |
> |
mp[0], mp[1], mp[2], |
497 |
> |
tabEOL[c >= cm->ncols-1]); |
498 |
|
break; |
499 |
|
case DTfloat: |
500 |
|
case DTdouble: |
501 |
|
if (sizeof(COLOR) == cm_elem_size[dtype]) { |
502 |
< |
r = cm->ncols*cm->nrows; |
503 |
< |
while (r > 0) { |
504 |
< |
c = fwrite(mp, sizeof(COLOR), r, fp); |
505 |
< |
if (c <= 0) |
502 |
> |
n = (size_t)cm->ncols*cm->nrows; |
503 |
> |
while (n > 0) { |
504 |
> |
rv = fwrite(mp, sizeof(COLOR), n, fp); |
505 |
> |
if (rv <= 0) |
506 |
|
return(0); |
507 |
< |
mp += 3*c; |
508 |
< |
r -= c; |
507 |
> |
mp += 3*rv; |
508 |
> |
n -= rv; |
509 |
|
} |
510 |
|
} else if (dtype == DTdouble) { |
511 |
|
double dc[3]; |
512 |
< |
r = cm->ncols*cm->nrows; |
513 |
< |
while (r--) { |
512 |
> |
n = (size_t)cm->ncols*cm->nrows; |
513 |
> |
while (n--) { |
514 |
|
copycolor(dc, mp); |
515 |
< |
if (fwrite(dc, sizeof(double), 3, fp) != 3) |
515 |
> |
if (putbinary(dc, sizeof(double), 3, fp) != 3) |
516 |
|
return(0); |
517 |
|
mp += 3; |
518 |
|
} |
519 |
|
} else /* dtype == DTfloat */ { |
520 |
|
float fc[3]; |
521 |
< |
r = cm->ncols*cm->nrows; |
522 |
< |
while (r--) { |
521 |
> |
n = (size_t)cm->ncols*cm->nrows; |
522 |
> |
while (n--) { |
523 |
|
copycolor(fc, mp); |
524 |
< |
if (fwrite(fc, sizeof(float), 3, fp) != 3) |
524 |
> |
if (putbinary(fc, sizeof(float), 3, fp) != 3) |
525 |
|
return(0); |
526 |
|
mp += 3; |
527 |
|
} |
538 |
|
fputs("Unsupported data type in cm_write()!\n", stderr); |
539 |
|
return(0); |
540 |
|
} |
541 |
+ |
#ifdef getc_unlocked |
542 |
+ |
funlockfile(fp); |
543 |
+ |
#endif |
544 |
|
return(fflush(fp) == 0); |
545 |
|
} |