Skip to content

Commit 547edee

Browse files
authored
Merge pull request #2 from awxkee/dev
Loop unrolling and `Area` method
2 parents 9fe2269 + 2ba1f58 commit 547edee

18 files changed

+1346
-367
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ workspace = { members = ["app"] }
22

33
[package]
44
name = "pic-scale-safe"
5-
version = "0.1.2"
5+
version = "0.1.3"
66
edition = "2021"
77
description = "Fast and safe performance image scaling"
88
readme = "README.md"

README.md

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ let img = ImageReader::open("./assets/nasa-4928x3279.png")
1212
let dimensions = img.dimensions();
1313
let transient = img.to_rgb8();
1414

15-
let start = Instant::now();
16-
1715
let src_size = ImageSize::new(dimensions.0 as usize, dimensions.1 as usize);
1816
let dst_size = ImageSize::new(dimensions.0 as usize / 4, dimensions.1 as usize / 4);
1917

@@ -30,28 +28,32 @@ cargo bench --bench resize_rgba --manifest-path ./app/Cargo.toml
3028
| | Lanczos3 | Bilinear |
3129
|---------------------------|:--------:|:--------:|
3230
| image(aarch64) | 121.19 | 48.89 |
33-
| pic-scale(aarch64) | 26.90 | 15.13 |
34-
| fir(aarch64) | 25.93 | 11.30 |
31+
| pic-scale(aarch64) | 11.89 | 8.92 |
32+
| fir(aarch64) | 25.89 | 11.30 |
3533
| image(x86) | 192.52 | 88.63 |
36-
| pic-scale(x86) | 49.79 | 35.98 |
37-
| pic-scale(x86-cpu-native) | 27.21 | 20.48 |
34+
| pic-scale(x86) | 25.50 | 18.37 |
3835
| fir(x86) | 42.89 | 24.13 |
39-
| fir(x86-cpu-native) | 41.17 | 23.62 |
36+
| image(x86-cpu-native) | 205.64 | 89.02 |
37+
| pic-scale(x86-cpu-native) | 14.39 | 11.31 |
38+
| fir(x86-cpu-native) | 41.21 | 22.77 |
4039

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

4342
```bash
4443
cargo bench --bench resize_rgb --manifest-path ./app/Cargo.toml
4544
```
4645

47-
| | Lanczos3 | Bilinear |
48-
|--------------------|:--------:|:--------:|
49-
| image(aarch64) | 123.85 | 51.30 |
50-
| pic-scale(aarch64) | 31.73 | 18.20 |
51-
| fir(aarch64) | 24.04 | 11.37 |
52-
| image(x86) | 201.52 | 90.82 |
53-
| pic-scale(x86) | 34.54 | 25.05 |
54-
| fir(x86) | 41.97 | 25.21 |
46+
| | Lanczos3 | Bilinear |
47+
|---------------------------|:--------:|:--------:|
48+
| image(aarch64) | 123.85 | 51.30 |
49+
| pic-scale(aarch64) | 17.23 | 12.32 |
50+
| fir(aarch64) | 23.61 | 10.53 |
51+
| image(x86) | 201.52 | 90.82 |
52+
| pic-scale(x86) | 27.17 | 21.08 |
53+
| fir(x86) | 41.97 | 24.39 |
54+
| image(x86-cpu-native) | 184.57 | 84.69 |
55+
| pic-scale(x86-cpu-native) | 20.96 | 15.16 |
56+
| fir(x86-cpu-native) | 41.49 | 20.38 |
5557

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

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

6264
| | Lanczos3 | Bilinear |
6365
|---------------------------|:--------:|:--------:|
64-
| image(aarch64) | 123.27 | 52.91 |
65-
| pic-scale(aarch64) | 28.041 | 18.89 |
66-
| fir(aarch64) | 149.87 | 50.08 |
67-
| image(x86) | 196.28 | 194.75 |
68-
| pic-scale(x86) | 59.89 | 57.99 |
69-
| pic-scale(x86-cpu-native) | 44.07 | 57.99 |
70-
| fir(x86) | 52.73 | 28.35 |
66+
| image(aarch64) | 262.32 | 76.91 |
67+
| pic-scale(aarch64) | 15.49 | 11.38 |
68+
| fir(aarch64) | 141.78 | 50.08 |
69+
| image(x86) | 196.28 | 107.78 |
70+
| pic-scale(x86) | 57.48 | 50.85 |
71+
| fir(x86) | 139.56 | 58.48 |
72+
| image(x86-cpu-native) | 192.85 | 102.05 |
73+
| pic-scale(x86-cpu-native) | 39.60 | 46.44 |
74+
| fir(x86-cpu-native) | 101.48 | 52.58 |
7175

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

