Speeding Up Clear

Aug 25, 2010 at 8:37 PM

You should be able to speed up the Clear method by using divide and conquer:

 

public static void Clear(this WriteableBitmap bmp, Color color)
{
float ai = color.A * PreMultiplyFactor;
int col = (color.A << 24) | ((byte)(color.R * ai) << 16) | ((byte)(color.G * ai) << 8) | (byte)(color.B * ai);
int[] pixels = bmp.Pixels;
int w = bmp.PixelWidth;
int h = bmp.PixelHeight;
int len = w * SizeOfARGB;

// Fill first line
for (int x = 0; x < w; x++)
{
pixels[x] = col;
}

// Copy first line
int blockHeight = 1;
int y = 1;
while (y < h)
{
Buffer.BlockCopy(pixels, 0, pixels, y * len, blockHeight * len);
y += blockHeight;
blockHeight = Math.Min(2 * blockHeight, h - y);
}
}

Instead of copying a scanline at a time, this doubles the amount of data copied per call until it fills the area - roughly log2(h) calls to BlockCopy instead on h-1 calls.
Coordinator
Aug 26, 2010 at 7:16 AM
Edited Aug 26, 2010 at 7:16 AM

Hi Plinth,

totally makes sense. How did I miss this. ;) I just checked the changed part in.

Thanks a lot for this.

Would you mind if I put your name on the projects home site under "Contributors". Do you have a internet site and a real name? :)

Aug 27, 2010 at 12:19 PM

How nice of you to offer –

You can credit me as Steve Hawley and probably the best link is my blog:

http://www.atalasoft.com/cs/blogs/stevehawley/default.aspx

Steve Hawley
Chief Software Architect

Atalasoft, Inc.

413-642-4074 |

.NET and SharePoint Imaging Specialists

From: teichgraf [mailto:notifications@codeplex.com]
Sent: Thursday, August 26, 2010 3:16 AM
To: Steve Hawley
Subject: Re: Speeding Up Clear [WriteableBitmapEx:224835]

From: teichgraf

Hi Plinth,

totally makes sense. How did I miss this. ;) I just checked the changed part in.

Would you mind if I put your name on the projects home site under "Contributors". Do you have a internet site and a real name? :)

Read the full discussion online.

To add a post to this discussion, reply to this email (WriteableBitmapEx@discussions.codeplex.com)

To start a new discussion for this project, email WriteableBitmapEx@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com