Sfoglia il codice sorgente

resize and tools tests

disintegration 10 anni fa
parent
commit
7506c8762f
2 ha cambiato i file con 531 aggiunte e 0 eliminazioni
  1. 281 0
      resize_test.go
  2. 250 0
      tools_test.go

+ 281 - 0
resize_test.go

@@ -0,0 +1,281 @@
+package imaging
+
+import (
+	"image"
+	"testing"
+)
+
+func TestResize(t *testing.T) {
+	td := []struct {
+		desc string
+		src  image.Image
+		w, h int
+		f    ResampleFilter
+		want *image.NRGBA
+	}{
+		{
+			"Resize 2x2 1x1 box",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 1, 1),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
+					0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
+				},
+			},
+			1, 1,
+			Box,
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 1, 1),
+				Stride: 1 * 4,
+				Pix:    []uint8{0x40, 0x40, 0x40, 0xc0},
+			},
+		},
+		{
+			"Resize 2x2 2x2 box",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 1, 1),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
+					0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
+				},
+			},
+			2, 2,
+			Box,
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 2, 2),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
+					0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
+				},
+			},
+		},
+		{
+			"Resize 3x1 1x1 nearest",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 2, 0),
+				Stride: 3 * 4,
+				Pix: []uint8{
+					0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
+				},
+			},
+			1, 1,
+			NearestNeighbor,
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 1, 1),
+				Stride: 1 * 4,
+				Pix:    []uint8{0x00, 0xff, 0x00, 0xff},
+			},
+		},
+		{
+			"Resize 2x2 0x4 box",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 1, 1),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
+					0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
+				},
+			},
+			0, 4,
+			Box,
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 4, 4),
+				Stride: 4 * 4,
+				Pix: []uint8{
+					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
+					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
+					0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
+					0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
+				},
+			},
+		},
+		{
+			"Resize 2x2 4x0 linear",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 1, 1),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
+					0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
+				},
+			},
+			4, 0,
+			Linear,
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 4, 4),
+				Stride: 4 * 4,
+				Pix: []uint8{
+					0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0xbf, 0x00, 0x00, 0xbf, 0xff, 0x00, 0x00, 0xff,
+					0x00, 0x40, 0x00, 0x40, 0x30, 0x30, 0x10, 0x70, 0x8f, 0x10, 0x30, 0xcf, 0xbf, 0x00, 0x40, 0xff,
+					0x00, 0xbf, 0x00, 0xbf, 0x10, 0x8f, 0x30, 0xcf, 0x30, 0x30, 0x8f, 0xef, 0x40, 0x00, 0xbf, 0xff,
+					0x00, 0xff, 0x00, 0xff, 0x00, 0xbf, 0x40, 0xff, 0x00, 0x40, 0xbf, 0xff, 0x00, 0x00, 0xff, 0xff,
+				},
+			},
+		},
+	}
+	for _, d := range td {
+		got := Resize(d.src, d.w, d.h, d.f)
+		want := d.want
+		if !compareNRGBA(got, want, 1) {
+			t.Errorf("test [%s] failed: %#v", d.desc, got)
+		}
+	}
+}
+
+func TestFit(t *testing.T) {
+	td := []struct {
+		desc string
+		src  image.Image
+		w, h int
+		f    ResampleFilter
+		want *image.NRGBA
+	}{
+		{
+			"Fit 2x2 1x10 box",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 1, 1),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
+					0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
+				},
+			},
+			1, 10,
+			Box,
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 1, 1),
+				Stride: 1 * 4,
+				Pix:    []uint8{0x40, 0x40, 0x40, 0xc0},
+			},
+		},
+		{
+			"Fit 2x2 10x1 box",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 1, 1),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
+					0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
+				},
+			},
+			10, 1,
+			Box,
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 1, 1),
+				Stride: 1 * 4,
+				Pix:    []uint8{0x40, 0x40, 0x40, 0xc0},
+			},
+		},
+		{
+			"Fit 2x2 10x10 box",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 1, 1),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
+					0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
+				},
+			},
+			10, 10,
+			Box,
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 2, 2),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
+					0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
+				},
+			},
+		},
+	}
+	for _, d := range td {
+		got := Fit(d.src, d.w, d.h, d.f)
+		want := d.want
+		if !compareNRGBA(got, want, 0) {
+			t.Errorf("test [%s] failed: %#v", d.desc, got)
+		}
+	}
+}
+
+func TestThumbnail(t *testing.T) {
+	td := []struct {
+		desc string
+		src  image.Image
+		w, h int
+		f    ResampleFilter
+		want *image.NRGBA
+	}{
+		{
+			"Thumbnail 6x2 1x1 box",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 5, 1),
+				Stride: 6 * 4,
+				Pix: []uint8{
+					0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+					0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+				},
+			},
+			1, 1,
+			Box,
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 1, 1),
+				Stride: 1 * 4,
+				Pix:    []uint8{0x40, 0x40, 0x40, 0xc0},
+			},
+		},
+		{
+			"Thumbnail 2x6 1x1 box",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 1, 5),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+					0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+					0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
+					0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
+					0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+					0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+				},
+			},
+			1, 1,
+			Box,
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 1, 1),
+				Stride: 1 * 4,
+				Pix:    []uint8{0x40, 0x40, 0x40, 0xc0},
+			},
+		},
+		{
+			"Thumbnail 1x3 2x2 box",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 0, 2),
+				Stride: 1 * 4,
+				Pix: []uint8{
+					0x00, 0x00, 0x00, 0x00,
+					0xff, 0x00, 0x00, 0xff,
+					0xff, 0xff, 0xff, 0xff,
+				},
+			},
+			2, 2,
+			Box,
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 2, 2),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
+					0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
+				},
+			},
+		},
+	}
+	for _, d := range td {
+		got := Thumbnail(d.src, d.w, d.h, d.f)
+		want := d.want
+		if !compareNRGBA(got, want, 0) {
+			t.Errorf("test [%s] failed: %#v", d.desc, got)
+		}
+	}
+}

