11 |
|
#include "standard.h" |
12 |
|
#include "cmatrix.h" |
13 |
|
#include "platform.h" |
14 |
+ |
#include "resolu.h" |
15 |
|
|
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 |
+ |
|
25 |
|
/* Allocate a color coefficient matrix */ |
26 |
|
CMATRIX * |
27 |
|
cm_alloc(int nrows, int ncols) |
31 |
|
if ((nrows <= 0) | (ncols <= 0)) |
32 |
|
error(USER, "attempt to create empty matrix"); |
33 |
|
cm = (CMATRIX *)malloc(sizeof(CMATRIX) + |
34 |
< |
3*sizeof(COLORV)*(nrows*ncols - 1)); |
34 |
> |
sizeof(COLOR)*(nrows*ncols - 1)); |
35 |
|
if (cm == NULL) |
36 |
|
error(SYSTEM, "out of memory in cm_alloc()"); |
37 |
|
cm->nrows = nrows; |
50 |
|
return(NULL); |
51 |
|
} |
52 |
|
cm = (CMATRIX *)realloc(cm, sizeof(CMATRIX) + |
53 |
< |
3*sizeof(COLORV)*(nrows*cm->ncols - 1)); |
53 |
> |
sizeof(COLOR)*(nrows*cm->ncols - 1)); |
54 |
|
if (cm == NULL) |
55 |
|
error(SYSTEM, "out of memory in cm_resize()"); |
56 |
|
cm->nrows = nrows; |
57 |
|
return(cm); |
58 |
|
} |
59 |
|
|
60 |
+ |
typedef struct { |
61 |
+ |
int dtype; /* data type */ |
62 |
+ |
int nrows, ncols; /* matrix size */ |
63 |
+ |
char *err; /* error message */ |
64 |
+ |
} CMINFO; /* header info record */ |
65 |
+ |
|
66 |
|
static int |
67 |
< |
getDT(char *s, void *p) |
67 |
> |
get_cminfo(char *s, void *p) |
68 |
|
{ |
69 |
+ |
CMINFO *ip = (CMINFO *)p; |
70 |
|
char fmt[32]; |
71 |
< |
|
72 |
< |
if (formatval(fmt, s)) { |
73 |
< |
if (!strcmp(fmt, "ascii")) |
74 |
< |
*((int *)p) = DTascii; |
75 |
< |
else if (!strcmp(fmt, "float")) |
59 |
< |
*((int *)p) = DTfloat; |
60 |
< |
else if (!strcmp(fmt, "double")) |
61 |
< |
*((int *)p) = DTdouble; |
62 |
< |
else if (!strcmp(fmt, COLRFMT)) |
63 |
< |
*((int *)p) = DTrgbe; |
64 |
< |
else if (!strcmp(fmt, CIEFMT)) |
65 |
< |
*((int *)p) = DTxyze; |
71 |
> |
int i; |
72 |
> |
|
73 |
> |
if (!strncmp(s, "NCOMP=", 6) && atoi(s+6) != 3) { |
74 |
> |
ip->err = "unexpected # components (must be 3)"; |
75 |
> |
return(-1); |
76 |
|
} |
77 |
+ |
if (!strncmp(s, "NROWS=", 6)) { |
78 |
+ |
ip->nrows = atoi(s+6); |
79 |
+ |
return(0); |
80 |
+ |
} |
81 |
+ |
if (!strncmp(s, "NCOLS=", 6)) { |
82 |
+ |
ip->ncols = atoi(s+6); |
83 |
+ |
return(0); |
84 |
+ |
} |
85 |
+ |
if (!formatval(fmt, s)) |
86 |
+ |
return(0); |
87 |
+ |
for (i = 1; i < DTend; i++) |
88 |
+ |
if (!strcmp(fmt, cm_fmt_id[i])) |
89 |
+ |
ip->dtype = i; |
90 |
|
return(0); |
91 |
|
} |
92 |
|
|
93 |
< |
/* Load header to obtain data type */ |
94 |
< |
int |
95 |
< |
getDTfromHeader(FILE *fp) |
93 |
> |
/* Load header to obtain/check data type and number of columns */ |
94 |
> |
char * |
95 |
> |
cm_getheader(int *dt, int *nr, int *nc, FILE *fp) |
96 |
|
{ |
97 |
< |
int dt = DTfromHeader; |
98 |
< |
|
99 |
< |
if (getheader(fp, getDT, &dt) < 0) |
100 |
< |
error(SYSTEM, "header read error"); |
101 |
< |
if (dt == DTfromHeader) |
102 |
< |
error(USER, "missing data format in header"); |
103 |
< |
return(dt); |
97 |
> |
CMINFO cmi; |
98 |
> |
/* read header */ |
99 |
> |
cmi.dtype = DTfromHeader; |
100 |
> |
cmi.nrows = cmi.ncols = 0; |
101 |
> |
cmi.err = "unexpected EOF in header"; |
102 |
> |
if (getheader(fp, &get_cminfo, &cmi) < 0) |
103 |
> |
return(cmi.err); |
104 |
> |
if (dt != NULL) { /* get/check data type? */ |
105 |
> |
if (cmi.dtype == DTfromHeader) { |
106 |
> |
if (*dt == DTfromHeader) |
107 |
> |
return("missing/unknown data format in header"); |
108 |
> |
} else if (*dt == DTfromHeader) |
109 |
> |
*dt = cmi.dtype; |
110 |
> |
else if (*dt != cmi.dtype) |
111 |
> |
return("unexpected data format in header"); |
112 |
> |
} |
113 |
> |
if (nr != NULL) { /* get/check #rows? */ |
114 |
> |
if (*nr <= 0) |
115 |
> |
*nr = cmi.nrows; |
116 |
> |
else if ((cmi.nrows > 0) & (*nr != cmi.nrows)) |
117 |
> |
return("unexpected row count in header"); |
118 |
> |
} |
119 |
> |
if (nc != NULL) { /* get/check #columns? */ |
120 |
> |
if (*nc <= 0) |
121 |
> |
*nc = cmi.ncols; |
122 |
> |
else if ((cmi.ncols > 0) & (*nc != cmi.ncols)) |
123 |
> |
return("unexpected column count in header"); |
124 |
> |
} |
125 |
> |
return(NULL); |
126 |
|
} |
127 |
|
|
128 |
|
/* Allocate and load a matrix from the given file (or stdin if NULL) */ |
132 |
|
FILE *fp = stdin; |
133 |
|
CMATRIX *cm; |
134 |
|
|
90 |
– |
if (ncols <= 0) |
91 |
– |
error(USER, "Non-positive number of columns"); |
135 |
|
if (fname == NULL) |
136 |
|
fname = "<stdin>"; |
137 |
|
else if ((fp = fopen(fname, "r")) == NULL) { |
143 |
|
#endif |
144 |
|
if (dtype != DTascii) |
145 |
|
SET_FILE_BINARY(fp); /* doesn't really work */ |
146 |
< |
if (dtype == DTfromHeader) |
147 |
< |
dtype = getDTfromHeader(fp); |
146 |
> |
if (!dtype | !ncols) { /* expecting header? */ |
147 |
> |
char *err = cm_getheader(&dtype, &nrows, &ncols, fp); |
148 |
> |
if (err != NULL) |
149 |
> |
error(USER, err); |
150 |
> |
if (ncols <= 0) |
151 |
> |
error(USER, "unspecified number of columns"); |
152 |
> |
} |
153 |
|
switch (dtype) { |
154 |
|
case DTascii: |
155 |
|
case DTfloat: |
213 |
|
break; |
214 |
|
} |
215 |
|
} else { /* read binary file */ |
216 |
< |
if (sizeof(COLORV) == (dtype==DTfloat ? sizeof(float) : |
169 |
< |
sizeof(double))) { |
216 |
> |
if (sizeof(COLOR) == cm_elem_size[dtype]) { |
217 |
|
int nread = 0; |
218 |
|
do { /* read all we can */ |
219 |
|
nread += fread(cm->cmem + 3*nread, |
220 |
< |
3*sizeof(COLORV), |
220 |
> |
sizeof(COLOR), |
221 |
|
cm->nrows*cm->ncols - nread, |
222 |
|
fp); |
223 |
|
if (nrows <= 0) { /* unknown length */ |
359 |
|
for (dr = 0; dr < cmr->nrows; dr++) |
360 |
|
for (dc = 0; dc < cmr->ncols; dc++) { |
361 |
|
COLORV *dp = cm_lval(cmr,dr,dc); |
362 |
+ |
double res[3]; |
363 |
|
dp[0] = dp[1] = dp[2] = 0; |
364 |
|
if (rowcheck != NULL && !rowcheck[dr]) |
365 |
|
continue; |
366 |
|
if (colcheck != NULL && !colcheck[dc]) |
367 |
|
continue; |
368 |
+ |
res[0] = res[1] = res[2] = 0; |
369 |
|
for (i = 0; i < cm1->ncols; i++) { |
370 |
|
const COLORV *cp1 = cm_lval(cm1,dr,i); |
371 |
|
const COLORV *cp2 = cm_lval(cm2,i,dc); |
372 |
< |
dp[0] += cp1[0] * cp2[0]; |
373 |
< |
dp[1] += cp1[1] * cp2[1]; |
374 |
< |
dp[2] += cp1[2] * cp2[2]; |
372 |
> |
res[0] += cp1[0] * cp2[0]; |
373 |
> |
res[1] += cp1[1] * cp2[1]; |
374 |
> |
res[2] += cp1[2] * cp2[2]; |
375 |
|
} |
376 |
+ |
copycolor(dp, res); |
377 |
|
} |
378 |
|
if (rowcheck != NULL) free(rowcheck); |
379 |
|
if (colcheck != NULL) free(colcheck); |
380 |
|
return(cmr); |
381 |
|
} |
382 |
|
|
383 |
< |
/* print out matrix as ASCII text -- no header */ |
384 |
< |
void |
385 |
< |
cm_print(const CMATRIX *cm, FILE *fp) |
383 |
> |
/* write out matrix to file (precede by resolution string if picture) */ |
384 |
> |
int |
385 |
> |
cm_write(const CMATRIX *cm, int dtype, FILE *fp) |
386 |
|
{ |
387 |
< |
int r, c; |
388 |
< |
const COLORV *mp = cm->cmem; |
389 |
< |
|
390 |
< |
for (r = 0; r < cm->nrows; r++) { |
391 |
< |
for (c = 0; c < cm->ncols; c++, mp += 3) |
392 |
< |
fprintf(fp, "\t%.6e %.6e %.6e", mp[0], mp[1], mp[2]); |
393 |
< |
fputc('\n', fp); |
387 |
> |
static const char tabEOL[2] = {'\t','\n'}; |
388 |
> |
const COLORV *mp = cm->cmem; |
389 |
> |
int r, c; |
390 |
> |
|
391 |
> |
switch (dtype) { |
392 |
> |
case DTascii: |
393 |
> |
for (r = 0; r < cm->nrows; r++) |
394 |
> |
for (c = 0; c < cm->ncols; c++, mp += 3) |
395 |
> |
fprintf(fp, "%.6e %.6e %.6e%c", |
396 |
> |
mp[0], mp[1], mp[2], |
397 |
> |
tabEOL[c >= cm->ncols-1]); |
398 |
> |
break; |
399 |
> |
case DTfloat: |
400 |
> |
case DTdouble: |
401 |
> |
if (sizeof(COLOR) == cm_elem_size[dtype]) { |
402 |
> |
r = cm->ncols*cm->nrows; |
403 |
> |
while (r > 0) { |
404 |
> |
c = fwrite(mp, sizeof(COLOR), r, fp); |
405 |
> |
if (c <= 0) |
406 |
> |
return(0); |
407 |
> |
mp += 3*c; |
408 |
> |
r -= c; |
409 |
> |
} |
410 |
> |
} else if (dtype == DTdouble) { |
411 |
> |
double dc[3]; |
412 |
> |
r = cm->ncols*cm->nrows; |
413 |
> |
while (r--) { |
414 |
> |
copycolor(dc, mp); |
415 |
> |
if (fwrite(dc, sizeof(double), 3, fp) != 3) |
416 |
> |
return(0); |
417 |
> |
mp += 3; |
418 |
> |
} |
419 |
> |
} else /* dtype == DTfloat */ { |
420 |
> |
float fc[3]; |
421 |
> |
r = cm->ncols*cm->nrows; |
422 |
> |
while (r--) { |
423 |
> |
copycolor(fc, mp); |
424 |
> |
if (fwrite(fc, sizeof(float), 3, fp) != 3) |
425 |
> |
return(0); |
426 |
> |
mp += 3; |
427 |
> |
} |
428 |
> |
} |
429 |
> |
break; |
430 |
> |
case DTrgbe: |
431 |
> |
case DTxyze: |
432 |
> |
fprtresolu(cm->ncols, cm->nrows, fp); |
433 |
> |
for (r = 0; r < cm->nrows; r++, mp += 3*cm->ncols) |
434 |
> |
if (fwritescan((COLOR *)mp, cm->ncols, fp) < 0) |
435 |
> |
return(0); |
436 |
> |
break; |
437 |
> |
default: |
438 |
> |
fputs("Unsupported data type in cm_write()!\n", stderr); |
439 |
> |
return(0); |
440 |
|
} |
441 |
+ |
return(fflush(fp) == 0); |
442 |
|
} |