Browse Source

Fix source rectangle regression in resize #118

af/merge-core
James Jackson-South 9 years ago
parent
commit
3ffb02844f
  1. 19
      src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs
  2. 8
      src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs
  3. 1
      tests/ImageSharp.Tests/FileTestBase.cs
  4. 1
      tests/ImageSharp.Tests/TestImages.cs
  5. 3
      tests/ImageSharp.Tests/TestImages/Formats/Png/cross.png

19
src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs

@ -52,13 +52,14 @@ namespace ImageSharp.Processing.Processors
/// Computes the sum of vectors in 'rowSpan' weighted by weight values, pointed by this <see cref="WeightsWindow"/> instance.
/// </summary>
/// <param name="rowSpan">The input span of vectors</param>
/// <param name="sourceX">The source row position.</param>
/// <returns>The weighted sum</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector4 ComputeWeightedRowSum(BufferSpan<Vector4> rowSpan)
public Vector4 ComputeWeightedRowSum(BufferSpan<Vector4> rowSpan, int sourceX)
{
ref float horizontalValues = ref this.Ptr;
int left = this.Left;
ref Vector4 vecPtr = ref Unsafe.Add(ref rowSpan.DangerousGetPinnableReference(), left);
ref Vector4 vecPtr = ref Unsafe.Add(ref rowSpan.DangerousGetPinnableReference(), left + sourceX);
// Destination color components
Vector4 result = Vector4.Zero;
@ -78,13 +79,14 @@ namespace ImageSharp.Processing.Processors
/// Applies <see cref="Vector4Extensions.Expand(float)"/> to all input vectors.
/// </summary>
/// <param name="rowSpan">The input span of vectors</param>
/// <param name="sourceX">The source row position.</param>
/// <returns>The weighted sum</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector4 ComputeExpandedWeightedRowSum(BufferSpan<Vector4> rowSpan)
public Vector4 ComputeExpandedWeightedRowSum(BufferSpan<Vector4> rowSpan, int sourceX)
{
ref float horizontalValues = ref this.Ptr;
int left = this.Left;
ref Vector4 vecPtr = ref Unsafe.Add(ref rowSpan.DangerousGetPinnableReference(), left);
ref Vector4 vecPtr = ref Unsafe.Add(ref rowSpan.DangerousGetPinnableReference(), left + sourceX);
// Destination color components
Vector4 result = Vector4.Zero;
@ -100,14 +102,15 @@ namespace ImageSharp.Processing.Processors
}
/// <summary>
/// Computes the sum of vectors in 'firstPassPixels' at a column pointed by 'x',
/// Computes the sum of vectors in 'firstPassPixels' at a row pointed by 'x',
/// weighted by weight values, pointed by this <see cref="WeightsWindow"/> instance.
/// </summary>
/// <param name="firstPassPixels">The buffer of input vectors in row first order</param>
/// <param name="x">The column position</param>
/// <param name="x">The row position</param>
/// <param name="sourceY">The source column position.</param>
/// <returns>The weighted sum</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector4 ComputeWeightedColumnSum(Buffer2D<Vector4> firstPassPixels, int x)
public Vector4 ComputeWeightedColumnSum(Buffer2D<Vector4> firstPassPixels, int x, int sourceY)
{
ref float verticalValues = ref this.Ptr;
int left = this.Left;
@ -118,7 +121,7 @@ namespace ImageSharp.Processing.Processors
for (int i = 0; i < this.Length; i++)
{
float yw = Unsafe.Add(ref verticalValues, i);
int index = left + i;
int index = left + i + sourceY;
result += firstPassPixels[x, index] * yw;
}

8
src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs

@ -133,7 +133,7 @@ namespace ImageSharp.Processing.Processors
for (int x = minX; x < maxX; x++)
{
WeightsWindow window = this.HorizontalWeights.Weights[x - startX];
firstPassPixels[x, y] = window.ComputeExpandedWeightedRowSum(tempRowBuffer);
firstPassPixels[x, y] = window.ComputeExpandedWeightedRowSum(tempRowBuffer, sourceX);
}
}
else
@ -141,7 +141,7 @@ namespace ImageSharp.Processing.Processors
for (int x = minX; x < maxX; x++)
{
WeightsWindow window = this.HorizontalWeights.Weights[x - startX];
firstPassPixels[x, y] = window.ComputeWeightedRowSum(tempRowBuffer);
firstPassPixels[x, y] = window.ComputeWeightedRowSum(tempRowBuffer, sourceX);
}
}
}
@ -162,7 +162,7 @@ namespace ImageSharp.Processing.Processors
for (int x = 0; x < width; x++)
{
// Destination color components
Vector4 destination = window.ComputeWeightedColumnSum(firstPassPixels, x);
Vector4 destination = window.ComputeWeightedColumnSum(firstPassPixels,x, sourceY);
destination = destination.Compress();
TPixel d = default(TPixel);
d.PackFromVector4(destination);
@ -174,7 +174,7 @@ namespace ImageSharp.Processing.Processors
for (int x = 0; x < width; x++)
{
// Destination color components
Vector4 destination = window.ComputeWeightedColumnSum(firstPassPixels, x);
Vector4 destination = window.ComputeWeightedColumnSum(firstPassPixels,x, sourceY);
TPixel d = default(TPixel);
d.PackFromVector4(destination);

1
tests/ImageSharp.Tests/FileTestBase.cs

@ -30,6 +30,7 @@ namespace ImageSharp.Tests
TestFile.Create(TestImages.Bmp.Car),
// TestFile.Create(TestImages.Bmp.NegHeight), // Perf: Enable for local testing only
TestFile.Create(TestImages.Png.Splash),
// TestFile.Create(TestImages.Png.Cross), // Perf: Enable for local testing only
// TestFile.Create(TestImages.Png.ChunkLength1), // Perf: Enable for local testing only
// TestFile.Create(TestImages.Png.ChunkLength2), // Perf: Enable for local testing only
// TestFile.Create(TestImages.Png.Powerpoint), // Perf: Enable for local testing only

1
tests/ImageSharp.Tests/TestImages.cs

@ -21,6 +21,7 @@ namespace ImageSharp.Tests
public const string Blur = "Png/blur.png";
public const string Indexed = "Png/indexed.png";
public const string Splash = "Png/splash.png";
public const string Cross = "Png/cross.png";
public const string Powerpoint = "Png/pp.png";
public const string SplashInterlaced = "Png/splash-interlaced.png";
public const string Interlaced = "Png/interlaced.png";

3
tests/ImageSharp.Tests/TestImages/Formats/Png/cross.png

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:0677fbb7f1bd8dd19e8bd7ee802e07f3600193dafbfccf89f43e64e4fdf02d8f
size 15227
Loading…
Cancel
Save