internal/restorable: refactoring

This commit is contained in:
Hajime Hoshi 2022-03-21 01:26:48 +09:00
parent 5fe6791b5d
commit 924f7ea932

View File

@ -26,7 +26,7 @@ type pixelsRecord struct {
pix []byte pix []byte
} }
func (p *pixelsRecord) ClearIfOverlapped(rect image.Rectangle) { func (p *pixelsRecord) clearIfOverlapped(rect image.Rectangle) {
r := p.rect.Intersect(rect) r := p.rect.Intersect(rect)
ox := r.Min.X - p.rect.Min.X ox := r.Min.X - p.rect.Min.X
oy := r.Min.Y - p.rect.Min.Y oy := r.Min.Y - p.rect.Min.Y
@ -42,6 +42,15 @@ func (p *pixelsRecord) ClearIfOverlapped(rect image.Rectangle) {
} }
} }
func (p *pixelsRecord) at(x, y int) (r, g, b, a byte, ok bool) {
if !image.Pt(x, y).In(p.rect) {
return 0, 0, 0, 0, false
}
idx := 4 * ((y-p.rect.Min.Y)*p.rect.Dx() + (x - p.rect.Min.X))
return p.pix[idx], p.pix[idx+1], p.pix[idx+2], p.pix[idx+3], true
}
type pixelsRecords struct { type pixelsRecords struct {
records []*pixelsRecord records []*pixelsRecord
} }
@ -84,7 +93,7 @@ func (pr *pixelsRecords) clear(x, y, width, height int) {
if r.rect.In(newr) { if r.rect.In(newr) {
continue continue
} }
r.ClearIfOverlapped(newr) r.clearIfOverlapped(newr)
pr.records[n] = r pr.records[n] = r
n++ n++
} }
@ -94,24 +103,16 @@ func (pr *pixelsRecords) clear(x, y, width, height int) {
pr.records = pr.records[:n] pr.records = pr.records[:n]
} }
func (pr *pixelsRecords) at(i, j int) (byte, byte, byte, byte, bool) { func (pr *pixelsRecords) at(i, j int) (r, g, b, a byte, ok bool) {
var record *pixelsRecord
pt := image.Pt(i, j)
// Traverse the slice in the reversed order. // Traverse the slice in the reversed order.
for i := len(pr.records) - 1; i >= 0; i-- { for idx := len(pr.records) - 1; idx >= 0; idx-- {
p := pr.records[i] r, g, b, a, ok := pr.records[idx].at(i, j)
if pt.In(p.rect) { if ok {
record = p return r, g, b, a, true
break
} }
} }
if record == nil { return 0, 0, 0, 0, false
return 0, 0, 0, 0, false
}
idx := 4 * ((j-record.rect.Min.Y)*record.rect.Dx() + (i - record.rect.Min.X))
return record.pix[idx], record.pix[idx+1], record.pix[idx+2], record.pix[idx+3], true
} }
func (pr *pixelsRecords) apply(img *graphicscommand.Image) { func (pr *pixelsRecords) apply(img *graphicscommand.Image) {