1 /* pgm.h - header file for libpgm portable graymap library 2 */ 3 4 #ifndef _PGM_H_ 5 #define _PGM_H_ 6 7 #include <netpbm/pm.h> 8 #include <netpbm/pbm.h> 9 10 #ifdef __cplusplus 11 extern "C" { 12 #endif 13 #if 0 14 } /* to fake out automatic code indenters */ 15 #endif 16 17 /* The following definition has nothing to do with the format of a PGM file */ 18 typedef unsigned int gray; 19 20 /* Since April 2000, we are capable of reading and generating raw 21 (binary) PGM files with maxvals up to 65535. However, before that 22 the maximum (as usually implemented) was 255, and people still want 23 to generate files with a maxval of no more than 255 in most cases 24 (because then old Netpbm programs can process them, and they're 25 only half as big). 26 27 So we keep PGM_MAXMAXVAL = 255, even though it's kind of a misnomer. 28 29 Note that one could always write a file with maxval > PGM_MAXMAXVAL and 30 it would just go into plain (text) format instead of raw (binary) format. 31 Along with the expansion to 16 bit raw files, we took away that ability. 32 Unless you specify 'forceplain' on the pgm_writepgminit() call, it will 33 fail if you specify a maxval > PGM_OVERALLMAXVAL. I made this design 34 decision because I don't think anyone really wants to get a plain format 35 file with samples larger than 65535 in it. However, it should be possible 36 just to increase PGM_OVERALLMAXVAL and get that old function back for 37 maxvals that won't fit in 16 bits. I think the only thing really 38 constraining PGM_OVERALLMAXVAL is the size of the 'gray' data structure, 39 which is generally 32 bits. 40 */ 41 42 #define PGM_OVERALLMAXVAL 65535 43 #define PGM_MAXMAXVAL 255 44 45 #define pgm_unnormalize(value, maxval) \ 46 ((gray)((value + 1e-6) * (maxval) + 0.5)) 47 48 /* Magic constants. */ 49 50 #define PGM_MAGIC1 'P' 51 #define PGM_MAGIC2 '2' 52 #define RPGM_MAGIC2 '5' 53 #define PGM_FORMAT (PGM_MAGIC1 * 256 + PGM_MAGIC2) 54 #define RPGM_FORMAT (PGM_MAGIC1 * 256 + RPGM_MAGIC2) 55 #define PGM_TYPE PGM_FORMAT 56 57 /* For the alpha-mask variant of PGM: */ 58 #define PGM_TRANSPARENT 0 59 60 /* Macro for turning a format number into a type number. */ 61 62 #define PGM_FORMAT_TYPE(f) ((f) == PGM_FORMAT || (f) == RPGM_FORMAT ? PGM_TYPE : PBM_FORMAT_TYPE(f)) 63 64 65 /* Declarations of routines. */ 66 67 void 68 pgm_init(int * const argcP, 69 char ** const argv); 70 71 gray * 72 pgm_allocrow(unsigned int const cols); 73 74 #define pgm_freerow(grayrow) pm_freerow(grayrow) 75 76 #define pgm_allocarray( cols, rows ) \ 77 ((gray**) pm_allocarray( cols, rows, sizeof(gray) )) 78 #define pgm_freearray( grays, rows ) pm_freearray( (char**) grays, rows ) 79 80 gray ** 81 pgm_readpgm(FILE * const file, 82 int * const colsP, 83 int * const rowsP, 84 gray * const maxvalP); 85 86 void 87 pgm_readpgminit(FILE * const file, 88 int * const colsP, 89 int * const rowsP, 90 gray * const maxvalP, 91 int * const formatP); 92 93 void 94 pgm_readpgmrow(FILE * const file, 95 gray * const grayrow, 96 int const cols, 97 gray const maxval, 98 int const format); 99 100 void 101 pgm_writepgminit(FILE * const fileP, 102 int const cols, 103 int const rows, 104 gray const maxval, 105 int const forceplain); 106 107 void 108 pgm_writepgmrow(FILE * const fileP, 109 const gray * const grayrow, 110 int const cols, 111 gray const maxval, 112 int const forceplain); 113 114 void 115 pgm_writepgm(FILE * const fileP, 116 gray ** const grays, 117 int const cols, 118 int const rows, 119 gray const maxval, 120 int const forceplain); 121 122 void 123 pgm_nextimage(FILE * const file, int * const eofP); 124 125 void 126 pgm_check(FILE * const file, 127 enum pm_check_type const check_type, 128 int const format, 129 int const cols, 130 int const rows, 131 gray const maxval, 132 enum pm_check_code * const retval_p); 133 134 #ifdef __cplusplus 135 } 136 #endif 137 138 139 #endif /*_PGM_H_*/ 140