Click here to Skip to main content
Licence CPOL
First Posted 15 Mar 2007
Views 22,198
Downloads 222
Bookmarked 22 times

Quick n' Dirty Alpha Mask Generator

By | 15 Mar 2007 | Article
A quick-to-implement method for generating an alpha mask from a flat image file with no alpha channel
Screenshot - screenshot.jpg

Introduction

During a recent project at work, I needed a quick solution to apply transparency on-the-fly to a flat raster image provided by the user. After Googling like a mad man for ten minutes, I opted for a hand-rolled solution. This app demonstrates the use of the exceptionally inefficient, but effective result.

Using the Code

The sample app's single method goes through the source image pixel-by-pixel, takes a sampling of the surrounding pixels, and uses the mathematical mean of this sampling to calculate the base alpha value for that pixel. The lighter the sampling, the lower the calculated alpha value... e.g. a sampling that equals pure white would render the given pixel as fully transparent (alpha 0).

for(int i=0; i<9; i++)
{
  Color sample;
  //SOOOOOOO inefficient!
  switch(i)
  {
    //TopLeft
    case 0:    
      sample = (x>0 && y>0) ? 
        oldBitmap.GetPixel(x-1,y-1) :
        oldBitmap.GetPixel(x,y);
      sampleGrid[i] = (sample.R + sample.G + sample.B)/3;
      break;

    //TopCenter
    case 1:
      sample = (y>0) ? 
        oldBitmap.GetPixel(x,y-1) :
        oldBitmap.GetPixel(x,y);
      sampleGrid[i] = (sample.R + sample.G + sample.B)/3;
      break;
                                
    /* etc., etc. */    

It then weighs that pixel's computed alpha value against a user-set threshold... if the value is above the threshold, the pixel is given full opacity (alpha 255). This could be easily modified to treat the darker parts of an image as transparent.

  int A = 0;
  for(int i=0; i<9; i++)
    A += sampleGrid[i];
                    
  A = 255 - (A/9);
  if (A > (int)this.numThreshold.Value)
    A = 255;
  
  newBitmap.SetPixel(x,y, Color.FromArgb(A, color));

Points of Interest

I am quite certain that there are better ways to have implemented this, but I was on an insanely tight schedule, and this sufficed for the task at hand. If anyone would care to demonstrate a better implementation, I would greatly appreciate your feedback.

History

  • 15th March, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

patchwerk

Software Developer
the patchwerks
United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralConvolution PinmemberAdam Honeybell2:42 14 Jul '08  
GeneralNice work Pinmemberstixoffire6:55 22 Mar '07  
Generalmaketransparent PinmemberPuReBRaiNZ12:54 19 Mar '07  
GeneralRe: maketransparent Pinmemberpatch_werk5:55 1 Apr '07  
GeneralFastBitmap PinmemberPatrick Sears6:22 16 Mar '07  
GeneralRe: FastBitmap PinmemberCarlosMMartins23:44 19 Mar '07  
AnswerRe: FastBitmap Pinmemberstixoffire6:50 22 Mar '07  
GeneralRe: FastBitmap PinmemberPatrick Sears10:19 22 Mar '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 15 Mar 2007
Article Copyright 2007 by patchwerk
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid