-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
69 lines (55 loc) · 1.13 KB
/
main.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
package main
import (
"flag"
"fmt"
"image"
"image/png"
"log"
"os"
diffimage "github.com/murooka/go-diff-image"
)
func mustOpen(filename string) *os.File {
f, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
return f
}
func mustLoadImage(filename string) image.Image {
f := mustOpen(filename)
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
log.Fatal(err)
}
return img
}
func mustSaveImage(img image.Image, output string) {
f, err := os.OpenFile(output, os.O_WRONLY|os.O_CREATE, 0644)
defer f.Close()
if err != nil {
log.Fatal(err)
}
png.Encode(f, img)
}
func rate16(c uint32, r float64) uint16 {
fc := float64(c)
return uint16(65535 - (65535-fc)*r)
}
func main() {
var (
output string
)
flag.StringVar(&output, "output", "diff.png", "output filename")
flag.StringVar(&output, "o", "diff.png", "output filename")
flag.Parse()
args := flag.Args()
if len(args) != 2 {
fmt.Println("usage: imagediff [<option>...] <image1> <image2>")
os.Exit(1)
}
img1 := mustLoadImage(args[0])
img2 := mustLoadImage(args[1])
dst := diffimage.DiffImage(img1, img2)
mustSaveImage(dst, output)
}