FORUM CLOSED, PLEASE REGISTER AT FORUM.SIO2INTERACTIVE.COM
Would you like to react to this message? Create an account in a few clicks or log in to continue.

PNG Loading

3 posters

Go down

PNG Loading Empty PNG Loading

Post  zzajin Thu Nov 13, 2008 4:03 pm

I had this code which load pngs in image.cc in my project for awhile. You will also need to add CoreGraphics framework to the project. It's mostly code from Crash Landing sample code. I never got the conversion at the end working however. I'm not asking it to be added to SIO2, but preempting any request for PNG support Wink

Perhaps it will be of use to someone.


Code:


void sio2ImageLoadPNG( SIO2image  *_SIO2image,
                      SIO2stream *_SIO2stream )
{
 
    //get the png data
    CGImageRef image;
    CGDataProviderRef provider;
    CFDataRef png_data = CFDataCreate (NULL,_SIO2stream->buf,_SIO2stream->size);
    provider = CGDataProviderCreateWithCFData (png_data);
    CFRelease (png_data);   
    image =  CGImageCreateWithPNGDataProvider( provider,NULL,NO,kCGRenderingIntentDefault);
   
    if(image == NULL) {
        NSLog(@"Image is Null");
        return;
    }

   
   
    //from crash landing
    NSUInteger                width,
    height,
    i;
    CGContextRef            context = nil;
    CGColorSpaceRef            colorSpace;
    void*                    data = nil;
    void*                    tempData;
    unsigned int*            inPixel32;
    unsigned short*            outPixel16;
    BOOL                    hasAlpha;
    CGImageAlphaInfo        info;
    CGAffineTransform        transform;
    CGSize                    imageSize;
    Texture2DPixelFormat    pixelFormat;
    BOOL                    sizeToFit = NO;
   
   
   
    info = CGImageGetAlphaInfo(image);
    hasAlpha = ((info == kCGImageAlphaPremultipliedLast) || (info == kCGImageAlphaPremultipliedFirst) || (info == kCGImageAlphaLast) || (info == kCGImageAlphaFirst) ? YES : NO);
    if(CGImageGetColorSpace(image)) {
        if(hasAlpha)
            pixelFormat = kTexture2DPixelFormat_RGBA8888;
        else
            pixelFormat = kTexture2DPixelFormat_RGB565;
    } else  //NOTE: No colorspace means a mask image
        pixelFormat = kTexture2DPixelFormat_A8;
   
   
    imageSize = CGSizeMake(CGImageGetWidth(image), CGImageGetHeight(image));
    transform = CGAffineTransformIdentity;
   
    width = imageSize.width;
   
    if((width != 1) && (width & (width - 1))) {
        i = 1;
        while((sizeToFit ? 2 * i : i) < width)
            i *= 2;
        width = i;
    }
    height = imageSize.height;
    if((height != 1) && (height & (height - 1))) {
        i = 1;
        while((sizeToFit ? 2 * i : i) < height)
            i *= 2;
        height = i;
    }
    int kMaxTextureSize    = 1024;
    while((width > kMaxTextureSize) || (height > kMaxTextureSize)) {
        width /= 2;
        height /= 2;
        transform = CGAffineTransformScale(transform, 0.5, 0.5);
        imageSize.width *= 0.5;
        imageSize.height *= 0.5;
    }
   
    switch(pixelFormat) {       
        case kTexture2DPixelFormat_RGBA8888:
            colorSpace = CGColorSpaceCreateDeviceRGB();
            _SIO2image->tex = ( unsigned char * )malloc(height * width * 4);
            context = CGBitmapContextCreate(_SIO2image->tex, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
            CGColorSpaceRelease(colorSpace);
            _SIO2image->bits  = 4;
            break;
        case kTexture2DPixelFormat_RGB565:
            colorSpace = CGColorSpaceCreateDeviceRGB();
            _SIO2image->tex = ( unsigned char * )malloc(height * width * 4);
            context = CGBitmapContextCreate(_SIO2image->tex, width, height, 8, 4 * width, colorSpace, kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Big);
            CGColorSpaceRelease(colorSpace);
            _SIO2image->bits  = 4;
            break;
           
        case kTexture2DPixelFormat_A8:
            _SIO2image->tex = ( unsigned char * )malloc(height * width);
            context = CGBitmapContextCreate(_SIO2image->tex, width, height, 8, width, NULL, kCGImageAlphaOnly);
            _SIO2image->bits  = 1;
            break;               
        default:
            NSLog(@"Invalid pixel format");
    }
   
   
    CGContextClearRect(context, CGRectMake(0, 0, width, height));
    CGContextTranslateCTM(context, 0, height - imageSize.height);
   
    if(!CGAffineTransformIsIdentity(transform))
        CGContextConcatCTM(context, transform);
    CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image);
    //Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGGBBBBB"
   
   
    if(pixelFormat == kTexture2DPixelFormat_RGB565) {
       
        /*
        tempData = malloc(height * width * 2);
        inPixel32 = (unsigned int*)_SIO2image->tex;
        outPixel16 = (unsigned short*)tempData;
        for(i = 0; i < width * height; ++i, ++inPixel32)
            *outPixel16++ = ((((*inPixel32 >> 0) & 0xFF) >> 3) << 11) | ((((*inPixel32 >> 8) & 0xFF) >> 2) << 5) | ((((*inPixel32 >> 16) & 0xFF) >> 3) << 0);
        */
    }
   
    CGContextRelease(context);
    //end crash landing
       
    _SIO2image->width  = width;
    _SIO2image->height = height;

    sio2ImageRGBAtoBGRA( _SIO2image );
}



