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