image_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. package image
  5. import (
  6. "image/color"
  7. "testing"
  8. )
  9. type image interface {
  10. Image
  11. Opaque() bool
  12. Set(int, int, color.Color)
  13. SubImage(Rectangle) Image
  14. }
  15. func cmp(t *testing.T, cm color.Model, c0, c1 color.Color) bool {
  16. r0, g0, b0, a0 := cm.Convert(c0).RGBA()
  17. r1, g1, b1, a1 := cm.Convert(c1).RGBA()
  18. return r0 == r1 && g0 == g1 && b0 == b1 && a0 == a1
  19. }
  20. func TestImage(t *testing.T) {
  21. testImage := []image{
  22. NewRGBA(Rect(0, 0, 10, 10)),
  23. NewRGBA64(Rect(0, 0, 10, 10)),
  24. NewNRGBA(Rect(0, 0, 10, 10)),
  25. NewNRGBA64(Rect(0, 0, 10, 10)),
  26. NewAlpha(Rect(0, 0, 10, 10)),
  27. NewAlpha16(Rect(0, 0, 10, 10)),
  28. NewGray(Rect(0, 0, 10, 10)),
  29. NewGray16(Rect(0, 0, 10, 10)),
  30. NewPaletted(Rect(0, 0, 10, 10), color.Palette{
  31. Transparent,
  32. Opaque,
  33. }),
  34. }
  35. for _, m := range testImage {
  36. if !Rect(0, 0, 10, 10).Eq(m.Bounds()) {
  37. t.Errorf("%T: want bounds %v, got %v", m, Rect(0, 0, 10, 10), m.Bounds())
  38. continue
  39. }
  40. if !cmp(t, m.ColorModel(), Transparent, m.At(6, 3)) {
  41. t.Errorf("%T: at (6, 3), want a zero color, got %v", m, m.At(6, 3))
  42. continue
  43. }
  44. m.Set(6, 3, Opaque)
  45. if !cmp(t, m.ColorModel(), Opaque, m.At(6, 3)) {
  46. t.Errorf("%T: at (6, 3), want a non-zero color, got %v", m, m.At(6, 3))
  47. continue
  48. }
  49. if !m.SubImage(Rect(6, 3, 7, 4)).(image).Opaque() {
  50. t.Errorf("%T: at (6, 3) was not opaque", m)
  51. continue
  52. }
  53. m = m.SubImage(Rect(3, 2, 9, 8)).(image)
  54. if !Rect(3, 2, 9, 8).Eq(m.Bounds()) {
  55. t.Errorf("%T: sub-image want bounds %v, got %v", m, Rect(3, 2, 9, 8), m.Bounds())
  56. continue
  57. }
  58. if !cmp(t, m.ColorModel(), Opaque, m.At(6, 3)) {
  59. t.Errorf("%T: sub-image at (6, 3), want a non-zero color, got %v", m, m.At(6, 3))
  60. continue
  61. }
  62. if !cmp(t, m.ColorModel(), Transparent, m.At(3, 3)) {
  63. t.Errorf("%T: sub-image at (3, 3), want a zero color, got %v", m, m.At(3, 3))
  64. continue
  65. }
  66. m.Set(3, 3, Opaque)
  67. if !cmp(t, m.ColorModel(), Opaque, m.At(3, 3)) {
  68. t.Errorf("%T: sub-image at (3, 3), want a non-zero color, got %v", m, m.At(3, 3))
  69. continue
  70. }
  71. // Test that taking an empty sub-image starting at a corner does not panic.
  72. m.SubImage(Rect(0, 0, 0, 0))
  73. m.SubImage(Rect(10, 0, 10, 0))
  74. m.SubImage(Rect(0, 10, 0, 10))
  75. m.SubImage(Rect(10, 10, 10, 10))
  76. }
  77. }
  78. func Test16BitsPerColorChannel(t *testing.T) {
  79. testColorModel := []color.Model{
  80. color.RGBA64Model,
  81. color.NRGBA64Model,
  82. color.Alpha16Model,
  83. color.Gray16Model,
  84. }
  85. for _, cm := range testColorModel {
  86. c := cm.Convert(color.RGBA64{0x1234, 0x1234, 0x1234, 0x1234}) // Premultiplied alpha.
  87. r, _, _, _ := c.RGBA()
  88. if r != 0x1234 {
  89. t.Errorf("%T: want red value 0x%04x got 0x%04x", c, 0x1234, r)
  90. continue
  91. }
  92. }
  93. testImage := []image{
  94. NewRGBA64(Rect(0, 0, 10, 10)),
  95. NewNRGBA64(Rect(0, 0, 10, 10)),
  96. NewAlpha16(Rect(0, 0, 10, 10)),
  97. NewGray16(Rect(0, 0, 10, 10)),
  98. }
  99. for _, m := range testImage {
  100. m.Set(1, 2, color.NRGBA64{0xffff, 0xffff, 0xffff, 0x1357}) // Non-premultiplied alpha.
  101. r, _, _, _ := m.At(1, 2).RGBA()
  102. if r != 0x1357 {
  103. t.Errorf("%T: want red value 0x%04x got 0x%04x", m, 0x1357, r)
  104. continue
  105. }
  106. }
  107. }