Eeek! Before GapiDraw came out I was working on my own blitters and started to do the same. I don't envy ya.
Here's the PNG loader. I should note, I use function scope objects a lot when doing dynamic mem allocs. VC has a bug however where it can only handle 1 function scope object in a function.
Also, you'll need to port libpng and zlib over (my port has a lot of it removed and is rather hacked to get the features I wanted out of it).
With out further ado:
- Code:
// LoadPNG
// NOTE: Previous contents of surface destroyed. Size = to png being read
LIBRARY_ERR Graphics2D::LoadPNG( const TCHAR *FileName,
CGapiSurface *surface )
{
//Check params
if( !FileName || !surface )
{
return LERR_INVALIDPARAMS;
}
//Internal PNG file object
struct _PNGFile
{
HANDLE hMyPic;
png_structp png_ptr;
png_infop info_ptr;
png_infop end_info;
//Constructor
_PNGFile():hMyPic(NULL), png_ptr(0), info_ptr(0), end_info(0){}
//Destructor
~_PNGFile()
{
//Close the file is handle is valid
if( hMyPic != NULL && hMyPic != INVALID_HANDLE_VALUE )
{
CloseHandle( hMyPic );
}
//Release PNG memory if PNG structure was created
if( png_ptr )
{
png_destroy_read_struct(&png;_ptr, &info;_ptr, &end;_info);
}
}
};
//Instantiate PNG File object
_PNGFile file;
//Open File
file.hMyPic = CreateFile( FileName, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
NULL);
if( file.hMyPic == NULL || file.hMyPic == INVALID_HANDLE_VALUE )
{
return LERR_FILENOTFOUND;
}
//Initialize PNG structures
file.png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING,
(png_voidp)NULL, NULL, NULL);
if( !file.png_ptr )
{
return LERR_NOFREEMEMORY;
}
//Get Info Pointers
file.info_ptr = png_create_info_struct( file.png_ptr );
if( !file.info_ptr )
{
return LERR_NOFREEMEMORY;
}
file.end_info = png_create_info_struct( file.png_ptr );
if( !file.end_info )
{
return LERR_NOFREEMEMORY;
}
//Setup the File IO functions for libpng
png_init_io( file.png_ptr, file.hMyPic );
//Read PNG
png_read_png( file.png_ptr, file.info_ptr,
PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_EXPAND, NULL);
//Read Image Data
png_bytep *row_pointers, *image_bits;
row_pointers = png_get_rows( file.png_ptr, file.info_ptr );
if( !row_pointers )
{
return LERR_PNGREADERROR;
}
image_bits = row_pointers;
//Setup Blit Variables
BYTE PixelRed = 0;
BYTE PixelGreen = 0;
BYTE PixelBlue = 0;
WORD PixelCol = 0;
RGB rgb = { 0,0,0 };
//Surface Props
DWORD ImageWidth = png_get_image_width( file.png_ptr,
file.info_ptr );
DWORD ImageHeight = png_get_image_height( file.png_ptr,
file.info_ptr );
//Create Surface
if( FAILED( surface->CreateSurface( GDCREATESURFACE_CLEAR,
ImageWidth, ImageHeight ) ) )
{
return LERR_SURFACENOTCREATED;
}
//Lock Surface
GDSURFACEDESC SurfDesc;
if( FAILED( surface->Lock( NULL, &SurfDesc; ) ) )
{
return LERR_NOFREEMEMORY;
}
//Get Surface
WORD *pDestLine = SurfDesc.pSurface;
WORD *pDest;
long Pitch = SurfDesc.lPitch >> 1;
//Loop Sentinals
DWORD y, x, j;
//Blit
for( y = 0; y < ImageHeight; ++y )
{
pDest = pDestLine;
j = 0;
for( x = 0; x < ImageWidth; ++x )
{
//Get Color from Image
rgb.red = image_bits[y][j++];
rgb.green = image_bits[y][j++];
rgb.blue = image_bits[y][j++];
//Pack Color
if( GDPIXELFORMAT_565 == SurfDesc.dwPixelFormat )
{
RGBPack565( &rgb, PixelCol );
}
else
{
RGBPack555( &rgb, PixelCol );
}
//Copy packed color to Dest
*pDest = PixelCol;
//Increment to next pixel
++pDest;
}
//Increment to next line
pDestLine += Pitch;
}
//Unlock Surface
if( FAILED( surface->Unlock( NULL ) ) )
{
return LERR_ERRUNKNOWN;
}
return LERR_OK;
}
Graphics2D is just a namespace.
RGB is the following struct
- Code:
struct RGB
{
BYTE red;
BYTE green;
BYTE blue;
};
And if you need them, here are my pixel packers. Note: I haven't tested the 555 ones at all, though I think they work (atleast they do on paper).
- Code:
//RGBPack565 - Packs an RGB value into 565 pixel format
inline void RGBPack565( RGB *rgb, WORD &packed; )
{
packed = (WORD)( ((rgb->red & 0xf8) << 8) |
((rgb->green & 0xfc) << 3) |
((rgb->blue & 0xf8) >> 3) );
}
//RGBUnpack565 - Unpacks an RGB value from 565 pixel format
// NOTE: Unpacked values are not = to original RGB components
inline void RGBUnpack565( WORD &packed;, RGB &unpacked; )
{
unpacked.red = ((packed >> 11) & 0x1f) << 3;
unpacked.green = ((packed >> 5) & 0x3f) << 2;
unpacked.blue = (packed & 0x1f) << 3;
}
//RGBPack555 - Packs an RGB value into 555 pixel format
inline void RGBPack555( RGB *rgb, WORD &packed )
{
packed = (WORD)( ((rgb->red & 0xf8) << 7) |
((rgb->green & 0xf8) << 2) |
((rgb->blue & 0xf8) >> 3) );
}
//RGBUnpack555 - Unpacks an RGB value from 555 pixel format
// NOTE: Unpacked values are not = to original RGB components
inline void RGBUnpack555( WORD &packed;, RGB &unpacked; )
{
unpacked.red = ((packed >> 10) & 0x1f) << 3;
unpacked.green = ((packed >> 5) & 0x1f) << 3;
unpacked.blue = (packed & 0x1f) << 3;
}