12 |
|
#include "resolu.h" |
13 |
|
#include "paths.h" |
14 |
|
#include "rmatrix.h" |
15 |
+ |
#if !defined(_WIN32) && !defined(_WIN64) |
16 |
+ |
#include <sys/mman.h> |
17 |
+ |
#endif |
18 |
|
|
19 |
|
static char rmx_mismatch_warn[] = "WARNING: data type mismatch\n"; |
20 |
|
|
21 |
< |
/* Allocate a nr x nc matrix with n components */ |
21 |
> |
#define array_size(rm) (sizeof(double)*(rm)->nrows*(rm)->ncols*(rm)->ncomp) |
22 |
> |
#define mapped_size(rm) ((char *)(rm)->mtx + array_size(rm) - (char *)(rm)->mapped) |
23 |
> |
|
24 |
> |
/* Initialize a RMATRIX struct but don't allocate array space */ |
25 |
|
RMATRIX * |
26 |
+ |
rmx_new(int nr, int nc, int n) |
27 |
+ |
{ |
28 |
+ |
RMATRIX *dnew = (RMATRIX *)calloc(1, sizeof(RMATRIX)); |
29 |
+ |
|
30 |
+ |
if (dnew) { |
31 |
+ |
dnew->dtype = DTdouble; |
32 |
+ |
dnew->nrows = nr; |
33 |
+ |
dnew->ncols = nc; |
34 |
+ |
dnew->ncomp = n; |
35 |
+ |
} |
36 |
+ |
return(dnew); |
37 |
+ |
} |
38 |
+ |
|
39 |
+ |
/* Prepare a RMATRIX for writing (allocate array if needed) */ |
40 |
+ |
int |
41 |
+ |
rmx_prepare(RMATRIX *rm) |
42 |
+ |
{ |
43 |
+ |
if (!rm) return(0); |
44 |
+ |
if (rm->mtx) |
45 |
+ |
return(1); |
46 |
+ |
rm->mtx = (double *)malloc(array_size(rm)); |
47 |
+ |
return(rm->mtx != NULL); |
48 |
+ |
} |
49 |
+ |
|
50 |
+ |
/* Call rmx_new() and rmx_prepare() */ |
51 |
+ |
RMATRIX * |
52 |
|
rmx_alloc(int nr, int nc, int n) |
53 |
|
{ |
54 |
< |
RMATRIX *dnew; |
54 |
> |
RMATRIX *dnew = rmx_new(nr, nc, n); |
55 |
|
|
56 |
< |
if ((nr <= 0) | (nc <= 0) | (n <= 0)) |
57 |
< |
return(NULL); |
58 |
< |
dnew = (RMATRIX *)malloc(sizeof(RMATRIX)-sizeof(dnew->mtx) + |
59 |
< |
sizeof(dnew->mtx[0])*n*nr*nc); |
28 |
< |
if (!dnew) |
29 |
< |
return(NULL); |
30 |
< |
dnew->nrows = nr; dnew->ncols = nc; dnew->ncomp = n; |
31 |
< |
dnew->dtype = DTdouble; |
32 |
< |
dnew->swapin = 0; |
33 |
< |
dnew->info = NULL; |
56 |
> |
if (dnew && !rmx_prepare(dnew)) { |
57 |
> |
rmx_free(dnew); |
58 |
> |
dnew = NULL; |
59 |
> |
} |
60 |
|
return(dnew); |
61 |
|
} |
62 |
|
|
67 |
|
if (!rm) return; |
68 |
|
if (rm->info) |
69 |
|
free(rm->info); |
70 |
+ |
#ifdef MAP_FILE |
71 |
+ |
if (rm->mapped) |
72 |
+ |
munmap(rm->mapped, mapped_size(rm)); |
73 |
+ |
else |
74 |
+ |
#endif |
75 |
+ |
free(rm->mtx); |
76 |
|
free(rm); |
77 |
|
} |
78 |
|
|
93 |
|
int |
94 |
|
rmx_addinfo(RMATRIX *rm, const char *info) |
95 |
|
{ |
96 |
+ |
int oldlen = 0; |
97 |
+ |
|
98 |
|
if (!rm || !info || !*info) |
99 |
|
return(0); |
100 |
|
if (!rm->info) { |
101 |
|
rm->info = (char *)malloc(strlen(info)+1); |
102 |
|
if (rm->info) rm->info[0] = '\0'; |
103 |
< |
} else |
103 |
> |
} else { |
104 |
> |
oldlen = strlen(rm->info); |
105 |
|
rm->info = (char *)realloc(rm->info, |
106 |
< |
strlen(rm->info)+strlen(info)+1); |
106 |
> |
oldlen+strlen(info)+1); |
107 |
> |
} |
108 |
|
if (!rm->info) |
109 |
|
return(0); |
110 |
< |
strcat(rm->info, info); |
110 |
> |
strcpy(rm->info+oldlen, info); |
111 |
|
return(1); |
112 |
|
} |
113 |
|
|
137 |
|
return(0); |
138 |
|
} |
139 |
|
if (isexpos(s)) { |
140 |
< |
double d = exposval(s); |
141 |
< |
scalecolor(ip->mtx, d); |
140 |
> |
float f = exposval(s); |
141 |
> |
scalecolor(ip->cexp, f); |
142 |
|
return(0); |
143 |
|
} |
144 |
|
if (iscolcor(s)) { |
145 |
|
COLOR ctmp; |
146 |
|
colcorval(ctmp, s); |
147 |
< |
multcolor(ip->mtx, ctmp); |
147 |
> |
multcolor(ip->cexp, ctmp); |
148 |
|
return(0); |
149 |
|
} |
150 |
|
if (!formatval(fmt, s)) { |
151 |
|
rmx_addinfo(ip, s); |
152 |
|
return(0); |
153 |
< |
} |
153 |
> |
} /* else check format */ |
154 |
|
for (i = 1; i < DTend; i++) |
155 |
|
if (!strcmp(fmt, cm_fmt_id[i])) { |
156 |
|
ip->dtype = i; |
164 |
|
{ |
165 |
|
int i, j, k; |
166 |
|
|
167 |
+ |
if (!rmx_prepare(rm)) |
168 |
+ |
return(0); |
169 |
|
for (i = 0; i < rm->nrows; i++) |
170 |
< |
for (j = 0; j < rm->ncols; j++) |
170 |
> |
for (j = 0; j < rm->ncols; j++) { |
171 |
> |
double *dp = rmx_lval(rm,i,j); |
172 |
|
for (k = 0; k < rm->ncomp; k++) |
173 |
< |
if (fscanf(fp, "%lf", &rmx_lval(rm,i,j,k)) != 1) |
173 |
> |
if (fscanf(fp, "%lf", &dp[k]) != 1) |
174 |
|
return(0); |
175 |
+ |
} |
176 |
|
return(1); |
177 |
|
} |
178 |
|
|
186 |
|
fputs("Unsupported # components in rmx_load_float()\n", stderr); |
187 |
|
exit(1); |
188 |
|
} |
189 |
+ |
if (!rmx_prepare(rm)) |
190 |
+ |
return(0); |
191 |
|
for (i = 0; i < rm->nrows; i++) |
192 |
|
for (j = 0; j < rm->ncols; j++) { |
193 |
+ |
double *dp = rmx_lval(rm,i,j); |
194 |
|
if (getbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) |
195 |
|
return(0); |
196 |
|
if (rm->swapin) |
197 |
|
swap32((char *)val, rm->ncomp); |
198 |
|
for (k = rm->ncomp; k--; ) |
199 |
< |
rmx_lval(rm,i,j,k) = val[k]; |
199 |
> |
dp[k] = val[k]; |
200 |
|
} |
201 |
|
return(1); |
202 |
|
} |
205 |
|
rmx_load_double(RMATRIX *rm, FILE *fp) |
206 |
|
{ |
207 |
|
int i; |
208 |
< |
|
209 |
< |
if ((char *)&rmx_lval(rm,1,0,0) - (char *)&rmx_lval(rm,0,0,0) != |
210 |
< |
sizeof(double)*rm->ncols*rm->ncomp) { |
211 |
< |
fputs("Code error in rmx_load_double()\n", stderr); |
212 |
< |
exit(1); |
208 |
> |
#ifdef MAP_FILE |
209 |
> |
long pos; /* map memory to file if possible */ |
210 |
> |
if (!rm->swapin && array_size(rm) >= 1L<<20 && |
211 |
> |
(pos = ftell(fp)) >= 0 && !(pos % sizeof(double))) { |
212 |
> |
rm->mapped = mmap(NULL, array_size(rm)+pos, PROT_READ|PROT_WRITE, |
213 |
> |
MAP_PRIVATE, fileno(fp), 0); |
214 |
> |
if (rm->mapped != MAP_FAILED) { |
215 |
> |
rm->mtx = (double *)rm->mapped + pos/sizeof(double); |
216 |
> |
return(1); |
217 |
> |
} |
218 |
> |
rm->mapped = NULL; |
219 |
|
} |
220 |
+ |
#endif |
221 |
+ |
if (!rmx_prepare(rm)) |
222 |
+ |
return(0); |
223 |
|
for (i = 0; i < rm->nrows; i++) { |
224 |
< |
if (getbinary(&rmx_lval(rm,i,0,0), sizeof(double)*rm->ncomp, |
224 |
> |
if (getbinary(rmx_lval(rm,i,0), sizeof(double)*rm->ncomp, |
225 |
|
rm->ncols, fp) != rm->ncols) |
226 |
|
return(0); |
227 |
|
if (rm->swapin) |
228 |
< |
swap64((char *)&rmx_lval(rm,i,0,0), rm->ncols*rm->ncomp); |
228 |
> |
swap64((char *)rmx_lval(rm,i,0), rm->ncols*rm->ncomp); |
229 |
|
} |
230 |
|
return(1); |
231 |
|
} |
238 |
|
|
239 |
|
if (!scan) |
240 |
|
return(0); |
241 |
+ |
if (!rmx_prepare(rm)) |
242 |
+ |
return(0); |
243 |
|
for (i = 0; i < rm->nrows; i++) { |
244 |
+ |
double *dp = rmx_lval(rm,i,j); |
245 |
|
if (freadscan(scan, rm->ncols, fp) < 0) { |
246 |
|
free(scan); |
247 |
|
return(0); |
248 |
|
} |
249 |
< |
for (j = rm->ncols; j--; ) { |
250 |
< |
rmx_lval(rm,i,j,0) = colval(scan[j],RED); |
251 |
< |
rmx_lval(rm,i,j,1) = colval(scan[j],GRN); |
252 |
< |
rmx_lval(rm,i,j,2) = colval(scan[j],BLU); |
249 |
> |
for (j = 0; j < rm->ncols; j++, dp += 3) { |
250 |
> |
dp[0] = colval(scan[j],RED); |
251 |
> |
dp[1] = colval(scan[j],GRN); |
252 |
> |
dp[2] = colval(scan[j],BLU); |
253 |
|
} |
254 |
|
} |
255 |
|
free(scan); |
258 |
|
|
259 |
|
/* Load matrix from supported file type */ |
260 |
|
RMATRIX * |
261 |
< |
rmx_load(const char *inspec) |
261 |
> |
rmx_load(const char *inspec, RMPref rmp) |
262 |
|
{ |
263 |
< |
FILE *fp = stdin; |
209 |
< |
RMATRIX dinfo; |
263 |
> |
FILE *fp; |
264 |
|
RMATRIX *dnew; |
265 |
|
|
266 |
< |
if (!inspec) { /* reading from stdin? */ |
267 |
< |
inspec = "<stdin>"; |
268 |
< |
SET_FILE_BINARY(stdin); |
266 |
> |
if (!inspec) |
267 |
> |
inspec = stdin_name; |
268 |
> |
else if (!*inspec) |
269 |
> |
return(NULL); |
270 |
> |
if (inspec == stdin_name) { /* reading from stdin? */ |
271 |
> |
fp = stdin; |
272 |
|
} else if (inspec[0] == '!') { |
273 |
|
if (!(fp = popen(inspec+1, "r"))) |
274 |
|
return(NULL); |
218 |
– |
SET_FILE_BINARY(fp); |
275 |
|
} else { |
276 |
|
const char *sp = inspec; /* check suffix */ |
277 |
|
while (*sp) |
279 |
|
while (sp > inspec && sp[-1] != '.') |
280 |
|
--sp; |
281 |
|
if (!strcasecmp(sp, "XML")) { /* assume it's a BSDF */ |
282 |
< |
CMATRIX *cm = cm_loadBTDF((char *)inspec); |
282 |
> |
CMATRIX *cm = rmp==RMPtrans ? cm_loadBTDF(inspec) : |
283 |
> |
cm_loadBRDF(inspec, rmp==RMPreflB) ; |
284 |
|
if (!cm) |
285 |
|
return(NULL); |
286 |
|
dnew = rmx_from_cmatrix(cm); |
289 |
|
return(dnew); |
290 |
|
} |
291 |
|
/* else open it ourselves */ |
292 |
< |
if (!(fp = fopen(inspec, "rb"))) |
292 |
> |
if (!(fp = fopen(inspec, "r"))) |
293 |
|
return(NULL); |
294 |
|
} |
295 |
+ |
SET_FILE_BINARY(fp); |
296 |
|
#ifdef getc_unlocked |
297 |
|
flockfile(fp); |
298 |
|
#endif |
299 |
< |
dinfo.nrows = dinfo.ncols = dinfo.ncomp = 0; |
242 |
< |
dinfo.dtype = DTascii; /* assumed w/o FORMAT */ |
243 |
< |
dinfo.swapin = 0; |
244 |
< |
dinfo.info = NULL; |
245 |
< |
dinfo.mtx[0] = dinfo.mtx[1] = dinfo.mtx[2] = 1.; |
246 |
< |
if (getheader(fp, get_dminfo, &dinfo) < 0) { |
299 |
> |
if (!(dnew = rmx_new(0,0,3))) { |
300 |
|
fclose(fp); |
301 |
|
return(NULL); |
302 |
|
} |
303 |
< |
if ((dinfo.nrows <= 0) | (dinfo.ncols <= 0)) { |
304 |
< |
if (!fscnresolu(&dinfo.ncols, &dinfo.nrows, fp)) { |
303 |
> |
dnew->dtype = DTascii; /* assumed w/o FORMAT */ |
304 |
> |
dnew->cexp[0] = dnew->cexp[1] = dnew->cexp[2] = 1.f; |
305 |
> |
if (getheader(fp, get_dminfo, dnew) < 0) { |
306 |
> |
fclose(fp); |
307 |
> |
return(NULL); |
308 |
> |
} |
309 |
> |
if ((dnew->nrows <= 0) | (dnew->ncols <= 0)) { |
310 |
> |
if (!fscnresolu(&dnew->ncols, &dnew->nrows, fp)) { |
311 |
|
fclose(fp); |
312 |
|
return(NULL); |
313 |
|
} |
314 |
< |
if (dinfo.ncomp <= 0) |
315 |
< |
dinfo.ncomp = 3; |
257 |
< |
else if ((dinfo.dtype == DTrgbe) | (dinfo.dtype == DTxyze) && |
258 |
< |
dinfo.ncomp != 3) { |
314 |
> |
if ((dnew->dtype == DTrgbe) | (dnew->dtype == DTxyze) && |
315 |
> |
dnew->ncomp != 3) { |
316 |
|
fclose(fp); |
317 |
|
return(NULL); |
318 |
|
} |
319 |
|
} |
320 |
< |
dnew = rmx_alloc(dinfo.nrows, dinfo.ncols, dinfo.ncomp); |
264 |
< |
if (!dnew) { |
265 |
< |
fclose(fp); |
266 |
< |
return(NULL); |
267 |
< |
} |
268 |
< |
dnew->info = dinfo.info; |
269 |
< |
switch (dinfo.dtype) { |
320 |
> |
switch (dnew->dtype) { |
321 |
|
case DTascii: |
322 |
|
SET_FILE_TEXT(fp); |
323 |
|
if (!rmx_load_ascii(dnew, fp)) |
325 |
|
dnew->dtype = DTascii; /* should leave double? */ |
326 |
|
break; |
327 |
|
case DTfloat: |
277 |
– |
dnew->swapin = dinfo.swapin; |
328 |
|
if (!rmx_load_float(dnew, fp)) |
329 |
|
goto loaderr; |
330 |
|
dnew->dtype = DTfloat; |
331 |
|
break; |
332 |
|
case DTdouble: |
283 |
– |
dnew->swapin = dinfo.swapin; |
333 |
|
if (!rmx_load_double(dnew, fp)) |
334 |
|
goto loaderr; |
335 |
|
dnew->dtype = DTdouble; |
338 |
|
case DTxyze: |
339 |
|
if (!rmx_load_rgbe(dnew, fp)) |
340 |
|
goto loaderr; |
341 |
< |
dnew->dtype = dinfo.dtype; /* undo exposure? */ |
342 |
< |
if ((dinfo.mtx[0] != 1.) | (dinfo.mtx[1] != 1.) | |
343 |
< |
(dinfo.mtx[2] != 1.)) { |
344 |
< |
dinfo.mtx[0] = 1./dinfo.mtx[0]; |
345 |
< |
dinfo.mtx[1] = 1./dinfo.mtx[1]; |
346 |
< |
dinfo.mtx[2] = 1./dinfo.mtx[2]; |
347 |
< |
rmx_scale(dnew, dinfo.mtx); |
341 |
> |
/* undo exposure? */ |
342 |
> |
if ((dnew->cexp[0] != 1.f) | (dnew->cexp[1] != 1.f) | |
343 |
> |
(dnew->cexp[2] != 1.f)) { |
344 |
> |
double cmlt[3]; |
345 |
> |
cmlt[0] = 1./dnew->cexp[0]; |
346 |
> |
cmlt[1] = 1./dnew->cexp[1]; |
347 |
> |
cmlt[2] = 1./dnew->cexp[2]; |
348 |
> |
rmx_scale(dnew, cmlt); |
349 |
|
} |
350 |
+ |
dnew->swapin = 0; |
351 |
|
break; |
352 |
|
default: |
353 |
|
goto loaderr; |
382 |
|
|
383 |
|
for (i = 0; i < rm->nrows; i++) { |
384 |
|
for (j = 0; j < rm->ncols; j++) { |
385 |
+ |
const double *dp = rmx_lval(rm,i,j); |
386 |
|
for (k = 0; k < rm->ncomp; k++) |
387 |
< |
fprintf(fp, fmt, rmx_lval(rm,i,j,k)); |
387 |
> |
fprintf(fp, fmt, dp[k]); |
388 |
|
fputc('\t', fp); |
389 |
|
} |
390 |
|
fputc('\n', fp); |
404 |
|
} |
405 |
|
for (i = 0; i < rm->nrows; i++) |
406 |
|
for (j = 0; j < rm->ncols; j++) { |
407 |
+ |
const double *dp = rmx_lval(rm,i,j); |
408 |
|
for (k = rm->ncomp; k--; ) |
409 |
< |
val[k] = (float)rmx_lval(rm,i,j,k); |
410 |
< |
if (putbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) |
409 |
> |
val[k] = (float)dp[k]; |
410 |
> |
if (putbinary(val, sizeof(float), rm->ncomp, fp) != rm->ncomp) |
411 |
|
return(0); |
412 |
|
} |
413 |
|
return(1); |
416 |
|
static int |
417 |
|
rmx_write_double(const RMATRIX *rm, FILE *fp) |
418 |
|
{ |
419 |
< |
int i, j; |
419 |
> |
int i; |
420 |
|
|
421 |
|
for (i = 0; i < rm->nrows; i++) |
422 |
< |
for (j = 0; j < rm->ncols; j++) |
423 |
< |
if (putbinary(&rmx_lval(rm,i,j,0), sizeof(double), rm->ncomp, fp) != rm->ncomp) |
422 |
> |
if (putbinary(rmx_lval(rm,i,0), sizeof(double)*rm->ncomp, |
423 |
> |
rm->ncols, fp) != rm->ncols) |
424 |
|
return(0); |
425 |
|
return(1); |
426 |
|
} |
434 |
|
if (!scan) |
435 |
|
return(0); |
436 |
|
for (i = 0; i < rm->nrows; i++) { |
437 |
< |
for (j = rm->ncols; j--; ) |
438 |
< |
setcolr(scan[j], rmx_lval(rm,i,j,0), |
439 |
< |
rmx_lval(rm,i,j,1), |
440 |
< |
rmx_lval(rm,i,j,2) ); |
437 |
> |
for (j = rm->ncols; j--; ) { |
438 |
> |
const double *dp = rmx_lval(rm,i,j); |
439 |
> |
if (rm->ncomp == 1) |
440 |
> |
setcolr(scan[j], dp[0], dp[0], dp[0]); |
441 |
> |
else |
442 |
> |
setcolr(scan[j], dp[0], dp[1], dp[2]); |
443 |
> |
} |
444 |
|
if (fwritecolrs(scan, rm->ncols, fp) < 0) { |
445 |
|
free(scan); |
446 |
|
return(0); |
450 |
|
return(1); |
451 |
|
} |
452 |
|
|
453 |
+ |
/* Check if CIE XYZ primaries were specified */ |
454 |
+ |
static int |
455 |
+ |
findCIEprims(const char *info) |
456 |
+ |
{ |
457 |
+ |
RGBPRIMS prims; |
458 |
+ |
|
459 |
+ |
if (!info) |
460 |
+ |
return(0); |
461 |
+ |
info = strstr(info, PRIMARYSTR); |
462 |
+ |
if (!info || !primsval(prims, info)) |
463 |
+ |
return(0); |
464 |
+ |
|
465 |
+ |
return((prims[RED][CIEX] > .99) & (prims[RED][CIEY] < .01) && |
466 |
+ |
(prims[GRN][CIEX] < .01) & (prims[GRN][CIEY] > .99) && |
467 |
+ |
(prims[BLU][CIEX] < .01) & (prims[BLU][CIEY] < .01)); |
468 |
+ |
} |
469 |
+ |
|
470 |
|
/* Write matrix to file type indicated by dtype */ |
471 |
|
int |
472 |
|
rmx_write(const RMATRIX *rm, int dtype, FILE *fp) |
473 |
|
{ |
401 |
– |
RMATRIX *mydm = NULL; |
474 |
|
int ok = 1; |
475 |
|
|
476 |
< |
if (!rm | !fp) |
476 |
> |
if (!rm | !fp || !rm->mtx) |
477 |
|
return(0); |
478 |
|
#ifdef getc_unlocked |
479 |
|
flockfile(fp); |
483 |
|
fputs(rm->info, fp); |
484 |
|
if (dtype == DTfromHeader) |
485 |
|
dtype = rm->dtype; |
486 |
< |
else if ((dtype == DTrgbe) & (rm->dtype == DTxyze)) |
486 |
> |
else if (dtype == DTrgbe && (rm->dtype == DTxyze || |
487 |
> |
findCIEprims(rm->info))) |
488 |
|
dtype = DTxyze; |
489 |
|
else if ((dtype == DTxyze) & (rm->dtype == DTrgbe)) |
490 |
|
dtype = DTrgbe; |
492 |
|
fprintf(fp, "NROWS=%d\n", rm->nrows); |
493 |
|
fprintf(fp, "NCOLS=%d\n", rm->ncols); |
494 |
|
fprintf(fp, "NCOMP=%d\n", rm->ncomp); |
495 |
< |
} else if (rm->ncomp != 3) { /* wrong # components? */ |
496 |
< |
double cmtx[3]; |
424 |
< |
if (rm->ncomp != 1) /* only convert grayscale */ |
425 |
< |
return(0); |
426 |
< |
cmtx[0] = cmtx[1] = cmtx[2] = 1; |
427 |
< |
mydm = rmx_transform(rm, 3, cmtx); |
428 |
< |
if (!mydm) |
429 |
< |
return(0); |
430 |
< |
rm = mydm; |
431 |
< |
} |
495 |
> |
} else if ((rm->ncomp != 3) & (rm->ncomp != 1)) |
496 |
> |
return(0); /* wrong # components */ |
497 |
|
if ((dtype == DTfloat) | (dtype == DTdouble)) |
498 |
|
fputendian(fp); /* important to record */ |
499 |
< |
fputformat((char *)cm_fmt_id[dtype], fp); |
499 |
> |
fputformat(cm_fmt_id[dtype], fp); |
500 |
|
fputc('\n', fp); |
501 |
|
switch (dtype) { /* write data */ |
502 |
|
case DTascii: |
520 |
|
#ifdef getc_unlocked |
521 |
|
funlockfile(fp); |
522 |
|
#endif |
458 |
– |
if (mydm) |
459 |
– |
rmx_free(mydm); |
523 |
|
return(ok); |
524 |
|
} |
525 |
|
|
532 |
|
|
533 |
|
if (!rid) |
534 |
|
return(NULL); |
535 |
< |
memset(rid->mtx, 0, sizeof(rid->mtx[0])*n*dim*dim); |
536 |
< |
for (i = dim; i--; ) |
535 |
> |
memset(rid->mtx, 0, array_size(rid)); |
536 |
> |
for (i = dim; i--; ) { |
537 |
> |
double *dp = rmx_lval(rid,i,i); |
538 |
|
for (k = n; k--; ) |
539 |
< |
rmx_lval(rid,i,i,k) = 1; |
539 |
> |
dp[k] = 1.; |
540 |
> |
} |
541 |
|
return(rid); |
542 |
|
} |
543 |
|
|
554 |
|
return(NULL); |
555 |
|
rmx_addinfo(dnew, rm->info); |
556 |
|
dnew->dtype = rm->dtype; |
557 |
< |
memcpy(dnew->mtx, rm->mtx, |
493 |
< |
sizeof(rm->mtx[0])*rm->ncomp*rm->nrows*rm->ncols); |
557 |
> |
memcpy(dnew->mtx, rm->mtx, array_size(dnew)); |
558 |
|
return(dnew); |
559 |
|
} |
560 |
|
|
585 |
|
dnew->dtype = rm->dtype; |
586 |
|
for (i = dnew->nrows; i--; ) |
587 |
|
for (j = dnew->ncols; j--; ) |
588 |
< |
for (k = dnew->ncomp; k--; ) |
589 |
< |
rmx_lval(dnew,i,j,k) = rmx_lval(rm,j,i,k); |
588 |
> |
memcpy(rmx_lval(dnew,i,j), rmx_lval(rm,i,j), |
589 |
> |
sizeof(double)*dnew->ncomp); |
590 |
|
return(dnew); |
591 |
|
} |
592 |
|
|
610 |
|
for (i = mres->nrows; i--; ) |
611 |
|
for (j = mres->ncols; j--; ) |
612 |
|
for (k = mres->ncomp; k--; ) { |
613 |
< |
long double d = 0; |
613 |
> |
double d = 0; |
614 |
|
for (h = m1->ncols; h--; ) |
615 |
< |
d += rmx_lval(m1,i,h,k) * rmx_lval(m2,h,j,k); |
616 |
< |
rmx_lval(mres,i,j,k) = (double)d; |
615 |
> |
d += rmx_lval(m1,i,h)[k] * rmx_lval(m2,h,j)[k]; |
616 |
> |
rmx_lval(mres,i,j)[k] = d; |
617 |
|
} |
618 |
|
return(mres); |
619 |
|
} |
639 |
|
if (divide) { |
640 |
|
double d; |
641 |
|
if (m2->ncomp == 1) { |
642 |
< |
d = rmx_lval(m2,i,j,0); |
642 |
> |
d = rmx_lval(m2,i,j)[0]; |
643 |
|
if (d == 0) { |
644 |
|
++zeroDivides; |
645 |
|
for (k = m1->ncomp; k--; ) |
646 |
< |
rmx_lval(m1,i,j,k) = 0; |
646 |
> |
rmx_lval(m1,i,j)[k] = 0; |
647 |
|
} else { |
648 |
|
d = 1./d; |
649 |
|
for (k = m1->ncomp; k--; ) |
650 |
< |
rmx_lval(m1,i,j,k) *= d; |
650 |
> |
rmx_lval(m1,i,j)[k] *= d; |
651 |
|
} |
652 |
|
} else |
653 |
|
for (k = m1->ncomp; k--; ) { |
654 |
< |
d = rmx_lval(m2,i,j,k); |
654 |
> |
d = rmx_lval(m2,i,j)[k]; |
655 |
|
if (d == 0) { |
656 |
|
++zeroDivides; |
657 |
< |
rmx_lval(m1,i,j,k) = 0; |
657 |
> |
rmx_lval(m1,i,j)[k] = 0; |
658 |
|
} else |
659 |
< |
rmx_lval(m1,i,j,k) /= d; |
659 |
> |
rmx_lval(m1,i,j)[k] /= d; |
660 |
|
} |
661 |
|
} else { |
662 |
|
if (m2->ncomp == 1) { |
663 |
< |
const double d = rmx_lval(m2,i,j,0); |
663 |
> |
const double d = rmx_lval(m2,i,j)[0]; |
664 |
|
for (k = m1->ncomp; k--; ) |
665 |
< |
rmx_lval(m1,i,j,k) *= d; |
665 |
> |
rmx_lval(m1,i,j)[k] *= d; |
666 |
|
} else |
667 |
|
for (k = m1->ncomp; k--; ) |
668 |
< |
rmx_lval(m1,i,j,k) *= rmx_lval(m2,i,j,k); |
668 |
> |
rmx_lval(m1,i,j)[k] *= rmx_lval(m2,i,j)[k]; |
669 |
|
} |
670 |
|
if (zeroDivides) { |
671 |
|
rmx_addinfo(m1, "WARNING: zero divide(s) corrupted results\n"); |
700 |
|
else |
701 |
|
rmx_addinfo(msum, rmx_mismatch_warn); |
702 |
|
for (i = msum->nrows; i--; ) |
703 |
< |
for (j = msum->ncols; j--; ) |
703 |
> |
for (j = msum->ncols; j--; ) { |
704 |
> |
const double *da = rmx_lval(madd,i,j); |
705 |
> |
double *ds = rmx_lval(msum,i,j); |
706 |
|
for (k = msum->ncomp; k--; ) |
707 |
< |
rmx_lval(msum,i,j,k) += sf[k] * rmx_lval(madd,i,j,k); |
707 |
> |
ds[k] += sf[k] * da[k]; |
708 |
> |
} |
709 |
|
if (mysf) |
710 |
|
free(mysf); |
711 |
|
return(1); |
720 |
|
if (!rm | !sf) |
721 |
|
return(0); |
722 |
|
for (i = rm->nrows; i--; ) |
723 |
< |
for (j = rm->ncols; j--; ) |
723 |
> |
for (j = rm->ncols; j--; ) { |
724 |
> |
double *dp = rmx_lval(rm,i,j); |
725 |
|
for (k = rm->ncomp; k--; ) |
726 |
< |
rmx_lval(rm,i,j,k) *= sf[k]; |
727 |
< |
|
726 |
> |
dp[k] *= sf[k]; |
727 |
> |
} |
728 |
|
if (rm->info) |
729 |
|
rmx_addinfo(rm, "Applied scalar\n"); |
730 |
|
return(1); |
751 |
|
} |
752 |
|
dnew->dtype = msrc->dtype; |
753 |
|
for (i = dnew->nrows; i--; ) |
754 |
< |
for (j = dnew->ncols; j--; ) |
754 |
> |
for (j = dnew->ncols; j--; ) { |
755 |
> |
const double *ds = rmx_lval(msrc,i,j); |
756 |
|
for (kd = dnew->ncomp; kd--; ) { |
757 |
|
double d = 0; |
758 |
|
for (ks = msrc->ncomp; ks--; ) |
759 |
< |
d += cmat[kd*msrc->ncomp + ks] * rmx_lval(msrc,i,j,ks); |
760 |
< |
rmx_lval(dnew,i,j,kd) = d; |
759 |
> |
d += cmat[kd*msrc->ncomp + ks] * ds[ks]; |
760 |
> |
rmx_lval(dnew,i,j)[kd] = d; |
761 |
|
} |
762 |
+ |
} |
763 |
|
return(dnew); |
764 |
|
} |
765 |
|
|
779 |
|
for (i = dnew->nrows; i--; ) |
780 |
|
for (j = dnew->ncols; j--; ) { |
781 |
|
const COLORV *cv = cm_lval(cm,i,j); |
782 |
< |
rmx_lval(dnew,i,j,0) = cv[0]; |
783 |
< |
rmx_lval(dnew,i,j,1) = cv[1]; |
784 |
< |
rmx_lval(dnew,i,j,2) = cv[2]; |
782 |
> |
double *dp = rmx_lval(dnew,i,j); |
783 |
> |
dp[0] = cv[0]; |
784 |
> |
dp[1] = cv[1]; |
785 |
> |
dp[2] = cv[2]; |
786 |
|
} |
787 |
|
return(dnew); |
788 |
|
} |
794 |
|
int i, j; |
795 |
|
CMATRIX *cnew; |
796 |
|
|
797 |
< |
if (!rm || rm->ncomp != 3) |
797 |
> |
if (!rm || !rm->mtx | ((rm->ncomp != 3) & (rm->ncomp != 1))) |
798 |
|
return(NULL); |
799 |
|
cnew = cm_alloc(rm->nrows, rm->ncols); |
800 |
|
if (!cnew) |
801 |
|
return(NULL); |
802 |
|
for (i = cnew->nrows; i--; ) |
803 |
|
for (j = cnew->ncols; j--; ) { |
804 |
< |
COLORV *cv = cm_lval(cnew,i,j); |
805 |
< |
cv[0] = (COLORV)rmx_lval(rm,i,j,0); |
806 |
< |
cv[1] = (COLORV)rmx_lval(rm,i,j,1); |
807 |
< |
cv[2] = (COLORV)rmx_lval(rm,i,j,2); |
804 |
> |
const double *dp = rmx_lval(rm,i,j); |
805 |
> |
COLORV *cv = cm_lval(cnew,i,j); |
806 |
> |
if (rm->ncomp == 1) |
807 |
> |
setcolor(cv, dp[0], dp[0], dp[0]); |
808 |
> |
else |
809 |
> |
setcolor(cv, dp[0], dp[1], dp[2]); |
810 |
|
} |
811 |
|
return(cnew); |
812 |
|
} |