Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loop unrolling and Area method #2

Merged
merged 9 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ workspace = { members = ["app"] }

[package]
name = "pic-scale-safe"
version = "0.1.2"
version = "0.1.3"
edition = "2021"
description = "Fast and safe performance image scaling"
readme = "README.md"
Expand Down
83 changes: 48 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ let img = ImageReader::open("./assets/nasa-4928x3279.png")
let dimensions = img.dimensions();
let transient = img.to_rgb8();

let start = Instant::now();

let src_size = ImageSize::new(dimensions.0 as usize, dimensions.1 as usize);
let dst_size = ImageSize::new(dimensions.0 as usize / 4, dimensions.1 as usize / 4);

Expand All @@ -30,28 +28,32 @@ cargo bench --bench resize_rgba --manifest-path ./app/Cargo.toml
| | Lanczos3 | Bilinear |
|---------------------------|:--------:|:--------:|
| image(aarch64) | 121.19 | 48.89 |
| pic-scale(aarch64) | 26.90 | 15.13 |
| fir(aarch64) | 25.93 | 11.30 |
| pic-scale(aarch64) | 11.89 | 8.92 |
| fir(aarch64) | 25.89 | 11.30 |
| image(x86) | 192.52 | 88.63 |
| pic-scale(x86) | 49.79 | 35.98 |
| pic-scale(x86-cpu-native) | 27.21 | 20.48 |
| pic-scale(x86) | 25.50 | 18.37 |
| fir(x86) | 42.89 | 24.13 |
| fir(x86-cpu-native) | 41.17 | 23.62 |
| image(x86-cpu-native) | 205.64 | 89.02 |
| pic-scale(x86-cpu-native) | 14.39 | 11.31 |
| fir(x86-cpu-native) | 41.21 | 22.77 |

Example comparison time for downscale RGB 4928x3279 `8 bit` image in 4 times.

```bash
cargo bench --bench resize_rgb --manifest-path ./app/Cargo.toml
```

| | Lanczos3 | Bilinear |
|--------------------|:--------:|:--------:|
| image(aarch64) | 123.85 | 51.30 |
| pic-scale(aarch64) | 31.73 | 18.20 |
| fir(aarch64) | 24.04 | 11.37 |
| image(x86) | 201.52 | 90.82 |
| pic-scale(x86) | 34.54 | 25.05 |
| fir(x86) | 41.97 | 25.21 |
| | Lanczos3 | Bilinear |
|---------------------------|:--------:|:--------:|
| image(aarch64) | 123.85 | 51.30 |
| pic-scale(aarch64) | 17.23 | 12.32 |
| fir(aarch64) | 23.61 | 10.53 |
| image(x86) | 201.52 | 90.82 |
| pic-scale(x86) | 27.17 | 21.08 |
| fir(x86) | 41.97 | 24.39 |
| image(x86-cpu-native) | 184.57 | 84.69 |
| pic-scale(x86-cpu-native) | 20.96 | 15.16 |
| fir(x86-cpu-native) | 41.49 | 20.38 |

Example comparison time for downscale RGBA 4928x3279 `16 bit` image in 4 times.

Expand All @@ -61,40 +63,51 @@ cargo bench --bench resize_rgba_u16 --manifest-path ./app/Cargo.toml

| | Lanczos3 | Bilinear |
|---------------------------|:--------:|:--------:|
| image(aarch64) | 123.27 | 52.91 |
| pic-scale(aarch64) | 28.041 | 18.89 |
| fir(aarch64) | 149.87 | 50.08 |
| image(x86) | 196.28 | 194.75 |
| pic-scale(x86) | 59.89 | 57.99 |
| pic-scale(x86-cpu-native) | 44.07 | 57.99 |
| fir(x86) | 52.73 | 28.35 |
| image(aarch64) | 262.32 | 76.91 |
| pic-scale(aarch64) | 15.49 | 11.38 |
| fir(aarch64) | 141.78 | 50.08 |
| image(x86) | 196.28 | 107.78 |
| pic-scale(x86) | 57.48 | 50.85 |
| fir(x86) | 139.56 | 58.48 |
| image(x86-cpu-native) | 192.85 | 102.05 |
| pic-scale(x86-cpu-native) | 39.60 | 46.44 |
| fir(x86-cpu-native) | 101.48 | 52.58 |

