5,667,575 members and growing! (15,822 online)
Email Password   helpLost your password?
Multimedia » General Graphics » Graphics     Intermediate

Painless yet unsafe grayscale conversion in C#

By Ennis Ray Lynch, Jr.

This is an article using pointer arithmetic for a quick conversion of an image to grayscale.
C#, Windows, .NET, Visual Studio, GDI+, Dev

Posted: 17 Apr 2006
Updated: 17 Apr 2006
Views: 41,521
Bookmarked: 32 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
14 votes for this Article.
Popularity: 4.03 Rating: 3.52 out of 5
5 votes, 35.7%
1
0 votes, 0.0%
2
2 votes, 14.3%
3
2 votes, 14.3%
4
5 votes, 35.7%
5

original image

modified image

Introduction

I was busy trying to write a motion detection algorithm in C#. To determine motion, I compared change values from a previous image by calculating the absolute value of the pixel difference. For doing this, grayscale is easier and probably faster.

Background

If MIT thinks using grayscale for motion detection is a good idea, then I am definitely on the right track with my motion detection algorithm. For a reference, see the Cog project.

I got the code for the image loop from here because I originally attempted to use SetPixel which is painfully slow.

Using the code

If you use this code in a multithreaded environment, be aware that the parameter image will be locked until this method unlocks it. This will raise an exception when another code attempts to lock the bitmap, such as a customer OnPaint. To avoid this, you can always make a copy of the bitmap.

Here is the entire method:

public Bitmap processImage(Bitmap image){
    Bitmap returnMap = new Bitmap(image.Width, image.Height, 
                           PixelFormat.Format32bppArgb);
    BitmapData bitmapData1 = image.LockBits(new Rectangle(0, 0, 
                             image.Width, image.Height), 
                             ImageLockMode.ReadOnly, 
                             PixelFormat.Format32bppArgb);
    BitmapData bitmapData2 = returnMap.LockBits(new Rectangle(0, 0, 
                             returnMap.Width, returnMap.Height), 
                             ImageLockMode.ReadOnly, 
                             PixelFormat.Format32bppArgb);
    int a = 0;
    unsafe {
        byte* imagePointer1 = (byte*)bitmapData1.Scan0;
        byte* imagePointer2 = (byte*)bitmapData2.Scan0;
        for(int i = 0; i < bitmapData1.Height; i++) {
            for(int j = 0; j < bitmapData1.Width; j++) {
                // write the logic implementation here

                a = (imagePointer1[0] + imagePointer1[1] + 
                     imagePointer1[2])/3;
                imagePointer2[0] = (byte)a;
                imagePointer2[1] = (byte)a;
                imagePointer2[2] = (byte)a;
                imagePointer2[3] = imagePointer1[3];
                //4 bytes per pixel

                imagePointer1 += 4;
                imagePointer2 += 4;
            }//end for j

            //4 bytes per pixel

            imagePointer1 += bitmapData1.Stride - 
                            (bitmapData1.Width * 4);
            imagePointer2 += bitmapData1.Stride - 
                            (bitmapData1.Width * 4);
        }//end for i

    }//end unsafe

    returnMap.UnlockBits(bitmapData2);
    image.UnlockBits(bitmapData1);
    return returnMap;
}//end processImage

The PixelFormat is crucial. This format determines the layout of the bitmap data. This is why the literal 4 is hard-coded (actually, it is hard-coded because I am lazy). If you use a different PixelFormat, you will need to determine the layout and offset for those formats.

BitmapData bitmapData1 = image.LockBits(new Rectangle(0, 0, 
                         image.Width, image.Height), 
                         ImageLockMode.ReadOnly, 
                         PixelFormat.Format32bppArgb);
