1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#[cfg(feature = "image")]
extern crate image;

#[cfg(feature = "image")]
use std::path::Path;

#[cfg(feature = "image")]
use self::image::{DynamicImage, RgbaImage};
use glium::backend::Facade;
use glium::texture::srgb_texture2d::SrgbTexture2d;
use glium::texture::{RawImage2d, TextureCreationError};
use glium::uniforms::SamplerWrapFunction;
use graphics::ImageSize;
use texture::{self, CreateTexture, Format, TextureOp, TextureSettings, UpdateTexture};

/// Flip settings.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Flip {
    /// Does not flip.
    None,
    /// Flips image vertically.
    Vertical,
}

/// Wrapper for 2D texture.
pub struct Texture(pub SrgbTexture2d, pub [SamplerWrapFunction; 2]);

impl Texture {
    /// Creates a new `Texture`.
    pub fn new(texture: SrgbTexture2d) -> Texture {
        Texture(texture, [SamplerWrapFunction::Clamp; 2])
    }

    /// Returns empty texture.
    pub fn empty<F>(factory: &mut F) -> Result<Self, TextureCreationError>
    where
        F: Facade,
    {
        CreateTexture::create(
            factory,
            Format::Rgba8,
            &[0u8; 4],
            [1, 1],
            &TextureSettings::new(),
        )
    }

    /// Creates a texture from path.
    #[cfg(feature = "image")]
    pub fn from_path<F, P>(
        factory: &mut F,
        path: P,
        flip: Flip,
        settings: &TextureSettings,
    ) -> Result<Self, String>
    where
        F: Facade,
        P: AsRef<Path>,
    {
        let img = image::open(path).map_err(|e| e.to_string())?;

        let img = match img {
            DynamicImage::ImageRgba8(img) => img,
            img => img.to_rgba8(),
        };

        let img = if flip == Flip::Vertical {
            image::imageops::flip_vertical(&img)
        } else {
            img
        };

        Texture::from_image(factory, &img, settings).map_err(|e| format!("{:?}", e))
    }

    /// Creates a texture from image.
    #[cfg(feature = "image")]
    pub fn from_image<F>(
        factory: &mut F,
        img: &RgbaImage,
        settings: &TextureSettings,
    ) -> Result<Self, TextureCreationError>
    where
        F: Facade,
    {
        let (width, height) = img.dimensions();
        CreateTexture::create(factory, Format::Rgba8, img, [width, height], settings)
    }

    /// Creates texture from memory alpha.
    pub fn from_memory_alpha<F>(
        factory: &mut F,
        buffer: &[u8],
        width: u32,
        height: u32,
        settings: &TextureSettings,
    ) -> Result<Self, TextureCreationError>
    where
        F: Facade,
    {
        if width == 0 || height == 0 {
            return Texture::empty(factory);
        }

        let size = [width, height];
        let buffer = texture::ops::alpha_to_rgba8(buffer, size);
        CreateTexture::create(factory, Format::Rgba8, &buffer, size, settings)
    }

    /// Updates texture with an image.
    #[cfg(feature = "image")]
    pub fn update<F>(
        &mut self,
        factory: &mut F,
        img: &RgbaImage,
    ) -> Result<(), TextureCreationError>
    where
        F: Facade,
    {
        let (width, height) = img.dimensions();
        UpdateTexture::update(self, factory, Format::Rgba8, img, [0, 0], [width, height])
    }
}

impl ImageSize for Texture {
    fn get_size(&self) -> (u32, u32) {
        let ref tex = self.0;
        (tex.get_width(), tex.get_height().unwrap())
    }
}

impl<F> TextureOp<F> for Texture {
    type Error = TextureCreationError;
}

impl<F> CreateTexture<F> for Texture
where
    F: Facade,
{
    fn create<S: Into<[u32; 2]>>(
        factory: &mut F,
        _format: Format,
        memory: &[u8],
        size: S,
        settings: &TextureSettings,
    ) -> Result<Self, Self::Error> {
        use texture::Wrap;
        let size = size.into();

        let f = |wrap| match wrap {
            Wrap::ClampToEdge => SamplerWrapFunction::Clamp,
            Wrap::Repeat => SamplerWrapFunction::Repeat,
            Wrap::MirroredRepeat => SamplerWrapFunction::Mirror,
            Wrap::ClampToBorder => SamplerWrapFunction::BorderClamp,
        };

        let wrap_u = f(settings.get_wrap_u());
        let wrap_v = f(settings.get_wrap_v());
        Ok(Texture(
            SrgbTexture2d::new(
                factory,
                RawImage2d::from_raw_rgba_reversed(memory, (size[0], size[1]))
            )?,
            [wrap_u, wrap_v],
        ))
    }
}

impl<F> UpdateTexture<F> for Texture
where
    F: Facade,
{
    #[allow(unused_variables)]
    fn update<O: Into<[u32; 2]>, S: Into<[u32; 2]>>(
        &mut self,
        factory: &mut F,
        _format: Format,
        memory: &[u8],
        offset: O,
        size: S,
    ) -> Result<(), Self::Error> {
        use glium::Rect;

        let offset = offset.into();
        let size = size.into();
        let (_, h) = self.get_size();
        self.0.write(
            Rect {
                left: offset[0],
                bottom: h - offset[1] - size[1],
                width: size[0],
                height: size[1],
            },
            RawImage2d::from_raw_rgba_reversed(memory, (size[0], size[1])),
        );
        Ok(())
    }
}