gotrace

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2021 License: GPL-2.0 Imports: 8 Imported by: 4

README

GoTrace

Pure Go implementation of Potrace vectorization library. Supports simple SVG output generation.

Also includes cgo bindings for the original Potrace library.

Original image

Original

Vectorized image

Vectorized

Installation

go get github.com/dennwc/gotrace

Usage

Process image with an alpha channel, generate SVG:

bm := gotrace.NewBitmapFromImage(img, nil)
paths, _ := gotrace.Trace(bm, nil)
gotrace.WriteSvg(file, img.Bounds(), paths, "")

Custom threshold function:

bm := gotrace.NewBitmapFromImage(img, func(x, y int, c color.Color) bool {
    r, g, b, _ := c.RGBA()
    return r + g + b > 128
})
paths, _ := gotrace.Trace(bm, nil)

Documentation

Overview

Package gotrace is a pure Go implementation of potrace vectorization library. More info at http://potrace.sourceforge.net/potracelib.pdf

Index

Constants

View Source
const (
	// TypeBezier represents Bezier curve segment.
	TypeBezier = SegmType(1)
	// TypeCorner represents a corner segment.
	TypeCorner = SegmType(2)
)
View Source
const (
	TurnBlack    = TurnPolicy(0)
	TurnWhite    = TurnPolicy(1)
	TurnLeft     = TurnPolicy(2)
	TurnRight    = TurnPolicy(3)
	TurnMinority = TurnPolicy(4)
	TurnMajority = TurnPolicy(5)
	TurnRandom   = TurnPolicy(6)
)

Supported turn policies.

Variables

View Source
var Defaults = Params{
	TurdSize:     2,
	TurnPolicy:   TurnMinority,
	AlphaMax:     1.0,
	OptiCurve:    true,
	OptTolerance: 0.2,
}

Defaults cores default parameters for tracing.

Functions

func ThresholdAlpha

func ThresholdAlpha(_, _ int, cl color.Color) bool

ThresholdAlpha is a threshold function that cuts fully transparent parts from the image.

func WriteSvg

func WriteSvg(w io.Writer, rect image.Rectangle, paths []Path, color string) error

WriteSvg writes an SVG file with specified paths. Dimensions of an image will be set to rect, and paths will be of a specified color (#rrggbb).

Types

type Bitmap

type Bitmap struct {
	W, H int    // width and height, in pixels
	Dy   int    // words per scanline (not bytes)
	Map  []Word // raw data, dy*h words
}

Bitmap is an internal bitmap format. The n-th scanline starts at scanline(n) = (map + n*dy). Raster data is stored as a sequence of potrace_words (NOT bytes). The leftmost bit of scanline n is the most significant bit of scanline(n)[0].

func NewBitmap

func NewBitmap(w, h int) *Bitmap

NewBitmap creates a new bitmap with given dimensions.

func NewBitmapFromImage

func NewBitmapFromImage(img image.Image, threshold ThresholdFunc) *Bitmap

NewBitmapFromImage converts an image to a bitmap using a threshold function. If function is not specified, an Alpha channel will be used for the threshold.

func (*Bitmap) Clear

func (bm *Bitmap) Clear(c bool)

Clear the given bitmap. Set all bits to c.

func (*Bitmap) Clone

func (bm *Bitmap) Clone() *Bitmap

Clone duplicates the given bitmap.

func (*Bitmap) Get

func (bm *Bitmap) Get(x, y int) bool

Get a bitmap value at given coordinates.

func (*Bitmap) Set

func (bm *Bitmap) Set(x, y int, v bool)

Set a bitmap value at given coordinates.

type Params

type Params struct {
	TurdSize     int
	TurnPolicy   TurnPolicy
	AlphaMax     float64
	OptiCurve    bool
	OptTolerance float64
}

Params is a set of tracing parameters.

type Path

type Path struct {
	Area  int
	Sign  int
	Curve []Segment

	Childs []Path
	// contains filtered or unexported fields
}

Path represents a curve path.

func Trace

func Trace(bm *Bitmap, param *Params) ([]Path, error)

Trace the bitmap using specified parameters for the algorithm. If parameters is nil, defaults will be used.

func (Path) ToSvgPath

func (p Path) ToSvgPath() string

ToSvgPath converts the path to an SVG path string.

type Point

type Point struct {
	X, Y float64
}

Point is a point on a 2D plane.

type SegmType

type SegmType int

SegmType is a type of a path segment.

type Segment

type Segment struct {
	Type SegmType
	Pnt  [3]Point
}

Segment is a pth segment.

type ThresholdFunc

type ThresholdFunc func(x, y int, cl color.Color) bool

ThresholdFunc is a function that converts a color to a a single bit value.

type TurnPolicy

type TurnPolicy int

TurnPolicy is a policy for tracing the bitmap.

type Word

type Word uint

Word packs multiple bits of a bitmap.

Directories

Path Synopsis
Package gotrace provides bindings for potracelib - bitmap to vector graphics converter.
Package gotrace provides bindings for potracelib - bitmap to vector graphics converter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL