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
6832706
pub fn linearize(c: u8) -> u8 {
18
6832706
    LINEARIZE[usize::from(c)]
19
6832706
}
20

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

            
27
/// Processing loop of `map_unpremultiplied_components`. Extracted (and public) for benchmarking.
28
#[inline]
29
509
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
1018
    output_surface.modify(&mut |data, stride| {
36
9269221
        for (x, y, pixel) in Pixels::within(surface, bounds) {
37
9268712
            if pixel.a > 0 {
38
4635048
                let alpha = f64::from(pixel.a) / 255f64;
39

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

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

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

            
61
/// Applies the function to each pixel component after unpremultiplying.
62
509
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
509
    let (width, height) = (surface.width(), surface.height());
69
509
    let mut output_surface = ExclusiveImageSurface::new(width, height, new_type)?;
70
509
    map_unpremultiplied_components_loop(surface, &mut output_surface, bounds, f);
71

            
72
509
    output_surface.share()
73
509
}
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
228
pub fn unlinearize_surface(
89
    surface: &SharedImageSurface,
90
    bounds: IRect,
91
) -> Result<SharedImageSurface, cairo::Error> {
92
228
    assert_eq!(surface.surface_type(), SurfaceType::LinearRgb);
93

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