void test_render_to_saturating() { static unsigned char expected1[] = { 0, 0, 0, 0, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255, 0, 0, 0, 0, }; static unsigned char expected2[] = { 0, 0, 0, 0, 191, 191, 191, 255, 0, 0, 0, 0, 191, 191, 191, 255, 255, 255, 255, 255, 191, 191, 191, 255, 0, 0, 0, 0, 191, 191, 191, 255, 0, 0, 0, 0, }; heatmap_t* hm = heatmap_new(3, 3); heatmap_add_point_with_stamp(hm, 1, 1, &g_3x3_stamp); heatmap_add_point_with_stamp(hm, 1, 1, &g_3x3_stamp); heatmap_add_point_with_stamp(hm, 1, 1, &g_3x3_stamp); unsigned char img[3*3*4] = {1}; heatmap_render_saturated_to(hm, heatmap_cs_b2w, 1.0f, img); ENSURE_THAT("saturated (1) rendered 3x3 heatmap is correct", 0 == memcmp(img, expected1, 3*3*4)); heatmap_render_saturated_to(hm, heatmap_cs_b2w, 2.0f, img); ENSURE_THAT("saturated (2) rendered 3x3 heatmap is correct", 0 == memcmp(img, expected2, 3*3*4)); heatmap_free(hm); // TODO: (Also try negative and non-one-max stamps?) }
unsigned char* heatmap_render_to(const heatmap_t* h, const heatmap_colorscheme_t* colorscheme, unsigned char* colorbuf) { /* TODO: Time whether it makes a noticeable difference to inline that code * here and drop the saturation step. */ /* 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); }