rotate_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2011 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 graphics
  5. import (
  6. "code.google.com/p/graphics-go/graphics/graphicstest"
  7. "image"
  8. "math"
  9. "testing"
  10. _ "image/png"
  11. )
  12. var rotateOneColorTests = []transformOneColorTest{
  13. {
  14. "onepixel-onequarter", 1, 1, 1, 1,
  15. &RotateOptions{math.Pi / 2},
  16. []uint8{0xff},
  17. []uint8{0xff},
  18. },
  19. {
  20. "onepixel-partial", 1, 1, 1, 1,
  21. &RotateOptions{math.Pi * 2.0 / 3.0},
  22. []uint8{0xff},
  23. []uint8{0xff},
  24. },
  25. {
  26. "onepixel-complete", 1, 1, 1, 1,
  27. &RotateOptions{2 * math.Pi},
  28. []uint8{0xff},
  29. []uint8{0xff},
  30. },
  31. {
  32. "even-onequarter", 2, 2, 2, 2,
  33. &RotateOptions{math.Pi / 2.0},
  34. []uint8{
  35. 0xff, 0x00,
  36. 0x00, 0xff,
  37. },
  38. []uint8{
  39. 0x00, 0xff,
  40. 0xff, 0x00,
  41. },
  42. },
  43. {
  44. "even-complete", 2, 2, 2, 2,
  45. &RotateOptions{2.0 * math.Pi},
  46. []uint8{
  47. 0xff, 0x00,
  48. 0x00, 0xff,
  49. },
  50. []uint8{
  51. 0xff, 0x00,
  52. 0x00, 0xff,
  53. },
  54. },
  55. {
  56. "line-partial", 3, 3, 3, 3,
  57. &RotateOptions{math.Pi * 1.0 / 3.0},
  58. []uint8{
  59. 0x00, 0x00, 0x00,
  60. 0xff, 0xff, 0xff,
  61. 0x00, 0x00, 0x00,
  62. },
  63. []uint8{
  64. 0xa2, 0x80, 0x00,
  65. 0x22, 0xff, 0x22,
  66. 0x00, 0x80, 0xa2,
  67. },
  68. },
  69. {
  70. "line-offset-partial", 3, 3, 3, 3,
  71. &RotateOptions{math.Pi * 3 / 2},
  72. []uint8{
  73. 0x00, 0x00, 0x00,
  74. 0x00, 0xff, 0xff,
  75. 0x00, 0x00, 0x00,
  76. },
  77. []uint8{
  78. 0x00, 0xff, 0x00,
  79. 0x00, 0xff, 0x00,
  80. 0x00, 0x00, 0x00,
  81. },
  82. },
  83. {
  84. "dot-partial", 4, 4, 4, 4,
  85. &RotateOptions{math.Pi},
  86. []uint8{
  87. 0x00, 0x00, 0x00, 0x00,
  88. 0x00, 0xff, 0x00, 0x00,
  89. 0x00, 0x00, 0x00, 0x00,
  90. 0x00, 0x00, 0x00, 0x00,
  91. },
  92. []uint8{
  93. 0x00, 0x00, 0x00, 0x00,
  94. 0x00, 0x00, 0x00, 0x00,
  95. 0x00, 0x00, 0xff, 0x00,
  96. 0x00, 0x00, 0x00, 0x00,
  97. },
  98. },
  99. }
  100. func TestRotateOneColor(t *testing.T) {
  101. for _, oc := range rotateOneColorTests {
  102. src := oc.newSrc()
  103. dst := oc.newDst()
  104. if err := Rotate(dst, src, oc.opt.(*RotateOptions)); err != nil {
  105. t.Errorf("rotate %s: %v", oc.desc, err)
  106. continue
  107. }
  108. if !checkTransformTest(t, &oc, dst) {
  109. continue
  110. }
  111. }
  112. }
  113. func TestRotateEmpty(t *testing.T) {
  114. empty := image.NewRGBA(image.Rect(0, 0, 0, 0))
  115. if err := Rotate(empty, empty, nil); err != nil {
  116. t.Fatal(err)
  117. }
  118. }
  119. func TestRotateGopherSide(t *testing.T) {
  120. src, err := graphicstest.LoadImage("../testdata/gopher.png")
  121. if err != nil {
  122. t.Fatal(err)
  123. }
  124. srcb := src.Bounds()
  125. dst := image.NewRGBA(image.Rect(0, 0, srcb.Dy(), srcb.Dx()))
  126. if err := Rotate(dst, src, &RotateOptions{math.Pi / 2.0}); err != nil {
  127. t.Fatal(err)
  128. }
  129. cmp, err := graphicstest.LoadImage("../testdata/gopher-rotate-side.png")
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. err = graphicstest.ImageWithinTolerance(dst, cmp, 0x101)
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. }
  138. func TestRotateGopherPartial(t *testing.T) {
  139. src, err := graphicstest.LoadImage("../testdata/gopher.png")
  140. if err != nil {
  141. t.Fatal(err)
  142. }
  143. srcb := src.Bounds()
  144. dst := image.NewRGBA(image.Rect(0, 0, srcb.Dx(), srcb.Dy()))
  145. if err := Rotate(dst, src, &RotateOptions{math.Pi / 3.0}); err != nil {
  146. t.Fatal(err)
  147. }
  148. cmp, err := graphicstest.LoadImage("../testdata/gopher-rotate-partial.png")
  149. if err != nil {
  150. t.Fatal(err)
  151. }
  152. err = graphicstest.ImageWithinTolerance(dst, cmp, 0x101)
  153. if err != nil {
  154. t.Fatal(err)
  155. }
  156. }