rgb565格式 alpha混合算法优化
原始的算法:
因为调用频繁,所以采用宏或内联函数方式实现。
#define RGB565_ALPHA_CONVERSION( d, s, alpha) \
{ \
unsigned int sr = (s) & 0xF800; \
unsigned int sg = (s) & 0x07E0; \
unsigned int sb = (s) & 0x001F; \
unsigned int dr = (d) & 0xF800; \
unsigned int dg = (d) & 0x07E0; \
unsigned int db = (d) & 0x001F; \
\
dr += ((sr - dr) * (alpha)) >> 8; \
dg += ((sg - dg) * (alpha)) >> 8; \
db += ((sb - db) * (alpha)) >> 8; \
dr &= 0xF800; \
dg &= 0x07E0; \
db &= 0x001F; \
d= dr | dg | db; \
}
根据实际应用测试,图片中点的alpha为0 或255居多,所以先处理,性能有显著提高:
#define RGB565_ALPHA_CONVERSION( d, s, alpha) \
{ \
if(alpha==0) \
{ \
\
}else if(alpha == 255) \
{ \
d = s; \
}else \
{ \
unsigned int sr = (s) & 0xF800; \
unsigned int sg = (s) & 0x07E0; \
unsigned int sb = (s) & 0x001F; \
unsigned int dr = (d) & 0xF800; \
unsigned int dg = (d) & 0x07E0; \
unsigned int db = (d) & 0x001F; \
\
dr += ((sr - dr) * (alpha)) >> 8; \
dg += ((sg - dg) * (alpha)) >> 8; \
db += ((sb - db) * (alpha)) >> 8; \
dr &= 0xF800; \
dg &= 0x07E0; \
db &= 0x001F; \
d= dr | dg | db; \
} \
}
再来:看以下代码:
TUint32 Fast_AlphaBlender(TUint32 x, TUint32 y, TUint32 Alpha)
{
x = ( x | ( x<<16 ) ) & 0x7E0F81F;
y = ( y | ( x<<16 ) ) & 0x7E0F81F;
TUint32 result = ( ((x - y) * Alpha) >> 5 + y ) & 0x7E0F81F;
return (TUint32)(result&0xFFFF) | (result>>16);
}
采用这个算法再加上用宏的形式并对0 255 单独处理,性能肯定会提高,不过我还没来得及测试,参考以下文档: