16 |
|
#include <sys/mman.h> |
17 |
|
#endif |
18 |
|
|
19 |
< |
static char rmx_mismatch_warn[] = "WARNING: data type mismatch\n"; |
19 |
> |
static const char rmx_mismatch_warn[] = "WARNING: data type mismatch\n"; |
20 |
|
|
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) |
25 |
|
RMATRIX * |
26 |
|
rmx_new(int nr, int nc, int n) |
27 |
|
{ |
28 |
< |
RMATRIX *dnew = (RMATRIX *)calloc(1, sizeof(RMATRIX)); |
28 |
> |
RMATRIX *dnew; |
29 |
|
|
30 |
+ |
if (n <= 0) |
31 |
+ |
return(NULL); |
32 |
+ |
|
33 |
+ |
dnew = (RMATRIX *)calloc(1, sizeof(RMATRIX)); |
34 |
|
if (dnew) { |
35 |
|
dnew->dtype = DTdouble; |
36 |
|
dnew->nrows = nr; |
37 |
|
dnew->ncols = nc; |
38 |
|
dnew->ncomp = n; |
39 |
+ |
setcolor(dnew->cexp, 1.f, 1.f, 1.f); |
40 |
+ |
memcpy(dnew->wlpart, WLPART, sizeof(dnew->wlpart)); |
41 |
|
} |
42 |
|
return(dnew); |
43 |
|
} |
49 |
|
if (!rm) return(0); |
50 |
|
if (rm->mtx) |
51 |
|
return(1); |
52 |
+ |
if ((rm->nrows <= 0) | (rm->ncols <= 0) | (rm->ncomp <= 0)) |
53 |
+ |
return(0); |
54 |
|
rm->mtx = (double *)malloc(array_size(rm)); |
55 |
|
return(rm->mtx != NULL); |
56 |
|
} |
88 |
|
int |
89 |
|
rmx_newtype(int dtyp1, int dtyp2) |
90 |
|
{ |
91 |
< |
if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) | |
92 |
< |
(dtyp2==DTxyze) | (dtyp2==DTrgbe) |
91 |
> |
if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) | (dtyp1==DTspec) | |
92 |
> |
(dtyp2==DTxyze) | (dtyp2==DTrgbe) | (dtyp2==DTspec) |
93 |
|
&& dtyp1 != dtyp2) |
94 |
|
return(0); |
95 |
|
if (dtyp1 < dtyp2) |
126 |
|
char fmt[MAXFMTLEN]; |
127 |
|
int i; |
128 |
|
|
129 |
< |
if (headidval(fmt, s)) |
129 |
> |
if (headidval(NULL, s)) |
130 |
|
return(0); |
131 |
< |
if (!strncmp(s, "NCOMP=", 6)) { |
132 |
< |
ip->ncomp = atoi(s+6); |
131 |
> |
if (isncomp(s)) { |
132 |
> |
ip->ncomp = ncompval(s); |
133 |
|
return(0); |
134 |
|
} |
135 |
|
if (!strncmp(s, "NROWS=", 6)) { |
155 |
|
multcolor(ip->cexp, ctmp); |
156 |
|
return(0); |
157 |
|
} |
158 |
+ |
if (iswlsplit(s)) { |
159 |
+ |
wlsplitval(ip->wlpart, s); |
160 |
+ |
return(0); |
161 |
+ |
} |
162 |
|
if (!formatval(fmt, s)) { |
163 |
|
rmx_addinfo(ip, s); |
164 |
|
return(0); |
218 |
|
{ |
219 |
|
int i; |
220 |
|
#ifdef MAP_FILE |
221 |
< |
long pos; /* map memory to file if possible */ |
221 |
> |
long pos; /* map memory for file > 1MB if possible */ |
222 |
|
if (!rm->swapin && array_size(rm) >= 1L<<20 && |
223 |
|
(pos = ftell(fp)) >= 0 && !(pos % sizeof(double))) { |
224 |
|
rm->mapped = mmap(NULL, array_size(rm)+pos, PROT_READ|PROT_WRITE, |
226 |
|
if (rm->mapped != MAP_FAILED) { |
227 |
|
rm->mtx = (double *)rm->mapped + pos/sizeof(double); |
228 |
|
return(1); |
229 |
< |
} |
229 |
> |
} /* else fall back on reading into memory */ |
230 |
|
rm->mapped = NULL; |
231 |
|
} |
232 |
|
#endif |
250 |
|
|
251 |
|
if (!scan) |
252 |
|
return(0); |
253 |
< |
if (!rmx_prepare(rm)) |
253 |
> |
if (!rmx_prepare(rm)) { |
254 |
> |
free(scan); |
255 |
|
return(0); |
256 |
+ |
} |
257 |
|
for (i = 0; i < rm->nrows; i++) { |
258 |
< |
double *dp = rmx_lval(rm,i,j); |
258 |
> |
double *dp = rmx_lval(rm,i,0); |
259 |
|
if (freadscan(scan, rm->ncols, fp) < 0) { |
260 |
|
free(scan); |
261 |
|
return(0); |
270 |
|
return(1); |
271 |
|
} |
272 |
|
|
273 |
+ |
static int |
274 |
+ |
rmx_load_spec(RMATRIX *rm, FILE *fp) |
275 |
+ |
{ |
276 |
+ |
uby8 *scan; |
277 |
+ |
SCOLOR scol; |
278 |
+ |
int i, j, k; |
279 |
+ |
|
280 |
+ |
if (rm->ncomp < 3) |
281 |
+ |
return(0); |
282 |
+ |
scan = (uby8 *)malloc((rm->ncomp+1)*rm->ncols); |
283 |
+ |
if (!scan) |
284 |
+ |
return(0); |
285 |
+ |
if (!rmx_prepare(rm)) { |
286 |
+ |
free(scan); |
287 |
+ |
return(0); |
288 |
+ |
} |
289 |
+ |
for (i = 0; i < rm->nrows; i++) { |
290 |
+ |
double *dp = rmx_lval(rm,i,0); |
291 |
+ |
if (freadscolrs(scan, rm->ncomp, rm->ncols, fp) < 0) { |
292 |
+ |
free(scan); |
293 |
+ |
return(0); |
294 |
+ |
} |
295 |
+ |
for (j = 0; j < rm->ncols; j++) { |
296 |
+ |
scolr2scolor(scol, scan+j*(rm->ncomp+1), rm->ncomp); |
297 |
+ |
for (k = 0; k < rm->ncomp; k++) |
298 |
+ |
*dp++ = scol[k]; |
299 |
+ |
} |
300 |
+ |
} |
301 |
+ |
free(scan); |
302 |
+ |
return(1); |
303 |
+ |
} |
304 |
+ |
|
305 |
+ |
/* Read matrix header from input stream (cannot be XML) */ |
306 |
+ |
int |
307 |
+ |
rmx_load_header(RMATRIX *rm, FILE *fp) |
308 |
+ |
{ |
309 |
+ |
if (!rm | !fp) |
310 |
+ |
return(0); |
311 |
+ |
if (rm->info) { /* clear state */ |
312 |
+ |
free(rm->info); |
313 |
+ |
rm->info = NULL; |
314 |
+ |
} |
315 |
+ |
if (rm->mtx) { /* ...and data */ |
316 |
+ |
#ifdef MAP_FILE |
317 |
+ |
if (rm->mapped) { |
318 |
+ |
munmap(rm->mapped, mapped_size(rm)); |
319 |
+ |
rm->mapped = NULL; |
320 |
+ |
} else |
321 |
+ |
#endif |
322 |
+ |
free(rm->mtx); |
323 |
+ |
rm->mtx = NULL; |
324 |
+ |
} |
325 |
+ |
if (rm->nrows | rm->ncols | !rm->dtype) { |
326 |
+ |
rm->nrows = rm->ncols = 0; |
327 |
+ |
rm->ncomp = 3; |
328 |
+ |
setcolor(rm->cexp, 1.f, 1.f, 1.f); |
329 |
+ |
memcpy(rm->wlpart, WLPART, sizeof(rm->wlpart)); |
330 |
+ |
rm->swapin = 0; |
331 |
+ |
} |
332 |
+ |
SET_FILE_BINARY(fp); |
333 |
+ |
rm->dtype = DTascii; /* assumed w/o FORMAT */ |
334 |
+ |
if (getheader(fp, get_dminfo, rm) < 0) { |
335 |
+ |
fputs("Unrecognized matrix format\n", stderr); |
336 |
+ |
return(0); |
337 |
+ |
} |
338 |
+ |
/* resolution string? */ |
339 |
+ |
if ((rm->nrows <= 0) | (rm->ncols <= 0)) { |
340 |
+ |
if (!fscnresolu(&rm->ncols, &rm->nrows, fp)) |
341 |
+ |
return(0); |
342 |
+ |
if ((rm->dtype == DTrgbe) | (rm->dtype == DTxyze) && |
343 |
+ |
rm->ncomp != 3) |
344 |
+ |
return(0); |
345 |
+ |
} |
346 |
+ |
return(1); |
347 |
+ |
} |
348 |
+ |
|
349 |
+ |
/* Allocate & load post-header data from stream given type set in rm->dtype */ |
350 |
+ |
int |
351 |
+ |
rmx_load_data(RMATRIX *rm, FILE *fp) |
352 |
+ |
{ |
353 |
+ |
switch (rm->dtype) { |
354 |
+ |
case DTascii: |
355 |
+ |
SET_FILE_TEXT(fp); |
356 |
+ |
return(rmx_load_ascii(rm, fp)); |
357 |
+ |
case DTfloat: |
358 |
+ |
return(rmx_load_float(rm, fp)); |
359 |
+ |
case DTdouble: |
360 |
+ |
return(rmx_load_double(rm, fp)); |
361 |
+ |
case DTrgbe: |
362 |
+ |
case DTxyze: |
363 |
+ |
return(rmx_load_rgbe(rm, fp)); |
364 |
+ |
case DTspec: |
365 |
+ |
return(rmx_load_spec(rm, fp)); |
366 |
+ |
default: |
367 |
+ |
fputs("Unsupported data type in rmx_loaddata()\n", stderr); |
368 |
+ |
} |
369 |
+ |
return(0); |
370 |
+ |
} |
371 |
+ |
|
372 |
|
/* Load matrix from supported file type */ |
373 |
|
RMATRIX * |
374 |
|
rmx_load(const char *inspec, RMPref rmp) |
375 |
|
{ |
376 |
|
FILE *fp; |
377 |
|
RMATRIX *dnew; |
378 |
+ |
int ok; |
379 |
|
|
380 |
|
if (!inspec) |
381 |
|
inspec = stdin_name; |
382 |
|
else if (!*inspec) |
383 |
|
return(NULL); |
384 |
< |
if (inspec == stdin_name) { /* reading from stdin? */ |
384 |
> |
if (inspec == stdin_name) /* reading from stdin? */ |
385 |
|
fp = stdin; |
386 |
< |
} else if (inspec[0] == '!') { |
387 |
< |
if (!(fp = popen(inspec+1, "r"))) |
388 |
< |
return(NULL); |
275 |
< |
} else { |
386 |
> |
else if (inspec[0] == '!') |
387 |
> |
fp = popen(inspec+1, "r"); |
388 |
> |
else { |
389 |
|
const char *sp = inspec; /* check suffix */ |
390 |
|
while (*sp) |
391 |
|
++sp; |
399 |
|
dnew = rmx_from_cmatrix(cm); |
400 |
|
cm_free(cm); |
401 |
|
dnew->dtype = DTascii; |
402 |
< |
return(dnew); |
403 |
< |
} |
404 |
< |
/* else open it ourselves */ |
292 |
< |
if (!(fp = fopen(inspec, "r"))) |
293 |
< |
return(NULL); |
402 |
> |
return(dnew); /* return here */ |
403 |
> |
} /* else open it ourselves */ |
404 |
> |
fp = fopen(inspec, "r"); |
405 |
|
} |
406 |
< |
SET_FILE_BINARY(fp); |
406 |
> |
if (!fp) |
407 |
> |
return(NULL); |
408 |
|
#ifdef getc_unlocked |
409 |
|
flockfile(fp); |
410 |
|
#endif |
411 |
< |
if (!(dnew = rmx_new(0,0,3))) { |
412 |
< |
fclose(fp); |
411 |
> |
/* load header info */ |
412 |
> |
if (!rmx_load_header(dnew = rmx_new(0,0,3), fp)) { |
413 |
> |
fprintf(stderr, "Bad header in: %s\n", inspec); |
414 |
> |
if (inspec[0] == '!') pclose(fp); |
415 |
> |
else fclose(fp); |
416 |
> |
rmx_free(dnew); |
417 |
|
return(NULL); |
418 |
|
} |
419 |
< |
dnew->dtype = DTascii; /* assumed w/o FORMAT */ |
420 |
< |
dnew->cexp[0] = dnew->cexp[1] = dnew->cexp[2] = 1.f; |
421 |
< |
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 ((dnew->dtype == DTrgbe) | (dnew->dtype == DTxyze) && |
315 |
< |
dnew->ncomp != 3) { |
316 |
< |
fclose(fp); |
317 |
< |
return(NULL); |
318 |
< |
} |
319 |
< |
} |
320 |
< |
switch (dnew->dtype) { |
321 |
< |
case DTascii: |
322 |
< |
SET_FILE_TEXT(fp); |
323 |
< |
if (!rmx_load_ascii(dnew, fp)) |
324 |
< |
goto loaderr; |
325 |
< |
dnew->dtype = DTascii; /* should leave double? */ |
326 |
< |
break; |
327 |
< |
case DTfloat: |
328 |
< |
if (!rmx_load_float(dnew, fp)) |
329 |
< |
goto loaderr; |
330 |
< |
dnew->dtype = DTfloat; |
331 |
< |
break; |
332 |
< |
case DTdouble: |
333 |
< |
if (!rmx_load_double(dnew, fp)) |
334 |
< |
goto loaderr; |
335 |
< |
dnew->dtype = DTdouble; |
336 |
< |
break; |
337 |
< |
case DTrgbe: |
338 |
< |
case DTxyze: |
339 |
< |
if (!rmx_load_rgbe(dnew, fp)) |
340 |
< |
goto loaderr; |
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; |
354 |
< |
} |
355 |
< |
if (fp != stdin) { |
419 |
> |
ok = rmx_load_data(dnew, fp); /* allocate & load data */ |
420 |
> |
|
421 |
> |
if (fp != stdin) { /* close input stream */ |
422 |
|
if (inspec[0] == '!') |
423 |
|
pclose(fp); |
424 |
|
else |
428 |
|
else |
429 |
|
funlockfile(fp); |
430 |
|
#endif |
431 |
+ |
if (!ok) { /* load failure? */ |
432 |
+ |
fprintf(stderr, "Error loading data from: %s\n", inspec); |
433 |
+ |
rmx_free(dnew); |
434 |
+ |
return(NULL); |
435 |
+ |
} |
436 |
+ |
/* undo exposure? */ |
437 |
+ |
if ((dnew->cexp[0] != 1.f) | |
438 |
+ |
(dnew->cexp[1] != 1.f) | (dnew->cexp[2] != 1.f)) { |
439 |
+ |
double cmlt[MAXCSAMP]; |
440 |
+ |
int i; |
441 |
+ |
cmlt[0] = 1./dnew->cexp[0]; |
442 |
+ |
cmlt[1] = 1./dnew->cexp[1]; |
443 |
+ |
cmlt[2] = 1./dnew->cexp[2]; |
444 |
+ |
if (dnew->ncomp > MAXCSAMP) { |
445 |
+ |
fprintf(stderr, "Excess spectral components in: %s\n", |
446 |
+ |
inspec); |
447 |
+ |
rmx_free(dnew); |
448 |
+ |
return(NULL); |
449 |
+ |
} |
450 |
+ |
for (i = dnew->ncomp; i-- > 3; ) |
451 |
+ |
cmlt[i] = cmlt[1]; |
452 |
+ |
rmx_scale(dnew, cmlt); |
453 |
+ |
setcolor(dnew->cexp, 1.f, 1.f, 1.f); |
454 |
+ |
} |
455 |
|
return(dnew); |
366 |
– |
loaderr: /* should report error? */ |
367 |
– |
if (inspec[0] == '!') |
368 |
– |
pclose(fp); |
369 |
– |
else |
370 |
– |
fclose(fp); |
371 |
– |
rmx_free(dnew); |
372 |
– |
return(NULL); |
456 |
|
} |
457 |
|
|
458 |
|
static int |
459 |
|
rmx_write_ascii(const RMATRIX *rm, FILE *fp) |
460 |
|
{ |
461 |
|
const char *fmt = (rm->dtype == DTfloat) ? " %.7e" : |
462 |
< |
(rm->dtype == DTrgbe) | (rm->dtype == DTxyze) ? " %.3e" : |
463 |
< |
" %.15e" ; |
462 |
> |
(rm->dtype == DTrgbe) | (rm->dtype == DTxyze) | |
463 |
> |
(rm->dtype == DTspec) ? " %.3e" : " %.15e" ; |
464 |
|
int i, j, k; |
465 |
|
|
466 |
|
for (i = 0; i < rm->nrows; i++) { |
467 |
|
for (j = 0; j < rm->ncols; j++) { |
468 |
< |
const double *dp = rmx_lval(rm,i,j); |
468 |
> |
const double *dp = rmx_val(rm,i,j); |
469 |
|
for (k = 0; k < rm->ncomp; k++) |
470 |
|
fprintf(fp, fmt, dp[k]); |
471 |
|
fputc('\t', fp); |
487 |
|
} |
488 |
|
for (i = 0; i < rm->nrows; i++) |
489 |
|
for (j = 0; j < rm->ncols; j++) { |
490 |
< |
const double *dp = rmx_lval(rm,i,j); |
490 |
> |
const double *dp = rmx_val(rm,i,j); |
491 |
|
for (k = rm->ncomp; k--; ) |
492 |
|
val[k] = (float)dp[k]; |
493 |
|
if (putbinary(val, sizeof(float), rm->ncomp, fp) != rm->ncomp) |
502 |
|
int i; |
503 |
|
|
504 |
|
for (i = 0; i < rm->nrows; i++) |
505 |
< |
if (putbinary(rmx_lval(rm,i,0), sizeof(double)*rm->ncomp, |
505 |
> |
if (putbinary(rmx_val(rm,i,0), sizeof(double)*rm->ncomp, |
506 |
|
rm->ncols, fp) != rm->ncols) |
507 |
|
return(0); |
508 |
|
return(1); |
518 |
|
return(0); |
519 |
|
for (i = 0; i < rm->nrows; i++) { |
520 |
|
for (j = rm->ncols; j--; ) { |
521 |
< |
const double *dp = rmx_lval(rm,i,j); |
521 |
> |
const double *dp = rmx_val(rm,i,j); |
522 |
|
if (rm->ncomp == 1) |
523 |
|
setcolr(scan[j], dp[0], dp[0], dp[0]); |
524 |
|
else |
533 |
|
return(1); |
534 |
|
} |
535 |
|
|
536 |
+ |
static int |
537 |
+ |
rmx_write_spec(const RMATRIX *rm, FILE *fp) |
538 |
+ |
{ |
539 |
+ |
uby8 *scan = (uby8 *)malloc((rm->ncomp+1)*rm->ncols); |
540 |
+ |
int ok = 1; |
541 |
+ |
SCOLOR scol; |
542 |
+ |
int i, j, k; |
543 |
+ |
|
544 |
+ |
if (!scan) |
545 |
+ |
return(0); |
546 |
+ |
for (i = 0; i < rm->nrows; i++) { |
547 |
+ |
for (j = rm->ncols; j--; ) { |
548 |
+ |
const double *dp = rmx_val(rm,i,j); |
549 |
+ |
for (k = rm->ncomp; k--; ) |
550 |
+ |
scol[k] = dp[k]; |
551 |
+ |
scolor2scolr(scan+j*(rm->ncomp+1), scol, rm->ncomp); |
552 |
+ |
} |
553 |
+ |
if (fwritescolrs(scan, rm->ncomp, rm->ncols, fp) < 0) { |
554 |
+ |
ok = 0; |
555 |
+ |
break; |
556 |
+ |
} |
557 |
+ |
} |
558 |
+ |
free(scan); |
559 |
+ |
return(ok); |
560 |
+ |
} |
561 |
+ |
|
562 |
|
/* Check if CIE XYZ primaries were specified */ |
563 |
|
static int |
564 |
|
findCIEprims(const char *info) |
597 |
|
dtype = DTxyze; |
598 |
|
else if ((dtype == DTxyze) & (rm->dtype == DTrgbe)) |
599 |
|
dtype = DTrgbe; |
600 |
+ |
/* write exposure? */ |
601 |
+ |
if (rm->ncomp == 3 && (rm->cexp[RED] != rm->cexp[GRN]) | |
602 |
+ |
(rm->cexp[GRN] != rm->cexp[BLU])) |
603 |
+ |
fputcolcor(rm->cexp, fp); |
604 |
+ |
else if (rm->cexp[GRN] != 1.f) |
605 |
+ |
fputexpos(rm->cexp[GRN], fp); |
606 |
|
if ((dtype != DTrgbe) & (dtype != DTxyze)) { |
607 |
< |
fprintf(fp, "NROWS=%d\n", rm->nrows); |
608 |
< |
fprintf(fp, "NCOLS=%d\n", rm->ncols); |
609 |
< |
fprintf(fp, "NCOMP=%d\n", rm->ncomp); |
607 |
> |
if (dtype == DTspec) { |
608 |
> |
if (rm->ncomp < 3) |
609 |
> |
return(0); /* bad # components */ |
610 |
> |
fputwlsplit(rm->wlpart, fp); |
611 |
> |
} else { |
612 |
> |
fprintf(fp, "NROWS=%d\n", rm->nrows); |
613 |
> |
fprintf(fp, "NCOLS=%d\n", rm->ncols); |
614 |
> |
} |
615 |
> |
fputncomp(rm->ncomp, fp); |
616 |
|
} else if ((rm->ncomp != 3) & (rm->ncomp != 1)) |
617 |
|
return(0); /* wrong # components */ |
618 |
|
if ((dtype == DTfloat) | (dtype == DTdouble)) |
634 |
|
fprtresolu(rm->ncols, rm->nrows, fp); |
635 |
|
ok = rmx_write_rgbe(rm, fp); |
636 |
|
break; |
637 |
+ |
case DTspec: |
638 |
+ |
fprtresolu(rm->ncols, rm->nrows, fp); |
639 |
+ |
ok = rmx_write_spec(rm, fp); |
640 |
+ |
break; |
641 |
|
default: |
642 |
|
return(0); |
643 |
|
} |
645 |
|
#ifdef getc_unlocked |
646 |
|
funlockfile(fp); |
647 |
|
#endif |
648 |
+ |
if (!ok) fputs("Error writing matrix\n", stderr); |
649 |
|
return(ok); |
650 |
|
} |
651 |
|
|
680 |
|
return(NULL); |
681 |
|
rmx_addinfo(dnew, rm->info); |
682 |
|
dnew->dtype = rm->dtype; |
683 |
+ |
copycolor(dnew->cexp, rm->cexp); |
684 |
+ |
memcpy(dnew->wlpart, rm->wlpart, sizeof(dnew->wlpart)); |
685 |
|
memcpy(dnew->mtx, rm->mtx, array_size(dnew)); |
686 |
|
return(dnew); |
687 |
|
} |
691 |
|
rmx_transpose(const RMATRIX *rm) |
692 |
|
{ |
693 |
|
RMATRIX *dnew; |
694 |
< |
int i, j, k; |
694 |
> |
int i, j; |
695 |
|
|
696 |
|
if (!rm) |
697 |
|
return(0); |
711 |
|
rmx_addinfo(dnew, "Transposed rows and columns\n"); |
712 |
|
} |
713 |
|
dnew->dtype = rm->dtype; |
714 |
< |
for (i = dnew->nrows; i--; ) |
715 |
< |
for (j = dnew->ncols; j--; ) |
716 |
< |
memcpy(rmx_lval(dnew,i,j), rmx_lval(rm,i,j), |
714 |
> |
copycolor(dnew->cexp, rm->cexp); |
715 |
> |
memcpy(dnew->wlpart, rm->wlpart, sizeof(dnew->wlpart)); |
716 |
> |
for (j = dnew->ncols; j--; ) |
717 |
> |
for (i = dnew->nrows; i--; ) |
718 |
> |
memcpy(rmx_lval(dnew,i,j), rmx_val(rm,j,i), |
719 |
|
sizeof(double)*dnew->ncomp); |
720 |
|
return(dnew); |
721 |
|
} |
742 |
|
for (k = mres->ncomp; k--; ) { |
743 |
|
double d = 0; |
744 |
|
for (h = m1->ncols; h--; ) |
745 |
< |
d += rmx_lval(m1,i,h)[k] * rmx_lval(m2,h,j)[k]; |
745 |
> |
d += rmx_val(m1,i,h)[k] * rmx_val(m2,h,j)[k]; |
746 |
|
rmx_lval(mres,i,j)[k] = d; |
747 |
|
} |
748 |
|
return(mres); |
769 |
|
if (divide) { |
770 |
|
double d; |
771 |
|
if (m2->ncomp == 1) { |
772 |
< |
d = rmx_lval(m2,i,j)[0]; |
772 |
> |
d = rmx_val(m2,i,j)[0]; |
773 |
|
if (d == 0) { |
774 |
|
++zeroDivides; |
775 |
|
for (k = m1->ncomp; k--; ) |
781 |
|
} |
782 |
|
} else |
783 |
|
for (k = m1->ncomp; k--; ) { |
784 |
< |
d = rmx_lval(m2,i,j)[k]; |
784 |
> |
d = rmx_val(m2,i,j)[k]; |
785 |
|
if (d == 0) { |
786 |
|
++zeroDivides; |
787 |
|
rmx_lval(m1,i,j)[k] = 0; |
790 |
|
} |
791 |
|
} else { |
792 |
|
if (m2->ncomp == 1) { |
793 |
< |
const double d = rmx_lval(m2,i,j)[0]; |
793 |
> |
const double d = rmx_val(m2,i,j)[0]; |
794 |
|
for (k = m1->ncomp; k--; ) |
795 |
|
rmx_lval(m1,i,j)[k] *= d; |
796 |
|
} else |
797 |
|
for (k = m1->ncomp; k--; ) |
798 |
< |
rmx_lval(m1,i,j)[k] *= rmx_lval(m2,i,j)[k]; |
798 |
> |
rmx_lval(m1,i,j)[k] *= rmx_val(m2,i,j)[k]; |
799 |
|
} |
800 |
|
if (zeroDivides) { |
801 |
|
rmx_addinfo(m1, "WARNING: zero divide(s) corrupted results\n"); |
831 |
|
rmx_addinfo(msum, rmx_mismatch_warn); |
832 |
|
for (i = msum->nrows; i--; ) |
833 |
|
for (j = msum->ncols; j--; ) { |
834 |
< |
const double *da = rmx_lval(madd,i,j); |
834 |
> |
const double *da = rmx_val(madd,i,j); |
835 |
|
double *ds = rmx_lval(msum,i,j); |
836 |
|
for (k = msum->ncomp; k--; ) |
837 |
|
ds[k] += sf[k] * da[k]; |
857 |
|
} |
858 |
|
if (rm->info) |
859 |
|
rmx_addinfo(rm, "Applied scalar\n"); |
860 |
+ |
/* XXX: should record as exposure for COLR and SCOLR types? */ |
861 |
|
return(1); |
862 |
|
} |
863 |
|
|
883 |
|
dnew->dtype = msrc->dtype; |
884 |
|
for (i = dnew->nrows; i--; ) |
885 |
|
for (j = dnew->ncols; j--; ) { |
886 |
< |
const double *ds = rmx_lval(msrc,i,j); |
886 |
> |
const double *ds = rmx_val(msrc,i,j); |
887 |
|
for (kd = dnew->ncomp; kd--; ) { |
888 |
|
double d = 0; |
889 |
|
for (ks = msrc->ncomp; ks--; ) |
925 |
|
int i, j; |
926 |
|
CMATRIX *cnew; |
927 |
|
|
928 |
< |
if (!rm || !rm->mtx | ((rm->ncomp != 3) & (rm->ncomp != 1))) |
928 |
> |
if (!rm || !rm->mtx | (rm->ncomp == 2)) |
929 |
|
return(NULL); |
930 |
|
cnew = cm_alloc(rm->nrows, rm->ncols); |
931 |
|
if (!cnew) |
932 |
|
return(NULL); |
933 |
|
for (i = cnew->nrows; i--; ) |
934 |
|
for (j = cnew->ncols; j--; ) { |
935 |
< |
const double *dp = rmx_lval(rm,i,j); |
935 |
> |
const double *dp = rmx_val(rm,i,j); |
936 |
|
COLORV *cv = cm_lval(cnew,i,j); |
937 |
< |
if (rm->ncomp == 1) |
938 |
< |
setcolor(cv, dp[0], dp[0], dp[0]); |
808 |
< |
else |
937 |
> |
switch (rm->ncomp) { |
938 |
> |
case 3: |
939 |
|
setcolor(cv, dp[0], dp[1], dp[2]); |
940 |
+ |
break; |
941 |
+ |
case 1: |
942 |
+ |
setcolor(cv, dp[0], dp[0], dp[0]); |
943 |
+ |
break; |
944 |
+ |
default: { |
945 |
+ |
SCOLOR scol; |
946 |
+ |
int k; |
947 |
+ |
for (k = rm->ncomp; k--; ) |
948 |
+ |
scol[k] = dp[k]; |
949 |
+ |
scolor2color(cv, scol, rm->ncomp, rm->wlpart); |
950 |
+ |
} break; |
951 |
+ |
} |
952 |
|
} |
953 |
|
return(cnew); |
954 |
|
} |