mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-10-15 15:30:29 +02:00
Deflate, zlib and gzip streams go through rinflate and rdeflate, zstd through rzstd, CHD images through rchd, zip archives through rzip_archive and PNG through rpng. The bundled libraries, their build glue and the system-library options are gone; adler32 and uncompress keep zlib's ABI for the prebuilt FFmpeg archives that still call them. libretro-common is one CMake library that the core, the headless tool and the unit tests all link. The reads take shorter paths on the way. CHD decodes straight from the loader's reads and a range of blocks at a time, and a file read at any alignment decodes into place; CSO keeps one inflate stream instead of one per block; a plain ISO serves a file read with one read of its exact bytes; a stored zip member is read in place and a deflated one through restart points rather than a copy of the whole image in memory; zip members decode into the caller's buffer; sequential reads through the frontend's VFS skip the seek, which also keeps its buffering. libretro/test reads one image as ISO, CSO, CHD and zip through the block devices, boots each, and checks the codecs against Python-made vectors; a CI job runs it.
152 lines
3.6 KiB
C++
152 lines
3.6 KiB
C++
#include <cstring>
|
|
#include <cstdint>
|
|
#include <encodings/rzstd.h>
|
|
|
|
#include "Common/Log.h"
|
|
#include "Common/Render/TextureAtlas.h"
|
|
|
|
class ByteReader {
|
|
public:
|
|
ByteReader(const uint8_t *data, size_t size) : data_(data), offset_(0), size_(size) {}
|
|
|
|
template<class T>
|
|
T Read() {
|
|
_dbg_assert_(offset_ + sizeof(T) <= size_);
|
|
T x;
|
|
memcpy(&x, data_ + offset_, sizeof(T));
|
|
offset_ += sizeof(T);
|
|
return x;
|
|
}
|
|
|
|
template<class T>
|
|
void ReadInto(T *t) {
|
|
_dbg_assert_(offset_ + sizeof(T) <= size_);
|
|
memcpy(t, data_ + offset_, sizeof(T));
|
|
offset_ += sizeof(T);
|
|
}
|
|
|
|
template<class T>
|
|
T *ReadMultipleAlloc(size_t count, bool compressed) {
|
|
T *t = new T[count];
|
|
if (!compressed) {
|
|
_dbg_assert_(offset_ + sizeof(T) * count <= size_);
|
|
memcpy(t, data_ + offset_, sizeof(T) * count);
|
|
offset_ += sizeof(T) * count;
|
|
} else {
|
|
_dbg_assert_(offset_ + sizeof(uint32_t) <= size_);
|
|
uint32_t compressed_size = 0;
|
|
memcpy(&compressed_size, data_ + offset_, sizeof(uint32_t));
|
|
offset_ += sizeof(uint32_t);
|
|
|
|
_dbg_assert_(offset_ + compressed_size <= size_);
|
|
size_t wrote = 0;
|
|
if (rzstd_decode((uint8_t *)t, sizeof(T) * count, data_ + offset_, compressed_size, &wrote) != RZSTD_PROCESS_END)
|
|
memset((void *)t, 0, sizeof(T) * count);
|
|
offset_ += compressed_size;
|
|
}
|
|
return t;
|
|
}
|
|
|
|
private:
|
|
const uint8_t *data_;
|
|
size_t offset_;
|
|
size_t size_;
|
|
};
|
|
|
|
void Atlas::Clear() {
|
|
num_images = 0;
|
|
num_fonts = 0;
|
|
delete[] images;
|
|
delete[] fonts;
|
|
images = nullptr;
|
|
fonts = nullptr;
|
|
}
|
|
|
|
bool Atlas::LoadMeta(const uint8_t *data, size_t data_size) {
|
|
ByteReader reader(data, data_size);
|
|
|
|
AtlasHeader header = reader.Read<AtlasHeader>();
|
|
if (header.magic != ATLAS_MAGIC) {
|
|
return false;
|
|
}
|
|
|
|
Clear();
|
|
|
|
num_images = header.numImages;
|
|
num_fonts = header.numFonts;
|
|
|
|
images = reader.ReadMultipleAlloc<AtlasImage>(num_images, header.version >= 1);
|
|
fonts = new AtlasFont[num_fonts];
|
|
for (int i = 0; i < num_fonts; i++) {
|
|
AtlasFontHeader font_header = reader.Read<AtlasFontHeader>();
|
|
fonts[i].padding = font_header.padding;
|
|
fonts[i].height = font_header.height;
|
|
fonts[i].ascend = font_header.ascend;
|
|
fonts[i].distslope = font_header.distslope;
|
|
fonts[i].numRanges = font_header.numRanges;
|
|
fonts[i].numChars = font_header.numChars;
|
|
fonts[i].ranges = reader.ReadMultipleAlloc<AtlasCharRange>(font_header.numRanges, header.version >= 1);
|
|
fonts[i].charData = reader.ReadMultipleAlloc<AtlasChar>(font_header.numChars, header.version >= 1);
|
|
memcpy(fonts[i].name, font_header.name, sizeof(font_header.name));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
const AtlasFont *Atlas::getFont(FontID id) const {
|
|
if (id.isInvalid())
|
|
return nullptr;
|
|
|
|
for (int i = 0; i < num_fonts; i++) {
|
|
if (!strcmp(id.id, fonts[i].name))
|
|
return &fonts[i];
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
const AtlasImage *Atlas::getImage(ImageID name) const {
|
|
if (name.isInvalid())
|
|
return nullptr;
|
|
|
|
for (int i = 0; i < num_images; i++) {
|
|
if (name.id == images[i].name)
|
|
return &images[i];
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
bool Atlas::measureImage(ImageID id, float *w, float *h) const {
|
|
const AtlasImage *image = getImage(id);
|
|
if (image) {
|
|
*w = (float)image->w;
|
|
*h = (float)image->h;
|
|
return true;
|
|
} else {
|
|
*w = 0.0f;
|
|
*h = 0.0f;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
const AtlasChar *AtlasFont::getChar(int utf32) const {
|
|
for (int i = 0; i < numRanges; i++) {
|
|
if (utf32 >= ranges[i].start && utf32 < ranges[i].end) {
|
|
const AtlasChar *c = &charData[ranges[i].result_index + utf32 - ranges[i].start];
|
|
if (c->ex == 0 && c->ey == 0)
|
|
return nullptr;
|
|
else
|
|
return c;
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
Atlas::~Atlas() {
|
|
delete[] images;
|
|
delete[] fonts;
|
|
}
|
|
|
|
AtlasFont::~AtlasFont() {
|
|
delete[] ranges;
|
|
delete[] charData;
|
|
}
|