Browse Source

Update ErrorDiffuser.cs

pull/1114/head
James Jackson-South 7 years ago
parent
commit
cbfdaa7dfc
  1. 20
      src/ImageSharp/Processing/Processors/Dithering/ErrorDiffuser.cs

20
src/ImageSharp/Processing/Processors/Dithering/ErrorDiffuser.cs

@ -54,6 +54,12 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
// Calculate the error // Calculate the error
Vector4 error = source.ToVector4() - transformed.ToVector4(); Vector4 error = source.ToVector4() - transformed.ToVector4();
if (Vector4.Dot(error, error) > 16F / 255F)
{
error *= .75F;
}
this.DoDither(image, x, y, minX, maxX, maxY, error); this.DoDither(image, x, y, minX, maxX, maxY, error);
} }
@ -65,15 +71,24 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
DenseMatrix<float> matrix = this.matrix; DenseMatrix<float> matrix = this.matrix;
// Loop through and distribute the error amongst neighboring pixels. // Loop through and distribute the error amongst neighboring pixels.
for (int row = 0, targetY = y; row < matrix.Rows && targetY < maxY; row++, targetY++) for (int row = 0, targetY = y; row < matrix.Rows; row++, targetY++)
{
// TODO: Quantize rectangle.
if (targetY >= maxY)
{ {
continue;
}
Span<TPixel> rowSpan = image.GetPixelRowSpan(targetY); Span<TPixel> rowSpan = image.GetPixelRowSpan(targetY);
for (int col = 0; col < matrix.Columns; col++) for (int col = 0; col < matrix.Columns; col++)
{ {
int targetX = x + (col - offset); int targetX = x + (col - offset);
if (targetX >= minX && targetX < maxX) if (targetX < minX || targetX >= maxX)
{ {
continue;
}
float coefficient = matrix[row, col]; float coefficient = matrix[row, col];
if (coefficient == 0) if (coefficient == 0)
{ {
@ -89,5 +104,4 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
} }
} }
} }
}
} }

Loading…
Cancel
Save