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