| 4 |  | /* | 
| 5 |  | *  header.c - routines for reading and writing information headers. | 
| 6 |  | * | 
| 7 | < | *  Externals declared in resolu.h | 
| 7 | > | *  Externals declared in rtio.h | 
| 8 |  | * | 
| 9 |  | *  newheader(t,fp)     start new information header identified by string t | 
| 10 |  | *  headidval(r,s)      copy header identifier value in s to r | 
| 15 |  | *  printargs(ac,av,fp) print an argument list to fp, followed by '\n' | 
| 16 |  | *  formatval(r,s)      copy the format value in s to r | 
| 17 |  | *  fputformat(s,fp)    write "FORMAT=%s" to fp | 
| 18 | + | *  nativebigendian()   are we native on big-endian machine? | 
| 19 | + | *  isbigendian(s)      header line matches "BigEndian=1" (-1 if irrelevant) | 
| 20 | + | *  fputendian(fp)      write native endianness in BigEndian= line | 
| 21 |  | *  getheader(fp,f,p)   read header from fp, calling f(s,p) on each line | 
| 22 |  | *  globmatch(pat, str) check for glob match of str against pat | 
| 23 |  | *  checkheader(i,p,o)  check header format from i against p and copy to o | 
| 29 |  |  | 
| 30 |  | #include  <ctype.h> | 
| 31 |  |  | 
| 32 | + | #include  "tiff.h"      /* for int32 */ | 
| 33 |  | #include  "rtio.h" | 
| 34 | + | #include  "color.h" | 
| 35 |  | #include  "resolu.h" | 
| 36 |  |  | 
| 37 |  | #define  MAXLINE        2048 | 
| 45 |  | const char  TMSTR[] = "CAPDATE=";       /* capture date identifier */ | 
| 46 |  | const char  GMTSTR[] = "GMT=";          /* GMT identifier */ | 
| 47 |  |  | 
| 48 | + | const char  BIGEND[] = "BigEndian=";    /* big-endian variable */ | 
| 49 | + |  | 
| 50 |  | static gethfunc mycheck; | 
| 51 |  |  | 
| 52 |  |  | 
| 58 |  | { | 
| 59 |  | fputs(HDRSTR, fp); | 
| 60 |  | fputs(s, fp); | 
| 61 | < | putc('\n', fp); | 
| 61 | > | fputc('\n', fp); | 
| 62 |  | } | 
| 63 |  |  | 
| 64 |  |  | 
| 168 |  | FILE  *fp | 
| 169 |  | ) | 
| 170 |  | { | 
| 171 | + | #if defined(_WIN32) || defined(_WIN64) | 
| 172 | + | extern char     *fixargv0(char *arg0); | 
| 173 | + | char            myav0[128]; | 
| 174 | + | /* clean up Windows executable path */ | 
| 175 | + | if (ac-- <= 0) return; | 
| 176 | + | fputs(fixargv0(strcpy(myav0, *av++)), fp); | 
| 177 | + | fputc(ac ? ' ' : '\n', fp); | 
| 178 | + | #endif | 
| 179 |  | while (ac-- > 0) { | 
| 180 |  | fputword(*av++, fp); | 
| 181 |  | fputc(ac ? ' ' : '\n', fp); | 
| 210 |  | FILE  *fp | 
| 211 |  | ) | 
| 212 |  | { | 
| 213 | + | int     align = 0; | 
| 214 | + |  | 
| 215 |  | fputs(FMTSTR, fp); | 
| 216 |  | fputs(s, fp); | 
| 217 | < | putc('\n', fp); | 
| 217 | > | /* pad to align binary type for mmap() */ | 
| 218 | > | if (globmatch(PICFMT, s)) | 
| 219 | > | align = 0;      /* not needed for picture data */ | 
| 220 | > | else if (!strncmp("float", s, 5)) | 
| 221 | > | align = sizeof(float); | 
| 222 | > | else if (!strncmp("double", s, 6)) | 
| 223 | > | align = sizeof(double); | 
| 224 | > | else if (!strncmp("16-bit", s, 6)) | 
| 225 | > | align = 2; | 
| 226 | > | else if (!strncmp("32-bit", s, 6)) | 
| 227 | > | align = 4; | 
| 228 | > | else if (!strncmp("64-bit", s, 6)) | 
| 229 | > | align = 8; | 
| 230 | > | if (align) { | 
| 231 | > | long    pos = ftell(fp); | 
| 232 | > | if (pos >= 0) { | 
| 233 | > | pos = (pos + 2) % align; | 
| 234 | > | if (pos) align -= pos; | 
| 235 | > | else align = 0; | 
| 236 | > | } else | 
| 237 | > | align = 0; | 
| 238 | > | } | 
| 239 | > | while (align-- > 0) | 
| 240 | > | putc(' ', fp); | 
| 241 | > | fputc('\n', fp); | 
| 242 |  | } | 
| 243 |  |  | 
| 244 |  |  | 
| 245 |  | int | 
| 246 | + | nativebigendian()       /* are we native on a big-endian machine? */ | 
| 247 | + | { | 
| 248 | + | union { int32 i; char c[4]; } u; | 
| 249 | + |  | 
| 250 | + | u.i = 1; | 
| 251 | + |  | 
| 252 | + | return(u.c[0] == 0); | 
| 253 | + | } | 
| 254 | + |  | 
| 255 | + |  | 
| 256 | + | int | 
| 257 | + | isbigendian(            /* header line says "BigEndian=1" (-1 if irrelevant) */ | 
| 258 | + | const char *s | 
| 259 | + | ) | 
| 260 | + | { | 
| 261 | + | const char      *be = BIGEND; | 
| 262 | + |  | 
| 263 | + | while ((*s != '\0') & (*be != '=') && *s++ == *be) | 
| 264 | + | ++be; | 
| 265 | + | if (*be != '=') | 
| 266 | + | return(-1);     /* irrelevant */ | 
| 267 | + | while (isspace(*s)) | 
| 268 | + | s++; | 
| 269 | + | if (*s++ != '=') | 
| 270 | + | return(-1); | 
| 271 | + | while (isspace(*s)) | 
| 272 | + | s++; | 
| 273 | + | return(*s == '1'); | 
| 274 | + | } | 
| 275 | + |  | 
| 276 | + |  | 
| 277 | + | void | 
| 278 | + | fputendian(             /* write native endianness in BigEndian= line */ | 
| 279 | + | FILE *fp | 
| 280 | + | ) | 
| 281 | + | { | 
| 282 | + | fputs(BIGEND, fp); | 
| 283 | + | if (nativebigendian()) | 
| 284 | + | fputs("1\n", fp); | 
| 285 | + | else | 
| 286 | + | fputs("0\n", fp); | 
| 287 | + | } | 
| 288 | + |  | 
| 289 | + |  | 
| 290 | + | int | 
| 291 |  | getheader(              /* get header from file */ | 
| 292 |  | FILE  *fp, | 
| 293 |  | gethfunc *f, | 
| 296 |  | { | 
| 297 |  | int   rtotal = 0; | 
| 298 |  | char  buf[MAXLINE]; | 
| 299 | + | int   firstc = fgetc(fp); | 
| 300 |  |  | 
| 301 | + | if (!isprint(firstc)) | 
| 302 | + | return(-1);                             /* messed up */ | 
| 303 | + | ungetc(firstc, fp); | 
| 304 |  | for ( ; ; ) { | 
| 305 |  | int     rval = 0; | 
| 306 |  | buf[MAXLINE-2] = '\n'; | 
| 307 |  | if (fgets(buf, MAXLINE, fp) == NULL) | 
| 308 |  | return(-1); | 
| 309 | < | if (buf[buf[0]=='\r'] == '\n') | 
| 309 | > | if (buf[buf[0]=='\r'] == '\n')          /* end of header? */ | 
| 310 |  | return(rtotal); | 
| 311 |  | if (buf[MAXLINE-2] != '\n') { | 
| 312 |  | ungetc(buf[MAXLINE-2], fp);     /* prevent false end */ | 
| 334 |  | struct check    *scp = (struct check *)cp; | 
| 335 |  |  | 
| 336 |  | if (!formatval(scp->fs, s) && scp->fp != NULL) | 
| 337 | < | fputs(s, scp->fp); | 
| 337 | > | return(fputs(s, scp->fp)); | 
| 338 |  |  | 
| 339 |  | return(0); | 
| 340 |  | } |