graphics: Add imageForPixels

This commit is contained in:
Hajime Hoshi 2016-07-24 01:50:22 +09:00
parent 8d074ecc20
commit b3aa567f38
2 changed files with 17 additions and 9 deletions

View File

@ -51,6 +51,7 @@ func newImageImpl(width, height int, filter Filter, volatile bool) (*imageImpl,
filter: filter, filter: filter,
volatile: volatile, volatile: volatile,
} }
i.pixels.image = i.image
i.pixels.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
@ -83,6 +84,7 @@ func newImageImplFromImage(source image.Image, filter Filter) (*imageImpl, error
height: h, height: h,
filter: filter, filter: filter,
} }
i.pixels.image = i.image
i.pixels.resetWithPixels(pixels) i.pixels.resetWithPixels(pixels)
runtime.SetFinalizer(i, (*imageImpl).Dispose) runtime.SetFinalizer(i, (*imageImpl).Dispose)
return i, nil return i, nil
@ -100,6 +102,7 @@ func newScreenImageImpl(width, height int) (*imageImpl, error) {
volatile: true, volatile: true,
screen: true, screen: true,
} }
i.pixels.image = i.image
i.pixels.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
@ -192,7 +195,7 @@ func (i *imageImpl) At(x, y int, context *opengl.Context) color.Color {
return color.Transparent return color.Transparent
} }
idx := 4*x + 4*y*i.width idx := 4*x + 4*y*i.width
clr, err := i.pixels.at(i.image, idx, context) clr, err := i.pixels.at(idx, context)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -205,7 +208,7 @@ func (i *imageImpl) flushPixelsIfInconsistent(context *opengl.Context) error {
if i.disposed { if i.disposed {
return nil return nil
} }
if err := i.pixels.flushIfInconsistent(i.image, context); err != nil { if err := i.pixels.flushIfInconsistent(context); err != nil {
return err return err
} }
return nil return nil
@ -217,7 +220,7 @@ func (i *imageImpl) flushPixelsIfNeeded(target *Image, context *opengl.Context)
if i.disposed { if i.disposed {
return nil return nil
} }
if err := i.pixels.flushIfNeeded(i.image, target, context); err != nil { if err := i.pixels.flushIfNeeded(target, context); err != nil {
return err return err
} }
return nil return nil

View File

@ -30,9 +30,14 @@ type drawImageHistoryItem struct {
mode opengl.CompositeMode mode opengl.CompositeMode
} }
type imageForPixels interface {
Pixels(context *opengl.Context) ([]uint8, error)
}
// basePixels and baseColor are exclusive. // basePixels and baseColor are exclusive.
type pixels struct { type pixels struct {
image imageForPixels
inconsistent bool inconsistent bool
basePixels []uint8 basePixels []uint8
baseColor color.Color baseColor color.Color
@ -80,11 +85,11 @@ func (p *pixels) appendDrawImageHistory(item *drawImageHistoryItem) {
p.drawImageHistory = append(p.drawImageHistory, item) p.drawImageHistory = append(p.drawImageHistory, item)
} }
func (p *pixels) at(image *graphics.Image, idx int, context *opengl.Context) (color.Color, error) { func (p *pixels) at(idx int, context *opengl.Context) (color.Color, error) {
if p.inconsistent || p.basePixels == nil || p.drawImageHistory != nil { if p.inconsistent || p.basePixels == nil || p.drawImageHistory != nil {
p.inconsistent = false p.inconsistent = false
var err error var err error
p.basePixels, err = image.Pixels(context) p.basePixels, err = p.image.Pixels(context)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -104,13 +109,13 @@ func (p *pixels) hasHistoryWith(target *Image) bool {
return false return false
} }
func (p *pixels) flushIfInconsistent(image *graphics.Image, context *opengl.Context) error { func (p *pixels) flushIfInconsistent(context *opengl.Context) error {
if !p.inconsistent { if !p.inconsistent {
return nil return nil
} }
p.inconsistent = false p.inconsistent = false
var err error var err error
p.basePixels, err = image.Pixels(context) p.basePixels, err = p.image.Pixels(context)
if err != nil { if err != nil {
return err return err
} }
@ -119,7 +124,7 @@ func (p *pixels) flushIfInconsistent(image *graphics.Image, context *opengl.Cont
return nil return nil
} }
func (p *pixels) flushIfNeeded(image *graphics.Image, target *Image, context *opengl.Context) error { func (p *pixels) flushIfNeeded(target *Image, context *opengl.Context) error {
if p.drawImageHistory == nil { if p.drawImageHistory == nil {
return nil return nil
} }
@ -132,7 +137,7 @@ func (p *pixels) flushIfNeeded(image *graphics.Image, target *Image, context *op
} }
p.inconsistent = false p.inconsistent = false
var err error var err error
p.basePixels, err = image.Pixels(context) p.basePixels, err = p.image.Pixels(context)
if err != nil { if err != nil {
return err return err
} }