🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #16348

Transparancy/Opacity using the *Blt functions

#16348
From: Ted Lyngmo
Date: Sat, 17 May 2003 08:46
67 lines
3367 bytes
(sorry if this appears twice)

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.

// macro to decode opacity value
#define GetOValue(col) ((col&0xFF000000)>>24)

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

-- 
Replace MY_FIRST_NAME in my email address with my first name to send a private
email to me.
Technical Details
Message-ID: <3EC5F6C8.4020903@lyncon.se>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!border3.nntp.aus1.giganews.com!border1.nntp.aus1.giganews.com!nntp.giganews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,a992b6e9cdc44901
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-05-17 01:47:36 PST
Message-ID: <3EC5F6C8.4020903@lyncon.se>
Date: Sat, 17 May 2003 10:46:00 +0200
From: Ted Lyngmo <MY_FIRST_NAME@lyncon.se>
Organization: Lyncon - Computer & Music consulting
User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:0.9.4) Gecko/20011128 Netscape6/6.2.1
X-Accept-Language: sv,en
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Transparancy/Opacity using the *Blt functions 
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 217.215.147.98
X-Trace: newsgroups.borland.com 1053160810 217.215.147.98 (17 May 2003 01:40:10 -0700)
Lines: 68
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!border3.nntp.aus1.giganews.com!border1.nntp.aus1.giganews.com!nntp.giganews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail