预乘透明度(Premultiplied Alpha)图像

最近在腾讯面试时被问到了预乘透明度(Premultiplied Alpha)图像相关的问题,之前使用Core Graphics接口时遇到过,也搜索了相关资料,可惜没用心学习。

Core Graphics的CGImage.h对图像透明度信息有如下定义

typedef CF_ENUM(uint32_t, CGImageAlphaInfo) {
  // ...
  /* For example, premultiplied RGBA */
  kCGImageAlphaPremultipliedLast, 
  /* For example, premultiplied ARGB */ 
  kCGImageAlphaPremultipliedFirst, 
  // ...
};

什么是预乘透明度(Premultiplied Alpha)图像?简单地说,比如常规的半透明半纯红色图像RGBA归一化值为(0.5, 0, 0, 0.5),由预乘透明度图像方式存储则RGBA值为(0.25, 0, 0, 0.5)。由此可知,即每个颜色分量都乘以alpha通道值作为结果值:

color.rgb *= color.alpha

为什么关注预乘透明度图像?根据面试官的介绍,此问题主要是因AR抢红包场景的OpenGL混色结果出错引起了他们团队的注意。

Premultiplied alpha is better than conventional blending for several reasons:

  • It works properly when filtering alpha cutouts (see below)
  • It works properly when doing image composition (stay tuned for my next post)
  • It is a superset of both conventional and additive blending. If you set alpha to zero while RGB is non zero, you get an additive blend. This can be handy for particle systems that want to smoothly transition from additive glowing sparks to dark pieces of soot as the particles age.
  • It plays nice with DXT compression, which only supports transparent pixels with an RGB of zero.
    摘自:Premultiplied alpha

参考:

  • Premultiplied alpha

你可能感兴趣的:(预乘透明度(Premultiplied Alpha)图像)