refilelabs-image 0.3.1

Wasm-based image processing library developed by re;file labs
Documentation

refilelabs-image

Crates.io Documentation

A Rust library for advanced image manipulation and format conversion. Provides tools for loading image metadata, converting images, resizing images, cropping images, and retrieving raw pixel data.

Used under the hood at re;file labs to power all image processing features.

Installation

[dependencies]
refilelabs-image = "0.2.5"

Features

  • Load image metadata (dimensions, EXIF, GPS)
  • Retrieve raw RGBA pixel data
  • Convert images between formats
  • Resize images to exact pixel dimensions
  • Crop images to any region
  • Custom conversion settings (e.g. SVG rasterization size)

Note: The native Rust API (#[cfg(not(feature = "wasm"))]) excludes save_metadata, which requires wasm-bindgen types. For JavaScript/WASM usage see the npm package.

API Reference

convert_image

pub fn convert_image(
    file: &[u8],
    src_type: &str,
    target_type: &str,
    convert_settings: &Option<Settings>,
) -> Result<Vec<u8>, WasmImageError>

Converts an image from one format to another.


load_metadata

pub fn load_metadata(file: &[u8], src_type: &str) -> Result<Metadata, WasmImageError>

Extracts dimensions and EXIF metadata from an image file.


resize_image

pub fn resize_image(
    file: &[u8],
    src_type: &str,
    width: u32,
    height: u32,
) -> Result<Vec<u8>, WasmImageError>

Resizes an image to exact pixel dimensions using the Lanczos3 filter. Preserves the source format. SVG input is rasterized to PNG.


crop_image

pub fn crop_image(
    file: &[u8],
    src_type: &str,
    x: u32,
    y: u32,
    width: u32,
    height: u32,
) -> Result<Vec<u8>, WasmImageError>

Crops an image to the specified region. Preserves the source format.


get_pixels

pub fn get_pixels(file: &[u8], src_type: &str) -> Result<ImageData, WasmImageError>

Decodes an image to raw RGBA pixel data.


Data Structures

Metadata

pub struct Metadata {
    pub width: u32,
    pub height: u32,
    pub other: Option<HashMap<String, String>>, // Non-GPS EXIF fields
    pub gps: Option<HashMap<String, String>>,   // GPS EXIF fields (None if not present)
    pub errors: Option<Vec<String>>,            // Non-fatal EXIF parse errors
}

ImageData

pub struct ImageData {
    pub width: u32,
    pub height: u32,
    pub aspect_ratio: f32,
    pub color_depth: u8,
    pub pixels: Vec<u8>, // Raw RGBA
}

Settings

Format-specific conversion settings. Currently supports SVG rasterization size.


Usage Example

use refilelabs_image::{convert_image, crop_image, get_pixels, load_metadata, resize_image};
use std::fs;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let file = fs::read("input.png")?;
    let src_type = "image/png";

    let metadata = load_metadata(&file, src_type)?;
    println!("{}x{}", metadata.width, metadata.height);
    if let Some(gps) = metadata.gps {
        println!("GPS: {:?}", gps);
    }

    let converted = convert_image(&file, src_type, "image/webp", &None)?;
    fs::write("output.webp", converted)?;

    let resized = resize_image(&file, src_type, 800, 600)?;
    fs::write("output_resized.png", resized)?;

    let cropped = crop_image(&file, src_type, 10, 10, 200, 150)?;
    fs::write("output_cropped.png", cropped)?;

    let pixels = get_pixels(&file, src_type)?;
    println!("{} RGBA pixels", pixels.pixels.len() / 4);

    Ok(())
}

License

MIT