Jelajahi Sumber

Speed up calculation by avoiding dynamic casting

jst 13 tahun lalu
induk
melakukan
339b8fd43a
3 mengubah file dengan 12 tambahan dan 6 penghapusan
  1. 2 2
      filters.go
  2. 2 2
      resize.go
  3. 8 2
      resize_test.go

+ 2 - 2
filters.go

@@ -27,8 +27,8 @@ type RGBA [4]uint16
 
 // build RGBA from an arbitrary color
 func toRGBA(c color.Color) RGBA {
-	n := color.RGBA64Model.Convert(c).(color.RGBA64)
-	return RGBA{n.R, n.G, n.B, n.A}
+	r, g, b, a := c.RGBA()
+	return RGBA{uint16(r), uint16(g), uint16(b), uint16(a)}
 }
 
 func clampToUint16(x float32) (y uint16) {

+ 2 - 2
resize.go

@@ -85,11 +85,11 @@ func Resize(width, height uint, img image.Image, interp InterpolationFunction) i
 
 	resizedImg := image.NewRGBA64(image.Rect(0, 0, int(oldWidth/scaleX), int(oldHeight/scaleY)))
 	b := resizedImg.Bounds()
-	
+
 	// prevent resize from doing too much work
 	// if #CPUs > width
 	n := 1
-	if (NCPU < b.Dy()) {
+	if NCPU < b.Dy() {
 		n = NCPU
 	} else {
 		n = b.Dy()

+ 8 - 2
resize_test.go

@@ -8,11 +8,12 @@ import (
 
 var img = image.NewGray16(image.Rect(0, 0, 3, 3))
 
-func Test_Nearest(t *testing.T) {
+func init() {
 	img.Set(1, 1, color.White)
+}
 
+func Test_Nearest(t *testing.T) {
 	m := Resize(6, 0, img, NearestNeighbor)
-
 	if m.At(2, 2) != m.At(3, 3) {
 		t.Fail()
 	}
@@ -40,3 +41,8 @@ func Test_ZeroImg(t *testing.T) {
 		t.Fail()
 	}
 }
+
+func Benchmark_BigResize(b *testing.B) {
+	m := Resize(1000, 1000, img, Lanczos3)
+	m.At(0, 0)
+}