Replies: 2 comments 3 replies
-
You should remove your bounds checks. You have three per loop. You can use P.S There are built in methods available that do grayscale properly. Using the average loses too much information. I recommend using BenchmarkDotNet for future benchmarking for better accuracy. |
Beta Was this translation helpful? Give feedback.
-
Hi, Thanks for your reply. for (int i = 0; i < 1000; i++)
{
image.Mutate(x => x
.Grayscale()
); As suggested, I changed the code to the following: for (int i = 0; i < 1000; i++)
{
for (int y = 0; y < hgt; y++)
{
ref Rgba32 rowBase = ref MemoryMarshal.GetReference(image.GetPixelRowSpan(y));
for (int x = 0; x < wid; x++)
{
Rgba32 pixelColor = Unsafe.Add(ref rowBase, x);
gray = (byte)((pixelColor.R + pixelColor.G + pixelColor.B) / 3);
rowBase = gs[gray]; //this needs to be changed I suppose?
}
}
} I am not sure if this is the correct way as speed is not much improved with this change. Please suggest. |
Beta Was this translation helpful? Give feedback.
-
I've been testing speed performance of ImageSharp and Bitmap.LockBits by running a function in loop to change a bitmap to grayscale. The result showed that a loop for 1000 times took approx. 11 seconds using Bitmap.LockBits while it took around 37 seconds using ImageSharp. Below is the code I've used in ImageSharp. Please suggest if there can be any improvements to improve the speed.
Beta Was this translation helpful? Give feedback.
All reactions