Merge pull request #8467 from pypewpew/qrio-find

Add qrio.QRDecoder.find() to locate codes without decoding
This commit is contained in:
Scott Shawcroft 2023-10-25 11:51:06 -07:00 committed by GitHub
commit 9354c921c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 141 additions and 15 deletions

View File

@ -56,6 +56,19 @@ STATIC mp_obj_t qrio_qrdecoder_make_new(const mp_obj_type_t *type, size_t n_args
return self; return self;
} }
STATIC void verify_buffer_size(qrio_qrdecoder_obj_t *self, mp_obj_t *buffer, size_t len, qrio_pixel_policy_t policy) {
int width = shared_module_qrio_qrdecoder_get_width(self);
int height = shared_module_qrio_qrdecoder_get_height(self);
// verify that the buffer is big enough
int sz = width * height;
if (policy != QRIO_EVERY_BYTE) {
sz *= 2;
}
mp_get_index(mp_obj_get_type(*buffer), len, MP_OBJ_NEW_SMALL_INT(sz - 1), false);
}
//| def decode( //| def decode(
//| self, buffer: ReadableBuffer, pixel_policy: PixelPolicy = PixelPolicy.EVERY_BYTE //| self, buffer: ReadableBuffer, pixel_policy: PixelPolicy = PixelPolicy.EVERY_BYTE
//| ) -> List[QRInfo]: //| ) -> List[QRInfo]:
@ -73,22 +86,39 @@ STATIC mp_obj_t qrio_qrdecoder_decode(size_t n_args, const mp_obj_t *pos_args, m
mp_buffer_info_t bufinfo; mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ); mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ);
int width = shared_module_qrio_qrdecoder_get_width(self);
int height = shared_module_qrio_qrdecoder_get_height(self);
// verify that the buffer is big enough
int sz = width * height;
qrio_pixel_policy_t policy = cp_enum_value(&qrio_pixel_policy_type, args[ARG_pixel_policy].u_obj, MP_QSTR_pixel_policy); qrio_pixel_policy_t policy = cp_enum_value(&qrio_pixel_policy_type, args[ARG_pixel_policy].u_obj, MP_QSTR_pixel_policy);
if (policy != QRIO_EVERY_BYTE) { verify_buffer_size(self, &args[ARG_buffer].u_obj, bufinfo.len, policy);
sz *= 2;
}
mp_get_index(mp_obj_get_type(args[ARG_buffer].u_obj), bufinfo.len, MP_OBJ_NEW_SMALL_INT(sz - 1), false);
return shared_module_qrio_qrdecoder_decode(self, &bufinfo, policy); return shared_module_qrio_qrdecoder_decode(self, &bufinfo, policy);
} }
MP_DEFINE_CONST_FUN_OBJ_KW(qrio_qrdecoder_decode_obj, 1, qrio_qrdecoder_decode); MP_DEFINE_CONST_FUN_OBJ_KW(qrio_qrdecoder_decode_obj, 1, qrio_qrdecoder_decode);
//| def find(
//| self, buffer: ReadableBuffer, pixel_policy: PixelPolicy = PixelPolicy.EVERY_BYTE
//| ) -> List[QRPosition]:
//| """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`."""
STATIC mp_obj_t qrio_qrdecoder_find(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
qrio_qrdecoder_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
enum { ARG_buffer, ARG_pixel_policy };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_int = 0} },
{ MP_QSTR_pixel_policy, MP_ARG_OBJ, {.u_obj = MP_ROM_PTR((mp_obj_t *)&qrio_pixel_policy_EVERY_BYTE_obj)} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ);
qrio_pixel_policy_t policy = cp_enum_value(&qrio_pixel_policy_type, args[ARG_pixel_policy].u_obj, MP_QSTR_pixel_policy);
verify_buffer_size(self, &args[ARG_buffer].u_obj, bufinfo.len, policy);
return shared_module_qrio_qrdecoder_find(self, &bufinfo, policy);
}
MP_DEFINE_CONST_FUN_OBJ_KW(qrio_qrdecoder_find_obj, 1, qrio_qrdecoder_find);
//| width: int //| width: int
//| """The width of image the decoder expects""" //| """The width of image the decoder expects"""
STATIC mp_obj_t qrio_qrdecoder_get_width(mp_obj_t self_in) { STATIC mp_obj_t qrio_qrdecoder_get_width(mp_obj_t self_in) {
@ -135,6 +165,7 @@ STATIC const mp_rom_map_elem_t qrio_qrdecoder_locals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&qrio_qrdecoder_width_obj) }, { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&qrio_qrdecoder_width_obj) },
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&qrio_qrdecoder_height_obj) }, { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&qrio_qrdecoder_height_obj) },
{ MP_ROM_QSTR(MP_QSTR_decode), MP_ROM_PTR(&qrio_qrdecoder_decode_obj) }, { MP_ROM_QSTR(MP_QSTR_decode), MP_ROM_PTR(&qrio_qrdecoder_decode_obj) },
{ MP_ROM_QSTR(MP_QSTR_find), MP_ROM_PTR(&qrio_qrdecoder_find_obj) },
}; };
STATIC MP_DEFINE_CONST_DICT(qrio_qrdecoder_locals, qrio_qrdecoder_locals_table); STATIC MP_DEFINE_CONST_DICT(qrio_qrdecoder_locals, qrio_qrdecoder_locals_table);

