-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdraw_img.c
268 lines (241 loc) · 6.29 KB
/
draw_img.c
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
258
259
260
261
262
263
264
265
266
267
#include <draw_img.h>
#include <string.h>
#include <math.h>
void (* draw_img_acc_prepare_cb)(struct draw_img *image, gui_rect_t *rect) = NULL;
void (* draw_img_acc_end_cb)(struct draw_img *image) = NULL;
bool draw_img_target_area(draw_img_t *image, struct gui_dispdev *dc, gui_rect_t *rect,
int32_t *x_start, int32_t *x_end, int32_t *y_start, int32_t *y_end)
{
int16_t image_x = image->img_target_x;
int16_t image_y = image->img_target_y;
int16_t image_w = image->img_target_w ;
int16_t image_h = image->img_target_h ;
*x_start = _UI_MAX(image_x, dc->section.x1);
*x_end = _UI_MIN(image_x + image_w - 1, dc->section.x2);
*y_start = _UI_MAX(image_y, dc->section.y1);
*y_end = _UI_MIN(image_y + image_h - 1, dc->section.y2);
if ((*x_start >= *x_end) || (*y_start >= *y_end))
{
return false;
}
return true;
}
gui_rgb_data_head_t draw_img_get_header(draw_img_t *img, IMG_SOURCE_MODE_TYPE src_mode)
{
struct gui_rgb_data_head head = {0};
if (src_mode == IMG_SRC_FILESYS)
{
int fd = gui_fs_open(img->data, 0);
if (fd <= 0)
{
gui_log("open file fail:%s !\n", (char *)img->data);
}
gui_fs_read(fd, &head, sizeof(head));
gui_fs_close(fd);
}
else if (src_mode == IMG_SRC_FTL)
{
uint32_t base = (uint32_t)(uintptr_t)img->data;
gui_ftl_read(base, (uint8_t *)&head, sizeof(gui_rgb_data_head_t));
}
else if (src_mode == IMG_SRC_MEMADDR)
{
memcpy(&head, img->data, sizeof(head));
}
return head;
}
void draw_img_load_scale(draw_img_t *img, IMG_SOURCE_MODE_TYPE src_mode)
{
struct gui_rgb_data_head head = {0};
head = draw_img_get_header(img, src_mode);
img->img_w = head.w;
img->img_h = head.h;
}
uint32_t draw_img_get_pixel_byte(draw_img_t *img, IMG_SOURCE_MODE_TYPE src_mode)
{
struct gui_rgb_data_head head = {0};
head = draw_img_get_header(img, src_mode);
if (head.type == RGB565)
{
return 2;
}
else if (head.type == RGB888)
{
return 3;
}
else if (head.type == ARGB8565)
{
return 3;
}
else if (head.type == ARGB8888)
{
return 4;
}
return 0;
}
bool draw_img_new_area(draw_img_t *img, gui_rect_t *rect)
{
gui_point3f_t pox = {0.0f};
float x_min = 0.0f;
float x_max = 0.0f;
float y_min = 0.0f;
float y_max = 0.0f;
float x1 = 0;
float y1 = 0;
float x2 = 0;
float y2 = 0;
if (rect == NULL)
{
x1 = 0;
y1 = 0;
x2 = img->img_w - 1;
y2 = img->img_h - 1;
}
else
{
x1 = _UI_MAX(0, rect->x1);
y1 = _UI_MAX(0, rect->y1);
x2 = _UI_MIN(img->img_w - 1, rect->x2);
y2 = _UI_MIN(img->img_h - 1, rect->y2);
}
pox.p[0] = x1;
pox.p[1] = y1;
pox.p[2] = 1.0f;
matrix_multiply_point(&img->matrix, &pox);
x_min = pox.p[0];
x_max = pox.p[0];
y_min = pox.p[1];
y_max = pox.p[1];
pox.p[0] = x2;
pox.p[1] = y1;
pox.p[2] = 1.0f;
matrix_multiply_point(&img->matrix, &pox);
if (x_min > pox.p[0])
{
x_min = pox.p[0];
}
if (x_max < pox.p[0])
{
x_max = pox.p[0];
}
if (y_min > pox.p[1])
{
y_min = pox.p[1];
}
if (y_max < pox.p[1])
{
y_max = pox.p[1];
}
pox.p[0] = x2;
pox.p[1] = y2;
pox.p[2] = 1.0f;
matrix_multiply_point(&img->matrix, &pox);
if (x_min > pox.p[0])
{
x_min = pox.p[0];
}
if (x_max < pox.p[0])
{
x_max = pox.p[0];
}
if (y_min > pox.p[1])
{
y_min = pox.p[1];
}
if (y_max < pox.p[1])
{
y_max = pox.p[1];
}
pox.p[0] = x1;
pox.p[1] = y2;
pox.p[2] = 1.0f;
matrix_multiply_point(&img->matrix, &pox);
if (x_min > pox.p[0])
{
x_min = pox.p[0];
}
if (x_max < pox.p[0])
{
x_max = pox.p[0];
}
if (y_min > pox.p[1])
{
y_min = pox.p[1];
}
if (y_max < pox.p[1])
{
y_max = pox.p[1];
}
if (draw_img_acc_prepare_cb != NULL)
{
draw_img_acc_prepare_cb(img, rect);
}
img->img_target_x = (int16_t)x_min;
img->img_target_y = (int16_t)y_min;
img->img_target_w = ceil(x_max) - (int16_t)x_min + 1;
img->img_target_h = ceil(y_max) - (int16_t)y_min + 1;
return true;
}
void draw_img_cache(draw_img_t *image, IMG_SOURCE_MODE_TYPE src_mode)
{
gui_dispdev_t *dc = gui_get_dc();
if (dc->section_count != 0)
{
return;
}
if (src_mode == IMG_SRC_FILESYS)
{
int fd = gui_fs_open((const char *)image->data, 0);
uint32_t size = gui_fs_lseek(fd, 0, SEEK_END) - gui_fs_lseek(fd, 0, SEEK_SET);
uint8_t *data = (uint8_t *)gui_malloc(size);
GUI_ASSERT(data != NULL);
gui_fs_read(fd, data, size);
gui_fs_close(fd);
image->data = data;
return;
}
else if (src_mode == IMG_SRC_FTL)
{
gui_rgb_data_head_t head;
uint32_t base = (uint32_t)(uintptr_t)image->data;
gui_ftl_read(base, (uint8_t *)&head, sizeof(gui_rgb_data_head_t));
uint8_t *data = NULL;
if (head.compress == true)
{
uint32_t end = 0;
gui_ftl_read(base + sizeof(gui_rgb_data_head_t) + sizeof(imdc_file_header_t) + 4 * (head.h),
(uint8_t *)&end, 4);
uint32_t size = end + sizeof(gui_rgb_data_head_t);
data = (uint8_t *)gui_malloc(size);
GUI_ASSERT(data != NULL);
gui_ftl_read(base, data, size);
}
else
{
draw_img_get_header(image, src_mode);
uint8_t pixel_byte = draw_img_get_pixel_byte(image, src_mode);
uint32_t size = head.w * head.h * pixel_byte;
data = (uint8_t *)gui_malloc(size);
GUI_ASSERT(data != NULL);
gui_ftl_read(base, data, size);
}
image->data = data;
return;
}
else if (src_mode == IMG_SRC_MEMADDR)
{
return;
}
}
void draw_img_free(draw_img_t *img, IMG_SOURCE_MODE_TYPE src_mode)
{
gui_dispdev_t *dc = gui_get_dc();
if (dc->section_count != dc->section_total - 1)
{
return;
}
if ((src_mode == IMG_SRC_FILESYS) || (src_mode == IMG_SRC_FTL))
{
gui_free(img->data);
}
}