Remove pixels.imageImpl

This commit is contained in:
Hajime Hoshi 2016-07-14 03:00:50 +09:00
parent 4e913531e0
commit dee602e792
2 changed files with 13 additions and 18 deletions

View File

@ -52,7 +52,6 @@ func newImageImpl(width, height int, filter Filter, volatile bool) (*imageImpl,
filter: filter,
volatile: volatile,
}
i.pixels.imageImpl = i
i.pixels.resetWithPixels(make([]uint8, width*height*4))
runtime.SetFinalizer(i, (*imageImpl).Dispose)
return i, nil
@ -85,7 +84,6 @@ func newImageImplFromImage(source image.Image, filter Filter) (*imageImpl, error
height: h,
filter: filter,
}
i.pixels.imageImpl = i
i.pixels.resetWithPixels(pixels)
runtime.SetFinalizer(i, (*imageImpl).Dispose)
return i, nil
@ -103,7 +101,6 @@ func newScreenImageImpl(width, height int) (*imageImpl, error) {
volatile: true,
screen: true,
}
i.pixels.imageImpl = i
i.pixels.resetWithPixels(make([]uint8, width*height*4))
runtime.SetFinalizer(i, (*imageImpl).Dispose)
return i, nil
@ -189,7 +186,8 @@ func (i *imageImpl) At(x, y int, context *opengl.Context) color.Color {
if i.disposed {
return color.Transparent
}
clr, err := i.pixels.at(x, y, context)
idx := 4*x + 4*y*i.width
clr, err := i.pixels.at(i.image, idx, context)
if err != nil {
panic(err)
}
@ -202,7 +200,7 @@ func (i *imageImpl) resetHistoryIfNeeded(target *Image, context *opengl.Context)
if i.disposed {
return nil
}
if err := i.pixels.resetHistoryIfNeeded(target, context); err != nil {
if err := i.pixels.resetHistoryIfNeeded(i.image, target, context); err != nil {
return err
}
return nil
@ -239,7 +237,7 @@ func (i *imageImpl) restore(context *opengl.Context) error {
return nil
}
var err error
i.image, err = i.pixels.restore(context)
i.image, err = i.pixels.restore(context, i.width, i.height, i.filter)
if err != nil {
return err
}

View File

@ -31,7 +31,6 @@ type drawImageHistoryItem struct {
}
type pixels struct {
imageImpl *imageImpl
pixels []uint8
baseColor color.Color
drawImageHistory []*drawImageHistoryItem
@ -62,17 +61,16 @@ func (p *pixels) appendDrawImageHistory(item *drawImageHistoryItem) {
p.drawImageHistory = append(p.drawImageHistory, item)
}
func (p *pixels) at(x, y int, context *opengl.Context) (color.Color, error) {
func (p *pixels) at(image *graphics.Image, idx int, context *opengl.Context) (color.Color, error) {
if p.pixels == nil || p.drawImageHistory != nil {
var err error
p.pixels, err = p.imageImpl.image.Pixels(context)
p.pixels, err = image.Pixels(context)
if err != nil {
return nil, err
}
p.baseColor = nil
p.drawImageHistory = nil
}
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
}
@ -86,7 +84,7 @@ func (p *pixels) hasHistoryWith(target *Image) bool {
return false
}
func (p *pixels) resetHistoryIfNeeded(target *Image, context *opengl.Context) error {
func (p *pixels) resetHistoryIfNeeded(image *graphics.Image, target *Image, context *opengl.Context) error {
if p.drawImageHistory == nil {
return nil
}
@ -94,7 +92,7 @@ func (p *pixels) resetHistoryIfNeeded(target *Image, context *opengl.Context) er
return nil
}
var err error
p.pixels, err = p.imageImpl.image.Pixels(context)
p.pixels, err = image.Pixels(context)
if err != nil {
return err
}
@ -107,12 +105,11 @@ func (p *pixels) hasHistory() bool {
return p.drawImageHistory != nil
}
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))
func (p *pixels) restore(context *opengl.Context, width, height int, filter Filter) (*graphics.Image, error) {
img := image.NewRGBA(image.Rect(0, 0, width, height))
if p.pixels != nil {
for j := 0; j < h; j++ {
copy(img.Pix[j*img.Stride:], p.pixels[j*w*4:(j+1)*w*4])
for j := 0; j < height; j++ {
copy(img.Pix[j*img.Stride:], p.pixels[j*width*4:(j+1)*width*4])
}
} else if p.baseColor != nil {
r32, g32, b32, a32 := p.baseColor.RGBA()
@ -124,7 +121,7 @@ func (p *pixels) restore(context *opengl.Context) (*graphics.Image, error) {
img.Pix[4*idx+3] = a
}
}
gimg, err := graphics.NewImageFromImage(img, glFilter(p.imageImpl.filter))
gimg, err := graphics.NewImageFromImage(img, glFilter(filter))
if err != nil {
return nil, err
}