hextree: avoid memory corruption in create_palette_rek() by not skipping <3 pixel nodes and avoid potential divide by zero in assign_node_colors() (led to hang on osx) - closes #1087

This commit is contained in:
Dane Springmeyer 2012-02-21 22:37:56 -05:00
parent 03bfc015a6
commit 5dee576df2

View file

@ -394,8 +394,16 @@ private:
// clip extreme alfa values // clip extreme alfa values
void create_palette_rek(std::vector<rgba> & palette, node * itr) const void create_palette_rek(std::vector<rgba> & palette, node * itr) const
{ {
/*
NOTE: previous code did:
// actually, ignore ones with < 3 pixels // actually, ignore ones with < 3 pixels
if (itr->count >= 3) if (itr->count >= 3)
But this could lead to memory corruption
*/
if (itr->count > 0)
{ {
unsigned count = itr->count; unsigned count = itr->count;
byte a = byte(itr->alphas/float(count)); byte a = byte(itr->alphas/float(count));
@ -475,8 +483,10 @@ private:
} }
tries = 0; tries = 0;
// ignore leaves and also nodes with small mean error and not excessive number of pixels // ignore leaves and also nodes with small mean error and not excessive number of pixels
if (((cur_node->reduce_cost / cur_node->pixel_count + 1) * std::log(double(cur_node->pixel_count))) > 15 if (cur_node->pixel_count > 0 &&
&& (cur_node->children_count > 0)) (cur_node->children_count > 0) &&
(((cur_node->reduce_cost / cur_node->pixel_count + 1) * std::log(double(cur_node->pixel_count))) > 15)
)
{ {
colors_--; colors_--;
cur_node->count = 0; cur_node->count = 0;