• Welcome to Theos PowerBasic Museum 2017.

AlphaBlend Primer

Started by Dan Gin zel, January 18, 2010, 05:02:16 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Dan Gin zel

Well, those famous words: "I'm baaaack!"

Briefly, I've been learning about BitBLT and StretchBLT and have graduated to AlphaBlend, which I have working but not quite as I had hoped.

I can call AlphaBlend and it displays my image. I can set .SourceConstantAlpha to a value and get the corresponding blending across the entire source image as I would expect from my current understanding.

The docs state: "Set the SourceConstantAlpha value to 255 (which indicates that the image is opaque) when you only want to use per-pixel alpha values."

Per-pixel alpha is exactly what I want. My source image is 32-bbp with transparency bits set. The result does display. Almost as this enlargement illustrates:




The black pixels have the transparency set to 0. This is perfect.

The white pixels have the transparency set between 8 and 157 (on a 0-255 scale), but they came out white.

The one pixels to the right of the right pixels is correct, however. It shows is the merging of the slightly transparent gray pixel and the background, which was originally tan. (The black pixels were tan, I just changed their color for the illustration to make them stand out. They were indeed correct when the original image was captured.)

What I'm guessing is that since AlphaBlend is functioning and it is partly recognizing the transparency values in the source image, I'm guessing that there is a threshold or other value that I need to set but cannot find. Or perhaps there is something else I am doing wrong...

I have verified that my source BMP file is correct, 32 bpp, and displays with correctly in several graphics programs against a variety of backgrounds, so I feel confident about this.

Here's how I'm calling AlphaBlend:

     Blend.BlendOp = %AC_SRC_OVER
     Blend.BlendFlags = 0
     Blend.SourceConstantAlpha = &FF
     Blend.AlphaFormat = %AC_SRC_ALPHA
     AlphaBlend hDC, x, y, cube, cube, hSource, cube * 7, 0, cube, cube, CVDWD(Blend)

Any thoughts?




[SIZE="1"]www.thecomputerarchive.com  Preserving the history of companies that advanced the computer revolution[/SIZE]

Patrice Terrier

Your use of Alphablend is correct,
however i encountered a similar problem, in the new WinLIFT, that i had to solve this way:


'         //4.36 Setup the alpha channel to preserve hilight.
          LOCAL P AS LONG, pBits AS BYTE PTR, bm AS BITMAP, AlphaCoef AS SINGLE
          CALL GetObject(hDIB, SIZEOF(bm), bm)
          pBits = bm.bmBits
          FOR P = (imgW * imgH) TO 1 STEP - 1
              AlphaCoef = (@pBits[3] / 255)
              @pBits[2] = @pBits[2] / AlphaCoef
              @pBits[1] = @pBits[1] / AlphaCoef
              @pBits[0] = @pBits[0] / AlphaCoef
              pBits = pBits + 4
          NEXT
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Dan Gin zel

Patrice, I appreciate your reply and have been playing with the routine you provided.

I have done some tests with smooth gradient blocks of various color and can see that the gray ones do well but the color ones go crazy when RGB values reach 255. Your routine helps on some and I've sorta figured out how to make it work on others, but it is clear that the built-in alphablend function has a few issues.

For the sake of time, I think I'm going to pre-render my graphics on the correct color backgrounds and move on as I have a prototype of the new version to show next week.

Thank you for your assistance. I do appreciate it!

Dan
[SIZE="1"]www.thecomputerarchive.com  Preserving the history of companies that advanced the computer revolution[/SIZE]

Patrice Terrier

#3
Quotethe built-in alphablend function has a few issues
Yes, and it is even worst when dealing with DWM, or when performing image rotation.
This is the reason why i had to writte my own in GDImage to handle all cases.

There is an undocumented API that converts image from one format to another, and that is alpahablend compatible.
See it there

It does well when converting PNG to transparent 32-bit bitmap using variable opacity.

...

Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Dan Gin zel

Patrice, just giving you an update.

I just finished my own color-image-alpha-blend routine that seems to work well. One neat thing that I added is that the new image can have a factor applied to it so that it can be "less than solid". For example, at 50%, the new image would only have half the effect as it would solid.

Thank you again for your assistance!

Dan
[SIZE="1"]www.thecomputerarchive.com  Preserving the history of companies that advanced the computer revolution[/SIZE]

Jürgen Huhn

Did anyone ever used the "GdiAlphaBlend" function??

I use this:


   ` Dara Types   !!
    Local bf          AS BLENDFUNCTION
    Local Alpha     AS BYTE
    Local aloc       AS long

    bf.BlendOp = %AC_SRC_OVER
    bf.BlendFlags = 0
    bf.AlphaFormat = %AC_SRC_ALPHA   ' Source alpha
    bf.SourceConstantAlpha = Alpha       ' use constant alpha!!
    aloc = -1

    CALL GdiAlphaBlend(hDC, x, y, cube, cube, hSource, cube * 7, 0, cube, cube, , bf) TO aloc

    IF aloc = 1 THEN ' <-returned 1 if no error - Job is done,,


Maybe this could be Helpful!!

I use GDI and GDI+...
.¸.•'´¯)¸.•'´¯)¸.•'´¯)¸.•'´¯)
¤ª"˜¨¨¯¯¨¨˜"ª¤....¤ ª"˜¨