zrle: Copy each tile region to a contiguous buffer before operating on it

tight-png
Andri Yngvason 2019-09-05 22:53:20 +00:00 committed by Andri Yngvason
parent 80e1093e71
commit 3e374c97f0
1 changed files with 45 additions and 36 deletions

View File

@ -156,17 +156,15 @@ static inline int find_colour_in_palette(uint32_t *palette, int len,
return -1; return -1;
} }
int zrle_get_tile_palette(uint32_t *palette, const uint32_t *src, int stride, int zrle_get_tile_palette(uint32_t *palette, const uint32_t *src, size_t length)
int width, int height)
{ {
int n = 0; int n = 0;
/* TODO: Maybe ignore the alpha channel */ /* TODO: Maybe ignore the alpha channel */
palette[n++] = src[0]; palette[n++] = src[0];
for (int y = 0; y < height; ++y) for (size_t i = 0; i < length; ++i) {
for (int x = 0; x < width; ++x) { uint32_t colour = src[i];
uint32_t colour = src[x + y * stride];
if (find_colour_in_palette(palette, n, colour) < 0) { if (find_colour_in_palette(palette, n, colour) < 0) {
if (n >= 16) if (n >= 16)
@ -215,7 +213,7 @@ void zrle_encode_packed_tile(struct vec *dst,
const struct rfb_pixel_format *dst_fmt, const struct rfb_pixel_format *dst_fmt,
const uint32_t *src, const uint32_t *src,
const struct rfb_pixel_format *src_fmt, const struct rfb_pixel_format *src_fmt,
int stride, int width, int height, size_t length,
uint32_t *palette, int palette_size) uint32_t *palette, int palette_size)
{ {
int bytes_per_cpixel = dst_fmt->depth / 8; int bytes_per_cpixel = dst_fmt->depth / 8;
@ -232,9 +230,8 @@ void zrle_encode_packed_tile(struct vec *dst,
int run_length = -1; int run_length = -1;
uint32_t old_colour = src[0]; uint32_t old_colour = src[0];
for (int y = 0; y < height; ++y) for (size_t i = 0; i < length; ++i) {
for (int x = 0; x < width; ++x) { uint32_t colour = src[i];
uint32_t colour = src[x + y * stride];
run_length++; run_length++;
if (colour == old_colour) if (colour == old_colour)
@ -256,18 +253,24 @@ void zrle_encode_packed_tile(struct vec *dst,
} }
} }
void zrle_copy_tile(uint32_t *dst, const uint32_t *src, int stride,
int width, int height)
{
for (int y = 0; y < height; ++y)
memcpy(dst + y * width, src + y * stride, width * 4);
}
void zrle_encode_tile(struct vec *dst, const struct rfb_pixel_format *dst_fmt, void zrle_encode_tile(struct vec *dst, const struct rfb_pixel_format *dst_fmt,
const uint32_t *src, const uint32_t *src,
const struct rfb_pixel_format *src_fmt, const struct rfb_pixel_format *src_fmt,
int stride, int width, int height) size_t length)
{ {
int bytes_per_cpixel = dst_fmt->depth / 8; int bytes_per_cpixel = dst_fmt->depth / 8;
vec_clear(dst); vec_clear(dst);
uint32_t palette[16]; uint32_t palette[16];
int palette_size = zrle_get_tile_palette(palette, src, stride, int palette_size = zrle_get_tile_palette(palette, src, length);
width, height);
if (palette_size == 1) { if (palette_size == 1) {
zrle_encode_unichrome_tile(dst, dst_fmt, palette[0], src_fmt); zrle_encode_unichrome_tile(dst, dst_fmt, palette[0], src_fmt);
@ -275,19 +278,17 @@ void zrle_encode_tile(struct vec *dst, const struct rfb_pixel_format *dst_fmt,
} }
if (palette_size > 1) { if (palette_size > 1) {
zrle_encode_packed_tile(dst, dst_fmt, src, src_fmt, stride, zrle_encode_packed_tile(dst, dst_fmt, src, src_fmt, length,
width, height, palette, palette_size); palette, palette_size);
return; return;
} }
vec_fast_append_8(dst, 0); vec_fast_append_8(dst, 0);
for (int y = 0; y < height; ++y) pixel32_to_cpixel(((uint8_t*)dst->data) + 1, dst_fmt, src, src_fmt,
pixel32_to_cpixel(((uint8_t*)dst->data) + 1 + width * y * bytes_per_cpixel, bytes_per_cpixel, length);
dst_fmt, src + stride * y,
src_fmt, bytes_per_cpixel, width);
dst->len += bytes_per_cpixel * width * height; dst->len += bytes_per_cpixel * length;
} }
int zrle_deflate(struct vec* dst, const struct vec* src, z_stream* zs, int zrle_deflate(struct vec* dst, const struct vec* src, z_stream* zs,
@ -326,6 +327,10 @@ int zrle_encode_box(struct vec* out, const struct rfb_pixel_format *dst_fmt,
int chunk_size = 1 + bytes_per_cpixel * 64 * 64; int chunk_size = 1 + bytes_per_cpixel * 64 * 64;
struct vec in; struct vec in;
uint32_t *tile = malloc(64 * 64 * 4);
if (!tile)
goto failure;
if (vec_init(&in, 1 + bytes_per_cpixel * 64 * 64) < 0) if (vec_init(&in, 1 + bytes_per_cpixel * 64 * 64) < 0)
goto failure; goto failure;
@ -354,9 +359,12 @@ int zrle_encode_box(struct vec* out, const struct rfb_pixel_format *dst_fmt,
int tile_width = width - tile_x >= 64 ? 64 : width - tile_x; int tile_width = width - tile_x >= 64 ? 64 : width - tile_x;
int tile_height = height - tile_y >= 64 ? 64 : height - tile_y; int tile_height = height - tile_y >= 64 ? 64 : height - tile_y;
zrle_encode_tile(&in, dst_fmt, zrle_copy_tile(tile,
((uint32_t*)src) + x + tile_x + (y + tile_y) * stride, ((uint32_t*)src) + x + tile_x + (y + tile_y) * stride,
src_fmt, stride, tile_width, tile_height); stride, tile_width, tile_height);
zrle_encode_tile(&in, dst_fmt, tile, src_fmt,
tile_width * tile_height);
r = zrle_deflate(out, &in, zs, i == n_tiles - 1); r = zrle_deflate(out, &in, zs, i == n_tiles - 1);
if (r < 0) if (r < 0)
@ -368,6 +376,7 @@ int zrle_encode_box(struct vec* out, const struct rfb_pixel_format *dst_fmt,
failure: failure:
vec_destroy(&in); vec_destroy(&in);
free(tile);
return r; return r;
#undef CHUNK #undef CHUNK
} }