--- ray/src/common/header.c 2009/05/21 18:08:43 2.25 +++ ray/src/common/header.c 2022/03/06 16:33:44 2.45 @@ -1,10 +1,10 @@ #ifndef lint -static const char RCSid[] = "$Id: header.c,v 2.25 2009/05/21 18:08:43 greg Exp $"; +static const char RCSid[] = "$Id: header.c,v 2.45 2022/03/06 16:33:44 greg Exp $"; #endif /* * header.c - routines for reading and writing information headers. * - * Externals declared in resolu.h + * Externals declared in rtio.h * * newheader(t,fp) start new information header identified by string t * headidval(r,s) copy header identifier value in s to r @@ -15,6 +15,9 @@ static const char RCSid[] = "$Id: header.c,v 2.25 2009 * printargs(ac,av,fp) print an argument list to fp, followed by '\n' * formatval(r,s) copy the format value in s to r * fputformat(s,fp) write "FORMAT=%s" to fp + * nativebigendian() are we native on big-endian machine? + * isbigendian(s) header line matches "BigEndian=1" (-1 if irrelevant) + * fputendian(fp) write native endianness in BigEndian= line * getheader(fp,f,p) read header from fp, calling f(s,p) on each line * globmatch(pat, str) check for glob match of str against pat * checkheader(i,p,o) check header format from i against p and copy to o @@ -26,11 +29,15 @@ static const char RCSid[] = "$Id: header.c,v 2.25 2009 #include +#include "tiff.h" /* for int32 */ #include "rtio.h" +#include "color.h" #include "resolu.h" #define MAXLINE 2048 +extern time_t timegm(struct tm *tm); + const char HDRSTR[] = "#?"; /* information header magic number */ const char FMTSTR[] = "FORMAT="; /* format identifier */ @@ -38,25 +45,27 @@ const char FMTSTR[] = "FORMAT="; /* format identifier const char TMSTR[] = "CAPDATE="; /* capture date identifier */ const char GMTSTR[] = "GMT="; /* GMT identifier */ +const char BIGEND[] = "BigEndian="; /* big-endian variable */ + static gethfunc mycheck; -extern void +void newheader( /* identifying line of information header */ - char *s, + const char *s, FILE *fp ) { fputs(HDRSTR, fp); fputs(s, fp); - putc('\n', fp); + fputc('\n', fp); } -extern int +int headidval( /* get header id (return true if is id) */ char *r, - char *s + const char *s ) { const char *cp = HDRSTR; @@ -69,10 +78,10 @@ headidval( /* get header id (return true if is id) * } -extern int +int dateval( /* convert capture date line to UTC */ time_t *tloc, - char *s + const char *s ) { struct tm tms; @@ -95,10 +104,10 @@ dateval( /* convert capture date line to UTC */ } -extern int +int gmtval( /* convert GMT date line to UTC */ time_t *tloc, - char *s + const char *s ) { struct tm tms; @@ -120,7 +129,7 @@ gmtval( /* convert GMT date line to UTC */ } -extern void +void fputdate( /* write out the given time value (local & GMT) */ time_t tv, FILE *fp @@ -141,7 +150,7 @@ fputdate( /* write out the given time value (local & } -extern void +void fputnow( /* write out the current time */ FILE *fp ) @@ -152,13 +161,21 @@ fputnow( /* write out the current time */ } -extern void +void printargs( /* print arguments to a file */ int ac, char **av, FILE *fp ) { +#if defined(_WIN32) || defined(_WIN64) + extern char *fixargv0(char *arg0); + char myav0[128]; + /* clean up Windows executable path */ + if (ac-- <= 0) return; + fputs(fixargv0(strcpy(myav0, *av++)), fp); + fputc(ac ? ' ' : '\n', fp); +#endif while (ac-- > 0) { fputword(*av++, fp); fputc(ac ? ' ' : '\n', fp); @@ -166,13 +183,14 @@ printargs( /* print arguments to a file */ } -extern int +int formatval( /* get format value (return true if format) */ - char *r, - char *s + char fmt[MAXFMTLEN], + const char *s ) { const char *cp = FMTSTR; + char *r = fmt; while (*cp) if (*cp++ != *s++) return(0); while (isspace(*s)) s++; @@ -180,56 +198,130 @@ formatval( /* get format value (return true if forma if (r == NULL) return(1); do *r++ = *s++; - while(*s && !isspace(*s)); + while (*s && !isspace(*s) && r-fmt < MAXFMTLEN-1); *r = '\0'; return(1); } -extern void +void fputformat( /* put out a format value */ - char *s, + const char *s, FILE *fp ) { + int align = 0; + fputs(FMTSTR, fp); fputs(s, fp); - putc('\n', fp); + /* pad to align binary type for mmap() */ + if (globmatch(PICFMT, s)) + align = 0; /* not needed for picture data */ + else if (!strncmp("float", s, 5)) + align = sizeof(float); + else if (!strncmp("double", s, 6)) + align = sizeof(double); + else if (!strncmp("16-bit", s, 6)) + align = 2; + else if (!strncmp("32-bit", s, 6)) + align = 4; + else if (!strncmp("64-bit", s, 6)) + align = 8; + if (align) { + long pos = ftell(fp); + if (pos >= 0) { + pos = (pos + 2) % align; + if (pos) align -= pos; + else align = 0; + } else + align = 0; + } + while (align-- > 0) + putc(' ', fp); + fputc('\n', fp); } -extern int +int +nativebigendian() /* are we native on a big-endian machine? */ +{ + union { int32 i; char c[4]; } u; + + u.i = 1; + + return(u.c[0] == 0); +} + + +int +isbigendian( /* header line says "BigEndian=1" (-1 if irrelevant) */ + const char *s +) +{ + const char *be = BIGEND; + + while ((*s != '\0') & (*be != '=') && *s++ == *be) + ++be; + if (*be != '=') + return(-1); /* irrelevant */ + while (isspace(*s)) + s++; + if (*s++ != '=') + return(-1); + while (isspace(*s)) + s++; + return(*s == '1'); +} + + +void +fputendian( /* write native endianness in BigEndian= line */ + FILE *fp +) +{ + fputs(BIGEND, fp); + if (nativebigendian()) + fputs("1\n", fp); + else + fputs("0\n", fp); +} + + +int getheader( /* get header from file */ FILE *fp, gethfunc *f, void *p ) { + int rtotal = 0; char buf[MAXLINE]; + int firstc = fgetc(fp); + if (!isprint(firstc)) + return(-1); /* messed up */ + ungetc(firstc, fp); for ( ; ; ) { + int rval = 0; buf[MAXLINE-2] = '\n'; if (fgets(buf, MAXLINE, fp) == NULL) return(-1); - if (buf[0] == '\n') - return(0); -#ifdef MSDOS - if (buf[0] == '\r' && buf[1] == '\n') - return(0); -#endif + if (buf[buf[0]=='\r'] == '\n') /* end of header? */ + return(rtotal); if (buf[MAXLINE-2] != '\n') { ungetc(buf[MAXLINE-2], fp); /* prevent false end */ buf[MAXLINE-2] = '\0'; } - if (f != NULL && (*f)(buf, p) < 0) + if (f != NULL && (rval = (*f)(buf, p)) < 0) return(-1); + rtotal += rval; } } struct check { FILE *fp; - char fs[64]; + char fs[MAXFMTLEN]; }; @@ -239,18 +331,19 @@ mycheck( /* check a header line for format info. */ void *cp ) { - if (!formatval(((struct check*)cp)->fs, s) - && ((struct check*)cp)->fp != NULL) { - fputs(s, ((struct check*)cp)->fp); - } + struct check *scp = (struct check *)cp; + + if (!formatval(scp->fs, s) && scp->fp != NULL) + return(fputs(s, scp->fp)); + return(0); } -extern int +int globmatch( /* check for match of s against pattern p */ - char *p, - char *s + const char *p, + const char *s ) { int setmatch; @@ -264,7 +357,7 @@ globmatch( /* check for match of s against pattern p case '*': /* match any string */ while (p[1] == '*') p++; do - if ( (p[1]=='?' || p[1]==*s) && + if ( (p[1]=='?') | (p[1]==*s) && globmatch(p+1,s) ) return(1); while (*s++); @@ -277,11 +370,11 @@ globmatch( /* check for match of s against pattern p if (!*p) return(0); if (*p == '-') { - setmatch += p[-1] <= *s && *s <= p[1]; + setmatch += (p[-1] <= *s) & (*s <= p[1]); if (!*++p) break; } else - setmatch += *p == *s; + setmatch += (*p == *s); } if (!setmatch) return(0); @@ -315,10 +408,10 @@ globmatch( /* check for match of s against pattern p * if fout is not NULL. */ -extern int +int checkheader( FILE *fin, - char *fmt, + char fmt[MAXFMTLEN], FILE *fout ) {