Skip to content

Commit dbe5186

Browse files
committed
Memory savings and more properties
1 parent 2b26242 commit dbe5186

File tree

4 files changed

+114
-79
lines changed

4 files changed

+114
-79
lines changed

shared-bindings/displayio/OnDiskGif.c

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -123,25 +123,6 @@ MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_get_height_obj, displayio_ondiskgi
123123
MP_PROPERTY_GETTER(displayio_ondiskgif_height_obj,
124124
(mp_obj_t)&displayio_ondiskgif_get_height_obj);
125125

126-
//| pixel_shader: Union[ColorConverter, Palette]
127-
//| """The image's pixel_shader. The type depends on the underlying
128-
//| bitmap's structure. The pixel shader can be modified (e.g., to set the
129-
//| transparent pixel or, for palette shaded images, to update the palette.)"""
130-
//|
131-
STATIC mp_obj_t displayio_ondiskgif_obj_get_pixel_shader(mp_obj_t self_in) {
132-
displayio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in);
133-
return common_hal_displayio_ondiskgif_get_pixel_shader(self);
134-
}
135-
136-
MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_get_pixel_shader_obj, displayio_ondiskgif_obj_get_pixel_shader);
137-
138-
const mp_obj_property_t displayio_ondiskgif_pixel_shader_obj = {
139-
.base.type = &mp_type_property,
140-
.proxy = {(mp_obj_t)&displayio_ondiskgif_get_pixel_shader_obj,
141-
(mp_obj_t)MP_ROM_NONE,
142-
(mp_obj_t)MP_ROM_NONE},
143-
};
144-
145126
//| bitmap: Bitmap
146127
//| """The image's bitmap. The type depends on the underlying
147128
//| 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) {
154135

155136
MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_get_bitmap_obj, displayio_ondiskgif_obj_get_bitmap);
156137

138+
MP_PROPERTY_GETTER(displayio_ondiskgif_bitmap_obj,
139+
(mp_obj_t)&displayio_ondiskgif_get_bitmap_obj);
140+
141+
/*
157142
const mp_obj_property_t displayio_ondiskgif_bitmap_obj = {
158143
.base.type = &mp_type_property,
159144
.proxy = {(mp_obj_t)&displayio_ondiskgif_get_bitmap_obj,
160145
(mp_obj_t)MP_ROM_NONE,
161146
(mp_obj_t)MP_ROM_NONE},
162-
};
147+
};*/
163148

164149

165150
//| play_frame: None
@@ -173,13 +158,68 @@ STATIC mp_obj_t displayio_ondiskgif_obj_play_frame(mp_obj_t self_in) {
173158

174159
MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_play_frame_obj, displayio_ondiskgif_obj_play_frame);
175160

