image icon indicating copy to clipboard operation
image copied to clipboard

Pixel conversions could be more generic to enable more optimal operations

Open nagisa opened this issue 5 years ago • 3 comments

To rotate by 180 degrees while also converting BGR to RGB, the most efficient implementation is to invert the memory buffer by e.g. a backward memcpy.

I think optimizer should be able to figure this out fairly easily, but we still need a way to do this conversion and rotation in a single operation, which would require some generic way to convert between colourspaces, to allow implementations like these (somewhat pseudo-code):

/// Rotate an image 180 degrees clockwise.
pub fn rotate180<I: GenericImageView, NewPixel: Pixel>(
    image: &I,
) -> ImageBuffer<NewPixel, Vec<<NewPixel as Pixel>::Subpixel>>
    where I::Pixel: Into<NewPixel> + 'static,
{
    let (width, height) = image.dimensions();
    let mut out = ImageBuffer::new(width, height);

    for y in 0..height {
        for x in 0..width {
            let p = image.get_pixel(x, y);
            out.put_pixel(width - 1 - x, height - 1 - y, p.into());
        }
    }

    out
}

nagisa avatar Nov 26 '19 18:11 nagisa