Support loading of 24 bit png.
This commit is contained in:
parent
d27b5773ca
commit
83d139d4b5
1 changed files with 36 additions and 9 deletions
|
@ -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<uint8_t>& parBuff) {
|
||||
SDL_Surface* SurfaceFromPng (PixelFormat parPixelFormat, ushort2 parSize, const png_structp& parPngPtr, const png_infop& parInfoPtr, std::vector<uint8_t>& 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<int>(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<uint8_t>(width), static_cast<uint8_t>(height)), pngptr, infoptr, parBuff);
|
||||
break;
|
||||
|
||||
case PNG_COLOR_TYPE_RGB_ALPHA:
|
||||
retSurf = SurfaceFromPngRGBA(ushort2(static_cast<uint8_t>(width), static_cast<uint8_t>(height)), bitDepth * 4, pngptr, infoptr, parBuff);
|
||||
assert(bitDepth * 4 == 32);
|
||||
retSurf = SurfaceFromPng(PixelFormat_RGBA_32, ushort2(static_cast<uint8_t>(width), static_cast<uint8_t>(height)), pngptr, infoptr, parBuff);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
|
Loading…
Reference in a new issue