Example #1
0
// At returns the color of the image at (x, y).
//
// This method loads pixels from VRAM to system memory if necessary.
func (i *Image) At(x, y int) color.Color {
	if i.pixels == nil {
		useGLContext(func(c *opengl.Context) {
			var err error
			i.pixels, err = i.framebuffer.Pixels(c)
			if err != nil {
				panic(err)
			}
		})
	}
	w, _ := i.Size()
	w = graphics.NextPowerOf2Int(w)
	idx := 4*x + 4*y*w
	r, g, b, a := i.pixels[idx], i.pixels[idx+1], i.pixels[idx+2], i.pixels[idx+3]
	return color.RGBA{r, g, b, a}
}
Example #2
0
func u(x int, width int) int {
	return math.MaxInt16 * x / graphics.NextPowerOf2Int(width)
}
Example #3
0
func v(y int, height int) int {
	return math.MaxInt16 * y / graphics.NextPowerOf2Int(height)
}
Example #4
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
}