17 #define HASH_SIZE 20023 20 #define ppm_hashpixel(p) ( ( (int) (p) & 0x7fffffff ) % HASH_SIZE ) 22 #define ppm_hashpixel(p) ( ( ( (long) PPM_GETR(p) * 33023 + (long) PPM_GETG(p) * 30013 + (long) PPM_GETB(p) * 27011 ) & 0x7fffffff ) % HASH_SIZE ) 26 ppm_computecolorhist(
pixel** pixels,
27 int cols,
int rows,
int maxcolors,
33 cht = ppm_computecolorhash( pixels, cols, rows, maxcolors, colorsP );
36 chv = ppm_colorhashtocolorhist( cht, maxcolors );
37 ppm_freecolorhash( cht );
46 int value,
int position)
51 for ( i = 0; i < *colorsP; ++i )
52 if ( PPM_EQUAL( chv[i].color, *colorP ) )
57 for ( j = i; j < position; ++j )
60 else if ( position < i )
62 for ( j = i; j > position; --j )
65 chv[position].color = *colorP;
66 chv[position].value = value;
69 if ( *colorsP < maxcolors )
72 for ( i = *colorsP; i > position; --i )
74 chv[position].color = *colorP;
75 chv[position].value = value;
81 ppm_computecolorhash(
pixel** pixels,
82 int cols,
int rows,
int maxcolors,
90 cht = ppm_alloccolorhash( );
94 for ( row = 0; row < rows; ++row )
95 for ( col = 0, pP = pixels[row]; col < cols; ++col, ++pP )
97 hash = ppm_hashpixel( *pP );
98 for ( chl = cht[hash]; chl !=
nullptr; chl = chl->next )
99 if ( PPM_EQUAL( chl->ch.color, *pP ) )
101 if ( chl !=
nullptr )
105 if ( ++(*colorsP) > maxcolors )
107 ppm_freecolorhash( cht );
111 if ( chl ==
nullptr )
112 pm_error(
"out of memory computing hash table" );
115 chl->next = cht[hash];
124 ppm_alloccolorhash( )
130 if ( cht ==
nullptr )
131 pm_error(
"out of memory allocating hash table" );
133 for ( i = 0; i < HASH_SIZE; ++i )
148 if ( chl ==
nullptr )
150 hash = ppm_hashpixel( *colorP );
151 chl->ch.color = *colorP;
152 chl->ch.value = value;
153 chl->next = cht[hash];
169 if ( chv ==
nullptr )
170 pm_error(
"out of memory generating histogram" );
174 for ( i = 0; i < HASH_SIZE; ++i )
175 for ( chl = cht[i]; chl !=
nullptr; chl = chl->next )
195 cht = ppm_alloccolorhash( );
197 for ( i = 0; i < colors; ++i )
199 color = chv[i].color;
200 hash = ppm_hashpixel( color );
201 for ( chl = cht[hash]; chl !=
nullptr; chl = chl->next )
202 if ( PPM_EQUAL( chl->ch.color, color ) )
204 "same color found twice - %d %d %d", PPM_GETR(color),
205 PPM_GETG(color), PPM_GETB(color) );
207 if ( chl ==
nullptr )
209 chl->ch.color = color;
211 chl->next = cht[hash];
225 hash = ppm_hashpixel( *colorP );
226 for ( chl = cht[hash]; chl !=
nullptr; chl = chl->next )
227 if ( PPM_EQUAL( chl->ch.color, *colorP ) )
228 return chl->ch.value;
245 for ( i = 0; i < HASH_SIZE; ++i )
246 for ( chl = cht[i]; chl !=
nullptr; chl = chlnext )
void pm_error(const char *format,...)
Outputs the given printf-style message to the user and terminates messily.