Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Image.SubImageInto method #2903

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 32 additions & 4 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,9 +827,37 @@ func (i *Image) DrawRectShader(width, height int, shader *Shader, options *DrawR
// Successive uses of multiple various regions as rendering destination is still efficient
// when all the underlying images are the same, but some platforms like browsers might not work efficiently.
func (i *Image) SubImage(r image.Rectangle) image.Image {
var s Image
if !i.SubImageInto(&s, r) {
return nil
}
return &s
}

// SubImageInto is like SubImage, but it returns the result through the s parameter.
// The s image will be initialized to a result of taking the SubImage from i.
//
// This code is semantically equivalent to:
//
// s := i.SubImage(r)
//
// You might want to use this method to avoid a new Image object heap allocations.
// Use it like this:
//
// var s ebiten.Image
// i.SubImageInto(&s, r)
//
// And then use s as your sub image.
//
// This method returns false if i is disposed.
func (i *Image) SubImageInto(s *Image, r image.Rectangle) bool {
// Try to keep this method zero alloc.
// There is a test for that (TestSubImageIntoZeroAlloc).
// There is also a benchmark for the comparison.

i.copyCheck()
if i.isDisposed() {
return nil
return false
}

r = r.Intersect(i.Bounds())
Expand All @@ -843,14 +871,14 @@ func (i *Image) SubImage(r image.Rectangle) image.Image {
orig = i.original
}

img := &Image{
*s = Image{
image: i.image,
bounds: r,
original: orig,
}
img.addr = img
s.addr = s

return img
return true
}

// Bounds returns the bounds of the image.
Expand Down
26 changes: 26 additions & 0 deletions image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4582,3 +4582,29 @@ func TestImageDrawImageAfterDeallocation(t *testing.T) {
}
}
}

func TestSubImageIntoZeroAlloc(t *testing.T) {
i := ebiten.NewImage(1, 1)
var s ebiten.Image
allocs := testing.AllocsPerRun(1, func() {
i.SubImageInto(&s, image.Rect(0, 0, 1, 1))
})
if allocs != 0 {
t.Fatalf("have %d allocs, wanted 0", int(allocs))
}
}

func BenchmarkSubImageInto(b *testing.B) {
img := ebiten.NewImage(1, 1)
var subimg ebiten.Image
for i := 0; i < b.N; i++ {
img.SubImageInto(&subimg, image.Rect(0, 0, 1, 1))
}
}

func BenchmarkSubImage(b *testing.B) {
img := ebiten.NewImage(1, 1)
for i := 0; i < b.N; i++ {
img.SubImage(image.Rect(0, 0, 1, 1))
}
}