Loading pngs

Member
Posts: 194
Joined: 2009.02
Post: #1
How do you guys load pngs on os x? Whenever I tried using libpng in the past, xcode would never statically link the libpng.a file, it only worked if I already had the dylib installed, which is obviously not what I desired.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
build libpng and libjpeg with http://onesadcookie.com/svn/Third-Party/
load pngs and jpegs with them with http://onesadcookie.com/svn/SimpleImage/ as demonstrated by http://onesadcookie.com/svn/SimpleImageCDemo/
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #3
Xcode will prefer to link against a dynamic library if it can find one with the same name as the static library you're trying to link. You'll never want to install any library you're compiling against if you can possibly help it; put the compiled static lib somewhere in your project directory and reference it directly as a file in other linker flags.
Quote this message in a reply
Member
Posts: 194
Joined: 2009.02
Post: #4
OneSadCookie Wrote:build libpng and libjpeg with http://onesadcookie.com/svn/Third-Party/
load pngs and jpegs with them with http://onesadcookie.com/svn/SimpleImage/ as demonstrated by http://onesadcookie.com/svn/SimpleImageCDemo/

How would I build libpng from the files in the first link?



ThemsAllTook Wrote:Xcode will prefer to link against a dynamic library if it can find one with the same name as the static library you're trying to link. You'll never want to install any library you're compiling against if you can possibly help it; put the compiled static lib somewhere in your project directory and reference it directly as a file in other linker flags.

I understand the thing about not having the dylib installed, which is why I currently do not. How would I directly reference it using linker flags though?
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #5
OTHER_LDFLAGS=path/to/libpng.a
Quote this message in a reply
Member
Posts: 194
Joined: 2009.02
Post: #6
do I actually specify path/to/libpng.a or do I specify the actual path, and if so what would the path look like?
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #7
NelsonMandella Wrote:do I actually specify path/to/libpng.a or do I specify the actual path, and if so what would the path look like?

The actual path. Find libpng.a on your disk. Get info on it. Copy the path that shows up in the info dialog. Paste it into the proper field in Xcode.
Quote this message in a reply
Member
Posts: 194
Joined: 2009.02
Post: #8
I went into build settings->Linking-> Other Linker Flags and entered exactly:

/Users/myComputer/Desktop/libpng.a for OTHER_LDFLAGS

libpng.a is located on my desktop.

And when I try to run the program I get this message:

i686-apple-darwin9-gcc-4.0.1: No such file or directory

I also tried creating a flag called OTHER_LDFLAGS and set it to the path and it didn't work either.

I also tried adding the path to "Library Search Paths" from the "Search Paths" group of build settings, and got a bunch of unknown symbol messsages. I also checked "Link Binary With Libraries" under target, to make sure that libpng.a was there and it was.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #9
NelsonMandella Wrote:How would I build libpng from the files in the first link?

Code:
svn co http://onesadcookie.com/svn/Third-Party
cd Third-Party
make

You may need to edit URLs and versions in some of the Makefiles if the libraries have been updated since the last time I used this myself Wink

If you only want to build certain of them, you can specify

Code:
make TP_PACKAGES='libpng libjpeg'
Quote this message in a reply
Member
Posts: 194
Joined: 2009.02
Post: #10
So I should type......

svn co http://onesadcookie.com/svn/Third-Party
cd Third-Party
make

....into the terminal? What does it do? And I don't understand what you mean about changing the url. Is the end result suppose to be that I end up with a libpng.a file(already have one)?

This is all terribly confusing to me and I don't really understand any of it, is there not a simpler method for using pngs? Why is it so easy to load pngs on then iphone and such a nightmare to do it on a mac?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #11
You should learn how to use the terminal.

You can do it exactly the same way as you do on the iPhone, on the Mac, if you like.
Quote this message in a reply
Member
Posts: 194
Joined: 2009.02
Post: #12
OneSadCookie Wrote:You can do it exactly the same way as you do on the iPhone, on the Mac, if you like.

When I try compiling the png loading routine I used on the iphone I get the error: 'UIImage' undeclrared

Code:
BOOL load_png_texture(int* texture, NSString* path)
{
    CGImageRef spriteImage;
    CGContextRef spriteContext;
    GLubyte *spriteData;
    size_t    width, height;
    
    spriteImage = [UIImage imageNamed:path].CGImage;
    
    width = CGImageGetWidth(spriteImage);
    height = CGImageGetHeight(spriteImage);
    
    
    if(spriteImage)
    {
        // Allocated memory needed for the bitmap context
        spriteData = (GLubyte *) malloc(width * height * 4);
        // Uses the bitmatp creation function provided by the Core Graphics framework.
        spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width * 4, CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast);
        // After you create the context, you can draw the sprite image to the context.
        CGContextDrawImage(spriteContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), spriteImage);
        // You don't need the context at this point, so you need to release it to avoid memory leaks.
        CGContextRelease(spriteContext);
        
        // Use OpenGL ES to generate a name for the texture.
        glGenTextures(1, texture);
        // Bind the texture name.
        glBindTexture(GL_TEXTURE_2D, *texture);
        // Speidfy a 2D texture image, provideing the a pointer to the image data in memory
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);
        // Release the image data
        free(spriteData);
        
        // Set the texture parameters to use a minifying filter and a linear filer (weighted average)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        
        glTexParameteri(GL_TEXTURE_2D,    GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,  GL_REPEAT);
        return TRUE;
    }
    
    return FALSE;
}
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #13
well, UIKit doesn't exist on the Mac Rasp

You can use NSImage and the 10.6+ method for creating a CGImage from an NSImage to replace it, or you can use ImageIO directly (which would also work on the phone).
Quote this message in a reply
Member
Posts: 194
Joined: 2009.02
Post: #14
I'm targeting 10.4 and isn't imageio java?
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #15
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.
Quote this message in a reply
Post Reply