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