Memory savings and more properties

This commit is contained in:
gamblor21 2023-02-05 10:54:04 -06:00
parent 2b26242c66
commit dbe518680a
4 changed files with 114 additions and 79 deletions

View File

@ -123,25 +123,6 @@ MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_get_height_obj, displayio_ondiskgi
MP_PROPERTY_GETTER(displayio_ondiskgif_height_obj, MP_PROPERTY_GETTER(displayio_ondiskgif_height_obj,
(mp_obj_t)&displayio_ondiskgif_get_height_obj); (mp_obj_t)&displayio_ondiskgif_get_height_obj);
//| pixel_shader: Union[ColorConverter, Palette]
//| """The image's pixel_shader. The type depends on the underlying
//| bitmap's structure. The pixel shader can be modified (e.g., to set the
//| transparent pixel or, for palette shaded images, to update the palette.)"""
//|
STATIC mp_obj_t displayio_ondiskgif_obj_get_pixel_shader(mp_obj_t self_in) {
displayio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in);
return common_hal_displayio_ondiskgif_get_pixel_shader(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_get_pixel_shader_obj, displayio_ondiskgif_obj_get_pixel_shader);
const mp_obj_property_t displayio_ondiskgif_pixel_shader_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&displayio_ondiskgif_get_pixel_shader_obj,
(mp_obj_t)MP_ROM_NONE,
(mp_obj_t)MP_ROM_NONE},
};
//| bitmap: Bitmap //| bitmap: Bitmap
//| """The image's bitmap. The type depends on the underlying //| """The image's bitmap. The type depends on the underlying
//| bitmap's structure. The pixel shader can be modified (e.g., to set the //| bitmap's structure. The pixel shader can be modified (e.g., to set the
@ -154,12 +135,16 @@ STATIC mp_obj_t displayio_ondiskgif_obj_get_bitmap(mp_obj_t self_in) {
MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_get_bitmap_obj, displayio_ondiskgif_obj_get_bitmap); MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_get_bitmap_obj, displayio_ondiskgif_obj_get_bitmap);
MP_PROPERTY_GETTER(displayio_ondiskgif_bitmap_obj,
(mp_obj_t)&displayio_ondiskgif_get_bitmap_obj);
/*
const mp_obj_property_t displayio_ondiskgif_bitmap_obj = { const mp_obj_property_t displayio_ondiskgif_bitmap_obj = {
.base.type = &mp_type_property, .base.type = &mp_type_property,
.proxy = {(mp_obj_t)&displayio_ondiskgif_get_bitmap_obj, .proxy = {(mp_obj_t)&displayio_ondiskgif_get_bitmap_obj,
(mp_obj_t)MP_ROM_NONE, (mp_obj_t)MP_ROM_NONE,
(mp_obj_t)MP_ROM_NONE}, (mp_obj_t)MP_ROM_NONE},
}; };*/
//| play_frame: None //| play_frame: None
@ -173,13 +158,68 @@ STATIC mp_obj_t displayio_ondiskgif_obj_play_frame(mp_obj_t self_in) {
MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_play_frame_obj, displayio_ondiskgif_obj_play_frame); MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_play_frame_obj, displayio_ondiskgif_obj_play_frame);
//| duration: int
//| """Height of the bitmap. (read only)"""
STATIC mp_obj_t displayio_ondiskgif_obj_get_duration(mp_obj_t self_in) {
displayio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_ondiskgif_get_duration(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_get_duration_obj, displayio_ondiskgif_obj_get_duration);
MP_PROPERTY_GETTER(displayio_ondiskgif_duration_obj,
(mp_obj_t)&displayio_ondiskgif_get_duration_obj);
//| frame_count: int
//| """Height of the bitmap. (read only)"""
STATIC mp_obj_t displayio_ondiskgif_obj_get_frame_count(mp_obj_t self_in) {
displayio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_ondiskgif_get_frame_count(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_get_frame_count_obj, displayio_ondiskgif_obj_get_frame_count);
MP_PROPERTY_GETTER(displayio_ondiskgif_frame_count_obj,
(mp_obj_t)&displayio_ondiskgif_get_frame_count_obj);
//| min_delay: int
//| """Height of the bitmap. (read only)"""
STATIC mp_obj_t displayio_ondiskgif_obj_get_min_delay(mp_obj_t self_in) {
displayio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_ondiskgif_get_min_delay(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_get_min_delay_obj, displayio_ondiskgif_obj_get_min_delay);
MP_PROPERTY_GETTER(displayio_ondiskgif_min_delay_obj,
(mp_obj_t)&displayio_ondiskgif_get_min_delay_obj);
//| max_delay: int
//| """Height of the bitmap. (read only)"""
//|
STATIC mp_obj_t displayio_ondiskgif_obj_get_max_delay(mp_obj_t self_in) {
displayio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_ondiskgif_get_max_delay(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_get_max_delay_obj, displayio_ondiskgif_obj_get_max_delay);
MP_PROPERTY_GETTER(displayio_ondiskgif_max_delay_obj,
(mp_obj_t)&displayio_ondiskgif_get_max_delay_obj);
STATIC const mp_rom_map_elem_t displayio_ondiskgif_locals_dict_table[] = { STATIC const mp_rom_map_elem_t displayio_ondiskgif_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&displayio_ondiskgif_height_obj) }, { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&displayio_ondiskgif_height_obj) },
{ MP_ROM_QSTR(MP_QSTR_pixel_shader), MP_ROM_PTR(&displayio_ondiskgif_pixel_shader_obj) },
{ MP_ROM_QSTR(MP_QSTR_bitmap), MP_ROM_PTR(&displayio_ondiskgif_bitmap_obj) }, { MP_ROM_QSTR(MP_QSTR_bitmap), MP_ROM_PTR(&displayio_ondiskgif_bitmap_obj) },
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&displayio_ondiskgif_width_obj) }, { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&displayio_ondiskgif_width_obj) },
{ MP_ROM_QSTR(MP_QSTR_play_frame), MP_ROM_PTR(&displayio_ondiskgif_play_frame_obj) }, { MP_ROM_QSTR(MP_QSTR_play_frame), MP_ROM_PTR(&displayio_ondiskgif_play_frame_obj) },
{ MP_ROM_QSTR(MP_QSTR_duration), MP_ROM_PTR(&displayio_ondiskgif_duration_obj) },
{ MP_ROM_QSTR(MP_QSTR_frame_count), MP_ROM_PTR(&displayio_ondiskgif_frame_count_obj) },
{ MP_ROM_QSTR(MP_QSTR_min_delay), MP_ROM_PTR(&displayio_ondiskgif_min_delay_obj) },
{ MP_ROM_QSTR(MP_QSTR_max_delay), MP_ROM_PTR(&displayio_ondiskgif_max_delay_obj) },
}; };
STATIC MP_DEFINE_CONST_DICT(displayio_ondiskgif_locals_dict, displayio_ondiskgif_locals_dict_table); STATIC MP_DEFINE_CONST_DICT(displayio_ondiskgif_locals_dict, displayio_ondiskgif_locals_dict_table);

