| 12 |
|
#include "resolu.h" |
| 13 |
|
#include "rmatrix.h" |
| 14 |
|
|
| 15 |
+ |
#define MAX_INFO 16000 |
| 16 |
+ |
|
| 17 |
|
typedef struct { |
| 18 |
|
int nrows, ncols, ncomp; |
| 19 |
|
int dtype; |
| 20 |
+ |
int info_len; |
| 21 |
+ |
char info[MAX_INFO]; |
| 22 |
|
} DMINFO; |
| 23 |
|
|
| 24 |
|
/* Allocate a nr x nc matrix with n components */ |
| 34 |
|
if (dnew == NULL) |
| 35 |
|
return(NULL); |
| 36 |
|
dnew->nrows = nr; dnew->ncols = nc; dnew->ncomp = n; |
| 37 |
+ |
dnew->info = NULL; |
| 38 |
|
return(dnew); |
| 39 |
|
} |
| 40 |
|
|
| 41 |
+ |
/* Append header information associated with matrix data */ |
| 42 |
+ |
int |
| 43 |
+ |
rmx_addinfo(RMATRIX *rm, const char *info) |
| 44 |
+ |
{ |
| 45 |
+ |
if (!info || !*info) |
| 46 |
+ |
return(0); |
| 47 |
+ |
if (!rm->info) |
| 48 |
+ |
rm->info = (char *)malloc(strlen(info)+1); |
| 49 |
+ |
else |
| 50 |
+ |
rm->info = (char *)realloc(rm->info, |
| 51 |
+ |
strlen(rm->info)+strlen(info)+1); |
| 52 |
+ |
if (!rm->info) |
| 53 |
+ |
return(0); |
| 54 |
+ |
strcat(rm->info, info); |
| 55 |
+ |
return(1); |
| 56 |
+ |
} |
| 57 |
+ |
|
| 58 |
|
static int |
| 59 |
|
get_dminfo(char *s, void *p) |
| 60 |
|
{ |
| 61 |
|
DMINFO *ip = (DMINFO *)p; |
| 62 |
< |
char fmt[32]; |
| 62 |
> |
char fmt[64]; |
| 63 |
|
int i; |
| 64 |
|
|
| 65 |
|
if (!strncmp(s, "NCOMP=", 6)) { |
| 74 |
|
ip->ncols = atoi(s+6); |
| 75 |
|
return(0); |
| 76 |
|
} |
| 77 |
< |
if (!formatval(fmt, s)) |
| 77 |
> |
if (!formatval(fmt, s)) { |
| 78 |
> |
if (headidval(fmt, s)) |
| 79 |
> |
return(0); |
| 80 |
> |
while (*s) { |
| 81 |
> |
if (ip->info_len == MAX_INFO-2 && |
| 82 |
> |
ip->info[MAX_INFO-3] != '\n') |
| 83 |
> |
ip->info[ip->info_len++] = '\n'; |
| 84 |
> |
if (ip->info_len >= MAX_INFO-1) |
| 85 |
> |
break; |
| 86 |
> |
ip->info[ip->info_len++] = *s++; |
| 87 |
> |
} |
| 88 |
> |
ip->info[ip->info_len] = '\0'; |
| 89 |
|
return(0); |
| 90 |
+ |
} |
| 91 |
|
for (i = 1; i < DTend; i++) |
| 92 |
|
if (!strcmp(fmt, cm_fmt_id[i])) { |
| 93 |
|
ip->dtype = i; |
| 207 |
|
#endif |
| 208 |
|
dinfo.nrows = dinfo.ncols = dinfo.ncomp = 0; |
| 209 |
|
dinfo.dtype = DTascii; |
| 210 |
< |
if (getheader(fp, &get_dminfo, &dinfo) < 0) { |
| 210 |
> |
dinfo.info_len = 0; |
| 211 |
> |
if (getheader(fp, get_dminfo, &dinfo) < 0) { |
| 212 |
|
fclose(fp); |
| 213 |
|
return(NULL); |
| 214 |
|
} |
| 215 |
< |
if ((dinfo.dtype == DTrgbe) | (dinfo.dtype == DTxyze)) { |
| 215 |
> |
if ((dinfo.nrows <= 0) | (dinfo.ncols <= 0)) { |
| 216 |
|
if (!fscnresolu(&dinfo.ncols, &dinfo.nrows, fp)) { |
| 217 |
|
fclose(fp); |
| 218 |
|
return(NULL); |
| 219 |
|
} |
| 220 |
< |
dinfo.ncomp = 3; |
| 220 |
> |
if (dinfo.ncomp <= 0) |
| 221 |
> |
dinfo.ncomp = 3; |
| 222 |
> |
else if ((dinfo.dtype == DTrgbe) | (dinfo.dtype == DTxyze) && |
| 223 |
> |
dinfo.ncomp != 3) { |
| 224 |
> |
fclose(fp); |
| 225 |
> |
return(NULL); |
| 226 |
> |
} |
| 227 |
|
} |
| 228 |
|
dnew = rmx_alloc(dinfo.nrows, dinfo.ncols, dinfo.ncomp); |
| 229 |
|
if (dnew == NULL) { |
| 230 |
|
fclose(fp); |
| 231 |
|
return(NULL); |
| 232 |
|
} |
| 233 |
+ |
if (dinfo.info_len) |
| 234 |
+ |
rmx_addinfo(dnew, dinfo.info); |
| 235 |
|
switch (dinfo.dtype) { |
| 236 |
|
case DTascii: |
| 237 |
|
if (!rmx_load_ascii(dnew, fp)) |
| 352 |
|
if ((rm == NULL) | (fp == NULL)) |
| 353 |
|
return(0); |
| 354 |
|
/* complete header */ |
| 355 |
+ |
if (rm->info) |
| 356 |
+ |
fputs(rm->info, fp); |
| 357 |
|
if ((dtype != DTrgbe) & (dtype != DTxyze)) { |
| 358 |
|
fprintf(fp, "NROWS=%d\n", rm->nrows); |
| 359 |
|
fprintf(fp, "NCOLS=%d\n", rm->ncols); |
| 422 |
|
dnew = rmx_alloc(rm->nrows, rm->ncols, rm->ncomp); |
| 423 |
|
if (dnew == NULL) |
| 424 |
|
return(NULL); |
| 425 |
+ |
rmx_addinfo(dnew, rm->info); |
| 426 |
|
memcpy(dnew->mtx, rm->mtx, |
| 427 |
|
sizeof(rm->mtx[0])*rm->ncomp*rm->nrows*rm->ncols); |
| 428 |
|
return(dnew); |
| 440 |
|
dnew = rmx_alloc(rm->ncols, rm->nrows, rm->ncomp); |
| 441 |
|
if (dnew == NULL) |
| 442 |
|
return(NULL); |
| 443 |
+ |
if (rm->info) { |
| 444 |
+ |
rmx_addinfo(dnew, rm->info); |
| 445 |
+ |
rmx_addinfo(dnew, "Transposed rows and columns\n"); |
| 446 |
+ |
} |
| 447 |
|
for (i = dnew->nrows; i--; ) |
| 448 |
|
for (j = dnew->ncols; j--; ) |
| 449 |
|
for (k = dnew->ncomp; k--; ) |