aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt6
-rw-r--r--LICENSE2
-rw-r--r--cmake/FindSameBoy.cmake1
-rw-r--r--cmake/FindSameBoyBootRom.cmake6
-rw-r--r--cmake/Findlibpng.cmake68
-rw-r--r--cmake/Findlodepng.cmake23
-rw-r--r--deps/lodepng/LICENSE (renamed from src/png/LICENSE)0
-rw-r--r--deps/lodepng/lodepng.c (renamed from src/png/lodepng.c)0
-rw-r--r--deps/lodepng/lodepng.h (renamed from src/png/lodepng.h)0
-rw-r--r--src/CMakeLists.txt23
-rw-r--r--src/common.c14
-rw-r--r--src/common.h8
-rw-r--r--src/embed.h19
-rw-r--r--src/image.c101
-rw-r--r--src/image.h20
-rw-r--r--src/main.c33
-rw-r--r--src/png/CMakeLists.txt8
17 files changed, 290 insertions, 42 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b27aaf4..602fbfa 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -11,8 +11,12 @@ endif()
project(sameboy-thumbnailer VERSION 0.0.0 LANGUAGES C)
+list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
+
+find_package(libpng)
+option(USE_LIBPNG "Use libpng instead of lodepng" ${LIBPNG_FOUND})
+
add_compile_definitions(VERSION=${CMAKE_PROJECT_VERSION})
-list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
add_subdirectory(src) \ No newline at end of file
diff --git a/LICENSE b/LICENSE
index 50f9d6e..1b5a336 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,6 +1,6 @@
MIT License
-Copyright (c) 2021 Jan Drögehoff
+Copyright (c) 2022 Jan Drögehoff
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
diff --git a/cmake/FindSameBoy.cmake b/cmake/FindSameBoy.cmake
index 8fa0986..6f507e4 100644
--- a/cmake/FindSameBoy.cmake
+++ b/cmake/FindSameBoy.cmake
@@ -16,6 +16,7 @@ endif()
option(SAMEBOY_STATIC "" ON)
+mark_as_advanced(SAMEBOY_STATIC)
file(GLOB SAMEBOY_CORE_SOURCES ${SAMEBOY_PATH}/Core/*.c)
diff --git a/cmake/FindSameBoyBootRom.cmake b/cmake/FindSameBoyBootRom.cmake
index 81c9da0..5aadef3 100644
--- a/cmake/FindSameBoyBootRom.cmake
+++ b/cmake/FindSameBoyBootRom.cmake
@@ -6,12 +6,14 @@ set(SAMEBOY_BOOTROM_PATH ${SAMEBOY_PATH}/BootROMs)
set(SAMEBOY_BOOTROM_VERSION "cgb_boot" CACHE STRING "")
set(SAMEBOY_BOOTROM_SIZE "2304" CACHE STRING "")
+mark_as_advanced(SAMEBOY_BOOTROM_VERSION SAMEBOY_BOOTROM_SIZE)
add_executable(pb12 ${SAMEBOY_BOOTROM_PATH}/pb12.c)
find_program(RGBASM rgbasm REQUIRED)
-find_program(RBLINK rgblink REQUIRED)
+find_program(RGBLINK rgblink REQUIRED)
find_program(RGBGFX rgbgfx REQUIRED)
+mark_as_advanced(RGBASM RGBLINK RGBGFX)
# custom build step
add_custom_command(
@@ -30,7 +32,7 @@ add_custom_command(
COMMAND
${RGBASM} -o ${SAMEBOY_BOOTROM_VERSION}.tmp ${SAMEBOY_BOOTROM_PATH}/${SAMEBOY_BOOTROM_VERSION}.asm
COMMAND
- ${RBLINK} -o ${SAMEBOY_BOOTROM_VERSION}.tmp2 ${SAMEBOY_BOOTROM_VERSION}.tmp
+ ${RGBLINK} -o ${SAMEBOY_BOOTROM_VERSION}.tmp2 ${SAMEBOY_BOOTROM_VERSION}.tmp
COMMAND
dd if=${SAMEBOY_BOOTROM_VERSION}.tmp2 of=${SAMEBOY_BOOTROM_VERSION}.bin count=1 bs=${SAMEBOY_BOOTROM_SIZE}
DEPENDS SameBoyLogo.pb12
diff --git a/cmake/Findlibpng.cmake b/cmake/Findlibpng.cmake
new file mode 100644
index 0000000..62c4d87
--- /dev/null
+++ b/cmake/Findlibpng.cmake
@@ -0,0 +1,68 @@
+#
+# LIBPNG_INCLUDE_DIRS
+# LIBPNG_LIBRARIES
+# LIBPNG_CFLAGS
+# LIBPNG_FOUND
+
+
+find_package(PkgConfig QUIET)
+if (PKG_CONFIG_FOUND)
+ pkg_check_modules(_LIBPNG libpng)
+
+ if (BUILD_STATIC AND NOT _LIBPNG_FOUND)
+ message(FATAL_ERROR "Cannot find static build information")
+ endif()
+endif()
+set(LIBPNG_FOUND ${_LIBPNG_FOUND})
+
+if (_LIBPNG_FOUND) # we can rely on pkg-config
+
+ if (NOT BUILD_STATIC)
+ set(LIBPNG_LIBRARIES ${_LIBPNG_LIBRARIES})
+ set(LIBPNG_INCLUDE_DIRS ${_LIBPNG_INCLUDE_DIRS})
+ set(LIBPNG_CFLAGS ${_LIBPNG_CFLAGS_OTHER})
+ else()
+ set(LIBPNG_LIBRARIES ${_LIBPNG_STATIC_LIBRARIES})
+ set(LIBPNG_INCLUDE_DIRS ${_LIBPNG_STATIC_INCLUDE_DIRS})
+ set(LIBPNG_CFLAGS ${_LIBPNG_STATIC_CFLAGS_OTHER})
+ endif()
+else()
+ if(CMAKE_SIZEOF_VOID_P EQUAL 8)
+ set(_lib_suffix 64)
+ else()
+ set(_lib_suffix 32)
+ endif()
+
+ find_path(LIBPNG_INC
+ NAMES png.h
+ HINTS
+ ENV LIBPNGPath${_lib_suffix}
+ ENV LIBPNGPath
+ ${_LIBPNG_INCLUDE_DIRS}
+ PATHS
+ /usr/include/libpng16 /usr/local/include/libpng16
+ /usr/include/libpng /usr/local/include/libpng)
+
+ find_library(LIBPNG_LIB
+ NAMES ${_LIBPNG_LIBRARIES} libpng.so libpng16.so
+ HINTS
+ ENV LIBPNGPath${_lib_suffix}
+ ENV LIBPNGPath
+ ${_LIBPNG_LIBRARY_DIRS}
+ ${_LIBPNG_STATIC_LIBRARY_DIRS}
+ PATHS
+ /usr/lib${_lib_suffix} /usr/local/lib${_lib_suffix}
+ /usr/lib /usr/local/lib)
+
+ include(FindPackageHandleStandardArgs)
+ find_package_handle_standard_args(LIBPNG DEFAULT_MSG LIBPNG_LIB LIBPNG_INC)
+ mark_as_advanced(LIBPNG_INC LIBPNG_LIB)
+
+ if(LIBPNG_FOUND)
+ set(LIBPNG_INCLUDE_DIRS ${LIBPNG_INC})
+ set(LIBPNG_LIBRARIES ${LIBPNG_LIB})
+ if (BUILD_STATIC)
+ set(LIBPNG_LIBRARIES ${LIBPNG_LIBRARIES} ${_LIBPNG_STATIC_LIBRARIES})
+ endif()
+ endif()
+endif() \ No newline at end of file
diff --git a/cmake/Findlodepng.cmake b/cmake/Findlodepng.cmake
new file mode 100644
index 0000000..b8f7a01
--- /dev/null
+++ b/cmake/Findlodepng.cmake
@@ -0,0 +1,23 @@
+#
+# LODEPNG_INCLUDE_DIRS
+# LODEPNG_LIBRARIES
+# LODEPNG_CFLAGS
+
+
+if(LODEPNG_PATH)
+ return()
+endif()
+
+set(LODEPNG_PATH ${PROJECT_SOURCE_DIR}/deps/lodepng)
+
+list(APPEND
+ LODEPNG_SOURCES
+ ${LODEPNG_PATH}/lodepng.c
+ ${LODEPNG_PATH}/lodepng.h
+)
+
+add_library(lodepng STATIC ${LODEPNG_SOURCES})
+
+set(LODEPNG_INCLUDE_DIRS ${LODEPNG_PATH})
+set(LODEPNG_LIBRARIES lodepng)
+set(LODEPNG_CFLAGS "") \ No newline at end of file
diff --git a/src/png/LICENSE b/deps/lodepng/LICENSE
index a5fb060..a5fb060 100644
--- a/src/png/LICENSE
+++ b/deps/lodepng/LICENSE
diff --git a/src/png/lodepng.c b/deps/lodepng/lodepng.c
index 51492b8..51492b8 100644
--- a/src/png/lodepng.c
+++ b/deps/lodepng/lodepng.c
diff --git a/src/png/lodepng.h b/deps/lodepng/lodepng.h
index 6801cb7..6801cb7 100644
--- a/src/png/lodepng.h
+++ b/deps/lodepng/lodepng.h
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index f5d8646..a3fdafb 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -3,14 +3,29 @@ find_package(SameBoy REQUIRED)
find_package(SameBoyBootRom REQUIRED)
find_package(SameBoyTemplate REQUIRED)
-include_directories(${SAMEBOY_INCLUDE_DIRS})
+if (USE_LIBPNG)
+ find_package(libpng REQUIRED)
+ add_compile_definitions(USE_LIBPNG)
+ set(PNG_INCLUDE_DIRS ${LIBPNG_INCLUDE_DIRS})
+ set(PNG_LIBRARIES ${LIBPNG_LIBRARIES})
+else()
+ find_package(lodepng REQUIRED)
+ add_compile_definitions(USE_LODEPNG)
+ set(PNG_INCLUDE_DIRS ${LODEPNG_INCLUDE_DIRS})
+ set(PNG_LIBRARIES ${LODEPNG_LIBRARIES})
+endif()
-add_subdirectory(png)
+include_directories(${SAMEBOY_INCLUDE_DIRS})
list(APPEND
THUMBNAILER_SOURCES
+ ${CMAKE_CURRENT_SOURCE_DIR}/common.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/common.h
+ ${CMAKE_CURRENT_SOURCE_DIR}/embed.h
${CMAKE_CURRENT_SOURCE_DIR}/get_image_for_rom.c
${CMAKE_CURRENT_SOURCE_DIR}/get_image_for_rom.h
+ ${CMAKE_CURRENT_SOURCE_DIR}/image.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/image.h
${CMAKE_CURRENT_SOURCE_DIR}/main.c
${SAMEBOY_BOOTROM_SOURCES}
${SAMEBOY_TEMPLATE_SOURCES}
@@ -22,7 +37,9 @@ add_executable(sameboy-thumbnailer ${THUMBNAILER_SOURCES})
add_dependencies(sameboy-thumbnailer sameboy_bootrom sameboy_template)
-target_link_libraries(sameboy-thumbnailer LINK_PUBLIC lodepng)
+target_include_directories(sameboy-thumbnailer PUBLIC ${PNG_INCLUDE_DIRS})
+
+target_link_libraries(sameboy-thumbnailer LINK_PUBLIC ${PNG_LIBRARIES})
target_link_libraries(sameboy-thumbnailer LINK_PUBLIC ${SAMEBOY_LIBRARIES})
target_link_libraries(sameboy-thumbnailer LINK_PUBLIC ${CMAKE_DL_LIBS})
target_link_libraries(sameboy-thumbnailer LINK_PUBLIC m)
diff --git a/src/common.c b/src/common.c
new file mode 100644
index 0000000..959cbf6
--- /dev/null
+++ b/src/common.c
@@ -0,0 +1,14 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "common.h"
+
+void* local_malloc(size_t size)
+{
+ void* mem = malloc(size);
+ if (!mem) {
+ fprintf(stderr, "Failed to allocate memory\n");
+ exit(-1);
+ }
+ return mem;
+} \ No newline at end of file
diff --git a/src/common.h b/src/common.h
new file mode 100644
index 0000000..06d8c27
--- /dev/null
+++ b/src/common.h
@@ -0,0 +1,8 @@
+#ifndef COMMON_H
+#define COMMON_H
+
+#include <stddef.h>
+
+void* local_malloc(size_t size);
+
+#endif \ No newline at end of file
diff --git a/src/embed.h b/src/embed.h
new file mode 100644
index 0000000..12eea23
--- /dev/null
+++ b/src/embed.h
@@ -0,0 +1,19 @@
+#ifndef EMBED_H
+#define EMBED_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+extern const uint8_t cgb_boot_bin[];
+extern const size_t cgb_boot_bin_size;
+
+extern unsigned char CartridgeTemplate_png[];
+extern unsigned int CartridgeTemplate_png_size;
+
+extern unsigned char ColorCartridgeTemplate_png[];
+extern unsigned int ColorCartridgeTemplate_png_size;
+
+extern unsigned char UniversalCartridgeTemplate_png[];
+extern unsigned int UniversalCartridgeTemplate_png_size;
+
+#endif \ No newline at end of file
diff --git a/src/image.c b/src/image.c
new file mode 100644
index 0000000..dd52b95
--- /dev/null
+++ b/src/image.c
@@ -0,0 +1,101 @@
+#include "image.h"
+#include "common.h"
+
+unsigned decode32(uint32_t** out, unsigned* w, unsigned* h,
+ unsigned char* in, size_t insize)
+{
+#if defined(USE_LIBPNG)
+ unsigned retval = 1;
+
+ FILE* fp = fmemopen(in, insize, "rb");;
+ png_bytep* row_pointers = NULL;
+
+ if (insize < 8 || png_sig_cmp(in, 0, 8)) abort();
+
+ png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ png_infop info = png_create_info_struct(png);
+
+ if(setjmp(png_jmpbuf(png))) goto cleanup;
+
+ png_init_io(png, fp);
+ png_read_info(png, info);
+
+ if(png_get_bit_depth(png, info) == 16) png_set_strip_16(png);
+
+ *w = png_get_image_width(png, info);
+ *h = png_get_image_height(png, info);
+
+ png_byte color_type = png_get_color_type(png, info);
+
+ if(color_type == PNG_COLOR_TYPE_PALETTE)
+ png_set_palette_to_rgb(png);
+
+ if(png_get_valid(png, info, PNG_INFO_tRNS))
+ png_set_tRNS_to_alpha(png);
+
+ row_pointers = local_malloc(sizeof(png_bytep) * *h);
+ *out = local_malloc(sizeof(**out) * (*w * *h));
+ for(int y = 0; y < *h; y++)
+ {
+ row_pointers[y] = (png_byte*)(*out+((*w)*y));
+ }
+
+ png_read_image(png, row_pointers);
+
+ retval = 0;
+
+ cleanup:
+ png_destroy_read_struct(&png, &info, NULL);
+
+ if (fp) fclose(fp);
+ if (row_pointers) free(row_pointers);
+
+ return retval;
+#elif defined(USE_LODEPNG)
+ return lodepng_decode32((unsigned char**)out, w, h, in, insize);
+#else
+ #error "No decode implementation"
+#endif
+}
+
+unsigned encode32_file(const char* filename, const uint32_t* image, unsigned w, unsigned h)
+{
+#if defined(USE_LIBPNG)
+ /* create file */
+ FILE *fp = fopen(filename, "wb");
+ png_bytep* row_pointers = NULL;
+
+ /* initialize stuff */
+ png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ png_infop info_ptr = png_create_info_struct(png);
+ if(setjmp(png_jmpbuf(png))) goto cleanup;
+
+ png_init_io(png, fp);
+
+
+ png_set_IHDR(png, info_ptr, w, h,
+ 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
+ PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
+
+ png_write_info(png, info_ptr);
+
+ row_pointers = local_malloc(sizeof(png_bytep) * h);
+ for(int y = 0; y < h; y++)
+ {
+ row_pointers[y] = (png_byte*)(image+(w*y));
+ }
+
+ png_write_image(png, row_pointers);
+
+ png_write_end(png, NULL);
+
+ cleanup:
+
+ free(row_pointers);
+ fclose(fp);
+#elif defined(USE_LODEPNG)
+ return lodepng_encode32_file(filename, image, w, h);
+#else
+ #error "No encode implementation"
+#endif
+} \ No newline at end of file
diff --git a/src/image.h b/src/image.h
new file mode 100644
index 0000000..1a9aeae
--- /dev/null
+++ b/src/image.h
@@ -0,0 +1,20 @@
+#ifndef IMAGE_H
+#define IMAGE_H
+
+#if defined(USE_LIBPNG)
+#include <png.h>
+#elif defined(USE_LODEPNG)
+#include "lodepng.h"
+#else
+#error "No png implementation"
+#endif
+
+#include <stdint.h>
+#include <stddef.h>
+
+// API structured after lodepng API
+
+unsigned decode32(uint32_t** out, unsigned* w, unsigned* h, unsigned char* in, size_t insize);
+unsigned encode32_file(const char* filename, const uint32_t* image, unsigned w, unsigned h);
+
+#endif \ No newline at end of file
diff --git a/src/main.c b/src/main.c
index e332fb1..5d3291f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -3,20 +3,10 @@
#include <stdint.h>
#include <string.h>
+#include "common.h"
+#include "embed.h"
#include "get_image_for_rom.h"
-#include "png/lodepng.h"
-
-extern const uint8_t cgb_boot_bin[];
-extern const size_t cgb_boot_bin_size;
-
-extern unsigned char CartridgeTemplate_png[];
-extern unsigned int CartridgeTemplate_png_size;
-
-extern unsigned char ColorCartridgeTemplate_png[];
-extern unsigned int ColorCartridgeTemplate_png_size;
-
-extern unsigned char UniversalCartridgeTemplate_png[];
-extern unsigned int UniversalCartridgeTemplate_png_size;
+#include "image.h"
static uint32_t alpha_blend(const uint32_t dest, const uint32_t src)
{
@@ -95,16 +85,6 @@ static void scale_image(const uint32_t* input, const signed input_width, const s
}
}
-static void* local_malloc(size_t size)
-{
- void* mem = malloc(size);
- if (!mem) {
- fprintf(stderr, "Failed to allocate memory\n");
- exit(-1);
- }
- return mem;
-}
-
int main (int argc , char** argv)
{
#define str(x) #x
@@ -121,7 +101,7 @@ int main (int argc , char** argv)
const uint16_t X_OFFSET = 192;
const uint16_t Y_OFFSET = 298;
- unsigned int size = 128;
+ int size = 128;
char* input = NULL;
char* output = NULL;
@@ -191,12 +171,11 @@ int main (int argc , char** argv)
template_size = CartridgeTemplate_png_size;
}
- if (lodepng_decode32((unsigned char**)&template, &template_width, &template_height, template_data, template_size)) {
+ if (decode32(&template, &template_width, &template_height, template_data, template_size)) {
fprintf(stderr, "Failed to decode template\n");
return -1;
}
-
uint32_t* canvas = local_malloc(template_width * template_height * sizeof(uint32_t));
// clear the canvas so we don't end up with garbage data somewhere
memset(canvas, 0, template_width * template_height * sizeof(uint32_t));
@@ -224,7 +203,7 @@ int main (int argc , char** argv)
uint32_t* final = local_malloc(size * size * sizeof(uint32_t));
scale_image(canvas, template_width, template_height, final, (double)size/template_width, template_width/size);
- lodepng_encode32_file(output, (const unsigned char*)final, size, size);
+ encode32_file(output, final, size, size);
free(final);
free(canvas);
diff --git a/src/png/CMakeLists.txt b/src/png/CMakeLists.txt
deleted file mode 100644
index 056e096..0000000
--- a/src/png/CMakeLists.txt
+++ /dev/null
@@ -1,8 +0,0 @@
-
-list(APPEND
- LODEPNG_SOURCES
- ${CMAKE_CURRENT_SOURCE_DIR}/lodepng.c
- ${CMAKE_CURRENT_SOURCE_DIR}/lodepng.h
-)
-
-add_library(lodepng STATIC ${LODEPNG_SOURCES}) \ No newline at end of file