-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Making go-sdl2 a bit more compatible with Go standard libraries (#369)
* Using image/color package for SDL_Color * Implementing basic image.Image and draw.Image interfaces * Introducing a truetype-compatible rasterizer * Added some doc * Added some doc * Fixes * Minor changes in test code * Fixed houndci-bot notes * Fixed houndci-bot notes * Fixed houndci-bot notes
- Loading branch information
1 parent
b4c40fb
commit 49a231d
Showing
4 changed files
with
184 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// This code is inspired to golang/freetype/raster | ||
|
||
// Package raster implements a Painter interface for rasterizing paths over | ||
// a generic Image, using its ColorModel to convert from a generic raster color | ||
// to the correct color model. | ||
package raster | ||
|
||
import ( | ||
"github.com/golang/freetype/raster" | ||
"image/color" | ||
"image/draw" | ||
) | ||
|
||
// ImagePainter operates on a generic Image (not only sdl.Surfaces) and allows | ||
// to rasterize a path using a specific color. | ||
type ImagePainter struct { | ||
Image draw.Image | ||
c color.Color | ||
} | ||
|
||
// Paint a batch of Spans using the current ImagePainter image and color. | ||
// Image's Color model will be used to convert the color. | ||
func (p *ImagePainter) Paint(ss []raster.Span, done bool) { | ||
// Convert color to RGBA | ||
dr, dg, db, da := p.c.RGBA() // 16 bit values | ||
// Alpha mask | ||
const m = 1<<16 - 1 | ||
// Draw spans | ||
b := p.Image.Bounds() | ||
for _, s := range ss { | ||
if s.Y < b.Min.Y || s.Y >= b.Max.Y { | ||
continue | ||
} | ||
if s.X0 < b.Min.X { | ||
s.X0 = b.Min.X | ||
} | ||
if s.X1 > b.Max.X { | ||
s.X1 = b.Max.X | ||
} | ||
if s.X0 >= s.X1 { | ||
continue | ||
} | ||
for x := s.X0; x < s.X1; x++ { | ||
y := s.Y - b.Min.Y | ||
var ma uint32 = s.Alpha // 16 bit value | ||
// Get destination pixel color in RGBA64 | ||
sr, sg, sb, sa := p.Image.At(x, y).RGBA() // 16 bit values | ||
// Compute destination color in RGBA64 | ||
var a = (m - (da * ma / m)) | ||
rr := uint16((dr*ma + sr*a) / m) | ||
gg := uint16((dg*ma + sg*a) / m) | ||
bb := uint16((db*ma + sb*a) / m) | ||
aa := uint16((da*ma + sa*a) / m) | ||
// Use image model to convert | ||
p.Image.Set(x, y, color.RGBA64{rr, gg, bb, aa}) | ||
} | ||
} | ||
} | ||
|
||
// SetColor set the color to use when rasterizing | ||
func (p *ImagePainter) SetColor(c color.Color) { | ||
p.c = c | ||
} | ||
|
||
// NewImagePainter builds a Painter for a generic Image | ||
func NewImagePainter(m draw.Image) *ImagePainter { | ||
return &ImagePainter{Image: m} | ||
} |
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,41 @@ | ||
package raster | ||
|
||
import ( | ||
"testing" | ||
|
||
"image" | ||
"image/color" | ||
"image/png" | ||
"os" | ||
|
||
"github.com/golang/freetype/raster" | ||
) | ||
|
||
func TestImagePainter(t *testing.T) { | ||
// Spans to render | ||
ss := []raster.Span{ | ||
raster.Span{0, 4, 6, 0xffff}, | ||
raster.Span{1, 3, 7, 0xf0f0}, | ||
raster.Span{2, 2, 8, 0x8f8f}, | ||
raster.Span{3, 1, 9, 0x8080}, | ||
raster.Span{4, 0, 10, 0x0f0f}, | ||
} | ||
|
||
img := image.NewRGBA(image.Rect(0, 0, 10, 10)) | ||
pt := NewImagePainter(img) | ||
pt.SetColor(color.RGBA{0xff, 0x00, 0x00, 0xff}) | ||
pt.Paint(ss, false) | ||
|
||
// Write to PNG | ||
f, err := os.Create("output.png") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if err := png.Encode(f, img); err != nil { | ||
f.Close() | ||
t.Error(err) | ||
} | ||
if err := f.Close(); err != nil { | ||
t.Error(err) | ||
} | ||
} |
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