Which is faster TransparentBlt or AlphaBlend
-
Hi all, I have finally managed to get a TransparentBlt function going in my program, the trouble is I`m finding it is too slow for what I need it to do (especially on larger images, with multiple layers). I am using a MDI app which supports multi layer bitmaps(hence the need for transparency). The OnDraw function is called every time I re-size the window etc. and so it ends up using blt many many times. So basically I wondered if : 1. Alpha Blend was faster? 2. Alpha Blend is capable of simply ommitting to change the color of a pixel where that pixel is the transparent color? I have only briefly looked through the alpha blend code, and it seems to merely blend two bitmaps together, thus creating a kind of duo-transparency. This isn`t particularly what I want, I just need to get rid of the main background colour of the foreground bitmaps. Any ideas on perhaps how I could speed up the process would be greatly received. Thankyou all. Alan.:| "When I left you I was but the learner, now I am the Master" - Darth Vader:mad:
-
Hi all, I have finally managed to get a TransparentBlt function going in my program, the trouble is I`m finding it is too slow for what I need it to do (especially on larger images, with multiple layers). I am using a MDI app which supports multi layer bitmaps(hence the need for transparency). The OnDraw function is called every time I re-size the window etc. and so it ends up using blt many many times. So basically I wondered if : 1. Alpha Blend was faster? 2. Alpha Blend is capable of simply ommitting to change the color of a pixel where that pixel is the transparent color? I have only briefly looked through the alpha blend code, and it seems to merely blend two bitmaps together, thus creating a kind of duo-transparency. This isn`t particularly what I want, I just need to get rid of the main background colour of the foreground bitmaps. Any ideas on perhaps how I could speed up the process would be greatly received. Thankyou all. Alan.:| "When I left you I was but the learner, now I am the Master" - Darth Vader:mad:
I can only give you a limited advice, but this may be better than nothing :) . In one of my projects, I have to transparently blt a bitmap. TransparentBlt, which is available on Win98 and 2K looks like good idea. However, its performance on Win98 is absolutely abysmal, my own code which creates a mask and then does two blts to destination was orders of magnitude faster. OTOH, the W2K implementation constantly beats my home-grown solution, sometimes its 100% faster... but not always. It seems that this speedup depends on the video driver - probably some drivers implement this functionality natively, while others do not and GDI must simulate it, creating a mask each time you call TransparentBlt. I've ended with a customizable option - users can enable TransparentBlt on W2K, but it's disabled by default. Tomasz Sowinski -- http://www.shooltz.com
-
I can only give you a limited advice, but this may be better than nothing :) . In one of my projects, I have to transparently blt a bitmap. TransparentBlt, which is available on Win98 and 2K looks like good idea. However, its performance on Win98 is absolutely abysmal, my own code which creates a mask and then does two blts to destination was orders of magnitude faster. OTOH, the W2K implementation constantly beats my home-grown solution, sometimes its 100% faster... but not always. It seems that this speedup depends on the video driver - probably some drivers implement this functionality natively, while others do not and GDI must simulate it, creating a mask each time you call TransparentBlt. I've ended with a customizable option - users can enable TransparentBlt on W2K, but it's disabled by default. Tomasz Sowinski -- http://www.shooltz.com
I`m using XP Business Edition RC1 at the moment, so god knows what its gonna be like on this. I`m using an adapted version of Christians code, which uses a good few Blts, including a few SRCANDS (they really are speed killers) raster operations. I have heard of a method using one BitBlt (or StretchBlt) and SRCCOPY, but it doesn`t seem to work. I`ll post the code here, and if anyone can see whats wrong cool (remember this is NOT my code, so if it raises any questions, chances are I won`t know the answer).
//Only attempt this if device supports functionality.
if(GetDeviceCaps(hdcDest, CAPS1) &C1_TRANSPARENT)
{
//Special transparency background mode.
oldMode = SetBkMode(hdcDest, NEWTRANSPARENT);
rgbBk = SetBkColor(hdcDest, rgbTransparent);
//Actual blt is a simple source copy; transparency is automatic.
BitBlt(hdcDest, x, y, dx, dy, hdcSrc, x0, y0, SRCCOPY);
SetBkColor(hdcDest, rgbBk);
SetBkMode(hdcDest, oldMode);
}Of course, this code doesn`t compile cause it don`t say what oldMode and rgbBk are variables of any particualar type. Even then NEWTRANSPARENT is undefined (I assumed whoever wrote the code #defined it to be = to 1, as SetBkMode's second argument is either 0 (Opaque) or 1(Transparent) as I understand it). I then assumed rgbTransparent to be the colour I don`t want copied onto the CDC (so I gave it a fixed value of 0 for black). Despite all this, it still don`t blinking work. This code would have been perfect for me, had it worked, because my programs layers need to be drawn (and re-drawn) fairly quickly. The strange thing is that Delphi actually manages to cope with this mechanism superbly, with not much performance loss at all, so I know there is a way get the desired results, but what is it? Anyway cheers all for reading this far (again), and cheers Tomasz for the quick reply, Alan.:confused: "When I left you I was but the learner, now I am the Master" - Darth Vader:mad:
-
I`m using XP Business Edition RC1 at the moment, so god knows what its gonna be like on this. I`m using an adapted version of Christians code, which uses a good few Blts, including a few SRCANDS (they really are speed killers) raster operations. I have heard of a method using one BitBlt (or StretchBlt) and SRCCOPY, but it doesn`t seem to work. I`ll post the code here, and if anyone can see whats wrong cool (remember this is NOT my code, so if it raises any questions, chances are I won`t know the answer).
//Only attempt this if device supports functionality.
if(GetDeviceCaps(hdcDest, CAPS1) &C1_TRANSPARENT)
{
//Special transparency background mode.
oldMode = SetBkMode(hdcDest, NEWTRANSPARENT);
rgbBk = SetBkColor(hdcDest, rgbTransparent);
//Actual blt is a simple source copy; transparency is automatic.
BitBlt(hdcDest, x, y, dx, dy, hdcSrc, x0, y0, SRCCOPY);
SetBkColor(hdcDest, rgbBk);
SetBkMode(hdcDest, oldMode);
}Of course, this code doesn`t compile cause it don`t say what oldMode and rgbBk are variables of any particualar type. Even then NEWTRANSPARENT is undefined (I assumed whoever wrote the code #defined it to be = to 1, as SetBkMode's second argument is either 0 (Opaque) or 1(Transparent) as I understand it). I then assumed rgbTransparent to be the colour I don`t want copied onto the CDC (so I gave it a fixed value of 0 for black). Despite all this, it still don`t blinking work. This code would have been perfect for me, had it worked, because my programs layers need to be drawn (and re-drawn) fairly quickly. The strange thing is that Delphi actually manages to cope with this mechanism superbly, with not much performance loss at all, so I know there is a way get the desired results, but what is it? Anyway cheers all for reading this far (again), and cheers Tomasz for the quick reply, Alan.:confused: "When I left you I was but the learner, now I am the Master" - Darth Vader:mad:
The key to adequate perf across all Win32 platforms is simple: you have to pre-calculate the mask. If you have the mask, it takes two blts to draw transparently. Creating the mask requires a two calls to Create[Compatible]Bitmap, two calls to CreateCompatibleDC and two BitBlts. It's *much* more expensive. Tomasz Sowinski -- http://www.shooltz.com
-
The key to adequate perf across all Win32 platforms is simple: you have to pre-calculate the mask. If you have the mask, it takes two blts to draw transparently. Creating the mask requires a two calls to Create[Compatible]Bitmap, two calls to CreateCompatibleDC and two BitBlts. It's *much* more expensive. Tomasz Sowinski -- http://www.shooltz.com
Right, got you. I`ve got something to work with now Tomasz, thanks for that. I`ll let you know how I get on. Thanks again for the quick response, Alan.:-D P.S. One more thing, I presume that once you`ve calculated the mask its stored as a HBITMAP variable, ready to be used over and over again? "When I left you I was but the learner, now I am the Master" - Darth Vader:mad:
-
Right, got you. I`ve got something to work with now Tomasz, thanks for that. I`ll let you know how I get on. Thanks again for the quick response, Alan.:-D P.S. One more thing, I presume that once you`ve calculated the mask its stored as a HBITMAP variable, ready to be used over and over again? "When I left you I was but the learner, now I am the Master" - Darth Vader:mad:
I presume that once you`ve calculated the mask its stored as a HBITMAP variable, ready to be used over and over again? Exaclty. I'm keeping it selected in its own memory DC to reduce overhead. Tomasz Sowinski -- http://www.shooltz.com
-
I presume that once you`ve calculated the mask its stored as a HBITMAP variable, ready to be used over and over again? Exaclty. I'm keeping it selected in its own memory DC to reduce overhead. Tomasz Sowinski -- http://www.shooltz.com
-
Hi all, I have finally managed to get a TransparentBlt function going in my program, the trouble is I`m finding it is too slow for what I need it to do (especially on larger images, with multiple layers). I am using a MDI app which supports multi layer bitmaps(hence the need for transparency). The OnDraw function is called every time I re-size the window etc. and so it ends up using blt many many times. So basically I wondered if : 1. Alpha Blend was faster? 2. Alpha Blend is capable of simply ommitting to change the color of a pixel where that pixel is the transparent color? I have only briefly looked through the alpha blend code, and it seems to merely blend two bitmaps together, thus creating a kind of duo-transparency. This isn`t particularly what I want, I just need to get rid of the main background colour of the foreground bitmaps. Any ideas on perhaps how I could speed up the process would be greatly received. Thankyou all. Alan.:| "When I left you I was but the learner, now I am the Master" - Darth Vader:mad:
As far as the functions I wrote are concerned, Alphablend is probably faster because it doesn't generate a mask on the fly, it accesses the bits directly. As Thomasz has said, if you're doing it over & over you're better off splitting the code so you only generate the mask once, that's probably faster again. The way to make my alpha blend function faster is to make sure the bitmap is a DIBSection to start with, then you can use GetObject to get the pointer to the underlying bits. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
-
As far as the functions I wrote are concerned, Alphablend is probably faster because it doesn't generate a mask on the fly, it accesses the bits directly. As Thomasz has said, if you're doing it over & over you're better off splitting the code so you only generate the mask once, that's probably faster again. The way to make my alpha blend function faster is to make sure the bitmap is a DIBSection to start with, then you can use GetObject to get the pointer to the underlying bits. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Thanks for the response Christian, I`ve decided to go and do it the Alpha Blend way, for the reasons you said. I already had a DIBSection, and the method became alot faster again. I also didn`t have to worry about masks and stuff either, which was the main problem I encountered with TransparentBlt (as Tomasz rightly pointed out). Cheers for all the help guys, very much appreciated.:-D "When I left you I was but the learner, now I am the Master" - Darth Vader:mad: