Loading pngs

Luminary
Posts: 5,143
Joined: 2002.04
Post: #16
ImageIO was introduced with 10.4.
Quote this message in a reply
Member
Posts: 194
Joined: 2009.02
Post: #17
AnotherJake Wrote:For something that works on both iPhone and Mac to load a PNG, you can do something like this (I just kind of threw it together real quick, so it's pseudocode-ish, but you should be able to figure it out from here):

Code:
NSURL *url = [NSURL fileURLWithPath:myPath];
if (url == NULL)
{
    NSLog(@"%s ERROR: Unable to create URL", __FUNCTION__);
    return;
}
CGDataProviderRef source = CGDataProviderCreateWithURL((CFURLRef)url);
if (source == NULL)
{
    printf("%s ERROR: Unable to create data provider from file URL", __FUNCTION__);
    return;
}
CGImageRef image = CGImageCreateWithPNGDataProvider(source, nil, NO, kCGRenderingIntentDefault);
if(image == NULL)
{
    NSLog(@"%s ERROR: unable to load: %@.%@", __FUNCTION__, texture[texID].file, extension);
    CGDataProviderRelease(source);
    return;
}

CGSize imageSize = CGSizeMake(CGImageGetWidth(image), CGImageGetHeight(image));
NSUInteger width = imageSize.width;
NSUInteger height = imageSize.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
void *pixels = calloc(width * height * 4, 1);
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
I don't know if this works on 10.4 though. I don't target 10.4 anymore since I've seen too much anecdotal evidence that few use it anymore.


Thanks a lot. The code works except that it's drawing everything rotated 180 degrees, upside down and backwards, is there some way to flip the context or image?
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #18
NelsonMandella Wrote:Thanks a lot. The code works except that it's drawing everything rotated 180 degrees, upside down and backwards, is there some way to flip the context or image?

Picky! Rasp

The flipY stuff is the only mod you should need:
Code:
colorSpace = CGColorSpaceCreateDeviceRGB();
pixels = calloc(width * height * 4, 1);
context = CGBitmapContextCreate(pixels, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);

if (flipY)
{
    CGContextTranslateCTM(context, 0, (float)(height));
    CGContextScaleCTM(context, 1.0, -1.0);
}

CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
Quote this message in a reply
Member
Posts: 194
Joined: 2009.02
Post: #19
Perfect! Thanks so much for the help.
Quote this message in a reply
Member
Posts: 260
Joined: 2005.05
Post: #20
I have switched to pnglite for my PNG loading problems. Much less trouble than libpng IMHO.

http://www.karlings.com/~danne/pnglite/
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #21
Ingemar Wrote:I have switched to pnglite for my PNG loading problems. Much less trouble than libpng IMHO.

http://www.karlings.com/~danne/pnglite/

Looks nice. Does it work on iPhone too?
Quote this message in a reply
Member
Posts: 260
Joined: 2005.05
Post: #22
AnotherJake Wrote:Looks nice. Does it work on iPhone too?
I havn't tried that myself. But I wouldn't expect that to be a problem.
Quote this message in a reply
Post Reply