sdlblendingfunctions.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "sdlblendingfunctions.h"
00031
00032 namespace FIFE {
00033
00034 struct ColorRGB8 {
00035 unsigned char r, g, b;
00036 };
00037
00038 struct ColorRGBA8 {
00039 unsigned char r, g, b, a;
00040 };
00041
00042 void SDL_BlendRow_RGBA8_to_RGBA8( const unsigned char* src, unsigned char* dst, unsigned int alpha, int n ) {
00043 const ColorRGBA8* srcColor = reinterpret_cast< const ColorRGBA8* >( src );
00044 ColorRGBA8* dstColor = reinterpret_cast< ColorRGBA8* >( dst );
00045
00046 for( int i = n; 0 < i; --i ) {
00047 register unsigned int aMulA = alpha * srcColor->a;
00048
00049 if( aMulA ) {
00050 register unsigned int OneMin_aMulA = 65535 - aMulA;
00051 dstColor->r = ( aMulA * srcColor->r + OneMin_aMulA * dstColor->r ) >> 16;
00052 dstColor->g = ( aMulA * srcColor->g + OneMin_aMulA * dstColor->g ) >> 16;
00053 dstColor->b = ( aMulA * srcColor->b + OneMin_aMulA * dstColor->b ) >> 16;
00054 dstColor->a = 255;
00055 }
00056 ++dstColor;
00057 ++srcColor;
00058 }
00059 }
00060
00061 void SDL_BlendRow_RGBA8_to_RGB8( const unsigned char* src, unsigned char* dst, unsigned int alpha, int n ) {
00062 const ColorRGBA8* srcColor = reinterpret_cast< const ColorRGBA8* >( src );
00063 ColorRGB8* dstColor = reinterpret_cast< ColorRGB8* >( dst );
00064
00065 for( int i = n; 0 < i; --i ) {
00066 register unsigned int aMulA = alpha * srcColor->a;
00067 if( aMulA ) {
00068 register unsigned int OneMin_aMulA = 65535 - aMulA;
00069 dstColor->r = ( aMulA * srcColor->r + OneMin_aMulA * dstColor->r ) >> 16;
00070 dstColor->g = ( aMulA * srcColor->g + OneMin_aMulA * dstColor->g ) >> 16;
00071 dstColor->b = ( aMulA * srcColor->b + OneMin_aMulA * dstColor->b ) >> 16;
00072 }
00073
00074 ++dstColor;
00075 ++srcColor;
00076 }
00077 }
00078
00079 void SDL_BlendRow_RGBA8_to_RGB565( const unsigned char* src, unsigned char* dst, unsigned int alpha, int n ) {
00080 const ColorRGBA8* srcColor = reinterpret_cast< const ColorRGBA8* >( src );
00081 unsigned short* dstColor = reinterpret_cast< unsigned short* >( dst );
00082
00083 for( int i = n; 0 < i; --i ) {
00084 register unsigned int aMulA = ( alpha * srcColor->a ) >> 8;
00085 if( aMulA ) {
00086 register unsigned int OneMin_aMulA = 255 - aMulA;
00087 register unsigned int c = *dstColor;
00088 *dstColor = ( ( ( srcColor->b * aMulA ) +
00089 ( ( ( c & 0xF800 ) >> 8 ) * OneMin_aMulA ) ) & 0xF800 ) |
00090 ( ( ( ( srcColor->g * aMulA ) +
00091 ( ( ( c & 0x07E0 ) >> 3 ) * OneMin_aMulA ) ) >> 5 ) & 0x07E0 ) |
00092 ( ( ( ( srcColor->r * aMulA ) +
00093 ( ( ( c & 0x001F ) << 3 ) * OneMin_aMulA ) ) >> 11 ) & 0x001F );
00094 }
00095
00096 ++dstColor;
00097 ++srcColor;
00098 }
00099 }
00100
00101
00102 void SDL_BlendRow_RGBA4_to_RGB565( const unsigned char* src, unsigned char* dst, unsigned int alpha, int n ) {
00103 const unsigned short* srcColor = reinterpret_cast< const unsigned short* >( src );
00104 unsigned short* dstColor = reinterpret_cast< unsigned short* >( dst );
00105
00106 for( int i = n; 0 < i; --i ) {
00107 register unsigned int c1 = *dstColor;
00108 register unsigned int c2 = *srcColor;
00109
00110 unsigned int aMulA = c2 & 0xF;
00111 aMulA = ( alpha * aMulA ) / 15;
00112 if( aMulA ) {
00113 register unsigned int OneMin_aMulA = 255 - aMulA;
00114 register unsigned int result;
00115 result = ( ( ( ( c2 & 0xF000 ) | 0x0800 ) * aMulA ) + ( ( c1 & 0xF800 ) * OneMin_aMulA ) ) & 0xF80000;
00116 result |= ( ( ( ( ( c2 & 0x0F00 ) >> 1 ) | 0x0040 ) * aMulA ) + ( ( c1 & 0x07E0 ) * OneMin_aMulA ) ) & 0x07E000;
00117 result |= ( ( ( ( ( c2 & 0x00F0 ) >> 3 ) | 0x0001 ) * aMulA ) + ( ( c1 & 0x001F ) * OneMin_aMulA ) ) & 0x001F00;
00119 *dstColor = static_cast< unsigned short int >( result >> 8 );
00120 }
00121
00122 ++dstColor;
00123 ++srcColor;
00124 }
00125 }
00126
00127 }