8000 Add qrio.QRDecode.find() to locate codes without decoding · pypewpew/circuitpython@43f4293 · GitHub
[go: up one dir, main page]

Skip to content

Commit 43f4293

Browse files
committed
Add qrio.QRDecode.find() to locate codes without decoding
Fix adafruit#8452
1 parent 843fca1 commit 43f4293

File tree

5 files changed

+103
-19
lines changed

5 files changed

+103
-19
lines changed

shared-bindings/qrio/QRDecoder.c

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ STATIC mp_obj_t qrio_qrdecoder_make_new(const mp_obj_type_t *type, size_t n_args
5656
return self;
5757
}
5858

59+
60+
STATIC void verify_buffer_size(qrio_qrdecoder_obj_t *self, mp_obj_t *buffer, size_t len, qrio_pixel_policy_t policy) {
61+
int width = shared_module_qrio_qrdecoder_get_width(self);
62+
int height = shared_module_qrio_qrdecoder_get_height(self);
63+
64+
// verify that the buffer is big enough
65+
int sz = width * height;
66+
if (policy != QRIO_EVERY_BYTE) {
67+
sz *= 2;
68+
}
69+
mp_get_index(mp_obj_get_type(*buffer), len, MP_OBJ_NEW_SMALL_INT(sz - 1), false);
70+
}
71+
5972
//| def decode(
6073
//| self, buffer: ReadableBuffer, pixel_policy: PixelPolicy = PixelPolicy.EVERY_BYTE
6174
//| ) -> List[QRInfo]:
@@ -73,21 +86,38 @@ STATIC mp_obj_t qrio_qrdecoder_decode(size_t n_args, const mp_obj_t *pos_args, m
7386

7487
mp_buffer_info_t bufinfo;
7588
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ);
89+
qrio_pixel_policy_t policy = cp_enum_value(&qrio_pixel_policy_type, args[ARG_pixel_policy].u_obj, MP_QSTR_pixel_policy);
90+
verify_buffer_size(self, &args[ARG_buffer].u_obj, bufinfo.len, policy);
7691

77-
int width = shared_module_qrio_qrdecoder_get_width(self);
78-
int height = shared_module_qrio_qrdecoder_get_height(self);
92+
return shared_module_qrio_qrdecoder_decode(self, &bufinfo, policy, true);
93+
}
94+
MP_DEFINE_CONST_FUN_OBJ_KW(qrio_qrdecoder_decode_obj, 1, qrio_qrdecoder_decode);
7995

80-
// verify that the buffer is big enough
81-
int sz = width * height;
96+
97+
//| def find(
98+
//| self, buffer: ReadableBuffer, pixel_policy: PixelPolicy = PixelPolicy.EVERY_BYTE
99+
//| ) -> List[QRPosition]:
100+
//| """Find all visible QR codes from the given image. The size of the buffer must be at least ``length``×``width`` bytes for `EVERY_BYTE`, and 2×``length``×``width`` bytes for `EVEN_BYTES` or `ODD_BYTES`."""
101+
STATIC mp_obj_t qrio_qrdecoder_find(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
102+
qrio_qrdecoder_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
103+
104+
enum { ARG_buffer, ARG_pixel_policy };
105+
static const mp_arg_t allowed_args[] = {
106+
{ MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_int = 0} },
107+
{ MP_QSTR_pixel_policy, MP_ARG_OBJ, {.u_obj = MP_ROM_PTR((mp_obj_t *)&qrio_pixel_policy_EVERY_BYTE_obj)} },
108+
};
109+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
110+
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
111+
112+
mp_buffer_info_t bufinfo;
113+
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ);
82114
qrio_pixel_policy_t policy = cp_enum_value(&qrio_pixel_policy_type, args[ARG_pixel_policy].u_obj, MP_QSTR_pixel_policy);
83-
if (policy != QRIO_EVERY_BYTE) {
84-
sz *= 2;
85-
}
86-
mp_get_index(mp_obj_get_type(args[ARG_buffer].u_obj), bufinfo.len, MP_OBJ_NEW_SMALL_INT(sz - 1), false);
115+
verify_buffer_size(self, &args[ARG_buffer].u_obj, bufinfo.len, policy);
87116

88-
return shared_module_qrio_qrdecoder_decode(self, &bufinfo, policy);
117+
return shared_module_qrio_qrdecoder_decode(self, &bufinfo, policy, false);
89118
}
90-
MP_DEFINE_CONST_FUN_OBJ_KW(qrio_qrdecoder_decode_obj, 1, qrio_qrdecoder_decode);
119+
MP_DEFINE_CONST_FUN_OBJ_KW(qrio_qrdecoder_find_obj, 1, qrio_qrdecoder_find);
120+
91121

92122
//| width: int
93123
//| """The width of image the decoder expects"""
@@ -135,6 +165,7 @@ STATIC const mp_rom_map_elem_t qrio_qrdecoder_locals_table[] = {
135165
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&qrio_qrdecoder_width_obj) },
136166
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&qrio_qrdecoder_height_obj) },
137167
{ MP_ROM_QSTR(MP_QSTR_decode), MP_ROM_PTR(&qrio_qrdecoder_decode_obj) },
168+
{ MP_ROM_QSTR(MP_QSTR_find), MP_ROM_PTR(&qrio_qrdecoder_find_obj) },
138169
};
139170

140171
STATIC MP_DEFINE_CONST_DICT(qrio_qrdecoder_locals, qrio_qrdecoder_locals_table);

