| 1 |
package gift |
| 2 |
|
| 3 |
import ( |
| 4 |
"image" |
| 5 |
"image/color" |
| 6 |
"image/draw" |
| 7 |
_ "image/jpeg" |
| 8 |
_ "image/png" |
| 9 |
"os" |
| 10 |
"runtime" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
type testFilter struct { |
| 15 |
z int |
| 16 |
} |
| 17 |
|
| 18 |
func (p *testFilter) Bounds(srcBounds image.Rectangle) (dstBounds image.Rectangle) { |
| 19 |
dstBounds = image.Rect(0, 0, srcBounds.Dx()+p.z, srcBounds.Dy()+p.z*2) |
| 20 |
return |
| 21 |
} |
| 22 |
|
| 23 |
func (p *testFilter) Draw(dst draw.Image, src image.Image, options *Options) { |
| 24 |
dst.Set(dst.Bounds().Min.X, dst.Bounds().Min.Y, color.Gray{123}) |
| 25 |
} |
| 26 |
|
| 27 |
func TestGIFT(t *testing.T) { |
| 28 |
g := New() |
| 29 |
if g.options.Workers != defaultOptions.Workers { |
| 30 |
t.Error("unexpected parallelization property") |
| 31 |
} |
| 32 |
|
| 33 |
g = New( |
| 34 |
&testFilter{1}, |
| 35 |
&testFilter{2}, |
| 36 |
&testFilter{3}, |
| 37 |
) |
| 38 |
if len(g.filters) != 3 { |
| 39 |
t.Error("unexpected filters count") |
| 40 |
} |
| 41 |
|
| 42 |
g = &GIFT{} |
| 43 |
src := image.NewGray(image.Rect(-1, -1, 1, 1)) |
| 44 |
src.Pix = []uint8{ |
| 45 |
1, 2, |
| 46 |
3, 4, |
| 47 |
} |
| 48 |
dst := image.NewGray(g.Bounds(src.Bounds())) |
| 49 |
g.Draw(dst, src) |
| 50 |
if !dst.Bounds().Size().Eq(src.Bounds().Size()) { |
| 51 |
t.Error("unexpected dst bounds") |
| 52 |
} |
| 53 |
for i := range dst.Pix { |
| 54 |
if dst.Pix[i] != src.Pix[i] { |
| 55 |
t.Error("unexpected dst pix") |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
func TestDrawAt(t *testing.T) { |
| 61 |
testDataGray := []struct { |
| 62 |
desc string |
| 63 |
filters []Filter |
| 64 |
pt image.Point |
| 65 |
op Operator |
| 66 |
srcb, dstb image.Rectangle |
| 67 |
srcPix, dstPix0, dstPix1 []uint8 |
| 68 |
}{ |
| 69 |
{ |
| 70 |
"draw at (Gray, [], -2, -2, copy)", |
| 71 |
[]Filter{}, |
| 72 |
image.Pt(-2, -2), |
| 73 |
CopyOperator, |
| 74 |
image.Rect(-1, -1, 2, 2), |
| 75 |
image.Rect(-2, -2, 2, 2), |
| 76 |
[]uint8{1, 2, 3, 4, 5, 6, 7, 8, 9}, |
| 77 |
[]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 78 |
[]uint8{1, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9, 0, 0, 0, 0, 0}, |
| 79 |
}, |
| 80 |
{ |
| 81 |
"draw at (Gray, [], -1, -1, copy)", |
| 82 |
[]Filter{}, |
| 83 |
image.Pt(-1, -1), |
| 84 |
CopyOperator, |
| 85 |
image.Rect(-1, -1, 2, 2), |
| 86 |
image.Rect(-2, -2, 2, 2), |
| 87 |
[]uint8{1, 2, 3, 4, 5, 6, 7, 8, 9}, |
| 88 |
[]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 89 |
[]uint8{0, 0, 0, 0, 0, 1, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9}, |
| 90 |
}, |
| 91 |
{ |
| 92 |
"draw at (Gray, [], 0, 0, copy)", |
| 93 |
[]Filter{}, |
| 94 |
image.Pt(0, 0), |
| 95 |
CopyOperator, |
| 96 |
image.Rect(-1, -1, 2, 2), |
| 97 |
image.Rect(-2, -2, 2, 2), |
| 98 |
[]uint8{1, 2, 3, 4, 5, 6, 7, 8, 9}, |
| 99 |
[]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 100 |
[]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 4, 5}, |
| 101 |
}, |
| 102 |
{ |
| 103 |
"draw at (Gray, [], 2, 2, copy)", |
| 104 |
[]Filter{}, |
| 105 |
image.Pt(2, 2), |
| 106 |
CopyOperator, |
| 107 |
image.Rect(-1, -1, 2, 2), |
| 108 |
image.Rect(-2, -2, 2, 2), |
| 109 |
[]uint8{1, 2, 3, 4, 5, 6, 7, 8, 9}, |
| 110 |
[]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 111 |
[]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 112 |
}, |
| 113 |
{ |
| 114 |
"draw at (Gray, [], 0, -10, copy)", |
| 115 |
[]Filter{}, |
| 116 |
image.Pt(0, -10), |
| 117 |
CopyOperator, |
| 118 |
image.Rect(-1, -1, 2, 2), |
| 119 |
image.Rect(-2, -2, 2, 2), |
| 120 |
[]uint8{1, 2, 3, 4, 5, 6, 7, 8, 9}, |
| 121 |
[]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 122 |
[]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 123 |
}, |
| 124 |
{ |
| 125 |
"draw at (Gray, [], -3, -3, copy)", |
| 126 |
[]Filter{}, |
| 127 |
image.Pt(-3, -3), |
| 128 |
CopyOperator, |
| 129 |
image.Rect(-1, -1, 2, 2), |
| 130 |
image.Rect(-2, -2, 2, 2), |
| 131 |
[]uint8{1, 2, 3, 4, 5, 6, 7, 8, 9}, |
| 132 |
[]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 133 |
[]uint8{5, 6, 0, 0, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 134 |
}, |
| 135 |
{ |
| 136 |
"draw at (Gray, [], -3, -3, over)", |
| 137 |
[]Filter{}, |
| 138 |
image.Pt(-3, -3), |
| 139 |
OverOperator, |
| 140 |
image.Rect(-1, -1, 2, 2), |
| 141 |
image.Rect(-2, -2, 2, 2), |
| 142 |
[]uint8{1, 2, 3, 4, 5, 6, 7, 8, 9}, |
| 143 |
[]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 144 |
[]uint8{5, 6, 0, 0, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 145 |
}, |
| 146 |
{ |
| 147 |
"draw at (Gray, [Resize], -2, -2, copy)", |
| 148 |
[]Filter{Resize(6, 6, NearestNeighborResampling)}, |
| 149 |
image.Pt(-2, -2), |
| 150 |
CopyOperator, |
| 151 |
image.Rect(-1, -1, 2, 2), |
| 152 |
image.Rect(-2, -2, 2, 2), |
| 153 |
[]uint8{1, 2, 3, 4, 5, 6, 7, 8, 9}, |
| 154 |
[]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 155 |
[]uint8{1, 1, 2, 2, 1, 1, 2, 2, 4, 4, 5, 5, 4, 4, 5, 5}, |
| 156 |
}, |
| 157 |
{ |
| 158 |
"draw at (Gray, [Resize], -3, -3, copy)", |
| 159 |
[]Filter{Resize(6, 6, NearestNeighborResampling)}, |
| 160 |
image.Pt(-3, -3), |
| 161 |
CopyOperator, |
| 162 |
image.Rect(-1, -1, 2, 2), |
| 163 |
image.Rect(-2, -2, 2, 2), |
| 164 |
[]uint8{1, 2, 3, 4, 5, 6, 7, 8, 9}, |
| 165 |
[]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 166 |
[]uint8{1, 2, 2, 3, 4, 5, 5, 6, 4, 5, 5, 6, 7, 8, 8, 9}, |
| 167 |
}, |
| 168 |
{ |
| 169 |
"draw at (Gray, [Resize], -1, -1, copy)", |
| 170 |
[]Filter{Resize(6, 6, NearestNeighborResampling)}, |
| 171 |
image.Pt(-1, -1), |
| 172 |
CopyOperator, |
| 173 |
image.Rect(-1, -1, 2, 2), |
| 174 |
image.Rect(-2, -2, 2, 2), |
| 175 |
[]uint8{1, 2, 3, 4, 5, 6, 7, 8, 9}, |
| 176 |
[]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 177 |
[]uint8{0, 0, 0, 0, 0, 1, 1, 2, 0, 1, 1, 2, 0, 4, 4, 5}, |
| 178 |
}, |
| 179 |
{ |
| 180 |
"draw at (Gray, [Resize], -1, -1, copy, empty)", |
| 181 |
[]Filter{Resize(6, 6, NearestNeighborResampling)}, |
| 182 |
image.Pt(-1, -1), |
| 183 |
CopyOperator, |
| 184 |
image.Rect(0, 0, 0, 0), |
| 185 |
image.Rect(0, 0, 0, 0), |
| 186 |
[]uint8{}, |
| 187 |
[]uint8{}, |
| 188 |
[]uint8{}, |
| 189 |
}, |
| 190 |
} |
| 191 |
|
| 192 |
for _, d := range testDataGray { |
| 193 |
src := image.NewGray(d.srcb) |
| 194 |
src.Pix = d.srcPix |
| 195 |
|
| 196 |
g := New(d.filters...) |
| 197 |
|
| 198 |
dst := image.NewGray(d.dstb) |
| 199 |
dst.Pix = d.dstPix0 |
| 200 |
|
| 201 |
if err := g.DrawAt(dst, src, d.pt, d.op); err != nil { |
| 202 |
t.Errorf("test [%s] failed: unexpected error: %v", d.desc, err) |
| 203 |
} |
| 204 |
|
| 205 |
if !checkBoundsAndPix(dst.Bounds(), d.dstb, dst.Pix, d.dstPix1) { |
| 206 |
t.Errorf("test [%s] failed: %#v, %#v", d.desc, dst.Bounds(), dst.Pix) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
testDataNRGBA := []struct { |
| 211 |
desc string |
| 212 |
filters []Filter |
| 213 |
pt image.Point |
| 214 |
op Operator |
| 215 |
srcb, dstb image.Rectangle |
| 216 |
srcPix, dstPix0, dstPix1 []uint8 |
| 217 |
}{ |
| 218 |
{ |
| 219 |
"draw at (NRGBA, [], 1, 1, over, 0% 100% alpha)", |
| 220 |
[]Filter{}, |
| 221 |
image.Pt(1, 1), |
| 222 |
OverOperator, |
| 223 |
image.Rect(0, 0, 2, 2), |
| 224 |
image.Rect(0, 0, 3, 3), |
| 225 |
[]uint8{ |
| 226 |
10, 20, 30, 255, 40, 50, 60, 255, |
| 227 |
100, 200, 0, 255, 0, 250, 200, 255, |
| 228 |
}, |
| 229 |
[]uint8{ |
| 230 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 231 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 232 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 233 |
}, |
| 234 |
[]uint8{ |
| 235 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 236 |
0, 0, 0, 0, 10, 20, 30, 255, 40, 50, 60, 255, |
| 237 |
0, 0, 0, 0, 100, 200, 0, 255, 0, 250, 200, 255, |
| 238 |
}, |
| 239 |
}, |
| 240 |
{ |
| 241 |
"draw at (NRGBA, [], 1, 1, over, 0% 50% alpha)", |
| 242 |
[]Filter{}, |
| 243 |
image.Pt(1, 1), |
| 244 |
OverOperator, |
| 245 |
image.Rect(0, 0, 2, 2), |
| 246 |
image.Rect(0, 0, 3, 3), |
| 247 |
[]uint8{ |
| 248 |
10, 20, 30, 127, 40, 50, 60, 127, |
| 249 |
100, 200, 0, 127, 0, 250, 200, 127, |
| 250 |
}, |
| 251 |
[]uint8{ |
| 252 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 253 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 254 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 255 |
}, |
| 256 |
[]uint8{ |
| 257 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 258 |
0, 0, 0, 0, 10, 20, 30, 127, 40, 50, 60, 127, |
| 259 |
0, 0, 0, 0, 100, 200, 0, 127, 0, 250, 200, 127, |
| 260 |
}, |
| 261 |
}, |
| 262 |
{ |
| 263 |
"draw at (NRGBA, [], 1, 1, over, 100% 50% alpha)", |
| 264 |
[]Filter{}, |
| 265 |
image.Pt(1, 1), |
| 266 |
OverOperator, |
| 267 |
image.Rect(0, 0, 2, 2), |
| 268 |
image.Rect(0, 0, 3, 3), |
| 269 |
[]uint8{ |
| 270 |
10, 20, 30, 128, 40, 50, 60, 128, |
| 271 |
100, 200, 0, 128, 0, 250, 200, 128, |
| 272 |
}, |
| 273 |
[]uint8{ |
| 274 |
0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, |
| 275 |
0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, |
| 276 |
0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, |
| 277 |
}, |
| 278 |
[]uint8{ |
| 279 |
0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, |
| 280 |
0, 0, 0, 255, 5, 10, 15, 255, 20, 25, 30, 255, |
| 281 |
0, 0, 0, 255, 50, 100, 0, 255, 0, 125, 100, 255, |
| 282 |
}, |
| 283 |
}, |
| 284 |
{ |
| 285 |
"draw at (NRGBA, [], 1, 1, over, 100% 25% alpha)", |
| 286 |
[]Filter{}, |
| 287 |
image.Pt(1, 1), |
| 288 |
OverOperator, |
| 289 |
image.Rect(0, 0, 2, 2), |
| 290 |
image.Rect(0, 0, 3, 3), |
| 291 |
[]uint8{ |
| 292 |
20, 40, 80, 64, 40, 80, 120, 64, |
| 293 |
100, 200, 0, 64, 0, 100, 200, 64, |
| 294 |
}, |
| 295 |
[]uint8{ |
| 296 |
0, 0, 0, 255, 1, 2, 3, 255, 0, 0, 0, 255, |
| 297 |
0, 0, 0, 255, 40, 80, 120, 255, 40, 40, 40, 255, |
| 298 |
0, 0, 0, 255, 200, 200, 12, 255, 0, 0, 0, 255, |
| 299 |
}, |
| 300 |
[]uint8{ |
| 301 |
0, 0, 0, 255, 1, 2, 3, 255, 0, 0, 0, 255, |
| 302 |
0, 0, 0, 255, 35, 70, 110, 255, 40, 50, 60, 255, |
| 303 |
0, 0, 0, 255, 175, 200, 9, 255, 0, 25, 50, 255, |
| 304 |
}, |
| 305 |
}, |
| 306 |
{ |
| 307 |
"draw at (NRGBA, [], 1, 1, over, shape)", |
| 308 |
[]Filter{}, |
| 309 |
image.Pt(1, 1), |
| 310 |
OverOperator, |
| 311 |
image.Rect(0, 0, 2, 2), |
| 312 |
image.Rect(0, 0, 3, 3), |
| 313 |
[]uint8{ |
| 314 |
100, 100, 100, 255, 100, 100, 100, 255, |
| 315 |
100, 100, 100, 255, 100, 100, 100, 0, |
| 316 |
}, |
| 317 |
[]uint8{ |
| 318 |
10, 10, 10, 255, 10, 10, 10, 255, 10, 10, 10, 255, |
| 319 |
10, 10, 10, 255, 10, 10, 10, 255, 10, 10, 10, 255, |
| 320 |
10, 10, 10, 255, 10, 10, 10, 255, 10, 10, 10, 255, |
| 321 |
}, |
| 322 |
[]uint8{ |
| 323 |
10, 10, 10, 255, 10, 10, 10, 255, 10, 10, 10, 255, |
| 324 |
10, 10, 10, 255, 100, 100, 100, 255, 100, 100, 100, 255, |
| 325 |
10, 10, 10, 255, 100, 100, 100, 255, 10, 10, 10, 255, |
| 326 |
}, |
| 327 |
}, |
| 328 |
} |
| 329 |
|
| 330 |
for _, d := range testDataNRGBA { |
| 331 |
src := image.NewNRGBA(d.srcb) |
| 332 |
src.Pix = d.srcPix |
| 333 |
|
| 334 |
g := New(d.filters...) |
| 335 |
|
| 336 |
dst := image.NewNRGBA(d.dstb) |
| 337 |
dst.Pix = d.dstPix0 |
| 338 |
|
| 339 |
if err := g.DrawAt(dst, src, d.pt, d.op); err != nil { |
| 340 |
t.Errorf("test [%s] failed: unexpected error: %v", d.desc, err) |
| 341 |
} |
| 342 |
|
| 343 |
if !checkBoundsAndPix(dst.Bounds(), d.dstb, dst.Pix, d.dstPix1) { |
| 344 |
t.Errorf("test [%s] failed: %#v, %#v", d.desc, dst.Bounds(), dst.Pix) |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
type fakeDrawImage struct { |
| 350 |
r image.Rectangle |
| 351 |
} |
| 352 |
|
| 353 |
func (p fakeDrawImage) Bounds() image.Rectangle { return p.r } |
| 354 |
func (p fakeDrawImage) At(x, y int) color.Color { return color.NRGBA{0, 0, 0, 0} } |
| 355 |
func (p fakeDrawImage) ColorModel() color.Model { return color.NRGBAModel } |
| 356 |
func (p fakeDrawImage) Set(x, y int, c color.Color) {} |
| 357 |
|
| 358 |
func TestSubImage(t *testing.T) { |
| 359 |
testData := []struct { |
| 360 |
desc string |
| 361 |
img draw.Image |
| 362 |
ok bool |
| 363 |
}{ |
| 364 |
{ |
| 365 |
"sub image (Gray)", |
| 366 |
image.NewGray(image.Rect(0, 0, 10, 10)), |
| 367 |
true, |
| 368 |
}, |
| 369 |
{ |
| 370 |
"sub image (Gray16)", |
| 371 |
image.NewGray16(image.Rect(0, 0, 10, 10)), |
| 372 |
true, |
| 373 |
}, |
| 374 |
{ |
| 375 |
"sub image (RGBA)", |
| 376 |
image.NewRGBA(image.Rect(0, 0, 10, 10)), |
| 377 |
true, |
| 378 |
}, |
| 379 |
{ |
| 380 |
"sub image (RGBA64)", |
| 381 |
image.NewRGBA64(image.Rect(0, 0, 10, 10)), |
| 382 |
true, |
| 383 |
}, |
| 384 |
{ |
| 385 |
"sub image (NRGBA)", |
| 386 |
image.NewNRGBA(image.Rect(0, 0, 10, 10)), |
| 387 |
true, |
| 388 |
}, |
| 389 |
{ |
| 390 |
"sub image (NRGBA64)", |
| 391 |
image.NewNRGBA64(image.Rect(0, 0, 10, 10)), |
| 392 |
true, |
| 393 |
}, |
| 394 |
{ |
| 395 |
"sub image (fake)", |
| 396 |
fakeDrawImage{image.Rect(0, 0, 10, 10)}, |
| 397 |
false, |
| 398 |
}, |
| 399 |
} |
| 400 |
|
| 401 |
for _, d := range testData { |
| 402 |
simg, ok := getSubImage(d.img, image.Pt(3, 3)) |
| 403 |
if ok != d.ok { |
| 404 |
t.Errorf("test [%s] failed: expected %#v, got %#v", d.desc, d.ok, ok) |
| 405 |
} else if ok { |
| 406 |
simg.Set(5, 5, color.NRGBA{255, 255, 255, 255}) |
| 407 |
r, g, b, a := d.img.At(5, 5).RGBA() |
| 408 |
if r != 0xffff || g != 0xffff || b != 0xffff || a != 0xffff { |
| 409 |
t.Errorf("test [%s] failed: expected (0xffff, 0xffff, 0xffff, 0xffff), got (%d, %d, %d, %d)", d.desc, r, g, b, a) |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
func TestDraw(t *testing.T) { |
| 416 |
filters := [][]Filter{ |
| 417 |
{}, |
| 418 |
{Resize(2, 2, NearestNeighborResampling), Crop(image.Rect(0, 0, 1, 1))}, |
| 419 |
{Resize(2, 2, NearestNeighborResampling), CropToSize(1, 1, CenterAnchor)}, |
| 420 |
{FlipHorizontal()}, |
| 421 |
{FlipVertical()}, |
| 422 |
{Resize(2, 2, NearestNeighborResampling), Resize(1, 1, NearestNeighborResampling)}, |
| 423 |
{Resize(2, 2, NearestNeighborResampling), ResizeToFit(1, 1, NearestNeighborResampling)}, |
| 424 |
{Resize(2, 2, NearestNeighborResampling), ResizeToFill(1, 1, NearestNeighborResampling, CenterAnchor)}, |
| 425 |
{Rotate(45, color.NRGBA{0, 0, 0, 0}, NearestNeighborInterpolation)}, |
| 426 |
{Rotate90()}, |
| 427 |
{Rotate180()}, |
| 428 |
{Rotate270()}, |
| 429 |
{Transpose()}, |
| 430 |
{Transverse()}, |
| 431 |
{Brightness(10)}, |
| 432 |
{ColorBalance(10, 10, 10)}, |
| 433 |
{ColorFunc(func(r0, g0, b0, a0 float32) (r, g, b, a float32) { return 1, 1, 1, 1 })}, |
| 434 |
{Colorize(240, 50, 100)}, |
| 435 |
{ColorspaceLinearToSRGB()}, |
| 436 |
{ColorspaceSRGBToLinear()}, |
| 437 |
{Contrast(10)}, |
| 438 |
{Convolution([]float32{-1, -1, 0, -1, 1, 1, 0, 1, 1}, false, false, false, 0)}, |
| 439 |
{Gamma(1.1)}, |
| 440 |
{GaussianBlur(3)}, |
| 441 |
{Grayscale()}, |
| 442 |
{Hue(90)}, |
| 443 |
{Invert()}, |
| 444 |
{Maximum(3, true)}, |
| 445 |
{Minimum(3, true)}, |
| 446 |
{Mean(3, true)}, |
| 447 |
{Median(3, true)}, |
| 448 |
{Pixelate(3)}, |
| 449 |
{Saturation(10)}, |
| 450 |
{Sepia(10)}, |
| 451 |
{Sigmoid(0.5, 5)}, |
| 452 |
{Sobel()}, |
| 453 |
{UnsharpMask(1, 1.5, 0.001)}, |
| 454 |
} |
| 455 |
|
| 456 |
for i, f := range filters { |
| 457 |
src := image.NewNRGBA(image.Rect(1, 1, 2, 2)) |
| 458 |
src.Pix = []uint8{255, 255, 255, 255} |
| 459 |
g := New(f...) |
| 460 |
dst := image.NewNRGBA(image.Rect(-100, -100, -95, -95)) |
| 461 |
g.Draw(dst, src) |
| 462 |
for x := dst.Bounds().Min.X; x < dst.Bounds().Max.X; x++ { |
| 463 |
for y := dst.Bounds().Min.Y; y < dst.Bounds().Max.Y; y++ { |
| 464 |
failed := false |
| 465 |
if x == -100 && y == -100 { |
| 466 |
if (color.NRGBAModel.Convert(dst.At(x, y)).(color.NRGBA) == color.NRGBA{0, 0, 0, 0}) { |
| 467 |
failed = true |
| 468 |
} |
| 469 |
} else { |
| 470 |
if (color.NRGBAModel.Convert(dst.At(x, y)).(color.NRGBA) != color.NRGBA{0, 0, 0, 0}) { |
| 471 |
failed = true |
| 472 |
} |
| 473 |
} |
| 474 |
if failed { |
| 475 |
t.Errorf("test draw pos failed: %d %#v %#v", i, f, dst.Pix) |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
func loadImage(t *testing.T, filename string) image.Image { |
| 483 |
f, err := os.Open(filename) |
| 484 |
if err != nil { |
| 485 |
t.Fatalf("os.Open (%q) failed: %v", filename, err) |
| 486 |
} |
| 487 |
img, _, err := image.Decode(f) |
| 488 |
if err != nil { |
| 489 |
t.Fatalf("image.Decode (%q) failed: %v", filename, err) |
| 490 |
} |
| 491 |
return img |
| 492 |
} |
| 493 |
|
| 494 |
func loadImageNRGBA(t *testing.T, filename string) *image.NRGBA { |
| 495 |
img := loadImage(t, filename) |
| 496 |
nrgba := image.NewNRGBA(img.Bounds()) |
| 497 |
New().Draw(nrgba, img) |
| 498 |
return nrgba |
| 499 |
} |
| 500 |
|
| 501 |
func TestGolden(t *testing.T) { |
| 502 |
filters := map[string]Filter{ |
| 503 |
"resize": Resize(100, 0, LanczosResampling), |
| 504 |
"crop_to_size": CropToSize(100, 100, LeftAnchor), |
| 505 |
"rotate_180": Rotate180(), |
| 506 |
"rotate_30": Rotate(30, color.Transparent, CubicInterpolation), |
| 507 |
"brightness_increase": Brightness(30), |
| 508 |
"brightness_decrease": Brightness(-30), |
| 509 |
"contrast_increase": Contrast(30), |
| 510 |
"contrast_decrease": Contrast(-30), |
| 511 |
"saturation_increase": Saturation(50), |
| 512 |
"saturation_decrease": Saturation(-50), |
| 513 |
"gamma_1.5": Gamma(1.5), |
| 514 |
"gamma_0.5": Gamma(0.5), |
| 515 |
"gaussian_blur": GaussianBlur(1), |
| 516 |
"unsharp_mask": UnsharpMask(1, 1, 0), |
| 517 |
"sigmoid": Sigmoid(0.5, 7), |
| 518 |
"pixelate": Pixelate(5), |
| 519 |
"colorize": Colorize(240, 50, 100), |
| 520 |
"grayscale": Grayscale(), |
| 521 |
"sepia": Sepia(100), |
| 522 |
"invert": Invert(), |
| 523 |
"mean": Mean(5, true), |
| 524 |
"median": Median(5, true), |
| 525 |
"minimum": Minimum(5, true), |
| 526 |
"maximum": Maximum(5, true), |
| 527 |
"hue_rotate": Hue(45), |
| 528 |
"color_balance": ColorBalance(10, -10, -10), |
| 529 |
"color_func": ColorFunc( |
| 530 |
func(r0, g0, b0, a0 float32) (r, g, b, a float32) { |
| 531 |
r = 1 - r0 |
| 532 |
g = g0 + 0.1 |
| 533 |
b = 0 |
| 534 |
a = a0 |
| 535 |
return r, g, b, a |
| 536 |
}, |
| 537 |
), |
| 538 |
"convolution_emboss": Convolution( |
| 539 |
[]float32{ |
| 540 |
-1, -1, 0, |
| 541 |
-1, 1, 1, |
| 542 |
0, 1, 1, |
| 543 |
}, |
| 544 |
false, false, false, 0, |
| 545 |
), |
| 546 |
} |
| 547 |
src := loadImage(t, "testdata/src.png") |
| 548 |
for name, filter := range filters { |
| 549 |
g := New(filter) |
| 550 |
dst := image.NewNRGBA(g.Bounds(src.Bounds())) |
| 551 |
g.Draw(dst, src) |
| 552 |
want := loadImageNRGBA(t, "testdata/dst_"+name+".png") |
| 553 |
if !goldenEqual(dst, want) { |
| 554 |
t.Errorf("resulting image differs from golden: %s", name) |
| 555 |
} |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
func goldenEqual(img1, img2 *image.NRGBA) bool { |
| 566 |
maxDiff := 0 |
| 567 |
if runtime.GOARCH != "amd64" { |
| 568 |
maxDiff = 1 |
| 569 |
} |
| 570 |
if !img1.Rect.Eq(img2.Rect) { |
| 571 |
return false |
| 572 |
} |
| 573 |
if len(img1.Pix) != len(img2.Pix) { |
| 574 |
return false |
| 575 |
} |
| 576 |
for i := 0; i < len(img1.Pix); i++ { |
| 577 |
diff := int(img1.Pix[i]) - int(img2.Pix[i]) |
| 578 |
if diff < 0 { |
| 579 |
diff = -diff |
| 580 |
} |
| 581 |
if diff > maxDiff { |
| 582 |
return false |
| 583 |
} |
| 584 |
} |
| 585 |
return true |
| 586 |
} |
| 587 |
|
| 588 |
func BenchmarkWorkers(b *testing.B) { |
| 589 |
file, err := os.Open("testdata/src.jpg") |
| 590 |
if err != nil { |
| 591 |
b.Fatalf("failed to open test image: %v", err) |
| 592 |
} |
| 593 |
src, _, err := image.Decode(file) |
| 594 |
if err != nil { |
| 595 |
b.Fatalf("failed to decode test image: %v", err) |
| 596 |
} |
| 597 |
|
| 598 |
runBench := func(b *testing.B, workers int) { |
| 599 |
options := &Options{Workers: workers} |
| 600 |
g := NewWithOptions(*options, Resize(150, 0, LanczosResampling)) |
| 601 |
dst := image.NewNRGBA(g.Bounds(src.Bounds())) |
| 602 |
b.ReportAllocs() |
| 603 |
b.ResetTimer() |
| 604 |
for b.Loop() { |
| 605 |
g.Draw(dst, src) |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
runBenchPara := func(b *testing.B, workers int) { |
| 610 |
b.RunParallel(func(pb *testing.PB) { |
| 611 |
options := &Options{Workers: workers} |
| 612 |
g := NewWithOptions(*options, Resize(150, 0, LanczosResampling)) |
| 613 |
dst := image.NewNRGBA(g.Bounds(src.Bounds())) |
| 614 |
b.ReportAllocs() |
| 615 |
for pb.Next() { |
| 616 |
g.Draw(dst, src) |
| 617 |
} |
| 618 |
}) |
| 619 |
} |
| 620 |
|
| 621 |
b.Run("workers default", func(b *testing.B) { |
| 622 |
runBench(b, 0) |
| 623 |
}) |
| 624 |
|
| 625 |
b.Run("workers 1", func(b *testing.B) { |
| 626 |
runBench(b, 1) |
| 627 |
}) |
| 628 |
|
| 629 |
b.Run("workers 10", func(b *testing.B) { |
| 630 |
runBench(b, 10) |
| 631 |
}) |
| 632 |
|
| 633 |
b.Run("workers default parallel", func(b *testing.B) { |
| 634 |
runBenchPara(b, 0) |
| 635 |
}) |
| 636 |
|
| 637 |
b.Run("workers 1 parallel", func(b *testing.B) { |
| 638 |
runBenchPara(b, 1) |
| 639 |
}) |
| 640 |
|
| 641 |
b.Run("workers 10 parallel", func(b *testing.B) { |
| 642 |
runBenchPara(b, 10) |
| 643 |
}) |
| 644 |
} |
| 645 |
|
| 646 |
func BenchmarkFilter(b *testing.B) { |
| 647 |
file, err := os.Open("testdata/src.jpg") |
| 648 |
if err != nil { |
| 649 |
b.Fatalf("failed to open test image: %v", err) |
| 650 |
} |
| 651 |
src, _, err := image.Decode(file) |
| 652 |
if err != nil { |
| 653 |
b.Fatalf("failed to decode test image: %v", err) |
| 654 |
} |
| 655 |
filters := []struct { |
| 656 |
name string |
| 657 |
filter Filter |
| 658 |
}{ |
| 659 |
{"Resize Lanczos", Resize(150, 0, LanczosResampling)}, |
| 660 |
{"Resize Cubic", Resize(150, 0, CubicResampling)}, |
| 661 |
{"Resize Linear", Resize(150, 0, LinearResampling)}, |
| 662 |
{"Resize Box", Resize(150, 0, BoxResampling)}, |
| 663 |
{"Resize Nearest", Resize(150, 0, NearestNeighborResampling)}, |
| 664 |
{"Crop", Crop(image.Rect(50, 50, 200, 200))}, |
| 665 |
{"CropToSize", CropToSize(150, 150, CenterAnchor)}, |
| 666 |
{"FlipHorizontal", FlipHorizontal()}, |
| 667 |
{"FlipVertical", FlipVertical()}, |
| 668 |
{"Transpose", Transpose()}, |
| 669 |
{"Transverse", Transverse()}, |
| 670 |
{"Rotate90", Rotate90()}, |
| 671 |
{"Rotate180", Rotate180()}, |
| 672 |
{"Rotate270", Rotate270()}, |
| 673 |
{"Rotate", Rotate(30, color.Transparent, CubicInterpolation)}, |
| 674 |
{"Brightness", Brightness(30)}, |
| 675 |
{"Contrast", Contrast(30)}, |
| 676 |
{"Saturation", Saturation(50)}, |
| 677 |
{"Gamma", Gamma(1.5)}, |
| 678 |
{"GaussianBlur", GaussianBlur(1)}, |
| 679 |
{"UnsharpMask", UnsharpMask(1, 1, 0)}, |
| 680 |
{"Sigmoid", Sigmoid(0.5, 7)}, |
| 681 |
{"Pixelate", Pixelate(5)}, |
| 682 |
{"Colorize", Colorize(240, 50, 100)}, |
| 683 |
{"ColorBalance", ColorBalance(10, -10, -10)}, |
| 684 |
{"Threshold", Threshold(50)}, |
| 685 |
{"Hue", Hue(45)}, |
| 686 |
{"Grayscale", Grayscale()}, |
| 687 |
{"Sepia", Sepia(100)}, |
| 688 |
{"Invert", Invert()}, |
| 689 |
{"ColorFunc", ColorFunc( |
| 690 |
func(r0, g0, b0, a0 float32) (r, g, b, a float32) { |
| 691 |
r = 1 - r0 |
| 692 |
g = g0 + 0.1 |
| 693 |
b = 0 |
| 694 |
a = a0 |
| 695 |
return r, g, b, a |
| 696 |
}, |
| 697 |
)}, |
| 698 |
{"ColorspaceSRGBToLinear", ColorspaceSRGBToLinear()}, |
| 699 |
{"ColorspaceLinearToSRGB", ColorspaceLinearToSRGB()}, |
| 700 |
{"Mean", Mean(5, true)}, |
| 701 |
{"Median", Median(5, true)}, |
| 702 |
{"Minimum", Minimum(5, true)}, |
| 703 |
{"Maximum", Maximum(5, true)}, |
| 704 |
{"Convolution", Convolution( |
| 705 |
[]float32{ |
| 706 |
-1, -1, 0, |
| 707 |
-1, 1, 1, |
| 708 |
0, 1, 1, |
| 709 |
}, |
| 710 |
false, false, false, 0, |
| 711 |
)}, |
| 712 |
{"Sobel", Sobel()}, |
| 713 |
} |
| 714 |
for _, f := range filters { |
| 715 |
b.Run(f.name, func(b *testing.B) { |
| 716 |
g := New(f.filter) |
| 717 |
dst := image.NewNRGBA(g.Bounds(src.Bounds())) |
| 718 |
b.ReportAllocs() |
| 719 |
b.ResetTimer() |
| 720 |
for b.Loop() { |
| 721 |
g.Draw(dst, src) |
| 722 |
} |
| 723 |
}) |
| 724 |
} |
| 725 |
} |