From b5c42efee50fc754749ecd3b45460e1e665d881f Mon Sep 17 00:00:00 2001 From: Howard Su Date: Mon, 14 Jan 2019 15:02:33 +0800 Subject: [PATCH 1/3] Reduce stack usage of LCD_DrawWindowedImageFromFile --- src/screen/320x240x16/lcd_gfx.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/screen/320x240x16/lcd_gfx.c b/src/screen/320x240x16/lcd_gfx.c index 584057f607..d59a0fb444 100644 --- a/src/screen/320x240x16/lcd_gfx.c +++ b/src/screen/320x240x16/lcd_gfx.c @@ -496,11 +496,12 @@ void LCD_DrawWindowedImageFromFile(u16 x, u16 y, const char *file, s16 w, s16 h, { int i, j; FILE *fh; +#define FILEBUF_SIZE 128 unsigned transparent = 0; unsigned row_has_transparency = 0; (void)row_has_transparency; - u8 buf[480 * 2]; + u8 buf[FILEBUF_SIZE * 2]; if (w == 0 || h == 0) return; @@ -568,13 +569,16 @@ void LCD_DrawWindowedImageFromFile(u16 x, u16 y, const char *file, s16 w, s16 h, LCD_DrawStart(x, y, x + w - 1, y + h - 1, DRAW_SWNE); /* Bitmap start is at lower-left corner */ for (j = 0; j < h; j++) { - if (fread(buf, 2 * w, 1, fh) != 1) - break; - u16 *color = (u16 *)buf; + u16 *color = NULL; if(transparent) { #ifdef TRANSPARENT_COLOR //Display supports a transparent color for (i = 0; i < w; i++ ) { + if (i % FILEBUF_SIZE == 0) { + fread(buf, w - i > FILEBUF_SIZE? FILEBUF_SIZE: w - i, 2, fh); + color = (u16 *)buf; + } + u32 c; if((*color & 0x8000)) { //convert 1555 -> 565 @@ -589,6 +593,11 @@ void LCD_DrawWindowedImageFromFile(u16 x, u16 y, const char *file, s16 w, s16 h, unsigned last_pixel_transparent = row_has_transparency; row_has_transparency = 0; for (i = 0; i < w; i++ ) { + if (i % FILEBUF_SIZE == 0) { + fread(buf, w - i > FILEBUF_SIZE? FILEBUF_SIZE: w - i, 2, fh); + color = (u16 *)buf; + } + if((*color & 0x8000)) { //convert 1555 -> 565 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, #endif } else { for (i = 0; i < w; i++ ) { + if (i % FILEBUF_SIZE == 0) { + fread(buf, w - i > FILEBUF_SIZE? FILEBUF_SIZE: w - i, 2, fh); + color = (u16*)buf; + } + if (LCD_DEPTH == 1) *color = (*color & 0x8410) == 0x8410 ? 0 : 0xffff; LCD_DrawPixel(*color++); From bee7da6744013b719101a85585dbeb4793503d0b Mon Sep 17 00:00:00 2001 From: Howard Su Date: Mon, 14 Jan 2019 23:15:19 +0800 Subject: [PATCH 2/3] Share the loop to reduce ROM size --- src/screen/320x240x16/lcd_gfx.c | 40 ++++++++++++--------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/src/screen/320x240x16/lcd_gfx.c b/src/screen/320x240x16/lcd_gfx.c index d59a0fb444..e85cca19fd 100644 --- a/src/screen/320x240x16/lcd_gfx.c +++ b/src/screen/320x240x16/lcd_gfx.c @@ -570,15 +570,19 @@ void LCD_DrawWindowedImageFromFile(u16 x, u16 y, const char *file, s16 w, s16 h, /* Bitmap start is at lower-left corner */ for (j = 0; j < h; j++) { u16 *color = NULL; - if(transparent) { -#ifdef TRANSPARENT_COLOR - //Display supports a transparent color - for (i = 0; i < w; i++ ) { - if (i % FILEBUF_SIZE == 0) { - fread(buf, w - i > FILEBUF_SIZE? FILEBUF_SIZE: w - i, 2, fh); - color = (u16 *)buf; - } +#ifndef TRANSPARENT_COLOR + unsigned last_pixel_transparent = row_has_transparency; + row_has_transparency = 0; +#endif + for (i = 0; i < w; i++ ) { + if (i % FILEBUF_SIZE == 0) { + fread(buf, w - i > FILEBUF_SIZE? FILEBUF_SIZE: w - i, 2, fh); + color = (u16 *)buf; + } + if(transparent) { +#ifdef TRANSPARENT_COLOR + //Display supports a transparent color u32 c; if((*color & 0x8000)) { //convert 1555 -> 565 @@ -588,16 +592,7 @@ void LCD_DrawWindowedImageFromFile(u16 x, u16 y, const char *file, s16 w, s16 h, } LCD_DrawPixel(c); color++; - } #else - unsigned last_pixel_transparent = row_has_transparency; - row_has_transparency = 0; - for (i = 0; i < w; i++ ) { - if (i % FILEBUF_SIZE == 0) { - fread(buf, w - i > FILEBUF_SIZE? FILEBUF_SIZE: w - i, 2, fh); - color = (u16 *)buf; - } - if((*color & 0x8000)) { //convert 1555 -> 565 unsigned c = ((*color & 0x7fe0) << 1) | (*color & 0x1f); @@ -614,20 +609,13 @@ void LCD_DrawWindowedImageFromFile(u16 x, u16 y, const char *file, s16 w, s16 h, last_pixel_transparent = 1; } color++; - } #endif - } else { - for (i = 0; i < w; i++ ) { - if (i % FILEBUF_SIZE == 0) { - fread(buf, w - i > FILEBUF_SIZE? FILEBUF_SIZE: w - i, 2, fh); - color = (u16*)buf; - } - + } else { if (LCD_DEPTH == 1) *color = (*color & 0x8410) == 0x8410 ? 0 : 0xffff; LCD_DrawPixel(*color++); } - } + } if((u16)w < img_w) { fseek(fh, 2 * (img_w - w), SEEK_CUR); } From dcb519749207dd259502e4683bbc243fa4bded81 Mon Sep 17 00:00:00 2001 From: Howard Su Date: Tue, 15 Jan 2019 07:25:49 +0800 Subject: [PATCH 3/3] Fix style --- src/screen/320x240x16/lcd_gfx.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/screen/320x240x16/lcd_gfx.c b/src/screen/320x240x16/lcd_gfx.c index e85cca19fd..ca0670f30b 100644 --- a/src/screen/320x240x16/lcd_gfx.c +++ b/src/screen/320x240x16/lcd_gfx.c @@ -574,15 +574,15 @@ void LCD_DrawWindowedImageFromFile(u16 x, u16 y, const char *file, s16 w, s16 h, unsigned last_pixel_transparent = row_has_transparency; row_has_transparency = 0; #endif - for (i = 0; i < w; i++ ) { + for (i = 0; i < w; i++) { if (i % FILEBUF_SIZE == 0) { fread(buf, w - i > FILEBUF_SIZE? FILEBUF_SIZE: w - i, 2, fh); color = (u16 *)buf; } - if(transparent) { + if (transparent) { #ifdef TRANSPARENT_COLOR - //Display supports a transparent color + // Display supports a transparent color u32 c; if((*color & 0x8000)) { //convert 1555 -> 565 @@ -615,7 +615,7 @@ void LCD_DrawWindowedImageFromFile(u16 x, u16 y, const char *file, s16 w, s16 h, *color = (*color & 0x8410) == 0x8410 ? 0 : 0xffff; LCD_DrawPixel(*color++); } - } + } if((u16)w < img_w) { fseek(fh, 2 * (img_w - w), SEEK_CUR); }