1
0

imagetobytes_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2016 The Ebiten Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ebiten_test
  15. import (
  16. "bytes"
  17. "image"
  18. "image/color"
  19. "image/color/palette"
  20. "testing"
  21. "github.com/hajimehoshi/ebiten/v2"
  22. )
  23. func TestImageToBytes(t *testing.T) {
  24. pal := make(color.Palette, 256)
  25. for i := range pal {
  26. pal[i] = color.White
  27. }
  28. p := make([]color.Color, 255)
  29. for i := range p {
  30. if i == 64 {
  31. p[i] = color.White
  32. } else {
  33. p[i] = color.Transparent
  34. }
  35. }
  36. bigPalette := color.Palette(p)
  37. cases := []struct {
  38. In image.Image
  39. Out []uint8
  40. }{
  41. {
  42. In: &image.Paletted{
  43. Pix: []uint8{0, 1, 1, 0},
  44. Stride: 2,
  45. Rect: image.Rect(0, 0, 2, 2),
  46. Palette: color.Palette([]color.Color{
  47. color.Transparent, color.White,
  48. }),
  49. },
  50. Out: []uint8{0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0},
  51. },
  52. {
  53. In: image.NewPaletted(image.Rect(0, 0, 240, 160), pal).SubImage(image.Rect(238, 158, 240, 160)),
  54. Out: []uint8{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  55. },
  56. {
  57. In: &image.RGBA{
  58. Pix: []uint8{0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0},
  59. Stride: 8,
  60. Rect: image.Rect(0, 0, 2, 2),
  61. },
  62. Out: []uint8{0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0},
  63. },
  64. {
  65. In: &image.NRGBA{
  66. Pix: []uint8{0, 0, 0, 0, 0xff, 0xff, 0xff, 0x80, 0x80, 0x80, 0x80, 0x80, 0, 0, 0, 0},
  67. Stride: 8,
  68. Rect: image.Rect(0, 0, 2, 2),
  69. },
  70. Out: []uint8{0, 0, 0, 0, 0x80, 0x80, 0x80, 0x80, 0x40, 0x40, 0x40, 0x80, 0, 0, 0, 0},
  71. },
  72. {
  73. In: &image.Paletted{
  74. Pix: []uint8{0, 64, 0, 0},
  75. Stride: 2,
  76. Rect: image.Rect(0, 0, 2, 2),
  77. Palette: bigPalette,
  78. },
  79. Out: []uint8{0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0},
  80. },
  81. {
  82. In: (&image.Paletted{
  83. Pix: []uint8{0, 64, 0, 0},
  84. Stride: 2,
  85. Rect: image.Rect(0, 0, 2, 2),
  86. Palette: bigPalette,
  87. }).SubImage(image.Rect(1, 0, 2, 1)),
  88. Out: []uint8{0xff, 0xff, 0xff, 0xff},
  89. },
  90. }
  91. for i, c := range cases {
  92. got := ebiten.ImageToBytes(c.In)
  93. want := c.Out
  94. if !bytes.Equal(got, want) {
  95. t.Errorf("Test %d: got: %v, want: %v", i, got, want)
  96. }
  97. }
  98. }
  99. func BenchmarkImageToBytesRGBA(b *testing.B) {
  100. img := image.NewRGBA(image.Rect(0, 0, 4096, 4096))
  101. b.ResetTimer()
  102. for i := 0; i < b.N; i++ {
  103. ebiten.ImageToBytes(img)
  104. }
  105. }
  106. func BenchmarkImageToBytesNRGBA(b *testing.B) {
  107. img := image.NewNRGBA(image.Rect(0, 0, 4096, 4096))
  108. b.ResetTimer()
  109. for i := 0; i < b.N; i++ {
  110. ebiten.ImageToBytes(img)
  111. }
  112. }
  113. func BenchmarkImageToBytesPaletted(b *testing.B) {
  114. img := image.NewPaletted(image.Rect(0, 0, 4096, 4096), palette.Plan9)
  115. b.ResetTimer()
  116. for i := 0; i < b.N; i++ {
  117. ebiten.ImageToBytes(img)
  118. }
  119. }