7478
```bash
7579
cargo bench --bench resize_rgb_u16 --manifest-path ./app/Cargo.toml
7680
```
7781

78-
| | Lanczos3 | Bilinear |
79-
|--------------------|:--------:|:--------:|
80-
| image(aarch64) | 130.45 | 61.06 |
81-
| pic-scale(aarch64) | 36.10 | 23.80 |
82-
| fir(aarch64) | 122.01 | 43.36 |
82+
| | Lanczos3 | Bilinear |
83+
|---------------------------|:--------:|:--------:|
84+
| image(aarch64) | 130.45 | 57.38 |
85+
| pic-scale(aarch64) | 16.17 | 12.11 |
86+
| fir(aarch64) | 110.06 | 42.04 |
87+
| image(x86) | 204.10 | 148.34 |
88+
| pic-scale(x86) | 43.21 | 145.73 |
89+
| fir(x86) | 210.28 | 51.29 |
90+
| image(x86-cpu-native) | 190.21 | 98.42 |
91+
| pic-scale(x86-cpu-native) | 33.48 | 28.50 |
92+
| fir(x86-cpu-native) | 72.88 | 45.17 |
8393

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

8696
```bash
8797
cargo bench --bench resize_rgba_f32 --manifest-path ./app/Cargo.toml
8898
```
8999

90-
| | Lanczos3 | Bilinear |
91-
|--------------------|:--------:|:--------:|
92-
| image(aarch64) | 100.16 | 51.21 |
93-
| pic-scale(aarch64) | 43.04 | 19.16 |
94-
| fir(aarch64) | 114.35 | 37.75 |
95-
| image(x86) | 164.04 | 98.90 |
96-
| pic-scale(x86) | 57.39 | 43.84 |
97-
| fir(x86) | 60.30 | 29.92 |
100+
| | Lanczos3 | Bilinear |
101+
|---------------------------|:--------:|:--------:|
102+
| image(aarch64) | 100.16 | 50.09 |
103+
| pic-scale(aarch64) | 14.07 | 11.18 |
104+
| fir(aarch64) | 105.30 | 37.75 |
105+
| image(x86) | 208.25 | 107.84 |
106+
| pic-scale(x86) | 33.55 | 28.97 |
107+
| fir(x86) | 92.38 | 74.12 |
108+
| image(x86-cpu-native) | 162.83 | 108.54 |
109+
| pic-scale(x86-cpu-native) | 33.13 | 28.54 |
110+
| fir(x86-cpu-native) | 56.65 | 59.96 |
98111

99112
This project is licensed under either of
100113

app/benches/resize_rgb/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
5757
})
5858
});
5959

