Skip to content

Commit 5bf9325

Browse files
markshannondpgeorge
authored andcommitted
Fix out-of-range values in Image constructor and handling of neg strides.
1 parent a240daa commit 5bf9325

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

source/microbit/microbitimage.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ STATIC mp_obj_t microbit_image_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_u
303303
mp_int_t i = 0;
304304
for (mp_int_t y = 0; y < h; y++) {
305305
for (mp_int_t x = 0; x < w; ++x) {
306-
image->setPixelValue(x,y, ((const uint8_t*)bufinfo.buf)[i]);
306+
uint8_t val = min(((const uint8_t*)bufinfo.buf)[i], MAX_BRIGHTNESS);
307+
image->setPixelValue(x, y, val);
307308
++i;
308309
}
309310
}
@@ -649,11 +650,13 @@ STATIC mp_obj_t microbit_image_slice_iter_next(mp_obj_t o_in) {
649650
image_slice_iterator_t *iter = (image_slice_iterator_t *)o_in;
650651
mp_obj_t result;
651652
microbit_image_obj_t *img = iter->slice->img;
652-
if (iter->next_start <= img->width()-iter->slice->width) {
653-
result = image_crop(img, iter->next_start, 0, iter->next_start + iter->slice->width, img->height());
654-
} else {
655-
result = MP_OBJ_STOP_ITERATION;
653+
if (iter->slice->stride > 0 && iter->next_start >= img->width()) {
654+
return MP_OBJ_STOP_ITERATION;
655+
}
656+
if (iter->slice->stride < 0 && iter->next_start <= -iter->slice->width) {
657+
return MP_OBJ_STOP_ITERATION;
656658
}
659+
result = image_crop(img, iter->next_start, 0, iter->slice->width, img->height());
657660
iter->next_start += iter->slice->stride;
658661
return result;
659662
}

0 commit comments

Comments
 (0)