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) |
61 |
|
getDT(char *s, void *p) |
62 |
|
{ |
63 |
|
char fmt[32]; |
64 |
+ |
int i; |
65 |
|
|
66 |
< |
if (formatval(fmt, s)) { |
67 |
< |
if (!strcmp(fmt, "ascii")) |
68 |
< |
*((int *)p) = DTascii; |
69 |
< |
else if (!strcmp(fmt, "float")) |
70 |
< |
*((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; |
66 |
< |
} |
66 |
> |
if (!formatval(fmt, s)) |
67 |
> |
return(0); |
68 |
> |
for (i = 1; i < DTend; i++) |
69 |
> |
if (!strcmp(fmt, cm_fmt_id[i])) |
70 |
> |
*((int *)p) = i; |
71 |
|
return(0); |
72 |
|
} |
73 |
|
|
169 |
|
break; |
170 |
|
} |
171 |
|
} else { /* read binary file */ |
172 |
< |
if (sizeof(COLORV) == (dtype==DTfloat ? sizeof(float) : |
169 |
< |
sizeof(double))) { |
172 |
> |
if (sizeof(COLOR) == cm_elem_size[dtype]) { |
173 |
|
int nread = 0; |
174 |
|
do { /* read all we can */ |
175 |
|
nread += fread(cm->cmem + 3*nread, |
333 |
|
return(cmr); |
334 |
|
} |
335 |
|
|
336 |
< |
/* print out matrix as ASCII text -- no header */ |
337 |
< |
void |
338 |
< |
cm_print(const CMATRIX *cm, FILE *fp) |
336 |
> |
/* write out matrix to file (precede by resolution string if picture) */ |
337 |
> |
int |
338 |
> |
cm_write(const CMATRIX *cm, int dtype, FILE *fp) |
339 |
|
{ |
337 |
– |
int r, c; |
340 |
|
const COLORV *mp = cm->cmem; |
341 |
< |
|
342 |
< |
for (r = 0; r < cm->nrows; r++) { |
343 |
< |
for (c = 0; c < cm->ncols; c++, mp += 3) |
344 |
< |
fprintf(fp, "\t%.6e %.6e %.6e", mp[0], mp[1], mp[2]); |
345 |
< |
fputc('\n', fp); |
341 |
> |
int r, c; |
342 |
> |
|
343 |
> |
switch (dtype) { |
344 |
> |
case DTascii: |
345 |
> |
for (r = 0; r < cm->nrows; r++) { |
346 |
> |
for (c = 0; c < cm->ncols; c++, mp += 3) |
347 |
> |
fprintf(fp, "\t%.6e %.6e %.6e", |
348 |
> |
mp[0], mp[1], mp[2]); |
349 |
> |
fputc('\n', fp); |
350 |
> |
} |
351 |
> |
break; |
352 |
> |
case DTfloat: |
353 |
> |
case DTdouble: |
354 |
> |
if (sizeof(COLOR) == cm_elem_size[dtype]) { |
355 |
> |
r = cm->ncols*cm->nrows; |
356 |
> |
while (r > 0) { |
357 |
> |
c = fwrite(mp, sizeof(COLOR), r, fp); |
358 |
> |
if (c <= 0) |
359 |
> |
return(0); |
360 |
> |
mp += 3*c; |
361 |
> |
r -= c; |
362 |
> |
} |
363 |
> |
} else if (dtype == DTdouble) { |
364 |
> |
double dc[3]; |
365 |
> |
r = cm->ncols*cm->nrows; |
366 |
> |
while (r--) { |
367 |
> |
copycolor(dc, mp); |
368 |
> |
if (fwrite(dc, sizeof(double), 3, fp) != 3) |
369 |
> |
return(0); |
370 |
> |
mp += 3; |
371 |
> |
} |
372 |
> |
} else /* dtype == DTfloat */ { |
373 |
> |
float fc[3]; |
374 |
> |
r = cm->ncols*cm->nrows; |
375 |
> |
while (r--) { |
376 |
> |
copycolor(fc, mp); |
377 |
> |
if (fwrite(fc, sizeof(float), 3, fp) != 3) |
378 |
> |
return(0); |
379 |
> |
mp += 3; |
380 |
> |
} |
381 |
> |
} |
382 |
> |
break; |
383 |
> |
case DTrgbe: |
384 |
> |
case DTxyze: |
385 |
> |
fprtresolu(cm->ncols, cm->nrows, fp); |
386 |
> |
for (r = 0; r < cm->nrows; r++, mp += 3*cm->ncols) |
387 |
> |
if (fwritescan((COLOR *)mp, cm->ncols, fp) < 0) |
388 |
> |
return(0); |
389 |
> |
break; |
390 |
> |
default: |
391 |
> |
fputs("Unsupported data type in cm_write()!\n", stderr); |
392 |
> |
return(0); |
393 |
|
} |
394 |
+ |
return(fflush(fp) == 0); |
395 |
|
} |