Skip to content

Instantly share code, notes, and snippets.

@afq984
Created December 4, 2018 15:27
Show Gist options
  • Save afq984/4c792639b553a85589a4fe591e9d5b64 to your computer and use it in GitHub Desktop.
Save afq984/4c792639b553a85589a4fe591e9d5b64 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"image"
"image/png"
"os"
)
func mustReadImage(filename string) image.Image {
file, err := os.Open(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "error opening %s: %s\n", filename, err)
os.Exit(66)
}
img, err := png.Decode(file)
if err != nil {
fmt.Fprintf(os.Stderr, "error decoding %s: %s\n", filename, err)
os.Exit(66)
}
return img
}
func exitResult(passed bool) {
if !passed {
fmt.Println("Rejected")
os.Exit(66)
} else {
fmt.Println("Accepted")
os.Exit(0)
}
}
func main() {
if len(os.Args) != 3 {
fmt.Fprintf(os.Stderr, "usage: %s ref.png out.png\n", os.Args[0])
os.Exit(87)
}
img0 := mustReadImage(os.Args[1])
img1 := mustReadImage(os.Args[2])
rect0 := img0.Bounds()
rect1 := img1.Bounds()
if !rect0.Eq(rect1) {
fmt.Println("different shape", rect0, rect1)
exitResult(false)
}
total := rect0.Dx() * rect0.Dy()
same := 0
for x := rect0.Min.X; x < rect0.Max.X; x++ {
for y := rect0.Min.Y; y < rect0.Max.Y; y++ {
color0 := img0.At(x, y)
color1 := img1.At(x, y)
r0, g0, b0, a0 := color0.RGBA()
r1, g1, b1, a1 := color1.RGBA()
if r0 == r1 && g0 == g1 && b0 == b1 && a0 == a1 {
same++
}
}
}
fmt.Printf("%v/%v\n", same, total)
exitResult(same == total)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment