| 21 |
|
}; |
| 22 |
|
|
| 23 |
|
const int cm_elem_size[] = { |
| 24 |
< |
0, 0, 3*sizeof(float), 3*sizeof(double), 4, 4 |
| 24 |
> |
0, 0, 4, 4, 3*sizeof(float), 3*sizeof(double) |
| 25 |
|
}; |
| 26 |
|
|
| 27 |
|
/* Allocate a color coefficient matrix */ |
| 34 |
|
error(USER, "attempt to create empty matrix"); |
| 35 |
|
cm = (CMATRIX *)malloc(sizeof(CMATRIX) + |
| 36 |
|
sizeof(COLOR)*(nrows*ncols - 1)); |
| 37 |
< |
if (cm == NULL) |
| 37 |
> |
if (!cm) |
| 38 |
|
error(SYSTEM, "out of memory in cm_alloc()"); |
| 39 |
|
cm->nrows = nrows; |
| 40 |
|
cm->ncols = ncols; |
| 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) { |
| 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 == NULL) |
| 75 |
> |
if (!cm) |
| 76 |
|
error(SYSTEM, "out of memory in cm_resize()"); |
| 77 |
|
} |
| 78 |
|
cm->nrows = nrows; |
| 81 |
|
|
| 82 |
|
typedef struct { |
| 83 |
|
int dtype; /* data type */ |
| 84 |
+ |
int need2swap; /* need byte swap? */ |
| 85 |
|
int nrows, ncols; /* matrix size */ |
| 86 |
|
char *err; /* error message */ |
| 87 |
|
} CMINFO; /* header info record */ |
| 90 |
|
get_cminfo(char *s, void *p) |
| 91 |
|
{ |
| 92 |
|
CMINFO *ip = (CMINFO *)p; |
| 93 |
< |
char fmt[32]; |
| 93 |
> |
char fmt[MAXFMTLEN]; |
| 94 |
|
int i; |
| 95 |
|
|
| 96 |
|
if (!strncmp(s, "NCOMP=", 6) && atoi(s+6) != 3) { |
| 105 |
|
ip->ncols = atoi(s+6); |
| 106 |
|
return(0); |
| 107 |
|
} |
| 108 |
+ |
if ((i = isbigendian(s)) >= 0) { |
| 109 |
+ |
ip->need2swap = (nativebigendian() != i); |
| 110 |
+ |
return(0); |
| 111 |
+ |
} |
| 112 |
|
if (!formatval(fmt, s)) |
| 113 |
|
return(0); |
| 114 |
|
for (i = 1; i < DTend; i++) |
| 119 |
|
|
| 120 |
|
/* Load header to obtain/check data type and number of columns */ |
| 121 |
|
char * |
| 122 |
< |
cm_getheader(int *dt, int *nr, int *nc, FILE *fp) |
| 122 |
> |
cm_getheader(int *dt, int *nr, int *nc, int *swp, FILE *fp) |
| 123 |
|
{ |
| 124 |
|
CMINFO cmi; |
| 125 |
|
/* read header */ |
| 126 |
|
cmi.dtype = DTfromHeader; |
| 127 |
+ |
cmi.need2swap = 0; |
| 128 |
|
cmi.nrows = cmi.ncols = 0; |
| 129 |
|
cmi.err = "unexpected EOF in header"; |
| 130 |
|
if (getheader(fp, get_cminfo, &cmi) < 0) |
| 131 |
|
return(cmi.err); |
| 132 |
< |
if (dt != NULL) { /* get/check data type? */ |
| 132 |
> |
if (dt) { /* get/check data type? */ |
| 133 |
|
if (cmi.dtype == DTfromHeader) { |
| 134 |
|
if (*dt == DTfromHeader) |
| 135 |
|
return("missing/unknown data format in header"); |
| 138 |
|
else if (*dt != cmi.dtype) |
| 139 |
|
return("unexpected data format in header"); |
| 140 |
|
} |
| 141 |
< |
if (nr != NULL) { /* get/check #rows? */ |
| 141 |
> |
if (nr) { /* get/check #rows? */ |
| 142 |
|
if (*nr <= 0) |
| 143 |
|
*nr = cmi.nrows; |
| 144 |
|
else if ((cmi.nrows > 0) & (*nr != cmi.nrows)) |
| 145 |
|
return("unexpected row count in header"); |
| 146 |
|
} |
| 147 |
< |
if (nc != NULL) { /* get/check #columns? */ |
| 147 |
> |
if (nc) { /* get/check #columns? */ |
| 148 |
|
if (*nc <= 0) |
| 149 |
|
*nc = cmi.ncols; |
| 150 |
|
else if ((cmi.ncols > 0) & (*nc != cmi.ncols)) |
| 151 |
|
return("unexpected column count in header"); |
| 152 |
|
} |
| 153 |
+ |
if (swp) /* get/check swap? */ |
| 154 |
+ |
*swp = cmi.need2swap; |
| 155 |
|
return(NULL); |
| 156 |
|
} |
| 157 |
|
|
| 158 |
+ |
/* Allocate and load image data into matrix */ |
| 159 |
+ |
static CMATRIX * |
| 160 |
+ |
cm_load_rgbe(FILE *fp, int nrows, int ncols) |
| 161 |
+ |
{ |
| 162 |
+ |
CMATRIX *cm; |
| 163 |
+ |
COLORV *mp; |
| 164 |
+ |
/* header already loaded */ |
| 165 |
+ |
if ((nrows <= 0) | (ncols <= 0) && !fscnresolu(&ncols, &nrows, fp)) { |
| 166 |
+ |
error(USER, "bad picture resolution string"); |
| 167 |
+ |
return(NULL); |
| 168 |
+ |
} |
| 169 |
+ |
cm = cm_alloc(nrows, ncols); |
| 170 |
+ |
if (!cm) |
| 171 |
+ |
return(NULL); |
| 172 |
+ |
mp = cm->cmem; |
| 173 |
+ |
while (nrows--) { |
| 174 |
+ |
if (freadscan((COLOR *)mp, ncols, fp) < 0) { |
| 175 |
+ |
error(USER, "error reading color picture as matrix"); |
| 176 |
+ |
cm_free(cm); |
| 177 |
+ |
return(NULL); |
| 178 |
+ |
} |
| 179 |
+ |
mp += 3*ncols; |
| 180 |
+ |
} /* caller closes stream */ |
| 181 |
+ |
return(cm); |
| 182 |
+ |
} |
| 183 |
+ |
|
| 184 |
|
/* Allocate and load a matrix from the given input (or stdin if NULL) */ |
| 185 |
|
CMATRIX * |
| 186 |
|
cm_load(const char *inspec, int nrows, int ncols, int dtype) |
| 187 |
|
{ |
| 188 |
|
const int ROWINC = 2048; |
| 189 |
|
FILE *fp = stdin; |
| 190 |
+ |
int swap = 0; |
| 191 |
|
CMATRIX *cm; |
| 192 |
|
|
| 193 |
< |
if (inspec == NULL) |
| 193 |
> |
if (!inspec) |
| 194 |
|
inspec = "<stdin>"; |
| 195 |
|
else if (inspec[0] == '!') { |
| 196 |
|
fp = popen(inspec+1, "r"); |
| 197 |
< |
if (fp == NULL) { |
| 197 |
> |
if (!fp) { |
| 198 |
|
sprintf(errmsg, "cannot start command '%s'", inspec); |
| 199 |
|
error(SYSTEM, errmsg); |
| 200 |
|
} |
| 201 |
< |
} else if ((fp = fopen(inspec, "r")) == NULL) { |
| 201 |
> |
} else if (!(fp = fopen(inspec, "r"))) { |
| 202 |
|
sprintf(errmsg, "cannot open file '%s'", inspec); |
| 203 |
|
error(SYSTEM, errmsg); |
| 204 |
|
} |
| 208 |
|
if (dtype != DTascii) |
| 209 |
|
SET_FILE_BINARY(fp); /* doesn't really work */ |
| 210 |
|
if (!dtype | !ncols) { /* expecting header? */ |
| 211 |
< |
char *err = cm_getheader(&dtype, &nrows, &ncols, fp); |
| 212 |
< |
if (err != NULL) |
| 211 |
> |
char *err = cm_getheader(&dtype, &nrows, &ncols, &swap, fp); |
| 212 |
> |
if (err) |
| 213 |
|
error(USER, err); |
| 214 |
|
if (ncols <= 0) |
| 215 |
|
error(USER, "unspecified number of columns"); |
| 219 |
|
case DTfloat: |
| 220 |
|
case DTdouble: |
| 221 |
|
break; |
| 222 |
+ |
case DTrgbe: |
| 223 |
+ |
case DTxyze: |
| 224 |
+ |
cm = cm_load_rgbe(fp, nrows, ncols); |
| 225 |
+ |
goto cleanup; |
| 226 |
|
default: |
| 227 |
|
error(USER, "unexpected data type in cm_load()"); |
| 228 |
|
} |
| 254 |
|
cm = cm_alloc(guessrows, ncols); |
| 255 |
|
} else |
| 256 |
|
cm = cm_alloc(nrows, ncols); |
| 257 |
< |
if (cm == NULL) /* XXX never happens */ |
| 257 |
> |
if (!cm) /* XXX never happens */ |
| 258 |
|
return(NULL); |
| 259 |
|
if (dtype == DTascii) { /* read text file */ |
| 260 |
|
int maxrow = (nrows > 0 ? nrows : 32000); |
| 285 |
|
if (sizeof(COLOR) == cm_elem_size[dtype]) { |
| 286 |
|
int nread = 0; |
| 287 |
|
do { /* read all we can */ |
| 288 |
< |
nread += fread(cm->cmem + 3*nread, |
| 288 |
> |
nread += getbinary(cm->cmem + 3*nread, |
| 289 |
|
sizeof(COLOR), |
| 290 |
|
cm->nrows*cm->ncols - nread, |
| 291 |
|
fp); |
| 310 |
|
if (n <= 0) |
| 311 |
|
goto not_handled; |
| 312 |
|
while (n--) { |
| 313 |
< |
if (fread(dc, sizeof(double), 3, fp) != 3) |
| 313 |
> |
if (getbinary(dc, sizeof(double), 3, fp) != 3) |
| 314 |
|
goto EOFerror; |
| 315 |
|
copycolor(cvp, dc); |
| 316 |
|
cvp += 3; |
| 323 |
|
if (n <= 0) |
| 324 |
|
goto not_handled; |
| 325 |
|
while (n--) { |
| 326 |
< |
if (fread(fc, sizeof(float), 3, fp) != 3) |
| 326 |
> |
if (getbinary(fc, sizeof(float), 3, fp) != 3) |
| 327 |
|
goto EOFerror; |
| 328 |
|
copycolor(cvp, fc); |
| 329 |
|
cvp += 3; |
| 336 |
|
error(WARNING, errmsg); |
| 337 |
|
} |
| 338 |
|
} |
| 339 |
+ |
if (swap) { |
| 340 |
+ |
if (dtype == DTfloat) |
| 341 |
+ |
swap32((char *)cm->cmem, 3*cm->nrows*cm->ncols); |
| 342 |
+ |
else if (dtype == DTdouble) |
| 343 |
+ |
swap64((char *)cm->cmem, 3*cm->nrows*cm->ncols); |
| 344 |
+ |
} |
| 345 |
+ |
cleanup: |
| 346 |
|
if (fp != stdin) { |
| 347 |
|
if (inspec[0] != '!') |
| 348 |
|
fclose(fp); |
| 359 |
|
EOFerror: |
| 360 |
|
sprintf(errmsg, "unexpected EOF reading %s", inspec); |
| 361 |
|
error(USER, errmsg); |
| 362 |
+ |
return(NULL); |
| 363 |
|
not_handled: |
| 364 |
|
error(INTERNAL, "unhandled data size or length in cm_load()"); |
| 365 |
|
return(NULL); /* gratis return */ |
| 372 |
|
CMATRIX *cvr; |
| 373 |
|
int dr; |
| 374 |
|
|
| 375 |
+ |
if (!cm) |
| 376 |
+ |
return(NULL); |
| 377 |
|
if ((c < 0) | (c >= cm->ncols)) |
| 378 |
|
error(INTERNAL, "column requested outside matrix"); |
| 379 |
|
cvr = cm_alloc(cm->nrows, 1); |
| 380 |
< |
if (cvr == NULL) |
| 380 |
> |
if (!cvr) |
| 381 |
|
return(NULL); |
| 382 |
|
for (dr = 0; dr < cm->nrows; dr++) { |
| 383 |
|
const COLORV *sp = cm_lval(cm,dr,c); |
| 389 |
|
return(cvr); |
| 390 |
|
} |
| 391 |
|
|
| 341 |
– |
/* Scale a matrix by a single value */ |
| 342 |
– |
CMATRIX * |
| 343 |
– |
cm_scale(const CMATRIX *cm1, const COLOR sca) |
| 344 |
– |
{ |
| 345 |
– |
CMATRIX *cmr; |
| 346 |
– |
int dr, dc; |
| 347 |
– |
|
| 348 |
– |
cmr = cm_alloc(cm1->nrows, cm1->ncols); |
| 349 |
– |
if (cmr == NULL) |
| 350 |
– |
return(NULL); |
| 351 |
– |
for (dr = 0; dr < cmr->nrows; dr++) |
| 352 |
– |
for (dc = 0; dc < cmr->ncols; dc++) { |
| 353 |
– |
const COLORV *sp = cm_lval(cm1,dr,dc); |
| 354 |
– |
COLORV *dp = cm_lval(cmr,dr,dc); |
| 355 |
– |
dp[0] = sp[0] * sca[0]; |
| 356 |
– |
dp[1] = sp[1] * sca[1]; |
| 357 |
– |
dp[2] = sp[2] * sca[2]; |
| 358 |
– |
} |
| 359 |
– |
return(cmr); |
| 360 |
– |
} |
| 361 |
– |
|
| 392 |
|
/* Multiply two matrices (or a matrix and a vector) and allocate the result */ |
| 393 |
|
CMATRIX * |
| 394 |
|
cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2) |
| 397 |
|
CMATRIX *cmr; |
| 398 |
|
int dr, dc, i; |
| 399 |
|
|
| 400 |
+ |
if (!cm1 | !cm2) |
| 401 |
+ |
return(NULL); |
| 402 |
|
if ((cm1->ncols <= 0) | (cm1->ncols != cm2->nrows)) |
| 403 |
|
error(INTERNAL, "matrix dimension mismatch in cm_multiply()"); |
| 404 |
|
cmr = cm_alloc(cm1->nrows, cm2->ncols); |
| 405 |
< |
if (cmr == NULL) |
| 405 |
> |
if (!cmr) |
| 406 |
|
return(NULL); |
| 407 |
|
/* optimization: check for zero rows & cols */ |
| 408 |
|
if (((cm1->nrows > 5) | (cm2->ncols > 5)) & (cm1->ncols > 5)) { |
| 427 |
|
COLORV *dp = cm_lval(cmr,dr,dc); |
| 428 |
|
double res[3]; |
| 429 |
|
dp[0] = dp[1] = dp[2] = 0; |
| 430 |
< |
if (rowcheck != NULL && !rowcheck[dr]) |
| 430 |
> |
if (rowcheck && !rowcheck[dr]) |
| 431 |
|
continue; |
| 432 |
< |
if (colcheck != NULL && !colcheck[dc]) |
| 432 |
> |
if (colcheck && !colcheck[dc]) |
| 433 |
|
continue; |
| 434 |
|
res[0] = res[1] = res[2] = 0; |
| 435 |
|
for (i = 0; i < cm1->ncols; i++) { |
| 436 |
|
const COLORV *cp1 = cm_lval(cm1,dr,i); |
| 437 |
|
const COLORV *cp2 = cm_lval(cm2,i,dc); |
| 438 |
< |
res[0] += cp1[0] * cp2[0]; |
| 439 |
< |
res[1] += cp1[1] * cp2[1]; |
| 440 |
< |
res[2] += cp1[2] * cp2[2]; |
| 438 |
> |
res[0] += (double)cp1[0] * cp2[0]; |
| 439 |
> |
res[1] += (double)cp1[1] * cp2[1]; |
| 440 |
> |
res[2] += (double)cp1[2] * cp2[2]; |
| 441 |
|
} |
| 442 |
|
copycolor(dp, res); |
| 443 |
|
} |
| 444 |
< |
if (rowcheck != NULL) free(rowcheck); |
| 445 |
< |
if (colcheck != NULL) free(colcheck); |
| 444 |
> |
if (rowcheck) free(rowcheck); |
| 445 |
> |
if (colcheck) free(colcheck); |
| 446 |
|
return(cmr); |
| 447 |
|
} |
| 448 |
|
|
| 451 |
|
cm_write(const CMATRIX *cm, int dtype, FILE *fp) |
| 452 |
|
{ |
| 453 |
|
static const char tabEOL[2] = {'\t','\n'}; |
| 454 |
< |
const COLORV *mp = cm->cmem; |
| 454 |
> |
const COLORV *mp; |
| 455 |
|
int r, c; |
| 456 |
|
|
| 457 |
+ |
if (!cm) |
| 458 |
+ |
return(0); |
| 459 |
+ |
mp = cm->cmem; |
| 460 |
|
switch (dtype) { |
| 461 |
|
case DTascii: |
| 462 |
|
for (r = 0; r < cm->nrows; r++) |
| 470 |
|
if (sizeof(COLOR) == cm_elem_size[dtype]) { |
| 471 |
|
r = cm->ncols*cm->nrows; |
| 472 |
|
while (r > 0) { |
| 473 |
< |
c = fwrite(mp, sizeof(COLOR), r, fp); |
| 473 |
> |
c = putbinary(mp, sizeof(COLOR), r, fp); |
| 474 |
|
if (c <= 0) |
| 475 |
|
return(0); |
| 476 |
|
mp += 3*c; |
| 481 |
|
r = cm->ncols*cm->nrows; |
| 482 |
|
while (r--) { |
| 483 |
|
copycolor(dc, mp); |
| 484 |
< |
if (fwrite(dc, sizeof(double), 3, fp) != 3) |
| 484 |
> |
if (putbinary(dc, sizeof(double), 3, fp) != 3) |
| 485 |
|
return(0); |
| 486 |
|
mp += 3; |
| 487 |
|
} |
| 490 |
|
r = cm->ncols*cm->nrows; |
| 491 |
|
while (r--) { |
| 492 |
|
copycolor(fc, mp); |
| 493 |
< |
if (fwrite(fc, sizeof(float), 3, fp) != 3) |
| 493 |
> |
if (putbinary(fc, sizeof(float), 3, fp) != 3) |
| 494 |
|
return(0); |
| 495 |
|
mp += 3; |
| 496 |
|
} |