fast_image_resize

repository·main·Indexed 19 days ago

https://github.com/cykooz/fast_image_resize

A high-performance Rust library for rapid image resizing using SIMD instructions (SSE4.1, AVX2, Neon, Wasm32 SIMD128). It supports various pixel formats (U8, U16, I32, F32) and algorithms including Nearest, Box, Bilinear, Bicubic (CatmullRom), and Lanczos3. The library provides optional multi-threading via rayon, no_std support, and tools for linear colorspace conversion using PixelComponentMapper.

Tokens
10.9K
Snippets
28
Records
51
Agent score
65%

What's inside fast_image_resize

  1. Overview of fast_image_resize

    main

    fast_image_resize is a high-performance Rust library for image resizing that utilizes SIMD instructions for acceleration. It supports various pixel formats including U8, U16, I32, and F32 with different component counts (e.g., RGB, RGBA, Grayscale).

    Supported Pixel Formats and Optimizations

    FormatDescriptionSSE4.1AVX2NeonWasm32 SIMD128
    U8One u8 component (e.g. L)++++
    U8x2Two u8 components (e.g. LA)++++
    U8x3Three u8 components (e.g. RGB)++++
    U8x4Four u8 components (e.g. RGBA)++++
    U16One u16 component (e.g. L16)++++
    U16x2Two u16 components (e.g. LA16)++++
    U16x3Three u16 components (e.g. RGB16)++++
    U16x4Four u16 components (e.g. RGBA16)++++
    I32One i32 component (e.g. L32)----
    F32One f32 component (e.g. L32F)++--
    F32x2Two f32 components (e.g. LA32F)++--
    F32x3Three f32 components (e.g. RGB32F)++--
    F32x4Four f32 components (e.g. RGBA32F)++--
  2. Wasm32 Performance Benchmarks for fast_image_resize

    main
    Benchmarks for the fast_image_resize crate on the Wasm32 architecture (tested with wasmtime). The results compare fir rust (standard Rust implementation) and fir simd128 (SIMD-accelerated implementation) against other crates like image and resize across various pixel formats and algorithms.
  3. Handle colorspace conversion for correct resizing

    main

    The resizer does not automatically convert images into a linear colorspace. For correct resizing of non-linear color spaces (like sRGB), you should convert the image to a linear colorspace before resizing and convert it back to the target colorspace afterward.

    To facilitate this, the crate provides:

    • PixelComponentMapper: A structure for creating colorspace converters for u8 and u16 based pixels.
    • create_gamma_22_mapper(): Creates a mapper for gamma 2.2 to linear conversion.
    • create_srgb_mapper(): Creates a mapper for sRGB to linear conversion.
  4. Supported Image Pixel Formats

    main

    The crate supports a wide range of pixel formats across different bit depths and color spaces, including:

    • U8 (8-bit Unsigned): L8 (Grayscale), RGB8 (U8x3), RGBA8 (U8x4), LA8 (Luma with Alpha, U8x2).
    • U16 (16-bit Unsigned): L16 (Grayscale), RGB16 (U16x3), RGBA16 (U16x4), LA16 (Luma with Alpha, U16x2).
    • F32 (32-bit Float): L32F (Grayscale), RGB32F (F32x3), RGBA32F (F32x4), LA32F (Luma with Alpha, F32x2).

    Note: For F32 based pixels, fast_image_resize uses f64 for intermediate calculations, which may result in performance that is slower than or equal to libraries using f32 for intermediate steps.

  5. Supported Resize Algorithms in fast_image_resize

    main

    The fast_image_resize crate supports several resizing algorithms with varying kernel sizes and performance characteristics:

    • Nearest: Nearest neighbor interpolation.
    • Box: Convolution with a minimal kernel size of 1x1 px.
    • Bilinear: Convolution with a minimal kernel size of 2x2 px.
    • Bicubic (CatmullRom): Convolution with a minimal kernel size of 4x4 px.
    • Lanczos3: Convolution with a minimal kernel size of 6x6 px.
  6. Supported Resize Algorithms

    main

    The fast_image_resize crate supports several resizing algorithms, which differ by their convolution kernel size and computational complexity:

    • Nearest: Nearest-neighbor interpolation.
    • Box: Convolution with a minimal kernel size of 1x1 px.
    • Bilinear: Convolution with a minimal kernel size of 2x2 px.
    • Bicubic (CatmullRom): Convolution with a minimal kernel size of 4x4 px.
    • Lanczos3: Convolution with a minimal kernel size of 6x6 px.
  7. Prepare the development environment

    main

    Before developing or running benchmarks, ensure you have the necessary system libraries and Rust toolchains installed.

    System Libraries

    • Install libvips-dev (required for benchmarks).

    Rust Toolchains

    Install the target toolchains using rustup depending on your target platform:

    • Arm64: aarch64-unknown-linux-gnu
    • Wasm32: wasm32-wasip2 (requires Wasmtime to be installed).
    rustup target add aarch64-unknown-linux-gnu
    rustup target add wasm32-wasip2
  8. Use fast_image_resize in a no_std environment

    main

    To use this crate in a no_std environment, you must disable default features and enable the no_std feature in your Cargo.toml.

    [dependencies]
    fast_image_resize = { version = "6.0", default-features = false, features = ["no_std"] }
  9. Configure and run for Wasm32

    main

    To target wasm32-wasip2, you must configure your .cargo/config.toml to specify the build target and the runner (Wasmtime).

    Configuration

    Add the following to .cargo/config.toml:

    [build]
    target = "wasm32-wasip2"
    
    [target.wasm32-wasip2]
    runner = "wasmtime --dir=. --"

    Running Tests and Benchmarks in Wasm32

    When running via the runner, you must pass environment variables through the runner command using the --env flag.

    Run tests with image saving:

    CARGO_TARGET_WASM32_WASIP2_RUNNER="wasmtime --dir=. --env SAVE_RESULT=1 --" cargo test

    Run comparison benchmarks:

    CARGO_TARGET_WASM32_WASIP2_RUNNER="wasmtime --dir=. --env WRITE_COMPARE_RESULT=1 --" cargo bench --no-fail-fast -- --color=always Compare
  10. Run and compare benchmarks

    main

    Benchmarks can be run to compare fast_image_resize against other crates. Results are written to report files (e.g., ./benchmarks-x86_64.md).

    Standard Comparison

    Use the Compare argument and set WRITE_COMPARE_RESULT=1:

    WRITE_COMPARE_RESULT=1 cargo bench -- Compare

    Using cached results

    If you want to use existing benchmark results for other crates, specify a RESULTS_LIFETIME in days:

    WRITE_COMPARE_RESULT=1 RESULTS_LIFETIME=5 cargo bench -- Compare

    Quick mode

    To run a specific benchmark in quick mode:

    cargo bench --bench bench_resize -- --color=always --quick