Skip to content

Commit 214ebc3

Browse files
committed
morph improvements
* weight can be any sequence (& test it) * improve error message * correct documentation of ``mask`` vs copypaste from openmv
1 parent 7e23fac commit 214ebc3

3 files changed

Lines changed: 22 additions & 17 deletions

File tree

locale/circuitpython.pot

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ msgstr ""
216216
msgid "%q must be of type %q, %q, or %q, not %q"
217217
msgstr ""
218218

219-
#: py/argcheck.c shared-module/synthio/__init__.c
219+
#: py/argcheck.c shared-bindings/bitmapfilter/__init__.c
220+
#: shared-module/synthio/__init__.c
220221
msgid "%q must be of type %q, not %q"
221222
msgstr ""
222223

@@ -1378,10 +1379,6 @@ msgstr ""
13781379
msgid "Must be a %q subclass."
13791380
msgstr ""
13801381

1381-
#: shared-bindings/bitmapfilter/__init__.c
1382-
msgid "Must be an odd square number of weights"
1383-
msgstr ""
1384-
13851382
#: ports/espressif/common-hal/dotclockframebuffer/DotClockFramebuffer.c
13861383
msgid "Must provide 5/6/5 RGB pins"
13871384
msgstr ""
@@ -4327,6 +4324,12 @@ msgstr ""
43274324
msgid "wbits"
43284325
msgstr ""
43294326

4327+
#: shared-bindings/bitmapfilter/__init__.c
4328+
msgid ""
4329+
"weights must be a sequence with an odd square number of elements (usually 9 "
4330+
"or 25)"
4331+
msgstr ""
4332+
43304333
#: shared-bindings/is31fl3741/FrameBuffer.c
43314334
msgid "width must be greater than zero"
43324335
msgstr ""

shared-bindings/bitmapfilter/__init__.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
//|
3434
//| def morph(
3535
//| bitmap: displayio.Bitmap,
36-
//| weights: tuple[int],
36+
//| weights: Sequence[int],
3737
//| mul: float = 1.0,
3838
//| add: int = 0,
3939
//| mask: displayio.Bitmap | None = None,
@@ -72,9 +72,8 @@
7272
//| changes to 1. Set ``invert`` to invert the binary image resulting output.
7373
//|
7474
//| ``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.
7877
//|
7978
//| .. code-block:: python
8079
//|
@@ -115,20 +114,23 @@ STATIC mp_obj_t bitmapfilter_morph(size_t n_args, const mp_obj_t *pos_args, mp_m
115114

116115
mp_int_t b = mp_obj_get_int(args[ARG_add].u_obj);
117116

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+
123125
size_t sq_n_weights = (int)MICROPY_FLOAT_C_FUN(sqrt)(n_weights);
124126
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)"));
126128
}
127129

128130
int iweights[n_weights];
129131
int weight_sum = 0;
130132
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));
132134
iweights[i] = j;
133135
weight_sum += j;
134136
}

tests/circuitpython/bitmapfilter_morph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def make_quadrant_bitmap():
3434

3535

3636
blur = (1, 2, 1, 2, 4, 2, 1, 2, 1)
37-
sharpen = (-1, -2, -1, -2, 4, -2, -1, -2, -1)
37+
sharpen = [-1, -2, -1, -2, 4, -2, -1, -2, -1]
3838
b = make_circle_bitmap()
3939
dump_bitmap(b)
4040
bitmapfilter.morph(b, weights=blur)

0 commit comments

Comments
 (0)