View File

@ -47,3 +47,66 @@ const mp_obj_namedtuple_type_t qrio_qrinfo_type_obj = {
MP_QSTR_data_type, MP_QSTR_data_type,
}, },
}; };
//| class QRPosition:
//| """Information about a non-decoded QR code"""
//|
//| top_left_x: int
//| """X coordinate of the top left corner"""
//|
//| top_left_y: int
//| """Y coordinate of the top left corner"""
//|
//| top_right_x: int
//| """X coordinate of the top right corner"""
//|
//| top_right_y: int
//| """Y coordinate of the top right corner"""
//|
//| bottom_right_x: int
//| """X coordinate of the bottom right corner"""
//|
//| bottom_right_y: int
//| """Y coordinate of the bottom right corner"""
//|
//| bottom_left_x: int
//| """X coordinate of the bottom left corner"""
//|
//| bottom_left_y: int
//| """Y coordinate of the bottom left corner"""
//|
//| size: int
//| """The number of bits the code contains"""
//|
const mp_obj_namedtuple_type_t qrio_qrposition_type_obj = {
.base = {
.base = {
.type = &mp_type_type
},
.flags = MP_TYPE_FLAG_EXTENDED,
.name = MP_QSTR_QRPosition,
.print = namedtuple_print,
.parent = &mp_type_tuple,
.make_new = namedtuple_make_new,
.attr = namedtuple_attr,
MP_TYPE_EXTENDED_FIELDS(
.unary_op = mp_obj_tuple_unary_op,
.binary_op = mp_obj_tuple_binary_op,
.subscr = mp_obj_tuple_subscr,
.getiter = mp_obj_tuple_getiter,
),
},
.n_fields = 9,
.fields = {
MP_QSTR_top_left_x,
MP_QSTR_top_left_y,
MP_QSTR_top_right_x,
MP_QSTR_top_right_y,
MP_QSTR_bottom_right_x,
MP_QSTR_bottom_right_y,
MP_QSTR_bottom_left_x,
MP_QSTR_bottom_left_y,
MP_QSTR_size,
},
};

View File

@ -29,3 +29,4 @@
#include "py/objnamedtuple.h" #include "py/objnamedtuple.h"
extern const mp_obj_namedtuple_type_t qrio_qrinfo_type_obj; extern const mp_obj_namedtuple_type_t qrio_qrinfo_type_obj;
extern const mp_obj_namedtuple_type_t qrio_qrposition_type_obj;

View File

