Browse Source

avoid unnecessary processing

Change-Id: I6adcf2a9bd1b6b30bcfb9a8351778b30f3c16caa
yefu 5 years ago
parent
commit
d8633a436a
3 changed files with 28 additions and 5 deletions
  1. 16 0
      adjust.go
  2. 6 5
      resize.go
  3. 6 0
      tools.go

+ 16 - 0
adjust.go

@@ -62,6 +62,10 @@ func Invert(img image.Image) *image.NRGBA {
 //  dstImage = imaging.AdjustSaturation(srcImage, -10) // Decrease image saturation by 10%.
 //
 func AdjustSaturation(img image.Image, percentage float64) *image.NRGBA {
+	if percentage == 0 {
+		return Clone(img)
+	}
+
 	percentage = math.Min(math.Max(percentage, -100), 100)
 	multiplier := 1 + percentage/100
 
@@ -86,6 +90,10 @@ func AdjustSaturation(img image.Image, percentage float64) *image.NRGBA {
 //	dstImage = imaging.AdjustContrast(srcImage, 20) // Increase image contrast by 20%.
 //
 func AdjustContrast(img image.Image, percentage float64) *image.NRGBA {
+	if percentage == 0 {
+		return Clone(img)
+	}
+
 	percentage = math.Min(math.Max(percentage, -100.0), 100.0)
 	lut := make([]uint8, 256)
 
@@ -114,6 +122,10 @@ func AdjustContrast(img image.Image, percentage float64) *image.NRGBA {
 //	dstImage = imaging.AdjustBrightness(srcImage, 10) // Increase image brightness by 10%.
 //
 func AdjustBrightness(img image.Image, percentage float64) *image.NRGBA {
+	if percentage == 0 {
+		return Clone(img)
+	}
+
 	percentage = math.Min(math.Max(percentage, -100.0), 100.0)
 	lut := make([]uint8, 256)
 
@@ -134,6 +146,10 @@ func AdjustBrightness(img image.Image, percentage float64) *image.NRGBA {
 //	dstImage = imaging.AdjustGamma(srcImage, 0.7)
 //
 func AdjustGamma(img image.Image, gamma float64) *image.NRGBA {
+	if gamma == 1 {
+		return Clone(img)
+	}
+
 	e := 1.0 / math.Max(gamma, 0.0001)
 	lut := make([]uint8, 256)
 

+ 6 - 5
resize.go

@@ -87,6 +87,10 @@ func Resize(img image.Image, width, height int, filter ResampleFilter) *image.NR
 		dstH = int(math.Max(1.0, math.Floor(tmpH+0.5)))
 	}
 
+	if srcW == dstW && srcH == dstH {
+		return Clone(img)
+	}
+
 	if filter.Support <= 0 {
 		// Nearest-neighbor special case.
 		return resizeNearest(img, dstW, dstH)
@@ -94,14 +98,11 @@ func Resize(img image.Image, width, height int, filter ResampleFilter) *image.NR
 
 	if srcW != dstW && srcH != dstH {
 		return resizeVertical(resizeHorizontal(img, dstW, filter), dstH, filter)
-	}
-	if srcW != dstW {
+	} else if srcW != dstW {
 		return resizeHorizontal(img, dstW, filter)
-	}
-	if srcH != dstH {
+	} else {
 		return resizeVertical(img, dstH, filter)
 	}
-	return Clone(img)
 }
 
 func resizeHorizontal(img image.Image, width int, filter ResampleFilter) *image.NRGBA {

+ 6 - 0
tools.go

@@ -95,7 +95,10 @@ func Crop(img image.Image, rect image.Rectangle) *image.NRGBA {
 	r := rect.Intersect(img.Bounds()).Sub(img.Bounds().Min)
 	if r.Empty() {
 		return &image.NRGBA{}
+	} else if r.Eq(img.Bounds().Sub(img.Bounds().Min)) {
+		return Clone(img)
 	}
+
 	src := newScanner(img)
 	dst := image.NewNRGBA(image.Rect(0, 0, r.Dx(), r.Dy()))
 	rowSize := r.Dx() * 4
@@ -132,7 +135,10 @@ func Paste(background, img image.Image, pos image.Point) *image.NRGBA {
 	interRect := pasteRect.Intersect(dst.Bounds())
 	if interRect.Empty() {
 		return dst
+	} else if interRect.Eq(dst.Bounds()) {
+		return Clone(img)
 	}
+
 	src := newScanner(img)
 	parallel(interRect.Min.Y, interRect.Max.Y, func(ys <-chan int) {
 		for y := range ys {