| 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[] = { | 
| 145 |  | return(NULL); | 
| 146 |  | } | 
| 147 |  |  | 
| 148 | < | /* Allocate and load a matrix from the given file (or stdin if NULL) */ | 
| 148 | > | /* Allocate and load a matrix from the given input (or stdin if NULL) */ | 
| 149 |  | CMATRIX * | 
| 150 | < | cm_load(const char *fname, int nrows, int ncols, int dtype) | 
| 150 | > | cm_load(const char *inspec, int nrows, int ncols, int dtype) | 
| 151 |  | { | 
| 152 | < | FILE    *fp = stdin; | 
| 153 | < | CMATRIX *cm; | 
| 152 | > | const int       ROWINC = 2048; | 
| 153 | > | FILE            *fp = stdin; | 
| 154 | > | CMATRIX         *cm; | 
| 155 |  |  | 
| 156 | < | if (fname == NULL) | 
| 157 | < | fname = "<stdin>"; | 
| 158 | < | else if ((fp = fopen(fname, "r")) == NULL) { | 
| 159 | < | sprintf(errmsg, "cannot open file '%s'", fname); | 
| 156 | > | if (inspec == NULL) | 
| 157 | > | inspec = "<stdin>"; | 
| 158 | > | else if (inspec[0] == '!') { | 
| 159 | > | fp = popen(inspec+1, "r"); | 
| 160 | > | if (fp == NULL) { | 
| 161 | > | sprintf(errmsg, "cannot start command '%s'", inspec); | 
| 162 | > | error(SYSTEM, errmsg); | 
| 163 | > | } | 
| 164 | > | } else if ((fp = fopen(inspec, "r")) == NULL) { | 
| 165 | > | sprintf(errmsg, "cannot open file '%s'", inspec); | 
| 166 |  | error(SYSTEM, errmsg); | 
| 167 |  | } | 
| 168 |  | #ifdef getc_unlocked | 
| 187 |  | } | 
| 188 |  | if (nrows <= 0) {                       /* don't know length? */ | 
| 189 |  | int     guessrows = 147;        /* usually big enough */ | 
| 190 | < | if ((dtype != DTascii) & (fp != stdin)) { | 
| 190 | > | if ((dtype != DTascii) & (fp != stdin) & (inspec[0] != '!')) { | 
| 191 |  | long    startpos = ftell(fp); | 
| 192 |  | if (fseek(fp, 0L, SEEK_END) == 0) { | 
| 193 |  | long    endpos = ftell(fp); | 
| 197 |  | if ((endpos - startpos) % (ncols*elemsiz)) { | 
| 198 |  | sprintf(errmsg, | 
| 199 |  | "improper length for binary file '%s'", | 
| 200 | < | fname); | 
| 200 | > | inspec); | 
| 201 |  | error(USER, errmsg); | 
| 202 |  | } | 
| 203 |  | guessrows = (endpos - startpos)/(ncols*elemsiz); | 
| 204 |  | if (fseek(fp, startpos, SEEK_SET) < 0) { | 
| 205 |  | sprintf(errmsg, | 
| 206 |  | "fseek() error on file '%s'", | 
| 207 | < | fname); | 
| 207 | > | inspec); | 
| 208 |  | error(SYSTEM, errmsg); | 
| 209 |  | } | 
| 210 |  | nrows = guessrows;      /* we're confident */ | 
| 220 |  | int     r, c; | 
| 221 |  | for (r = 0; r < maxrow; r++) { | 
| 222 |  | if (r >= cm->nrows)                 /* need more space? */ | 
| 223 | < | cm = cm_resize(cm, 2*cm->nrows); | 
| 223 | > | cm = cm_resize(cm, cm->nrows+ROWINC); | 
| 224 |  | for (c = 0; c < ncols; c++) { | 
| 225 |  | COLORV  *cv = cm_lval(cm,r,c); | 
| 226 | < | if (fscanf(fp, COLSPEC, cv, cv+1, cv+2) != 3) | 
| 226 | > | if (fscanf(fp, COLSPEC, cv, cv+1, cv+2) != 3) { | 
| 227 |  | if ((nrows <= 0) & (r > 0) & !c) { | 
| 228 |  | cm = cm_resize(cm, maxrow=r); | 
| 229 |  | break; | 
| 230 |  | } else | 
| 231 |  | goto EOFerror; | 
| 232 | + | } | 
| 233 |  | } | 
| 234 |  | } | 
| 235 |  | while ((c = getc(fp)) != EOF) | 
| 236 |  | if (!isspace(c)) { | 
| 237 |  | sprintf(errmsg, | 
| 238 | < | "unexpected data at end of ascii file %s", | 
| 239 | < | fname); | 
| 238 | > | "unexpected data at end of ascii input '%s'", | 
| 239 | > | inspec); | 
| 240 |  | error(WARNING, errmsg); | 
| 241 |  | break; | 
| 242 |  | } | 
| 244 |  | if (sizeof(COLOR) == cm_elem_size[dtype]) { | 
| 245 |  | int     nread = 0; | 
| 246 |  | do {                            /* read all we can */ | 
| 247 | < | nread += fread(cm->cmem + 3*nread, | 
| 247 | > | nread += getbinary(cm->cmem + 3*nread, | 
| 248 |  | sizeof(COLOR), | 
| 249 |  | cm->nrows*cm->ncols - nread, | 
| 250 |  | fp); | 
| 251 |  | if (nrows <= 0) {       /* unknown length */ | 
| 252 |  | if (nread == cm->nrows*cm->ncols) | 
| 253 |  | /* need more space? */ | 
| 254 | < | cm = cm_resize(cm, 2*cm->nrows); | 
| 254 | > | cm = cm_resize(cm, cm->nrows+ROWINC); | 
| 255 |  | else if (nread && !(nread % cm->ncols)) | 
| 256 |  | /* seem to be  done */ | 
| 257 |  | cm = cm_resize(cm, nread/cm->ncols); | 
| 269 |  | if (n <= 0) | 
| 270 |  | goto not_handled; | 
| 271 |  | while (n--) { | 
| 272 | < | if (fread(dc, sizeof(double), 3, fp) != 3) | 
| 272 | > | if (getbinary(dc, sizeof(double), 3, fp) != 3) | 
| 273 |  | goto EOFerror; | 
| 274 |  | copycolor(cvp, dc); | 
| 275 |  | cvp += 3; | 
| 282 |  | if (n <= 0) | 
| 283 |  | goto not_handled; | 
| 284 |  | while (n--) { | 
| 285 | < | if (fread(fc, sizeof(float), 3, fp) != 3) | 
| 285 | > | if (getbinary(fc, sizeof(float), 3, fp) != 3) | 
| 286 |  | goto EOFerror; | 
| 287 |  | copycolor(cvp, fc); | 
| 288 |  | cvp += 3; | 
| 290 |  | } | 
| 291 |  | if (fgetc(fp) != EOF) { | 
| 292 |  | sprintf(errmsg, | 
| 293 | < | "unexpected data at end of binary file %s", | 
| 294 | < | fname); | 
| 293 | > | "unexpected data at end of binary input '%s'", | 
| 294 | > | inspec); | 
| 295 |  | error(WARNING, errmsg); | 
| 296 |  | } | 
| 297 |  | } | 
| 298 | < | if (fp != stdin) | 
| 299 | < | fclose(fp); | 
| 298 | > | if (fp != stdin) { | 
| 299 | > | if (inspec[0] != '!') | 
| 300 | > | fclose(fp); | 
| 301 | > | else if (pclose(fp)) { | 
| 302 | > | sprintf(errmsg, "error running command '%s'", inspec); | 
| 303 | > | error(WARNING, errmsg); | 
| 304 | > | } | 
| 305 | > | } | 
| 306 |  | #ifdef getc_unlocked | 
| 307 |  | else | 
| 308 |  | funlockfile(fp); | 
| 309 |  | #endif | 
| 310 |  | return(cm); | 
| 311 |  | EOFerror: | 
| 312 | < | sprintf(errmsg, "unexpected EOF reading %s", fname); | 
| 312 | > | sprintf(errmsg, "unexpected EOF reading %s", inspec); | 
| 313 |  | error(USER, errmsg); | 
| 314 |  | not_handled: | 
| 315 |  | error(INTERNAL, "unhandled data size or length in cm_load()"); | 
| 403 |  | for (i = 0; i < cm1->ncols; i++) { | 
| 404 |  | const COLORV        *cp1 = cm_lval(cm1,dr,i); | 
| 405 |  | const COLORV        *cp2 = cm_lval(cm2,i,dc); | 
| 406 | < | res[0] += cp1[0] * cp2[0]; | 
| 407 | < | res[1] += cp1[1] * cp2[1]; | 
| 408 | < | res[2] += cp1[2] * cp2[2]; | 
| 406 | > | res[0] += (double)cp1[0] * cp2[0]; | 
| 407 | > | res[1] += (double)cp1[1] * cp2[1]; | 
| 408 | > | res[2] += (double)cp1[2] * cp2[2]; | 
| 409 |  | } | 
| 410 |  | copycolor(dp, res); | 
| 411 |  | } | 
| 435 |  | if (sizeof(COLOR) == cm_elem_size[dtype]) { | 
| 436 |  | r = cm->ncols*cm->nrows; | 
| 437 |  | while (r > 0) { | 
| 438 | < | c = fwrite(mp, sizeof(COLOR), r, fp); | 
| 438 | > | c = putbinary(mp, sizeof(COLOR), r, fp); | 
| 439 |  | if (c <= 0) | 
| 440 |  | return(0); | 
| 441 |  | mp += 3*c; | 
| 446 |  | r = cm->ncols*cm->nrows; | 
| 447 |  | while (r--) { | 
| 448 |  | copycolor(dc, mp); | 
| 449 | < | if (fwrite(dc, sizeof(double), 3, fp) != 3) | 
| 449 | > | if (putbinary(dc, sizeof(double), 3, fp) != 3) | 
| 450 |  | return(0); | 
| 451 |  | mp += 3; | 
| 452 |  | } | 
| 455 |  | r = cm->ncols*cm->nrows; | 
| 456 |  | while (r--) { | 
| 457 |  | copycolor(fc, mp); | 
| 458 | < | if (fwrite(fc, sizeof(float), 3, fp) != 3) | 
| 458 | > | if (putbinary(fc, sizeof(float), 3, fp) != 3) | 
| 459 |  | return(0); | 
| 460 |  | mp += 3; | 
| 461 |  | } |