Guest User

Untitled

a guest
Sep 30th, 2011
112
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Once we have our image, we need to get it into an SDL_Surface
  2. // (Copied straight from the ImageIO backend.)
  3. static SDL_Surface* Create_SDL_Surface_From_CGImage(CGImageRef image_ref)
  4. {
  5.     /* This code is adapted from Apple's Documentation found here:
  6.      * http://developer.apple.com/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/index.html
  7.      * Listing 9-4††Using a Quartz image as a texture source.
  8.      * Unfortunately, this guide doesn't show what to do about
  9.      * non-RGBA image formats so I'm making the rest up.
  10.      * All this code should be scrutinized.
  11.      */
  12.  
  13.     size_t w = CGImageGetWidth(image_ref);
  14.     size_t h = CGImageGetHeight(image_ref);
  15.     CGRect rect = {{0, 0}, {w, h}};
  16.  
  17.     CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image_ref);
  18.     //size_t bits_per_pixel = CGImageGetBitsPerPixel(image_ref);
  19.     size_t bits_per_component = 8;
  20.  
  21.     SDL_Surface* surface;
  22.     Uint32 Amask;
  23.     Uint32 Rmask;
  24.     Uint32 Gmask;
  25.     Uint32 Bmask;
  26.  
  27.     CGContextRef bitmap_context;
  28.     CGBitmapInfo bitmap_info;
  29.     CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
  30.  
  31.     if (alpha == kCGImageAlphaNone ||
  32.         alpha == kCGImageAlphaNoneSkipFirst ||
  33.         alpha == kCGImageAlphaNoneSkipLast) {
  34.         bitmap_info = kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host; /* XRGB */
  35.         Amask = 0x00000000;
  36.     } else {
  37.         /* kCGImageAlphaFirst isn't supported */
  38.         //bitmap_info = kCGImageAlphaFirst | kCGBitmapByteOrder32Host; /* ARGB */
  39.         bitmap_info = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host; /* ARGB */
  40.         Amask = 0xFF000000;
  41.     }
  42.  
  43.     Rmask = 0x00FF0000;
  44.     Gmask = 0x0000FF00;
  45.     Bmask = 0x000000FF;
  46.  
  47.     surface = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, Rmask, Gmask, Bmask, Amask);
  48.     if (surface)
  49.     {
  50.         // Sets up a context to be drawn to with surface->pixels as the area to be drawn to
  51.         bitmap_context = CGBitmapContextCreate(
  52.                                                             surface->pixels,
  53.                                                             surface->w,
  54.                                                             surface->h,
  55.                                                             bits_per_component,
  56.                                                             surface->pitch,
  57.                                                             color_space,
  58.                                                             bitmap_info
  59.                                                             );
  60.  
  61.         // Draws the image into the context's image_data
  62.         CGContextDrawImage(bitmap_context, rect, image_ref);
  63.  
  64.         CGContextRelease(bitmap_context);
  65.  
  66.         // FIXME: Reverse the premultiplied alpha
  67.         if ((bitmap_info & kCGBitmapAlphaInfoMask) == kCGImageAlphaPremultipliedFirst) {
  68.             int i, j;
  69.             Uint8 *p = (Uint8 *)surface->pixels;
  70.             for (i = surface->h * surface->pitch/4; i--; ) {
  71. #if __LITTLE_ENDIAN__
  72.                 Uint8 A = p[3];
  73.                 if (A) {
  74.                     for (j = 0; j < 3; ++j) {
  75.                         p[j] = (p[j] * 255) / A;
  76.                     }
  77.                 }
  78. #else
  79.                 Uint8 A = p[0];
  80.                 if (A) {
  81.                     for (j = 1; j < 4; ++j) {
  82.                         p[j] = (p[j] * 255) / A;
  83.                     }
  84.                 }
  85. #endif /* ENDIAN */
  86.                 p += 4;
  87.             }
  88.         }
  89.     }
  90.  
  91.     if (color_space)
  92.     {
  93.         CGColorSpaceRelease(color_space);          
  94.     }
  95.  
  96.     return surface;
  97. }
  98.  
  99.  
RAW Paste Data