I have a tool with caterpillar slider controls that are used to adjust brightness, contrast, gamma, etc.
I am trying to get real-time updates for my image while the user drags the slider. Brightness and gamma algorithms are an acceptable speed (about 170 ms). But the contrast algorithm is about 380 ms.
Basically my form is a window with sliders. Each time the image is updated, it dispatches an event to the parent object, which redraws the new image. The tool window stores the original unmodified image in memory, so I always have access to its bytes. So basically, I do this every time the ValueChanged event for the slider (for example, the Contrast slider) changes.
- LockBits of the working (target) bitmap in Format24bppRgb format (the original bitmap is in 32bppPArgb format)
- Marshal. Copy bits to byte [] array
- Check which operation I perform (which slider was selected)
- Use the following code for Contrast:
the code:
double newValue = 0; double c = (100.0 + contrast) / 100.0; c *= c; for (int i = 0; i < sourcePixels.Length; i++) { newValue = sourcePixels[i]; newValue /= 255.0; newValue -= 0.5; newValue *= c; newValue += 0.5; newValue *= 255; if (newValue < 0) newValue = 0; if (newValue > 255) newValue = 255; destPixels[i] = (byte)newValue; }
I read once about using an integer instead of floating point values ββto increase the speed of contrast, but I could not find this article again.
I tried using unsafe code (pointers), but actually noticed a decrease in speed. I guess this was because the code used nested for loops to iterate x and y instead of a single loop.
performance c # bitmap contrast
Trevor elliott
source share