// Copyright (c) Six Labors. // Licensed under the Six Labors Split License. using System.Diagnostics.CodeAnalysis; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Drawing.Processing.Backends { /// /// Frame adapter that exposes a clipped subregion of another frame. /// /// The pixel format. internal sealed class CanvasRegionFrame : ICanvasFrame where TPixel : unmanaged, IPixel { private readonly ICanvasFrame parent; private readonly Rectangle region; /// /// Initializes a new instance of the class. /// /// The parent frame that owns the target pixels. /// The child region in parent-local coordinates. public CanvasRegionFrame(ICanvasFrame parent, Rectangle region) { Guard.NotNull(parent, nameof(parent)); Guard.MustBeGreaterThanOrEqualTo(region.Width, 0, nameof(region)); Guard.MustBeGreaterThanOrEqualTo(region.Height, 0, nameof(region)); this.parent = parent; this.region = region; } /// public Rectangle Bounds => new( this.parent.Bounds.X + this.region.X, this.parent.Bounds.Y + this.region.Y, this.region.Width, this.region.Height); /// public bool TryGetCpuRegion(out Buffer2DRegion region) { if (!this.parent.TryGetCpuRegion(out Buffer2DRegion parentRegion)) { region = default; return false; } region = parentRegion.GetSubRegion(this.region); return true; } /// public bool TryGetNativeSurface([NotNullWhen(true)] out NativeSurface? surface) => this.parent.TryGetNativeSurface(out surface); } }