1
use std::fmt;
2

            
3
use crate::surface_utils::{
4
    iterators::Pixels,
5
    shared_surface::{SharedImageSurface, SurfaceType},
6
    ImageSurfaceDataExt, Pixel, PixelOps,
7
};
8

            
9
use rgb::{ComponentMap, RGB};
10

            
11
pub enum BufferDiff {
12
    DifferentSizes,
13
    Diff(Diff),
14
}
15

            
16
pub struct Diff {
17
    pub num_pixels_changed: usize,
18
    pub max_diff: u8,
19
    pub surface: SharedImageSurface,
20
}
21

            
22
impl fmt::Display for BufferDiff {
23
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24
        match self {
25
            BufferDiff::DifferentSizes => write!(f, "different sizes"),
26
            BufferDiff::Diff(diff) => diff.fmt(f),
27
        }
28
    }
29
}
30

            
31
impl fmt::Display for Diff {
32
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33
        write!(
34
            f,
35
            "{} pixels are different, with a maximum difference of {}",
36
            self.num_pixels_changed, self.max_diff
37
        )
38
    }
39
}
40

            
41
#[inline]
42
498045
fn emphasize(p: &Pixel) -> Pixel {
43
2476608
    let emphasize_component = |c| {
44
        // emphasize
45
1978563
        let mut c = c as u32 * 4;
46
        // make sure it's visible
47
1978563
        if c > 0 {
48
784693
            c += 128;
49
        }
50
1978563
        c.min(255) as u8
51
1978563
    };
52
498045
    p.map(emphasize_component)
53
498045
}
54

            
55
36691734
pub fn compare_surfaces(
56
    surf_a: &SharedImageSurface,
57
    surf_b: &SharedImageSurface,
58
) -> Result<BufferDiff, cairo::Error> {
59
36691734
    let a_width = surf_a.width();
60
36691734
    let a_height = surf_a.height();
61

            
62
36691734
    let b_width = surf_b.width();
63
36691734
    let b_height = surf_b.height();
64

            
65
36691734
    if a_width != b_width || a_height != b_height {
66
        return Ok(BufferDiff::DifferentSizes);
67
    }
68

            
69
36691734
    let mut surf_diff = cairo::ImageSurface::create(cairo::Format::ARgb32, a_width, a_height)?;
70
36691734
    let diff_stride = surf_diff.stride() as usize;
71

            
72
814
    let mut num_pixels_changed = 0;
73
814
    let mut max_diff = 0;
74

            
75
814
    let black = Pixel::default().with_alpha(255);
76

            
77
    {
78
814
        let mut diff_data = surf_diff.data().unwrap();
79

            
80
63795313
        for ((xa, ya, pixel_a), (_, _, pixel_b)) in Pixels::new(surf_a).zip(Pixels::new(surf_b)) {
81
45439527
            let dest = if pixel_a != pixel_b {
82
504050
                num_pixels_changed += 1;
83

            
84
496314
                let pixel_diff = pixel_a.diff(&pixel_b);
85

            
86
2479302
                max_diff = pixel_diff.iter().fold(max_diff, |acc, c| acc.max(c));
87

            
88
498121
                let pixel_diff = emphasize(&pixel_diff);
89

            
90
499367
                if pixel_diff.rgb() == RGB::default() {
91
                    // alpha only difference; convert alpha to gray
92
4399
                    let a = pixel_diff.a;
93
17596
                    pixel_diff.map_rgb(|_| a)
94
                } else {
95
491079
                    pixel_diff.with_alpha(255)
96
                }
97
            } else {
98
30164413
                black
99
            };
100

            
101
30664238
            diff_data.set_pixel(diff_stride, dest, xa, ya);
102
        }
103
814
    }
104

            
105
814
    let surface = SharedImageSurface::wrap(surf_diff, SurfaceType::SRgb)?;
106

            
107
814
    Ok(BufferDiff::Diff(Diff {
108
814
        num_pixels_changed,
109
814
        max_diff,
110
        surface,
111
    }))
112
814
}