Use mp_int_t for setting pixelbuf slice indices

When handling negative steps, start and stop need to be mp_int_t so they
can be checked against a potential negative value during the for loop
used to set the slice values.
This commit is contained in:
George Waters 2020-05-22 16:28:09 -04:00
parent 16ffc731f3
commit f078055f59
No known key found for this signature in database
GPG Key ID: D993F8B1CC21DB25
3 changed files with 6 additions and 6 deletions

View File

@ -303,7 +303,7 @@ STATIC mp_obj_t pixelbuf_pixelbuf_subscr(mp_obj_t self_in, mp_obj_t index_in, mp
size_t slice_len; size_t slice_len;
if (slice.step > 0) { if (slice.step > 0) {
slice_len = slice.stop - slice.start; slice_len = slice.stop - slice.start;
}else{ } else {
slice_len = 1 + slice.start - slice.stop; slice_len = 1 + slice.start - slice.stop;
} }
if (slice.step > 1 || slice.step < -1) { if (slice.step > 1 || slice.step < -1) {

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, size_t stop, mp_int_t step, mp_obj_t* values); void common_hal__pixelbuf_pixelbuf_set_pixels(mp_obj_t self_in, mp_int_t start, mp_int_t stop, mp_int_t step, mp_obj_t* values);
#endif // CP_SHARED_BINDINGS_PIXELBUF_PIXELBUF_H #endif // CP_SHARED_BINDINGS_PIXELBUF_PIXELBUF_H

View File

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