You'll need this too


Code:

typedef enum {
   kTexture2DPixelFormat_Automatic = 0,
   kTexture2DPixelFormat_RGBA8888,
   kTexture2DPixelFormat_RGB565,
   kTexture2DPixelFormat_A8,
} Texture2DPixelFormat;


zzajin

Posts : 81
Join date : 2008-10-14

Back to top Go down

PNG Loading Empty Re: PNG Loading

Post  sio2interactive Thu Nov 13, 2008 4:19 pm

Good stuff, but I would prefer if the implementation was coming from libpng... Of course SIO2 is mainly build for iPhone but Im trying to keep the implementation as "cross-platform" as possible... So basically only the input system have to be changed and SIO2 can run on another platform... as a matter of fact Im working on a demo that is using GLUT with SIO2 and practically no changes in the code is made (except to replace some OES with function with their GL counterpart). Keeping SIO2 like that will make it alot easier to port to other platform, that's why Im trying to don't depend on any platform specific code inside the core...

I'll be gladly integrating PNG support to SIO2 if that was through libpng that is free, and cross platform...

Cheers,
sio2interactive
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 44
Location : Shanghai

http://sio2interactive.com

Back to top Go down

PNG Loading Empty PNG Loading

Post  tomw Sun Jan 11, 2009 9:46 pm

Have you added support for loading PNGs? I'm looking through the source but just found code for loading jpgs and tga.
I use libpng on my pc framework and it would make it so much easier if SIO2 loaded PNGs?

sio2ImageLoadPNG()?

Right now I can only find
sio2ImageLoadTGA,sio2ImageLoadJPG

thanks
-tom

tomw

Posts : 1
Join date : 2009-01-11

Back to top Go down

PNG Loading Empty Re: PNG Loading

Post  sio2interactive Sun Jan 11, 2009 10:52 pm

Actually its funny that this topic pop back again, I was actually looking at PNG support right now... the example above use Apple specific API so I will not use that code... but I was digging on libpng... so it'll be probably supported by the next revision.

Cheers,

ps: Also PCX have been added... RGB, Grayscale and Indexed (palette) which really speed up loading at the cost of pixel precision.
sio2interactive
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 44
Location : Shanghai

http://sio2interactive.com

Back to top Go down

PNG Loading Empty Re: PNG Loading

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum