- Notifications
You must be signed in to change notification settings - Fork32
Rust library for fast image resizing with using of SIMD instructions.
License
Apache-2.0, MIT licenses found
Licenses found
Cykooz/fast_image_resize
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Rust library for fast image resizing with using of SIMD instructions.
Supported pixel formats and available optimizations:
Format | Description | SSE4.1 | AVX2 | Neon | Wasm32 SIMD128 |
---|---|---|---|---|---|
U8 | Oneu8 component per pixel (e.g. L) | + | + | + | + |
U8x2 | Twou8 components per pixel (e.g. LA) | + | + | + | + |
U8x3 | Threeu8 components per pixel (e.g. RGB) | + | + | + | + |
U8x4 | Fouru8 components per pixel (e.g. RGBA, RGBx, CMYK) | + | + | + | + |
U16 | Oneu16 components per pixel (e.g. L16) | + | + | + | + |
U16x2 | Twou16 components per pixel (e.g. LA16) | + | + | + | + |
U16x3 | Threeu16 components per pixel (e.g. RGB16) | + | + | + | + |
U16x4 | Fouru16 components per pixel (e.g. RGBA16, RGBx16, CMYK16) | + | + | + | + |
I32 | Onei32 component per pixel (e.g. L32) | - | - | - | - |
F32 | Onef32 component per pixel (e.g. L32F) | + | + | - | - |
F32x2 | Twof32 components per pixel (e.g. LA32F) | + | + | - | - |
F32x3 | Threef32 components per pixel (e.g. RGB32F) | + | + | - | - |
F32x4 | Fourf32 components per pixel (e.g. RGBA32F) | + | + | - | - |
Resizer from this crate does not convert image into linear colorspaceduring a resize process. If it is important for you to resize images with anon-linear color space (e.g. sRGB) correctly, then you have to convertit to a linear color space before resizing and convert back to the color space ofresult image.Read moreabout resizing with respect to color space.
This crate provides thePixelComponentMapperstructure that allows you to create colorspace converters for imageswhose pixels based onu8
andu16
components.
In addition, the crate contains functionscreate_gamma_22_mapper()
andcreate_srgb_mapper()
to create instance ofPixelComponentMapper
that converts images from sRGB or gamma 2.2 into linear colorspace and back.
You should enable"rayon"
feature to turn on image processing inrayon thread pool.
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, single-threaded mode)
- libvips (single-threaded mode)
Pipeline:
src_image => resize => dst_image
- Source imagenasa-4928x3279.png
- Numbers in the table mean a duration of image resizing in milliseconds.
Nearest | Box | Bilinear | Bicubic | Lanczos3 | |
---|---|---|---|---|---|
image | 34.13 | - | 88.09 | 142.99 | 191.80 |
resize | 8.87 | 26.97 | 53.08 | 98.26 | 145.74 |
libvips | 2.38 | 61.57 | 5.66 | 9.70 | 16.03 |
fir rust | 0.28 | 10.93 | 15.35 | 25.77 | 37.09 |
fir sse4.1 | 0.28 | 3.43 | 5.39 | 9.82 | 15.34 |
fir avx2 | 0.28 | 2.62 | 3.80 | 6.89 | 13.22 |
Pipeline:
src_image => multiply by alpha => resize => divide by alpha => dst_image
- Source imagenasa-4928x3279-rgba.png
- Numbers in the table mean a duration of image resizing in milliseconds.
- The
image
crate does not support multiplying and dividing by alpha channel.
Nearest | Box | Bilinear | Bicubic | Lanczos3 | |
---|---|---|---|---|---|
resize | 9.90 | 37.85 | 74.20 | 133.72 | 201.29 |
libvips | 4.17 | 169.03 | 141.52 | 232.30 | 330.89 |
fir rust | 0.19 | 20.66 | 26.02 | 37.27 | 50.21 |
fir sse4.1 | 0.19 | 9.59 | 11.99 | 17.79 | 24.83 |
fir avx2 | 0.19 | 7.21 | 8.61 | 13.22 | 22.41 |
Pipeline:
src_image => resize => dst_image
- Source imagenasa-4928x3279.pnghas converted into grayscale image with one byte per pixel.
- Numbers in the table mean a duration of image resizing in milliseconds.
Nearest | Box | Bilinear | Bicubic | Lanczos3 | |
---|---|---|---|---|---|
image | 29.07 | - | 60.25 | 89.15 | 117.51 |
resize | 6.42 | 11.26 | 20.87 | 42.87 | 69.50 |
libvips | 2.57 | 25.05 | 6.82 | 9.85 | 12.68 |
fir rust | 0.15 | 4.45 | 5.57 | 9.02 | 12.31 |
fir sse4.1 | 0.15 | 1.52 | 2.09 | 3.52 | 5.65 |
fir avx2 | 0.15 | 1.54 | 1.76 | 2.80 | 4.03 |
Note: You must enable"image"
feature to support ofimage::DynamicImage.Otherwise, you have to convert such images into supported by the crate image type.
use std::io::BufWriter;use image::codecs::png::PngEncoder;use image::{ExtendedColorType,ImageEncoder,ImageReader};use fast_image_resize::{IntoImageView,Resizer};use fast_image_resize::images::Image;fnmain(){// Read source image from filelet src_image =ImageReader::open("./data/nasa-4928x3279.png").unwrap().decode().unwrap();// Create container for data of destination imagelet dst_width =1024;let dst_height =768;letmut dst_image =Image::new( dst_width, dst_height, src_image.pixel_type().unwrap(),);// Create Resizer instance and resize source image// into buffer of destination imageletmut resizer =Resizer::new(); resizer.resize(&src_image,&mut dst_image,None).unwrap();// Write destination image as PNG-fileletmut result_buf =BufWriter::new(Vec::new());PngEncoder::new(&mut result_buf).write_image( dst_image.buffer(), dst_width, dst_height, src_image.color().into(),).unwrap();}
use image::codecs::png::PngEncoder;use image::{ColorType,ImageReader,GenericImageView};use fast_image_resize::{IntoImageView,Resizer,ResizeOptions};use fast_image_resize::images::Image;fnmain(){let img =ImageReader::open("./data/nasa-4928x3279.png").unwrap().decode().unwrap();// Create container for data of destination imageletmut dst_image =Image::new(1024,768, img.pixel_type().unwrap(),);// Create Resizer instance and resize cropped source image// into buffer of destination imageletmut resizer =Resizer::new(); resizer.resize(&img,&mut dst_image,&ResizeOptions::new().crop(10.0,// left10.0,// top2000.0,// width2000.0,// height),).unwrap();}
use fast_image_resizeas fr;fnmain(){letmut resizer = fr::Resizer::new();#[cfg(target_arch ="x86_64")]unsafe{ resizer.set_cpu_extensions(fr::CpuExtensions::Sse4_1);}}
About
Rust library for fast image resizing with using of SIMD instructions.