Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/libs/lvgl/lv_draw/lv_draw_img.c
1476 views
1
/**
2
* @file lv_draw_img.c
3
*
4
*/
5
6
/*********************
7
* INCLUDES
8
*********************/
9
#include "lv_draw_img.h"
10
#include "../lv_misc/lv_fs.h"
11
12
/*********************
13
* DEFINES
14
*********************/
15
16
/**********************
17
* TYPEDEFS
18
**********************/
19
20
/**********************
21
* STATIC PROTOTYPES
22
**********************/
23
static lv_res_t lv_img_draw_core(const lv_area_t * coords, const lv_area_t * mask,
24
const void * src, const lv_style_t * style, lv_opa_t opa_scale);
25
26
static const uint8_t * lv_img_decoder_open(const void * src, const lv_style_t * style);
27
static lv_res_t lv_img_decoder_read_line(lv_coord_t x, lv_coord_t y, lv_coord_t len, uint8_t * buf);
28
static void lv_img_decoder_close(void);
29
static lv_res_t lv_img_built_in_decoder_line_alpha(lv_coord_t x, lv_coord_t y, lv_coord_t len, uint8_t * buf);
30
static lv_res_t lv_img_built_in_decoder_line_indexed(lv_coord_t x, lv_coord_t y, lv_coord_t len, uint8_t * buf);
31
32
/**********************
33
* STATIC VARIABLES
34
**********************/
35
static bool decoder_custom;
36
static const void * decoder_src;
37
static lv_img_src_t decoder_src_type;
38
static lv_img_header_t decoder_header;
39
static const lv_style_t * decoder_style;
40
#if USE_LV_FILESYSTEM
41
static lv_fs_file_t decoder_file;
42
#endif
43
#if LV_IMG_CF_INDEXED
44
static lv_color_t decoder_index_map[256];
45
#endif
46
47
static lv_img_decoder_info_f_t lv_img_decoder_info_custom;
48
static lv_img_decoder_open_f_t lv_img_decoder_open_custom;
49
static lv_img_decoder_read_line_f_t lv_img_decoder_read_line_custom;
50
static lv_img_decoder_close_f_t lv_img_decoder_close_custom;
51
52
/**********************
53
* MACROS
54
**********************/
55
56
/**********************
57
* GLOBAL FUNCTIONS
58
**********************/
59
60
/**
61
* Draw an image
62
* @param coords the coordinates of the image
63
* @param mask the image will be drawn only in this area
64
* @param src pointer to a lv_color_t array which contains the pixels of the image
65
* @param style style of the image
66
* @param opa_scale scale down all opacities by the factor
67
*/
68
void lv_draw_img(const lv_area_t * coords, const lv_area_t * mask,
69
const void * src, const lv_style_t * style, lv_opa_t opa_scale)
70
{
71
if(src == NULL) {
72
LV_LOG_WARN("Image draw: src is NULL");
73
lv_draw_rect(coords, mask, &lv_style_plain, LV_OPA_COVER);
74
lv_draw_label(coords, mask, &lv_style_plain, LV_OPA_COVER, "No\ndata", LV_TXT_FLAG_NONE, NULL);
75
return;
76
}
77
78
lv_res_t res;
79
res = lv_img_draw_core(coords, mask, src, style, opa_scale);
80
81
if(res == LV_RES_INV) {
82
LV_LOG_WARN("Image draw error");
83
lv_draw_rect(coords, mask, &lv_style_plain, LV_OPA_COVER);
84
lv_draw_label(coords, mask, &lv_style_plain, LV_OPA_COVER, "No\ndata", LV_TXT_FLAG_NONE, NULL);
85
return;
86
}
87
}
88
89
90
/**
91
*
92
* @param src
93
* @param header
94
* @param style
95
* @return
96
*/
97
lv_res_t lv_img_dsc_get_info(const char * src, lv_img_header_t * header)
98
{
99
header->always_zero = 0;
100
/*Try to get info with the custom functions first*/
101
if(lv_img_decoder_info_custom) {
102
lv_res_t custom_res;
103
custom_res = lv_img_decoder_info_custom(src, header);
104
if(custom_res == LV_RES_OK) return LV_RES_OK; /*Custom info has supported this source*/
105
}
106
107
lv_img_src_t src_type = lv_img_src_get_type(src);
108
if(src_type == LV_IMG_SRC_VARIABLE) {
109
header->w = ((lv_img_dsc_t *)src)->header.w;
110
header->h = ((lv_img_dsc_t *)src)->header.h;
111
header->cf = ((lv_img_dsc_t *)src)->header.cf;
112
}
113
#if USE_LV_FILESYSTEM
114
else if(src_type == LV_IMG_SRC_FILE) {
115
lv_fs_file_t file;
116
lv_fs_res_t res;
117
uint32_t rn;
118
res = lv_fs_open(&file, src, LV_FS_MODE_RD);
119
if(res == LV_FS_RES_OK) {
120
res = lv_fs_read(&file, header, sizeof(lv_img_header_t), &rn);
121
}
122
123
/*Create a dummy header on fs error*/
124
if(res != LV_FS_RES_OK || rn != sizeof(lv_img_header_t)) {
125
header->w = LV_DPI;
126
header->h = LV_DPI;
127
header->cf = LV_IMG_CF_UNKOWN;
128
}
129
130
lv_fs_close(&file);
131
}
132
#endif
133
else if(src_type == LV_IMG_SRC_SYMBOL) {
134
/*The size depend on the font but it is unknown here. It should be handled outside of the function*/
135
header->w = 1;
136
header->h = 1;
137
/* Symbols always have transparent parts. Important because of cover check in the design function.
138
* The actual value doesn't matter because lv_draw_label will draw it*/
139
header->cf = LV_IMG_CF_ALPHA_1BIT;
140
} else {
141
LV_LOG_WARN("Image get info found unknown src type");
142
return false;
143
}
144
return true;
145
146
}
147
148
uint8_t lv_img_color_format_get_px_size(lv_img_cf_t cf)
149
{
150
uint8_t px_size = 0;
151
152
switch(cf) {
153
case LV_IMG_CF_UNKOWN:
154
case LV_IMG_CF_RAW:
155
px_size = 0;
156
break;
157
case LV_IMG_CF_TRUE_COLOR:
158
case LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED:
159
px_size = LV_COLOR_SIZE;
160
break;
161
case LV_IMG_CF_TRUE_COLOR_ALPHA:
162
px_size = LV_IMG_PX_SIZE_ALPHA_BYTE << 3;
163
break;
164
case LV_IMG_CF_INDEXED_1BIT:
165
case LV_IMG_CF_ALPHA_1BIT:
166
px_size = 1;
167
break;
168
case LV_IMG_CF_INDEXED_2BIT:
169
case LV_IMG_CF_ALPHA_2BIT:
170
px_size = 2;
171
break;
172
case LV_IMG_CF_INDEXED_4BIT:
173
case LV_IMG_CF_ALPHA_4BIT:
174
px_size = 4;
175
break;
176
case LV_IMG_CF_INDEXED_8BIT:
177
case LV_IMG_CF_ALPHA_8BIT:
178
px_size = 8;
179
break;
180
default:
181
px_size = 0;
182
break;
183
}
184
185
return px_size;
186
}
187
188
bool lv_img_color_format_is_chroma_keyed(lv_img_cf_t cf)
189
{
190
bool is_chroma_keyed = false;
191
192
switch(cf) {
193
case LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED:
194
case LV_IMG_CF_RAW_CHROMA_KEYED:
195
case LV_IMG_CF_INDEXED_1BIT:
196
case LV_IMG_CF_INDEXED_2BIT:
197
case LV_IMG_CF_INDEXED_4BIT:
198
case LV_IMG_CF_INDEXED_8BIT:
199
is_chroma_keyed = true;
200
break;
201
default:
202
is_chroma_keyed = false;
203
break;
204
}
205
206
return is_chroma_keyed;
207
}
208
209
210
bool lv_img_color_format_has_alpha(lv_img_cf_t cf)
211
{
212
bool has_alpha = false;
213
214
switch(cf) {
215
case LV_IMG_CF_TRUE_COLOR_ALPHA:
216
case LV_IMG_CF_RAW_ALPHA:
217
case LV_IMG_CF_ALPHA_1BIT:
218
case LV_IMG_CF_ALPHA_2BIT:
219
case LV_IMG_CF_ALPHA_4BIT:
220
case LV_IMG_CF_ALPHA_8BIT:
221
has_alpha = true;
222
break;
223
default:
224
has_alpha = false;
225
break;
226
}
227
228
return has_alpha;
229
}
230
231
/**
232
* Get the type of an image source
233
* @param src pointer to an image source:
234
* - pointer to an 'lv_img_t' variable (image stored internally and compiled into the code)
235
* - a path to a file (e.g. "S:/folder/image.bin")
236
* - or a symbol (e.g. SYMBOL_CLOSE)
237
* @return type of the image source LV_IMG_SRC_VARIABLE/FILE/SYMBOL/UNKOWN
238
*/
239
lv_img_src_t lv_img_src_get_type(const void * src)
240
{
241
lv_img_src_t img_src_type = LV_IMG_SRC_UNKNOWN;
242
243
if(src == NULL) return img_src_type;
244
const uint8_t * u8_p = src;
245
246
/*The first byte shows the type of the image source*/
247
if(u8_p[0] >= 0x20 && u8_p[0] <= 0x7F) {
248
img_src_type = LV_IMG_SRC_FILE; /*If it's an ASCII character then it's file name*/
249
} else if(u8_p[0] >= 0x80) {
250
img_src_type = LV_IMG_SRC_SYMBOL; /*Symbols begins after 0x7F*/
251
} else {
252
img_src_type = LV_IMG_SRC_VARIABLE; /*`lv_img_dsc_t` is design to the first byte < 0x20*/
253
}
254
255
if (LV_IMG_SRC_UNKNOWN == img_src_type) {
256
LV_LOG_WARN("lv_img_src_get_type: unknown image type");
257
}
258
259
return img_src_type;
260
}
261
262
/**
263
* Set custom decoder functions. See the typdefs of the function typed above for more info about them
264
* @param info_fp info get function
265
* @param open_fp open function
266
* @param read_fp read line function
267
* @param close_fp clode function
268
*/
269
void lv_img_decoder_set_custom(lv_img_decoder_info_f_t info_fp, lv_img_decoder_open_f_t open_fp,
270
lv_img_decoder_read_line_f_t read_fp, lv_img_decoder_close_f_t close_fp)
271
{
272
lv_img_decoder_info_custom = info_fp;
273
lv_img_decoder_open_custom = open_fp;
274
lv_img_decoder_read_line_custom = read_fp;
275
lv_img_decoder_close_custom = close_fp;
276
}
277
278
279
/**********************
280
* STATIC FUNCTIONS
281
**********************/
282
283
284
static lv_res_t lv_img_draw_core(const lv_area_t * coords, const lv_area_t * mask,
285
const void * src, const lv_style_t * style, lv_opa_t opa_scale)
286
{
287
288
lv_area_t mask_com; /*Common area of mask and coords*/
289
bool union_ok;
290
union_ok = lv_area_intersect(&mask_com, mask, coords);
291
if(union_ok == false) {
292
return LV_RES_OK; /*Out of mask. There is nothing to draw so the image is drawn successfully.*/
293
}
294
295
lv_opa_t opa = opa_scale == LV_OPA_COVER ? style->image.opa : (uint16_t)((uint16_t) style->image.opa * opa_scale) >> 8;
296
297
lv_img_header_t header;
298
lv_res_t header_res;
299
header_res = lv_img_dsc_get_info(src, &header);
300
if(header_res != LV_RES_OK) {
301
LV_LOG_WARN("Image draw can't get image info");
302
lv_img_decoder_close();
303
return LV_RES_INV;
304
}
305
306
bool chroma_keyed = lv_img_color_format_is_chroma_keyed(header.cf);
307
bool alpha_byte = lv_img_color_format_has_alpha(header.cf);
308
309
const uint8_t * img_data = lv_img_decoder_open(src, style);
310
if(img_data == LV_IMG_DECODER_OPEN_FAIL) {
311
LV_LOG_WARN("Image draw cannot open the image resource");
312
lv_img_decoder_close();
313
return LV_RES_INV;
314
}
315
316
/* The decoder open could open the image and gave the entire uncompressed image.
317
* Just draw it!*/
318
if(img_data) {
319
map_fp(coords, mask, img_data, opa, chroma_keyed, alpha_byte, style->image.color, style->image.intense);
320
}
321
/* The whole uncompressed image is not available. Try to read it line-by-line*/
322
else {
323
lv_coord_t width = lv_area_get_width(&mask_com);
324
325
#if LV_COMPILER_VLA_SUPPORTED
326
uint8_t buf[(lv_area_get_width(&mask_com) * ((LV_COLOR_DEPTH >> 3) + 1))];
327
#else
328
uint8_t buf[LV_HOR_RES * ((LV_COLOR_DEPTH >> 3) + 1)]; /*+1 because of the possible alpha byte*/
329
#endif
330
lv_area_t line;
331
lv_area_copy(&line, &mask_com);
332
lv_area_set_height(&line, 1);
333
lv_coord_t x = mask_com.x1 - coords->x1;
334
lv_coord_t y = mask_com.y1 - coords->y1;
335
lv_coord_t row;
336
lv_res_t read_res;
337
for(row = mask_com.y1; row <= mask_com.y2; row++) {
338
read_res = lv_img_decoder_read_line(x, y, width, buf);
339
if(read_res != LV_RES_OK) {
340
lv_img_decoder_close();
341
LV_LOG_WARN("Image draw can't read the line");
342
return LV_RES_INV;
343
}
344
map_fp(&line, mask, buf, opa, chroma_keyed, alpha_byte, style->image.color, style->image.intense);
345
line.y1++;
346
line.y2++;
347
y++;
348
}
349
}
350
351
lv_img_decoder_close();
352
353
return LV_RES_OK;
354
}
355
356
357
static const uint8_t * lv_img_decoder_open(const void * src, const lv_style_t * style)
358
{
359
decoder_custom = false;
360
361
/*Try to open with the custom functions first*/
362
if(lv_img_decoder_open_custom) {
363
const uint8_t * custom_res;
364
custom_res = lv_img_decoder_open_custom(src, style);
365
if(custom_res != LV_IMG_DECODER_OPEN_FAIL) {
366
decoder_custom = true; /*Mark that custom decoder function should be used for this img source.*/
367
return custom_res; /*Custom open supported this source*/
368
}
369
}
370
371
decoder_src = src;
372
decoder_style = style;
373
decoder_src_type = lv_img_src_get_type(src);
374
375
lv_res_t header_res;
376
header_res = lv_img_dsc_get_info(src, &decoder_header);
377
if(header_res == LV_RES_INV) {
378
decoder_src = NULL;
379
decoder_src_type = LV_IMG_SRC_UNKNOWN;
380
LV_LOG_WARN("Built-in image decoder can't get the header info");
381
return LV_IMG_DECODER_OPEN_FAIL;
382
}
383
384
/*Open the file if it's a file*/
385
if(decoder_src_type == LV_IMG_SRC_FILE) {
386
#if USE_LV_FILESYSTEM
387
lv_fs_res_t res = lv_fs_open(&decoder_file, src, LV_FS_MODE_RD);
388
if(res != LV_FS_RES_OK) {
389
LV_LOG_WARN("Built-in image decoder can't open the file");
390
return LV_IMG_DECODER_OPEN_FAIL;
391
}
392
#else
393
LV_LOG_WARN("Image built-in decoder can read file because USE_LV_FILESYSTEM = 0");
394
return LV_IMG_DECODER_OPEN_FAIL;
395
#endif
396
}
397
398
399
/*Process the different color formats*/
400
lv_img_cf_t cf = decoder_header.cf;
401
if(cf == LV_IMG_CF_TRUE_COLOR ||
402
cf == LV_IMG_CF_TRUE_COLOR_ALPHA ||
403
cf == LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED) {
404
if(decoder_src_type == LV_IMG_SRC_VARIABLE) {
405
/*In case of uncompressed formats if the image stored in the ROM/RAM simply give it's pointer*/
406
return ((lv_img_dsc_t *)decoder_src)->data;
407
} else {
408
/*If it's file it need to be read line by line later*/
409
return NULL;
410
}
411
} else if(cf == LV_IMG_CF_INDEXED_1BIT ||
412
cf == LV_IMG_CF_INDEXED_2BIT ||
413
cf == LV_IMG_CF_INDEXED_4BIT ||
414
cf == LV_IMG_CF_INDEXED_8BIT) {
415
416
#if LV_IMG_CF_INDEXED
417
#if USE_LV_FILESYSTEM
418
lv_color32_t palette_file[256];
419
#endif
420
421
lv_color32_t * palette_p = NULL;
422
uint8_t px_size = lv_img_color_format_get_px_size(cf);
423
uint32_t palette_size = 1 << px_size;
424
425
if(decoder_src_type == LV_IMG_SRC_FILE) {
426
/*Read the palette from file*/
427
#if USE_LV_FILESYSTEM
428
lv_fs_seek(&decoder_file, 4); /*Skip the header*/
429
lv_fs_read(&decoder_file, palette_file, palette_size * sizeof(lv_color32_t), NULL);
430
palette_p = palette_file;
431
#else
432
LV_LOG_WARN("Image built-in decoder can read the palette because USE_LV_FILESYSTEM = 0");
433
return LV_IMG_DECODER_OPEN_FAIL;
434
#endif
435
} else {
436
/*The palette begins in the beginning of the image data. Just point to it.*/
437
palette_p = (lv_color32_t *)((lv_img_dsc_t *)decoder_src)->data;
438
}
439
440
uint32_t i;
441
for(i = 0; i < palette_size; i++) {
442
decoder_index_map[i] = LV_COLOR_MAKE(palette_p[i].red, palette_p[i].green, palette_p[i].blue);
443
}
444
return NULL;
445
#else
446
LV_LOG_WARN("Indexed (palette) images are not enabled in lv_conf.h. See LV_IMG_CF_INDEXED");
447
return LV_IMG_DECODER_OPEN_FAIL;
448
#endif
449
} else if(cf == LV_IMG_CF_ALPHA_1BIT ||
450
cf == LV_IMG_CF_ALPHA_2BIT ||
451
cf == LV_IMG_CF_ALPHA_4BIT ||
452
cf == LV_IMG_CF_ALPHA_8BIT) {
453
#if LV_IMG_CF_ALPHA
454
return NULL; /*Nothing to process*/
455
#else
456
LV_LOG_WARN("Alpha indexed images are not enabled in lv_conf.h. See LV_IMG_CF_ALPHA");
457
return LV_IMG_DECODER_OPEN_FAIL;
458
#endif
459
} else {
460
LV_LOG_WARN("Image decoder open: unknown color format")
461
return LV_IMG_DECODER_OPEN_FAIL;
462
}
463
}
464
465
466
static lv_res_t lv_img_decoder_read_line(lv_coord_t x, lv_coord_t y, lv_coord_t len, uint8_t * buf)
467
{
468
/*Try to read the line with the custom functions*/
469
if(decoder_custom) {
470
if(lv_img_decoder_read_line_custom) {
471
lv_res_t custom_res;
472
custom_res = lv_img_decoder_read_line_custom(x, y, len, buf);
473
return custom_res;
474
} else {
475
LV_LOG_WARN("Image open with custom decoder but read not supported")
476
}
477
return LV_RES_INV; /*It"s an error if not returned earlier*/
478
}
479
480
if(decoder_src_type == LV_IMG_SRC_FILE) {
481
#if USE_LV_FILESYSTEM
482
uint8_t px_size = lv_img_color_format_get_px_size(decoder_header.cf);
483
484
lv_fs_res_t res;
485
486
if(decoder_header.cf == LV_IMG_CF_TRUE_COLOR ||
487
decoder_header.cf == LV_IMG_CF_TRUE_COLOR_ALPHA ||
488
decoder_header.cf == LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED) {
489
uint32_t pos = ((y * decoder_header.w + x) * px_size) >> 3;
490
pos += 4; /*Skip the header*/
491
res = lv_fs_seek(&decoder_file, pos);
492
if(res != LV_FS_RES_OK) {
493
LV_LOG_WARN("Built-in image decoder seek failed");
494
return false;
495
}
496
uint32_t btr = len * (px_size >> 3);
497
uint32_t br = 0;
498
lv_fs_read(&decoder_file, buf, btr, &br);
499
if(res != LV_FS_RES_OK || btr != br) {
500
LV_LOG_WARN("Built-in image decoder read failed");
501
return false;
502
}
503
} else if(decoder_header.cf == LV_IMG_CF_ALPHA_1BIT ||
504
decoder_header.cf == LV_IMG_CF_ALPHA_2BIT ||
505
decoder_header.cf == LV_IMG_CF_ALPHA_4BIT ||
506
decoder_header.cf == LV_IMG_CF_ALPHA_8BIT) {
507
508
lv_img_built_in_decoder_line_alpha(x, y, len, buf);
509
} else if(decoder_header.cf == LV_IMG_CF_INDEXED_1BIT ||
510
decoder_header.cf == LV_IMG_CF_INDEXED_2BIT ||
511
decoder_header.cf == LV_IMG_CF_INDEXED_4BIT ||
512
decoder_header.cf == LV_IMG_CF_INDEXED_8BIT) {
513
lv_img_built_in_decoder_line_indexed(x, y, len, buf);
514
} else {
515
LV_LOG_WARN("Built-in image decoder read not supports the color format");
516
return false;
517
}
518
#else
519
LV_LOG_WARN("Image built-in decoder can't read file because USE_LV_FILESYSTEM = 0");
520
return false;
521
#endif
522
} else if(decoder_src_type == LV_IMG_SRC_VARIABLE) {
523
const lv_img_dsc_t * img_dsc = decoder_src;
524
525
if(img_dsc->header.cf == LV_IMG_CF_ALPHA_1BIT ||
526
img_dsc->header.cf == LV_IMG_CF_ALPHA_2BIT ||
527
img_dsc->header.cf == LV_IMG_CF_ALPHA_4BIT ||
528
img_dsc->header.cf == LV_IMG_CF_ALPHA_8BIT) {
529
lv_img_built_in_decoder_line_alpha(x, y, len, buf);
530
} else if(img_dsc->header.cf == LV_IMG_CF_INDEXED_1BIT ||
531
img_dsc->header.cf == LV_IMG_CF_INDEXED_2BIT ||
532
img_dsc->header.cf == LV_IMG_CF_INDEXED_4BIT ||
533
img_dsc->header.cf == LV_IMG_CF_INDEXED_8BIT) {
534
lv_img_built_in_decoder_line_indexed(x, y, len, buf);
535
} else {
536
LV_LOG_WARN("Built-in image decoder not supports the color format");
537
return false;
538
}
539
}
540
541
return true;
542
}
543
544
static void lv_img_decoder_close(void)
545
{
546
/*Try to close with the custom functions*/
547
if(decoder_custom) {
548
if(lv_img_decoder_close_custom) lv_img_decoder_close_custom();
549
return;
550
}
551
552
/*It was opened with built-in decoder*/
553
if(decoder_src) {
554
#if USE_LV_FILESYSTEM
555
if(decoder_src_type == LV_IMG_SRC_FILE) {
556
lv_fs_close(&decoder_file);
557
}
558
#endif
559
decoder_src_type = LV_IMG_SRC_UNKNOWN;
560
decoder_src = NULL;
561
}
562
}
563
564
static lv_res_t lv_img_built_in_decoder_line_alpha(lv_coord_t x, lv_coord_t y, lv_coord_t len, uint8_t * buf)
565
{
566
567
#if LV_IMG_CF_ALPHA
568
const lv_opa_t alpha1_opa_table[2] = {0, 255}; /*Opacity mapping with bpp = 1 (Just for compatibility)*/
569
const lv_opa_t alpha2_opa_table[4] = {0, 85, 170, 255}; /*Opacity mapping with bpp = 2*/
570
const lv_opa_t alpha4_opa_table[16] = {0, 17, 34, 51, /*Opacity mapping with bpp = 4*/
571
68, 85, 102, 119,
572
136, 153, 170, 187,
573
204, 221, 238, 255
574
};
575
576
/*Simply fill the buffer with the color. Later only the alpha value will be modified.*/
577
lv_color_t bg_color = decoder_style->image.color;
578
lv_coord_t i;
579
for(i = 0; i < len; i++) {
580
#if LV_COLOR_DEPTH == 8 || LV_COLOR_DEPTH == 1
581
buf[i * LV_IMG_PX_SIZE_ALPHA_BYTE] = bg_color.full;
582
#elif LV_COLOR_DEPTH == 16
583
/*Because of Alpha byte 16 bit color can start on odd address which can cause crash*/
584
buf[i * LV_IMG_PX_SIZE_ALPHA_BYTE] = bg_color.full & 0xFF;
585
buf[i * LV_IMG_PX_SIZE_ALPHA_BYTE + 1] = (bg_color.full >> 8) & 0xFF;
586
#elif LV_COLOR_DEPTH == 32
587
*((uint32_t *)&buf[i * LV_IMG_PX_SIZE_ALPHA_BYTE]) = bg_color.full;
588
#else
589
#error "Invalid LV_COLOR_DEPTH. Check it in lv_conf.h"
590
#endif
591
}
592
593
const lv_opa_t * opa_table = NULL;
594
uint8_t px_size = lv_img_color_format_get_px_size(decoder_header.cf);
595
uint16_t mask = (1 << px_size) - 1; /*E.g. px_size = 2; mask = 0x03*/
596
597
lv_coord_t w = 0;
598
uint32_t ofs = 0;
599
int8_t pos = 0;
600
switch(decoder_header.cf) {
601
case LV_IMG_CF_ALPHA_1BIT:
602
w = (decoder_header.w >> 3); /*E.g. w = 20 -> w = 2 + 1*/
603
if(decoder_header.w & 0x7) w++;
604
ofs += w * y + (x >> 3); /*First pixel*/
605
pos = 7 - (x & 0x7);
606
opa_table = alpha1_opa_table;
607
break;
608
case LV_IMG_CF_ALPHA_2BIT:
609
w = (decoder_header.w >> 2); /*E.g. w = 13 -> w = 3 + 1 (bytes)*/
610
if(decoder_header.w & 0x3) w++;
611
ofs += w * y + (x >> 2); /*First pixel*/
612
pos = 6 - ((x & 0x3) * 2);
613
opa_table = alpha2_opa_table;
614
break;
615
case LV_IMG_CF_ALPHA_4BIT:
616
w = (decoder_header.w >> 1); /*E.g. w = 13 -> w = 6 + 1 (bytes)*/
617
if(decoder_header.w & 0x1) w++;
618
ofs += w * y + (x >> 1); /*First pixel*/
619
pos = 4 - ((x & 0x1) * 4);
620
opa_table = alpha4_opa_table;
621
break;
622
case LV_IMG_CF_ALPHA_8BIT:
623
w = decoder_header.w; /*E.g. x = 7 -> w = 7 (bytes)*/
624
ofs += w * y + x; /*First pixel*/
625
pos = 0;
626
break;
627
}
628
629
#if USE_LV_FILESYSTEM
630
# if LV_COMPILER_VLA_SUPPORTED
631
uint8_t fs_buf[w];
632
# else
633
uint8_t fs_buf[LV_HOR_RES];
634
# endif
635
#endif
636
const uint8_t * data_tmp = NULL;
637
if(decoder_src_type == LV_IMG_SRC_VARIABLE) {
638
const lv_img_dsc_t * img_dsc = decoder_src;
639
data_tmp = img_dsc->data + ofs;
640
} else {
641
#if USE_LV_FILESYSTEM
642
lv_fs_seek(&decoder_file, ofs + 4); /*+4 to skip the header*/
643
lv_fs_read(&decoder_file, fs_buf, w, NULL);
644
data_tmp = fs_buf;
645
#else
646
LV_LOG_WARN("Image built-in alpha line reader can't read file because USE_LV_FILESYSTEM = 0");
647
data_tmp = NULL; /*To avoid warnings*/
648
return LV_RES_INV;
649
#endif
650
}
651
652
653
uint8_t byte_act = 0;
654
uint8_t val_act;
655
for(i = 0; i < len; i ++) {
656
val_act = (data_tmp[byte_act] & (mask << pos)) >> pos;
657
658
buf[i * LV_IMG_PX_SIZE_ALPHA_BYTE + LV_IMG_PX_SIZE_ALPHA_BYTE - 1] =
659
decoder_header.cf == LV_IMG_CF_ALPHA_8BIT ? val_act : opa_table[val_act];
660
661
pos -= px_size;
662
if(pos < 0) {
663
pos = 8 - px_size;
664
data_tmp++;
665
}
666
}
667
668
return LV_RES_OK;
669
670
#else
671
LV_LOG_WARN("Image built-in alpha line reader failed because LV_IMG_CF_ALPHA is 0 in lv_conf.h");
672
return LV_RES_INV;
673
#endif
674
}
675
676
static lv_res_t lv_img_built_in_decoder_line_indexed(lv_coord_t x, lv_coord_t y, lv_coord_t len, uint8_t * buf)
677
{
678
679
#if LV_IMG_CF_INDEXED
680
uint8_t px_size = lv_img_color_format_get_px_size(decoder_header.cf);
681
uint16_t mask = (1 << px_size) - 1; /*E.g. px_size = 2; mask = 0x03*/
682
683
lv_coord_t w = 0;
684
int8_t pos = 0;
685
uint32_t ofs = 0;
686
switch(decoder_header.cf) {
687
case LV_IMG_CF_INDEXED_1BIT:
688
w = (decoder_header.w >> 3); /*E.g. w = 20 -> w = 2 + 1*/
689
if(decoder_header.w & 0x7) w++;
690
ofs += w * y + (x >> 3); /*First pixel*/
691
ofs += 8; /*Skip the palette*/
692
pos = 7 - (x & 0x7);
693
break;
694
case LV_IMG_CF_INDEXED_2BIT:
695
w = (decoder_header.w >> 2); /*E.g. w = 13 -> w = 3 + 1 (bytes)*/
696
if(decoder_header.w & 0x3) w++;
697
ofs += w * y + (x >> 2); /*First pixel*/
698
ofs += 16; /*Skip the palette*/
699
pos = 6 - ((x & 0x3) * 2);
700
break;
701
case LV_IMG_CF_INDEXED_4BIT:
702
w = (decoder_header.w >> 1); /*E.g. w = 13 -> w = 6 + 1 (bytes)*/
703
if(decoder_header.w & 0x1) w++;
704
ofs += w * y + (x >> 1); /*First pixel*/
705
ofs += 64; /*Skip the palette*/
706
pos = 4 - ((x & 0x1) * 4);
707
break;
708
case LV_IMG_CF_INDEXED_8BIT:
709
w = decoder_header.w; /*E.g. x = 7 -> w = 7 (bytes)*/
710
ofs += w * y + x; /*First pixel*/
711
ofs += 1024; /*Skip the palette*/
712
pos = 0;
713
break;
714
}
715
716
#if USE_LV_FILESYSTEM
717
# if LV_COMPILER_VLA_SUPPORTED
718
uint8_t fs_buf[w];
719
# else
720
uint8_t fs_buf[LV_HOR_RES];
721
# endif
722
#endif
723
const uint8_t * data_tmp = NULL;
724
if(decoder_src_type == LV_IMG_SRC_VARIABLE) {
725
const lv_img_dsc_t * img_dsc = decoder_src;
726
data_tmp = img_dsc->data + ofs;
727
} else {
728
#if USE_LV_FILESYSTEM
729
lv_fs_seek(&decoder_file, ofs + 4); /*+4 to skip the header*/
730
lv_fs_read(&decoder_file, fs_buf, w, NULL);
731
data_tmp = fs_buf;
732
#else
733
LV_LOG_WARN("Image built-in indexed line reader can't read file because USE_LV_FILESYSTEM = 0");
734
data_tmp = NULL; /*To avoid warnings*/
735
return LV_RES_INV;
736
#endif
737
}
738
739
uint8_t byte_act = 0;
740
uint8_t val_act;
741
lv_coord_t i;
742
lv_color_t * cbuf = (lv_color_t *) buf;
743
for(i = 0; i < len; i ++) {
744
val_act = (data_tmp[byte_act] & (mask << pos)) >> pos;
745
cbuf[i] = decoder_index_map[val_act];
746
747
pos -= px_size;
748
if(pos < 0) {
749
pos = 8 - px_size;
750
data_tmp++;
751
}
752
}
753
754
return LV_RES_OK;
755
#else
756
LV_LOG_WARN("Image built-in indexed line reader failed because LV_IMG_CF_INDEXED is 0 in lv_conf.h");
757
return LV_RES_INV;
758
#endif
759
}
760
761