--- ray/src/common/header.c 2019/07/19 17:37:56 2.35 +++ ray/src/common/header.c 2025/06/06 19:11:21 2.50 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: header.c,v 2.35 2019/07/19 17:37:56 greg Exp $"; +static const char RCSid[] = "$Id: header.c,v 2.50 2025/06/06 19:11:21 greg Exp $"; #endif /* * header.c - routines for reading and writing information headers. @@ -15,6 +15,9 @@ static const char RCSid[] = "$Id: header.c,v 2.35 2019 * 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,7 +29,10 @@ static const char RCSid[] = "$Id: header.c,v 2.35 2019 #include +#include "tiff.h" /* for int32 */ +#include "paths.h" /* for fixargv0() */ #include "rtio.h" +#include "color.h" #include "resolu.h" #define MAXLINE 2048 @@ -40,6 +46,8 @@ 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; @@ -51,7 +59,7 @@ newheader( /* identifying line of information header { fputs(HDRSTR, fp); fputs(s, fp); - putc('\n', fp); + fputc('\n', fp); } @@ -155,16 +163,26 @@ fputnow( /* write out the current time */ void -printargs( /* print arguments to a file */ +printargs( /* print command arguments to a file */ int ac, char **av, FILE *fp ) { - while (ac-- > 0) { - fputword(*av++, fp); - fputc(ac ? ' ' : '\n', fp); + if (ac <= 0) + return; + if (progname == NULL) + fixargv0(av[0]); /* sets global progname */ + + if (progname >= av[0] && progname - av[0] < strlen(av[0])) + fputword(progname, fp); + else + fputword(av[0], fp); + while (--ac > 0) { + fputc(' ', fp); + fputword(*++av, fp); } + fputc('\n', fp); } @@ -176,15 +194,20 @@ formatval( /* get format value (return true if forma { const char *cp = FMTSTR; char *r = fmt; - + /* check against format string */ while (*cp) if (*cp++ != *s++) return(0); while (isspace(*s)) s++; if (!*s) return(0); - if (r == NULL) return(1); - do + if (r == NULL) /* just checking if format? */ + return(1); + do /* copy format ID */ *r++ = *s++; - while (*s && !isspace(*s) && r-fmt < MAXFMTLEN-1); - *r = '\0'; + while (*s && r-fmt < MAXFMTLEN-1); + + do /* remove trailing white space */ + *r-- = '\0'; + while (r > fmt && isspace(*r)); + return(1); } @@ -195,13 +218,84 @@ fputformat( /* put out a format value */ 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); } 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, @@ -248,7 +342,7 @@ mycheck( /* check a header line for format info. */ struct check *scp = (struct check *)cp; if (!formatval(scp->fs, s) && scp->fp != NULL) - fputs(s, scp->fp); + return(fputs(s, scp->fp)); return(0); } @@ -277,14 +371,14 @@ globmatch( /* check for match of s against pattern p while (*s++); return(0); case '[': /* character set */ - setmatch = *s == *++p; + setmatch = (*s == *++p); if (!*p) return(0); while (*++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 @@ -295,7 +389,8 @@ globmatch( /* check for match of s against pattern p s++; break; case '\\': /* literal next */ - p++; + if (!*++p) + return(0); /* fall through */ default: /* normal character */ if (*p != *s)