From 9facc5f7d32a88b43583c1c965e2ae601afbca2e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 27 Sep 2017 14:17:24 +0530 Subject: [PATCH] DRYer --- kitty/graphics.c | 18 ++++++++---------- kitty/graphics.h | 2 +- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/kitty/graphics.c b/kitty/graphics.c index 7564d83b8..a2af6ab21 100644 --- a/kitty/graphics.c +++ b/kitty/graphics.c @@ -137,6 +137,7 @@ handle_add_command(GraphicsManager *self, const GraphicsCommand *g, const uint8_ bool existing, init_img = true; Image *img; unsigned char tt = g->transmission_type ? g->transmission_type : 'd'; + enum FORMATS { RGB=24, RGBA=32, PNG=100 }; if (tt == 'd' && (g->more && self->loading_image)) init_img = false; if (init_img) { remove_images(self, add_trim_predicate); @@ -151,23 +152,20 @@ handle_add_command(GraphicsManager *self, const GraphicsCommand *g, const uint8_ img->width = g->data_width; img->height = g->data_height; size_t sz = img->width * img->height; switch(g->format) { - case 100: // PNG - sz = sz * 4 + 1024; + case PNG: + sz *= 4; break; - case 8: - case 24: - case 32: + case RGB: + case RGBA: sz *= g->format / 8; break; - default: break; } - if (g->compressed) sz += 1024; // compression header - img->load_data.max_data_sz = sz + 10; + img->load_data.data_sz = sz; if (tt == 'd') { if (g->more) self->loading_image = img->internal_id; - img->load_data.buf = malloc(img->load_data.max_data_sz + 4); + img->load_data.buf_capacity = sz + ((g->compressed || g->format == PNG) ? 1024 : 10); // compression header + img->load_data.buf = malloc(img->load_data.buf_capacity + 4); if (img->load_data.buf == NULL) fatal("Out of memory while allocating image load data buffer"); - img->load_data.buf_capacity = img->load_data.max_data_sz; img->load_data.buf_used = 0; } } else { diff --git a/kitty/graphics.h b/kitty/graphics.h index 05744e3d7..9ca408896 100644 --- a/kitty/graphics.h +++ b/kitty/graphics.h @@ -23,7 +23,7 @@ typedef struct { uint8_t *mapped_file; size_t mapped_file_sz; - size_t max_data_sz; + size_t data_sz; } LoadData; typedef struct {