github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/gift/pixels_test.go (about) 1 package gift 2 3 import ( 4 "image" 5 "image/color" 6 "image/draw" 7 "math" 8 "testing" 9 ) 10 11 func TestNewPixelGetter(t *testing.T) { 12 var img image.Image 13 var pg *pixelGetter 14 img = image.NewNRGBA(image.Rect(0, 0, 1, 1)) 15 pg = newPixelGetter(img) 16 if pg.imgType != itNRGBA || pg.imgNRGBA == nil || !img.Bounds().Eq(pg.imgBounds) { 17 t.Error("newPixelGetter NRGBA") 18 } 19 img = image.NewNRGBA64(image.Rect(0, 0, 1, 1)) 20 pg = newPixelGetter(img) 21 if pg.imgType != itNRGBA64 || pg.imgNRGBA64 == nil || !img.Bounds().Eq(pg.imgBounds) { 22 t.Error("newPixelGetter NRGBA64") 23 } 24 img = image.NewRGBA(image.Rect(0, 0, 1, 1)) 25 pg = newPixelGetter(img) 26 if pg.imgType != itRGBA || pg.imgRGBA == nil || !img.Bounds().Eq(pg.imgBounds) { 27 t.Error("newPixelGetter RGBA") 28 } 29 img = image.NewRGBA64(image.Rect(0, 0, 1, 1)) 30 pg = newPixelGetter(img) 31 if pg.imgType != itRGBA64 || pg.imgRGBA64 == nil || !img.Bounds().Eq(pg.imgBounds) { 32 t.Error("newPixelGetter RGBA64") 33 } 34 img = image.NewGray(image.Rect(0, 0, 1, 1)) 35 pg = newPixelGetter(img) 36 if pg.imgType != itGray || pg.imgGray == nil || !img.Bounds().Eq(pg.imgBounds) { 37 t.Error("newPixelGetter Gray") 38 } 39 img = image.NewGray16(image.Rect(0, 0, 1, 1)) 40 pg = newPixelGetter(img) 41 if pg.imgType != itGray16 || pg.imgGray16 == nil || !img.Bounds().Eq(pg.imgBounds) { 42 t.Error("newPixelGetter Gray16") 43 } 44 img = image.NewYCbCr(image.Rect(0, 0, 1, 1), image.YCbCrSubsampleRatio422) 45 pg = newPixelGetter(img) 46 if pg.imgType != itYCbCr || pg.imgYCbCr == nil || !img.Bounds().Eq(pg.imgBounds) { 47 t.Error("newPixelGetter YCbCr") 48 } 49 img = image.NewUniform(color.NRGBA64{0, 0, 0, 0}) 50 pg = newPixelGetter(img) 51 if pg.imgType != itGeneric || pg.imgGeneric == nil || !img.Bounds().Eq(pg.imgBounds) { 52 t.Error("newPixelGetter Generic(Uniform)") 53 } 54 img = image.NewAlpha(image.Rect(0, 0, 1, 1)) 55 pg = newPixelGetter(img) 56 if pg.imgType != itGeneric || pg.imgGeneric == nil || !img.Bounds().Eq(pg.imgBounds) { 57 t.Error("newPixelGetter Generic(Alpha)") 58 } 59 } 60 61 func comparePixels(px1, px2 pixel, dif float64) bool { 62 if math.Abs(float64(px1.R)-float64(px2.R)) > dif { 63 return false 64 } 65 if math.Abs(float64(px1.G)-float64(px2.G)) > dif { 66 return false 67 } 68 if math.Abs(float64(px1.B)-float64(px2.B)) > dif { 69 return false 70 } 71 if math.Abs(float64(px1.A)-float64(px2.A)) > dif { 72 return false 73 } 74 return true 75 76 } 77 78 func compareColorsNRGBA(c1, c2 color.NRGBA, dif int) bool { 79 if math.Abs(float64(c1.R)-float64(c2.R)) > float64(dif) { 80 return false 81 } 82 if math.Abs(float64(c1.G)-float64(c2.G)) > float64(dif) { 83 return false 84 } 85 if math.Abs(float64(c1.B)-float64(c2.B)) > float64(dif) { 86 return false 87 } 88 if math.Abs(float64(c1.A)-float64(c2.A)) > float64(dif) { 89 return false 90 } 91 return true 92 } 93 94 func TestGetPixel(t *testing.T) { 95 var pg *pixelGetter 96 97 // RGBA, NRGBA, RGBA64, NRGBA64 98 99 palette := []color.Color{ 100 color.NRGBA{0, 0, 0, 0}, 101 color.NRGBA{255, 255, 255, 255}, 102 color.NRGBA{50, 100, 150, 255}, 103 color.NRGBA{150, 100, 50, 200}, 104 } 105 106 images1 := []draw.Image{ 107 image.NewRGBA(image.Rect(-1, -2, 3, 4)), 108 image.NewRGBA64(image.Rect(-1, -2, 3, 4)), 109 image.NewNRGBA(image.Rect(-1, -2, 3, 4)), 110 image.NewNRGBA64(image.Rect(-1, -2, 3, 4)), 111 image.NewPaletted(image.Rect(-1, -2, 3, 4), palette), 112 } 113 114 colors1 := []struct { 115 c color.NRGBA 116 px pixel 117 }{ 118 {color.NRGBA{0, 0, 0, 0}, pixel{0, 0, 0, 0}}, 119 {color.NRGBA{255, 255, 255, 255}, pixel{1, 1, 1, 1}}, 120 {color.NRGBA{50, 100, 150, 255}, pixel{0.196, 0.392, 0.588, 1}}, 121 {color.NRGBA{150, 100, 50, 200}, pixel{0.588, 0.392, 0.196, 0.784}}, 122 } 123 124 for _, img := range images1 { 125 pg = newPixelGetter(img) 126 for _, k := range colors1 { 127 for _, x := range []int{-1, 0, 2} { 128 for _, y := range []int{-2, 0, 3} { 129 img.Set(x, y, k.c) 130 px := pg.getPixel(x, y) 131 if !comparePixels(k.px, px, 0.005) { 132 t.Errorf("getPixel %T %v %dx%d %v %v", img, k.c, x, y, k.px, px) 133 } 134 } 135 } 136 } 137 } 138 139 // Uniform (Generic) 140 141 for _, k := range colors1 { 142 img := image.NewUniform(k.c) 143 pg = newPixelGetter(img) 144 for _, x := range []int{-1, 0, 2} { 145 for _, y := range []int{-2, 0, 3} { 146 px := pg.getPixel(x, y) 147 if !comparePixels(k.px, px, 0.005) { 148 t.Errorf("getPixel %T %v %dx%d %v %v", img, k.c, x, y, k.px, px) 149 } 150 } 151 } 152 } 153 154 // YCbCr 155 156 colors2 := []struct { 157 c color.NRGBA 158 px pixel 159 }{ 160 {color.NRGBA{0, 0, 0, 255}, pixel{0, 0, 0, 1}}, 161 {color.NRGBA{255, 255, 255, 255}, pixel{1, 1, 1, 1}}, 162 {color.NRGBA{50, 100, 150, 255}, pixel{0.196, 0.392, 0.588, 1}}, 163 } 164 165 for _, k := range colors2 { 166 img := image.NewYCbCr(image.Rect(-1, -2, 3, 4), image.YCbCrSubsampleRatio444) 167 pg = newPixelGetter(img) 168 for _, x := range []int{-1, 0, 2} { 169 for _, y := range []int{-2, 0, 3} { 170 iy := img.YOffset(x, y) 171 ic := img.COffset(x, y) 172 img.Y[iy], img.Cb[ic], img.Cr[ic] = color.RGBToYCbCr(k.c.R, k.c.G, k.c.B) 173 px := pg.getPixel(x, y) 174 if !comparePixels(k.px, px, 0.005) { 175 t.Errorf("getPixel %T %v %dx%d %v %v", img, k.c, x, y, k.px, px) 176 } 177 } 178 } 179 } 180 181 // Gray, Gray16 182 183 images2 := []draw.Image{ 184 image.NewGray(image.Rect(-1, -2, 3, 4)), 185 image.NewGray16(image.Rect(-1, -2, 3, 4)), 186 } 187 188 colors3 := []struct { 189 c color.NRGBA 190 px pixel 191 }{ 192 {color.NRGBA{0, 0, 0, 0}, pixel{0, 0, 0, 1}}, 193 {color.NRGBA{255, 255, 255, 255}, pixel{1, 1, 1, 1}}, 194 {color.NRGBA{50, 100, 150, 255}, pixel{0.356, 0.356, 0.356, 1}}, 195 {color.NRGBA{150, 100, 50, 200}, pixel{0.337, 0.337, 0.337, 1}}, 196 } 197 198 for _, img := range images2 { 199 pg = newPixelGetter(img) 200 for _, k := range colors3 { 201 for _, x := range []int{-1, 0, 2} { 202 for _, y := range []int{-2, 0, 3} { 203 img.Set(x, y, k.c) 204 px := pg.getPixel(x, y) 205 if !comparePixels(k.px, px, 0.005) { 206 t.Errorf("getPixel %T %v %dx%d %v %v", img, k.c, x, y, k.px, px) 207 } 208 } 209 } 210 } 211 } 212 } 213 214 func comparePixelSlices(s1, s2 []pixel, dif float64) bool { 215 if len(s1) != len(s2) { 216 return false 217 } 218 for i := 1; i < len(s1); i++ { 219 if !comparePixels(s1[i], s2[i], dif) { 220 return false 221 } 222 } 223 return true 224 } 225 226 func TestGetPixelRow(t *testing.T) { 227 colors := []color.NRGBA{ 228 {0, 0, 0, 0}, 229 {255, 255, 255, 255}, 230 {50, 100, 150, 255}, 231 {150, 100, 50, 200}, 232 } 233 pixels := []pixel{ 234 pixel{0, 0, 0, 0}, 235 pixel{1, 1, 1, 1}, 236 pixel{0.196, 0.392, 0.588, 1}, 237 pixel{0.588, 0.392, 0.196, 0.784}, 238 } 239 240 img := image.NewNRGBA(image.Rect(-1, -2, 3, 2)) 241 pg := newPixelGetter(img) 242 var row []pixel 243 for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ { 244 for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ { 245 img.Set(x, y, colors[x-img.Bounds().Min.X]) 246 } 247 pg.getPixelRow(y, &row) 248 if !comparePixelSlices(row, pixels, 0.005) { 249 t.Errorf("getPixelRow y=%d %v %v", y, row, pixels) 250 } 251 } 252 } 253 254 func TestGetPixelColumn(t *testing.T) { 255 colors := []color.NRGBA{ 256 {0, 0, 0, 0}, 257 {255, 255, 255, 255}, 258 {50, 100, 150, 255}, 259 {150, 100, 50, 200}, 260 } 261 pixels := []pixel{ 262 pixel{0, 0, 0, 0}, 263 pixel{1, 1, 1, 1}, 264 pixel{0.196, 0.392, 0.588, 1}, 265 pixel{0.588, 0.392, 0.196, 0.784}, 266 } 267 268 img := image.NewNRGBA(image.Rect(-1, -2, 3, 2)) 269 pg := newPixelGetter(img) 270 var column []pixel 271 for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ { 272 for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ { 273 img.Set(x, y, colors[y-img.Bounds().Min.Y]) 274 } 275 pg.getPixelColumn(x, &column) 276 if !comparePixelSlices(column, pixels, 0.005) { 277 t.Errorf("getPixelColumn x=%d %v %v", x, column, pixels) 278 } 279 } 280 } 281 282 func TestF32u8(t *testing.T) { 283 testData := []struct { 284 x float32 285 y uint8 286 }{ 287 {-1.0, 0}, 288 {0.0, 0}, 289 {100.0, 100}, 290 {255.0, 255}, 291 {256.0, 255}, 292 } 293 for _, p := range testData { 294 v := f32u8(p.x) 295 if v != p.y { 296 t.Errorf("f32u8(%f) != %d: %d", p.x, p.y, v) 297 } 298 } 299 } 300 301 func TestF32u16(t *testing.T) { 302 testData := []struct { 303 x float32 304 y uint16 305 }{ 306 {-1.0, 0}, 307 {0.0, 0}, 308 {1.0, 1}, 309 {10000.0, 10000}, 310 {65535.0, 65535}, 311 {65536.0, 65535}, 312 } 313 for _, p := range testData { 314 v := f32u16(p.x) 315 if v != p.y { 316 t.Errorf("f32u16(%f) != %d: %d", p.x, p.y, v) 317 } 318 } 319 } 320 321 func TestNewPixelSetter(t *testing.T) { 322 var img draw.Image 323 var pg *pixelSetter 324 img = image.NewNRGBA(image.Rect(0, 0, 1, 1)) 325 pg = newPixelSetter(img) 326 if pg.imgType != itNRGBA || pg.imgNRGBA == nil || !img.Bounds().Eq(pg.imgBounds) { 327 t.Error("newPixelSetter NRGBA") 328 } 329 img = image.NewNRGBA64(image.Rect(0, 0, 1, 1)) 330 pg = newPixelSetter(img) 331 if pg.imgType != itNRGBA64 || pg.imgNRGBA64 == nil || !img.Bounds().Eq(pg.imgBounds) { 332 t.Error("newPixelSetter NRGBA64") 333 } 334 img = image.NewRGBA(image.Rect(0, 0, 1, 1)) 335 pg = newPixelSetter(img) 336 if pg.imgType != itRGBA || pg.imgRGBA == nil || !img.Bounds().Eq(pg.imgBounds) { 337 t.Error("newPixelSetter RGBA") 338 } 339 img = image.NewRGBA64(image.Rect(0, 0, 1, 1)) 340 pg = newPixelSetter(img) 341 if pg.imgType != itRGBA64 || pg.imgRGBA64 == nil || !img.Bounds().Eq(pg.imgBounds) { 342 t.Error("newPixelSetter RGBA64") 343 } 344 img = image.NewGray(image.Rect(0, 0, 1, 1)) 345 pg = newPixelSetter(img) 346 if pg.imgType != itGray || pg.imgGray == nil || !img.Bounds().Eq(pg.imgBounds) { 347 t.Error("newPixelSetter Gray") 348 } 349 img = image.NewGray16(image.Rect(0, 0, 1, 1)) 350 pg = newPixelSetter(img) 351 if pg.imgType != itGray16 || pg.imgGray16 == nil || !img.Bounds().Eq(pg.imgBounds) { 352 t.Error("newPixelSetter Gray16") 353 } 354 img = image.NewPaletted(image.Rect(0, 0, 1, 1), color.Palette{}) 355 pg = newPixelSetter(img) 356 if pg.imgType != itPaletted || pg.imgPaletted == nil || !img.Bounds().Eq(pg.imgBounds) { 357 t.Error("newPixelSetter Paletted") 358 } 359 img = image.NewAlpha(image.Rect(0, 0, 1, 1)) 360 pg = newPixelSetter(img) 361 if pg.imgType != itGeneric || pg.imgGeneric == nil || !img.Bounds().Eq(pg.imgBounds) { 362 t.Error("newPixelSetter Generic(Alpha)") 363 } 364 } 365 366 func TestSetPixel(t *testing.T) { 367 var ps *pixelSetter 368 369 // RGBA, NRGBA, RGBA64, NRGBA64 370 371 images1 := []draw.Image{ 372 image.NewRGBA(image.Rect(-1, -2, 3, 4)), 373 image.NewRGBA64(image.Rect(-1, -2, 3, 4)), 374 image.NewNRGBA(image.Rect(-1, -2, 3, 4)), 375 image.NewNRGBA64(image.Rect(-1, -2, 3, 4)), 376 } 377 378 colors1 := []struct { 379 c color.NRGBA 380 px pixel 381 }{ 382 {color.NRGBA{0, 0, 0, 0}, pixel{0, 0, 0, 0}}, 383 {color.NRGBA{0, 0, 0, 255}, pixel{0, 0, 0, 1}}, 384 {color.NRGBA{255, 255, 255, 255}, pixel{1, 1, 1, 1}}, 385 {color.NRGBA{50, 100, 150, 255}, pixel{0.196, 0.392, 0.588, 1}}, 386 {color.NRGBA{150, 100, 50, 200}, pixel{0.588, 0.392, 0.196, 0.784}}, 387 } 388 389 for _, img := range images1 { 390 ps = newPixelSetter(img) 391 for _, k := range colors1 { 392 for _, x := range []int{-1, 0, 2} { 393 for _, y := range []int{-2, 0, 3} { 394 ps.setPixel(x, y, k.px) 395 c := color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA) 396 if !compareColorsNRGBA(c, k.c, 1) { 397 t.Errorf("setPixel %T %v %dx%d %v %v", img, k.px, x, y, k.c, c) 398 } 399 } 400 } 401 } 402 } 403 404 // Gray, Gray16 405 406 images2 := []draw.Image{ 407 image.NewGray(image.Rect(-1, -2, 3, 4)), 408 image.NewGray16(image.Rect(-1, -2, 3, 4)), 409 } 410 411 colors2 := []struct { 412 c color.NRGBA 413 px pixel 414 }{ 415 {color.NRGBA{0, 0, 0, 255}, pixel{0, 0, 0, 1}}, 416 {color.NRGBA{255, 255, 255, 255}, pixel{1, 1, 1, 1}}, 417 {color.NRGBA{110, 110, 110, 255}, pixel{0.2, 0.5, 0.7, 1}}, 418 {color.NRGBA{55, 55, 55, 255}, pixel{0.2, 0.5, 0.7, 0.5}}, 419 } 420 421 for _, img := range images2 { 422 ps = newPixelSetter(img) 423 for _, k := range colors2 { 424 for _, x := range []int{-1, 0, 2} { 425 for _, y := range []int{-2, 0, 3} { 426 ps.setPixel(x, y, k.px) 427 c := color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA) 428 if !compareColorsNRGBA(c, k.c, 1) { 429 t.Errorf("setPixel %T %v %dx%d %v %v", img, k.px, x, y, k.c, c) 430 } 431 } 432 } 433 } 434 } 435 436 // Generic(Alpha) 437 438 colors3 := []struct { 439 c color.NRGBA 440 px pixel 441 }{ 442 {color.NRGBA{255, 255, 255, 255}, pixel{0, 0, 0, 1}}, 443 {color.NRGBA{255, 255, 255, 127}, pixel{0.2, 0.5, 0.7, 0.5}}, 444 {color.NRGBA{255, 255, 255, 63}, pixel{0.1, 0.2, 0.3, 0.25}}, 445 } 446 447 img := image.NewAlpha(image.Rect(-1, -2, 3, 4)) 448 ps = newPixelSetter(img) 449 for _, k := range colors3 { 450 for _, x := range []int{-1, 0, 2} { 451 for _, y := range []int{-2, 0, 3} { 452 ps.setPixel(x, y, k.px) 453 c := color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA) 454 if !compareColorsNRGBA(c, k.c, 1) { 455 t.Errorf("setPixel %T %v %dx%d %v %v", img, k.px, x, y, k.c, c) 456 } 457 } 458 } 459 } 460 461 // Paletted 462 463 images4 := []draw.Image{ 464 image.NewPaletted( 465 image.Rect(-1, -2, 3, 4), 466 color.Palette{ 467 color.NRGBA{0, 0, 0, 0}, 468 color.NRGBA{0, 0, 0, 255}, 469 color.NRGBA{255, 255, 255, 255}, 470 color.NRGBA{50, 100, 150, 255}, 471 color.NRGBA{150, 100, 50, 200}, 472 color.NRGBA{1, 255, 255, 255}, 473 color.NRGBA{2, 255, 255, 255}, 474 color.NRGBA{3, 255, 255, 255}, 475 }, 476 ), 477 } 478 479 colors4 := []struct { 480 c color.NRGBA 481 px pixel 482 }{ 483 {color.NRGBA{0, 0, 0, 0}, pixel{0, 0, 0, 0}}, 484 {color.NRGBA{0, 0, 0, 255}, pixel{0, 0, 0, 1}}, 485 {color.NRGBA{255, 255, 255, 255}, pixel{1, 1, 1, 1}}, 486 {color.NRGBA{50, 100, 150, 255}, pixel{0.196, 0.392, 0.588, 1}}, 487 {color.NRGBA{150, 100, 50, 200}, pixel{0.588, 0.392, 0.196, 0.784}}, 488 {color.NRGBA{0, 0, 0, 0}, pixel{0.1, 0.01, 0.001, 0.1}}, 489 {color.NRGBA{0, 0, 0, 255}, pixel{0, 0, 0, 0.9}}, 490 {color.NRGBA{255, 255, 255, 255}, pixel{1, 0.9, 1, 0.9}}, 491 {color.NRGBA{1, 255, 255, 255}, pixel{0.001 / 255, 1, 1, 1}}, 492 {color.NRGBA{1, 255, 255, 255}, pixel{1.001 / 255, 1, 1, 1}}, 493 {color.NRGBA{2, 255, 255, 255}, pixel{2.001 / 255, 1, 1, 1}}, 494 {color.NRGBA{3, 255, 255, 255}, pixel{3.001 / 255, 1, 1, 1}}, 495 {color.NRGBA{3, 255, 255, 255}, pixel{4.001 / 255, 1, 1, 1}}, 496 } 497 498 for _, img := range images4 { 499 ps = newPixelSetter(img) 500 for _, k := range colors4 { 501 for _, x := range []int{-1, 0, 2} { 502 for _, y := range []int{-2, 0, 3} { 503 ps.setPixel(x, y, k.px) 504 c := color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA) 505 if !compareColorsNRGBA(c, k.c, 0) { 506 t.Errorf("setPixel %T %v %dx%d %v %v", img, k.px, x, y, k.c, c) 507 } 508 } 509 } 510 } 511 } 512 513 } 514 515 func TestSetPixelRow(t *testing.T) { 516 colors := []color.NRGBA{ 517 {0, 0, 0, 0}, 518 {255, 255, 255, 255}, 519 {50, 100, 150, 255}, 520 {150, 100, 50, 200}, 521 } 522 pixels := []pixel{ 523 pixel{0, 0, 0, 0}, 524 pixel{1, 1, 1, 1}, 525 pixel{0.196, 0.392, 0.588, 1}, 526 pixel{0.588, 0.392, 0.196, 0.784}, 527 } 528 529 img := image.NewNRGBA(image.Rect(-1, -2, 3, 2)) 530 ps := newPixelSetter(img) 531 for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ { 532 ps.setPixelRow(y, pixels) 533 for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ { 534 c := img.At(x, y).(color.NRGBA) 535 wantedColor := colors[x-img.Bounds().Min.X] 536 if !compareColorsNRGBA(wantedColor, c, 1) { 537 t.Errorf("setPixelRow y=%d x=%d %v %v", y, x, wantedColor, c) 538 } 539 } 540 } 541 } 542 543 func TestSetPixelColumn(t *testing.T) { 544 colors := []color.NRGBA{ 545 {0, 0, 0, 0}, 546 {255, 255, 255, 255}, 547 {50, 100, 150, 255}, 548 {150, 100, 50, 200}, 549 } 550 pixels := []pixel{ 551 pixel{0, 0, 0, 0}, 552 pixel{1, 1, 1, 1}, 553 pixel{0.196, 0.392, 0.588, 1}, 554 pixel{0.588, 0.392, 0.196, 0.784}, 555 } 556 557 img := image.NewNRGBA(image.Rect(-1, -2, 3, 2)) 558 ps := newPixelSetter(img) 559 for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ { 560 ps.setPixelColumn(x, pixels) 561 for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ { 562 c := img.At(x, y).(color.NRGBA) 563 wantedColor := colors[y-img.Bounds().Min.Y] 564 if !compareColorsNRGBA(wantedColor, c, 1) { 565 t.Errorf("setPixelColumn x=%d y=%d %v %v", x, y, wantedColor, c) 566 } 567 } 568 } 569 }