Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Once we have our image, we need to get it into an SDL_Surface
- // (Copied straight from the ImageIO backend.)
- static SDL_Surface* Create_SDL_Surface_From_CGImage(CGImageRef image_ref)
- {
- /* This code is adapted from Apple's Documentation found here:
- * http://developer.apple.com/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/index.html
- * Listing 9-4††Using a Quartz image as a texture source.
- * Unfortunately, this guide doesn't show what to do about
- * non-RGBA image formats so I'm making the rest up.
- * All this code should be scrutinized.
- */
- size_t w = CGImageGetWidth(image_ref);
- size_t h = CGImageGetHeight(image_ref);
- CGRect rect = {{0, 0}, {w, h}};
- CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image_ref);
- //size_t bits_per_pixel = CGImageGetBitsPerPixel(image_ref);
- size_t bits_per_component = 8;
- SDL_Surface* surface;
- Uint32 Amask;
- Uint32 Rmask;
- Uint32 Gmask;
- Uint32 Bmask;
- CGContextRef bitmap_context;
- CGBitmapInfo bitmap_info;
- CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
- if (alpha == kCGImageAlphaNone ||
- alpha == kCGImageAlphaNoneSkipFirst ||
- alpha == kCGImageAlphaNoneSkipLast) {
- bitmap_info = kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host; /* XRGB */
- Amask = 0x00000000;
- } else {
- /* kCGImageAlphaFirst isn't supported */
- //bitmap_info = kCGImageAlphaFirst | kCGBitmapByteOrder32Host; /* ARGB */
- bitmap_info = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host; /* ARGB */
- Amask = 0xFF000000;
- }
- Rmask = 0x00FF0000;
- Gmask = 0x0000FF00;
- Bmask = 0x000000FF;
- surface = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, Rmask, Gmask, Bmask, Amask);
- if (surface)
- {
- // Sets up a context to be drawn to with surface->pixels as the area to be drawn to
- bitmap_context = CGBitmapContextCreate(
- surface->pixels,
- surface->w,
- surface->h,
- bits_per_component,
- surface->pitch,
- color_space,
- bitmap_info
- );
- // Draws the image into the context's image_data
- CGContextDrawImage(bitmap_context, rect, image_ref);
- CGContextRelease(bitmap_context);
- // FIXME: Reverse the premultiplied alpha
- if ((bitmap_info & kCGBitmapAlphaInfoMask) == kCGImageAlphaPremultipliedFirst) {
- int i, j;
- Uint8 *p = (Uint8 *)surface->pixels;
- for (i = surface->h * surface->pitch/4; i--; ) {
- #if __LITTLE_ENDIAN__
- Uint8 A = p[3];
- if (A) {
- for (j = 0; j < 3; ++j) {
- p[j] = (p[j] * 255) / A;
- }
- }
- #else
- Uint8 A = p[0];
- if (A) {
- for (j = 1; j < 4; ++j) {
- p[j] = (p[j] * 255) / A;
- }
- }
- #endif /* ENDIAN */
- p += 4;
- }
- }
- }
- if (color_space)
- {
- CGColorSpaceRelease(color_space);
- }
- return surface;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement