| 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 |
|
|
| 183 |
|
{ |
| 184 |
|
const char *cp = FMTSTR; |
| 185 |
|
char *r = fmt; |
| 186 |
< |
|
| 186 |
> |
/* check against format string */ |
| 187 |
|
while (*cp) if (*cp++ != *s++) return(0); |
| 188 |
|
while (isspace(*s)) s++; |
| 189 |
|
if (!*s) return(0); |
| 190 |
< |
if (r == NULL) return(1); |
| 191 |
< |
do |
| 190 |
> |
if (r == NULL) /* just checking if format? */ |
| 191 |
> |
return(1); |
| 192 |
> |
do /* copy format ID */ |
| 193 |
|
*r++ = *s++; |
| 194 |
< |
while (*s && !isspace(*s) && r-fmt < MAXFMTLEN-1); |
| 195 |
< |
*r = '\0'; |
| 194 |
> |
while (*s && r-fmt < MAXFMTLEN-1); |
| 195 |
> |
|
| 196 |
> |
do /* remove trailing white space */ |
| 197 |
> |
*r-- = '\0'; |
| 198 |
> |
while (r > fmt && isspace(*r)); |
| 199 |
> |
|
| 200 |
|
return(1); |
| 201 |
|
} |
| 202 |
|
|
| 207 |
|
FILE *fp |
| 208 |
|
) |
| 209 |
|
{ |
| 210 |
+ |
int align = 0; |
| 211 |
+ |
|
| 212 |
|
fputs(FMTSTR, fp); |
| 213 |
|
fputs(s, fp); |
| 214 |
< |
putc('\n', fp); |
| 214 |
> |
/* pad to align binary type for mmap() */ |
| 215 |
> |
if (globmatch(PICFMT, s)) |
| 216 |
> |
align = 0; /* not needed for picture data */ |
| 217 |
> |
else if (!strncmp("float", s, 5)) |
| 218 |
> |
align = sizeof(float); |
| 219 |
> |
else if (!strncmp("double", s, 6)) |
| 220 |
> |
align = sizeof(double); |
| 221 |
> |
else if (!strncmp("16-bit", s, 6)) |
| 222 |
> |
align = 2; |
| 223 |
> |
else if (!strncmp("32-bit", s, 6)) |
| 224 |
> |
align = 4; |
| 225 |
> |
else if (!strncmp("64-bit", s, 6)) |
| 226 |
> |
align = 8; |
| 227 |
> |
if (align) { |
| 228 |
> |
long pos = ftell(fp); |
| 229 |
> |
if (pos >= 0) { |
| 230 |
> |
pos = (pos + 2) % align; |
| 231 |
> |
if (pos) align -= pos; |
| 232 |
> |
else align = 0; |
| 233 |
> |
} else |
| 234 |
> |
align = 0; |
| 235 |
> |
} |
| 236 |
> |
while (align-- > 0) |
| 237 |
> |
putc(' ', fp); |
| 238 |
> |
fputc('\n', fp); |
| 239 |
|
} |
| 240 |
|
|
| 241 |
|
|
| 242 |
|
int |
| 243 |
+ |
nativebigendian() /* are we native on a big-endian machine? */ |
| 244 |
+ |
{ |
| 245 |
+ |
union { int32 i; char c[4]; } u; |
| 246 |
+ |
|
| 247 |
+ |
u.i = 1; |
| 248 |
+ |
|
| 249 |
+ |
return(u.c[0] == 0); |
| 250 |
+ |
} |
| 251 |
+ |
|
| 252 |
+ |
|
| 253 |
+ |
int |
| 254 |
+ |
isbigendian( /* header line says "BigEndian=1" (-1 if irrelevant) */ |
| 255 |
+ |
const char *s |
| 256 |
+ |
) |
| 257 |
+ |
{ |
| 258 |
+ |
const char *be = BIGEND; |
| 259 |
+ |
|
| 260 |
+ |
while ((*s != '\0') & (*be != '=') && *s++ == *be) |
| 261 |
+ |
++be; |
| 262 |
+ |
if (*be != '=') |
| 263 |
+ |
return(-1); /* irrelevant */ |
| 264 |
+ |
while (isspace(*s)) |
| 265 |
+ |
s++; |
| 266 |
+ |
if (*s++ != '=') |
| 267 |
+ |
return(-1); |
| 268 |
+ |
while (isspace(*s)) |
| 269 |
+ |
s++; |
| 270 |
+ |
return(*s == '1'); |
| 271 |
+ |
} |
| 272 |
+ |
|
| 273 |
+ |
|
| 274 |
+ |
void |
| 275 |
+ |
fputendian( /* write native endianness in BigEndian= line */ |
| 276 |
+ |
FILE *fp |
| 277 |
+ |
) |
| 278 |
+ |
{ |
| 279 |
+ |
fputs(BIGEND, fp); |
| 280 |
+ |
if (nativebigendian()) |
| 281 |
+ |
fputs("1\n", fp); |
| 282 |
+ |
else |
| 283 |
+ |
fputs("0\n", fp); |
| 284 |
+ |
} |
| 285 |
+ |
|
| 286 |
+ |
|
| 287 |
+ |
int |
| 288 |
|
getheader( /* get header from file */ |
| 289 |
|
FILE *fp, |
| 290 |
|
gethfunc *f, |
| 293 |
|
{ |
| 294 |
|
int rtotal = 0; |
| 295 |
|
char buf[MAXLINE]; |
| 296 |
+ |
int firstc = fgetc(fp); |
| 297 |
|
|
| 298 |
+ |
if (!isprint(firstc)) |
| 299 |
+ |
return(-1); /* messed up */ |
| 300 |
+ |
ungetc(firstc, fp); |
| 301 |
|
for ( ; ; ) { |
| 302 |
|
int rval = 0; |
| 303 |
|
buf[MAXLINE-2] = '\n'; |
| 304 |
|
if (fgets(buf, MAXLINE, fp) == NULL) |
| 305 |
|
return(-1); |
| 306 |
< |
if (buf[buf[0]=='\r'] == '\n') |
| 306 |
> |
if (buf[buf[0]=='\r'] == '\n') /* end of header? */ |
| 307 |
|
return(rtotal); |
| 308 |
|
if (buf[MAXLINE-2] != '\n') { |
| 309 |
|
ungetc(buf[MAXLINE-2], fp); /* prevent false end */ |
| 331 |
|
struct check *scp = (struct check *)cp; |
| 332 |
|
|
| 333 |
|
if (!formatval(scp->fs, s) && scp->fp != NULL) |
| 334 |
< |
fputs(s, scp->fp); |
| 334 |
> |
return(fputs(s, scp->fp)); |
| 335 |
|
|
| 336 |
|
return(0); |
| 337 |
|
} |
| 360 |
|
while (*s++); |
| 361 |
|
return(0); |
| 362 |
|
case '[': /* character set */ |
| 363 |
< |
setmatch = *s == *++p; |
| 363 |
> |
setmatch = (*s == *++p); |
| 364 |
|
if (!*p) |
| 365 |
|
return(0); |
| 366 |
|
while (*++p != ']') { |
| 367 |
|
if (!*p) |
| 368 |
|
return(0); |
| 369 |
|
if (*p == '-') { |
| 370 |
< |
setmatch += (p[-1] <= *s && *s <= p[1]); |
| 370 |
> |
setmatch += (p[-1] <= *s) & (*s <= p[1]); |
| 371 |
|
if (!*++p) |
| 372 |
|
break; |
| 373 |
|
} else |
| 378 |
|
s++; |
| 379 |
|
break; |
| 380 |
|
case '\\': /* literal next */ |
| 381 |
< |
p++; |
| 381 |
> |
if (!*++p) |
| 382 |
> |
return(0); |
| 383 |
|
/* fall through */ |
| 384 |
|
default: /* normal character */ |
| 385 |
|
if (*p != *s) |