]> Cypherpunks repositories - gostls13.git/commitdiff
image: use three-index slice for NewYCbCr.
authorNigel Tao <nigeltao@golang.org>
Mon, 5 Jan 2015 02:50:09 +0000 (13:50 +1100)
committerRob Pike <r@golang.org>
Mon, 5 Jan 2015 03:32:15 +0000 (03:32 +0000)
This ensures that changing an image.YCbCr's Y values can't change its
chroma values, even after re-slicing up to capacity.

Change-Id: Icb626561522e336a3220e10f456c95330ae7db9e
Reviewed-on: https://go-review.googlesource.com/2209
Reviewed-by: Rob Pike <r@golang.org>
src/image/ycbcr.go
src/image/ycbcr_test.go

index 7c773f2f0a496049b763e60f3bb608c74a197312..0126060d00c1bae82e590fa6ae4112c6f66057fe 100644 (file)
@@ -144,11 +144,14 @@ func NewYCbCr(r Rectangle, subsampleRatio YCbCrSubsampleRatio) *YCbCr {
                cw = w
                ch = h
        }
-       b := make([]byte, w*h+2*cw*ch)
+       i0 := w*h + 0*cw*ch
+       i1 := w*h + 1*cw*ch
+       i2 := w*h + 2*cw*ch
+       b := make([]byte, i2)
        return &YCbCr{
-               Y:              b[:w*h],
-               Cb:             b[w*h+0*cw*ch : w*h+1*cw*ch],
-               Cr:             b[w*h+1*cw*ch : w*h+2*cw*ch],
+               Y:              b[:i0:i0],
+               Cb:             b[i0:i1:i1],
+               Cr:             b[i1:i2:i2],
                SubsampleRatio: subsampleRatio,
                YStride:        w,
                CStride:        cw,
index a5f4482654feba6f29f246ef55d26b3eb0dc684b..5ab4fc3fea44cb1f5615e67ff4dff9ab3a2c45a7 100644 (file)
@@ -105,3 +105,27 @@ func testYCbCr(t *testing.T, r Rectangle, subsampleRatio YCbCrSubsampleRatio, de
                }
        }
 }
+
+func TestYCbCrSlicesDontOverlap(t *testing.T) {
+       m := NewYCbCr(Rect(0, 0, 8, 8), YCbCrSubsampleRatio420)
+       names := []string{"Y", "Cb", "Cr"}
+       slices := [][]byte{
+               m.Y[:cap(m.Y)],
+               m.Cb[:cap(m.Cb)],
+               m.Cr[:cap(m.Cr)],
+       }
+       for i, slice := range slices {
+               want := uint8(10 + i)
+               for j := range slice {
+                       slice[j] = want
+               }
+       }
+       for i, slice := range slices {
+               want := uint8(10 + i)
+               for j, got := range slice {
+                       if got != want {
+                               t.Fatalf("m.%s[%d]: got %d, want %d", names[i], j, got, want)
+                       }
+               }
+       }
+}