Drawing Alphablend From Stream !!!

Post Reply
toninhofwi
Posts: 161
Joined: Tue Oct 18, 2005 10:01 am

Drawing Alphablend From Stream !!!

Post by toninhofwi »

This code can be useful to draw a bitmap with alphablend directly from a stream.

Sample code, 7777 is my alphabitmap resource. This code can be optimized by calling cResToStr() directly from StreamAlphaBlend():

Code: Select all

   local hDC := oDlg:GetDC()

   StreamAlphaBlend( hDC, 7777, 0, 0, 255, cResToStr( 7777, 2 ) )

   oDlg:ReleaseDC()

Code: Select all

#pragma BEGINDUMP

#include "windows.h"
#include "hbapi.h"

HINSTANCE GetResources( void );

WORD DibNumColors( void * pv );

HPALETTE CreateDIBPalette( HGLOBAL hDIB );

HANDLE DibFromBitmap( HBITMAP, DWORD, WORD, HPALETTE );

BOOL DibDraw( HDC hDC, HGLOBAL hDib, WORD wCol, WORD wRow, HPALETTE hPalette, WORD wWidth, WORD wHeight, DWORD dwRop );

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 hDC1, HBITMAP hBitmap1, int iRow, int iCol, int alpha, char * szText )
{
   HDC hDC2;

   HANDLE hDib2;

   unsigned char * uc2;

   unsigned char a1, a2;

   unsigned int i1, iPos = 40;

   LPBITMAPINFO lpbmi2;

   HBITMAP hBitmap2, hBmpOld;

   BITMAP bm;

   hDC2 = CreateCompatibleDC( hDC1 );

   GetObject( ( HGDIOBJ ) hBitmap1, sizeof( BITMAP ), ( LPSTR ) &bm );

   hBitmap2 = CreateCompatibleBitmap( hDC1, bm.bmWidth, bm.bmHeight );

   hBmpOld = ( HBITMAP ) SelectObject( hDC2, hBitmap2 );

   BitBlt( hDC2, 0, 0, bm.bmWidth, bm.bmHeight, hDC1, iCol, iRow, SRCCOPY );

   //---------------------------------------------------------------------------------------------------------------//

   hDib2 = DibFromBitmap( hBitmap2, 0, 32, ( HPALETTE ) 0 );

   lpbmi2 = ( LPBITMAPINFO ) GlobalLock( hDib2 );

   uc2 = ( LPBYTE ) lpbmi2 + ( WORD ) lpbmi2->bmiHeader.biSize + PaletteSize( lpbmi2 );

   //---------------------------------------------------------------------------------------------------------------//

   for( i1 = 3; i1 <= lpbmi2->bmiHeader.biSizeImage; i1 += 4 )
   {
        a1 = (unsigned char) szText[ i1 + iPos ];

        if( a1 != 0 )
        {
            a1 = alpha * ( ( a1 * 100 ) / 255 ) / 100;

            a2 = 255 - a1;

            uc2[ i1 - 1 ] = ( ( a1 * (unsigned char) szText[ i1 + iPos - 1 ] ) + ( a2 * uc2[ i1 - 1 ] ) ) >> 8;
            uc2[ i1 - 2 ] = ( ( a1 * (unsigned char) szText[ i1 + iPos - 2 ] ) + ( a2 * uc2[ i1 - 2 ] ) ) >> 8;
            uc2[ i1 - 3 ] = ( ( a1 * (unsigned char) szText[ i1 + iPos - 3 ] ) + ( a2 * uc2[ i1 - 3 ] ) ) >> 8;
        }
   }

   DibDraw( hDC1, hDib2, iCol, iRow, 0, 0, 0, 0 );

   //---------------------------------------------------------------------------------------------------------------//

   GlobalUnlock( hDib2 );

   GlobalFree( hDib2 );

   SelectObject( hDC2, hBmpOld );

   DeleteObject( hBitmap2 );

   DeleteDC( hDC2 );
}

//------------------------------------------------------------------------------------------------------------------//

HB_FUNC( STREAMALPHABLEND )
{
   HMODULE hMod = GetResources();

   HBITMAP hBmp = LoadBitmap( ( HINSTANCE ) hMod, ( ISCHAR( 2 ) ? hb_parc( 2 ) : ( LPSTR ) MAKEINTRESOURCE( hb_parnl( 2 ) ) ) );

   DrawNewAlpha( ( HDC )  hb_parnl( 1 ), ( HBITMAP ) hBmp, hb_parni( 3 ), hb_parni( 4 ), hb_parni( 5 ), hb_parc( 6 ) );
}

//------------------------------------------------------------------------------------------------------------------//

HB_FUNC( CRESTOSTR )
{
   HMODULE hMod = GetResources();

   HRSRC hRes = FindResource( hMod, MAKEINTRESOURCE( hb_parni( 1 ) ), MAKEINTRESOURCE( hb_parni( 2 ) ) );

   DWORD size = SizeofResource( hMod, hRes );

   HGLOBAL pt = LoadResource( hMod, hRes );

   LPSTR pResult = ( LPSTR ) hb_xgrab( size + 1 );

   memcpy( pResult, pt, size );

   hb_retclen( pResult, size );

   hb_xfree( pResult );
}

#pragma ENDDUMP

Regards,

Toninho.
toninhofwi
Posts: 161
Joined: Tue Oct 18, 2005 10:01 am

Re: Drawing Alphablend From Stream !!!

Post by toninhofwi »

Hi friends,

I'm trying to draw a PNG file in this same way. Anybody knows where I can find information about PNG file format please ?

Regards,

Toninho.
StefanHaupt
Posts: 824
Joined: Thu Oct 13, 2005 7:39 am
Location: Germany

Re: Drawing Alphablend From Stream !!!

Post by StefanHaupt »

Toninho,

here you can find informations about png graphics file format

http://www.libpng.org/pub/png/

Another good site is http://www.wotsit.org, here you can find links for near every filefomat that exist
kind regards
Stefan
toninhofwi
Posts: 161
Joined: Tue Oct 18, 2005 10:01 am

Re: Drawing Alphablend From Stream !!!

Post by toninhofwi »

Hi Stefan,

Thanks, the second link is great.

Toninho.
Post Reply