Explorar el Código

Fill and Fit tests improvements

- Separate test cases for crop->resize and resize->crop fill variants
- Use Fill with different anchor points in golden tests
- Add Fit golden test
Grigory Dryapak hace 6 años
padre
commit
6b1a0e7447

+ 5 - 5
resize.go

@@ -270,9 +270,9 @@ func Fit(img image.Image, width, height int, filter ResampleFilter) *image.NRGBA
 //	dstImage := imaging.Fill(srcImage, 800, 600, imaging.Center, imaging.Lanczos)
 //
 func Fill(img image.Image, width, height int, anchor Anchor, filter ResampleFilter) *image.NRGBA {
-	minW, minH := width, height
+	dstW, dstH := width, height
 
-	if minW <= 0 || minH <= 0 {
+	if dstW <= 0 || dstH <= 0 {
 		return &image.NRGBA{}
 	}
 
@@ -284,14 +284,14 @@ func Fill(img image.Image, width, height int, anchor Anchor, filter ResampleFilt
 		return &image.NRGBA{}
 	}
 
-	if srcW == minW && srcH == minH {
+	if srcW == dstW && srcH == dstH {
 		return Clone(img)
 	}
 
 	if srcW >= 100 && srcH >= 100 {
-		return cropAndResize(img, minW, minH, anchor, filter)
+		return cropAndResize(img, dstW, dstH, anchor, filter)
 	}
-	return resizeAndCrop(img, minW, minH, anchor, filter)
+	return resizeAndCrop(img, dstW, dstH, anchor, filter)
 }
 
 // cropAndResize crops the image to the smallest possible size that has the required aspect ratio using

+ 259 - 33
resize_test.go

@@ -3,6 +3,7 @@ package imaging
 import (
 	"fmt"
 	"image"
+	"path/filepath"
 	"testing"
 )
 
@@ -374,6 +375,18 @@ func TestFit(t *testing.T) {
 	}
 }
 
+func TestFitGolden(t *testing.T) {
+	got := Fit(testdataBranchesPNG, 150, 150, Box)
+	name := filepath.Join("testdata", "out_fit.png")
+	want, err := Open(name)
+	if err != nil {
+		t.Fatalf("failed to open image: %v", err)
+	}
+	if !compareNRGBA(got, toNRGBA(want), 0) {
+		t.Fatalf("resulting image differs from golden: %s", name)
+	}
+}
+
 func TestFill(t *testing.T) {
 	testCases := []struct {
 		name string
@@ -384,7 +397,113 @@ func TestFill(t *testing.T) {
 		want *image.NRGBA
 	}{
 		{
-			"Fill 4x4 2x2 Center Nearest",
+			"Fill 4x4 4x4 TopRight Box",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 3, 3),
+				Stride: 4 * 4,
+				Pix: []uint8{
+					0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+					0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+					0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+					0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+				},
+			},
+			4, 4,
+			TopRight,
+			Box,
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 4, 4),
+				Stride: 4 * 4,
+				Pix: []uint8{
+					0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+					0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+					0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+					0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+				},
+			},
+		},
+		{
+			"Fill 4x4 0x4 Left Box",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 3, 3),
+				Stride: 4 * 4,
+				Pix: []uint8{
+					0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+					0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+					0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+					0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+				},
+			},
+			0, 4,
+			Left,
+			Box,
+			&image.NRGBA{},
+		},
+		{
+			"Fill 0x0 4x4 Right Box",
+			&image.NRGBA{},
+			4, 4,
+			Right,
+			Box,
+			&image.NRGBA{},
+		},
+		{
+			"Fill 100x200 20x10 Center Linear",
+			image.NewRGBA(image.Rect(0, 0, 100, 200)),
+			20, 10,
+			Center,
+			Linear,
+			image.NewNRGBA(image.Rect(0, 0, 20, 10)),
+		},
+		{
+			"Fill 10x20 20x10 Center Linear",
+			image.NewRGBA(image.Rect(0, 0, 10, 20)),
+			20, 10,
+			Center,
+			Linear,
+			image.NewNRGBA(image.Rect(0, 0, 20, 10)),
+		},
+	}
+	for _, tc := range testCases {
+		t.Run(tc.name, func(t *testing.T) {
+			got := Fill(tc.src, tc.w, tc.h, tc.a, tc.f)
+			if !compareNRGBA(got, tc.want, 0) {
+				t.Fatalf("got result %#v want %#v", got, tc.want)
+			}
+		})
+	}
+}
+
+func TestFillGolden(t *testing.T) {
+	anchorPoints := map[string]Anchor{
+		"left":   Left,
+		"center": Center,
+		"right":  Right,
+	}
+	for apName, ap := range anchorPoints {
+		got := Fill(testdataBranchesPNG, 150, 150, ap, Box)
+		name := filepath.Join("testdata", "out_fill_"+apName+".png")
+		want, err := Open(name)
+		if err != nil {
+			t.Fatalf("failed to open image: %v", err)
+		}
+		if !compareNRGBA(got, toNRGBA(want), 0) {
+			t.Fatalf("resulting image differs from golden: %s", name)
+		}
+	}
+}
+
+func TestResizeAndCrop(t *testing.T) {
+	testCases := []struct {
+		name string
+		src  image.Image
+		w, h int
+		a    Anchor
+		f    ResampleFilter
+		want *image.NRGBA
+	}{
+		{
+			"resizeAndCrop 4x4 2x2 Center Nearest",
 			&image.NRGBA{
 				Rect:   image.Rect(-1, -1, 3, 3),
 				Stride: 4 * 4,
@@ -408,7 +527,7 @@ func TestFill(t *testing.T) {
 			},
 		},
 		{
-			"Fill 4x4 1x4 TopLeft Nearest",
+			"resizeAndCrop 4x4 1x4 TopLeft Nearest",
 			&image.NRGBA{
 				Rect:   image.Rect(-1, -1, 3, 3),
 				Stride: 4 * 4,
@@ -434,7 +553,7 @@ func TestFill(t *testing.T) {
 			},
 		},
 		{
-			"Fill 4x4 8x2 Bottom Nearest",
+			"resizeAndCrop 4x4 8x2 Bottom Nearest",
 			&image.NRGBA{
 				Rect:   image.Rect(-1, -1, 3, 3),
 				Stride: 4 * 4,
@@ -458,7 +577,7 @@ func TestFill(t *testing.T) {
 			},
 		},
 		{
-			"Fill 4x4 2x8 Top Nearest",
+			"resizeAndCrop 4x4 2x8 Top Nearest",
 			&image.NRGBA{
 				Rect:   image.Rect(-1, -1, 3, 3),
 				Stride: 4 * 4,
@@ -488,7 +607,7 @@ func TestFill(t *testing.T) {
 			},
 		},
 		{
-			"Fill 4x4 4x4 TopRight Box",
+			"resizeAndCrop 4x4 4x4 TopRight Box",
 			&image.NRGBA{
 				Rect:   image.Rect(-1, -1, 3, 3),
 				Stride: 4 * 4,
@@ -513,8 +632,28 @@ func TestFill(t *testing.T) {
 				},
 			},
 		},
+	}
+	for _, tc := range testCases {
+		t.Run(tc.name, func(t *testing.T) {
+			got := resizeAndCrop(tc.src, tc.w, tc.h, tc.a, tc.f)
+			if !compareNRGBA(got, tc.want, 0) {
+				t.Fatalf("got result %#v want %#v", got, tc.want)
+			}
+		})
+	}
+}
+
+func TestCropAndResize(t *testing.T) {
+	testCases := []struct {
+		name string
+		src  image.Image
+		w, h int
+		a    Anchor
+		f    ResampleFilter
+		want *image.NRGBA
+	}{
 		{
-			"Fill 4x4 0x4 Left Box",
+			"cropAndResize 4x4 2x2 Center Nearest",
 			&image.NRGBA{
 				Rect:   image.Rect(-1, -1, 3, 3),
 				Stride: 4 * 4,
@@ -525,23 +664,128 @@ func TestFill(t *testing.T) {
 					0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
 				},
 			},
-			0, 4,
-			Left,
-			Box,
-			&image.NRGBA{},
+			2, 2,
+			Center,
+			NearestNeighbor,
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 2, 2),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x14, 0x15, 0x16, 0x17, 0x1c, 0x1d, 0x1e, 0x1f,
+					0x34, 0x35, 0x36, 0x37, 0x3c, 0x3d, 0x3e, 0x3f,
+				},
+			},
 		},
 		{
-			"Fill 0x0 4x4 Right Box",
-			&image.NRGBA{},
+			"cropAndResize 4x4 1x4 TopLeft Nearest",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 3, 3),
+				Stride: 4 * 4,
+				Pix: []uint8{
+					0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+					0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+					0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+					0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+				},
+			},
+			1, 4,
+			TopLeft,
+			NearestNeighbor,
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 1, 4),
+				Stride: 1 * 4,
+				Pix: []uint8{
+					0x00, 0x01, 0x02, 0x03,
+					0x10, 0x11, 0x12, 0x13,
+					0x20, 0x21, 0x22, 0x23,
+					0x30, 0x31, 0x32, 0x33,
+				},
+			},
+		},
+		{
+			"cropAndResize 4x4 8x2 Bottom Nearest",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 3, 3),
+				Stride: 4 * 4,
+				Pix: []uint8{
+					0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+					0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+					0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+					0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+				},
+			},
+			8, 2,
+			Bottom,
+			NearestNeighbor,
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 8, 2),
+				Stride: 8 * 4,
+				Pix: []uint8{
+					0x30, 0x31, 0x32, 0x33, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x3c, 0x3d, 0x3e, 0x3f,
+					0x30, 0x31, 0x32, 0x33, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x3c, 0x3d, 0x3e, 0x3f,
+				},
+			},
+		},
+		{
+			"cropAndResize 4x4 2x8 Top Nearest",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 3, 3),
+				Stride: 4 * 4,
+				Pix: []uint8{
+					0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+					0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+					0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+					0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+				},
+			},
+			2, 8,
+			Top,
+			NearestNeighbor,
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 2, 8),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x04, 0x05, 0x06, 0x07, 0x04, 0x05, 0x06, 0x07,
+					0x04, 0x05, 0x06, 0x07, 0x04, 0x05, 0x06, 0x07,
+					0x14, 0x15, 0x16, 0x17, 0x14, 0x15, 0x16, 0x17,
+					0x14, 0x15, 0x16, 0x17, 0x14, 0x15, 0x16, 0x17,
+					0x24, 0x25, 0x26, 0x27, 0x24, 0x25, 0x26, 0x27,
+					0x24, 0x25, 0x26, 0x27, 0x24, 0x25, 0x26, 0x27,
+					0x34, 0x35, 0x36, 0x37, 0x34, 0x35, 0x36, 0x37,
+					0x34, 0x35, 0x36, 0x37, 0x34, 0x35, 0x36, 0x37,
+				},
+			},
+		},
+		{
+			"cropAndResize 4x4 4x4 TopRight Box",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 3, 3),
+				Stride: 4 * 4,
+				Pix: []uint8{
+					0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+					0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+					0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+					0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+				},
+			},
 			4, 4,
