Browse Source

Merge branch 'refs/heads/pr/411' into Core

# Conflicts:
#	src/ImageProcessorCore/Samplers/Processors/SkewProcessor.cs


Former-commit-id: 99f04d0e9a34df99f397b9afd0d2f5f040f13d17
Former-commit-id: 5a45eace93c3a651dce6e53faa821061b5aa8e4d
Former-commit-id: b75e6d8f24e9677c5c1fd696c7c6d15f7b77275b
pull/1/head
James Jackson-South 10 years ago
parent
commit
a58af597e5
  1. 1
      src/ImageProcessorCore/Common/Helpers/ImageMaths.cs
  2. 140
      src/ImageProcessorCore/Samplers/Processors/SkewProcessor.cs
  3. 2
      tests/ImageProcessorCore.Tests/Processors/Samplers/SamplerTests.cs

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

@ -6,7 +6,6 @@
namespace ImageProcessorCore namespace ImageProcessorCore
{ {
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;

140
src/ImageProcessorCore/Samplers/Processors/SkewProcessor.cs

@ -13,75 +13,18 @@ namespace ImageProcessorCore.Processors
/// </summary> /// </summary>
public class SkewProcessor : ImageSampler public class SkewProcessor : ImageSampler
{ {
/// <summary>
/// The image used for storing the first pass pixels.
/// </summary>
private Image firstPass;
/// <summary>
/// The angle of rotation along the x-axis.
/// </summary>
private float angleX;
/// <summary>
/// The angle of rotation along the y-axis.
/// </summary>
private float angleY;
/// <inheritdoc/> /// <inheritdoc/>
public override int Parallelism { get; set; } = 1; public override int Parallelism { get; set; } = 1;
/// <summary> /// <summary>
/// Gets or sets the angle of rotation along the x-axis in degrees. /// Gets or sets the angle of rotation along the x-axis in degrees.
/// </summary> /// </summary>
public float AngleX public float AngleX { get; set; }
{
get
{
return this.angleX;
}
set
{
if (value > 360)
{
value -= 360;
}
if (value < 0)
{
value += 360;
}
this.angleX = value;
}
}
/// <summary> /// <summary>
/// Gets or sets the angle of rotation along the y-axis in degrees. /// Gets or sets the angle of rotation along the y-axis in degrees.
/// </summary> /// </summary>
public float AngleY public float AngleY { get; set; }
{
get
{
return this.angleY;
}
set
{
if (value > 360)
{
value -= 360;
}
if (value < 0)
{
value += 360;
}
this.angleY = value;
}
}
/// <summary> /// <summary>
/// Gets or sets the center point. /// Gets or sets the center point.
@ -96,65 +39,64 @@ namespace ImageProcessorCore.Processors
/// <inheritdoc/> /// <inheritdoc/>
protected override void OnApply(ImageBase target, ImageBase source, Rectangle targetRectangle, Rectangle sourceRectangle) protected override void OnApply(ImageBase target, ImageBase source, Rectangle targetRectangle, Rectangle sourceRectangle)
{ {
// If we are expanding we need to pad the bounds of the source rectangle.
// We can use the resizer in nearest neighbor mode to do this fairly quickly.
if (this.Expand) if (this.Expand)
{ {
// First find out how big the target rectangle should be. Point centre = this.Center;
Point centre = this.Center == Point.Empty ? Rectangle.Center(sourceRectangle) : this.Center; Matrix3x2 skew = Point.CreateSkew(centre, -this.AngleX, -this.AngleY);
Matrix3x2 skew = Point.CreateSkew(centre, -this.angleX, -this.angleY); Matrix3x2 invertedSkew;
Rectangle rectangle = ImageMaths.GetBoundingRectangle(sourceRectangle, skew); Matrix3x2.Invert(skew, out invertedSkew);
ResizeOptions options = new ResizeOptions Rectangle bounds = ImageMaths.GetBoundingRectangle(source.Bounds, invertedSkew);
{ target.SetPixels(bounds.Width, bounds.Height, new float[bounds.Width * bounds.Height * 4]);
Size = new Size(rectangle.Width, rectangle.Height),
Mode = ResizeMode.BoxPad
};
// Get the padded bounds and resize the image.
Rectangle bounds = ResizeHelper.CalculateTargetLocationAndBounds(source, options);
this.firstPass = new Image(rectangle.Width, rectangle.Height);
target.SetPixels(rectangle.Width, rectangle.Height, new float[rectangle.Width * rectangle.Height * 4]);
new ResizeProcessor(new NearestNeighborResampler()).Apply(this.firstPass, source, rectangle.Width, rectangle.Height, bounds, sourceRectangle);
}
else
{
// Just clone the pixels across.
this.firstPass = new Image(source.Width, source.Height);
this.firstPass.ClonePixels(source.Width, source.Height, source.Pixels);
} }
} }
/// <inheritdoc/> /// <inheritdoc/>
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 = target.Height;
int startX = 0; int startX = 0;
int endX = this.firstPass.Width; int endX = target.Width;
Point centre = this.Center == Point.Empty ? Rectangle.Center(this.firstPass.Bounds) : this.Center; Point centre = this.Center;
Matrix3x2 skew = Point.CreateSkew(centre, -this.angleX, -this.angleY);
Matrix3x2 invertedSkew;
Matrix3x2 skew = Point.CreateSkew(centre, -this.AngleX, -this.AngleY);
Matrix3x2.Invert(skew, out invertedSkew);
Vector2 rightTop = Vector2.Transform(new Vector2(source.Width, 0), invertedSkew);
Vector2 leftBottom = Vector2.Transform(new Vector2(0, source.Height), invertedSkew);
// Since we are not working in parallel we use full height and width if (this.AngleX < 0 && this.AngleY > 0)
// of the first pass image. {
using (PixelAccessor firstPassPixels = this.firstPass.Lock()) skew = Point.CreateSkew(new Point((int)-leftBottom.X, (int)leftBottom.Y), -this.AngleX, -this.AngleY);
}
if (this.AngleX > 0 && this.AngleY < 0)
{
skew = Point.CreateSkew(new Point((int)rightTop.X, (int)-rightTop.Y), -this.AngleX, -this.AngleY);
}
if (this.AngleX < 0 && this.AngleY < 0)
{
skew = Point.CreateSkew(new Point(target.Width - 1, target.Height - 1), -this.AngleX, -this.AngleY);
}
using (PixelAccessor sourcePixels = source.Lock())
using (PixelAccessor targetPixels = target.Lock()) using (PixelAccessor targetPixels = target.Lock())
{ {
Parallel.For( Parallel.For(
0, 0,
height, height,
y => y =>
{
for (int x = startX; x < endX; x++)
{ {
for (int x = startX; x < endX; x++) Point skewed = Point.Skew(new Point(x, y), skew);
if (source.Bounds.Contains(skewed.X, skewed.Y))
{ {
// Skew at the centre point targetPixels[x, y] = sourcePixels[skewed.X, skewed.Y];
Point skewed = Point.Skew(new Point(x, y), skew);
if (this.firstPass.Bounds.Contains(skewed.X, skewed.Y))
{
targetPixels[x, y] = firstPassPixels[skewed.X, skewed.Y];
}
} }
}
this.OnRowProcessed(); this.OnRowProcessed();
}); });
} }
} }
} }

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

@ -454,7 +454,7 @@ namespace ImageProcessorCore.Tests
Image image = new Image(stream); Image image = new Image(stream);
using (FileStream output = File.OpenWrite($"TestOutput/Skew/{filename}")) using (FileStream output = File.OpenWrite($"TestOutput/Skew/{filename}"))
{ {
image.Skew(20, 10, this.ProgressUpdate) image.Skew(-15, 12, this.ProgressUpdate)
.Save(output); .Save(output);
} }
} }

Loading…
Cancel
Save