fast_image_resize
Rust library for fast image resizing with using of SIMD instructions.
Supported pixel formats and available optimisations:
| Format | Description | SSE4.1 | AVX2 | Neon | Wasm32 SIMD128 |
|---|---|---|---|---|---|
| U8 | One u8 component per pixel (e.g. L) |
+ | + | + | + |
| U8x2 | Two u8 components per pixel (e.g. LA) |
+ | + | + | + |
| U8x3 | Three u8 components per pixel (e.g. RGB) |
+ | + | + | + |
| U8x4 | Four u8 components per pixel (e.g. RGBA, RGBx, CMYK) |
+ | + | + | + |
| U16 | One u16 components per pixel (e.g. L16) |
+ | + | + | + |
| U16x2 | Two u16 components per pixel (e.g. LA16) |
+ | + | + | + |
| U16x3 | Three u16 components per pixel (e.g. RGB16) |
+ | + | + | + |
| U16x4 | Four u16 components per pixel (e.g. RGBA16, RGBx16, CMYK16) |
+ | + | + | + |
| I32 | One i32 component per pixel |
- | - | - | - |
| F32 | One f32 component per pixel |
- | - | - | - |
Colorspace
Resizer from this crate does not convert image into linear colorspace during resize process. If it is important for you to resize images with a non-linear color space (e.g. sRGB) correctly, then you have to convert it to a linear color space before resizing and convert back to the color space of result image. Read more about resizing with respect to color space.
This crate provides the
PixelComponentMapper
structure that allows you to create colorspace converters for images
whose pixels based on u8 and u16 components.
In addition, the crate contains functions create_gamma_22_mapper()
and create_srgb_mapper() to create instance of PixelComponentMapper
that converts images from sRGB or gamma 2.2 into linear colorspace and back.
Some benchmarks for x86_64
All benchmarks: x86_64, ARM64, WASM32.
Other libraries used to compare of resizing speed:
- image (https://crates.io/crates/image)
- resize (https://crates.io/crates/resize)
- libvips (single-threaded mode, cache disabled)
Resize RGB8 image (U8x3) 4928x3279 => 852x567
Pipeline:
src_image => resize => dst_image
- Source image nasa-4928x3279.png
- Numbers in table is mean duration of image resizing in milliseconds.
| Nearest | Box | Bilinear | Bicubic | Lanczos3 | |
|---|---|---|---|---|---|
| image | 28.20 | - | 82.45 | 134.07 | 192.70 |
| resize | - | 26.83 | 53.56 | 97.73 | 144.63 |
| libvips | 7.73 | 60.66 | 19.84 | 30.15 | 39.46 |
| fir rust | 0.28 | 9.78 | 15.46 | 27.36 | 39.57 |
| fir sse4.1 | 0.28 | 3.87 | 5.59 | 9.89 | 15.44 |
| fir avx2 | 0.28 | 2.67 | 3.54 | 6.96 | 13.22 |
Resize RGBA8 image (U8x4) 4928x3279 => 852x567
Pipeline:
src_image => multiply by alpha => resize => divide by alpha => dst_image
- Source image nasa-4928x3279-rgba.png
- Numbers in table is mean duration of image resizing in milliseconds.
- The
imagecrate does not support multiplying and dividing by alpha channel.
| Nearest | Box | Bilinear | Bicubic | Lanczos3 | |
|---|---|---|---|---|---|
| resize | - | 42.96 | 85.43 | 147.79 | 211.49 |
| libvips | 10.06 | 122.80 | 188.97 | 338.18 | 499.99 |
| fir rust | 0.19 | 20.10 | 27.08 | 41.32 | 56.79 |
| fir sse4.1 | 0.19 | 10.03 | 12.24 | 18.57 | 25.15 |
| fir avx2 | 0.19 | 6.98 | 8.26 | 13.97 | 21.55 |
Resize L8 image (U8) 4928x3279 => 852x567
Pipeline:
src_image => resize => dst_image
- Source image nasa-4928x3279.png has converted into grayscale image with one byte per pixel.
- Numbers in table is mean duration of image resizing in milliseconds.
| Nearest | Box | Bilinear | Bicubic | Lanczos3 | |
|---|---|---|---|---|---|
| image | 25.96 | - | 56.78 | 84.17 | 112.12 |
| resize | - | 10.67 | 18.54 | 39.06 | 62.71 |
| libvips | 4.72 | 24.93 | 9.70 | 13.68 | 18.07 |
| fir rust | 0.15 | 4.08 | 5.24 | 7.48 | 11.33 |
| fir sse4.1 | 0.15 | 1.86 | 2.30 | 3.58 | 5.88 |
| fir avx2 | 0.15 | 1.66 | 1.86 | 2.24 | 4.21 |
Examples
Resize RGBA8 image
use BufWriter;
use NonZeroU32;
use PngEncoder;
use Reader as ImageReader;
use ;
use fast_image_resize as fr;
Resize with cropping
use NonZeroU32;
use PngEncoder;
use Reader as ImageReader;
use ;
use fast_image_resize as fr;
Change CPU extensions used by resizer
use fast_image_resize as fr;