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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use super::image::RGBAPLU;
use rgb::*;
use rgb::alt::*;
extern crate lodepng;

/// See `GammaPixel` & `ToRGBAPLU`
pub trait GammaComponent {
    fn max_value() -> usize;
    fn to_linear(&self, lut: &[f32]) -> f32;
}

/// Downsampling should be done in linear RGB color space.
///
/// Used by `ToRGBAPLU`
///
/// This trait provides gamma to linear conversion via lookup table,
/// and there's implementation for sRGB for common RGB types.
pub trait GammaPixel {
    type Component: GammaComponent;
    type Output;

    fn to_linear(&self, gamma_lut: &[f32]) -> Self::Output;

    fn make_lut() -> Vec<f32> {
        (0..Self::Component::max_value() + 1)
            .map(|i| to_linear(i as f32 / Self::Component::max_value() as f32))
            .collect()
    }
}

fn to_linear(s: f32) -> f32 {
    if s <= 0.04045 {
        s / 12.92
    } else {
        ((s + 0.055) / 1.055).powf(2.4)
    }
}

/// RGBA Premultiplied Linear-light Unit scale
///
/// Convenience function `.to_rgbaplu()` to convert RGBA bitmaps to a format useful for DSSIM.
pub trait ToRGBAPLU {
    fn to_rgbaplu(&self) -> Vec<RGBAPLU>;
}

/// Grayscale Linear-light Unit scale
pub trait ToGLU {
    fn to_glu(&self) -> Vec<f32>;
}

impl GammaComponent for u8 {
    fn max_value() -> usize { 255 }
    fn to_linear(&self, lut: &[f32]) -> f32 {
        lut[*self as usize]
    }
}

impl GammaComponent for u16 {
    fn max_value() -> usize { 65535 }
    fn to_linear(&self, lut: &[f32]) -> f32 {
        lut[*self as usize]
    }
}

impl<M> ToGLU for [M] where M: GammaPixel<Output=f32> {
    fn to_glu(&self) -> Vec<f32> {
        let gamma_lut = M::make_lut();
        self.iter().map(|px| px.to_linear(&gamma_lut)).collect()
    }
}

impl<M> GammaPixel for RGBA<M> where M: Clone + Into<f32> + GammaComponent {
    type Component = M;
    type Output = RGBAPLU;
    fn to_linear(&self, gamma_lut: &[f32]) -> RGBAPLU {
        let a_unit = self.a.clone().into() / M::max_value() as f32;
        RGBAPLU {
            r: self.r.to_linear(gamma_lut) * a_unit,
            g: self.g.to_linear(gamma_lut) * a_unit,
            b: self.b.to_linear(gamma_lut) * a_unit,
            a: a_unit,
        }
    }
}

impl<M> GammaPixel for BGRA<M> where M: Clone + Into<f32> + GammaComponent {
    type Component = M;
    type Output = RGBAPLU;
    fn to_linear(&self, gamma_lut: &[f32]) -> RGBAPLU {
        let a_unit = self.a.clone().into() / M::max_value() as f32;
        RGBAPLU {
            r: self.r.to_linear(gamma_lut) * a_unit,
            g: self.g.to_linear(gamma_lut) * a_unit,
            b: self.b.to_linear(gamma_lut) * a_unit,
            a: a_unit,
        }
    }
}

impl<M> GammaPixel for RGB<M> where M: GammaComponent {
    type Component = M;
    type Output = RGBAPLU;
    fn to_linear(&self, gamma_lut: &[f32]) -> RGBAPLU {
        RGBAPLU {
            r: self.r.to_linear(gamma_lut),
            g: self.g.to_linear(gamma_lut),
            b: self.b.to_linear(gamma_lut),
            a: 1.0,
        }
    }
}

impl<M> GammaPixel for BGR<M> where M: GammaComponent {
    type Component = M;
    type Output = RGBAPLU;
    fn to_linear(&self, gamma_lut: &[f32]) -> RGBAPLU {
        RGBAPLU {
            r: self.r.to_linear(gamma_lut),
            g: self.g.to_linear(gamma_lut),
            b: self.b.to_linear(gamma_lut),
            a: 1.0,
        }
    }
}

impl<M> GammaPixel for lodepng::GreyAlpha<M> where M: Copy + Clone + Into<f32> + GammaComponent {
    type Component = M;
    type Output = RGBAPLU;
    fn to_linear(&self, gamma_lut: &[f32]) -> RGBAPLU {
        let a_unit = self.1.clone().into() / M::max_value() as f32;
        let g = self.0.to_linear(gamma_lut);
        RGBAPLU {
            r: g * a_unit,
            g: g * a_unit,
            b: g * a_unit,
            a: a_unit,
        }
    }
}

impl<M> GammaPixel for M where M: GammaComponent {
    type Component = M;
    type Output = f32;
    fn to_linear(&self, gamma_lut: &[f32]) -> f32 {
        self.to_linear(gamma_lut)
    }
}

impl<M> GammaPixel for lodepng::Grey<M> where M: Copy + GammaComponent {
    type Component = M;
    type Output = RGBAPLU;
    fn to_linear(&self, gamma_lut: &[f32]) -> RGBAPLU {
        let g = self.0.to_linear(gamma_lut);
        RGBAPLU {
            r: g,
            g: g,
            b: g,
            a: 1.0,
        }
    }
}

impl<P> ToRGBAPLU for [P] where P: GammaPixel<Output=RGBAPLU> {
    fn to_rgbaplu(&self) -> Vec<RGBAPLU> {
        let gamma_lut = P::make_lut();
        self.iter().map(|px| px.to_linear(&gamma_lut)).collect()
    }
}