Masking Unmasked!

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

 

Search

Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

 

Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 

     

We receive a few cents from each purchase.  Thanks

 


Support DFF - Donate

 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.

Mensa Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa 365 Puzzlers  Calendar 2017

Mensa 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)

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: July 29, 2017

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