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