Skip to content

Commit 24ca133

Browse files
committed
Updating main to adafruit/main
2 parents 1e7e42b + b6008d0 commit 24ca133

File tree

3 files changed

+154
-1
lines changed

3 files changed

+154
-1
lines changed

shared-bindings/displayio/Bitmap.c

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,78 @@ 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 insert(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_insert(mp_obj_t self_in, mp_obj_t x_obj, mp_obj_t y_obj, mp_obj_t source_in, mp_obj_t x1_obj, mp_obj_t y1_obj, mp_obj_t x2_obj, mp_obj_t y2_obj){
192+
STATIC mp_obj_t displayio_bitmap_obj_insert(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args){
193+
194+
// // convert the inputs into the correct type
195+
// displayio_bitmap_t *self = MP_OBJ_TO_PTR(self_in);
196+
// int16_t x = mp_obj_get_int(x_obj);
197+
// int16_t y = mp_obj_get_int(y_obj);
198+
// displayio_bitmap_t *source = MP_OBJ_TO_PTR(source_in);
199+
// int16_t x1 = mp_obj_get_int(x1_obj);
200+
// int16_t y1 = mp_obj_get_int(y1_obj);
201+
// int16_t x2 = mp_obj_get_int(x2_obj);
202+
// int16_t y2 = mp_obj_get_int(y2_obj);
203+
displayio_bitmap_t *self = MP_OBJ_TO_PTR(pos_args[0]);
204+
int16_t x = mp_obj_get_int(pos_args[1]);
205+
int16_t y = mp_obj_get_int(pos_args[2]);
206+
displayio_bitmap_t *source = MP_OBJ_TO_PTR(pos_args[3]);
207+
int16_t x1 = mp_obj_get_int(pos_args[4]);
208+
int16_t y1 = mp_obj_get_int(pos_args[5]);
209+
int16_t x2 = mp_obj_get_int(pos_args[6]);
210+
int16_t y2 = mp_obj_get_int(pos_args[7]);
211+
212+
213+
214+
215+
if ( (x<0) || (y<0) || (x > self-> width) || (y > self->height) ) {
216+
mp_raise_ValueError(translate("(x,y): out of range of target bitmap"));
217+
}
218+
if ( (x1 < 0) || (x1 > source->width) ||
219+
(y1 < 0) || (y1 > source->height) ||
220+
(x2 < 0) || (x2 > source->width) ||
221+
(y2 < 0) || (y2 > source->height) ) {
222+
mp_raise_ValueError(translate("(x1,y1) or (x2,y2): out of range of source bitmap"));
223+
}
224+
225+
// Ensure x1 < x2 and y1 < y2
226+
if (x1 > x2) {
227+
int16_t temp=x2;
228+
x2=x1;
229+
x1=temp;
230+
}
231+
if (y1 > y2) {
232+
int16_t temp=y2;
233+
y2=y1;
234+
y1=temp;
235+
}
236+
237+
common_hal_displayio_bitmap_insert(self, x, y, source, x1, y1, x2, y2);
238+
239+
return mp_const_none;
240+
}
241+
242+
MP_DEFINE_CONST_FUN_OBJ_KW(displayio_bitmap_insert_obj, 8, displayio_bitmap_obj_insert);
243+
/// What should this number value be? ****
244+
245+
246+
//| def fill(self, value: Any) -> Any:
176247
//| """Fills the bitmap with the supplied palette index value."""
177248
//| ...
178249
//|
@@ -192,6 +263,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(displayio_bitmap_fill_obj, displayio_bitmap_obj_fill);
192263
STATIC const mp_rom_map_elem_t displayio_bitmap_locals_dict_table[] = {
193264
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&displayio_bitmap_height_obj) },
194265
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&displayio_bitmap_width_obj) },
266+
{ MP_ROM_QSTR(MP_QSTR_insert), MP_ROM_PTR(&displayio_bitmap_insert_obj) }, // Added insert function 8/7/2020
195267
{ MP_ROM_QSTR(MP_QSTR_fill), MP_ROM_PTR(&displayio_bitmap_fill_obj) },
196268

197269
};

shared-bindings/displayio/Bitmap.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ 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_insert(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+
// New selective bitmap copy function "insert": KMatocha 8/7/2020
46+
4347
uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *bitmap, int16_t x, int16_t y);
4448
void common_hal_displayio_bitmap_fill(displayio_bitmap_t *bitmap, uint32_t value);
4549

shared-module/displayio/Bitmap.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,83 @@ 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_insert(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+
//// check for zero width, this would be a single pixel
123+
// if (x1_source == x2_source) || (y1_source == y2_source) {
124+
// return 0
125+
// }
126+
127+
// Dirty area update is unnecessary with simplest version.
128+
// int16_t x_max=x+(x2_source-x1_source);
129+
// int16_t y_max=y+(y2_source-y1_source);
130+
//
131+
//
132+
// // Update the dirty area.
133+
// if (x < self->dirty_area.x1) {
134+
// self->dirty_area.x1 = x;
135+
// } else if (x_max >= self->dirty_area.x2) {
136+
// self->dirty_area.x2 = x_max);
137+
// }
138+
// if (y < self->dirty_area.y1) {
139+
// self->dirty_area.y1 = y;
140+
// } else if (y_max >= self->dirty_area.y2) {
141+
// self->dirty_area.y2 = y_max;
142+
// }
143+
144+
// simplest version - use internal functions for get/set pixels
145+
for (uint16_t i=0; i<= (x2-x1) ; i++) {
146+
for (uint16_t j=0; j<= (y2-y1) ; j++){
147+
uint32_t value = common_hal_displayio_bitmap_get_pixel(source, x1+i, y1+j);
148+
if (value != skip_value) {
149+
if ( (x+i >= 0) && (y+j >= 0) && (x+i <= self->width-1) && (y+j <= self->height-1) ){
150+
common_hal_displayio_bitmap_set_pixel(self, x+i, y+j, value);
151+
}
152+
}
153+
}
154+
}
155+
156+
// Would be faster if copying a full word at a time.
157+
/// Hard work here!!!!
158+
// index into the original row
159+
// figure out how to shift the bits into the right location
160+
// check the boolean and don't update any zero's
161+
162+
// // Update our data
163+
// int32_t row_start = y * self->stride;
164+
// uint32_t bytes_per_value = self->bits_per_value / 8;
165+
// if (bytes_per_value < 1) {
166+
// uint32_t bit_position = (sizeof(size_t) * 8 - ((x & self->x_mask) + 1) * self->bits_per_value);
167+
// uint32_t index = row_start + (x >> self->x_shift);
168+
// uint32_t word = self->data[index];
169+
// word &= ~(self->bitmask << bit_position);
170+
// word |= (value & self->bitmask) << bit_position;
171+
// self->data[index] = word;
172+
// } else {
173+
// size_t* row = self->data + row_start;
174+
// if (bytes_per_value == 1) {
175+
// ((uint8_t*) row)[x] = value;
176+
// } else if (bytes_per_value == 2) {
177+
// ((uint16_t*) row)[x] = value;
178+
// } else if (bytes_per_value == 4) {
179+
// ((uint32_t*) row)[x] = value;
180+
// }
181+
// }
182+
}
183+
184+
108185
void common_hal_displayio_bitmap_set_pixel(displayio_bitmap_t *self, int16_t x, int16_t y, uint32_t value) {
109186
if (self->read_only) {
110187
mp_raise_RuntimeError(translate("Read-only object"));

0 commit comments

Comments
 (0)