1
//! Utility functions for dealing with sRGB colors.
2
//!
3
//! The constant values in this module are taken from <http://www.color.org/chardata/rgb/srgb.xalter>
4

            
5
use crate::rect::IRect;
6
use crate::surface_utils::{
7
    iterators::Pixels,
8
    shared_surface::{ExclusiveImageSurface, SharedImageSurface, SurfaceType},
9
    ImageSurfaceDataExt, Pixel,
10
};
11

            
12
// Include the linearization and unlinearization tables.
13
include!(concat!(env!("OUT_DIR"), "/srgb-codegen.rs"));
14

            
15
/// Converts an sRGB color value to a linear sRGB color value (undoes the gamma correction).
16
#[inline]
17
6537706
pub fn linearize(c: u8) -> u8 {
18
6537706
    LINEARIZE[usize::from(c)]
19
6537706
}
20

            
21
/// Converts a linear sRGB color value to a normal sRGB color value (applies the gamma correction).
22
#[inline]
23
5467697
pub fn unlinearize(c: u8) -> u8 {
24
5467697
    UNLINEARIZE[usize::from(c)]
25
5467697
}
26

            
27
/// Processing loop of `map_unpremultiplied_components`. Extracted (and public) for benchmarking.
28
#[inline]
29
508
pub fn map_unpremultiplied_components_loop<F: Fn(u8) -> u8>(
30
    surface: &SharedImageSurface,
31
    output_surface: &mut ExclusiveImageSurface,
32
    bounds: IRect,
33
    f: F,
34
) {
35
1016
    output_surface.modify(&mut |data, stride| {
36
9363048
        for (x, y, pixel) in Pixels::within(surface, bounds) {
37
9362540
            if pixel.a > 0 {
38
4650127
                let alpha = f64::from(pixel.a) / 255f64;
39

            
40
16931907
                let compute = |x| {
41
12281780
                    let x = f64::from(x) / alpha; // Unpremultiply alpha.
42
12281780
                    let x = (x + 0.5) as u8; // Round to nearest u8.
43
12281780
                    let x = f(x);
44
12281780
                    let x = f64::from(x) * alpha; // Premultiply alpha again.
45
12281780
                    (x + 0.5) as u8
46
12281780
                };
47

            
48
4650127
                let output_pixel = Pixel {
49
4650127
                    r: compute(pixel.r),
50
4650127
                    g: compute(pixel.g),
51
4650127
                    b: compute(pixel.b),
52
4650127
                    a: pixel.a,
53
                };
54

            
55
4650127
                data.set_pixel(stride, output_pixel, x, y);
56
            }
57
        }
58
508
    });
59
508
}
60

            
61
/// Applies the function to each pixel component after unpremultiplying.
62
508
fn map_unpremultiplied_components<F: Fn(u8) -> u8>(
63
    surface: &SharedImageSurface,
64
    bounds: IRect,
65
    f: F,
66
    new_type: SurfaceType,
67
) -> Result<SharedImageSurface, cairo::Error> {
68
508
    let (width, height) = (surface.width(), surface.height());
69
508
    let mut output_surface = ExclusiveImageSurface::new(width, height, new_type)?;
70
508
    map_unpremultiplied_components_loop(surface, &mut output_surface, bounds, f);
71

            
72
508
    output_surface.share()
73
508
}
74

            
75
/// Converts an sRGB surface to a linear sRGB surface (undoes the gamma correction).
76
#[inline]
77
281
pub fn linearize_surface(
78
    surface: &SharedImageSurface,
79
    bounds: IRect,
80
) -> Result<SharedImageSurface, cairo::Error> {
81
281
    assert_eq!(surface.surface_type(), SurfaceType::SRgb);
82

            
83
281
    map_unpremultiplied_components(surface, bounds, linearize, SurfaceType::LinearRgb)
84
281
}
85

            
86
/// Converts a linear sRGB surface to a normal sRGB surface (applies the gamma correction).
87
#[inline]
88
227
pub fn unlinearize_surface(
89
    surface: &SharedImageSurface,
90
    bounds: IRect,
91
) -> Result<SharedImageSurface, cairo::Error> {
92
227
    assert_eq!(surface.surface_type(), SurfaceType::LinearRgb);
93

            
94
227
    map_unpremultiplied_components(surface, bounds, unlinearize, SurfaceType::SRgb)
95
227
}