/*		 
		GESTION DU FORMAT DE FICHIER GRAPHIQUE AVEC COUCHE ALPHA POUR SCOL
								Mathieu CHAMPLON
									jul. 99

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


//
// Modifications History
//
//$LB (17/12/2002) : 16bits to 24bits
//
//$LB (07/02/2003) : flags debug
//


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

#include "colors.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_LIB2DOS
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_LIB2DOS
MMechostr (0, "\nalphaBlit end");
#endif
//***********************************

	return bDest;
}






OBJBITMAP_BUFFER alphaStretchBlit( OBJBITMAP_BUFFER bDest, OBJBITMAP_BUFFER bRGB, OBJBITMAP_BUFFER bA,
	  					     int DestBPL, int RGBBPL, int ABPL,
							 int destX1, int destY1, int destX2, int destY2,
							 int srcX1, int srcY1, int srcX2, int srcY2,
							 int transparency )
{
    int ddx, ddy, dsx, dsy, dex, dey;
    int ody, odx, ocy, ocx, ocax, ocay;
	unsigned char sr, sg, sb, a, dr, dg, db;
    register i ,j;
	int color;

//***********************************
#if DEBUG_LIB2DOS
MMechostr (0, "\nalphaStretchBlit");
#endif
//***********************************


    ddx = destX2 - destX1 + 1 ;
    ddy = destY2 - destY1 + 1 ;
    dsx = srcX2  - srcX1  + 1 ;
    dsy = srcY2  - srcY1  + 1 ;
    dex = 0 ;
    dey = 0 ;

    ody  = destY1 * DestBPL   + (destX1*3);
    ocy  = srcY1  * RGBBPL  + (srcX1*3);
	ocay = srcY1  * ABPL + srcX1;

    for ( j = destY1 ; j <= destY2 ; j ++ )
    {
        odx  = ody;
        ocx  = ocy;
		ocax = ocay;
        dex  = 0;

        for ( i = destX1 ; i <= destX2 ; i ++ )
        {
			sb = bRGB[ ocx ] ;
			sg = bRGB[ ocx+1 ];
			sr = bRGB[ ocx+2 ];

			color = _COLOR_BGR_TO_I (sb, sg, sr);

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

					//$LB
					dr = dr + a * ( sr - dr ) / 255;
					dg = dg + a * ( sg - dg ) / 255;
					db = db + a * ( sb - db ) / 255;
					bDest[ odx   ] = db;
					bDest[ odx+1 ] = dg;
					bDest[ odx+2 ] = dr;
					
				}
			}

            odx +=3;
            dex += dsx;
            while ( dex >= ddx )
            {
                ocx +=3;
				ocax++;
                dex -= ddx;
            }
        }

        ody += DestBPL;
        dey += dsy;

        while ( dey >= ddy )
        {
            ocy  += RGBBPL;
			ocay += ABPL;
            dey  -= ddy;
        }
    }


//***********************************
#if DEBUG_LIB2DOS
MMechostr (0, "\nalphaStretchBlit end");
#endif
//***********************************

	return bDest;
}