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
|
GraphicFormat_Png
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum PixelFormat {
|
||||||
|
PixelFormat_RGB_24,
|
||||||
|
PixelFormat_RGBA_32
|
||||||
|
};
|
||||||
|
|
||||||
enum ColorChannelMask {
|
enum ColorChannelMask {
|
||||||
#if __BYTE_ORDER == __BIG_ENDIAN
|
#if __BYTE_ORDER == __BIG_ENDIAN
|
||||||
ColorChannelMask_Red = 0xff000000,
|
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);
|
const png_size_t stride = png_get_rowbytes(parPngPtr, parInfoPtr);
|
||||||
assert(stride > 0);
|
assert(stride > 0);
|
||||||
|
|
||||||
|
@ -79,16 +84,37 @@ namespace cloonel {
|
||||||
png_read_row(parPngPtr, imagePtr + stride * y, nullptr);
|
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(
|
SDL_Surface* const retSurf = SDL_CreateRGBSurfaceFrom(
|
||||||
imagePtr,
|
imagePtr,
|
||||||
parSize.x(),
|
parSize.x(),
|
||||||
parSize.y(),
|
parSize.y(),
|
||||||
parBpp,
|
bpp,
|
||||||
static_cast<int>(stride),
|
static_cast<int>(stride),
|
||||||
ColorChannelMask_Red,
|
redMask,
|
||||||
ColorChannelMask_Green,
|
greenMask,
|
||||||
ColorChannelMask_Blue,
|
blueMask,
|
||||||
ColorChannelMask_Alpha
|
alphaMask
|
||||||
);
|
);
|
||||||
return retSurf;
|
return retSurf;
|
||||||
}
|
}
|
||||||
|
@ -168,12 +194,13 @@ namespace cloonel {
|
||||||
SDL_Surface* retSurf;
|
SDL_Surface* retSurf;
|
||||||
switch (colorType) {
|
switch (colorType) {
|
||||||
case PNG_COLOR_TYPE_RGB:
|
case PNG_COLOR_TYPE_RGB:
|
||||||
retSurf = nullptr; //SurfaceFromPngRGB();
|
assert(3 * bitDepth == 24);
|
||||||
assert(false); //not implemented
|
retSurf = SurfaceFromPng(PixelFormat_RGB_24, ushort2(static_cast<uint8_t>(width), static_cast<uint8_t>(height)), pngptr, infoptr, parBuff);
|
||||||
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, 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;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
Loading…
Reference in a new issue