From ca218770006f19f4082e573c66cfb65d2dbdd29b Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Mon, 13 Jun 2022 20:49:09 -0400 Subject: [PATCH] Ditch FreeImage --- dependencies/CMakeLists.txt | 10 +- dependencies/glfw/CMakeLists.txt | 20 ++-- dependencies/libpng/CMakeLists.txt | 32 ++++++- docs/ARCHITECTURE.md | 7 +- docs/BUILDING.md | 9 +- media-layer/core/CMakeLists.txt | 4 +- media-layer/core/src/screenshot.c | 91 ++++++++++++++----- scripts/install-dependencies.sh | 4 +- .../tools/generate-appimage-builder-yaml.js | 1 - 9 files changed, 114 insertions(+), 64 deletions(-) diff --git a/dependencies/CMakeLists.txt b/dependencies/CMakeLists.txt index d7fd6a5e..ad82d152 100644 --- a/dependencies/CMakeLists.txt +++ b/dependencies/CMakeLists.txt @@ -1,13 +1,9 @@ project(dependencies) -# ZLib -if(BUILD_ARM_COMPONENTS) - add_subdirectory(zlib) -endif() +# ZLib (Needed By libpng) +add_subdirectory(zlib) # LibPNG -if(BUILD_ARM_COMPONENTS) - add_subdirectory(libpng) -endif() +add_subdirectory(libpng) # Minecraft: Pi Edition if(BUILD_ARM_COMPONENTS AND NOT MCPI_OPEN_SOURCE_ONLY) add_subdirectory(minecraft-pi) diff --git a/dependencies/glfw/CMakeLists.txt b/dependencies/glfw/CMakeLists.txt index 6501429d..895e9fc8 100644 --- a/dependencies/glfw/CMakeLists.txt +++ b/dependencies/glfw/CMakeLists.txt @@ -6,16 +6,16 @@ add_compile_options(-w) ## GLFW # Download -set(BUILD_SHARED_LIBS FALSE) -set(GLFW_BUILD_EXAMPLES FALSE) -set(GLFW_BUILD_TESTS FALSE) -set(GLFW_BUILD_DOCS FALSE) -set(GLFW_INSTALL FALSE) -set(GLFW_BUILD_WIN32 FALSE) -set(GLFW_BUILD_COCOA FALSE) -set(GLFW_BUILD_X11 TRUE) -set(GLFW_BUILD_WAYLAND TRUE) -set(GLFW_LIBRARY_TYPE "STATIC") +set(BUILD_SHARED_LIBS FALSE CACHE BOOL "" FORCE) +set(GLFW_BUILD_EXAMPLES FALSE CACHE BOOL "" FORCE) +set(GLFW_BUILD_TESTS FALSE CACHE BOOL "" FORCE) +set(GLFW_BUILD_DOCS FALSE CACHE BOOL "" FORCE) +set(GLFW_INSTALL FALSE CACHE BOOL "" FORCE) +set(GLFW_BUILD_WIN32 FALSE CACHE BOOL "" FORCE) +set(GLFW_BUILD_COCOA FALSE CACHE BOOL "" FORCE) +set(GLFW_BUILD_X11 TRUE CACHE BOOL "" FORCE) +set(GLFW_BUILD_WAYLAND TRUE CACHE BOOL "" FORCE) +set(GLFW_LIBRARY_TYPE "STATIC" CACHE BOOL "" FORCE) add_subdirectory(src EXCLUDE_FROM_ALL) # Ensure Build diff --git a/dependencies/libpng/CMakeLists.txt b/dependencies/libpng/CMakeLists.txt index f2df11bf..6caea996 100644 --- a/dependencies/libpng/CMakeLists.txt +++ b/dependencies/libpng/CMakeLists.txt @@ -5,16 +5,40 @@ add_compile_options(-w) ## LibPNG +# Options +set(PNG_TESTS FALSE CACHE BOOL "" FORCE) +set(PNG_STATIC ${BUILD_NATIVE_COMPONENTS} CACHE BOOL "" FORCE) +set(PNG_SHARED ${BUILD_ARM_COMPONENTS} CACHE BOOL "" FORCE) +if(BUILD_NATIVE_COMPONENTS) + set(PNG_NO_STDIO FALSE CACHE BOOL "" FORCE) +else() + set(PNG_NO_STDIO TRUE CACHE BOOL "" FORCE) +endif() + # Download set(ZLIB_LIBRARY zlibstatic) set(ZLIB_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../zlib/src" "${CMAKE_CURRENT_BINARY_DIR}/../zlib/src") set(CMAKE_POLICY_DEFAULT_CMP0054 OLD) # Silence Warning add_subdirectory(src EXCLUDE_FROM_ALL) set(CMAKE_POLICY_DEFAULT_CMP0054 NEW) # Re-Enable New Behavior -set_target_properties(png12 PROPERTIES LINK_OPTIONS "LINKER:--version-script=${CMAKE_CURRENT_SOURCE_DIR}/libpng.vers") # Use Symbol Versioning -set_target_properties(png12 PROPERTIES DEBUG_POSTFIX "") # Fix LibPNG Suffix In Debug Mode +if(TARGET png12) + set_target_properties(png12 PROPERTIES LINK_OPTIONS "LINKER:--version-script=${CMAKE_CURRENT_SOURCE_DIR}/libpng.vers") # Use Symbol Versioning + set_target_properties(png12 PROPERTIES DEBUG_POSTFIX "") # Fix LibPNG Suffix In Debug Mode +endif() +if(TARGET png12_static) + find_library(M_LIBRARY NAMES m REQUIRED) + target_link_libraries(png12_static "${ZLIB_LIBRARY}" "${M_LIBRARY}") + target_include_directories(png12_static PUBLIC src) +endif() # Ensure Build -add_custom_target(png12-build ALL DEPENDS png12) +if(TARGET png12) + add_custom_target(png12-build ALL DEPENDS png12) +endif() +if(TARGET png12_static) + add_custom_target(png12_static-build ALL DEPENDS png12_static) +endif() # Install -install(TARGETS png12 DESTINATION "${MCPI_LIB_DIR}") +if(TARGET png12) + install(TARGETS png12 DESTINATION "${MCPI_LIB_DIR}") +endif() diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index d88e0838..609d24eb 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -98,12 +98,11 @@ This component contains all MCPI symbols. ## Dependencies MCPI-Reborn has several dependencies: * MCPI (Bundled) -* GLFW (Only In Client Mode) - * Open GL ES 1.1 +* GLFW (Only In Client Mode; Bundled) + * Open GL ES 2.0 * EGL * OpenAL (Only In Client Mode) * ZLib (Required By LibPNG; Bundled) * LibPNG (Bundled) -* FreeImage (Only In Client Mode) * QEMU User Mode (Only On Non-ARM Hosts; Runtime Only) -* Zenity (Only In Client Mode; Runtime Only) +* Zenity (Only In Client Mode; Runtime Only; Bundled) diff --git a/docs/BUILDING.md b/docs/BUILDING.md index 4a00904f..95651db9 100644 --- a/docs/BUILDING.md +++ b/docs/BUILDING.md @@ -3,12 +3,10 @@ ## Build Dependencies * Common * ARM Compiler - * Host Compiler (Clang) + * Host Compiler * CMake * Host Architecture Dependencies * Client Mode Only - * GLFW - * FreeImage * OpenAL ## Runtime Dependencies @@ -16,11 +14,8 @@ * QEMU User-Mode * Host Architecture Dependencies * Client Mode Only - * OpenGL ES 1.1 - * GLFW - * FreeImage + * OpenGL ES 2.0 * OpenAL - * Zenity ## Instructions ```sh diff --git a/media-layer/core/CMakeLists.txt b/media-layer/core/CMakeLists.txt index 5aa8c09b..0990fb34 100644 --- a/media-layer/core/CMakeLists.txt +++ b/media-layer/core/CMakeLists.txt @@ -26,12 +26,10 @@ if(TARGET media-layer-core-real) # Link target_link_libraries(media-layer-core-real media-layer-headers reborn-util pthread dl) if(NOT MCPI_HEADLESS_MODE) - # Find FreeImage - find_library(FREEIMAGE_LIBRARY NAMES freeimage libfreeimage.so.3 REQUIRED) # OpenAL find_library(OPENAL_LIBRARY NAMES openal REQUIRED) # Link - target_link_libraries(media-layer-core-real "${FREEIMAGE_LIBRARY}" "${OPENAL_LIBRARY}" m GLESv1_CM glfw) + target_link_libraries(media-layer-core-real png12_static "${OPENAL_LIBRARY}" m GLESv1_CM glfw) endif() endif() diff --git a/media-layer/core/src/screenshot.c b/media-layer/core/src/screenshot.c index 69a018a7..87cdc692 100644 --- a/media-layer/core/src/screenshot.c +++ b/media-layer/core/src/screenshot.c @@ -12,7 +12,7 @@ #include #include -#include +#include #include #include @@ -35,6 +35,70 @@ static void ensure_screenshots_folder(char *screenshots) { #define TIME_SIZE 20 // Take Screenshot +static int save_png(const char *filename, unsigned char *pixels, int line_size, int width, int height) { + // Return value + int ret = 0; + + // Variables + png_structp png = NULL; + png_infop info = NULL; + FILE *file = NULL; + png_colorp palette = NULL; + png_bytep rows[height]; + for (int i = 0; i < height; ++i) { + rows[height - i - 1] = (png_bytep) (&pixels[i * line_size]); + } + + // Init + png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + if (!png) { + ret = 1; + goto ret; + } + info = png_create_info_struct(png); + if (!info) { + ret = 1; + goto ret; + } + + // Open File + file = fopen(filename, "wb"); + if (!file) { + ret = 1; + goto ret; + } + + // Prepare To Write + png_init_io(png, file); + png_set_IHDR(png, info, width, height, 8 /* Depth */, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); + palette = (png_colorp) png_malloc(png, PNG_MAX_PALETTE_LENGTH * sizeof(png_color)); + if (!palette) { + ret = 1; + goto ret; + } + png_set_PLTE(png, info, palette, PNG_MAX_PALETTE_LENGTH); + png_write_info(png, info); + png_set_packing(png); + + // Write + png_write_image(png, rows); + png_write_end(png, info); + + ret: + // Free + if (palette != NULL) { + png_free(png, palette); + } + if (file != NULL) { + fclose(file); + } + if (png != NULL) { + png_destroy_write_struct(&png, &info); + } + + // Return + return ret; +} void media_take_screenshot(char *home) { // Get Directory char *screenshots = NULL; @@ -88,41 +152,18 @@ void media_take_screenshot(char *home) { unsigned char pixels[size]; glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); - // Handle Little Endian Systems -#if __BYTE_ORDER == __LITTLE_ENDIAN - // Swap Red And Blue - for (int j = 0; j < width; j++) { - for (int k = 0; k < height; k++) { - int pixel = (k * line_size) + (j * 4); - // Swap - int red = pixels[pixel]; - int blue = pixels[pixel + 2]; - pixels[pixel] = blue; - pixels[pixel + 2] = red; - } - } -#endif - // Save Image - FIBITMAP *image = FreeImage_ConvertFromRawBits(pixels, width, height, line_size, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, 0); - if (!FreeImage_Save(FIF_PNG, image, file, 0)) { + if (save_png(file, pixels, line_size, width, height)) { INFO("Screenshot Failed: %s", file); } else { INFO("Screenshot Saved: %s", file); } - FreeImage_Unload(image); // Free free(file); free(screenshots); } -// Init -__attribute__((constructor)) static void init() { - // Init FreeImage - FreeImage_Initialise(0); -} - #else void media_take_screenshot() { // NOP diff --git a/scripts/install-dependencies.sh b/scripts/install-dependencies.sh index 16393a70..781a85ae 100755 --- a/scripts/install-dependencies.sh +++ b/scripts/install-dependencies.sh @@ -28,8 +28,7 @@ run() { # Host Dependencies Needed For Compile queue_pkg \ - libwayland-bin \ - libfreeimage-dev + libwayland-bin # Host Dependencies Needed For Running queue_pkg \ @@ -44,7 +43,6 @@ run() { # Dependencies queue_pkg \ - libfreeimage3:$1 \ libopenal-dev:$1 # GLFW Dependencies diff --git a/scripts/tools/generate-appimage-builder-yaml.js b/scripts/tools/generate-appimage-builder-yaml.js index 7b64e075..5d4e19d3 100755 --- a/scripts/tools/generate-appimage-builder-yaml.js +++ b/scripts/tools/generate-appimage-builder-yaml.js @@ -40,7 +40,6 @@ if (mode === 'client') { 'libgdk-pixbuf2.0-0', 'librsvg2-common', 'shared-mime-info', - 'libfreeimage3', 'libopenal1' ); }