Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Thursday, January 3, 2008 4:10:04 AM(UTC)
planespace

Groups: Registered
Posts: 8


This is a long set of questions so I'll put the most important question here at the top: Is there a way to combine a source
bitmap that has a region against a destination bitmap while applying alpha blending at the same time?

Here's some explanation:

There are two parts to my question but first I'll explain what I'm trying to do.  I'm trying to provide watermarking capabilities in two ways: 1) an additional image that is overlayed on top of the original image and 2) text that is drawn on top of the original image.

These are pretty simple to solve using Lead tools L_AlphaBlendBitmap and/or L_CombineBitmap.  There are two problems though.  The first has to do with the image overlay part:  what I'd really like to do is, if a PNG file is chosen as an overlay image then overlay the png file maintaining transparency as specified in the png's alpha channel.  That appears to not be possible using the raster api.  I base that on other forum posts I've seen - please correct me if i'm wrong.

The second part is overlaying text.  To do that, I use L_CreateLeadDC and just draw text using GDI.  That works great.  But things seem to get complicated if I want to draw that text semi-transparent.  I don't believe it's possible to do that with GDI so I opted to draw to a second BITMAPHANDLE that is the same size as the original.  I follow these steps (pImage is the original):

// Create the overlay
L_CreateBitmap(&overlayImage,sizeof(BITMAPHANDLE),

        TYPE_CONV,BITMAPWIDTH(pImage),BITMAPHEIGHT(pImage),
        pImage->BitsPerPixel,pImage->Order,pImage->pPalette,
        pImage->ViewPerspective,NULL,NULL);



// Fill it with my transparency color
L_FillBitmap(&overlayImage,RGB(0,255,0));

HDC dc = L_CreateLeadDC(&overlayImage);

// Draw the text on the overlay
....

L_DeleteLeadDC(hDc);

// Select the text region
L_SetBitmapRgnColor(&overlayImage,RGB(0,255,0),L_RGN_SETNOT);

// Combine the overlay with the original (using only the selected region)
L_CombineBitmap(pImage,0,0,BITMAPWIDTH(pImage),BITMAPHEIGHT(pImage),&overlayImage,0,0,CB_OP_ADD|CB_DST_0);


This works great except for the opacity.  It turns out L_AlphaBlendBitmap ignores the region which is unfortunate because that would solve my problem.  So, is there a way to combine a source bitmap with a region against a destination bitmap while applying alpha blending at the same time?
 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

#2 Posted : Thursday, January 3, 2008 8:52:24 AM(UTC)

GregR  
GregR

Groups: Registered, Tech Support, Administrators
Posts: 766


Good, long question...easy answer.  You might even kick yourself for being so close ;-).

You can use the L_FeatherAlphaBlendBitmap function to combine images together with an alpha mask.  This will work just like L_CombineBitmap, but has an additional 8bpp mask image which serves as the alpha channel.  As for loading a PNG and preserving its alpha channel, you can get the mask from the image with L_GetBitmapAlpha.  You'll have to create the mask manually for text, but based on your snippets, you pretty much have that process down (creating a copy, getting regions, filling regions with colors, etc.).

As for the other posts where you said they made it seem impossible, could you let me know what they are so I can correct them if necessary?
 
#3 Posted : Thursday, January 3, 2008 1:01:44 PM(UTC)
planespace

Groups: Registered
Posts: 8


Thanks!

This worked great once I started mucking with the mask to show the part of the image that needed to be visible but semitransparent.  For posterity:

    BITMAPHANDLE overlayImage;
    L_INT err =
        L_CreateBitmap(&overlayImage,sizeof(BITMAPHANDLE),
        TYPE_CONV,BITMAPWIDTH(pImage),BITMAPHEIGHT(pImage),
        8,ORDER_BGR,NULL,
        pImage->ViewPerspective,NULL,NULL);

    if (err == SUCCESS)
    {

        // Transparent color
        L_FillBitmap(&overlayImage,RGB(0,255,0));

        HDC hDc = L_CreateLeadDC(&overlayImage);

       // Do my drawing to the dc using a non-green color

        L_DeleteLeadDC(hDc);

        // Now create the mask
        BITMAPHANDLE overlayMask;               
        L_CopyBitmap(&overlayMask,&overlayImage,sizeof(BITMAPHANDLE));

        // Make visible area
        L_SetBitmapRgnColor(&overlayMask,RGB(0,255,0),L_RGN_SETNOT);
        L_FillBitmap(&overlayMask,RGB(opacity,opacity,opacity));
        L_FreeBitmapRgn(&overlayMask);

        // Make mask area
        L_SetBitmapRgnColor(&overlayMask,RGB(0,255,0),L_RGN_SET);
        L_FillBitmap(&overlayMask,RGB(0,0,0));
        L_FreeBitmapRgn(&overlayMask);

        // Now blend mask,overlay and destination
        L_FeatherAlphaBlendBitmap(pImage,0,0,
            BITMAPWIDTH(pImage),BITMAPHEIGHT(pImage),
            &overlayImage,0,0,&overlayMask,0,0);

        L_FreeBitmap(&overlayImage);
        L_FreeBitmap(&overlayMask);

 
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2020, Yet Another Forum.NET
This page was generated in 0.192 seconds.