@@ -6,7 +6,7 @@ package resize
6
6
7
7
import (
8
8
"image"
9
- "image/ycbcr "
9
+ "image/color "
10
10
)
11
11
12
12
// Resize returns a scaled copy of the image slice r of m.
@@ -16,12 +16,12 @@ func Resize(m image.Image, r image.Rectangle, w, h int) image.Image {
16
16
return nil
17
17
}
18
18
if w == 0 || h == 0 || r .Dx () <= 0 || r .Dy () <= 0 {
19
- return image .NewRGBA64 (w , h )
19
+ return image .NewRGBA64 (image . Rect ( 0 , 0 , w , h ) )
20
20
}
21
21
switch m := m .(type ) {
22
22
case * image.RGBA :
23
23
return resizeRGBA (m , r , w , h )
24
- case * ycbcr .YCbCr :
24
+ case * image .YCbCr :
25
25
if m , ok := resizeYCbCr (m , r , w , h ); ok {
26
26
return m
27
27
}
@@ -66,14 +66,14 @@ func Resize(m image.Image, r image.Rectangle, w, h int) image.Image {
66
66
b64 := uint64 (b32 )
67
67
a64 := uint64 (a32 )
68
68
// Spread the source pixel over 1 or more destination rows.
69
- py := uint64 (y - r . Min . Y ) * hh
69
+ py := uint64 (y ) * hh
70
70
for remy := hh ; remy > 0 ; {
71
71
qy := dy - (py % dy )
72
72
if qy > remy {
73
73
qy = remy
74
74
}
75
75
// Spread the source pixel over 1 or more destination columns.
76
- px := uint64 (x - r . Min . X ) * ww
76
+ px := uint64 (x ) * ww
77
77
index := 4 * ((py / dy )* ww + (px / dx ))
78
78
for remx := ww ; remx > 0 ; {
79
79
qx := dx - (px % dx )
@@ -98,28 +98,29 @@ func Resize(m image.Image, r image.Rectangle, w, h int) image.Image {
98
98
99
99
// average convert the sums to averages and returns the result.
100
100
func average (sum []uint64 , w , h int , n uint64 ) image.Image {
101
- ret := image .NewRGBA (w , h )
101
+ ret := image .NewRGBA (image . Rect ( 0 , 0 , w , h ) )
102
102
for y := 0 ; y < h ; y ++ {
103
103
for x := 0 ; x < w ; x ++ {
104
- i := y * ret .Stride + x * 4
105
- j := 4 * (y * w + x )
106
- ret .Pix [i + 0 ] = uint8 (sum [j + 0 ] / n )
107
- ret .Pix [i + 1 ] = uint8 (sum [j + 1 ] / n )
108
- ret .Pix [i + 2 ] = uint8 (sum [j + 2 ] / n )
109
- ret .Pix [i + 3 ] = uint8 (sum [j + 3 ] / n )
104
+ index := 4 * (y * w + x )
105
+ ret .SetRGBA (x , y , color.RGBA {
106
+ uint8 (sum [index + 0 ] / n ),
107
+ uint8 (sum [index + 1 ] / n ),
108
+ uint8 (sum [index + 2 ] / n ),
109
+ uint8 (sum [index + 3 ] / n ),
110
+ })
110
111
}
111
112
}
112
113
return ret
113
114
}
114
115
115
116
// resizeYCbCr returns a scaled copy of the YCbCr image slice r of m.
116
117
// The returned image has width w and height h.
117
- func resizeYCbCr (m * ycbcr .YCbCr , r image.Rectangle , w , h int ) (image.Image , bool ) {
118
+ func resizeYCbCr (m * image .YCbCr , r image.Rectangle , w , h int ) (image.Image , bool ) {
118
119
var verticalRes int
119
120
switch m .SubsampleRatio {
120
- case ycbcr . SubsampleRatio420 :
121
+ case image . YCbCrSubsampleRatio420 :
121
122
verticalRes = 2
122
- case ycbcr . SubsampleRatio422 :
123
+ case image . YCbCrSubsampleRatio422 :
123
124
verticalRes = 1
124
125
default :
125
126
return nil , false
@@ -134,19 +135,19 @@ func resizeYCbCr(m *ycbcr.YCbCr, r image.Rectangle, w, h int) (image.Image, bool
134
135
Cr := m .Cr [y / verticalRes * m .CStride :]
135
136
for x := r .Min .X ; x < r .Max .X ; x ++ {
136
137
// Get the source pixel.
137
- r8 , g8 , b8 := ycbcr .YCbCrToRGB (Y [x ], Cb [x / 2 ], Cr [x / 2 ])
138
+ r8 , g8 , b8 := color .YCbCrToRGB (Y [x ], Cb [x / 2 ], Cr [x / 2 ])
138
139
r64 := uint64 (r8 )
139
140
g64 := uint64 (g8 )
140
141
b64 := uint64 (b8 )
141
142
// Spread the source pixel over 1 or more destination rows.
142
- py := uint64 (y - r . Min . Y ) * hh
143
+ py := uint64 (y ) * hh
143
144
for remy := hh ; remy > 0 ; {
144
145
qy := dy - (py % dy )
145
146
if qy > remy {
146
147
qy = remy
147
148
}
148
149
// Spread the source pixel over 1 or more destination columns.
149
- px := uint64 (x - r . Min . X ) * ww
150
+ px := uint64 (x ) * ww
150
151
index := 4 * ((py / dy )* ww + (px / dx ))
151
152
for remx := ww ; remx > 0 ; {
152
153
qx := dx - (px % dx )
@@ -178,23 +179,23 @@ func resizeRGBA(m *image.RGBA, r image.Rectangle, w, h int) image.Image {
178
179
// See comment in Resize.
179
180
n , sum := dx * dy , make ([]uint64 , 4 * w * h )
180
181
for y := r .Min .Y ; y < r .Max .Y ; y ++ {
181
- pix := m .Pix [( y - m . Rect . Min .Y ) * m . Stride :]
182
+ pixOffset := m .PixOffset ( r . Min .X , y )
182
183
for x := r .Min .X ; x < r .Max .X ; x ++ {
183
184
// Get the source pixel.
184
- p := pix [( x - m . Rect . Min . X ) * 4 :]
185
- r64 := uint64 (p [ 0 ])
186
- g64 := uint64 (p [ 1 ])
187
- b64 := uint64 (p [ 2 ])
188
- a64 := uint64 ( p [ 3 ])
185
+ r64 := uint64 ( m . Pix [ pixOffset + 0 ])
186
+ g64 := uint64 (m . Pix [ pixOffset + 1 ])
187
+ b64 := uint64 (m . Pix [ pixOffset + 2 ])
188
+ a64 := uint64 (m . Pix [ pixOffset + 3 ])
189
+ pixOffset += 4
189
190
// Spread the source pixel over 1 or more destination rows.
190
- py := uint64 (y - r . Min . Y ) * hh
191
+ py := uint64 (y ) * hh
191
192
for remy := hh ; remy > 0 ; {
192
193
qy := dy - (py % dy )
193
194
if qy > remy {
194
195
qy = remy
195
196
}
196
197
// Spread the source pixel over 1 or more destination columns.
197
- px := uint64 (x - r . Min . X ) * ww
198
+ px := uint64 (x ) * ww
198
199
index := 4 * ((py / dy )* ww + (px / dx ))
199
200
for remx := ww ; remx > 0 ; {
200
201
qx := dx - (px % dx )
@@ -225,10 +226,10 @@ func Resample(m image.Image, r image.Rectangle, w, h int) image.Image {
225
226
return nil
226
227
}
227
228
if w == 0 || h == 0 || r .Dx () <= 0 || r .Dy () <= 0 {
228
- return image .NewRGBA64 (w , h )
229
+ return image .NewRGBA64 (image . Rect ( 0 , 0 , w , h ) )
229
230
}
230
231
curw , curh := r .Dx (), r .Dy ()
231
- img := image .NewRGBA (w , h )
232
+ img := image .NewRGBA (image . Rect ( 0 , 0 , w , h ) )
232
233
for y := 0 ; y < h ; y ++ {
233
234
for x := 0 ; x < w ; x ++ {
234
235
// Get a source pixel.
@@ -239,7 +240,7 @@ func Resample(m image.Image, r image.Rectangle, w, h int) image.Image {
239
240
g := uint8 (g32 >> 8 )
240
241
b := uint8 (b32 >> 8 )
241
242
a := uint8 (a32 >> 8 )
242
- img .SetRGBA (x , y , image. RGBAColor {r , g , b , a })
243
+ img .SetRGBA (x , y , color. RGBA {r , g , b , a })
243
244
}
244
245
}
245
246
return img
0 commit comments