PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Other image format than bmp?

Hello,

I was using Diesel, and I'm switching to your API...
We're a research center and can't afford their licence...
You're doing a great job. Congratulations.

I was wondering if you planned to add other image format like jpg or gif in your API, or if someone did.

And, do you think it's possible to play video in a surface? I don't think it's the aim of it, but it would help me in doing my project... :wink:

jul'
Hi,

Different formats are most definitely planned (using the pImageType parameter in CreateSurface). Right now JPG support is at the top of my list.

/Johan
I wrote a PNG loader. I'm using libpng (Yes, it's amazingly scary looking, but I get zero memleaks so I guess it works) for the PNG image decompression and loading into a locked GapiSurface.

The function is about 150 lines, but if there's interest I could upload the source.
Heya,

If you can stand the thought of me shuffling your code right into GapiDraw then please upload it! :)

It's about time GapiDraw supported more image formats, and right now I really could use any help I can get (I am reading through the entire blit core assembler output two or three times more just to make sure every operation performs in an optimal way). I have actually rewritten basically every blit operation from scratch, and expanded each function as many times as possible to ensure optimization.

For example, the standard BltFast operation today has more than 20 different internal paths depending on what options are set (memcpy as block if no options and target is not windowed display and surfaces are of matching size, memcpy as rows if no options are set, 8 fast blt operations, 8 blt fx operations)... It takes a while to go through all this code, but in the end it will be as fast as it possible can get.

/Johan
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;
}