Hey all.
I’m working on a game on Mac OS X (Snow Leopard, SDL 1.2.14). I’m using PNG
images (created in Gimp) to store the levels and then build the level based
on each pixel’s color – the problem is the colors I’m getting are slightly
inaccurate, which is breaking my program.
I’m using the getpixel() method from the SDL
websitehttp://www.libsdl.org/cgi/docwiki.cgi/Pixel_Access.
Each color in the image is off by just a few integers. For example (126,
126, 180) is being read as (127, 127, 178). Also, for the record, I took a
screenshot of my program and compared it to the colors to my sprites in
Gimp, and the screenshot was slightly off as well.
I think I have an idea of what’s going on though. When I change the level
image to a .BMP, the pixels are read 100% accurately, so I’m assuming it’s
just a bug in SDL_image. I did a little digging, and I found this:
http://playcontrol.net/ewing/jibberjabber/big_behind-the-scenes_chang.html#SDL_image
“We are changing the backend of SDL_image to use Apple’s ImageIO API”
I had this exact same problem when using ImageIO a few months ago. I can’t
remember exactly how I was doing it before, but when I load images like
below the pixels are read correctly:
- (CGImageRef)loadImageFromFile:(NSString *)pathToResource {
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
[[NSFileManager defaultManager] changeCurrentDirectoryPath:resourcePath];
NSURL* url = [NSURL fileURLWithPath:pathToResource];
CGImageRef imageRef = NULL;
CGImageSourceRef sourceRef;
sourceRef = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
if(sourceRef) {
imageRef = CGImageSourceCreateImageAtIndex(sourceRef, 0, NULL);
CFRelease(sourceRef);
} else {
NSLog(@“Failed to load image\n”);
}
return imageRef;
}
// Example getting the pixel data (it’s read-only though – figures this is
how Apple recommends it)
CGImageRef imageRef = [loadImageFromFile:@“example.png”];
CFDataRef imageDataRef = CGDataProviderCopyData(CGImageGetDataProvider
(imageRef));
const UInt8 *pixelData = CFDataGetBytePtr(imageDataRef);
Any chance someone could fix this? BMPs are so huge.
Cheers,
- pixelman