|
33 | 33 | //| |
34 | 34 | //| def morph( |
35 | 35 | //| bitmap: displayio.Bitmap, |
36 | | -//| weights: tuple[int], |
| 36 | +//| weights: Sequence[int], |
37 | 37 | //| mul: float = 1.0, |
38 | 38 | //| add: int = 0, |
39 | 39 | //| mask: displayio.Bitmap | None = None, |
|
72 | 72 | //| changes to 1. Set ``invert`` to invert the binary image resulting output. |
73 | 73 | //| |
74 | 74 | //| ``mask`` is another image to use as a pixel level mask for the operation. |
75 | | -//| The mask should be an image with just black or white pixels and should |
76 | | -//| be the same size as the image being operated on. Only pixels set in the |
77 | | -//| mask are modified. |
| 75 | +//| The mask should be an image the same size as the image being operated on. |
| 76 | +//| Only pixels set to a non-zero value in the mask are modified. |
78 | 77 | //| |
79 | 78 | //| .. code-block:: python |
80 | 79 | //| |
@@ -115,20 +114,23 @@ STATIC mp_obj_t bitmapfilter_morph(size_t n_args, const mp_obj_t *pos_args, mp_m |
115 | 114 |
|
116 | 115 | mp_int_t b = mp_obj_get_int(args[ARG_add].u_obj); |
117 | 116 |
|
118 | | - size_t n_weights; |
119 | | - mp_obj_t weights = mp_arg_validate_type(args[ARG_weights].u_obj, &mp_type_tuple, MP_QSTR_weights); |
120 | | - mp_obj_t *items; |
121 | | - mp_obj_tuple_get(weights, &n_weights, &items); |
122 | | - mp_arg_validate_length_min(n_weights, 9, MP_QSTR_items); |
| 117 | + mp_obj_t weights = args[ARG_weights].u_obj; |
| 118 | + mp_obj_t obj_len = mp_obj_len(weights); |
| 119 | + if (obj_len == MP_OBJ_NULL || !mp_obj_is_small_int(obj_len)) { |
| 120 | + mp_raise_ValueError_varg(MP_ERROR_TEXT("%q must be of type %q, not %q"), MP_QSTR_weights, MP_QSTR_Sequence, mp_obj_get_type(weights)->name); |
| 121 | + } |
| 122 | + |
| 123 | + size_t n_weights = MP_OBJ_SMALL_INT_VALUE(obj_len); |
| 124 | + |
123 | 125 | size_t sq_n_weights = (int)MICROPY_FLOAT_C_FUN(sqrt)(n_weights); |
124 | 126 | if (sq_n_weights % 2 == 0 || sq_n_weights * sq_n_weights != n_weights) { |
125 | | - mp_raise_ValueError(MP_ERROR_TEXT("Must be an odd square number of weights")); |
| 127 | + mp_raise_ValueError(MP_ERROR_TEXT("weights must be a sequence with an odd square number of elements (usually 9 or 25)")); |
126 | 128 | } |
127 | 129 |
|
128 | 130 | int iweights[n_weights]; |
129 | 131 | int weight_sum = 0; |
130 | 132 | for (size_t i = 0; i < n_weights; i++) { |
131 | | - mp_int_t j = mp_obj_get_int(items[i]); |
| 133 | + mp_int_t j = mp_obj_get_int(mp_obj_subscr(weights, MP_OBJ_NEW_SMALL_INT(i), MP_OBJ_SENTINEL)); |
132 | 134 | iweights[i] = j; |
133 | 135 | weight_sum += j; |
134 | 136 | } |
|
0 commit comments