Added bitmap.insert function for slice copy into a bitmap from another bitmap

This commit is contained in:
Margaret Matocha 2020-08-07 15:46:00 -05:00
parent eea01c32b8
commit 64c9baa6aa
3 changed files with 147 additions and 0 deletions

View File

@ -172,6 +172,77 @@ STATIC mp_obj_t bitmap_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t val
return mp_const_none;
}
//| def insert(self, x: int, y: int, source_bitmap: bitmap, x1: int, y1: int, x2: int, y2:int) -> Any:
//| """Inserts the source_bitmap region defined by rectangular boundaries
//| (x1,y1) and (x2,y2) into the bitmap at the specified (x,y) location.
//| :param int x: Horizontal pixel location in bitmap where source_bitmap upper-left
//| corner will be placed
//| :param int y: Vertical pixel location in bitmap where source_bitmap upper-left
//| corner will be placed
//| :param bitmap source_bitmap: Source bitmap that contains the graphical region to be copied
//| : param x1: Minimum x-value for rectangular bounding box to be copied from the source bitmap
//| : param y1: Minimum y-value for rectangular bounding box to be copied from the source bitmap
//| : param x2: Maximum x-value for rectangular bounding box to be copied from the source bitmap
//| : param y2: Maximum y-value for rectangular bounding box to be copied from the source bitmap
//|
//| ...
//|
//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){
STATIC mp_obj_t displayio_bitmap_obj_insert(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args){
// // convert the inputs into the correct type
// displayio_bitmap_t *self = MP_OBJ_TO_PTR(self_in);
// int16_t x = mp_obj_get_int(x_obj);
// int16_t y = mp_obj_get_int(y_obj);
// displayio_bitmap_t *source = MP_OBJ_TO_PTR(source_in);
// int16_t x1 = mp_obj_get_int(x1_obj);
// int16_t y1 = mp_obj_get_int(y1_obj);
// int16_t x2 = mp_obj_get_int(x2_obj);
// int16_t y2 = mp_obj_get_int(y2_obj);
displayio_bitmap_t *self = MP_OBJ_TO_PTR(pos_args[0]);
int16_t x = mp_obj_get_int(pos_args[1]);
int16_t y = mp_obj_get_int(pos_args[2]);
displayio_bitmap_t *source = MP_OBJ_TO_PTR(pos_args[3]);
int16_t x1 = mp_obj_get_int(pos_args[4]);
int16_t y1 = mp_obj_get_int(pos_args[5]);
int16_t x2 = mp_obj_get_int(pos_args[6]);
int16_t y2 = mp_obj_get_int(pos_args[7]);
if ( (x<0) || (y<0) || (x > self-> width) || (y > self->height) ) {
mp_raise_ValueError(translate("(x,y): out of range of target bitmap"));
}
if ( (x1 < 0) || (x1 > source->width) ||
(y1 < 0) || (y1 > source->height) ||
(x2 < 0) || (x2 > source->width) ||
(y2 < 0) || (y2 > source->height) ) {
mp_raise_ValueError(translate("(x1,y1) or (x2,y2): out of range of source bitmap"));
}
// Ensure x1 < x2 and y1 < y2
if (x1 > x2) {
int16_t temp=x2;
x2=x1;
x1=temp;
}
if (y1 > y2) {
int16_t temp=y2;
y2=y1;
y1=temp;
}
common_hal_displayio_bitmap_insert(self, x, y, source, x1, y1, x2, y2);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(displayio_bitmap_insert_obj, 8, displayio_bitmap_obj_insert);
/// What should this number value be? ****
//| def fill(self, value: Any) -> Any:
//| """Fills the bitmap with the supplied palette index value."""
//| ...
@ -192,6 +263,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(displayio_bitmap_fill_obj, displayio_bitmap_obj_fill);
STATIC const mp_rom_map_elem_t displayio_bitmap_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&displayio_bitmap_height_obj) },
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&displayio_bitmap_width_obj) },
{ MP_ROM_QSTR(MP_QSTR_insert), MP_ROM_PTR(&displayio_bitmap_insert_obj) }, // Added insert function 8/7/2020
{ MP_ROM_QSTR(MP_QSTR_fill), MP_ROM_PTR(&displayio_bitmap_fill_obj) },
};