-			Right,
+			TopRight,
 			Box,
-			&image.NRGBA{},
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 4, 4),
+				Stride: 4 * 4,
+				Pix: []uint8{
+					0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+					0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+					0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+					0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+				},
+			},
 		},
 	}
 	for _, tc := range testCases {
 		t.Run(tc.name, func(t *testing.T) {
-			got := Fill(tc.src, tc.w, tc.h, tc.a, tc.f)
+			got := cropAndResize(tc.src, tc.w, tc.h, tc.a, tc.f)
 			if !compareNRGBA(got, tc.want, 0) {
 				t.Fatalf("got result %#v want %#v", got, tc.want)
 			}
@@ -630,24 +874,6 @@ func TestThumbnail(t *testing.T) {
 	}
 }
 
-func TestThumbnailGolden(t *testing.T) {
-	for name, filter := range map[string]ResampleFilter{
-		"out_thumbnail_nearest.png": NearestNeighbor,
-		"out_thumbnail_linear.png":  Linear,
-		"out_thumbnail_catrom.png":  CatmullRom,
-		"out_thumbnail_lanczos.png": Lanczos,
-	} {
-		got := Thumbnail(testdataBranchesPNG, 150, 100, filter)
-		want, err := Open("testdata/" + name)
-		if err != nil {
-			t.Fatalf("failed to open image: %v", err)
-		}
-		if !compareNRGBA(got, toNRGBA(want), 0) {
-			t.Fatalf("resulting image differs from golden: %s", name)
-		}
-	}
-}
-
 func BenchmarkResize(b *testing.B) {
 	for _, dir := range []string{"Down", "Up"} {
 		for _, filter := range []string{"NearestNeighbor", "Linear", "CatmullRom", "Lanczos"} {

BIN
testdata/out_fill_center.png


BIN
testdata/out_fill_left.png


BIN
testdata/out_fill_right.png


BIN
testdata/out_fit.png


BIN
testdata/out_thumbnail_catrom.png


BIN
testdata/out_thumbnail_lanczos.png


BIN
testdata/out_thumbnail_linear.png


BIN
testdata/out_thumbnail_nearest.png