1 /** 2 * @file lv_img_decoder.h 3 * 4 */ 5 6 #ifndef LV_IMG_DECODER_H 7 #define LV_IMG_DECODER_H 8 9 #ifdef __cplusplus 10 extern "C" { 11 #endif 12 13 /********************* 14 * INCLUDES 15 *********************/ 16 #include "../lv_conf_internal.h" 17 18 #include <stdint.h> 19 #include "lv_img_buf.h" 20 #include "../misc/lv_fs.h" 21 #include "../misc/lv_types.h" 22 #include "../misc/lv_area.h" 23 24 /********************* 25 * DEFINES 26 *********************/ 27 28 /********************** 29 * TYPEDEFS 30 **********************/ 31 32 /** 33 * Source of image.*/ 34 enum { 35 LV_IMG_SRC_VARIABLE, /** Binary/C variable*/ 36 LV_IMG_SRC_FILE, /** File in filesystem*/ 37 LV_IMG_SRC_SYMBOL, /** Symbol (@ref lv_symbol_def.h)*/ 38 LV_IMG_SRC_UNKNOWN, /** Unknown source*/ 39 }; 40 41 typedef uint8_t lv_img_src_t; 42 43 /*Decoder function definitions*/ 44 struct _lv_img_decoder_dsc_t; 45 struct _lv_img_decoder_t; 46 47 /** 48 * Get info from an image and store in the `header` 49 * @param src the image source. Can be a pointer to a C array or a file name (Use 50 * `lv_img_src_get_type` to determine the type) 51 * @param header store the info here 52 * @return LV_RES_OK: info written correctly; LV_RES_INV: failed 53 */ 54 typedef lv_res_t (*lv_img_decoder_info_f_t)(struct _lv_img_decoder_t * decoder, const void * src, 55 lv_img_header_t * header); 56 57 /** 58 * Open an image for decoding. Prepare it as it is required to read it later 59 * @param decoder pointer to the decoder the function associated with 60 * @param dsc pointer to decoder descriptor. `src`, `color` are already initialized in it. 61 */ 62 typedef lv_res_t (*lv_img_decoder_open_f_t)(struct _lv_img_decoder_t * decoder, struct _lv_img_decoder_dsc_t * dsc); 63 64 /** 65 * Decode `len` pixels starting from the given `x`, `y` coordinates and store them in `buf`. 66 * Required only if the "open" function can't return with the whole decoded pixel array. 67 * @param decoder pointer to the decoder the function associated with 68 * @param dsc pointer to decoder descriptor 69 * @param x start x coordinate 70 * @param y start y coordinate 71 * @param len number of pixels to decode 72 * @param buf a buffer to store the decoded pixels 73 * @return LV_RES_OK: ok; LV_RES_INV: failed 74 */ 75 typedef lv_res_t (*lv_img_decoder_read_line_f_t)(struct _lv_img_decoder_t * decoder, struct _lv_img_decoder_dsc_t * dsc, 76 lv_coord_t x, lv_coord_t y, lv_coord_t len, uint8_t * buf); 77 78 /** 79 * Close the pending decoding. Free resources etc. 80 * @param decoder pointer to the decoder the function associated with 81 * @param dsc pointer to decoder descriptor 82 */ 83 typedef void (*lv_img_decoder_close_f_t)(struct _lv_img_decoder_t * decoder, struct _lv_img_decoder_dsc_t * dsc); 84 85 typedef struct _lv_img_decoder_t { 86 lv_img_decoder_info_f_t info_cb; 87 lv_img_decoder_open_f_t open_cb; 88 lv_img_decoder_read_line_f_t read_line_cb; 89 lv_img_decoder_close_f_t close_cb; 90 91 #if LV_USE_USER_DATA 92 void * user_data; 93 #endif 94 } lv_img_decoder_t; 95 96 /**Describe an image decoding session. Stores data about the decoding*/ 97 typedef struct _lv_img_decoder_dsc_t { 98 /**The decoder which was able to open the image source*/ 99 lv_img_decoder_t * decoder; 100 101 /**The image source. A file path like "S:my_img.png" or pointer to an `lv_img_dsc_t` variable*/ 102 const void * src; 103 104 /**Color to draw the image. USed when the image has alpha channel only*/ 105 lv_color_t color; 106 107 /**Frame of the image, using with animated images*/ 108 int32_t frame_id; 109 110 /**Type of the source: file or variable. Can be set in `open` function if required*/ 111 lv_img_src_t src_type; 112 113 /**Info about the opened image: color format, size, etc. MUST be set in `open` function*/ 114 lv_img_header_t header; 115 116 /** Pointer to a buffer where the image's data (pixels) are stored in a decoded, plain format. 117 * MUST be set in `open` function*/ 118 const uint8_t * img_data; 119 120 /** How much time did it take to open the image. [ms] 121 * If not set `lv_img_cache` will measure and set the time to open*/ 122 uint32_t time_to_open; 123 124 /**A text to display instead of the image when the image can't be opened. 125 * Can be set in `open` function or set NULL.*/ 126 const char * error_msg; 127 128 /**Store any custom data here is required*/ 129 void * user_data; 130 } lv_img_decoder_dsc_t; 131 132 /********************** 133 * GLOBAL PROTOTYPES 134 **********************/ 135 136 /** 137 * Initialize the image decoder module 138 */ 139 void _lv_img_decoder_init(void); 140 141 /** 142 * Get information about an image. 143 * Try the created image decoder one by one. Once one is able to get info that info will be used. 144 * @param src the image source. Can be 145 * 1) File name: E.g. "S:folder/img1.png" (The drivers needs to registered via `lv_fs_drv_register()`) 146 * 2) Variable: Pointer to an `lv_img_dsc_t` variable 147 * 3) Symbol: E.g. `LV_SYMBOL_OK` 148 * @param header the image info will be stored here 149 * @return LV_RES_OK: success; LV_RES_INV: wasn't able to get info about the image 150 */ 151 lv_res_t lv_img_decoder_get_info(const void * src, lv_img_header_t * header); 152 153 /** 154 * Open an image. 155 * Try the created image decoders one by one. Once one is able to open the image that decoder is saved in `dsc` 156 * @param dsc describes a decoding session. Simply a pointer to an `lv_img_decoder_dsc_t` variable. 157 * @param src the image source. Can be 158 * 1) File name: E.g. "S:folder/img1.png" (The drivers needs to registered via `lv_fs_drv_register())`) 159 * 2) Variable: Pointer to an `lv_img_dsc_t` variable 160 * 3) Symbol: E.g. `LV_SYMBOL_OK` 161 * @param color The color of the image with `LV_IMG_CF_ALPHA_...` 162 * @param frame_id the index of the frame. Used only with animated images, set 0 for normal images 163 * @return LV_RES_OK: opened the image. `dsc->img_data` and `dsc->header` are set. 164 * LV_RES_INV: none of the registered image decoders were able to open the image. 165 */ 166 lv_res_t lv_img_decoder_open(lv_img_decoder_dsc_t * dsc, const void * src, lv_color_t color, int32_t frame_id); 167 168 /** 169 * Read a line from an opened image 170 * @param dsc pointer to `lv_img_decoder_dsc_t` used in `lv_img_decoder_open` 171 * @param x start X coordinate (from left) 172 * @param y start Y coordinate (from top) 173 * @param len number of pixels to read 174 * @param buf store the data here 175 * @return LV_RES_OK: success; LV_RES_INV: an error occurred 176 */ 177 lv_res_t lv_img_decoder_read_line(lv_img_decoder_dsc_t * dsc, lv_coord_t x, lv_coord_t y, lv_coord_t len, 178 uint8_t * buf); 179 180 /** 181 * Close a decoding session 182 * @param dsc pointer to `lv_img_decoder_dsc_t` used in `lv_img_decoder_open` 183 */ 184 void lv_img_decoder_close(lv_img_decoder_dsc_t * dsc); 185 186 /** 187 * Create a new image decoder 188 * @return pointer to the new image decoder 189 */ 190 lv_img_decoder_t * lv_img_decoder_create(void); 191 192 /** 193 * Delete an image decoder 194 * @param decoder pointer to an image decoder 195 */ 196 void lv_img_decoder_delete(lv_img_decoder_t * decoder); 197 198 /** 199 * Set a callback to get information about the image 200 * @param decoder pointer to an image decoder 201 * @param info_cb a function to collect info about an image (fill an `lv_img_header_t` struct) 202 */ 203 void lv_img_decoder_set_info_cb(lv_img_decoder_t * decoder, lv_img_decoder_info_f_t info_cb); 204 205 /** 206 * Set a callback to open an image 207 * @param decoder pointer to an image decoder 208 * @param open_cb a function to open an image 209 */ 210 void lv_img_decoder_set_open_cb(lv_img_decoder_t * decoder, lv_img_decoder_open_f_t open_cb); 211 212 /** 213 * Set a callback to a decoded line of an image 214 * @param decoder pointer to an image decoder 215 * @param read_line_cb a function to read a line of an image 216 */ 217 void lv_img_decoder_set_read_line_cb(lv_img_decoder_t * decoder, lv_img_decoder_read_line_f_t read_line_cb); 218 219 /** 220 * Set a callback to close a decoding session. E.g. close files and free other resources. 221 * @param decoder pointer to an image decoder 222 * @param close_cb a function to close a decoding session 223 */ 224 void lv_img_decoder_set_close_cb(lv_img_decoder_t * decoder, lv_img_decoder_close_f_t close_cb); 225 226 /** 227 * Get info about a built-in image 228 * @param decoder the decoder where this function belongs 229 * @param src the image source: pointer to an `lv_img_dsc_t` variable, a file path or a symbol 230 * @param header store the image data here 231 * @return LV_RES_OK: the info is successfully stored in `header`; LV_RES_INV: unknown format or other error. 232 */ 233 lv_res_t lv_img_decoder_built_in_info(lv_img_decoder_t * decoder, const void * src, lv_img_header_t * header); 234 235 /** 236 * Open a built in image 237 * @param decoder the decoder where this function belongs 238 * @param dsc pointer to decoder descriptor. `src`, `style` are already initialized in it. 239 * @return LV_RES_OK: the info is successfully stored in `header`; LV_RES_INV: unknown format or other error. 240 */ 241 lv_res_t lv_img_decoder_built_in_open(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * dsc); 242 243 /** 244 * Decode `len` pixels starting from the given `x`, `y` coordinates and store them in `buf`. 245 * Required only if the "open" function can't return with the whole decoded pixel array. 246 * @param decoder pointer to the decoder the function associated with 247 * @param dsc pointer to decoder descriptor 248 * @param x start x coordinate 249 * @param y start y coordinate 250 * @param len number of pixels to decode 251 * @param buf a buffer to store the decoded pixels 252 * @return LV_RES_OK: ok; LV_RES_INV: failed 253 */ 254 lv_res_t lv_img_decoder_built_in_read_line(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * dsc, lv_coord_t x, 255 lv_coord_t y, lv_coord_t len, uint8_t * buf); 256 257 /** 258 * Close the pending decoding. Free resources etc. 259 * @param decoder pointer to the decoder the function associated with 260 * @param dsc pointer to decoder descriptor 261 */ 262 void lv_img_decoder_built_in_close(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * dsc); 263 264 /********************** 265 * MACROS 266 **********************/ 267 268 #ifdef __cplusplus 269 } /*extern "C"*/ 270 #endif 271 272 #endif /*LV_IMG_DECODER_H*/ 273