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
//! Pixel formats types and function manipulation.
//!
//! The `Pixel` trait is used to reify a pixel type at runtime via `PixelFormat`.

/// Reify a static pixel format to runtime.
pub trait Pixel {
  /// Encoding of a single pixel. It should match the `PixelFormat` mapping.
  type Encoding;

  fn pixel_format() -> PixelFormat;
}

/// Constraint on `Pixel` for color ones.
pub trait ColorPixel: Pixel {}

/// Constraint on `Pixel` for depth ones.
pub trait DepthPixel: Pixel {}

/// A `PixelFormat` gathers a `Type` along with a `Format`.
#[derive(Clone, Copy, Debug)]
pub struct PixelFormat {
    pub encoding: Type
  , pub format: Format
}

/// Pixel type.
#[derive(Clone, Copy, Debug)]
pub enum Type {
  Integral,
  Unsigned,
  Floating
}

/// Format of a pixel.
///
/// Whichever the constructor you choose, the carried `u8`s represents how many bytes are used to
/// represent each channel.
#[derive(Clone, Copy, Debug)]
pub enum Format {
    /// Holds a red-only channel.
  R(u8),
    /// Holds red and green channels. 
  RG(u8, u8),
  /// Holds red, green and blue channels.
  RGB(u8, u8, u8),
  /// Holds red, green, blue and alpha channels.
  RGBA(u8, u8, u8, u8),
  /// Holds a depth channel.
  Depth(u8)
}

/// Does a `PixelFormat` represent a color?
pub fn is_color_pixel(f: PixelFormat) -> bool {
  match f.format {
      Format::Depth(_) => false
    , _ => true
  }
}

/// Does a `PixelFormat` represent depth information?
pub fn is_depth_pixel(f: PixelFormat) -> bool {
  !is_color_pixel(f)
}

/// A red, green and blue 8-bit unsigned pixel format.
#[derive(Clone, Copy, Debug)]
pub struct RGB8UI;

impl Pixel for RGB8UI {
  type Encoding = (u8, u8, u8);

  fn pixel_format() -> PixelFormat {
    PixelFormat {
        encoding: Type::Unsigned
      , format: Format::RGB(8, 8, 8)
    }
  }
}

impl ColorPixel for RGB8UI {}

/// A red, green, blue and alpha 8-bit unsigned pixel format.
#[derive(Clone, Copy, Debug)]
pub struct RGBA8UI;

impl Pixel for RGBA8UI {
  type Encoding = (u8, u8, u8, u8);

  fn pixel_format() -> PixelFormat {
    PixelFormat {
        encoding: Type::Unsigned
      , format: Format::RGBA(8, 8, 8, 8)
    }
  }
}

impl ColorPixel for RGBA8UI {}

/// A red, green and blue 32-bit floating pixel format.
#[derive(Clone, Copy, Debug)]
pub struct RGB32F;

impl Pixel for RGB32F {
  type Encoding = (f32, f32, f32);

  fn pixel_format() -> PixelFormat {
    PixelFormat {
        encoding: Type::Floating
      , format: Format::RGB(32, 32, 32)
    }
  }
}

impl ColorPixel for RGB32F {}

/// A red, green, blue and alpha 32-bit floating pixel format.
#[derive(Clone, Copy, Debug)]
pub struct RGBA32F;

impl Pixel for RGBA32F {
  type Encoding = (f32, f32, f32, f32);

  fn pixel_format() -> PixelFormat {
    PixelFormat {
        encoding: Type::Floating
      , format: Format::RGBA(32, 32, 32, 32)
    }
  }
}

impl ColorPixel for RGBA32F {}

/// A depth 32-bit floating pixel format.
#[derive(Clone, Copy, Debug)]
pub struct Depth32F;

impl Pixel for Depth32F {
  type Encoding = f32;

  fn pixel_format() -> PixelFormat {
    PixelFormat {
        encoding: Type::Floating
      , format: Format::Depth(32)
    }
  }
}

impl DepthPixel for RGBA32F {}