Speed up calculation by avoiding dynamic casting

This commit is contained in:
jst 2012-09-01 00:21:10 +02:00
parent 294efa80bb
commit 339b8fd43a
3 changed files with 12 additions and 6 deletions

View File

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

View File

@ -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))) resizedImg := image.NewRGBA64(image.Rect(0, 0, int(oldWidth/scaleX), int(oldHeight/scaleY)))
b := resizedImg.Bounds() b := resizedImg.Bounds()
// prevent resize from doing too much work // prevent resize from doing too much work
// if #CPUs > width // if #CPUs > width
n := 1 n := 1
if (NCPU < b.Dy()) { if NCPU < b.Dy() {
n = NCPU n = NCPU
} else { } else {
n = b.Dy() n = b.Dy()

View File

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