-
Notifications
You must be signed in to change notification settings - Fork 4
/
binary.go
69 lines (62 loc) · 1.7 KB
/
binary.go
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
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package imgdiff
import (
"image"
"image/color"
)
type binary struct{}
// NewBinary creates a new Differ based on simple binary algorithm.
func NewBinary() Differ {
return &binary{}
}
// Compare compares a and b using binary comparison.
func (d *binary) Compare(a, b image.Image) (image.Image, int, error) {
ab, bb := a.Bounds(), b.Bounds()
w, h := ab.Dx(), ab.Dy()
if w != bb.Dx() || h != bb.Dy() {
return nil, -1, ErrSize
}
diff := image.NewNRGBA(image.Rect(0, 0, w, h))
n := 0
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
d := diffColor(a.At(ab.Min.X+x, ab.Min.Y+y), b.At(bb.Min.X+x, bb.Min.Y+y))
c := color.RGBA{0, 0, 0, 0xff}
if d > 0 {
c.R = 0xff
//c.A = uint8(100 + d*0xff/0xffff)
n++
}
diff.Set(x, y, c)
}
}
return diff, n, nil
}
func diffColor(c1, c2 color.Color) int64 {
r1, g1, b1, a1 := c1.RGBA()
r2, g2, b2, a2 := c2.RGBA()
var diff int64
diff += abs(int64(r1) - int64(r2))
diff += abs(int64(g1) - int64(g2))
diff += abs(int64(b1) - int64(b2))
diff += abs(int64(a1) - int64(a2))
return diff
}
func abs(x int64) int64 {
if x < 0 {
return -x
}
return x
}