Skip to content

Conversation

Glatzel
Copy link

@Glatzel Glatzel commented Apr 15, 2025

This trait will allow user to restrict type or extend the T in Rgb<T: Primitive Enlargeable> and Rgba<T: Primitive Enlargeable>.

example1

A simple example for allowing restrict type.

pub trait  MyPrimitive:
    image::Primitive + image::Enlargeable
{}
impl MyPrimitivefor u8 {}
impl MyPrimitivefor u16 {}
// This function only allow 8bit or 16bit image as input.
pub fn process<T>(src: &image::ImageBuffer<image::Rgb<T>, Vec<T>>)
{
    todo!();
}

example2

This is what I used in camera raw image processing with libraw. libraw gives a pointer that could convert to u16 rgba (representing R,G,B,G2 pixel in sensor) imagebuffer after raw2image() and subtract_black(). But in custom raw image post processing for those camera with standard bayer sensor, knowing CFA pattern and a grayscale bayer image is satisfied and could reduce the memory usage.

pub trait  BayerPrimitive :
    image::Primitive + image::Enlargeable + std::marker::Send + std::marker::Sync
{}
impl BayerPrimitive for u16 {}
impl BayerPrimitive for f32 {}
// This function only allow 16bit or f32 image as output.
// 16bit for general photographic workflow.
// 32bit for calibration and analysis purpose.
// Other types like i32 which have negative values are not prctical for photographic workflow.
pub fn extract_bayer<T>(src: &image::ImageBuffer<image::Rgba<u16>, Vec<u16>>)
->&image::ImageBuffer<image::Rgb<T>, Vec<T>>
{
    let dst = ImageBuffer::from_par_fn(raw_img.width(), raw_img.height(), |x, y| {
            let pixel = raw_img.get_pixel(x, y);
            // Extract the max value in pixel and convert it to `T` type.
            // `T::from()` require the `T` impl `Send` and `Sync` trait.
            let value = T::from(pixel[0].max(pixel[1]).max(pixel[2]).max(pixel[3])).unwrap();
            image::Luma::<T>([value])
        });
    dst
}

@Glatzel Glatzel marked this pull request as draft April 15, 2025 13:31
@Glatzel Glatzel marked this pull request as ready for review April 15, 2025 13:50
@fintelia
Copy link
Contributor

Could you say what this is trying to solve / what impact this has on the overall API?

@Glatzel
Copy link
Author

Glatzel commented Apr 16, 2025

Hi, @fintelia . Sorry for missing the description. I have added it above.

@fintelia
Copy link
Contributor

fintelia commented Apr 17, 2025

Ah, so the goal is calling generically calling methods that are bounded by Enlargeable but not actually wanting to directly implement the trait or access its methods.

I created #2453 to try out having a public PrimitiveExt trait which lets you generically call methods that need the extra functionality, but not to use Enlargeable directly. Do you think that would work for what you're trying to do?

@Glatzel
Copy link
Author

Glatzel commented Apr 17, 2025

I tried it in my repo, the PrimitiveExt does work.

@Glatzel
Copy link
Author

Glatzel commented Apr 17, 2025

Close this pr as this requirement will be implemented in #2453

@Glatzel Glatzel closed this Apr 17, 2025
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.

2 participants