Sign in to follow this  
cambalinho

how add a color key with AlphaBlend() API function?

Recommended Posts

Perhaps you haven't provided enough details about what you're trying to do or the over all situation.

I don't know how to do it with the API that you're using (it sounds like VB6) but in SFML, the sf::Image class contains a method called CreateMaskFromColor() that you can use on a loaded image. You can then use that sf::Image to create an sf::Sprite and then later use sf::Sprite::setColor() to modulate the color and the alpha of the sprite before it's blt to its destination.
 

Share this post


Link to post
Share on other sites

i have 2 diferent images:

- the 1st(have a transparent backcolor): i can draw it transparent and with alpha... great;

- the 2nd(don't have a transparent backcolor): i can draw it not transparent and with alpha.

both images are a gif files

why these conclusion between the 2 images?

Edited by cambalinho

Share this post


Link to post
Share on other sites

almost fixed:

Public Sub Draw(DestinationHDC As Long, blnTransparent As Boolean, Optional Transparency As Long = 255)
    RaiseEvent BeforeDrawImage(intSelectedFrame)
    Dim AO As AlphaOptions
    Dim PointerAlpha As Long
    If (blnTransparent = True) Then
        Dim color As Long
        Dim btMap As BITMAP
        If (GetObject(ImageBitmap, Len(btMap), btMap) = 0) Then Debug.Print "error"
        Dim s As BITMAPINFO
        Dim BytesPerScanLine As Long
        s.bmiHeader.biSize = 40
        s.bmiHeader.biPlanes = 1
        s.bmiHeader.biBitCount = 24
        s.bmiHeader.biHeight = -lngheight
        s.bmiHeader.biWidth = lngwidth
        s.bmiHeader.biPlanes = btMap.bmPlanes
        s.bmiHeader.biCompression = 0
        BytesPerScanLine = ((Abs(s.bmiHeader.biWidth) * 3) + 3) And &HFFFFFFFC
        s.bmiHeader.biSizeImage = BytesPerScanLine * lngheight
        Dim ImageData() As Byte
        ReDim ImageData(3, s.bmiHeader.biWidth, lngheight)
        GetDIBits ImageHDC, ImageBitmap, 0, lngheight, ImageData(0, 0, 0), s, 0
        If ((((color And &HFF000000) \ 16777216) And &HFF) <> 255) Then
            Dim x As Long
            Dim y As Long
            For y = 0 To lngheight - 1
                For x = 0 To lngwidth - 1
                    color = GetPixel(ImageHDC, x, y)
                    If (color = GetPixel(ImageHDC, 0, 0)) Then
                        'Debug.Print "changing alpha pixels "
                        color = RGBA(color, 0)
                        SetPixel ImageHDC, x, y, color
                    End If
                Next x
            Next y
        End If
        AO.AlphaFlags = 0
        AO.AlphaFormat = AC_SRC_ALPHA
        AO.AlphaOption = AC_SRC_OVER
        AO.SourceConstantAlpha = Transparency
        RtlMoveMemory PointerAlpha, AO, 4
        AlphaBlend DestinationHDC, 0, 0, lngwidth, lngheight, ImageHDC, 0, 0, lngwidth, lngheight, PointerAlpha
    Else
        
        AO.AlphaFlags = 0
        AO.AlphaFormat = 0 'AC_SRC_ALPHA
        AO.AlphaOption = AC_SRC_OVER
        AO.SourceConstantAlpha = Transparency
        RtlMoveMemory PointerAlpha, AO, 4
        AlphaBlend DestinationHDC, 0, 0, lngwidth, lngheight, ImageHDC, 0, 0, lngwidth, lngheight, PointerAlpha
    End If
End Sub

i'm getting trouble on getting the alpha values.

can anyone correct me how can i calculate the alpha on pixel?

(yes, for now, i'm using GetPixel() and SetPixel(). they are very slow. but for now i need test the alpha value correctly)

the:

(((color And &HFF000000) \ 16777216) And &HFF)

give me always zero :(

Share this post


Link to post
Share on other sites

You haven't initialized color to anything by the time it's gotten to the condition you asked about.

 

Also, I'm not sure on whether GetPixel includes an alpha value. I haven't looked extensively but this documentation   suggests that maybe the alpha value isn't included. I'd recommend maybe checking by setting a pixel at a know location to something with an alpha value you know and verify the get returns that value as expected.

 

i have 2 diferent images: - the 1st(have a transparent backcolor): i can draw it transparent and with alpha... great; - the 2nd(don't have a transparent backcolor): i can draw it not transparent and with alpha. both images are a gif files why these conclusion between the 2 images?

If they were files that were provided to you from someone else, it could depend on how the gif images were created and saved. I'd recommend converting them to a format or recreating the files so you're confident that they have the alpha value saved with them.

Share this post


Link to post
Share on other sites

Test to see if it has an alpha channel.

 

If it has an alpha channel render using alpha.  

 

If it doesn't have an alpha channel, render with a color key.  Alternatively, process the image yourself and add an alpha channel, setting the alpha value based on matching the color key.

 

If I'm remembering rightly you should be able to tell from the bitmap object's header info if it has transparency, but if somehow you have no way to tell if the image has alpha or color key, you might always apply the color key in addition to whatever alpha values may already be in there.

Share this post


Link to post
Share on other sites

i'm sorry, but the number of bits it's important?

heres how i create the Bitmap:

ImageBitmap = CreateCompatibleBitmap(GetDC(0), Width, Height)

so. how can i create a bitmap with 32 bit's(seems that the AlphaBlend() can fail if it's less than 32bpps) or more?

can anyone explain to me what is 'bpps'?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this