using System;
using System.IO;
namespace LLama.Native
{
///
/// A Reference to a llava Image Embed handle
///
public sealed class SafeLlavaImageEmbedHandle
: SafeLLamaHandleBase
{
///
/// Create an image embed from an image file
///
///
///
/// Path to the image file. Supported formats:
///
/// - JPG
/// - PNG
/// - BMP
/// - TGA
///
///
///
///
public static SafeLlavaImageEmbedHandle CreateFromFileName( SafeLlavaModelHandle ctxLlava, LLamaContext ctxLlama, string image )
{
// Try to open the image file, this will check:
// - File exists (automatically throws FileNotFoundException)
// - File is readable (explicit check)
// This provides better error messages that llama.cpp, which would throw an access violation exception in both cases.
using (var fs = new FileStream(image, FileMode.Open))
if (!fs.CanRead)
throw new InvalidOperationException($"Llava image file '{image}' is not readable");
return NativeApi.llava_image_embed_make_with_filename(ctxLlava, (int) ctxLlama.BatchThreads, image);
}
///
/// Create an image embed from the bytes of an image.
///
///
///
/// Image bytes. Supported formats:
///
/// - JPG
/// - PNG
/// - BMP
/// - TGA
///
///
///
public static SafeLlavaImageEmbedHandle CreateFromMemory( SafeLlavaModelHandle ctxLlava, LLamaContext ctxLlama, byte[] image )
{
return NativeApi.llava_image_embed_make_with_bytes(ctxLlava, (int) ctxLlama.BatchThreads, image, image.Length);
}
///
protected override bool ReleaseHandle()
{
NativeApi.llava_image_embed_free(DangerousGetHandle());
SetHandle(IntPtr.Zero);
return true;
}
}
}