1. Replace 32bppPArgb bitmap with true graylevel bitmap which has one byte per pixel.
2. int color = pixels[++index];
This requires calculating of pixel offset. Replace this with unsafe pointer which is incremented in every iteration.
3. Make some math optimizations. For example:
if (color < (ImgLevel - ImgWidth / 2))
ImgLevel - ImgWidth / 2 can be calculated once before loop. However, I beleive that JIT compiler optimizes this.
if (ImgWidth == 0)
Is this test really necessary?
Main Topics
Browse All Topics





by: _TAD_Posted on 2007-04-03 at 07:51:39ID: 18843747
A few things....
1) How much of your CPU is being used for this process? If you aren't using 100% of your CPU, you can multithread this piece and have 1 thread convert the top half of the image and the second thread convert the bottom half (or have more threads).
2) Division is an expensive process. I'm not sure how .Net optimizes its code, but on the cpu level multiplying by 0.5 is 20 times faster than dividing by 2. .Net may do this conversion for you, but it's worth the small change just to test it out.
3) A switch statement is, in most cases, most efficient. If/else blocks can approach the efficiency of a switch statement, but will never be faster than a switch.
4) I also see that you are doing the same calculation over and over and over again within the loop. By the time you get into the loop, the ImgWidth is constant - don't divide by 2 for each itteration. Create a new variable halfImgWidth and work with that value.