From 2d66ae87f91c4fd25445a597be9b3863482fd94d Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Mon, 17 Aug 2015 11:02:18 -0700 Subject: [PATCH] GUAC-240: Improve PNG optimality approximation algorithm (count average run length). --- src/common/guac_surface.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/common/guac_surface.c b/src/common/guac_surface.c index 69c7fa18..d5b43ead 100644 --- a/src/common/guac_surface.c +++ b/src/common/guac_surface.c @@ -334,7 +334,8 @@ static int guac_common_surface_png_optimality(guac_common_surface* surface, int x, y; - int similarity = 0; + int num_same = 0; + int num_different = 1; /* Get image/buffer metrics */ int width = rect->width; @@ -356,12 +357,12 @@ static int guac_common_surface_png_optimality(guac_common_surface* surface, /* Get next pixel */ uint32_t current_pixel = *(row++) | 0xFF000000; - /* Update similarity according to whether pixel is identical */ + /* Update same/different counts according to pixel value */ if (x != 0) { if (current_pixel == last_pixel) - similarity++; + num_same++; else - similarity--; + num_different++; } last_pixel = current_pixel; @@ -374,7 +375,7 @@ static int guac_common_surface_png_optimality(guac_common_surface* surface, } /* Return rough approximation of optimality for PNG compression */ - return 0xFF * similarity / width / height; + return 0x100 * num_same / num_different - 0x400; }