1 /**
2  * @file lv_img_buf.h
3  *
4  */
5 
6 #ifndef LV_IMG_BUF_H
7 #define LV_IMG_BUF_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /*********************
14  *      INCLUDES
15  *********************/
16 #include <stdbool.h>
17 #include "../misc/lv_color.h"
18 #include "../misc/lv_area.h"
19 
20 /*********************
21  *      DEFINES
22  *********************/
23 /*If image pixels contains alpha we need to know how much byte is a pixel*/
24 #if LV_COLOR_DEPTH == 1 || LV_COLOR_DEPTH == 8
25 #define LV_IMG_PX_SIZE_ALPHA_BYTE 2
26 #elif LV_COLOR_DEPTH == 16
27 #define LV_IMG_PX_SIZE_ALPHA_BYTE 3
28 #elif LV_COLOR_DEPTH == 32
29 #define LV_IMG_PX_SIZE_ALPHA_BYTE 4
30 #endif
31 
32 #define LV_IMG_BUF_SIZE_TRUE_COLOR(w, h) ((LV_COLOR_SIZE / 8) * w * h)
33 #define LV_IMG_BUF_SIZE_TRUE_COLOR_CHROMA_KEYED(w, h) ((LV_COLOR_SIZE / 8) * w * h)
34 #define LV_IMG_BUF_SIZE_TRUE_COLOR_ALPHA(w, h) (LV_IMG_PX_SIZE_ALPHA_BYTE * w * h)
35 
36 /*+ 1: to be sure no fractional row*/
37 #define LV_IMG_BUF_SIZE_ALPHA_1BIT(w, h) ((((w / 8) + 1) * h))
38 #define LV_IMG_BUF_SIZE_ALPHA_2BIT(w, h) ((((w / 4) + 1) * h))
39 #define LV_IMG_BUF_SIZE_ALPHA_4BIT(w, h) ((((w / 2) + 1) * h))
40 #define LV_IMG_BUF_SIZE_ALPHA_8BIT(w, h) ((w * h))
41 
42 /*4 * X: for palette*/
43 #define LV_IMG_BUF_SIZE_INDEXED_1BIT(w, h) (LV_IMG_BUF_SIZE_ALPHA_1BIT(w, h) + 4 * 2)
44 #define LV_IMG_BUF_SIZE_INDEXED_2BIT(w, h) (LV_IMG_BUF_SIZE_ALPHA_2BIT(w, h) + 4 * 4)
45 #define LV_IMG_BUF_SIZE_INDEXED_4BIT(w, h) (LV_IMG_BUF_SIZE_ALPHA_4BIT(w, h) + 4 * 16)
46 #define LV_IMG_BUF_SIZE_INDEXED_8BIT(w, h) (LV_IMG_BUF_SIZE_ALPHA_8BIT(w, h) + 4 * 256)
47 
48 #define _LV_ZOOM_INV_UPSCALE 5
49 
50 /**********************
51  *      TYPEDEFS
52  **********************/
53 
54 /*Image color format*/
55 enum {
56     LV_IMG_CF_UNKNOWN = 0,
57 
58     LV_IMG_CF_RAW,              /**< Contains the file as it is. Needs custom decoder function*/
59     LV_IMG_CF_RAW_ALPHA,        /**< Contains the file as it is. The image has alpha. Needs custom decoder
60                                    function*/
61     LV_IMG_CF_RAW_CHROMA_KEYED, /**< Contains the file as it is. The image is chroma keyed. Needs
62                                    custom decoder function*/
63 
64     LV_IMG_CF_TRUE_COLOR,              /**< Color format and depth should match with LV_COLOR settings*/
65     LV_IMG_CF_TRUE_COLOR_ALPHA,        /**< Same as `LV_IMG_CF_TRUE_COLOR` but every pixel has an alpha byte*/
66     LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED, /**< Same as `LV_IMG_CF_TRUE_COLOR` but LV_COLOR_TRANSP pixels
67                                           will be transparent*/
68 
69     LV_IMG_CF_INDEXED_1BIT, /**< Can have 2 different colors in a palette (can't be chroma keyed)*/
70     LV_IMG_CF_INDEXED_2BIT, /**< Can have 4 different colors in a palette (can't be chroma keyed)*/
71     LV_IMG_CF_INDEXED_4BIT, /**< Can have 16 different colors in a palette (can't be chroma keyed)*/
72     LV_IMG_CF_INDEXED_8BIT, /**< Can have 256 different colors in a palette (can't be chroma keyed)*/
73 
74     LV_IMG_CF_ALPHA_1BIT, /**< Can have one color and it can be drawn or not*/
75     LV_IMG_CF_ALPHA_2BIT, /**< Can have one color but 4 different alpha value*/
76     LV_IMG_CF_ALPHA_4BIT, /**< Can have one color but 16 different alpha value*/
77     LV_IMG_CF_ALPHA_8BIT, /**< Can have one color but 256 different alpha value*/
78 
79     LV_IMG_CF_RGB888,
80     LV_IMG_CF_RGBA8888,
81     LV_IMG_CF_RGBX8888,
82     LV_IMG_CF_RGB565,
83     LV_IMG_CF_RGBA5658,
84     LV_IMG_CF_RGB565A8,
85 
86     LV_IMG_CF_RESERVED_15,              /**< Reserved for further use.*/
87     LV_IMG_CF_RESERVED_16,              /**< Reserved for further use.*/
88     LV_IMG_CF_RESERVED_17,              /**< Reserved for further use.*/
89     LV_IMG_CF_RESERVED_18,              /**< Reserved for further use.*/
90     LV_IMG_CF_RESERVED_19,              /**< Reserved for further use.*/
91     LV_IMG_CF_RESERVED_20,              /**< Reserved for further use.*/
92     LV_IMG_CF_RESERVED_21,              /**< Reserved for further use.*/
93     LV_IMG_CF_RESERVED_22,              /**< Reserved for further use.*/
94     LV_IMG_CF_RESERVED_23,              /**< Reserved for further use.*/
95 
96     LV_IMG_CF_USER_ENCODED_0,          /**< User holder encoding format.*/
97     LV_IMG_CF_USER_ENCODED_1,          /**< User holder encoding format.*/
98     LV_IMG_CF_USER_ENCODED_2,          /**< User holder encoding format.*/
99     LV_IMG_CF_USER_ENCODED_3,          /**< User holder encoding format.*/
100     LV_IMG_CF_USER_ENCODED_4,          /**< User holder encoding format.*/
101     LV_IMG_CF_USER_ENCODED_5,          /**< User holder encoding format.*/
102     LV_IMG_CF_USER_ENCODED_6,          /**< User holder encoding format.*/
103     LV_IMG_CF_USER_ENCODED_7,          /**< User holder encoding format.*/
104 };
105 typedef uint8_t lv_img_cf_t;
106 
107 
108 /**
109  * The first 8 bit is very important to distinguish the different source types.
110  * For more info see `lv_img_get_src_type()` in lv_img.c
111  * On big endian systems the order is reversed so cf and always_zero must be at
112  * the end of the struct.
113  */
114 #if LV_BIG_ENDIAN_SYSTEM
115 typedef struct {
116 
117     uint32_t h : 11; /*Height of the image map*/
118     uint32_t w : 11; /*Width of the image map*/
119     uint32_t reserved : 2; /*Reserved to be used later*/
120     uint32_t always_zero : 3; /*It the upper bits of the first byte. Always zero to look like a
121                                  non-printable character*/
122     uint32_t cf : 5;          /*Color format: See `lv_img_color_format_t`*/
123 
124 } lv_img_header_t;
125 #else
126 typedef struct {
127 
128     uint32_t cf : 5;          /*Color format: See `lv_img_color_format_t`*/
129     uint32_t always_zero : 3; /*It the upper bits of the first byte. Always zero to look like a
130                                  non-printable character*/
131 
132     uint32_t reserved : 2; /*Reserved to be used later*/
133 
134     uint32_t w : 11; /*Width of the image map*/
135     uint32_t h : 11; /*Height of the image map*/
136 } lv_img_header_t;
137 #endif
138 
139 /** Image header it is compatible with
140  * the result from image converter utility*/
141 typedef struct {
142     lv_img_header_t header; /**< A header describing the basics of the image*/
143     uint32_t data_size;     /**< Size of the image in bytes*/
144     const uint8_t * data;   /**< Pointer to the data of the image*/
145 } lv_img_dsc_t;
146 
147 /**********************
148  * GLOBAL PROTOTYPES
149  **********************/
150 
151 /**
152  * Allocate an image buffer in RAM
153  * @param w width of image
154  * @param h height of image
155  * @param cf a color format (`LV_IMG_CF_...`)
156  * @return an allocated image, or NULL on failure
157  */
158 lv_img_dsc_t * lv_img_buf_alloc(lv_coord_t w, lv_coord_t h, lv_img_cf_t cf);
159 
160 /**
161  * Get the color of an image's pixel
162  * @param dsc an image descriptor
163  * @param x x coordinate of the point to get
164  * @param y x coordinate of the point to get
165  * @param color the color of the image. In case of `LV_IMG_CF_ALPHA_1/2/4/8` this color is used.
166  * Not used in other cases.
167  * @param safe true: check out of bounds
168  * @return color of the point
169  */
170 lv_color_t lv_img_buf_get_px_color(lv_img_dsc_t * dsc, lv_coord_t x, lv_coord_t y, lv_color_t color);
171 
172 /**
173  * Get the alpha value of an image's pixel
174  * @param dsc pointer to an image descriptor
175  * @param x x coordinate of the point to set
176  * @param y x coordinate of the point to set
177  * @param safe true: check out of bounds
178  * @return alpha value of the point
179  */
180 lv_opa_t lv_img_buf_get_px_alpha(lv_img_dsc_t * dsc, lv_coord_t x, lv_coord_t y);
181 
182 /**
183  * Set the color of a pixel of an image. The alpha channel won't be affected.
184  * @param dsc pointer to an image descriptor
185  * @param x x coordinate of the point to set
186  * @param y x coordinate of the point to set
187  * @param c color of the point
188  * @param safe true: check out of bounds
189  */
190 void lv_img_buf_set_px_color(lv_img_dsc_t * dsc, lv_coord_t x, lv_coord_t y, lv_color_t c);
191 
192 /**
193  * Set the alpha value of a pixel of an image. The color won't be affected
194  * @param dsc pointer to an image descriptor
195  * @param x x coordinate of the point to set
196  * @param y x coordinate of the point to set
197  * @param opa the desired opacity
198  * @param safe true: check out of bounds
199  */
200 void lv_img_buf_set_px_alpha(lv_img_dsc_t * dsc, lv_coord_t x, lv_coord_t y, lv_opa_t opa);
201 
202 /**
203  * Set the palette color of an indexed image. Valid only for `LV_IMG_CF_INDEXED1/2/4/8`
204  * @param dsc pointer to an image descriptor
205  * @param id the palette color to set:
206  *   - for `LV_IMG_CF_INDEXED1`: 0..1
207  *   - for `LV_IMG_CF_INDEXED2`: 0..3
208  *   - for `LV_IMG_CF_INDEXED4`: 0..15
209  *   - for `LV_IMG_CF_INDEXED8`: 0..255
210  * @param c the color to set
211  */
212 void lv_img_buf_set_palette(lv_img_dsc_t * dsc, uint8_t id, lv_color_t c);
213 
214 /**
215  * Free an allocated image buffer
216  * @param dsc image buffer to free
217  */
218 void lv_img_buf_free(lv_img_dsc_t * dsc);
219 
220 /**
221  * Get the memory consumption of a raw bitmap, given color format and dimensions.
222  * @param w width
223  * @param h height
224  * @param cf color format
225  * @return size in bytes
226  */
227 uint32_t lv_img_buf_get_img_size(lv_coord_t w, lv_coord_t h, lv_img_cf_t cf);
228 
229 /**
230  * Get the area of a rectangle if its rotated and scaled
231  * @param res store the coordinates here
232  * @param w width of the rectangle to transform
233  * @param h height of the rectangle to transform
234  * @param angle angle of rotation
235  * @param zoom zoom, (256 no zoom)
236  * @param pivot x,y pivot coordinates of rotation
237  */
238 void _lv_img_buf_get_transformed_area(lv_area_t * res, lv_coord_t w, lv_coord_t h, int16_t angle, uint16_t zoom,
239                                       const lv_point_t * pivot);
240 
241 /**********************
242  *      MACROS
243  **********************/
244 
245 #ifdef __cplusplus
246 } /*extern "C"*/
247 #endif
248 
249 #endif /*LV_IMG_BUF_H*/
250