graphics: Rename imageRestoringInfo -> pixels

This commit is contained in:
Hajime Hoshi 2016-07-14 02:52:24 +09:00
parent 3b8d5026c3
commit 4e913531e0
2 changed files with 69 additions and 69 deletions

View File

@ -34,7 +34,7 @@ type imageImpl struct {
width int
height int
filter Filter
restoringInfo imageRestoringInfo
pixels pixels
volatile bool
screen bool
m sync.Mutex
@ -52,8 +52,8 @@ func newImageImpl(width, height int, filter Filter, volatile bool) (*imageImpl,
filter: filter,
volatile: volatile,
}
i.restoringInfo.imageImpl = i
i.restoringInfo.resetWithPixels(make([]uint8, width*height*4))
i.pixels.imageImpl = i
i.pixels.resetWithPixels(make([]uint8, width*height*4))
runtime.SetFinalizer(i, (*imageImpl).Dispose)
return i, nil
}
@ -85,8 +85,8 @@ func newImageImplFromImage(source image.Image, filter Filter) (*imageImpl, error
height: h,
filter: filter,
}
i.restoringInfo.imageImpl = i
i.restoringInfo.resetWithPixels(pixels)
i.pixels.imageImpl = i
i.pixels.resetWithPixels(pixels)
runtime.SetFinalizer(i, (*imageImpl).Dispose)
return i, nil
}
@ -103,8 +103,8 @@ func newScreenImageImpl(width, height int) (*imageImpl, error) {
volatile: true,
screen: true,
}
i.restoringInfo.imageImpl = i
i.restoringInfo.resetWithPixels(make([]uint8, width*height*4))
i.pixels.imageImpl = i
i.pixels.resetWithPixels(make([]uint8, width*height*4))
runtime.SetFinalizer(i, (*imageImpl).Dispose)
return i, nil
}
@ -115,7 +115,7 @@ func (i *imageImpl) Fill(clr color.Color) error {
if i.disposed {
return errors.New("ebiten: image is already disposed")
}
i.restoringInfo.fill(clr)
i.pixels.fill(clr)
return i.image.Fill(clr)
}
@ -128,7 +128,7 @@ func (i *imageImpl) clearIfVolatile() error {
if !i.volatile {
return nil
}
i.restoringInfo.clear()
i.pixels.clear()
return i.image.Fill(color.Transparent)
}
@ -170,7 +170,7 @@ func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error {
colorm: options.ColorM,
mode: opengl.CompositeMode(options.CompositeMode),
}
i.restoringInfo.appendDrawImageHistory(c)
i.pixels.appendDrawImageHistory(c)
geom := &options.GeoM
colorm := &options.ColorM
mode := opengl.CompositeMode(options.CompositeMode)
@ -189,7 +189,7 @@ func (i *imageImpl) At(x, y int, context *opengl.Context) color.Color {
if i.disposed {
return color.Transparent
}
clr, err := i.restoringInfo.at(x, y, context)
clr, err := i.pixels.at(x, y, context)
if err != nil {
panic(err)
}
@ -202,7 +202,7 @@ func (i *imageImpl) resetHistoryIfNeeded(target *Image, context *opengl.Context)
if i.disposed {
return nil
}
if err := i.restoringInfo.resetHistoryIfNeeded(target, context); err != nil {
if err := i.pixels.resetHistoryIfNeeded(target, context); err != nil {
return err
}
return nil
@ -211,7 +211,7 @@ func (i *imageImpl) resetHistoryIfNeeded(target *Image, context *opengl.Context)
func (i *imageImpl) hasHistory() bool {
i.m.Lock()
defer i.m.Unlock()
return i.restoringInfo.hasHistory()
return i.pixels.hasHistory()
}
func (i *imageImpl) restore(context *opengl.Context) error {
@ -239,7 +239,7 @@ func (i *imageImpl) restore(context *opengl.Context) error {
return nil
}
var err error
i.image, err = i.restoringInfo.restore(context)
i.image, err = i.pixels.restore(context)
if err != nil {
return err
}
@ -259,7 +259,7 @@ func (i *imageImpl) Dispose() error {
}
i.image = nil
i.disposed = true
i.restoringInfo.clear()
i.pixels.clear()
runtime.SetFinalizer(i, nil)
return nil
}
@ -270,7 +270,7 @@ func (i *imageImpl) ReplacePixels(p []uint8) error {
}
i.m.Lock()
defer i.m.Unlock()
i.restoringInfo.resetWithPixels(p)
i.pixels.resetWithPixels(p)
if i.disposed {
return errors.New("ebiten: image is already disposed")
}

View File

@ -30,55 +30,55 @@ type drawImageHistoryItem struct {
mode opengl.CompositeMode
}
type imageRestoringInfo struct {
type pixels struct {
imageImpl *imageImpl
pixels []uint8
baseColor color.Color
drawImageHistory []*drawImageHistoryItem
}
func (i *imageRestoringInfo) resetWithPixels(pixels []uint8) {
if i.pixels == nil {
i.pixels = make([]uint8, len(pixels))
func (p *pixels) resetWithPixels(pixels []uint8) {
if p.pixels == nil {
p.pixels = make([]uint8, len(pixels))
}
copy(i.pixels, pixels)
i.baseColor = nil
i.drawImageHistory = nil
copy(p.pixels, pixels)
p.baseColor = nil
p.drawImageHistory = nil
}
func (i *imageRestoringInfo) clear() {
i.pixels = nil
i.baseColor = nil
i.drawImageHistory = nil
func (p *pixels) clear() {
p.pixels = nil
p.baseColor = nil
p.drawImageHistory = nil
}
func (i *imageRestoringInfo) fill(clr color.Color) {
i.pixels = nil
i.baseColor = clr
i.drawImageHistory = nil
func (p *pixels) fill(clr color.Color) {
p.pixels = nil
p.baseColor = clr
p.drawImageHistory = nil
}
func (i *imageRestoringInfo) appendDrawImageHistory(item *drawImageHistoryItem) {
i.drawImageHistory = append(i.drawImageHistory, item)
func (p *pixels) appendDrawImageHistory(item *drawImageHistoryItem) {
p.drawImageHistory = append(p.drawImageHistory, item)
}
func (i *imageRestoringInfo) at(x, y int, context *opengl.Context) (color.Color, error) {
if i.pixels == nil || i.drawImageHistory != nil {
func (p *pixels) at(x, y int, context *opengl.Context) (color.Color, error) {
if p.pixels == nil || p.drawImageHistory != nil {
var err error
i.pixels, err = i.imageImpl.image.Pixels(context)
p.pixels, err = p.imageImpl.image.Pixels(context)
if err != nil {
return nil, err
}
i.baseColor = nil
i.drawImageHistory = nil
p.baseColor = nil
p.drawImageHistory = nil
}
idx := 4*x + 4*y*i.imageImpl.width
r, g, b, a := i.pixels[idx], i.pixels[idx+1], i.pixels[idx+2], i.pixels[idx+3]
idx := 4*x + 4*y*p.imageImpl.width
r, g, b, a := p.pixels[idx], p.pixels[idx+1], p.pixels[idx+2], p.pixels[idx+3]
return color.RGBA{r, g, b, a}, nil
}
func (i *imageRestoringInfo) hasHistoryWith(target *Image) bool {
for _, c := range i.drawImageHistory {
func (p *pixels) hasHistoryWith(target *Image) bool {
for _, c := range p.drawImageHistory {
if c.image == target {
return true
}
@ -86,36 +86,36 @@ func (i *imageRestoringInfo) hasHistoryWith(target *Image) bool {
return false
}
func (i *imageRestoringInfo) resetHistoryIfNeeded(target *Image, context *opengl.Context) error {
if i.drawImageHistory == nil {
func (p *pixels) resetHistoryIfNeeded(target *Image, context *opengl.Context) error {
if p.drawImageHistory == nil {
return nil
}
if !i.hasHistoryWith(target) {
if !p.hasHistoryWith(target) {
return nil
}
var err error
i.pixels, err = i.imageImpl.image.Pixels(context)
p.pixels, err = p.imageImpl.image.Pixels(context)
if err != nil {
return err
}
i.baseColor = nil
i.drawImageHistory = nil
p.baseColor = nil
p.drawImageHistory = nil
return nil
}
func (i *imageRestoringInfo) hasHistory() bool {
return i.drawImageHistory != nil
func (p *pixels) hasHistory() bool {
return p.drawImageHistory != nil
}
func (i *imageRestoringInfo) restore(context *opengl.Context) (*graphics.Image, error) {
w, h := i.imageImpl.width, i.imageImpl.height
func (p *pixels) restore(context *opengl.Context) (*graphics.Image, error) {
w, h := p.imageImpl.width, p.imageImpl.height
img := image.NewRGBA(image.Rect(0, 0, w, h))
if i.pixels != nil {
if p.pixels != nil {
for j := 0; j < h; j++ {
copy(img.Pix[j*img.Stride:], i.pixels[j*w*4:(j+1)*w*4])
copy(img.Pix[j*img.Stride:], p.pixels[j*w*4:(j+1)*w*4])
}
} else if i.baseColor != nil {
r32, g32, b32, a32 := i.baseColor.RGBA()
} else if p.baseColor != nil {
r32, g32, b32, a32 := p.baseColor.RGBA()
r, g, b, a := uint8(r32), uint8(g32), uint8(b32), uint8(a32)
for idx := 0; idx < len(img.Pix)/4; idx++ {
img.Pix[4*idx] = r
@ -124,11 +124,11 @@ func (i *imageRestoringInfo) restore(context *opengl.Context) (*graphics.Image,
img.Pix[4*idx+3] = a
}
}
gimg, err := graphics.NewImageFromImage(img, glFilter(i.imageImpl.filter))
gimg, err := graphics.NewImageFromImage(img, glFilter(p.imageImpl.filter))
if err != nil {
return nil, err
}
for _, c := range i.drawImageHistory {
for _, c := range p.drawImageHistory {
if c.image.impl.hasHistory() {
panic("not reach")
}
@ -136,11 +136,11 @@ func (i *imageRestoringInfo) restore(context *opengl.Context) (*graphics.Image,
return nil, err
}
}
i.pixels, err = gimg.Pixels(context)
p.pixels, err = gimg.Pixels(context)
if err != nil {
return nil, err
}
i.baseColor = nil
i.drawImageHistory = nil
p.baseColor = nil
p.drawImageHistory = nil
return gimg, nil
}