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
  •  
    Video
    Top 10
    -  Next – Gen Broadband – Optimizing Your Current Broadband Connection (Part 4)
    -  Next – Gen Broadband – Optimizing Your Current Broadband Connection (Part 3)
    -  Next–Gen Broadband – Optimizing Your Current Broadband Connection (Part 2)
    -  Next–Gen Broadband – Optimizing Your Current Broadband Connection (Part 1)
    -  Side Channel Attacks Explained
    -  Canon EOS M With Wonderful Touchscreen Interface (Part 3)
    -  Canon EOS M With Wonderful Touchscreen Interface (Part 2)
    -  Canon EOS M With Wonderful Touchscreen Interface (Part 1)
    -  Canon Powershot G15 With Immaculate Photos And Superb Controls
    -  Fujifilm XF1 - Compact Camera With Retro Design
    Most View
    -  Master Apple Mail (Part 3)
    -  Windows Server 2003 : Active Directory Federation Services - The Flow of Applications and Claims, Collaboration with Windows SharePoint Services
    -  Programming with DirectX : Game Math - Vectors
    -  Year End 2012 - Reviews & Rankings (Part 3)
    -  Windows Server 2003 : Dynamic DNS
    -  SQL Server 2008 : Service Broker Defined
    -  Windows Vista : Internet Me (part 2) - Test Your Throughput, Set Up Virtual Private Networking
    -  Pantum P2000 Laser Printer - Perfect Printer For Your Home Or Office
    -  Remote Administration of Exchange Server 2010 Servers : Using the Remote Exchange Management Shell
    -  iPhone 3D Programming : Textures and Image Capture - Generating and Transforming OpenGL Textures with Quartz
    -  Push Your Phone To Its Limits (Part 3)
    -  Best Apps For Your SmartPhones – December 2012 (Part 2)
    -  SQL Server 2005 Data Protection
    -  The State Of Mobile... Creative Media In 2012 (Part 1)
    -  Integrating Office Communications Server 2007 in an Exchange Server 2010 Environment - Understanding Microsoft’s Unified Communications Strategy
    -  AOC E2262VW - The Showy And Unique
    -  Writer, Reader, Speaker
    -  Windows Azure : Understanding the Blob Service
    -  Hide Files with Steganography (Part 1)
    -  SQL Server 2005 : Using Excel (part 2) - Using PivotTables and Charts in Applications and Web Pages