As a followup as to why there is sometimes a discrepancy between the GD and IM output, it's because GD gets the actual palette, whearas the IM histogram method only gets the actual colors used. So you may have a palette with 17 colors, but only 15 may be used. I've also seen palettes with black(0,0,0) listed multiple times....that will screwup accurate color counts.