Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4097c94

Browse files
committed
Update for clean blitting into itself
1 parent d4bf0d5 commit 4097c94

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

shared-module/displayio/Bitmap.c

+24-5
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,33 @@ void common_hal_displayio_bitmap_blit(displayio_bitmap_t *self, int16_t x, int16
116116
mp_raise_RuntimeError(translate("Read-only object"));
117117
}
118118

119+
bool x_reverse = false;
120+
bool y_reverse = false;
121+
122+
// Add reverse direction option to protect blitting of self bitmap back into self bitmap
123+
if (x > x1) {
124+
x_reverse = true;
125+
}
126+
if (y > y1) {
127+
y_reverse = true;
128+
}
129+
119130
// simplest version - use internal functions for get/set pixels
120-
for (int16_t i=0; i < (x2-x1) ; i++) {
121-
if ( (x+i >= 0) && (x+i < self->width) ) {
131+
for (int16_t i=0; i < (x2-x1); i++) {
132+
133+
const int xs_index = x_reverse ? ( (x2 - 1) - i ) : x1+i; // x-index into the source bitmap
134+
const int xd_index = x_reverse ? ((x + (x2-x1)) - i ) : x+i; // x-index into the destination bitmap
135+
136+
if ( (xd_index >= 0) && (xd_index < self->width) ) {
122137
for (int16_t j=0; j < (y2-y1) ; j++){
123-
if ((y+j >= 0) && (y+j < self->height) ) {
124-
uint32_t value = common_hal_displayio_bitmap_get_pixel(source, x1+i, y1+j);
138+
139+
const int ys_index = y_reverse ? ( (y2 - 1) - j ) : y1+j ; // y-index into the source bitmap
140+
const int yd_index = y_reverse ? ((y + (y2-y1)) - j ) : y+j ; // y-index into the destination bitmap
141+
142+
if ((yd_index >= 0) && (yd_index < self->height) ) {
143+
uint32_t value = common_hal_displayio_bitmap_get_pixel(source, xs_index, ys_index);
125144
if ( (skip_index_none) || (value != skip_index) ) { // write if skip_value_none is True
126-
common_hal_displayio_bitmap_set_pixel(self, x+i, y+j, value);
145+
common_hal_displayio_bitmap_set_pixel(self, xd_index, yd_index, value);
127146
}
128147
}
129148
}

0 commit comments

Comments
 (0)