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