From 222dd29a142a530b43fdf4d1ea01e276962b9886 Mon Sep 17 00:00:00 2001 From: Roy Hooper Date: Tue, 10 Dec 2019 20:42:39 -0500 Subject: [PATCH] Fix slice step. --- shared-bindings/_pixelbuf/PixelBuf.c | 8 +++++--- shared-module/_pixelbuf/PixelBuf.c | 3 --- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/shared-bindings/_pixelbuf/PixelBuf.c b/shared-bindings/_pixelbuf/PixelBuf.c index dabc23d359..feda2c573a 100644 --- a/shared-bindings/_pixelbuf/PixelBuf.c +++ b/shared-bindings/_pixelbuf/PixelBuf.c @@ -391,6 +391,9 @@ STATIC mp_obj_t pixelbuf_pixelbuf_subscr(mp_obj_t self_in, mp_obj_t index_in, mp if (value == MP_OBJ_SENTINEL) { // Get size_t len = slice.stop - slice.start; + if (slice.step > 1) { + len = (len / slice.step) + (len % slice.step ? 1 : 0); + } uint8_t *readbuf = self->two_buffers ? self->rawbuf : self->buf; return pixelbuf_get_pixel_array(readbuf + slice.start, len, &self->byteorder, self->pixel_step, slice.step, self->byteorder.is_dotstar); } else { // Set @@ -399,10 +402,9 @@ STATIC mp_obj_t pixelbuf_pixelbuf_subscr(mp_obj_t self_in, mp_obj_t index_in, mp if (!(MP_OBJ_IS_TYPE(value, &mp_type_list) || MP_OBJ_IS_TYPE(value, &mp_type_tuple))) mp_raise_ValueError(translate("tuple/list required on RHS")); - size_t dst_len = (slice.stop - slice.start) / slice.step; + size_t dst_len = (slice.stop - slice.start); if (slice.step > 1) { - if ((slice.stop - slice.start) % slice.step) - dst_len ++; + dst_len = (dst_len / slice.step) + (dst_len % slice.step ? 1 : 0); } mp_obj_t *src_objs; size_t num_items; diff --git a/shared-module/_pixelbuf/PixelBuf.c b/shared-module/_pixelbuf/PixelBuf.c index a0071903a9..020151bc31 100644 --- a/shared-module/_pixelbuf/PixelBuf.c +++ b/shared-module/_pixelbuf/PixelBuf.c @@ -96,9 +96,6 @@ void pixelbuf_set_pixel(uint8_t *buf, uint8_t *rawbuf, float brightness, mp_obj_ mp_obj_t *pixelbuf_get_pixel_array(uint8_t *buf, uint len, pixelbuf_byteorder_details_t *byteorder, uint8_t step, mp_int_t slice_step, bool dotstar) { mp_obj_t elems[len]; - if (slice_step > 1) { - len = len / slice_step; - } for (uint i = 0; i < len; i++) { elems[i] = pixelbuf_get_pixel(buf + ((i * slice_step) * step), byteorder, dotstar); }