00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 #ifndef _PNMRW_H_
00014 #define _PNMRW_H_
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 #if !defined(sco)  &&  !defined(sgi) && !defined(IRIX)
00024 extern void* malloc();
00025 #endif
00026 
00027 
00028 
00029 
00030 
00031 #if __STDC__
00032 #define ARGS(alist) alist
00033 #else 
00034 #define ARGS(alist) ()
00035 #define const
00036 #endif 
00037 
00038 
00039 
00040 
00041 typedef unsigned char bit;
00042 #define PBM_WHITE 0
00043 #define PBM_BLACK 1
00044 #define PBM_FORMAT_TYPE(f) ((f) == PBM_FORMAT || (f) == RPBM_FORMAT ? PBM_TYPE : -1)
00045 
00046 typedef unsigned char gray;
00047 #define PGM_MAXMAXVAL 255
00048 #define PGM_FORMAT_TYPE(f) ((f) == PGM_FORMAT || (f) == RPGM_FORMAT ? PGM_TYPE : PBM_FORMAT_TYPE(f))
00049 
00050 typedef gray pixval;
00051 #define PPM_MAXMAXVAL PGM_MAXMAXVAL
00052 typedef struct
00053     {
00054     pixval r, g, b;
00055     } pixel;
00056 #define PPM_GETR(p) ((p).r)
00057 #define PPM_GETG(p) ((p).g)
00058 #define PPM_GETB(p) ((p).b)
00059 #define PPM_ASSIGN(p,red,grn,blu) do { (p).r = (red); (p).g = (grn); (p).b = (blu); } while ( 0 )
00060 #define PPM_EQUAL(p,q) ( (p).r == (q).r && (p).g == (q).g && (p).b == (q).b )
00061 #define PPM_FORMAT_TYPE(f) ((f) == PPM_FORMAT || (f) == RPPM_FORMAT ? PPM_TYPE : PGM_FORMAT_TYPE(f))
00062 
00063 typedef pixel xel;
00064 typedef pixval xelval;
00065 #define PNM_MAXMAXVAL PPM_MAXMAXVAL
00066 #define PNM_GET1(x) PPM_GETB(x)
00067 #define PNM_ASSIGN1(x,v) PPM_ASSIGN(x,0,0,v)
00068 #define PNM_EQUAL(x,y) PPM_EQUAL(x,y)
00069 #define PNM_FORMAT_TYPE(f) PPM_FORMAT_TYPE(f)
00070 
00071 
00072 
00073 
00074 #define PBM_MAGIC1 'P'
00075 #define PBM_MAGIC2 '1'
00076 #define RPBM_MAGIC2 '4'
00077 #define PBM_FORMAT (PBM_MAGIC1 * 256 + PBM_MAGIC2)
00078 #define RPBM_FORMAT (PBM_MAGIC1 * 256 + RPBM_MAGIC2)
00079 #define PBM_TYPE PBM_FORMAT
00080 
00081 #define PGM_MAGIC1 'P'
00082 #define PGM_MAGIC2 '2'
00083 #define RPGM_MAGIC2 '5'
00084 #define PGM_FORMAT (PGM_MAGIC1 * 256 + PGM_MAGIC2)
00085 #define RPGM_FORMAT (PGM_MAGIC1 * 256 + RPGM_MAGIC2)
00086 #define PGM_TYPE PGM_FORMAT
00087 
00088 #define PPM_MAGIC1 'P'
00089 #define PPM_MAGIC2 '3'
00090 #define RPPM_MAGIC2 '6'
00091 #define PPM_FORMAT (PPM_MAGIC1 * 256 + PPM_MAGIC2)
00092 #define RPPM_FORMAT (PPM_MAGIC1 * 256 + RPPM_MAGIC2)
00093 #define PPM_TYPE PPM_FORMAT
00094 
00095 
00096 
00097 
00098 #define PPM_DEPTH(newp,p,oldmaxval,newmaxval) \
00099     PPM_ASSIGN( (newp), \
00100         ( (int) PPM_GETR(p) * (newmaxval) + (oldmaxval) / 2 ) / (oldmaxval), \
00101         ( (int) PPM_GETG(p) * (newmaxval) + (oldmaxval) / 2 ) / (oldmaxval), \
00102         ( (int) PPM_GETB(p) * (newmaxval) + (oldmaxval) / 2 ) / (oldmaxval) )
00103 
00104 
00105 
00106 
00107 #define PPM_LUMIN(p) ( 0.299 * PPM_GETR(p) + 0.587 * PPM_GETG(p) + 0.114 * PPM_GETB(p) )
00108 
00109 
00110 
00111 
00112 void pnm_init2 ARGS(( char* pn ));
00113 
00114 char** pm_allocarray ARGS(( int cols, int rows, int size ));
00115 #define pnm_allocarray( cols, rows ) ((xel**) pm_allocarray( cols, rows, sizeof(xel) ))
00116 char* pm_allocrow ARGS(( int cols, int size ));
00117 #define pnm_allocrow( cols ) ((xel*) pm_allocrow( cols, sizeof(xel) ))
00118 void pm_freearray ARGS(( char** its, int rows ));
00119 #define pnm_freearray( xels, rows ) pm_freearray( (char**) xels, rows )
00120 void pm_freerow ARGS(( char* itrow ));
00121 #define pnm_freerow( xelrow ) pm_freerow( (char*) xelrow )
00122 
00123 xel** pnm_readpnm ARGS(( FILE* file, int* colsP, int* rowsP, xelval* maxvalP, int* formatP ));
00124 int pnm_readpnminit ARGS(( FILE* file, int* colsP, int* rowsP, xelval* maxvalP, int* formatP ));
00125 int pnm_readpnmrow ARGS(( FILE* file, xel* xelrow, int cols, xelval maxval, int format ));
00126 
00127 int pnm_writepnm ARGS(( FILE* file, xel** xels, int cols, int rows, xelval maxval, int format, int forceplain ));
00128 int pnm_writepnminit ARGS(( FILE* file, int cols, int rows, xelval maxval, int format, int forceplain ));
00129 int pnm_writepnmrow ARGS(( FILE* file, xel* xelrow, int cols, xelval maxval, int format, int forceplain ));
00130 
00131 extern xelval pnm_pbmmaxval;
00132 
00133 
00134 
00135 
00136 
00137 
00138 
00139 FILE* pm_openr ARGS(( char* name ));
00140 FILE* pm_openw ARGS(( char* name ));
00141 int pm_closer ARGS(( FILE* f ));
00142 int pm_closew ARGS(( FILE* f ));
00143 
00144 
00145 
00146 
00147 typedef struct colorhist_item* colorhist_vector;
00148 struct colorhist_item
00149     {
00150     pixel color;
00151     int value;
00152     };
00153 
00154 typedef struct colorhist_list_item* colorhist_list;
00155 struct colorhist_list_item
00156     {
00157     struct colorhist_item ch;
00158     colorhist_list next;
00159     };
00160 
00161 typedef colorhist_list* colorhash_table;
00162 
00163 colorhist_vector ppm_computecolorhist ARGS(( pixel** pixels, int cols, int rows, int maxcolors, int* colorsP ));
00164 
00165 
00166 void ppm_addtocolorhist ARGS(( colorhist_vector chv, int* colorsP, int maxcolors, pixel* colorP, int value, int position ));
00167 
00168 void ppm_freecolorhist ARGS(( colorhist_vector chv ));
00169 
00170 colorhash_table ppm_computecolorhash ARGS(( pixel** pixels, int cols, int rows, int maxcolors, int* colorsP ));
00171 
00172 int
00173 ppm_lookupcolor ARGS(( colorhash_table cht, pixel* colorP ));
00174 
00175 colorhist_vector ppm_colorhashtocolorhist ARGS(( colorhash_table cht, int maxcolors ));
00176 colorhash_table ppm_colorhisttocolorhash ARGS(( colorhist_vector chv, int colors ));
00177 
00178 int ppm_addtocolorhash ARGS(( colorhash_table cht, pixel* colorP, int value ));
00179 
00180 
00181 colorhash_table ppm_alloccolorhash ARGS(( void ));
00182 
00183 void ppm_freecolorhash ARGS(( colorhash_table cht ));
00184 
00185 
00186 void pnm_promoteformat ARGS(( xel** xels, int cols, int rows, xelval maxval,
00187 int format, xelval newmaxval, int newformat ));
00188 void pnm_promoteformatrow ARGS(( xel* xelrow, int cols, xelval maxval, 
00189 int format, xelval newmaxval, int newformat ));
00190 
00191 xel pnm_backgroundxel ARGS(( xel** xels, int cols, int rows, xelval maxval, int format ));
00192 xel pnm_backgroundxelrow ARGS(( xel* xelrow, int cols, xelval maxval, int format ));
00193 xel pnm_whitexel ARGS(( xelval maxval, int format ));
00194 xel pnm_blackxel ARGS(( xelval maxval, int format ));
00195 void pnm_invertxel ARGS(( xel* xP, xelval maxval, int format ));
00196 
00197 #endif