Masking Unmasked!

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math Topics]   [Library]   [Utilities]

Search

 

Search DelphiForFun.org only

Support DFF

 

If you shop at Amazon anyway,  consider using this link. We receive a few cents from each purchase.   Thanks.

In Association with Amazon.com

 

Support DFF

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

 

 

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

 

Search DelphiForFun.org only

 

 

 

 

 

 

+  

=

 

 

Mask drawing is the technique for placing a non-rectangular image on top of another image.    Here is an example and the Delphi code to accomplish the trick.  

The complication is that  Windows only knows how to draw rectangles.  So what to we do with the  dark gray area around the balloon?   We get rid of it  a  4 step process likes this:

 

Step 1:  Create a mask image of the balloon that has all 1  pixels (white) values where the dark gray is, and 0 (black) pixels where  the balloon image existed.  
Step 2:  Make a copy of the balloon with all of dark gray pixels changed to Black.  (The implementation of this step takes two draw operations and is shown as  Steps 2A and 2B in the demo program output).  

Step 3: Put the mask on the cloud image using a logical AND operation.  The key points here:
bullet 0 AND anything is 0  (Black and anything is Black)
bullet1 AND anything remains unchanged (White and anything remains unchanged)

So ANDing our Black balloon image with the cloud image creates a  Black balloon shaped hole in the clouds without changing the surrounding image.

 

Step 4: OR the image created in Step 2 using the same coordinates that we used to draw the mask in Step 3.   For logical OR operations
bullet0 OR anything remains unchanged   (Black OR anything remains unchanged)
bullet1 OR anything is (White or anything is White) 

It's the first OR  property that we use in this step.  In the image created in Step 2, everything outside the balloon shape proper is Black so when ORed with clouds, the clouds remain unchanged.    For the balloon shape itself, Step 3 put a Black balloon shaped hole in the clouds which when ORed with the balloon pixels, fills the Black hole with the balloon image.   

Cool! 

If you have access to Delphi source code, you can browse the TransparentStretchBlt function in  the Graphics unit  to find Delphi's implementation of this.    TBitmap's Draw method calls it when Transparent property is True.  The  BrushCopy method of TCanvas uses it if BrushStyle is bsClear;      TImageList uses the Windows API function ImageList_DrawEx to accomplish the same thing, (I assume in the same manner.

Download  Delphi source for  MaskDraw demo,  just to prove that it really works.

Created: September 11, 2002

Modified: May 18, 2009

 

  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2012, Gary Darby    All rights reserved.