github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/image/draw/stdlib_test.go (about)

     1  // Copyright 2015 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  // +build go1.5
     6  
     7  package draw
     8  
     9  // This file contains tests that depend on the exact behavior of the
    10  // image/color package in the standard library. The color conversion formula
    11  // from YCbCr to RGBA changed between Go 1.4 and Go 1.5, so this file's tests
    12  // are only enabled for Go 1.5 and above.
    13  
    14  import (
    15  	"bytes"
    16  	"image"
    17  	"image/color"
    18  	"testing"
    19  )
    20  
    21  // TestFastPaths tests that the fast path implementations produce identical
    22  // results to the generic implementation.
    23  func TestFastPaths(t *testing.T) {
    24  	drs := []image.Rectangle{
    25  		image.Rect(0, 0, 10, 10),   // The dst bounds.
    26  		image.Rect(3, 4, 8, 6),     // A strict subset of the dst bounds.
    27  		image.Rect(-3, -5, 2, 4),   // Partial out-of-bounds #0.
    28  		image.Rect(4, -2, 6, 12),   // Partial out-of-bounds #1.
    29  		image.Rect(12, 14, 23, 45), // Complete out-of-bounds.
    30  		image.Rect(5, 5, 5, 5),     // Empty.
    31  	}
    32  	srs := []image.Rectangle{
    33  		image.Rect(0, 0, 12, 9),    // The src bounds.
    34  		image.Rect(2, 2, 10, 8),    // A strict subset of the src bounds.
    35  		image.Rect(10, 5, 20, 20),  // Partial out-of-bounds #0.
    36  		image.Rect(-40, 0, 40, 8),  // Partial out-of-bounds #1.
    37  		image.Rect(-8, -8, -4, -4), // Complete out-of-bounds.
    38  		image.Rect(5, 5, 5, 5),     // Empty.
    39  	}
    40  	srcfs := []func(image.Rectangle) (image.Image, error){
    41  		srcGray,
    42  		srcNRGBA,
    43  		srcRGBA,
    44  		srcUnif,
    45  		srcYCbCr,
    46  	}
    47  	var srcs []image.Image
    48  	for _, srcf := range srcfs {
    49  		src, err := srcf(srs[0])
    50  		if err != nil {
    51  			t.Fatal(err)
    52  		}
    53  		srcs = append(srcs, src)
    54  	}
    55  	qs := []Interpolator{
    56  		NearestNeighbor,
    57  		ApproxBiLinear,
    58  		CatmullRom,
    59  	}
    60  	ops := []Op{
    61  		Over,
    62  		Src,
    63  	}
    64  	blue := image.NewUniform(color.RGBA{0x11, 0x22, 0x44, 0x7f})
    65  
    66  	for _, dr := range drs {
    67  		for _, src := range srcs {
    68  			for _, sr := range srs {
    69  				for _, transform := range []bool{false, true} {
    70  					for _, q := range qs {
    71  						for _, op := range ops {
    72  							dst0 := image.NewRGBA(drs[0])
    73  							dst1 := image.NewRGBA(drs[0])
    74  							Draw(dst0, dst0.Bounds(), blue, image.Point{}, Src)
    75  							Draw(dstWrapper{dst1}, dst1.Bounds(), srcWrapper{blue}, image.Point{}, Src)
    76  
    77  							if transform {
    78  								m := transformMatrix(3.75, 2, 1)
    79  								q.Transform(dst0, m, src, sr, op, nil)
    80  								q.Transform(dstWrapper{dst1}, m, srcWrapper{src}, sr, op, nil)
    81  							} else {
    82  								q.Scale(dst0, dr, src, sr, op, nil)
    83  								q.Scale(dstWrapper{dst1}, dr, srcWrapper{src}, sr, op, nil)
    84  							}
    85  
    86  							if !bytes.Equal(dst0.Pix, dst1.Pix) {
    87  								t.Errorf("pix differ for dr=%v, src=%T, sr=%v, transform=%t, q=%T",
    88  									dr, src, sr, transform, q)
    89  							}
    90  						}
    91  					}
    92  				}
    93  			}
    94  		}
    95  	}
    96  }