fn horizontal_sample<I, P, S>(
    image: &I,
    new_width: 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(new_width, height);

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

    for y in 0..height {
        for outx in 0..new_width {
            // Find the point in the input image corresponding to the centre
            // of the current pixel in the output image.
            let inputx = (outx as f32 + 0.5) * ratio;

            // Left and right are slice bounds for the input pixels relevant
            // to the output pixel we are calculating.  Pixel x is relevant
            // if and only if (x >= left) && (x < right).

            // Invariant: 0 <= left < right <= width

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

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

            // Go back to left boundary of pixel, to properly compare with i
            // below, as the kernel treats the centre of a pixel as 0.
            let inputx = inputx - 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 - inputx) / sratio);
                sum += w;

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

                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(outx, y, t);
        }
    }

    out
}
// Sample the rows of the supplied image using the provided filter.
// The height of the image remains unchanged.
// ```new_width``` is the desired width 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 rows of the supplied image using the provided filter.
The height of the image remains unchanged.
Sample the rows of the supplied image using the provided filter . The height of the image remains unchanged .
fn horizontal_sample<I, P, S>(
    image: &I,
    new_width: 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(new_width, height);
    let max: f32 = NumCast::from(S::max_value()).unwrap();
    let ratio = width as f32 / new_width as f32;
    let sratio = if ratio < 1.0 { 1.0 } else { ratio };
    let src_support = filter.support * sratio;
    for y in 0..height {
        for outx in 0..new_width {
            // Find the point in the input image corresponding to the centre
            // of the current pixel in the output image.
            let inputx = (outx as f32 + 0.5) * ratio;
            // Left and right are slice bounds for the input pixels relevant
            // to the output pixel we are calculating.  Pixel x is relevant
            // if and only if (x >= left) && (x < right).
            // Invariant: 0 <= left < right <= width
            let left = (inputx - src_support).floor() as i64;
            let left = clamp(left, 0, <i64 as From<_>>::from(width) - 1) as u32;
            let right = (inputx + src_support).ceil() as i64;
            let right = clamp(
                right,
                <i64 as From<_>>::from(left) + 1,
                <i64 as From<_>>::from(width),
            ) as u32;
            // Go back to left boundary of pixel, to properly compare with i
            // below, as the kernel treats the centre of a pixel as 0.
            let inputx = inputx - 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 - inputx) / sratio);
                sum += w;
                let p = image.get_pixel(i, y);
                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(outx, y, t);
        }
    }
    out
}
fn horizontal_sample < I , P , S > ( image : & I , new_width : 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 ( new_width , height ) ; let max : f32 = NumCast :: from ( S :: max_value ( ) ) . unwrap ( ) ; let ratio = width as f32 / new_width as f32 ; let sratio = if ratio < 1.0 { 1.0 } else { ratio } ; let src_support = filter . support * sratio ; for y in 0 .. height { for outx in 0 .. new_width { // Find the point in the input image corresponding to the centre // of the current pixel in the output image. let inputx = ( outx as f32 + 0.5 ) * ratio ; // Left and right are slice bounds for the input pixels relevant // to the output pixel we are calculating.  Pixel x is relevant // if and only if (x >= left) && (x < right). // Invariant: 0 <= left < right <= width let left = ( inputx - src_support ) . floor ( ) as i64 ; let left = clamp ( left , 0 , < i64 as From < _ > > :: from ( width ) - 1 ) as u32 ; let right = ( inputx + src_support ) . ceil ( ) as i64 ; let right = clamp ( right , < i64 as From < _ > > :: from ( left ) + 1 , < i64 as From < _ > > :: from ( width ) , ) as u32 ; // Go back to left boundary of pixel, to properly compare with i // below, as the kernel treats the centre of a pixel as 0. let inputx = inputx - 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 - inputx ) / sratio ) ; sum += w ; let p = image . get_pixel ( i , y ) ; 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 ( outx , y , t ) ; } } out }
Sample the rows of the supplied image using the provided filter.
Sample the rows of the supplied image using the provided filter .
image
&I
new_width
u32
filter
&mut Filter