@@ -56,6 +56,19 @@ STATIC mp_obj_t qrio_qrdecoder_make_new(const mp_obj_type_t *type, size_t n_args
56
56
return self ;
57
57
}
58
58
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
+
59
72
//| def decode(
60
73
//| self, buffer: ReadableBuffer, pixel_policy: PixelPolicy = PixelPolicy.EVERY_BYTE
61
74
//| ) -> List[QRInfo]:
@@ -73,21 +86,38 @@ STATIC mp_obj_t qrio_qrdecoder_decode(size_t n_args, const mp_obj_t *pos_args, m
73
86
74
87
mp_buffer_info_t bufinfo ;
75
88
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 );
76
91
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 );
79
95
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 );
82
114
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 );
87
116
88
- return shared_module_qrio_qrdecoder_decode (self , & bufinfo , policy );
117
+ return shared_module_qrio_qrdecoder_decode (self , & bufinfo , policy , false );
89
118
}
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
+
91
121
92
122
//| width: int
93
123
//| """The width of image the decoder expects"""
@@ -135,6 +165,7 @@ STATIC const mp_rom_map_elem_t qrio_qrdecoder_locals_table[] = {
135
165
{ MP_ROM_QSTR (MP_QSTR_width ), MP_ROM_PTR (& qrio_qrdecoder_width_obj ) },
136
166
{ MP_ROM_QSTR (MP_QSTR_height ), MP_ROM_PTR (& qrio_qrdecoder_height_obj ) },
137
167
{ 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 ) },
138
169
};
139
170
140
171
STATIC MP_DEFINE_CONST_DICT (qrio_qrdecoder_locals , qrio_qrdecoder_locals_table );
0 commit comments