bilinear_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2012 The Graphics-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. package interp
  5. import (
  6. "image"
  7. "image/color"
  8. "testing"
  9. )
  10. type interpTest struct {
  11. desc string
  12. src []uint8
  13. srcWidth int
  14. x, y float64
  15. expect uint8
  16. }
  17. func (p *interpTest) newSrc() *image.RGBA {
  18. b := image.Rect(0, 0, p.srcWidth, len(p.src)/p.srcWidth)
  19. src := image.NewRGBA(b)
  20. i := 0
  21. for y := b.Min.Y; y < b.Max.Y; y++ {
  22. for x := b.Min.X; x < b.Max.X; x++ {
  23. src.SetRGBA(x, y, color.RGBA{
  24. R: p.src[i],
  25. G: p.src[i],
  26. B: p.src[i],
  27. A: 0xff,
  28. })
  29. i++
  30. }
  31. }
  32. return src
  33. }
  34. var interpTests = []interpTest{
  35. {
  36. desc: "center of a single white pixel should match that pixel",
  37. src: []uint8{0x00},
  38. srcWidth: 1,
  39. x: 0.5,
  40. y: 0.5,
  41. expect: 0x00,
  42. },
  43. {
  44. desc: "middle of a square is equally weighted",
  45. src: []uint8{
  46. 0x00, 0xff,
  47. 0xff, 0x00,
  48. },
  49. srcWidth: 2,
  50. x: 1.0,
  51. y: 1.0,
  52. expect: 0x80,
  53. },
  54. {
  55. desc: "center of a pixel is just that pixel",
  56. src: []uint8{
  57. 0x00, 0xff,
  58. 0xff, 0x00,
  59. },
  60. srcWidth: 2,
  61. x: 1.5,
  62. y: 0.5,
  63. expect: 0xff,
  64. },
  65. {
  66. desc: "asymmetry abounds",
  67. src: []uint8{
  68. 0xaa, 0x11, 0x55,
  69. 0xff, 0x95, 0xdd,
  70. },
  71. srcWidth: 3,
  72. x: 2.0,
  73. y: 1.0,
  74. expect: 0x76, // (0x11 + 0x55 + 0x95 + 0xdd) / 4
  75. },
  76. }
  77. func TestBilinearRGBA(t *testing.T) {
  78. for _, p := range interpTests {
  79. src := p.newSrc()
  80. // Fast path.
  81. c := Bilinear.(RGBA).RGBA(src, p.x, p.y)
  82. if c.R != c.G || c.R != c.B || c.A != 0xff {
  83. t.Errorf("expect channels to match, got %v", c)
  84. continue
  85. }
  86. if c.R != p.expect {
  87. t.Errorf("%s: got 0x%02x want 0x%02x", p.desc, c.R, p.expect)
  88. continue
  89. }
  90. // Standard Interp should use the fast path.
  91. cStd := Bilinear.Interp(src, p.x, p.y)
  92. if cStd != c {
  93. t.Errorf("%s: standard mismatch got %v want %v", p.desc, cStd, c)
  94. continue
  95. }
  96. // General case should match the fast path.
  97. cGen := color.RGBAModel.Convert(bilinearGeneral(src, p.x, p.y))
  98. r0, g0, b0, a0 := c.RGBA()
  99. r1, g1, b1, a1 := cGen.RGBA()
  100. if r0 != r1 || g0 != g1 || b0 != b1 || a0 != a1 {
  101. t.Errorf("%s: general case mismatch got %v want %v", p.desc, c, cGen)
  102. continue
  103. }
  104. }
  105. }
  106. func TestBilinearSubImage(t *testing.T) {
  107. b0 := image.Rect(0, 0, 4, 4)
  108. src0 := image.NewRGBA(b0)
  109. b1 := image.Rect(1, 1, 3, 3)
  110. src1 := src0.SubImage(b1).(*image.RGBA)
  111. src1.Set(1, 1, color.RGBA{0x11, 0, 0, 0xff})
  112. src1.Set(2, 1, color.RGBA{0x22, 0, 0, 0xff})
  113. src1.Set(1, 2, color.RGBA{0x33, 0, 0, 0xff})
  114. src1.Set(2, 2, color.RGBA{0x44, 0, 0, 0xff})
  115. tests := []struct {
  116. x, y float64
  117. want uint8
  118. }{
  119. {1, 1, 0x11},
  120. {3, 1, 0x22},
  121. {1, 3, 0x33},
  122. {3, 3, 0x44},
  123. {2, 2, 0x2b},
  124. }
  125. for _, p := range tests {
  126. c := Bilinear.(RGBA).RGBA(src1, p.x, p.y)
  127. if c.R != p.want {
  128. t.Errorf("(%.0f, %.0f): got 0x%02x want 0x%02x", p.x, p.y, c.R, p.want)
  129. }
  130. }
  131. }