<commit_before>//! An `Image` texture is a `Texture` whose samples come
//! from an image file.

use std::sync::Arc;

use image::{self, GenericImage};

use linalg::clamp;
use film::Colorf;
use texture::{Texture, bilinear_interpolate};

pub struct Image {
    img: Arc<image::DynamicImage>,
}

impl Image {
    pub fn new(img: Arc<image::DynamicImage>) -> Image {
        Image { img: img }
    }
    fn get_float(&self, x: u32, y: u32) -> f32 {
        let dims = self.img.dimensions();
        let x = clamp(x, 0, dims.0 - 1);
        let y = clamp(y, 0, dims.1 - 1);
        self.img.get_pixel(x, y).data[0] as f32 / 255.0
    }
    fn get_color(&self, x: u32, y: u32) -> Colorf {
        let dims = self.img.dimensions();
        let x = clamp(x, 0, dims.0 - 1);
        let y = clamp(y, 0, dims.1 - 1);
        let px = self.img.get_pixel(x, y);
        Colorf::with_alpha(px.data[0] as f32 / 255.0,
                           px.data[1] as f32 / 255.0,
                           px.data[2] as f32 / 255.0,
                           px.data[3] as f32 / 255.0)
    }
}

impl Texture<f32> for Image {
    fn sample(&self, u: f32, v: f32, _: f32) -> f32 {
        let dims = self.img.dimensions();
        let x = u * dims.0 as f32;
        let y = v * dims.1 as f32;
        bilinear_interpolate(x, y, |px, py| self.get_float(px, py))
    }
}

impl Texture<Colorf> for Image {
    fn sample(&self, u: f32, v: f32, _: f32) -> Colorf {
        let x = u * self.img.dimensions().0 as f32;
        let y = v * self.img.dimensions().1 as f32;
        bilinear_interpolate(x, y, |px, py| self.get_color(px, py))
    }
}

<commit_msg>The Textures themselves will be refcounted, not the images underneath<commit_after>//! An `Image` texture is a `Texture` whose samples come
//! from an image file.

use image::{self, GenericImage};

use linalg::clamp;
use film::Colorf;
use texture::{Texture, bilinear_interpolate};

pub struct Image {
    img: image::DynamicImage,
}

impl Image {
    pub fn new(img: image::DynamicImage) -> Image {
        Image { img: img }
    }
    fn get_float(&self, x: u32, y: u32) -> f32 {
        let dims = self.img.dimensions();
        let x = clamp(x, 0, dims.0 - 1);
        let y = clamp(y, 0, dims.1 - 1);
        self.img.get_pixel(x, y).data[0] as f32 / 255.0
    }
    fn get_color(&self, x: u32, y: u32) -> Colorf {
        let dims = self.img.dimensions();
        let x = clamp(x, 0, dims.0 - 1);
        let y = clamp(y, 0, dims.1 - 1);
        let px = self.img.get_pixel(x, y);
        Colorf::with_alpha(px.data[0] as f32 / 255.0,
                           px.data[1] as f32 / 255.0,
                           px.data[2] as f32 / 255.0,
                           px.data[3] as f32 / 255.0)
    }
}

impl Texture<f32> for Image {
    fn sample(&self, u: f32, v: f32, _: f32) -> f32 {
        let dims = self.img.dimensions();
        let x = u * dims.0 as f32;
        let y = v * dims.1 as f32;
        bilinear_interpolate(x, y, |px, py| self.get_float(px, py))
    }
}

impl Texture<Colorf> for Image {
    fn sample(&self, u: f32, v: f32, _: f32) -> Colorf {
        let x = u * self.img.dimensions().0 as f32;
        let y = v * self.img.dimensions().1 as f32;
        bilinear_interpolate(x, y, |px, py| self.get_color(px, py))
    }
}


{'fraction_non_alphanumeric': 0.15393976270155157, 'fraction_numerical': 0.05171889260724064, 'mean_word_length': 1.9809610154125112, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, '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': '1417971', 'n_tokens_mistral': 1357, 'n_tokens_neox': 1253, 'n_words': 353}