BitmapData bitmapData2 = returnMap.LockBits(new Rectangle(0, 0, 
                         returnMap.Width, returnMap.Height), 
                         ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

This code locks the bitmap and returns its image data. Since I am converting the entire image to grayscale, I lock the entire bitmap. It is possible to lock a smaller area.

byte* imagePointer1 = (byte*)bitmapData1.Scan0;
byte* imagePointer2 = (byte*)bitmapData2.Scan0;

Pointer arithmetic is unsafe in C# :(. Too bad because it is fast! Although, it is strange getting Access Violation errors in C#. If you change the code, test thoroughly using Mathematics to make sure you don't overwrite any protected memory. Scan0 is the pointer to the first byte of the bitmap's data.

for(int i = 0; i < bitmapData1.Height; i++) {
    for(int j = 0; j < bitmapData1.Width; j++) {

The astute will immediately recognize this will only work if the bitmaps are the same size. And fortunately, this always is since this method will create the returnMap bitmap based on the input bitmap. An incorrect loop will also cause an Access Violation.

imagePointer2[0] = (byte)a; //Array index 0 is blue

imagePointer2[1] = (byte)a; //Array index 1 is green

imagePointer2[2] = (byte)a; //Array index 2 is red

imagePointer2[3] = imagePointer1[3]; //Array index 3 is alpha

See the comments in the code snippet above, to understand the layout of the bitmap data.

a = (imagePointer1[0] + imagePointer1[1] + imagePointer1[2])/3;

Average the three color components in the original bitmap. Keep the alpha the same, otherwise your grayscale will also cause undesired blending.

imagePointer1 += 4;
imagePointer2 += 4;

Move forward 4 bytes:

imagePointer1 += bitmapData1.Stride - (bitmapData1.Width * 4);
imagePointer2 += bitmapData1.Stride - (bitmapData1.Width * 4);

This is definitely the most confusing part. See this for a picture of what is happening. The width of a bitmap is actually the composed width and an unused buffer to pad the bitmap to be a multiple of 4. Width + buffer = stride. It works, so good enough for me.

returnMap.UnlockBits(bitmapData2);
image.UnlockBits(bitmapData1);
return returnMap;

UnlockBits unlocks the bitmap data, probably a good thing to do.

Points of Interest

A good place to find answers to hard problems: Google.

People much smarter than me: MIT Cog Project.

Possible Errors

I use the bitmap data from bitmapData1 for both bitmapData1 and bitmapData2. This could lead to unexpected errors if the data were some how different. However, in testing, everything works fine. A lot more prudent coder would verify a lot of things before putting this sort of code into anything critical.

If there are any more errors, I am quite sure that the overly critical among you will gladly point them out as glaring criticisms.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Ennis Ray Lynch, Jr.


WTF, this Bio is a PITA, the tool keeps reformatting a dumping stuff.

My company is ERL GLOBAL, INC. I develop software, and I do it right. I mean, who is John Galt really?

There is a recurring joke about the name of my company on CP but I don't get it. One day someone will explain it to me.
Occupation: Architect
Company: ERL GLOBAL, INC
Location: United States United States

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Really cool visual FX
    A set of classes for doing stunning visual effects, including water, plasma and fire.
  • ImageStone
    An article on a library for image manipulation.
Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 30 (Total in Forum: 30) (Refresh)FirstPrevNext
Generalpointer indexingmemberkanza azhar12:42 22 Apr '08  
GeneralRe: pointer indexingmemberEnnis Ray Lynch, Jr.12:53 22 Apr '08  
QuestionTypo in ImageLockMode?memberdwieringa12:46 5 Sep '07  
AnswerYou would thinkmemberEnnis Ray Lynch, Jr.13:33 5 Sep '07  
GeneralSimple improvement to make it 3 times fastermembermattrs2:01 18 May '07  
GeneralI can believe I missed thatmemberEnnis Ray Lynch, Jr.3:42 18 May '07  
QuestionStride Question and Issue [modified]memberjouwpaard2:03 21 Sep '06  
AnswerRe: Stride Question and IssuememberEnnis Ray Lynch, Jr.14:54 21 Sep '06  
GeneralCheesy alternativememberRavi Bhavnani13:58 18 Apr '06  
GeneralRe: Cheesy alternativemembermrBussy5:12 25 Apr '06  
JokeUse ColorMatrixmemberRay Hayes12:00 18 Apr '06  
General:( Too Slowmemberelynch12:55 18 Apr '06  
GeneralRe: :( Too SlowmemberRay Hayes13:06 18 Apr '06  
GeneralRe: :( Too Slowmemberelynch13:17 18 Apr '06  
GeneralRe: :( Too SlowmemberRay Hayes13:34 18 Apr '06  
GeneralApologiesmemberAyyaz Ahmed7:54 18 Apr '06  
GeneralRe: Apologiesmemberelynch8:07 18 Apr '06  
GeneralRe: ApologiesmemberAyyaz Ahmed8:22 18 Apr '06  
GeneralRe: Apologiesmemberelynch8:44 18 Apr '06  
GeneralRe: ApologiesstaffNishant Sivakumar9:18 18 Apr '06  
GeneralRe: ApologiesmemberLukas Fellechner5:01 26 Apr '07  
GeneralRe: ApologiesmemberCoLithium13:32 26 Apr '06  
General[Message Deleted]memberAyyaz Ahmed23:53 17 Apr '06  
GeneralRe: Very Poor Conversion Algo.memberDirk Veldhuis0:14 18 Apr '06  
GeneralRe: Very Poor Conversion Algo.memberaparra1:43 18 Apr '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Apr 2006
Editor: Smitha Vijayan
Copyright 2006 by Ennis Ray Lynch, Jr.
Everything else Copyright © CodeProject, 1999-2008
Web13 | Advertise on the Code Project