Browse Source

formatting

af/merge-core
Anton Firszov 8 years ago
parent
commit
b92731896d
  1. 21
      src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor{TPixel}.cs
  2. 5
      src/ImageSharp/Processing/Processors/Transforms/AutoOrientProcessor{TPixel}.cs
  3. 27
      src/ImageSharp/Processing/Processors/Transforms/ProjectiveTransformProcessor{TPixel}.cs
  4. 68
      src/ImageSharp/Processing/Processors/Transforms/RotateProcessor{TPixel}.cs

21
src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor{TPixel}.cs

@ -25,18 +25,18 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
this.Definition = definition; this.Definition = definition;
} }
protected AffineTransformProcessor Definition { get; }
private Size TargetDimensions => this.Definition.TargetDimensions; private Size TargetDimensions => this.Definition.TargetDimensions;
private Matrix3x2 TransformMatrix => this.Definition.TransformMatrix; private Matrix3x2 TransformMatrix => this.Definition.TransformMatrix;
protected AffineTransformProcessor Definition { get; }
/// <inheritdoc/> /// <inheritdoc/>
protected override Image<TPixel> CreateDestination(Image<TPixel> source, Rectangle sourceRectangle) protected override Image<TPixel> CreateDestination(Image<TPixel> source, Rectangle sourceRectangle)
{ {
// We will always be creating the clone even for mutate because we may need to resize the canvas // We will always be creating the clone even for mutate because we may need to resize the canvas
IEnumerable<ImageFrame<TPixel>> frames = IEnumerable<ImageFrame<TPixel>> frames = source.Frames.Select(
source.Frames.Select(x => new ImageFrame<TPixel>(source.GetConfiguration(), this.TargetDimensions, x.Metadata.DeepClone())); x => new ImageFrame<TPixel>(source.GetConfiguration(), this.TargetDimensions, x.Metadata.DeepClone()));
// Use the overload to prevent an extra frame being added // Use the overload to prevent an extra frame being added
return new Image<TPixel>(source.GetConfiguration(), source.Metadata.DeepClone(), frames); return new Image<TPixel>(source.GetConfiguration(), source.Metadata.DeepClone(), frames);
@ -111,10 +111,19 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
// Use the single precision position to calculate correct bounding pixels // Use the single precision position to calculate correct bounding pixels
// otherwise we get rogue pixels outside of the bounds. // otherwise we get rogue pixels outside of the bounds.
var point = Vector2.Transform(new Vector2(x, y), matrix); var point = Vector2.Transform(new Vector2(x, y), matrix);
kernel.Convolve(point, x, ref ySpanRef, ref xSpanRef, source.PixelBuffer, vectorSpan); kernel.Convolve(
point,
x,
ref ySpanRef,
ref xSpanRef,
source.PixelBuffer,
vectorSpan);
} }
PixelOperations<TPixel>.Instance.FromVector4Destructive(configuration, vectorSpan, targetRowSpan); PixelOperations<TPixel>.Instance.FromVector4Destructive(
configuration,
vectorSpan,
targetRowSpan);
} }
}); });
} }

5
src/ImageSharp/Processing/Processors/Transforms/AutoOrientProcessor{TPixel}.cs

@ -61,7 +61,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
} }
/// <inheritdoc/> /// <inheritdoc/>
protected override void OnFrameApply(ImageFrame<TPixel> sourceBase, Rectangle sourceRectangle, Configuration config) protected override void OnFrameApply(
ImageFrame<TPixel> sourceBase,
Rectangle sourceRectangle,
Configuration config)
{ {
// All processing happens at the image level within BeforeImageApply(); // All processing happens at the image level within BeforeImageApply();
} }

27
src/ImageSharp/Processing/Processors/Transforms/ProjectiveTransformProcessor{TPixel}.cs

