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
extern crate freeimage;

use ::Image;

use gl::types::*;
use gl;

impl Image for freeimage::Bitmap{
    type DataType = u8;

    #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    fn gl_internal_format_type(&self) -> Option<(GLenum, GLenum, GLenum)>{
        let bytes_pp = self.bpp()/8;
        match self.ty(){
            freeimage::Type::BITMAP => {
                let (internal, format) = match bytes_pp{
                    0 | 1 => (gl::R8, gl::RED),
                    2 => (gl::RG8, gl::RG),
                    3 => (gl::RGB8, gl::BGR),
                    4 => (gl::RGBA8, gl::BGRA),
                    _ => panic!("non recognized image depth {}", bytes_pp),
                };
                Some((internal, format, gl::UNSIGNED_BYTE))
            },
            freeimage::Type::UINT16 => Some((gl::R16, gl::RED, gl::UNSIGNED_SHORT)),
            freeimage::Type::INT16 =>  Some((gl::R16, gl::RED, gl::SHORT)),
            freeimage::Type::RGB16 =>  Some((gl::RGB16, gl::RGB, gl::UNSIGNED_SHORT)),
            freeimage::Type::UINT32 => Some((gl::R32UI, gl::RED, gl::UNSIGNED_INT)),
            freeimage::Type::INT32 =>  Some((gl::R32I, gl::RED, gl::INT)),
            freeimage::Type::FLOAT =>  Some((gl::R32F, gl::RED, gl::FLOAT)),
            freeimage::Type::RGBA16 => Some((gl::RGBA16, gl::RGBA, gl::UNSIGNED_SHORT)),
            freeimage::Type::RGBF =>   Some((gl::RGB32F, gl::RGB, gl::FLOAT)),
            freeimage::Type::RGBAF =>  Some((gl::RGB32F, gl::RGBA, gl::FLOAT)),
            _ => None
        }
    }


    #[cfg(any(feature = "gles", feature="webgl"))]
    fn gl_internal_format_type(&self) -> Option<(GLenum, GLenum, GLenum)> {
        let bytes_pp = self.bpp()/8;
        match self.ty(){
            freeimage::Type::BITMAP => {
                let (internal, format) = match bytes_pp{
                    0 | 1 => (gl::R8, gl::RED),
                    2 => (gl::RG8, gl::RG),
                    3 => (gl::RGB8, gl::RGB), //TODO: This will probably show up as BGR
                    4 => (gl::RGBA8, gl::BGRA_EXT),
                    _ => panic!("non recognized image depth"),
                };
                Some((internal, format, gl::UNSIGNED_BYTE))
            },
            freeimage::Type::UINT16 => Some((gl::R16UI, gl::RED, gl::UNSIGNED_SHORT)),
            freeimage::Type::INT16 =>  Some((gl::R16I, gl::RED, gl::SHORT)),
            freeimage::Type::RGB16 =>  Some((gl::RGB16UI, gl::RGB, gl::UNSIGNED_SHORT)),
            freeimage::Type::UINT32 => Some((gl::R32UI, gl::RED, gl::UNSIGNED_INT)),
            freeimage::Type::INT32 =>  Some((gl::R32I, gl::RED, gl::INT)),
            freeimage::Type::FLOAT =>  Some((gl::R32F, gl::RED, gl::FLOAT)),
            freeimage::Type::RGBA16 => Some((gl::RGBA16UI, gl::RGBA, gl::UNSIGNED_SHORT)),
            freeimage::Type::RGBF =>   Some((gl::RGB32F, gl::RGB, gl::FLOAT)),
            freeimage::Type::RGBAF =>  Some((gl::RGB32F, gl::RGBA, gl::FLOAT)),
            _ => None
        }
    }

    fn width(&self, _level: usize) -> usize{
        self.width()
    }

    fn height(&self, _level: usize) -> usize{
        self.height()
    }

    fn pitch_bytes(&self, _level: usize) -> usize{
        self.pitch()
    }

    fn bytes_per_pixel(&self) -> usize{
        self.bpp()/8
    }

    fn pitch_components(&self, _level: usize) -> usize{
        let bytes_pp = self.bpp() / 8;
        self.pitch() / bytes_pp.max(1)
    }

    fn num_components(&self) -> usize{
        let bytes_pp = self.bpp()/8;
        match self.ty(){
            freeimage::Type::BITMAP => bytes_pp,
            freeimage::Type::UINT16 => 1,
            freeimage::Type::INT16 =>  1,
            freeimage::Type::RGB16 =>  3,
            freeimage::Type::UINT32 => 1,
            freeimage::Type::INT32 =>  1,
            freeimage::Type::FLOAT =>  1,
            freeimage::Type::DOUBLE =>  1,
            freeimage::Type::RGBA16 => 4,
            freeimage::Type::RGBF =>   3,
            freeimage::Type::RGBAF =>  4,
            freeimage::Type::UNKNOWN | freeimage::Type::COMPLEX => panic!()
        }
    }

    fn data(&self) -> &[u8]{
        self.bits()
    }

    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
    }
}