capture.go 969 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package gl
  2. import (
  3. "image"
  4. "image/color"
  5. "fyne.io/fyne/v2"
  6. )
  7. type captureImage struct {
  8. pix []uint8
  9. width, height int
  10. }
  11. func (c *captureImage) ColorModel() color.Model {
  12. return color.RGBAModel
  13. }
  14. func (c *captureImage) Bounds() image.Rectangle {
  15. return image.Rect(0, 0, c.width, c.height)
  16. }
  17. func (c *captureImage) At(x, y int) color.Color {
  18. start := ((c.height-y-1)*c.width + x) * 4
  19. return color.RGBA{R: c.pix[start], G: c.pix[start+1], B: c.pix[start+2], A: c.pix[start+3]}
  20. }
  21. func (p *painter) Capture(c fyne.Canvas) image.Image {
  22. pos := fyne.NewPos(c.Size().Width, c.Size().Height)
  23. width, height := c.PixelCoordinateForPosition(pos)
  24. pixels := make([]uint8, width*height*4)
  25. p.contextProvider.RunWithContext(func() {
  26. p.ctx.ReadBuffer(front)
  27. p.logError()
  28. p.ctx.ReadPixels(0, 0, width, height, colorFormatRGBA, unsignedByte, pixels)
  29. p.logError()
  30. })
  31. return &captureImage{
  32. pix: pixels,
  33. width: width,
  34. height: height,
  35. }
  36. }