@ -33,15 +33,23 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
protected override Image<TPixel> CreateDestination(Image<TPixel> source, Rectangle sourceRectangle) protected override Image<TPixel> CreateDestination(Image<TPixel> source, Rectangle sourceRectangle)
{ {
// We will always be creating the clone even for mutate because we may need to resize the canvas // We will always be creating the clone even for mutate because we may need to resize the canvas
IEnumerable<ImageFrame<TPixel>> frames = IEnumerable<ImageFrame<TPixel>> frames = source.Frames.Select(
source.Frames.Select(x => new ImageFrame<TPixel>(source.GetConfiguration(), this.TargetDimensions.Width, this.TargetDimensions.Height, x.Metadata.DeepClone())); x => new ImageFrame<TPixel>(
source.GetConfiguration(),
this.TargetDimensions.Width,
this.TargetDimensions.Height,
x.Metadata.DeepClone()));
// Use the overload to prevent an extra frame being added // Use the overload to prevent an extra frame being added
return new Image<TPixel>(source.GetConfiguration(), source.Metadata.DeepClone(), frames); return new Image<TPixel>(source.GetConfiguration(), source.Metadata.DeepClone(), frames);
} }
/// <inheritdoc/> /// <inheritdoc/>
protected override void OnFrameApply(ImageFrame<TPixel> source, ImageFrame<TPixel> destination, Rectangle sourceRectangle, Configuration configuration) protected override void OnFrameApply(
ImageFrame<TPixel> source,
ImageFrame<TPixel> destination,
Rectangle sourceRectangle,
Configuration configuration)
{ {
Matrix4x4 transformMatrix = this.definition.TransformMatrix; Matrix4x4 transformMatrix = this.definition.TransformMatrix;
@ -110,10 +118,19 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
// Use the single precision position to calculate correct bounding pixels // Use the single precision position to calculate correct bounding pixels
// otherwise we get rogue pixels outside of the bounds. // otherwise we get rogue pixels outside of the bounds.
Vector2 point = TransformUtils.ProjectiveTransform2D(x, y, matrix); Vector2 point = TransformUtils.ProjectiveTransform2D(x, y, matrix);
kernel.Convolve(point, x, ref ySpanRef, ref xSpanRef, source.PixelBuffer, vectorSpan); kernel.Convolve(
point,
x,
ref ySpanRef,
ref xSpanRef,
source.PixelBuffer,
vectorSpan);
} }
PixelOperations<TPixel>.Instance.FromVector4Destructive(configuration, vectorSpan, targetRowSpan); PixelOperations<TPixel>.Instance.FromVector4Destructive(
configuration,
vectorSpan,
targetRowSpan);
} }
}); });
} }

68
src/ImageSharp/Processing/Processors/Transforms/RotateProcessor{TPixel}.cs

