Browse Source

Resize should respect source rectangle Fix #118

af/merge-core
James Jackson-South 9 years ago
parent
commit
43b08e509a
  1. 12
      src/ImageSharp.Processing/Processors/Transforms/CompandingResizeProcessor.cs
  2. 12
      src/ImageSharp.Processing/Processors/Transforms/ResizeProcessor.cs
  3. 21
      tests/ImageSharp.Tests/Processors/Filters/ResizeTests.cs

12
src/ImageSharp.Processing/Processors/Transforms/CompandingResizeProcessor.cs

@ -56,6 +56,8 @@ namespace ImageSharp.Processing.Processors
int width = this.Width;
int height = this.Height;
int sourceX = sourceRectangle.X;
int sourceY = sourceRectangle.Y;
int startY = this.ResizeRectangle.Y;
int endY = this.ResizeRectangle.Bottom;
int startX = this.ResizeRectangle.X;
@ -83,12 +85,12 @@ namespace ImageSharp.Processing.Processors
y =>
{
// Y coordinates of source points
int originY = (int)((y - startY) * heightFactor);
int originY = (int)(((y - startY) * heightFactor) + sourceY);
for (int x = minX; x < maxX; x++)
{
// X coordinates of source points
targetPixels[x, y] = sourcePixels[(int)((x - startX) * widthFactor), originY];
targetPixels[x, y] = sourcePixels[(int)(((x - startX) * widthFactor) + sourceX), originY];
}
});
}
@ -110,7 +112,7 @@ namespace ImageSharp.Processing.Processors
{
Parallel.For(
0,
sourceRectangle.Height,
sourceRectangle.Bottom,
this.ParallelOptions,
y =>
{
@ -125,7 +127,7 @@ namespace ImageSharp.Processing.Processors
for (int i = 0; i < horizontalValues.Length; i++)
{
Weight xw = horizontalValues[i];
destination += sourcePixels[xw.Index, y].ToVector4().Expand() * xw.Value;
destination += sourcePixels[xw.Index + sourceX, y].ToVector4().Expand() * xw.Value;
}
TColor d = default(TColor);
@ -152,7 +154,7 @@ namespace ImageSharp.Processing.Processors
for (int i = 0; i < verticalValues.Length; i++)
{
Weight yw = verticalValues[i];
destination += firstPassPixels[x, yw.Index].ToVector4().Expand() * yw.Value;
destination += firstPassPixels[x, yw.Index + sourceY].ToVector4().Expand() * yw.Value;
}
TColor d = default(TColor);

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

@ -55,6 +55,8 @@ namespace ImageSharp.Processing.Processors
int width = this.Width;
int height = this.Height;
int sourceX = sourceRectangle.X;
int sourceY = sourceRectangle.Y;
int startY = this.ResizeRectangle.Y;
int endY = this.ResizeRectangle.Bottom;
int startX = this.ResizeRectangle.X;
@ -82,12 +84,12 @@ namespace ImageSharp.Processing.Processors
y =>
{
// Y coordinates of source points
int originY = (int)((y - startY) * heightFactor);
int originY = (int)(((y - startY) * heightFactor) + sourceY);
for (int x = minX; x < maxX; x++)
{
// X coordinates of source points
targetPixels[x, y] = sourcePixels[(int)((x - startX) * widthFactor), originY];
targetPixels[x, y] = sourcePixels[(int)(((x - startX) * widthFactor) + sourceX), originY];
}
});
}
@ -109,7 +111,7 @@ namespace ImageSharp.Processing.Processors
{
Parallel.For(
0,
sourceRectangle.Height,
sourceRectangle.Bottom,
this.ParallelOptions,
y =>
{
@ -124,7 +126,7 @@ namespace ImageSharp.Processing.Processors
for (int i = 0; i < horizontalValues.Length; i++)
{
Weight xw = horizontalValues[i];
destination += sourcePixels[xw.Index, y].ToVector4() * xw.Value;
destination += sourcePixels[xw.Index + sourceX, y].ToVector4() * xw.Value;
}
TColor d = default(TColor);
@ -151,7 +153,7 @@ namespace ImageSharp.Processing.Processors
for (int i = 0; i < verticalValues.Length; i++)
{
Weight yw = verticalValues[i];
destination += firstPassPixels[x, yw.Index].ToVector4() * yw.Value;
destination += firstPassPixels[x, yw.Index + sourceY].ToVector4() * yw.Value;
}
TColor d = default(TColor);

21
tests/ImageSharp.Tests/Processors/Filters/ResizeTests.cs

@ -50,6 +50,27 @@ namespace ImageSharp.Tests
}
}
[Theory]
[MemberData(nameof(ReSamplers))]
public void ImageShouldResizeFromSourceRectangle(string name, IResampler sampler)
{
name = $"{name}-SourceRect";
string path = this.CreateOutputDirectory("Resize");
foreach (TestFile file in Files)
{
string filename = file.GetFileName(name);
using (Image image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{filename}"))
{
var sourceRectangle = new Rectangle(image.Width / 8, image.Height / 8, image.Width / 4, image.Height / 4);
var destRectangle = new Rectangle(image.Width / 4, image.Height / 4, image.Width / 2, image.Height / 2);
image.Resize(image.Width, image.Height, sampler, sourceRectangle, destRectangle, false).Save(output);
}
}
}
[Theory]
[MemberData(nameof(ReSamplers))]
public void ImageShouldResizeWidthAndKeepAspect(string name, IResampler sampler)

Loading…
Cancel
Save