+ 250 - 0
tools_test.go

@@ -0,0 +1,250 @@
+package imaging
+
+import (
+	"image"
+	"testing"
+)
+
+func TestCrop(t *testing.T) {
+	td := []struct {
+		desc string
+		src  image.Image
+		r    image.Rectangle
+		want *image.NRGBA
+	}{
+		{
+			"Crop 2x3 2x1",
+			&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, 0, 1, 1),
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 2, 1),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
+				},
+			},
+		},
+	}
+	for _, d := range td {
+		got := Crop(d.src, d.r)
+		want := d.want
+		if !compareNRGBA(got, want, 0) {
+			t.Errorf("test [%s] failed: %#v", d.desc, got)
+		}
+	}
+}
+
+func TestCropCenter(t *testing.T) {
+	td := []struct {
+		desc string
+		src  image.Image
+		w, h int
+		want *image.NRGBA
+	}{
+		{
+			"CropCenter 2x3 2x1",
+			&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,
+				},
+			},
+			2, 1,
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 2, 1),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
+				},
+			},
+		},
+	}
+	for _, d := range td {
+		got := CropCenter(d.src, d.w, d.h)
+		want := d.want
+		if !compareNRGBA(got, want, 0) {
+			t.Errorf("test [%s] failed: %#v", d.desc, got)
+		}
+	}
+}
+
+func TestPaste(t *testing.T) {
+	td := []struct {
+		desc string
+		src1 image.Image
+		src2 image.Image
+		p    image.Point
+		want *image.NRGBA
+	}{
+		{
+			"Paste 2x3 2x1",
+			&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.NRGBA{
+				Rect:   image.Rect(1, 1, 3, 2),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
+				},
+			},
+			image.Pt(-1, 0),
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 2, 3),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
+					0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
+					0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
+				},
+			},
+		},
+	}
+	for _, d := range td {
+		got := Paste(d.src1, d.src2, d.p)
+		want := d.want
+		if !compareNRGBA(got, want, 0) {
+			t.Errorf("test [%s] failed: %#v", d.desc, got)
+		}
+	}
+}
+
+func TestPasteCenter(t *testing.T) {
+	td := []struct {
+		desc string
+		src1 image.Image
+		src2 image.Image
+		want *image.NRGBA
+	}{
+		{
+			"PasteCenter 2x3 2x1",
+			&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.NRGBA{
+				Rect:   image.Rect(1, 1, 3, 2),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
+				},
+			},
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 2, 3),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
+					0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
+					0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
+				},
+			},
+		},
+	}
+	for _, d := range td {
+		got := PasteCenter(d.src1, d.src2)
+		want := d.want
+		if !compareNRGBA(got, want, 0) {
+			t.Errorf("test [%s] failed: %#v", d.desc, got)
+		}
+	}
+}
+
+func TestOverlay(t *testing.T) {
+	td := []struct {
+		desc string
+		src1 image.Image
+		src2 image.Image
+		p    image.Point
+		a    float64
+		want *image.NRGBA
+	}{
+		{
+			"Overlay 2x3 2x1 1.0",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 1, 2),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
+					0x60, 0x00, 0x90, 0xff, 0xff, 0x00, 0x99, 0x7f,
+					0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
+				},
+			},
+			&image.NRGBA{
+				Rect:   image.Rect(1, 1, 3, 2),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x20, 0x40, 0x80, 0x7f, 0xaa, 0xbb, 0xcc, 0xff,
+				},
+			},
+			image.Pt(-1, 0),
+			1.0,
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 2, 3),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
+					0x40, 0x1f, 0x88, 0xff, 0xaa, 0xbb, 0xcc, 0xff,
+					0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
+				},
+			},
+		},
+		{
+			"Overlay 2x2 2x2 0.5",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 1, 1),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
+					0x00, 0x00, 0xff, 0xff, 0x20, 0x20, 0x20, 0x00,
+				},
+			},
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 1, 1),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
+					0xff, 0xff, 0x00, 0xff, 0x20, 0x20, 0x20, 0xff,
+				},
+			},
+			image.Pt(-1, -1),
+			0.5,
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 2, 2),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0xff, 0x7f, 0x7f, 0xff, 0x00, 0xff, 0x00, 0xff,
+					0x7f, 0x7f, 0x7f, 0xff, 0x20, 0x20, 0x20, 0x7f,
+				},
+			},
+		},
+	}
+	for _, d := range td {
+		got := Overlay(d.src1, d.src2, d.p, d.a)
+		want := d.want
+		if !compareNRGBA(got, want, 1) {
+			t.Errorf("test [%s] failed: %#v", d.desc, got)
+		}
+	}
+}