internal/atlas: refactoring: use image.Rectangle

This commit is contained in:
Hajime Hoshi 2023-04-27 23:55:11 +09:00
parent 82c7436be5
commit 9e61b52a70

View File

@ -368,16 +368,14 @@ func (i *Image) putOnSourceBackend(graphicsDriver graphicsdriver.Graphics) error
return nil return nil
} }
func (i *Image) regionWithPadding() (x, y, width, height int) { func (i *Image) regionWithPadding() image.Rectangle {
if i.backend == nil { if i.backend == nil {
panic("atlas: backend must not be nil: not allocated yet?") panic("atlas: backend must not be nil: not allocated yet?")
} }
if !i.isOnAtlas() { if !i.isOnAtlas() {
return 0, 0, i.width + i.paddingSize(), i.height + i.paddingSize() return image.Rect(0, 0, i.width+i.paddingSize(), i.height+i.paddingSize())
} }
// TODO: Use image.Rectangle as it is. return i.node.Region()
r := i.node.Region()
return r.Min.X, r.Min.Y, r.Dx(), r.Dy()
} }
func (i *Image) processSrc(src *Image) { func (i *Image) processSrc(src *Image) {
@ -445,8 +443,8 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [
i.processSrc(src) i.processSrc(src)
} }
x, y, _, _ := i.regionWithPadding() r := i.regionWithPadding()
dx, dy := float32(x), float32(y) dx, dy := float32(r.Min.X), float32(r.Min.Y)
// TODO: Check if dstRegion does not to violate the region. // TODO: Check if dstRegion does not to violate the region.
dstRegion.X += dx dstRegion.X += dx
@ -454,8 +452,8 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [
var oxf, oyf float32 var oxf, oyf float32
if srcs[0] != nil { if srcs[0] != nil {
ox, oy, _, _ := srcs[0].regionWithPadding() r := srcs[0].regionWithPadding()
oxf, oyf = float32(ox), float32(oy) oxf, oyf = float32(r.Min.X), float32(r.Min.Y)
n := len(vertices) n := len(vertices)
for i := 0; i < n; i += graphics.VertexFloatCount { for i := 0; i < n; i += graphics.VertexFloatCount {
vertices[i] += dx vertices[i] += dx
@ -492,9 +490,9 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [
if src == nil { if src == nil {
continue continue
} }
ox, oy, _, _ := src.regionWithPadding() r := src.regionWithPadding()
offsets[i][0] = float32(ox) - oxf + subimageOffset[0] offsets[i][0] = float32(r.Min.X) - oxf + subimageOffset[0]
offsets[i][1] = float32(oy) - oyf + subimageOffset[1] offsets[i][1] = float32(r.Min.Y) - oyf + subimageOffset[1]
} }
for i, src := range srcs { for i, src := range srcs {
if src == nil { if src == nil {
@ -542,11 +540,11 @@ func (i *Image) writePixels(pix []byte, x, y, width, height int) {
i.allocate(nil, true) i.allocate(nil, true)
} }
px, py, pw, ph := i.regionWithPadding() r := i.regionWithPadding()
if x != 0 || y != 0 || width != i.width || height != i.height || i.paddingSize() == 0 { if x != 0 || y != 0 || width != i.width || height != i.height || i.paddingSize() == 0 {
x += px x += r.Min.X
y += py y += r.Min.Y
if pix == nil { if pix == nil {
i.backend.restorable.WritePixels(nil, x, y, width, height) i.backend.restorable.WritePixels(nil, x, y, width, height)
@ -560,7 +558,7 @@ func (i *Image) writePixels(pix []byte, x, y, width, height int) {
return return
} }
pixb := theTemporaryBytes.alloc(4 * pw * ph) pixb := theTemporaryBytes.alloc(4 * r.Dx() * r.Dy())
// Clear the edges. pixb might not be zero-cleared. // Clear the edges. pixb might not be zero-cleared.
// TODO: These loops assume that paddingSize is 1. // TODO: These loops assume that paddingSize is 1.
@ -569,11 +567,11 @@ func (i *Image) writePixels(pix []byte, x, y, width, height int) {
if paddingSize != i.paddingSize() { if paddingSize != i.paddingSize() {
panic(fmt.Sprintf("atlas: writePixels assumes the padding is always 1 but the actual padding was %d", i.paddingSize())) panic(fmt.Sprintf("atlas: writePixels assumes the padding is always 1 but the actual padding was %d", i.paddingSize()))
} }
rowPixels := 4 * pw rowPixels := 4 * r.Dx()
for i := 0; i < rowPixels; i++ { for i := 0; i < rowPixels; i++ {
pixb[rowPixels*(ph-1)+i] = 0 pixb[rowPixels*(r.Dy()-1)+i] = 0
} }
for j := 1; j < ph; j++ { for j := 1; j < r.Dy(); j++ {
pixb[rowPixels*j-4] = 0 pixb[rowPixels*j-4] = 0
pixb[rowPixels*j-3] = 0 pixb[rowPixels*j-3] = 0
pixb[rowPixels*j-2] = 0 pixb[rowPixels*j-2] = 0
@ -582,12 +580,12 @@ func (i *Image) writePixels(pix []byte, x, y, width, height int) {
// Copy the content. // Copy the content.
for j := 0; j < height; j++ { for j := 0; j < height; j++ {
copy(pixb[4*j*pw:], pix[4*j*width:4*(j+1)*width]) copy(pixb[4*j*r.Dx():], pix[4*j*width:4*(j+1)*width])
} }
x += px x += r.Min.X
y += py y += r.Min.Y
i.backend.restorable.WritePixels(pixb, x, y, pw, ph) i.backend.restorable.WritePixels(pixb, x, y, r.Dx(), r.Dy())
} }
func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte, x, y, width, height int) error { func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte, x, y, width, height int) error {
@ -605,9 +603,9 @@ func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte
return nil return nil
} }
ox, oy, _, _ := i.regionWithPadding() r := i.regionWithPadding()
x += ox x += r.Min.X
y += oy y += r.Min.Y
return i.backend.restorable.ReadPixels(graphicsDriver, pixels, x, y, width, height) return i.backend.restorable.ReadPixels(graphicsDriver, pixels, x, y, width, height)
} }
@ -656,7 +654,8 @@ func (i *Image) dispose(markDisposed bool) {
i.backend.page.Free(i.node) i.backend.page.Free(i.node)
if !i.backend.page.IsEmpty() { if !i.backend.page.IsEmpty() {
// As this part can be reused, this should be cleared explicitly. // As this part can be reused, this should be cleared explicitly.
i.backend.restorable.ClearPixels(i.regionWithPadding()) r := i.regionWithPadding()
i.backend.restorable.ClearPixels(r.Min.X, r.Min.Y, r.Dx(), r.Dy())
return return
} }