1 /**
2  * @file lv_canvas.h
3  *
4  */
5 
6 #ifndef LV_CANVAS_H
7 #define LV_CANVAS_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /*********************
14  *      INCLUDES
15  *********************/
16 #ifdef LV_CONF_INCLUDE_SIMPLE
17 #include "lv_conf.h"
18 #else
19 #include "../../lv_conf.h"
20 #endif
21 
22 #if LV_USE_CANVAS != 0
23 
24 #include "../lv_core/lv_obj.h"
25 #include "../lv_objx/lv_img.h"
26 
27 /*********************
28  *      DEFINES
29  *********************/
30 
31 /**********************
32  *      TYPEDEFS
33  **********************/
34 /*Data of canvas*/
35 typedef struct
36 {
37     lv_img_ext_t img; /*Ext. of ancestor*/
38     /*New data for this type */
39     lv_img_dsc_t dsc;
40 } lv_canvas_ext_t;
41 
42 /*Styles*/
43 enum {
44     LV_CANVAS_STYLE_MAIN,
45 };
46 typedef uint8_t lv_canvas_style_t;
47 
48 /**********************
49  * GLOBAL PROTOTYPES
50  **********************/
51 
52 /**
53  * Create a canvas object
54  * @param par pointer to an object, it will be the parent of the new canvas
55  * @param copy pointer to a canvas object, if not NULL then the new object will be copied from it
56  * @return pointer to the created canvas
57  */
58 lv_obj_t * lv_canvas_create(lv_obj_t * par, const lv_obj_t * copy);
59 
60 /*=====================
61  * Setter functions
62  *====================*/
63 
64 /**
65  * Set a buffer for the canvas.
66  * @param buf a buffer where the content of the canvas will be.
67  * The required size is (lv_img_color_format_get_px_size(cf) * w * h) / 8)
68  * It can be allocated with `lv_mem_alloc()` or
69  * it can be statically allocated array (e.g. static lv_color_t buf[100*50]) or
70  * it can be an address in RAM or external SRAM
71  * @param canvas pointer to a canvas object
72  * @param w width of the canvas
73  * @param h height of the canvas
74  * @param cf color format. `LV_IMG_CF_...`
75  */
76 void lv_canvas_set_buffer(lv_obj_t * canvas, void * buf, lv_coord_t w, lv_coord_t h, lv_img_cf_t cf);
77 
78 /**
79  * Set the color of a pixel on the canvas
80  * @param canvas
81  * @param x x coordinate of the point to set
82  * @param y x coordinate of the point to set
83  * @param c color of the point
84  */
85 void lv_canvas_set_px(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y, lv_color_t c);
86 
87 /**
88  * Set the palette color of a canvas with index format. Valid only for `LV_IMG_CF_INDEXED1/2/4/8`
89  * @param canvas pointer to canvas object
90  * @param id the palette color to set:
91  *   - for `LV_IMG_CF_INDEXED1`: 0..1
92  *   - for `LV_IMG_CF_INDEXED2`: 0..3
93  *   - for `LV_IMG_CF_INDEXED4`: 0..15
94  *   - for `LV_IMG_CF_INDEXED8`: 0..255
95  * @param c the color to set
96  */
97 void lv_canvas_set_palette(lv_obj_t * canvas, uint8_t id, lv_color_t c);
98 
99 /**
100  * Set a style of a canvas.
101  * @param canvas pointer to canvas object
102  * @param type which style should be set
103  * @param style pointer to a style
104  */
105 void lv_canvas_set_style(lv_obj_t * canvas, lv_canvas_style_t type, const lv_style_t * style);
106 
107 /*=====================
108  * Getter functions
109  *====================*/
110 
111 /**
112  * Get the color of a pixel on the canvas
113  * @param canvas
114  * @param x x coordinate of the point to set
115  * @param y x coordinate of the point to set
116  * @return color of the point
117  */
118 lv_color_t lv_canvas_get_px(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y);
119 
120 /**
121  * Get the image of the canvas as a pointer to an `lv_img_dsc_t` variable.
122  * @param canvas pointer to a canvas object
123  * @return pointer to the image descriptor.
124  */
125 lv_img_dsc_t * lv_canvas_get_img(lv_obj_t * canvas);
126 
127 /**
128  * Get style of a canvas.
129  * @param canvas pointer to canvas object
130  * @param type which style should be get
131  * @return style pointer to the style
132  */
133 const lv_style_t * lv_canvas_get_style(const lv_obj_t * canvas, lv_canvas_style_t type);
134 
135 /*=====================
136  * Other functions
137  *====================*/
138 
139 /**
140  * Copy a buffer to the canvas
141  * @param canvas pointer to a canvas object
142  * @param to_copy buffer to copy. The color format has to match with the canvas's buffer color
143  * format
144  * @param x left side of the destination position
145  * @param y top side of the destination position
146  * @param w width of the buffer to copy
147  * @param h height of the buffer to copy
148  */
149 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,
150                         lv_coord_t h);
151 
152 /**
153  * Rotate and image and store the result on a canvas.
154  * @param canvas pointer to a canvas object
155  * @param img pointer to an image descriptor.
156  *             Can be the image descriptor of an other canvas too (`lv_canvas_get_img()`).
157  * @param angle the angle of rotation (0..360);
158  * @param offset_x offset X to tell where to put the result data on destination canvas
159  * @param offset_y offset X to tell where to put the result data on destination canvas
160  * @param pivot_x pivot X of rotation. Relative to the source canvas
161  *                Set to `source width / 2` to rotate around the center
162  * @param pivot_y pivot Y of rotation. Relative to the source canvas
163  *                Set to `source height / 2` to rotate around the center
164  */
165 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,
166                       int32_t pivot_x, int32_t pivot_y);
167 
168 /**
169  * Fill the canvas with color
170  * @param canvas pointer to a canvas
171  * @param color the background color
172  */
173 void lv_canvas_fill_bg(lv_obj_t * canvas, lv_color_t color);
174 
175 /**
176  * Draw a rectangle on the canvas
177  * @param canvas pointer to a canvas object
178  * @param x left coordinate of the rectangle
179  * @param y top coordinate of the rectangle
180  * @param w width of the rectangle
181  * @param h height of the rectangle
182  * @param style style of the rectangle (`body` properties are used except `padding`)
183  */
184 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,
185                          const lv_style_t * style);
186 
187 /**
188  * Draw a text on the canvas.
189  * @param canvas pointer to a canvas object
190  * @param x left coordinate of the text
191  * @param y top coordinate of the text
192  * @param max_w max width of the text. The text will be wrapped to fit into this size
193  * @param style style of the text (`text` properties are used)
194  * @param txt text to display
195  * @param align align of the text (`LV_LABEL_ALIGN_LEFT/RIGHT/CENTER`)
196  */
197 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,
198                          const char * txt, lv_label_align_t align);
199 
200 /**
201  * Draw an image on the canvas
202  * @param canvas pointer to a canvas object
203  * @param src image source. Can be a pointer an `lv_img_dsc_t` variable or a path an image.
204  * @param style style of the image (`image` properties are used)
205  */
206 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);
207 
208 /**
209  * Draw a line on the canvas
210  * @param canvas pointer to a canvas object
211  * @param points point of the line
212  * @param point_cnt number of points
213  * @param style style of the line (`line` properties are used)
214  */
215 void lv_canvas_draw_line(lv_obj_t * canvas, const lv_point_t * points, uint32_t point_cnt, const lv_style_t * style);
216 
217 /**
218  * Draw a polygon on the canvas
219  * @param canvas pointer to a canvas object
220  * @param points point of the polygon
221  * @param point_cnt number of points
222  * @param style style of the polygon (`body.main_color` and `body.opa` is used)
223  */
224 void lv_canvas_draw_polygon(lv_obj_t * canvas, const lv_point_t * points, uint32_t point_cnt, const lv_style_t * style);
225 
226 /**
227  * Draw an arc on the canvas
228  * @param canvas pointer to a canvas object
229  * @param x origo x  of the arc
230  * @param y origo y of the arc
231  * @param r radius of the arc
232  * @param start_angle start angle in degrees
233  * @param end_angle end angle in degrees
234  * @param style style of the polygon (`body.main_color` and `body.opa` is used)
235  */
236 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,
237                         int32_t end_angle, const lv_style_t * style);
238 
239 /**********************
240  *      MACROS
241  **********************/
242 #define LV_CANVAS_BUF_SIZE_TRUE_COLOR(w, h) ((LV_COLOR_SIZE / 8) * w * h)
243 #define LV_CANVAS_BUF_SIZE_TRUE_COLOR_CHROMA_KEYED(w, h) ((LV_COLOR_SIZE / 8) * w * h)
244 #define LV_CANVAS_BUF_SIZE_TRUE_COLOR_ALPHA(w, h) (LV_IMG_PX_SIZE_ALPHA_BYTE * w * h)
245 
246 /*+ 1: to be sure no fractional row*/
247 #define LV_CANVAS_BUF_SIZE_ALPHA_1BIT(w, h) ((((w / 8) + 1) * h))
248 #define LV_CANVAS_BUF_SIZE_ALPHA_2BIT(w, h) ((((w / 4) + 1) * h))
249 #define LV_CANVAS_BUF_SIZE_ALPHA_4BIT(w, h) ((((w / 2) + 1) * h))
250 #define LV_CANVAS_BUF_SIZE_ALPHA_8BIT(w, h) ((w * h))
251 
252 /*4 * X: for palette*/
253 #define LV_CANVAS_BUF_SIZE_INDEXED_1BIT(w, h) (LV_CANVAS_BUF_SIZE_ALPHA_1BIT(w, h) + 4 * 2)
254 #define LV_CANVAS_BUF_SIZE_INDEXED_2BIT(w, h) (LV_CANVAS_BUF_SIZE_ALPHA_2BIT(w, h) + 4 * 4)
255 #define LV_CANVAS_BUF_SIZE_INDEXED_4BIT(w, h) (LV_CANVAS_BUF_SIZE_ALPHA_4BIT(w, h) + 4 * 16)
256 #define LV_CANVAS_BUF_SIZE_INDEXED_8BIT(w, h) (LV_CANVAS_BUF_SIZE_ALPHA_8BIT(w, h) + 4 * 256)
257 
258 #endif /*LV_USE_CANVAS*/
259 
260 #ifdef __cplusplus
261 } /* extern "C" */
262 #endif
263 
264 #endif /*LV_CANVAS_H*/
265