161+
//| duration: int
162+
//| """Height of the bitmap. (read only)"""
163+
STATIC mp_obj_t displayio_ondiskgif_obj_get_duration(mp_obj_t self_in) {
164+
displayio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in);
165+
166+
return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_ondiskgif_get_duration(self));
167+
}
168+
169+
MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_get_duration_obj, displayio_ondiskgif_obj_get_duration);
170+
171+
MP_PROPERTY_GETTER(displayio_ondiskgif_duration_obj,
172+
(mp_obj_t)&displayio_ondiskgif_get_duration_obj);
173+
174+
//| frame_count: int
175+
//| """Height of the bitmap. (read only)"""
176+
STATIC mp_obj_t displayio_ondiskgif_obj_get_frame_count(mp_obj_t self_in) {
177+
displayio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in);
178+
179+
return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_ondiskgif_get_frame_count(self));
180+
}
181+
182+
MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_get_frame_count_obj, displayio_ondiskgif_obj_get_frame_count);
183+
184+
MP_PROPERTY_GETTER(displayio_ondiskgif_frame_count_obj,
185+
(mp_obj_t)&displayio_ondiskgif_get_frame_count_obj);
186+
187+
//| min_delay: int
188+
//| """Height of the bitmap. (read only)"""
189+
STATIC mp_obj_t displayio_ondiskgif_obj_get_min_delay(mp_obj_t self_in) {
190+
displayio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in);
191+
192+
return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_ondiskgif_get_min_delay(self));
193+
}
194+
195+
MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_get_min_delay_obj, displayio_ondiskgif_obj_get_min_delay);
196+
197+
MP_PROPERTY_GETTER(displayio_ondiskgif_min_delay_obj,
198+
(mp_obj_t)&displayio_ondiskgif_get_min_delay_obj);
199+
200+
//| max_delay: int
201+
//| """Height of the bitmap. (read only)"""
202+
//|
203+
STATIC mp_obj_t displayio_ondiskgif_obj_get_max_delay(mp_obj_t self_in) {
204+
displayio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in);
205+
206+
return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_ondiskgif_get_max_delay(self));
207+
}
208+
209+
MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_get_max_delay_obj, displayio_ondiskgif_obj_get_max_delay);
210+
211+
MP_PROPERTY_GETTER(displayio_ondiskgif_max_delay_obj,
212+
(mp_obj_t)&displayio_ondiskgif_get_max_delay_obj);
176213

177214
STATIC const mp_rom_map_elem_t displayio_ondiskgif_locals_dict_table[] = {
178215
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&displayio_ondiskgif_height_obj) },
179-
{ MP_ROM_QSTR(MP_QSTR_pixel_shader), MP_ROM_PTR(&displayio_ondiskgif_pixel_shader_obj) },
180216
{ MP_ROM_QSTR(MP_QSTR_bitmap), MP_ROM_PTR(&displayio_ondiskgif_bitmap_obj) },
181217
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&displayio_ondiskgif_width_obj) },
182218
{ MP_ROM_QSTR(MP_QSTR_play_frame), MP_ROM_PTR(&displayio_ondiskgif_play_frame_obj) },
219+
{ MP_ROM_QSTR(MP_QSTR_duration), MP_ROM_PTR(&displayio_ondiskgif_duration_obj) },
220+
{ MP_ROM_QSTR(MP_QSTR_frame_count), MP_ROM_PTR(&displayio_ondiskgif_frame_count_obj) },
221+
{ MP_ROM_QSTR(MP_QSTR_min_delay), MP_ROM_PTR(&displayio_ondiskgif_min_delay_obj) },
222+
{ MP_ROM_QSTR(MP_QSTR_max_delay), MP_ROM_PTR(&displayio_ondiskgif_max_delay_obj) },
183223
};
184224
STATIC MP_DEFINE_CONST_DICT(displayio_ondiskgif_locals_dict, displayio_ondiskgif_locals_dict_table);
185225

