ycc_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // +build go1.5
  2. /*
  3. Copyright (c) 2014, Charlie Vieth <charlie.vieth@gmail.com>
  4. Permission to use, copy, modify, and/or distribute this software for any purpose
  5. with or without fee is hereby granted, provided that the above copyright notice
  6. and this permission notice appear in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  8. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  11. OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  12. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  13. THIS SOFTWARE.
  14. */
  15. package resize
  16. import (
  17. "image"
  18. "image/color"
  19. "testing"
  20. )
  21. type Image interface {
  22. image.Image
  23. SubImage(image.Rectangle) image.Image
  24. }
  25. func TestImage(t *testing.T) {
  26. testImage := []Image{
  27. newYCC(image.Rect(0, 0, 10, 10), image.YCbCrSubsampleRatio420),
  28. newYCC(image.Rect(0, 0, 10, 10), image.YCbCrSubsampleRatio422),
  29. newYCC(image.Rect(0, 0, 10, 10), image.YCbCrSubsampleRatio440),
  30. newYCC(image.Rect(0, 0, 10, 10), image.YCbCrSubsampleRatio444),
  31. newYCC(image.Rect(0, 0, 10, 10), image.YCbCrSubsampleRatio411),
  32. newYCC(image.Rect(0, 0, 10, 10), image.YCbCrSubsampleRatio410),
  33. }
  34. for _, m := range testImage {
  35. if !image.Rect(0, 0, 10, 10).Eq(m.Bounds()) {
  36. t.Errorf("%T: want bounds %v, got %v",
  37. m, image.Rect(0, 0, 10, 10), m.Bounds())
  38. continue
  39. }
  40. m = m.SubImage(image.Rect(3, 2, 9, 8)).(Image)
  41. if !image.Rect(3, 2, 9, 8).Eq(m.Bounds()) {
  42. t.Errorf("%T: sub-image want bounds %v, got %v",
  43. m, image.Rect(3, 2, 9, 8), m.Bounds())
  44. continue
  45. }
  46. // Test that taking an empty sub-image starting at a corner does not panic.
  47. m.SubImage(image.Rect(0, 0, 0, 0))
  48. m.SubImage(image.Rect(10, 0, 10, 0))
  49. m.SubImage(image.Rect(0, 10, 0, 10))
  50. m.SubImage(image.Rect(10, 10, 10, 10))
  51. }
  52. }
  53. func TestConvertYCbCr(t *testing.T) {
  54. testImage := []Image{
  55. image.NewYCbCr(image.Rect(0, 0, 50, 50), image.YCbCrSubsampleRatio420),
  56. image.NewYCbCr(image.Rect(0, 0, 50, 50), image.YCbCrSubsampleRatio422),
  57. image.NewYCbCr(image.Rect(0, 0, 50, 50), image.YCbCrSubsampleRatio440),
  58. image.NewYCbCr(image.Rect(0, 0, 50, 50), image.YCbCrSubsampleRatio444),
  59. image.NewYCbCr(image.Rect(0, 0, 10, 10), image.YCbCrSubsampleRatio411),
  60. image.NewYCbCr(image.Rect(0, 0, 10, 10), image.YCbCrSubsampleRatio410),
  61. }
  62. for _, img := range testImage {
  63. m := img.(*image.YCbCr)
  64. for y := m.Rect.Min.Y; y < m.Rect.Max.Y; y++ {
  65. for x := m.Rect.Min.X; x < m.Rect.Max.X; x++ {
  66. yi := m.YOffset(x, y)
  67. ci := m.COffset(x, y)
  68. m.Y[yi] = uint8(16*y + x)
  69. m.Cb[ci] = uint8(y + 16*x)
  70. m.Cr[ci] = uint8(y + 16*x)
  71. }
  72. }
  73. // test conversion from YCbCr to ycc
  74. yc := imageYCbCrToYCC(m)
  75. for y := m.Rect.Min.Y; y < m.Rect.Max.Y; y++ {
  76. for x := m.Rect.Min.X; x < m.Rect.Max.X; x++ {
  77. ystride := 3 * (m.Rect.Max.X - m.Rect.Min.X)
  78. xstride := 3
  79. yi := m.YOffset(x, y)
  80. ci := m.COffset(x, y)
  81. si := (y * ystride) + (x * xstride)
  82. if m.Y[yi] != yc.Pix[si] {
  83. t.Errorf("Err Y - found: %d expected: %d x: %d y: %d yi: %d si: %d",
  84. m.Y[yi], yc.Pix[si], x, y, yi, si)
  85. }
  86. if m.Cb[ci] != yc.Pix[si+1] {
  87. t.Errorf("Err Cb - found: %d expected: %d x: %d y: %d ci: %d si: %d",
  88. m.Cb[ci], yc.Pix[si+1], x, y, ci, si+1)
  89. }
  90. if m.Cr[ci] != yc.Pix[si+2] {
  91. t.Errorf("Err Cr - found: %d expected: %d x: %d y: %d ci: %d si: %d",
  92. m.Cr[ci], yc.Pix[si+2], x, y, ci, si+2)
  93. }
  94. }
  95. }
  96. // test conversion from ycc back to YCbCr
  97. ym := yc.YCbCr()
  98. for y := m.Rect.Min.Y; y < m.Rect.Max.Y; y++ {
  99. for x := m.Rect.Min.X; x < m.Rect.Max.X; x++ {
  100. yi := m.YOffset(x, y)
  101. ci := m.COffset(x, y)
  102. if m.Y[yi] != ym.Y[yi] {
  103. t.Errorf("Err Y - found: %d expected: %d x: %d y: %d yi: %d",
  104. m.Y[yi], ym.Y[yi], x, y, yi)
  105. }
  106. if m.Cb[ci] != ym.Cb[ci] {
  107. t.Errorf("Err Cb - found: %d expected: %d x: %d y: %d ci: %d",
  108. m.Cb[ci], ym.Cb[ci], x, y, ci)
  109. }
  110. if m.Cr[ci] != ym.Cr[ci] {
  111. t.Errorf("Err Cr - found: %d expected: %d x: %d y: %d ci: %d",
  112. m.Cr[ci], ym.Cr[ci], x, y, ci)
  113. }
  114. }
  115. }
  116. }
  117. }
  118. func TestYCbCr(t *testing.T) {
  119. rects := []image.Rectangle{
  120. image.Rect(0, 0, 16, 16),
  121. image.Rect(1, 0, 16, 16),
  122. image.Rect(0, 1, 16, 16),
  123. image.Rect(1, 1, 16, 16),
  124. image.Rect(1, 1, 15, 16),
  125. image.Rect(1, 1, 16, 15),
  126. image.Rect(1, 1, 15, 15),
  127. image.Rect(2, 3, 14, 15),
  128. image.Rect(7, 0, 7, 16),
  129. image.Rect(0, 8, 16, 8),
  130. image.Rect(0, 0, 10, 11),
  131. image.Rect(5, 6, 16, 16),
  132. image.Rect(7, 7, 8, 8),
  133. image.Rect(7, 8, 8, 9),
  134. image.Rect(8, 7, 9, 8),
  135. image.Rect(8, 8, 9, 9),
  136. image.Rect(7, 7, 17, 17),
  137. image.Rect(8, 8, 17, 17),
  138. image.Rect(9, 9, 17, 17),
  139. image.Rect(10, 10, 17, 17),
  140. }
  141. subsampleRatios := []image.YCbCrSubsampleRatio{
  142. image.YCbCrSubsampleRatio444,
  143. image.YCbCrSubsampleRatio422,
  144. image.YCbCrSubsampleRatio420,
  145. image.YCbCrSubsampleRatio440,
  146. }
  147. deltas := []image.Point{
  148. image.Pt(0, 0),
  149. image.Pt(1000, 1001),
  150. image.Pt(5001, -400),
  151. image.Pt(-701, -801),
  152. }
  153. for _, r := range rects {
  154. for _, subsampleRatio := range subsampleRatios {
  155. for _, delta := range deltas {
  156. testYCbCr(t, r, subsampleRatio, delta)
  157. }
  158. }
  159. if testing.Short() {
  160. break
  161. }
  162. }
  163. }
  164. func testYCbCr(t *testing.T, r image.Rectangle, subsampleRatio image.YCbCrSubsampleRatio, delta image.Point) {
  165. // Create a YCbCr image m, whose bounds are r translated by (delta.X, delta.Y).
  166. r1 := r.Add(delta)
  167. img := image.NewYCbCr(r1, subsampleRatio)
  168. // Initialize img's pixels. For 422 and 420 subsampling, some of the Cb and Cr elements
  169. // will be set multiple times. That's OK. We just want to avoid a uniform image.
  170. for y := r1.Min.Y; y < r1.Max.Y; y++ {
  171. for x := r1.Min.X; x < r1.Max.X; x++ {
  172. yi := img.YOffset(x, y)
  173. ci := img.COffset(x, y)
  174. img.Y[yi] = uint8(16*y + x)
  175. img.Cb[ci] = uint8(y + 16*x)
  176. img.Cr[ci] = uint8(y + 16*x)
  177. }
  178. }
  179. m := imageYCbCrToYCC(img)
  180. // Make various sub-images of m.
  181. for y0 := delta.Y + 3; y0 < delta.Y+7; y0++ {
  182. for y1 := delta.Y + 8; y1 < delta.Y+13; y1++ {
  183. for x0 := delta.X + 3; x0 < delta.X+7; x0++ {
  184. for x1 := delta.X + 8; x1 < delta.X+13; x1++ {
  185. subRect := image.Rect(x0, y0, x1, y1)
  186. sub := m.SubImage(subRect).(*ycc)
  187. // For each point in the sub-image's bounds, check that m.At(x, y) equals sub.At(x, y).
  188. for y := sub.Rect.Min.Y; y < sub.Rect.Max.Y; y++ {
  189. for x := sub.Rect.Min.X; x < sub.Rect.Max.X; x++ {
  190. color0 := m.At(x, y).(color.YCbCr)
  191. color1 := sub.At(x, y).(color.YCbCr)
  192. if color0 != color1 {
  193. t.Errorf("r=%v, subsampleRatio=%v, delta=%v, x=%d, y=%d, color0=%v, color1=%v",
  194. r, subsampleRatio, delta, x, y, color0, color1)
  195. return
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }
  202. }
  203. }