helpers_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package imaging
  2. import (
  3. "bytes"
  4. "image"
  5. "image/color"
  6. "testing"
  7. )
  8. func compareNRGBA(img1, img2 *image.NRGBA, delta int) bool {
  9. if !img1.Rect.Eq(img2.Rect) {
  10. return false
  11. }
  12. if len(img1.Pix) != len(img2.Pix) {
  13. return false
  14. }
  15. for i := 0; i < len(img1.Pix); i++ {
  16. if absint(int(img1.Pix[i])-int(img2.Pix[i])) > delta {
  17. return false
  18. }
  19. }
  20. return true
  21. }
  22. func TestEncodeDecode(t *testing.T) {
  23. imgWithAlpha := image.NewNRGBA(image.Rect(0, 0, 3, 3))
  24. imgWithAlpha.Pix = []uint8{
  25. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  26. 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138,
  27. 244, 245, 246, 247, 248, 249, 250, 252, 252, 253, 254, 255,
  28. }
  29. imgWithoutAlpha := image.NewNRGBA(image.Rect(0, 0, 3, 3))
  30. imgWithoutAlpha.Pix = []uint8{
  31. 0, 1, 2, 255, 4, 5, 6, 255, 8, 9, 10, 255,
  32. 127, 128, 129, 255, 131, 132, 133, 255, 135, 136, 137, 255,
  33. 244, 245, 246, 255, 248, 249, 250, 255, 252, 253, 254, 255,
  34. }
  35. for _, format := range []Format{JPEG, PNG, GIF, BMP, TIFF} {
  36. img := imgWithoutAlpha
  37. if format == PNG {
  38. img = imgWithAlpha
  39. }
  40. buf := &bytes.Buffer{}
  41. err := Encode(buf, img, format)
  42. if err != nil {
  43. t.Errorf("fail encoding format %s", format)
  44. continue
  45. }
  46. img2, err := Decode(buf)
  47. if err != nil {
  48. t.Errorf("fail decoding format %s", format)
  49. continue
  50. }
  51. img2cloned := Clone(img2)
  52. delta := 0
  53. if format == JPEG {
  54. delta = 3
  55. } else if format == GIF {
  56. delta = 16
  57. }
  58. if !compareNRGBA(img, img2cloned, delta) {
  59. t.Errorf("test [DecodeEncode %s] failed: %#v %#v", format, img, img2cloned)
  60. continue
  61. }
  62. }
  63. buf := &bytes.Buffer{}
  64. err := Encode(buf, imgWithAlpha, JPEG)
  65. if err != nil {
  66. t.Errorf("failed encoding alpha to JPEG format %s", err)
  67. }
  68. buf = &bytes.Buffer{}
  69. err = Encode(buf, imgWithAlpha, Format(100))
  70. if err != ErrUnsupportedFormat {
  71. t.Errorf("expected ErrUnsupportedFormat")
  72. }
  73. buf = bytes.NewBuffer([]byte("bad data"))
  74. _, err = Decode(buf)
  75. if err == nil {
  76. t.Errorf("decoding bad data, expected error")
  77. }
  78. }
  79. func TestNew(t *testing.T) {
  80. td := []struct {
  81. desc string
  82. w, h int
  83. c color.Color
  84. dstBounds image.Rectangle
  85. dstPix []uint8
  86. }{
  87. {
  88. "New 1x1 black",
  89. 1, 1,
  90. color.NRGBA{0, 0, 0, 0},
  91. image.Rect(0, 0, 1, 1),
  92. []uint8{0x00, 0x00, 0x00, 0x00},
  93. },
  94. {
  95. "New 1x2 red",
  96. 1, 2,
  97. color.NRGBA{255, 0, 0, 255},
  98. image.Rect(0, 0, 1, 2),
  99. []uint8{0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff},
  100. },
  101. {
  102. "New 2x1 white",
  103. 2, 1,
  104. color.NRGBA{255, 255, 255, 255},
  105. image.Rect(0, 0, 2, 1),
  106. []uint8{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  107. },
  108. {
  109. "New 0x0 white",
  110. 0, 0,
  111. color.NRGBA{255, 255, 255, 255},
  112. image.Rect(0, 0, 0, 0),
  113. nil,
  114. },
  115. }
  116. for _, d := range td {
  117. got := New(d.w, d.h, d.c)
  118. want := image.NewNRGBA(d.dstBounds)
  119. want.Pix = d.dstPix
  120. if !compareNRGBA(got, want, 0) {
  121. t.Errorf("test [%s] failed: %#v", d.desc, got)
  122. }
  123. }
  124. }
  125. func BenchmarkNew(b *testing.B) {
  126. b.ReportAllocs()
  127. for i := 0; i < b.N; i++ {
  128. New(1024, 1024, color.White)
  129. }
  130. }
  131. func TestFormats(t *testing.T) {
  132. formatNames := map[Format]string{
  133. JPEG: "JPEG",
  134. PNG: "PNG",
  135. GIF: "GIF",
  136. BMP: "BMP",
  137. TIFF: "TIFF",
  138. Format(-1): "Unsupported",
  139. }
  140. for format, name := range formatNames {
  141. got := format.String()
  142. if got != name {
  143. t.Errorf("test [Format names] failed: got %#v want %#v", got, name)
  144. continue
  145. }
  146. }
  147. }