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



//
// Modifications History
//
//$LB (07/02/2003) : flags debug
//


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



OBJBITMAP_BUFFER classicBlit( OBJBITMAP_BUFFER bDest, OBJBITMAP_BUFFER bSrc,
					    int DestBPL, int SrcBPL,
					    int destX, int destY, int srcX, int srcY, int width, int height,
						int transparency)
{
    int dcx, dcy, scx, scy, color;
	unsigned char r, g, b;
	register i, j;
	register OBJBITMAP_BUFFER BufS;
	register OBJBITMAP_BUFFER BufD;

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

	dcy = destY * DestBPL + (destX*3);
    scy = srcY  * SrcBPL  + (srcX*3);

	if ( transparency == NIL ) 
	{            
	   BufS = ((OBJBITMAP_BUFFER) bSrc ) + scy ;
	   BufD = ((OBJBITMAP_BUFFER) bDest ) + dcy ;

	   for ( j = 0 ; j < height ; j ++ )
	   {                 
				memcpy( BufD, BufS, width*3) ;
				BufD += DestBPL ;
				BufS += SrcBPL ;
	   }
	}
	else
	{
		for ( j = 0 ; j < height ; j ++ )
		{
			dcx = dcy ;
			scx = scy ;
			for ( i = 0 ; i < width ; i ++ , dcx +=3 , scx +=3 )
			{
				b = bSrc[ scx ];
				g = bSrc[ scx+1 ];
				r = bSrc[ scx+2 ];

				color = _COLOR_BGR_TO_I (b, g, r);

				if (color != transparency)
				{
					bDest[ dcx ] = b;
					bDest[ dcx+1 ] = g;
					bDest[ dcx+2 ] = r;
				}
			}
			dcy += DestBPL;
			scy += SrcBPL;
		}
	}

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

	return bDest;
}





OBJBITMAP_BUFFER classicStretchBlit( OBJBITMAP_BUFFER bDest, OBJBITMAP_BUFFER bRGB,
 	  						   int DestBPL, int RGBBPL,
							   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;
    register i ,j;
	int color;
	unsigned char r, g, b;

//***********************************
#if DEBUG_LIB2DOS
MMechostr (0, "\nclassicStretchBlit");
#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);

    for ( j = destY1 ; j <= destY2 ; j ++ )
    {
        odx  = ody;
        ocx  = ocy;
        dex  = 0;
        for ( i = destX1 ; i <= destX2 ; i ++ )
        {
			r = bRGB[ ocx ];
			g = bRGB[ ocx+1 ];
			b = bRGB[ ocx+2 ];
			color = _COLOR_BGR_TO_I (b, g, r);

			if ( transparency !=  color)
			{
				bDest[ odx+0 ] = bRGB[ ocx+0 ];
				bDest[ odx+1 ] = bRGB[ ocx+1 ];
				bDest[ odx+2 ] = bRGB[ ocx+2 ];
			}

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

        ody += DestBPL;
        dey += dsy;

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


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

	return bDest;
}