<commit_before>//!  Image Processing Functions
use std::cmp;

use image:: {
    SubImage,
    GenericImage,
};

use buffer::Pixel;
use traits::Primitive;

pub use self::sample::FilterType;

pub use self::sample::FilterType:: {
    Triangle,
    Nearest,
    CatmullRom,
    Gaussian,
    Lanczos3
};

///Affine transformations
pub use self::affine:: {
    rotate90,
    rotate180,
    rotate270,
    flip_horizontal,
    flip_vertical,
};

///Image sampling
pub use self::sample:: {
    filter3x3,
    resize,
    blur,
    unsharpen,
};

///Color operations
pub use self::colorops:: {
    grayscale,
    invert,
    contrast,
    brighten,
};

mod affine;
mod colorops;
mod sample;

/// Return a mutable view into an image
pub fn crop<P: Primitive + 'static, T: Pixel<P>, I: GenericImage<T>>(
    image:  &mut I,
    x: u32,
    y: u32,
    width: u32,
    height: u32) -> SubImage<I> {

    let (iwidth, iheight) = image.dimensions();

    let x = cmp::min(x, iwidth);
    let y = cmp::min(y, iheight);

    let height = cmp::min(height, iheight - y);
    let width  = cmp::min(width, iwidth - x);

    SubImage::new(image, x, y, width, height)
}

/// Overlay an image at a given coordinate (x, y)
#[allow(deprecated)]
pub fn overlay<P: Primitive, T: Pixel<P>, I: GenericImage<T>>(
    bottom: &mut I,
    top: &I,
    x: u32,
    y: u32)  {
    let (top_width, top_height) = top.dimensions();
    let (bottom_width, bottom_height) = bottom.dimensions();

    //Crop our top image if we're going out of bounds
    let range_width = if x + top_width > bottom_width {
        bottom_width - x
    } else {
        top_width
    };

    let range_height = if y + top_height > bottom_height {
        bottom_height - y
    } else {
        top_height
    };

    for top_y in range(0, range_height) {
        for top_x in range(0, range_width) {
            let p = top.get_pixel(top_x, top_y);
            bottom.blend_pixel(x + top_x, y + top_y, p);
        }
    }
}

#[cfg(test)]
mod tests {

    use image::GenericImage;
    use buffer::ImageBuffer;
    use color::{Rgb};
    use super::overlay;<commit_msg>update function to  "preferred" method<commit_after>//!  Image Processing Functions
use std::cmp;

use image:: {
    SubImage,
    GenericImage,
};

use buffer::Pixel;
use traits::Primitive;

pub use self::sample::FilterType;

pub use self::sample::FilterType:: {
    Triangle,
    Nearest,
    CatmullRom,
    Gaussian,
    Lanczos3
};

///Affine transformations
pub use self::affine:: {
    rotate90,
    rotate180,
    rotate270,
    flip_horizontal,
    flip_vertical,
};

///Image sampling
pub use self::sample:: {
    filter3x3,
    resize,
    blur,
    unsharpen,
};

///Color operations
pub use self::colorops:: {
    grayscale,
    invert,
    contrast,
    brighten,
};

mod affine;
mod colorops;
mod sample;

/// Return a mutable view into an image
pub fn crop<P: Primitive + 'static, T: Pixel<P>, I: GenericImage<T>>(
    image:  &mut I,
    x: u32,
    y: u32,
    width: u32,
    height: u32) -> SubImage<I> {

    let (iwidth, iheight) = image.dimensions();

    let x = cmp::min(x, iwidth);
    let y = cmp::min(y, iheight);

    let height = cmp::min(height, iheight - y);
    let width  = cmp::min(width, iwidth - x);

    SubImage::new(image, x, y, width, height)
}

/// Overlay an image at a given coordinate (x, y)
pub fn overlay<P: Primitive, T: Pixel<P>, I: GenericImage<T>>(
    bottom: &mut I,
    top: &I,
    x: u32,
    y: u32)  {
    let (top_width, top_height) = top.dimensions();
    let (bottom_width, bottom_height) = bottom.dimensions();

    //Crop our top image if we're going out of bounds
    let range_width = if x + top_width > bottom_width {
        bottom_width - x
    } else {
        top_width
    };

    let range_height = if y + top_height > bottom_height {
        bottom_height - y
    } else {
        top_height
    };

    for top_y in range(0, range_height) {
        for top_x in range(0, range_width) {
            let p = top.get_pixel(top_x, top_y);
            bottom.get_pixel_mut(x + top_x, y + top_y).blend(&p);
        }
    }
}

#[cfg(test)]
mod tests {

    use image::GenericImage;
    use buffer::ImageBuffer;
    use color::{Rgb};
    use super::overlay;
{'fraction_non_alphanumeric': 0.1332388377037562, 'fraction_numerical': 0.011811953697141508, 'mean_word_length': 2.6157130657557643, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7349320', 'n_tokens_mistral': 1559, 'n_tokens_neox': 1499, 'n_words': 419}