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

add support for block pixel enum in ImageBuffer #1919

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bfraboni
Copy link

@bfraboni bfraboni commented Apr 30, 2023

Hey team,

I'm very new to Rust, and very new to using the Image-rs crate -- so please forgive the potential mistakes I could write/say.

I'm trying to add a block enumerator for incoherent ImagBuffer access.
This is usually faster than regular pixel enum when we want to perform a column-major operation on a image, even though the memory layout is row-major.
Basically the following code:

fn transpose_enum<P, Container>(input: &ImageBuffer<P, Container>, output: &mut ImageBuffer<P, Container>)
where 
   P: Pixel, 
   Container: Deref<Target = [P::Subpixel]> + DerefMut
{
   for (x, y, pix) in input.enumerate_pixels() {
       output.put_pixel(y, x, *pix);
   }
}

can be outperformed by the PR modified version:

fn transpose_enum_blocks<P, Container>(input: &ImageBuffer<P, Container>, output: &mut ImageBuffer<P, Container>, block_size: u32)
where 
    P: Pixel, 
    Container: Deref<Target = [P::Subpixel]> + DerefMut
{
    for (x, y, pix) in input.enumerate_pixels_blocks(block_size) {
        output.put_pixel(y, x, *pix);
    }
}

On my old 2016 laptop I get between 1.3x to 2x perf improvements for some adequate block sizes when tranposing 2K - 4K and 8K images. I still didn't properly becnhmarked on my desktop machine though (Ryzen 7 5800x) but will soon do.

Let me know if that's of interest for the image crate, and please let me know of any dumb mistake I could have done in the code. Note that I randomly chose function and variable names, but we can completely change that, and I can also make a mutable version of the enumerator.

I license past and future contributions under the dual MIT/Apache-2.0 license,
allowing licensees to choose either at their option.

… pixel enum for non consistent memory access for e.g. image transpose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant