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  * The first 8 bit is very important to distinguish the different source types.
109  * For more info see `lv_img_get_src_type()` in lv_img.c
110  * On big endian systems the order is reversed so cf and always_zero must be at
111  * the end of the struct.
112  */
113 #if LV_BIG_ENDIAN_SYSTEM
114 typedef struct {
115 
116     uint32_t h : 11; /*Height of the image map*/
117     uint32_t w : 11; /*Width of the image map*/
118     uint32_t reserved : 2; /*Reserved to be used later*/
119     uint32_t always_zero : 3; /*It the upper bits of the first byte. Always zero to look like a
120                                  non-printable character*/
121     uint32_t cf : 5;          /*Color format: See `lv_img_color_format_t`*/
122 
123 } lv_img_header_t;
124 #else
125 typedef struct {
126 
127     uint32_t cf : 5;          /*Color format: See `lv_img_color_format_t`*/
128     uint32_t always_zero : 3; /*It the upper bits of the first byte. Always zero to look like a
129                                  non-printable character*/
130 
131     uint32_t reserved : 2; /*Reserved to be used later*/
132 
133     uint32_t w : 11; /*Width of the image map*/
134     uint32_t h : 11; /*Height of the image map*/
135 } lv_img_header_t;
136 #endif
137 
138 /** Image header it is compatible with
139  * the result from image converter utility*/
140 typedef struct {
141     lv_img_header_t header; /**< A header describing the basics of the image*/
142     uint32_t data_size;     /**< Size of the image in bytes*/
143     const uint8_t * data;   /**< Pointer to the data of the image*/
144 } lv_img_dsc_t;
145 
146 /**********************
147  * GLOBAL PROTOTYPES
148  **********************/
149 
150 /**
151  * Allocate an image buffer in RAM
152  * @param w width of image
153  * @param h height of image
154  * @param cf a color format (`LV_IMG_CF_...`)
155  * @return an allocated image, or NULL on failure
156  */
157 lv_img_dsc_t * lv_img_buf_alloc(lv_coord_t w, lv_coord_t h, lv_img_cf_t cf);
158 
159 /**
160  * Get the color of an image's pixel
161  * @param dsc an image descriptor
162  * @param x x coordinate of the point to get
163  * @param y x coordinate of the point to get
164  * @param color the color of the image. In case of `LV_IMG_CF_ALPHA_1/2/4/8` this color is used.
165  * Not used in other cases.
166  * @param safe true: check out of bounds
167  * @return color of the point
168  */
169 lv_color_t lv_img_buf_get_px_color(const lv_img_dsc_t * dsc, lv_coord_t x, lv_coord_t y, lv_color_t color);
170 
171 /**
172  * Get the alpha value of an image's pixel
173  * @param dsc pointer to an image descriptor
174  * @param x x coordinate of the point to set
175  * @param y x coordinate of the point to set
176  * @param safe true: check out of bounds
177  * @return alpha value of the point
178  */
179 lv_opa_t lv_img_buf_get_px_alpha(const lv_img_dsc_t * dsc, lv_coord_t x, lv_coord_t y);
180 
181 /**
182  * Set the color of a pixel of an image. The alpha channel won't be affected.
183  * @param dsc pointer to an image descriptor
184  * @param x x coordinate of the point to set
185  * @param y x coordinate of the point to set
186  * @param c color of the point
187  * @param safe true: check out of bounds
188  */
189 void lv_img_buf_set_px_color(const lv_img_dsc_t * dsc, lv_coord_t x, lv_coord_t y, lv_color_t c);
190 
191 /**
192  * Set the alpha value of a pixel of an image. The color won't be affected
193  * @param dsc pointer to an image descriptor
194  * @param x x coordinate of the point to set
195  * @param y x coordinate of the point to set
196  * @param opa the desired opacity
197  * @param safe true: check out of bounds
198  */
199 void lv_img_buf_set_px_alpha(const lv_img_dsc_t * dsc, lv_coord_t x, lv_coord_t y, lv_opa_t opa);
200 
201 /**
202  * Set the palette color of an indexed image. Valid only for `LV_IMG_CF_INDEXED1/2/4/8`
203  * @param dsc pointer to an image descriptor
204  * @param id the palette color to set:
205  *   - for `LV_IMG_CF_INDEXED1`: 0..1
206  *   - for `LV_IMG_CF_INDEXED2`: 0..3
207  *   - for `LV_IMG_CF_INDEXED4`: 0..15
208  *   - for `LV_IMG_CF_INDEXED8`: 0..255
209  * @param c the color to set
210  */
211 void lv_img_buf_set_palette(const lv_img_dsc_t * dsc, uint8_t id, lv_color_t c);
212 
213 /**
214  * Free an allocated image buffer
215  * @param dsc image buffer to free
216  */
217 void lv_img_buf_free(lv_img_dsc_t * dsc);
218 
219 /**
220  * Get the memory consumption of a raw bitmap, given color format and dimensions.
221  * @param w width
222  * @param h height
223  * @param cf color format
224  * @return size in bytes
225  */
226 uint32_t lv_img_buf_get_img_size(lv_coord_t w, lv_coord_t h, lv_img_cf_t cf);
227 
228 /**
229  * Get the area of a rectangle if its rotated and scaled
230  * @param res store the coordinates here
231  * @param w width of the rectangle to transform
232  * @param h height of the rectangle to transform
233  * @param angle angle of rotation
234  * @param zoom zoom, (256 no zoom)
235  * @param pivot x,y pivot coordinates of rotation
236  */
237 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,
238                                       const lv_point_t * pivot);
239 
240 /**********************
241  *      MACROS
242  **********************/
243 
244 #ifdef __cplusplus
245 } /*extern "C"*/
246 #endif
247 
248 #endif /*LV_IMG_BUF_H*/
249