2011-06-01 07:38:45 +00:00
|
|
|
package raster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2012-01-13 09:14:12 +00:00
|
|
|
"image"
|
|
|
|
"image/color"
|
2011-06-01 07:38:45 +00:00
|
|
|
"image/png"
|
2012-01-13 09:14:12 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2015-04-16 09:51:13 +00:00
|
|
|
|
|
|
|
"code.google.com/p/freetype-go/freetype/raster"
|
2015-04-19 15:14:42 +00:00
|
|
|
"github.com/llgcode/draw2d/curve"
|
2011-06-01 07:38:45 +00:00
|
|
|
)
|
|
|
|
|
2015-07-10 14:58:36 +00:00
|
|
|
var flatteningThreshold = 0.5
|
2011-06-01 07:38:45 +00:00
|
|
|
|
|
|
|
func savepng(filePath string, m image.Image) {
|
|
|
|
f, err := os.Create(filePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
b := bufio.NewWriter(f)
|
|
|
|
err = png.Encode(b, m)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
err = b.Flush()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Path struct {
|
|
|
|
points []float64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Path) LineTo(x, y float64) {
|
|
|
|
if len(p.points)+2 > cap(p.points) {
|
|
|
|
points := make([]float64, len(p.points)+2, len(p.points)+32)
|
|
|
|
copy(points, p.points)
|
|
|
|
p.points = points
|
|
|
|
} else {
|
|
|
|
p.points = p.points[0 : len(p.points)+2]
|
|
|
|
}
|
|
|
|
p.points[len(p.points)-2] = x
|
|
|
|
p.points[len(p.points)-1] = y
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFreetype(t *testing.T) {
|
|
|
|
var p Path
|
|
|
|
p.LineTo(10, 190)
|
|
|
|
c := curve.CubicCurveFloat64{10, 190, 10, 10, 190, 10, 190, 190}
|
2015-07-10 14:58:36 +00:00
|
|
|
c.Segment(&p, flatteningThreshold)
|
2011-06-01 07:38:45 +00:00
|
|
|
poly := Polygon(p.points)
|
2012-01-13 09:14:12 +00:00
|
|
|
color := color.RGBA{0, 0, 0, 0xff}
|
2011-06-01 07:38:45 +00:00
|
|
|
|
2012-01-13 09:14:12 +00:00
|
|
|
img := image.NewRGBA(image.Rect(0, 0, 200, 200))
|
2011-06-01 07:38:45 +00:00
|
|
|
rasterizer := raster.NewRasterizer(200, 200)
|
|
|
|
rasterizer.UseNonZeroWinding = false
|
2015-07-10 13:24:23 +00:00
|
|
|
rasterizer.Start(raster.Point{
|
|
|
|
X: raster.Fix32(10 * 256),
|
|
|
|
Y: raster.Fix32(190 * 256)})
|
2011-06-01 07:38:45 +00:00
|
|
|
for j := 0; j < len(poly); j = j + 2 {
|
2015-07-10 13:24:23 +00:00
|
|
|
rasterizer.Add1(raster.Point{
|
|
|
|
X: raster.Fix32(poly[j] * 256),
|
|
|
|
Y: raster.Fix32(poly[j+1] * 256)})
|
2011-06-01 07:38:45 +00:00
|
|
|
}
|
|
|
|
painter := raster.NewRGBAPainter(img)
|
|
|
|
painter.SetColor(color)
|
|
|
|
rasterizer.Rasterize(painter)
|
|
|
|
|
2015-07-10 15:20:23 +00:00
|
|
|
savepng("../output/raster/TestFreetype.png", img)
|
2011-06-01 07:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFreetypeNonZeroWinding(t *testing.T) {
|
|
|
|
var p Path
|
|
|
|
p.LineTo(10, 190)
|
|
|
|
c := curve.CubicCurveFloat64{10, 190, 10, 10, 190, 10, 190, 190}
|
2015-07-10 14:58:36 +00:00
|
|
|
c.Segment(&p, flatteningThreshold)
|
2011-06-01 07:38:45 +00:00
|
|
|
poly := Polygon(p.points)
|
2012-01-13 09:14:12 +00:00
|
|
|
color := color.RGBA{0, 0, 0, 0xff}
|
2011-06-01 07:38:45 +00:00
|
|
|
|
2012-01-13 09:14:12 +00:00
|
|
|
img := image.NewRGBA(image.Rect(0, 0, 200, 200))
|
2011-06-01 07:38:45 +00:00
|
|
|
rasterizer := raster.NewRasterizer(200, 200)
|
|
|
|
rasterizer.UseNonZeroWinding = true
|
2015-07-10 13:24:23 +00:00
|
|
|
rasterizer.Start(raster.Point{
|
|
|
|
X: raster.Fix32(10 * 256),
|
|
|
|
Y: raster.Fix32(190 * 256)})
|
2011-06-01 07:38:45 +00:00
|
|
|
for j := 0; j < len(poly); j = j + 2 {
|
2015-07-10 13:24:23 +00:00
|
|
|
rasterizer.Add1(raster.Point{
|
|
|
|
X: raster.Fix32(poly[j] * 256),
|
|
|
|
Y: raster.Fix32(poly[j+1] * 256)})
|
2011-06-01 07:38:45 +00:00
|
|
|
}
|
|
|
|
painter := raster.NewRGBAPainter(img)
|
|
|
|
painter.SetColor(color)
|
|
|
|
rasterizer.Rasterize(painter)
|
|
|
|
|
2015-07-10 15:20:23 +00:00
|
|
|
savepng("../output/raster/TestFreetypeNonZeroWinding.png", img)
|
2011-06-01 07:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRasterizer(t *testing.T) {
|
2012-01-13 09:14:12 +00:00
|
|
|
img := image.NewRGBA(image.Rect(0, 0, 200, 200))
|
2011-06-01 07:38:45 +00:00
|
|
|
var p Path
|
|
|
|
p.LineTo(10, 190)
|
|
|
|
c := curve.CubicCurveFloat64{10, 190, 10, 10, 190, 10, 190, 190}
|
2015-07-10 14:58:36 +00:00
|
|
|
c.Segment(&p, flatteningThreshold)
|
2011-06-01 07:38:45 +00:00
|
|
|
poly := Polygon(p.points)
|
2012-01-13 09:14:12 +00:00
|
|
|
color := color.RGBA{0, 0, 0, 0xff}
|
2011-06-01 07:38:45 +00:00
|
|
|
tr := [6]float64{1, 0, 0, 1, 0, 0}
|
|
|
|
r := NewRasterizer8BitsSample(200, 200)
|
2015-04-16 09:51:13 +00:00
|
|
|
//PolylineBresenham(img, image.Black, poly...)
|
2011-06-01 07:38:45 +00:00
|
|
|
|
|
|
|
r.RenderEvenOdd(img, &color, &poly, tr)
|
2015-07-10 15:20:23 +00:00
|
|
|
savepng("../output/raster/TestRasterizer.png", img)
|
2011-06-01 07:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRasterizerNonZeroWinding(t *testing.T) {
|
2012-01-13 09:14:12 +00:00
|
|
|
img := image.NewRGBA(image.Rect(0, 0, 200, 200))
|
2011-06-01 07:38:45 +00:00
|
|
|
var p Path
|
|
|
|
p.LineTo(10, 190)
|
|
|
|
c := curve.CubicCurveFloat64{10, 190, 10, 10, 190, 10, 190, 190}
|
2015-07-10 14:58:36 +00:00
|
|
|
c.Segment(&p, flatteningThreshold)
|
2011-06-01 07:38:45 +00:00
|
|
|
poly := Polygon(p.points)
|
2012-01-13 09:14:12 +00:00
|
|
|
color := color.RGBA{0, 0, 0, 0xff}
|
2011-06-01 07:38:45 +00:00
|
|
|
tr := [6]float64{1, 0, 0, 1, 0, 0}
|
|
|
|
r := NewRasterizer8BitsSample(200, 200)
|
2015-04-16 09:51:13 +00:00
|
|
|
//PolylineBresenham(img, image.Black, poly...)
|
2011-06-01 07:38:45 +00:00
|
|
|
|
|
|
|
r.RenderNonZeroWinding(img, &color, &poly, tr)
|
2015-07-10 15:20:23 +00:00
|
|
|
savepng("../output/raster/TestRasterizerNonZeroWinding.png", img)
|
2011-06-01 07:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkFreetype(b *testing.B) {
|
|
|
|
var p Path
|
|
|
|
p.LineTo(10, 190)
|
|
|
|
c := curve.CubicCurveFloat64{10, 190, 10, 10, 190, 10, 190, 190}
|
2015-07-10 14:58:36 +00:00
|
|
|
c.Segment(&p, flatteningThreshold)
|
2011-06-01 07:38:45 +00:00
|
|
|
poly := Polygon(p.points)
|
2012-01-13 09:14:12 +00:00
|
|
|
color := color.RGBA{0, 0, 0, 0xff}
|
2011-06-01 07:38:45 +00:00
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2012-01-13 09:14:12 +00:00
|
|
|
img := image.NewRGBA(image.Rect(0, 0, 200, 200))
|
2011-06-01 07:38:45 +00:00
|
|
|
rasterizer := raster.NewRasterizer(200, 200)
|
|
|
|
rasterizer.UseNonZeroWinding = false
|
2015-07-10 13:24:23 +00:00
|
|
|
rasterizer.Start(raster.Point{
|
|
|
|
X: raster.Fix32(10 * 256),
|
|
|
|
Y: raster.Fix32(190 * 256)})
|
2011-06-01 07:38:45 +00:00
|
|
|
for j := 0; j < len(poly); j = j + 2 {
|
2015-07-10 13:24:23 +00:00
|
|
|
rasterizer.Add1(raster.Point{
|
|
|
|
X: raster.Fix32(poly[j] * 256),
|
|
|
|
Y: raster.Fix32(poly[j+1] * 256)})
|
2011-06-01 07:38:45 +00:00
|
|
|
}
|
|
|
|
painter := raster.NewRGBAPainter(img)
|
|
|
|
painter.SetColor(color)
|
|
|
|
rasterizer.Rasterize(painter)
|
|
|
|
}
|
|
|
|
}
|
2015-07-10 13:24:23 +00:00
|
|
|
|
2011-06-01 07:38:45 +00:00
|
|
|
func BenchmarkFreetypeNonZeroWinding(b *testing.B) {
|
|
|
|
var p Path
|
|
|
|
p.LineTo(10, 190)
|
|
|
|
c := curve.CubicCurveFloat64{10, 190, 10, 10, 190, 10, 190, 190}
|
2015-07-10 14:58:36 +00:00
|
|
|
c.Segment(&p, flatteningThreshold)
|
2011-06-01 07:38:45 +00:00
|
|
|
poly := Polygon(p.points)
|
2012-01-13 09:14:12 +00:00
|
|
|
color := color.RGBA{0, 0, 0, 0xff}
|
2011-06-01 07:38:45 +00:00
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2012-01-13 09:14:12 +00:00
|
|
|
img := image.NewRGBA(image.Rect(0, 0, 200, 200))
|
2011-06-01 07:38:45 +00:00
|
|
|
rasterizer := raster.NewRasterizer(200, 200)
|
|
|
|
rasterizer.UseNonZeroWinding = true
|
2015-07-10 13:24:23 +00:00
|
|
|
rasterizer.Start(raster.Point{
|
|
|
|
X: raster.Fix32(10 * 256),
|
|
|
|
Y: raster.Fix32(190 * 256)})
|
2011-06-01 07:38:45 +00:00
|
|
|
for j := 0; j < len(poly); j = j + 2 {
|
2015-07-10 13:24:23 +00:00
|
|
|
rasterizer.Add1(raster.Point{
|
|
|
|
X: raster.Fix32(poly[j] * 256),
|
|
|
|
Y: raster.Fix32(poly[j+1] * 256)})
|
2011-06-01 07:38:45 +00:00
|
|
|
}
|
|
|
|
painter := raster.NewRGBAPainter(img)
|
|
|
|
painter.SetColor(color)
|
|
|
|
rasterizer.Rasterize(painter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkRasterizerNonZeroWinding(b *testing.B) {
|
|
|
|
var p Path
|
|
|
|
p.LineTo(10, 190)
|
|
|
|
c := curve.CubicCurveFloat64{10, 190, 10, 10, 190, 10, 190, 190}
|
2015-07-10 14:58:36 +00:00
|
|
|
c.Segment(&p, flatteningThreshold)
|
2011-06-01 07:38:45 +00:00
|
|
|
poly := Polygon(p.points)
|
2012-01-13 09:14:12 +00:00
|
|
|
color := color.RGBA{0, 0, 0, 0xff}
|
2011-06-01 07:38:45 +00:00
|
|
|
tr := [6]float64{1, 0, 0, 1, 0, 0}
|
|
|
|
for i := 0; i < b.N; i++ {
|
2012-01-13 09:14:12 +00:00
|
|
|
img := image.NewRGBA(image.Rect(0, 0, 200, 200))
|
2011-06-01 07:38:45 +00:00
|
|
|
rasterizer := NewRasterizer8BitsSample(200, 200)
|
|
|
|
rasterizer.RenderNonZeroWinding(img, &color, &poly, tr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkRasterizer(b *testing.B) {
|
|
|
|
var p Path
|
|
|
|
p.LineTo(10, 190)
|
|
|
|
c := curve.CubicCurveFloat64{10, 190, 10, 10, 190, 10, 190, 190}
|
2015-07-10 14:58:36 +00:00
|
|
|
c.Segment(&p, flatteningThreshold)
|
2011-06-01 07:38:45 +00:00
|
|
|
poly := Polygon(p.points)
|
2012-01-13 09:14:12 +00:00
|
|
|
color := color.RGBA{0, 0, 0, 0xff}
|
2011-06-01 07:38:45 +00:00
|
|
|
tr := [6]float64{1, 0, 0, 1, 0, 0}
|
|
|
|
for i := 0; i < b.N; i++ {
|
2012-01-13 09:14:12 +00:00
|
|
|
img := image.NewRGBA(image.Rect(0, 0, 200, 200))
|
2011-06-01 07:38:45 +00:00
|
|
|
rasterizer := NewRasterizer8BitsSample(200, 200)
|
|
|
|
rasterizer.RenderEvenOdd(img, &color, &poly, tr)
|
|
|
|
}
|
|
|
|
}
|