Browse Source

Use float threshold for dither

pull/456/head
James Jackson-South 8 years ago
parent
commit
42cbb47bc3
  1. 2
      src/ImageSharp/Dithering/Ordered/IOrderedDither.cs
  2. 2
      src/ImageSharp/Dithering/Ordered/OrderedDither.cs
  3. 4
      src/ImageSharp/Processing/Processors/Binarization/BinaryErrorDiffusionProcessor.cs
  4. 4
      src/ImageSharp/Processing/Processors/Binarization/BinaryOrderedDitherProcessor.cs
  5. 4
      src/ImageSharp/Processing/Processors/Dithering/ErrorDiffusionPaletteProcessor.cs
  6. 4
      src/ImageSharp/Processing/Processors/Dithering/OrderedDitherPaletteProcessor.cs

2
src/ImageSharp/Dithering/Ordered/IOrderedDither.cs

@ -21,7 +21,7 @@ namespace SixLabors.ImageSharp.Dithering
/// <param name="x">The column index.</param> /// <param name="x">The column index.</param>
/// <param name="y">The row index.</param> /// <param name="y">The row index.</param>
/// <typeparam name="TPixel">The pixel format.</typeparam> /// <typeparam name="TPixel">The pixel format.</typeparam>
void Dither<TPixel>(ImageFrame<TPixel> image, TPixel source, TPixel upper, TPixel lower, byte threshold, int x, int y) void Dither<TPixel>(ImageFrame<TPixel> image, TPixel source, TPixel upper, TPixel lower, float threshold, int x, int y)
where TPixel : struct, IPixel<TPixel>; where TPixel : struct, IPixel<TPixel>;
} }
} }

2
src/ImageSharp/Dithering/Ordered/OrderedDither.cs

@ -41,7 +41,7 @@ namespace SixLabors.ImageSharp.Dithering
} }
/// <inheritdoc /> /// <inheritdoc />
public void Dither<TPixel>(ImageFrame<TPixel> image, TPixel source, TPixel upper, TPixel lower, byte threshold, int x, int y) public void Dither<TPixel>(ImageFrame<TPixel> image, TPixel source, TPixel upper, TPixel lower, float threshold, int x, int y)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
image[x, y] = this.thresholdMatrix[y % this.modulusY, x % this.modulusX] >= threshold ? lower : upper; image[x, y] = this.thresholdMatrix[y % this.modulusY, x % this.modulusX] >= threshold ? lower : upper;

4
src/ImageSharp/Processing/Processors/Binarization/BinaryErrorDiffusionProcessor.cs