shared-bindings/qrio/QRInfo.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,39 @@ const mp_obj_namedtuple_type_t qrio_qrinfo_type_obj = {
6363
MP_QSTR_data_type,
6464
},
6565
};
66+
67+
//| class QRPosition:
68+
//| """Information about a non-decoded QR code"""
69+
//|
70+
71+
const mp_obj_namedtuple_type_t qrio_qrposition_type_obj = {
72+
.base = {
73+
.base = {
74+
.type = &mp_type_type
75+
},
76+
.flags = MP_TYPE_FLAG_EXTENDED,
77+
.name = MP_QSTR_QRPosition,
78+
.print = namedtuple_print,
79+
.parent = &mp_type_tuple,
80+
.make_new = namedtuple_make_new,
81+
.attr = namedtuple_attr,
82+
MP_TYPE_EXTENDED_FI 3419 ELDS(
83+
.unary_op = mp_obj_tuple_unary_op,
84+
.binary_op = mp_obj_tuple_binary_op,
85+
.subscr = mp_obj_tuple_subscr,
86+
.getiter = mp_obj_tuple_getiter,
87+
),
88+
},
89+
.n_fields = 9,
90+
.fields = {
91+
MP_QSTR_top_left_x,
92+
MP_QSTR_top_left_y,
93+
MP_QSTR_top_right_x,
94+
MP_QSTR_top_right_y,
95+
MP_QSTR_bottom_right_x,
96+
MP_QSTR_bottom_right_y,
97+
MP_QSTR_bottom_left_x,
98+
MP_QSTR_bottom_left_y,
99+
MP_QSTR_size,
100+
},
101+
};

shared-bindings/qrio/QRInfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@
2929
#include "py/objnamedtuple.h"
3030

3131
extern const mp_obj_namedtuple_type_t qrio_qrinfo_type_obj;
32+
extern const mp_obj_namedtuple_type_t qrio_qrposition_type_obj;

shared-module/qrio/QRDecoder.c

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ STATIC mp_obj_t data_type(int type) {
9898
return mp_obj_new_int(type);
9999
}
100100

101-
mp_obj_t shared_module_qrio_qrdecoder_decode(qrdecoder_qrdecoder_obj_t *self, const mp_buffer_info_t *bufinfo, qrio_pixel_policy_t policy) {
101+
mp_obj_t shared_module_qrio_qrdecoder_decode(qrdecoder_qrdecoder_obj_t *self, const mp_buffer_info_t *bufinfo, qrio_pixel_policy_t policy, bool decode) {
102102
int width, height;
103103
uint8_t *framebuffer = quirc_begin(self->quirc, &width, &height);
104104
uint8_t *src = bufinfo->buf;
@@ -138,14 +138,30 @@ mp_obj_t shared_module_qrio_qrdecoder_decode(qrdecoder_qrdecoder_obj_t *self, co
138138
mp_obj_t result = mp_obj_new_list(0, NULL);
139139
for (int i = 0; i < count; i++) {
140140
quirc_extract(self->quirc, i, &self->code);
141-
if (quirc_decode(&self->code, &self->data) != QUIRC_SUCCESS) {
142-
continue;
141+
mp_obj_t code_obj;
142+
if (decode) {
143+
if (quirc_decode(&self->code, &self->data) != QUIRC_SUCCESS) {
144+
continue;
145+
}
146+
mp_obj_t elems[2] = {
147+
mp_obj_new_bytes(self->data.payload, self->data.payload_len),
148+
data_type(self->data.data_type),
149+
};
150+
code_obj = namedtuple_make_new((const mp_obj_type_t *)&qrio_qrinfo_type_obj, 2, 0, elems);
151+
} else {
152+
mp_obj_t elems[9] = {
153+
mp_obj_new_int(self->code.corners[0].x),
154+
mp_obj_new_int(self->code.corners[0].y),
155+
mp_obj_new_int(self->code.corners[1].x),
156+
mp_obj_new_int(self->code.corners[1].y),
157+
mp_obj_new_int(self->code.corners[2].x),
158+
mp_obj_new_int(self->code.corners[2].y),
159+
mp_obj_new_int(self->code.corners[3].x),
160+
mp_obj_new_int(self->code.corners[3].y),
161+
mp_obj_new_int(self->code.size),
162+
};
163+
code_obj = namedtuple_make_new((const mp_obj_type_t *)&qrio_qrposition_type_obj, 9, 0, elems);
143164
}
144-
mp_obj_t elems[2] = {
145-
mp_obj_new_bytes(self->data.payload, self->data.payload_len),
146-
data_type(self->data.data_type),
147-
};
148-
mp_obj_t code_obj = namedtuple_make_new((const mp_obj_type_t *)&qrio_qrinfo_type_obj, 2, 0, elems);
149165
mp_obj_list_append(result, code_obj);
150166
}
151167
return result;

shared-module/qrio/QRDecoder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ int shared_module_qrio_qrdecoder_get_height(qrdecoder_qrdecoder_obj_t *);
4242
int shared_module_qrio_qrdecoder_get_width(qrdecoder_qrdecoder_obj_t *);
4343
void shared_module_qrio_qrdecoder_set_height(qrdecoder_qrdecoder_obj_t *, int height);
4444
void shared_module_qrio_qrdecoder_set_width(qrdecoder_qrdecoder_obj_t *, int width);
45-
mp_obj_t shared_module_qrio_qrdecoder_decode(qrdecoder_qrdecoder_obj_t *, const mp_buffer_info_t *bufinfo, qrio_pixel_policy_t policy);
45+
mp_obj_t shared_module_qrio_qrdecoder_decode(qrdecoder_qrdecoder_obj_t *, const mp_buffer_info_t *bufinfo, qrio_pixel_policy_t policy, bool decode);

0 commit comments

Comments
 (0)
0