Browse Source

Moar resize and skew [skip ci]

Somebody help me fix skew please.


Former-commit-id: 4c22456179078f77a0f523a9b4f6eb3af2da42be
Former-commit-id: 3fc9f06730164cffa4d6ca23e43dbd8e81869bf8
Former-commit-id: 357a39fe9c72ae0584e0b05014a6759b2b3af547
pull/1/head
James Jackson-South 10 years ago
parent
commit
be101c6029
  1. 10
      src/ImageProcessorCore/Common/Helpers/ImageMaths.cs
  2. 7
      src/ImageProcessorCore/Samplers/Rotate.cs
  3. 4
      src/ImageProcessorCore/Samplers/Skew.cs
  4. 2
      tests/ImageProcessorCore.Tests/Processors/Samplers/SamplerTests.cs

10
src/ImageProcessorCore/Common/Helpers/ImageMaths.cs

@ -6,6 +6,8 @@
namespace ImageProcessorCore namespace ImageProcessorCore
{ {
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics; using System.Numerics;
/// <summary> /// <summary>
@ -145,10 +147,10 @@ namespace ImageProcessorCore
Vector2 leftBottom = Vector2.Transform(new Vector2(rectangle.Left, rectangle.Bottom), matrix); Vector2 leftBottom = Vector2.Transform(new Vector2(rectangle.Left, rectangle.Bottom), matrix);
Vector2 rightBottom = Vector2.Transform(new Vector2(rectangle.Right, rectangle.Bottom), matrix); Vector2 rightBottom = Vector2.Transform(new Vector2(rectangle.Right, rectangle.Bottom), matrix);
Vector2 min = Vector2.Min(Vector2.Min(leftTop, rightTop), Vector2.Min(leftBottom, rightBottom)); Vector2[] allCorners = { leftTop, rightTop, leftBottom, rightBottom };
Vector2 max = Vector2.Max(Vector2.Max(leftTop, rightTop), Vector2.Max(leftBottom, rightBottom)); float extentX = allCorners.Select(v => v.X).Max() - allCorners.Select(v => v.X).Min();
float extentY = allCorners.Select(v => v.Y).Max() - allCorners.Select(v => v.Y).Min();
return new Rectangle(0, 0, (int)(max.X - min.X), (int)(max.Y - min.Y)); return new Rectangle(0, 0, (int)extentX, (int)extentY);
} }
/// <summary> /// <summary>

7
src/ImageProcessorCore/Samplers/Rotate.cs

@ -76,8 +76,7 @@ namespace ImageProcessorCore.Samplers
ResizeOptions options = new ResizeOptions ResizeOptions options = new ResizeOptions
{ {
Size = new Size(rectangle.Width, rectangle.Height), Size = new Size(rectangle.Width, rectangle.Height),
Mode = ResizeMode.BoxPad, Mode = ResizeMode.BoxPad
Sampler = new NearestNeighborResampler()
}; };
// Get the padded bounds and resize the image. // Get the padded bounds and resize the image.
@ -98,8 +97,8 @@ namespace ImageProcessorCore.Samplers
protected override void Apply(ImageBase target, ImageBase source, Rectangle targetRectangle, Rectangle sourceRectangle, int startY, int endY) protected override void Apply(ImageBase target, ImageBase source, Rectangle targetRectangle, Rectangle sourceRectangle, int startY, int endY)
{ {
int height = this.firstPass.Height; int height = this.firstPass.Height;
int startX = this.firstPass.Bounds.X; int startX = 0;
int endX = this.firstPass.Bounds.Right; int endX = this.firstPass.Width;
Point centre = this.Center == Point.Empty ? Rectangle.Center(this.firstPass.Bounds) : this.Center; Point centre = this.Center == Point.Empty ? Rectangle.Center(this.firstPass.Bounds) : this.Center;
Matrix3x2 rotation = Point.CreateRotation(centre, -this.angle); Matrix3x2 rotation = Point.CreateRotation(centre, -this.angle);

4
src/ImageProcessorCore/Samplers/Skew.cs

@ -128,8 +128,8 @@ namespace ImageProcessorCore.Samplers
protected override void Apply(ImageBase target, ImageBase source, Rectangle targetRectangle, Rectangle sourceRectangle, int startY, int endY) protected override void Apply(ImageBase target, ImageBase source, Rectangle targetRectangle, Rectangle sourceRectangle, int startY, int endY)
{ {
int height = this.firstPass.Height; int height = this.firstPass.Height;
int startX = this.firstPass.Bounds.X; int startX = 0;
int endX = this.firstPass.Bounds.Right; int endX = this.firstPass.Width;
Point centre = this.Center == Point.Empty ? Rectangle.Center(this.firstPass.Bounds) : this.Center; Point centre = this.Center == Point.Empty ? Rectangle.Center(this.firstPass.Bounds) : this.Center;
Matrix3x2 skew = Point.CreateSkew(centre, -this.angleX, -this.angleY); Matrix3x2 skew = Point.CreateSkew(centre, -this.angleX, -this.angleY);

2
tests/ImageProcessorCore.Tests/Processors/Samplers/SamplerTests.cs

@ -435,7 +435,7 @@
using (Image image = new Image(stream)) using (Image image = new Image(stream))
using (FileStream output = File.OpenWrite($"TestOutput/Rotate/{filename}")) using (FileStream output = File.OpenWrite($"TestOutput/Rotate/{filename}"))
{ {
image.Rotate(45, this.ProgressUpdate) image.Rotate(63, this.ProgressUpdate)
.Save(output); .Save(output);
} }

Loading…
Cancel
Save