Example comparison time for downscale RGB 4928x3279 `16 bit` image in 4 times.

```bash
cargo bench --bench resize_rgb_u16 --manifest-path ./app/Cargo.toml
```

| | Lanczos3 | Bilinear |
|--------------------|:--------:|:--------:|
| image(aarch64) | 130.45 | 61.06 |
| pic-scale(aarch64) | 36.10 | 23.80 |
| fir(aarch64) | 122.01 | 43.36 |
| | Lanczos3 | Bilinear |
|---------------------------|:--------:|:--------:|
| image(aarch64) | 130.45 | 57.38 |
| pic-scale(aarch64) | 16.17 | 12.11 |
| fir(aarch64) | 110.06 | 42.04 |
| image(x86) | 204.10 | 148.34 |
| pic-scale(x86) | 43.21 | 145.73 |
| fir(x86) | 210.28 | 51.29 |
| image(x86-cpu-native) | 190.21 | 98.42 |
| pic-scale(x86-cpu-native) | 33.48 | 28.50 |
| fir(x86-cpu-native) | 72.88 | 45.17 |

Example comparison time for downscale RGBA 4928x3279 `f32` image in 4 times.

```bash
cargo bench --bench resize_rgba_f32 --manifest-path ./app/Cargo.toml
```

| | Lanczos3 | Bilinear |
|--------------------|:--------:|:--------:|
| image(aarch64) | 100.16 | 51.21 |
| pic-scale(aarch64) | 43.04 | 19.16 |
| fir(aarch64) | 114.35 | 37.75 |
| image(x86) | 164.04 | 98.90 |
| pic-scale(x86) | 57.39 | 43.84 |
| fir(x86) | 60.30 | 29.92 |
| | Lanczos3 | Bilinear |
|---------------------------|:--------:|:--------:|
| image(aarch64) | 100.16 | 50.09 |
| pic-scale(aarch64) | 14.07 | 11.18 |
| fir(aarch64) | 105.30 | 37.75 |
| image(x86) | 208.25 | 107.84 |
| pic-scale(x86) | 33.55 | 28.97 |
| fir(x86) | 92.38 | 74.12 |
| image(x86-cpu-native) | 162.83 | 108.54 |
| pic-scale(x86-cpu-native) | 33.13 | 28.54 |
| fir(x86-cpu-native) | 56.65 | 59.96 |

This project is licensed under either of

Expand Down
2 changes: 1 addition & 1 deletion app/benches/resize_rgb/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
})
});

