From 398c52f8a5a2b94364dd44a984a1004d9de22fe7 Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Sun, 8 May 2016 17:33:51 +0200 Subject: [PATCH] Removed unnecessary general code. --- common/basetilecreator.go | 5 ++--- common/basetilehash.go | 4 +--- common/image.go | 39 ++------------------------------------- 3 files changed, 5 insertions(+), 43 deletions(-) diff --git a/common/basetilecreator.go b/common/basetilecreator.go index 1b290f5..a53ba05 100644 --- a/common/basetilecreator.go +++ b/common/basetilecreator.go @@ -5,7 +5,6 @@ package common import ( - "image" "image/color" "io/ioutil" "log" @@ -50,7 +49,7 @@ var tileDepths = [...][2]int16{ var BackgroundColor = color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff} -type BaseTileUpdateFunc func(x, y int, img image.Image) bool +type BaseTileUpdateFunc func(x, y int, hash []byte) bool type BaseTileCreator struct { client *RedisClient @@ -165,7 +164,7 @@ func (btc *BaseTileCreator) CreateTile(x, z int16, i, j int) (bool, error) { return true, SaveAsPNG(path, image) } - if btc.update(i, j, image) { + if btc.update(i, j, SHA1Image(image)) { log.Printf("Writing (%d, %d) to file %s.\n", x, z, path) return true, SaveAsPNGAtomic(path, image) } diff --git a/common/basetilehash.go b/common/basetilehash.go index 19e0146..4adbefa 100644 --- a/common/basetilehash.go +++ b/common/basetilehash.go @@ -6,7 +6,6 @@ package common import ( "bytes" - "image" "sync" ) @@ -20,8 +19,7 @@ func NewBaseTileHash() *BaseTileHash { return &BaseTileHash{hashes: map[struct{ x, y int }][]byte{}} } -func (bth *BaseTileHash) Update(x, y int, img image.Image) bool { - hash := SHA1Image(img) +func (bth *BaseTileHash) Update(x, y int, hash []byte) bool { key := struct{ x, y int }{x, y} bth.Lock() defer bth.Unlock() diff --git a/common/image.go b/common/image.go index 45f2a01..c97ab5f 100644 --- a/common/image.go +++ b/common/image.go @@ -112,7 +112,8 @@ func LoadPNG(path string, bg color.RGBA) image.Image { return img } -func sha1rgba(img *image.RGBA) []byte { +func SHA1Image(img *image.RGBA) []byte { + hash := sha1.New() w, h := img.Rect.Dx()*4, img.Rect.Dy() @@ -124,39 +125,3 @@ func sha1rgba(img *image.RGBA) []byte { return hash.Sum(nil) } - -func sha1uniform(img *image.Uniform) []byte { - r, g, b, a := img.C.RGBA() - return sha1.New().Sum([]byte{ - byte(r >> 16), - byte(g >> 16), - byte(b >> 16), - byte(a >> 16)}) -} - -func SHA1Image(img image.Image) []byte { - - switch i := img.(type) { - case *image.RGBA: - return sha1rgba(i) - case *image.Uniform: - return sha1uniform(i) - } - - log.Println("WARN: slow path for SHA1Image") - - hash := sha1.New() - bounds := img.Bounds() - var pix [4]byte - for x := bounds.Min.X; x <= bounds.Max.X; x++ { - for y := bounds.Min.Y; y <= bounds.Max.Y; y++ { - r, g, b, a := img.At(x, y).RGBA() - pix[0] = byte(r >> 16) - pix[1] = byte(g >> 16) - pix[2] = byte(b >> 16) - pix[3] = byte(a >> 16) - hash.Write(pix[:]) - } - } - return hash.Sum(nil) -}