Fix chunked loading of images

This commit is contained in:
Kovid Goyal 2017-09-28 13:01:13 +05:30
parent 8cd1f76d2b
commit 35acb1497a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 13 additions and 2 deletions

View File

@ -260,14 +260,15 @@ add_trim_predicate(Image *img) {
static bool
handle_add_command(GraphicsManager *self, const GraphicsCommand *g, const uint8_t *payload) {
#define ABRT(code, ...) { set_add_response(#code, __VA_ARGS__); return false; }
#define ABRT(code, ...) { set_add_response(#code, __VA_ARGS__); self->loading_image = 0; return false; }
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 };
uint32_t fmt = g->format ? g->format : RGBA;
if (tt == 'd' && (g->more && self->loading_image)) init_img = false;
if (tt == 'd' && self->loading_image) init_img = false;
if (init_img) {
self->loading_image = 0;
size_t sz = g->data_width * g->data_height;
if (!sz) ABRT(EINVAL, "Zero width/height not allowed");
if (g->data_width > 10000 || g->data_height > 10000) ABRT(EINVAL, "Image too large");
@ -336,6 +337,7 @@ handle_add_command(GraphicsManager *self, const GraphicsCommand *g, const uint8_
ABRT(EINVAL, "Unknown transmission type: %c", g->transmission_type);
}
if (!img->data_loaded) return false;
self->loading_image = 0;
bool needs_processing = g->compressed || fmt == PNG;
if (needs_processing) {
uint8_t *buf; size_t bufsz;

View File

@ -55,7 +55,16 @@ class TestGraphics(BaseTest):
self.ae((kw['s'], kw['v']), (img['width'], img['height']))
return img
# Test simple load
for f in 32, 24:
p = 'abc' + ('d' if f == 32 else '')
img = sl(p, s=1, v=1, f=f)
self.ae(bool(img['is_4byte_aligned']), f == 32)
# Test chuunked load
self.assertIsNone(l('abcd', s=2, v=2, m=1))
self.assertIsNone(l('efgh', m=1))
self.assertIsNone(l('ijkl', m=1))
self.ae(l('mnop', m=0), 'OK')
img = g.image_for_client_id(1)
self.ae(img['data'], b'abcdefghijklmnop')