| 16 |
|
#include "resolu.h" |
| 17 |
|
|
| 18 |
|
const char *cm_fmt_id[] = { |
| 19 |
< |
"unknown", "ascii", COLRFMT, CIEFMT, |
| 20 |
< |
"float", "double" |
| 19 |
> |
"unknown", COLRFMT, CIEFMT, |
| 20 |
> |
"float", "ascii", "double" |
| 21 |
|
}; |
| 22 |
|
|
| 23 |
|
const int cm_elem_size[] = { |
| 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 */ |
| 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) |
| 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) |
| 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); |
| 211 |
> |
char *err = cm_getheader(&dtype, &nrows, &ncols, &swap, fp); |
| 212 |
|
if (err) |
| 213 |
|
error(USER, err); |
| 214 |
|
if (ncols <= 0) |
| 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 |
|
} |
| 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 */ |