|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2023 Jeff Epler for Adafruit Industries |
| 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/displayio/Bitmap.h" |
| 31 | +#include "shared-bindings/jpegio/JpegDecoder.h" |
| 32 | +#include "shared-module/jpegio/JpegDecoder.h" |
| 33 | +#include "shared-module/displayio/Bitmap.h" |
| 34 | + |
| 35 | +//| class JpegDecoder: |
| 36 | +//| """A JPEG decoder""" |
| 37 | +//| |
| 38 | +//| def __init__(self) -> None: ... |
| 39 | +//| |
| 40 | +STATIC mp_obj_t jpegio_jpegdecoder_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { |
| 41 | + static const mp_arg_t allowed_args[] = { |
| 42 | + }; |
| 43 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 44 | + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 45 | + |
| 46 | + jpegio_jpegdecoder_obj_t *self = mp_obj_malloc(jpegio_jpegdecoder_obj_t, &jpegio_jpegdecoder_type); |
| 47 | + self->base.type = &jpegio_jpegdecoder_type; |
| 48 | + |
| 49 | + return MP_OBJ_FROM_PTR(self); |
| 50 | +} |
| 51 | + |
| 52 | +//| def decode( |
| 53 | +//| self, data: ReadableBuffer, bitmap: displaycore.Bitmap | None = None, scale=0 |
| 54 | +//| ) -> tuple[int, int]: |
| 55 | +//| """Decode JPEG data |
| 56 | +//| |
| 57 | +//| If ``bitmap`` is None, only the header is decoded. |
| 58 | +//| Otherwise, the bitmap must be large enough to contain the decoded image. |
| 59 | +//| |
| 60 | +//| The image is optionally downscaled by a factor of ``2**scale``. |
| 61 | +//| Scaling by a factor of 8 (scale=3) is particularly efficient in terms of decoding time. |
| 62 | +//| |
| 63 | +//| :param ReadableBuffer data: Data in JPEG format |
| 64 | +//| :param Bitmap bitmap: Optional output buffer |
| 65 | +//| :param int scale: Scale factor from 0 to 3. |
| 66 | +//| :returns: The size of the (possibly scaled) image as ``width, height`` |
| 67 | +//| """ |
| 68 | +//| |
| 69 | +STATIC mp_obj_t jpegio_jpegdecoder_decode(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 70 | + jpegio_jpegdecoder_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); |
| 71 | + |
| 72 | + enum { ARG_data, ARG_bitmap, ARG_scale }; |
| 73 | + static const mp_arg_t allowed_args[] = { |
| 74 | + { MP_QSTR_data, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = mp_const_none } }, |
| 75 | + { MP_QSTR_bitmap, MP_ARG_OBJ, {.u_obj = mp_const_none } }, |
| 76 | + { MP_QSTR_scale, MP_ARG_INT, {.u_int = 0 } }, |
| 77 | + }; |
| 78 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 79 | + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 80 | + |
| 81 | + mp_buffer_info_t bufinfo; |
| 82 | + mp_get_buffer_raise(args[ARG_data].u_obj, &bufinfo, MP_BUFFER_READ); |
| 83 | + |
| 84 | + mp_obj_t bitmap_in = args[ARG_bitmap].u_obj; |
| 85 | + mp_arg_validate_type_or_none(bitmap_in, &displayio_bitmap_type, MP_QSTR_bitmap); |
| 86 | + displayio_bitmap_t *bitmap = (args[ARG_bitmap].u_obj != mp_const_none) ? MP_OBJ_TO_PTR(args[ARG_bitmap].u_obj) : NULL; |
| 87 | + |
| 88 | + int scale = args[ARG_scale].u_int; |
| 89 | + mp_arg_validate_int_range(scale, 0, 3, MP_QSTR_scale); |
| 90 | + |
| 91 | + return common_hal_jpegio_jpegdecoder_decode(self, bitmap, &bufinfo, scale); |
| 92 | +} |
| 93 | +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(jpegio_jpegdecoder_decode_obj, 2, jpegio_jpegdecoder_decode); |
| 94 | + |
| 95 | +STATIC const mp_rom_map_elem_t jpegio_jpegdecoder_locals_dict_table[] = { |
| 96 | + { MP_ROM_QSTR(MP_QSTR_decode), MP_ROM_PTR(&jpegio_jpegdecoder_decode_obj) }, |
| 97 | +}; |
| 98 | +STATIC MP_DEFINE_CONST_DICT(jpegio_jpegdecoder_locals_dict, jpegio_jpegdecoder_locals_dict_table); |
| 99 | + |
| 100 | +MP_DEFINE_CONST_OBJ_TYPE( |
| 101 | + jpegio_jpegdecoder_type, |
| 102 | + MP_QSTR_JpegDecoder, |
| 103 | + MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, |
| 104 | + make_new, jpegio_jpegdecoder_make_new, |
| 105 | + locals_dict, &jpegio_jpegdecoder_locals_dict |
| 106 | + ); |
0 commit comments