extern crate image;
use std::ops::Deref;
use std::mem;
use ::Image;
use self::image::{GenericImageView, Pixel};
use std::slice;
use gl::types::*;
use gl;
impl<Pixel,C> Image for image::ImageBuffer<Pixel,C>
where Pixel: image::Pixel + 'static,
Pixel::Subpixel: 'static,
C: Deref<Target=[Pixel::Subpixel]> {
type DataType = Pixel::Subpixel;
#[cfg(all(not(feature = "gles"), not(feature="webgl")))]
fn gl_internal_format_type(&self) -> Option<(GLenum, GLenum, GLenum)> {
let ift = match Pixel::COLOR_TYPE {
image::ColorType::Rgb8 => (gl::RGB8, gl::RGB, gl::UNSIGNED_BYTE),
image::ColorType::Rgb16 => (gl::RGB16, gl::RGB, gl::UNSIGNED_SHORT),
image::ColorType::Rgba8 => (gl::RGBA8, gl::RGBA, gl::UNSIGNED_BYTE),
image::ColorType::Rgba16 => (gl::RGBA16, gl::RGBA, gl::UNSIGNED_SHORT),
image::ColorType::Bgr8 => (gl::RGB8, gl::BGR, gl::UNSIGNED_BYTE),
image::ColorType::Bgra8 => (gl::RGBA8, gl::BGRA, gl::UNSIGNED_BYTE),
image::ColorType::L8 => (gl::R8, gl::RED, gl::UNSIGNED_BYTE),
image::ColorType::L16 => (gl::R16, gl::RED, gl::UNSIGNED_SHORT),
image::ColorType::La8 => (gl::RG8, gl::RG, gl::UNSIGNED_BYTE),
image::ColorType::La16 => (gl::RG16, gl::RG, gl::UNSIGNED_SHORT),
_ => unimplemented!(),
};
Some(ift)
}
fn num_components(&self) -> usize{
Pixel::CHANNEL_COUNT as usize
}
#[cfg(any(feature = "gles", feature="webgl"))]
fn gl_internal_format_type(&self) -> Option<(GLenum, GLenum, GLenum)> {
match Pixel::COLOR_TYPE {
image::ColorType::Rgb8 => Some((gl::RGB8, gl::RGB, gl::UNSIGNED_BYTE)),
image::ColorType::Rgb16 => Some((gl::RGB16UI, gl::RGB_INTEGER, gl::UNSIGNED_SHORT)),
image::ColorType::Rgba8 => Some((gl::RGBA8, gl::RGBA, gl::UNSIGNED_BYTE)),
image::ColorType::Rgba16 => Some((gl::RGBA16UI, gl::RGBA_INTEGER, gl::UNSIGNED_SHORT)),
image::ColorType::Bgr8 => None,
image::ColorType::Bgra8 => Some((gl::RGBA8, gl::BGRA_EXT, gl::UNSIGNED_BYTE)),
image::ColorType::L8 => Some((gl::R8, gl::RED, gl::UNSIGNED_BYTE)),
image::ColorType::L16 => Some((gl::R16UI, gl::RED_INTEGER, gl::UNSIGNED_SHORT)),
image::ColorType::La8 => Some((gl::RG8, gl::RG, gl::UNSIGNED_BYTE)),
image::ColorType::La16 => Some((gl::RG16UI, gl::RG_INTEGER, gl::UNSIGNED_SHORT)),
_ => None,
}
}
fn width(&self, _level: usize) -> usize{
self.width() as usize
}
fn height(&self, _level: usize) -> usize{
self.height() as usize
}
fn pitch_bytes(&self, _level: usize) -> usize{
self.width() as usize * self.bytes_per_pixel()
}
fn bytes_per_pixel(&self) -> usize{
Pixel::CHANNEL_COUNT as usize * mem::size_of::<Pixel::Subpixel>()
}
fn pitch_components(&self, _level: usize) -> usize{
self.width() as usize
}
fn data(&self) -> &[Pixel::Subpixel]{
self
}
fn levels(&self) -> usize{
1
}
fn mipmap(&self, level: usize) -> Option<&[Pixel::Subpixel]>{
if level == 0 {
Some(self.data())
}else{
None
}
}
fn is_gpu_compressed(&self) -> bool{
false
}
}
impl Image for image::DynamicImage{
type DataType = u8;
#[cfg(all(not(feature = "gles"), not(feature="webgl")))]
fn gl_internal_format_type(&self) -> Option<(GLenum, GLenum, GLenum)> {
match self{
image::DynamicImage::ImageLuma8(_) => Some((gl::R8, gl::RED, gl::UNSIGNED_BYTE)),
image::DynamicImage::ImageLuma16(_) => Some((gl::R16, gl::RED, gl::UNSIGNED_SHORT)),
image::DynamicImage::ImageLumaA8(_) => Some((gl::RG8, gl::RG, gl::UNSIGNED_BYTE)),
image::DynamicImage::ImageLumaA16(_) => Some((gl::RG16, gl::RG, gl::UNSIGNED_SHORT)),
image::DynamicImage::ImageRgb8(_) => Some((gl::RGB8, gl::RGB, gl::UNSIGNED_BYTE)),
image::DynamicImage::ImageRgb16(_) => Some((gl::RGB16, gl::RGB, gl::UNSIGNED_SHORT)),
image::DynamicImage::ImageRgba8(_) => Some((gl::RGBA8, gl::RGBA, gl::UNSIGNED_BYTE)),
image::DynamicImage::ImageRgba16(_) => Some((gl::RGBA16, gl::RGBA, gl::UNSIGNED_SHORT)),
image::DynamicImage::ImageBgr8(_) => Some((gl::RGB8, gl::BGR, gl::UNSIGNED_BYTE)),
image::DynamicImage::ImageBgra8(_) => Some((gl::RGBA8, gl::BGRA, gl::UNSIGNED_BYTE)),
}
}
fn num_components(&self) -> usize{
<Self as GenericImageView>::Pixel::CHANNEL_COUNT as usize
}
#[cfg(any(feature = "gles", feature="webgl"))]
fn gl_internal_format_type(&self) -> Option<(GLenum, GLenum, GLenum)> {
match self{
image::DynamicImage::ImageLuma8(_) => Some((gl::R8, gl::RED, gl::UNSIGNED_BYTE)),
image::DynamicImage::ImageLuma16(_) => Some((gl::R16UI, gl::RED_INTEGER, gl::UNSIGNED_SHORT)),
image::DynamicImage::ImageLumaA8(_) => Some((gl::RG8, gl::RG, gl::UNSIGNED_BYTE)),
image::DynamicImage::ImageLumaA16(_) => Some((gl::RG16UI, gl::RG_INTEGER, gl::UNSIGNED_SHORT)),
image::DynamicImage::ImageRgb8(_) => Some((gl::RGB8, gl::RGB, gl::UNSIGNED_BYTE)),
image::DynamicImage::ImageRgb16(_) => Some((gl::RGB16UI, gl::RGB_INTEGER, gl::UNSIGNED_SHORT)),
image::DynamicImage::ImageRgba8(_) => Some((gl::RGBA8, gl::RGBA, gl::UNSIGNED_BYTE)),
image::DynamicImage::ImageRgba16(_) => Some((gl::RGBA16UI, gl::RGBA_INTEGER, gl::UNSIGNED_SHORT)),
image::DynamicImage::ImageBgr8(_) => None,
image::DynamicImage::ImageBgra8(_) => Some((gl::RGBA8, gl::BGRA_EXT, gl::UNSIGNED_BYTE)),
}
}
fn width(&self, _level: usize) -> usize{
GenericImageView::width(self) as usize
}
fn height(&self, _level: usize) -> usize{
GenericImageView::height(self) as usize
}
fn pitch_bytes(&self, _level: usize) -> usize{
Image::width(self, 0) * self.bytes_per_pixel()
}
fn bytes_per_pixel(&self) -> usize{
<Self as GenericImageView>::Pixel::CHANNEL_COUNT as usize
* mem::size_of::<<<Self as GenericImageView>::Pixel as Pixel>::Subpixel>()
}
fn pitch_components(&self, _level: usize) -> usize{
Image::width(self, 0)
}
fn data(&self) -> &[u8]{
match self{
image::DynamicImage::ImageLuma8(img) => img,
image::DynamicImage::ImageLumaA8(img) => img,
image::DynamicImage::ImageRgb8(img) => img,
image::DynamicImage::ImageRgba8(img) => img,
image::DynamicImage::ImageBgr8(img) => img,
image::DynamicImage::ImageBgra8(img) => img,
image::DynamicImage::ImageLuma16(img) => unsafe {
let pixels = img.as_ptr();
slice::from_raw_parts(pixels as *const u8, img.len() * mem::size_of::<u16>())
},
image::DynamicImage::ImageLumaA16(img) => unsafe {
let pixels = img.as_ptr();
slice::from_raw_parts(pixels as *const u8, img.len() * mem::size_of::<u16>())
},
image::DynamicImage::ImageRgb16(img) => unsafe {
let pixels = img.as_ptr();
slice::from_raw_parts(pixels as *const u8, img.len() * mem::size_of::<u16>())
},
image::DynamicImage::ImageRgba16(img) => unsafe {
let pixels = img.as_ptr();
slice::from_raw_parts(pixels as *const u8, img.len() * mem::size_of::<u16>())
},
}
}
fn levels(&self) -> usize{
1
}
fn mipmap(&self, level: usize) -> Option<&[u8]>{
if level == 0 {
Some(self.data())
}else{
None
}
}
fn is_gpu_compressed(&self) -> bool{
false
}
}