fn vertical_sample<I, P, S>(
    image: &I,
    new_height: u32,
    filter: &mut Filter,
) -> ImageBuffer<P, Vec<S>>
where
    I: GenericImage<Pixel = P> + 'static,
    P: Pixel<Subpixel = S> + 'static,
    S: Primitive + 'static,
{
    let (width, height) = image.dimensions();
    let mut out = ImageBuffer::new(width, new_height);

    let max: f32 = NumCast::from(S::max_value()).unwrap();
    let ratio = height as f32 / new_height as f32;
    let sratio = if ratio < 1.0 { 1.0 } else { ratio };
    let src_support = filter.support * sratio;

    for x in 0..width {
        for outy in 0..new_height {
            // For an explanation of this algorithm, see the comments
            // in horizontal_sample.
            let inputy = (outy as f32 + 0.5) * ratio;

            let left = (inputy - src_support).floor() as i64;
            let left = clamp(left, 0, <i64 as From<_>>::from(height) - 1) as u32;

            let right = (inputy + src_support).ceil() as i64;
            let right = clamp(
                right,
                <i64 as From<_>>::from(left) + 1,
                <i64 as From<_>>::from(height),
            ) as u32;

            let inputy = inputy - 0.5;

            let mut sum = 0.0;

            let mut t = (0.0, 0.0, 0.0, 0.0);

            for i in left..right {
                let w = (filter.kernel)((i as f32 - inputy) / sratio);
                sum += w;

                let p = image.get_pixel(x, i);

                let (k1, k2, k3, k4) = p.channels4();
                let vec: (f32, f32, f32, f32) = (
                    NumCast::from(k1).unwrap(),
                    NumCast::from(k2).unwrap(),
                    NumCast::from(k3).unwrap(),
                    NumCast::from(k4).unwrap(),
                );

                t.0 += vec.0 * w;
                t.1 += vec.1 * w;
                t.2 += vec.2 * w;
                t.3 += vec.3 * w;
            }

            let (t1, t2, t3, t4) = (t.0 / sum, t.1 / sum, t.2 / sum, t.3 / sum);
            let t = Pixel::from_channels(
                NumCast::from(clamp(t1, 0.0, max)).unwrap(),
                NumCast::from(clamp(t2, 0.0, max)).unwrap(),
                NumCast::from(clamp(t3, 0.0, max)).unwrap(),
                NumCast::from(clamp(t4, 0.0, max)).unwrap(),
            );

            out.put_pixel(x, outy, t);
        }
    }

    out
}
// Sample the columns of the supplied image using the provided filter.
// The width of the image remains unchanged.
// ```new_height``` is the desired height of the new image
// ```filter``` is the filter to use for sampling.
// TODO: Do we really need the 'static bound on `I`? Can we avoid it?
Sample the columns of the supplied image using the provided filter.
The width of the image remains unchanged.
Sample the columns of the supplied image using the provided filter . The width of the image remains unchanged .
fn vertical_sample<I, P, S>(
    image: &I,
    new_height: u32,
    filter: &mut Filter,
) -> ImageBuffer<P, Vec<S>>
where
    I: GenericImage<Pixel = P> + 'static,
    P: Pixel<Subpixel = S> + 'static,
    S: Primitive + 'static,
{
    let (width, height) = image.dimensions();
    let mut out = ImageBuffer::new(width, new_height);
    let max: f32 = NumCast::from(S::max_value()).unwrap();
    let ratio = height as f32 / new_height as f32;
    let sratio = if ratio < 1.0 { 1.0 } else { ratio };
    let src_support = filter.support * sratio;
    for x in 0..width {
        for outy in 0..new_height {
            // For an explanation of this algorithm, see the comments
            // in horizontal_sample.
            let inputy = (outy as f32 + 0.5) * ratio;
            let left = (inputy - src_support).floor() as i64;
            let left = clamp(left, 0, <i64 as From<_>>::from(height) - 1) as u32;
            let right = (inputy + src_support).ceil() as i64;
            let right = clamp(
                right,
                <i64 as From<_>>::from(left) + 1,
                <i64 as From<_>>::from(height),
            ) as u32;
            let inputy = inputy - 0.5;
            let mut sum = 0.0;
            let mut t = (0.0, 0.0, 0.0, 0.0);
            for i in left..right {
                let w = (filter.kernel)((i as f32 - inputy) / sratio);
                sum += w;
                let p = image.get_pixel(x, i);
                let (k1, k2, k3, k4) = p.channels4();
                let vec: (f32, f32, f32, f32) = (
                    NumCast::from(k1).unwrap(),
                    NumCast::from(k2).unwrap(),
                    NumCast::from(k3).unwrap(),
                    NumCast::from(k4).unwrap(),
                );
                t.0 += vec.0 * w;
                t.1 += vec.1 * w;
                t.2 += vec.2 * w;
                t.3 += vec.3 * w;
            }
            let (t1, t2, t3, t4) = (t.0 / sum, t.1 / sum, t.2 / sum, t.3 / sum);
            let t = Pixel::from_channels(
                NumCast::from(clamp(t1, 0.0, max)).unwrap(),
                NumCast::from(clamp(t2, 0.0, max)).unwrap(),
                NumCast::from(clamp(t3, 0.0, max)).unwrap(),
                NumCast::from(clamp(t4, 0.0, max)).unwrap(),
            );
            out.put_pixel(x, outy, t);
        }
    }
    out
}
fn vertical_sample < I , P , S > ( image : & I , new_height : u32 , filter : & mut Filter , ) -> ImageBuffer < P , Vec < S > > where I : GenericImage < Pixel = P > + ' static , P : Pixel < Subpixel = S > + ' static , S : Primitive + ' static , { let ( width , height ) = image . dimensions ( ) ; let mut out = ImageBuffer :: new ( width , new_height ) ; let max : f32 = NumCast :: from ( S :: max_value ( ) ) . unwrap ( ) ; let ratio = height as f32 / new_height as f32 ; let sratio = if ratio < 1.0 { 1.0 } else { ratio } ; let src_support = filter . support * sratio ; for x in 0 .. width { for outy in 0 .. new_height { // For an explanation of this algorithm, see the comments // in horizontal_sample. let inputy = ( outy as f32 + 0.5 ) * ratio ; let left = ( inputy - src_support ) . floor ( ) as i64 ; let left = clamp ( left , 0 , < i64 as From < _ > > :: from ( height ) - 1 ) as u32 ; let right = ( inputy + src_support ) . ceil ( ) as i64 ; let right = clamp ( right , < i64 as From < _ > > :: from ( left ) + 1 , < i64 as From < _ > > :: from ( height ) , ) as u32 ; let inputy = inputy - 0.5 ; let mut sum = 0.0 ; let mut t = ( 0.0 , 0.0 , 0.0 , 0.0 ) ; for i in left .. right { let w = ( filter . kernel ) ( ( i as f32 - inputy ) / sratio ) ; sum += w ; let p = image . get_pixel ( x , i ) ; let ( k1 , k2 , k3 , k4 ) = p . channels4 ( ) ; let vec : ( f32 , f32 , f32 , f32 ) = ( NumCast :: from ( k1 ) . unwrap ( ) , NumCast :: from ( k2 ) . unwrap ( ) , NumCast :: from ( k3 ) . unwrap ( ) , NumCast :: from ( k4 ) . unwrap ( ) , ) ; t . 0 += vec . 0 * w ; t . 1 += vec . 1 * w ; t . 2 += vec . 2 * w ; t . 3 += vec . 3 * w ; } let ( t1 , t2 , t3 , t4 ) = ( t . 0 / sum , t . 1 / sum , t . 2 / sum , t . 3 / sum ) ; let t = Pixel :: from_channels ( NumCast :: from ( clamp ( t1 , 0.0 , max ) ) . unwrap ( ) , NumCast :: from ( clamp ( t2 , 0.0 , max ) ) . unwrap ( ) , NumCast :: from ( clamp ( t3 , 0.0 , max ) ) . unwrap ( ) , NumCast :: from ( clamp ( t4 , 0.0 , max ) ) . unwrap ( ) , ) ; out . put_pixel ( x , outy , t ) ; } } out }
Sample the columns of the supplied image using the provided filter.
Sample the columns of the supplied image using the provided filter .
image
&I
new_height
u32
filter
&mut Filter