image.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package painter
  2. import (
  3. "image"
  4. _ "image/jpeg" // avoid users having to import when using image widget
  5. _ "image/png" // avoid the same for PNG images
  6. "golang.org/x/image/draw"
  7. "fyne.io/fyne/v2"
  8. "fyne.io/fyne/v2/canvas"
  9. )
  10. // PaintImage renders a given fyne Image to a Go standard image
  11. // If a fyne.Canvas is given and the image’s fill mode is “fill original” the image’s min size has
  12. // to fit its original size. If it doesn’t, PaintImage does not paint the image but adjusts its min size.
  13. // The image will then be painted on the next frame because of the min size change.
  14. func PaintImage(img *canvas.Image, c fyne.Canvas, width, height int) image.Image {
  15. if img.Size().IsZero() && c == nil { // an image without size or canvas won't get rendered unless we setup
  16. img.Resize(fyne.NewSize(float32(width), float32(height)))
  17. }
  18. dst, err := paintImage(img, width, height)
  19. if err != nil {
  20. fyne.LogError("failed to paint image", err)
  21. }
  22. return dst
  23. }
  24. func paintImage(img *canvas.Image, width, height int) (dst image.Image, err error) {
  25. if width <= 0 || height <= 0 {
  26. return
  27. }
  28. dst = img.Image
  29. if dst == nil {
  30. dst = image.NewNRGBA(image.Rect(0, 0, width, height))
  31. }
  32. size := dst.Bounds().Size()
  33. if width != size.X || height != size.Y {
  34. dst = scaleImage(dst, width, height, img.ScaleMode)
  35. }
  36. return
  37. }
  38. func scaleImage(pixels image.Image, scaledW, scaledH int, scale canvas.ImageScale) image.Image {
  39. if scale == canvas.ImageScaleFastest || scale == canvas.ImageScalePixels {
  40. // do not perform software scaling
  41. return pixels
  42. }
  43. bounds := pixels.Bounds()
  44. pixW := int(fyne.Min(float32(scaledW), float32(bounds.Dx()))) // don't push more pixels than we have to
  45. pixH := int(fyne.Min(float32(scaledH), float32(bounds.Dy()))) // the GL calls will scale this up on GPU.
  46. scaledBounds := image.Rect(0, 0, pixW, pixH)
  47. tex := image.NewNRGBA(scaledBounds)
  48. switch scale {
  49. case canvas.ImageScalePixels:
  50. draw.NearestNeighbor.Scale(tex, scaledBounds, pixels, bounds, draw.Over, nil)
  51. default:
  52. if scale != canvas.ImageScaleSmooth {
  53. fyne.LogError("Invalid canvas.ImageScale value, using canvas.ImageScaleSmooth", nil)
  54. }
  55. draw.CatmullRom.Scale(tex, scaledBounds, pixels, bounds, draw.Over, nil)
  56. }
  57. return tex
  58. }