60-
c.bench_function("Image RGB: Bilinear", |b| {
60+
c.bench_function("Image RGB: Lanczos3", |b| {
6161
b.iter(|| {
6262
_ = dyn_image.clone().resize_exact(
6363
dimensions.0 / 4,

app/src/main.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ mod image_wrapper;
3131
use fast_image_resize::images::Image;
3232
use fast_image_resize::{CpuExtensions, FilterType, PixelType, ResizeAlg, ResizeOptions, Resizer};
3333
use image::{
34-
DynamicImage, EncodableLayout, GenericImageView, ImageBuffer, ImageFormat, ImageReader, Rgb,
35-
RgbImage,
34+
imageops, DynamicImage, EncodableLayout, GenericImageView, ImageBuffer, ImageFormat,
35+
ImageReader, Rgb, RgbImage,
3636
};
3737
use pic_scale_safe::{
38-
resize_floating_point, resize_rgb16, resize_rgb8, resize_rgba16, resize_rgba8, ImageSize,
38+
premultiply_rgba8, resize_fixed_point, resize_floating_point, resize_rgb16, resize_rgb8,
39+
resize_rgb_f32, resize_rgba16, resize_rgba8, unpremultiply_rgba8, ImageSize,
3940
ResamplingFunction,
4041
};
42+
use std::ops::{BitXor, Shr};
4143
use std::time::Instant;
4244

4345
fn main() {
@@ -46,44 +48,55 @@ fn main() {
4648
.decode()
4749
.unwrap();
4850
let dimensions = img.dimensions();
49-
let transient = img.to_rgb8();
51+
let transient = img.to_rgba8();
5052

5153
let mut working_store = transient.to_vec();
5254

5355
let start = Instant::now();
5456

5557
let src_size = ImageSize::new(dimensions.0 as usize, dimensions.1 as usize);
56-
let dst_size = ImageSize::new(dimensions.0 as usize * 4, dimensions.1 as usize * 4);
58+
let dst_size = ImageSize::new(dimensions.0 as usize / 2, dimensions.1 as usize / 2);
5759

58-
let mut resized = resize_rgb8(
60+
let start_mul = Instant::now();
61+
62+
premultiply_rgba8(&mut working_store);
63+
64+
println!("Alpha mul time {:?}", start_mul.elapsed());
65+
66+
let mut resized = resize_rgba8(
5967
&working_store,
6068
src_size,
6169
dst_size,
62-
ResamplingFunction::MitchellNetravalli,
70+
ResamplingFunction::Lanczos3,
6371
)
6472
.unwrap();
6573

74+
// unpremultiply_rgba8(&mut resized);
75+
6676
println!("Working time {:?}", start.elapsed());
6777

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

71-
// let shifted = resized.iter().map(|&x| (x >> 8) as u8).collect::<Vec<_>>();
81+
// let shifted = resized
82+
// .iter()
83+
// .map(|&x| (x * 255.) as u8)
84+
// .collect::<Vec<_>>();
7285

7386
image::save_buffer(
74-
"converted.jpg",
87+
"converted.png",
7588
&resized,
7689
dst_size.width as u32,
7790
dst_size.height as u32,
78-
image::ColorType::Rgb8,
91+
image::ColorType::Rgba8,
7992
)
8093
.unwrap();
8194

8295
// let mut src_bytes = transient.as_bytes().to_vec();
8396
// let pixel_type: PixelType = PixelType::U8x3;
8497
// let src_image =
8598
// Image::from_slice_u8(dimensions.0, dimensions.1, &mut src_bytes, pixel_type).unwrap();
86-
// let mut dst_image = Image::new(dimensions.0 * 4, dimensions.1 * 4, pixel_type);
99+
// let mut dst_image = Image::new(dimensions.0 / 8, dimensions.1 / 8, pixel_type);
87100
//
88101
// let mut resizer = Resizer::new();
89102
// unsafe {
@@ -97,7 +110,7 @@ fn main() {
97110
// &src_image,
98111
// &mut dst_image,
99112
// &ResizeOptions::new()
100-
// .resize_alg(ResizeAlg::Convolution(FilterType::Mitchell))
113+
// .resize_alg(ResizeAlg::Convolution(FilterType::Bilinear))
101114
// .use_alpha(false),
102115
// )
103116
// .unwrap();
@@ -109,13 +122,13 @@ fn main() {
109122
// // let rgba_image = DynamicImage::ImageRgb8(RgbImage::from_raw(dst_image.width() as u32, dst_image.height() as u32, dst_image.buffer().to_vec()).unwrap());
110123
// // rgba_image.save_with_format("fast_image.png", ImageFormat::Png).unwrap();
111124
// image::save_buffer(
112-
// "fast_image.jpg",
125+
// "fast_image.png",
113126
// dst_image.buffer(),
114127
// dst_image.width(),
115128
// dst_image.height(),
116129
// image::ColorType::Rgb8,
117130
// )
118-
// .unwrap();
131+
// .unwrap();
119132
}
120133

121134
fn u8_to_u16(u8_buffer: &[u8]) -> &[u16] {

command.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# $env:RUSTFLAGS = "-C target-cpu=native"
2-
cargo bench --bench resize_rgba_u16 --manifest-path ./app/Cargo.toml
1+
$env:RUSTFLAGS = "-C target-cpu=native"
2+
cargo bench --bench resize_rgba_f32 --manifest-path ./app/Cargo.toml

0 commit comments

Comments
 (0)