@ -93,7 +93,7 @@ namespace SixLabors.ImageSharp.Processing.Processors
sourcePixel.ToRgba32(ref rgba); sourcePixel.ToRgba32(ref rgba);
// Convert to grayscale using ITU-R Recommendation BT.709 if required // Convert to grayscale using ITU-R Recommendation BT.709 if required
byte luminance = (byte)(isAlphaOnly ? rgba.A : (.2126F * rgba.R) + (.7152F * rgba.G) + (.0722F * rgba.B)).Clamp(0, 255); float luminance = isAlphaOnly ? rgba.A : (.2126F * rgba.R) + (.7152F * rgba.G) + (.0722F * rgba.B);
for (int y = startY; y < endY; y++) for (int y = startY; y < endY; y++)
{ {
@ -108,7 +108,7 @@ namespace SixLabors.ImageSharp.Processing.Processors
if (!previousPixel.Equals(sourcePixel)) if (!previousPixel.Equals(sourcePixel))
{ {
sourcePixel.ToRgba32(ref rgba); sourcePixel.ToRgba32(ref rgba);
luminance = (byte)(isAlphaOnly ? rgba.A : (.2126F * rgba.R) + (.7152F * rgba.G) + (.0722F * rgba.B)).Clamp(0, 255); luminance = isAlphaOnly ? rgba.A : (.2126F * rgba.R) + (.7152F * rgba.G) + (.0722F * rgba.B);
// Setup the previous pointer // Setup the previous pointer
previousPixel = sourcePixel; previousPixel = sourcePixel;

4
src/ImageSharp/Processing/Processors/Binarization/BinaryOrderedDitherProcessor.cs

@ -74,7 +74,7 @@ namespace SixLabors.ImageSharp.Processing.Processors
sourcePixel.ToRgba32(ref rgba); sourcePixel.ToRgba32(ref rgba);
// Convert to grayscale using ITU-R Recommendation BT.709 if required // Convert to grayscale using ITU-R Recommendation BT.709 if required
byte luminance = (byte)(isAlphaOnly ? rgba.A : (.2126F * rgba.R) + (.7152F * rgba.G) + (.0722F * rgba.B)).Clamp(0, 255); float luminance = isAlphaOnly ? rgba.A : (.2126F * rgba.R) + (.7152F * rgba.G) + (.0722F * rgba.B);
for (int y = startY; y < endY; y++) for (int y = startY; y < endY; y++)
{ {
@ -89,7 +89,7 @@ namespace SixLabors.ImageSharp.Processing.Processors
if (!previousPixel.Equals(sourcePixel)) if (!previousPixel.Equals(sourcePixel))
{ {
sourcePixel.ToRgba32(ref rgba); sourcePixel.ToRgba32(ref rgba);
luminance = (byte)(isAlphaOnly ? rgba.A : (.2126F * rgba.R) + (.7152F * rgba.G) + (.0722F * rgba.B)).Clamp(0, 255); luminance = isAlphaOnly ? rgba.A : (.2126F * rgba.R) + (.7152F * rgba.G) + (.0722F * rgba.B);
// Setup the previous pointer // Setup the previous pointer
previousPixel = sourcePixel; previousPixel = sourcePixel;

4
src/ImageSharp/Processing/Processors/Dithering/ErrorDiffusionPaletteProcessor.cs

@ -82,7 +82,7 @@ namespace SixLabors.ImageSharp.Processing.Processors
sourcePixel.ToRgba32(ref rgba); sourcePixel.ToRgba32(ref rgba);
// Convert to grayscale using ITU-R Recommendation BT.709 if required // Convert to grayscale using ITU-R Recommendation BT.709 if required
byte luminance = (byte)(isAlphaOnly ? rgba.A : (.2126F * rgba.R) + (.7152F * rgba.G) + (.0722F * rgba.B)).Clamp(0, 255); float luminance = isAlphaOnly ? rgba.A : (.2126F * rgba.R) + (.7152F * rgba.G) + (.0722F * rgba.B);
for (int y = startY; y < endY; y++) for (int y = startY; y < endY; y++)
{ {
@ -98,7 +98,7 @@ namespace SixLabors.ImageSharp.Processing.Processors
{ {
pair = this.GetClosestPixelPair(ref sourcePixel, this.Palette); pair = this.GetClosestPixelPair(ref sourcePixel, this.Palette);
sourcePixel.ToRgba32(ref rgba); sourcePixel.ToRgba32(ref rgba);
luminance = (byte)(isAlphaOnly ? rgba.A : (.2126F * rgba.R) + (.7152F * rgba.G) + (.0722F * rgba.B)).Clamp(0, 255); luminance = isAlphaOnly ? rgba.A : (.2126F * rgba.R) + (.7152F * rgba.G) + (.0722F * rgba.B);
// Setup the previous pointer // Setup the previous pointer
previousPixel = sourcePixel; previousPixel = sourcePixel;

4
src/ImageSharp/Processing/Processors/Dithering/OrderedDitherPaletteProcessor.cs

@ -63,7 +63,7 @@ namespace SixLabors.ImageSharp.Processing.Processors
sourcePixel.ToRgba32(ref rgba); sourcePixel.ToRgba32(ref rgba);
// Convert to grayscale using ITU-R Recommendation BT.709 if required // Convert to grayscale using ITU-R Recommendation BT.709 if required
byte luminance = (byte)(isAlphaOnly ? rgba.A : (.2126F * rgba.R) + (.7152F * rgba.G) + (.0722F * rgba.B)).Clamp(0, 255); float luminance = isAlphaOnly ? rgba.A : (.2126F * rgba.R) + (.7152F * rgba.G) + (.0722F * rgba.B);
for (int y = startY; y < endY; y++) for (int y = startY; y < endY; y++)
{ {
@ -79,7 +79,7 @@ namespace SixLabors.ImageSharp.Processing.Processors
{ {
pair = this.GetClosestPixelPair(ref sourcePixel, this.Palette); pair = this.GetClosestPixelPair(ref sourcePixel, this.Palette);
sourcePixel.ToRgba32(ref rgba); sourcePixel.ToRgba32(ref rgba);
luminance = (byte)(isAlphaOnly ? rgba.A : (.2126F * rgba.R) + (.7152F * rgba.G) + (.0722F * rgba.B)).Clamp(0, 255); luminance = isAlphaOnly ? rgba.A : (.2126F * rgba.R) + (.7152F * rgba.G) + (.0722F * rgba.B);
// Setup the previous pointer // Setup the previous pointer
previousPixel = sourcePixel; previousPixel = sourcePixel;

Loading…
Cancel
Save