-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfiffrgb.go
157 lines (143 loc) · 3.22 KB
/
dfiffrgb.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
package main
import (
"image"
"image/draw"
"image/png"
"log"
"os"
"sync"
)
func readRGB(img image.Image) []uint32 {
dx := img.Bounds().Dx()
dy := img.Bounds().Dy()
mx := img.Bounds().Min.X
my := img.Bounds().Min.Y
buf := make([]uint32, dx*dy*3)
for y := 0; y < dy; y++ {
for x := 0; x < dx; x++ {
p := (x*y + x) * 3
r, g, b, _ := img.At(mx+x, my+y).RGBA()
// fmt.Println(x, y, r, g, b)
buf[p] = r
buf[p+1] = g
buf[p+2] = b
}
}
// fmt.Println(img)
// fmt.Println(buf)
return buf
}
func diffRGB(left, right []uint32) float64 {
j := float64(0)
for i := 0; i < len(left); i++ {
if left[i] == right[i] {
j++
}
}
return j / float64(len(left))
}
func puzzleDiffRGB(source, part, output string) error {
log.Println("[+] puzzleDiffRGB")
_, pWidth, pHeight := readImage(part)
imgSrc, sWidth, sHeight := readImage(source)
m = intRound(sWidth / pWidth)
n = intRound(sHeight / pHeight)
log.Printf("[+] m = %d, n = %d\n", m, n)
// 原图 - 切割 - 哈希
imgMatrix = make([][]*ImageMatrixData, m, m)
for i := 0; i < m; i++ {
imgMatrix[i] = make([]*ImageMatrixData, n, n)
}
log.Printf("[+] Cut and Hash Source Image\n")
for x := 0; x < m; x++ {
for y := 0; y < n; y++ {
subImg, err := clipImage(imgSrc, x*pWidth, y*pHeight, (x+1)*pWidth, (y+1)*pHeight)
if err != nil {
log.Println("[-] clipImage:", err)
}
buff := readRGB(subImg)
imgMatrix[x][y] = &ImageMatrixData{
X: x * pWidth, Y: y * pHeight,
Img: subImg, Buff: buff,
}
}
}
wg := &sync.WaitGroup{}
// 碎片 - 哈希
log.Printf("[+] Hash Part Image\n")
pathDir := getParentDirectory(part)
parts, _ := getAllParts(pathDir)
flagMatrix = make([]*ImageMatrixData, len(parts))
for n, partPath := range parts {
wg.Add(1)
go func(wg *sync.WaitGroup, n int, partPath string) {
defer wg.Done()
imgFile, err := os.Open(partPath)
if err != nil {
log.Println(n, partPath, err)
}
defer imgFile.Close()
img, _ := png.Decode(imgFile)
buff := readRGB(img)
flagMatrix[n] = &ImageMatrixData{
Name: partPath, Buff: buff, Img: img,
}
}(wg, n, partPath)
if n%100 == 0 {
wg.Wait()
}
}
wg.Wait()
extBlock := image.NewNRGBA(image.Rect(0, 0, pWidth, pHeight))
// TODO: color form extColor
draw.Draw(extBlock, extBlock.Bounds(), image.Black, image.ZP, draw.Src)
// blockDistance := float64(pWidth*pHeight) * 0.8
var ch = make(chan bool, 1)
wg.Add(1)
go func() {
defer wg.Done()
p := 0
for {
select {
case <-ch:
p++
log.Println("[+] finish :", p, "/", m)
if p == m {
close(ch)
return
}
}
}
}()
// Compare Image
log.Printf("[+] Compare Image\n")
for x := 0; x < m; x++ {
imX := imgMatrix[x]
wg.Add(1)
go func(x int, imX []*ImageMatrixData) {
defer wg.Done()
for y := 0; y < n; y++ {
distance := float64(0)
im := imX[y]
for _, fm := range flagMatrix {
similar := diffRGB(im.Buff, fm.Buff)
if similar > distance {
distance = similar
im.Img = fm.Img
im.Name = fm.Name
}
}
}
ch <- true
}(x, imX)
}
wg.Wait()
log.Println("[+] Finish Compare")
// Merge Image
log.Printf("[+] Merge Image\n")
err := mergeImage(imgMatrix, output, sWidth, sHeight, m, n)
if err != nil {
return err
}
return nil
}