1
0
mirror of https://github.com/golang/go synced 2024-11-26 18:16:48 -07:00

image: use three-index slice for NewYCbCr.

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>
This commit is contained in:
Nigel Tao 2015-01-05 13:50:09 +11:00 committed by Rob Pike
parent 43ce5c0306
commit 0b52392ef7
2 changed files with 31 additions and 4 deletions

View 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,

View 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)
}
}
}
}