1 /**
2 * @file lv_img_decoder.c
3 *
4 */
5
6 /*********************
7 * INCLUDES
8 *********************/
9 #include "lv_img_decoder.h"
10 #include "../lv_misc/lv_debug.h"
11 #include "../lv_draw/lv_draw_img.h"
12 #include "../lv_misc/lv_ll.h"
13 #include "../lv_misc/lv_color.h"
14 #include "../lv_misc/lv_gc.h"
15
16 #if defined(LV_GC_INCLUDE)
17 #include LV_GC_INCLUDE
18 #endif /* LV_ENABLE_GC */
19
20 /*********************
21 * DEFINES
22 *********************/
23 #define CF_BUILT_IN_FIRST LV_IMG_CF_TRUE_COLOR
24 #define CF_BUILT_IN_LAST LV_IMG_CF_ALPHA_8BIT
25
26 /**********************
27 * TYPEDEFS
28 **********************/
29 typedef struct {
30 #if LV_USE_FILESYSTEM
31 lv_fs_file_t * f;
32 #endif
33 lv_color_t * palette;
34 lv_opa_t * opa;
35 } lv_img_decoder_built_in_data_t;
36
37 /**********************
38 * STATIC PROTOTYPES
39 **********************/
40 static lv_res_t lv_img_decoder_built_in_line_true_color(lv_img_decoder_dsc_t * dsc, lv_coord_t x, lv_coord_t y,
41 lv_coord_t len, uint8_t * buf);
42 static lv_res_t lv_img_decoder_built_in_line_alpha(lv_img_decoder_dsc_t * dsc, lv_coord_t x, lv_coord_t y,
43 lv_coord_t len, uint8_t * buf);
44 static lv_res_t lv_img_decoder_built_in_line_indexed(lv_img_decoder_dsc_t * dsc, lv_coord_t x, lv_coord_t y,
45 lv_coord_t len, uint8_t * buf);
46
47 /**********************
48 * STATIC VARIABLES
49 **********************/
50
51 /**********************
52 * MACROS
53 **********************/
54
55 /**********************
56 * GLOBAL FUNCTIONS
57 **********************/
58
59 /**
60 * Initialize the image decoder module
61 * */
_lv_img_decoder_init(void)62 void _lv_img_decoder_init(void)
63 {
64 _lv_ll_init(&LV_GC_ROOT(_lv_img_defoder_ll), sizeof(lv_img_decoder_t));
65
66 lv_img_decoder_t * decoder;
67
68 /*Create a decoder for the built in color format*/
69 decoder = lv_img_decoder_create();
70 if(decoder == NULL) {
71 LV_LOG_WARN("lv_img_decoder_init: out of memory");
72 LV_ASSERT_MEM(decoder);
73 return;
74 }
75
76 lv_img_decoder_set_info_cb(decoder, lv_img_decoder_built_in_info);
77 lv_img_decoder_set_open_cb(decoder, lv_img_decoder_built_in_open);
78 lv_img_decoder_set_read_line_cb(decoder, lv_img_decoder_built_in_read_line);
79 lv_img_decoder_set_close_cb(decoder, lv_img_decoder_built_in_close);
80 }
81
82 /**
83 * Get information about an image.
84 * Try the created image decoder one by one. Once one is able to get info that info will be used.
85 * @param src the image source. E.g. file name or variable.
86 * @param header the image info will be stored here
87 * @return LV_RES_OK: success; LV_RES_INV: wasn't able to get info about the image
88 */
lv_img_decoder_get_info(const char * src,lv_img_header_t * header)89 lv_res_t lv_img_decoder_get_info(const char * src, lv_img_header_t * header)
90 {
91 header->always_zero = 0;
92 header->h = 0;
93 header->w = 0;
94 header->cf = LV_IMG_CF_UNKNOWN;
95
96 lv_res_t res = LV_RES_INV;
97 lv_img_decoder_t * d;
98 _LV_LL_READ(LV_GC_ROOT(_lv_img_defoder_ll), d) {
99 res = LV_RES_INV;
100 if(d->info_cb) {
101 res = d->info_cb(d, src, header);
102 if(res == LV_RES_OK) break;
103 }
104 }
105
106 return res;
107 }
108
109 /**
110 * Open an image.
111 * Try the created image decoder one by one. Once one is able to open the image that decoder is save in `dsc`
112 * @param dsc describe a decoding session. Simply a pointer to an `lv_img_decoder_dsc_t` variable.
113 * @param src the image source. Can be
114 * 1) File name: E.g. "S:folder/img1.png" (The drivers needs to registered via `lv_fs_add_drv()`)
115 * 2) Variable: Pointer to an `lv_img_dsc_t` variable
116 * 3) Symbol: E.g. `LV_SYMBOL_OK`
117 * @param color The color of the image with `LV_IMG_CF_ALPHA_...`
118 * @return LV_RES_OK: opened the image. `dsc->img_data` and `dsc->header` are set.
119 * LV_RES_INV: none of the registered image decoders were able to open the image.
120 */
lv_img_decoder_open(lv_img_decoder_dsc_t * dsc,const void * src,lv_color_t color)121 lv_res_t lv_img_decoder_open(lv_img_decoder_dsc_t * dsc, const void * src, lv_color_t color)
122 {
123 dsc->color = color;
124 dsc->src_type = lv_img_src_get_type(src);
125 dsc->user_data = NULL;
126
127 if(dsc->src_type == LV_IMG_SRC_FILE) {
128 size_t fnlen = strlen(src);
129 dsc->src = lv_mem_alloc(fnlen + 1);
130 strcpy((char *)dsc->src, src);
131 }
132 else {
133 dsc->src = src;
134 }
135
136 lv_res_t res = LV_RES_INV;
137
138 lv_img_decoder_t * d;
139 _LV_LL_READ(LV_GC_ROOT(_lv_img_defoder_ll), d) {
140 /*Info an Open callbacks are required*/
141 if(d->info_cb == NULL || d->open_cb == NULL) continue;
142
143 res = d->info_cb(d, src, &dsc->header);
144 if(res != LV_RES_OK) continue;
145
146 dsc->error_msg = NULL;
147 dsc->img_data = NULL;
148 dsc->decoder = d;
149
150 res = d->open_cb(d, dsc);
151
152 /*Opened successfully. It is a good decoder to for this image source*/
153 if(res == LV_RES_OK) break;
154 }
155
156 return res;
157 }
158
159 /**
160 * Read a line from an opened image
161 * @param dsc pointer to `lv_img_decoder_dsc_t` used in `lv_img_decoder_open`
162 * @param x start X coordinate (from left)
163 * @param y start Y coordinate (from top)
164 * @param len number of pixels to read
165 * @param buf store the data here
166 * @return LV_RES_OK: success; LV_RES_INV: an error occurred
167 */
lv_img_decoder_read_line(lv_img_decoder_dsc_t * dsc,lv_coord_t x,lv_coord_t y,lv_coord_t len,uint8_t * buf)168 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, uint8_t * buf)
169 {
170 lv_res_t res = LV_RES_INV;
171 if(dsc->decoder->read_line_cb) res = dsc->decoder->read_line_cb(dsc->decoder, dsc, x, y, len, buf);
172
173 return res;
174 }
175
176 /**
177 * Close a decoding session
178 * @param dsc pointer to `lv_img_decoder_dsc_t` used in `lv_img_decoder_open`
179 */
lv_img_decoder_close(lv_img_decoder_dsc_t * dsc)180 void lv_img_decoder_close(lv_img_decoder_dsc_t * dsc)
181 {
182 if(dsc->decoder) {
183 if(dsc->decoder->close_cb) dsc->decoder->close_cb(dsc->decoder, dsc);
184
185 if(dsc->src_type == LV_IMG_SRC_FILE) {
186 lv_mem_free(dsc->src);
187 dsc->src = NULL;
188 }
189 }
190 }
191
192 /**
193 * Create a new image decoder
194 * @return pointer to the new image decoder
195 */
lv_img_decoder_create(void)196 lv_img_decoder_t * lv_img_decoder_create(void)
197 {
198 lv_img_decoder_t * decoder;
199 decoder = _lv_ll_ins_head(&LV_GC_ROOT(_lv_img_defoder_ll));
200 LV_ASSERT_MEM(decoder);
201 if(decoder == NULL) return NULL;
202
203 _lv_memset_00(decoder, sizeof(lv_img_decoder_t));
204
205 return decoder;
206 }
207
208 /**
209 * Delete an image decoder
210 * @param decoder pointer to an image decoder
211 */
lv_img_decoder_delete(lv_img_decoder_t * decoder)212 void lv_img_decoder_delete(lv_img_decoder_t * decoder)
213 {
214 _lv_ll_remove(&LV_GC_ROOT(_lv_img_defoder_ll), decoder);
215 lv_mem_free(decoder);
216 }
217
218 /**
219 * Set a callback to get information about the image
220 * @param decoder pointer to an image decoder
221 * @param info_cb a function to collect info about an image (fill an `lv_img_header_t` struct)
222 */
lv_img_decoder_set_info_cb(lv_img_decoder_t * decoder,lv_img_decoder_info_f_t info_cb)223 void lv_img_decoder_set_info_cb(lv_img_decoder_t * decoder, lv_img_decoder_info_f_t info_cb)
224 {
225 decoder->info_cb = info_cb;
226 }
227
228 /**
229 * Set a callback to open an image
230 * @param decoder pointer to an image decoder
231 * @param open_cb a function to open an image
232 */
lv_img_decoder_set_open_cb(lv_img_decoder_t * decoder,lv_img_decoder_open_f_t open_cb)233 void lv_img_decoder_set_open_cb(lv_img_decoder_t * decoder, lv_img_decoder_open_f_t open_cb)
234 {
235 decoder->open_cb = open_cb;
236 }
237
238 /**
239 * Set a callback to a decoded line of an image
240 * @param decoder pointer to an image decoder
241 * @param read_line_cb a function to read a line of an image
242 */
lv_img_decoder_set_read_line_cb(lv_img_decoder_t * decoder,lv_img_decoder_read_line_f_t read_line_cb)243 void lv_img_decoder_set_read_line_cb(lv_img_decoder_t * decoder, lv_img_decoder_read_line_f_t read_line_cb)
244 {
245 decoder->read_line_cb = read_line_cb;
246 }
247
248 /**
249 * Set a callback to close a decoding session. E.g. close files and free other resources.
250 * @param decoder pointer to an image decoder
251 * @param close_cb a function to close a decoding session
252 */
lv_img_decoder_set_close_cb(lv_img_decoder_t * decoder,lv_img_decoder_close_f_t close_cb)253 void lv_img_decoder_set_close_cb(lv_img_decoder_t * decoder, lv_img_decoder_close_f_t close_cb)
254 {
255 decoder->close_cb = close_cb;
256 }
257
258 /**
259 * Get info about a built-in image
260 * @param decoder the decoder where this function belongs
261 * @param src the image source: pointer to an `lv_img_dsc_t` variable, a file path or a symbol
262 * @param header store the image data here
263 * @return LV_RES_OK: the info is successfully stored in `header`; LV_RES_INV: unknown format or other error.
264 */
lv_img_decoder_built_in_info(lv_img_decoder_t * decoder,const void * src,lv_img_header_t * header)265 lv_res_t lv_img_decoder_built_in_info(lv_img_decoder_t * decoder, const void * src, lv_img_header_t * header)
266 {
267 (void)decoder; /*Unused*/
268
269 lv_img_src_t src_type = lv_img_src_get_type(src);
270 if(src_type == LV_IMG_SRC_VARIABLE) {
271 lv_img_cf_t cf = ((lv_img_dsc_t *)src)->header.cf;
272 if(cf < CF_BUILT_IN_FIRST || cf > CF_BUILT_IN_LAST) return LV_RES_INV;
273
274 header->w = ((lv_img_dsc_t *)src)->header.w;
275 header->h = ((lv_img_dsc_t *)src)->header.h;
276 header->cf = ((lv_img_dsc_t *)src)->header.cf;
277 }
278 #if LV_USE_FILESYSTEM
279 else if(src_type == LV_IMG_SRC_FILE) {
280 lv_fs_file_t file;
281 lv_fs_res_t res;
282 uint32_t rn;
283 res = lv_fs_open(&file, src, LV_FS_MODE_RD);
284 if(res == LV_FS_RES_OK) {
285 res = lv_fs_read(&file, header, sizeof(lv_img_header_t), &rn);
286 lv_fs_close(&file);
287 if(res != LV_FS_RES_OK || rn != sizeof(lv_img_header_t)) {
288 LV_LOG_WARN("Image get info get read file header");
289 return LV_RES_INV;
290 }
291 }
292
293 if(header->cf < CF_BUILT_IN_FIRST || header->cf > CF_BUILT_IN_LAST) return LV_RES_INV;
294
295 }
296 #endif
297 else if(src_type == LV_IMG_SRC_SYMBOL) {
298 /*The size depend on the font but it is unknown here. It should be handled outside of the
299 * function*/
300 header->w = 1;
301 header->h = 1;
302 /* Symbols always have transparent parts. Important because of cover check in the design
303 * function. The actual value doesn't matter because lv_draw_label will draw it*/
304 header->cf = LV_IMG_CF_ALPHA_1BIT;
305 }
306 else {
307 LV_LOG_WARN("Image get info found unknown src type");
308 return LV_RES_INV;
309 }
310 return LV_RES_OK;
311 }
312
313 /**
314 * Open a built in image
315 * @param decoder the decoder where this function belongs
316 * @param dsc pointer to decoder descriptor. `src`, `style` are already initialized in it.
317 * @return LV_RES_OK: the info is successfully stored in `header`; LV_RES_INV: unknown format or other error.
318 */
lv_img_decoder_built_in_open(lv_img_decoder_t * decoder,lv_img_decoder_dsc_t * dsc)319 lv_res_t lv_img_decoder_built_in_open(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * dsc)
320 {
321 /*Open the file if it's a file*/
322 if(dsc->src_type == LV_IMG_SRC_FILE) {
323 #if LV_USE_FILESYSTEM
324
325 /*Support only "*.bin" files*/
326 if(strcmp(lv_fs_get_ext(dsc->src), "bin")) return LV_RES_INV;
327
328 lv_fs_file_t f;
329 lv_fs_res_t res = lv_fs_open(&f, dsc->src, LV_FS_MODE_RD);
330 if(res != LV_FS_RES_OK) {
331 LV_LOG_WARN("Built-in image decoder can't open the file");
332 return LV_RES_INV;
333 }
334
335 /*If the file was open successfully save the file descriptor*/
336 if(dsc->user_data == NULL) {
337 dsc->user_data = lv_mem_alloc(sizeof(lv_img_decoder_built_in_data_t));
338 LV_ASSERT_MEM(dsc->user_data);
339 if(dsc->user_data == NULL) {
340 LV_LOG_ERROR("img_decoder_built_in_open: out of memory");
341 return LV_RES_INV;
342 }
343 _lv_memset_00(dsc->user_data, sizeof(lv_img_decoder_built_in_data_t));
344 }
345
346 lv_img_decoder_built_in_data_t * user_data = dsc->user_data;
347 user_data->f = lv_mem_alloc(sizeof(f));
348 LV_ASSERT_MEM(user_data->f);
349 if(user_data->f == NULL) {
350 LV_LOG_ERROR("img_decoder_built_in_open: out of memory");
351 lv_img_decoder_built_in_close(decoder, dsc);
352 return LV_RES_INV;
353 }
354
355 _lv_memcpy_small(user_data->f, &f, sizeof(f));
356
357 #else
358 LV_LOG_WARN("Image built-in decoder cannot read file because LV_USE_FILESYSTEM = 0");
359 return LV_RES_INV;
360 #endif
361 }
362 else if(dsc->src_type == LV_IMG_SRC_VARIABLE) {
363 /*The variables should have valid data*/
364 if(((lv_img_dsc_t *)dsc->src)->data == NULL) {
365 return LV_RES_INV;
366 }
367 }
368
369 lv_img_cf_t cf = dsc->header.cf;
370 /*Process true color formats*/
371 if(cf == LV_IMG_CF_TRUE_COLOR || cf == LV_IMG_CF_TRUE_COLOR_ALPHA || cf == LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED) {
372 if(dsc->src_type == LV_IMG_SRC_VARIABLE) {
373 /* In case of uncompressed formats the image stored in the ROM/RAM.
374 * So simply give its pointer*/
375 dsc->img_data = ((lv_img_dsc_t *)dsc->src)->data;
376 return LV_RES_OK;
377 }
378 else {
379 /*If it's a file it need to be read line by line later*/
380 dsc->img_data = NULL;
381 return LV_RES_OK;
382 }
383 }
384 /*Process indexed images. Build a palette*/
385 else if(cf == LV_IMG_CF_INDEXED_1BIT || cf == LV_IMG_CF_INDEXED_2BIT || cf == LV_IMG_CF_INDEXED_4BIT ||
386 cf == LV_IMG_CF_INDEXED_8BIT) {
387
388 #if LV_IMG_CF_INDEXED
389 uint8_t px_size = lv_img_cf_get_px_size(cf);
390 uint32_t palette_size = 1 << px_size;
391
392 /*Allocate the palette*/
393 if(dsc->user_data == NULL) {
394 dsc->user_data = lv_mem_alloc(sizeof(lv_img_decoder_built_in_data_t));
395 LV_ASSERT_MEM(dsc->user_data);
396 if(dsc->user_data == NULL) {
397 LV_LOG_ERROR("img_decoder_built_in_open: out of memory");
398 lv_img_decoder_built_in_close(decoder, dsc);
399 return LV_RES_INV;
400 }
401 _lv_memset_00(dsc->user_data, sizeof(lv_img_decoder_built_in_data_t));
402 }
403
404 lv_img_decoder_built_in_data_t * user_data = dsc->user_data;
405 user_data->palette = lv_mem_alloc(palette_size * sizeof(lv_color_t));
406 LV_ASSERT_MEM(user_data->palette);
407 user_data->opa = lv_mem_alloc(palette_size * sizeof(lv_opa_t));
408 LV_ASSERT_MEM(user_data->opa);
409 if(user_data->palette == NULL || user_data->opa == NULL) {
410 LV_LOG_ERROR("img_decoder_built_in_open: out of memory");
411 lv_img_decoder_built_in_close(decoder, dsc);
412 return LV_RES_INV;
413 }
414
415 if(dsc->src_type == LV_IMG_SRC_FILE) {
416 /*Read the palette from file*/
417 #if LV_USE_FILESYSTEM
418 lv_fs_seek(user_data->f, 4); /*Skip the header*/
419 lv_color32_t cur_color;
420 uint32_t i;
421 for(i = 0; i < palette_size; i++) {
422 lv_fs_read(user_data->f, &cur_color, sizeof(lv_color32_t), NULL);
423 user_data->palette[i] = lv_color_make(cur_color.ch.red, cur_color.ch.green, cur_color.ch.blue);
424 user_data->opa[i] = cur_color.ch.alpha;
425 }
426 #else
427 LV_LOG_WARN("Image built-in decoder can read the palette because LV_USE_FILESYSTEM = 0");
428 return LV_RES_INV;
429 #endif
430 }
431 else {
432 /*The palette begins in the beginning of the image data. Just point to it.*/
433 lv_color32_t * palette_p = (lv_color32_t *)((lv_img_dsc_t *)dsc->src)->data;
434
435
436 uint32_t i;
437 for(i = 0; i < palette_size; i++) {
438 user_data->palette[i] = lv_color_make(palette_p[i].ch.red, palette_p[i].ch.green, palette_p[i].ch.blue);
439 user_data->opa[i] = palette_p[i].ch.alpha;
440 }
441 }
442
443 dsc->img_data = NULL;
444 return LV_RES_OK;
445 #else
446 LV_LOG_WARN("Indexed (palette) images are not enabled in lv_conf.h. See LV_IMG_CF_INDEXED");
447 return LV_RES_INV;
448 #endif
449 }
450 /*Alpha indexed images. */
451 else if(cf == LV_IMG_CF_ALPHA_1BIT || cf == LV_IMG_CF_ALPHA_2BIT || cf == LV_IMG_CF_ALPHA_4BIT ||
452 cf == LV_IMG_CF_ALPHA_8BIT) {
453 #if LV_IMG_CF_ALPHA
454 dsc->img_data = NULL;
455 return LV_RES_OK; /*Nothing to process*/
456 #else
457 LV_LOG_WARN("Alpha indexed images are not enabled in lv_conf.h. See LV_IMG_CF_ALPHA");
458 return LV_RES_INV;
459 #endif
460 }
461 /*Unknown format. Can't decode it.*/
462 else {
463 /*Free the potentially allocated memories*/
464 lv_img_decoder_built_in_close(decoder, dsc);
465
466 LV_LOG_WARN("Image decoder open: unknown color format")
467 return LV_RES_INV;
468 }
469 }
470
471 /**
472 * Decode `len` pixels starting from the given `x`, `y` coordinates and store them in `buf`.
473 * Required only if the "open" function can't return with the whole decoded pixel array.
474 * @param decoder pointer to the decoder the function associated with
475 * @param dsc pointer to decoder descriptor
476 * @param x start x coordinate
477 * @param y start y coordinate
478 * @param len number of pixels to decode
479 * @param buf a buffer to store the decoded pixels
480 * @return LV_RES_OK: ok; LV_RES_INV: failed
481 */
lv_img_decoder_built_in_read_line(lv_img_decoder_t * decoder,lv_img_decoder_dsc_t * dsc,lv_coord_t x,lv_coord_t y,lv_coord_t len,uint8_t * buf)482 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,
483 lv_coord_t y, lv_coord_t len, uint8_t * buf)
484 {
485 (void)decoder; /*Unused*/
486
487 lv_res_t res = LV_RES_INV;
488
489 if(dsc->header.cf == LV_IMG_CF_TRUE_COLOR || dsc->header.cf == LV_IMG_CF_TRUE_COLOR_ALPHA ||
490 dsc->header.cf == LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED) {
491 /* For TRUE_COLOR images read line required only for files.
492 * For variables the image data was returned in `open`*/
493 if(dsc->src_type == LV_IMG_SRC_FILE) {
494 res = lv_img_decoder_built_in_line_true_color(dsc, x, y, len, buf);
495 }
496 }
497 else if(dsc->header.cf == LV_IMG_CF_ALPHA_1BIT || dsc->header.cf == LV_IMG_CF_ALPHA_2BIT ||
498 dsc->header.cf == LV_IMG_CF_ALPHA_4BIT || dsc->header.cf == LV_IMG_CF_ALPHA_8BIT) {
499
500 res = lv_img_decoder_built_in_line_alpha(dsc, x, y, len, buf);
501 }
502 else if(dsc->header.cf == LV_IMG_CF_INDEXED_1BIT || dsc->header.cf == LV_IMG_CF_INDEXED_2BIT ||
503 dsc->header.cf == LV_IMG_CF_INDEXED_4BIT || dsc->header.cf == LV_IMG_CF_INDEXED_8BIT) {
504 res = lv_img_decoder_built_in_line_indexed(dsc, x, y, len, buf);
505 }
506 else {
507 LV_LOG_WARN("Built-in image decoder read not supports the color format");
508 return LV_RES_INV;
509 }
510
511 return res;
512 }
513
514 /**
515 * Close the pending decoding. Free resources etc.
516 * @param decoder pointer to the decoder the function associated with
517 * @param dsc pointer to decoder descriptor
518 */
lv_img_decoder_built_in_close(lv_img_decoder_t * decoder,lv_img_decoder_dsc_t * dsc)519 void lv_img_decoder_built_in_close(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * dsc)
520 {
521 (void)decoder; /*Unused*/
522
523 lv_img_decoder_built_in_data_t * user_data = dsc->user_data;
524 if(user_data) {
525 #if LV_USE_FILESYSTEM
526 if(user_data->f) {
527 lv_fs_close(user_data->f);
528 lv_mem_free(user_data->f);
529 }
530 #endif
531 if(user_data->palette) lv_mem_free(user_data->palette);
532 if(user_data->opa) lv_mem_free(user_data->opa);
533
534 lv_mem_free(user_data);
535
536 dsc->user_data = NULL;
537 }
538 }
539
540
541 /**********************
542 * STATIC FUNCTIONS
543 **********************/
544
lv_img_decoder_built_in_line_true_color(lv_img_decoder_dsc_t * dsc,lv_coord_t x,lv_coord_t y,lv_coord_t len,uint8_t * buf)545 static lv_res_t lv_img_decoder_built_in_line_true_color(lv_img_decoder_dsc_t * dsc, lv_coord_t x, lv_coord_t y,
546 lv_coord_t len, uint8_t * buf)
547 {
548 #if LV_USE_FILESYSTEM
549 lv_img_decoder_built_in_data_t * user_data = dsc->user_data;
550 lv_fs_res_t res;
551 uint8_t px_size = lv_img_cf_get_px_size(dsc->header.cf);
552
553 uint32_t pos = ((y * dsc->header.w + x) * px_size) >> 3;
554 pos += 4; /*Skip the header*/
555 res = lv_fs_seek(user_data->f, pos);
556 if(res != LV_FS_RES_OK) {
557 LV_LOG_WARN("Built-in image decoder seek failed");
558 return LV_RES_INV;
559 }
560 uint32_t btr = len * (px_size >> 3);
561 uint32_t br = 0;
562 res = lv_fs_read(user_data->f, buf, btr, &br);
563 if(res != LV_FS_RES_OK || btr != br) {
564 LV_LOG_WARN("Built-in image decoder read failed");
565 return LV_RES_INV;
566 }
567
568 return LV_RES_OK;
569 #else
570 LV_UNUSED(dsc);
571 LV_UNUSED(x);
572 LV_UNUSED(y);
573 LV_UNUSED(len);
574 LV_UNUSED(buf);
575 LV_LOG_WARN("Image built-in decoder cannot read file because LV_USE_FILESYSTEM = 0");
576 return LV_RES_INV;
577 #endif
578 }
579
lv_img_decoder_built_in_line_alpha(lv_img_decoder_dsc_t * dsc,lv_coord_t x,lv_coord_t y,lv_coord_t len,uint8_t * buf)580 static lv_res_t lv_img_decoder_built_in_line_alpha(lv_img_decoder_dsc_t * dsc, lv_coord_t x, lv_coord_t y,
581 lv_coord_t len, uint8_t * buf)
582 {
583
584 #if LV_IMG_CF_ALPHA
585 const lv_opa_t alpha1_opa_table[2] = {0, 255}; /*Opacity mapping with bpp = 1 (Just for compatibility)*/
586 const lv_opa_t alpha2_opa_table[4] = {0, 85, 170, 255}; /*Opacity mapping with bpp = 2*/
587 const lv_opa_t alpha4_opa_table[16] = {0, 17, 34, 51, /*Opacity mapping with bpp = 4*/
588 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255
589 };
590
591 /*Simply fill the buffer with the color. Later only the alpha value will be modified.*/
592 lv_color_t bg_color = dsc->color;
593 lv_coord_t i;
594 for(i = 0; i < len; i++) {
595 #if LV_COLOR_DEPTH == 8 || LV_COLOR_DEPTH == 1
596 buf[i * LV_IMG_PX_SIZE_ALPHA_BYTE] = bg_color.full;
597 #elif LV_COLOR_DEPTH == 16
598 /*Because of Alpha byte 16 bit color can start on odd address which can cause crash*/
599 buf[i * LV_IMG_PX_SIZE_ALPHA_BYTE] = bg_color.full & 0xFF;
600 buf[i * LV_IMG_PX_SIZE_ALPHA_BYTE + 1] = (bg_color.full >> 8) & 0xFF;
601 #elif LV_COLOR_DEPTH == 32
602 *((uint32_t *)&buf[i * LV_IMG_PX_SIZE_ALPHA_BYTE]) = bg_color.full;
603 #else
604 #error "Invalid LV_COLOR_DEPTH. Check it in lv_conf.h"
605 #endif
606 }
607
608 const lv_opa_t * opa_table = NULL;
609 uint8_t px_size = lv_img_cf_get_px_size(dsc->header.cf);
610 uint16_t mask = (1 << px_size) - 1; /*E.g. px_size = 2; mask = 0x03*/
611
612 lv_coord_t w = 0;
613 uint32_t ofs = 0;
614 int8_t pos = 0;
615 switch(dsc->header.cf) {
616 case LV_IMG_CF_ALPHA_1BIT:
617 w = (dsc->header.w >> 3); /*E.g. w = 20 -> w = 2 + 1*/
618 if(dsc->header.w & 0x7) w++;
619 ofs += w * y + (x >> 3); /*First pixel*/
620 pos = 7 - (x & 0x7);
621 opa_table = alpha1_opa_table;
622 break;
623 case LV_IMG_CF_ALPHA_2BIT:
624 w = (dsc->header.w >> 2); /*E.g. w = 13 -> w = 3 + 1 (bytes)*/
625 if(dsc->header.w & 0x3) w++;
626 ofs += w * y + (x >> 2); /*First pixel*/
627 pos = 6 - ((x & 0x3) * 2);
628 opa_table = alpha2_opa_table;
629 break;
630 case LV_IMG_CF_ALPHA_4BIT:
631 w = (dsc->header.w >> 1); /*E.g. w = 13 -> w = 6 + 1 (bytes)*/
632 if(dsc->header.w & 0x1) w++;
633 ofs += w * y + (x >> 1); /*First pixel*/
634 pos = 4 - ((x & 0x1) * 4);
635 opa_table = alpha4_opa_table;
636 break;
637 case LV_IMG_CF_ALPHA_8BIT:
638 w = dsc->header.w; /*E.g. x = 7 -> w = 7 (bytes)*/
639 ofs += w * y + x; /*First pixel*/
640 pos = 0;
641 break;
642 }
643
644 #if LV_USE_FILESYSTEM
645 lv_img_decoder_built_in_data_t * user_data = dsc->user_data;
646 uint8_t * fs_buf = _lv_mem_buf_get(w);
647 #endif
648
649 const uint8_t * data_tmp = NULL;
650 if(dsc->src_type == LV_IMG_SRC_VARIABLE) {
651 const lv_img_dsc_t * img_dsc = dsc->src;
652
653 data_tmp = img_dsc->data + ofs;
654 }
655 else {
656 #if LV_USE_FILESYSTEM
657 lv_fs_seek(user_data->f, ofs + 4); /*+4 to skip the header*/
658 lv_fs_read(user_data->f, fs_buf, w, NULL);
659 data_tmp = fs_buf;
660 #else
661 LV_LOG_WARN("Image built-in alpha line reader can't read file because LV_USE_FILESYSTEM = 0");
662 data_tmp = NULL; /*To avoid warnings*/
663 return LV_RES_INV;
664 #endif
665 }
666
667 for(i = 0; i < len; i++) {
668 uint8_t val_act = (*data_tmp & (mask << pos)) >> pos;
669
670 buf[i * LV_IMG_PX_SIZE_ALPHA_BYTE + LV_IMG_PX_SIZE_ALPHA_BYTE - 1] =
671 dsc->header.cf == LV_IMG_CF_ALPHA_8BIT ? val_act : opa_table[val_act];
672
673 pos -= px_size;
674 if(pos < 0) {
675 pos = 8 - px_size;
676 data_tmp++;
677 }
678 }
679 #if LV_USE_FILESYSTEM
680 _lv_mem_buf_release(fs_buf);
681 #endif
682 return LV_RES_OK;
683
684 #else
685 LV_LOG_WARN("Image built-in alpha line reader failed because LV_IMG_CF_ALPHA is 0 in lv_conf.h");
686 return LV_RES_INV;
687 #endif
688 }
689
lv_img_decoder_built_in_line_indexed(lv_img_decoder_dsc_t * dsc,lv_coord_t x,lv_coord_t y,lv_coord_t len,uint8_t * buf)690 static lv_res_t lv_img_decoder_built_in_line_indexed(lv_img_decoder_dsc_t * dsc, lv_coord_t x, lv_coord_t y,
691 lv_coord_t len, uint8_t * buf)
692 {
693
694 #if LV_IMG_CF_INDEXED
695 uint8_t px_size = lv_img_cf_get_px_size(dsc->header.cf);
696 uint16_t mask = (1 << px_size) - 1; /*E.g. px_size = 2; mask = 0x03*/
697
698 lv_coord_t w = 0;
699 int8_t pos = 0;
700 uint32_t ofs = 0;
701 switch(dsc->header.cf) {
702 case LV_IMG_CF_INDEXED_1BIT:
703 w = (dsc->header.w >> 3); /*E.g. w = 20 -> w = 2 + 1*/
704 if(dsc->header.w & 0x7) w++;
705 ofs += w * y + (x >> 3); /*First pixel*/
706 ofs += 8; /*Skip the palette*/
707 pos = 7 - (x & 0x7);
708 break;
709 case LV_IMG_CF_INDEXED_2BIT:
710 w = (dsc->header.w >> 2); /*E.g. w = 13 -> w = 3 + 1 (bytes)*/
711 if(dsc->header.w & 0x3) w++;
712 ofs += w * y + (x >> 2); /*First pixel*/
713 ofs += 16; /*Skip the palette*/
714 pos = 6 - ((x & 0x3) * 2);
715 break;
716 case LV_IMG_CF_INDEXED_4BIT:
717 w = (dsc->header.w >> 1); /*E.g. w = 13 -> w = 6 + 1 (bytes)*/
718 if(dsc->header.w & 0x1) w++;
719 ofs += w * y + (x >> 1); /*First pixel*/
720 ofs += 64; /*Skip the palette*/
721 pos = 4 - ((x & 0x1) * 4);
722 break;
723 case LV_IMG_CF_INDEXED_8BIT:
724 w = dsc->header.w; /*E.g. x = 7 -> w = 7 (bytes)*/
725 ofs += w * y + x; /*First pixel*/
726 ofs += 1024; /*Skip the palette*/
727 pos = 0;
728 break;
729 }
730
731 lv_img_decoder_built_in_data_t * user_data = dsc->user_data;
732
733 #if LV_USE_FILESYSTEM
734 uint8_t * fs_buf = _lv_mem_buf_get(w);
735 #endif
736 const uint8_t * data_tmp = NULL;
737 if(dsc->src_type == LV_IMG_SRC_VARIABLE) {
738 const lv_img_dsc_t * img_dsc = dsc->src;
739 data_tmp = img_dsc->data + ofs;
740 }
741 else {
742 #if LV_USE_FILESYSTEM
743 lv_fs_seek(user_data->f, ofs + 4); /*+4 to skip the header*/
744 lv_fs_read(user_data->f, fs_buf, w, NULL);
745 data_tmp = fs_buf;
746 #else
747 LV_LOG_WARN("Image built-in indexed line reader can't read file because LV_USE_FILESYSTEM = 0");
748 data_tmp = NULL; /*To avoid warnings*/
749 return LV_RES_INV;
750 #endif
751 }
752
753 lv_coord_t i;
754 for(i = 0; i < len; i++) {
755 uint8_t val_act = (*data_tmp & (mask << pos)) >> pos;
756
757 lv_color_t color = user_data->palette[val_act];
758 #if LV_COLOR_DEPTH == 8 || LV_COLOR_DEPTH == 1
759 buf[i * LV_IMG_PX_SIZE_ALPHA_BYTE] = color.full;
760 #elif LV_COLOR_DEPTH == 16
761 /*Because of Alpha byte 16 bit color can start on odd address which can cause crash*/
762 buf[i * LV_IMG_PX_SIZE_ALPHA_BYTE] = color.full & 0xFF;
763 buf[i * LV_IMG_PX_SIZE_ALPHA_BYTE + 1] = (color.full >> 8) & 0xFF;
764 #elif LV_COLOR_DEPTH == 32
765 *((uint32_t *)&buf[i * LV_IMG_PX_SIZE_ALPHA_BYTE]) = color.full;
766 #else
767 #error "Invalid LV_COLOR_DEPTH. Check it in lv_conf.h"
768 #endif
769 buf[i * LV_IMG_PX_SIZE_ALPHA_BYTE + LV_IMG_PX_SIZE_ALPHA_BYTE - 1] = user_data->opa[val_act];
770
771 pos -= px_size;
772 if(pos < 0) {
773 pos = 8 - px_size;
774 data_tmp++;
775 }
776 }
777 #if LV_USE_FILESYSTEM
778 _lv_mem_buf_release(fs_buf);
779 #endif
780 return LV_RES_OK;
781 #else
782 LV_LOG_WARN("Image built-in indexed line reader failed because LV_IMG_CF_INDEXED is 0 in lv_conf.h");
783 return LV_RES_INV;
784 #endif
785 }
786