Added code to cope with dithering in xrdp_bitmap_load tests

This commit is contained in:
matt335672 2021-07-22 15:16:11 +01:00
parent 8f8b4c5868
commit 19d49e3622

View File

@ -35,6 +35,15 @@ static int WHITE = COLOR24RGB(255, 255, 255);
#define TEST_BM_BOTTOM_LEFT_PIXEL BLUE #define TEST_BM_BOTTOM_LEFT_PIXEL BLUE
#define TEST_BM_BOTTOM_RIGHT_PIXEL WHITE #define TEST_BM_BOTTOM_RIGHT_PIXEL WHITE
/*
* Scaling bitmaps properly will introduce color changes with dithering.
* Also some filetypes use compression, and these do not represent colors
* perfectly.
*
* This is the Pythagorean distance we allow between two colors for them to
* be considered close enough to each other */
#define MAX_SIMILAR_COLOR_DISTANCE 3
void setup(void) void setup(void)
{ {
} }
@ -68,14 +77,42 @@ check_pixel(struct xrdp_bitmap *bm, int i, int j, int expected)
int pixel = xrdp_bitmap_get_pixel(bm, i, j); int pixel = xrdp_bitmap_get_pixel(bm, i, j);
if (pixel != expected) if (pixel != expected)
{ {
ck_abort_msg("Pixmap (%d,%d) expected %06x, got %06x", ck_abort_msg("Pixmap (%d,%d) expected 0x%06x, got 0x%06x",
i, j, expected, pixel); i, j, expected, pixel);
} }
} }
/* Check we can load bitmaps of various depths with various transforms */ /* Calculates whether two colors are close enough to be considered the same */
static void static void
load_and_transform_bm(int depth, check_is_close_color(struct xrdp_bitmap *bm, int i, int j, int expected)
{
int pixel = xrdp_bitmap_get_pixel(bm, i, j);
int r1;
int g1;
int b1;
int r2;
int g2;
int b2;
int variance;
SPLITCOLOR32(r1, g1, b1, pixel);
SPLITCOLOR32(r2, g2, b2, expected);
variance = ((r1 - r2) * (r1 - r2) +
(g1 - g2) * (g1 - g2) +
(b1 - b2) * (b1 - b2));
if (variance > MAX_SIMILAR_COLOR_DISTANCE * MAX_SIMILAR_COLOR_DISTANCE)
{
ck_abort_msg("Pixmap (%d,%d) expected 0x%06x, got 0x%06x"
" which exceeds distance of %d",
i, j, expected, pixel, MAX_SIMILAR_COLOR_DISTANCE);
}
}
/* Check we can load images of various depths with various transforms */
static void
load_and_transform_img(const char *name,
enum xrdp_bitmap_load_transform transform, enum xrdp_bitmap_load_transform transform,
int twidth, int theight) int twidth, int theight)
{ {
@ -85,18 +122,15 @@ load_and_transform_bm(int depth,
int width; int width;
int height; int height;
char name[256]; char full_name[256];
int top_left_pixel;
int top_right_pixel;
int bottom_left_pixel;
int bottom_right_pixel;
struct xrdp_bitmap *bm = xrdp_bitmap_create(4, 4, 32, WND_TYPE_IMAGE, wm); struct xrdp_bitmap *bm = xrdp_bitmap_create(4, 4, 32, WND_TYPE_IMAGE, wm);
ck_assert_ptr_ne(bm, NULL); ck_assert_ptr_ne(bm, NULL);
g_snprintf(name, sizeof(name), IMAGEDIR "/test_%dbit.bmp", depth); g_snprintf(full_name, sizeof(full_name), IMAGEDIR "/%s", name);
result = xrdp_bitmap_load(bm, name, NULL, 0, transform, twidth, theight); result = xrdp_bitmap_load(bm, full_name, NULL, HCOLOR(bm->bpp, WHITE),
transform, twidth, theight);
ck_assert_int_eq(result, 0); ck_assert_int_eq(result, 0);
@ -116,23 +150,18 @@ load_and_transform_bm(int depth,
ck_assert_int_eq(bm->height, theight); ck_assert_int_eq(bm->height, theight);
} }
/* Corners OK? */ /* Corners OK? Allow for dithering */
top_left_pixel = xrdp_bitmap_get_pixel(bm, 0, 0); check_is_close_color(bm, 0, 0, TEST_BM_TOP_LEFT_PIXEL);
top_right_pixel = xrdp_bitmap_get_pixel(bm, width - 1, 0); check_is_close_color(bm, width - 1, 0, TEST_BM_TOP_RIGHT_PIXEL);
bottom_left_pixel = xrdp_bitmap_get_pixel(bm, 0, height - 1); check_is_close_color(bm, 0, height - 1, TEST_BM_BOTTOM_LEFT_PIXEL);
bottom_right_pixel = xrdp_bitmap_get_pixel(bm, width - 1, height - 1); check_is_close_color(bm, width - 1, height - 1, TEST_BM_BOTTOM_RIGHT_PIXEL);
ck_assert_int_eq(top_left_pixel, TEST_BM_TOP_LEFT_PIXEL);
ck_assert_int_eq(top_right_pixel, TEST_BM_TOP_RIGHT_PIXEL);
ck_assert_int_eq(bottom_left_pixel, TEST_BM_BOTTOM_LEFT_PIXEL);
ck_assert_int_eq(bottom_right_pixel, TEST_BM_BOTTOM_RIGHT_PIXEL);
xrdp_bitmap_delete(bm); xrdp_bitmap_delete(bm);
} }
/* Check we can load bitmaps that aren't a multiple of 4 pixels wide */ /* Check we can load bitmaps that aren't a multiple of 4 pixels wide */
static void static void
load_not4_bm(int depth) load_not4_img(const char *name)
{ {
struct xrdp_wm *wm = NULL; struct xrdp_wm *wm = NULL;
int result; int result;
@ -140,7 +169,7 @@ load_not4_bm(int depth)
const int width = TEST_NOT4_BM_WIDTH; const int width = TEST_NOT4_BM_WIDTH;
const int height = TEST_NOT4_BM_HEIGHT; const int height = TEST_NOT4_BM_HEIGHT;
char name[256]; char full_name[256];
int i; int i;
int j; int j;
@ -148,8 +177,9 @@ load_not4_bm(int depth)
ck_assert_ptr_ne(bm, NULL); ck_assert_ptr_ne(bm, NULL);
g_snprintf(name, sizeof(name), IMAGEDIR "/test_not4_%dbit.bmp", depth); g_snprintf(full_name, sizeof(full_name), IMAGEDIR "/%s", name);
result = xrdp_bitmap_load(bm, name, NULL, 0, XBLT_NONE, 0, 0); result = xrdp_bitmap_load(bm, full_name, NULL, HCOLOR(bm->bpp, WHITE),
XBLT_NONE, 0, 0);
ck_assert_int_eq(result, 0); ck_assert_int_eq(result, 0);
@ -175,79 +205,91 @@ load_not4_bm(int depth)
START_TEST(test_bitmap_load__4_bit__ok) START_TEST(test_bitmap_load__4_bit__ok)
{ {
load_and_transform_bm(4, XBLT_NONE, 0, 0); load_and_transform_img("test_4bit.bmp", XBLT_NONE, 0, 0);
} }
END_TEST END_TEST
START_TEST(test_bitmap_load__8_bit__ok) START_TEST(test_bitmap_load__8_bit__ok)
{ {
load_and_transform_bm(8, XBLT_NONE, 0, 0); load_and_transform_img("test_8bit.bmp", XBLT_NONE, 0, 0);
} }
END_TEST END_TEST
START_TEST(test_bitmap_load__24_bit__ok) START_TEST(test_bitmap_load__24_bit__ok)
{ {
load_and_transform_bm(24, XBLT_NONE, 0, 0); load_and_transform_img("test_24bit.bmp", XBLT_NONE, 0, 0);
} }
END_TEST END_TEST
START_TEST(test_bitmap_load__max_width_zoom__ok) START_TEST(test_bitmap_load__max_width_zoom__ok)
{ {
load_and_transform_bm(24, load_and_transform_img("test_24bit.bmp",
XBLT_ZOOM, MAX_VDESKTOP_WIDTH, MIN_VDESKTOP_HEIGHT); XBLT_ZOOM, MAX_VDESKTOP_WIDTH, MIN_VDESKTOP_HEIGHT);
} }
END_TEST END_TEST
START_TEST(test_bitmap_load__max_height_zoom__ok) START_TEST(test_bitmap_load__max_height_zoom__ok)
{ {
load_and_transform_bm(24, load_and_transform_img("test_24bit.bmp",
XBLT_ZOOM, MIN_VDESKTOP_WIDTH, MAX_VDESKTOP_HEIGHT); XBLT_ZOOM, MIN_VDESKTOP_WIDTH, MAX_VDESKTOP_HEIGHT);
} }
END_TEST END_TEST
START_TEST(test_bitmap_load__min_zoom__ok) START_TEST(test_bitmap_load__min_zoom__ok)
{ {
load_and_transform_bm(24, load_and_transform_img("test_24bit.bmp",
XBLT_ZOOM, MIN_VDESKTOP_WIDTH, MIN_VDESKTOP_HEIGHT); XBLT_ZOOM, MIN_VDESKTOP_WIDTH, MIN_VDESKTOP_HEIGHT);
} }
END_TEST END_TEST
START_TEST(test_bitmap_load__max_width_scale__ok) START_TEST(test_bitmap_load__max_width_scale__ok)
{ {
load_and_transform_bm(24, load_and_transform_img("test_24bit.bmp",
XBLT_SCALE, MAX_VDESKTOP_WIDTH, MIN_VDESKTOP_HEIGHT); XBLT_SCALE, MAX_VDESKTOP_WIDTH, MIN_VDESKTOP_HEIGHT);
} }
END_TEST END_TEST
START_TEST(test_bitmap_load__max_height_scale__ok) START_TEST(test_bitmap_load__max_height_scale__ok)
{ {
load_and_transform_bm(24, load_and_transform_img("test_24bit.bmp",
XBLT_SCALE, MIN_VDESKTOP_WIDTH, MAX_VDESKTOP_HEIGHT); XBLT_SCALE, MIN_VDESKTOP_WIDTH, MAX_VDESKTOP_HEIGHT);
} }
END_TEST END_TEST
START_TEST(test_bitmap_load__min_scale__ok) START_TEST(test_bitmap_load__min_scale__ok)
{ {
load_and_transform_bm(24, load_and_transform_img("test_24bit.bmp",
XBLT_SCALE, MIN_VDESKTOP_WIDTH, MIN_VDESKTOP_HEIGHT); XBLT_SCALE, MIN_VDESKTOP_WIDTH, MIN_VDESKTOP_HEIGHT);
} }
END_TEST END_TEST
START_TEST(test_bitmap_load__not_4_pixels_wide_4_bit__ok) START_TEST(test_bitmap_load__not_4_pixels_wide_4_bit__ok)
{ {
load_not4_bm(4); load_not4_img("test_not4_4bit.bmp");
} }
END_TEST END_TEST
START_TEST(test_bitmap_load__not_4_pixels_wide_8_bit__ok) START_TEST(test_bitmap_load__not_4_pixels_wide_8_bit__ok)
{ {
load_not4_bm(8); load_not4_img("test_not4_8bit.bmp");
} }
END_TEST END_TEST
START_TEST(test_bitmap_load__not_4_pixels_wide_24_bit__ok) START_TEST(test_bitmap_load__not_4_pixels_wide_24_bit__ok)
{ {
load_not4_bm(24); load_not4_img("test_not4_24bit.bmp");
}
END_TEST
START_TEST(test_png_load__blend_ok)
{
load_and_transform_img("test_alpha_blend.png", XBLT_NONE, 0, 0);
}
END_TEST
START_TEST(test_jpg_load__ok)
{
load_and_transform_img("test1.jpg", XBLT_NONE, 0, 0);
} }
END_TEST END_TEST