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

WIP #13

Merged
merged 9 commits into from
Dec 28, 2024
Merged

WIP #13

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
1 change: 1 addition & 0 deletions .github/workflows/build_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ jobs:
- run: cargo fuzz run resize_rgba -- -max_total_time=30
- run: cargo fuzz run resize_rgb -- -max_total_time=30
- run: cargo fuzz run resize_plane -- -max_total_time=30
- run: cargo fuzz run colorspaces -- -max_total_time=10

fuzz_rgba_high_bit:
name: Fuzzing High bit-depth
Expand Down
6 changes: 3 additions & 3 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ workspace = { members = ["app", "wasm", "fuzz"] }

[package]
name = "pic-scale"
version = "0.3.6"
version = "0.4.0"
edition = "2021"
description = "High performance image scaling"
readme = "README.md"
Expand All @@ -14,10 +14,10 @@ categories = ["multimedia::images", "multimedia::video"]
homepage = "https://github.com/awxkee/pic-scale"
repository = "https://github.com/awxkee/pic-scale"
exclude = ["*.jpg", "/assets", "*.png", "*.sh", "/assets/*"]
rust-version = "1.73.0"
rust-version = "1.82.0"

[dependencies]
colorutils-rs = {version = "0.7.0", optional = true}
colorutils-rs = {version = "0.7.4", optional = true}
half = { version = "2.4.1", optional = true, features = ["alloc", "std", "num-traits"] }
num-traits = { version = "0.2.19", features = ["std"] }
rayon = "1.10.0"
Expand Down
16 changes: 7 additions & 9 deletions app/benches/resize_rgb/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use fast_image_resize::FilterType::Lanczos3;
use fast_image_resize::{CpuExtensions, PixelType, ResizeAlg, ResizeOptions, Resizer};
use image::{EncodableLayout, GenericImageView, ImageReader};
use pic_scale::{
ImageSize, ImageStore, ResamplingFunction, Scaler, Scaling, ScalingF32, ThreadingPolicy,
ImageStore, ImageStoreMut, ResamplingFunction, Scaler, Scaling, ScalingF32, ThreadingPolicy,
};

pub fn criterion_benchmark(c: &mut Criterion) {
Expand All @@ -27,10 +27,9 @@ pub fn criterion_benchmark(c: &mut Criterion) {
dimensions.1 as usize,
)
.unwrap();
_ = scaler.resize_rgb(
ImageSize::new(dimensions.0 as usize / 4, dimensions.1 as usize / 4),
store,
);
let mut target =
ImageStoreMut::alloc(dimensions.0 as usize / 4, dimensions.1 as usize / 4);
scaler.resize_rgb(&store, &mut target).unwrap();
})
});

Expand All @@ -47,10 +46,9 @@ pub fn criterion_benchmark(c: &mut Criterion) {
dimensions.1 as usize,
)
.unwrap();
_ = scaler.resize_rgb_f32(
ImageSize::new(dimensions.0 as usize / 4, dimensions.1 as usize / 4),
store,
);
let mut target =
ImageStoreMut::alloc(dimensions.0 as usize / 4, dimensions.1 as usize / 4);
scaler.resize_rgb_f32(&store, &mut target).unwrap();
})
});

Expand Down
32 changes: 13 additions & 19 deletions app/benches/resize_rgba/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use fast_image_resize::FilterType::Lanczos3;
use fast_image_resize::{CpuExtensions, PixelType, ResizeAlg, ResizeOptions, Resizer};
use image::{GenericImageView, ImageReader};
use pic_scale::{
ImageSize, ImageStore, ResamplingFunction, Scaler, Scaling, ScalingF32, ThreadingPolicy,
ImageStore, ImageStoreMut, ResamplingFunction, Scaler, Scaling, ScalingF32, ThreadingPolicy,
};

pub fn criterion_benchmark(c: &mut Criterion) {
Expand All @@ -25,11 +25,9 @@ pub fn criterion_benchmark(c: &mut Criterion) {
dimensions.1 as usize,
)
.unwrap();
_ = scaler.resize_rgba(
ImageSize::new(dimensions.0 as usize / 2, dimensions.1 as usize / 2),
store,
true,
);
let mut target =
ImageStoreMut::alloc(dimensions.0 as usize / 4, dimensions.1 as usize / 4);
_ = scaler.resize_rgba(&store, &mut target, true);
})
});

Expand All @@ -46,17 +44,15 @@ pub fn criterion_benchmark(c: &mut Criterion) {
dimensions.1 as usize,
)
.unwrap();
_ = scaler.resize_rgba_f32(
ImageSize::new(dimensions.0 as usize / 2, dimensions.1 as usize / 2),
store,
false,
);
let mut target =
ImageStoreMut::alloc(dimensions.0 as usize / 4, dimensions.1 as usize / 4);
_ = scaler.resize_rgba_f32(&store, &mut target, false);
})
});

