Skip to content

Commit b5c42ef

Browse files
committed
Reduce stack usage of LCD_DrawWindowedImageFromFile
1 parent c835a4b commit b5c42ef

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

src/screen/320x240x16/lcd_gfx.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,11 +496,12 @@ void LCD_DrawWindowedImageFromFile(u16 x, u16 y, const char *file, s16 w, s16 h,
496496
{
497497
int i, j;
498498
FILE *fh;
499+
#define FILEBUF_SIZE 128
499500
unsigned transparent = 0;
500501
unsigned row_has_transparency = 0;
501502
(void)row_has_transparency;
502503

503-
u8 buf[480 * 2];
504+
u8 buf[FILEBUF_SIZE * 2];
504505

505506
if (w == 0 || h == 0)
506507
return;
@@ -568,13 +569,16 @@ void LCD_DrawWindowedImageFromFile(u16 x, u16 y, const char *file, s16 w, s16 h,
568569
LCD_DrawStart(x, y, x + w - 1, y + h - 1, DRAW_SWNE);
569570
/* Bitmap start is at lower-left corner */
570571
for (j = 0; j < h; j++) {
571-
if (fread(buf, 2 * w, 1, fh) != 1)
572-
break;
573-
u16 *color = (u16 *)buf;
572+
u16 *color = NULL;
574573
if(transparent) {
575574
#ifdef TRANSPARENT_COLOR
576575
//Display supports a transparent color
577576
for (i = 0; i < w; i++ ) {
577+
if (i % FILEBUF_SIZE == 0) {
578+
fread(buf, w - i > FILEBUF_SIZE? FILEBUF_SIZE: w - i, 2, fh);
579+
color = (u16 *)buf;
580+
}
581+
578582
u32 c;
579583
if((*color & 0x8000)) {
580584
//convert 1555 -> 565
@@ -589,6 +593,11 @@ void LCD_DrawWindowedImageFromFile(u16 x, u16 y, const char *file, s16 w, s16 h,
589593
unsigned last_pixel_transparent = row_has_transparency;
590594
row_has_transparency = 0;
591595
for (i = 0; i < w; i++ ) {
596+
if (i % FILEBUF_SIZE == 0) {
597+
fread(buf, w - i > FILEBUF_SIZE? FILEBUF_SIZE: w - i, 2, fh);
598+
color = (u16 *)buf;
599+
}
600+
592601
if((*color & 0x8000)) {
593602
//convert 1555 -> 565
594603
unsigned c = ((*color & 0x7fe0) << 1) | (*color & 0x1f);
@@ -609,6 +618,11 @@ void LCD_DrawWindowedImageFromFile(u16 x, u16 y, const char *file, s16 w, s16 h,
609618
#endif
610619
} else {
611620
for (i = 0; i < w; i++ ) {
621+
if (i % FILEBUF_SIZE == 0) {
622+
fread(buf, w - i > FILEBUF_SIZE? FILEBUF_SIZE: w - i, 2, fh);
623+
color = (u16*)buf;
624+
}
625+
612626
if (LCD_DEPTH == 1)
613627
*color = (*color & 0x8410) == 0x8410 ? 0 : 0xffff;
614628
LCD_DrawPixel(*color++);

0 commit comments

Comments
 (0)