using System;
using System.Runtime.InteropServices;
using NetVips.Internal;
namespace NetVips;
///
/// Wrap a object.
///
///
/// A region is a small part of an image. You use regions to read pixels
/// out of images without storing the entire image in memory.
/// At least libvips 8.8 is needed.
///
public class Region : VipsObject
{
private Region(nint pointer) : base(pointer)
{
}
///
/// Make a region on an image.
///
/// to create this region on.
/// A new .
/// If unable to make a new region on .
public static Region New(Image image)
{
var vi = VipsRegion.New(image);
if (vi == IntPtr.Zero)
{
throw new VipsException("unable to make region");
}
return new Region(vi);
}
///
/// Width of pixels held by region.
///
public int Width => VipsRegion.Width(this);
///
/// Height of pixels held by region.
///
public int Height => VipsRegion.Height(this);
///
/// Fetch an area of pixels.
///
/// Left edge of area to fetch.
/// Top edge of area to fetch.
/// Width of area to fetch.
/// Height of area to fetch.
/// An array of bytes filled with pixel data.
public byte[] Fetch(int left, int top, int width, int height)
{
var pointer = VipsRegion.Fetch(this, left, top, width, height, out var size);
if (pointer == IntPtr.Zero)
{
throw new VipsException("unable to fetch from region");
}
var managedArray = new byte[size];
Marshal.Copy(pointer, managedArray, 0, (int)size);
GLib.GFree(pointer);
return managedArray;
}
}