Skip to content

Commit

Permalink
PSP: Avoid parsing the PNG headers twice
Browse files Browse the repository at this point in the history
This avoids rewinding the stream which is always bad when loading the
PNG from a Zip archive.
  • Loading branch information
lephilousophe committed Dec 21, 2024
1 parent 54cb0d1 commit 5149559
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
24 changes: 11 additions & 13 deletions backends/platform/psp/png_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@

#include "backends/platform/psp/trace.h"

PngLoader::~PngLoader() {
if (!_pngPtr) {
return;
}
png_destroy_read_struct(&_pngPtr, &_infoPtr, nullptr);
}

PngLoader::Status PngLoader::allocate() {
DEBUG_ENTER_FUNC();

Expand Down Expand Up @@ -76,9 +83,8 @@ PngLoader::Status PngLoader::allocate() {

bool PngLoader::load() {
DEBUG_ENTER_FUNC();
// Try to load the image
_file.seek(0); // Go back to start

// Try to really load the image
if (!loadImageIntoBuffer()) {
PSP_DEBUG_PRINT("failed to load image\n");
return false;
Expand Down Expand Up @@ -121,15 +127,11 @@ bool PngLoader::basicImageLoad() {

_infoPtr = png_create_info_struct(_pngPtr);
if (!_infoPtr) {
png_destroy_read_struct(&_pngPtr, nullptr, nullptr);
return false;
}
// Set the png lib to use our read function
png_set_read_fn(_pngPtr, &_file, libReadFunc);

unsigned int sig_read = 0;

png_set_sig_bytes(_pngPtr, sig_read);
png_read_info(_pngPtr, _infoPtr);
int interlaceType;
png_get_IHDR(_pngPtr, _infoPtr, (png_uint_32 *)&_width, (png_uint_32 *)&_height, &_bitDepth,
Expand All @@ -154,7 +156,6 @@ bool PngLoader::findImageDimensions() {
bool status = basicImageLoad();

PSP_DEBUG_PRINT("width[%d], height[%d], paletteSize[%d], bitDepth[%d], channels[%d], rowBytes[%d]\n", _width, _height, _paletteSize, _bitDepth, _channels, png_get_rowbytes(_pngPtr, _infoPtr));
png_destroy_read_struct(&_pngPtr, &_infoPtr, nullptr);
return status;
}

Expand All @@ -164,10 +165,9 @@ bool PngLoader::findImageDimensions() {
bool PngLoader::loadImageIntoBuffer() {
DEBUG_ENTER_FUNC();

if (!basicImageLoad()) {
png_destroy_read_struct(&_pngPtr, &_infoPtr, nullptr);
return false;
}
// Everything has already been set up in allocate
assert(_pngPtr);

png_set_strip_16(_pngPtr); // Strip off 16 bit channels in case they occur

if (_paletteSize) {
Expand Down Expand Up @@ -206,7 +206,6 @@ bool PngLoader::loadImageIntoBuffer() {

unsigned char *line = (unsigned char*) malloc(rowBytes);
if (!line) {
png_destroy_read_struct(&_pngPtr, nullptr, nullptr);
PSP_ERROR("Couldn't allocate line\n");
return false;
}
Expand All @@ -217,7 +216,6 @@ bool PngLoader::loadImageIntoBuffer() {
}
free(line);
png_read_end(_pngPtr, _infoPtr);
png_destroy_read_struct(&_pngPtr, &_infoPtr, nullptr);

return true;
}
1 change: 1 addition & 0 deletions backends/platform/psp/png_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class PngLoader {
_width(0), _height(0), _paletteSize(0),
_bitDepth(0), _sizeBy(sizeBy), _pngPtr(0),
_infoPtr(0), _colorType(0), _channels(0) {}
~PngLoader();

PngLoader::Status allocate();
bool load();
Expand Down

0 comments on commit 5149559

Please sign in to comment.