1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.13 |
static const char RCSid[] = "$Id: cmatrix.c,v 2.12 2015/07/22 04:29:56 greg Exp $"; |
3 |
greg |
2.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Color matrix routines. |
6 |
|
|
* |
7 |
|
|
* G. Ward |
8 |
|
|
*/ |
9 |
|
|
|
10 |
|
|
#include <ctype.h> |
11 |
|
|
#include "standard.h" |
12 |
|
|
#include "cmatrix.h" |
13 |
|
|
#include "platform.h" |
14 |
greg |
2.10 |
#include "rtprocess.h" |
15 |
greg |
2.2 |
#include "resolu.h" |
16 |
|
|
|
17 |
|
|
const char *cm_fmt_id[] = { |
18 |
greg |
2.8 |
"unknown", "ascii", COLRFMT, CIEFMT, |
19 |
|
|
"float", "double" |
20 |
greg |
2.2 |
}; |
21 |
|
|
|
22 |
|
|
const int cm_elem_size[] = { |
23 |
|
|
0, 0, 3*sizeof(float), 3*sizeof(double), 4, 4 |
24 |
|
|
}; |
25 |
greg |
2.1 |
|
26 |
|
|
/* Allocate a color coefficient matrix */ |
27 |
|
|
CMATRIX * |
28 |
|
|
cm_alloc(int nrows, int ncols) |
29 |
|
|
{ |
30 |
|
|
CMATRIX *cm; |
31 |
|
|
|
32 |
|
|
if ((nrows <= 0) | (ncols <= 0)) |
33 |
|
|
error(USER, "attempt to create empty matrix"); |
34 |
|
|
cm = (CMATRIX *)malloc(sizeof(CMATRIX) + |
35 |
greg |
2.4 |
sizeof(COLOR)*(nrows*ncols - 1)); |
36 |
greg |
2.1 |
if (cm == NULL) |
37 |
|
|
error(SYSTEM, "out of memory in cm_alloc()"); |
38 |
|
|
cm->nrows = nrows; |
39 |
|
|
cm->ncols = ncols; |
40 |
|
|
return(cm); |
41 |
|
|
} |
42 |
|
|
|
43 |
greg |
2.6 |
static void |
44 |
|
|
adjacent_ra_sizes(size_t bounds[2], size_t target) |
45 |
|
|
{ |
46 |
|
|
bounds[0] = 0; bounds[1] = 2048; |
47 |
|
|
while (bounds[1] < target) { |
48 |
|
|
bounds[0] = bounds[1]; |
49 |
|
|
bounds[1] += bounds[1]>>1; |
50 |
|
|
} |
51 |
|
|
} |
52 |
|
|
|
53 |
greg |
2.1 |
/* Resize color coefficient matrix */ |
54 |
|
|
CMATRIX * |
55 |
|
|
cm_resize(CMATRIX *cm, int nrows) |
56 |
|
|
{ |
57 |
greg |
2.6 |
size_t old_size, new_size, ra_bounds[2]; |
58 |
|
|
|
59 |
greg |
2.1 |
if (nrows == cm->nrows) |
60 |
|
|
return(cm); |
61 |
|
|
if (nrows <= 0) { |
62 |
|
|
cm_free(cm); |
63 |
|
|
return(NULL); |
64 |
|
|
} |
65 |
greg |
2.6 |
old_size = sizeof(CMATRIX) + sizeof(COLOR)*(cm->nrows*cm->ncols - 1); |
66 |
|
|
adjacent_ra_sizes(ra_bounds, old_size); |
67 |
|
|
new_size = sizeof(CMATRIX) + sizeof(COLOR)*(nrows*cm->ncols - 1); |
68 |
|
|
if (nrows < cm->nrows ? new_size <= ra_bounds[0] : |
69 |
|
|
new_size > ra_bounds[1]) { |
70 |
|
|
adjacent_ra_sizes(ra_bounds, new_size); |
71 |
|
|
cm = (CMATRIX *)realloc(cm, ra_bounds[1]); |
72 |
|
|
if (cm == NULL) |
73 |
|
|
error(SYSTEM, "out of memory in cm_resize()"); |
74 |
|
|
} |
75 |
greg |
2.1 |
cm->nrows = nrows; |
76 |
|
|
return(cm); |
77 |
|
|
} |
78 |
|
|
|
79 |
greg |
2.5 |
typedef struct { |
80 |
|
|
int dtype; /* data type */ |
81 |
|
|
int nrows, ncols; /* matrix size */ |
82 |
|
|
char *err; /* error message */ |
83 |
|
|
} CMINFO; /* header info record */ |
84 |
|
|
|
85 |
greg |
2.1 |
static int |
86 |
greg |
2.5 |
get_cminfo(char *s, void *p) |
87 |
greg |
2.1 |
{ |
88 |
greg |
2.5 |
CMINFO *ip = (CMINFO *)p; |
89 |
greg |
2.1 |
char fmt[32]; |
90 |
greg |
2.2 |
int i; |
91 |
greg |
2.5 |
|
92 |
|
|
if (!strncmp(s, "NCOMP=", 6) && atoi(s+6) != 3) { |
93 |
|
|
ip->err = "unexpected # components (must be 3)"; |
94 |
|
|
return(-1); |
95 |
|
|
} |
96 |
|
|
if (!strncmp(s, "NROWS=", 6)) { |
97 |
|
|
ip->nrows = atoi(s+6); |
98 |
|
|
return(0); |
99 |
|
|
} |
100 |
|
|
if (!strncmp(s, "NCOLS=", 6)) { |
101 |
|
|
ip->ncols = atoi(s+6); |
102 |
|
|
return(0); |
103 |
|
|
} |
104 |
greg |
2.2 |
if (!formatval(fmt, s)) |
105 |
|
|
return(0); |
106 |
|
|
for (i = 1; i < DTend; i++) |
107 |
|
|
if (!strcmp(fmt, cm_fmt_id[i])) |
108 |
greg |
2.5 |
ip->dtype = i; |
109 |
greg |
2.1 |
return(0); |
110 |
|
|
} |
111 |
|
|
|
112 |
greg |
2.5 |
/* Load header to obtain/check data type and number of columns */ |
113 |
|
|
char * |
114 |
|
|
cm_getheader(int *dt, int *nr, int *nc, FILE *fp) |
115 |
greg |
2.1 |
{ |
116 |
greg |
2.5 |
CMINFO cmi; |
117 |
|
|
/* read header */ |
118 |
|
|
cmi.dtype = DTfromHeader; |
119 |
|
|
cmi.nrows = cmi.ncols = 0; |
120 |
|
|
cmi.err = "unexpected EOF in header"; |
121 |
greg |
2.7 |
if (getheader(fp, get_cminfo, &cmi) < 0) |
122 |
greg |
2.5 |
return(cmi.err); |
123 |
|
|
if (dt != NULL) { /* get/check data type? */ |
124 |
|
|
if (cmi.dtype == DTfromHeader) { |
125 |
|
|
if (*dt == DTfromHeader) |
126 |
|
|
return("missing/unknown data format in header"); |
127 |
|
|
} else if (*dt == DTfromHeader) |
128 |
|
|
*dt = cmi.dtype; |
129 |
|
|
else if (*dt != cmi.dtype) |
130 |
|
|
return("unexpected data format in header"); |
131 |
|
|
} |
132 |
|
|
if (nr != NULL) { /* get/check #rows? */ |
133 |
|
|
if (*nr <= 0) |
134 |
|
|
*nr = cmi.nrows; |
135 |
|
|
else if ((cmi.nrows > 0) & (*nr != cmi.nrows)) |
136 |
|
|
return("unexpected row count in header"); |
137 |
|
|
} |
138 |
|
|
if (nc != NULL) { /* get/check #columns? */ |
139 |
|
|
if (*nc <= 0) |
140 |
|
|
*nc = cmi.ncols; |
141 |
|
|
else if ((cmi.ncols > 0) & (*nc != cmi.ncols)) |
142 |
|
|
return("unexpected column count in header"); |
143 |
|
|
} |
144 |
|
|
return(NULL); |
145 |
greg |
2.1 |
} |
146 |
|
|
|
147 |
greg |
2.9 |
/* Allocate and load a matrix from the given input (or stdin if NULL) */ |
148 |
greg |
2.1 |
CMATRIX * |
149 |
greg |
2.9 |
cm_load(const char *inspec, int nrows, int ncols, int dtype) |
150 |
greg |
2.1 |
{ |
151 |
|
|
FILE *fp = stdin; |
152 |
|
|
CMATRIX *cm; |
153 |
|
|
|
154 |
greg |
2.9 |
if (inspec == NULL) |
155 |
|
|
inspec = "<stdin>"; |
156 |
|
|
else if (inspec[0] == '!') { |
157 |
|
|
fp = popen(inspec+1, "r"); |
158 |
|
|
if (fp == NULL) { |
159 |
|
|
sprintf(errmsg, "cannot start command '%s'", inspec); |
160 |
|
|
error(SYSTEM, errmsg); |
161 |
|
|
} |
162 |
|
|
} else if ((fp = fopen(inspec, "r")) == NULL) { |
163 |
|
|
sprintf(errmsg, "cannot open file '%s'", inspec); |
164 |
greg |
2.1 |
error(SYSTEM, errmsg); |
165 |
|
|
} |
166 |
|
|
#ifdef getc_unlocked |
167 |
|
|
flockfile(fp); |
168 |
|
|
#endif |
169 |
|
|
if (dtype != DTascii) |
170 |
|
|
SET_FILE_BINARY(fp); /* doesn't really work */ |
171 |
greg |
2.5 |
if (!dtype | !ncols) { /* expecting header? */ |
172 |
|
|
char *err = cm_getheader(&dtype, &nrows, &ncols, fp); |
173 |
|
|
if (err != NULL) |
174 |
|
|
error(USER, err); |
175 |
|
|
if (ncols <= 0) |
176 |
|
|
error(USER, "unspecified number of columns"); |
177 |
|
|
} |
178 |
greg |
2.1 |
switch (dtype) { |
179 |
|
|
case DTascii: |
180 |
|
|
case DTfloat: |
181 |
|
|
case DTdouble: |
182 |
|
|
break; |
183 |
|
|
default: |
184 |
|
|
error(USER, "unexpected data type in cm_load()"); |
185 |
|
|
} |
186 |
|
|
if (nrows <= 0) { /* don't know length? */ |
187 |
|
|
int guessrows = 147; /* usually big enough */ |
188 |
greg |
2.9 |
if ((dtype != DTascii) & (fp != stdin) & (inspec[0] != '!')) { |
189 |
greg |
2.1 |
long startpos = ftell(fp); |
190 |
|
|
if (fseek(fp, 0L, SEEK_END) == 0) { |
191 |
|
|
long endpos = ftell(fp); |
192 |
|
|
long elemsiz = 3*(dtype==DTfloat ? |
193 |
|
|
sizeof(float) : sizeof(double)); |
194 |
|
|
|
195 |
|
|
if ((endpos - startpos) % (ncols*elemsiz)) { |
196 |
|
|
sprintf(errmsg, |
197 |
|
|
"improper length for binary file '%s'", |
198 |
greg |
2.9 |
inspec); |
199 |
greg |
2.1 |
error(USER, errmsg); |
200 |
|
|
} |
201 |
|
|
guessrows = (endpos - startpos)/(ncols*elemsiz); |
202 |
|
|
if (fseek(fp, startpos, SEEK_SET) < 0) { |
203 |
|
|
sprintf(errmsg, |
204 |
|
|
"fseek() error on file '%s'", |
205 |
greg |
2.9 |
inspec); |
206 |
greg |
2.1 |
error(SYSTEM, errmsg); |
207 |
|
|
} |
208 |
|
|
nrows = guessrows; /* we're confident */ |
209 |
|
|
} |
210 |
|
|
} |
211 |
|
|
cm = cm_alloc(guessrows, ncols); |
212 |
|
|
} else |
213 |
|
|
cm = cm_alloc(nrows, ncols); |
214 |
|
|
if (cm == NULL) /* XXX never happens */ |
215 |
|
|
return(NULL); |
216 |
|
|
if (dtype == DTascii) { /* read text file */ |
217 |
|
|
int maxrow = (nrows > 0 ? nrows : 32000); |
218 |
|
|
int r, c; |
219 |
|
|
for (r = 0; r < maxrow; r++) { |
220 |
|
|
if (r >= cm->nrows) /* need more space? */ |
221 |
|
|
cm = cm_resize(cm, 2*cm->nrows); |
222 |
|
|
for (c = 0; c < ncols; c++) { |
223 |
|
|
COLORV *cv = cm_lval(cm,r,c); |
224 |
greg |
2.13 |
if (fscanf(fp, COLSPEC, cv, cv+1, cv+2) != 3) { |
225 |
greg |
2.1 |
if ((nrows <= 0) & (r > 0) & !c) { |
226 |
|
|
cm = cm_resize(cm, maxrow=r); |
227 |
|
|
break; |
228 |
|
|
} else |
229 |
|
|
goto EOFerror; |
230 |
greg |
2.13 |
} |
231 |
greg |
2.1 |
} |
232 |
|
|
} |
233 |
|
|
while ((c = getc(fp)) != EOF) |
234 |
|
|
if (!isspace(c)) { |
235 |
|
|
sprintf(errmsg, |
236 |
greg |
2.9 |
"unexpected data at end of ascii input '%s'", |
237 |
|
|
inspec); |
238 |
greg |
2.1 |
error(WARNING, errmsg); |
239 |
|
|
break; |
240 |
|
|
} |
241 |
|
|
} else { /* read binary file */ |
242 |
greg |
2.2 |
if (sizeof(COLOR) == cm_elem_size[dtype]) { |
243 |
greg |
2.1 |
int nread = 0; |
244 |
|
|
do { /* read all we can */ |
245 |
|
|
nread += fread(cm->cmem + 3*nread, |
246 |
greg |
2.4 |
sizeof(COLOR), |
247 |
greg |
2.1 |
cm->nrows*cm->ncols - nread, |
248 |
|
|
fp); |
249 |
|
|
if (nrows <= 0) { /* unknown length */ |
250 |
|
|
if (nread == cm->nrows*cm->ncols) |
251 |
|
|
/* need more space? */ |
252 |
|
|
cm = cm_resize(cm, 2*cm->nrows); |
253 |
|
|
else if (nread && !(nread % cm->ncols)) |
254 |
|
|
/* seem to be done */ |
255 |
|
|
cm = cm_resize(cm, nread/cm->ncols); |
256 |
|
|
else /* ended mid-row */ |
257 |
|
|
goto EOFerror; |
258 |
|
|
} else if (nread < cm->nrows*cm->ncols) |
259 |
|
|
goto EOFerror; |
260 |
|
|
} while (nread < cm->nrows*cm->ncols); |
261 |
|
|
|
262 |
|
|
} else if (dtype == DTdouble) { |
263 |
|
|
double dc[3]; /* load from double */ |
264 |
|
|
COLORV *cvp = cm->cmem; |
265 |
|
|
int n = nrows*ncols; |
266 |
|
|
|
267 |
|
|
if (n <= 0) |
268 |
|
|
goto not_handled; |
269 |
|
|
while (n--) { |
270 |
|
|
if (fread(dc, sizeof(double), 3, fp) != 3) |
271 |
|
|
goto EOFerror; |
272 |
|
|
copycolor(cvp, dc); |
273 |
|
|
cvp += 3; |
274 |
|
|
} |
275 |
|
|
} else /* dtype == DTfloat */ { |
276 |
|
|
float fc[3]; /* load from float */ |
277 |
|
|
COLORV *cvp = cm->cmem; |
278 |
|
|
int n = nrows*ncols; |
279 |
|
|
|
280 |
|
|
if (n <= 0) |
281 |
|
|
goto not_handled; |
282 |
|
|
while (n--) { |
283 |
|
|
if (fread(fc, sizeof(float), 3, fp) != 3) |
284 |
|
|
goto EOFerror; |
285 |
|
|
copycolor(cvp, fc); |
286 |
|
|
cvp += 3; |
287 |
|
|
} |
288 |
|
|
} |
289 |
|
|
if (fgetc(fp) != EOF) { |
290 |
|
|
sprintf(errmsg, |
291 |
greg |
2.9 |
"unexpected data at end of binary input '%s'", |
292 |
|
|
inspec); |
293 |
greg |
2.1 |
error(WARNING, errmsg); |
294 |
|
|
} |
295 |
|
|
} |
296 |
greg |
2.9 |
if (fp != stdin) { |
297 |
|
|
if (inspec[0] != '!') |
298 |
|
|
fclose(fp); |
299 |
|
|
else if (pclose(fp)) { |
300 |
|
|
sprintf(errmsg, "error running command '%s'", inspec); |
301 |
|
|
error(WARNING, errmsg); |
302 |
|
|
} |
303 |
|
|
} |
304 |
greg |
2.1 |
#ifdef getc_unlocked |
305 |
|
|
else |
306 |
|
|
funlockfile(fp); |
307 |
|
|
#endif |
308 |
|
|
return(cm); |
309 |
|
|
EOFerror: |
310 |
greg |
2.9 |
sprintf(errmsg, "unexpected EOF reading %s", inspec); |
311 |
greg |
2.1 |
error(USER, errmsg); |
312 |
|
|
not_handled: |
313 |
|
|
error(INTERNAL, "unhandled data size or length in cm_load()"); |
314 |
|
|
return(NULL); /* gratis return */ |
315 |
|
|
} |
316 |
|
|
|
317 |
|
|
/* Extract a column vector from a matrix */ |
318 |
|
|
CMATRIX * |
319 |
|
|
cm_column(const CMATRIX *cm, int c) |
320 |
|
|
{ |
321 |
|
|
CMATRIX *cvr; |
322 |
|
|
int dr; |
323 |
|
|
|
324 |
|
|
if ((c < 0) | (c >= cm->ncols)) |
325 |
|
|
error(INTERNAL, "column requested outside matrix"); |
326 |
|
|
cvr = cm_alloc(cm->nrows, 1); |
327 |
|
|
if (cvr == NULL) |
328 |
|
|
return(NULL); |
329 |
|
|
for (dr = 0; dr < cm->nrows; dr++) { |
330 |
|
|
const COLORV *sp = cm_lval(cm,dr,c); |
331 |
|
|
COLORV *dp = cv_lval(cvr,dr); |
332 |
|
|
dp[0] = sp[0]; |
333 |
|
|
dp[1] = sp[1]; |
334 |
|
|
dp[2] = sp[2]; |
335 |
|
|
} |
336 |
|
|
return(cvr); |
337 |
|
|
} |
338 |
|
|
|
339 |
|
|
/* Scale a matrix by a single value */ |
340 |
|
|
CMATRIX * |
341 |
|
|
cm_scale(const CMATRIX *cm1, const COLOR sca) |
342 |
|
|
{ |
343 |
|
|
CMATRIX *cmr; |
344 |
|
|
int dr, dc; |
345 |
|
|
|
346 |
|
|
cmr = cm_alloc(cm1->nrows, cm1->ncols); |
347 |
|
|
if (cmr == NULL) |
348 |
|
|
return(NULL); |
349 |
|
|
for (dr = 0; dr < cmr->nrows; dr++) |
350 |
|
|
for (dc = 0; dc < cmr->ncols; dc++) { |
351 |
|
|
const COLORV *sp = cm_lval(cm1,dr,dc); |
352 |
|
|
COLORV *dp = cm_lval(cmr,dr,dc); |
353 |
|
|
dp[0] = sp[0] * sca[0]; |
354 |
|
|
dp[1] = sp[1] * sca[1]; |
355 |
|
|
dp[2] = sp[2] * sca[2]; |
356 |
|
|
} |
357 |
|
|
return(cmr); |
358 |
|
|
} |
359 |
|
|
|
360 |
|
|
/* Multiply two matrices (or a matrix and a vector) and allocate the result */ |
361 |
|
|
CMATRIX * |
362 |
|
|
cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2) |
363 |
|
|
{ |
364 |
|
|
char *rowcheck=NULL, *colcheck=NULL; |
365 |
|
|
CMATRIX *cmr; |
366 |
|
|
int dr, dc, i; |
367 |
|
|
|
368 |
|
|
if ((cm1->ncols <= 0) | (cm1->ncols != cm2->nrows)) |
369 |
|
|
error(INTERNAL, "matrix dimension mismatch in cm_multiply()"); |
370 |
|
|
cmr = cm_alloc(cm1->nrows, cm2->ncols); |
371 |
|
|
if (cmr == NULL) |
372 |
|
|
return(NULL); |
373 |
|
|
/* optimization: check for zero rows & cols */ |
374 |
|
|
if (((cm1->nrows > 5) | (cm2->ncols > 5)) & (cm1->ncols > 5)) { |
375 |
|
|
static const COLOR czero; |
376 |
|
|
rowcheck = (char *)calloc(cmr->nrows, 1); |
377 |
|
|
for (dr = cm1->nrows*(rowcheck != NULL); dr--; ) |
378 |
|
|
for (dc = cm1->ncols; dc--; ) |
379 |
|
|
if (memcmp(cm_lval(cm1,dr,dc), czero, sizeof(COLOR))) { |
380 |
|
|
rowcheck[dr] = 1; |
381 |
|
|
break; |
382 |
|
|
} |
383 |
|
|
colcheck = (char *)calloc(cmr->ncols, 1); |
384 |
|
|
for (dc = cm2->ncols*(colcheck != NULL); dc--; ) |
385 |
|
|
for (dr = cm2->nrows; dr--; ) |
386 |
|
|
if (memcmp(cm_lval(cm2,dr,dc), czero, sizeof(COLOR))) { |
387 |
|
|
colcheck[dc] = 1; |
388 |
|
|
break; |
389 |
|
|
} |
390 |
|
|
} |
391 |
|
|
for (dr = 0; dr < cmr->nrows; dr++) |
392 |
|
|
for (dc = 0; dc < cmr->ncols; dc++) { |
393 |
|
|
COLORV *dp = cm_lval(cmr,dr,dc); |
394 |
greg |
2.4 |
double res[3]; |
395 |
greg |
2.1 |
dp[0] = dp[1] = dp[2] = 0; |
396 |
|
|
if (rowcheck != NULL && !rowcheck[dr]) |
397 |
|
|
continue; |
398 |
|
|
if (colcheck != NULL && !colcheck[dc]) |
399 |
|
|
continue; |
400 |
greg |
2.4 |
res[0] = res[1] = res[2] = 0; |
401 |
greg |
2.1 |
for (i = 0; i < cm1->ncols; i++) { |
402 |
|
|
const COLORV *cp1 = cm_lval(cm1,dr,i); |
403 |
|
|
const COLORV *cp2 = cm_lval(cm2,i,dc); |
404 |
greg |
2.12 |
res[0] += cp1[0] * cp2[0]; |
405 |
|
|
res[1] += cp1[1] * cp2[1]; |
406 |
|
|
res[2] += cp1[2] * cp2[2]; |
407 |
greg |
2.1 |
} |
408 |
greg |
2.4 |
copycolor(dp, res); |
409 |
greg |
2.1 |
} |
410 |
|
|
if (rowcheck != NULL) free(rowcheck); |
411 |
|
|
if (colcheck != NULL) free(colcheck); |
412 |
|
|
return(cmr); |
413 |
|
|
} |
414 |
|
|
|
415 |
greg |
2.2 |
/* write out matrix to file (precede by resolution string if picture) */ |
416 |
|
|
int |
417 |
|
|
cm_write(const CMATRIX *cm, int dtype, FILE *fp) |
418 |
greg |
2.1 |
{ |
419 |
greg |
2.3 |
static const char tabEOL[2] = {'\t','\n'}; |
420 |
|
|
const COLORV *mp = cm->cmem; |
421 |
|
|
int r, c; |
422 |
greg |
2.2 |
|
423 |
|
|
switch (dtype) { |
424 |
|
|
case DTascii: |
425 |
greg |
2.3 |
for (r = 0; r < cm->nrows; r++) |
426 |
greg |
2.2 |
for (c = 0; c < cm->ncols; c++, mp += 3) |
427 |
greg |
2.3 |
fprintf(fp, "%.6e %.6e %.6e%c", |
428 |
|
|
mp[0], mp[1], mp[2], |
429 |
|
|
tabEOL[c >= cm->ncols-1]); |
430 |
greg |
2.2 |
break; |
431 |
|
|
case DTfloat: |
432 |
|
|
case DTdouble: |
433 |
|
|
if (sizeof(COLOR) == cm_elem_size[dtype]) { |
434 |
|
|
r = cm->ncols*cm->nrows; |
435 |
|
|
while (r > 0) { |
436 |
|
|
c = fwrite(mp, sizeof(COLOR), r, fp); |
437 |
|
|
if (c <= 0) |
438 |
|
|
return(0); |
439 |
|
|
mp += 3*c; |
440 |
|
|
r -= c; |
441 |
|
|
} |
442 |
|
|
} else if (dtype == DTdouble) { |
443 |
|
|
double dc[3]; |
444 |
|
|
r = cm->ncols*cm->nrows; |
445 |
|
|
while (r--) { |
446 |
|
|
copycolor(dc, mp); |
447 |
|
|
if (fwrite(dc, sizeof(double), 3, fp) != 3) |
448 |
|
|
return(0); |
449 |
|
|
mp += 3; |
450 |
|
|
} |
451 |
|
|
} else /* dtype == DTfloat */ { |
452 |
|
|
float fc[3]; |
453 |
|
|
r = cm->ncols*cm->nrows; |
454 |
|
|
while (r--) { |
455 |
|
|
copycolor(fc, mp); |
456 |
|
|
if (fwrite(fc, sizeof(float), 3, fp) != 3) |
457 |
|
|
return(0); |
458 |
|
|
mp += 3; |
459 |
|
|
} |
460 |
|
|
} |
461 |
|
|
break; |
462 |
|
|
case DTrgbe: |
463 |
|
|
case DTxyze: |
464 |
|
|
fprtresolu(cm->ncols, cm->nrows, fp); |
465 |
|
|
for (r = 0; r < cm->nrows; r++, mp += 3*cm->ncols) |
466 |
|
|
if (fwritescan((COLOR *)mp, cm->ncols, fp) < 0) |
467 |
|
|
return(0); |
468 |
|
|
break; |
469 |
|
|
default: |
470 |
|
|
fputs("Unsupported data type in cm_write()!\n", stderr); |
471 |
|
|
return(0); |
472 |
greg |
2.1 |
} |
473 |
greg |
2.2 |
return(fflush(fp) == 0); |
474 |
greg |
2.1 |
} |