Exemplo n.º 1
0
func NewImageFromImage(source *image.RGBA, width, height int, filter opengl.Filter) (*Image, error) {
	img, err := graphics.NewImageFromImage(source, width, height, filter)
	if err != nil {
		// TODO: texture should be removed here?
		return nil, err
	}
	return &Image{
		image:  img,
		filter: filter,
	}, nil
}
Exemplo n.º 2
0
// RestoreImage restores *graphics.Image from the pixels using its state.
func (p *Image) Restore(context *opengl.Context) error {
	w, h := p.image.Size()
	if p.screen {
		// The screen image should also be recreated because framebuffer might
		// be changed.
		var err error
		p.image, err = graphics.NewScreenFramebufferImage(w, h)
		if err != nil {
			return err
		}
		p.basePixels = nil
		p.baseColor = color.RGBA{}
		p.drawImageHistory = nil
		p.stale = false
		return nil
	}
	if p.volatile {
		var err error
		p.image, err = graphics.NewImage(w, h, p.filter)
		if err != nil {
			return err
		}
		p.basePixels = nil
		p.baseColor = color.RGBA{}
		p.drawImageHistory = nil
		p.stale = false
		return nil
	}
	if p.stale {
		return errors.New("restorable: pixels must not be stale when restoring")
	}
	img := image.NewRGBA(image.Rect(0, 0, graphics.NextPowerOf2Int(w), graphics.NextPowerOf2Int(h)))
	if p.basePixels != nil {
		for j := 0; j < h; j++ {
			copy(img.Pix[j*img.Stride:], p.basePixels[j*w*4:(j+1)*w*4])
		}
	}
	gimg, err := graphics.NewImageFromImage(img, w, h, p.filter)
	if err != nil {
		return err
	}
	if p.baseColor != (color.RGBA{}) {
		if p.basePixels != nil {
			panic("not reach")
		}
		if err := gimg.Fill(p.baseColor); err != nil {
			return err
		}
	}
	for _, c := range p.drawImageHistory {
		// c.image.impl must be already restored.
		/*if c.image.impl.hasHistory() {
			panic("not reach")
		}*/
		if err := gimg.DrawImage(c.image, c.vertices, c.colorm, c.mode); err != nil {
			return err
		}
	}
	p.image = gimg

	p.basePixels, err = gimg.Pixels(context)
	if err != nil {
		return err
	}
	p.baseColor = color.RGBA{}
	p.drawImageHistory = nil
	p.stale = false
	return nil
}