/* pbmplus.h - header file for PBM, PGM, PPM, and PNM ** ** Copyright (C) 1988, 1989, 1991 by Jef Poskanzer. ** ** Permission to use, copy, modify, and distribute this software and its ** documentation for any purpose and without fee is hereby granted, provided ** that the above copyright notice appear in all copies and that both that ** copyright notice and this permission notice appear in supporting ** documentation. This software is provided "as is" without express or ** implied warranty. */ #ifndef _PBMPLUS_H_ #define _PBMPLUS_H_ #include #include #include #include #include #include #if defined(USG) || defined(SVR4) #define SYSV #endif #ifdef __MSDOS__ #define MSDOS #endif #if ! ( defined(BSD) || defined(SYSV) || defined(MSDOS) ) /* CONFIGURE: If your system is >= 4.2BSD, set the BSD option; if you're a ** System V site, set the SYSV option; and if you're IBM-compatible, set ** MSDOS. If your compiler is ANSI C, you're probably better off setting ** SYSV - all it affects is string handling. */ #define BSD /* #define SYSV */ /* #define MSDOS */ #endif /* CONFIGURE: PGM can store gray values as either bytes or shorts. For most ** applications, bytes will be big enough, and the memory savings can be ** substantial. However, if you need more than 8 bits of grayscale resolution, ** then define this symbol. */ /* #define PGM_BIGGRAYS */ /* CONFIGURE: uncomment this to enable debugging checks. */ /* #define DEBUG */ #ifdef SYSV /* In case anyone still uses the old BSD-style string/memory routines. */ #define index(s,c) strchr(s,c) #define rindex(s,c) strrchr(s,c) #define bzero(dst,len) memset(dst,0,len) #define bcopy(src,dst,len) memcpy(dst,src,len) #define bcmp memcmp #endif /*SYSV*/ /* CONFIGURE: On some systems, malloc.h doesn't declare these, so we have ** to do it. On other systems, for example HP/UX, it declares them ** incompatibly. And some systems, for example Dynix, don't have a ** malloc.h at all. A sad situation. If you have compilation problems ** that point here, feel free to tweak or remove these declarations. */ #ifdef notdef #include extern void* malloc(); extern void* realloc(); extern void* calloc(); #endif /* CONFIGURE: Some systems don't have vfprintf(), which we need for the ** error-reporting routines. If you compile and get a link error about ** this routine, uncomment the first define, which gives you a vfprintf ** that uses the theoretically non-portable but fairly common routine ** _doprnt(). If you then get a link error about _doprnt, or ** message-printing doesn't look like it's working, try the second ** define instead. */ /* #define NEED_VFPRINTF1 */ /* #define NEED_VFPRINTF2 */ /* End of configurable definitions. */ #undef max #define max(a,b) ((a) > (b) ? (a) : (b)) #undef min #define min(a,b) ((a) < (b) ? (a) : (b)) #undef abs #define abs(a) ((a) >= 0 ? (a) : -(a)) #undef odd #define odd(n) ((n) & 1) /* Initialization. */ void pm_init( int* argcP, char* argv[] ); /* Variable-sized arrays definitions. */ char** pm_allocarray( int cols, int rows, int size ); char* pm_allocrow( int cols, int size ); void pm_freearray( char** its, int rows ); void pm_freerow( char* itrow ); /* Case-insensitive keyword matcher. */ int pm_keymatch( char* str, char* keyword, int minchars ); /* Log base two hacks. */ int pm_maxvaltobits( int maxval ); int pm_bitstomaxval( int bits ); /* Error handling definitions. */ void pm_message( char*, ... ); void pm_error( char*, ... ) __attribute__((noreturn)); void pm_perror( char* reason ) __attribute__((noreturn)); void pm_usage( char* usage ) __attribute__((noreturn)); /* File open/close that handles "-" as stdin and checks errors. */ FILE* pm_openr( char* name ); FILE* pm_openw( char* name ); void pm_closer( FILE* f ); void pm_closew( FILE* f ); /* Endian I/O. */ int pm_readbigshort( FILE* in, unsigned short* sP ); int pm_writebigshort( FILE* out, unsigned short s ); int pm_readbiglong( FILE* in, unsigned long* lP ); int pm_writebiglong( FILE* out, unsigned long l ); int pm_readlittleshort( FILE* in, unsigned short* sP ); int pm_writelittleshort( FILE* out, unsigned short s ); int pm_readlittlelong( FILE* in, unsigned long* lP ); int pm_writelittlelong( FILE* out, unsigned long l ); /* Convenience routine to read an entire file into a malloced area. */ char* pm_read_whole_file( FILE* in, long* sizeP ); /* Simple, portable, reasonably robust random number generator. */ void pm_srandom( unsigned long seed ); long pm_random( void ); /* Bitstream routines. */ typedef struct bitstream* BITSTREAM; /* pm_bitinit() - allocate and return a BITSTREAM for the given FILE*. ** ** mode must be one of "r" or "w", according to whether you will be ** reading from or writing to the BITSTREAM. ** ** Returns 0 on error. */ extern BITSTREAM pm_bitinit( FILE* f, char* mode ); /* pm_bitfini() - deallocate the given BITSTREAM. ** ** You must call this after you are done with the BITSTREAM. ** ** It may flush some bits left in the buffer. ** ** Returns the number of bytes written, -1 on error. */ extern int pm_bitfini( BITSTREAM b ); /* pm_bitread() - read the next nbits into *val from the given file. ** ** Returns the number of bytes read, -1 on error. */ extern int pm_bitread( BITSTREAM b, unsigned long nbits, unsigned long* val ); /* pm_bitwrite() - write the low nbits of val to the given file. ** ** The last pm_bitwrite() must be followed by a call to pm_bitflush(). ** ** Returns the number of bytes written, -1 on error. */ extern int pm_bitwrite( BITSTREAM b, unsigned long nbits, unsigned long val ); #endif /*_PBMPLUS_H_*/