diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8cd21fbd..ecd0c98b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -14,6 +14,7 @@ find_package(OpenGL REQUIRED) find_package(GLEW REQUIRED) find_package(OpenAL REQUIRED) find_package(ZLIB REQUIRED) +find_package(PNG REQUIRED) if (NOT APPLE) find_package(EnTT REQUIRED) endif() @@ -23,14 +24,11 @@ if (WIN32) set(LUA_LIBRARIES "${CMAKE_CURRENT_SOURCE_DIR}/../vcpkg/packages/luajit_x64-windows/lib/lua51.lib") set(LUA_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../vcpkg/packages/luajit_x64-windows/include/luajit") find_package(glfw3 REQUIRED) - find_package(spng REQUIRED) find_package(glm REQUIRED) find_package(vorbis REQUIRED) - set(PNGLIB spng::spng) set(VORBISLIB Vorbis::vorbis Vorbis::vorbisfile) else() find_package(Lua REQUIRED) - set(PNGLIB spng) set(VORBISLIB vorbis vorbisfile) # not tested add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/libs/glfw) endif() @@ -42,8 +40,7 @@ elseif(APPLE) set(LUA_LIBRARIES "/opt/homebrew/lib/libluajit-5.1.a") message(STATUS "LUA Libraries: ${LUA_LIBRARIES}") message(STATUS "LUA Include Dir: ${LUA_INCLUDE_DIR}") - find_package(PNG REQUIRED) - set(PNGLIB PNG::PNG) + set(VORBISLIB ${VORBIS_LDFLAGS}) message(STATUS "Vorbis Lib: ${VORBIS_LDFLAGS}") else() @@ -52,8 +49,6 @@ else() pkg_check_modules(VORBIS REQUIRED vorbis vorbisfile) set(LUA_LIBRARIES ${LUAJIT_LIBRARIES}) set(LUA_INCLUDE_DIR ${LUAJIT_INCLUDE_DIRS}) - find_package(PNG REQUIRED) - set(PNGLIB PNG::PNG) set(VORBISLIB ${VORBIS_LDFLAGS}) endif() @@ -67,4 +62,4 @@ endif() include_directories(${LUA_INCLUDE_DIR}) target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(${PROJECT_NAME} ${LIBS} glfw OpenGL::GL ${OPENAL_LIBRARY} GLEW::GLEW ZLIB::ZLIB ${VORBISLIB} ${PNGLIB} ${LUA_LIBRARIES} ${CMAKE_DL_LIBS}) +target_link_libraries(${PROJECT_NAME} ${LIBS} glfw OpenGL::GL ${OPENAL_LIBRARY} GLEW::GLEW ZLIB::ZLIB PNG::PNG ${VORBISLIB} ${LUA_LIBRARIES} ${CMAKE_DL_LIBS}) diff --git a/src/coders/png.cpp b/src/coders/png.cpp index 76f4a7b2..123cc2f5 100644 --- a/src/coders/png.cpp +++ b/src/coders/png.cpp @@ -1,5 +1,6 @@ #include "png.hpp" +#include #include #include @@ -11,13 +12,6 @@ static debug::Logger logger("png-coder"); -#ifndef _WIN32 -#define LIBPNG -#endif - -#ifdef LIBPNG -#include - // returns 0 if all-right, 1 otherwise int _png_write( const char* filename, uint width, uint height, const ubyte* data, bool alpha @@ -195,142 +189,6 @@ std::unique_ptr _png_load(const char* file) { return image; } -#else -#include -#include -#include - -static const int SPNG_SUCCESS = 0; -// returns spng result code -int _png_write( - const char* filename, uint width, uint height, const ubyte* data, bool alpha -) { - int fmt; - int ret = 0; - spng_ctx* ctx = nullptr; - spng_ihdr ihdr = {0}; - uint pixsize = alpha ? 4 : 3; - - ctx = spng_ctx_new(SPNG_CTX_ENCODER); - spng_set_option(ctx, SPNG_ENCODE_TO_BUFFER, 1); - - ihdr.width = width; - ihdr.height = height; - ihdr.color_type = - alpha ? SPNG_COLOR_TYPE_TRUECOLOR_ALPHA : SPNG_COLOR_TYPE_TRUECOLOR; - ihdr.bit_depth = 8; - - spng_set_ihdr(ctx, &ihdr); - fmt = SPNG_FMT_PNG; - ret = spng_encode_image( - ctx, - data, - (size_t)width * (size_t)height * pixsize, - fmt, - SPNG_ENCODE_FINALIZE - ); - if (ret != SPNG_SUCCESS) { - logger.error() << "spng_encode_image() error: " << spng_strerror(ret); - spng_ctx_free(ctx); - return ret; - } - - size_t png_size; - void* png_buf = spng_get_png_buffer(ctx, &png_size, &ret); - - if (png_buf == nullptr) { - logger.error() << "spng_get_png_buffer() error: " << spng_strerror(ret); - } else { - files::write_bytes(filename, (const unsigned char*)png_buf, png_size); - } - spng_ctx_free(ctx); - return ret; -} - -std::unique_ptr _png_load(const char* file) { - int r = 0; - FILE* png = nullptr; - spng_ctx* ctx = nullptr; - - png = fopen(file, "rb"); - if (png == nullptr) { - logger.error() << "could not to open file " << file; - return nullptr; - } - - fseek(png, 0, SEEK_END); - long siz_pngbuf = ftell(png); - rewind(png); - if (siz_pngbuf < 1) { - fclose(png); - logger.error() << "could not to read file " << file; - return nullptr; - } - auto pngbuf = std::make_unique(siz_pngbuf); - if (fread(pngbuf.get(), siz_pngbuf, 1, png) != - 1) { // check of read elements count - fclose(png); - logger.error() << "fread() failed: " << file; - return nullptr; - } - fclose(png); // <- finally closing file - ctx = spng_ctx_new(0); - if (ctx == nullptr) { - logger.error() << "spng_ctx_new() failed"; - return nullptr; - } - r = spng_set_crc_action(ctx, SPNG_CRC_USE, SPNG_CRC_USE); - if (r != SPNG_SUCCESS) { - spng_ctx_free(ctx); - logger.error() << "spng_set_crc_action(): " << spng_strerror(r); - return nullptr; - } - r = spng_set_png_buffer(ctx, pngbuf.get(), siz_pngbuf); - if (r != SPNG_SUCCESS) { - spng_ctx_free(ctx); - logger.error() << "spng_set_png_buffer(): " << spng_strerror(r); - return nullptr; - } - - spng_ihdr ihdr; - r = spng_get_ihdr(ctx, &ihdr); - if (r != SPNG_SUCCESS) { - spng_ctx_free(ctx); - logger.error() << "spng_get_ihdr(): " << spng_strerror(r); - return nullptr; - } - - size_t out_size; - r = spng_decoded_image_size(ctx, SPNG_FMT_RGBA8, &out_size); - if (r != SPNG_SUCCESS) { - spng_ctx_free(ctx); - logger.error() << "spng_decoded_image_size(): " << spng_strerror(r); - return nullptr; - } - auto out = std::make_unique(out_size); - r = spng_decode_image(ctx, out.get(), out_size, SPNG_FMT_RGBA8, 0); - if (r != SPNG_SUCCESS) { - spng_ctx_free(ctx); - logger.error() << "spng_decode_image(): " << spng_strerror(r); - return nullptr; - } - - auto flipped = std::make_unique(out_size); - for (size_t i = 0; i < ihdr.height; i += 1) { - size_t rowsize = ihdr.width * 4; - for (size_t j = 0; j < rowsize; j++) { - flipped[(ihdr.height - i - 1) * rowsize + j] = out[i * rowsize + j]; - } - } - - auto image = std::make_unique( - ImageFormat::rgba8888, ihdr.width, ihdr.height, std::move(flipped) - ); - spng_ctx_free(ctx); - return image; -} -#endif - std::unique_ptr png::load_image(const std::string& filename) { auto image = _png_load(filename.c_str()); if (image == nullptr) { diff --git a/vcpkg.json b/vcpkg.json index 05fe48e2..54ee4ec8 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -7,7 +7,7 @@ "glfw3", "glew", "glm", - "libspng", + "libpng", "zlib", "luajit", "libvorbis",