Use libpng for reading png instead of stb

This commit is contained in:
Joel16 2020-07-11 16:00:27 -04:00
parent 1eb1d7727b
commit 9d3510eef7
4 changed files with 56 additions and 9 deletions

View File

@ -78,6 +78,7 @@ target_link_libraries(${PROJECT_NAME}
tiff
webp
jpeg
png
pthread
lzma
z

View File

@ -18,6 +18,7 @@ namespace Textures {
bool LoadImageGIF(const std::string &path, Tex *texture, unsigned int *frames);
bool LoadImageICO(const std::string &path, Tex *texture);
bool LoadImagePCX(const std::string &path, Tex *texture);
bool LoadImagePNG(const std::string &path, Tex *texture);
bool LoadImageTIFF(const std::string &path, Tex *texture);
bool LoadImageWEBP(const std::string &path, Tex *texture);
void Free(Tex *texture);

View File

@ -169,8 +169,8 @@ namespace GUI {
IM_ASSERT(image_ret);
gui_state = GUI_STATE_IMAGE_PREVIEW;
}
else if ((ext == ".JPG") || (ext == ".JPEG") || (ext == ".PNG") || (ext == ".PGM") || (ext == ".PPM")
|| (ext == ".PSD") || (ext == ".TGA")) {
else if ((ext == ".JPG") || (ext == ".JPEG") || (ext == ".PGM") || (ext == ".PPM") || (ext == ".PSD")
|| (ext == ".TGA")) {
SceBool image_ret = Textures::LoadImageFile(path, &texture);
IM_ASSERT(image_ret);
gui_state = GUI_STATE_IMAGE_PREVIEW;
@ -193,6 +193,11 @@ namespace GUI {
IM_ASSERT(image_ret);
gui_state = GUI_STATE_IMAGE_PREVIEW;
}
else if (ext == ".PNG") {
SceBool image_ret = Textures::LoadImagePNG(path, &texture);
IM_ASSERT(image_ret);
gui_state = GUI_STATE_IMAGE_PREVIEW;
}
else if (ext == ".TIFF") {
SceBool image_ret = Textures::LoadImageTIFF(path, &texture);
IM_ASSERT(image_ret);

View File

@ -1,7 +1,4 @@
// PCX
#define DR_PCX_IMPLEMENTATION
#define DR_PCX_NO_STDIO
#include "dr_pcx.h"
#include <cstring>
// BMP
#include "libnsbmp.h"
@ -9,6 +6,14 @@
// GIF
#include "libnsgif.h"
// PCX
#define DR_PCX_IMPLEMENTATION
#define DR_PCX_NO_STDIO
#include "dr_pcx.h"
// PNG
#include <png.h>
// STB
#define STB_IMAGE_IMPLEMENTATION
#define STBI_NO_STDIO
@ -16,6 +21,7 @@
#define STBI_NO_GIF
#define STBI_NO_HDR
#define STBI_NO_PIC
#define STBI_NO_PNG
#define STBI_ONLY_JPEG
#define STBI_ONLY_PNG
#define STBI_ONLY_PNM
@ -331,6 +337,40 @@ namespace Textures {
return ret;
}
bool LoadImagePNG(const std::string &path, Tex *texture) {
unsigned char *data = nullptr;
SceOff size = 0;
if (R_FAILED(FS::ReadFile(path, &data, &size)))
return false;
png_image image;
std::memset(&image, 0, (sizeof image));
image.version = PNG_IMAGE_VERSION;
if (R_FAILED(png_image_begin_read_from_memory(&image, data, size))) {
delete[] data;
return false;
}
image.format = PNG_FORMAT_RGBA;
png_bytep buffer = new png_byte[PNG_IMAGE_SIZE(image)];
if (buffer != nullptr) {
if (R_FAILED(png_image_finish_read(&image, nullptr, buffer, 0, nullptr))) {
png_image_free(&image);
delete[] buffer;
delete[] data;
return false;
}
}
bool ret = LoadImage(buffer, (int *)&image.width, (int *)&image.height, texture, nullptr);
delete[] buffer;
delete[] data;
return ret;
}
bool LoadImageTIFF(const std::string &path, Tex *texture) {
TIFF *tif = TIFFOpen(path.c_str(), "r");
if (tif) {
@ -373,13 +413,13 @@ namespace Textures {
}
void Init(void) {
bool image_ret = LoadImageFile("app0:res/file.png", &file_texture);
bool image_ret = LoadImagePNG("app0:res/file.png", &file_texture);
IM_ASSERT(image_ret);
image_ret = LoadImageFile("app0:res/folder.png", &folder_texture);
image_ret = LoadImagePNG("app0:res/folder.png", &folder_texture);
IM_ASSERT(image_ret);
image_ret = LoadImageFile("app0:res/image.png", &image_texture);
image_ret = LoadImagePNG("app0:res/image.png", &image_texture);
IM_ASSERT(image_ret);
}