shared-bindings/displayio/OnDiskGif.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,8 @@ mp_obj_t common_hal_displayio_ondiskgif_get_pixel_shader(displayio_ondiskgif_t *
4242
mp_obj_t common_hal_displayio_ondiskgif_get_bitmap(displayio_ondiskgif_t *self);
4343
uint16_t common_hal_displayio_ondiskgif_get_width(displayio_ondiskgif_t *self);
4444
uint8_t common_hal_displayio_ondiskgif_play_frame(displayio_ondiskgif_t *self);
45+
int32_t common_hal_displayio_ondiskgif_get_duration(displayio_ondiskgif_t *self);
46+
int32_t common_hal_displayio_ondiskgif_get_frame_count(displayio_ondiskgif_t *self);
47+
int32_t common_hal_displayio_ondiskgif_get_min_delay(displayio_ondiskgif_t *self);
48+
int32_t common_hal_displayio_ondiskgif_get_max_delay(displayio_ondiskgif_t *self);
4549
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_ONDISKGIF_H

shared-module/displayio/OnDiskGif.c

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -76,41 +76,33 @@ static void GIFDraw(GIFDRAW *pDraw) {
7676

7777
displayio_bitmap_t *bitmap = (displayio_bitmap_t *)pDraw->pUser;
7878

79-
// 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",
80-
// pDraw->y, pDraw->iX, pDraw->iY, pDraw->iWidth, pDraw->iHeight, pDraw->ucTransparent, pDraw->ucHasTransparency, pDraw->ucBackground, pDraw->pUser, bitmap->data);
79+
uint8_t *s;
80+
uint16_t *d;
8181

82-
/*
83-
int iX, iY; // Corner offset of this frame on the canvas
84-
int y; // current line being drawn (0 = top line of image)
85-
int iWidth, iHeight; // size of this frame
86-
void *pUser; // user supplied pointer
87-
uint8_t *pPixels; // 8-bit source pixels for this line
88-
uint16_t *pPalette; // little or big-endian RGB565 palette entries (default)
89-
uint8_t *pPalette24; // RGB888 palette (optional)
90-
uint8_t ucTransparent; // transparent color
91-
uint8_t ucHasTransparency; // flag indicating the transparent color is in use
92-
uint8_t ucDisposalMethod; // frame disposal method
93-
uint8_t ucBackground; // background color
94-
uint8_t ucIsGlobalPalette; // Flag to indicate that a global palette, rather than a local palette is being used
95-
*/
96-
97-
// For all other lines, just push the pixels to the display
98-
// (uint8_t *)pDraw->pPixels, pDraw->iWidth*2);
82+
int32_t row_start = pDraw->y * bitmap->stride;
83+
uint32_t *row = bitmap->data + row_start;
84+
s = pDraw->pPixels;
85+
d = (uint16_t *)row;
9986

87+
uint16_t *pPal;
88+
pPal = (uint16_t *)pDraw->pPalette;
10089

90+
if (pDraw->ucDisposalMethod == 2) { // restore to background color
91+
memset(d, pDraw->ucBackground, pDraw->iWidth);
92+
}
10193

102-
int32_t row_start = pDraw->y * bitmap->stride;
103-
uint32_t *row = bitmap->data + row_start;
104-
uint8_t *d = (uint8_t *)row;
105-
// mp_printf(&mp_plat_print, "rs %d strd %d:\n", row_start, bitmap->stride);
106-
107-
uint8_t *p = (uint8_t *)pDraw->pPixels;
108-
for (int w = 0; w < pDraw->iWidth * 2; w++) {
109-
// mp_printf(&mp_plat_print, "%x:", *p);
110-
*d++ = *p;
111-
p++;
94+
// We always check for transpancy even if the gif does not have it
95+
// as we also convert the color to the palette here
96+
// Could separate it but would not sure it would be much a speed up
97+
uint8_t c, ucTransparent = pDraw->ucTransparent;
98+
for (int x = 0; x < pDraw->iWidth; x++)
99+
{
100+
c = *s++;
101+
if (c != ucTransparent) {
102+
*d = pPal[c];
103+
}
104+
d++;
112105
}
113-
// mp_printf(&mp_plat_print, "\n");
114106
}
115107

116108
void common_hal_displayio_ondiskgif_construct(displayio_ondiskgif_t *self, pyb_file_obj_t *file) {
@@ -135,48 +127,49 @@ void common_hal_displayio_ondiskgif_construct(displayio_ondiskgif_t *self, pyb_f
135127
mp_arg_error_invalid(MP_QSTR_file);
136128
}
137129

138-
self->frame = m_malloc(self->gif.iCanvasWidth * self->gif.iCanvasHeight * sizeof(uint16_t), false); // MUST FREE LATER?
139-
self->gif.pFrameBuffer = (uint8_t *)self->frame;
140-
self->gif.ucDrawType = GIF_DRAW_COOKED;
141-
142130
displayio_bitmap_t *bitmap = m_new_obj(displayio_bitmap_t);
143131
bitmap->base.type = &displayio_bitmap_type;
144132
common_hal_displayio_bitmap_construct(bitmap, self->gif.iCanvasWidth, self->gif.iCanvasHeight, 16);
145133
self->bitmap = bitmap;
146134

147-
// mp_printf(&mp_plat_print, "GIF_init returned %d %x\n", result, bitmap->data);
148-
}
149-
150-
151-
uint32_t common_hal_displayio_ondiskgif_get_pixel(displayio_ondiskgif_t *self,
152-
int16_t x, int16_t y) {
153-
if (x < 0 || x >= self->gif.iCanvasWidth || y < 0 || y >= self->gif.iCanvasHeight) {
154-
return 0;
155-
}
135+
GIFINFO info;
136+
GIF_getInfo(&self->gif, &info);
137+
self->duration = info.iDuration;
138+
self->frame_count = info.iFrameCount;
139+
self->min_delay = info.iMinDelay;
140+
self->max_delay = info.iMaxDelay;
156141

157-
return 0;
142+
// mp_printf(&mp_plat_print, "GIF_init returned %d %x\n", result, bitmap->data);
158143
}
159144

160145
uint16_t common_hal_displayio_ondiskgif_get_height(displayio_ondiskgif_t *self) {
161-
GIFINFO info;
162-
GIF_getInfo(&self->gif, &info);
163-
mp_printf(&mp_plat_print, "dur %d fc %d max %d min %d\n", info.iDuration, info.iFrameCount, info.iMaxDelay, info.iMinDelay);
164-
165146
return (uint16_t)self->gif.iCanvasHeight;
166147
}
167148

168149
uint16_t common_hal_displayio_ondiskgif_get_width(displayio_ondiskgif_t *self) {
169150
return (uint16_t)self->gif.iCanvasWidth;
170151
}
171152

172-
mp_obj_t common_hal_displayio_ondiskgif_get_pixel_shader(displayio_ondiskgif_t *self) {
173-
return MP_OBJ_FROM_PTR(self->pixel_shader_base);
174-
}
175-
176153
mp_obj_t common_hal_displayio_ondiskgif_get_bitmap(displayio_ondiskgif_t *self) {
177154
return MP_OBJ_FROM_PTR(self->bitmap);
178155
}
179156

157+
int32_t common_hal_displayio_ondiskgif_get_duration(displayio_ondiskgif_t *self) {
158+
return self->duration;
159+
}
160+
161+
int32_t common_hal_displayio_ondiskgif_get_frame_count(displayio_ondiskgif_t *self) {
162+
return self->frame_count;
163+
}
164+
165+
int32_t common_hal_displayio_ondiskgif_get_min_delay(displayio_ondiskgif_t *self) {
166+
return self->min_delay;
167+
}
168+
169+
int32_t common_hal_displayio_ondiskgif_get_max_delay(displayio_ondiskgif_t *self) {
170+
return self->max_delay;
171+
}
172+
180173
uint8_t common_hal_displayio_ondiskgif_play_frame(displayio_ondiskgif_t *self) {
181174
int result = GIF_playFrame(&self->gif, 0, self->bitmap);
182175

shared-module/displayio/OnDiskGif.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,11 @@ typedef struct {
4242
mp_obj_base_t base;
4343
GIFIMAGE gif;
4444
pyb_file_obj_t *file;
45-
uint32_t *frame;
4645
displayio_bitmap_t *bitmap;
47-
union {
48-
mp_obj_base_t *pixel_shader_base;
49-
struct displayio_palette *palette;
50-
struct displayio_colorconverter *colorconverter;
51-
};
46+
int32_t duration;
47+
int32_t frame_count;
48+
int32_t min_delay;
49+
int32_t max_delay;
5250
} displayio_ondiskgif_t;
5351

5452
#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_ONDISKGIF_H

0 commit comments

Comments
 (0)