Skip to content

Commit 5b1a1c7

Browse files
authored
Merge pull request #3275 from kmatch98/bitmap_v2
Add `bitmap.blit` command for bitmap copy
2 parents 5b8ded2 + f55f2bf commit 5b1a1c7

File tree

4 files changed

+146
-6
lines changed

4 files changed

+146
-6
lines changed

locale/circuitpython.pot

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2020-08-18 11:19-0400\n"
11+
"POT-Creation-Date: 2020-08-21 21:39-0500\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -1349,10 +1349,6 @@ msgstr ""
13491349
msgid "Pull not used when direction is output."
13501350
msgstr ""
13511351

1352-
#: ports/stm/ref/pulseout-pre-timeralloc.c
1353-
msgid "PulseOut not supported on this chip"
1354-
msgstr ""
1355-
13561352
#: ports/stm/common-hal/os/__init__.c
13571353
msgid "RNG DeInit Error"
13581354
msgstr ""
@@ -2879,6 +2875,14 @@ msgstr ""
28792875
msgid "ord() expected a character, but string of length %d found"
28802876
msgstr ""
28812877

2878+
#: shared-bindings/displayio/Bitmap.c
2879+
msgid "out of range of source"
2880+
msgstr ""
2881+
2882+
#: shared-bindings/displayio/Bitmap.c
2883+
msgid "out of range of target"
2884+
msgstr ""
2885+
28822886
#: py/objint_mpz.c
28832887
msgid "overflow converting long int to machine word"
28842888
msgstr ""
@@ -3056,6 +3060,10 @@ msgstr ""
30563060
msgid "sosfilt requires iterable arguments"
30573061
msgstr ""
30583062

3063+
#: shared-bindings/displayio/Bitmap.c
3064+
msgid "source palette too large"
3065+
msgstr ""
3066+
30593067
#: py/objstr.c
30603068
msgid "start/end indices"
30613069
msgstr ""

