ycbcr_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2012 The 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 image
  5. import (
  6. "image/color"
  7. "testing"
  8. )
  9. func TestYCbCr(t *testing.T) {
  10. rects := []Rectangle{
  11. Rect(0, 0, 16, 16),
  12. Rect(1, 0, 16, 16),
  13. Rect(0, 1, 16, 16),
  14. Rect(1, 1, 16, 16),
  15. Rect(1, 1, 15, 16),
  16. Rect(1, 1, 16, 15),
  17. Rect(1, 1, 15, 15),
  18. Rect(2, 3, 14, 15),
  19. Rect(7, 0, 7, 16),
  20. Rect(0, 8, 16, 8),
  21. Rect(0, 0, 10, 11),
  22. Rect(5, 6, 16, 16),
  23. Rect(7, 7, 8, 8),
  24. Rect(7, 8, 8, 9),
  25. Rect(8, 7, 9, 8),
  26. Rect(8, 8, 9, 9),
  27. Rect(7, 7, 17, 17),
  28. Rect(8, 8, 17, 17),
  29. Rect(9, 9, 17, 17),
  30. Rect(10, 10, 17, 17),
  31. }
  32. subsampleRatios := []YCbCrSubsampleRatio{
  33. YCbCrSubsampleRatio444,
  34. YCbCrSubsampleRatio422,
  35. YCbCrSubsampleRatio420,
  36. YCbCrSubsampleRatio440,
  37. }
  38. deltas := []Point{
  39. Pt(0, 0),
  40. Pt(1000, 1001),
  41. Pt(5001, -400),
  42. Pt(-701, -801),
  43. }
  44. for _, r := range rects {
  45. for _, subsampleRatio := range subsampleRatios {
  46. for _, delta := range deltas {
  47. testYCbCr(t, r, subsampleRatio, delta)
  48. }
  49. }
  50. if testing.Short() {
  51. break
  52. }
  53. }
  54. }
  55. func testYCbCr(t *testing.T, r Rectangle, subsampleRatio YCbCrSubsampleRatio, delta Point) {
  56. // Create a YCbCr image m, whose bounds are r translated by (delta.X, delta.Y).
  57. r1 := r.Add(delta)
  58. m := NewYCbCr(r1, subsampleRatio)
  59. // Test that the image buffer is reasonably small even if (delta.X, delta.Y) is far from the origin.
  60. if len(m.Y) > 100*100 {
  61. t.Errorf("r=%v, subsampleRatio=%v, delta=%v: image buffer is too large",
  62. r, subsampleRatio, delta)
  63. return
  64. }
  65. // Initialize m's pixels. For 422 and 420 subsampling, some of the Cb and Cr elements
  66. // will be set multiple times. That's OK. We just want to avoid a uniform image.
  67. for y := r1.Min.Y; y < r1.Max.Y; y++ {
  68. for x := r1.Min.X; x < r1.Max.X; x++ {
  69. yi := m.YOffset(x, y)
  70. ci := m.COffset(x, y)
  71. m.Y[yi] = uint8(16*y + x)
  72. m.Cb[ci] = uint8(y + 16*x)
  73. m.Cr[ci] = uint8(y + 16*x)
  74. }
  75. }
  76. // Make various sub-images of m.
  77. for y0 := delta.Y + 3; y0 < delta.Y+7; y0++ {
  78. for y1 := delta.Y + 8; y1 < delta.Y+13; y1++ {
  79. for x0 := delta.X + 3; x0 < delta.X+7; x0++ {
  80. for x1 := delta.X + 8; x1 < delta.X+13; x1++ {
  81. subRect := Rect(x0, y0, x1, y1)
  82. sub := m.SubImage(subRect).(*YCbCr)
  83. // For each point in the sub-image's bounds, check that m.At(x, y) equals sub.At(x, y).
  84. for y := sub.Rect.Min.Y; y < sub.Rect.Max.Y; y++ {
  85. for x := sub.Rect.Min.X; x < sub.Rect.Max.X; x++ {
  86. color0 := m.At(x, y).(color.YCbCr)
  87. color1 := sub.At(x, y).(color.YCbCr)
  88. if color0 != color1 {
  89. t.Errorf("r=%v, subsampleRatio=%v, delta=%v, x=%d, y=%d, color0=%v, color1=%v",
  90. r, subsampleRatio, delta, x, y, color0, color1)
  91. return
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }