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

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

View File

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