12 |
|
#include "resolu.h" |
13 |
|
#include "rmatrix.h" |
14 |
|
|
15 |
< |
typedef struct { |
16 |
< |
int nrows, ncols, ncomp; |
17 |
< |
int dtype; |
18 |
< |
} DMINFO; |
15 |
> |
static char rmx_mismatch_warn[] = "WARNING: data type mismatch\n"; |
16 |
|
|
17 |
|
/* Allocate a nr x nc matrix with n components */ |
18 |
|
RMATRIX * |
27 |
|
if (dnew == NULL) |
28 |
|
return(NULL); |
29 |
|
dnew->nrows = nr; dnew->ncols = nc; dnew->ncomp = n; |
30 |
+ |
dnew->dtype = DTdouble; |
31 |
+ |
dnew->info = NULL; |
32 |
|
return(dnew); |
33 |
|
} |
34 |
|
|
35 |
+ |
/* Free a RMATRIX array */ |
36 |
+ |
void |
37 |
+ |
rmx_free(RMATRIX *rm) |
38 |
+ |
{ |
39 |
+ |
if (!rm) return; |
40 |
+ |
if (rm->info) |
41 |
+ |
free(rm->info); |
42 |
+ |
free(rm); |
43 |
+ |
} |
44 |
+ |
|
45 |
+ |
/* Resolve data type based on two input types (returns 0 for mismatch) */ |
46 |
+ |
int |
47 |
+ |
rmx_newtype(int dtyp1, int dtyp2) |
48 |
+ |
{ |
49 |
+ |
if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) && dtyp1 != dtyp2) |
50 |
+ |
return(0); |
51 |
+ |
if (dtyp1 < dtyp2) |
52 |
+ |
return(dtyp1); |
53 |
+ |
return(dtyp2); |
54 |
+ |
} |
55 |
+ |
|
56 |
+ |
/* Append header information associated with matrix data */ |
57 |
+ |
int |
58 |
+ |
rmx_addinfo(RMATRIX *rm, const char *info) |
59 |
+ |
{ |
60 |
+ |
if (!info || !*info) |
61 |
+ |
return(0); |
62 |
+ |
if (!rm->info) |
63 |
+ |
rm->info = (char *)malloc(strlen(info)+1); |
64 |
+ |
else |
65 |
+ |
rm->info = (char *)realloc(rm->info, |
66 |
+ |
strlen(rm->info)+strlen(info)+1); |
67 |
+ |
if (!rm->info) |
68 |
+ |
return(0); |
69 |
+ |
strcat(rm->info, info); |
70 |
+ |
return(1); |
71 |
+ |
} |
72 |
+ |
|
73 |
|
static int |
74 |
|
get_dminfo(char *s, void *p) |
75 |
|
{ |
76 |
< |
DMINFO *ip = (DMINFO *)p; |
77 |
< |
char fmt[32]; |
76 |
> |
RMATRIX *ip = (RMATRIX *)p; |
77 |
> |
char fmt[64]; |
78 |
|
int i; |
79 |
|
|
80 |
+ |
if (headidval(fmt, s)) |
81 |
+ |
return(0); |
82 |
|
if (!strncmp(s, "NCOMP=", 6)) { |
83 |
|
ip->ncomp = atoi(s+6); |
84 |
|
return(0); |
91 |
|
ip->ncols = atoi(s+6); |
92 |
|
return(0); |
93 |
|
} |
94 |
< |
if (!formatval(fmt, s)) |
94 |
> |
if (!formatval(fmt, s)) { |
95 |
> |
rmx_addinfo(ip, s); |
96 |
|
return(0); |
97 |
+ |
} |
98 |
|
for (i = 1; i < DTend; i++) |
99 |
|
if (!strcmp(fmt, cm_fmt_id[i])) { |
100 |
|
ip->dtype = i; |
186 |
|
rmx_load(const char *fname) |
187 |
|
{ |
188 |
|
FILE *fp = stdin; |
189 |
< |
DMINFO dinfo; |
189 |
> |
RMATRIX dinfo; |
190 |
|
RMATRIX *dnew; |
191 |
|
|
192 |
|
if (fname == NULL) { /* reading from stdin? */ |
193 |
|
fname = "<stdin>"; |
194 |
+ |
#ifdef _WIN32 |
195 |
+ |
_setmode(fileno(stdin), _O_BINARY); |
196 |
+ |
#endif |
197 |
|
} else { |
198 |
|
const char *sp = fname; /* check suffix */ |
199 |
|
while (*sp) |
216 |
|
flockfile(fp); |
217 |
|
#endif |
218 |
|
dinfo.nrows = dinfo.ncols = dinfo.ncomp = 0; |
219 |
< |
dinfo.dtype = DTascii; |
219 |
> |
dinfo.dtype = DTascii; /* assumed w/o FORMAT */ |
220 |
> |
dinfo.info = NULL; |
221 |
|
if (getheader(fp, get_dminfo, &dinfo) < 0) { |
222 |
|
fclose(fp); |
223 |
|
return(NULL); |
224 |
|
} |
225 |
< |
if ((dinfo.dtype == DTrgbe) | (dinfo.dtype == DTxyze)) { |
225 |
> |
if ((dinfo.nrows <= 0) | (dinfo.ncols <= 0)) { |
226 |
|
if (!fscnresolu(&dinfo.ncols, &dinfo.nrows, fp)) { |
227 |
|
fclose(fp); |
228 |
|
return(NULL); |
229 |
|
} |
230 |
< |
dinfo.ncomp = 3; |
230 |
> |
if (dinfo.ncomp <= 0) |
231 |
> |
dinfo.ncomp = 3; |
232 |
> |
else if ((dinfo.dtype == DTrgbe) | (dinfo.dtype == DTxyze) && |
233 |
> |
dinfo.ncomp != 3) { |
234 |
> |
fclose(fp); |
235 |
> |
return(NULL); |
236 |
> |
} |
237 |
|
} |
238 |
|
dnew = rmx_alloc(dinfo.nrows, dinfo.ncols, dinfo.ncomp); |
239 |
|
if (dnew == NULL) { |
240 |
|
fclose(fp); |
241 |
|
return(NULL); |
242 |
|
} |
243 |
+ |
dnew->info = dinfo.info; |
244 |
|
switch (dinfo.dtype) { |
245 |
|
case DTascii: |
246 |
|
if (!rmx_load_ascii(dnew, fp)) |
249 |
|
case DTfloat: |
250 |
|
if (!rmx_load_float(dnew, fp)) |
251 |
|
goto loaderr; |
252 |
+ |
dnew->dtype = DTfloat; |
253 |
|
break; |
254 |
|
case DTdouble: |
255 |
|
if (!rmx_load_double(dnew, fp)) |
256 |
|
goto loaderr; |
257 |
+ |
dnew->dtype = DTdouble; |
258 |
|
break; |
259 |
|
case DTrgbe: |
260 |
|
case DTxyze: |
261 |
|
if (!rmx_load_rgbe(dnew, fp)) |
262 |
|
goto loaderr; |
263 |
+ |
dnew->dtype = dinfo.dtype; |
264 |
|
break; |
265 |
|
default: |
266 |
|
goto loaderr; |
354 |
|
return(1); |
355 |
|
} |
356 |
|
|
357 |
< |
/* Write matrix to file type indicated by dt */ |
357 |
> |
/* Write matrix to file type indicated by dtype */ |
358 |
|
long |
359 |
|
rmx_write(const RMATRIX *rm, int dtype, FILE *fp) |
360 |
|
{ |
364 |
|
if ((rm == NULL) | (fp == NULL)) |
365 |
|
return(0); |
366 |
|
/* complete header */ |
367 |
+ |
if (rm->info) |
368 |
+ |
fputs(rm->info, fp); |
369 |
+ |
if (dtype == DTfromHeader) |
370 |
+ |
dtype = rm->dtype; |
371 |
+ |
else if ((dtype == DTrgbe) & (rm->dtype == DTxyze)) |
372 |
+ |
dtype = DTxyze; |
373 |
+ |
else if ((dtype = DTxyze) & (rm->dtype == DTrgbe)) |
374 |
+ |
dtype = DTrgbe; |
375 |
|
if ((dtype != DTrgbe) & (dtype != DTxyze)) { |
376 |
|
fprintf(fp, "NROWS=%d\n", rm->nrows); |
377 |
|
fprintf(fp, "NCOLS=%d\n", rm->ncols); |
440 |
|
dnew = rmx_alloc(rm->nrows, rm->ncols, rm->ncomp); |
441 |
|
if (dnew == NULL) |
442 |
|
return(NULL); |
443 |
+ |
rmx_addinfo(dnew, rm->info); |
444 |
+ |
dnew->dtype = rm->dtype; |
445 |
|
memcpy(dnew->mtx, rm->mtx, |
446 |
|
sizeof(rm->mtx[0])*rm->ncomp*rm->nrows*rm->ncols); |
447 |
|
return(dnew); |
459 |
|
dnew = rmx_alloc(rm->ncols, rm->nrows, rm->ncomp); |
460 |
|
if (dnew == NULL) |
461 |
|
return(NULL); |
462 |
+ |
if (rm->info) { |
463 |
+ |
rmx_addinfo(dnew, rm->info); |
464 |
+ |
rmx_addinfo(dnew, "Transposed rows and columns\n"); |
465 |
+ |
} |
466 |
+ |
dnew->dtype = rm->dtype; |
467 |
|
for (i = dnew->nrows; i--; ) |
468 |
|
for (j = dnew->ncols; j--; ) |
469 |
|
for (k = dnew->ncomp; k--; ) |
484 |
|
mres = rmx_alloc(m1->nrows, m2->ncols, m1->ncomp); |
485 |
|
if (mres == NULL) |
486 |
|
return(NULL); |
487 |
+ |
i = rmx_newtype(m1->dtype, m2->dtype); |
488 |
+ |
if (i) |
489 |
+ |
mres->dtype = i; |
490 |
+ |
else |
491 |
+ |
rmx_addinfo(mres, rmx_mismatch_warn); |
492 |
|
for (i = mres->nrows; i--; ) |
493 |
|
for (j = mres->ncols; j--; ) |
494 |
|
for (h = m1->ncols; h--; ) { |
521 |
|
mysf[k] = 1; |
522 |
|
sf = mysf; |
523 |
|
} |
524 |
+ |
i = rmx_newtype(msum->dtype, madd->dtype); |
525 |
+ |
if (i) |
526 |
+ |
msum->dtype = i; |
527 |
+ |
else |
528 |
+ |
rmx_addinfo(msum, rmx_mismatch_warn); |
529 |
|
for (i = msum->nrows; i--; ) |
530 |
|
for (j = msum->ncols; j--; ) |
531 |
|
for (k = msum->ncomp; k--; ) |
563 |
|
dnew = rmx_alloc(msrc->nrows, msrc->ncols, n); |
564 |
|
if (dnew == NULL) |
565 |
|
return(NULL); |
566 |
+ |
dnew->dtype = msrc->dtype; |
567 |
|
for (i = dnew->nrows; i--; ) |
568 |
|
for (j = dnew->ncols; j--; ) |
569 |
|
for (kd = dnew->ncomp; kd--; ) { |
587 |
|
dnew = rmx_alloc(cm->nrows, cm->ncols, 3); |
588 |
|
if (dnew == NULL) |
589 |
|
return(NULL); |
590 |
+ |
dnew->dtype = DTfloat; |
591 |
|
for (i = dnew->nrows; i--; ) |
592 |
|
for (j = dnew->ncols; j--; ) { |
593 |
|
const COLORV *cv = cm_lval(cm,i,j); |