package app import ( "bytes" "image" "image/color" "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(ctx Context, size image.Point) draw.Image { switch ctx.Display().ColorModel() { 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) }