Thread View: borland.public.cppbuilder.nativeapi
1 messages
1 total messages
Started by Ted Lyngmo
Fri, 16 May 2003 13:23
Transparancy/Opacity using the *Blt functions
Author: Ted Lyngmo
Date: Fri, 16 May 2003 13:23
Date: Fri, 16 May 2003 13:23
59 lines
3016 bytes
3016 bytes
Hi!
I've got some bitmaps (TImages) in which each pixel has an opacity
value encoded. I'd like to copy each bitmap to one common destination
bitmap, so that the first copied bitmap becomes the background, and
the last the foreground. I have a working solution, but it's way to
slow, so I'd rather learn how to use the *Blt functions. Some example
code would be great! Flickering isn't a big concern, but speed is.
This is how I've solved it today. This code contain a low quality
stretching functionality. Any suggested *Blt solution doesn't have to
include stretching.
#define GetOValue(col) ((col&0xFF000000)>>24) // macro to decode
opacity value
class Layer {
TObject *obj; // pointer to an object I'd like to render
void Draw(TCanvas *Dest, TRect &DestRect) {
// ...here I check what kind of object I've stored in 'obj'.
// If it's a TImage, see below:
TImage *imgSource=(TImage*)obj; // Source TImage
int x,y, r,g,b,
destw=DestRect.Width(),
desth=DestRect.Height(),
sourcew=imgSource->Width,
sourceh=imgSource->Height;
TColor sourcecol, destcol;
int opacity;
for( x=0; x<destw; x++ ) {
for( y=0; y<desth; y++ ) {
// Extract source color
sourcecol=imgSource->Canvas->Pixels[sourcew*x/destw][sourceh*y/desth];
// Extract existing destination color
destcol=Dest->Pixels[DestRect.Left+x][DestRect.Top+y];
// Extract opacity value from the source color
opacity=GetOValue(sourcecol);
// Calc new rgb values for the destination
r=(GetRValue(sourcecol)*opacity/255+GetRValue(destcol)*(255-opacity)/255);
g=(GetGValue(sourcecol)*opacity/255+GetGValue(destcol)*(255-opacity)/255);
b=(GetBValue(sourcecol)*opacity/255+GetBValue(destcol)*(255-opacity)/255);
// Set the new color
Dest->Pixels[DestRect.Left+x][DestRect.Top+y] =
(TColor) RGB(r,g,b);
}
}
}
};
Kind regards,
Ted Lyngmo
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads