From 01522559d61ac26713e1400a4b4692cca9f60e73 Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Fri, 24 Jan 2020 23:16:08 +0000 Subject: [PATCH] zrle: Fix pixel conversion for bits-per-pixel < 32 --- src/zrle.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/zrle.c b/src/zrle.c index de7145d..30c623e 100644 --- a/src/zrle.c +++ b/src/zrle.c @@ -45,6 +45,12 @@ static inline int find_colour_in_palette(uint32_t* palette, int len, return -1; } +static inline int calc_bytes_per_cpixel(const struct rfb_pixel_format* fmt) +{ + return fmt->bits_per_pixel == 32 ? fmt->depth / 8 + : fmt->bits_per_pixel / 8; +} + int zrle_get_tile_palette(uint32_t* palette, const uint32_t* src, size_t length) { int n = 0; @@ -71,7 +77,7 @@ void zrle_encode_unichrome_tile(struct vec* dst, uint32_t colour, const struct rfb_pixel_format* src_fmt) { - int bytes_per_cpixel = dst_fmt->depth / 8; + int bytes_per_cpixel = calc_bytes_per_cpixel(dst_fmt); vec_fast_append_8(dst, 1); @@ -104,7 +110,7 @@ void zrle_encode_packed_tile(struct vec* dst, const struct rfb_pixel_format* src_fmt, size_t length, uint32_t* palette, int palette_size) { - int bytes_per_cpixel = dst_fmt->depth / 8; + int bytes_per_cpixel = calc_bytes_per_cpixel(dst_fmt); uint8_t cpalette[16 * 3]; pixel32_to_cpixel((uint8_t*)cpalette, dst_fmt, palette, src_fmt, @@ -146,7 +152,7 @@ void zrle_encode_tile(struct vec* dst, const struct rfb_pixel_format* dst_fmt, const uint32_t* src, const struct rfb_pixel_format* src_fmt, size_t length) { - int bytes_per_cpixel = dst_fmt->depth / 8; + int bytes_per_cpixel = calc_bytes_per_cpixel(dst_fmt); vec_clear(dst); @@ -203,7 +209,7 @@ int zrle_encode_box(struct vec* out, const struct rfb_pixel_format* dst_fmt, int stride, int width, int height, z_stream* zs) { int r = -1; - int bytes_per_cpixel = dst_fmt->depth / 8; + int bytes_per_cpixel = calc_bytes_per_cpixel(dst_fmt); struct vec in; uint32_t* tile = malloc(TILE_LENGTH * TILE_LENGTH * 4);