graphics: Refactoring: Remove (*sharedImagePart).region() usages from *Image

This commit is contained in:
Hajime Hoshi 2018-03-10 23:36:07 +09:00
parent 26f4999ddd
commit 9d0ea5c241
2 changed files with 25 additions and 22 deletions

View File

@ -15,7 +15,6 @@
package ebiten package ebiten
import ( import (
"fmt"
"image" "image"
"image/color" "image/color"
"runtime" "runtime"
@ -71,8 +70,7 @@ func (i *Image) copyCheck() {
// Size returns the size of the image. // Size returns the size of the image.
func (i *Image) Size() (width, height int) { func (i *Image) Size() (width, height int) {
_, _, w, h := i.sharedImagePart.region() return i.sharedImagePart.Size()
return w, h
} }
// Clear resets the pixels of the image into 0. // Clear resets the pixels of the image into 0.
@ -231,12 +229,6 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error {
geom = g geom = g
} }
dx, dy, _, _ := img.sharedImagePart.region()
sx0 += dx
sy0 += dy
sx1 += dx
sy1 += dy
mode := opengl.CompositeMode(options.CompositeMode) mode := opengl.CompositeMode(options.CompositeMode)
filter := graphics.FilterNearest filter := graphics.FilterNearest
@ -272,11 +264,7 @@ func (i *Image) At(x, y int) color.Color {
if i.isDisposed() { if i.isDisposed() {
return color.RGBA{} return color.RGBA{}
} }
ox, oy, w, h := i.sharedImagePart.region() clr, err := i.sharedImagePart.At(x, y)
if x < 0 || y < 0 || x >= w || y >= h {
return color.RGBA{}
}
clr, err := i.sharedImagePart.At(x+ox, y+oy)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -317,11 +305,7 @@ func (i *Image) ReplacePixels(p []byte) error {
if i.isDisposed() { if i.isDisposed() {
return nil return nil
} }
x, y, w, h := i.sharedImagePart.region() i.sharedImagePart.ReplacePixels(p)
if l := 4 * w * h; len(p) != l {
panic(fmt.Sprintf("ebiten: len(p) was %d but must be %d", len(p), l))
}
i.sharedImagePart.ReplacePixels(p, x, y, w, h)
return nil return nil
} }

View File

@ -15,6 +15,7 @@
package ebiten package ebiten
import ( import (
"fmt"
"image/color" "image/color"
"github.com/hajimehoshi/ebiten/internal/affine" "github.com/hajimehoshi/ebiten/internal/affine"
@ -64,16 +65,34 @@ func (s *sharedImagePart) region() (x, y, width, height int) {
return s.node.Region() return s.node.Region()
} }
func (s *sharedImagePart) Size() (width, height int) {
_, _, w, h := s.region()
return w, h
}
func (s *sharedImagePart) DrawImage(img *sharedImagePart, sx0, sy0, sx1, sy1 int, geom *affine.GeoM, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) { func (s *sharedImagePart) DrawImage(img *sharedImagePart, sx0, sy0, sx1, sy1 int, geom *affine.GeoM, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) {
dx, dy, _, _ := img.region()
sx0 += dx
sy0 += dy
sx1 += dx
sy1 += dy
s.sharedImage.restorable.DrawImage(img.sharedImage.restorable, sx0, sy0, sx1, sy1, geom, colorm, mode, filter) s.sharedImage.restorable.DrawImage(img.sharedImage.restorable, sx0, sy0, sx1, sy1, geom, colorm, mode, filter)
} }
func (s *sharedImagePart) ReplacePixels(pixels []byte, x, y, width, height int) { func (s *sharedImagePart) ReplacePixels(p []byte) {
s.sharedImage.restorable.ReplacePixels(pixels, x, y, width, height) x, y, w, h := s.region()
if l := 4 * w * h; len(p) != l {
panic(fmt.Sprintf("ebiten: len(p) was %d but must be %d", len(p), l))
}
s.sharedImage.restorable.ReplacePixels(p, x, y, w, h)
} }
func (s *sharedImagePart) At(x, y int) (color.Color, error) { func (s *sharedImagePart) At(x, y int) (color.Color, error) {
return s.sharedImage.restorable.At(x, y) ox, oy, w, h := s.region()
if x < 0 || y < 0 || x >= w || y >= h {
return color.RGBA{}, nil
}
return s.sharedImage.restorable.At(x+ox, y+oy)
} }
func (s *sharedImagePart) isDisposed() bool { func (s *sharedImagePart) isDisposed() bool {