using System;
using System.Text;
namespace NetVips;
///
/// An output connection.
///
public class Target : Connection
{
///
internal Target(nint pointer) : base(pointer)
{
}
///
/// Get the memory object held by the target when using .
///
public byte[] Blob => (byte[])Get("blob");
///
/// Make a new target to write to a file descriptor (a small integer).
///
///
/// Make a new target that is attached to the descriptor. For example:
///
/// using var target = Target.NewToDescriptor(1);
///
/// Makes a descriptor attached to stdout.
///
/// You can pass this target to (for example) .
///
/// Write to this file descriptor.
/// A new .
/// If unable to create a new from .
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);
}
///
/// Make a new target to write to a file.
///
///
/// Make a new target that will write to the named file. For example:
///
/// using var target = Target.NewToFile("myfile.jpg");
///
/// You can pass this target to (for example) .
///
/// Write to this this file.
/// A new .
/// If unable to create a new from .
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);
}
///
/// Make a new target to write to an area of memory.
///
///
/// Make a new target that will write to memory. For example:
///
/// using var target = Target.NewToMemory();
///
/// You can pass this target to (for example) .
///
/// After writing to the target, fetch the bytes from the target object with:
///
/// var bytes = target.Blob;
///
///
/// A new .
/// If unable to create a new .
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);
}
}