From e29210889a38db46d6186fc64a2aa7eec105253d Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Fri, 8 Nov 2019 22:43:58 +0100 Subject: [PATCH] add image zoom --- src/lv_draw/lv_draw_img.c | 71 ++++++--- src/lv_draw/lv_draw_img.h | 3 +- src/lv_draw/lv_img_buf.c | 298 +++++++++++--------------------------- src/lv_draw/lv_img_buf.h | 84 +++++------ src/lv_objx/lv_canvas.c | 46 +++--- src/lv_objx/lv_canvas.h | 8 +- src/lv_objx/lv_img.c | 74 +++++++++- src/lv_objx/lv_img.h | 35 +++++ src/lv_objx/lv_imgbtn.c | 2 +- 9 files changed, 317 insertions(+), 304 deletions(-) diff --git a/src/lv_draw/lv_draw_img.c b/src/lv_draw/lv_draw_img.c index dbac1a4f4..74f736868 100644 --- a/src/lv_draw/lv_draw_img.c +++ b/src/lv_draw/lv_draw_img.c @@ -26,10 +26,10 @@ * STATIC PROTOTYPES **********************/ static lv_res_t lv_img_draw_core(const lv_area_t * coords, const lv_area_t * mask, const void * src, - const lv_style_t * style, uint16_t angle, lv_opa_t opa_scale); + const lv_style_t * style, uint16_t angle, uint16_t zoom, bool antialaias, lv_opa_t opa_scale); static void lv_draw_map(const lv_area_t * map_area, const lv_area_t * clip_area, const uint8_t * map_p, lv_opa_t opa, - bool chroma_key, bool alpha_byte, const lv_style_t * style, uint16_t angle); + bool chroma_key, bool alpha_byte, const lv_style_t * style, uint16_t angle, uint16_t zoom, bool antialaias); /********************** * STATIC VARIABLES @@ -49,10 +49,11 @@ static void lv_draw_map(const lv_area_t * map_area, const lv_area_t * clip_area, * @param mask the image will be drawn only in this area * @param src pointer to a lv_color_t array which contains the pixels of the image * @param style style of the image + * @param antialias anti-alias transformations (rotate, zoom) or not * @param opa_scale scale down all opacities by the factor */ void lv_draw_img(const lv_area_t * coords, const lv_area_t * mask, const void * src, const lv_style_t * style, - uint16_t angle, lv_opa_t opa_scale) + uint16_t angle, uint16_t zoom, bool antialias, lv_opa_t opa_scale) { if(src == NULL) { LV_LOG_WARN("Image draw: src is NULL"); @@ -62,7 +63,7 @@ void lv_draw_img(const lv_area_t * coords, const lv_area_t * mask, const void * } lv_res_t res; - res = lv_img_draw_core(coords, mask, src, style, angle, opa_scale); + res = lv_img_draw_core(coords, mask, src, style, angle, zoom, antialias, opa_scale); if(res == LV_RES_INV) { LV_LOG_WARN("Image draw error"); @@ -189,7 +190,7 @@ lv_img_src_t lv_img_src_get_type(const void * src) **********************/ static lv_res_t lv_img_draw_core(const lv_area_t * coords, const lv_area_t * mask, const void * src, - const lv_style_t * style, uint16_t angle, lv_opa_t opa_scale) + const lv_style_t * style, uint16_t angle, uint16_t zoom, bool antialias, lv_opa_t opa_scale) { lv_opa_t opa = opa_scale == LV_OPA_COVER ? style->image.opa : (uint16_t)((uint16_t)style->image.opa * opa_scale) >> 8; @@ -211,16 +212,21 @@ static lv_res_t lv_img_draw_core(const lv_area_t * coords, const lv_area_t * mas else if(cdsc->dec_dsc.img_data) { lv_area_t map_area_rot; lv_area_copy(&map_area_rot, coords); - if(angle) { + if(angle || zoom != LV_IMG_ZOOM_NONE) { /*Get the exact area which is required to show the rotated image*/ lv_coord_t pivot_x = lv_area_get_width(coords) / 2 + coords->x1; lv_coord_t pivot_y = lv_area_get_height(coords) / 2 + coords->y1; + lv_coord_t w = lv_area_get_width(coords); + lv_coord_t w_zoom = (((w * zoom) >> 8) - w) / 2; + lv_coord_t h = lv_area_get_height(coords); + lv_coord_t h_zoom = (((h * zoom) >> 8) - h) / 2; + lv_area_t norm; - norm.x1 = + coords->x1 - pivot_x; - norm.y1 = + coords->y1 - pivot_y; - norm.x2 = + coords->x2 - pivot_x; - norm.y2 = + coords->y2 - pivot_y; + norm.x1 = coords->x1 - pivot_x - w_zoom; + norm.y1 = coords->y1 - pivot_y - h_zoom; + norm.x2 = coords->x2 - pivot_x + w_zoom; + norm.y2 = coords->y2 - pivot_y + h_zoom; int16_t sinma = lv_trigo_sin(-angle); int16_t cosma = lv_trigo_sin(-angle + 90); @@ -255,7 +261,7 @@ static lv_res_t lv_img_draw_core(const lv_area_t * coords, const lv_area_t * mas successfully.*/ } - lv_draw_map(coords, &mask_com, cdsc->dec_dsc.img_data, opa, chroma_keyed, alpha_byte, style, angle); + lv_draw_map(coords, &mask_com, cdsc->dec_dsc.img_data, opa, chroma_keyed, alpha_byte, style, angle, zoom, antialias); } /* The whole uncompressed image is not available. Try to read it line-by-line*/ else { @@ -292,7 +298,7 @@ static lv_res_t lv_img_draw_core(const lv_area_t * coords, const lv_area_t * mas } - lv_draw_map(&line, &mask_line, buf, opa, chroma_keyed, alpha_byte, style, 0); + lv_draw_map(&line, &mask_line, buf, opa, chroma_keyed, alpha_byte, style, 0, LV_IMG_ZOOM_NONE, false); line.y1++; line.y2++; y++; @@ -312,9 +318,12 @@ static lv_res_t lv_img_draw_core(const lv_area_t * coords, const lv_area_t * mas * @param chroma_keyed true: enable transparency of LV_IMG_LV_COLOR_TRANSP color pixels * @param alpha_byte true: extra alpha byte is inserted for every pixel * @param style style of the image + * @param angle angle in degree + * @param zoom zoom factor + * @param antialias anti-alias transformations (rotate, zoom) or not */ static void lv_draw_map(const lv_area_t * map_area, const lv_area_t * clip_area, const uint8_t * map_p, lv_opa_t opa, - bool chroma_key, bool alpha_byte, const lv_style_t * style, uint16_t angle) + bool chroma_key, bool alpha_byte, const lv_style_t * style, uint16_t angle, uint16_t zoom, bool antialaias) { if(opa < LV_OPA_MIN) return; @@ -338,7 +347,9 @@ static void lv_draw_map(const lv_area_t * map_area, const lv_area_t * clip_area, uint8_t other_mask_cnt = lv_draw_mask_get_cnt(); /*The simplest case just copy the pixels into the VDB*/ - if(angle == 0 && other_mask_cnt == 0 && chroma_key == false && alpha_byte == false && opa == LV_OPA_COVER && style->image.intense == LV_OPA_TRANSP) { + if(other_mask_cnt == 0 && angle == 0 && zoom == LV_IMG_ZOOM_NONE && + chroma_key == false && alpha_byte == false && + opa == LV_OPA_COVER && style->image.intense == LV_OPA_TRANSP) { lv_blend_map(clip_area, map_area, (lv_color_t *)map_p, NULL, LV_DRAW_MASK_RES_FULL_COVER, LV_OPA_COVER, style->image.blend_mode); } /*In the other cases every pixel need to be checked one-by-one*/ @@ -377,14 +388,30 @@ static void lv_draw_map(const lv_area_t * map_area, const lv_area_t * clip_area, } - lv_img_rotate_dsc_t rotate_dsc; - memset(&rotate_dsc, 0, sizeof(lv_img_rotate_dsc_t)); - if(angle) { + bool transform = angle != 0 || zoom != LV_IMG_ZOOM_NONE ? true : false; + lv_img_transform_dsc_t trans_dsc; + memset(&trans_dsc, 0, sizeof(lv_img_transform_dsc_t)); + if(transform) { lv_img_cf_t cf = LV_IMG_CF_TRUE_COLOR; if(alpha_byte) cf = LV_IMG_CF_TRUE_COLOR_ALPHA; else if(chroma_key) cf = LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED; - lv_img_buf_rotate_init(&rotate_dsc, angle, map_p, map_w, map_h, cf, map_w/2, map_h / 2, LV_COLOR_BLACK); + + + trans_dsc.cfg.angle = angle; + trans_dsc.cfg.zoom = zoom; + trans_dsc.cfg.src = map_p; + trans_dsc.cfg.src_w = map_w; + trans_dsc.cfg.src_h = map_h; + trans_dsc.cfg.cf = cf; + trans_dsc.cfg.pivot_x = map_w / 2; + trans_dsc.cfg.pivot_y = map_h / 2; + trans_dsc.cfg.color = style->image.color; + trans_dsc.cfg.antialias = true; + + lv_img_buf_transform_init(&trans_dsc); } + + lv_draw_mask_res_t mask_res; mask_res = (alpha_byte || chroma_key || angle) ? LV_DRAW_MASK_RES_CHANGED : LV_DRAW_MASK_RES_FULL_COVER; lv_coord_t x; @@ -395,7 +422,7 @@ static void lv_draw_map(const lv_area_t * map_area, const lv_area_t * clip_area, for(x = 0; x < lv_area_get_width(&draw_area); x++, map_px += px_size_byte, px_i++) { - if(angle == 0) { + if(transform == false) { if(alpha_byte) { lv_opa_t px_opa = map_px[LV_IMG_PX_SIZE_ALPHA_BYTE - 1]; mask_buf[px_i] = px_opa; @@ -422,13 +449,13 @@ static void lv_draw_map(const lv_area_t * map_area, const lv_area_t * clip_area, bool ret; lv_coord_t rot_x = x + (disp_area->x1 + draw_area.x1) - map_area->x1; lv_coord_t rot_y = y + (disp_area->y1 + draw_area.y1) - map_area->y1; - ret = lv_img_buf_get_px_rotated(&rotate_dsc, rot_x, rot_y); + ret = lv_img_buf_transform(&trans_dsc, rot_x, rot_y); if(ret == false) { mask_buf[px_i] = LV_OPA_TRANSP; continue; } else { - mask_buf[px_i] = rotate_dsc.res_opa; - c.full = rotate_dsc.res_color.full; + mask_buf[px_i] = trans_dsc.res.opa; + c.full = trans_dsc.res.color.full; } } diff --git a/src/lv_draw/lv_draw_img.h b/src/lv_draw/lv_draw_img.h index 7633cc039..fed52911b 100644 --- a/src/lv_draw/lv_draw_img.h +++ b/src/lv_draw/lv_draw_img.h @@ -40,10 +40,11 @@ extern "C" { * @param mask the image will be drawn only in this area * @param src pointer to a lv_color_t array which contains the pixels of the image * @param style style of the image + * @param antialias anti-alias transformations (rotate, zoom) or not * @param opa_scale scale down all opacities by the factor */ void lv_draw_img(const lv_area_t * coords, const lv_area_t * mask, const void * src, const lv_style_t * style, - uint16_t angle, lv_opa_t opa_scale); + uint16_t angle, uint16_t zoom, bool antialaias, lv_opa_t opa_scale); /** * Get the type of an image source diff --git a/src/lv_draw/lv_img_buf.c b/src/lv_draw/lv_img_buf.c index bc998b8d8..89a57708a 100644 --- a/src/lv_draw/lv_img_buf.c +++ b/src/lv_draw/lv_img_buf.c @@ -24,7 +24,7 @@ /********************** * STATIC PROTOTYPES **********************/ -static inline bool transform_anti_alias(lv_img_rotate_dsc_t * dsc); +static inline bool transform_anti_alias(lv_img_transform_dsc_t * dsc); /********************** * STATIC VARIABLES @@ -161,118 +161,6 @@ lv_opa_t lv_img_buf_get_px_alpha(lv_img_dsc_t * dsc, lv_coord_t x, lv_coord_t y) return LV_OPA_COVER; } -/** - * Get the color of an image's pixel - * @param dsc an image descriptor - * @param x x coordinate of the point to get - * @param y x coordinate of the point to get - * @param color the color of the image. In case of `LV_IMG_CF_ALPHA_1/2/4/8` this color is used. - * Not used in other cases. - * @param safe true: check out of bounds - * @return color of the point - */ -void lv_img_buf_get_px(lv_img_dsc_t * dsc, lv_coord_t x, lv_coord_t y, lv_color_t color, lv_color_t * px_color, lv_opa_t * px_opa) -{ - uint8_t * buf_u8 = (uint8_t *)dsc->data; - - if(dsc->header.cf == LV_IMG_CF_TRUE_COLOR || dsc->header.cf == LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED) { - uint32_t px = dsc->header.w * y * sizeof(lv_color_t) + x * sizeof(lv_color_t); - memcpy(px_color, &buf_u8[px], sizeof(lv_color_t)); - *px_opa = LV_OPA_COVER; - } else if(dsc->header.cf == LV_IMG_CF_TRUE_COLOR_ALPHA) { - uint32_t px = dsc->header.w * y * LV_IMG_PX_SIZE_ALPHA_BYTE + x * LV_IMG_PX_SIZE_ALPHA_BYTE; - memcpy(px_color, &buf_u8[px], sizeof(lv_color_t)); - *px_opa = buf_u8[px + LV_IMG_PX_SIZE_ALPHA_BYTE - 1]; -#if LV_COLOR_SIZE == 32 - p_color.ch.alpha = 0xFF; /*Only the color should be get so use a default alpha value*/ -#endif - } else if(dsc->header.cf == LV_IMG_CF_INDEXED_1BIT) { - buf_u8 += 4 * 2; - uint8_t bit = x & 0x7; - x = x >> 3; - - /* Get the current pixel. - * dsc->header.w + 7 means rounding up to 8 because the lines are byte aligned - * so the possible real width are 8, 16, 24 ...*/ - uint32_t px = ((dsc->header.w + 7) >> 3) * y + x; - px_color->full = (buf_u8[px] & (1 << (7 - bit))) >> (7 - bit); - *px_opa = LV_OPA_COVER; - } else if(dsc->header.cf == LV_IMG_CF_INDEXED_2BIT) { - buf_u8 += 4 * 4; - uint8_t bit = (x & 0x3) * 2; - x = x >> 2; - - /* Get the current pixel. - * dsc->header.w + 3 means rounding up to 4 because the lines are byte aligned - * so the possible real width are 4, 8, 12 ...*/ - uint32_t px = ((dsc->header.w + 3) >> 2) * y + x; - px_color->full = (buf_u8[px] & (3 << (6 - bit))) >> (6 - bit); - *px_opa = LV_OPA_COVER; - } else if(dsc->header.cf == LV_IMG_CF_INDEXED_4BIT) { - buf_u8 += 4 * 16; - uint8_t bit = (x & 0x1) * 4; - x = x >> 1; - - /* Get the current pixel. - * dsc->header.w + 1 means rounding up to 2 because the lines are byte aligned - * so the possible real width are 2, 4, 6 ...*/ - uint32_t px = ((dsc->header.w + 1) >> 1) * y + x; - px_color->full = (buf_u8[px] & (0xF << (4 - bit))) >> (4 - bit); - *px_opa = LV_OPA_COVER; - } else if(dsc->header.cf == LV_IMG_CF_INDEXED_8BIT) { - buf_u8 += 4 * 256; - uint32_t px = dsc->header.w * y + x; - px_color->full = buf_u8[px]; - } else if(dsc->header.cf == LV_IMG_CF_ALPHA_1BIT || dsc->header.cf == LV_IMG_CF_ALPHA_2BIT || - dsc->header.cf == LV_IMG_CF_ALPHA_4BIT || dsc->header.cf == LV_IMG_CF_ALPHA_8BIT) { - *px_color = color; - *px_opa = LV_OPA_COVER; - } else if(dsc->header.cf == LV_IMG_CF_ALPHA_1BIT) { - uint8_t bit = x & 0x7; - x = x >> 3; - - /* Get the current pixel. - * dsc->header.w + 7 means rounding up to 8 because the lines are byte aligned - * so the possible real width are 8 ,16, 24 ...*/ - uint32_t px = ((dsc->header.w + 7) >> 3) * y + x; - uint8_t tmp = (buf_u8[px] & (1 << (7 - bit))) >> (7 - bit); - *px_opa = tmp ? LV_OPA_TRANSP : LV_OPA_COVER; - *px_color = color; - } else if(dsc->header.cf == LV_IMG_CF_ALPHA_2BIT) { - const uint8_t opa_table[4] = {0, 85, 170, 255}; /*Opacity mapping with bpp = 2*/ - - uint8_t bit = (x & 0x3) * 2; - x = x >> 2; - - /* Get the current pixel. - * dsc->header.w + 4 means rounding up to 8 because the lines are byte aligned - * so the possible real width are 4 ,8, 12 ...*/ - uint32_t px = ((dsc->header.w + 3) >> 2) * y + x; - uint8_t tmp = (buf_u8[px] & (3 << (6 - bit))) >> (6 - bit); - *px_opa = opa_table[tmp]; - *px_color = color; - } else if(dsc->header.cf == LV_IMG_CF_ALPHA_4BIT) { - const uint8_t opa_table[16] = {0, 17, 34, 51, /*Opacity mapping with bpp = 4*/ - 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255}; - - uint8_t bit = (x & 0x1) * 4; - x = x >> 1; - - /* Get the current pixel. - * dsc->header.w + 1 means rounding up to 8 because the lines are byte aligned - * so the possible real width are 2 ,4, 6 ...*/ - uint32_t px = ((dsc->header.w + 1) >> 1) * y + x; - uint8_t tmp = (buf_u8[px] & (0xF << (4 - bit))) >> (4 - bit); - *px_opa = opa_table[tmp]; - *px_color = color; - } else if(dsc->header.cf == LV_IMG_CF_ALPHA_8BIT) { - uint32_t px = dsc->header.w * y + x; - *px_opa = buf_u8[px]; - *px_color = color; - } -} - - /** * Set the color of a pixel of an image. The alpha channel won't be affected. * @param dsc pointer to an image descriptor @@ -493,48 +381,33 @@ uint32_t lv_img_buf_get_img_size(lv_coord_t w, lv_coord_t h, lv_img_cf_t cf) } /** - * Initialize a descriptor to rotate an image - * @param dsc pointer to an `lv_img_rotate_dsc_t` variable - * @param angle angle to rotate - * @param src image source (array of pixels) - * @param src_w width of the image to rotate - * @param src_h height of the image to rotate - * @param cf color format of the image to rotate - * @param pivot_x pivot x - * @param pivot_y pivot y - * @param color a color used for `LV_IMG_CF_INDEXED_1/2/4/8BIT` color formats + * Initialize a descriptor to tranform an image + * @param dsc pointer to an `lv_img_transform_dsc_t` variable whose `cfg` field is initialized */ -void lv_img_buf_rotate_init(lv_img_rotate_dsc_t * dsc, int16_t angle, const void * src, lv_coord_t src_w, lv_coord_t src_h, - lv_img_cf_t cf, lv_coord_t pivot_x, lv_coord_t pivot_y, lv_color_t color) +void lv_img_buf_transform_init(lv_img_transform_dsc_t * dsc) { - memset(dsc, 0x00, sizeof(lv_img_rotate_dsc_t)); + dsc->tmp.pivot_x_256 = dsc->cfg.pivot_x * 256; + dsc->tmp.pivot_y_256 = dsc->cfg.pivot_y * 256; + dsc->tmp.sinma = lv_trigo_sin(-dsc->cfg.angle); + dsc->tmp.cosma = lv_trigo_sin(-dsc->cfg.angle + 90); - dsc->angle = angle; - dsc->src = src; - dsc->src_w = src_w; - dsc->src_h = src_h; - dsc->cf = cf; - dsc->color = color; - dsc->pivot_x = pivot_x; - dsc->pivot_y = pivot_y; - dsc->pivot_x_256 = pivot_x * 256; - dsc->pivot_y_256 = pivot_y * 256; - dsc->sinma = lv_trigo_sin(-angle); - dsc->cosma = lv_trigo_sin(-angle + 90); - - dsc->chroma_keyed = lv_img_cf_is_chroma_keyed(cf) ? 1 : 0; - dsc->has_alpha = lv_img_cf_has_alpha(cf) ? 1 : 0; - if(cf == LV_IMG_CF_TRUE_COLOR || cf == LV_IMG_CF_TRUE_COLOR_ALPHA || cf == LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED) { - dsc->native_color = 1; + dsc->tmp.chroma_keyed = lv_img_cf_is_chroma_keyed(dsc->cfg.cf) ? 1 : 0; + dsc->tmp.has_alpha = lv_img_cf_has_alpha(dsc->cfg.cf) ? 1 : 0; + if(dsc->cfg.cf == LV_IMG_CF_TRUE_COLOR || dsc->cfg.cf == LV_IMG_CF_TRUE_COLOR_ALPHA || dsc->cfg.cf == LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED) { + dsc->tmp.native_color = 1; } - dsc->img_dsc.data = src; - dsc->img_dsc.header.always_zero = 0; - dsc->img_dsc.header.cf = cf; - dsc->img_dsc.header.w = src_w; - dsc->img_dsc.header.h = src_h; + dsc->tmp.img_dsc.data = dsc->cfg.src; + dsc->tmp.img_dsc.header.always_zero = 0; + dsc->tmp.img_dsc.header.cf = dsc->cfg.cf; + dsc->tmp.img_dsc.header.w = dsc->cfg.src_w; + dsc->tmp.img_dsc.header.h = dsc->cfg.src_h; - dsc->res_opa = LV_OPA_COVER; + + dsc->tmp.zoom_inv = (256 * 256) / dsc->cfg.zoom; + + dsc->res.opa = LV_OPA_COVER; + dsc->res.color = dsc->cfg.color; } /** @@ -545,43 +418,36 @@ void lv_img_buf_rotate_init(lv_img_rotate_dsc_t * dsc, int16_t angle, const void * @return true: there is valid pixel on these x/y coordinates; false: the rotated pixel was out of the image * @note the result is written back to `dsc->res_color` and `dsc->res_opa` */ -bool lv_img_buf_get_px_rotated(lv_img_rotate_dsc_t * dsc, lv_coord_t x, lv_coord_t y) +bool lv_img_buf_transform(lv_img_transform_dsc_t * dsc, lv_coord_t x, lv_coord_t y) { - const uint8_t * src_u8 = dsc->src; + const uint8_t * src_u8 = dsc->cfg.src; /*Get the target point relative coordinates to the pivot*/ - int32_t xt = x - dsc->pivot_x; - int32_t yt = y - dsc->pivot_y; + int32_t xt = x - dsc->cfg.pivot_x; + int32_t yt = y - dsc->cfg.pivot_y; - bool fast = false; - bool zoomed = false; - uint16_t zoom = 128; - uint16_t zoom_inv = (256 / zoom) * 256;; int32_t xs; int32_t ys; - if(!zoomed) { + if(dsc->cfg.zoom == LV_IMG_ZOOM_NONE) { /*Get the source pixel from the upscaled image*/ - xs = ((dsc->cosma * xt - dsc->sinma * yt) >> (LV_TRIGO_SHIFT - 8)) + dsc->pivot_x_256; - ys = ((dsc->sinma * xt + dsc->cosma * yt) >> (LV_TRIGO_SHIFT - 8)) + dsc->pivot_y_256; + xs = ((dsc->tmp.cosma * xt - dsc->tmp.sinma * yt) >> (LV_TRIGO_SHIFT - 8)) + dsc->tmp.pivot_x_256; + ys = ((dsc->tmp.sinma * xt + dsc->tmp.cosma * yt) >> (LV_TRIGO_SHIFT - 8)) + dsc->tmp.pivot_y_256; } else { - xt *= zoom_inv; - yt *= zoom_inv; - xs = ((dsc->cosma * xt - dsc->sinma * yt) >> (LV_TRIGO_SHIFT)) + dsc->pivot_x_256; - ys = ((dsc->sinma * xt + dsc->cosma * yt) >> (LV_TRIGO_SHIFT)) + dsc->pivot_y_256; + xt *= dsc->tmp.zoom_inv; + yt *= dsc->tmp.zoom_inv; + xs = ((dsc->tmp.cosma * xt - dsc->tmp.sinma * yt) >> (LV_TRIGO_SHIFT)) + dsc->tmp.pivot_x_256; + ys = ((dsc->tmp.sinma * xt + dsc->tmp.cosma * yt) >> (LV_TRIGO_SHIFT)) + dsc->tmp.pivot_y_256; } - // xt = xt / 2; - // yt = yt / 2; - /*Get the integer part of the source pixel*/ int xs_int = xs >> 8; int ys_int = ys >> 8; - if(xs_int >= dsc->src_w) return false; + if(xs_int >= dsc->cfg.src_w) return false; else if(xs_int < 0) return false; - if(ys_int >= dsc->src_h) return false; + if(ys_int >= dsc->cfg.src_h) return false; else if(ys_int < 0) return false; /* If the fractional < 0x70 mix the source pixel with the left/top pixel @@ -590,39 +456,39 @@ bool lv_img_buf_get_px_rotated(lv_img_rotate_dsc_t * dsc, lv_coord_t x, lv_coord uint8_t px_size; uint32_t pxi; - if(dsc->native_color) { - if(dsc->has_alpha == 0) { + if(dsc->tmp.native_color) { + if(dsc->tmp.has_alpha == 0) { px_size = LV_COLOR_SIZE >> 3; - pxi = dsc->src_w * ys_int * px_size + xs_int * px_size; - memcpy(&dsc->res_color, &src_u8[pxi], px_size); + pxi = dsc->cfg.src_w * ys_int * px_size + xs_int * px_size; + memcpy(&dsc->res.color, &src_u8[pxi], px_size); } else { px_size = LV_IMG_PX_SIZE_ALPHA_BYTE; - pxi = dsc->src_w * ys_int * px_size + xs_int * px_size; - memcpy(&dsc->res_color, &src_u8[pxi], px_size - 1); - dsc->res_opa = src_u8[pxi + px_size - 1]; + pxi = dsc->cfg.src_w * ys_int * px_size + xs_int * px_size; + memcpy(&dsc->res.color, &src_u8[pxi], px_size - 1); + dsc->res.opa = src_u8[pxi + px_size - 1]; } } else { pxi = 0; /*unused*/ px_size = 0; /*unused*/ - dsc->res_color = lv_img_buf_get_px_color(&dsc->img_dsc, xs_int, ys_int, dsc->color); - dsc->res_opa = lv_img_buf_get_px_alpha(&dsc->img_dsc, xs_int, ys_int); + dsc->res.color = lv_img_buf_get_px_color(&dsc->tmp.img_dsc, xs_int, ys_int, dsc->cfg.color); + dsc->res.opa = lv_img_buf_get_px_alpha(&dsc->tmp.img_dsc, xs_int, ys_int); } - if(dsc->chroma_keyed) { + if(dsc->tmp.chroma_keyed) { lv_color_t ct = LV_COLOR_TRANSP; - if(dsc->res_color.full == ct.full) return false; + if(dsc->res.color.full == ct.full) return false; } - if(fast) return true; + if(dsc->cfg.antialias == false) return true; - dsc->xs = xs; - dsc->ys = ys; - dsc->xs_int = xs_int; - dsc->ys_int = ys_int; - dsc->pxi = pxi; - dsc->px_size = px_size; + dsc->tmp.xs = xs; + dsc->tmp.ys = ys; + dsc->tmp.xs_int = xs_int; + dsc->tmp.ys_int = ys_int; + dsc->tmp.pxi = pxi; + dsc->tmp.px_size = px_size; bool ret; @@ -634,23 +500,23 @@ bool lv_img_buf_get_px_rotated(lv_img_rotate_dsc_t * dsc, lv_coord_t x, lv_coord /********************** * STATIC FUNCTIONS **********************/ -static inline bool transform_anti_alias(lv_img_rotate_dsc_t * dsc) +static inline bool transform_anti_alias(lv_img_transform_dsc_t * dsc) { - const uint8_t * src_u8 = dsc->src; + const uint8_t * src_u8 = dsc->cfg.src; /*Get the fractional part of the source pixel*/ - int xs_fract = dsc->xs & 0xff; - int ys_fract = dsc->ys & 0xff; + int xs_fract = dsc->tmp.xs & 0xff; + int ys_fract = dsc->tmp.ys & 0xff; int32_t xn; /*x neightboor*/ lv_opa_t xr; /*x mix ratio*/ if(xs_fract < 0x70) { xn = - 1; - if(dsc->xs_int + xn < 0) return false; + if(dsc->tmp.xs_int + xn < 0) return false; xr = xs_fract + 0x80; } else if(xs_fract > 0x90) { xn = 1; - if(dsc->xs_int + xn >= dsc->src_w) return false; + if(dsc->tmp.xs_int + xn >= dsc->cfg.src_w) return false; xr = (0xFF - xs_fract) + 0x80; } else { xn = 0; @@ -662,12 +528,12 @@ static inline bool transform_anti_alias(lv_img_rotate_dsc_t * dsc) if(ys_fract < 0x70) { yn = - 1; - if(dsc->ys_int + yn < 0) return false; + if(dsc->tmp.ys_int + yn < 0) return false; yr = ys_fract + 0x80; } else if(ys_fract > 0x90) { yn = 1; - if(dsc->ys_int + yn >= dsc->src_h) return false; + if(dsc->tmp.ys_int + yn >= dsc->cfg.src_h) return false; yr = (0xFF - ys_fract) + 0x80; } else { @@ -675,34 +541,34 @@ static inline bool transform_anti_alias(lv_img_rotate_dsc_t * dsc) yr = 0xFF; } - lv_color_t c00 = dsc->res_color; + lv_color_t c00 = dsc->res.color; lv_color_t c01; lv_color_t c10; lv_color_t c11; - lv_opa_t a00 = dsc->res_opa; + lv_opa_t a00 = dsc->res.opa; lv_opa_t a10; lv_opa_t a01; lv_opa_t a11; - if(dsc->native_color) { - memcpy(&c01, &src_u8[dsc->pxi + dsc->px_size * xn], sizeof(lv_color_t)); - memcpy(&c10, &src_u8[dsc->pxi + dsc->src_w * dsc->px_size * yn], sizeof(lv_color_t)); - memcpy(&c11, &src_u8[dsc->pxi + dsc->src_w * dsc->px_size * yn + dsc->px_size * xn], sizeof(lv_color_t)); - if(dsc->has_alpha) { - a10 = src_u8[dsc->pxi + dsc->px_size * xn + dsc->px_size - 1]; - a01 = src_u8[dsc->pxi + dsc->src_w * dsc->px_size * yn + dsc->px_size - 1]; - a11 = src_u8[dsc->pxi + dsc->src_w * dsc->px_size * yn + dsc->px_size * xn + dsc->px_size - 1]; + if(dsc->tmp.native_color) { + memcpy(&c01, &src_u8[dsc->tmp.pxi + dsc->tmp.px_size * xn], sizeof(lv_color_t)); + memcpy(&c10, &src_u8[dsc->tmp.pxi + dsc->cfg.src_w * dsc->tmp.px_size * yn], sizeof(lv_color_t)); + memcpy(&c11, &src_u8[dsc->tmp.pxi + dsc->cfg.src_w * dsc->tmp.px_size * yn + dsc->tmp.px_size * xn], sizeof(lv_color_t)); + if(dsc->tmp.has_alpha) { + a10 = src_u8[dsc->tmp.pxi + dsc->tmp.px_size * xn + dsc->tmp.px_size - 1]; + a01 = src_u8[dsc->tmp.pxi + dsc->cfg.src_w * dsc->tmp.px_size * yn + dsc->tmp.px_size - 1]; + a11 = src_u8[dsc->tmp.pxi + dsc->cfg.src_w * dsc->tmp.px_size * yn + dsc->tmp.px_size * xn + dsc->tmp.px_size - 1]; } } else { - c01 = lv_img_buf_get_px_color(&dsc->img_dsc, dsc->xs_int + xn, dsc->ys_int, dsc->color); - c10 = lv_img_buf_get_px_color(&dsc->img_dsc, dsc->xs_int, dsc->ys_int + yn, dsc->color); - c11 = lv_img_buf_get_px_color(&dsc->img_dsc, dsc->xs_int + xn, dsc->ys_int + yn, dsc->color); + c01 = lv_img_buf_get_px_color(&dsc->tmp.img_dsc, dsc->tmp.xs_int + xn, dsc->tmp.ys_int, dsc->cfg.color); + c10 = lv_img_buf_get_px_color(&dsc->tmp.img_dsc, dsc->tmp.xs_int, dsc->tmp.ys_int + yn, dsc->cfg.color); + c11 = lv_img_buf_get_px_color(&dsc->tmp.img_dsc, dsc->tmp.xs_int + xn, dsc->tmp.ys_int + yn, dsc->cfg.color); - if(dsc->has_alpha) { - a10 = lv_img_buf_get_px_alpha(&dsc->img_dsc, dsc->xs_int + xn, dsc->ys_int); - a01 = lv_img_buf_get_px_alpha(&dsc->img_dsc, dsc->xs_int, dsc->ys_int + yn); - a11 = lv_img_buf_get_px_alpha(&dsc->img_dsc, dsc->xs_int + xn, dsc->ys_int + yn); + if(dsc->tmp.has_alpha) { + a10 = lv_img_buf_get_px_alpha(&dsc->tmp.img_dsc, dsc->tmp.xs_int + xn, dsc->tmp.ys_int); + a01 = lv_img_buf_get_px_alpha(&dsc->tmp.img_dsc, dsc->tmp.xs_int, dsc->tmp.ys_int + yn); + a11 = lv_img_buf_get_px_alpha(&dsc->tmp.img_dsc, dsc->tmp.xs_int + xn, dsc->tmp.ys_int + yn); } } @@ -711,10 +577,10 @@ static inline bool transform_anti_alias(lv_img_rotate_dsc_t * dsc) lv_opa_t a1; lv_opa_t xr0 = xr; lv_opa_t xr1 = xr; - if(dsc->has_alpha) { + if(dsc->tmp.has_alpha) { a0 = (a00 * xr + (a10 * (255 - xr))) >> 8; a1 = (a01 * xr + (a11 * (255 - xr))) >> 8; - dsc->res_opa = (a0 * yr + (a1 * (255 - yr))) >> 8; + dsc->res.opa = (a0 * yr + (a1 * (255 - yr))) >> 8; if(a0 <= LV_OPA_MIN && a1 <= LV_OPA_MIN) return false; @@ -728,13 +594,13 @@ static inline bool transform_anti_alias(lv_img_rotate_dsc_t * dsc) } else { xr0 = xr; xr1 = xr; - dsc->res_opa = LV_OPA_COVER; + dsc->res.opa = LV_OPA_COVER; } lv_color_t c0 = lv_color_mix(c00, c01, xr0); lv_color_t c1 = lv_color_mix(c10, c11, xr1); - dsc->res_color = lv_color_mix(c0, c1, yr); + dsc->res.color = lv_color_mix(c0, c1, yr); return true; } diff --git a/src/lv_draw/lv_img_buf.h b/src/lv_draw/lv_img_buf.h index 1cfa4b436..3c1d82738 100644 --- a/src/lv_draw/lv_img_buf.h +++ b/src/lv_draw/lv_img_buf.h @@ -46,6 +46,8 @@ extern "C" { #define LV_IMG_BUF_SIZE_INDEXED_4BIT(w, h) (LV_IMG_BUF_SIZE_ALPHA_4BIT(w, h) + 4 * 16) #define LV_IMG_BUF_SIZE_INDEXED_8BIT(w, h) (LV_IMG_BUF_SIZE_ALPHA_8BIT(w, h) + 4 * 256) +#define LV_IMG_ZOOM_NONE 256 + /********************** * TYPEDEFS **********************/ @@ -125,38 +127,48 @@ typedef struct const uint8_t * data; } lv_img_dsc_t; - typedef struct { - lv_color_t res_color; - lv_opa_t res_opa; + struct { + const void * src; /*image source (array of pixels)*/ + lv_coord_t src_w; /*width of the image source*/ + lv_coord_t src_h; /*height of the image source*/ + lv_coord_t pivot_x; /*pivot x*/ + lv_coord_t pivot_y; /* pivot y*/ + int16_t angle; /*angle to rotate*/ + uint16_t zoom; /*256 no zoom, 128 half size, 512 double size*/ + lv_color_t color; /*a color used for `LV_IMG_CF_INDEXED_1/2/4/8BIT` color formats*/ + lv_img_cf_t cf; /*color format of the image to rotate*/ + bool antialias; + }cfg; - const void * src; - lv_coord_t src_w; - lv_coord_t src_h; - lv_coord_t pivot_x; - lv_coord_t pivot_y; - int32_t pivot_x_256; - int32_t pivot_y_256; - lv_img_dsc_t img_dsc; - int32_t sinma; - int32_t cosma; - int16_t angle; - lv_color_t color; - lv_img_cf_t cf; + struct { + lv_color_t color; + lv_opa_t opa; + }res; - uint8_t chroma_keyed :1; - uint8_t has_alpha :1; - uint8_t native_color :1; - /*Runtime data*/ - lv_coord_t xs; - lv_coord_t ys; - lv_coord_t xs_int; - lv_coord_t ys_int; - uint32_t pxi; - uint8_t px_size; + struct { + lv_img_dsc_t img_dsc; + int32_t pivot_x_256; + int32_t pivot_y_256; + int32_t sinma; + int32_t cosma; -}lv_img_rotate_dsc_t; + uint8_t chroma_keyed :1; + uint8_t has_alpha :1; + uint8_t native_color :1; + + uint16_t zoom_inv; + + /*Runtime data*/ + lv_coord_t xs; + lv_coord_t ys; + lv_coord_t xs_int; + lv_coord_t ys_int; + uint32_t pxi; + uint8_t px_size; + }tmp; +}lv_img_transform_dsc_t; /********************** * GLOBAL PROTOTYPES @@ -194,8 +206,6 @@ lv_color_t lv_img_buf_get_px_color(lv_img_dsc_t * dsc, lv_coord_t x, lv_coord_t lv_opa_t lv_img_buf_get_px_alpha(lv_img_dsc_t * dsc, lv_coord_t x, lv_coord_t y); -void lv_img_buf_get_px(lv_img_dsc_t * dsc, lv_coord_t x, lv_coord_t y, lv_color_t color, lv_color_t * px_color, lv_opa_t * px_opa); - /** * Set the color of a pixel of an image. The alpha channel won't be affected. * @param dsc pointer to an image descriptor @@ -243,20 +253,12 @@ void lv_img_buf_free(lv_img_dsc_t *dsc); */ uint32_t lv_img_buf_get_img_size(lv_coord_t w, lv_coord_t h, lv_img_cf_t cf); + /** * Initialize a descriptor to rotate an image - * @param dsc pointer to an `lv_img_rotate_dsc_t` variable - * @param angle angle to rotate - * @param src image source (array of pixels) - * @param src_w width of the image to rotate - * @param src_h height of the image to rotate - * @param cf color format of the image to rotate - * @param pivot_x pivot x - * @param pivot_y pivot y - * @param color a color used for `LV_IMG_CF_INDEXED_1/2/4/8BIT` color formats + * @param dsc pointer to an `lv_img_transform_dsc_t` variable whose `cfg` field is initialized */ -void lv_img_buf_rotate_init(lv_img_rotate_dsc_t * dsc, int16_t angle, const void * src, lv_coord_t src_w, lv_coord_t src_h, - lv_img_cf_t cf, lv_coord_t pivot_x, lv_coord_t pivot_y, lv_color_t color); +void lv_img_buf_transform_init(lv_img_transform_dsc_t * dsc); /** * Get which color and opa would come to a pixel if it were rotated @@ -266,7 +268,7 @@ void lv_img_buf_rotate_init(lv_img_rotate_dsc_t * dsc, int16_t angle, const void * @return true: there is valid pixel on these x/y coordinates; false: the rotated pixel was out of the image * @note the result is written back to `dsc->res_color` and `dsc->res_opa` */ -bool lv_img_buf_get_px_rotated(lv_img_rotate_dsc_t * dsc, lv_coord_t x, lv_coord_t y); +bool lv_img_buf_transform(lv_img_transform_dsc_t * dsc, lv_coord_t x, lv_coord_t y); /********************** diff --git a/src/lv_objx/lv_canvas.c b/src/lv_objx/lv_canvas.c index d4e056aa1..4c7fbdaa5 100644 --- a/src/lv_objx/lv_canvas.c +++ b/src/lv_objx/lv_canvas.c @@ -292,20 +292,22 @@ void lv_canvas_copy_buf(lv_obj_t * canvas, const void * to_copy, lv_coord_t x, l } /** - * Rotate and image and store the result on a canvas. + * Transform and image and store the result on a canvas. * @param canvas pointer to a canvas object * @param img pointer to an image descriptor. * Can be the image descriptor of an other canvas too (`lv_canvas_get_img()`). * @param angle the angle of rotation (0..360); + * @param zoom zoom factor (256 no zoom); * @param offset_x offset X to tell where to put the result data on destination canvas * @param offset_y offset X to tell where to put the result data on destination canvas * @param pivot_x pivot X of rotation. Relative to the source canvas * Set to `source width / 2` to rotate around the center * @param pivot_y pivot Y of rotation. Relative to the source canvas * Set to `source height / 2` to rotate around the center + * @param antialias apply anti-aliasing during the transformation. Looks better but slower. */ -void lv_canvas_rotate(lv_obj_t * canvas, lv_img_dsc_t * img, int16_t angle, lv_coord_t offset_x, lv_coord_t offset_y, - int32_t pivot_x, int32_t pivot_y) +void lv_canvas_transform(lv_obj_t * canvas, lv_img_dsc_t * img, int16_t angle, uint16_t zoom, lv_coord_t offset_x, lv_coord_t offset_y, + int32_t pivot_x, int32_t pivot_y, bool antialias) { LV_ASSERT_OBJ(canvas, LV_OBJX_NAME); LV_ASSERT_NULL(img); @@ -320,28 +322,38 @@ void lv_canvas_rotate(lv_obj_t * canvas, lv_img_dsc_t * img, int16_t angle, lv_c int32_t y; bool ret; - lv_img_rotate_dsc_t dsc; - lv_img_buf_rotate_init(&dsc, angle, img->data, img->header.w, img->header.h, img->header.cf, pivot_x, pivot_y, style->image.color); + lv_img_transform_dsc_t dsc; + dsc.cfg.angle = angle; + dsc.cfg.zoom = zoom; + dsc.cfg.src = img->data; + dsc.cfg.src_w = img->header.w; + dsc.cfg.src_h = img->header.h; + dsc.cfg.cf = img->header.cf; + dsc.cfg.pivot_x = pivot_x; + dsc.cfg.pivot_y = pivot_y; + dsc.cfg.color = style->image.color; + dsc.cfg.antialias = antialias; + lv_img_buf_transform_init(&dsc); for(y = -offset_y; y < dest_height - offset_y; y++) { for(x = -offset_x; x < dest_width - offset_x; x++) { - ret = lv_img_buf_get_px_rotated(&dsc, x, y); + ret = lv_img_buf_transform(&dsc, x, y); if(ret == false) continue; if(x + offset_x >= 0 && x + offset_x < dest_width && y + offset_y >= 0 && y + offset_y < dest_height) { /*If the image has no alpha channel just simple set the result color on the canvas*/ if(lv_img_cf_has_alpha(img->header.cf) == false) { - lv_img_buf_set_px_color(&ext_dst->dsc, x + offset_x, y + offset_y, dsc.res_color); + lv_img_buf_set_px_color(&ext_dst->dsc, x + offset_x, y + offset_y, dsc.res.color); } else { lv_color_t bg_color = lv_img_buf_get_px_color(&ext_dst->dsc, x + offset_x, y + offset_y, style->image.color); /*If the canvas has no alpha but the image has mix the image's color with * canvas*/ if(lv_img_cf_has_alpha(ext_dst->dsc.header.cf) == false) { - if(dsc.res_opa < LV_OPA_MAX) dsc.res_color = lv_color_mix(dsc.res_color, bg_color, dsc.res_opa); - lv_img_buf_set_px_color(&ext_dst->dsc, x + offset_x, y + offset_y, dsc.res_color); + if(dsc.res.opa < LV_OPA_MAX) dsc.res.color = lv_color_mix(dsc.res.color, bg_color, dsc.res.opa); + lv_img_buf_set_px_color(&ext_dst->dsc, x + offset_x, y + offset_y, dsc.res.color); } /*Both the image and canvas has alpha channel. Some extra calculation is required*/ @@ -349,28 +361,28 @@ void lv_canvas_rotate(lv_obj_t * canvas, lv_img_dsc_t * img, int16_t angle, lv_c lv_opa_t bg_opa = lv_img_buf_get_px_alpha(&ext_dst->dsc, x + offset_x, y + offset_y); /* Pick the foreground if it's fully opaque or the Background is fully * transparent*/ - if(dsc.res_opa >= LV_OPA_MAX || bg_opa <= LV_OPA_MIN) { - lv_img_buf_set_px_color(&ext_dst->dsc, x + offset_x, y + offset_y, dsc.res_color); - lv_img_buf_set_px_alpha(&ext_dst->dsc, x + offset_x, y + offset_y, dsc.res_opa); + if(dsc.res.opa >= LV_OPA_MAX || bg_opa <= LV_OPA_MIN) { + lv_img_buf_set_px_color(&ext_dst->dsc, x + offset_x, y + offset_y, dsc.res.color); + lv_img_buf_set_px_alpha(&ext_dst->dsc, x + offset_x, y + offset_y, dsc.res.opa); } /*Opaque background: use simple mix*/ else if(bg_opa >= LV_OPA_MAX) { lv_img_buf_set_px_color(&ext_dst->dsc, x + offset_x, y + offset_y, - lv_color_mix(dsc.res_color, bg_color, dsc.res_opa)); + lv_color_mix(dsc.res.color, bg_color, dsc.res.opa)); } /*Both colors have alpha. Expensive calculation need to be applied*/ else { /*Info: * https://en.wikipedia.org/wiki/Alpha_compositing#Analytical_derivation_of_the_over_operator*/ - lv_opa_t opa_res_2 = 255 - ((uint16_t)((uint16_t)(255 - dsc.res_opa) * (255 - bg_opa)) >> 8); + lv_opa_t opa_res_2 = 255 - ((uint16_t)((uint16_t)(255 - dsc.res.opa) * (255 - bg_opa)) >> 8); if(opa_res_2 == 0) { opa_res_2 = 1; /*never happens, just to be sure*/ } - lv_opa_t ratio = (uint16_t)((uint16_t)dsc.res_opa * 255) / opa_res_2; + lv_opa_t ratio = (uint16_t)((uint16_t)dsc.res.opa * 255) / opa_res_2; lv_img_buf_set_px_color(&ext_dst->dsc, x + offset_x, y + offset_y, - lv_color_mix(dsc.res_color, bg_color, ratio)); + lv_color_mix(dsc.res.color, bg_color, ratio)); lv_img_buf_set_px_alpha(&ext_dst->dsc, x + offset_x, y + offset_y, opa_res_2); } } @@ -873,7 +885,7 @@ void lv_canvas_draw_img(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y, const voi lv_disp_t * refr_ori = lv_refr_get_disp_refreshing(); lv_refr_set_disp_refreshing(&disp); - lv_draw_img(&coords, &mask, src, style, 0, LV_OPA_COVER); + lv_draw_img(&coords, &mask, src, style, 0, LV_IMG_ZOOM_NONE, false, LV_OPA_COVER); lv_refr_set_disp_refreshing(refr_ori); } diff --git a/src/lv_objx/lv_canvas.h b/src/lv_objx/lv_canvas.h index 9ef6c1a3d..9a648e5c1 100644 --- a/src/lv_objx/lv_canvas.h +++ b/src/lv_objx/lv_canvas.h @@ -151,20 +151,22 @@ void lv_canvas_copy_buf(lv_obj_t * canvas, const void * to_copy, lv_coord_t x, l lv_coord_t h); /** - * Rotate and image and store the result on a canvas. + * Transform and image and store the result on a canvas. * @param canvas pointer to a canvas object * @param img pointer to an image descriptor. * Can be the image descriptor of an other canvas too (`lv_canvas_get_img()`). * @param angle the angle of rotation (0..360); + * @param zoom zoom factor (256 no zoom); * @param offset_x offset X to tell where to put the result data on destination canvas * @param offset_y offset X to tell where to put the result data on destination canvas * @param pivot_x pivot X of rotation. Relative to the source canvas * Set to `source width / 2` to rotate around the center * @param pivot_y pivot Y of rotation. Relative to the source canvas * Set to `source height / 2` to rotate around the center + * @param antialias apply anti-aliasing during the transformation. Looks better but slower. */ -void lv_canvas_rotate(lv_obj_t * canvas, lv_img_dsc_t * img, int16_t angle, lv_coord_t offset_x, lv_coord_t offset_y, - int32_t pivot_x, int32_t pivot_y); +void lv_canvas_transform(lv_obj_t * canvas, lv_img_dsc_t * img, int16_t angle, uint16_t zoom, lv_coord_t offset_x, lv_coord_t offset_y, + int32_t pivot_x, int32_t pivot_y, bool antialias); diff --git a/src/lv_objx/lv_img.c b/src/lv_objx/lv_img.c index d70a1707a..f792867ab 100644 --- a/src/lv_objx/lv_img.c +++ b/src/lv_objx/lv_img.c @@ -80,6 +80,8 @@ lv_obj_t * lv_img_create(lv_obj_t * par, const lv_obj_t * copy) ext->w = lv_obj_get_width(new_img); ext->h = lv_obj_get_height(new_img); ext->angle = 0; + ext->zoom = LV_IMG_ZOOM_NONE; + ext->antialias = LV_ANTIALIAS ? 1 : 0; ext->auto_size = 1; ext->offset.x = 0; ext->offset.y = 0; @@ -269,10 +271,47 @@ void lv_img_set_angle(lv_obj_t * img, int16_t angle) if(angle < 0 || angle >= 360) angle = angle % 360; lv_img_ext_t * ext = lv_obj_get_ext_attr(img); + if(angle == ext->angle) return; + ext->angle = angle; lv_obj_refresh_ext_draw_pad(img); lv_obj_invalidate(img); +} +/** + * Set the zoom factor of the image. + * @param img pointer to an image object + * @param zoom the zoom factor. + * - 256 or LV_ZOOM_IMG_NONE for no zoom + * - <256: scale down + * - >256 scale up + * - 128 half size + * - 512 double size + */ +void lv_img_set_zoom(lv_obj_t * img, uint16_t zoom) +{ + lv_img_ext_t * ext = lv_obj_get_ext_attr(img); + if(zoom == ext->zoom) return; + + if(zoom == 0) zoom = 1; + + ext->zoom = zoom; + lv_obj_refresh_ext_draw_pad(img); + lv_obj_invalidate(img); +} + +/** + * Enable/disable anti-aliasing for the transformations (rotate, zoom) or not + * @param img pointer to an image object + * @param antialias true: anti-aliased; false: not anti-aliased + */ +void lv_img_set_antialais(lv_obj_t * img, bool antialias) +{ + lv_img_ext_t * ext = lv_obj_get_ext_attr(img); + if(antialias == ext->antialias) return; + + ext->antialias = antialias; + lv_obj_invalidate(img); } /*===================== @@ -366,6 +405,34 @@ uint16_t lv_img_get_angle(lv_obj_t * img) return ext->angle; } +/** + * Get the zoom factor of the image. + * @param img pointer to an image object + * @return zoom factor (256: no zoom) + */ +uint16_t lv_img_get_zoom(lv_obj_t * img) +{ + LV_ASSERT_OBJ(img, LV_OBJX_NAME); + + lv_img_ext_t * ext = lv_obj_get_ext_attr(img); + + return ext->zoom; +} + +/** + * Get whether the transformations (rotate, zoom) are anti-aliased or not + * @param img pointer to an image object + * @return true: anti-aliased; false: not anti-aliased + */ +bool lv_img_get_antialais(lv_obj_t * img) +{ + LV_ASSERT_OBJ(img, LV_OBJX_NAME); + + lv_img_ext_t * ext = lv_obj_get_ext_attr(img); + + return ext->antialias ? true : false; +} + /********************** * STATIC FUNCTIONS **********************/ @@ -414,7 +481,7 @@ static lv_design_res_t lv_img_design(lv_obj_t * img, const lv_area_t * clip_area cords_tmp.x1 = coords.x1; cords_tmp.x2 = coords.x1 + ext->w - 1; for(; cords_tmp.x1 < coords.x2; cords_tmp.x1 += ext->w, cords_tmp.x2 += ext->w) { - lv_draw_img(&cords_tmp, clip_area, ext->src, style, ext->angle, opa_scale); + lv_draw_img(&cords_tmp, clip_area, ext->src, style, ext->angle, ext->zoom, ext->antialias, opa_scale); } } } else if(ext->src_type == LV_IMG_SRC_SYMBOL) { @@ -426,7 +493,7 @@ static lv_design_res_t lv_img_design(lv_obj_t * img, const lv_area_t * clip_area } else { /*Trigger the error handler of image drawer*/ LV_LOG_WARN("lv_img_design: image source type is unknown"); - lv_draw_img(&img->coords, clip_area, NULL, style, 0, opa_scale); + lv_draw_img(&img->coords, clip_area, NULL, style, 0, LV_IMG_ZOOM_NONE, false, opa_scale); } } @@ -464,9 +531,10 @@ static lv_res_t lv_img_signal(lv_obj_t * img, lv_signal_t sign, void * param) } } else if(sign == LV_SIGNAL_REFR_EXT_DRAW_PAD) { /*If the image has angle provide enough room for the rotated corners */ - if(ext->angle) { + if(ext->angle && ext->zoom) { lv_sqrt_res_t ds; lv_sqrt(ext->w * ext->w + ext->h * ext->h, &ds); + ds.i = (ds.i * ext->zoom + 0) >> 8; /*+10 to be sure anything won't be clipped*/ lv_coord_t d = (ds.i - LV_MATH_MIN(ext->w, ext->h)) / 2; img->ext_draw_pad = LV_MATH_MAX(img->ext_draw_pad, d); diff --git a/src/lv_objx/lv_img.h b/src/lv_objx/lv_img.h index cb02a1655..d9a308e2f 100644 --- a/src/lv_objx/lv_img.h +++ b/src/lv_objx/lv_img.h @@ -43,9 +43,11 @@ typedef struct lv_coord_t w; /*Width of the image (Handled by the library)*/ lv_coord_t h; /*Height of the image (Handled by the library)*/ uint16_t angle; + uint16_t zoom; /*256 means no zoom, 512 double size, 128 hasl size*/ uint8_t src_type : 2; /*See: lv_img_src_t*/ uint8_t auto_size : 1; /*1: automatically set the object size to the image size*/ uint8_t cf : 5; /*Color format from `lv_img_color_format_t`*/ + uint8_t antialias :1; /*Apply anti-aliasing in transformations (rotate, zoom)*/ } lv_img_ext_t; /*Styles*/ @@ -109,6 +111,25 @@ void lv_img_set_offset_y(lv_obj_t * img, lv_coord_t y); */ void lv_img_set_angle(lv_obj_t * img, int16_t angle); +/** + * Set the zoom factor of the image. + * @param img pointer to an image object + * @param zoom the zoom factor. + * - 256 or LV_ZOOM_IMG_NONE for no zoom + * - <256: scale down + * - >256 scale up + * - 128 half size + * - 512 double size + */ +void lv_img_set_zoom(lv_obj_t * img, uint16_t zoom); + +/** + * Enable/disable anti-aliasing for the transformations (rotate, zoom) or not + * @param img pointer to an image object + * @param antialias true: anti-aliased; false: not anti-aliased + */ +void lv_img_set_antialais(lv_obj_t * img, bool antialias); + /** * Set the style of an image * @param img pointer to an image object @@ -167,6 +188,20 @@ lv_coord_t lv_img_get_offset_y(lv_obj_t * img); */ uint16_t lv_img_get_angle(lv_obj_t * img); +/** + * Get the zoom factor of the image. + * @param img pointer to an image object + * @return zoom factor (256: no zoom) + */ +uint16_t lv_img_get_zoom(lv_obj_t * img); + +/** + * Get whether the transformations (rotate, zoom) are anti-aliased or not + * @param img pointer to an image object + * @return true: anti-aliased; false: not anti-aliased + */ +bool lv_img_get_antialais(lv_obj_t * img); + /** * Get the style of an image object * @param img pointer to an image object diff --git a/src/lv_objx/lv_imgbtn.c b/src/lv_objx/lv_imgbtn.c index d264f6a2d..16a468689 100644 --- a/src/lv_objx/lv_imgbtn.c +++ b/src/lv_objx/lv_imgbtn.c @@ -303,7 +303,7 @@ static lv_design_res_t lv_imgbtn_design(lv_obj_t * imgbtn, const lv_area_t * cli if(lv_img_src_get_type(src) == LV_IMG_SRC_SYMBOL) { lv_draw_label(&imgbtn->coords, clip_area, style, opa_scale, src, LV_TXT_FLAG_NONE, NULL, NULL, NULL); } else { - lv_draw_img(&imgbtn->coords, clip_area, src, style, 0, opa_scale); + lv_draw_img(&imgbtn->coords, clip_area, src, style, 0, LV_IMG_ZOOM_NONE, false, opa_scale); } #else const void * src = ext->img_src_left[state];