From 83d139d4b5af1177371be7bd50e085817ebaa8ce Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 6 Mar 2014 10:55:14 +0100 Subject: [PATCH] Support loading of 24 bit png. --- src/texture.cpp | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/src/texture.cpp b/src/texture.cpp index 450010f..e8b2683 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -38,6 +38,11 @@ namespace cloonel { GraphicFormat_Png }; + enum PixelFormat { + PixelFormat_RGB_24, + PixelFormat_RGBA_32 + }; + enum ColorChannelMask { #if __BYTE_ORDER == __BIG_ENDIAN ColorChannelMask_Red = 0xff000000, @@ -68,7 +73,7 @@ namespace cloonel { ///---------------------------------------------------------------------- ///---------------------------------------------------------------------- - SDL_Surface* SurfaceFromPngRGBA (ushort2 parSize, int parBpp, const png_structp& parPngPtr, const png_infop& parInfoPtr, std::vector& parBuff) { + SDL_Surface* SurfaceFromPng (PixelFormat parPixelFormat, ushort2 parSize, const png_structp& parPngPtr, const png_infop& parInfoPtr, std::vector& parBuff) { const png_size_t stride = png_get_rowbytes(parPngPtr, parInfoPtr); assert(stride > 0); @@ -79,16 +84,37 @@ namespace cloonel { png_read_row(parPngPtr, imagePtr + stride * y, nullptr); } + int bpp; + uint32_t redMask, greenMask, blueMask, alphaMask; + switch (parPixelFormat) { + case PixelFormat_RGB_24: + bpp = 24; + redMask = ColorChannelMask_Red; + greenMask = ColorChannelMask_Green; + blueMask = ColorChannelMask_Blue; + alphaMask = 0; + break; + case PixelFormat_RGBA_32: + bpp = 32; + redMask = ColorChannelMask_Red; + greenMask = ColorChannelMask_Green; + blueMask = ColorChannelMask_Blue; + alphaMask = ColorChannelMask_Alpha; + break; + default: + assert(false); + } + SDL_Surface* const retSurf = SDL_CreateRGBSurfaceFrom( imagePtr, parSize.x(), parSize.y(), - parBpp, + bpp, static_cast(stride), - ColorChannelMask_Red, - ColorChannelMask_Green, - ColorChannelMask_Blue, - ColorChannelMask_Alpha + redMask, + greenMask, + blueMask, + alphaMask ); return retSurf; } @@ -168,12 +194,13 @@ namespace cloonel { SDL_Surface* retSurf; switch (colorType) { case PNG_COLOR_TYPE_RGB: - retSurf = nullptr; //SurfaceFromPngRGB(); - assert(false); //not implemented + assert(3 * bitDepth == 24); + retSurf = SurfaceFromPng(PixelFormat_RGB_24, ushort2(static_cast(width), static_cast(height)), pngptr, infoptr, parBuff); break; case PNG_COLOR_TYPE_RGB_ALPHA: - retSurf = SurfaceFromPngRGBA(ushort2(static_cast(width), static_cast(height)), bitDepth * 4, pngptr, infoptr, parBuff); + assert(bitDepth * 4 == 32); + retSurf = SurfaceFromPng(PixelFormat_RGBA_32, ushort2(static_cast(width), static_cast(height)), pngptr, infoptr, parBuff); break; default: