Ditch FreeImage

This commit is contained in:
TheBrokenRail 2022-06-13 20:49:09 -04:00
parent f1ec29ec86
commit ca21877000
9 changed files with 114 additions and 64 deletions

View File

@ -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)

View File

@ -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

View File

@ -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()

View File

@ -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)

View File

@ -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

View File

@ -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()

View File

@ -12,7 +12,7 @@
#include <errno.h>
#include <sys/stat.h>
#include <FreeImage.h>
#include <png.h>
#include <GLES/gl.h>
#include <media-layer/core.h>
@ -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

View File

@ -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

View File

@ -40,7 +40,6 @@ if (mode === 'client') {
'libgdk-pixbuf2.0-0',
'librsvg2-common',
'shared-mime-info',
'libfreeimage3',
'libopenal1'
);
}