@ -98,21 +98,21 @@ STATIC mp_obj_t data_type(int type) {
return mp_obj_new_int(type); return mp_obj_new_int(type);
} }
mp_obj_t shared_module_qrio_qrdecoder_decode(qrdecoder_qrdecoder_obj_t *self, const mp_buffer_info_t *bufinfo, qrio_pixel_policy_t policy) { STATIC void quirc_fill_buffer(qrdecoder_qrdecoder_obj_t *self, void *buf, qrio_pixel_policy_t policy) {
int width, height; int width, height;
uint8_t *framebuffer = quirc_begin(self->quirc, &width, &height); uint8_t *framebuffer = quirc_begin(self->quirc, &width, &height);
uint8_t *src = bufinfo->buf; uint8_t *src = buf;
switch (policy) { switch (policy) {
case QRIO_RGB565: { case QRIO_RGB565: {
uint16_t *src16 = bufinfo->buf; uint16_t *src16 = buf;
for (int i = 0; i < width * height; i++) { for (int i = 0; i < width * height; i++) {
framebuffer[i] = (src16[i] >> 3) & 0xfc; framebuffer[i] = (src16[i] >> 3) & 0xfc;
} }
break; break;
} }
case QRIO_RGB565_SWAPPED: { case QRIO_RGB565_SWAPPED: {
uint16_t *src16 = bufinfo->buf; uint16_t *src16 = buf;
for (int i = 0; i < width * height; i++) { for (int i = 0; i < width * height; i++) {
framebuffer[i] = (__builtin_bswap16(src16[i]) >> 3) & 0xfc; framebuffer[i] = (__builtin_bswap16(src16[i]) >> 3) & 0xfc;
} }
@ -133,11 +133,16 @@ mp_obj_t shared_module_qrio_qrdecoder_decode(qrdecoder_qrdecoder_obj_t *self, co
break; break;
} }
quirc_end(self->quirc); quirc_end(self->quirc);
}
mp_obj_t shared_module_qrio_qrdecoder_decode(qrdecoder_qrdecoder_obj_t *self, const mp_buffer_info_t *bufinfo, qrio_pixel_policy_t policy) {
quirc_fill_buffer(self, bufinfo->buf, policy);
int count = quirc_count(self->quirc); int count = quirc_count(self->quirc);
mp_obj_t result = mp_obj_new_list(0, NULL); mp_obj_t result = mp_obj_new_list(0, NULL);
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
quirc_extract(self->quirc, i, &self->code); quirc_extract(self->quirc, i, &self->code);
mp_obj_t code_obj;
if (quirc_decode(&self->code, &self->data) != QUIRC_SUCCESS) { if (quirc_decode(&self->code, &self->data) != QUIRC_SUCCESS) {
continue; continue;
} }
@ -145,7 +150,32 @@ mp_obj_t shared_module_qrio_qrdecoder_decode(qrdecoder_qrdecoder_obj_t *self, co
mp_obj_new_bytes(self->data.payload, self->data.payload_len), mp_obj_new_bytes(self->data.payload, self->data.payload_len),
data_type(self->data.data_type), data_type(self->data.data_type),
}; };
mp_obj_t code_obj = namedtuple_make_new((const mp_obj_type_t *)&qrio_qrinfo_type_obj, 2, 0, elems); code_obj = namedtuple_make_new((const mp_obj_type_t *)&qrio_qrinfo_type_obj, 2, 0, elems);
mp_obj_list_append(result, code_obj);
}
return result;
}
mp_obj_t shared_module_qrio_qrdecoder_find(qrdecoder_qrdecoder_obj_t *self, const mp_buffer_info_t *bufinfo, qrio_pixel_policy_t policy) {
quirc_fill_buffer(self, bufinfo->buf, policy);
int count = quirc_count(self->quirc);
mp_obj_t result = mp_obj_new_list(0, NULL);
for (int i = 0; i < count; i++) {
quirc_extract(self->quirc, i, &self->code);
mp_obj_t code_obj;
mp_obj_t elems[9] = {
mp_obj_new_int(self->code.corners[0].x),
mp_obj_new_int(self->code.corners[0].y),
mp_obj_new_int(self->code.corners[1].x),
mp_obj_new_int(self->code.corners[1].y),
mp_obj_new_int(self->code.corners[2].x),
mp_obj_new_int(self->code.corners[2].y),
mp_obj_new_int(self->code.corners[3].x),
mp_obj_new_int(self->code.corners[3].y),
mp_obj_new_int(self->code.size),
};
code_obj = namedtuple_make_new((const mp_obj_type_t *)&qrio_qrposition_type_obj, 9, 0, elems);
mp_obj_list_append(result, code_obj); mp_obj_list_append(result, code_obj);
} }
return result; return result;

View File

@ -43,3 +43,4 @@ int shared_module_qrio_qrdecoder_get_width(qrdecoder_qrdecoder_obj_t *);
void shared_module_qrio_qrdecoder_set_height(qrdecoder_qrdecoder_obj_t *, int height); void shared_module_qrio_qrdecoder_set_height(qrdecoder_qrdecoder_obj_t *, int height);
void shared_module_qrio_qrdecoder_set_width(qrdecoder_qrdecoder_obj_t *, int width); void shared_module_qrio_qrdecoder_set_width(qrdecoder_qrdecoder_obj_t *, int width);
mp_obj_t shared_module_qrio_qrdecoder_decode(qrdecoder_qrdecoder_obj_t *, const mp_buffer_info_t *bufinfo, qrio_pixel_policy_t policy); mp_obj_t shared_module_qrio_qrdecoder_decode(qrdecoder_qrdecoder_obj_t *, const mp_buffer_info_t *bufinfo, qrio_pixel_policy_t policy);
mp_obj_t shared_module_qrio_qrdecoder_find(qrdecoder_qrdecoder_obj_t *, const mp_buffer_info_t *bufinfo, qrio_pixel_policy_t policy);