diff options
-rw-r--r-- | src/draw/lv_draw_buf.c | 6 | ||||
-rw-r--r-- | src/draw/lv_draw_buf.h | 4 | ||||
-rw-r--r-- | src/widgets/canvas/lv_canvas.c | 34 | ||||
-rw-r--r-- | tests/ref_imgs/widgets/canvas_1.png | bin | 0 -> 2287 bytes | |||
-rw-r--r-- | tests/src/test_cases/widgets/test_canvas.c | 70 |
5 files changed, 99 insertions, 15 deletions
diff --git a/src/draw/lv_draw_buf.c b/src/draw/lv_draw_buf.c index 10e65b2b5..71058c767 100644 --- a/src/draw/lv_draw_buf.c +++ b/src/draw/lv_draw_buf.c @@ -379,7 +379,7 @@ void * lv_draw_buf_goto_xy(const lv_draw_buf_t * buf, uint32_t x, uint32_t y) if(x == 0) return data; - return data + x * lv_color_format_get_size(buf->header.cf); + return data + x * lv_color_format_get_bpp(buf->header.cf) / 8; } lv_result_t lv_draw_buf_adjust_stride(lv_draw_buf_t * src, uint32_t stride) @@ -529,8 +529,8 @@ void lv_draw_buf_set_palette(lv_draw_buf_t * draw_buf, uint8_t index, lv_color32 return; } - uint8_t * buf = (uint8_t *)draw_buf->data; - lv_memcpy(&buf[index * sizeof(color)], &color, sizeof(color)); + lv_color32_t * palette = (lv_color32_t *)draw_buf->data; + palette[index] = color; } /********************** diff --git a/src/draw/lv_draw_buf.h b/src/draw/lv_draw_buf.h index 4fd0ee7bb..05f54780a 100644 --- a/src/draw/lv_draw_buf.h +++ b/src/draw/lv_draw_buf.h @@ -47,7 +47,9 @@ typedef struct { LV_ROUND_UP(((w) * LV_COLOR_FORMAT_GET_BPP(cf) + 7) / 8, LV_DRAW_BUF_STRIDE_ALIGN) /* Allocate a slightly larger buffer, so we can adjust the start address to meet alignment */ -#define _LV_DRAW_BUF_SIZE(w, h, cf) (_LV_DRAW_BUF_STRIDE(w, cf) * (h) + LV_DRAW_BUF_ALIGN) +#define _LV_DRAW_BUF_SIZE(w, h, cf) \ + (_LV_DRAW_BUF_STRIDE(w, cf) * (h) + LV_DRAW_BUF_ALIGN + \ + LV_COLOR_INDEXED_PALETTE_SIZE(cf) * sizeof(lv_color32_t)) /** * Define a static draw buffer with the given width, height, and color format. diff --git a/src/widgets/canvas/lv_canvas.c b/src/widgets/canvas/lv_canvas.c index 877f6bfa9..836e81cbf 100644 --- a/src/widgets/canvas/lv_canvas.c +++ b/src/widgets/canvas/lv_canvas.c @@ -107,20 +107,32 @@ void lv_canvas_set_px(lv_obj_t * obj, int32_t x, int32_t y, lv_color_t color, lv lv_draw_buf_t * draw_buf = canvas->draw_buf; lv_color_format_t cf = draw_buf->header.cf; - uint32_t stride = draw_buf->header.stride; uint8_t * data = lv_draw_buf_goto_xy(draw_buf, x, y); if(LV_COLOR_FORMAT_IS_INDEXED(cf)) { - /*Indexed image bpp could be less than 8, calculate again*/ - uint8_t * buf = (uint8_t *)canvas->draw_buf->data; - buf += 8; - buf += y * stride; - buf += x >> 3; - uint32_t bit = 7 - (x & 0x7); - uint32_t c_int = color.blue; - - *buf &= ~(1 << bit); - *buf |= c_int << bit; + uint8_t shift; + uint8_t c_int = color.blue; + switch(cf) { + case LV_COLOR_FORMAT_I1: + shift = 7 - (x & 0x7); + break; + case LV_COLOR_FORMAT_I2: + shift = 6 - 2 * (x & 0x3); + break; + case LV_COLOR_FORMAT_I4: + shift = 4 - 4 * (x & 0x1); + break; + case LV_COLOR_FORMAT_I8: + /*Indexed8 format is a easy case, process and return.*/ + *data = c_int; + default: + return; + } + + uint8_t bpp = lv_color_format_get_bpp(cf); + uint8_t mask = (1 << bpp) - 1; + c_int &= mask; + *data = (*data & ~(mask << shift)) | (c_int << shift); } else if(cf == LV_COLOR_FORMAT_L8) { *data = lv_color_luminance(color); diff --git a/tests/ref_imgs/widgets/canvas_1.png b/tests/ref_imgs/widgets/canvas_1.png Binary files differnew file mode 100644 index 000000000..fdb1f503e --- /dev/null +++ b/tests/ref_imgs/widgets/canvas_1.png diff --git a/tests/src/test_cases/widgets/test_canvas.c b/tests/src/test_cases/widgets/test_canvas.c index 96a01e31f..0b3eb836e 100644 --- a/tests/src/test_cases/widgets/test_canvas.c +++ b/tests/src/test_cases/widgets/test_canvas.c @@ -61,4 +61,74 @@ void test_canvas_functions_invalidate(void) TEST_ASSERT(draw_counter == 4); } +void test_canvas_fill_and_set_px(void) +{ + lv_obj_t * canvas = lv_canvas_create(lv_screen_active()); + lv_obj_center(canvas); + + LV_DRAW_BUF_DEFINE_STATIC(buf_i1, 10, 10, LV_COLOR_FORMAT_I1); + LV_DRAW_BUF_INIT_STATIC(buf_i1); + lv_canvas_set_draw_buf(canvas, &buf_i1); + lv_canvas_set_palette(canvas, 0, lv_color32_make(0x00, 0xff, 0x00, 0xff)); + lv_canvas_set_palette(canvas, 1, lv_color32_make(0x00, 0x00, 0xff, 0xff)); + lv_canvas_fill_bg(canvas, lv_color_hex(0), LV_OPA_COVER); + lv_canvas_set_px(canvas, 1, 7, lv_color_hex(1), 0); + TEST_ASSERT_EQUAL_SCREENSHOT("widgets/canvas_1.png"); + + LV_DRAW_BUF_DEFINE_STATIC(buf_i2, 10, 10, LV_COLOR_FORMAT_I2); + LV_DRAW_BUF_INIT_STATIC(buf_i2); + lv_canvas_set_draw_buf(canvas, &buf_i2); + lv_canvas_set_palette(canvas, 0, lv_color32_make(0x00, 0xff, 0x00, 0xff)); + lv_canvas_set_palette(canvas, 3, lv_color32_make(0x00, 0x00, 0xff, 0xff)); + lv_canvas_fill_bg(canvas, lv_color_hex(0), LV_OPA_COVER); + lv_canvas_set_px(canvas, 1, 7, lv_color_hex(3), 0); + TEST_ASSERT_EQUAL_SCREENSHOT("widgets/canvas_1.png"); + + LV_DRAW_BUF_DEFINE_STATIC(buf_i4, 10, 10, LV_COLOR_FORMAT_I4); + LV_DRAW_BUF_INIT_STATIC(buf_i4); + lv_canvas_set_draw_buf(canvas, &buf_i4); + lv_canvas_set_palette(canvas, 0, lv_color32_make(0x00, 0xff, 0x00, 0xff)); + lv_canvas_set_palette(canvas, 15, lv_color32_make(0x00, 0x00, 0xff, 0xff)); + lv_canvas_fill_bg(canvas, lv_color_hex(0), LV_OPA_COVER); + lv_canvas_set_px(canvas, 1, 7, lv_color_hex(15), 0); + TEST_ASSERT_EQUAL_SCREENSHOT("widgets/canvas_1.png"); + + LV_DRAW_BUF_DEFINE_STATIC(buf_i8, 10, 10, LV_COLOR_FORMAT_I8); + LV_DRAW_BUF_INIT_STATIC(buf_i8); + lv_canvas_set_draw_buf(canvas, &buf_i8); + lv_canvas_set_palette(canvas, 0, lv_color32_make(0x00, 0xff, 0x00, 0xff)); + lv_canvas_set_palette(canvas, 255, lv_color32_make(0x00, 0x00, 0xff, 0xff)); + lv_canvas_fill_bg(canvas, lv_color_hex(0), LV_OPA_COVER); + lv_canvas_set_px(canvas, 1, 7, lv_color_hex(255), 0); + TEST_ASSERT_EQUAL_SCREENSHOT("widgets/canvas_1.png"); + + LV_DRAW_BUF_DEFINE_STATIC(buf_rgb888, 10, 10, LV_COLOR_FORMAT_RGB888); + LV_DRAW_BUF_INIT_STATIC(buf_rgb888); + lv_canvas_set_draw_buf(canvas, &buf_rgb888); + lv_canvas_fill_bg(canvas, lv_color_hex(0x00ff00), LV_OPA_COVER); + lv_canvas_set_px(canvas, 1, 7, lv_color_hex(0x0000ff), 0); + TEST_ASSERT_EQUAL_SCREENSHOT("widgets/canvas_1.png"); + + LV_DRAW_BUF_DEFINE_STATIC(buf_rgb565, 10, 10, LV_COLOR_FORMAT_RGB565); + LV_DRAW_BUF_INIT_STATIC(buf_rgb565); + lv_canvas_set_draw_buf(canvas, &buf_rgb565); + lv_canvas_fill_bg(canvas, lv_color_hex(0x00ff00), LV_OPA_COVER); + lv_canvas_set_px(canvas, 1, 7, lv_color_hex(0x0000ff), 0); + TEST_ASSERT_EQUAL_SCREENSHOT("widgets/canvas_1.png"); + + LV_DRAW_BUF_DEFINE_STATIC(buf_xrgb8888, 10, 10, LV_COLOR_FORMAT_XRGB8888); + LV_DRAW_BUF_INIT_STATIC(buf_xrgb8888); + lv_canvas_set_draw_buf(canvas, &buf_xrgb8888); + lv_canvas_fill_bg(canvas, lv_color_hex(0x00ff00), LV_OPA_COVER); + lv_canvas_set_px(canvas, 1, 7, lv_color_hex(0x0000ff), 0); + TEST_ASSERT_EQUAL_SCREENSHOT("widgets/canvas_1.png"); + + LV_DRAW_BUF_DEFINE_STATIC(buf_argb8888, 10, 10, LV_COLOR_FORMAT_ARGB8888); + LV_DRAW_BUF_INIT_STATIC(buf_argb8888); + lv_canvas_set_draw_buf(canvas, &buf_argb8888); + lv_canvas_fill_bg(canvas, lv_color_hex(0x00ff00), LV_OPA_COVER); + lv_canvas_set_px(canvas, 1, 7, lv_color_hex(0x0000ff), LV_OPA_COVER); + TEST_ASSERT_EQUAL_SCREENSHOT("widgets/canvas_1.png"); +} + #endif |