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