From 2f4e06cae870f894162b4567080f2ce888240896 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 26 Nov 2017 02:04:28 +1100 Subject: [PATCH] Update dependencies, remove code duplication --- .../ImageSharp.Drawing.csproj | 6 +- src/ImageSharp/ImageSharp.csproj | 2 +- .../Processors/Transforms/AffineProcessor.cs | 61 ++++++++++++++++++ .../Processors/Transforms/RotateProcessor.cs | 54 +--------------- .../Processors/Transforms/SkewProcessor.cs | 62 ------------------- 5 files changed, 66 insertions(+), 119 deletions(-) diff --git a/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj b/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj index eb3c29dd9..3e320dccc 100644 --- a/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj +++ b/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj @@ -36,9 +36,9 @@ - - - + + + All diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index 8c22237cf..f1bcb2ecd 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -34,7 +34,7 @@ - + All diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs index 4db2aacba..cffe6674d 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs @@ -1,12 +1,15 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Numerics; using System.Runtime.CompilerServices; +using System.Threading.Tasks; using SixLabors.ImageSharp.Advanced; +using SixLabors.ImageSharp.Helpers; using SixLabors.ImageSharp.PixelFormats; using SixLabors.Primitives; @@ -76,6 +79,64 @@ namespace SixLabors.ImageSharp.Processing.Processors return new Image(source.GetConfiguration(), source.MetaData.Clone(), frames); } + /// + protected override void OnApply(ImageFrame source, ImageFrame destination, Rectangle sourceRectangle, Configuration configuration) + { + int height = this.ResizeRectangle.Height; + int width = this.ResizeRectangle.Width; + Matrix3x2 matrix = this.GetCenteredMatrix(source); + Rectangle sourceBounds = source.Bounds(); + + if (this.Sampler is NearestNeighborResampler) + { + Parallel.For( + 0, + height, + configuration.ParallelOptions, + y => + { + Span destRow = destination.GetPixelRowSpan(y); + + for (int x = 0; x < width; x++) + { + var transformedPoint = Point.Transform(new Point(x, y), matrix); + if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) + { + destRow[x] = source[transformedPoint.X, transformedPoint.Y]; + } + } + }); + + return; + } + + int maxX = source.Width - 1; + int maxY = source.Height - 1; + int radius = Math.Max((int)this.Sampler.Radius, 1); + + Parallel.For( + 0, + height, + configuration.ParallelOptions, + y => + { + Span destRow = destination.GetPixelRowSpan(y); + for (int x = 0; x < width; x++) + { + var transformedPoint = Point.Transform(new Point(x, y), matrix); + if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) + { + WeightsWindow windowX = this.HorizontalWeights.Weights[transformedPoint.X]; + WeightsWindow windowY = this.VerticalWeights.Weights[transformedPoint.Y]; + + Vector4 dXY = this.ComputeWeightedSumAtPosition(source, maxX, maxY, radius, ref windowX, ref windowY, ref transformedPoint); + ref TPixel dest = ref destRow[x]; + dest.PackFromVector4(dXY); + } + } + }); + } + /// /// Gets a transform matrix adjusted to center upon the target image bounds. /// diff --git a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs index a1b181e69..eb6110fa1 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs @@ -81,59 +81,7 @@ namespace SixLabors.ImageSharp.Processing.Processors return; } - int height = this.ResizeRectangle.Height; - int width = this.ResizeRectangle.Width; - Matrix3x2 matrix = this.GetCenteredMatrix(source); - Rectangle sourceBounds = source.Bounds(); - - if (this.Sampler is NearestNeighborResampler) - { - Parallel.For( - 0, - height, - configuration.ParallelOptions, - y => - { - Span destRow = destination.GetPixelRowSpan(y); - - for (int x = 0; x < width; x++) - { - var transformedPoint = Point.Rotate(new Point(x, y), matrix); - if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) - { - destRow[x] = source[transformedPoint.X, transformedPoint.Y]; - } - } - }); - - return; - } - - int maxX = source.Width - 1; - int maxY = source.Height - 1; - int radius = Math.Max((int)this.Sampler.Radius, 1); - - Parallel.For( - 0, - height, - configuration.ParallelOptions, - y => - { - Span destRow = destination.GetPixelRowSpan(y); - for (int x = 0; x < width; x++) - { - var transformedPoint = Point.Rotate(new Point(x, y), matrix); - if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) - { - WeightsWindow windowX = this.HorizontalWeights.Weights[transformedPoint.X]; - WeightsWindow windowY = this.VerticalWeights.Weights[transformedPoint.Y]; - - Vector4 dXY = this.ComputeWeightedSumAtPosition(source, maxX, maxY, radius, ref windowX, ref windowY, ref transformedPoint); - ref TPixel dest = ref destRow[x]; - dest.PackFromVector4(dXY); - } - } - }); + base.OnApply(source, destination, sourceRectangle, configuration); } /// diff --git a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs index 0f67f724c..fc70330e8 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs @@ -1,11 +1,7 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System; using System.Numerics; -using System.Threading.Tasks; -using SixLabors.ImageSharp.Advanced; -using SixLabors.ImageSharp.Helpers; using SixLabors.ImageSharp.PixelFormats; using SixLabors.Primitives; @@ -49,63 +45,5 @@ namespace SixLabors.ImageSharp.Processing.Processors return this.transformMatrix; } - - /// - protected override void OnApply(ImageFrame source, ImageFrame destination, Rectangle sourceRectangle, Configuration configuration) - { - int height = this.ResizeRectangle.Height; - int width = this.ResizeRectangle.Width; - Matrix3x2 matrix = this.GetCenteredMatrix(source); - Rectangle sourceBounds = source.Bounds(); - - if (this.Sampler is NearestNeighborResampler) - { - Parallel.For( - 0, - height, - configuration.ParallelOptions, - y => - { - Span destRow = destination.GetPixelRowSpan(y); - - for (int x = 0; x < width; x++) - { - var transformedPoint = Point.Skew(new Point(x, y), matrix); - if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) - { - destRow[x] = source[transformedPoint.X, transformedPoint.Y]; - } - } - }); - - return; - } - - int maxX = source.Width - 1; - int maxY = source.Height - 1; - int radius = Math.Max((int)this.Sampler.Radius, 1); - - Parallel.For( - 0, - height, - configuration.ParallelOptions, - y => - { - Span destRow = destination.GetPixelRowSpan(y); - for (int x = 0; x < width; x++) - { - var transformedPoint = Point.Skew(new Point(x, y), matrix); - if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) - { - WeightsWindow windowX = this.HorizontalWeights.Weights[transformedPoint.X]; - WeightsWindow windowY = this.VerticalWeights.Weights[transformedPoint.Y]; - - Vector4 dXY = this.ComputeWeightedSumAtPosition(source, maxX, maxY, radius, ref windowX, ref windowY, ref transformedPoint); - ref TPixel dest = ref destRow[x]; - dest.PackFromVector4(dXY); - } - } - }); - } } } \ No newline at end of file