From f2225e21c95be46f41f05227e410f12335061444 Mon Sep 17 00:00:00 2001 From: tGecko Date: Sat, 10 Feb 2024 17:51:14 +0100 Subject: [PATCH 1/5] Fix infoPanel crash when surface formats aren't identical only stretch images if they're too big, it can look horrible when stretching smaller ones some extra error handling in Gallery/infoPanel --- src/infoPanel/imagesCache.c | 24 +++++++++++++++---- .../App/Screenshots_Viewer/launch.sh | 9 ++++--- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/infoPanel/imagesCache.c b/src/infoPanel/imagesCache.c index 813142882d..90abfbcf1d 100644 --- a/src/infoPanel/imagesCache.c +++ b/src/infoPanel/imagesCache.c @@ -21,12 +21,26 @@ static void drawImage(SDL_Surface *image_to_draw, SDL_Surface *screen, if (!image_to_draw) return; - if (image_to_draw->w != 640 || image_to_draw->h != 480) { - // scale image to 640x480 if needed + if (image_to_draw->w > 640 || image_to_draw->h > 480) { + // scale image to 640x480 only if bigger (v4 752x560) + SDL_Rect dest_rect = {0, 0, 640, 480}; - SDL_SoftStretch(image_to_draw, NULL, image_to_draw, &dest_rect); - image_to_draw->w = 640; - image_to_draw->h = 480; + SDL_Surface *scaled_image = SDL_CreateRGBSurface( + SDL_SWSURFACE, 640, 480, image_to_draw->format->BitsPerPixel, + image_to_draw->format->Rmask, image_to_draw->format->Gmask, + image_to_draw->format->Bmask, image_to_draw->format->Amask); + + if (!scaled_image) { + printf("SDL_CreateRGBSurface failed: %s\n", SDL_GetError()); + } + else { + int ret = SDL_SoftStretch(image_to_draw, NULL, scaled_image, &dest_rect); + if (ret == 0) + image_to_draw = scaled_image; + else + // failed to scale, draw unscaled + printf("SDL_SoftStretch failed: %s\n", SDL_GetError()); + } } DEBUG_PRINT(("frame %p\n", frame)); diff --git a/static/packages/App/Gallery (Screenshot viewer)/App/Screenshots_Viewer/launch.sh b/static/packages/App/Gallery (Screenshot viewer)/App/Screenshots_Viewer/launch.sh index d0b52e7378..7e07a821a7 100755 --- a/static/packages/App/Gallery (Screenshot viewer)/App/Screenshots_Viewer/launch.sh +++ b/static/packages/App/Gallery (Screenshot viewer)/App/Screenshots_Viewer/launch.sh @@ -5,8 +5,11 @@ echo ":: Gallery - Screenshots Viewer ::" ec=$? # cancel or success from infoPanel -if [ $ec -eq 255 ] || [ $ec -eq 0 ] ; then +if [ $ec -eq 255 ] || [ $ec -eq 0 ]; then exit 0 +elif [ $ec -eq 1 ]; then + /mnt/SDCARD/.tmp_update/bin/infoPanel -t Gallery -m "No screenshots found" +else + # something went wrong + /mnt/SDCARD/.tmp_update/bin/infoPanel -t Error -m "Error: $ec" fi - -/mnt/SDCARD/.tmp_update/bin/infoPanel -t Gallery -m "No screenshots found" From 6f8176d57f47f9abe0f89f9b8ba433c1071329ec Mon Sep 17 00:00:00 2001 From: Aemiii91 <44569252+Aemiii91@users.noreply.github.com> Date: Sat, 24 Feb 2024 15:06:20 +0100 Subject: [PATCH 2/5] Improved scaling --- src/infoPanel/imagesCache.c | 68 ++++++++----------- src/infoPanel/imagesCache.h | 1 + src/infoPanel/infoPanel.c | 49 +++++++------ .../App/Screenshots_Viewer/launch.sh | 6 +- .../Quick Guide/App/Onion_Manual/launch.sh | 2 +- 5 files changed, 64 insertions(+), 62 deletions(-) diff --git a/src/infoPanel/imagesCache.c b/src/infoPanel/imagesCache.c index 90abfbcf1d..d1fa093918 100644 --- a/src/infoPanel/imagesCache.c +++ b/src/infoPanel/imagesCache.c @@ -2,6 +2,7 @@ #include #include +#include static SDL_Surface *g_image_cache_prev = NULL; static SDL_Surface *g_image_cache_current = NULL; @@ -15,56 +16,45 @@ static SDL_Surface *g_image_cache_next = NULL; } while (0) #endif -static void drawImage(SDL_Surface *image_to_draw, SDL_Surface *screen, - const SDL_Rect *frame) +#define MIN(a, b) (a < b) ? (a) : (b) + +void drawImage(SDL_Surface *image, SDL_Surface *screen, + const SDL_Rect *frame) { - if (!image_to_draw) + if (!image) return; - if (image_to_draw->w > 640 || image_to_draw->h > 480) { - // scale image to 640x480 only if bigger (v4 752x560) + SDL_Surface *scaled_image = NULL; + SDL_Rect target = {0, 0, screen->w, screen->h}; - SDL_Rect dest_rect = {0, 0, 640, 480}; - SDL_Surface *scaled_image = SDL_CreateRGBSurface( - SDL_SWSURFACE, 640, 480, image_to_draw->format->BitsPerPixel, - image_to_draw->format->Rmask, image_to_draw->format->Gmask, - image_to_draw->format->Bmask, image_to_draw->format->Amask); + if (frame != NULL) { + target = *frame; + } - if (!scaled_image) { - printf("SDL_CreateRGBSurface failed: %s\n", SDL_GetError()); + // scale image to 640x480 only if bigger (v4 752x560) + if (image->w > target.w || image->h > target.h) { + double ratio_x = (double)target.w / image->w; + double ratio_y = (double)target.h / image->h; + double scale = MIN(ratio_x, ratio_y); + scaled_image = rotozoomSurface(image, 0.0, scale, true); + + if (scaled_image == NULL) { + printf("rotozoomSurface failed: %s\n", SDL_GetError()); } else { - int ret = SDL_SoftStretch(image_to_draw, NULL, scaled_image, &dest_rect); - if (ret == 0) - image_to_draw = scaled_image; - else - // failed to scale, draw unscaled - printf("SDL_SoftStretch failed: %s\n", SDL_GetError()); + image = scaled_image; } } - DEBUG_PRINT(("frame %p\n", frame)); - int border_left = 0; - SDL_Rect new_frame = {0, 0}; - if (frame != NULL) { - DEBUG_PRINT(("x-%d y-%d\n", frame->x, frame->y)); - new_frame = *frame; - border_left = new_frame.x; - } - DEBUG_PRINT(("border_left %d\n", border_left)); - int16_t image_x = 320 - (int16_t)(image_to_draw->w / 2); - if (image_x < border_left) { - image_x = border_left; - } - else { - new_frame.x -= border_left; + SDL_Rect image_pos = { + target.x + (target.w - image->w) / 2, + target.y + (target.h - image->h) / 2}; + + SDL_BlitSurface(image, NULL, screen, &image_pos); + + if (scaled_image != NULL) { + SDL_FreeSurface(scaled_image); } - SDL_Rect image_rect = {image_x, (int16_t)(240 - image_to_draw->h / 2)}; - DEBUG_PRINT(("image_rect x-%d y-%d\n", image_rect.x, image_rect.y)); - if (frame != NULL) - SDL_BlitSurface(image_to_draw, &new_frame, screen, &image_rect); - else - SDL_BlitSurface(image_to_draw, NULL, screen, &image_rect); } char *drawImageByIndex(const int new_image_index, const int image_index, diff --git a/src/infoPanel/imagesCache.h b/src/infoPanel/imagesCache.h index c1f280919d..d22933fc95 100644 --- a/src/infoPanel/imagesCache.h +++ b/src/infoPanel/imagesCache.h @@ -7,6 +7,7 @@ extern "C" { #include +void drawImage(SDL_Surface *image_to_draw, SDL_Surface *screen, const SDL_Rect *frame); char *drawImageByIndex(const int index, const int image_index, char **images_paths, const int images_paths_count, SDL_Surface *screen, const SDL_Rect *frame, diff --git a/src/infoPanel/infoPanel.c b/src/infoPanel/infoPanel.c index ff967ab53e..0596f48165 100644 --- a/src/infoPanel/infoPanel.c +++ b/src/infoPanel/infoPanel.c @@ -102,16 +102,6 @@ static void drawInfoPanel(SDL_Surface *screen, SDL_Surface *video, } } -static void drawImage(const char *image_path, SDL_Surface *screen) -{ - SDL_Surface *image = IMG_Load(image_path); - if (image) { - SDL_Rect image_rect = {320 - image->w / 2, 240 - image->h / 2}; - SDL_BlitSurface(image, NULL, screen, &image_rect); - SDL_FreeSurface(image); - } -} - static void sdlQuit(SDL_Surface *screen, SDL_Surface *video) { SDL_FreeSurface(screen); @@ -127,6 +117,16 @@ const SDL_Rect *getControlsAwareFrame(const SDL_Rect *frame) return NULL; } +void drawBackground(void) +{ + if (g_show_theme_controls) { + SDL_BlitSurface(theme_background(), NULL, screen, NULL); + } + else { + SDL_FillRect(screen, NULL, 0); + } +} + int main(int argc, char *argv[]) { char title_str[STR_MAX] = ""; @@ -180,20 +180,27 @@ int main(int argc, char *argv[]) bool cache_used = false; - const SDL_Rect themedFrame = {theme()->frame.border_left, 0, 640 - theme()->frame.border_right, 480}; + const SDL_Rect themedFrame = { + theme()->frame.border_left, 60, + 640 - theme()->frame.border_left - theme()->frame.border_right, 360}; + + SDL_Surface *static_image = NULL; if (exists(image_path)) { g_images_paths_count = 1; g_image_index = 0; - SDL_BlitSurface(theme_background(), NULL, screen, NULL); - drawImage(image_path, screen); + + drawBackground(); + + static_image = IMG_Load(image_path); + drawImage(static_image, screen, NULL); } else if (exists(images_json_path)) { if (loadImagesPathsFromJson(images_json_path, &g_images_paths, &g_images_paths_count, &g_images_titles) && g_images_paths_count > 0) { g_image_index = 0; - SDL_BlitSurface(theme_background(), NULL, screen, NULL); + drawBackground(); drawImageByIndex(0, g_image_index, g_images_paths, g_images_paths_count, screen, getControlsAwareFrame(&themedFrame), &cache_used); @@ -208,7 +215,7 @@ int main(int argc, char *argv[]) &g_images_paths_count) && g_images_paths_count > 0) { g_image_index = 0; - SDL_BlitSurface(theme_background(), NULL, screen, NULL); + drawBackground(); drawImageByIndex(0, g_image_index, g_images_paths, g_images_paths_count, screen, getControlsAwareFrame(&themedFrame), &cache_used); @@ -267,7 +274,7 @@ int main(int argc, char *argv[]) break; case SW_BTN_Y: g_show_theme_controls = !g_show_theme_controls; - SDL_BlitSurface(theme_background(), NULL, screen, NULL); + drawBackground(); if (g_images_paths) { drawImageByIndex( @@ -275,8 +282,8 @@ int main(int argc, char *argv[]) g_images_paths_count, screen, getControlsAwareFrame(&themedFrame), &cache_used); } - else if (exists(image_path)) { - drawImage(image_path, screen); + else if (static_image != NULL) { + drawImage(static_image, screen, NULL); } else { g_show_theme_controls = true; @@ -312,7 +319,7 @@ int main(int argc, char *argv[]) const int current_index = g_image_index; navigating_forward ? g_image_index++ : g_image_index--; - SDL_BlitSurface(theme_background(), NULL, screen, NULL); + drawBackground(); drawImageByIndex(g_image_index, current_index, g_images_paths, g_images_paths_count, screen, getControlsAwareFrame(&themedFrame), @@ -436,6 +443,10 @@ int main(int argc, char *argv[]) cleanImagesCache(); + if (static_image != NULL) { + SDL_FreeSurface(static_image); + } + lang_free(); resources_free(); SDL_FreeSurface(screen); diff --git a/static/packages/App/Gallery (Screenshot viewer)/App/Screenshots_Viewer/launch.sh b/static/packages/App/Gallery (Screenshot viewer)/App/Screenshots_Viewer/launch.sh index 7e07a821a7..6ab0e88d90 100755 --- a/static/packages/App/Gallery (Screenshot viewer)/App/Screenshots_Viewer/launch.sh +++ b/static/packages/App/Gallery (Screenshot viewer)/App/Screenshots_Viewer/launch.sh @@ -1,15 +1,15 @@ #!/bin/sh echo ":: Gallery - Screenshots Viewer ::" -/mnt/SDCARD/.tmp_update/bin/infoPanel -d /mnt/SDCARD/Screenshots --show-theme-controls +infoPanel -d /mnt/SDCARD/Screenshots --show-theme-controls ec=$? # cancel or success from infoPanel if [ $ec -eq 255 ] || [ $ec -eq 0 ]; then exit 0 elif [ $ec -eq 1 ]; then - /mnt/SDCARD/.tmp_update/bin/infoPanel -t Gallery -m "No screenshots found" + infoPanel -t Gallery -m "No screenshots found" else # something went wrong - /mnt/SDCARD/.tmp_update/bin/infoPanel -t Error -m "Error: $ec" + infoPanel -t Gallery -m "An error occurred - code: $ec" fi diff --git a/static/packages/App/Quick Guide/App/Onion_Manual/launch.sh b/static/packages/App/Quick Guide/App/Onion_Manual/launch.sh index cea6a03126..88376a5042 100644 --- a/static/packages/App/Quick Guide/App/Onion_Manual/launch.sh +++ b/static/packages/App/Quick Guide/App/Onion_Manual/launch.sh @@ -1,2 +1,2 @@ #!/bin/sh -infoPanel --images-json /mnt/SDCARD/App/Onion_Manual/images.json --show-theme-controls +infoPanel --images-json /mnt/SDCARD/App/Onion_Manual/images.json From 43d3af87321b83d6c9f3205cd10c261e2f6ebc10 Mon Sep 17 00:00:00 2001 From: Aemiii91 <44569252+Aemiii91@users.noreply.github.com> Date: Sat, 24 Feb 2024 15:15:59 +0100 Subject: [PATCH 3/5] Fix test --- test/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Makefile b/test/Makefile index 669827b5ce..62efafac8e 100644 --- a/test/Makefile +++ b/test/Makefile @@ -4,7 +4,7 @@ CFILES := ../src/infoPanel/imagesCache.c include ../src/common/config.mk TARGET = test -LDFLAGS := $(LDFLAGS) -L../lib -s -lSDL_image -lSDL -lgtest -lgtest_main -lpthread +LDFLAGS := $(LDFLAGS) -L../lib -s -lSDL_image -lSDL -lSDL_rotozoom -lgtest -lgtest_main -lpthread include ../src/common/commands.mk include ../src/common/recipes.mk \ No newline at end of file From c6332965764703f02fcc8e9cc51ddcf467e81c0f Mon Sep 17 00:00:00 2001 From: Aemiii91 <44569252+Aemiii91@users.noreply.github.com> Date: Sat, 24 Feb 2024 15:27:11 +0100 Subject: [PATCH 4/5] Fix test 2 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 6630842f52..d1a8e4874d 100644 --- a/Makefile +++ b/Makefile @@ -270,7 +270,7 @@ test: @cp -R $(TEST_SRC_DIR)/infoPanel_test_data $(BUILD_TEST_DIR)/ cd $(BUILD_TEST_DIR) && ./test -static-analysis: +static-analysis: external-libs @cd $(ROOT_DIR) && cppcheck -I $(INCLUDE_DIR) --enable=all $(SRC_DIR) format: From fb844c68fe15533eb8b22b6e01bd67a5163b9124 Mon Sep 17 00:00:00 2001 From: Aemiii91 <44569252+Aemiii91@users.noreply.github.com> Date: Sat, 24 Feb 2024 15:29:21 +0100 Subject: [PATCH 5/5] Fix test 3 --- Makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Makefile b/Makefile index d1a8e4874d..9bc1e7e44e 100644 --- a/Makefile +++ b/Makefile @@ -262,10 +262,9 @@ patch: @chmod a+x $(ROOT_DIR)/.github/create_patch.sh && $(ROOT_DIR)/.github/create_patch.sh external-libs: - @cd $(ROOT_DIR)/include/cJSON && make clean && make @cd $(ROOT_DIR)/include/SDL && make clean && make -test: +test: external-libs @mkdir -p $(BUILD_TEST_DIR)/infoPanel_test_data && cd $(TEST_SRC_DIR) && BUILD_DIR=$(BUILD_TEST_DIR)/ make dev @cp -R $(TEST_SRC_DIR)/infoPanel_test_data $(BUILD_TEST_DIR)/ cd $(BUILD_TEST_DIR) && ./test