Răsfoiți Sursa

Merge pull request #109 from yef-ksh/master

avoid unnecessary processing
Grigory Dryapak 5 ani în urmă
părinte
comite
879073f233
4 a modificat fișierele cu 83 adăugiri și 4 ștergeri
  1. 16 0
      adjust.go
  2. 6 4
      resize.go
  3. 8 0
      tools.go
  4. 53 0
      tools_test.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 - 4
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)
@@ -98,10 +102,8 @@ func Resize(img image.Image, width, height int, filter ResampleFilter) *image.NR
 	if srcW != dstW {
 		return resizeHorizontal(img, dstW, filter)
 	}
-	if srcH != dstH {
-		return resizeVertical(img, dstH, filter)
-	}
-	return Clone(img)
+	return resizeVertical(img, dstH, filter)
+
 }
 
 func resizeHorizontal(img image.Image, width int, filter ResampleFilter) *image.NRGBA {

+ 8 - 0
tools.go

@@ -96,6 +96,10 @@ func Crop(img image.Image, rect image.Rectangle) *image.NRGBA {
 	if r.Empty() {
 		return &image.NRGBA{}
 	}
+	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
@@ -133,6 +137,10 @@ func Paste(background, img image.Image, pos image.Point) *image.NRGBA {
 	if interRect.Empty() {
 		return dst
 	}
+	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 {

+ 53 - 0
tools_test.go

@@ -328,6 +328,28 @@ func TestCrop(t *testing.T) {
 		r    image.Rectangle
 		want *image.NRGBA
 	}{
+		{
+			"Crop 2x3 2x3",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 1, 2),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
+					0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
+					0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
+				},
+			},
+			image.Rect(-1, -1, 1, 2),
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 2, 3),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
+					0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
+					0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
+				},
+			},
+		},
 		{
 			"Crop 2x3 2x1",
 			&image.NRGBA{
@@ -768,6 +790,37 @@ func TestPaste(t *testing.T) {
 		p    image.Point
 		want *image.NRGBA
 	}{
+		{
+			"Paste 2x3 2x3",
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 2, 3),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
+					0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
+					0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
+				},
+			},
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 1, 2),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33,
+					0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00,
+					0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
+				},
+			},
+			image.Pt(0, 0),
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 2, 3),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33,
+					0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00,
+					0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
+				},
+			},
+		},
 		{
 			"Paste 2x3 2x1",
 			&image.NRGBA{