Checkpoint

This commit is contained in:
maze authored and maze committed 2026-10-29 17:29:54 +01:00
1 parent c5d6137fab
commit 4ae7655cd8
223 files changed
+14770 -18352

No files matched your search

+78 -8
View File
@@ -2,8 +2,14 @@ package widget
import (
"image"
"image/color"
"github.com/makeworld-the-better-one/dither/v2"
"github.com/nfnt/resize"
"github.com/sirupsen/logrus"
"github.com/BeatGlow/beatglow/app"
"github.com/BeatGlow/display/pixel"
)
type Image struct {
@@ -19,31 +25,95 @@ func NewImage(size image.Point) *Image {
}
func (w *Image) String() string {
return "image:" + w.image.Bounds().String()
if w.image != nil {
return "image:" + w.image.Bounds().String()
}
return "image"
}
func (w *Image) Update(image image.Image) app.Widget {
func (w *Image) Update(image image.Image, model color.Model) app.Widget {
log := logrus.WithField("widget", w.String())
max := w.Bounds().Size()
thumbnail := resize.Thumbnail(
uint(max.X), uint(max.Y),
convertImageFor(model, image),
resize.Lanczos3)
log.WithFields(logrus.Fields{
"size_max": max,
"size_org": image.Bounds().Size(),
"size_new": thumbnail.Bounds().Size(),
}).Debug("update image")
w.mu.Lock()
w.image = image
w.image = thumbnail
w.mu.Unlock()
w.changed.Store(true)
return w
}
func (w *Image) Refresh(ctx app.Context) app.Widget {
func (w *Image) Refresh(ctx app.Context) bool {
log := logrus.WithField("widget", w.String())
if !w.IsVisible() {
return w
log.Trace("not refreshing; not visisble")
return false
}
if !w.changed.CompareAndSwap(true, false) {
return w
log.Trace("not refreshing; not changed")
return false
}
var refreshed bool
w.mu.RLock()
app.ImageCopyBounds(ctx.Display(), w.image, w.Bounds())
if w.image == nil {
log.Trace("not refreshing; not image")
} else {
log.WithField("size", w.Bounds().Size()).Trace("refreshing image")
app.ImageCopyBounds(ctx.Display(), w.image, w.Bounds().Add(w.position.Load()))
refreshed = true
}
w.mu.RUnlock()
return w
return refreshed
}
var ditherCache = make(map[color.Model]*dither.Ditherer)
func convertImageFor(model color.Model, image image.Image) image.Image {
var palette []color.Color
switch model {
case pixel.MonoModel:
palette = []color.Color{pixel.Off, pixel.On}
case pixel.Gray2Model:
for y := 0; y < 4; y++ {
v := uint8(y) * 0x3F
palette = append(palette, color.RGBA{v, v, v, 0xFF})
}
case pixel.Gray4Model:
for y := 0; y < 16; y++ {
v := uint8(y) * 0x0F
palette = append(palette, color.RGBA{v, v, v, 0xFF})
}
default:
return image
}
d, ok := ditherCache[model]
if !ok {
d = dither.NewDitherer(palette)
d.Matrix = dither.FloydSteinberg
d.Serpentine = true
ditherCache[model] = d
}
return d.Dither(image)
}
var _ app.Widget = (*Image)(nil)