Przeglądaj źródła

Fix error in YCC PixOffset and SubImage.

Charlie Vieth 10 lat temu
rodzic
commit
acea8646b2
2 zmienionych plików z 94 dodań i 1 usunięć
  1. 1
    1
      ycc.go
  2. 93
    0
      ycc_test.go

+ 1
- 1
ycc.go Wyświetl plik

@@ -38,7 +38,7 @@ type ycc struct {
38 38
 // PixOffset returns the index of the first element of Pix that corresponds to
39 39
 // the pixel at (x, y).
40 40
 func (p *ycc) PixOffset(x, y int) int {
41
-	return (y * p.Stride) + (x * 3)
41
+	return (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*3
42 42
 }
43 43
 
44 44
 func (p *ycc) Bounds() image.Rectangle {

+ 93
- 0
ycc_test.go Wyświetl plik

@@ -18,6 +18,7 @@ package resize
18 18
 
19 19
 import (
20 20
 	"image"
21
+	"image/color"
21 22
 	"testing"
22 23
 )
23 24
 
@@ -119,3 +120,95 @@ func TestConvertYCbCr(t *testing.T) {
119 120
 		}
120 121
 	}
121 122
 }
123
+
124
+func TestYCbCr(t *testing.T) {
125
+	rects := []image.Rectangle{
126
+		image.Rect(0, 0, 16, 16),
127
+		image.Rect(1, 0, 16, 16),
128
+		image.Rect(0, 1, 16, 16),
129
+		image.Rect(1, 1, 16, 16),
130
+		image.Rect(1, 1, 15, 16),
131
+		image.Rect(1, 1, 16, 15),
132
+		image.Rect(1, 1, 15, 15),
133
+		image.Rect(2, 3, 14, 15),
134
+		image.Rect(7, 0, 7, 16),
135
+		image.Rect(0, 8, 16, 8),
136
+		image.Rect(0, 0, 10, 11),
137
+		image.Rect(5, 6, 16, 16),
138
+		image.Rect(7, 7, 8, 8),
139
+		image.Rect(7, 8, 8, 9),
140
+		image.Rect(8, 7, 9, 8),
141
+		image.Rect(8, 8, 9, 9),
142
+		image.Rect(7, 7, 17, 17),
143
+		image.Rect(8, 8, 17, 17),
144
+		image.Rect(9, 9, 17, 17),
145
+		image.Rect(10, 10, 17, 17),
146
+	}
147
+	subsampleRatios := []image.YCbCrSubsampleRatio{
148
+		image.YCbCrSubsampleRatio444,
149
+		image.YCbCrSubsampleRatio422,
150
+		image.YCbCrSubsampleRatio420,
151
+		image.YCbCrSubsampleRatio440,
152
+	}
153
+	deltas := []image.Point{
154
+		image.Pt(0, 0),
155
+		image.Pt(1000, 1001),
156
+		image.Pt(5001, -400),
157
+		image.Pt(-701, -801),
158
+	}
159
+	for _, r := range rects {
160
+		for _, subsampleRatio := range subsampleRatios {
161
+			for _, delta := range deltas {
162
+				testYCbCr(t, r, subsampleRatio, delta)
163
+			}
164
+		}
165
+		if testing.Short() {
166
+			break
167
+		}
168
+	}
169
+}
170
+
171
+func testYCbCr(t *testing.T, r image.Rectangle, subsampleRatio image.YCbCrSubsampleRatio, delta image.Point) {
172
+	// Create a YCbCr image m, whose bounds are r translated by (delta.X, delta.Y).
173
+	r1 := r.Add(delta)
174
+	img := image.NewYCbCr(r1, subsampleRatio)
175
+
176
+	// Initialize img's pixels. For 422 and 420 subsampling, some of the Cb and Cr elements
177
+	// will be set multiple times. That's OK. We just want to avoid a uniform image.
178
+	for y := r1.Min.Y; y < r1.Max.Y; y++ {
179
+		for x := r1.Min.X; x < r1.Max.X; x++ {
180
+			yi := img.YOffset(x, y)
181
+			ci := img.COffset(x, y)
182
+			img.Y[yi] = uint8(16*y + x)
183
+			img.Cb[ci] = uint8(y + 16*x)
184
+			img.Cr[ci] = uint8(y + 16*x)
185
+		}
186
+	}
187
+
188
+	m := imageYCbCrToYCC(img)
189
+
190
+	// Make various sub-images of m.
191
+	for y0 := delta.Y + 3; y0 < delta.Y+7; y0++ {
192
+		for y1 := delta.Y + 8; y1 < delta.Y+13; y1++ {
193
+			for x0 := delta.X + 3; x0 < delta.X+7; x0++ {
194
+				for x1 := delta.X + 8; x1 < delta.X+13; x1++ {
195
+					subRect := image.Rect(x0, y0, x1, y1)
196
+					sub := m.SubImage(subRect).(*ycc)
197
+
198
+					// For each point in the sub-image's bounds, check that m.At(x, y) equals sub.At(x, y).
199
+					for y := sub.Rect.Min.Y; y < sub.Rect.Max.Y; y++ {
200
+						for x := sub.Rect.Min.X; x < sub.Rect.Max.X; x++ {
201
+							color0 := m.At(x, y).(color.YCbCr)
202
+							color1 := sub.At(x, y).(color.YCbCr)
203
+							if color0 != color1 {
204
+								t.Errorf("r=%v, subsampleRatio=%v, delta=%v, x=%d, y=%d, color0=%v, color1=%v",
205
+									r, subsampleRatio, delta, x, y, color0, color1)
206
+								return
207
+							}
208
+						}
209
+					}
210
+				}
211
+			}
212
+		}
213
+	}
214
+}