FIFE 2008.0
sdlblendingfunctions.cpp
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 
00024 // 3rd party library includes
00025 
00026 // FIFE includes
00027 // These includes are split up in two parts, separated by one empty line
00028 // First block: files included from the FIFE root src directory
00029 // Second block: files included from the same folder
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 }
 All Classes Namespaces Functions Variables Enumerations Enumerator