//
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
//
namespace ImageSharp.Drawing
{
using System.Buffers;
using System.Numerics;
using SixLabors.Shapes;
using Rectangle = ImageSharp.Rectangle;
///
/// A mapping between a and a region.
///
internal class ShapeRegion : Region
{
///
/// Initializes a new instance of the class.
///
/// The shape.
public ShapeRegion(IPath shape)
{
this.Shape = shape.AsClosedPath();
this.Bounds = shape.Bounds.Convert();
}
///
/// Gets the fillable shape
///
public IPath Shape { get; }
///
public override int MaxIntersections => this.Shape.MaxIntersections;
///
public override Rectangle Bounds { get; }
///
public override int ScanX(int x, float[] buffer, int length, int offset)
{
Vector2 start = new Vector2(x, this.Bounds.Top - 1);
Vector2 end = new Vector2(x, this.Bounds.Bottom + 1);
Vector2[] innerbuffer = ArrayPool.Shared.Rent(length);
try
{
int count = this.Shape.FindIntersections(start, end, innerbuffer, length, 0);
for (int i = 0; i < count; i++)
{
buffer[i + offset] = innerbuffer[i].Y;
}
return count;
}
finally
{
ArrayPool.Shared.Return(innerbuffer);
}
}
///
public override int ScanY(int y, float[] buffer, int length, int offset)
{
Vector2 start = new Vector2(this.Bounds.Left - 1, y);
Vector2 end = new Vector2(this.Bounds.Right + 1, y);
Vector2[] innerbuffer = ArrayPool.Shared.Rent(length);
try
{
int count = this.Shape.FindIntersections(start, end, innerbuffer, length, 0);
for (int i = 0; i < count; i++)
{
buffer[i + offset] = innerbuffer[i].X;
}
return count;
}
finally
{
ArrayPool.Shared.Return(innerbuffer);
}
}
}
}