use the stack

This commit is contained in:
Roy Hooper 2020-08-10 18:05:13 -04:00
parent ef1782f85e
commit 1b7709f325
3 changed files with 21 additions and 12 deletions

View File

@ -309,6 +309,16 @@ STATIC mp_obj_t pixelbuf_pixelbuf_subscr(mp_obj_t self_in, mp_obj_t index_in, mp
size_t length = common_hal__pixelbuf_pixelbuf_get_len(self_in); size_t length = common_hal__pixelbuf_pixelbuf_get_len(self_in);
mp_seq_get_fast_slice_indexes(length, index_in, &slice); mp_seq_get_fast_slice_indexes(length, index_in, &slice);
static mp_obj_tuple_t flat_item_tuple = {
.base = {&mp_type_tuple},
.len = 0,
.items = {
mp_const_none,
mp_const_none,
mp_const_none,
mp_const_none,
}
};
size_t slice_len; size_t slice_len;
if (slice.step > 0) { if (slice.step > 0) {
@ -335,8 +345,8 @@ STATIC mp_obj_t pixelbuf_pixelbuf_subscr(mp_obj_t self_in, mp_obj_t index_in, mp
if (num_items != slice_len && num_items != (slice_len * common_hal__pixelbuf_pixelbuf_get_bpp(self_in))) { if (num_items != slice_len && num_items != (slice_len * common_hal__pixelbuf_pixelbuf_get_bpp(self_in))) {
mp_raise_ValueError_varg(translate("Unmatched number of items on RHS (expected %d, got %d)."), slice_len, num_items); mp_raise_ValueError_varg(translate("Unmatched number of items on RHS (expected %d, got %d)."), slice_len, num_items);
} }
common_hal__pixelbuf_pixelbuf_set_pixels(self_in, slice.start, slice.step, slice_len, value,
common_hal__pixelbuf_pixelbuf_set_pixels(self_in, slice.start, slice.step, slice_len, value, num_items != slice_len); num_items != slice_len ? &flat_item_tuple : mp_const_none);
return mp_const_none; return mp_const_none;
#else #else
return MP_OBJ_NULL; // op not supported return MP_OBJ_NULL; // op not supported

View File

@ -47,6 +47,6 @@ void common_hal__pixelbuf_pixelbuf_fill(mp_obj_t self, mp_obj_t item);
void common_hal__pixelbuf_pixelbuf_show(mp_obj_t self); void common_hal__pixelbuf_pixelbuf_show(mp_obj_t self);
mp_obj_t common_hal__pixelbuf_pixelbuf_get_pixel(mp_obj_t self, size_t index); mp_obj_t common_hal__pixelbuf_pixelbuf_get_pixel(mp_obj_t self, size_t index);
void common_hal__pixelbuf_pixelbuf_set_pixel(mp_obj_t self, size_t index, mp_obj_t item); void common_hal__pixelbuf_pixelbuf_set_pixel(mp_obj_t self, size_t index, mp_obj_t item);
void common_hal__pixelbuf_pixelbuf_set_pixels(mp_obj_t self_in, size_t start, mp_int_t step, size_t slice_len, mp_obj_t* values, bool flattened); void common_hal__pixelbuf_pixelbuf_set_pixels(mp_obj_t self_in, size_t start, mp_int_t step, size_t slice_len, mp_obj_t* values, mp_obj_tuple_t *flatten_to);
#endif // CP_SHARED_BINDINGS_PIXELBUF_PIXELBUF_H #endif // CP_SHARED_BINDINGS_PIXELBUF_PIXELBUF_H

View File

@ -230,22 +230,21 @@ void _pixelbuf_set_pixel(pixelbuf_pixelbuf_obj_t* self, size_t index, mp_obj_t v
_pixelbuf_set_pixel_color(self, index, r, g, b, w); _pixelbuf_set_pixel_color(self, index, r, g, b, w);
} }
void common_hal__pixelbuf_pixelbuf_set_pixels(mp_obj_t self_in, size_t start, mp_int_t step, size_t slice_len, mp_obj_t* values, bool flattened) { void common_hal__pixelbuf_pixelbuf_set_pixels(mp_obj_t self_in, size_t start, mp_int_t step, size_t slice_len, mp_obj_t* values,
mp_obj_tuple_t *flatten_to)
{
pixelbuf_pixelbuf_obj_t* self = native_pixelbuf(self_in); pixelbuf_pixelbuf_obj_t* self = native_pixelbuf(self_in);
mp_obj_iter_buf_t iter_buf; mp_obj_iter_buf_t iter_buf;
mp_obj_t iterable = mp_getiter(values, &iter_buf); mp_obj_t iterable = mp_getiter(values, &iter_buf);
mp_obj_t item; mp_obj_t item;
size_t i = 0; size_t i = 0;
mp_obj_tuple_t *tuple; bool flattened = flatten_to != mp_const_none;
uint bpp = self->bytes_per_pixel; if (flattened) flatten_to->len = self->bytes_per_pixel;
if (flattened) {
tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(bpp, NULL));
}
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
if (flattened) { if (flattened) {
tuple->items[i % bpp] = item; flatten_to->items[i % self->bytes_per_pixel] = item;
if (++i % bpp == 0) { if (++i % self->bytes_per_pixel == 0) {
_pixelbuf_set_pixel(self, start, tuple); _pixelbuf_set_pixel(self, start, flatten_to);
start+=step; start+=step;
} }
} else { } else {