219 lines
4.5 KiB
Go
219 lines
4.5 KiB
Go
package app
|
|
|
|
import (
|
|
"bytes"
|
|
"image"
|
|
"image/color"
|
|
"image/color/palette"
|
|
"image/draw"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/BeatGlow/display/pixel"
|
|
|
|
_ "image/gif"
|
|
_ "image/jpeg"
|
|
_ "image/png"
|
|
)
|
|
|
|
// BufferFor creates a new image buffer for the provided display matching its color model.
|
|
func BufferFor(model color.Model, size image.Point) draw.Image {
|
|
switch model {
|
|
case pixel.MonoModel:
|
|
return pixel.NewMonoImage(size.X, size.Y)
|
|
case pixel.Gray2Model:
|
|
return pixel.NewGray2Image(size.X, size.Y)
|
|
case pixel.Gray4Model:
|
|
return pixel.NewGray4Image(size.X, size.Y)
|
|
case pixel.CRGB15Model:
|
|
return pixel.NewCRGB15Image(size.X, size.Y)
|
|
case pixel.CRGB16Model:
|
|
return pixel.NewCRGB16Image(size.X, size.Y)
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
type clearer interface {
|
|
Clear()
|
|
}
|
|
|
|
// ImageCopy copies an image over dst.
|
|
func ImageCopy(dst draw.Image, src image.Image) {
|
|
logrus.Tracef("image copy %s -> %s", src.Bounds(), dst.Bounds())
|
|
draw.Draw(dst, dst.Bounds(), src, image.Point{}, draw.Src)
|
|
}
|
|
|
|
// ImageCopyBounds copies an image over dst bounded by r.
|
|
func ImageCopyBounds(dst draw.Image, src image.Image, r image.Rectangle) {
|
|
logrus.Tracef("image copy %s -> %s (of %s)", src.Bounds(), r, dst.Bounds())
|
|
draw.Draw(dst, r, src, image.Point{}, draw.Src)
|
|
}
|
|
|
|
// Clear an image.
|
|
func ImageClear(dst draw.Image) {
|
|
if c, ok := dst.(clearer); ok {
|
|
c.Clear()
|
|
return
|
|
}
|
|
ImageFill(dst, color.Black)
|
|
}
|
|
|
|
type filler interface {
|
|
Fill(color.Color)
|
|
}
|
|
|
|
// Fill an imagre with a single color.
|
|
func ImageFill(dst draw.Image, c color.Color) {
|
|
if f, ok := dst.(filler); ok {
|
|
f.Fill(c)
|
|
return
|
|
}
|
|
|
|
var src image.Image
|
|
switch c {
|
|
case color.Black:
|
|
src = image.Black
|
|
case color.White:
|
|
src = image.White
|
|
case color.Transparent:
|
|
src = image.Transparent
|
|
default:
|
|
src = image.NewUniform(c)
|
|
}
|
|
|
|
draw.Draw(dst, dst.Bounds(), src, image.Point{}, draw.Src)
|
|
}
|
|
|
|
// MustDecodeImage can decode JPEG, GIF and PNG images.
|
|
func MustDecodeImage(b []byte) image.Image {
|
|
i, _, err := image.Decode(bytes.NewBuffer(b))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return i
|
|
}
|
|
|
|
type HorizontalWrappingImage struct {
|
|
image.Image
|
|
Gap int
|
|
}
|
|
|
|
func (i HorizontalWrappingImage) At(x, y int) color.Color {
|
|
var (
|
|
bounds = i.Image.Bounds()
|
|
dx = (bounds.Max.X - bounds.Min.X)
|
|
ox = x
|
|
oy = y
|
|
)
|
|
for ox < bounds.Min.X {
|
|
ox += dx
|
|
}
|
|
for ox >= bounds.Max.X {
|
|
ox -= dx
|
|
}
|
|
return i.Image.At(ox, oy)
|
|
}
|
|
|
|
func (i HorizontalWrappingImage) Bounds() image.Rectangle {
|
|
var (
|
|
bounds = i.Image.Bounds()
|
|
delta = image.Point{X: (bounds.Max.X - bounds.Min.X) + i.Gap}
|
|
)
|
|
return image.Rectangle{
|
|
Min: bounds.Min.Sub(delta),
|
|
Max: bounds.Max.Add(delta),
|
|
}
|
|
}
|
|
|
|
type imageView struct {
|
|
draw.Image
|
|
Rect image.Rectangle
|
|
}
|
|
|
|
func ImageView(dst draw.Image, rect image.Rectangle) draw.Image {
|
|
return imageView{
|
|
Image: dst,
|
|
Rect: rect,
|
|
}
|
|
}
|
|
|
|
func (i imageView) Bounds() image.Rectangle {
|
|
return image.Rectangle{
|
|
Max: i.Rect.Max.Sub(i.Rect.Min),
|
|
}
|
|
}
|
|
|
|
func (i imageView) At(x, y int) color.Color {
|
|
// Translate x, y to inside our view
|
|
x += i.Rect.Min.X
|
|
y += i.Rect.Min.Y
|
|
if !(image.Point{X: x, Y: y}).In(i.Rect) {
|
|
return color.Transparent
|
|
}
|
|
return i.Image.At(x, y)
|
|
}
|
|
|
|
func (i imageView) Set(x, y int, c color.Color) {
|
|
// Translate x, y to inside our view
|
|
x += i.Rect.Min.X
|
|
y += i.Rect.Min.Y
|
|
if !(image.Point{X: x, Y: y}).In(i.Rect) {
|
|
return
|
|
}
|
|
i.Image.Set(x, y, c)
|
|
}
|
|
|
|
func PaletteFor(model color.Model) color.Palette {
|
|
var p color.Palette
|
|
|
|
switch model {
|
|
case pixel.MonoModel:
|
|
p = color.Palette{
|
|
pixel.Off,
|
|
pixel.On,
|
|
}
|
|
|
|
case pixel.Gray2Model:
|
|
p = color.Palette{
|
|
pixel.Gray2{Y: 0},
|
|
pixel.Gray2{Y: 1},
|
|
pixel.Gray2{Y: 2},
|
|
pixel.Gray2{Y: 3},
|
|
}
|
|
|
|
case pixel.Gray4Model:
|
|
p = color.Palette{
|
|
pixel.Gray4{Y: 0x0}, pixel.Gray4{Y: 0x1}, pixel.Gray4{Y: 0x2}, pixel.Gray4{Y: 0x3},
|
|
pixel.Gray4{Y: 0x4}, pixel.Gray4{Y: 0x5}, pixel.Gray4{Y: 0x6}, pixel.Gray4{Y: 0x7},
|
|
pixel.Gray4{Y: 0x8}, pixel.Gray4{Y: 0x9}, pixel.Gray4{Y: 0xa}, pixel.Gray4{Y: 0xb},
|
|
pixel.Gray4{Y: 0xc}, pixel.Gray4{Y: 0xd}, pixel.Gray4{Y: 0xe}, pixel.Gray4{Y: 0xf},
|
|
}
|
|
|
|
default:
|
|
// TODO(maze): implement color quantization
|
|
return palette.Plan9
|
|
}
|
|
|
|
// Convert to RGBA.
|
|
for i, c := range p {
|
|
r, g, b, _ := c.RGBA()
|
|
p[i] = color.RGBA{R: uint8(r >> 8), G: uint8(g >> 8), B: uint8(b >> 8), A: 0xFF}
|
|
}
|
|
return p
|
|
}
|
|
|
|
func ImageToPaletted(dst *image.Paletted, src image.Image, palette color.Palette) *image.Paletted {
|
|
if palette == nil {
|
|
palette = PaletteFor(src.ColorModel())
|
|
}
|
|
|
|
if dst == nil {
|
|
dst = image.NewPaletted(src.Bounds(), palette)
|
|
}
|
|
|
|
ImageCopy(dst, src)
|
|
|
|
return dst
|
|
}
|