I have no idea how you could do this other than processing every pixel in the image as you'd have to read from the two source and write to the destination.
Well, there was an old trick I used to use when copying or clearing a buffer... the image data was a byte array but if I processed it as an integer array I could do my operations 4 bytes at a time, which gave significant speed improvements.
However, your image data here is a full integer anyway... four bytes for red, green, blue, and alpha. I don't know how you could "cheat" and process it more quickly.
A couple of optimizations you can do though...
* The less loops the better -- a single control loop is often better than a nested loop as there are less condition checks per iteration.
* Avoid conditionals in your loop if possible -- condition checks take time away from your calculations, use switch over if.
* Do not create/destroy objects in your loop -- C# memory management is expensive and needless in a loop.
* Avoid a foreach loop if you can -- Sometimes foreach loops create extra variables but for the most part they're actually ok. Just a precaution here.
For example... instead of:
- Color[,] data = new Color[200,200];
-
-
for (int i = 0; i < 200; i++)
-
{
-
for (int j = 0; i < 200; j++)
-
{
-
Color c = data[i,j];
-
if (c.R > 200) { ... }
-
else if (c.R ...) { ... }
-
else { ... }
-
}
-
}
Do this...
- Color[] data = new Color[200 * 200];
-
-
Color c;
-
for (int i = 0; i < 200 * 200; i++)
-
{
-
c = data[i];
-
switch (c.R)
-
{
-
case 200: ...
-
...
-
}
-
}
If you need the x & y coordinate of the pixel, you can calculate it fairly easily... the y value is the counter divided by the width and the x value is the remainder of that (mod).
If you need to do range checks, you're probably stuck with the if, but them's the breaks ;)