@ -27,18 +27,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
private float Degrees { get; } private float Degrees { get; }
/// <inheritdoc/> /// <inheritdoc/>
protected override void OnFrameApply(ImageFrame<TPixel> source, ImageFrame<TPixel> destination, Rectangle sourceRectangle, Configuration configuration) protected override void AfterImageApply(
{ Image<TPixel> source,
if (this.OptimizedApply(source, destination, configuration)) Image<TPixel> destination,
{ Rectangle sourceRectangle)
return;
}
base.OnFrameApply(source, destination, sourceRectangle, configuration);
}
/// <inheritdoc/>
protected override void AfterImageApply(Image<TPixel> source, Image<TPixel> destination, Rectangle sourceRectangle)
{ {
ExifProfile profile = destination.Metadata.ExifProfile; ExifProfile profile = destination.Metadata.ExifProfile;
if (profile is null) if (profile is null)
@ -57,6 +49,21 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
base.AfterImageApply(source, destination, sourceRectangle); base.AfterImageApply(source, destination, sourceRectangle);
} }
/// <inheritdoc/>
protected override void OnFrameApply(
ImageFrame<TPixel> source,
ImageFrame<TPixel> destination,
Rectangle sourceRectangle,
Configuration configuration)
{
if (this.OptimizedApply(source, destination, configuration))
{
return;
}
base.OnFrameApply(source, destination, sourceRectangle, configuration);
}
/// <summary> /// <summary>
/// Wraps a given angle in degrees so that it falls withing the 0-360 degree range /// Wraps a given angle in degrees so that it falls withing the 0-360 degree range
/// </summary> /// </summary>
@ -83,7 +90,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
/// <returns> /// <returns>
/// The <see cref="bool" /> /// The <see cref="bool" />
/// </returns> /// </returns>
private bool OptimizedApply(ImageFrame<TPixel> source, ImageFrame<TPixel> destination, Configuration configuration) private bool OptimizedApply(
ImageFrame<TPixel> source,
ImageFrame<TPixel> destination,
Configuration configuration)
{ {
// Wrap the degrees to keep within 0-360 so we can apply optimizations when possible. // Wrap the degrees to keep within 0-360 so we can apply optimizations when possible.
float degrees = WrapDegrees(this.Degrees); float degrees = WrapDegrees(this.Degrees);
@ -117,16 +127,15 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
} }
/// <summary> /// <summary>
/// Rotates the image 270 degrees clockwise at the centre point. /// Rotates the image 180 degrees clockwise at the centre point.
/// </summary> /// </summary>
/// <param name="source">The source image.</param> /// <param name="source">The source image.</param>
/// <param name="destination">The destination image.</param> /// <param name="destination">The destination image.</param>
/// <param name="configuration">The configuration.</param> /// <param name="configuration">The configuration.</param>
private void Rotate270(ImageFrame<TPixel> source, ImageFrame<TPixel> destination, Configuration configuration) private void Rotate180(ImageFrame<TPixel> source, ImageFrame<TPixel> destination, Configuration configuration)
{ {
int width = source.Width; int width = source.Width;
int height = source.Height; int height = source.Height;
Rectangle destinationBounds = destination.Bounds();
ParallelHelper.IterateRows( ParallelHelper.IterateRows(
source.Bounds(), source.Bounds(),
@ -136,31 +145,27 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
for (int y = rows.Min; y < rows.Max; y++) for (int y = rows.Min; y < rows.Max; y++)
{ {
Span<TPixel> sourceRow = source.GetPixelRowSpan(y); Span<TPixel> sourceRow = source.GetPixelRowSpan(y);
Span<TPixel> targetRow = destination.GetPixelRowSpan(height - y - 1);
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
int newX = height - y - 1; targetRow[width - x - 1] = sourceRow[x];
newX = height - newX - 1;
int newY = width - x - 1;
if (destinationBounds.Contains(newX, newY))
{
destination[newX, newY] = sourceRow[x];
}
} }
} }
}); });
} }
/// <summary> /// <summary>
/// Rotates the image 180 degrees clockwise at the centre point. /// Rotates the image 270 degrees clockwise at the centre point.
/// </summary> /// </summary>
/// <param name="source">The source image.</param> /// <param name="source">The source image.</param>
/// <param name="destination">The destination image.</param> /// <param name="destination">The destination image.</param>
/// <param name="configuration">The configuration.</param> /// <param name="configuration">The configuration.</param>
private void Rotate180(ImageFrame<TPixel> source, ImageFrame<TPixel> destination, Configuration configuration) private void Rotate270(ImageFrame<TPixel> source, ImageFrame<TPixel> destination, Configuration configuration)
{ {
int width = source.Width; int width = source.Width;
int height = source.Height; int height = source.Height;
Rectangle destinationBounds = destination.Bounds();
ParallelHelper.IterateRows( ParallelHelper.IterateRows(
source.Bounds(), source.Bounds(),
@ -170,11 +175,16 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
for (int y = rows.Min; y < rows.Max; y++) for (int y = rows.Min; y < rows.Max; y++)
{ {
Span<TPixel> sourceRow = source.GetPixelRowSpan(y); Span<TPixel> sourceRow = source.GetPixelRowSpan(y);
Span<TPixel> targetRow = destination.GetPixelRowSpan(height - y - 1);
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
targetRow[width - x - 1] = sourceRow[x]; int newX = height - y - 1;
newX = height - newX - 1;
int newY = width - x - 1;
if (destinationBounds.Contains(newX, newY))
{
destination[newX, newY] = sourceRow[x];
}
} }
} }
}); });

Loading…
Cancel
Save