Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ if(LIBRETRO)
if(NOT MSVC)
add_compile_options(-fPIC)
endif()
add_compile_definitions(HAVE_LIBRETRO_VFS)
endif()

if(ANDROID)
Expand Down Expand Up @@ -944,6 +945,10 @@ add_library(Common STATIC
Common/TimeUtil.h
)

if(LIBRETRO)
target_include_directories(Common PRIVATE libretro/libretro-common/include)
endif()

include_directories(Common)
setup_target_project(Common Common)

Expand Down Expand Up @@ -1685,6 +1690,10 @@ add_library(native STATIC
ext/jpge/jpge.h
)

if(LIBRETRO)
target_include_directories(native PRIVATE libretro/libretro-common/include)
endif()

if(LINUX AND NOT ANDROID)
set(RT_LIB rt)
endif()
Expand Down Expand Up @@ -2509,6 +2518,10 @@ add_library(${CoreLibName} ${CoreLinkType}
${CMAKE_CURRENT_BINARY_DIR}/git-version.cpp
)

if(LIBRETRO)
target_include_directories(${CoreLibName} PRIVATE libretro/libretro-common/include)
endif()

if(ANDROID)
set(CoreExtraLibs ${CoreExtraLibs} android)
if(X86_64)
Expand Down
1 change: 0 additions & 1 deletion Common/Data/Format/IniFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#endif

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
Expand Down
52 changes: 40 additions & 12 deletions Common/Data/Format/PNGLoad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,35 +134,63 @@ bool PNGHeaderPeek::IsValidPNGHeader() const {
}

bool pngSave(const Path &filename, const void *buffer, int w, int h, int bytesPerPixel) {
png_image png{};
png.version = PNG_IMAGE_VERSION;
png.format = bytesPerPixel == 3 ? PNG_FORMAT_RGB : PNG_FORMAT_RGBA;
png.width = w;
png.height = h;
const int row_stride = w * bytesPerPixel;
png_bytepp row_ptrs = nullptr;

FILE *fp = File::OpenCFile(filename, "wb");
if (!fp) {
ERROR_LOG(Log::IO, "Unable to open png file for writing: %s", filename.c_str());
return false;
}

int result = png_image_write_to_stdio(&png, fp, 0, buffer, row_stride, nullptr);
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, pngErrorHandler, pngWarningHandler);
if (png_ptr == nullptr) {
fclose(fp);
ERROR_LOG(Log::IO, "PNG encode failed.");
return false;
}

if (png.warning_or_error >= 2) {
ERROR_LOG(Log::IO, "Saving image to PNG produced errors.");
png_infop info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == nullptr) {
png_destroy_write_struct(&png_ptr, nullptr);
fclose(fp);
ERROR_LOG(Log::IO, "PNG encode failed.");
return false;
}

png_image_free(&png);
fclose(fp);
if (setjmp(png_jmpbuf(png_ptr))) {
if (row_ptrs != nullptr) {
png_free(png_ptr, row_ptrs);
}
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);

if (!result) {
// Should we even do this?
File::Delete(filename);

ERROR_LOG(Log::IO, "PNG encode failed.");
return false;
}

png_set_write_fn(png_ptr, fp, [](png_structp png_ptr, png_bytep data, png_size_t size) {
if (fwrite(data, 1, size, (FILE *)png_get_io_ptr(png_ptr)) < size) {
png_error(png_ptr, "Failed to write to file.");
}
}, [](png_structp png_ptr) {
fflush((FILE *)png_get_io_ptr(png_ptr));
});

png_set_IHDR(png_ptr, info_ptr, w, h, 8, bytesPerPixel == 3 ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);

row_ptrs = (png_bytepp)png_malloc(png_ptr, (png_alloc_size_t)h * (png_alloc_size_t)sizeof(png_bytep));
for (png_alloc_size_t i = 0; i < h; ++i) {
row_ptrs[i] = (png_bytep)buffer + (png_alloc_size_t)w * (png_alloc_size_t)bytesPerPixel * i;
}
png_set_rows(png_ptr, info_ptr, row_ptrs);

png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, nullptr);

png_free(png_ptr, row_ptrs);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
return true;
}
Loading
Loading