-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathdiff.go
159 lines (133 loc) · 3.23 KB
/
diff.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package diffimage
import (
"bytes"
"encoding/base64"
"image"
"image/color"
"io"
"log"
"strings"
"github.com/sergi/go-diff/diffmatchpatch"
)
type HashLines struct {
Lines []string
}
func writeUint32(w io.Writer, n uint32) {
w.Write([]byte{
byte((n >> 24) & 0xff),
byte((n >> 16) & 0xff),
byte((n >> 8) & 0xff),
byte((n >> 0) & 0xff),
})
}
func readUint32(r io.Reader) uint32 {
var bs [4]byte
r.Read(bs[:])
n := uint32(bs[0])
for i := 1; i < 4; i++ {
n = n << 8
n += uint32(bs[i])
}
return n
}
func encodeLine(img image.Image, y int) string {
buf := &bytes.Buffer{}
w := img.Bounds().Size().X
for x := 0; x < w; x++ {
r, g, b, a := img.At(x, y).RGBA()
writeUint32(buf, r)
writeUint32(buf, g)
writeUint32(buf, b)
writeUint32(buf, a)
}
return base64.RawURLEncoding.EncodeToString(buf.Bytes())
}
func decodeLine(s string) []color.Color {
bs, err := base64.RawURLEncoding.DecodeString(s)
if err != nil {
log.Fatal(err)
}
buf := bytes.NewBuffer(bs)
colors := make([]color.Color, 0)
for buf.Len() != 0 {
r := readUint32(buf)
g := readUint32(buf)
b := readUint32(buf)
a := readUint32(buf)
c := color.NRGBA64{uint16(r), uint16(g), uint16(b), uint16(a)}
colors = append(colors, c)
}
return colors
}
func disassembleDiffs(diffs []diffmatchpatch.Diff) []diffmatchpatch.Diff {
res := make([]diffmatchpatch.Diff, 0, len(diffs))
for _, diff := range diffs {
lines := strings.Split(diff.Text, "\n")
for _, line := range lines {
res = append(res, diffmatchpatch.Diff{Type: diff.Type, Text: line})
}
}
return res
}
func diff(from, to []string) []diffmatchpatch.Diff {
dmp := diffmatchpatch.New()
a, b, c := dmp.DiffLinesToChars(strings.Join(from, "\n"), strings.Join(to, "\n"))
diffs := dmp.DiffMain(a, b, false)
result := dmp.DiffCharsToLines(diffs, c)
return disassembleDiffs(result)
}
func blend(dst, src color.Color) color.Color {
srcR, srcG, srcB, srcA := src.RGBA()
dstR, dstG, dstB, _ := dst.RGBA()
srcAp := float64(srcA) / 0xffff
outR := float64(srcR)*srcAp + float64(dstR)*(1-srcAp)
outG := float64(srcG)*srcAp + float64(dstG)*(1-srcAp)
outB := float64(srcB)*srcAp + float64(dstB)*(1-srcAp)
return color.NRGBA64{
uint16(outR),
uint16(outG),
uint16(outB),
0xffff,
}
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func DiffImage(img1, img2 image.Image) image.Image {
sz1 := img1.Bounds().Size()
sz2 := img2.Bounds().Size()
w := max(sz1.X, sz2.X)
lines1 := make([]string, 0, sz1.Y)
for y := 0; y < sz1.Y; y++ {
lines1 = append(lines1, encodeLine(img1, y))
}
lines2 := make([]string, 0, sz2.Y)
for y := 0; y < sz2.Y; y++ {
lines2 = append(lines2, encodeLine(img2, y))
}
diffs := diff(lines1, lines2)
diffImg := image.NewRGBA(image.Rect(0, 0, w, len(diffs)))
for y, diff := range diffs {
colors := decodeLine(diff.Text)
switch diff.Type {
case diffmatchpatch.DiffDelete:
red := color.RGBA64{0xffff, 0, 0, 0x4000}
for x, c := range colors {
diffImg.Set(x, y, blend(c, red))
}
case diffmatchpatch.DiffInsert:
green := color.RGBA64{0, 0xffff, 0, 0x4000}
for x, c := range colors {
diffImg.Set(x, y, blend(c, green))
}
case diffmatchpatch.DiffEqual:
for x, c := range colors {
diffImg.Set(x, y, c)
}
}
}
return diffImg
}