2019-01-14 19:37:55 +01:00
|
|
|
/**
|
|
|
|
* @file lv_canvas.c
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*********************
|
|
|
|
* INCLUDES
|
|
|
|
*********************/
|
2019-01-25 16:01:52 +02:00
|
|
|
#include <stdlib.h>
|
2019-01-14 19:37:55 +01:00
|
|
|
#include "lv_canvas.h"
|
2019-09-24 16:30:38 +02:00
|
|
|
#include "../lv_core/lv_debug.h"
|
2019-04-04 07:15:40 +02:00
|
|
|
#include "../lv_misc/lv_math.h"
|
2019-06-02 20:35:33 +02:00
|
|
|
#include "../lv_draw/lv_draw.h"
|
|
|
|
#include "../lv_core/lv_refr.h"
|
|
|
|
|
2019-03-07 00:05:16 +01:00
|
|
|
#if LV_USE_CANVAS != 0
|
2019-01-14 19:37:55 +01:00
|
|
|
|
|
|
|
/*********************
|
|
|
|
* DEFINES
|
|
|
|
*********************/
|
2019-09-26 10:51:54 +02:00
|
|
|
#define LV_OBJX_NAME "lv_canvas"
|
2019-01-14 19:37:55 +01:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* TYPEDEFS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC PROTOTYPES
|
|
|
|
**********************/
|
|
|
|
static lv_res_t lv_canvas_signal(lv_obj_t * canvas, lv_signal_t sign, void * param);
|
2019-09-08 15:25:20 +02:00
|
|
|
static void set_set_px_cb(lv_disp_drv_t * disp_drv, lv_img_cf_t cf);
|
|
|
|
|
2019-11-01 06:04:03 +01:00
|
|
|
static void set_px_true_color_alpha(lv_disp_drv_t * disp_drv, uint8_t * buf, lv_coord_t buf_w, lv_coord_t x, lv_coord_t y,
|
2019-09-08 15:25:20 +02:00
|
|
|
lv_color_t color, lv_opa_t opa);
|
|
|
|
|
|
|
|
static void set_px_cb_alpha1(lv_disp_drv_t * disp_drv, uint8_t * buf, lv_coord_t buf_w, lv_coord_t x, lv_coord_t y,
|
|
|
|
lv_color_t color, lv_opa_t opa);
|
|
|
|
|
|
|
|
static void set_px_cb_alpha2(lv_disp_drv_t * disp_drv, uint8_t * buf, lv_coord_t buf_w, lv_coord_t x, lv_coord_t y,
|
|
|
|
lv_color_t color, lv_opa_t opa);
|
|
|
|
|
|
|
|
static void set_px_cb_alpha4(lv_disp_drv_t * disp_drv, uint8_t * buf, lv_coord_t buf_w, lv_coord_t x, lv_coord_t y,
|
|
|
|
lv_color_t color, lv_opa_t opa);
|
|
|
|
|
|
|
|
|
|
|
|
static void set_px_cb_alpha8(lv_disp_drv_t * disp_drv, uint8_t * buf, lv_coord_t buf_w, lv_coord_t x, lv_coord_t y,
|
|
|
|
lv_color_t color, lv_opa_t opa);
|
|
|
|
|
|
|
|
void set_px_alpha_generic(lv_img_dsc_t * d, lv_coord_t x, lv_coord_t y, lv_color_t color, lv_opa_t opa);
|
|
|
|
|
|
|
|
|
2019-01-14 19:37:55 +01:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC VARIABLES
|
|
|
|
**********************/
|
2019-02-26 09:25:46 +01:00
|
|
|
static lv_signal_cb_t ancestor_signal;
|
|
|
|
static lv_design_cb_t ancestor_design;
|
2019-01-14 19:37:55 +01:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* MACROS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* GLOBAL FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a canvas object
|
|
|
|
* @param par pointer to an object, it will be the parent of the new canvas
|
|
|
|
* @param copy pointer to a canvas object, if not NULL then the new object will be copied from it
|
|
|
|
* @return pointer to the created canvas
|
|
|
|
*/
|
|
|
|
lv_obj_t * lv_canvas_create(lv_obj_t * par, const lv_obj_t * copy)
|
|
|
|
{
|
|
|
|
LV_LOG_TRACE("canvas create started");
|
|
|
|
|
|
|
|
/*Create the ancestor of canvas*/
|
|
|
|
lv_obj_t * new_canvas = lv_img_create(par, copy);
|
2019-09-24 23:14:17 +02:00
|
|
|
LV_ASSERT_MEM(new_canvas);
|
2019-01-14 19:37:55 +01:00
|
|
|
if(new_canvas == NULL) return NULL;
|
|
|
|
|
|
|
|
/*Allocate the canvas type specific extended data*/
|
|
|
|
lv_canvas_ext_t * ext = lv_obj_allocate_ext_attr(new_canvas, sizeof(lv_canvas_ext_t));
|
2019-09-24 23:14:17 +02:00
|
|
|
LV_ASSERT_MEM(ext);
|
2019-01-14 19:37:55 +01:00
|
|
|
if(ext == NULL) return NULL;
|
2019-04-10 06:40:49 +02:00
|
|
|
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_canvas);
|
|
|
|
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(new_canvas);
|
2019-01-14 19:37:55 +01:00
|
|
|
|
|
|
|
/*Initialize the allocated 'ext' */
|
|
|
|
ext->dsc.header.always_zero = 0;
|
2019-04-04 07:15:40 +02:00
|
|
|
ext->dsc.header.cf = LV_IMG_CF_TRUE_COLOR;
|
|
|
|
ext->dsc.header.h = 0;
|
|
|
|
ext->dsc.header.w = 0;
|
|
|
|
ext->dsc.data_size = 0;
|
|
|
|
ext->dsc.data = NULL;
|
2019-01-14 19:37:55 +01:00
|
|
|
|
|
|
|
lv_img_set_src(new_canvas, &ext->dsc);
|
|
|
|
|
|
|
|
/*The signal and design functions are not copied so set them here*/
|
2019-02-26 09:25:46 +01:00
|
|
|
lv_obj_set_signal_cb(new_canvas, lv_canvas_signal);
|
2019-01-14 19:37:55 +01:00
|
|
|
|
|
|
|
/*Init the new canvas canvas*/
|
|
|
|
if(copy == NULL) {
|
|
|
|
|
|
|
|
}
|
|
|
|
/*Copy an existing canvas*/
|
|
|
|
else {
|
|
|
|
/*Refresh the style with new signal function*/
|
|
|
|
lv_obj_refresh_style(new_canvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
LV_LOG_INFO("canvas created");
|
|
|
|
|
|
|
|
return new_canvas;
|
|
|
|
}
|
|
|
|
|
2019-01-31 21:06:28 +01:00
|
|
|
/*=====================
|
|
|
|
* Setter functions
|
|
|
|
*====================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a buffer for the canvas.
|
|
|
|
* @param buf a buffer where the content of the canvas will be.
|
|
|
|
* The required size is (lv_img_color_format_get_px_size(cf) * w * h) / 8)
|
|
|
|
* It can be allocated with `lv_mem_alloc()` or
|
|
|
|
* it can be statically allocated array (e.g. static lv_color_t buf[100*50]) or
|
|
|
|
* it can be an address in RAM or external SRAM
|
|
|
|
* @param canvas pointer to a canvas object
|
|
|
|
* @param w width of the canvas
|
|
|
|
* @param h height of the canvas
|
|
|
|
* @param cf color format. The following formats are supported:
|
|
|
|
* LV_IMG_CF_TRUE_COLOR, LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED, LV_IMG_CF_INDEXES_1/2/4/8BIT
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void lv_canvas_set_buffer(lv_obj_t * canvas, void * buf, lv_coord_t w, lv_coord_t h, lv_img_cf_t cf)
|
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(canvas, LV_OBJX_NAME);
|
|
|
|
LV_ASSERT_NULL(buf);
|
|
|
|
|
2019-01-31 21:06:28 +01:00
|
|
|
lv_canvas_ext_t * ext = lv_obj_get_ext_attr(canvas);
|
|
|
|
|
|
|
|
ext->dsc.header.cf = cf;
|
2019-04-04 07:15:40 +02:00
|
|
|
ext->dsc.header.w = w;
|
|
|
|
ext->dsc.header.h = h;
|
|
|
|
ext->dsc.data = buf;
|
2019-01-31 21:06:28 +01:00
|
|
|
ext->dsc.data_size = (lv_img_color_format_get_px_size(cf) * w * h) / 8;
|
2019-01-14 19:37:55 +01:00
|
|
|
|
2019-01-31 21:06:28 +01:00
|
|
|
lv_img_set_src(canvas, &ext->dsc);
|
|
|
|
}
|
2019-03-20 09:02:55 +01:00
|
|
|
|
2019-01-14 19:37:55 +01:00
|
|
|
/**
|
|
|
|
* Set the color of a pixel on the canvas
|
2019-03-20 09:02:55 +01:00
|
|
|
* @param canvas pointer to canvas object
|
2019-01-16 08:37:18 +01:00
|
|
|
* @param x x coordinate of the point to set
|
|
|
|
* @param y x coordinate of the point to set
|
2019-01-14 19:37:55 +01:00
|
|
|
* @param c color of the point
|
|
|
|
*/
|
|
|
|
void lv_canvas_set_px(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y, lv_color_t c)
|
2019-04-04 07:15:40 +02:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(canvas, LV_OBJX_NAME);
|
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
lv_canvas_ext_t * ext = lv_obj_get_ext_attr(canvas);
|
2019-03-24 09:52:24 -04:00
|
|
|
|
2019-03-27 00:05:15 +01:00
|
|
|
lv_img_buf_set_px_color(&ext->dsc, x, y, c);
|
2019-03-20 09:02:55 +01:00
|
|
|
lv_obj_invalidate(canvas);
|
2019-01-31 21:06:28 +01:00
|
|
|
}
|
|
|
|
|
2019-06-14 07:17:02 +02:00
|
|
|
/**
|
|
|
|
* Set the palette color of a canvas with index format. Valid only for `LV_IMG_CF_INDEXED1/2/4/8`
|
|
|
|
* @param canvas pointer to canvas object
|
|
|
|
* @param id the palette color to set:
|
|
|
|
* - for `LV_IMG_CF_INDEXED1`: 0..1
|
|
|
|
* - for `LV_IMG_CF_INDEXED2`: 0..3
|
|
|
|
* - for `LV_IMG_CF_INDEXED4`: 0..15
|
|
|
|
* - for `LV_IMG_CF_INDEXED8`: 0..255
|
|
|
|
* @param c the color to set
|
|
|
|
*/
|
|
|
|
void lv_canvas_set_palette(lv_obj_t * canvas, uint8_t id, lv_color_t c)
|
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(canvas, LV_OBJX_NAME);
|
|
|
|
|
2019-06-14 07:17:02 +02:00
|
|
|
lv_canvas_ext_t * ext = lv_obj_get_ext_attr(canvas);
|
|
|
|
|
|
|
|
lv_img_buf_set_palette(&ext->dsc, id, c);
|
|
|
|
lv_obj_invalidate(canvas);
|
|
|
|
}
|
|
|
|
|
2019-01-31 21:06:28 +01:00
|
|
|
/**
|
|
|
|
* Set a style of a canvas.
|
|
|
|
* @param canvas pointer to canvas object
|
|
|
|
* @param type which style should be set
|
|
|
|
* @param style pointer to a style
|
|
|
|
*/
|
2019-04-11 19:59:55 +08:00
|
|
|
void lv_canvas_set_style(lv_obj_t * canvas, lv_canvas_style_t type, const lv_style_t * style)
|
2019-01-31 21:06:28 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(canvas, LV_OBJX_NAME);
|
|
|
|
|
2019-01-31 21:06:28 +01:00
|
|
|
switch(type) {
|
2019-06-11 06:33:33 +02:00
|
|
|
case LV_CANVAS_STYLE_MAIN: lv_img_set_style(canvas, LV_IMG_STYLE_MAIN, style); break;
|
2019-01-31 21:06:28 +01:00
|
|
|
}
|
2019-01-14 19:37:55 +01:00
|
|
|
}
|
|
|
|
|
2019-01-31 21:06:28 +01:00
|
|
|
/*=====================
|
|
|
|
* Getter functions
|
|
|
|
*====================*/
|
|
|
|
|
2019-01-25 14:14:22 +03:00
|
|
|
/**
|
|
|
|
* Get the color of a pixel on the canvas
|
|
|
|
* @param canvas
|
|
|
|
* @param x x coordinate of the point to set
|
|
|
|
* @param y x coordinate of the point to set
|
2019-01-31 21:06:28 +01:00
|
|
|
* @return color of the point
|
2019-01-25 14:14:22 +03:00
|
|
|
*/
|
2019-01-31 21:06:28 +01:00
|
|
|
lv_color_t lv_canvas_get_px(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y)
|
2019-01-25 14:14:22 +03:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(canvas, LV_OBJX_NAME);
|
|
|
|
|
2019-04-11 19:59:55 +08:00
|
|
|
lv_canvas_ext_t * ext = lv_obj_get_ext_attr(canvas);
|
|
|
|
const lv_style_t * style = lv_canvas_get_style(canvas, LV_CANVAS_STYLE_MAIN);
|
2019-04-04 07:15:40 +02:00
|
|
|
|
2019-11-01 06:00:04 +01:00
|
|
|
if(style == NULL) style = &lv_style_scr;
|
|
|
|
|
|
|
|
return lv_img_buf_get_px_color(&ext->dsc, x, y, style->image.color);
|
2019-01-31 21:06:28 +01:00
|
|
|
}
|
|
|
|
|
2019-03-26 15:35:45 +01:00
|
|
|
/**
|
|
|
|
* Get the image of the canvas as a pointer to an `lv_img_dsc_t` variable.
|
|
|
|
* @param canvas pointer to a canvas object
|
|
|
|
* @return pointer to the image descriptor.
|
|
|
|
*/
|
|
|
|
lv_img_dsc_t * lv_canvas_get_img(lv_obj_t * canvas)
|
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(canvas, LV_OBJX_NAME);
|
|
|
|
|
2019-03-26 15:35:45 +01:00
|
|
|
lv_canvas_ext_t * ext = lv_obj_get_ext_attr(canvas);
|
|
|
|
|
2019-03-27 00:05:15 +01:00
|
|
|
return &ext->dsc;
|
2019-03-26 15:35:45 +01:00
|
|
|
}
|
|
|
|
|
2019-01-31 21:06:28 +01:00
|
|
|
/**
|
|
|
|
* Get style of a canvas.
|
|
|
|
* @param canvas pointer to canvas object
|
|
|
|
* @param type which style should be get
|
|
|
|
* @return style pointer to the style
|
|
|
|
*/
|
2019-04-11 19:59:55 +08:00
|
|
|
const lv_style_t * lv_canvas_get_style(const lv_obj_t * canvas, lv_canvas_style_t type)
|
2019-01-31 21:06:28 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(canvas, LV_OBJX_NAME);
|
|
|
|
|
2019-04-11 19:59:55 +08:00
|
|
|
const lv_style_t * style = NULL;
|
2019-01-31 21:06:28 +01:00
|
|
|
|
|
|
|
switch(type) {
|
2019-06-11 06:33:33 +02:00
|
|
|
case LV_CANVAS_STYLE_MAIN: style = lv_img_get_style(canvas, LV_IMG_STYLE_MAIN); break;
|
2019-04-04 07:15:40 +02:00
|
|
|
default: style = NULL;
|
2019-01-31 21:06:28 +01:00
|
|
|
}
|
2019-01-25 14:14:22 +03:00
|
|
|
|
2019-01-31 21:06:28 +01:00
|
|
|
return style;
|
2019-01-25 14:14:22 +03:00
|
|
|
}
|
|
|
|
|
2019-01-31 21:06:28 +01:00
|
|
|
/*=====================
|
|
|
|
* Other functions
|
|
|
|
*====================*/
|
|
|
|
|
2019-01-16 08:37:18 +01:00
|
|
|
/**
|
|
|
|
* Copy a buffer to the canvas
|
|
|
|
* @param canvas pointer to a canvas object
|
2019-04-04 07:15:40 +02:00
|
|
|
* @param to_copy buffer to copy. The color format has to match with the canvas's buffer color
|
|
|
|
* format
|
2019-01-16 08:37:18 +01:00
|
|
|
* @param w width of the buffer to copy
|
|
|
|
* @param h height of the buffer to copy
|
|
|
|
* @param x left side of the destination position
|
|
|
|
* @param y top side of the destination position
|
|
|
|
*/
|
2019-06-25 08:21:24 -04:00
|
|
|
void lv_canvas_copy_buf(lv_obj_t * canvas, const void * to_copy, lv_coord_t x, lv_coord_t y, lv_coord_t w, lv_coord_t h)
|
2019-01-16 08:37:18 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(canvas, LV_OBJX_NAME);
|
|
|
|
LV_ASSERT_NULL(to_copy);
|
|
|
|
|
2019-01-16 08:37:18 +01:00
|
|
|
lv_canvas_ext_t * ext = lv_obj_get_ext_attr(canvas);
|
2019-01-17 16:23:50 +01:00
|
|
|
if(x + w >= ext->dsc.header.w || y + h >= ext->dsc.header.h) {
|
|
|
|
LV_LOG_WARN("lv_canvas_copy_buf: x or y out of the canvas");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
uint32_t px_size = lv_img_color_format_get_px_size(ext->dsc.header.cf) >> 3;
|
|
|
|
uint32_t px = ext->dsc.header.w * y * px_size + x * px_size;
|
|
|
|
uint8_t * to_copy8 = (uint8_t *)to_copy;
|
2019-01-17 16:23:50 +01:00
|
|
|
lv_coord_t i;
|
|
|
|
for(i = 0; i < h; i++) {
|
2019-04-04 07:15:40 +02:00
|
|
|
memcpy((void *)&ext->dsc.data[px], to_copy8, w * px_size);
|
2019-01-17 16:23:50 +01:00
|
|
|
px += ext->dsc.header.w * px_size;
|
|
|
|
to_copy8 += w * px_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 09:15:56 +01:00
|
|
|
/**
|
2019-03-24 19:35:10 +01:00
|
|
|
* Rotate 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()`).
|
2019-03-20 09:15:56 +01:00
|
|
|
* @param angle the angle of rotation (0..360);
|
|
|
|
* @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
|
|
|
|
*/
|
2019-06-06 06:05:40 +02:00
|
|
|
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)
|
2019-03-20 08:01:01 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(canvas, LV_OBJX_NAME);
|
|
|
|
LV_ASSERT_NULL(img);
|
|
|
|
|
2019-03-24 19:35:10 +01:00
|
|
|
lv_canvas_ext_t * ext_dst = lv_obj_get_ext_attr(canvas);
|
2019-04-11 19:59:55 +08:00
|
|
|
const lv_style_t * style = lv_canvas_get_style(canvas, LV_CANVAS_STYLE_MAIN);
|
2019-03-20 09:02:55 +01:00
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
int32_t dest_width = ext_dst->dsc.header.w;
|
2019-03-20 09:02:55 +01:00
|
|
|
int32_t dest_height = ext_dst->dsc.header.h;
|
|
|
|
|
|
|
|
int32_t x;
|
|
|
|
int32_t y;
|
2019-11-01 06:00:04 +01:00
|
|
|
lv_point_t point_p;
|
2019-03-20 09:02:55 +01:00
|
|
|
|
2019-11-01 06:00:04 +01:00
|
|
|
lv_point_t pivot_p;
|
|
|
|
pivot_p.x = pivot_x;
|
|
|
|
pivot_p.y = pivot_y;
|
2019-03-20 09:02:55 +01:00
|
|
|
|
2019-11-01 06:00:04 +01:00
|
|
|
lv_color_t color_res;
|
|
|
|
lv_opa_t opa_res;
|
2019-03-20 09:02:55 +01:00
|
|
|
|
2019-11-01 06:00:04 +01:00
|
|
|
bool ret;
|
2019-03-27 00:05:15 +01:00
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
for(x = -offset_x; x < dest_width - offset_x; x++) {
|
|
|
|
for(y = -offset_y; y < dest_height - offset_y; y++) {
|
2019-11-01 06:00:04 +01:00
|
|
|
point_p.x = x;
|
|
|
|
point_p.y = y;
|
2019-03-27 00:05:15 +01:00
|
|
|
|
2019-11-01 06:00:04 +01:00
|
|
|
ret = lv_img_get_px_rotated(img, angle, style->image.color, &point_p, &pivot_p, &color_res, &opa_res);
|
|
|
|
if(ret == false) continue;
|
2019-03-20 09:02:55 +01:00
|
|
|
|
2019-06-06 06:05:40 +02:00
|
|
|
if(x + offset_x >= 0 && x + offset_x < dest_width && y + offset_y >= 0 && y + offset_y < dest_height) {
|
2019-03-27 00:05:15 +01:00
|
|
|
/*If the image has no alpha channel just simple set the result color on the canvas*/
|
|
|
|
if(lv_img_color_format_has_alpha(img->header.cf) == false) {
|
|
|
|
lv_img_buf_set_px_color(&ext_dst->dsc, x + offset_x, y + offset_y, color_res);
|
|
|
|
} else {
|
2019-11-01 06:00:04 +01:00
|
|
|
lv_color_t bg_color = lv_img_buf_get_px_color(&ext_dst->dsc, x + offset_x, y + offset_y, style->image.color);
|
2019-03-27 00:05:15 +01:00
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
/*If the canvas has no alpha but the image has mix the image's color with
|
|
|
|
* canvas*/
|
2019-03-27 00:05:15 +01:00
|
|
|
if(lv_img_color_format_has_alpha(ext_dst->dsc.header.cf) == false) {
|
2019-06-06 06:05:40 +02:00
|
|
|
if(opa_res < LV_OPA_MAX) color_res = lv_color_mix(color_res, bg_color, opa_res);
|
|
|
|
lv_img_buf_set_px_color(&ext_dst->dsc, x + offset_x, y + offset_y, color_res);
|
2019-03-27 00:05:15 +01:00
|
|
|
}
|
2019-04-04 07:15:40 +02:00
|
|
|
/*Both the image and canvas has alpha channel. Some extra calculation is
|
|
|
|
required*/
|
2019-03-27 00:05:15 +01:00
|
|
|
else {
|
2019-06-06 06:05:40 +02:00
|
|
|
lv_opa_t bg_opa = lv_img_buf_get_px_alpha(&ext_dst->dsc, x + offset_x, y + offset_y);
|
2019-04-04 07:15:40 +02:00
|
|
|
/* Pick the foreground if it's fully opaque or the Background is fully
|
|
|
|
* transparent*/
|
2019-03-27 00:05:15 +01:00
|
|
|
if(opa_res >= LV_OPA_MAX || bg_opa <= LV_OPA_MIN) {
|
2019-06-06 06:05:40 +02:00
|
|
|
lv_img_buf_set_px_color(&ext_dst->dsc, x + offset_x, y + offset_y, color_res);
|
|
|
|
lv_img_buf_set_px_alpha(&ext_dst->dsc, x + offset_x, y + offset_y, opa_res);
|
2019-03-27 00:05:15 +01:00
|
|
|
}
|
|
|
|
/*Opaque background: use simple mix*/
|
|
|
|
else if(bg_opa >= LV_OPA_MAX) {
|
2019-04-04 07:15:40 +02:00
|
|
|
lv_img_buf_set_px_color(&ext_dst->dsc, x + offset_x, y + offset_y,
|
|
|
|
lv_color_mix(color_res, bg_color, opa_res));
|
2019-03-27 00:05:15 +01:00
|
|
|
}
|
|
|
|
/*Both colors have alpha. Expensive calculation need to be applied*/
|
|
|
|
else {
|
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
/*Info:
|
|
|
|
* https://en.wikipedia.org/wiki/Alpha_compositing#Analytical_derivation_of_the_over_operator*/
|
2019-06-06 06:05:40 +02:00
|
|
|
lv_opa_t opa_res_2 = 255 - ((uint16_t)((uint16_t)(255 - opa_res) * (255 - bg_opa)) >> 8);
|
2019-03-27 00:05:15 +01:00
|
|
|
if(opa_res_2 == 0) {
|
2019-04-04 07:15:40 +02:00
|
|
|
opa_res_2 = 1; /*never happens, just to be sure*/
|
2019-03-27 00:05:15 +01:00
|
|
|
}
|
2019-04-04 07:15:40 +02:00
|
|
|
lv_opa_t ratio = (uint16_t)((uint16_t)opa_res * 255) / opa_res_2;
|
2019-03-27 00:05:15 +01:00
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
lv_img_buf_set_px_color(&ext_dst->dsc, x + offset_x, y + offset_y,
|
|
|
|
lv_color_mix(color_res, bg_color, ratio));
|
2019-06-06 06:05:40 +02:00
|
|
|
lv_img_buf_set_px_alpha(&ext_dst->dsc, x + offset_x, y + offset_y, opa_res_2);
|
2019-03-27 00:05:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-20 09:02:55 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-20 08:01:01 +01:00
|
|
|
}
|
|
|
|
|
2019-03-24 19:35:10 +01:00
|
|
|
lv_obj_invalidate(canvas);
|
2019-03-20 08:01:01 +01:00
|
|
|
}
|
|
|
|
|
2019-01-25 14:14:22 +03:00
|
|
|
/**
|
2019-06-02 20:35:33 +02:00
|
|
|
* Fill the canvas with color
|
|
|
|
* @param canvas pointer to a canvas
|
|
|
|
* @param color the background color
|
2019-01-25 14:14:22 +03:00
|
|
|
*/
|
2019-06-02 20:35:33 +02:00
|
|
|
void lv_canvas_fill_bg(lv_obj_t * canvas, lv_color_t color)
|
2019-01-25 14:14:22 +03:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(canvas, LV_OBJX_NAME);
|
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
|
2019-01-25 14:14:22 +03:00
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
uint32_t x = dsc->header.w * dsc->header.h;
|
|
|
|
uint32_t y;
|
|
|
|
for(y = 0; y < dsc->header.h; y++) {
|
|
|
|
for(x = 0; x < dsc->header.w; x++) {
|
|
|
|
lv_img_buf_set_px_color(dsc, x, y, color);
|
2019-01-25 14:14:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-02 20:35:33 +02:00
|
|
|
* Draw a rectangle on the canvas
|
2019-01-25 14:14:22 +03:00
|
|
|
* @param canvas pointer to a canvas object
|
2019-06-02 20:35:33 +02:00
|
|
|
* @param x left coordinate of the rectangle
|
|
|
|
* @param y top coordinate of the rectangle
|
|
|
|
* @param w width of the rectangle
|
|
|
|
* @param h height of the rectangle
|
|
|
|
* @param style style of the rectangle (`body` properties are used except `padding`)
|
2019-01-25 14:14:22 +03:00
|
|
|
*/
|
2019-06-06 06:05:40 +02:00
|
|
|
void lv_canvas_draw_rect(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y, lv_coord_t w, lv_coord_t h,
|
|
|
|
const lv_style_t * style)
|
2019-01-25 14:14:22 +03:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(canvas, LV_OBJX_NAME);
|
|
|
|
LV_ASSERT_NULL(style);
|
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
|
2019-03-20 09:02:55 +01:00
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
/* Create a dummy display to fool the lv_draw function.
|
|
|
|
* It will think it draws to real screen. */
|
|
|
|
lv_area_t mask;
|
|
|
|
mask.x1 = 0;
|
|
|
|
mask.x2 = dsc->header.w - 1;
|
|
|
|
mask.y1 = 0;
|
|
|
|
mask.y2 = dsc->header.h - 1;
|
2019-03-20 09:02:55 +01:00
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_area_t coords;
|
|
|
|
coords.x1 = x;
|
|
|
|
coords.y1 = y;
|
|
|
|
coords.x2 = x + w - 1;
|
|
|
|
coords.y2 = y + h - 1;
|
2019-03-20 09:02:55 +01:00
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_disp_t disp;
|
|
|
|
memset(&disp, 0, sizeof(lv_disp_t));
|
2019-03-20 09:02:55 +01:00
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_disp_buf_t disp_buf;
|
2019-06-06 06:05:40 +02:00
|
|
|
lv_disp_buf_init(&disp_buf, (void *)dsc->data, NULL, dsc->header.w * dsc->header.h);
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_area_copy(&disp_buf.area, &mask);
|
2019-01-25 14:14:22 +03:00
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_disp_drv_init(&disp.driver);
|
|
|
|
|
2019-06-06 06:05:40 +02:00
|
|
|
disp.driver.buffer = &disp_buf;
|
2019-06-02 20:35:33 +02:00
|
|
|
disp.driver.hor_res = dsc->header.w;
|
|
|
|
disp.driver.ver_res = dsc->header.h;
|
|
|
|
|
2019-09-08 15:25:20 +02:00
|
|
|
set_set_px_cb(&disp.driver, dsc->header.cf);
|
|
|
|
|
2019-08-15 09:24:22 -04:00
|
|
|
#if LV_ANTIALIAS
|
2019-09-16 10:58:28 +02:00
|
|
|
|
2019-07-25 11:36:11 +02:00
|
|
|
/*Disable anti-aliasing if drawing with transparent color to chroma keyed canvas*/
|
|
|
|
lv_color_t ctransp = LV_COLOR_TRANSP;
|
|
|
|
if(dsc->header.cf == LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED &&
|
|
|
|
style->body.main_color.full == ctransp.full &&
|
|
|
|
style->body.grad_color.full == ctransp.full)
|
|
|
|
{
|
|
|
|
disp.driver.antialiasing = 0;
|
|
|
|
}
|
2019-08-15 09:24:22 -04:00
|
|
|
#endif
|
2019-07-25 11:36:11 +02:00
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_disp_t * refr_ori = lv_refr_get_disp_refreshing();
|
|
|
|
lv_refr_set_disp_refreshing(&disp);
|
|
|
|
|
|
|
|
lv_draw_rect(&coords, &mask, style, LV_OPA_COVER);
|
|
|
|
|
|
|
|
lv_refr_set_disp_refreshing(refr_ori);
|
2019-01-25 14:14:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-02 20:35:33 +02:00
|
|
|
* Draw a text on the canvas.
|
2019-01-25 14:14:22 +03:00
|
|
|
* @param canvas pointer to a canvas object
|
2019-06-02 20:35:33 +02:00
|
|
|
* @param x left coordinate of the text
|
|
|
|
* @param y top coordinate of the text
|
|
|
|
* @param max_w max width of the text. The text will be wrapped to fit into this size
|
|
|
|
* @param style style of the text (`text` properties are used)
|
|
|
|
* @param txt text to display
|
|
|
|
* @param align align of the text (`LV_LABEL_ALIGN_LEFT/RIGHT/CENTER`)
|
2019-01-25 14:14:22 +03:00
|
|
|
*/
|
2019-06-06 06:05:40 +02:00
|
|
|
void lv_canvas_draw_text(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y, lv_coord_t max_w, const lv_style_t * style,
|
|
|
|
const char * txt, lv_label_align_t align)
|
2019-03-24 09:52:24 -04:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(canvas, LV_OBJX_NAME);
|
|
|
|
LV_ASSERT_NULL(style);
|
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
|
|
|
|
|
|
|
|
/* Create a dummy display to fool the lv_draw function.
|
|
|
|
* It will think it draws to real screen. */
|
|
|
|
lv_area_t mask;
|
|
|
|
mask.x1 = 0;
|
|
|
|
mask.x2 = dsc->header.w - 1;
|
|
|
|
mask.y1 = 0;
|
|
|
|
mask.y2 = dsc->header.h - 1;
|
|
|
|
|
|
|
|
lv_area_t coords;
|
|
|
|
coords.x1 = x;
|
|
|
|
coords.y1 = y;
|
|
|
|
coords.x2 = x + max_w - 1;
|
|
|
|
coords.y2 = dsc->header.h - 1;
|
|
|
|
|
|
|
|
lv_disp_t disp;
|
|
|
|
memset(&disp, 0, sizeof(lv_disp_t));
|
|
|
|
|
|
|
|
lv_disp_buf_t disp_buf;
|
2019-06-06 06:05:40 +02:00
|
|
|
lv_disp_buf_init(&disp_buf, (void *)dsc->data, NULL, dsc->header.w * dsc->header.h);
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_area_copy(&disp_buf.area, &mask);
|
|
|
|
|
|
|
|
lv_disp_drv_init(&disp.driver);
|
|
|
|
|
2019-06-06 06:05:40 +02:00
|
|
|
disp.driver.buffer = &disp_buf;
|
2019-06-02 20:35:33 +02:00
|
|
|
disp.driver.hor_res = dsc->header.w;
|
|
|
|
disp.driver.ver_res = dsc->header.h;
|
|
|
|
|
2019-09-08 15:25:20 +02:00
|
|
|
set_set_px_cb(&disp.driver, dsc->header.cf);
|
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_disp_t * refr_ori = lv_refr_get_disp_refreshing();
|
|
|
|
lv_refr_set_disp_refreshing(&disp);
|
|
|
|
|
|
|
|
lv_txt_flag_t flag;
|
|
|
|
switch(align) {
|
2019-06-06 06:05:40 +02:00
|
|
|
case LV_LABEL_ALIGN_LEFT: flag = LV_TXT_FLAG_NONE; break;
|
|
|
|
case LV_LABEL_ALIGN_RIGHT: flag = LV_TXT_FLAG_RIGHT; break;
|
|
|
|
case LV_LABEL_ALIGN_CENTER: flag = LV_TXT_FLAG_CENTER; break;
|
|
|
|
default: flag = LV_TXT_FLAG_NONE; break;
|
2019-06-02 20:35:33 +02:00
|
|
|
}
|
|
|
|
|
2019-10-30 10:16:06 +01:00
|
|
|
lv_draw_label(&coords, &mask, style, LV_OPA_COVER, txt, flag, NULL, NULL, NULL);
|
2019-06-02 20:35:33 +02:00
|
|
|
|
|
|
|
lv_refr_set_disp_refreshing(refr_ori);
|
2019-01-25 14:14:22 +03:00
|
|
|
}
|
|
|
|
|
2019-06-17 16:05:30 +02:00
|
|
|
/**
|
|
|
|
* Draw an image on the canvas
|
|
|
|
* @param canvas pointer to a canvas object
|
|
|
|
* @param src image source. Can be a pointer an `lv_img_dsc_t` variable or a path an image.
|
|
|
|
* @param style style of the image (`image` properties are used)
|
|
|
|
*/
|
|
|
|
void lv_canvas_draw_img(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y, const void * src, const lv_style_t * style)
|
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(canvas, LV_OBJX_NAME);
|
|
|
|
LV_ASSERT_NULL(style);
|
|
|
|
|
2019-06-17 16:05:30 +02:00
|
|
|
lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
|
|
|
|
|
|
|
|
/* Create a dummy display to fool the lv_draw function.
|
|
|
|
* It will think it draws to real screen. */
|
|
|
|
lv_area_t mask;
|
|
|
|
mask.x1 = 0;
|
|
|
|
mask.x2 = dsc->header.w - 1;
|
|
|
|
mask.y1 = 0;
|
|
|
|
mask.y2 = dsc->header.h - 1;
|
|
|
|
|
|
|
|
lv_img_header_t header;
|
|
|
|
lv_res_t res = lv_img_decoder_get_info(src, &header);
|
|
|
|
if(res != LV_RES_OK) {
|
|
|
|
LV_LOG_WARN("lv_canvas_draw_img: Couldn't get the image data.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lv_area_t coords;
|
|
|
|
coords.x1 = x;
|
|
|
|
coords.y1 = y;
|
|
|
|
coords.x2 = x + header.w - 1;
|
|
|
|
coords.y2 = y + header.h - 1;
|
|
|
|
|
|
|
|
lv_disp_t disp;
|
|
|
|
memset(&disp, 0, sizeof(lv_disp_t));
|
|
|
|
|
|
|
|
lv_disp_buf_t disp_buf;
|
|
|
|
lv_disp_buf_init(&disp_buf, (void *)dsc->data, NULL, dsc->header.w * dsc->header.h);
|
|
|
|
lv_area_copy(&disp_buf.area, &mask);
|
|
|
|
|
|
|
|
lv_disp_drv_init(&disp.driver);
|
|
|
|
|
|
|
|
disp.driver.buffer = &disp_buf;
|
|
|
|
disp.driver.hor_res = dsc->header.w;
|
|
|
|
disp.driver.ver_res = dsc->header.h;
|
|
|
|
|
2019-09-08 15:25:20 +02:00
|
|
|
set_set_px_cb(&disp.driver, dsc->header.cf);
|
|
|
|
|
2019-06-17 16:05:30 +02:00
|
|
|
lv_disp_t * refr_ori = lv_refr_get_disp_refreshing();
|
|
|
|
lv_refr_set_disp_refreshing(&disp);
|
|
|
|
|
|
|
|
lv_draw_img(&coords, &mask, src, style, LV_OPA_COVER);
|
|
|
|
|
2019-06-19 00:40:20 +02:00
|
|
|
lv_refr_set_disp_refreshing(refr_ori);
|
2019-06-17 16:05:30 +02:00
|
|
|
}
|
|
|
|
|
2019-01-25 14:14:22 +03:00
|
|
|
/**
|
2019-06-02 20:35:33 +02:00
|
|
|
* Draw a line on the canvas
|
2019-01-25 14:14:22 +03:00
|
|
|
* @param canvas pointer to a canvas object
|
2019-06-02 20:35:33 +02:00
|
|
|
* @param points point of the line
|
|
|
|
* @param point_cnt number of points
|
|
|
|
* @param style style of the line (`line` properties are used)
|
2019-01-25 14:14:22 +03:00
|
|
|
*/
|
2019-06-02 20:35:33 +02:00
|
|
|
void lv_canvas_draw_line(lv_obj_t * canvas, const lv_point_t * points, uint32_t point_cnt, const lv_style_t * style)
|
2019-03-24 09:52:24 -04:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(canvas, LV_OBJX_NAME);
|
|
|
|
LV_ASSERT_NULL(style);
|
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
|
|
|
|
|
|
|
|
/* Create a dummy display to fool the lv_draw function.
|
|
|
|
* It will think it draws to real screen. */
|
|
|
|
lv_area_t mask;
|
|
|
|
mask.x1 = 0;
|
|
|
|
mask.x2 = dsc->header.w - 1;
|
|
|
|
mask.y1 = 0;
|
|
|
|
mask.y2 = dsc->header.h - 1;
|
|
|
|
|
|
|
|
lv_disp_t disp;
|
|
|
|
memset(&disp, 0, sizeof(lv_disp_t));
|
|
|
|
|
|
|
|
lv_disp_buf_t disp_buf;
|
2019-06-06 06:05:40 +02:00
|
|
|
lv_disp_buf_init(&disp_buf, (void *)dsc->data, NULL, dsc->header.w * dsc->header.h);
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_area_copy(&disp_buf.area, &mask);
|
|
|
|
|
|
|
|
lv_disp_drv_init(&disp.driver);
|
|
|
|
|
2019-06-06 06:05:40 +02:00
|
|
|
disp.driver.buffer = &disp_buf;
|
2019-06-02 20:35:33 +02:00
|
|
|
disp.driver.hor_res = dsc->header.w;
|
|
|
|
disp.driver.ver_res = dsc->header.h;
|
2019-01-25 14:14:22 +03:00
|
|
|
|
2019-09-08 15:25:20 +02:00
|
|
|
set_set_px_cb(&disp.driver, dsc->header.cf);
|
|
|
|
|
2019-08-15 09:24:22 -04:00
|
|
|
#if LV_ANTIALIAS
|
2019-07-25 11:36:11 +02:00
|
|
|
/*Disable anti-aliasing if drawing with transparent color to chroma keyed canvas*/
|
|
|
|
lv_color_t ctransp = LV_COLOR_TRANSP;
|
|
|
|
if(dsc->header.cf == LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED &&
|
|
|
|
style->body.main_color.full == ctransp.full &&
|
|
|
|
style->body.grad_color.full == ctransp.full)
|
|
|
|
{
|
|
|
|
disp.driver.antialiasing = 0;
|
|
|
|
}
|
2019-08-15 09:24:22 -04:00
|
|
|
#endif
|
2019-07-25 11:36:11 +02:00
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_disp_t * refr_ori = lv_refr_get_disp_refreshing();
|
|
|
|
lv_refr_set_disp_refreshing(&disp);
|
|
|
|
|
2019-10-06 11:46:44 +07:00
|
|
|
lv_style_t circle_style_tmp; /*If rounded...*/
|
2019-10-09 23:11:16 +07:00
|
|
|
if(style->line.rounded) {
|
|
|
|
lv_style_copy(&circle_style_tmp, style);
|
|
|
|
circle_style_tmp.body.radius = LV_RADIUS_CIRCLE;
|
|
|
|
circle_style_tmp.body.main_color = style->line.color;
|
|
|
|
circle_style_tmp.body.grad_color = style->line.color;
|
|
|
|
circle_style_tmp.body.opa = style->line.opa;
|
|
|
|
}
|
2019-10-06 11:46:44 +07:00
|
|
|
lv_area_t circle_area;
|
2019-06-02 20:35:33 +02:00
|
|
|
uint32_t i;
|
|
|
|
for(i = 0; i < point_cnt - 1; i++) {
|
|
|
|
lv_draw_line(&points[i], &points[i + 1], &mask, style, LV_OPA_COVER);
|
2019-10-06 11:46:44 +07:00
|
|
|
|
|
|
|
/*Draw circle on the joints if enabled*/
|
|
|
|
if(style->line.rounded) {
|
|
|
|
circle_area.x1 = points[i].x - ((style->line.width - 1) >> 1) - ((style->line.width - 1) & 0x1);
|
|
|
|
circle_area.y1 = points[i].y - ((style->line.width - 1) >> 1) - ((style->line.width - 1) & 0x1);
|
|
|
|
circle_area.x2 = points[i].x + ((style->line.width - 1) >> 1);
|
|
|
|
circle_area.y2 = points[i].y + ((style->line.width - 1) >> 1);
|
|
|
|
lv_draw_rect(&circle_area, &mask, &circle_style_tmp, LV_OPA_COVER);
|
|
|
|
}
|
2019-03-20 09:02:55 +01:00
|
|
|
}
|
2019-10-06 11:46:44 +07:00
|
|
|
/*Draw circle on the last point too if enabled*/
|
|
|
|
if(style->line.rounded) {
|
|
|
|
circle_area.x1 = points[i].x - ((style->line.width - 1) >> 1) - ((style->line.width - 1) & 0x1);
|
|
|
|
circle_area.y1 = points[i].y - ((style->line.width - 1) >> 1) - ((style->line.width - 1) & 0x1);
|
|
|
|
circle_area.x2 = points[i].x + ((style->line.width - 1) >> 1);
|
|
|
|
circle_area.y2 = points[i].y + ((style->line.width - 1) >> 1);
|
|
|
|
lv_draw_rect(&circle_area, &mask, &circle_style_tmp, LV_OPA_COVER);
|
|
|
|
}
|
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_refr_set_disp_refreshing(refr_ori);
|
2019-01-25 14:14:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-02 20:35:33 +02:00
|
|
|
* Draw a polygon on the canvas
|
2019-01-25 14:14:22 +03:00
|
|
|
* @param canvas pointer to a canvas object
|
2019-06-02 20:35:33 +02:00
|
|
|
* @param points point of the polygon
|
|
|
|
* @param point_cnt number of points
|
|
|
|
* @param style style of the polygon (`body.main_color` and `body.opa` is used)
|
2019-01-25 14:14:22 +03:00
|
|
|
*/
|
2019-06-02 20:35:33 +02:00
|
|
|
void lv_canvas_draw_polygon(lv_obj_t * canvas, const lv_point_t * points, uint32_t point_cnt, const lv_style_t * style)
|
2019-01-25 14:14:22 +03:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(canvas, LV_OBJX_NAME);
|
|
|
|
LV_ASSERT_NULL(style);
|
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
|
2019-01-25 14:14:22 +03:00
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
/* Create a dummy display to fool the lv_draw function.
|
|
|
|
* It will think it draws to real screen. */
|
|
|
|
lv_area_t mask;
|
|
|
|
mask.x1 = 0;
|
|
|
|
mask.x2 = dsc->header.w - 1;
|
|
|
|
mask.y1 = 0;
|
|
|
|
mask.y2 = dsc->header.h - 1;
|
2019-01-25 14:14:22 +03:00
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_disp_t disp;
|
|
|
|
memset(&disp, 0, sizeof(lv_disp_t));
|
2019-01-25 14:14:22 +03:00
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_disp_buf_t disp_buf;
|
2019-06-06 06:05:40 +02:00
|
|
|
lv_disp_buf_init(&disp_buf, (void *)dsc->data, NULL, dsc->header.w * dsc->header.h);
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_area_copy(&disp_buf.area, &mask);
|
2019-01-25 14:14:22 +03:00
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_disp_drv_init(&disp.driver);
|
2019-01-25 14:14:22 +03:00
|
|
|
|
2019-06-06 06:05:40 +02:00
|
|
|
disp.driver.buffer = &disp_buf;
|
2019-06-02 20:35:33 +02:00
|
|
|
disp.driver.hor_res = dsc->header.w;
|
|
|
|
disp.driver.ver_res = dsc->header.h;
|
2019-01-25 14:14:22 +03:00
|
|
|
|
2019-09-08 15:25:20 +02:00
|
|
|
set_set_px_cb(&disp.driver, dsc->header.cf);
|
|
|
|
|
2019-08-15 09:24:22 -04:00
|
|
|
#if LV_ANTIALIAS
|
2019-07-25 11:36:11 +02:00
|
|
|
/*Disable anti-aliasing if drawing with transparent color to chroma keyed canvas*/
|
|
|
|
lv_color_t ctransp = LV_COLOR_TRANSP;
|
|
|
|
if(dsc->header.cf == LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED &&
|
|
|
|
style->body.main_color.full == ctransp.full &&
|
|
|
|
style->body.grad_color.full == ctransp.full)
|
|
|
|
{
|
|
|
|
disp.driver.antialiasing = 0;
|
|
|
|
}
|
2019-08-15 09:24:22 -04:00
|
|
|
#endif
|
2019-07-25 11:36:11 +02:00
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_disp_t * refr_ori = lv_refr_get_disp_refreshing();
|
|
|
|
lv_refr_set_disp_refreshing(&disp);
|
2019-01-25 14:14:22 +03:00
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_draw_polygon(points, point_cnt, &mask, style, LV_OPA_COVER);
|
|
|
|
|
|
|
|
lv_refr_set_disp_refreshing(refr_ori);
|
2019-01-25 14:14:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-02 20:35:33 +02:00
|
|
|
* Draw an arc on the canvas
|
2019-01-25 14:14:22 +03:00
|
|
|
* @param canvas pointer to a canvas object
|
2019-06-02 20:35:33 +02:00
|
|
|
* @param x origo x of the arc
|
|
|
|
* @param y origo y of the arc
|
|
|
|
* @param r radius of the arc
|
|
|
|
* @param start_angle start angle in degrees
|
|
|
|
* @param end_angle end angle in degrees
|
|
|
|
* @param style style of the polygon (`body.main_color` and `body.opa` is used)
|
2019-01-25 14:14:22 +03:00
|
|
|
*/
|
2019-06-06 06:05:40 +02:00
|
|
|
void lv_canvas_draw_arc(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y, lv_coord_t r, int32_t start_angle,
|
|
|
|
int32_t end_angle, const lv_style_t * style)
|
2019-01-25 14:14:22 +03:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(canvas, LV_OBJX_NAME);
|
|
|
|
LV_ASSERT_NULL(style);
|
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
|
2019-01-25 14:14:22 +03:00
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
/* Create a dummy display to fool the lv_draw function.
|
|
|
|
* It will think it draws to real screen. */
|
|
|
|
lv_area_t mask;
|
|
|
|
mask.x1 = 0;
|
|
|
|
mask.x2 = dsc->header.w - 1;
|
|
|
|
mask.y1 = 0;
|
|
|
|
mask.y2 = dsc->header.h - 1;
|
2019-01-25 14:14:22 +03:00
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_disp_t disp;
|
|
|
|
memset(&disp, 0, sizeof(lv_disp_t));
|
2019-01-25 14:14:22 +03:00
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_disp_buf_t disp_buf;
|
2019-06-06 06:05:40 +02:00
|
|
|
lv_disp_buf_init(&disp_buf, (void *)dsc->data, NULL, dsc->header.w * dsc->header.h);
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_area_copy(&disp_buf.area, &mask);
|
|
|
|
|
|
|
|
lv_disp_drv_init(&disp.driver);
|
|
|
|
|
2019-06-06 06:05:40 +02:00
|
|
|
disp.driver.buffer = &disp_buf;
|
2019-06-02 20:35:33 +02:00
|
|
|
disp.driver.hor_res = dsc->header.w;
|
|
|
|
disp.driver.ver_res = dsc->header.h;
|
|
|
|
|
2019-09-08 15:25:20 +02:00
|
|
|
set_set_px_cb(&disp.driver, dsc->header.cf);
|
|
|
|
|
2019-08-15 09:24:22 -04:00
|
|
|
#if LV_ANTIALIAS
|
2019-07-25 11:36:11 +02:00
|
|
|
/*Disable anti-aliasing if drawing with transparent color to chroma keyed canvas*/
|
|
|
|
lv_color_t ctransp = LV_COLOR_TRANSP;
|
|
|
|
if(dsc->header.cf == LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED &&
|
|
|
|
style->body.main_color.full == ctransp.full &&
|
|
|
|
style->body.grad_color.full == ctransp.full)
|
|
|
|
{
|
|
|
|
disp.driver.antialiasing = 0;
|
|
|
|
}
|
2019-08-15 09:24:22 -04:00
|
|
|
#endif
|
2019-07-25 11:36:11 +02:00
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
lv_disp_t * refr_ori = lv_refr_get_disp_refreshing();
|
|
|
|
lv_refr_set_disp_refreshing(&disp);
|
|
|
|
|
|
|
|
lv_draw_arc(x, y, r, &mask, start_angle, end_angle, style, LV_OPA_COVER);
|
|
|
|
|
|
|
|
lv_refr_set_disp_refreshing(refr_ori);
|
2019-01-25 14:14:22 +03:00
|
|
|
}
|
2019-01-14 19:37:55 +01:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Signal function of the canvas
|
|
|
|
* @param canvas pointer to a canvas object
|
|
|
|
* @param sign a signal type from lv_signal_t enum
|
|
|
|
* @param param pointer to a signal specific variable
|
|
|
|
* @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
|
|
|
|
*/
|
|
|
|
static lv_res_t lv_canvas_signal(lv_obj_t * canvas, lv_signal_t sign, void * param)
|
|
|
|
{
|
|
|
|
lv_res_t res;
|
|
|
|
|
|
|
|
/* Include the ancient signal function */
|
|
|
|
res = ancestor_signal(canvas, sign, param);
|
|
|
|
if(res != LV_RES_OK) return res;
|
2019-09-26 12:54:40 +02:00
|
|
|
if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, LV_OBJX_NAME);
|
2019-01-14 19:37:55 +01:00
|
|
|
|
|
|
|
if(sign == LV_SIGNAL_CLEANUP) {
|
|
|
|
/*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-09-08 15:25:20 +02:00
|
|
|
static void set_set_px_cb(lv_disp_drv_t * disp_drv, lv_img_cf_t cf)
|
|
|
|
{
|
|
|
|
switch(cf) {
|
2019-11-01 06:04:03 +01:00
|
|
|
case LV_IMG_CF_TRUE_COLOR_ALPHA: disp_drv->set_px_cb = set_px_true_color_alpha; break;
|
2019-09-08 15:25:20 +02:00
|
|
|
case LV_IMG_CF_ALPHA_1BIT: disp_drv->set_px_cb = set_px_cb_alpha1; break;
|
|
|
|
case LV_IMG_CF_ALPHA_2BIT: disp_drv->set_px_cb = set_px_cb_alpha2; break;
|
|
|
|
case LV_IMG_CF_ALPHA_4BIT: disp_drv->set_px_cb = set_px_cb_alpha4; break;
|
|
|
|
case LV_IMG_CF_ALPHA_8BIT: disp_drv->set_px_cb = set_px_cb_alpha8; break;
|
|
|
|
default: disp_drv->set_px_cb = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_px_cb_alpha1(lv_disp_drv_t * disp_drv, uint8_t * buf, lv_coord_t buf_w, lv_coord_t x, lv_coord_t y,
|
|
|
|
lv_color_t color, lv_opa_t opa)
|
|
|
|
{
|
|
|
|
if(opa <= LV_OPA_MIN) return;
|
|
|
|
lv_img_dsc_t d;
|
|
|
|
d.data = buf;
|
|
|
|
d.header.w = buf_w;
|
|
|
|
d.header.cf = LV_IMG_CF_ALPHA_1BIT;
|
|
|
|
|
|
|
|
set_px_alpha_generic(&d, x, y, color, opa);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_px_cb_alpha2(lv_disp_drv_t * disp_drv, uint8_t * buf, lv_coord_t buf_w, lv_coord_t x, lv_coord_t y,
|
|
|
|
lv_color_t color, lv_opa_t opa)
|
|
|
|
{
|
|
|
|
if(opa <= LV_OPA_MIN) return;
|
|
|
|
lv_img_dsc_t d;
|
|
|
|
d.data = buf;
|
|
|
|
d.header.w = buf_w;
|
|
|
|
d.header.cf = LV_IMG_CF_ALPHA_2BIT;
|
|
|
|
|
|
|
|
set_px_alpha_generic(&d, x, y, color, opa);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_px_cb_alpha4(lv_disp_drv_t * disp_drv, uint8_t * buf, lv_coord_t buf_w, lv_coord_t x, lv_coord_t y,
|
|
|
|
lv_color_t color, lv_opa_t opa)
|
|
|
|
{
|
|
|
|
if(opa <= LV_OPA_MIN) return;
|
|
|
|
lv_img_dsc_t d;
|
|
|
|
d.data = buf;
|
|
|
|
d.header.w = buf_w;
|
|
|
|
d.header.cf = LV_IMG_CF_ALPHA_4BIT;
|
|
|
|
|
|
|
|
set_px_alpha_generic(&d, x, y, color, opa);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_px_cb_alpha8(lv_disp_drv_t * disp_drv, uint8_t * buf, lv_coord_t buf_w, lv_coord_t x, lv_coord_t y,
|
|
|
|
lv_color_t color, lv_opa_t opa)
|
|
|
|
{
|
|
|
|
if(opa <= LV_OPA_MIN) return;
|
|
|
|
lv_img_dsc_t d;
|
|
|
|
d.data = buf;
|
|
|
|
d.header.w = buf_w;
|
|
|
|
d.header.cf = LV_IMG_CF_ALPHA_8BIT;
|
|
|
|
|
|
|
|
set_px_alpha_generic(&d, x, y, color, opa);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_px_alpha_generic(lv_img_dsc_t * d, lv_coord_t x, lv_coord_t y, lv_color_t color, lv_opa_t opa)
|
|
|
|
{
|
|
|
|
|
|
|
|
d->header.always_zero = 0;
|
|
|
|
d->header.h = LV_VER_RES_MAX;
|
|
|
|
|
|
|
|
uint8_t br = lv_color_brightness(color);
|
|
|
|
if(opa < LV_OPA_MAX) {
|
|
|
|
uint8_t bg = lv_img_buf_get_px_alpha(d, x, y);
|
|
|
|
br = (uint16_t)((uint16_t)br * opa + (bg * (255 - opa))) >> 8; //LV_MATH_MIN(opa + bg, 0xFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
lv_img_buf_set_px_alpha(d, x, y, br);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-01 06:04:03 +01:00
|
|
|
static void set_px_true_color_alpha(lv_disp_drv_t * disp_drv, uint8_t * buf, lv_coord_t buf_w, lv_coord_t x, lv_coord_t y,
|
2019-09-08 15:25:20 +02:00
|
|
|
lv_color_t color, lv_opa_t opa)
|
|
|
|
{
|
|
|
|
if(opa <= LV_OPA_MIN) return;
|
|
|
|
lv_img_dsc_t d;
|
|
|
|
d.data = buf;
|
|
|
|
d.header.always_zero = 0;
|
|
|
|
d.header.h = LV_VER_RES_MAX;
|
|
|
|
d.header.w = buf_w;
|
|
|
|
d.header.cf = LV_IMG_CF_TRUE_COLOR_ALPHA;
|
|
|
|
|
2019-11-01 06:04:03 +01:00
|
|
|
lv_color_t bg_color = lv_img_buf_get_px_color(&d, x, y, LV_COLOR_BLACK);
|
2019-09-08 15:25:20 +02:00
|
|
|
lv_opa_t bg_opa = lv_img_buf_get_px_alpha(&d, x, y);
|
|
|
|
|
|
|
|
lv_opa_t res_opa;
|
|
|
|
lv_color_t res_color;
|
|
|
|
|
|
|
|
lv_color_mix_with_alpha(bg_color, bg_opa, color, opa, &res_color, &res_opa);
|
|
|
|
|
|
|
|
|
|
|
|
lv_img_buf_set_px_alpha(&d, x, y, res_opa);
|
|
|
|
lv_img_buf_set_px_color(&d, x, y, res_color);
|
|
|
|
}
|
|
|
|
|
2019-01-14 19:37:55 +01:00
|
|
|
#endif
|