-
Notifications
You must be signed in to change notification settings - Fork 1
/
screenshot.go
51 lines (44 loc) · 1.45 KB
/
screenshot.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
package screenshot
import (
"image"
"image/color"
// Packages image/gif, image/jpeg and image/png are not used explicitly in the code below,
// but are imported for its initialization side-effect, which allows
// image.Decode to understand these formatted images.
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"log"
)
// MinColorPercent is a lower bound for one particular color in percent to total number of colors in image
var MinColorPercent = 0.2
// MaxColorPercent is a upper bound for one particular color in percent to total number of colors in image
var MaxColorPercent = 0.4
// MinColorMapSize is a lower bound for size of color map
var MinColorMapSize = 1000
//Detect reports whether an Image m is screenshot
func Detect(m image.Image) bool {
res := false
colormap := make(map[color.Color]int)
bounds := m.Bounds()
total := (bounds.Max.X - bounds.Min.X) * (bounds.Max.Y - bounds.Min.Y)
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
color := m.At(x, y)
colormap[color]++
}
}
colormapSize := len(colormap)
for c, n := range colormap {
percent := float64(n) / float64(total)
if percent > MaxColorPercent {
log.Printf("Map size = %d; Color:%v, percent = %.4f", colormapSize, c, percent)
return true
}
if percent > MinColorPercent && colormapSize < MinColorMapSize {
log.Printf("Map size = %d; Color:%v, percent = %.4f", colormapSize, c, percent)
return true
}
}
return res
}