|
| 1 | +/* |
| 2 | + * This file is part of the Circuit Python project, https://github.com/adafruit/circuitpython |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2018 Roy Hooper |
| 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 "py/obj.h" |
| 28 | +#include "py/runtime.h" |
| 29 | + |
| 30 | +#include "shared-bindings/bitops/__init__.h" |
| 31 | + |
| 32 | +//| """Routines for low-level manipulation of binary data""" |
| 33 | +//| |
| 34 | +//| |
| 35 | + |
| 36 | +//| def bit_transpose(input: _typing.ReadableBuffer, *, width:int = 8, output: Optional[_typing.WriteableBuffer]=None) -> WriteableBuffer: |
| 37 | +//| """"Transpose" a buffer by assembling each output byte with bits taken from each of ``width`` different input bytes. |
| 38 | +//| |
| 39 | +//| This can be useful to convert a sequence of pixel values into a single |
| 40 | +//| stream of bytes suitable for sending via a parallel conversion method. |
| 41 | +//| |
| 42 | +//| The number of bytes in the input buffer must be a multiple of the width, |
| 43 | +//| and the width can be any value from 2 to 8. If the width is fewer than 8, |
| 44 | +//| then the remaining (less significant) bits of the output are set to zero. |
| 45 | +//| |
| 46 | +//| Let ``stride = len(input)//width``. Then the first byte is made out of the |
| 47 | +//| most significant bits of ``[input[0], input[stride], input[2*stride], ...]``. |
| 48 | +//| The second byte is made out of the second bits, and so on until the 8th output |
| 49 | +//| byte which is made of the first bits of ``input[1], input[1+stride, |
| 50 | +//| input[2*stride], ...]``. |
| 51 | +//| |
| 52 | +//| The required output buffer size is ``len(input) * 8 // width``. |
| 53 | +//| |
| 54 | +//| Returns the output buffer.""" |
| 55 | +//| ... |
| 56 | + |
| 57 | +STATIC mp_obj_t bit_transpose(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 58 | + enum { ARG_input, ARG_width, ARG_output }; |
| 59 | + static const mp_arg_t allowed_args[] = { |
| 60 | + { MP_QSTR_input, MP_ARG_OBJ | MP_ARG_REQUIRED }, |
| 61 | + { MP_QSTR_output, MP_ARG_OBJ | MP_ARG_REQUIRED }, |
| 62 | + { MP_QSTR_width, MP_ARG_INT | MP_ARG_KW_ONLY, { .u_int = 8 } }, |
| 63 | + }; |
| 64 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 65 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 66 | + |
| 67 | + int width = args[ARG_width].u_int; |
| 68 | + if (width < 2 || width > 8) { |
| 69 | + mp_raise_ValueError(translate("width must be from 2 to 8 (inclusive)")); |
| 70 | + } |
| 71 | + |
| 72 | + mp_buffer_info_t input_bufinfo; |
| 73 | + mp_get_buffer_raise(args[ARG_input].u_obj, &input_bufinfo, MP_BUFFER_READ); |
| 74 | + int inlen = input_bufinfo.len; |
| 75 | + if (inlen % width != 0) { |
| 76 | + mp_raise_ValueError_varg(translate("Input buffer length (%d) must be a multiple of the strand count (%d)"), inlen, width); |
| 77 | + } |
| 78 | + |
| 79 | + mp_buffer_info_t output_bufinfo; |
| 80 | + mp_get_buffer_raise(args[ARG_output].u_obj, &output_bufinfo, MP_BUFFER_WRITE); |
| 81 | + int avail = output_bufinfo.len; |
| 82 | + int outlen = 8 * (inlen / width); |
| 83 | + if (avail < outlen) { |
| 84 | + mp_raise_ValueError_varg(translate("Output buffer must be at least %d bytes"), outlen); |
| 85 | + } |
| 86 | + common_hal_bitops_bit_transpose(output_bufinfo.buf, input_bufinfo.buf, inlen, width); |
| 87 | + return args[ARG_output].u_obj; |
| 88 | +} |
| 89 | +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bitops_bit_transpose_obj, 1, bit_transpose); |
| 90 | + |
| 91 | +STATIC const mp_rom_map_elem_t bitops_module_globals_table[] = { |
| 92 | + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bitops) }, |
| 93 | + { MP_ROM_QSTR(MP_QSTR_bit_transpose), MP_ROM_PTR(&bitops_bit_transpose_obj) }, |
| 94 | +}; |
| 95 | + |
| 96 | +STATIC MP_DEFINE_CONST_DICT(bitops_module_globals, bitops_module_globals_table); |
| 97 | + |
| 98 | +const mp_obj_module_t bitops_module = { |
| 99 | + .base = { &mp_type_module }, |
| 100 | + .globals = (mp_obj_dict_t*)&bitops_module_globals, |
| 101 | +}; |
0 commit comments