Last active
August 29, 2015 14:01
-
-
Save nholland94/9fd127b76a94905ea854 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func checkImageDiff(img image.Image) bool { | |
r, g, b, a := img.At(0, 0).RGBA() | |
baseColor := [4]uint32{r, g, b, a} | |
bounds := img.Bounds() | |
results := make(chan bool) | |
quit := make(chan bool) | |
isSameColor := func(r, g, b, a uint32) bool { | |
return baseColor[0] == r && baseColor[1] == g && baseColor[2] == b && baseColor[3] == a | |
} | |
for y := bounds.Min.Y; y < bounds.Max.Y; y++ { | |
go func() { | |
matching := true | |
for x := bounds.Min.X; x < bounds.Max.X; x++ { | |
// Have to be renamed so that it can acquire | |
// new memory address for each goroutine | |
// and not all use the memory address shared | |
// by the r, g, b, a closure | |
nr, ng, nb, na := img.At(x, y).RGBA() | |
if !isSameColor(nr, ng, nb, na) { | |
matching = false | |
break | |
} | |
select { | |
case <-quit: | |
return | |
default: | |
continue | |
} | |
} | |
results <- matching | |
}() | |
} | |
for y := bounds.Min.Y; y < bounds.Max.Y; y++ { | |
result := <-results | |
if !result { | |
quit <- true | |
return false | |
} | |
} | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment