-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad790cc
commit 847c9e2
Showing
26 changed files
with
631 additions
and
464 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package bgra | ||
|
||
import ( | ||
"image" | ||
"image/color" | ||
) | ||
|
||
type BGRA struct { | ||
image.RGBA | ||
} | ||
|
||
var BGRAModel = color.ModelFunc(func(c color.Color) color.Color { | ||
if _, ok := c.(BGRAColor); ok { | ||
return c | ||
} | ||
r, g, b, a := c.RGBA() | ||
return BGRAColor{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8), uint8(a >> 8)} | ||
}) | ||
|
||
// BGRAColor represents a BGRA color. | ||
type BGRAColor struct { | ||
R, G, B, A uint8 | ||
} | ||
|
||
func (c BGRAColor) RGBA() (r, g, b, a uint32) { | ||
r = uint32(c.B) | ||
r |= r << 8 | ||
g = uint32(c.G) | ||
g |= g << 8 | ||
b = uint32(c.R) | ||
b |= b << 8 | ||
a = uint32(255) //uint32(c.A) | ||
a |= a << 8 | ||
return | ||
} | ||
|
||
func NewBGRA(r image.Rectangle) *BGRA { | ||
return &BGRA{*image.NewRGBA(r)} | ||
} | ||
|
||
func (p *BGRA) ColorModel() color.Model { return BGRAModel } | ||
func (p *BGRA) At(x, y int) color.Color { | ||
i := p.PixOffset(x, y) | ||
s := p.Pix[i : i+4 : i+4] | ||
return BGRAColor{s[0], s[1], s[2], s[3]} | ||
} | ||
|
||
func (p *BGRA) Set(x, y int, c color.Color) { | ||
i := p.PixOffset(x, y) | ||
c1 := BGRAModel.Convert(c).(BGRAColor) | ||
s := p.Pix[i : i+4 : i+4] | ||
s[0] = c1.R | ||
s[1] = c1.G | ||
s[2] = c1.B | ||
s[3] = 255 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package rgb565 | ||
|
||
import ( | ||
"encoding/binary" | ||
"image" | ||
"image/color" | ||
"math" | ||
) | ||
|
||
// RGB565 is an in-memory image whose At method returns RGB565 values. | ||
type RGB565 struct { | ||
// Pix holds the image's pixels, as RGB565 values in big-endian format. The pixel at | ||
// (x, y) starts at Pix[(y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*2]. | ||
Pix []uint8 | ||
// Stride is the Pix stride (in bytes) between vertically adjacent pixels. | ||
Stride int | ||
// Rect is the image's bounds. | ||
Rect image.Rectangle | ||
} | ||
|
||
// Model is the model for RGB565 colors. | ||
var Model = color.ModelFunc(func(c color.Color) color.Color { | ||
//if _, ok := c.(Color); ok { | ||
// return c | ||
//} | ||
r, g, b, _ := c.RGBA() | ||
return Color(uint16((r<<8)&rMask | (g<<3)&gMask | (b>>3)&bMask)) | ||
}) | ||
|
||
const ( | ||
rMask = 0b1111100000000000 | ||
gMask = 0b0000011111100000 | ||
bMask = 0b0000000000011111 | ||
) | ||
|
||
// Color represents an RGB565 color. | ||
type Color uint16 | ||
|
||
func (c Color) RGBA() (r, g, b, a uint32) { | ||
return uint32(math.Round(float64(c&rMask>>11)*255.0/31.0)) << 8, | ||
uint32(math.Round(float64(c&gMask>>5)*255.0/63.0)) << 8, | ||
uint32(math.Round(float64(c&bMask)*255.0/31.0)) << 8, | ||
0xffff | ||
} | ||
|
||
func NewRGB565(r image.Rectangle) *RGB565 { | ||
return &RGB565{Pix: make([]uint8, r.Dx()*r.Dy()<<1), Stride: r.Dx() << 1, Rect: r} | ||
} | ||
|
||
func (p *RGB565) Bounds() image.Rectangle { return p.Rect } | ||
func (p *RGB565) ColorModel() color.Model { return Model } | ||
func (p *RGB565) PixOffset(x, y int) int { return (x-p.Rect.Min.X)<<1 + (y-p.Rect.Min.Y)*p.Stride } | ||
|
||
func (p *RGB565) At(x, y int) color.Color { | ||
i := p.PixOffset(x, y) | ||
return Color(binary.LittleEndian.Uint16(p.Pix[i : i+2])) | ||
} | ||
|
||
func (p *RGB565) Set(x, y int, c color.Color) { | ||
i := p.PixOffset(x, y) | ||
binary.LittleEndian.PutUint16(p.Pix[i:i+2], uint16(Model.Convert(c).(Color))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package libretro | ||
|
||
import "sync" | ||
|
||
// Canvas is a stateful drawing surface | ||
type Canvas struct { | ||
w, h int | ||
pool sync.Pool | ||
|
||
Disabled bool | ||
} | ||
|
||
type Frame struct { | ||
Data []byte | ||
Stride int | ||
W, H int | ||
} | ||
|
||
func NewCanvas(w, h, size int) *Canvas { | ||
return &Canvas{w: w, h: h, | ||
pool: sync.Pool{New: func() any { return &Frame{Data: make([]byte, size<<2)} }}} | ||
} | ||
|
||
func (c *Canvas) Get(w, h, stride int, data []byte) Frame { | ||
i := c.pool.Get().(*Frame) | ||
i.Stride = stride | ||
i.W = w | ||
i.H = h | ||
l := stride * h | ||
if data != nil { | ||
i.Data = data | ||
} else if len(i.Data) != l { | ||
i.Data = i.Data[:l] | ||
} | ||
return *i | ||
} | ||
|
||
func (c *Canvas) Put(i *Frame) { | ||
if !c.Disabled { | ||
c.pool.Put(i) | ||
} | ||
} |
Oops, something went wrong.