/*		 
		note: compilation de ce fichier optimisée en vitesse
*/



//
// Modifications History
//
//$LB (20/12/2002) : 16bits to 24bits
//


#include "../x/objstr.h"
#include "utils.h"
#include "colors.h"

#include "../x/scolplugin.h"


OBJBITMAP_BUFFER alphaBlit(OBJBITMAP_BUFFER bDest, OBJBITMAP_BUFFER bRGB, OBJBITMAP_BUFFER bA,
					  int DestBPL, int RGBBPL, int ABPL,
					  int destX, int destY, int srcX, int srcY, int width, int height,
					  int transparency )
{
    int dcx, dcy, scx, scy, scax, scay;
	register i, j;
	unsigned char sr, sg, sb, a, dr, dg, db;
	int color;


//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nalphaBlit");
#endif
//***********************************

	dcy = destY * DestBPL + (destX * 3);
    scy = srcY  * RGBBPL  + (srcX * 3);
    scay = srcY * ABPL    + srcX;

    for ( j = 0 ; j < height ; j ++ )
    {
        dcx = dcy ;
        scx = scy ;
        scax = scay ;

        for ( i = 0 ; i < width ; i ++ , dcx+=3 , scx +=3 , scax ++ )
		{

			sb = bRGB[ scx   ];
			sg = bRGB[ scx+1 ];
			sr = bRGB[ scx+2 ];

			color = _COLOR_BGR_TO_I (sb, sg, sr);

			if ( transparency != color)
			{
				a = bA[ scax ];
				if ( a == 255 )
				{
					bDest[ dcx   ] = sb;
					bDest[ dcx+1 ] = sg;
					bDest[ dcx+2 ] = sr;
				}
				else if ( a != 0 )
				{
					db = bDest[ dcx   ] ;
					dg = bDest[ dcx+1 ] ;
					dr = bDest[ dcx+2 ] ;

					dr = dr + a * ( sr - dr ) / 255;
					dg = dg + a * ( sg - dg ) / 255;
					db = db + a * ( sb - db ) / 255;

					bDest[ dcx   ] = db;
					bDest[ dcx+1 ] = dg;
					bDest[ dcx+2 ] = dr;

				}
			}
		}
		dcy += DestBPL;
		scy += RGBBPL;
		scay += ABPL;
    }

//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nalphaBlit end");
#endif
//***********************************

	return bDest;
}
