Skip to content

Commit

Permalink
Fixed a bug when no single point is added.
Browse files Browse the repository at this point in the history
Thanks @Grabber.
  • Loading branch information
lucasb-eyer committed Jan 3, 2014
1 parent 2649bdb commit b77a840
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
16 changes: 10 additions & 6 deletions heatmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,17 @@ unsigned char* heatmap_render_to(const heatmap_t* h, const heatmap_colorscheme_t
/* TODO: Time whether it makes a noticeable difference to inline that code
* here and drop the saturation step.
*/
return heatmap_render_saturated_to(h, colorscheme, h->max, colorbuf);
/* If the heatmap is empty, h->max (and thus the saturation value) is 0.0, resulting in a 0-by-0 division.
* In that case, we should set the saturation to anything but 0, since we want the result of the division to be 0.
* Also, a comparison to exact 0.0f (as opposed to 1e-14) is OK, since we only do division.
*/
return heatmap_render_saturated_to(h, colorscheme, h->max > 0.0f ? h->max : 1.0f, colorbuf);
}

unsigned char* heatmap_render_saturated_to(const heatmap_t* h, const heatmap_colorscheme_t* colorscheme, float saturation, unsigned char* colorbuf)
{
unsigned y;
assert(saturation >= 0.0f);
assert(saturation > 0.0f);

/* For convenience, if no buffer is given, malloc a new one. */
if(!colorbuf) {
Expand All @@ -140,7 +144,7 @@ unsigned char* heatmap_render_saturated_to(const heatmap_t* h, const heatmap_col
}

/* TODO: could actually even flatten this loop before parallelizing it. */
/* WTH DID I MEAN WITH THAT? */
/* I.e., to go i = 0 ; i < h*w since I don't have any padding! (yet?) */
for(y = 0 ; y < h->h ; ++y) {
float* bufline = h->buf + y*h->w;
unsigned char* colorline = colorbuf + 4*y*h->w;
Expand All @@ -152,15 +156,15 @@ unsigned char* heatmap_render_saturated_to(const heatmap_t* h, const heatmap_col
*/
const float val = (*bufline > saturation ? saturation : *bufline)/saturation;

/* This is probably caused by a negative entry in the stamp! */
assert(val >= 0.0f);

/* We add 0.5 in order to do real rounding, not just dropping the
* decimal part. That way we are certain the highest value in the
* colorscheme is actually used.
*/
const size_t idx = (size_t)((float)(colorscheme->ncolors-1)*val + 0.5f);

/* This is probably caused by a negative entry in the stamp! */
assert(val >= 0.0f);

/* This should never happen. It is likely a bug in this library. */
assert(idx < colorscheme->ncolors);

Expand Down
36 changes: 36 additions & 0 deletions tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ static bool stamp_almost_eq(heatmap_stamp_t* s, float* expected)
return almost_eq(s->buf, expected, s->w*s->h);
}

void test_add_nothing()
{
heatmap_t* hm = heatmap_new(3, 3);

static float expected[] = {
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
};


ENSURE_THAT("the heatmap is full of zeros", heatmap_eq(hm, expected));
ENSURE_THAT("the max of the heatmap is zero", hm->max == 0.0f);

heatmap_free(hm);
}

void test_add_point_with_stamp_center()
{
heatmap_t* hm = heatmap_new(3, 3);
Expand Down Expand Up @@ -184,6 +201,23 @@ void test_stamp_gen_nonlinear()
heatmap_stamp_free(s3);
}

void test_render_to_nothing()
{
static unsigned char expected[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};

heatmap_t* hm = heatmap_new(3, 3);

unsigned char img[3*3*4] = {1};
heatmap_render_to(hm, heatmap_cs_b2w, img);
ENSURE_THAT("empty rendered 3x3 heatmap is correct", 0 == memcmp(img, expected, 3*3*4));

heatmap_free(hm);
}

void test_render_to_creation()
{
static unsigned char expected[] = {
Expand Down Expand Up @@ -260,6 +294,7 @@ void test_render_to_saturating()

int main()
{
test_add_nothing();
test_add_point_with_stamp_center();
test_add_point_with_stamp_topleft();
test_add_point_with_stamp_botright();
Expand All @@ -268,6 +303,7 @@ int main()
test_stamp_gen();
test_stamp_gen_nonlinear();

test_render_to_nothing();
test_render_to_creation();
test_render_to_normalizing();
test_render_to_saturating();
Expand Down

0 comments on commit b77a840

Please sign in to comment.