Skip to content

Commit 4ba9ff8

Browse files
Margaret Matochakmatch98
authored andcommitted
Added bitmap.blit function for copying slices of bitmaps
1 parent 9846932 commit 4ba9ff8

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

shared-bindings/displayio/Bitmap.c

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,66 @@ STATIC mp_obj_t bitmap_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t val
172172
return mp_const_none;
173173
}
174174

175-
//| def fill(self, value: int) -> None:
175+
//| def blit(self, x: int, y: int, source_bitmap: bitmap, x1: int, y1: int, x2: int, y2:int) -> Any:
176+
//| """Inserts the source_bitmap region defined by rectangular boundaries
177+
//| (x1,y1) and (x2,y2) into the bitmap at the specified (x,y) location.
178+
//| :param int x: Horizontal pixel location in bitmap where source_bitmap upper-left
179+
//| corner will be placed
180+
//| :param int y: Vertical pixel location in bitmap where source_bitmap upper-left
181+
//| corner will be placed
182+
//| :param bitmap source_bitmap: Source bitmap that contains the graphical region to be copied
183+
//| : param x1: Minimum x-value for rectangular bounding box to be copied from the source bitmap
184+
//| : param y1: Minimum y-value for rectangular bounding box to be copied from the source bitmap
185+
//| : param x2: Maximum x-value for rectangular bounding box to be copied from the source bitmap
186+
//| : param y2: Maximum y-value for rectangular bounding box to be copied from the source bitmap
187+
//|
188+
//| ...
189+
//|
190+
191+
STATIC mp_obj_t displayio_bitmap_obj_blit(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args){
192+
193+
// Consider improving the input checking.
194+
195+
displayio_bitmap_t *self = MP_OBJ_TO_PTR(pos_args[0]);
196+
int16_t x = mp_obj_get_int(pos_args[1]);
197+
int16_t y = mp_obj_get_int(pos_args[2]);
198+
displayio_bitmap_t *source = MP_OBJ_TO_PTR(pos_args[3]);
199+
int16_t x1 = mp_obj_get_int(pos_args[4]);
200+
int16_t y1 = mp_obj_get_int(pos_args[5]);
201+
int16_t x2 = mp_obj_get_int(pos_args[6]);
202+
int16_t y2 = mp_obj_get_int(pos_args[7]);
203+
204+
if ( (x<0) || (y<0) || (x > self-> width) || (y > self->height) ) {
205+
mp_raise_ValueError(translate("(x,y): out of range of target bitmap"));
206+
}
207+
if ( (x1 < 0) || (x1 > source->width) ||
208+
(y1 < 0) || (y1 > source->height) ||
209+
(x2 < 0) || (x2 > source->width) ||
210+
(y2 < 0) || (y2 > source->height) ) {
211+
mp_raise_ValueError(translate("(x1,y1) or (x2,y2): out of range of source bitmap"));
212+
}
213+
214+
// Ensure x1 < x2 and y1 < y2
215+
if (x1 > x2) {
216+
int16_t temp=x2;
217+
x2=x1;
218+
x1=temp;
219+
}
220+
if (y1 > y2) {
221+
int16_t temp=y2;
222+
y2=y1;
223+
y1=temp;
224+
}
225+
226+
common_hal_displayio_bitmap_blit(self, x, y, source, x1, y1, x2, y2);
227+
228+
return mp_const_none;
229+
}
230+
231+
MP_DEFINE_CONST_FUN_OBJ_KW(displayio_bitmap_blit_obj, 8, displayio_bitmap_obj_blit);
232+
// requires 8 parameters
233+
234+
//| def fill(self, value: Any) -> Any:
176235
//| """Fills the bitmap with the supplied palette index value."""
177236
//| ...
178237
//|
@@ -192,6 +251,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(displayio_bitmap_fill_obj, displayio_bitmap_obj_fill);
192251
STATIC const mp_rom_map_elem_t displayio_bitmap_locals_dict_table[] = {
193252
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&displayio_bitmap_height_obj) },
194253
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&displayio_bitmap_width_obj) },
254+
{ MP_ROM_QSTR(MP_QSTR_blit), MP_ROM_PTR(&displayio_bitmap_blit_obj) },
195255
{ MP_ROM_QSTR(MP_QSTR_fill), MP_ROM_PTR(&displayio_bitmap_fill_obj) },
196256

197257
};

shared-bindings/displayio/Bitmap.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ uint16_t common_hal_displayio_bitmap_get_height(displayio_bitmap_t *self);
4040
uint16_t common_hal_displayio_bitmap_get_width(displayio_bitmap_t *self);
4141
uint32_t common_hal_displayio_bitmap_get_bits_per_value(displayio_bitmap_t *self);
4242
void common_hal_displayio_bitmap_set_pixel(displayio_bitmap_t *bitmap, int16_t x, int16_t y, uint32_t value);
43+
void common_hal_displayio_bitmap_blit(displayio_bitmap_t *self, int16_t x, int16_t y, displayio_bitmap_t *source,
44+
int16_t x1, int16_t y1, int16_t x2, int16_t y2);
4345
uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *bitmap, int16_t x, int16_t y);
4446
void common_hal_displayio_bitmap_fill(displayio_bitmap_t *bitmap, uint32_t value);
4547

shared-module/displayio/Bitmap.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,37 @@ uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *self, int16_t
105105
return 0;
106106
}
107107

108+
void common_hal_displayio_bitmap_blit(displayio_bitmap_t *self, int16_t x, int16_t y, displayio_bitmap_t *source,
109+
int16_t x1, int16_t y1, int16_t x2, int16_t y2) {
110+
// Copy complete "source" bitmap into "self" bitmap at location x,y in the "self"
111+
// Add a boolean to determine if all values are copied, or only if non-zero
112+
// Default is copy all values, but for text glyphs this should copy only non-zero values
113+
114+
if (self->read_only) {
115+
mp_raise_RuntimeError(translate("Read-only object"));
116+
}
117+
118+
// If this value is encountered in the source bitmap, it will not be copied (for text glyphs)
119+
// This should be added as an optional parameter, and if it is `None`, then all pixels are copied
120+
uint32_t skip_value=0;
121+
122+
// simplest version - use internal functions for get/set pixels
123+
for (uint16_t i=0; i<= (x2-x1) ; i++) {
124+
if ( (x+i >= 0) && (x+i < self->width) ) {
125+
for (uint16_t j=0; j<= (y2-y1) ; j++){
126+
if ((y+j >= 0) && (y+j < self->height) ) {
127+
uint32_t value = common_hal_displayio_bitmap_get_pixel(source, x1+i, y1+j);
128+
if ( (value != skip_value) ) {
129+
common_hal_displayio_bitmap_set_pixel(self, x+i, y+j, value);
130+
}
131+
}
132+
}
133+
}
134+
}
135+
136+
}
137+
138+
108139
void common_hal_displayio_bitmap_set_pixel(displayio_bitmap_t *self, int16_t x, int16_t y, uint32_t value) {
109140
if (self->read_only) {
110141
mp_raise_RuntimeError(translate("Read-only object"));

0 commit comments

Comments
 (0)