stitchaton/vendor/NetVips/Target.cs

99 lines
3.5 KiB
C#
Raw Permalink Normal View History

2025-07-31 00:17:59 +07:00
using System;
using System.Text;
namespace NetVips;
/// <summary>
/// An output connection.
/// </summary>
public class Target : Connection
{
/// <inheritdoc cref="Connection"/>
internal Target(nint pointer) : base(pointer)
{
}
/// <summary>
/// Get the memory object held by the target when using <see cref="NewToMemory"/>.
/// </summary>
public byte[] Blob => (byte[])Get("blob");
/// <summary>
/// Make a new target to write to a file descriptor (a small integer).
/// </summary>
/// <remarks>
/// Make a new target that is attached to the descriptor. For example:
/// <code language="lang-csharp">
/// using var target = Target.NewToDescriptor(1);
/// </code>
/// Makes a descriptor attached to stdout.
///
/// You can pass this target to (for example) <see cref="Image.WriteToTarget"/>.
/// </remarks>
/// <param name="descriptor">Write to this file descriptor.</param>
/// <returns>A new <see cref="Target"/>.</returns>
/// <exception cref="VipsException">If unable to create a new <see cref="Target"/> from <paramref name="descriptor"/>.</exception>
public static Target NewToDescriptor(int descriptor)
{
var pointer = Internal.VipsTarget.NewToDescriptor(descriptor);
if (pointer == IntPtr.Zero)
{
throw new VipsException($"can't create output target to descriptor {descriptor}");
}
return new Target(pointer);
}
/// <summary>
/// Make a new target to write to a file.
/// </summary>
/// <remarks>
/// Make a new target that will write to the named file. For example:
/// <code language="lang-csharp">
/// using var target = Target.NewToFile("myfile.jpg");
/// </code>
/// You can pass this target to (for example) <see cref="Image.WriteToTarget"/>.
/// </remarks>
/// <param name="filename">Write to this this file.</param>
/// <returns>A new <see cref="Target"/>.</returns>
/// <exception cref="VipsException">If unable to create a new <see cref="Target"/> from <paramref name="filename"/>.</exception>
public static Target NewToFile(string filename)
{
var bytes = Encoding.UTF8.GetBytes(filename + char.MinValue); // Ensure null-terminated string
var pointer = Internal.VipsTarget.NewToFile(bytes);
if (pointer == IntPtr.Zero)
{
throw new VipsException($"can't create output target to filename {filename}");
}
return new Target(pointer);
}
/// <summary>
/// Make a new target to write to an area of memory.
/// </summary>
/// <remarks>
/// Make a new target that will write to memory. For example:
/// <code language="lang-csharp">
/// using var target = Target.NewToMemory();
/// </code>
/// You can pass this target to (for example) <see cref="Image.WriteToTarget"/>.
///
/// After writing to the target, fetch the bytes from the target object with:
/// <code language="lang-csharp">
/// var bytes = target.Blob;
/// </code>
/// </remarks>
/// <returns>A new <see cref="Target"/>.</returns>
/// <exception cref="VipsException">If unable to create a new <see cref="Target"/>.</exception>
public static Target NewToMemory()
{
var pointer = Internal.VipsTarget.NewToMemory();
if (pointer == IntPtr.Zero)
{
throw new VipsException("can't create output target to memory");
}
return new Target(pointer);
}
}