forked from lvgl-micropython/lvgl_micropython
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlib_lvgl_lv_bmp.c.patch
More file actions
257 lines (241 loc) · 9.4 KB
/
Copy pathlib_lvgl_lv_bmp.c.patch
File metadata and controls
257 lines (241 loc) · 9.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
diff --git a/src/libs/bmp/lv_bmp.c b/src/libs/bmp/lv_bmp.c
index aeb9dad2..ef66b28e 100644
--- a/src/libs/bmp/lv_bmp.c
+++ b/src/libs/bmp/lv_bmp.c
@@ -21,28 +21,11 @@
#define image_cache_draw_buf_handlers &(LV_GLOBAL_DEFAULT()->image_cache_draw_buf_handlers)
-/**********************
- * TYPEDEFS
- **********************/
-
-typedef struct {
- lv_fs_file_t f;
- unsigned int px_offset;
- int px_width;
- int px_height;
- unsigned int bpp;
- int row_size_bytes;
-} bmp_dsc_t;
-
/**********************
* STATIC PROTOTYPES
**********************/
static lv_result_t decoder_info(lv_image_decoder_t * decoder, lv_image_decoder_dsc_t * src, lv_image_header_t * header);
static lv_result_t decoder_open(lv_image_decoder_t * decoder, lv_image_decoder_dsc_t * dsc);
-
-static lv_result_t decoder_get_area(lv_image_decoder_t * decoder, lv_image_decoder_dsc_t * dsc,
- const lv_area_t * full_area, lv_area_t * decoded_area);
-
static void decoder_close(lv_image_decoder_t * dec, lv_image_decoder_dsc_t * dsc);
/**********************
@@ -61,7 +44,6 @@ void lv_bmp_init(void)
lv_image_decoder_t * dec = lv_image_decoder_create();
lv_image_decoder_set_info_cb(dec, decoder_info);
lv_image_decoder_set_open_cb(dec, decoder_open);
- lv_image_decoder_set_get_area_cb(dec, decoder_get_area);
lv_image_decoder_set_close_cb(dec, decoder_close);
dec->name = DECODER_NAME;
@@ -115,16 +97,19 @@ static lv_result_t decoder_info(lv_image_decoder_t * decoder, lv_image_decoder_d
switch(bpp) {
case 16:
header->cf = LV_COLOR_FORMAT_RGB565;
+ header->stride = w * 2;
break;
case 24:
header->cf = LV_COLOR_FORMAT_RGB888;
+ header->stride = w * 3;
break;
case 32:
header->cf = LV_COLOR_FORMAT_ARGB8888;
+ header->stride = w * 4;
break;
default:
LV_LOG_WARN("Not supported bpp: %d", bpp);
- return LV_RESULT_OK;
+ return LV_RESULT_INVALID;
}
return LV_RESULT_OK;
}
@@ -139,7 +124,7 @@ static lv_result_t decoder_info(lv_image_decoder_t * decoder, lv_image_decoder_d
}
/**
- * Open a BMP image and return the decided image
+ * Open a BMP image and decode it into a draw buffer.
* @param decoder pointer to the decoder
* @param dsc pointer to the decoder descriptor
* @return LV_RESULT_OK: no error; LV_RESULT_INVALID: can't open the image
@@ -156,85 +141,111 @@ static lv_result_t decoder_open(lv_image_decoder_t * decoder, lv_image_decoder_d
return LV_RESULT_INVALID; /*Check the extension*/
}
- bmp_dsc_t b;
- lv_memset(&b, 0x00, sizeof(b));
-
- lv_fs_res_t res = lv_fs_open(&b.f, dsc->src, LV_FS_MODE_RD);
+ lv_fs_file_t f;
+ lv_fs_res_t res = lv_fs_open(&f, dsc->src, LV_FS_MODE_RD);
if(res != LV_FS_RES_OK) return LV_RESULT_INVALID;
uint8_t header[54];
- lv_fs_read(&b.f, header, 54, NULL);
+ lv_fs_read(&f, header, 54, NULL);
if(0x42 != header[0] || 0x4d != header[1]) {
- lv_fs_close(&b.f);
+ lv_fs_close(&f);
return LV_RESULT_INVALID;
}
- lv_memcpy(&b.px_offset, header + 10, 4);
- lv_memcpy(&b.px_width, header + 18, 4);
- lv_memcpy(&b.px_height, header + 22, 4);
- lv_memcpy(&b.bpp, header + 28, 2);
- b.row_size_bytes = ((b.bpp * b.px_width + 31) / 32) * 4;
+ unsigned int px_offset;
+ int px_width;
+ int px_height;
+ unsigned int bpp;
+ lv_memcpy(&px_offset, header + 10, 4);
+ lv_memcpy(&px_width, header + 18, 4);
+ lv_memcpy(&px_height, header + 22, 4);
+ lv_memcpy(&bpp, header + 28, 2);
+
+ lv_color_format_t cf;
+ switch(bpp) {
+ case 16:
+ cf = LV_COLOR_FORMAT_RGB565;
+ break;
+ case 24:
+ cf = LV_COLOR_FORMAT_RGB888;
+ break;
+ case 32:
+ cf = LV_COLOR_FORMAT_ARGB8888;
+ break;
+ default:
+ LV_LOG_WARN("Not supported bpp: %d", bpp);
+ lv_fs_close(&f);
+ return LV_RESULT_INVALID;
+ }
- dsc->user_data = lv_malloc(sizeof(bmp_dsc_t));
- LV_ASSERT_MALLOC(dsc->user_data);
- if(dsc->user_data == NULL) return LV_RESULT_INVALID;
- lv_memcpy(dsc->user_data, &b, sizeof(b));
- return LV_RESULT_OK;
- }
- /* BMP file as data not supported for simplicity.
- * Convert them to LVGL compatible C arrays directly. */
- else if(dsc->src_type == LV_IMAGE_SRC_VARIABLE) {
- return LV_RESULT_INVALID;
- }
+ if(px_width <= 0 || px_height <= 0) {
+ lv_fs_close(&f);
+ return LV_RESULT_INVALID;
+ }
- return LV_RESULT_INVALID; /*If not returned earlier then it failed*/
-}
+ int row_size_bytes = ((bpp * px_width + 31) / 32) * 4;
+ int px_size = bpp / 8;
-static lv_result_t decoder_get_area(lv_image_decoder_t * decoder, lv_image_decoder_dsc_t * dsc,
- const lv_area_t * full_area, lv_area_t * decoded_area)
-{
- LV_UNUSED(decoder);
- bmp_dsc_t * b = dsc->user_data;
- lv_draw_buf_t * decoded = (void *)dsc->decoded;
-
- if(decoded_area->y1 == LV_COORD_MIN) {
- *decoded_area = *full_area;
- decoded_area->y2 = decoded_area->y1;
- int32_t w_px = lv_area_get_width(full_area);
- lv_draw_buf_t * reshaped = lv_draw_buf_reshape(decoded, dsc->header.cf, w_px, 1, LV_STRIDE_AUTO);
- if(reshaped == NULL) {
- if(decoded != NULL) {
+ lv_draw_buf_t * decoded = lv_draw_buf_create_ex(image_cache_draw_buf_handlers, px_width, px_height, cf,
+ LV_STRIDE_AUTO);
+ if(decoded == NULL) {
+ lv_fs_close(&f);
+ return LV_RESULT_INVALID;
+ }
+
+ uint8_t * buf = decoded->data;
+ int32_t stride = decoded->header.stride;
+
+ for(int y = 0; y < px_height; y++) {
+ int file_y = (px_height - 1) - y; /*BMP images are stored upside down*/
+ uint32_t p = px_offset + row_size_bytes * file_y;
+ res = lv_fs_seek(&f, p, LV_FS_SEEK_SET);
+ if(res != LV_FS_RES_OK) {
lv_draw_buf_destroy(decoded);
- decoded = NULL;
- dsc->decoded = NULL;
+ lv_fs_close(&f);
+ return LV_RESULT_INVALID;
+ }
+ uint32_t rn;
+ res = lv_fs_read(&f, buf + y * stride, px_width * px_size, &rn);
+ if(res != LV_FS_RES_OK || rn != (uint32_t)px_width * px_size) {
+ lv_draw_buf_destroy(decoded);
+ lv_fs_close(&f);
+ return LV_RESULT_INVALID;
}
- decoded = lv_draw_buf_create_ex(image_cache_draw_buf_handlers, w_px, 1, dsc->header.cf, LV_STRIDE_AUTO);
- if(decoded == NULL) return LV_RESULT_INVALID;
- }
- else {
- decoded = reshaped;
}
+
+ lv_fs_close(&f);
+
dsc->decoded = decoded;
- }
- else {
- decoded_area->y1++;
- decoded_area->y2++;
- }
+ dsc->header.cf = cf;
+ dsc->header.w = px_width;
+ dsc->header.h = px_height;
+ dsc->header.stride = decoded->header.stride;
+
+ if(!dsc->args.no_cache && lv_image_cache_is_enabled()) {
+ lv_image_cache_data_t search_key;
+ search_key.src_type = dsc->src_type;
+ search_key.src = dsc->src;
+ search_key.slot.size = decoded->data_size;
+
+ lv_cache_entry_t * entry = lv_image_decoder_add_to_cache(decoder, &search_key, decoded, NULL);
+ if(entry == NULL) {
+ lv_draw_buf_destroy(decoded);
+ return LV_RESULT_INVALID;
+ }
+ dsc->cache_entry = entry;
+ }
- if(decoded_area->y1 > full_area->y2) {
+ return LV_RESULT_OK;
+ }
+ /* BMP file as data not supported for simplicity.
+ * Convert them to LVGL compatible C arrays directly. */
+ else if(dsc->src_type == LV_IMAGE_SRC_VARIABLE) {
return LV_RESULT_INVALID;
}
- else {
- int32_t y = (b->px_height - 1) - (decoded_area->y1); /*BMP images are stored upside down*/
- uint32_t p = b->px_offset + b->row_size_bytes * y;
- p += (decoded_area->x1) * (b->bpp / 8);
- lv_fs_seek(&b->f, p, LV_FS_SEEK_SET);
- uint32_t line_width_byte = lv_area_get_width(full_area) * (b->bpp / 8);
- lv_fs_read(&b->f, decoded->data, line_width_byte, NULL);
- return LV_RESULT_OK;
- }
+ return LV_RESULT_INVALID; /*If not returned earlier then it failed*/
}
/**
@@ -243,11 +254,8 @@ static lv_result_t decoder_get_area(lv_image_decoder_t * decoder, lv_image_decod
static void decoder_close(lv_image_decoder_t * decoder, lv_image_decoder_dsc_t * dsc)
{
LV_UNUSED(decoder);
- bmp_dsc_t * b = dsc->user_data;
- lv_fs_close(&b->f);
- lv_free(dsc->user_data);
- if(dsc->decoded) lv_draw_buf_destroy((void *)dsc->decoded);
-
+ if(dsc->args.no_cache ||
+ !lv_image_cache_is_enabled()) lv_draw_buf_destroy((lv_draw_buf_t *)dsc->decoded);
}
#endif /*LV_USE_BMP*/