@@ -6,7 +6,7 @@ package resize
66
77import (
88 "image"
9- "image/ycbcr "
9+ "image/color "
1010)
1111
1212// 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 {
1616 return nil
1717 }
1818 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 ) )
2020 }
2121 switch m := m .(type ) {
2222 case * image.RGBA :
2323 return resizeRGBA (m , r , w , h )
24- case * ycbcr .YCbCr :
24+ case * image .YCbCr :
2525 if m , ok := resizeYCbCr (m , r , w , h ); ok {
2626 return m
2727 }
@@ -66,14 +66,14 @@ func Resize(m image.Image, r image.Rectangle, w, h int) image.Image {
6666 b64 := uint64 (b32 )
6767 a64 := uint64 (a32 )
6868 // Spread the source pixel over 1 or more destination rows.
69- py := uint64 (y - r . Min . Y ) * hh
69+ py := uint64 (y ) * hh
7070 for remy := hh ; remy > 0 ; {
7171 qy := dy - (py % dy )
7272 if qy > remy {
7373 qy = remy
7474 }
7575 // Spread the source pixel over 1 or more destination columns.
76- px := uint64 (x - r . Min . X ) * ww
76+ px := uint64 (x ) * ww
7777 index := 4 * ((py / dy )* ww + (px / dx ))
7878 for remx := ww ; remx > 0 ; {
7979 qx := dx - (px % dx )
@@ -98,28 +98,29 @@ func Resize(m image.Image, r image.Rectangle, w, h int) image.Image {
9898
9999// average convert the sums to averages and returns the result.
100100func 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 ) )
102102 for y := 0 ; y < h ; y ++ {
103103 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+ })
110111 }
111112 }
112113 return ret
113114}
114115
115116// resizeYCbCr returns a scaled copy of the YCbCr image slice r of m.
116117// 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 ) {
118119 var verticalRes int
119120 switch m .SubsampleRatio {
120- case ycbcr . SubsampleRatio420 :
121+ case image . YCbCrSubsampleRatio420 :
121122 verticalRes = 2
122- case ycbcr . SubsampleRatio422 :
123+ case image . YCbCrSubsampleRatio422 :
123124 verticalRes = 1
124125 default :
125126 return nil , false
@@ -134,19 +135,19 @@ func resizeYCbCr(m *ycbcr.YCbCr, r image.Rectangle, w, h int) (image.Image, bool
134135 Cr := m .Cr [y / verticalRes * m .CStride :]
135136 for x := r .Min .X ; x < r .Max .X ; x ++ {
136137 // 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 ])
138139 r64 := uint64 (r8 )
139140 g64 := uint64 (g8 )
140141 b64 := uint64 (b8 )
141142 // Spread the source pixel over 1 or more destination rows.
142- py := uint64 (y - r . Min . Y ) * hh
143+ py := uint64 (y ) * hh
143144 for remy := hh ; remy > 0 ; {
144145 qy := dy - (py % dy )
145146 if qy > remy {
146147 qy = remy
147148 }
148149 // Spread the source pixel over 1 or more destination columns.
149- px := uint64 (x - r . Min . X ) * ww
150+ px := uint64 (x ) * ww
150151 index := 4 * ((py / dy )* ww + (px / dx ))
151152 for remx := ww ; remx > 0 ; {
152153 qx := dx - (px % dx )
@@ -178,23 +179,23 @@ func resizeRGBA(m *image.RGBA, r image.Rectangle, w, h int) image.Image {
178179 // See comment in Resize.
179180 n , sum := dx * dy , make ([]uint64 , 4 * w * h )
180181 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 )
182183 for x := r .Min .X ; x < r .Max .X ; x ++ {
183184 // 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
189190 // Spread the source pixel over 1 or more destination rows.
190- py := uint64 (y - r . Min . Y ) * hh
191+ py := uint64 (y ) * hh
191192 for remy := hh ; remy > 0 ; {
192193 qy := dy - (py % dy )
193194 if qy > remy {
194195 qy = remy
195196 }
196197 // Spread the source pixel over 1 or more destination columns.
197- px := uint64 (x - r . Min . X ) * ww
198+ px := uint64 (x ) * ww
198199 index := 4 * ((py / dy )* ww + (px / dx ))
199200 for remx := ww ; remx > 0 ; {
200201 qx := dx - (px % dx )
@@ -225,10 +226,10 @@ func Resample(m image.Image, r image.Rectangle, w, h int) image.Image {
225226 return nil
226227 }
227228 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 ) )
229230 }
230231 curw , curh := r .Dx (), r .Dy ()
231- img := image .NewRGBA (w , h )
232+ img := image .NewRGBA (image . Rect ( 0 , 0 , w , h ) )
232233 for y := 0 ; y < h ; y ++ {
233234 for x := 0 ; x < w ; x ++ {
234235 // Get a source pixel.
@@ -239,7 +240,7 @@ func Resample(m image.Image, r image.Rectangle, w, h int) image.Image {
239240 g := uint8 (g32 >> 8 )
240241 b := uint8 (b32 >> 8 )
241242 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 })
243244 }
244245 }
245246 return img
0 commit comments