Skip to content

Commit 1365a06

Browse files
committed
bitmapfilter: return bitmap from the functions. add 6-number mix()
1 parent 4cb193d commit 1365a06

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

locale/circuitpython.pot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4325,7 +4325,7 @@ msgid "wbits"
43254325
msgstr ""
43264326

43274327
#: shared-bindings/bitmapfilter/__init__.c
4328-
msgid "weights must be a sequence of length 3, 9, or 12"
4328+
msgid "weights must be a sequence of length 3, 6, 9, or 12"
43294329
msgstr ""
43304330

43314331
#: shared-bindings/bitmapfilter/__init__.c

shared-bindings/bitmapfilter/__init__.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
//| threshold=False,
4141
//| offset: int = 0,
4242
//| invert: bool = False,
43-
//| ):
43+
//| ) -> displayio.Bitmap:
4444
//| """Convolve an image with a kernel
4545
//|
4646
//| The ``bitmap``, which must be in RGB565_SWAPPED format, is modified
@@ -139,7 +139,7 @@ STATIC mp_obj_t bitmapfilter_morph(size_t n_args, const mp_obj_t *pos_args, mp_m
139139

140140
shared_module_bitmapfilter_morph(bitmap, mask, sq_n_weights / 2, iweights, m, b,
141141
args[ARG_threshold].u_bool, args[ARG_offset].u_bool, args[ARG_invert].u_bool);
142-
return mp_const_none;
142+
return args[ARG_bitmap].u_obj;
143143
}
144144
MP_DEFINE_CONST_FUN_OBJ_KW(bitmapfilter_morph_obj, 0, bitmapfilter_morph);
145145

@@ -149,7 +149,7 @@ static mp_float_t float_subscr(mp_obj_t o, int i) {
149149
}
150150
//| def mix(
151151
//| bitmap: displayio.Bitmap, weights: Sequence[int], mask: displayio.Bitmap | None = None
152-
//| ):
152+
//| ) -> displayio.Bitmap:
153153
//| """Perform a channel mixing operation on the bitmap
154154
//|
155155
//| The ``bitmap``, which must be in RGB565_SWAPPED format, is modified
@@ -158,6 +158,11 @@ static mp_float_t float_subscr(mp_obj_t o, int i) {
158158
//| If ``weights`` is a list of length 3, then each channel is scaled independently:
159159
//| The numbers are the red, green, and blue channel scales.
160160
//|
161+
//| If ``weights`` is a list of length 6, then each channel is scaled and
162+
//| offset independently: The first two numbers are applied to the red channel:
163+
//| scale and offset. The second two number are applied to the green
164+
//| channel, and the last two numbers to the blue channel.
165+
//|
161166
//| If ``weights`` is a list of length 9, then channels are mixed. The first three
162167
//| numbers are the fraction of red, green and blue input channels mixed into the
163168
//| red output channel. The next 3 numbers are for green, and the final 3 are for blue.
@@ -204,6 +209,12 @@ STATIC mp_obj_t bitmapfilter_mix(size_t n_args, const mp_obj_t *pos_args, mp_map
204209
weights[5 * i] = float_subscr(weights_obj, i);
205210
}
206211
break;
212+
case 6:
213+
for (int i = 0; i < 3; i++) {
214+
weights[5 * i] = float_subscr(weights_obj, i * 2);
215+
weights[4 * i + 3] = float_subscr(weights_obj, i * 2 + 1);
216+
}
217+
break;
207218
case 9:
208219
for (int i = 0; i < 9; i++) {
209220
weights[i + i / 3] = float_subscr(weights_obj, i);
@@ -216,7 +227,7 @@ STATIC mp_obj_t bitmapfilter_mix(size_t n_args, const mp_obj_t *pos_args, mp_map
216227
break;
217228
default:
218229
mp_raise_ValueError(
219-
MP_ERROR_TEXT("weights must be a sequence of length 3, 9, or 12"));
230+
MP_ERROR_TEXT("weights must be a sequence of length 3, 6, 9, or 12"));
220231
}
221232

222233

@@ -227,7 +238,7 @@ STATIC mp_obj_t bitmapfilter_mix(size_t n_args, const mp_obj_t *pos_args, mp_map
227238
}
228239

229240
shared_module_bitmapfilter_mix(bitmap, mask, weights);
230-
return mp_const_none;
241+
return args[ARG_bitmap].u_obj;
231242
}
232243
MP_DEFINE_CONST_FUN_OBJ_KW(bitmapfilter_mix_obj, 0, bitmapfilter_mix);
233244

@@ -253,7 +264,7 @@ STATIC mp_obj_t bitmapfilter_solarize(size_t n_args, const mp_obj_t *pos_args, m
253264
}
254265

255266
shared_module_bitmapfilter_solarize(bitmap, mask, threshold);
256-
return mp_const_none;
267+
return args[ARG_bitmap].u_obj;
257268
}
258269

259270
MP_DEFINE_CONST_FUN_OBJ_KW(bitmapfilter_solarize_obj, 0, bitmapfilter_solarize);

0 commit comments

Comments
 (0)