Browse Source

Remove first pass from Rotate [skip ci]

Former-commit-id: 0c0c868ae14312f4ab5d7bc78a0c8e118d10f404
Former-commit-id: 3406deb6ce32e26d84ccc744b094e65a5583d78a
Former-commit-id: e2cfeca125cfefc1a48088d63707379fd85f036e
af/merge-core
James Jackson-South 10 years ago
parent
commit
e780b08d20
  1. 90
      src/ImageProcessorCore/Samplers/Processors/RotateProcessor.cs

90
src/ImageProcessorCore/Samplers/Processors/RotateProcessor.cs

@ -13,44 +13,13 @@ namespace ImageProcessorCore.Processors
/// </summary> /// </summary>
public class RotateProcessor : ImageSampler public class RotateProcessor : ImageSampler
{ {
/// <summary>
/// The image used for storing the first pass pixels.
/// </summary>
private Image firstPass;
/// <summary>
/// The angle of rotation in degrees.
/// </summary>
private float angle;
/// <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 in degrees. /// Gets or sets the angle of rotation in degrees.
/// </summary> /// </summary>
public float Angle public float Angle { get; set; }
{
get
{
return this.angle;
}
set
{
if (value > 360)
{
value -= 360;
}
if (value < 0)
{
value += 360;
}
this.angle = value;
}
}
/// <summary> /// <summary>
/// Gets or sets the center point. /// Gets or sets the center point.
@ -65,46 +34,44 @@ 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.Empty ? Rectangle.Center(sourceRectangle) : this.Center; Point centre = this.Center == Point.Empty ? Rectangle.Center(sourceRectangle) : this.Center;
Matrix3x2 rotation = Point.CreateRotation(centre, -this.angle); Matrix3x2 rotation = Point.CreateRotation(centre, -this.Angle);
Rectangle rectangle = ImageMaths.GetBoundingRectangle(sourceRectangle, rotation); Matrix3x2 invertedRotation;
ResizeOptions options = new ResizeOptions Matrix3x2.Invert(rotation, out invertedRotation);
{ Rectangle bounds = ImageMaths.GetBoundingRectangle(source.Bounds, invertedRotation);
Size = new Size(rectangle.Width, rectangle.Height), target.SetPixels(bounds.Width, bounds.Height, new float[bounds.Width * bounds.Height * 4]);
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 == Point.Empty ? Rectangle.Center(target.Bounds) : this.Center;
Matrix3x2 rotation = Point.CreateRotation(centre, -this.angle);
//Matrix3x2 invertedRotation;
Matrix3x2 rotation = Point.CreateRotation(centre, -this.Angle);
//Matrix3x2.Invert(rotation, out invertedRotation);
//Vector2 rightTop = Vector2.Transform(new Vector2(source.Width, 0), invertedRotation);
//Vector2 leftBottom = Vector2.Transform(new Vector2(0, source.Height), invertedRotation);
//if (this.Angle < 0)
//{
// rotation = Point.CreateRotation(new Point((int)-leftBottom.X, (int)leftBottom.Y), -this.Angle);
//}
//if (this.Angle > 0)
//{
// rotation = Point.CreateRotation(new Point((int)rightTop.X, (int)-rightTop.Y), -this.Angle);
//}
// Since we are not working in parallel we use full height and width // Since we are not working in parallel we use full height and width
// of the first pass image. // of the first pass image.
using (PixelAccessor firstPassPixels = this.firstPass.Lock()) using (PixelAccessor sourcePixels = source.Lock())
using (PixelAccessor targetPixels = target.Lock()) using (PixelAccessor targetPixels = target.Lock())
{ {
Parallel.For( Parallel.For(
@ -114,11 +81,10 @@ namespace ImageProcessorCore.Processors
{ {
for (int x = startX; x < endX; x++) for (int x = startX; x < endX; x++)
{ {
// Rotate at the centre point
Point rotated = Point.Rotate(new Point(x, y), rotation); Point rotated = Point.Rotate(new Point(x, y), rotation);
if (this.firstPass.Bounds.Contains(rotated.X, rotated.Y)) if (source.Bounds.Contains(rotated.X, rotated.Y))
{ {
targetPixels[x, y] = firstPassPixels[rotated.X, rotated.Y]; targetPixels[x, y] = sourcePixels[rotated.X, rotated.Y];
} }
} }

Loading…
Cancel
Save