PNG loading fixed.

This commit is contained in:
King_DuckZ 2014-02-21 22:31:16 +01:00
parent 28bd73a1f7
commit 05a8d0188c

View file

@ -7,6 +7,8 @@
#include <cassert> #include <cassert>
#include <ciso646> #include <ciso646>
#include <png.h> #include <png.h>
#include <endian.h>
#include <vector>
#define lengthof(a) (static_cast<int32_t>(sizeof(a)) / static_cast<int32_t>(sizeof(a[0]))) #define lengthof(a) (static_cast<int32_t>(sizeof(a)) / static_cast<int32_t>(sizeof(a[0])))
@ -18,12 +20,12 @@ namespace cloonel {
}; };
enum ColorChannelMask { enum ColorChannelMask {
#if SDL_BYTE_ORDER == SDL_BIGENDIAN #if __BYTE_ORDER == __BIG_ENDIAN
ColorChannelMask_Red = 0xff000000, ColorChannelMask_Red = 0xff000000,
ColorChannelMask_Green = 0xff0000, ColorChannelMask_Green = 0xff0000,
ColorChannelMask_Blue = 0xff00, ColorChannelMask_Blue = 0xff00,
ColorChannelMask_Alpha = 0xff ColorChannelMask_Alpha = 0xff
#elif SDL_BYTE_ORDER == SDL_LITTLEENDIAN #elif __BYTE_ORDER == __LITTLE_ENDIAN
ColorChannelMask_Red = 0xff, ColorChannelMask_Red = 0xff,
ColorChannelMask_Green = 0xff00, ColorChannelMask_Green = 0xff00,
ColorChannelMask_Blue = 0xff0000, ColorChannelMask_Blue = 0xff0000,
@ -47,12 +49,13 @@ namespace cloonel {
///---------------------------------------------------------------------- ///----------------------------------------------------------------------
///---------------------------------------------------------------------- ///----------------------------------------------------------------------
SDL_Surface* SurfaceFromPngRGBA (ushort2 parSize, int parBpp, const png_structp& parPngPtr, const png_infop& parInfoPtr) { SDL_Surface* SurfaceFromPngRGBA (ushort2 parSize, int parBpp, const png_structp& parPngPtr, const png_infop& parInfoPtr, std::vector<uint8_t>& parBuff) {
const png_size_t stride = png_get_rowbytes(parPngPtr, parInfoPtr); const png_size_t stride = png_get_rowbytes(parPngPtr, parInfoPtr);
assert(stride > 0); assert(stride > 0);
std::unique_ptr<uint8_t[]> image(new uint8_t[stride * parSize.y()]); parBuff.resize(stride * parSize.y());
uint8_t* const imagePtr = image.get(); uint8_t* const imagePtr = parBuff.data();
for (uint16_t y = 0; y < parSize.y(); ++y) { for (uint16_t y = 0; y < parSize.y(); ++y) {
png_read_row(parPngPtr, imagePtr + stride * y, nullptr); png_read_row(parPngPtr, imagePtr + stride * y, nullptr);
} }
@ -106,7 +109,7 @@ namespace cloonel {
///---------------------------------------------------------------------- ///----------------------------------------------------------------------
///http://blog.hammerian.net/2009/reading-png-images-from-memory/ ///http://blog.hammerian.net/2009/reading-png-images-from-memory/
///---------------------------------------------------------------------- ///----------------------------------------------------------------------
SDL_Surface* LoadNewPngSurface (const std::string& parPath) { SDL_Surface* LoadNewPngSurface (const std::string& parPath, std::vector<uint8_t>& parBuff) {
PhysicsFSFile rawfile(parPath.c_str(), PhysicsFSFile::OpenMode_Read, "graphics"); PhysicsFSFile rawfile(parPath.c_str(), PhysicsFSFile::OpenMode_Read, "graphics");
unsigned char header[8]; unsigned char header[8];
assert(rawfile.IsOpen()); assert(rawfile.IsOpen());
@ -151,7 +154,7 @@ namespace cloonel {
break; break;
case PNG_COLOR_TYPE_RGB_ALPHA: case PNG_COLOR_TYPE_RGB_ALPHA:
retSurf = SurfaceFromPngRGBA(ushort2(static_cast<uint8_t>(width), static_cast<uint8_t>(height)), bitDepth * 4, pngptr, infoptr); retSurf = SurfaceFromPngRGBA(ushort2(static_cast<uint8_t>(width), static_cast<uint8_t>(height)), bitDepth * 4, pngptr, infoptr, parBuff);
break; break;
default: default:
@ -196,9 +199,10 @@ namespace cloonel {
Destroy(); Destroy();
SDL_Surface* surf = nullptr; SDL_Surface* surf = nullptr;
std::vector<uint8_t> memBuff;
switch (fmt) { switch (fmt) {
case GraphicFormat_Png: case GraphicFormat_Png:
surf = LoadNewPngSurface(m_path.c_str()); surf = LoadNewPngSurface(m_path.c_str(), memBuff);
break; break;
default: default: