70 lines
1.8 KiB
Go
70 lines
1.8 KiB
Go
package xcanvas
|
|
|
|
import "image"
|
|
import "git.tebibyte.media/tomo/tomo/canvas"
|
|
|
|
// Texture is a read-only image texture that can be quickly written to a canvas.
|
|
// It must be closed manually after use.
|
|
type Texture struct {
|
|
pix []uint8
|
|
stride int
|
|
rect image.Rectangle
|
|
transparent bool
|
|
}
|
|
|
|
// NewTextureFrom creates a new texture from a source image.
|
|
func NewTextureFrom (source image.Image) *Texture {
|
|
bounds := source.Bounds()
|
|
texture := &Texture {
|
|
pix: make([]uint8, bounds.Dx() * bounds.Dy() * 4),
|
|
stride: bounds.Dx(),
|
|
rect: bounds.Sub(bounds.Min),
|
|
}
|
|
|
|
index := 0
|
|
var point image.Point
|
|
for point.Y = bounds.Min.Y; point.Y < bounds.Max.Y; point.Y ++ {
|
|
for point.X = bounds.Min.X; point.X < bounds.Max.X; point.X ++ {
|
|
r, g, b, a := source.At(point.X, point.Y).RGBA()
|
|
texture.pix[index + 0] = uint8(b >> 8)
|
|
texture.pix[index + 1] = uint8(g >> 8)
|
|
texture.pix[index + 2] = uint8(r >> 8)
|
|
texture.pix[index + 3] = uint8(a >> 8)
|
|
index += 4
|
|
|
|
if a != 0xFFFF {
|
|
texture.transparent = true
|
|
}
|
|
}}
|
|
|
|
return texture
|
|
}
|
|
|
|
// Opaque reports whether or not the texture is fully opaque.
|
|
func (this *Texture) Opaque () bool {
|
|
return !this.transparent
|
|
}
|
|
|
|
// Close frees the texture from memory.
|
|
func (this *Texture) Close () error {
|
|
// i lied we dont actually need to close this, but we will once this
|
|
// texture resides on the x server or in video memory.
|
|
return nil
|
|
}
|
|
|
|
// Clip returns a subset of this texture that points to the same data.
|
|
func (this *Texture) Clip (bounds image.Rectangle) canvas.Texture {
|
|
clipped := *this
|
|
clipped.rect = bounds
|
|
return &clipped
|
|
}
|
|
|
|
// AssertTexture checks if a given canvas.Texture is a texture from this package.
|
|
func AssertTexture (unknown canvas.Texture) *Texture {
|
|
if tx, ok := unknown.(*Texture); ok {
|
|
return tx
|
|
} else {
|
|
panic("foregin texture implementation, i did not make this!")
|
|
}
|
|
}
|