github.com/rsc/go@v0.0.0-20150416155037-e040fd465409/src/image/draw/bench_test.go (about) 1 // Copyright 2011 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 draw 6 7 import ( 8 "image" 9 "image/color" 10 "testing" 11 ) 12 13 const ( 14 dstw, dsth = 640, 480 15 srcw, srch = 400, 300 16 ) 17 18 // bench benchmarks drawing src and mask images onto a dst image with the 19 // given op and the color models to create those images from. 20 // The created images' pixels are initialized to non-zero values. 21 func bench(b *testing.B, dcm, scm, mcm color.Model, op Op) { 22 b.StopTimer() 23 24 var dst Image 25 switch dcm { 26 case color.RGBAModel: 27 dst1 := image.NewRGBA(image.Rect(0, 0, dstw, dsth)) 28 for y := 0; y < dsth; y++ { 29 for x := 0; x < dstw; x++ { 30 dst1.SetRGBA(x, y, color.RGBA{ 31 uint8(5 * x % 0x100), 32 uint8(7 * y % 0x100), 33 uint8((7*x + 5*y) % 0x100), 34 0xff, 35 }) 36 } 37 } 38 dst = dst1 39 case color.RGBA64Model: 40 dst1 := image.NewRGBA64(image.Rect(0, 0, dstw, dsth)) 41 for y := 0; y < dsth; y++ { 42 for x := 0; x < dstw; x++ { 43 dst1.SetRGBA64(x, y, color.RGBA64{ 44 uint16(53 * x % 0x10000), 45 uint16(59 * y % 0x10000), 46 uint16((59*x + 53*y) % 0x10000), 47 0xffff, 48 }) 49 } 50 } 51 dst = dst1 52 default: 53 b.Fatal("unknown destination color model", dcm) 54 } 55 56 var src image.Image 57 switch scm { 58 case nil: 59 src = &image.Uniform{C: color.RGBA{0x11, 0x22, 0x33, 0xff}} 60 case color.CMYKModel: 61 src1 := image.NewCMYK(image.Rect(0, 0, srcw, srch)) 62 for y := 0; y < srch; y++ { 63 for x := 0; x < srcw; x++ { 64 src1.SetCMYK(x, y, color.CMYK{ 65 uint8(13 * x % 0x100), 66 uint8(11 * y % 0x100), 67 uint8((11*x + 13*y) % 0x100), 68 uint8((31*x + 37*y) % 0x100), 69 }) 70 } 71 } 72 src = src1 73 case color.GrayModel: 74 src1 := image.NewGray(image.Rect(0, 0, srcw, srch)) 75 for y := 0; y < srch; y++ { 76 for x := 0; x < srcw; x++ { 77 src1.SetGray(x, y, color.Gray{ 78 uint8((11*x + 13*y) % 0x100), 79 }) 80 } 81 } 82 src = src1 83 case color.RGBAModel: 84 src1 := image.NewRGBA(image.Rect(0, 0, srcw, srch)) 85 for y := 0; y < srch; y++ { 86 for x := 0; x < srcw; x++ { 87 src1.SetRGBA(x, y, color.RGBA{ 88 uint8(13 * x % 0x80), 89 uint8(11 * y % 0x80), 90 uint8((11*x + 13*y) % 0x80), 91 0x7f, 92 }) 93 } 94 } 95 src = src1 96 case color.RGBA64Model: 97 src1 := image.NewRGBA64(image.Rect(0, 0, srcw, srch)) 98 for y := 0; y < srch; y++ { 99 for x := 0; x < srcw; x++ { 100 src1.SetRGBA64(x, y, color.RGBA64{ 101 uint16(103 * x % 0x8000), 102 uint16(101 * y % 0x8000), 103 uint16((101*x + 103*y) % 0x8000), 104 0x7fff, 105 }) 106 } 107 } 108 src = src1 109 case color.NRGBAModel: 110 src1 := image.NewNRGBA(image.Rect(0, 0, srcw, srch)) 111 for y := 0; y < srch; y++ { 112 for x := 0; x < srcw; x++ { 113 src1.SetNRGBA(x, y, color.NRGBA{ 114 uint8(13 * x % 0x100), 115 uint8(11 * y % 0x100), 116 uint8((11*x + 13*y) % 0x100), 117 0x7f, 118 }) 119 } 120 } 121 src = src1 122 case color.YCbCrModel: 123 yy := make([]uint8, srcw*srch) 124 cb := make([]uint8, srcw*srch) 125 cr := make([]uint8, srcw*srch) 126 for i := range yy { 127 yy[i] = uint8(3 * i % 0x100) 128 cb[i] = uint8(5 * i % 0x100) 129 cr[i] = uint8(7 * i % 0x100) 130 } 131 src = &image.YCbCr{ 132 Y: yy, 133 Cb: cb, 134 Cr: cr, 135 YStride: srcw, 136 CStride: srcw, 137 SubsampleRatio: image.YCbCrSubsampleRatio444, 138 Rect: image.Rect(0, 0, srcw, srch), 139 } 140 default: 141 b.Fatal("unknown source color model", scm) 142 } 143 144 var mask image.Image 145 switch mcm { 146 case nil: 147 // No-op. 148 case color.AlphaModel: 149 mask1 := image.NewAlpha(image.Rect(0, 0, srcw, srch)) 150 for y := 0; y < srch; y++ { 151 for x := 0; x < srcw; x++ { 152 a := uint8((23*x + 29*y) % 0x100) 153 // Glyph masks are typically mostly zero, 154 // so we only set a quarter of mask1's pixels. 155 if a >= 0xc0 { 156 mask1.SetAlpha(x, y, color.Alpha{a}) 157 } 158 } 159 } 160 mask = mask1 161 default: 162 b.Fatal("unknown mask color model", mcm) 163 } 164 165 b.StartTimer() 166 for i := 0; i < b.N; i++ { 167 // Scatter the destination rectangle to draw into. 168 x := 3 * i % (dstw - srcw) 169 y := 7 * i % (dsth - srch) 170 171 DrawMask(dst, dst.Bounds().Add(image.Pt(x, y)), src, image.ZP, mask, image.ZP, op) 172 } 173 } 174 175 // The BenchmarkFoo functions exercise a drawFoo fast-path function in draw.go. 176 177 func BenchmarkFillOver(b *testing.B) { 178 bench(b, color.RGBAModel, nil, nil, Over) 179 } 180 181 func BenchmarkFillSrc(b *testing.B) { 182 bench(b, color.RGBAModel, nil, nil, Src) 183 } 184 185 func BenchmarkCopyOver(b *testing.B) { 186 bench(b, color.RGBAModel, color.RGBAModel, nil, Over) 187 } 188 189 func BenchmarkCopySrc(b *testing.B) { 190 bench(b, color.RGBAModel, color.RGBAModel, nil, Src) 191 } 192 193 func BenchmarkNRGBAOver(b *testing.B) { 194 bench(b, color.RGBAModel, color.NRGBAModel, nil, Over) 195 } 196 197 func BenchmarkNRGBASrc(b *testing.B) { 198 bench(b, color.RGBAModel, color.NRGBAModel, nil, Src) 199 } 200 201 func BenchmarkYCbCr(b *testing.B) { 202 bench(b, color.RGBAModel, color.YCbCrModel, nil, Over) 203 } 204 205 func BenchmarkGray(b *testing.B) { 206 bench(b, color.RGBAModel, color.GrayModel, nil, Over) 207 } 208 209 func BenchmarkCMYK(b *testing.B) { 210 bench(b, color.RGBAModel, color.CMYKModel, nil, Over) 211 } 212 213 func BenchmarkGlyphOver(b *testing.B) { 214 bench(b, color.RGBAModel, nil, color.AlphaModel, Over) 215 } 216 217 func BenchmarkRGBA(b *testing.B) { 218 bench(b, color.RGBAModel, color.RGBA64Model, nil, Src) 219 } 220 221 // The BenchmarkGenericFoo functions exercise the generic, slow-path code. 222 223 func BenchmarkGenericOver(b *testing.B) { 224 bench(b, color.RGBA64Model, color.RGBA64Model, nil, Over) 225 } 226 227 func BenchmarkGenericMaskOver(b *testing.B) { 228 bench(b, color.RGBA64Model, color.RGBA64Model, color.AlphaModel, Over) 229 } 230 231 func BenchmarkGenericSrc(b *testing.B) { 232 bench(b, color.RGBA64Model, color.RGBA64Model, nil, Src) 233 } 234 235 func BenchmarkGenericMaskSrc(b *testing.B) { 236 bench(b, color.RGBA64Model, color.RGBA64Model, color.AlphaModel, Src) 237 }