c.bench_function("Fast image resize RGBA with alpha: Lanczos 3", |b| {
let mut vc = Vec::from(img.as_bytes());
b.iter(|| {
let mut vc = Vec::from(img.as_bytes());
let pixel_type: PixelType = PixelType::U8x4;
let src_image =
Image::from_slice_u8(dimensions.0, dimensions.1, &mut vc, pixel_type).unwrap();
Expand Down Expand Up @@ -84,27 +80,25 @@ pub fn criterion_benchmark(c: &mut Criterion) {
});

c.bench_function("Pic scale RGBA without alpha: Lanczos 3", |b| {
let mut copied: Vec<u8> = Vec::from(src_bytes);
b.iter(|| {
let mut scaler = Scaler::new(ResamplingFunction::Lanczos3);
scaler.set_threading_policy(ThreadingPolicy::Single);
let mut copied: Vec<u8> = Vec::from(src_bytes);
let store = ImageStore::<u8, 4>::from_slice(
&mut copied,
dimensions.0 as usize,
dimensions.1 as usize,
)
.unwrap();
_ = scaler.resize_rgba(
ImageSize::new(dimensions.0 as usize / 2, dimensions.1 as usize / 2),
store,
false,
);
let mut target =
ImageStoreMut::alloc(dimensions.0 as usize / 4, dimensions.1 as usize / 4);
_ = scaler.resize_rgba(&store, &mut target, false);
})
});

c.bench_function("Fast image resize RGBA without alpha: Lanczos 3", |b| {
let mut vc = Vec::from(img.as_bytes());
b.iter(|| {
let mut vc = Vec::from(img.as_bytes());
let pixel_type: PixelType = PixelType::U8x4;
let src_image =
Image::from_slice_u8(dimensions.0, dimensions.1, &mut vc, pixel_type).unwrap();
Expand Down
33 changes: 15 additions & 18 deletions app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use fast_image_resize::{
};
use image::{EncodableLayout, GenericImageView, ImageReader};
use pic_scale::{
Ar30ByteOrder, ImageSize, ImageStore, LinearApproxScaler, LinearScaler, ResamplingFunction,
Scaler, Scaling, ScalingU16, ThreadingPolicy,
Ar30ByteOrder, ImageSize, ImageStore, ImageStoreMut, JzazbzScaler, LChScaler, LabScaler,
LinearApproxScaler, LinearScaler, LuvScaler, OklabScaler, ResamplingFunction, Scaler, Scaling,
ScalingU16, SigmoidalScaler, ThreadingPolicy, TransferFunction, XYZScaler,
};

fn resize_plane(
Expand All @@ -37,10 +38,9 @@ fn resize_plane(
let mut src_data = vec![15u8; src_width * src_height * 1];

let store = ImageStore::<u8, 1>::from_slice(&mut src_data, src_width, src_height).unwrap();
let mut dst_store = ImageStoreMut::<u8, 1>::alloc(src_width / 2, src_height / 2);
let scaler = Scaler::new(sampler);
_ = scaler
.resize_plane(ImageSize::new(dst_width, dst_height), store)
.unwrap();
_ = scaler.resize_plane(&store, &mut dst_store).unwrap();
}

fn main() {
Expand Down Expand Up @@ -77,13 +77,10 @@ fn main() {
// )
// .unwrap();

let resized = scaler
.resize_rgba(
ImageSize::new(dimensions.0 as usize / 2, dimensions.1 as usize / 2),
store,
false,
)
.unwrap();
let mut dst_store =
ImageStoreMut::<u8, 4>::alloc(dimensions.0 as usize / 2, dimensions.1 as usize / 2);

scaler.resize_rgba(&store, &mut dst_store, false).unwrap();

let elapsed_time = start_time.elapsed();
// Print the elapsed time in milliseconds
Expand Down Expand Up @@ -162,7 +159,7 @@ fn main() {

// let dst: Vec<u8> = resized.as_bytes().iter().map(|&x| (x >> 2) as u8).collect();
//
let dst = resized.as_bytes();
let dst = dst_store.as_bytes();
// let dst = resized;
// image::save_buffer(
// "converted.png",
Expand All @@ -173,21 +170,21 @@ fn main() {
// )
// .unwrap();

if resized.channels == 4 {
if dst_store.channels == 4 {
image::save_buffer(
"converted.png",
&dst,
resized.width as u32,
resized.height as u32,
dst_store.width as u32,
dst_store.height as u32,
image::ColorType::Rgba8,
)
.unwrap();
} else {
image::save_buffer(
"converted.png",
&dst,
resized.width as u32,
resized.height as u32,
dst_store.width as u32,
dst_store.height as u32,
image::ColorType::Rgb8,
)
.unwrap();
Expand Down
7 changes: 7 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,10 @@ path = "resize_plane_f32/resize_plane_f32.rs"
test = false
doc = false
bench = false

[[bin]]
name = "colorspaces"
path = "colorspaces/colorspaces.rs"
test = false
doc = false
bench = false
95 changes: 95 additions & 0 deletions fuzz/colorspaces/colorspaces.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) Radzivon Bartoshyk. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#![no_main]

use libfuzzer_sys::fuzz_target;
use pic_scale::{
ImageStore, ImageStoreMut, JzazbzScaler, LChScaler, LabScaler, LinearApproxScaler,
LinearScaler, LuvScaler, OklabScaler, ResamplingFunction, Scaling, SigmoidalScaler,
TransferFunction, XYZScaler,
};

fuzz_target!(|data: (u16, u16, u16, u16)| {
resize_plane(
data.0 as usize,
data.1 as usize,
data.2 as usize,
data.3 as usize,
ResamplingFunction::Bilinear,
)
});

fn resize_plane(
src_width: usize,
src_height: usize,
dst_width: usize,
dst_height: usize,
sampler: ResamplingFunction,
) {
if src_width == 0
|| src_width > 2000
|| src_height == 0
|| src_height > 2000
|| dst_width == 0
|| dst_width > 512
|| dst_height == 0
|| dst_height > 512
{
return;
}

let scalers: Vec<Box<dyn Scaling>> = vec![
Box::new(JzazbzScaler::new(sampler, 203f32, TransferFunction::Srgb)),
Box::new(LabScaler::new(sampler)),
Box::new(LChScaler::new(sampler)),
Box::new(LinearScaler::new(sampler)),
Box::new(LinearApproxScaler::new(sampler)),
Box::new(LuvScaler::new(sampler)),
Box::new(OklabScaler::new(sampler, TransferFunction::Srgb)),
Box::new(SigmoidalScaler::new(sampler)),
Box::new(XYZScaler::new(sampler)),
];

for scaler in scalers {
let mut src_data_rgb = vec![15u8; src_width * src_height * 3];
let store =
ImageStore::<u8, 3>::from_slice(&mut src_data_rgb, src_width, src_height).unwrap();
let mut target_store = ImageStoreMut::alloc(dst_width, dst_height);
scaler.resize_rgb(&store, &mut target_store).unwrap();

let mut src_data_rgba = vec![15u8; src_width * src_height * 4];
let store_rgba =
ImageStore::<u8, 4>::from_slice(&mut src_data_rgba, src_width, src_height).unwrap();
let mut target_store_rgba = ImageStoreMut::alloc(dst_width, dst_height);
scaler
.resize_rgba(&store_rgba, &mut target_store_rgba, false)
.unwrap();
}
}
7 changes: 3 additions & 4 deletions fuzz/resize_plane/resize_plane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#![no_main]

use libfuzzer_sys::fuzz_target;
use pic_scale::{ImageSize, ImageStore, ResamplingFunction, Scaler};
use pic_scale::{ImageStore, ImageStoreMut, ResamplingFunction, Scaler};

fuzz_target!(|data: (u16, u16, u16, u16)| {
resize_plane(
Expand Down Expand Up @@ -64,8 +64,7 @@ fn resize_plane(
let mut src_data = vec![15u8; src_width * src_height];

let store = ImageStore::<u8, 1>::from_slice(&mut src_data, src_width, src_height).unwrap();
let mut target = ImageStoreMut::alloc(dst_width, dst_height);
let scaler = Scaler::new(sampler);
_ = scaler
.resize_plane(ImageSize::new(dst_width, dst_height), store)
.unwrap();
scaler.resize_plane(&store, &mut target).unwrap();
}
12 changes: 4 additions & 8 deletions fuzz/resize_plane_f32/resize_plane_f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#![no_main]

use libfuzzer_sys::fuzz_target;
use pic_scale::{ImageSize, ImageStore, ResamplingFunction, Scaler};
use pic_scale::{ImageStore, ImageStoreMut, ResamplingFunction, Scaler};

fuzz_target!(|data: (u16, u16, u16, u16)| {
resize_plane(
Expand Down Expand Up @@ -64,12 +64,8 @@ fn resize_plane(
let mut src_data = vec![0f32; src_width * src_height];

let store = ImageStore::<f32, 1>::from_slice(&mut src_data, src_width, src_height).unwrap();
let mut target = ImageStoreMut::alloc(dst_width, dst_height);

let scaler = Scaler::new(sampler);
_ = scaler
.resize_plane_f32(ImageSize::new(dst_width, dst_height), store)
.unwrap();
let store = ImageStore::<f32, 1>::from_slice(&mut src_data, src_width, src_height).unwrap();
_ = scaler
.resize_plane_f32(ImageSize::new(dst_width, dst_height), store)
.unwrap();
scaler.resize_plane_f32(&store, &mut target).unwrap();
}
Loading
Loading