Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected override void DoWork(IProgress<string> progress, CancellationToken cancellationToken)
- {
- cancellationToken.Register(SwitchToCancelledState);
- var processorCount = Environment.ProcessorCount;
- using (var bmp = new Bitmap(fileName))
- {
- // Lock the bitmap's bits.
- var rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
- var bmpData =
- bmp.LockBits(rect, ImageLockMode.ReadWrite,
- bmp.PixelFormat);
- // Get the address of the first line.
- var ptr = bmpData.Scan0;
- // Declare an array to hold the bytes of the bitmap.
- var bytes = Math.Abs(bmpData.Stride) * bmp.Height;
- var rgbValues = new byte[bytes];
- // Copy the RGB values into the array.
- System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
- var partSize = rgbValues.Length / processorCount;
- var oneQuarter = rgbValues.Length / 100 * 25;
- var i = -25;
- var counter = 0;
- for (var j = 0; j <= rgbValues.Length - partSize; j += partSize)
- {
- var start = j;
- var end = j + partSize - 1;
- Task.Run(() =>
- {
- for (var k = start; k <= end; k++)
- {
- rgbValues[k] = (byte) (rgbValues[k] + brightnessChange);
- Interlocked.Increment(ref counter);
- if (counter % oneQuarter != 0) return;
- i += 25;
- progress.Report($"Brightness job (ID {Id}) - {i}%");
- }
- }, cancellationToken);
- }
- Task.WaitAll();
- // Copy the RGB values back to the bitmap
- System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
- // Unlock the bits.
- bmp.UnlockBits(bmpData);
- // Save image
- bmp.Save(Paths.GetOutputImageFullName(Id, "brightness" + brightnessChange), ImageFormat.Jpeg);
- }
- SwitchToFinishedState($"Changed brightness by {brightnessChange} point(s)");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement