pub struct VipsImage<'a> {
pub c: *mut VipsImage,
/* private fields */
}Expand description
Representation of a libvips image.
This struct wraps a raw pointer to a VipsImage from the libvips C library.
It provides methods for creating, manipulating, and destroying images.
§Lifetimes
The 'a lifetime parameter ensures that the VipsImage does not outlive any data it references.
This is particularly important for images created from memory buffers, where the buffer must remain valid
for the lifetime of the VipsImage.
§Memory Management
The VipsImage struct implements the Drop trait to automatically unreference the underlying
libvips image when the VipsImage instance goes out of scope. This helps prevent memory leaks
when working with images in Rust.
§Thread Safety
The VipsImage struct is not inherently thread-safe. Users must ensure that instances are not
accessed concurrently from multiple threads unless proper synchronization is implemented.
§Error Handling
Many methods on VipsImage return a Result type to handle errors that may occur during
image operations. Users should handle these errors appropriately in their code.
§Safety Note
The VipsImage struct contains a raw pointer to a libvips image.
The user must ensure that the pointer is valid and that the image is properly managed.
The Drop implementation will unreference the image when the VipsImage instance is dropped.
§Example
use vips::*;
fn main() -> Result<()> {
let _instance = VipsInstance::new("app_test", true)?;
let img = VipsImage::from_file("input.jpg")?;
let thumb = img.thumbnail(100, 100, VipsSize::VIPS_SIZE_BOTH)?;
thumb.write_to_file("thumb.jpg")?;
Ok(())
}Fields§
§c: *mut VipsImageImplementations§
Source§impl<'a> VipsImage<'a>
impl<'a> VipsImage<'a>
Sourcepub fn new_memory() -> Result<VipsImage<'a>>
pub fn new_memory() -> Result<VipsImage<'a>>
Sourcepub fn from_file<S: Into<Vec<u8>>>(path: S) -> Result<VipsImage<'a>>
pub fn from_file<S: Into<Vec<u8>>>(path: S) -> Result<VipsImage<'a>>
Create a VipsImage from a file.
§Arguments
path- The file path to load the image from.
§Errors
Returns an error if the image loading fails.
§Returns
A Result containing the loaded VipsImage or an error.
§Example
use vips::*;
fn main() -> Result<()> {
let _instance = VipsInstance::new("app_test", true)?;
let img = VipsImage::from_file("input.jpg")?;
Ok(())
}Sourcepub fn from_memory(
buf: Vec<u8>,
width: u32,
height: u32,
bands: u8,
format: VipsBandFormat,
) -> Result<VipsImage<'a>>
pub fn from_memory( buf: Vec<u8>, width: u32, height: u32, bands: u8, format: VipsBandFormat, ) -> Result<VipsImage<'a>>
Create a VipsImage from a memory buffer.
§Arguments
buf- The buffer containing the image data.width- The width of the image.height- The height of the image.bands- The number of bands (channels) in the image.format- The band format of the image.
§Errors
Returns an error if the image creation fails.
§Example
use vips::*;
fn main() -> Result<()> {
let _instance = VipsInstance::new("app_test", true)?;
let img_data: Vec<u8> = vec![/* image data */];
let img = VipsImage::from_memory(img_data, 800, 600, 3, VipsBandFormat::VIPS_FORMAT_UCHAR)?;
Ok(())
}Sourcepub fn from_memory_reference(
buf: &'a [u8],
width: u32,
height: u32,
bands: u8,
format: VipsBandFormat,
) -> Result<VipsImage<'a>>
pub fn from_memory_reference( buf: &'a [u8], width: u32, height: u32, bands: u8, format: VipsBandFormat, ) -> Result<VipsImage<'a>>
Create a VipsImage from a memory buffer reference.
§Arguments
buf- The buffer slice containing the image data.width- The width of the image.height- The height of the image.bands- The number of bands (channels) in the image.format- The band format of the image.
§Returns
A Result containing the created VipsImage or an error.
§Errors
Returns an error if the image creation fails.
§Example
use vips::*;
fn main() -> Result<()> {
let _instance = VipsInstance::new("app_test", true)?;
let img_data: &[u8] = &[/* image data */];
let img = VipsImage::from_memory_reference(img_data, 800, 600, 3, VipsBandFormat::VIPS_FORMAT_UCHAR)?;
Ok(())
}Sourcepub fn from_buffer(buf: &'a [u8]) -> Result<VipsImage<'a>>
pub fn from_buffer(buf: &'a [u8]) -> Result<VipsImage<'a>>
Create a VipsImage from a byte buffer.
§Arguments
buf- The buffer slice containing the image data.
§Returns
A Result containing the created VipsImage or an error.
§Errors
Returns an error if the image creation fails.
§Example
use vips::*;
fn main() -> Result<()> {
let _instance = VipsInstance::new("app_test", true)?;
let img_data: &[u8] = &[/* image data */];
let img = VipsImage::from_buffer(img_data)?;
Ok(())
}Sourcepub fn draw_rect(
&mut self,
ink: &[f64],
left: u32,
top: u32,
width: u32,
height: u32,
) -> Result<()>
pub fn draw_rect( &mut self, ink: &[f64], left: u32, top: u32, width: u32, height: u32, ) -> Result<()>
Draw a rectangle on the image.
§Arguments
ink- The color to use for drawing, as a slice of f64 values.left- The left coordinate of the rectangle.top- The top coordinate of the rectangle.width- The width of the rectangle.height- The height of the rectangle.
§Returns
A Result indicating success or failure.
§Errors
Returns an error if the drawing operation fails.
§Example
use vips::*;
fn main() -> Result<()> {
let _instance = VipsInstance::new("app_test", true)?;
let mut img = VipsImage::from_file("input.jpg")?;
img.draw_rect(&[255.0, 0.0, 0.0], 10, 10, 100, 50)?;
img.write_to_file("output.jpg")?;
Ok(())
}pub fn draw_rect1( &mut self, ink: f64, left: u32, top: u32, width: u32, height: u32, ) -> Result<()>
pub fn draw_point(&mut self, ink: &[f64], x: i32, y: i32) -> Result<()>
pub fn draw_point1(&mut self, ink: f64, x: i32, y: i32) -> Result<()>
pub fn draw_image( &mut self, img: &VipsImage<'_>, x: i32, y: i32, mode: VipsCombineMode, ) -> Result<()>
pub fn draw_mask( &mut self, ink: &[f64], mask: &VipsImage<'_>, x: i32, y: i32, ) -> Result<()>
pub fn draw_mask1( &mut self, ink: f64, mask: &VipsImage<'_>, x: i32, y: i32, ) -> Result<()>
pub fn draw_line( &mut self, ink: &[f64], x1: i32, y1: i32, x2: i32, y2: i32, ) -> Result<()>
pub fn draw_line1( &mut self, ink: f64, x1: i32, y1: i32, x2: i32, y2: i32, ) -> Result<()>
pub fn draw_circle( &mut self, ink: &[f64], cx: i32, cy: i32, r: i32, fill: bool, ) -> Result<()>
pub fn draw_circle1( &mut self, ink: f64, cx: i32, cy: i32, r: i32, fill: bool, ) -> Result<()>
pub fn draw_flood(&mut self, ink: &[f64], x: i32, y: i32) -> Result<()>
pub fn draw_flood1(&mut self, ink: f64, x: i32, y: i32) -> Result<()>
pub fn draw_smudge( &mut self, left: u32, top: u32, width: u32, height: u32, ) -> Result<()>
pub fn merge( &self, another: &VipsImage<'_>, direction: VipsDirection, dx: i32, dy: i32, mblend: Option<i32>, ) -> Result<VipsImage<'a>>
pub fn mosaic( &self, sec: &VipsImage<'_>, direction: VipsDirection, xref: i32, yref: i32, xsec: i32, ysec: i32, bandno: Option<i32>, hwindow: Option<i32>, harea: Option<i32>, mblend: Option<i32>, ) -> Result<VipsImage<'_>>
pub fn mosaic1( &self, sec: &VipsImage<'_>, direction: VipsDirection, xr1: i32, yr1: i32, xs1: i32, ys1: i32, xr2: i32, yr2: i32, xs2: i32, ys2: i32, search: Option<bool>, hwindow: Option<i32>, harea: Option<i32>, interpolate: Option<VipsInterpolate>, mblend: Option<i32>, bandno: Option<i32>, ) -> Result<VipsImage<'_>>
pub fn match_( &self, sec: &VipsImage<'_>, xr1: i32, yr1: i32, xs1: i32, ys1: i32, xr2: i32, yr2: i32, xs2: i32, ys2: i32, search: Option<bool>, hwindow: Option<i32>, harea: Option<i32>, interpolate: Option<VipsInterpolate>, ) -> Result<VipsImage<'_>>
pub fn globalbalance( &self, gamma: Option<f64>, int_output: Option<bool>, ) -> Result<VipsImage<'_>>
pub fn remosaic(&self, old_str: &str, new_str: &str) -> Result<VipsImage<'_>>
Sourcepub fn thumbnail(
&self,
width: u32,
height: u32,
size: VipsSize,
) -> Result<VipsImage<'_>>
pub fn thumbnail( &self, width: u32, height: u32, size: VipsSize, ) -> Result<VipsImage<'_>>
Create a thumbnail of the image.
§Arguments
width- The desired width of the thumbnail.height- The desired height of the thumbnail.size- The size mode for the thumbnail (e.g.,VIPS_SIZE_BOTH,VIPS_SIZE_UP, etc.).
§Returns
A Result containing the thumbnail VipsImage or an error.
§Errors
Returns an error if the thumbnail creation fails.
§Example
use vips::*;
fn main() -> Result<()> {
let _instance = VipsInstance::new("app_test", true)?;
let img = VipsImage::from_file("input.jpg")?;
let thumb = img.thumbnail(100, 100, VipsSize::VIPS_SIZE_BOTH)?;
thumb.write_to_file("thumb.jpg")?;
Ok(())
}Sourcepub fn resize(
&self,
scale: f64,
vscale: Option<f64>,
kernel: Option<VipsKernel>,
) -> Result<VipsImage<'_>>
pub fn resize( &self, scale: f64, vscale: Option<f64>, kernel: Option<VipsKernel>, ) -> Result<VipsImage<'_>>
Resize the image.
§Arguments
scale- The scaling factor for the horizontal dimension.vscale- Optional scaling factor for the vertical dimension. If not provided, it defaults to the value ofscale.kernel- Optional kernel to use for resizing. If not provided, it defaults toVIPS_KERNEL_LANCZOS3.
§Returns
A Result containing the resized VipsImage or an error.
§Errors
Returns an error if the resizing operation fails.
§Example
use vips::*;
fn main() -> Result<()> {
let _instance = VipsInstance::new("app_test", true)?;
let img = VipsImage::from_file("input.jpg")?;
let resized_img = img.resize(0.5, None, None)?;
resized_img.write_to_file("resized.jpg")?;
Ok(())
}