View File

@ -40,6 +40,10 @@ uint16_t common_hal_displayio_bitmap_get_height(displayio_bitmap_t *self);
uint16_t common_hal_displayio_bitmap_get_width(displayio_bitmap_t *self);
uint32_t common_hal_displayio_bitmap_get_bits_per_value(displayio_bitmap_t *self);
void common_hal_displayio_bitmap_set_pixel(displayio_bitmap_t *bitmap, int16_t x, int16_t y, uint32_t value);
void common_hal_displayio_bitmap_insert(displayio_bitmap_t *self, int16_t x, int16_t y, displayio_bitmap_t *source,
int16_t x1, int16_t y1, int16_t x2, int16_t y2);
// New selective bitmap copy function "insert": KMatocha 8/7/2020
uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *bitmap, int16_t x, int16_t y);
void common_hal_displayio_bitmap_fill(displayio_bitmap_t *bitmap, uint32_t value);

View File

@ -105,6 +105,77 @@ uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *self, int16_t
return 0;
}
void common_hal_displayio_bitmap_insert(displayio_bitmap_t *self, int16_t x, int16_t y, displayio_bitmap_t *source,
int16_t x1, int16_t y1, int16_t x2, int16_t y2) {
// Copy complete "source" bitmap into "self" bitmap at location x,y in the "self"
// Add a boolean to determine if all values are copied, or only if non-zero
// Default is copy all values, but for text glyphs this should copy only non-zero values
if (self->read_only) {
mp_raise_RuntimeError(translate("Read-only object"));
}
//// check for zero width, this would be a single pixel
// if (x1_source == x2_source) || (y1_source == y2_source) {
// return 0
// }
// Dirty area update is unnecessary with simplest version.
// int16_t x_max=x+(x2_source-x1_source);
// int16_t y_max=y+(y2_source-y1_source);
//
//
// // Update the dirty area.
// if (x < self->dirty_area.x1) {
// self->dirty_area.x1 = x;
// } else if (x_max >= self->dirty_area.x2) {
// self->dirty_area.x2 = x_max);
// }
// if (y < self->dirty_area.y1) {
// self->dirty_area.y1 = y;
// } else if (y_max >= self->dirty_area.y2) {
// self->dirty_area.y2 = y_max;
// }
// simplest version - use internal functions for get/set pixels
for (uint16_t i=0; i<= (x2-x1) ; i++) {
for (uint16_t j=0; j<= (y2-y1) ; j++){
uint16_t value = common_hal_displayio_bitmap_get_pixel(source, x1+i, y1+j);
if ( (x+i >= 0) && (y+j >= 0) && (x+i <= self->width-1) && (y+j <= self->height-1) ){
common_hal_displayio_bitmap_set_pixel(self, x+i, y+j, value);
}
}
}
// Would be faster if copying a full word at a time.
/// Hard work here!!!!
// index into the original row
// figure out how to shift the bits into the right location
// check the boolean and don't update any zero's
// // Update our data
// int32_t row_start = y * self->stride;
// uint32_t bytes_per_value = self->bits_per_value / 8;
// if (bytes_per_value < 1) {
// uint32_t bit_position = (sizeof(size_t) * 8 - ((x & self->x_mask) + 1) * self->bits_per_value);
// uint32_t index = row_start + (x >> self->x_shift);
// uint32_t word = self->data[index];
// word &= ~(self->bitmask << bit_position);
// word |= (value & self->bitmask) << bit_position;
// self->data[index] = word;
// } else {
// size_t* row = self->data + row_start;
// if (bytes_per_value == 1) {
// ((uint8_t*) row)[x] = value;
// } else if (bytes_per_value == 2) {
// ((uint16_t*) row)[x] = value;
// } else if (bytes_per_value == 4) {
// ((uint32_t*) row)[x] = value;
// }
// }
}
void common_hal_displayio_bitmap_set_pixel(displayio_bitmap_t *self, int16_t x, int16_t y, uint32_t value) {
if (self->read_only) {
mp_raise_RuntimeError(translate("Read-only object"));