shared-bindings/displayio/Bitmap.c

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,109 @@ 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, skip_index: int) -> None:
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+
//|
179+
//| :param int x: Horizontal pixel location in bitmap where source_bitmap upper-left
180+
//| corner will be placed
181+
//| :param int y: Vertical pixel location in bitmap where source_bitmap upper-left
182+
//| corner will be placed
183+
//| :param bitmap source_bitmap: Source bitmap that contains the graphical region to be copied
184+
//| :param int x1: Minimum x-value for rectangular bounding box to be copied from the source bitmap
185+
//| :param int y1: Minimum y-value for rectangular bounding box to be copied from the source bitmap
186+
//| :param int x2: Maximum x-value (exclusive) for rectangular bounding box to be copied from the source bitmap
187+
//| :param int y2: Maximum y-value (exclusive) for rectangular bounding box to be copied from the source bitmap
188+
//| :param int skip_index: bitmap palette index in the source that will not be copied,
189+
//| set to None to copy all pixels"""
190+
//| ...
191+
//|
192+
STATIC mp_obj_t displayio_bitmap_obj_blit(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args){
193+
enum {ARG_x, ARG_y, ARG_source, ARG_x1, ARG_y1, ARG_x2, ARG_y2, ARG_skip_index};
194+
static const mp_arg_t allowed_args[] = {
195+
{MP_QSTR_x, MP_ARG_REQUIRED | MP_ARG_INT},
196+
{MP_QSTR_y, MP_ARG_REQUIRED | MP_ARG_INT},
197+
{MP_QSTR_source_bitmap, MP_ARG_REQUIRED | MP_ARG_OBJ},
198+
{MP_QSTR_x1, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
199+
{MP_QSTR_y1, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
200+
{MP_QSTR_x2, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, // None convert to source->width
201+
{MP_QSTR_y2, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, // None convert to source->height
202+
{MP_QSTR_skip_index, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj=mp_const_none} },
203+
};
204+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
205+
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
206+
207+
displayio_bitmap_t *self = MP_OBJ_TO_PTR(pos_args[0]);
208+
209+
int16_t x = args[ARG_x].u_int;
210+
int16_t y = args[ARG_y].u_int;
211+
212+
displayio_bitmap_t *source = MP_OBJ_TO_PTR(args[ARG_source].u_obj);
213+
214+
// ensure that the target bitmap (self) has at least as many `bits_per_value` as the source
215+
if (self->bits_per_value < source->bits_per_value) {
216+
mp_raise_ValueError(translate("source palette too large"));
217+
}
218+
219+
int16_t x1 = args[ARG_x1].u_int;
220+
int16_t y1 = args[ARG_y1].u_int;
221+
int16_t x2, y2;
222+
// if x2 or y2 is None, then set as the maximum size of the source bitmap
223+
if ( args[ARG_x2].u_obj == mp_const_none ) {
224+
x2 = source->width;
225+
} else {
226+
x2 = mp_obj_get_int(args[ARG_x2].u_obj);
227+
}
228+
//int16_t y2;
229+
if ( args[ARG_y2].u_obj == mp_const_none ) {
230+
y2 = source->height;
231+
} else {
232+
y2 = mp_obj_get_int(args[ARG_y2].u_obj);
233+
}
234+
235+
// Check x,y are within self (target) bitmap boundary
236+
if ( (x < 0) || (y < 0) || (x > self->width) || (y > self->height) ) {
237+
mp_raise_ValueError(translate("out of range of target"));
238+
}
239+
// Check x1,y1,x2,y2 are within source bitmap boundary
240+
if ( (x1 < 0) || (x1 > source->width) ||
241+
(y1 < 0) || (y1 > source->height) ||
242+
(x2 < 0) || (x2 > source->width) ||
243+
(y2 < 0) || (y2 > source->height) ) {
244+
mp_raise_ValueError(translate("out of range of source"));
245+
}
246+
247+
// Ensure x1 < x2 and y1 < y2
248+
if (x1 > x2) {
249+
int16_t temp=x2;
250+
x2=x1;
251+
x1=temp;
252+
}
253+
if (y1 > y2) {
254+
int16_t temp=y2;
255+
y2=y1;
256+
y1=temp;
257+
}
258+
259+
uint32_t skip_index;
260+
bool skip_index_none; // flag whether skip_value was None
261+
262+
if (args[ARG_skip_index].u_obj == mp_const_none ) {
263+
skip_index = 0;
264+
skip_index_none = true;
265+
} else {
266+
skip_index = mp_obj_get_int(args[ARG_skip_index].u_obj);
267+
skip_index_none = false;
268+
}
269+
270+
common_hal_displayio_bitmap_blit(self, x, y, source, x1, y1, x2, y2, skip_index, skip_index_none);
271+
272+
return mp_const_none;
273+
}
274+
MP_DEFINE_CONST_FUN_OBJ_KW(displayio_bitmap_blit_obj, 4, displayio_bitmap_obj_blit);
275+
// `displayio_bitmap_obj_blit` requires at least 4 arguments
276+
277+
//| def fill(self, value: Any) -> None:
176278
//| """Fills the bitmap with the supplied palette index value."""
177279
//| ...
178280
//|
@@ -192,6 +294,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(displayio_bitmap_fill_obj, displayio_bitmap_obj_fill);
192294
STATIC const mp_rom_map_elem_t displayio_bitmap_locals_dict_table[] = {
193295
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&displayio_bitmap_height_obj) },
194296
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&displayio_bitmap_width_obj) },
297+
{ MP_ROM_QSTR(MP_QSTR_blit), MP_ROM_PTR(&displayio_bitmap_blit_obj) },
195298
{ MP_ROM_QSTR(MP_QSTR_fill), MP_ROM_PTR(&displayio_bitmap_fill_obj) },
196299

197300
};

shared-bindings/displayio/Bitmap.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ 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,
45+
uint32_t skip_index, bool skip_index_none);
4346
uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *bitmap, int16_t x, int16_t y);
4447
void common_hal_displayio_bitmap_fill(displayio_bitmap_t *bitmap, uint32_t value);
4548

shared-module/displayio/Bitmap.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,32 @@ 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, uint32_t skip_index, bool skip_index_none) {
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+
// If skip_value is encountered in the source bitmap, it will not be copied.
113+
// If skip_value is `None`, then all pixels are copied.
114+
115+
if (self->read_only) {
116+
mp_raise_RuntimeError(translate("Read-only object"));
117+
}
118+
119+
// 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) ) {
122+
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);
125+
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);
127+
}
128+
}
129+
}
130+
}
131+
}
132+
}
133+
108134
void common_hal_displayio_bitmap_set_pixel(displayio_bitmap_t *self, int16_t x, int16_t y, uint32_t value) {
109135
if (self->read_only) {
110136
mp_raise_RuntimeError(translate("Read-only object"));

0 commit comments

Comments
 (0)