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
use std::path::Path;
use { gfx, ImageSize, Settings };
use image::{
self,
DynamicImage,
GenericImage,
RgbaImage,
};
#[derive(Clone, Debug, PartialEq)]
pub struct Texture<R> where R: gfx::Resources {
handle: gfx::handle::Texture<R>
}
impl<R: gfx::Resources> Texture<R> {
pub fn handle(&self) -> gfx::handle::Texture<R> {
self.handle.clone()
}
pub fn empty<F>(factory: &mut F) -> Result<Self, gfx::tex::TextureError>
where F: gfx::Factory<R>
{
use gfx::traits::*;
let tex_handle = try!(factory.create_texture_rgba8(1, 1));
let ref image_info = tex_handle.get_info().to_image_info();
try!(factory.update_texture(
&tex_handle,
image_info,
&[0u8; 4],
Some(gfx::tex::TextureKind::Texture2D)
));
Ok(Texture {
handle: tex_handle
})
}
pub fn from_path<F, P>(
factory: &mut F,
path: P,
settings: &Settings,
) -> Result<Self, String>
where F: gfx::Factory<R>,
P: AsRef<Path>
{
let img = try!(image::open(path).map_err(|e| e.to_string()));
let mut img = match img {
DynamicImage::ImageRgba8(img) => img,
img => img.to_rgba()
};
if settings.flip_vertical {
img = image::imageops::flip_vertical(&img);
}
Ok(Texture::from_image(factory, &img,
settings.convert_gamma,
settings.compress,
settings.generate_mipmap))
}
pub fn from_image<F>(
factory: &mut F,
image: &RgbaImage,
convert_gamma: bool,
_compress: bool,
generate_mipmap: bool
) -> Self
where F: gfx::Factory<R>
{
let (width, height) = image.dimensions();
let tex_info = gfx::tex::TextureInfo {
width: width as u16,
height: height as u16,
depth: 1,
levels: 1,
kind: gfx::tex::TextureKind::Texture2D,
format: if convert_gamma {
gfx::tex::Format::SRGB8_A8
} else { gfx::tex::RGBA8 }
};
let tex_handle = factory.create_texture_static(tex_info, &image).unwrap();
if generate_mipmap {
factory.generate_mipmap(&tex_handle);
}
Texture {
handle: tex_handle
}
}
pub fn from_memory_alpha<F>(
factory: &mut F,
buffer: &[u8],
width: u32,
height: u32,
) -> Self
where F: gfx::Factory<R>
{
use gfx::traits::*;
let width = if width == 0 { 1 } else { width as u16 };
let height = if height == 0 { 1 } else { height as u16 };
let mut pixels = vec![];
for alpha in buffer {
pixels.extend(vec![255; 3]);
pixels.push(*alpha);
}
let tex_handle = factory.create_texture_rgba8(width, height).unwrap();
let ref image_info = tex_handle.get_info().to_image_info();
factory.update_texture(
&tex_handle,
image_info,
&pixels,
Some(gfx::tex::TextureKind::Texture2D)
).unwrap();
Texture {
handle: tex_handle
}
}
pub fn update<F>(&mut self, factory: &mut F, image: &RgbaImage)
where F: gfx::Factory<R>
{
factory.update_texture(&self.handle,
&self.handle.get_info().to_image_info(),
&image,
Some(gfx::tex::TextureKind::Texture2D)
).unwrap();
}
}
impl<R> ImageSize for Texture<R> where R: gfx::Resources {
#[inline(always)]
fn get_size(&self) -> (u32, u32) {
let info = self.handle.get_info();
(info.width as u32, info.height as u32)
}
}