View File

@ -42,4 +42,8 @@ mp_obj_t common_hal_displayio_ondiskgif_get_pixel_shader(displayio_ondiskgif_t *
mp_obj_t common_hal_displayio_ondiskgif_get_bitmap(displayio_ondiskgif_t *self); mp_obj_t common_hal_displayio_ondiskgif_get_bitmap(displayio_ondiskgif_t *self);
uint16_t common_hal_displayio_ondiskgif_get_width(displayio_ondiskgif_t *self); uint16_t common_hal_displayio_ondiskgif_get_width(displayio_ondiskgif_t *self);
uint8_t common_hal_displayio_ondiskgif_play_frame(displayio_ondiskgif_t *self); uint8_t common_hal_displayio_ondiskgif_play_frame(displayio_ondiskgif_t *self);
int32_t common_hal_displayio_ondiskgif_get_duration(displayio_ondiskgif_t *self);
int32_t common_hal_displayio_ondiskgif_get_frame_count(displayio_ondiskgif_t *self);
int32_t common_hal_displayio_ondiskgif_get_min_delay(displayio_ondiskgif_t *self);
int32_t common_hal_displayio_ondiskgif_get_max_delay(displayio_ondiskgif_t *self);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_ONDISKGIF_H #endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_ONDISKGIF_H

View File

@ -76,41 +76,33 @@ static void GIFDraw(GIFDRAW *pDraw) {
displayio_bitmap_t *bitmap = (displayio_bitmap_t *)pDraw->pUser; displayio_bitmap_t *bitmap = (displayio_bitmap_t *)pDraw->pUser;
// mp_printf(&mp_plat_print, "GD: y%d iX%d iY%d iW%d iH%d Trans%d hasT%d bk%d pUser %x bmp%x\n", uint8_t *s;
// pDraw->y, pDraw->iX, pDraw->iY, pDraw->iWidth, pDraw->iHeight, pDraw->ucTransparent, pDraw->ucHasTransparency, pDraw->ucBackground, pDraw->pUser, bitmap->data); uint16_t *d;
/*
int iX, iY; // Corner offset of this frame on the canvas
int y; // current line being drawn (0 = top line of image)
int iWidth, iHeight; // size of this frame
void *pUser; // user supplied pointer
uint8_t *pPixels; // 8-bit source pixels for this line
uint16_t *pPalette; // little or big-endian RGB565 palette entries (default)
uint8_t *pPalette24; // RGB888 palette (optional)
uint8_t ucTransparent; // transparent color
uint8_t ucHasTransparency; // flag indicating the transparent color is in use
uint8_t ucDisposalMethod; // frame disposal method
uint8_t ucBackground; // background color
uint8_t ucIsGlobalPalette; // Flag to indicate that a global palette, rather than a local palette is being used
*/
// For all other lines, just push the pixels to the display
// (uint8_t *)pDraw->pPixels, pDraw->iWidth*2);
int32_t row_start = pDraw->y * bitmap->stride; int32_t row_start = pDraw->y * bitmap->stride;
uint32_t *row = bitmap->data + row_start; uint32_t *row = bitmap->data + row_start;
uint8_t *d = (uint8_t *)row; s = pDraw->pPixels;
// mp_printf(&mp_plat_print, "rs %d strd %d:\n", row_start, bitmap->stride); d = (uint16_t *)row;
uint8_t *p = (uint8_t *)pDraw->pPixels; uint16_t *pPal;
for (int w = 0; w < pDraw->iWidth * 2; w++) { pPal = (uint16_t *)pDraw->pPalette;
// mp_printf(&mp_plat_print, "%x:", *p);
*d++ = *p; if (pDraw->ucDisposalMethod == 2) { // restore to background color
p++; memset(d, pDraw->ucBackground, pDraw->iWidth);
}
// We always check for transpancy even if the gif does not have it
// as we also convert the color to the palette here
// Could separate it but would not sure it would be much a speed up
uint8_t c, ucTransparent = pDraw->ucTransparent;
for (int x = 0; x < pDraw->iWidth; x++)
{
c = *s++;
if (c != ucTransparent) {
*d = pPal[c];
}
d++;
} }
// mp_printf(&mp_plat_print, "\n");
} }
void common_hal_displayio_ondiskgif_construct(displayio_ondiskgif_t *self, pyb_file_obj_t *file) { void common_hal_displayio_ondiskgif_construct(displayio_ondiskgif_t *self, pyb_file_obj_t *file) {
@ -135,33 +127,22 @@ void common_hal_displayio_ondiskgif_construct(displayio_ondiskgif_t *self, pyb_f
mp_arg_error_invalid(MP_QSTR_file); mp_arg_error_invalid(MP_QSTR_file);
} }
self->frame = m_malloc(self->gif.iCanvasWidth * self->gif.iCanvasHeight * sizeof(uint16_t), false); // MUST FREE LATER?
self->gif.pFrameBuffer = (uint8_t *)self->frame;
self->gif.ucDrawType = GIF_DRAW_COOKED;
displayio_bitmap_t *bitmap = m_new_obj(displayio_bitmap_t); displayio_bitmap_t *bitmap = m_new_obj(displayio_bitmap_t);
bitmap->base.type = &displayio_bitmap_type; bitmap->base.type = &displayio_bitmap_type;
common_hal_displayio_bitmap_construct(bitmap, self->gif.iCanvasWidth, self->gif.iCanvasHeight, 16); common_hal_displayio_bitmap_construct(bitmap, self->gif.iCanvasWidth, self->gif.iCanvasHeight, 16);
self->bitmap = bitmap; self->bitmap = bitmap;
GIFINFO info;
GIF_getInfo(&self->gif, &info);
self->duration = info.iDuration;
self->frame_count = info.iFrameCount;
self->min_delay = info.iMinDelay;
self->max_delay = info.iMaxDelay;
// mp_printf(&mp_plat_print, "GIF_init returned %d %x\n", result, bitmap->data); // mp_printf(&mp_plat_print, "GIF_init returned %d %x\n", result, bitmap->data);
} }
uint32_t common_hal_displayio_ondiskgif_get_pixel(displayio_ondiskgif_t *self,
int16_t x, int16_t y) {
if (x < 0 || x >= self->gif.iCanvasWidth || y < 0 || y >= self->gif.iCanvasHeight) {
return 0;
}
return 0;
}
uint16_t common_hal_displayio_ondiskgif_get_height(displayio_ondiskgif_t *self) { uint16_t common_hal_displayio_ondiskgif_get_height(displayio_ondiskgif_t *self) {
GIFINFO info;
GIF_getInfo(&self->gif, &info);
mp_printf(&mp_plat_print, "dur %d fc %d max %d min %d\n", info.iDuration, info.iFrameCount, info.iMaxDelay, info.iMinDelay);
return (uint16_t)self->gif.iCanvasHeight; return (uint16_t)self->gif.iCanvasHeight;
} }
@ -169,14 +150,26 @@ uint16_t common_hal_displayio_ondiskgif_get_width(displayio_ondiskgif_t *self) {
return (uint16_t)self->gif.iCanvasWidth; return (uint16_t)self->gif.iCanvasWidth;
} }
mp_obj_t common_hal_displayio_ondiskgif_get_pixel_shader(displayio_ondiskgif_t *self) {
return MP_OBJ_FROM_PTR(self->pixel_shader_base);
}
mp_obj_t common_hal_displayio_ondiskgif_get_bitmap(displayio_ondiskgif_t *self) { mp_obj_t common_hal_displayio_ondiskgif_get_bitmap(displayio_ondiskgif_t *self) {
return MP_OBJ_FROM_PTR(self->bitmap); return MP_OBJ_FROM_PTR(self->bitmap);
} }
int32_t common_hal_displayio_ondiskgif_get_duration(displayio_ondiskgif_t *self) {
return self->duration;
}
int32_t common_hal_displayio_ondiskgif_get_frame_count(displayio_ondiskgif_t *self) {
return self->frame_count;
}
int32_t common_hal_displayio_ondiskgif_get_min_delay(displayio_ondiskgif_t *self) {
return self->min_delay;
}
int32_t common_hal_displayio_ondiskgif_get_max_delay(displayio_ondiskgif_t *self) {
return self->max_delay;
}
uint8_t common_hal_displayio_ondiskgif_play_frame(displayio_ondiskgif_t *self) { uint8_t common_hal_displayio_ondiskgif_play_frame(displayio_ondiskgif_t *self) {
int result = GIF_playFrame(&self->gif, 0, self->bitmap); int result = GIF_playFrame(&self->gif, 0, self->bitmap);

View File

@ -42,13 +42,11 @@ typedef struct {
mp_obj_base_t base; mp_obj_base_t base;
GIFIMAGE gif; GIFIMAGE gif;
pyb_file_obj_t *file; pyb_file_obj_t *file;
uint32_t *frame;
displayio_bitmap_t *bitmap; displayio_bitmap_t *bitmap;
union { int32_t duration;
mp_obj_base_t *pixel_shader_base; int32_t frame_count;
struct displayio_palette *palette; int32_t min_delay;
struct displayio_colorconverter *colorconverter; int32_t max_delay;
};
} displayio_ondiskgif_t; } displayio_ondiskgif_t;
#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_ONDISKGIF_H #endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_ONDISKGIF_H