github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/mobile/sprite/portable/bilinear_test.go (about)

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package portable
     6  
     7  import (
     8  	"image"
     9  	"image/color"
    10  	"testing"
    11  )
    12  
    13  type interpTest struct {
    14  	desc     string
    15  	src      []uint8
    16  	srcWidth int
    17  	x, y     float32
    18  	expect   uint8
    19  }
    20  
    21  func (p *interpTest) newSrc() *image.RGBA {
    22  	b := image.Rect(0, 0, p.srcWidth, len(p.src)/p.srcWidth)
    23  	src := image.NewRGBA(b)
    24  	i := 0
    25  	for y := b.Min.Y; y < b.Max.Y; y++ {
    26  		for x := b.Min.X; x < b.Max.X; x++ {
    27  			src.SetRGBA(x, y, color.RGBA{
    28  				R: p.src[i],
    29  				G: p.src[i],
    30  				B: p.src[i],
    31  				A: 0xff,
    32  			})
    33  			i++
    34  		}
    35  	}
    36  	return src
    37  }
    38  
    39  var interpTests = []interpTest{
    40  	{
    41  		desc:     "center of a single white pixel should match that pixel",
    42  		src:      []uint8{0x00},
    43  		srcWidth: 1,
    44  		x:        0.5,
    45  		y:        0.5,
    46  		expect:   0x00,
    47  	},
    48  	{
    49  		desc: "middle of a square is equally weighted",
    50  		src: []uint8{
    51  			0x00, 0xff,
    52  			0xff, 0x00,
    53  		},
    54  		srcWidth: 2,
    55  		x:        1.0,
    56  		y:        1.0,
    57  		expect:   0x80,
    58  	},
    59  	{
    60  		desc: "center of a pixel is just that pixel",
    61  		src: []uint8{
    62  			0x00, 0xff,
    63  			0xff, 0x00,
    64  		},
    65  		srcWidth: 2,
    66  		x:        1.5,
    67  		y:        0.5,
    68  		expect:   0xff,
    69  	},
    70  	{
    71  		desc: "asymmetry abounds",
    72  		src: []uint8{
    73  			0xaa, 0x11, 0x55,
    74  			0xff, 0x95, 0xdd,
    75  		},
    76  		srcWidth: 3,
    77  		x:        2.0,
    78  		y:        1.0,
    79  		expect:   0x76, // (0x11 + 0x55 + 0x95 + 0xdd) / 4
    80  	},
    81  }
    82  
    83  func TestBilinear(t *testing.T) {
    84  	for _, p := range interpTests {
    85  		src := p.newSrc()
    86  
    87  		c0 := bilinearGeneral(src, p.x, p.y)
    88  		c0R, c0G, c0B, c0A := c0.RGBA()
    89  		r := uint8(c0R >> 8)
    90  		g := uint8(c0G >> 8)
    91  		b := uint8(c0B >> 8)
    92  		a := uint8(c0A >> 8)
    93  
    94  		if r != g || r != b || a != 0xff {
    95  			t.Errorf("expect channels to match, got %v", c0)
    96  			continue
    97  		}
    98  		if r != p.expect {
    99  			t.Errorf("%s: got 0x%02x want 0x%02x", p.desc, r, p.expect)
   100  			continue
   101  		}
   102  
   103  		// fast path for *image.RGBA
   104  		c1 := bilinearRGBA(src, p.x, p.y)
   105  		if r != c1.R || g != c1.G || b != c1.B || a != c1.A {
   106  			t.Errorf("%s: RGBA fast path mismatch got %v want %v", p.desc, c1, c0)
   107  			continue
   108  		}
   109  
   110  		// fast path for *image.Alpha
   111  		alpha := image.NewAlpha(src.Bounds())
   112  		for y := src.Bounds().Min.Y; y < src.Bounds().Max.Y; y++ {
   113  			for x := src.Bounds().Min.X; x < src.Bounds().Max.X; x++ {
   114  				r, _, _, _ := src.At(x, y).RGBA()
   115  				alpha.Set(x, y, color.Alpha{A: uint8(r >> 8)})
   116  			}
   117  		}
   118  		c2 := bilinearAlpha(alpha, p.x, p.y)
   119  		if c2.A != r {
   120  			t.Errorf("%s: Alpha fast path mismatch got %v want %v", p.desc, c2, c0)
   121  			continue
   122  		}
   123  	}
   124  }
   125  
   126  func TestBilinearSubImage(t *testing.T) {
   127  	b0 := image.Rect(0, 0, 4, 4)
   128  	src0 := image.NewRGBA(b0)
   129  	b1 := image.Rect(1, 1, 3, 3)
   130  	src1 := src0.SubImage(b1).(*image.RGBA)
   131  	src1.Set(1, 1, color.RGBA{0x11, 0, 0, 0xff})
   132  	src1.Set(2, 1, color.RGBA{0x22, 0, 0, 0xff})
   133  	src1.Set(1, 2, color.RGBA{0x33, 0, 0, 0xff})
   134  	src1.Set(2, 2, color.RGBA{0x44, 0, 0, 0xff})
   135  
   136  	tests := []struct {
   137  		x, y float32
   138  		want uint32
   139  	}{
   140  		{1, 1, 0x11},
   141  		{3, 1, 0x22},
   142  		{1, 3, 0x33},
   143  		{3, 3, 0x44},
   144  		{2, 2, 0x2b},
   145  	}
   146  
   147  	for _, p := range tests {
   148  		r, _, _, _ := bilinear(src1, p.x, p.y).RGBA()
   149  		r >>= 8
   150  		if r != p.want {
   151  			t.Errorf("(%.0f, %.0f): got 0x%02x want 0x%02x", p.x, p.y, r, p.want)
   152  		}
   153  	}
   154  }