Skip to content

Commit

Permalink
Merge branch 'master' into af/externalize-RentReturnRelease_Subsequen…
Browse files Browse the repository at this point in the history
…tRentReturnsDifferentHandles
  • Loading branch information
antonfirsov authored Jan 25, 2022
2 parents bbbf687 + a9e718c commit 30669dd
Show file tree
Hide file tree
Showing 22 changed files with 222 additions and 74 deletions.
4 changes: 2 additions & 2 deletions src/ImageSharp/Memory/Buffer2DRegion{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public Buffer2DRegion(Buffer2D<T> buffer)
/// <param name="y">The row index</param>
/// <returns>The span</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span<T> GetRowSpan(int y)
public Span<T> DangerousGetRowSpan(int y)
{
int yy = this.Rectangle.Y + y;
int xx = this.Rectangle.X;
Expand Down Expand Up @@ -152,7 +152,7 @@ internal void Clear()

for (int y = 0; y < this.Rectangle.Height; y++)
{
Span<T> row = this.GetRowSpan(y);
Span<T> row = this.DangerousGetRowSpan(y);
row.Clear();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,38 +46,25 @@ protected CloningImageProcessor(Configuration configuration, Image<TPixel> sourc
/// <inheritdoc/>
Image<TPixel> ICloningImageProcessor<TPixel>.CloneAndExecute()
{
try
{
Image<TPixel> clone = this.CreateTarget();
this.CheckFrameCount(this.Source, clone);
Image<TPixel> clone = this.CreateTarget();
this.CheckFrameCount(this.Source, clone);

Configuration configuration = this.Configuration;
this.BeforeImageApply(clone);
Configuration configuration = this.Configuration;
this.BeforeImageApply(clone);

for (int i = 0; i < this.Source.Frames.Count; i++)
{
ImageFrame<TPixel> sourceFrame = this.Source.Frames[i];
ImageFrame<TPixel> clonedFrame = clone.Frames[i];
for (int i = 0; i < this.Source.Frames.Count; i++)
{
ImageFrame<TPixel> sourceFrame = this.Source.Frames[i];
ImageFrame<TPixel> clonedFrame = clone.Frames[i];

this.BeforeFrameApply(sourceFrame, clonedFrame);
this.OnFrameApply(sourceFrame, clonedFrame);
this.AfterFrameApply(sourceFrame, clonedFrame);
}
this.BeforeFrameApply(sourceFrame, clonedFrame);
this.OnFrameApply(sourceFrame, clonedFrame);
this.AfterFrameApply(sourceFrame, clonedFrame);
}

this.AfterImageApply(clone);
this.AfterImageApply(clone);

return clone;
}
#if DEBUG
catch (Exception)
{
throw;
#else
catch (Exception ex)
{
throw new ImageProcessingException($"An error occurred when processing the image using {this.GetType().Name}. See the inner exception for more detail.", ex);
#endif
}
return clone;
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public AffineTransformProcessor(Matrix3x2 matrix, IResampler sampler, Size targe
Guard.NotNull(sampler, nameof(sampler));
Guard.MustBeValueType(sampler, nameof(sampler));

if (TransformUtils.IsDegenerate(matrix))
{
throw new DegenerateTransformException("Matrix is degenerate. Check input values.");
}

this.Sampler = sampler;
this.TransformMatrix = matrix;
this.DestinationSize = targetDimensions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,18 @@ public void ApplyTransform<TResampler>(in TResampler sampler)
Matrix3x2 matrix = this.transformMatrix;

// Handle transforms that result in output identical to the original.
if (matrix.Equals(default) || matrix.Equals(Matrix3x2.Identity))
// Degenerate matrices are already handled in the upstream definition.
if (matrix.Equals(Matrix3x2.Identity))
{
// The clone will be blank here copy all the pixel data over
source.GetPixelMemoryGroup().CopyTo(destination.GetPixelMemoryGroup());
var interest = Rectangle.Intersect(this.SourceRectangle, destination.Bounds());
Buffer2DRegion<TPixel> sourceBuffer = source.PixelBuffer.GetRegion(interest);
Buffer2DRegion<TPixel> destbuffer = destination.PixelBuffer.GetRegion(interest);
for (int y = 0; y < sourceBuffer.Height; y++)
{
sourceBuffer.DangerousGetRowSpan(y).CopyTo(destbuffer.DangerousGetRowSpan(y));
}

return;
}

Expand All @@ -73,7 +81,12 @@ public void ApplyTransform<TResampler>(in TResampler sampler)

if (sampler is NearestNeighborResampler)
{
var nnOperation = new NNAffineOperation(source.PixelBuffer, destination.PixelBuffer, matrix);
var nnOperation = new NNAffineOperation(
source.PixelBuffer,
Rectangle.Intersect(this.SourceRectangle, source.Bounds()),
destination.PixelBuffer,
matrix);

ParallelRowIterator.IterateRows(
configuration,
destination.Bounds(),
Expand All @@ -85,6 +98,7 @@ public void ApplyTransform<TResampler>(in TResampler sampler)
var operation = new AffineOperation<TResampler>(
configuration,
source.PixelBuffer,
Rectangle.Intersect(this.SourceRectangle, source.Bounds()),
destination.PixelBuffer,
in sampler,
matrix);
Expand All @@ -105,12 +119,13 @@ public void ApplyTransform<TResampler>(in TResampler sampler)
[MethodImpl(InliningOptions.ShortMethod)]
public NNAffineOperation(
Buffer2D<TPixel> source,
Rectangle bounds,
Buffer2D<TPixel> destination,
Matrix3x2 matrix)
{
this.source = source;
this.bounds = bounds;
this.destination = destination;
this.bounds = source.Bounds();
this.matrix = matrix;
}

Expand Down Expand Up @@ -138,6 +153,7 @@ public void Invoke(int y)
{
private readonly Configuration configuration;
private readonly Buffer2D<TPixel> source;
private readonly Rectangle bounds;
private readonly Buffer2D<TPixel> destination;
private readonly TResampler sampler;
private readonly Matrix3x2 matrix;
Expand All @@ -148,12 +164,14 @@ public void Invoke(int y)
public AffineOperation(
Configuration configuration,
Buffer2D<TPixel> source,
Rectangle bounds,
Buffer2D<TPixel> destination,
in TResampler sampler,
Matrix3x2 matrix)
{
this.configuration = configuration;
this.source = source;
this.bounds = bounds;
this.destination = destination;
this.sampler = sampler;
this.matrix = matrix;
Expand Down Expand Up @@ -182,8 +200,10 @@ public void Invoke(in RowInterval rows, Span<Vector4> span)
TResampler sampler = this.sampler;
float yRadius = this.yRadius;
float xRadius = this.xRadius;
int maxY = this.source.Height - 1;
int maxX = this.source.Width - 1;
int minY = this.bounds.Y;
int maxY = this.bounds.Bottom - 1;
int minX = this.bounds.X;
int maxX = this.bounds.Right - 1;

for (int y = rows.Min; y < rows.Max; y++)
{
Expand All @@ -200,10 +220,10 @@ public void Invoke(in RowInterval rows, Span<Vector4> span)
float pY = point.Y;
float pX = point.X;

int top = LinearTransformUtility.GetRangeStart(yRadius, pY, maxY);
int bottom = LinearTransformUtility.GetRangeEnd(yRadius, pY, maxY);
int left = LinearTransformUtility.GetRangeStart(xRadius, pX, maxX);
int right = LinearTransformUtility.GetRangeEnd(xRadius, pX, maxX);
int top = LinearTransformUtility.GetRangeStart(yRadius, pY, minY, maxY);
int bottom = LinearTransformUtility.GetRangeEnd(yRadius, pY, minY, maxY);
int left = LinearTransformUtility.GetRangeStart(xRadius, pX, minX, maxX);
int right = LinearTransformUtility.GetRangeEnd(xRadius, pX, minX, maxX);

if (bottom == top || right == left)
{
Expand Down Expand Up @@ -245,8 +265,10 @@ private void InvokeMacOSX(in RowInterval rows, Span<Vector4> span)
TResampler sampler = this.sampler;
float yRadius = this.yRadius;
float xRadius = this.xRadius;
int maxY = this.source.Height - 1;
int maxX = this.source.Width - 1;
int minY = this.bounds.Y;
int maxY = this.bounds.Bottom - 1;
int minX = this.bounds.X;
int maxX = this.bounds.Right - 1;

for (int y = rows.Min; y < rows.Max; y++)
{
Expand All @@ -263,10 +285,10 @@ private void InvokeMacOSX(in RowInterval rows, Span<Vector4> span)
float pY = point.Y;
float pX = point.X;

int top = LinearTransformUtility.GetRangeStart(yRadius, pY, maxY);
int bottom = LinearTransformUtility.GetRangeEnd(yRadius, pY, maxY);
int left = LinearTransformUtility.GetRangeStart(xRadius, pX, maxX);
int right = LinearTransformUtility.GetRangeEnd(xRadius, pX, maxX);
int top = LinearTransformUtility.GetRangeStart(yRadius, pY, minY, maxY);
int bottom = LinearTransformUtility.GetRangeEnd(yRadius, pY, minY, maxY);
int left = LinearTransformUtility.GetRangeStart(xRadius, pX, minX, maxX);
int right = LinearTransformUtility.GetRangeEnd(xRadius, pX, minX, maxX);

if (bottom == top || right == left)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,24 @@ public static float GetSamplingRadius<TResampler>(in TResampler sampler, int sou
/// </summary>
/// <param name="radius">The radius.</param>
/// <param name="center">The center position.</param>
/// <param name="min">The min allowed amouunt.</param>
/// <param name="max">The max allowed amouunt.</param>
/// <returns>The <see cref="int"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static int GetRangeStart(float radius, float center, int max)
=> Numerics.Clamp((int)MathF.Ceiling(center - radius), 0, max);
public static int GetRangeStart(float radius, float center, int min, int max)
=> Numerics.Clamp((int)MathF.Ceiling(center - radius), min, max);

/// <summary>
/// Gets the end position (inclusive) for a sampling range given
/// the radius, center position and max constraint.
/// </summary>
/// <param name="radius">The radius.</param>
/// <param name="center">The center position.</param>
/// <param name="min">The min allowed amouunt.</param>
/// <param name="max">The max allowed amouunt.</param>
/// <returns>The <see cref="int"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static int GetRangeEnd(float radius, float center, int max)
=> Numerics.Clamp((int)MathF.Floor(center + radius), 0, max);
public static int GetRangeEnd(float radius, float center, int min, int max)
=> Numerics.Clamp((int)MathF.Floor(center + radius), min, max);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public ProjectiveTransformProcessor(Matrix4x4 matrix, IResampler sampler, Size t
Guard.NotNull(sampler, nameof(sampler));
Guard.MustBeValueType(sampler, nameof(sampler));

if (TransformUtils.IsDegenerate(matrix))
{
throw new DegenerateTransformException("Matrix is degenerate. Check input values.");
}

this.Sampler = sampler;
this.TransformMatrix = matrix;
this.DestinationSize = targetDimensions;
Expand Down
Loading

0 comments on commit 30669dd

Please sign in to comment.