github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/image/draw/draw_test.go (about)

     1  // Copyright 2014 <chaishushan{AT}gmail.com>. 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 draw
     6  
     7  import (
     8  	"fmt"
     9  	"image"
    10  	"image/color"
    11  	"image/draw"
    12  	"testing"
    13  
    14  	"github.com/chai2010/gopkg/builtin"
    15  	image_ext "github.com/chai2010/gopkg/image"
    16  	color_ext "github.com/chai2010/gopkg/image/color"
    17  )
    18  
    19  type tDrawTester struct {
    20  	BgdImage draw.Image
    21  	BgdColor color.Color
    22  	FgdImage draw.Image
    23  	FgdColor color.Color
    24  	DrawRect image.Rectangle
    25  	DrawSp   image.Point
    26  	FgdRect  image.Rectangle
    27  }
    28  
    29  func TestDraw(t *testing.T) {
    30  	for i, v := range tDrawTesterList {
    31  		tClearImage(v.BgdImage, v.BgdColor)
    32  		tClearImage(v.FgdImage, v.FgdColor)
    33  		Draw(v.BgdImage, v.DrawRect, v.FgdImage, v.DrawSp)
    34  		err := tCheckImageColor(v.BgdImage, v.FgdRect, v.FgdColor, v.BgdColor)
    35  		if err != nil {
    36  			t.Fatalf("%d: %v", i, err)
    37  		}
    38  	}
    39  }
    40  
    41  func tClearImage(m draw.Image, c color.Color) {
    42  	b := m.Bounds()
    43  	for y := b.Min.Y; y < b.Max.Y; y++ {
    44  		for x := b.Min.X; x < b.Max.X; x++ {
    45  			m.Set(x, y, c)
    46  		}
    47  	}
    48  }
    49  
    50  func tCheckImageColor(m draw.Image, fgdRect image.Rectangle, fgdColor, bgdColor color.Color) error {
    51  	b := m.Bounds()
    52  	for y := b.Min.Y; y < b.Max.Y; y++ {
    53  		for x := b.Min.X; x < b.Max.X; x++ {
    54  			c0 := builtin.If(image.Pt(x, y).In(fgdRect), fgdColor, bgdColor).(color.Color)
    55  			c1 := m.At(x, y)
    56  			r0, g0, b0, a0 := c0.RGBA()
    57  			r1, g1, b1, a1 := c1.RGBA()
    58  			if r0 != r1 || g0 != g1 || b0 != b1 || a0 != a1 {
    59  				return fmt.Errorf("pixel(%d, %d): want %v, got %v", x, y, c0, c1)
    60  			}
    61  		}
    62  	}
    63  	return nil
    64  }
    65  
    66  var tDrawTesterList = []tDrawTester{
    67  	// Gray
    68  	tDrawTester{
    69  		BgdImage: image.NewGray(image.Rect(0, 0, 10, 10)),
    70  		BgdColor: color.Gray{100},
    71  		FgdImage: image.NewGray(image.Rect(0, 0, 10, 10)),
    72  		FgdColor: color.Gray{250},
    73  		DrawRect: image.Rect(0, 0, 5, 5),
    74  		DrawSp:   image.Pt(0, 0),
    75  		FgdRect:  image.Rect(0, 0, 5, 5),
    76  	},
    77  	tDrawTester{
    78  		BgdImage: image.NewGray(image.Rect(0, 0, 10, 10)),
    79  		BgdColor: color.Gray{100},
    80  		FgdImage: image.NewGray(image.Rect(0, 0, 10, 10)),
    81  		FgdColor: color.Gray{250},
    82  		DrawRect: image.Rect(0, 0, 10, 10),
    83  		DrawSp:   image.Pt(0, 0),
    84  		FgdRect:  image.Rect(0, 0, 10, 10),
    85  	},
    86  	tDrawTester{
    87  		BgdImage: image.NewGray(image.Rect(0, 0, 10, 10)),
    88  		BgdColor: color.Gray{100},
    89  		FgdImage: image.NewGray(image.Rect(0, 0, 10, 10)),
    90  		FgdColor: color.Gray{250},
    91  		DrawRect: image.Rect(0, 0, 15, 15), // +overflow
    92  		DrawSp:   image.Pt(0, 0),
    93  		FgdRect:  image.Rect(0, 0, 10, 10),
    94  	},
    95  	tDrawTester{
    96  		BgdImage: image.NewGray(image.Rect(0, 0, 10, 10)),
    97  		BgdColor: color.Gray{100},
    98  		FgdImage: image.NewGray(image.Rect(0, 0, 10, 10)),
    99  		FgdColor: color.Gray{250},
   100  		DrawRect: image.Rect(0, 0, 15, 15), // +overflow
   101  		DrawSp:   image.Pt(5, 5),           // +overflow
   102  		FgdRect:  image.Rect(0, 0, 5, 5),
   103  	},
   104  	tDrawTester{
   105  		BgdImage: image.NewGray(image.Rect(0, 0, 10, 10)),
   106  		BgdColor: color.Gray{100},
   107  		FgdImage: image.NewGray(image.Rect(0, 0, 8, 8)),
   108  		FgdColor: color.Gray{250},
   109  		DrawRect: image.Rect(0, 0, 15, 15), // +overflow
   110  		DrawSp:   image.Pt(5, 5),           // +overflow
   111  		FgdRect:  image.Rect(0, 0, 3, 3),
   112  	},
   113  	// Gray16
   114  	tDrawTester{
   115  		BgdImage: image.NewGray16(image.Rect(0, 0, 10, 10)),
   116  		BgdColor: color.Gray16{100 << 8},
   117  		FgdImage: image.NewGray16(image.Rect(0, 0, 10, 10)),
   118  		FgdColor: color.Gray16{250 << 8},
   119  		DrawRect: image.Rect(0, 0, 5, 5),
   120  		DrawSp:   image.Pt(0, 0),
   121  		FgdRect:  image.Rect(0, 0, 5, 5),
   122  	},
   123  	tDrawTester{
   124  		BgdImage: image.NewGray16(image.Rect(0, 0, 10, 10)),
   125  		BgdColor: color.Gray16{100 << 8},
   126  		FgdImage: image.NewGray16(image.Rect(0, 0, 10, 10)),
   127  		FgdColor: color.Gray16{250 << 8},
   128  		DrawRect: image.Rect(0, 0, 10, 10),
   129  		DrawSp:   image.Pt(0, 0),
   130  		FgdRect:  image.Rect(0, 0, 10, 10),
   131  	},
   132  	tDrawTester{
   133  		BgdImage: image.NewGray16(image.Rect(0, 0, 10, 10)),
   134  		BgdColor: color.Gray16{100 << 8},
   135  		FgdImage: image.NewGray16(image.Rect(0, 0, 10, 10)),
   136  		FgdColor: color.Gray16{250 << 8},
   137  		DrawRect: image.Rect(0, 0, 15, 15), // +overflow
   138  		DrawSp:   image.Pt(0, 0),
   139  		FgdRect:  image.Rect(0, 0, 10, 10),
   140  	},
   141  	tDrawTester{
   142  		BgdImage: image.NewGray16(image.Rect(0, 0, 10, 10)),
   143  		BgdColor: color.Gray16{100 << 8},
   144  		FgdImage: image.NewGray16(image.Rect(0, 0, 10, 10)),
   145  		FgdColor: color.Gray16{250 << 8},
   146  		DrawRect: image.Rect(0, 0, 15, 15), // +overflow
   147  		DrawSp:   image.Pt(5, 5),           // +overflow
   148  		FgdRect:  image.Rect(0, 0, 5, 5),
   149  	},
   150  	tDrawTester{
   151  		BgdImage: image.NewGray16(image.Rect(0, 0, 10, 10)),
   152  		BgdColor: color.Gray16{100 << 8},
   153  		FgdImage: image.NewGray16(image.Rect(0, 0, 8, 8)),
   154  		FgdColor: color.Gray16{250 << 8},
   155  		DrawRect: image.Rect(0, 0, 15, 15), // +overflow
   156  		DrawSp:   image.Pt(5, 5),           // +overflow
   157  		FgdRect:  image.Rect(0, 0, 3, 3),
   158  	},
   159  	// Gray32f
   160  	tDrawTester{
   161  		BgdImage: image_ext.NewGray32f(image.Rect(0, 0, 10, 10)),
   162  		BgdColor: color_ext.Gray32f{Y: 100 << 8},
   163  		FgdImage: image_ext.NewGray32f(image.Rect(0, 0, 10, 10)),
   164  		FgdColor: color_ext.Gray32f{Y: 250 << 8},
   165  		DrawRect: image.Rect(0, 0, 5, 5),
   166  		DrawSp:   image.Pt(0, 0),
   167  		FgdRect:  image.Rect(0, 0, 5, 5),
   168  	},
   169  	tDrawTester{
   170  		BgdImage: image_ext.NewGray32f(image.Rect(0, 0, 10, 10)),
   171  		BgdColor: color_ext.Gray32f{Y: 100 << 8},
   172  		FgdImage: image_ext.NewGray32f(image.Rect(0, 0, 10, 10)),
   173  		FgdColor: color_ext.Gray32f{Y: 250 << 8},
   174  		DrawRect: image.Rect(0, 0, 10, 10),
   175  		DrawSp:   image.Pt(0, 0),
   176  		FgdRect:  image.Rect(0, 0, 10, 10),
   177  	},
   178  	tDrawTester{
   179  		BgdImage: image_ext.NewGray32f(image.Rect(0, 0, 10, 10)),
   180  		BgdColor: color_ext.Gray32f{Y: 100 << 8},
   181  		FgdImage: image_ext.NewGray32f(image.Rect(0, 0, 10, 10)),
   182  		FgdColor: color_ext.Gray32f{Y: 250 << 8},
   183  		DrawRect: image.Rect(0, 0, 15, 15), // +overflow
   184  		DrawSp:   image.Pt(0, 0),
   185  		FgdRect:  image.Rect(0, 0, 10, 10),
   186  	},
   187  	tDrawTester{
   188  		BgdImage: image_ext.NewGray32f(image.Rect(0, 0, 10, 10)),
   189  		BgdColor: color_ext.Gray32f{Y: 100 << 8},
   190  		FgdImage: image_ext.NewGray32f(image.Rect(0, 0, 10, 10)),
   191  		FgdColor: color_ext.Gray32f{Y: 250 << 8},
   192  		DrawRect: image.Rect(0, 0, 15, 15), // +overflow
   193  		DrawSp:   image.Pt(5, 5),           // +overflow
   194  		FgdRect:  image.Rect(0, 0, 5, 5),
   195  	},
   196  	tDrawTester{
   197  		BgdImage: image_ext.NewGray32f(image.Rect(0, 0, 10, 10)),
   198  		BgdColor: color_ext.Gray32f{Y: 100 << 8},
   199  		FgdImage: image_ext.NewGray32f(image.Rect(0, 0, 8, 8)),
   200  		FgdColor: color_ext.Gray32f{Y: 250 << 8},
   201  		DrawRect: image.Rect(0, 0, 15, 15), // +overflow
   202  		DrawSp:   image.Pt(5, 5),           // +overflow
   203  		FgdRect:  image.Rect(0, 0, 3, 3),
   204  	},
   205  
   206  	// RGBA
   207  	tDrawTester{
   208  		BgdImage: image.NewRGBA(image.Rect(0, 0, 10, 10)),
   209  		BgdColor: color.RGBA{100, 101, 102, 103},
   210  		FgdImage: image.NewRGBA(image.Rect(0, 0, 10, 10)),
   211  		FgdColor: color.RGBA{250, 251, 252, 253},
   212  		DrawRect: image.Rect(0, 0, 5, 5),
   213  		DrawSp:   image.Pt(0, 0),
   214  		FgdRect:  image.Rect(0, 0, 5, 5),
   215  	},
   216  	tDrawTester{
   217  		BgdImage: image.NewRGBA(image.Rect(0, 0, 10, 10)),
   218  		BgdColor: color.RGBA{100, 101, 102, 103},
   219  		FgdImage: image.NewRGBA(image.Rect(0, 0, 10, 10)),
   220  		FgdColor: color.RGBA{250, 251, 252, 253},
   221  		DrawRect: image.Rect(0, 0, 10, 10),
   222  		DrawSp:   image.Pt(0, 0),
   223  		FgdRect:  image.Rect(0, 0, 10, 10),
   224  	},
   225  	tDrawTester{
   226  		BgdImage: image.NewRGBA(image.Rect(0, 0, 10, 10)),
   227  		BgdColor: color.RGBA{100, 101, 102, 103},
   228  		FgdImage: image.NewRGBA(image.Rect(0, 0, 10, 10)),
   229  		FgdColor: color.RGBA{250, 251, 252, 253},
   230  		DrawRect: image.Rect(0, 0, 15, 15), // +overflow
   231  		DrawSp:   image.Pt(0, 0),
   232  		FgdRect:  image.Rect(0, 0, 10, 10),
   233  	},
   234  	tDrawTester{
   235  		BgdImage: image.NewRGBA(image.Rect(0, 0, 10, 10)),
   236  		BgdColor: color.RGBA{100, 101, 102, 103},
   237  		FgdImage: image.NewRGBA(image.Rect(0, 0, 10, 10)),
   238  		FgdColor: color.RGBA{250, 251, 252, 253},
   239  		DrawRect: image.Rect(0, 0, 15, 15), // +overflow
   240  		DrawSp:   image.Pt(5, 5),           // +overflow
   241  		FgdRect:  image.Rect(0, 0, 5, 5),
   242  	},
   243  	tDrawTester{
   244  		BgdImage: image.NewRGBA(image.Rect(0, 0, 10, 10)),
   245  		BgdColor: color.RGBA{100, 101, 102, 103},
   246  		FgdImage: image.NewRGBA(image.Rect(0, 0, 8, 8)),
   247  		FgdColor: color.RGBA{250, 251, 252, 253},
   248  		DrawRect: image.Rect(0, 0, 15, 15), // +overflow
   249  		DrawSp:   image.Pt(5, 5),           // +overflow
   250  		FgdRect:  image.Rect(0, 0, 3, 3),
   251  	},
   252  
   253  	// RGBA64
   254  	tDrawTester{
   255  		BgdImage: image.NewRGBA64(image.Rect(0, 0, 10, 10)),
   256  		BgdColor: color.RGBA64{100 << 8, 101 << 8, 102 << 8, 103 << 8},
   257  		FgdImage: image.NewRGBA64(image.Rect(0, 0, 10, 10)),
   258  		FgdColor: color.RGBA64{250 << 8, 251 << 8, 252 << 8, 253 << 8},
   259  		DrawRect: image.Rect(0, 0, 5, 5),
   260  		DrawSp:   image.Pt(0, 0),
   261  		FgdRect:  image.Rect(0, 0, 5, 5),
   262  	},
   263  	tDrawTester{
   264  		BgdImage: image.NewRGBA64(image.Rect(0, 0, 10, 10)),
   265  		BgdColor: color.RGBA64{100 << 8, 101 << 8, 102 << 8, 103 << 8},
   266  		FgdImage: image.NewRGBA64(image.Rect(0, 0, 10, 10)),
   267  		FgdColor: color.RGBA64{250 << 8, 251 << 8, 252 << 8, 253 << 8},
   268  		DrawRect: image.Rect(0, 0, 10, 10),
   269  		DrawSp:   image.Pt(0, 0),
   270  		FgdRect:  image.Rect(0, 0, 10, 10),
   271  	},
   272  	tDrawTester{
   273  		BgdImage: image.NewRGBA64(image.Rect(0, 0, 10, 10)),
   274  		BgdColor: color.RGBA64{100 << 8, 101 << 8, 102 << 8, 103 << 8},
   275  		FgdImage: image.NewRGBA64(image.Rect(0, 0, 10, 10)),
   276  		FgdColor: color.RGBA64{250 << 8, 251 << 8, 252 << 8, 253 << 8},
   277  		DrawRect: image.Rect(0, 0, 15, 15), // +overflow
   278  		DrawSp:   image.Pt(0, 0),
   279  		FgdRect:  image.Rect(0, 0, 10, 10),
   280  	},
   281  	tDrawTester{
   282  		BgdImage: image.NewRGBA64(image.Rect(0, 0, 10, 10)),
   283  		BgdColor: color.RGBA64{100 << 8, 101 << 8, 102 << 8, 103 << 8},
   284  		FgdImage: image.NewRGBA64(image.Rect(0, 0, 10, 10)),
   285  		FgdColor: color.RGBA64{250 << 8, 251 << 8, 252 << 8, 253 << 8},
   286  		DrawRect: image.Rect(0, 0, 15, 15), // +overflow
   287  		DrawSp:   image.Pt(5, 5),           // +overflow
   288  		FgdRect:  image.Rect(0, 0, 5, 5),
   289  	},
   290  	tDrawTester{
   291  		BgdImage: image.NewRGBA64(image.Rect(0, 0, 10, 10)),
   292  		BgdColor: color.RGBA64{100 << 8, 101 << 8, 102 << 8, 103 << 8},
   293  		FgdImage: image.NewRGBA64(image.Rect(0, 0, 8, 8)),
   294  		FgdColor: color.RGBA64{250 << 8, 251 << 8, 252 << 8, 253 << 8},
   295  		DrawRect: image.Rect(0, 0, 15, 15), // +overflow
   296  		DrawSp:   image.Pt(5, 5),           // +overflow
   297  		FgdRect:  image.Rect(0, 0, 3, 3),
   298  	},
   299  
   300  	// RGBA128f
   301  	tDrawTester{
   302  		BgdImage: image_ext.NewRGBA128f(image.Rect(0, 0, 10, 10)),
   303  		BgdColor: color_ext.RGBA128f{R: 100 << 8, G: 101 << 8, B: 102 << 8, A: 103 << 8},
   304  		FgdImage: image_ext.NewRGBA128f(image.Rect(0, 0, 10, 10)),
   305  		FgdColor: color_ext.RGBA128f{R: 250 << 8, G: 251 << 8, B: 252 << 8, A: 253 << 8},
   306  		DrawRect: image.Rect(0, 0, 5, 5),
   307  		DrawSp:   image.Pt(0, 0),
   308  		FgdRect:  image.Rect(0, 0, 5, 5),
   309  	},
   310  	tDrawTester{
   311  		BgdImage: image_ext.NewRGBA128f(image.Rect(0, 0, 10, 10)),
   312  		BgdColor: color_ext.RGBA128f{R: 100 << 8, G: 101 << 8, B: 102 << 8, A: 103 << 8},
   313  		FgdImage: image_ext.NewRGBA128f(image.Rect(0, 0, 10, 10)),
   314  		FgdColor: color_ext.RGBA128f{R: 250 << 8, G: 251 << 8, B: 252 << 8, A: 253 << 8},
   315  		DrawRect: image.Rect(0, 0, 10, 10),
   316  		DrawSp:   image.Pt(0, 0),
   317  		FgdRect:  image.Rect(0, 0, 10, 10),
   318  	},
   319  	tDrawTester{
   320  		BgdImage: image_ext.NewRGBA128f(image.Rect(0, 0, 10, 10)),
   321  		BgdColor: color_ext.RGBA128f{R: 100 << 8, G: 101 << 8, B: 102 << 8, A: 103 << 8},
   322  		FgdImage: image_ext.NewRGBA128f(image.Rect(0, 0, 10, 10)),
   323  		FgdColor: color_ext.RGBA128f{R: 250 << 8, G: 251 << 8, B: 252 << 8, A: 253 << 8},
   324  		DrawRect: image.Rect(0, 0, 15, 15), // +overflow
   325  		DrawSp:   image.Pt(0, 0),
   326  		FgdRect:  image.Rect(0, 0, 10, 10),
   327  	},
   328  	tDrawTester{
   329  		BgdImage: image_ext.NewRGBA128f(image.Rect(0, 0, 10, 10)),
   330  		BgdColor: color_ext.RGBA128f{R: 100 << 8, G: 101 << 8, B: 102 << 8, A: 103 << 8},
   331  		FgdImage: image_ext.NewRGBA128f(image.Rect(0, 0, 10, 10)),
   332  		FgdColor: color_ext.RGBA128f{R: 250 << 8, G: 251 << 8, B: 252 << 8, A: 253 << 8},
   333  		DrawRect: image.Rect(0, 0, 15, 15), // +overflow
   334  		DrawSp:   image.Pt(5, 5),           // +overflow
   335  		FgdRect:  image.Rect(0, 0, 5, 5),
   336  	},
   337  	tDrawTester{
   338  		BgdImage: image_ext.NewRGBA128f(image.Rect(0, 0, 10, 10)),
   339  		BgdColor: color_ext.RGBA128f{R: 100 << 8, G: 101 << 8, B: 102 << 8, A: 103 << 8},
   340  		FgdImage: image_ext.NewRGBA128f(image.Rect(0, 0, 8, 8)),
   341  		FgdColor: color_ext.RGBA128f{R: 250 << 8, G: 251 << 8, B: 252 << 8, A: 253 << 8},
   342  		DrawRect: image.Rect(0, 0, 15, 15), // +overflow
   343  		DrawSp:   image.Pt(5, 5),           // +overflow
   344  		FgdRect:  image.Rect(0, 0, 3, 3),
   345  	},
   346  
   347  	// YCbCr
   348  	tDrawTester{
   349  		BgdImage: image_ext.NewYCbCr(image.Rect(0, 0, 10, 10), image.YCbCrSubsampleRatio444),
   350  		BgdColor: color.YCbCr{100, 101, 102},
   351  		FgdImage: image_ext.NewYCbCr(image.Rect(0, 0, 10, 10), image.YCbCrSubsampleRatio444),
   352  		FgdColor: color.YCbCr{250, 251, 252},
   353  		DrawRect: image.Rect(0, 0, 5, 5),
   354  		DrawSp:   image.Pt(0, 0),
   355  		FgdRect:  image.Rect(0, 0, 5, 5),
   356  	},
   357  	tDrawTester{
   358  		BgdImage: image_ext.NewYCbCr(image.Rect(0, 0, 10, 10), image.YCbCrSubsampleRatio444),
   359  		BgdColor: color.YCbCr{100, 101, 102},
   360  		FgdImage: image_ext.NewYCbCr(image.Rect(0, 0, 10, 10), image.YCbCrSubsampleRatio444),
   361  		FgdColor: color.YCbCr{250, 251, 252},
   362  		DrawRect: image.Rect(0, 0, 10, 10),
   363  		DrawSp:   image.Pt(0, 0),
   364  		FgdRect:  image.Rect(0, 0, 10, 10),
   365  	},
   366  	tDrawTester{
   367  		BgdImage: image_ext.NewYCbCr(image.Rect(0, 0, 10, 10), image.YCbCrSubsampleRatio444),
   368  		BgdColor: color.YCbCr{100, 101, 102},
   369  		FgdImage: image_ext.NewYCbCr(image.Rect(0, 0, 10, 10), image.YCbCrSubsampleRatio444),
   370  		FgdColor: color.YCbCr{250, 251, 252},
   371  		DrawRect: image.Rect(0, 0, 15, 15), // +overflow
   372  		DrawSp:   image.Pt(0, 0),
   373  		FgdRect:  image.Rect(0, 0, 10, 10),
   374  	},
   375  	tDrawTester{
   376  		BgdImage: image_ext.NewYCbCr(image.Rect(0, 0, 10, 10), image.YCbCrSubsampleRatio444),
   377  		BgdColor: color.YCbCr{100, 101, 102},
   378  		FgdImage: image_ext.NewYCbCr(image.Rect(0, 0, 10, 10), image.YCbCrSubsampleRatio444),
   379  		FgdColor: color.YCbCr{250, 251, 252},
   380  		DrawRect: image.Rect(0, 0, 15, 15), // +overflow
   381  		DrawSp:   image.Pt(5, 5),           // +overflow
   382  		FgdRect:  image.Rect(0, 0, 5, 5),
   383  	},
   384  	tDrawTester{
   385  		BgdImage: image_ext.NewYCbCr(image.Rect(0, 0, 10, 10), image.YCbCrSubsampleRatio444),
   386  		BgdColor: color.YCbCr{100, 101, 102},
   387  		FgdImage: image_ext.NewYCbCr(image.Rect(0, 0, 8, 8), image.YCbCrSubsampleRatio444),
   388  		FgdColor: color.YCbCr{250, 251, 252},
   389  		DrawRect: image.Rect(0, 0, 15, 15), // +overflow
   390  		DrawSp:   image.Pt(5, 5),           // +overflow
   391  		FgdRect:  image.Rect(0, 0, 3, 3),
   392  	},
   393  }