This is my first try of an AlphaBlend funtion that works on all windows version, and can be changed to work on linux too:
It is working but need be optmized for speed:
- Code: Select all Expand view
- #pragma BEGINDUMP
#include "windows.h"
#include "hbapi.h"
WORD DibNumColors( void * pv );
HANDLE DibFromBitmap( HBITMAP, DWORD, WORD, HPALETTE );
static WORD PaletteSize( void * pv )
{
LPBITMAPINFOHEADER lpbi = ( LPBITMAPINFOHEADER ) pv;
WORD NumColors = DibNumColors( lpbi );
if( lpbi->biSize == sizeof( BITMAPCOREHEADER ) )
{
return ( WORD )( NumColors * sizeof( RGBTRIPLE ) );
}
else
{
return ( WORD )( NumColors * sizeof( RGBQUAD ) );
}
}
void DrawNewAlpha( HDC hdc, HBITMAP hbm, int iRow, int iCol )
{
HANDLE hDib = DibFromBitmap( hbm, 0, 0, ( HPALETTE ) 0 );
COLORREF clr;
int i, j;
BYTE r, g, b;
if( hDib )
{
LPBITMAPINFO lpbmi = ( LPBITMAPINFO ) GlobalLock( hDib );
unsigned char * uc = ( LPBYTE ) lpbmi + ( WORD ) lpbmi->bmiHeader.biSize + PaletteSize( lpbmi );
unsigned long ul = 3, a1, a2;
for( j = 0; j < lpbmi->bmiHeader.biHeight; j++ )
{
for( i = 0; i < lpbmi->bmiHeader.biWidth; i++ )
{
a1 = uc[ ul ];
if( a1 != 0 )
{
if( a1 == 255 )
{
clr = RGB( uc[ ul - 1 ], uc[ ul - 2 ], uc[ ul - 3 ] );
SetPixelV( hdc, i + iCol, lpbmi->bmiHeader.biHeight - j + iRow, clr );
}
else
{
a2 = 255 - a1;
clr = GetPixel( hdc, i + iCol, lpbmi->bmiHeader.biHeight - j + iRow );
b = clr / 65536;
g = clr / 256;
r = ( clr - ( b * 65536 ) - ( g * 256 ) );
SetPixelV( hdc, i + iCol, lpbmi->bmiHeader.biHeight - j + iRow, RGB( ( ( ( a1 * uc[ ul - 1 ] ) + ( a2 * r ) ) / 255 ), ( ( ( a1 * uc[ ul - 2 ] ) + ( a2 * g ) ) / 255 ), ( ( ( a1 * uc[ ul - 3 ] ) + ( a2 * b ) ) / 255 ) ) );
}
}
ul += 4;
}
}
GlobalUnlock( hDib );
}
hb_retl( 1 );
}
//------------------------------------------------------------------------------------------------------------------//
HB_FUNC( NEWALPHABLEND )
{
DrawNewAlpha( ( HDC ) hb_parnl( 1 ), ( HBITMAP ) hb_parnl( 2 ), hb_parni( 3 ), hb_parni( 4 ) );
}
#pragma ENDDUMP
To test:
- Code: Select all Expand view
- local hBmp := LoadBitmap(GetResources(), 7777 )
NewAlphaBlend( oDlg:GetDC(), hBmp, 40, 40 )
oDlg:ReleaseDC()
Regards,
Toninho.