forked from shawnridgeway/wfc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testutils.go
52 lines (48 loc) · 974 Bytes
/
testutils.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
package testutils
import (
"image"
"image/png"
"os"
)
func LoadImage(file string) (image.Image, error) {
raw, err := os.Open(file)
if err != nil {
return nil, err
}
defer raw.Close()
img, _, err := image.Decode(raw)
if err != nil {
return nil, err
}
return img, nil
}
func SaveImage(file string, img image.Image) error {
// Check if file exists, remove if it does
if _, err := os.Stat(file); err == nil {
err := os.Remove(file)
if err != nil {
return err
}
}
// Save new file
f, err := os.Create(file)
if err != nil {
return err
}
defer f.Close()
png.Encode(f, img)
return nil
}
func CompareImages(source, target image.Image) bool {
if target.Bounds().Max.X != source.Bounds().Max.X || target.Bounds().Max.Y != source.Bounds().Max.Y {
return false
}
for x := 0; x < target.Bounds().Max.X; x++ {
for y := 0; y < target.Bounds().Max.Y; y++ {
if target.At(x, y) != source.At(x, y) {
return false
}
}
}
return true
}