using Brutal.Pointers;
namespace Brutal.NotCursesApi;
///
/// Hand-written ergonomics for the notcurses visual handle. Most instance methods (incl.
/// ) are auto-generated in Generated/Wrappers.gen.cs; this file adds
/// the static factory for raw RGBA pixel data and the contract.
///
public readonly unsafe partial struct NcVisualHandle : IDisposable
{
///
/// Creates a visual from a flat row-major RGBA8888 pixel buffer. The buffer is copied internally,
/// so the caller doesn't need to keep it alive.
///
/// Pixel data, × × 4 bytes.
/// Image height in pixels.
/// Image width in pixels.
public static NcVisualHandle FromRgba(ReadOnlySpan rgba, int rows, int cols)
{
if (rgba.Length < rows * cols * 4)
throw new ArgumentException(
$"RGBA buffer is {rgba.Length} bytes but {rows * cols * 4} were expected.",
nameof(rgba)
);
fixed (byte* p = rgba)
{
var v = NotCurses.PInvoke.ncvisual_from_rgba(new Ptr(p), rows, cols * 4, cols);
if (v.IsNull())
throw new InvalidOperationException("ncvisual_from_rgba failed.");
return v;
}
}
/// Releases the visual and any plane it owns. Calls .
public void Dispose() => Destroy();
}