scale_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. "testing"
  9. _ "image/png"
  10. )
  11. var scaleOneColorTests = []transformOneColorTest{
  12. {
  13. "down-half",
  14. 1, 1,
  15. 2, 2,
  16. nil,
  17. []uint8{
  18. 0x80, 0x00,
  19. 0x00, 0x80,
  20. },
  21. []uint8{
  22. 0x40,
  23. },
  24. },
  25. {
  26. "up-double",
  27. 4, 4,
  28. 2, 2,
  29. nil,
  30. []uint8{
  31. 0x80, 0x00,
  32. 0x00, 0x80,
  33. },
  34. []uint8{
  35. 0x80, 0x60, 0x20, 0x00,
  36. 0x60, 0x50, 0x30, 0x20,
  37. 0x20, 0x30, 0x50, 0x60,
  38. 0x00, 0x20, 0x60, 0x80,
  39. },
  40. },
  41. {
  42. "up-doublewidth",
  43. 4, 2,
  44. 2, 2,
  45. nil,
  46. []uint8{
  47. 0x80, 0x00,
  48. 0x00, 0x80,
  49. },
  50. []uint8{
  51. 0x80, 0x60, 0x20, 0x00,
  52. 0x00, 0x20, 0x60, 0x80,
  53. },
  54. },
  55. {
  56. "up-doubleheight",
  57. 2, 4,
  58. 2, 2,
  59. nil,
  60. []uint8{
  61. 0x80, 0x00,
  62. 0x00, 0x80,
  63. },
  64. []uint8{
  65. 0x80, 0x00,
  66. 0x60, 0x20,
  67. 0x20, 0x60,
  68. 0x00, 0x80,
  69. },
  70. },
  71. {
  72. "up-partial",
  73. 3, 3,
  74. 2, 2,
  75. nil,
  76. []uint8{
  77. 0x80, 0x00,
  78. 0x00, 0x80,
  79. },
  80. []uint8{
  81. 0x80, 0x40, 0x00,
  82. 0x40, 0x40, 0x40,
  83. 0x00, 0x40, 0x80,
  84. },
  85. },
  86. }
  87. func TestScaleOneColor(t *testing.T) {
  88. for _, oc := range scaleOneColorTests {
  89. dst := oc.newDst()
  90. src := oc.newSrc()
  91. if err := Scale(dst, src); err != nil {
  92. t.Errorf("scale %s: %v", oc.desc, err)
  93. continue
  94. }
  95. if !checkTransformTest(t, &oc, dst) {
  96. continue
  97. }
  98. }
  99. }
  100. func TestScaleEmpty(t *testing.T) {
  101. empty := image.NewRGBA(image.Rect(0, 0, 0, 0))
  102. if err := Scale(empty, empty); err != nil {
  103. t.Fatal(err)
  104. }
  105. }
  106. func TestScaleGopher(t *testing.T) {
  107. dst := image.NewRGBA(image.Rect(0, 0, 100, 150))
  108. src, err := graphicstest.LoadImage("../testdata/gopher.png")
  109. if err != nil {
  110. t.Error(err)
  111. return
  112. }
  113. // Down-sample.
  114. if err := Scale(dst, src); err != nil {
  115. t.Fatal(err)
  116. }
  117. cmp, err := graphicstest.LoadImage("../testdata/gopher-100x150.png")
  118. if err != nil {
  119. t.Error(err)
  120. return
  121. }
  122. err = graphicstest.ImageWithinTolerance(dst, cmp, 0)
  123. if err != nil {
  124. t.Error(err)
  125. return
  126. }
  127. // Up-sample.
  128. dst = image.NewRGBA(image.Rect(0, 0, 500, 750))
  129. if err := Scale(dst, src); err != nil {
  130. t.Fatal(err)
  131. }
  132. cmp, err = graphicstest.LoadImage("../testdata/gopher-500x750.png")
  133. if err != nil {
  134. t.Error(err)
  135. return
  136. }
  137. err = graphicstest.ImageWithinTolerance(dst, cmp, 0)
  138. if err != nil {
  139. t.Error(err)
  140. return
  141. }
  142. }