VipsImage

Struct VipsImage 

Source
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 VipsImage

Implementations§

Source§

impl<'a> VipsImage<'a>

Source

pub fn new() -> Result<VipsImage<'a>>

Create a new empty VipsImage.

§Errors

Returns an error if the image creation fails.

§Example
use vips::*;

fn main() -> Result<()> {
    let _instance = VipsInstance::new("app_test", true)?;
    let img = VipsImage::new()?;
    Ok(())
}
Source

pub fn new_memory() -> Result<VipsImage<'a>>

Create a new empty VipsImage in memory.

§Errors

Returns an error if the image creation fails.

§Example
use vips::*;

fn main() -> Result<()> {
    let _instance = VipsInstance::new("app_test", true)?;
    let img = VipsImage::new_memory()?;
    Ok(())
}
Source

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(())
}
Source

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(())
}
Source

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(())
}
Source

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(())
}
Source

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(())
}
Source

pub fn draw_rect1( &mut self, ink: f64, left: u32, top: u32, width: u32, height: u32, ) -> Result<()>

Source

pub fn draw_point(&mut self, ink: &[f64], x: i32, y: i32) -> Result<()>

Source

pub fn draw_point1(&mut self, ink: f64, x: i32, y: i32) -> Result<()>

Source

pub fn draw_image( &mut self, img: &VipsImage<'_>, x: i32, y: i32, mode: VipsCombineMode, ) -> Result<()>

Source

pub fn draw_mask( &mut self, ink: &[f64], mask: &VipsImage<'_>, x: i32, y: i32, ) -> Result<()>

Source

pub fn draw_mask1( &mut self, ink: f64, mask: &VipsImage<'_>, x: i32, y: i32, ) -> Result<()>

Source

pub fn draw_line( &mut self, ink: &[f64], x1: i32, y1: i32, x2: i32, y2: i32, ) -> Result<()>

Source

pub fn draw_line1( &mut self, ink: f64, x1: i32, y1: i32, x2: i32, y2: i32, ) -> Result<()>

Source

pub fn draw_circle( &mut self, ink: &[f64], cx: i32, cy: i32, r: i32, fill: bool, ) -> Result<()>

Source

pub fn draw_circle1( &mut self, ink: f64, cx: i32, cy: i32, r: i32, fill: bool, ) -> Result<()>

Source

pub fn draw_flood(&mut self, ink: &[f64], x: i32, y: i32) -> Result<()>

Source

pub fn draw_flood1(&mut self, ink: f64, x: i32, y: i32) -> Result<()>

Source

pub fn draw_smudge( &mut self, left: u32, top: u32, width: u32, height: u32, ) -> Result<()>

Source

pub fn merge( &self, another: &VipsImage<'_>, direction: VipsDirection, dx: i32, dy: i32, mblend: Option<i32>, ) -> Result<VipsImage<'a>>

Source

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<'_>>

Source

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<'_>>

Source

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<'_>>

Source

pub fn globalbalance( &self, gamma: Option<f64>, int_output: Option<bool>, ) -> Result<VipsImage<'_>>

Source

pub fn remosaic(&self, old_str: &str, new_str: &str) -> Result<VipsImage<'_>>

Source

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(())
}
Source

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 of scale.
  • kernel - Optional kernel to use for resizing. If not provided, it defaults to VIPS_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(())
}
Source

pub fn write_to_file<S: Into<Vec<u8>>>(&self, path: S) -> Result<()>

Trait Implementations§

Source§

impl<'a> Drop for VipsImage<'a>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for VipsImage<'a>

§

impl<'a> RefUnwindSafe for VipsImage<'a>

§

impl<'a> !Send for VipsImage<'a>

§

impl<'a> !Sync for VipsImage<'a>

§

impl<'a> Unpin for VipsImage<'a>

§

impl<'a> UnwindSafe for VipsImage<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.