mirror of
https://github.com/shadps4-emu/shadPS4.git
synced 2025-06-23 10:56:19 +00:00
core: Library cleanup (#1631)
* core: Split error codes into separate files * Reduces build times and is cleaner * core: Bring structs and enums to codebase style * core: More style changes
This commit is contained in:
parent
3d0aacd43d
commit
5b6e0ab238
114 changed files with 2158 additions and 2509 deletions
|
@ -4,9 +4,9 @@
|
|||
#include <png.h>
|
||||
#include "common/assert.h"
|
||||
#include "common/logging/log.h"
|
||||
#include "core/libraries/error_codes.h"
|
||||
#include "core/libraries/libpng/pngdec.h"
|
||||
#include "core/libraries/libpng/pngdec_error.h"
|
||||
#include "core/libraries/libs.h"
|
||||
#include "pngdec.h"
|
||||
|
||||
namespace Libraries::PngDec {
|
||||
|
||||
|
@ -15,24 +15,25 @@ struct PngHandler {
|
|||
png_infop info_ptr;
|
||||
};
|
||||
|
||||
struct PngStruct {
|
||||
const u8* data;
|
||||
size_t size;
|
||||
u64 offset;
|
||||
};
|
||||
|
||||
static inline OrbisPngDecColorSpace MapPngColor(int color) {
|
||||
switch (color) {
|
||||
case PNG_COLOR_TYPE_GRAY:
|
||||
return OrbisPngDecColorSpace::ORBIS_PNG_DEC_COLOR_SPACE_GRAYSCALE;
|
||||
|
||||
return OrbisPngDecColorSpace::Grayscale;
|
||||
case PNG_COLOR_TYPE_GRAY_ALPHA:
|
||||
return OrbisPngDecColorSpace::ORBIS_PNG_DEC_COLOR_SPACE_GRAYSCALE_ALPHA;
|
||||
|
||||
return OrbisPngDecColorSpace::GrayscaleAlpha;
|
||||
case PNG_COLOR_TYPE_PALETTE:
|
||||
return OrbisPngDecColorSpace::ORBIS_PNG_DEC_COLOR_SPACE_CLUT;
|
||||
|
||||
return OrbisPngDecColorSpace::Clut;
|
||||
case PNG_COLOR_TYPE_RGB:
|
||||
return OrbisPngDecColorSpace::ORBIS_PNG_DEC_COLOR_SPACE_RGB;
|
||||
|
||||
return OrbisPngDecColorSpace::Rgb;
|
||||
case PNG_COLOR_TYPE_RGB_ALPHA:
|
||||
return OrbisPngDecColorSpace::ORBIS_PNG_DEC_COLOR_SPACE_RGBA;
|
||||
return OrbisPngDecColorSpace::Rgba;
|
||||
}
|
||||
|
||||
UNREACHABLE_MSG("unknown png color type");
|
||||
}
|
||||
|
||||
|
@ -54,8 +55,8 @@ s32 PS4_SYSV_ABI scePngDecCreate(const OrbisPngDecCreateParam* param, void* memo
|
|||
LOG_ERROR(Lib_Png, "Invalid memory address!");
|
||||
return ORBIS_PNG_DEC_ERROR_INVALID_ADDR;
|
||||
}
|
||||
if (param->maxImageWidth - 1 > 1000000) {
|
||||
LOG_ERROR(Lib_Png, "Invalid size! width = {}", param->maxImageWidth);
|
||||
if (param->max_image_width - 1 > 1000000) {
|
||||
LOG_ERROR(Lib_Png, "Invalid size! width = {}", param->max_image_width);
|
||||
return ORBIS_PNG_DEC_ERROR_INVALID_SIZE;
|
||||
}
|
||||
auto pngh = (PngHandler*)memoryAddress;
|
||||
|
@ -86,7 +87,7 @@ s32 PS4_SYSV_ABI scePngDecDecode(OrbisPngDecHandle handle, const OrbisPngDecDeco
|
|||
LOG_ERROR(Lib_Png, "Invalid param!");
|
||||
return ORBIS_PNG_DEC_ERROR_INVALID_PARAM;
|
||||
}
|
||||
if (param->pngMemAddr == nullptr || param->pngMemAddr == nullptr) {
|
||||
if (param->png_mem_addr == nullptr || param->image_mem_addr == nullptr) {
|
||||
LOG_ERROR(Lib_Png, "invalid image address!");
|
||||
return ORBIS_PNG_DEC_ERROR_INVALID_ADDR;
|
||||
}
|
||||
|
@ -98,76 +99,74 @@ s32 PS4_SYSV_ABI scePngDecDecode(OrbisPngDecHandle handle, const OrbisPngDecDeco
|
|||
|
||||
auto pngh = (PngHandler*)handle;
|
||||
|
||||
struct pngstruct {
|
||||
const u8* data;
|
||||
size_t size;
|
||||
u64 offset;
|
||||
} pngdata = {
|
||||
.data = (const u8*)param->pngMemAddr,
|
||||
.size = param->pngMemSize,
|
||||
const auto pngdata = PngStruct{
|
||||
.data = param->png_mem_addr,
|
||||
.size = param->png_mem_size,
|
||||
.offset = 0,
|
||||
};
|
||||
|
||||
// Read png from memory
|
||||
png_set_read_fn(pngh->png_ptr, (void*)&pngdata,
|
||||
[](png_structp ps, png_bytep data, png_size_t len) {
|
||||
if (len == 0)
|
||||
return;
|
||||
auto pngdata = (pngstruct*)png_get_io_ptr(ps);
|
||||
auto pngdata = (PngStruct*)png_get_io_ptr(ps);
|
||||
::memcpy(data, pngdata->data + pngdata->offset, len);
|
||||
pngdata->offset += len;
|
||||
});
|
||||
|
||||
u32 width, height;
|
||||
int color_type, bit_depth;
|
||||
png_read_info(pngh->png_ptr, pngh->info_ptr);
|
||||
|
||||
width = png_get_image_width(pngh->png_ptr, pngh->info_ptr);
|
||||
height = png_get_image_height(pngh->png_ptr, pngh->info_ptr);
|
||||
color_type = MapPngColor(png_get_color_type(pngh->png_ptr, pngh->info_ptr));
|
||||
bit_depth = png_get_bit_depth(pngh->png_ptr, pngh->info_ptr);
|
||||
const u32 width = png_get_image_width(pngh->png_ptr, pngh->info_ptr);
|
||||
const u32 height = png_get_image_height(pngh->png_ptr, pngh->info_ptr);
|
||||
const auto color_type = MapPngColor(png_get_color_type(pngh->png_ptr, pngh->info_ptr));
|
||||
const auto bit_depth = png_get_bit_depth(pngh->png_ptr, pngh->info_ptr);
|
||||
|
||||
if (imageInfo != nullptr) {
|
||||
imageInfo->bitDepth = bit_depth;
|
||||
imageInfo->imageWidth = width;
|
||||
imageInfo->imageHeight = height;
|
||||
imageInfo->colorSpace = color_type;
|
||||
imageInfo->imageFlag = 0;
|
||||
imageInfo->bit_depth = bit_depth;
|
||||
imageInfo->image_width = width;
|
||||
imageInfo->image_height = height;
|
||||
imageInfo->color_space = color_type;
|
||||
imageInfo->image_flag = OrbisPngDecImageFlag::None;
|
||||
if (png_get_interlace_type(pngh->png_ptr, pngh->info_ptr) == 1) {
|
||||
imageInfo->imageFlag |= OrbisPngDecImageFlag::ORBIS_PNG_DEC_IMAGE_FLAG_ADAM7_INTERLACE;
|
||||
imageInfo->image_flag |= OrbisPngDecImageFlag::Adam7Interlace;
|
||||
}
|
||||
if (png_get_valid(pngh->png_ptr, pngh->info_ptr, PNG_INFO_tRNS)) {
|
||||
|
||||
imageInfo->imageFlag |= ORBIS_PNG_DEC_IMAGE_FLAG_TRNS_CHUNK_EXIST;
|
||||
imageInfo->image_flag |= OrbisPngDecImageFlag::TrnsChunkExist;
|
||||
}
|
||||
}
|
||||
|
||||
if (bit_depth == 16)
|
||||
if (bit_depth == 16) {
|
||||
png_set_strip_16(pngh->png_ptr);
|
||||
if (color_type == PNG_COLOR_TYPE_PALETTE)
|
||||
}
|
||||
if (color_type == OrbisPngDecColorSpace::Clut) {
|
||||
png_set_palette_to_rgb(pngh->png_ptr);
|
||||
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
|
||||
}
|
||||
if (color_type == OrbisPngDecColorSpace::Grayscale && bit_depth < 8) {
|
||||
png_set_expand_gray_1_2_4_to_8(pngh->png_ptr);
|
||||
if (png_get_valid(pngh->png_ptr, pngh->info_ptr, PNG_INFO_tRNS))
|
||||
}
|
||||
if (png_get_valid(pngh->png_ptr, pngh->info_ptr, PNG_INFO_tRNS)) {
|
||||
png_set_tRNS_to_alpha(pngh->png_ptr);
|
||||
if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
|
||||
}
|
||||
if (color_type == OrbisPngDecColorSpace::Grayscale ||
|
||||
color_type == OrbisPngDecColorSpace::GrayscaleAlpha) {
|
||||
png_set_gray_to_rgb(pngh->png_ptr);
|
||||
if (param->pixelFormat == OrbisPngDecPixelFormat::ORBIS_PNG_DEC_PIXEL_FORMAT_B8G8R8A8)
|
||||
}
|
||||
if (param->pixel_format == OrbisPngDecPixelFormat::B8G8R8A8) {
|
||||
png_set_bgr(pngh->png_ptr);
|
||||
if (color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_GRAY ||
|
||||
color_type == PNG_COLOR_TYPE_PALETTE)
|
||||
png_set_add_alpha(pngh->png_ptr, param->alphaValue, PNG_FILLER_AFTER);
|
||||
}
|
||||
if (color_type == OrbisPngDecColorSpace::Rgb ||
|
||||
color_type == OrbisPngDecColorSpace::Grayscale ||
|
||||
color_type == OrbisPngDecColorSpace::Clut) {
|
||||
png_set_add_alpha(pngh->png_ptr, param->alpha_value, PNG_FILLER_AFTER);
|
||||
}
|
||||
|
||||
int pass = png_set_interlace_handling(pngh->png_ptr);
|
||||
const s32 pass = png_set_interlace_handling(pngh->png_ptr);
|
||||
png_read_update_info(pngh->png_ptr, pngh->info_ptr);
|
||||
|
||||
auto const numChannels = png_get_channels(pngh->png_ptr, pngh->info_ptr);
|
||||
auto horizontal_bytes = numChannels * width;
|
||||
const s32 num_channels = png_get_channels(pngh->png_ptr, pngh->info_ptr);
|
||||
const s32 horizontal_bytes = num_channels * width;
|
||||
const s32 stride = param->image_pitch > 0 ? param->image_pitch : horizontal_bytes;
|
||||
|
||||
int stride = param->imagePitch > 0 ? param->imagePitch : horizontal_bytes;
|
||||
|
||||
for (int j = 0; j < pass; j++) { // interlaced
|
||||
auto ptr = (png_bytep)param->imageMemAddr;
|
||||
for (int j = 0; j < pass; j++) {
|
||||
auto ptr = reinterpret_cast<png_bytep>(param->image_mem_addr);
|
||||
for (int y = 0; y < height; y++) {
|
||||
png_read_row(pngh->png_ptr, ptr, nullptr);
|
||||
ptr += stride;
|
||||
|
@ -196,7 +195,7 @@ s32 PS4_SYSV_ABI scePngDecParseHeader(const OrbisPngDecParseParam* param,
|
|||
}
|
||||
|
||||
u8 header[8];
|
||||
memcpy(header, param->pngMemAddr, 8);
|
||||
memcpy(header, param->png_mem_addr, 8);
|
||||
// Check if the header indicates a valid PNG file
|
||||
if (png_sig_cmp(header, 0, 8)) {
|
||||
LOG_ERROR(Lib_Png, "Memory doesn't contain a valid png file");
|
||||
|
@ -209,18 +208,14 @@ s32 PS4_SYSV_ABI scePngDecParseHeader(const OrbisPngDecParseParam* param,
|
|||
// Create a libpng info structure
|
||||
auto info_ptr = png_create_info_struct(png_ptr);
|
||||
|
||||
struct pngstruct {
|
||||
const u8* data;
|
||||
size_t size;
|
||||
u64 offset;
|
||||
} pngdata = {
|
||||
.data = (const u8*)param->pngMemAddr,
|
||||
.size = param->pngMemSize,
|
||||
const auto pngdata = PngStruct{
|
||||
.data = param->png_mem_addr,
|
||||
.size = param->png_mem_size,
|
||||
.offset = 0,
|
||||
};
|
||||
|
||||
png_set_read_fn(png_ptr, (void*)&pngdata, [](png_structp ps, png_bytep data, png_size_t len) {
|
||||
auto pngdata = (pngstruct*)png_get_io_ptr(ps);
|
||||
auto pngdata = (PngStruct*)png_get_io_ptr(ps);
|
||||
::memcpy(data, pngdata->data + pngdata->offset, len);
|
||||
pngdata->offset += len;
|
||||
});
|
||||
|
@ -229,17 +224,16 @@ s32 PS4_SYSV_ABI scePngDecParseHeader(const OrbisPngDecParseParam* param,
|
|||
// info.
|
||||
png_read_info(png_ptr, info_ptr);
|
||||
|
||||
imageInfo->imageWidth = png_get_image_width(png_ptr, info_ptr);
|
||||
imageInfo->imageHeight = png_get_image_height(png_ptr, info_ptr);
|
||||
imageInfo->colorSpace = MapPngColor(png_get_color_type(png_ptr, info_ptr));
|
||||
imageInfo->bitDepth = png_get_bit_depth(png_ptr, info_ptr);
|
||||
imageInfo->imageFlag = 0;
|
||||
imageInfo->image_width = png_get_image_width(png_ptr, info_ptr);
|
||||
imageInfo->image_height = png_get_image_height(png_ptr, info_ptr);
|
||||
imageInfo->color_space = MapPngColor(png_get_color_type(png_ptr, info_ptr));
|
||||
imageInfo->bit_depth = png_get_bit_depth(png_ptr, info_ptr);
|
||||
imageInfo->image_flag = OrbisPngDecImageFlag::None;
|
||||
if (png_get_interlace_type(png_ptr, info_ptr) == 1) {
|
||||
imageInfo->imageFlag |= OrbisPngDecImageFlag::ORBIS_PNG_DEC_IMAGE_FLAG_ADAM7_INTERLACE;
|
||||
imageInfo->image_flag |= OrbisPngDecImageFlag::Adam7Interlace;
|
||||
}
|
||||
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
|
||||
|
||||
imageInfo->imageFlag |= ORBIS_PNG_DEC_IMAGE_FLAG_TRNS_CHUNK_EXIST;
|
||||
imageInfo->image_flag |= OrbisPngDecImageFlag::TrnsChunkExist;
|
||||
}
|
||||
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
|
||||
|
@ -260,8 +254,8 @@ s32 PS4_SYSV_ABI scePngDecQueryMemorySize(const OrbisPngDecCreateParam* param) {
|
|||
LOG_ERROR(Lib_Png, "Invalid attribute! attribute = {}", param->attribute);
|
||||
return ORBIS_PNG_DEC_ERROR_INVALID_ADDR;
|
||||
}
|
||||
if (param->maxImageWidth - 1 > 1000000) {
|
||||
LOG_ERROR(Lib_Png, "Invalid size! width = {}", param->maxImageWidth);
|
||||
if (param->max_image_width - 1 > 1000000) {
|
||||
LOG_ERROR(Lib_Png, "Invalid size! width = {}", param->max_image_width);
|
||||
return ORBIS_PNG_DEC_ERROR_INVALID_SIZE;
|
||||
}
|
||||
return sizeof(PngHandler);
|
||||
|
@ -279,4 +273,4 @@ void RegisterlibScePngDec(Core::Loader::SymbolsResolver* sym) {
|
|||
scePngDecDecodeWithInputControl);
|
||||
};
|
||||
|
||||
} // namespace Libraries::PngDec
|
||||
} // namespace Libraries::PngDec
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "common/enum.h"
|
||||
#include "common/types.h"
|
||||
|
||||
namespace Core::Loader {
|
||||
|
@ -11,59 +12,61 @@ class SymbolsResolver;
|
|||
|
||||
namespace Libraries::PngDec {
|
||||
|
||||
enum OrbisPngDecColorSpace {
|
||||
ORBIS_PNG_DEC_COLOR_SPACE_GRAYSCALE = 2,
|
||||
ORBIS_PNG_DEC_COLOR_SPACE_RGB,
|
||||
ORBIS_PNG_DEC_COLOR_SPACE_CLUT,
|
||||
ORBIS_PNG_DEC_COLOR_SPACE_GRAYSCALE_ALPHA = 18,
|
||||
ORBIS_PNG_DEC_COLOR_SPACE_RGBA
|
||||
enum class OrbisPngDecColorSpace : u16 {
|
||||
Grayscale = 2,
|
||||
Rgb,
|
||||
Clut,
|
||||
GrayscaleAlpha = 18,
|
||||
Rgba,
|
||||
};
|
||||
|
||||
enum OrbisPngDecImageFlag {
|
||||
ORBIS_PNG_DEC_IMAGE_FLAG_ADAM7_INTERLACE = 1,
|
||||
ORBIS_PNG_DEC_IMAGE_FLAG_TRNS_CHUNK_EXIST = 2
|
||||
enum class OrbisPngDecImageFlag : u32 {
|
||||
None = 0,
|
||||
Adam7Interlace = 1,
|
||||
TrnsChunkExist = 2,
|
||||
};
|
||||
DECLARE_ENUM_FLAG_OPERATORS(OrbisPngDecImageFlag)
|
||||
|
||||
enum class OrbisPngDecPixelFormat : u16 {
|
||||
R8G8B8A8 = 0,
|
||||
B8G8R8A8,
|
||||
};
|
||||
|
||||
enum OrbisPngDecPixelFormat {
|
||||
ORBIS_PNG_DEC_PIXEL_FORMAT_R8G8B8A8 = 0,
|
||||
ORBIS_PNG_DEC_PIXEL_FORMAT_B8G8R8A8
|
||||
};
|
||||
|
||||
enum OrbisPngDecAttribute {
|
||||
ORBIS_PNG_DEC_ATTRIBUTE_NONE = 0,
|
||||
ORBIS_PNG_DEC_ATTRIBUTE_BIT_DEPTH_16
|
||||
enum class OrbisPngDecAttribute {
|
||||
None = 0,
|
||||
BitDepth16,
|
||||
};
|
||||
|
||||
struct OrbisPngDecParseParam {
|
||||
const void* pngMemAddr;
|
||||
u32 pngMemSize;
|
||||
const u8* png_mem_addr;
|
||||
u32 png_mem_size;
|
||||
u32 reserved;
|
||||
};
|
||||
|
||||
struct OrbisPngDecImageInfo {
|
||||
u32 imageWidth;
|
||||
u32 imageHeight;
|
||||
u16 colorSpace;
|
||||
u16 bitDepth;
|
||||
u32 imageFlag;
|
||||
u32 image_width;
|
||||
u32 image_height;
|
||||
OrbisPngDecColorSpace color_space;
|
||||
u16 bit_depth;
|
||||
OrbisPngDecImageFlag image_flag;
|
||||
};
|
||||
|
||||
struct OrbisPngDecCreateParam {
|
||||
u32 thisSize;
|
||||
u32 this_size;
|
||||
u32 attribute;
|
||||
u32 maxImageWidth;
|
||||
u32 max_image_width;
|
||||
};
|
||||
|
||||
typedef void* OrbisPngDecHandle;
|
||||
using OrbisPngDecHandle = void*;
|
||||
|
||||
struct OrbisPngDecDecodeParam {
|
||||
const void* pngMemAddr;
|
||||
void* imageMemAddr;
|
||||
u32 pngMemSize;
|
||||
u32 imageMemSize;
|
||||
u16 pixelFormat;
|
||||
u16 alphaValue;
|
||||
u32 imagePitch;
|
||||
const u8* png_mem_addr;
|
||||
u8* image_mem_addr;
|
||||
u32 png_mem_size;
|
||||
u32 image_mem_size;
|
||||
OrbisPngDecPixelFormat pixel_format;
|
||||
u16 alpha_value;
|
||||
u32 image_pitch;
|
||||
};
|
||||
|
||||
s32 PS4_SYSV_ABI scePngDecCreate(const OrbisPngDecCreateParam* param, void* memoryAddress,
|
||||
|
@ -77,4 +80,4 @@ s32 PS4_SYSV_ABI scePngDecParseHeader(const OrbisPngDecParseParam* param,
|
|||
s32 PS4_SYSV_ABI scePngDecQueryMemorySize(const OrbisPngDecCreateParam* param);
|
||||
|
||||
void RegisterlibScePngDec(Core::Loader::SymbolsResolver* sym);
|
||||
} // namespace Libraries::PngDec
|
||||
} // namespace Libraries::PngDec
|
||||
|
|
17
src/core/libraries/libpng/pngdec_error.h
Normal file
17
src/core/libraries/libpng/pngdec_error.h
Normal file
|
@ -0,0 +1,17 @@
|
|||
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/libraries/error_codes.h"
|
||||
|
||||
// PngDec library
|
||||
constexpr int ORBIS_PNG_DEC_ERROR_INVALID_ADDR = 0x80690001;
|
||||
constexpr int ORBIS_PNG_DEC_ERROR_INVALID_SIZE = 0x80690002;
|
||||
constexpr int ORBIS_PNG_DEC_ERROR_INVALID_PARAM = 0x80690003;
|
||||
constexpr int ORBIS_PNG_DEC_ERROR_INVALID_HANDLE = 0x80690004;
|
||||
constexpr int ORBIS_PNG_DEC_ERROR_INVALID_WORK_MEMORY = 0x80690005;
|
||||
constexpr int ORBIS_PNG_DEC_ERROR_INVALID_DATA = 0x80690010;
|
||||
constexpr int ORBIS_PNG_DEC_ERROR_UNSUPPORT_DATA = 0x80690011;
|
||||
constexpr int ORBIS_PNG_DEC_ERROR_DECODE_ERROR = 0x80690012;
|
||||
constexpr int ORBIS_PNG_DEC_ERROR_FATAL = 0x80690020;
|
Loading…
Add table
Add a link
Reference in a new issue