No Description

ycc_test.go 6.5KB

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