1use std::ops::DerefMut;
2
3use glium;
4use image_ext;
5
6use {Screen, ScreenType};
7use errors::ProcessingErr;
8
9const GL_TEXTURE_MAX_ANISOTROPY_EXT: u32 = 0x84FE;
11
12impl<'a> Screen<'a> {
13 pub fn texture<P, S, C>(
19 &mut self,
20 img: &image_ext::ImageBuffer<P, C>,
21 ) -> Result<(glium::texture::Texture2d, f64, f64), ProcessingErr>
22 where
23 P: image_ext::Pixel<Subpixel = S> + 'static,
24 S: image_ext::Primitive + 'static + glium::texture::ToClientFormat + glium::texture::PixelValue,
25 C: DerefMut<Target = [P::Subpixel]>,
26 {
27 let wh = img.dimensions();
28 let image_ext = glium::texture::RawImage2d::from_raw_rgba_reversed(&img, wh);
30 let texture = match self.display {
31 ScreenType::Window(ref d) => {
32 glium::texture::Texture2d::with_format(
33 d,
34 image_ext,
35 glium::texture::UncompressedFloatFormat::F32F32F32F32,
36 glium::texture::MipmapsOption::AutoGeneratedMipmaps,
37 ).map_err(|e| ProcessingErr::TextureNoCreate(e))?
38 }
39 ScreenType::Headless(ref d) => {
40 glium::texture::Texture2d::with_format(
41 d,
42 image_ext,
43 glium::texture::UncompressedFloatFormat::F32F32F32F32,
44 glium::texture::MipmapsOption::AutoGeneratedMipmaps,
45 ).map_err(|e| ProcessingErr::TextureNoCreate(e))?
46 }
47 };
48
49 Ok((
57 texture,
58 wh.0 as f64 / self.width as f64,
59 wh.1 as f64 / self.height as f64,
60 ))
61 }
62
63 pub fn empty_texture(&self, w: u32, h: u32) -> Result<(glium::texture::Texture2d, f64, f64), ProcessingErr> {
68 let texture = match self.display {
69 ScreenType::Window(ref d) => {
70 glium::texture::Texture2d::empty_with_format(
71 d,
72 glium::texture::UncompressedFloatFormat::F32F32F32F32,
73 glium::texture::MipmapsOption::AutoGeneratedMipmaps,
74 w,
75 h,
76 ).map_err(|e| ProcessingErr::TextureNoCreate(e))?
77 }
78 ScreenType::Headless(ref d) => {
79 glium::texture::Texture2d::empty_with_format(
80 d,
81 glium::texture::UncompressedFloatFormat::F32F32F32F32,
82 glium::texture::MipmapsOption::AutoGeneratedMipmaps,
83 w,
84 h,
85 ).map_err(|e| ProcessingErr::TextureNoCreate(e))?
86 }
87 };
88
89 Ok((
90 texture,
91 w as f64 / self.width as f64,
92 h as f64 / self.height as f64,
93 ))
94 }
95
96 pub fn texture_from_data<P: glium::texture::PixelValue>(
101 &self,
102 data: Vec<Vec<P>>,
103 ) -> Result<(glium::texture::Texture2d, f64, f64), ProcessingErr> {
104 let h = data.len();
105 let w = data[0].len();
106 let texture = match self.display {
107 ScreenType::Window(ref d) => {
108 glium::texture::Texture2d::with_mipmaps(
109 d,
110 data,
111 glium::texture::MipmapsOption::AutoGeneratedMipmaps,
112 ).map_err(|e| ProcessingErr::TextureNoCreate(e))?
113 }
114 ScreenType::Headless(ref d) => {
115 glium::texture::Texture2d::with_mipmaps(
116 d,
117 data,
118 glium::texture::MipmapsOption::AutoGeneratedMipmaps,
119 ).map_err(|e| ProcessingErr::TextureNoCreate(e))?
120 }
121 };
122
123
124 Ok((
125 texture,
126 w as f64 / self.width as f64,
127 h as f64 / self.height as f64,
128 ))
129 }
130
131 pub fn texture_array<T: Clone + 'a + glium::texture::PixelValue>(&mut self, images: Vec<glium::texture::RawImage2d<T>>) -> Result<glium::texture::Texture2dArray, ProcessingErr> {
138 match self.display {
139 ScreenType::Window(ref d) => {
140 glium::texture::Texture2dArray::with_format(d,
141 images,
142 glium::texture::UncompressedFloatFormat::F32F32F32F32,
143 glium::texture::MipmapsOption::AutoGeneratedMipmaps)
144 .map_err(|e| ProcessingErr::TextureNoCreate(e))
145 }
146 ScreenType::Headless(ref d) => {
147 glium::texture::Texture2dArray::with_format(d,
148 images,
149 glium::texture::UncompressedFloatFormat::F32F32F32F32,
150 glium::texture::MipmapsOption::AutoGeneratedMipmaps)
151 .map_err(|e| ProcessingErr::TextureNoCreate(e))
152 }
153 }
154 }
155
156
157 pub fn texture_wrap(&mut self, wrap: &str) {
161 if wrap == "CLAMP" {
162 self.wrap = glium::uniforms::SamplerWrapFunction::Clamp;
163 } else if wrap == "REPEAT" {
164 self.wrap = glium::uniforms::SamplerWrapFunction::Repeat;
165 }
166 }
167}
168
169