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 * Set the palette color of a canvas with index format. Valid only for `LV_IMG_CF_INDEXED1/2/4/8`
96 * @param canvas pointer to canvas object
97 * @param id the palette color to set:
98 * - for `LV_IMG_CF_INDEXED1`: 0..1
99 * - for `LV_IMG_CF_INDEXED2`: 0..3
100 * - for `LV_IMG_CF_INDEXED4`: 0..15
101 * - for `LV_IMG_CF_INDEXED8`: 0..255
102 * @param c the color to set
103 */
104 void lv_canvas_set_palette(lv_obj_t * canvas, uint8_t id, lv_color_t c);
105
106 /*=====================
107 * Getter functions
108 *====================*/
109
110 /**
111 * Get the color of a pixel on the canvas
112 * @param canvas
113 * @param x x coordinate of the point to set
114 * @param y x coordinate of the point to set
115 * @return color of the point
116 */
117 lv_color_t lv_canvas_get_px(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y);
118
119 /**
120 * Get the image of the canvas as a pointer to an `lv_img_dsc_t` variable.
121 * @param canvas pointer to a canvas object
122 * @return pointer to the image descriptor.
123 */
124 lv_img_dsc_t * lv_canvas_get_img(lv_obj_t * canvas);
125
126 /*=====================
127 * Other functions
128 *====================*/
129
130 /**
131 * Copy a buffer to the canvas
132 * @param canvas pointer to a canvas object
133 * @param to_copy buffer to copy. The color format has to match with the canvas's buffer color
134 * format
135 * @param x left side of the destination position
136 * @param y top side of the destination position
137 * @param w width of the buffer to copy
138 * @param h height of the buffer to copy
139 */
140 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,
141 lv_coord_t h);
142
143 /**
144 * Transform and image and store the result on a canvas.
145 * @param canvas pointer to a canvas object to store the result of the transformation.
146 * @param img pointer to an image descriptor to transform.
147 * Can be the image descriptor of an other canvas too (`lv_canvas_get_img()`).
148 * @param angle the angle of rotation (0..3600), 0.1 deg resolution
149 * @param zoom zoom factor (256 no zoom);
150 * @param offset_x offset X to tell where to put the result data on destination canvas
151 * @param offset_y offset X to tell where to put the result data on destination canvas
152 * @param pivot_x pivot X of rotation. Relative to the source canvas
153 * Set to `source width / 2` to rotate around the center
154 * @param pivot_y pivot Y of rotation. Relative to the source canvas
155 * Set to `source height / 2` to rotate around the center
156 * @param antialias apply anti-aliasing during the transformation. Looks better but slower.
157 */
158 void lv_canvas_transform(lv_obj_t * canvas, lv_img_dsc_t * img, int16_t angle, uint16_t zoom, lv_coord_t offset_x,
159 lv_coord_t offset_y,
160 int32_t pivot_x, int32_t pivot_y, bool antialias);
161
162 /**
163 * Apply horizontal blur on the canvas
164 * @param canvas pointer to a canvas object
165 * @param area the area to blur. If `NULL` the whole canvas will be blurred.
166 * @param r radius of the blur
167 */
168 void lv_canvas_blur_hor(lv_obj_t * canvas, const lv_area_t * area, uint16_t r);
169
170 /**
171 * Apply vertical blur on the canvas
172 * @param canvas pointer to a canvas object
173 * @param area the area to blur. If `NULL` the whole canvas will be blurred.
174 * @param r radius of the blur
175 */
176 void lv_canvas_blur_ver(lv_obj_t * canvas, const lv_area_t * area, uint16_t r);
177
178 /**
179 * Fill the canvas with color
180 * @param canvas pointer to a canvas
181 * @param color the background color
182 * @param opa the desired opacity
183 */
184 void lv_canvas_fill_bg(lv_obj_t * canvas, lv_color_t color, lv_opa_t opa);
185
186 /**
187 * Draw a rectangle on the canvas
188 * @param canvas pointer to a canvas object
189 * @param x left coordinate of the rectangle
190 * @param y top coordinate of the rectangle
191 * @param w width of the rectangle
192 * @param h height of the rectangle
193 * @param draw_dsc descriptor of the rectangle
194 */
195 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,
196 const lv_draw_rect_dsc_t * draw_dsc);
197
198 /**
199 * Draw a text on the canvas.
200 * @param canvas pointer to a canvas object
201 * @param x left coordinate of the text
202 * @param y top coordinate of the text
203 * @param max_w max width of the text. The text will be wrapped to fit into this size
204 * @param draw_dsc pointer to a valid label descriptor `lv_draw_label_dsc_t`
205 * @param txt text to display
206 */
207 void lv_canvas_draw_text(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y, lv_coord_t max_w,
208 lv_draw_label_dsc_t * draw_dsc, const char * txt);
209
210 /**
211 * Draw an image on the canvas
212 * @param canvas pointer to a canvas object
213 * @param x left coordinate of the image
214 * @param y top coordinate of the image
215 * @param src image source. Can be a pointer an `lv_img_dsc_t` variable or a path an image.
216 * @param draw_dsc pointer to a valid label descriptor `lv_draw_img_dsc_t`
217 */
218 void lv_canvas_draw_img(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y, const void * src,
219 const lv_draw_img_dsc_t * draw_dsc);
220
221 /**
222 * Draw a line on the canvas
223 * @param canvas pointer to a canvas object
224 * @param points point of the line
225 * @param point_cnt number of points
226 * @param draw_dsc pointer to an initialized `lv_draw_line_dsc_t` variable
227 */
228 void lv_canvas_draw_line(lv_obj_t * canvas, const lv_point_t points[], uint32_t point_cnt,
229 const lv_draw_line_dsc_t * draw_dsc);
230
231 /**
232 * Draw a polygon on the canvas
233 * @param canvas pointer to a canvas object
234 * @param points point of the polygon
235 * @param point_cnt number of points
236 * @param draw_dsc pointer to an initialized `lv_draw_rect_dsc_t` variable
237 */
238 void lv_canvas_draw_polygon(lv_obj_t * canvas, const lv_point_t points[], uint32_t point_cnt,
239 const lv_draw_rect_dsc_t * draw_dsc);
240
241 /**
242 * Draw an arc on the canvas
243 * @param canvas pointer to a canvas object
244 * @param x origo x of the arc
245 * @param y origo y of the arc
246 * @param r radius of the arc
247 * @param start_angle start angle in degrees
248 * @param end_angle end angle in degrees
249 * @param draw_dsc pointer to an initialized `lv_draw_line_dsc_t` variable
250 */
251 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,
252 int32_t end_angle, const lv_draw_arc_dsc_t * draw_dsc);
253
254 /**********************
255 * MACROS
256 **********************/
257 #define LV_CANVAS_BUF_SIZE_TRUE_COLOR(w, h) LV_IMG_BUF_SIZE_TRUE_COLOR(w, h)
258 #define LV_CANVAS_BUF_SIZE_TRUE_COLOR_CHROMA_KEYED(w, h) LV_IMG_BUF_SIZE_TRUE_COLOR_CHROMA_KEYED(w, h)
259 #define LV_CANVAS_BUF_SIZE_TRUE_COLOR_ALPHA(w, h) LV_IMG_BUF_SIZE_TRUE_COLOR_ALPHA(w, h)
260
261 /*+ 1: to be sure no fractional row*/
262 #define LV_CANVAS_BUF_SIZE_ALPHA_1BIT(w, h) LV_IMG_BUF_SIZE_ALPHA_1BIT(w, h)
263 #define LV_CANVAS_BUF_SIZE_ALPHA_2BIT(w, h) LV_IMG_BUF_SIZE_ALPHA_2BIT(w, h)
264 #define LV_CANVAS_BUF_SIZE_ALPHA_4BIT(w, h) LV_IMG_BUF_SIZE_ALPHA_4BIT(w, h)
265 #define LV_CANVAS_BUF_SIZE_ALPHA_8BIT(w, h) LV_IMG_BUF_SIZE_ALPHA_8BIT(w, h)
266
267 /*4 * X: for palette*/
268 #define LV_CANVAS_BUF_SIZE_INDEXED_1BIT(w, h) LV_IMG_BUF_SIZE_INDEXED_1BIT(w, h)
269 #define LV_CANVAS_BUF_SIZE_INDEXED_2BIT(w, h) LV_IMG_BUF_SIZE_INDEXED_2BIT(w, h)
270 #define LV_CANVAS_BUF_SIZE_INDEXED_4BIT(w, h) LV_IMG_BUF_SIZE_INDEXED_4BIT(w, h)
271 #define LV_CANVAS_BUF_SIZE_INDEXED_8BIT(w, h) LV_IMG_BUF_SIZE_INDEXED_8BIT(w, h)
272
273 #endif /*LV_USE_CANVAS*/
274
275 #ifdef __cplusplus
276 } /*extern "C"*/
277 #endif
278
279 #endif /*LV_CANVAS_H*/
280