Skip to content

Commit 5a96826

Browse files
committed
More functions
1 parent 2486880 commit 5a96826

File tree

1 file changed

+45
-19
lines changed

1 file changed

+45
-19
lines changed

src/mod_img_convertes.c

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22
#include "py/runtime.h"
33
#include "img_converters.h"
44

5-
// Wrapper für frame2jpg
6-
static mp_obj_t mp_frame2jpg(mp_obj_t fb_in, mp_obj_t quality_in) {
7-
if (mp_obj_get_type(fb_in) != &mp_type_memoryview) {
8-
mp_raise_TypeError("Expected a memoryview object");
5+
// Helper function to get buffer info from memoryview or bytearray
6+
static void get_buffer_info(mp_obj_t obj, mp_buffer_info_t *bufinfo) {
7+
if (mp_obj_get_type(obj) == &mp_type_memoryview || mp_obj_get_type(obj) == &mp_type_bytearray) {
8+
mp_get_buffer_raise(obj, bufinfo, MP_BUFFER_READ);
9+
} else {
10+
mp_raise_TypeError("Expected a memoryview or bytearray object");
911
}
12+
}
1013

14+
// Wrapper für frame2jpg
15+
static mp_obj_t mp_frame2jpg(mp_obj_t fb_in, mp_obj_t quality_in) {
1116
mp_buffer_info_t bufinfo;
12-
mp_get_buffer_raise(fb_in, &bufinfo, MP_BUFFER_READ);
17+
get_buffer_info(fb_in, &bufinfo);
1318
camera_fb_t *fb = (camera_fb_t *)bufinfo.buf;
1419

1520
uint8_t quality = mp_obj_get_int(quality_in);
@@ -30,12 +35,8 @@ static MP_DEFINE_CONST_FUN_OBJ_2(mp_frame2jpg_obj, mp_frame2jpg);
3035

3136
// Wrapper für frame2bmp
3237
static mp_obj_t mp_frame2bmp(mp_obj_t fb_in) {
33-
if (mp_obj_get_type(fb_in) != &mp_type_memoryview) {
34-
mp_raise_TypeError("Expected a memoryview object");
35-
}
36-
3738
mp_buffer_info_t bufinfo;
38-
mp_get_buffer_raise(fb_in, &bufinfo, MP_BUFFER_READ);
39+
get_buffer_info(fb_in, &bufinfo);
3940
camera_fb_t *fb = (camera_fb_t *)bufinfo.buf;
4041

4142
uint8_t *out = NULL;
@@ -52,18 +53,17 @@ static mp_obj_t mp_frame2bmp(mp_obj_t fb_in) {
5253
}
5354
static MP_DEFINE_CONST_FUN_OBJ_1(mp_frame2bmp_obj, mp_frame2bmp);
5455

55-
// Wrapper für fmt2rgb888
56-
static mp_obj_t mp_fmt2rgb888(mp_obj_t src_buf_in, mp_obj_t format_in) {
57-
if (mp_obj_get_type(src_buf_in) != &mp_type_memoryview) {
58-
mp_raise_TypeError("Expected a memoryview object");
59-
}
60-
56+
// Wrapper für fmt2rgb888 mit optionalem format-Argument
57+
static mp_obj_t mp_fmt2rgb888(size_t n_args, const mp_obj_t *args) {
6158
mp_buffer_info_t bufinfo;
62-
mp_get_buffer_raise(src_buf_in, &bufinfo, MP_BUFFER_READ);
59+
get_buffer_info(args[0], &bufinfo);
6360
const uint8_t *src_buf = (const uint8_t *)bufinfo.buf;
6461
size_t src_len = bufinfo.len;
6562

66-
pixformat_t format = mp_obj_get_int(format_in);
63+
pixformat_t format = PIXFORMAT_JPEG; // Standardformat ist JPEG
64+
if (n_args > 1) {
65+
format = mp_obj_get_int(args[1]);
66+
}
6767

6868
uint8_t *rgb_buf = m_new(uint8_t, bufinfo.len * 3); // Allocate buffer for RGB888
6969

@@ -76,13 +76,39 @@ static mp_obj_t mp_fmt2rgb888(mp_obj_t src_buf_in, mp_obj_t format_in) {
7676
mp_obj_t result = mp_obj_new_bytearray(src_len * 3, rgb_buf);
7777
return result;
7878
}
79-
static MP_DEFINE_CONST_FUN_OBJ_2(mp_fmt2rgb888_obj, mp_fmt2rgb888);
79+
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_fmt2rgb888_obj, 1, 2, mp_fmt2rgb888);
80+
81+
// Wrapper für jpg2rgb565 mit optionalem scale-Argument
82+
static mp_obj_t mp_jpg2rgb565(size_t n_args, const mp_obj_t *args) {
83+
mp_buffer_info_t bufinfo;
84+
get_buffer_info(args[0], &bufinfo);
85+
const uint8_t *src_buf = (const uint8_t *)bufinfo.buf;
86+
size_t src_len = bufinfo.len;
87+
88+
jpg_scale_t scale = JPG_SCALE_NONE; // Standardwert ist JPG_SCALE_NONE
89+
if (n_args > 1) {
90+
scale = mp_obj_get_int(args[1]);
91+
}
92+
93+
uint8_t *rgb_buf = m_new(uint8_t, bufinfo.len * 2); // Allocate buffer for RGB565
94+
95+
bool success = jpg2rgb565(src_buf, src_len, rgb_buf, scale);
96+
97+
if (!success) {
98+
mp_raise_msg(&mp_type_RuntimeError, "RGB565 conversion failed");
99+
}
100+
101+
mp_obj_t result = mp_obj_new_bytearray(src_len * 2, rgb_buf);
102+
return result;
103+
}
104+
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_jpg2rgb565_obj, 1, 2, mp_jpg2rgb565);
80105

81106
// Modulmethoden
82107
static const mp_rom_map_elem_t img_converters_module_globals_table[] = {
83108
{ MP_ROM_QSTR(MP_QSTR_frame2jpg), MP_ROM_PTR(&mp_frame2jpg_obj) },
84109
{ MP_ROM_QSTR(MP_QSTR_frame2bmp), MP_ROM_PTR(&mp_frame2bmp_obj) },
85110
{ MP_ROM_QSTR(MP_QSTR_fmt2rgb888), MP_ROM_PTR(&mp_fmt2rgb888_obj) },
111+
{ MP_ROM_QSTR(MP_QSTR_jpg2rgb565), MP_ROM_PTR(&mp_jpg2rgb565_obj) },
86112
};
87113

88114
static MP_DEFINE_CONST_DICT(img_converters_module_globals, img_converters_module_globals_table);

0 commit comments

Comments
 (0)