forked from graciegutman/vnc_server
/
imageTsts.go
138 lines (109 loc) · 3.26 KB
/
imageTsts.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
package main
import (
"image"
//"vnc/vnc"
//"io/ioutil"
//"time"
"fmt"
"vnc/vnc"
//"os"
)
// in a loop
// get X, Y value
// get color values at X, Y
// find deltas at each color value
// if deltas > or < 0, X becomes left bound, Y becomes upper and lower bound
// continue to loop
// if deltas > or < 0, and this X < old RX, RX = thisX if X > old LX, LX = thisX
// if deltas > or < 0, and this Y > old LY, move LY = thisY
var threshold int = 5000
type Point struct{
x, y int
}
type RectBounds struct{
leftb, upperb, rightb, lowerb Point
}
func (rb *RectBounds) setRight (x, y int) {
rb.rightb.x = x
rb.rightb.y = y
}
func (rb *RectBounds) setLeft (x, y int) {
rb.leftb.x = x
rb.leftb.y = y
}
func (rb *RectBounds) setUpper (x, y int) {
rb.upperb.x = x
rb.upperb.y = y
}
func (rb *RectBounds) setLower (x, y int) {
rb.lowerb.x = x
rb.lowerb.y = y
}
func abs(x uint32) uint32 {
switch {
case x < 0:
return -x
case x == 0:
return 0 // return correctly abs(-0)
}
return x
}
func pixDifference(x, y, threshold int, img1, img2 image.Image)(bool) {
t := uint32(threshold)
r1, g1, b1, a1 := img1.At(x, y).RGBA()
r2, g2, b2, a2 := img2.At(x, y).RGBA()
if abs(r2 - r1) > t || abs(g2 - g1) > t || abs(b2 - b1) > t || abs(a2 - a1) > t {
return true
}
return false
}
func findFirstPixel(rectArea int, img1, img2 image.Image, rect image.Rectangle, threshold int) *RectBounds {
count := 0
for ; count < rectArea; count++ {
x, y := vnc.FindXY(count, rect)
if pixDifference(x, y, threshold, img1, img2) {
fmt.Println("in loop iteration, %d", count)
rb := &RectBounds{}
rb.setLeft(x, y)
rb.setRight(x, y)
rb.setUpper(x, y)
rb.setLower(x, y)
return rb
}
}
return &RectBounds{}
}
func findRectBounds(rectArea int, img1, img2 image.Image, rb *RectBounds, rect image.Rectangle, threshold int) {
totalChanges := 0
count := 0
for ; count < rectArea; count++ {
x, y := vnc.FindXY(count, rect)
if pixDifference(x, y, threshold, img1, img2) {
totalChanges += 1
if x < rb.leftb.x {
fmt.Println("leftbound changed to", x, y)
rb.setLeft(x, y)
}else if x > rb.rightb.x {
fmt.Println("rightbound changed to", x, y)
rb.setRight(x, y)
}
if y > rb.lowerb.y {
fmt.Println("lowerbound changed to", x, y)
rb.setLower(x, y)
}
}
}
fmt.Println(totalChanges)
}
func main() {
// decode into image.images
PNG1, _ := vnc.DecodeFileToPNGtst("screen4.png")
PNG2, _ := vnc.DecodeFileToPNGtst("screen4.png")
// prelim rectangle bounds are X,Y value of first pixel that differs.
PNGRect := PNG1.Bounds()
rectArea := PNGRect.Dx() * PNGRect.Dy()
// establish rect RX, LX, UY, LY
rb := findFirstPixel(rectArea, PNG1, PNG2, PNGRect, threshold)
findRectBounds(rectArea, PNG1, PNG2, rb, PNGRect, threshold)
fmt.Println(rb.leftb.x, rb.rightb.x, rb.upperb.y, rb.lowerb.y)
}