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
use std::path::Path;

use graphics::ImageSize;
use glium::texture::srgb_texture2d::{ SrgbTexture2d };
use glium::texture::{ RawImage2d, TextureCreationError };
use glium::backend::Facade;
use image::{ self, DynamicImage, RgbaImage };
use texture::{ self, TextureSettings, CreateTexture, UpdateTexture, Format };

/// 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);

impl Texture {
    /// Creates a new `Texture`.
    pub fn new(texture: SrgbTexture2d) -> Texture {
        Texture(texture)
    }

    /// 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.
    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 = try!(image::open(path).map_err(|e| e.to_string()));

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

        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.
    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.
    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, [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> CreateTexture<F> for Texture
    where F: Facade
{
    type Error = TextureCreationError;

    fn create<S: Into<[u32; 2]>>(
        factory: &mut F,
        _format: Format,
        memory: &[u8],
        size: S,
        _settings: &TextureSettings
    ) -> Result<Self, Self::Error> {
        let size = size.into();
        Ok(Texture(try!(SrgbTexture2d::new(factory,
                RawImage2d::from_raw_rgba_reversed(memory.to_owned(),
                    (size[0], size[1]))))))
    }
}

impl<F> UpdateTexture<F> for Texture
    where F: Facade
{
    type Error = TextureCreationError;

    #[allow(unused_variables)]
    fn update<S: Into<[u32; 2]>>(
        &mut self,
        factory: &mut F,
        _format: Format,
        memory: &[u8],
        size: S
    ) -> Result<(), Self::Error> {
        unimplemented!()
    }
}