Skip to content

Commit

Permalink
Plane f32 added convolution
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Jul 23, 2024
1 parent ccbbc35 commit 0de0674
Show file tree
Hide file tree
Showing 8 changed files with 454 additions and 13 deletions.
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"
version = "0.1.24"
version = "0.1.25"
edition = "2021"
description = "High performance image scaling"
readme = "README.md"
Expand Down
56 changes: 48 additions & 8 deletions app/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
mod merge;
mod split;

use std::time::Instant;

use fast_image_resize::images::Image;
Expand All @@ -9,6 +12,8 @@ use half::f16;
use image::io::Reader as ImageReader;
use image::{EncodableLayout, GenericImageView};

use crate::merge::merge_channels_3;
use crate::split::split_channels_3;
use pic_scale::{
ImageSize, ImageStore, JzazbzScaler, OklabScaler, ResamplingFunction, Scaler, Scaling,
ThreadingPolicy, TransferFunction,
Expand Down Expand Up @@ -39,26 +44,61 @@ fn main() {

let start_time = Instant::now();

let store = ImageStore::<f32, 3>::from_slice(
&mut f16_bytes,
let mut rec1 = vec![0f32; dimensions.0 as usize * dimensions.1 as usize];
let mut rec2 = vec![0f32; dimensions.0 as usize * dimensions.1 as usize];
let mut rec3 = vec![0f32; dimensions.0 as usize * dimensions.1 as usize];

split_channels_3(
&f16_bytes,
dimensions.0 as usize,
dimensions.1 as usize,
&mut rec1,
&mut rec2,
&mut rec3,
);

let store =
ImageStore::<f32, 1>::from_slice(&mut rec1, dimensions.0 as usize, dimensions.1 as usize);

let resized = scaler.resize_plane_f32(
ImageSize::new(dimensions.0 as usize / 2, dimensions.1 as usize / 2),
store,
);
rec1 = Vec::from(resized.as_bytes());

let resized = scaler.resize_rgb_f32(
let store =
ImageStore::<f32, 1>::from_slice(&mut rec2, dimensions.0 as usize, dimensions.1 as usize);

let resized = scaler.resize_plane_f32(
ImageSize::new(dimensions.0 as usize / 2, dimensions.1 as usize / 2),
store,
);
rec2 = Vec::from(resized.as_bytes());

let store =
ImageStore::<f32, 1>::from_slice(&mut rec3, dimensions.0 as usize, dimensions.1 as usize);

let resized = scaler.resize_plane_f32(
ImageSize::new(dimensions.0 as usize / 2, dimensions.1 as usize / 2),
store,
);
rec3 = Vec::from(resized.as_bytes());

let mut resized_data: Vec<f32> = vec![0f32; resized.width * resized.height * 3];
merge_channels_3(
&mut resized_data,
resized.width,
resized.height,
&rec1,
&rec2,
&rec3,
);

let elapsed_time = start_time.elapsed();
// Print the elapsed time in milliseconds
println!("Scaler: {:.2?}", elapsed_time);

let dst: Vec<u8> = resized
.as_bytes()
.iter()
.map(|&x| (x * 255f32) as u8)
.collect();
let dst: Vec<u8> = resized_data.iter().map(|&x| (x * 255f32) as u8).collect();
// let dst = resized.as_bytes();

if resized.channels == 4 {
Expand Down
25 changes: 25 additions & 0 deletions app/src/merge.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pub(crate) fn merge_channels_3<T: Copy>(
image: &mut [T],
width: usize,
height: usize,
first: &[T],
second: &[T],
third: &[T],
) {
let mut shift = 0usize;
let mut shift_plane = 0usize;
for _ in 0..height {
let shifted_image = &mut image[shift..];
let shifted_first_plane = &first[shift_plane..];
let shifted_second_plane = &second[shift_plane..];
let shifted_third_plane = &third[shift_plane..];
for x in 0..width {
let px = x * 3;
shifted_image[px] = shifted_first_plane[x];
shifted_image[px + 1] = shifted_second_plane[x];
shifted_image[px + 2] = shifted_third_plane[x];
}
shift += width * 3;
shift_plane += width;
}
}
25 changes: 25 additions & 0 deletions app/src/split.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pub(crate) fn split_channels_3<T: Copy>(
image: &[T],
width: usize,
height: usize,
first: &mut [T],
second: &mut [T],
third: &mut [T],
) {
let mut shift = 0usize;
let mut shift_plane = 0usize;
for _ in 0..height {
let shifted_image = &image[shift..];
let shifted_first_plane = &mut first[shift_plane..];
let shifted_second_plane = &mut second[shift_plane..];
let shifted_third_plane = &mut third[shift_plane..];
for x in 0..width {
let px = x * 3;
shifted_first_plane[x] = shifted_image[px];
shifted_second_plane[x] = shifted_image[px + 1];
shifted_third_plane[x] = shifted_image[px + 2];
}
shift += width * 3;
shift_plane += width;
}
}
3 changes: 3 additions & 0 deletions src/neon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ mod convolve_f16;
mod convolve_f32;
#[cfg(all(feature = "half"))]
mod f16_utils;
mod plane_f32;
#[cfg(all(feature = "half"))]
mod rgb_f16;
mod rgb_f32;
Expand All @@ -51,6 +52,8 @@ pub use alpha::neon_premultiply_alpha_rgba;
pub use alpha::neon_unpremultiply_alpha_rgba;
#[cfg(all(feature = "half"))]
pub use f16_utils::*;
pub use plane_f32::convolve_horizontal_plane_neon_row_one;
pub use plane_f32::convolve_horizontal_plane_neon_rows_4;
#[cfg(all(feature = "half"))]
pub use rgb_f16::{
convolve_horizontal_rgb_neon_row_one_f16, convolve_horizontal_rgb_neon_rows_4_f16,
Expand Down
Loading

0 comments on commit 0de0674

Please sign in to comment.