🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Transparancy/Opacity using the *Blt functions

#16334
From: Ted Lyngmo
Date: Fri, 16 May 2003 13:23
59 lines
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
Technical Details
Message-ID: <68a07e12.0305160523.79ac30d9@posting.google.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,a992b6e9cdc44901,start
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-05-16 06:23:21 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!not-for-mail
From: ted@lyncon.se (Ted Lyngmo)
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Transparancy/Opacity using the *Blt functions
Date: 16 May 2003 06:23:21 -0700
Organization: http://groups.google.com/
Lines: 59
Message-ID: <68a07e12.0305160523.79ac30d9@posting.google.com>
NNTP-Posting-Host: 62.127.121.67
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 1053091401 14820 127.0.0.1 (16 May 2003 13:23:21 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: 16 May 2003 13:23:21 GMT