|
| 1 | +/* |
| 2 | + * This file is part of the CircuitPython project, https://github.com/adafruit/circuitpython |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2021 Jeff Epler |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "shared-bindings/qrio/__init__.h" |
| 28 | +#include "shared-bindings/qrio/QRDecoder.h" |
| 29 | +#include "shared-module/qrio/QRDecoder.h" |
| 30 | + |
| 31 | +#include "py/obj.h" |
| 32 | +#include "py/objproperty.h" |
| 33 | +#include "py/enum.h" |
| 34 | + |
| 35 | +//| class QRDecoder: |
| 36 | +//| |
| 37 | +//| def __init__(self, width: int, height: int) -> None: |
| 38 | +//| """Construct a QRDecoder object""" |
| 39 | +//| |
| 40 | + |
| 41 | +STATIC mp_obj_t qrio_qrdecoder_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 42 | + enum { ARG_width, ARG_height }; |
| 43 | + static const mp_arg_t allowed_args[] = { |
| 44 | + { MP_QSTR_width, MP_ARG_INT | MP_ARG_REQUIRED, {.u_int = 0} }, |
| 45 | + { MP_QSTR_height, MP_ARG_INT | MP_ARG_REQUIRED, {.u_int = 0} }, |
| 46 | + }; |
| 47 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 48 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 49 | + |
| 50 | + qrio_qrdecoder_obj_t *self = m_new_obj(qrio_qrdecoder_obj_t); |
| 51 | + self->base.type = &qrio_qrdecoder_type_obj; |
| 52 | + shared_module_qrio_qrdecoder_construct(self, args[ARG_width].u_int, args[ARG_height].u_int); |
| 53 | + |
| 54 | + return self; |
| 55 | +} |
| 56 | + |
| 57 | +//| def decode(self, buffer: ReadableBuffer) -> List[qrinfo]: |
| 58 | +//| """Decode zero or more QR codes from the given image in L8 format""" |
| 59 | +//| |
| 60 | +STATIC mp_obj_t qrio_qrdecoder_decode(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 61 | + qrio_qrdecoder_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); |
| 62 | + |
| 63 | + enum { ARG_buffer, ARG_pixel_policy }; |
| 64 | + static const mp_arg_t allowed_args[] = { |
| 65 | + { MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_int = 0} }, |
| 66 | + { MP_QSTR_pixel_policy, MP_ARG_OBJ, {.u_int = 0} }, |
| 67 | + }; |
| 68 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 69 | + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 70 | + |
| 71 | + mp_buffer_info_t bufinfo; |
| 72 | + mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ); |
| 73 | + |
| 74 | + int width = shared_module_qrio_qrdecoder_get_width(self); |
| 75 | + int height = shared_module_qrio_qrdecoder_get_height(self); |
| 76 | + |
| 77 | + // verify that the buffer is big enough |
| 78 | + int sz = width * height; |
| 79 | + qrio_pixel_policy_t policy = cp_enum_value(&qrio_pixel_policy_type, args[ARG_pixel_policy].u_obj); |
| 80 | + if (policy != QRIO_EVERY_BYTE) { |
| 81 | + sz *= 2; |
| 82 | + } |
| 83 | + mp_get_index(mp_obj_get_type(args[ARG_buffer].u_obj), bufinfo.len, MP_OBJ_NEW_SMALL_INT(sz - 1), false); |
| 84 | + |
| 85 | + return shared_module_qrio_qrdecoder_decode(self, &bufinfo, policy); |
| 86 | +} |
| 87 | +MP_DEFINE_CONST_FUN_OBJ_KW(qrio_qrdecoder_decode_obj, 2, qrio_qrdecoder_decode); |
| 88 | + |
| 89 | +//| width: int |
| 90 | +//| """The width of image the decoder expects""" |
| 91 | +//| |
| 92 | +STATIC mp_obj_t qrio_qrdecoder_get_width(mp_obj_t self_in) { |
| 93 | + qrio_qrdecoder_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 94 | + return mp_obj_new_int(shared_module_qrio_qrdecoder_get_width(self)); |
| 95 | +} |
| 96 | +MP_DEFINE_CONST_FUN_OBJ_1(qrio_qrdecoder_get_width_obj, qrio_qrdecoder_get_width); |
| 97 | + |
| 98 | +STATIC mp_obj_t qrio_qrdecoder_set_width(mp_obj_t self_in, mp_obj_t width_in) { |
| 99 | + qrio_qrdecoder_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 100 | + int width = mp_obj_get_int(width_in); |
| 101 | + shared_module_qrio_qrdecoder_set_width(self, width); |
| 102 | + return mp_const_none; |
| 103 | +} |
| 104 | +MP_DEFINE_CONST_FUN_OBJ_2(qrio_qrdecoder_set_width_obj, qrio_qrdecoder_set_width); |
| 105 | + |
| 106 | +const mp_obj_property_t qrio_qrdecoder_width_obj = { |
| 107 | + .base.type = &mp_type_property, |
| 108 | + .proxy = {(mp_obj_t)&qrio_qrdecoder_get_width_obj, |
| 109 | + (mp_obj_t)&qrio_qrdecoder_set_width_obj, |
| 110 | + MP_ROM_NONE}, |
| 111 | +}; |
| 112 | + |
| 113 | +//| height: int |
| 114 | +//| """The height of image the decoder expects""" |
| 115 | +//| |
| 116 | +STATIC mp_obj_t qrio_qrdecoder_get_height(mp_obj_t self_in) { |
| 117 | + qrio_qrdecoder_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 118 | + return mp_obj_new_int(shared_module_qrio_qrdecoder_get_height(self)); |
| 119 | +} |
| 120 | +MP_DEFINE_CONST_FUN_OBJ_1(qrio_qrdecoder_get_height_obj, qrio_qrdecoder_get_height); |
| 121 | + |
| 122 | +STATIC mp_obj_t qrio_qrdecoder_set_height(mp_obj_t self_in, mp_obj_t height_in) { |
| 123 | + qrio_qrdecoder_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 124 | + int height = mp_obj_get_int(height_in); |
| 125 | + shared_module_qrio_qrdecoder_set_height(self, height); |
| 126 | + return mp_const_none; |
| 127 | +} |
| 128 | +MP_DEFINE_CONST_FUN_OBJ_2(qrio_qrdecoder_set_height_obj, qrio_qrdecoder_set_height); |
| 129 | + |
| 130 | +const mp_obj_property_t qrio_qrdecoder_height_obj = { |
| 131 | + .base.type = &mp_type_property, |
| 132 | + .proxy = {(mp_obj_t)&qrio_qrdecoder_get_height_obj, |
| 133 | + (mp_obj_t)&qrio_qrdecoder_set_height_obj, |
| 134 | + MP_ROM_NONE}, |
| 135 | +}; |
| 136 | + |
| 137 | +STATIC const mp_rom_map_elem_t qrio_qrdecoder_locals_table[] = { |
| 138 | + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_QRDecoder) }, |
| 139 | + { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&qrio_qrdecoder_width_obj) }, |
| 140 | + { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&qrio_qrdecoder_height_obj) }, |
| 141 | + { MP_ROM_QSTR(MP_QSTR_decode), MP_ROM_PTR(&qrio_qrdecoder_decode_obj) }, |
| 142 | +}; |
| 143 | + |
| 144 | +STATIC MP_DEFINE_CONST_DICT(qrio_qrdecoder_locals, qrio_qrdecoder_locals_table); |
| 145 | + |
| 146 | +const mp_obj_type_t qrio_qrdecoder_type_obj = { |
| 147 | + { &mp_type_type }, |
| 148 | + .name = MP_QSTR_QRDecoder, |
| 149 | + .make_new = qrio_qrdecoder_make_new, |
| 150 | + .locals_dict = (mp_obj_dict_t *)&qrio_qrdecoder_locals, |
| 151 | +}; |
0 commit comments