Browse Source

Resize optimization

disintegration 12 years ago
parent
commit
5626030d0a
1 changed files with 17 additions and 17 deletions
  1. 17 17
      imaging.go

+ 17 - 17
imaging.go

@@ -619,20 +619,25 @@ func Resize(img image.Image, width, height int) *image.NRGBA {
 
 	weightsX := make([]float64, int(radiusX+2)*2)
 
-	for dstY := 0; dstY < dstH; dstY++ {
-		fy := float64(srcMinY) + (float64(dstY)+0.5)*dy - 0.5
+	for dstX := 0; dstX < dstW; dstX++ {
+		fx := float64(srcMinX) + (float64(dstX)+0.5)*dx - 0.5
 
-		for dstX := 0; dstX < dstW; dstX++ {
-			fx := float64(srcMinX) + (float64(dstX)+0.5)*dx - 0.5
+		startX := int(math.Ceil(fx - radiusX))
+		if startX < srcMinX {
+			startX = srcMinX
+		}
+		endX := int(math.Floor(fx + radiusX))
+		if endX > srcMaxX-1 {
+			endX = srcMaxX - 1
+		}
 
-			startX := int(math.Ceil(fx - radiusX))
-			if startX < srcMinX {
-				startX = srcMinX
-			}
-			endX := int(math.Floor(fx + radiusX))
-			if endX > srcMaxX-1 {
-				endX = srcMaxX - 1
-			}
+		// cache weights for xs
+		for x := startX; x <= endX; x++ {
+			weightsX[x-startX] = antialiasFilter((float64(x) - fx) / radiusX)
+		}
+
+		for dstY := 0; dstY < dstH; dstY++ {
+			fy := float64(srcMinY) + (float64(dstY)+0.5)*dy - 0.5
 
 			startY := int(math.Ceil(fy - radiusY))
 			if startY < srcMinY {
@@ -643,11 +648,6 @@ func Resize(img image.Image, width, height int) *image.NRGBA {
 				endY = srcMaxY - 1
 			}
 
-			// cache weights for xs
-			for x := startX; x <= endX; x++ {
-				weightsX[x-startX] = antialiasFilter((float64(x) - fx) / radiusX)
-			}
-
 			weightSum := 0.0
 			r, g, b, a := 0.0, 0.0, 0.0, 0.0