op/paint: add nearest neighbor scaling

This adds support for nearest neighbor filtering,
which can be useful in quite a few scenarios.

  img := paint.NewImageOp(m)
  img.Filter = paint.FilterNearest
  img.Add(gtx.Ops)

Fixes: https://todo.sr.ht/~eliasnaur/gio/414
Signed-off-by: Egon Elbre <egonelbre@gmail.com>
This commit is contained in:
Egon Elbre
2023-11-15 12:02:08 +02:00
committed by Elias Naur
parent 23b6f06e3e
commit 5fa94ff67b
6 changed files with 113 additions and 4 deletions
+13
View File
@@ -15,8 +15,20 @@ import (
"gioui.org/op/clip"
)
// ImageFilter is the scaling filter for images.
type ImageFilter byte
const (
// FilterLinear uses linear interpolation for scaling.
FilterLinear ImageFilter = iota
// FilterNearest uses nearest neighbor interpolation for scaling.
FilterNearest
)
// ImageOp sets the brush to an image.
type ImageOp struct {
Filter ImageFilter
uniform bool
color color.NRGBA
src *image.RGBA
@@ -103,6 +115,7 @@ func (i ImageOp) Add(o *op.Ops) {
}
data := ops.Write2(&o.Internal, ops.TypeImageLen, i.src, i.handle)
data[0] = byte(ops.TypeImage)
data[1] = byte(i.Filter)
}
func (c ColorOp) Add(o *op.Ops) {