From d733308eb14edc1b51d30419166abec7d4377534 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 6 Sep 2024 14:02:41 +0900 Subject: [PATCH] Revert "internal/restorable: integrate Image.WritePixels into internal/atlas" This reverts commit 6cc815018556913bad690bd76acb5a9fdb940475. Updates #3083 --- internal/atlas/image.go | 25 ++----------------------- internal/restorable/image.go | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/internal/atlas/image.go b/internal/atlas/image.go index 9a24f14ad..e275b7e7d 100644 --- a/internal/atlas/image.go +++ b/internal/atlas/image.go @@ -94,9 +94,6 @@ type backend struct { // restorable is an atlas on which there might be multiple images. restorable *restorable.Image - width int - height int - // page is an atlas map. Each part is called a node. // If page is nil, the backend's image is isolated and not on an atlas. page *packing.Page @@ -124,8 +121,6 @@ func (b *backend) tryAlloc(width, height int) (*packing.Node, bool) { w, h := b.page.Size() b.restorable = b.restorable.Extend(w, h) - b.width = w - b.height = h return n, true } @@ -522,7 +517,7 @@ func (i *Image) writePixels(pix []byte, region image.Rectangle) { pix2 := graphics.NewManagedBytes(len(pix), func(bs []byte) { copy(bs, pix) }) - i.backend.writePixels(pix2, region) + i.backend.restorable.WritePixels(pix2, region) return } @@ -551,17 +546,7 @@ func (i *Image) writePixels(pix []byte, region image.Rectangle) { copy(bs[4*j*r.Dx():], pix[4*j*region.Dx():4*(j+1)*region.Dx()]) } }) - i.backend.writePixels(pixb, r) -} - -func (b *backend) writePixels(pixels *graphics.ManagedBytes, region image.Rectangle) { - if region.Dx() <= 0 || region.Dy() <= 0 { - panic("atlas: width/height must be positive") - } - if !region.In(image.Rect(0, 0, b.width, b.height)) { - panic(fmt.Sprintf("atlas: out of range %v", region)) - } - b.restorable.Image.WritePixels(pixels, region) + i.backend.restorable.WritePixels(pixb, r) } func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte, region image.Rectangle) (ok bool, err error) { @@ -699,8 +684,6 @@ func (i *Image) allocate(forbiddenBackends []*backend, asSource bool) { // A screen image doesn't have a padding. i.backend = &backend{ restorable: restorable.NewImage(i.width, i.height, true), - width: i.width, - height: i.height, } theBackends = append(theBackends, i.backend) return @@ -716,8 +699,6 @@ func (i *Image) allocate(forbiddenBackends []*backend, asSource bool) { i.backend = &backend{ restorable: restorable.NewImage(wp, hp, false), - width: wp, - height: hp, source: asSource && i.imageType == ImageTypeRegular, } theBackends = append(theBackends, i.backend) @@ -764,8 +745,6 @@ loop: b := &backend{ restorable: restorable.NewImage(width, height, false), - width: width, - height: height, page: packing.NewPage(width, height, maxSize), source: asSource, } diff --git a/internal/restorable/image.go b/internal/restorable/image.go index 7131ca639..4bcda8d52 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -15,6 +15,7 @@ package restorable import ( + "fmt" "image" "github.com/hajimehoshi/ebiten/v2/internal/graphics" @@ -92,3 +93,18 @@ func (i *Image) ClearPixels(region image.Rectangle) { } clearImage(i.Image, region.Intersect(image.Rect(0, 0, i.width, i.height))) } + +// WritePixels replaces the image pixels with the given pixels slice. +// +// The specified region must not be overlapped with other regions by WritePixels. +func (i *Image) WritePixels(pixels *graphics.ManagedBytes, region image.Rectangle) { + if region.Dx() <= 0 || region.Dy() <= 0 { + panic("restorable: width/height must be positive") + } + w, h := i.width, i.height + if !region.In(image.Rect(0, 0, w, h)) { + panic(fmt.Sprintf("restorable: out of range %v", region)) + } + + i.Image.WritePixels(pixels, region) +}