Skip to content

Commit

Permalink
Fixed loading 8-bit PNG images on Mac OS X
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Jan 14, 2012
1 parent 648895d commit e9e797d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES
@@ -1,4 +1,6 @@
1.2.11:
Sam Lantinga - Sat Jan 14 17:54:38 EST 2012
* Fixed loading 8-bit PNG images on Mac OS X
Sam Lantinga - Sat Dec 31 09:35:40 EST 2011
* SDL_image is now under the zlib license
Michael Bonfils - Mon Nov 28 21:46:00 EST 2011
Expand Down
63 changes: 61 additions & 2 deletions IMG_ImageIO.m
Expand Up @@ -184,9 +184,8 @@ static CFDictionaryRef CreateHintDictionary(CFStringRef uti_string_hint)
return hint_dictionary;
}


// Once we have our image, we need to get it into an SDL_Surface
static SDL_Surface* Create_SDL_Surface_From_CGImage(CGImageRef image_ref)
static SDL_Surface* Create_SDL_Surface_From_CGImage_RGB(CGImageRef image_ref)
{
/* This code is adapted from Apple's Documentation found here:
* http://developer.apple.com/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/index.html
Expand Down Expand Up @@ -298,6 +297,66 @@ static CFDictionaryRef CreateHintDictionary(CFStringRef uti_string_hint)

return surface;
}
static SDL_Surface* Create_SDL_Surface_From_CGImage_Index(CGImageRef image_ref)
{
size_t w = CGImageGetWidth(image_ref);
size_t h = CGImageGetHeight(image_ref);
size_t bits_per_pixel = CGImageGetBitsPerPixel(image_ref);
size_t bytes_per_row = CGImageGetBytesPerRow(image_ref);

SDL_Surface* surface;
SDL_Palette* palette;
CGColorSpaceRef color_space = CGImageGetColorSpace(image_ref);
CGColorSpaceRef base_color_space = CGColorSpaceGetBaseColorSpace(color_space);
size_t num_components = CGColorSpaceGetNumberOfComponents(base_color_space);
size_t num_entries = CGColorSpaceGetColorTableCount(color_space);
uint8_t *entry, entries[num_components * num_entries];

/* What do we do if it's not RGB? */
if (num_components != 3) {
SDL_SetError("Unknown colorspace components %lu", num_components);
return NULL;
}
if (bits_per_pixel != 8) {
SDL_SetError("Unknown bits_per_pixel %lu", bits_per_pixel);
return NULL;
}

CGColorSpaceGetColorTable(color_space, entries);
surface = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, bits_per_pixel, 0, 0, 0, 0);
if (surface) {
uint8_t* pixels = (uint8_t*)surface->pixels;
CGDataProviderRef provider = CGImageGetDataProvider(image_ref);
NSData* data = (id)CGDataProviderCopyData(provider);
[data autorelease];
const uint8_t* bytes = [data bytes];
size_t i;

palette = surface->format->palette;
for (i = 0, entry = entries; i < num_entries; ++i) {
palette->colors[i].r = entry[0];
palette->colors[i].g = entry[1];
palette->colors[i].b = entry[2];
entry += num_components;
}

for (i = 0; i < h; ++i) {
SDL_memcpy(pixels, bytes, w);
pixels += surface->pitch;
bytes += bytes_per_row;
}
}
return surface;
}
static SDL_Surface* Create_SDL_Surface_From_CGImage(CGImageRef image_ref)
{
CGColorSpaceRef color_space = CGImageGetColorSpace(image_ref);
if (CGColorSpaceGetModel(color_space) == kCGColorSpaceModelIndexed) {
return Create_SDL_Surface_From_CGImage_Index(image_ref);
} else {
return Create_SDL_Surface_From_CGImage_RGB(image_ref);
}
}


#pragma mark -
Expand Down

0 comments on commit e9e797d

Please sign in to comment.