c.bench_function("Image RGB: Bilinear", |b| {
c.bench_function("Image RGB: Lanczos3", |b| {
b.iter(|| {
_ = dyn_image.clone().resize_exact(
dimensions.0 / 4,
Expand Down
41 changes: 27 additions & 14 deletions app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ mod image_wrapper;
use fast_image_resize::images::Image;
use fast_image_resize::{CpuExtensions, FilterType, PixelType, ResizeAlg, ResizeOptions, Resizer};
use image::{
DynamicImage, EncodableLayout, GenericImageView, ImageBuffer, ImageFormat, ImageReader, Rgb,
RgbImage,
imageops, DynamicImage, EncodableLayout, GenericImageView, ImageBuffer, ImageFormat,
ImageReader, Rgb, RgbImage,
};
use pic_scale_safe::{
resize_floating_point, resize_rgb16, resize_rgb8, resize_rgba16, resize_rgba8, ImageSize,
premultiply_rgba8, resize_fixed_point, resize_floating_point, resize_rgb16, resize_rgb8,
resize_rgb_f32, resize_rgba16, resize_rgba8, unpremultiply_rgba8, ImageSize,
ResamplingFunction,
};
use std::ops::{BitXor, Shr};
use std::time::Instant;

fn main() {
Expand All @@ -46,44 +48,55 @@ fn main() {
.decode()
.unwrap();
let dimensions = img.dimensions();
let transient = img.to_rgb8();
let transient = img.to_rgba8();

let mut working_store = transient.to_vec();

let start = Instant::now();

let src_size = ImageSize::new(dimensions.0 as usize, dimensions.1 as usize);
let dst_size = ImageSize::new(dimensions.0 as usize * 4, dimensions.1 as usize * 4);
let dst_size = ImageSize::new(dimensions.0 as usize / 2, dimensions.1 as usize / 2);

let mut resized = resize_rgb8(
let start_mul = Instant::now();

premultiply_rgba8(&mut working_store);

println!("Alpha mul time {:?}", start_mul.elapsed());

let mut resized = resize_rgba8(
&working_store,
src_size,
dst_size,
ResamplingFunction::MitchellNetravalli,
ResamplingFunction::Lanczos3,
)
.unwrap();

// unpremultiply_rgba8(&mut resized);

println!("Working time {:?}", start.elapsed());

// let rgba_image = DynamicImage::ImageRgb16(ImageBuffer::<Rgb<u16>, Vec<u16>>::from_vec(dimensions.0 * 4, dimensions.1 / 4, resized).unwrap());
// rgba_image.save_with_format("converted.png", ImageFormat::Png).unwrap();

// let shifted = resized.iter().map(|&x| (x >> 8) as u8).collect::<Vec<_>>();
// let shifted = resized
// .iter()
// .map(|&x| (x * 255.) as u8)
// .collect::<Vec<_>>();

image::save_buffer(
"converted.jpg",
"converted.png",
&resized,
dst_size.width as u32,
dst_size.height as u32,
image::ColorType::Rgb8,
image::ColorType::Rgba8,
)
.unwrap();

// let mut src_bytes = transient.as_bytes().to_vec();
// let pixel_type: PixelType = PixelType::U8x3;
// let src_image =
// Image::from_slice_u8(dimensions.0, dimensions.1, &mut src_bytes, pixel_type).unwrap();
// let mut dst_image = Image::new(dimensions.0 * 4, dimensions.1 * 4, pixel_type);
// let mut dst_image = Image::new(dimensions.0 / 8, dimensions.1 / 8, pixel_type);
//
// let mut resizer = Resizer::new();
// unsafe {
Expand All @@ -97,7 +110,7 @@ fn main() {
// &src_image,
// &mut dst_image,
// &ResizeOptions::new()
// .resize_alg(ResizeAlg::Convolution(FilterType::Mitchell))
// .resize_alg(ResizeAlg::Convolution(FilterType::Bilinear))
// .use_alpha(false),
// )
// .unwrap();
Expand All @@ -109,13 +122,13 @@ fn main() {
// // let rgba_image = DynamicImage::ImageRgb8(RgbImage::from_raw(dst_image.width() as u32, dst_image.height() as u32, dst_image.buffer().to_vec()).unwrap());
// // rgba_image.save_with_format("fast_image.png", ImageFormat::Png).unwrap();
// image::save_buffer(
// "fast_image.jpg",
// "fast_image.png",
// dst_image.buffer(),
// dst_image.width(),
// dst_image.height(),
// image::ColorType::Rgb8,
// )
// .unwrap();
// .unwrap();
}

fn u8_to_u16(u8_buffer: &[u8]) -> &[u16] {
Expand Down
4 changes: 2 additions & 2 deletions command.ps1
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# $env:RUSTFLAGS = "-C target-cpu=native"
cargo bench --bench resize_rgba_u16 --manifest-path ./app/Cargo.toml
$env:RUSTFLAGS = "-C target-cpu=native"
cargo bench --bench resize_rgba_f32 --manifest-path ./app/Cargo.toml
Loading