|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2021 Kevin Matocha |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <math.h> |
| 28 | + |
| 29 | +#include "py/runtime.h" |
| 30 | +#include "shared-bindings/displayio/Bitmap.h" |
| 31 | +#include "shared-bindings/bitmapfilter/__init__.h" |
| 32 | + |
| 33 | +STATIC mp_obj_t bitmapfilter_morph(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 34 | + enum { ARG_dest_bitmap, ARG_source_bitmap, ARG_weights, ARG_m, ARG_b }; |
| 35 | + static const mp_arg_t allowed_args[] = { |
| 36 | + { MP_QSTR_dest_bitmap, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } }, |
| 37 | + { MP_QSTR_source_bitmap, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } }, |
| 38 | + { MP_QSTR_weights, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } }, |
| 39 | + { MP_QSTR_m, MP_ARG_OBJ, { .u_obj = MP_ROM_INT(1) } }, |
| 40 | + { MP_QSTR_b, MP_ARG_OBJ, { .u_obj = MP_ROM_INT(0) } }, |
| 41 | + }; |
| 42 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 43 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 44 | + |
| 45 | + mp_arg_validate_type(args[ARG_dest_bitmap].u_obj, &displayio_bitmap_type, MP_QSTR_dest_bitmap); |
| 46 | + mp_arg_validate_type(args[ARG_source_bitmap].u_obj, &displayio_bitmap_type, MP_QSTR_source_bitmap); |
| 47 | + displayio_bitmap_t *destination = MP_OBJ_TO_PTR(args[ARG_dest_bitmap].u_obj); // the destination bitmap |
| 48 | + displayio_bitmap_t *source = MP_OBJ_TO_PTR(args[ARG_source_bitmap].u_obj); // the source bitmap |
| 49 | + |
| 50 | + mp_float_t m = mp_obj_get_float(args[ARG_m].u_obj); |
| 51 | + mp_int_t b = mp_obj_get_int(args[ARG_b].u_obj); |
| 52 | + |
| 53 | + size_t n_weights; |
| 54 | + mp_obj_t weights = mp_arg_validate_type(args[ARG_weights].u_obj, &mp_type_tuple, MP_QSTR_weights); |
| 55 | + mp_obj_t *items; |
| 56 | + mp_obj_tuple_get(weights, &n_weights, &items); |
| 57 | + mp_arg_validate_length_min(n_weights, 9, MP_QSTR_items); |
| 58 | + size_t sq_n_weights = (int)MICROPY_FLOAT_C_FUN(sqrt)(n_weights); |
| 59 | + if (sq_n_weights % 2 == 0 || sq_n_weights * sq_n_weights != n_weights) { |
| 60 | + mp_raise_ValueError(MP_ERROR_TEXT("Must be an odd square number of weights")); |
| 61 | + } |
| 62 | + |
| 63 | + int iweights[n_weights]; |
| 64 | + for (size_t i = 0; i < n_weights; i++) { |
| 65 | + iweights[i] = mp_obj_get_int(items[i]); |
| 66 | + } |
| 67 | + |
| 68 | + shared_module_bitmapfilter_morph(source, destination, sq_n_weights / 2, iweights, (float)m, b, false, 0, false); |
| 69 | + return mp_const_none; |
| 70 | +} |
| 71 | + |
| 72 | +MP_DEFINE_CONST_FUN_OBJ_KW(bitmapfilter_morph_obj, 0, bitmapfilter_morph); |
| 73 | + |
| 74 | +STATIC const mp_rom_map_elem_t bitmapfilter_module_globals_table[] = { |
| 75 | + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bitmapfilter) }, |
| 76 | + { MP_ROM_QSTR(MP_QSTR_morph), MP_ROM_PTR(&bitmapfilter_morph_obj) }, |
| 77 | +}; |
| 78 | +STATIC MP_DEFINE_CONST_DICT(bitmapfilter_module_globals, bitmapfilter_module_globals_table); |
| 79 | + |
| 80 | +const mp_obj_module_t bitmapfilter_module = { |
| 81 | + .base = {&mp_type_module }, |
| 82 | + .globals = (mp_obj_dict_t *)&bitmapfilter_module_globals, |
| 83 | +}; |
| 84 | + |
| 85 | +MP_REGISTER_MODULE(MP_QSTR_bitmapfilter, bitmapfilter_module); |
0 commit comments