Browse Source

Optimize dither

pull/221/head
James Jackson-South 9 years ago
parent
commit
232bc7e796
  1. 2
      README.md
  2. 33
      src/ImageSharp/Processing/Processors/Binarization/BinaryThresholdProcessor.cs
  3. 3
      src/ImageSharp/Processing/Processors/Binarization/OrderedDitherProcessor.cs

2
README.md

@ -119,7 +119,7 @@ using (Image<Rgba32> image = new Image<Rgba32>(400, 400)
For optimized access within a loop it is recommended that the following methods are used. For optimized access within a loop it is recommended that the following methods are used.
1. `image.GetRowSpan(y)` 1. `image.GetRowSpan(y)`
2. `image.GetRowSPan(x, y)` 2. `image.GetRowSpan(x, y)`
For advanced pixel format usage there are multiple [PixelFormat implementations](https://github.com/JimBobSquarePants/ImageSharp/tree/master/src/ImageSharp/PixelFormats) available allowing developers to implement their own color models in the same manner as Microsoft XNA Game Studio and MonoGame. For advanced pixel format usage there are multiple [PixelFormat implementations](https://github.com/JimBobSquarePants/ImageSharp/tree/master/src/ImageSharp/PixelFormats) available allowing developers to implement their own color models in the same manner as Microsoft XNA Game Studio and MonoGame.

33
src/ImageSharp/Processing/Processors/Binarization/BinaryThresholdProcessor.cs

@ -83,25 +83,22 @@ namespace ImageSharp.Processing.Processors
startY = 0; startY = 0;
} }
using (PixelAccessor<TPixel> sourcePixels = source.Lock()) Parallel.For(
{ minY,
Parallel.For( maxY,
minY, this.ParallelOptions,
maxY, y =>
this.ParallelOptions, {
y => Span<TPixel> row = source.GetRowSpan(y - startY);
for (int x = minX; x < maxX; x++)
{ {
int offsetY = y - startY; ref TPixel color = ref row[x - startX];
for (int x = minX; x < maxX; x++)
{ // Any channel will do since it's Grayscale.
int offsetX = x - startX; color = color.ToVector4().X >= threshold ? upper : lower;
TPixel color = sourcePixels[offsetX, offsetY]; }
});
// Any channel will do since it's Grayscale.
sourcePixels[offsetX, offsetY] = color.ToVector4().X >= threshold ? upper : lower;
}
});
}
} }
} }
} }

3
src/ImageSharp/Processing/Processors/Binarization/OrderedDitherProcessor.cs

@ -97,11 +97,12 @@ namespace ImageSharp.Processing.Processors
for (int y = minY; y < maxY; y++) for (int y = minY; y < maxY; y++)
{ {
int offsetY = y - startY; int offsetY = y - startY;
Span<TPixel> row = source.GetRowSpan(offsetY);
for (int x = minX; x < maxX; x++) for (int x = minX; x < maxX; x++)
{ {
int offsetX = x - startX; int offsetX = x - startX;
TPixel sourceColor = source[offsetX, offsetY]; TPixel sourceColor = row[offsetX];
this.Dither.Dither(source, sourceColor, this.UpperColor, this.LowerColor, bytes, this.Index, offsetX, offsetY, maxX, maxY); this.Dither.Dither(source, sourceColor, this.UpperColor, this.LowerColor, bytes, this.Index, offsetX, offsetY, maxX, maxY);
} }
} }

Loading…
Cancel
Save