Skip to content

Commit 4cb193d

Browse files
committed
bitmapfilter: Add solarize, add comments
1 parent 7e1c05e commit 4cb193d

3 files changed

Lines changed: 69 additions & 5 deletions

File tree

shared-bindings/bitmapfilter/__init__.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,40 @@ STATIC mp_obj_t bitmapfilter_mix(size_t n_args, const mp_obj_t *pos_args, mp_map
229229
shared_module_bitmapfilter_mix(bitmap, mask, weights);
230230
return mp_const_none;
231231
}
232-
233232
MP_DEFINE_CONST_FUN_OBJ_KW(bitmapfilter_mix_obj, 0, bitmapfilter_mix);
234233

234+
STATIC mp_obj_t bitmapfilter_solarize(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
235+
enum { ARG_bitmap, ARG_threshold, ARG_mask };
236+
static const mp_arg_t allowed_args[] = {
237+
{ MP_QSTR_bitmap, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } },
238+
{ MP_QSTR_threshold, MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } },
239+
{ MP_QSTR_mask, MP_ARG_OBJ, { .u_obj = MP_ROM_NONE } },
240+
};
241+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
242+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
243+
244+
mp_float_t threshold = (args[ARG_threshold].u_obj == NULL) ? MICROPY_FLOAT_CONST(0.5) : mp_obj_get_float(args[ARG_threshold].u_obj);
245+
mp_arg_validate_type(args[ARG_bitmap].u_obj, &displayio_bitmap_type, MP_QSTR_bitmap);
246+
displayio_bitmap_t *bitmap = MP_OBJ_TO_PTR(args[ARG_bitmap].u_obj);
247+
248+
249+
displayio_bitmap_t *mask = NULL;
250+
if (args[ARG_mask].u_obj != mp_const_none) {
251+
mp_arg_validate_type(args[ARG_mask].u_obj, &displayio_bitmap_type, MP_QSTR_mask);
252+
mask = MP_OBJ_TO_PTR(args[ARG_mask].u_obj);
253+
}
254+
255+
shared_module_bitmapfilter_solarize(bitmap, mask, threshold);
256+
return mp_const_none;
257+
}
258+
259+
MP_DEFINE_CONST_FUN_OBJ_KW(bitmapfilter_solarize_obj, 0, bitmapfilter_solarize);
260+
235261
STATIC const mp_rom_map_elem_t bitmapfilter_module_globals_table[] = {
236262
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bitmapfilter) },
237263
{ MP_ROM_QSTR(MP_QSTR_morph), MP_ROM_PTR(&bitmapfilter_morph_obj) },
238264
{ MP_ROM_QSTR(MP_QSTR_mix), MP_ROM_PTR(&bitmapfilter_mix_obj) },
265+
{ MP_ROM_QSTR(MP_QSTR_solarize), MP_ROM_PTR(&bitmapfilter_solarize_obj) },
239266
};
240267
STATIC MP_DEFINE_CONST_DICT(bitmapfilter_module_globals, bitmapfilter_module_globals_table);
241268

shared-bindings/bitmapfilter/__init__.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,8 @@ void shared_module_bitmapfilter_mix(
4343
displayio_bitmap_t *bitmap,
4444
displayio_bitmap_t *mask,
4545
const mp_float_t weights[12]);
46+
47+
void shared_module_bitmapfilter_solarize(
48+
displayio_bitmap_t *bitmap,
49+
displayio_bitmap_t *mask,
50+
const mp_float_t threshold);

shared-module/bitmapfilter/__init__.c

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,10 @@ void shared_module_bitmapfilter_mix(
258258
// As well, the final value in each row has to be scaled up by the
259259
// component's maxval.
260260
int scale =
261-
(i == 1 || i == 9) ? 32768 :
262-
(i == 4 || i == 6) ? 131072 :
263-
(i == 3 || i == 11) ? 65535 * COLOR_B5_MAX :
264-
(i == 7) ? 65535 * COLOR_G6_MAX :
261+
(i == 1 || i == 9) ? 32768 : // Mixing G into R/B
262+
(i == 4 || i == 6) ? 131072 : // Mixing R/B into G
263+
(i == 3 || i == 11) ? 65535 * COLOR_B5_MAX : // Offset for R/B
264+
(i == 7) ? 65535 * COLOR_G6_MAX : // Offset for G
265265
65536;
266266
wt[i] = (int32_t)MICROPY_FLOAT_C_FUN(round)(scale * weights[i]);
267267
}
@@ -315,3 +315,35 @@ void shared_module_bitmapfilter_mix(
315315
}
316316
}
317317
}
318+
319+
void shared_module_bitmapfilter_solarize(
320+
displayio_bitmap_t *bitmap,
321+
displayio_bitmap_t *mask,
322+
const mp_float_t threshold) {
323+
324+
int threshold_i = (int32_t)MICROPY_FLOAT_C_FUN(round)(256 * threshold);
325+
switch (bitmap->bits_per_value) {
326+
default:
327+
mp_raise_ValueError(MP_ERROR_TEXT("unsupported bitmap depth"));
328+
case 16: {
329+
for (int y = 0, yy = bitmap->height; y < yy; y++) {
330+
uint16_t *row_ptr = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(bitmap, y);
331+
for (int x = 0, xx = bitmap->width; x < xx; x++) {
332+
if (mask && common_hal_displayio_bitmap_get_pixel(mask, x, y)) {
333+
continue; // Short circuit.
334+
}
335+
int pixel = IMAGE_GET_RGB565_PIXEL_FAST(row_ptr, x);
336+
if (COLOR_RGB565_TO_Y(pixel) > threshold_i) {
337+
int r = COLOR_R5_MAX - COLOR_RGB565_TO_R5(pixel);
338+
int g = COLOR_G6_MAX - COLOR_RGB565_TO_G6(pixel);
339+
int b = COLOR_B5_MAX - COLOR_RGB565_TO_B5(pixel);
340+
341+
pixel = COLOR_R5_G6_B5_TO_RGB565(r, g, b);
342+
IMAGE_PUT_RGB565_PIXEL_FAST(row_ptr, x, pixel);
343+
}
344+
}
345+
}
346+
break;
347+
}
348+
}
349+
}

0 commit comments

Comments
 (0)