Merge pull request #2941 from dunkmann00/pixelbuf-negative-step-slice

Implement negative step for pixelbuf slices
This commit is contained in:
Jeff Epler 2020-05-23 22:23:43 -05:00 committed by GitHub
commit 37a7cf9934
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 21 deletions

View File

@ -300,17 +300,20 @@ 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);
mp_seq_get_fast_slice_indexes(length, index_in, &slice);
if (slice.step < 0) {
mp_raise_IndexError(translate("Negative step not supported"));
size_t slice_len;
if (slice.step > 0) {
slice_len = slice.stop - slice.start;
} else {
slice_len = 1 + slice.start - slice.stop;
}
if (slice.step > 1 || slice.step < -1) {
size_t step = slice.step > 0 ? slice.step : slice.step * -1;
slice_len = (slice_len / step) + (slice_len % step ? 1 : 0);
}
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);
}
mp_obj_tuple_t* t = MP_OBJ_TO_PTR(mp_obj_new_tuple(len, NULL));
for (uint i = 0; i < len; i++) {
mp_obj_tuple_t* t = MP_OBJ_TO_PTR(mp_obj_new_tuple(slice_len, NULL));
for (uint i = 0; i < slice_len; i++) {
t->items[i] = common_hal__pixelbuf_pixelbuf_get_pixel(self_in, i * slice.step + slice.start);
}
return MP_OBJ_FROM_PTR(t);
@ -321,10 +324,6 @@ STATIC mp_obj_t pixelbuf_pixelbuf_subscr(mp_obj_t self_in, mp_obj_t index_in, mp
mp_raise_ValueError(translate("tuple/list required on RHS"));
}
size_t dst_len = (slice.stop - slice.start);
if (slice.step > 1) {
dst_len = (dst_len / slice.step) + (dst_len % slice.step ? 1 : 0);
}
mp_obj_t *src_objs;
size_t num_items;
if (MP_OBJ_IS_TYPE(value, &mp_type_list)) {
@ -336,12 +335,12 @@ STATIC mp_obj_t pixelbuf_pixelbuf_subscr(mp_obj_t self_in, mp_obj_t index_in, mp
num_items = l->len;
src_objs = l->items;
}
if (num_items != dst_len) {
if (num_items != slice_len) {
mp_raise_ValueError_varg(translate("Unmatched number of items on RHS (expected %d, got %d)."),
dst_len, num_items);
slice_len, num_items);
}
common_hal__pixelbuf_pixelbuf_set_pixels(self_in, slice.start, slice.stop, slice.step, src_objs);
common_hal__pixelbuf_pixelbuf_set_pixels(self_in, slice.start, slice.step, slice_len, src_objs);
return mp_const_none;
#else
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);
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_pixels(mp_obj_t self_in, size_t start, size_t stop, size_t step, mp_obj_t* values);
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);
#endif // CP_SHARED_BINDINGS_PIXELBUF_PIXELBUF_H

View File

@ -216,12 +216,11 @@ 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);
}
void common_hal__pixelbuf_pixelbuf_set_pixels(mp_obj_t self_in, size_t start, size_t stop, size_t step, mp_obj_t* values) {
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) {
pixelbuf_pixelbuf_obj_t* self = native_pixelbuf(self_in);
size_t source_i = 0;
for (size_t target_i = start; target_i < stop; target_i += step) {
_pixelbuf_set_pixel(self, target_i, values[source_i]);
source_i++;
for (size_t i = 0; i < slice_len; i++) {
_pixelbuf_set_pixel(self, start, values[i]);
start+=step;
}
if (self->auto_write) {
common_hal__pixelbuf_pixelbuf_show(self_in);