MULTIMEDIA

iPhone 3D Programming : Blending and Augmented Reality - Wrangle Premultiplied Alpha

2/16/2011 2:21:27 PM
One of the biggest gotchas with textures on Apple devices is the issue of premultiplied alpha. If the RGB components in an image have already been scaled by their associated alpha value, the image is considered to be premultiplied. Normally, PNG images do not store premultiplied RGB values, but Xcode does some tampering with them when it creates the application bundle.

You might recall that we passed in a flag to the CGBitmapInfo mask that’s related to this; Example 1 shows a snippet of the ResourceManager class, with the flag of interest highlighted in bold.

Example 1. Using a CGContext
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo =
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big;
CGContextRef context = CGBitmapContextCreate(data,
description.Size.x,
description.Size.y,
description.BitsPerComponent,
bpp * description.Size.x,
colorSpace,
bitmapInfo);
CGColorSpaceRelease(colorSpace);
CGRect rect = CGRectMake(0, 0, description.Size.x, description.Size.y);
CGContextDrawImage(context, rect, uiImage.CGImage);
CGContextRelease(context);

For nonpremultiplied alpha, there’s a flag called kCGImageAlphaLast that you’re welcome to try, but at the time of this writing, the Quartz implementation on the iPhone does not support it, and I doubt it ever will, because of the funky preprocessing that Xcode performs on image files.

So, you’re stuck with premultiplied alpha. Don’t panic! There are two rather elegant ways to deal with it:

  • Use PVRTexTool to encode your data into a PVR file. Remember, PVRTexTool can encode your image into any OpenGL format; it’s not restricted to the compressed formats.

  • Or, adjust your blending equation so that it takes premultiplied alpha into account, like so:

    glBlendFunction(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)

    By using GL_ONE for the sfactor argument, you’re telling OpenGL there’s no need to multiply the RGB components by alpha.



In Figure 1, the left column contains a normal texture, and the right column contains a texture with premultiplied alpha. In every row, the dfactor argument is GL_ONE_MINUS_SRC_ALPHA.

The following list summarizes the best results from Figure 1:

  • For textures with straight alpha, set sfactor to GL_SRC_ALPHA and dfactor to GL_ONE_MINUS_SRC_ALPHA.

  • For textures with premultiplied alpha, set sfactor to GL_ONE and dfactor to GL_ONE_MINUS_SRC_ALPHA.

  • To check whether a texture has premultiplied alpha, disable blending, and look at the silhouette.

Figure 1. Texture alpha


Other  
  •  iPhone 3D Programming : Blending and Augmented Reality - Blending Recipe
  •  Microsoft XNA Game Studio 3.0 : Controlling Color (part 3)
  •  Microsoft XNA Game Studio 3.0 : Controlling Color (part 2)
  •  Microsoft XNA Game Studio 3.0 : Controlling Color (part 1) - Games and Classes & Classes as Offices
  •  Microsoft XNA Game Studio 3.0 : Working with Colors
  •  iPhone 3D Programming : Textures and Image Capture - Creating Textures with the Camera
  •  iPhone 3D Programming : Textures and Image Capture - Dealing with Size Constraints
  •  Programming with DirectX : Game Math - Vectors
  •  iPhone 3D Programming : Textures and Image Capture - Generating and Transforming OpenGL Textures with Quartz
  •  iPhone 3D Programming : Textures and Image Capture - The PowerVR SDK and Low-Precision Textures
  •  Building LOB Applications : Using Visual Studio 2010 WCF Data Services Tooling
  •  Building LOB Applications : Accessing RESTful Data using OData
  •  Programming with DirectX : Additional Texture Mapping - Image Filters
  •  Microsoft XNA Game Studio 3.0 : Making a Game Program
  •  iPhone 3D Programming : Textures and Image Capture - Texture Compression with PVRTC
  •  iPhone 3D Programming : Textures and Image Capture - Texture Formats and Types
  •  iPhone 3D Programming : Textures and Image Capture - Fight Aliasing with Filtering
  •  iPhone 3D Programming : Textures and Image Capture - Texture Coordinates Revisited
  •  Programming with DirectX : Additional Texture Mapping - Sprites
  •  Programming with DirectX : Additional Texture Mapping - Alpha Mapping
  •  
    Most View
    Toshiba SSD PC Upgrade Kit 60GB
    Run Android Apps on Windows
    Application Patterns and Tips : Localize a Silverlight Application
    Managing Internet Time in Vista
    Deploying the Client for Microsoft Exchange Server 2010 : Understanding Deployment Options
    SQL Server 2005 : Report Definition and Design (part 3) - Report Builder
    Samsung Galaxy Note 10.1 - The Company’s New Flagship Slate
    iPhone 3D Programming : Holodeck Sample (part 2) - Rendering the Dome, Clouds, and Text
    Canon EOS 6D - Full-Frame Your Photos
    Visual Studio Team System 2008 : Working with Test Results (part 2) - Build report and test result
    Top 10
    Windows Phone 8 In-Depth Review (Part 6)
    Windows Phone 8 In-Depth Review (Part 5)
    Windows Phone 8 In-Depth Review (Part 4)
    Windows Phone 8 In-Depth Review (Part 3)
    Windows Phone 8 In-Depth Review (Part 2)
    Windows Phone 8 In-Depth Review (Part 1)
    Xiaomi Phone 2 - High-End Specifications In A Surprisingly Cheap Package (Part 5)
    Xiaomi Phone 2 - High-End Specifications In A Surprisingly Cheap Package (Part 4)
    Xiaomi Phone 2 - High-End Specifications In A Surprisingly Cheap Package (Part 3)
    Xiaomi Phone 2 - High-End Specifications In A Surprisingly Cheap Package (Part 2)