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
469109
fn emphasize(p: &Pixel) -> Pixel {
43
2348533
    let emphasize_component = |c| {
44
        // emphasize
45
1879424
        let mut c = c as u32 * 4;
46
        // make sure it's visible
47
1879424
        if c > 0 {
48
731117
            c += 128;
49
        }
50
1879424
        c.min(255) as u8
51
1879424
    };
52
469109
    p.map(emphasize_component)
53
469109
}
54

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

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

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

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

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

            
75
810
    let black = Pixel::default().alpha(255);
76

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

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

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

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

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

            
90
471024
                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
465367
                    pixel_diff.alpha(255)
96
                }
97
            } else {
98
32993738
                black
99
            };
100

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

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

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