Skip to content

Commit 961a63b

Browse files
committed
Require use of the ChannelMixer / ChannelScaler types in mix()
.. and update the test accordingly, fixing a bug discovered in the process.
1 parent ff3947a commit 961a63b

File tree

3 files changed

+36
-37
lines changed

3 files changed

+36
-37
lines changed

locale/circuitpython.pot

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4324,16 +4324,16 @@ msgstr ""
43244324
msgid "wbits"
43254325
msgstr ""
43264326

4327-
#: shared-bindings/bitmapfilter/__init__.c
4328-
msgid "weights must be a sequence of length 3, 6, 9, or 12"
4329-
msgstr ""
4330-
43314327
#: shared-bindings/bitmapfilter/__init__.c
43324328
msgid ""
43334329
"weights must be a sequence with an odd square number of elements (usually 9 "
43344330
"or 25)"
43354331
msgstr ""
43364332

4333+
#: shared-bindings/bitmapfilter/__init__.c
4334+
msgid "weights must be an object of type %q, %q, %q, or %q, not %q "
4335+
msgstr ""
4336+
43374337
#: shared-bindings/is31fl3741/FrameBuffer.c
43384338
msgid "width must be greater than zero"
43394339
msgstr ""

shared-bindings/bitmapfilter/__init__.c

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ static const mp_obj_namedtuple_type_t bitmapfilter_channel_mixer_type = {
273273
//|
274274
static const mp_obj_namedtuple_type_t bitmapfilter_channel_mixer_offset_type = {
275275
NAMEDTUPLE_TYPE_BASE_AND_SLOTS(MP_QSTR_ChannelMixerOffset),
276-
.n_fields = 9,
276+
.n_fields = 12,
277277
.fields = {
278278
MP_QSTR_rr,
279279
MP_QSTR_rg,
@@ -359,33 +359,30 @@ STATIC mp_obj_t bitmapfilter_mix(size_t n_args, const mp_obj_t *pos_args, mp_map
359359
memset(weights, 0, sizeof(weights));
360360

361361
mp_obj_t weights_obj = args[ARG_weights].u_obj;
362-
mp_int_t len = mp_obj_get_int(mp_obj_len(weights_obj));
363-
364-
switch (len) {
365-
case 3:
366-
for (int i = 0; i < 3; i++) {
367-
weights[5 * i] = float_subscr(weights_obj, i);
368-
}
369-
break;
370-
case 6:
371-
for (int i = 0; i < 3; i++) {
372-
weights[5 * i] = float_subscr(weights_obj, i * 2);
373-
weights[4 * i + 3] = float_subscr(weights_obj, i * 2 + 1);
374-
}
375-
break;
376-
case 9:
377-
for (int i = 0; i < 9; i++) {
378-
weights[i + i / 3] = float_subscr(weights_obj, i);
379-
}
380-
break;
381-
case 12:
382-
for (int i = 0; i < 12; i++) {
383-
weights[i] = float_subscr(weights_obj, i);
384-
}
385-
break;
386-
default:
387-
mp_raise_ValueError(
388-
MP_ERROR_TEXT("weights must be a sequence of length 3, 6, 9, or 12"));
362+
if (mp_obj_is_type(weights_obj, (const mp_obj_type_t *)&bitmapfilter_channel_scale_type)) {
363+
for (int i = 0; i < 3; i++) {
364+
weights[5 * i] = float_subscr(weights_obj, i);
365+
}
366+
} else if (mp_obj_is_type(weights_obj, (const mp_obj_type_t *)&bitmapfilter_channel_scale_offset_type)) {
367+
for (int i = 0; i < 3; i++) {
368+
weights[5 * i] = float_subscr(weights_obj, i * 2);
369+
weights[4 * i + 3] = float_subscr(weights_obj, i * 2 + 1);
370+
}
371+
} else if (mp_obj_is_type(weights_obj, (const mp_obj_type_t *)&bitmapfilter_channel_mixer_type)) {
372+
for (int i = 0; i < 9; i++) {
373+
weights[i + i / 3] = float_subscr(weights_obj, i);
374+
}
375+
} else if (mp_obj_is_type(weights_obj, (const mp_obj_type_t *)&bitmapfilter_channel_mixer_offset_type)) {
376+
for (int i = 0; i < 12; i++) {
377+
weights[i] = float_subscr(weights_obj, i);
378+
}
379+
} else {
380+
mp_raise_ValueError_varg(
381+
MP_ERROR_TEXT("weights must be an object of type %q, %q, %q, or %q, not %q "),
382+
MP_QSTR_ScaleMixer, MP_QSTR_ScaleMixerOffset,
383+
MP_QSTR_ChannelMixer, MP_QSTR_ChannelMixerOffset,
384+
mp_obj_get_type_qstr(weights_obj)
385+
);
389386
}
390387

391388

tests/circuitpython/bitmapfilter_mix.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ def make_quadrant_bitmap():
2121
b = test_pattern()
2222
dump_bitmap_rgb_swapped(b)
2323

24-
sepia_weights = [0.393, 0.769, 0.189, 0.349, 0.686, 0.168, 0.272, 0.534, 0.131]
24+
sepia_weights = bitmapfilter.ChannelMixer(
25+
0.393, 0.769, 0.189, 0.349, 0.686, 0.168, 0.272, 0.534, 0.131
26+
)
2527

2628
print("sepia")
2729
bitmapfilter.mix(b, sepia_weights)
@@ -30,23 +32,23 @@ def make_quadrant_bitmap():
3032
# Red channel only
3133
print("red channel only (note: masked)")
3234
b = test_pattern()
33-
bitmapfilter.mix(b, [1, 0, 0], mask=q)
35+
bitmapfilter.mix(b, bitmapfilter.ChannelScale(1, 0, 0), mask=q)
3436
dump_bitmap_rgb_swapped(b)
3537

3638
# Scale green channel
3739
print("scale green channel (note: masked)")
3840
b = test_pattern()
39-
bitmapfilter.mix(b, [1, 2, 0], mask=q)
41+
bitmapfilter.mix(b, bitmapfilter.ChannelScale(1, 2, 0), mask=q)
4042
dump_bitmap_rgb_swapped(b)
4143

4244
# Swap R & G channels
4345
print("swap R&G")
4446
b = test_pattern()
45-
bitmapfilter.mix(b, [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0])
47+
bitmapfilter.mix(b, bitmapfilter.ChannelMixerOffset(0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0))
4648
dump_bitmap_rgb_swapped(b)
4749

4850
# invert B
4951
print("invert B")
5052
b = test_pattern()
51-
bitmapfilter.mix(b, [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, 1])
53+
bitmapfilter.mix(b, bitmapfilter.ChannelScaleOffset(1, 0, 1, 0, -1, 1))
5254
dump_bitmap_rgb_swapped(b)

0 commit comments

Comments
 (0)