gotrace

package module
v0.0.0-...-8c9665a Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2023 License: GPL-2.0 Imports: 11 Imported by: 0

README

GoTrace

Pure Go implementation of Potrace vectorization library. Supports SVG, PDF and DXF output generation.

It is a direct machine translation (transpilation) of potrace using cxgo.

Original image

Original

Vectorized image

Vectorized

Installation

Tool

go install github.com/gotranspile/gotrace@latest
Usage

Convert PNG image to SVG:

gotrace -s -o ./testdata/stanford.svg ./testdata/stanford.png

Library

go get -u github.com/gotranspile/gotrace
Usage

Minimal example:

func traceImage(outPath string, img image.Image) error {
    bm := gotrace.BitmapFromImage(img, nil)
    
    paths, err := gotrace.Trace(bm, nil)
    if err != nil {
        return err
    }
    
    sz := img.Bounds().Size()
    return gotrace.RenderFile("svg", nil, outPath, paths, sz.X, sz.Y)
}

For a full example, see example_test.go.

Updating the source

This library uses cxgo to translate C source code directly to Go. See cxgo.yml for the config.

To regenerate source, install cxgo and goimports and run:

go generate

Documentation

Index

Constants

View Source
const DefaultPaperHeight = 792
View Source
const DefaultPaperWidth = 612
View Source
const GM_MODE_NEGATIVE = 4
View Source
const GM_MODE_NONZERO = 1
View Source
const GM_MODE_ODD = 2
View Source
const GM_MODE_POSITIVE = 3
View Source
const POTRACE_CORNER = 2
View Source
const POTRACE_CURVETO = 1
View Source
const TurnBlack = 0
View Source
const TurnLeft = 2
View Source
const TurnMajority = 5
View Source
const TurnMinority = 4
View Source
const TurnRandom = 6
View Source
const TurnRight = 3
View Source
const TurnWhite = 1
View Source
const Version = "1.16"

Variables

View Source
var (
	ErrIncomplete = errors.New("tracing incomplete")
)

Functions

func Backends

func Backends() []string

Backends lists names of supported backends.

func GreymapRead

func GreymapRead(f *stdio.File, gmp **Greymap) int

func Render

func Render(backend string, conf *RenderConf, out io.Writer, paths *Path, width, height int) error

Render paths with a given backend.

func RenderFile

func RenderFile(backend string, conf *RenderConf, out string, paths *Path, width, height int) error

RenderFile writes paths with a given backend to a file.

Types

type BackendInfo

type BackendInfo struct {
	Fixed bool
	Pixel bool
}

type Bitmap

type Bitmap struct {
	W   int
	H   int
	Dy  int
	Map []Word
}

func BitmapFromGray

func BitmapFromGray(img *image.Gray, fnc func(c color.Gray) bool) *Bitmap

func BitmapFromGray16

func BitmapFromGray16(img *image.Gray16, fnc func(c color.Gray16) bool) *Bitmap

func BitmapFromImage

func BitmapFromImage(img image.Image, fnc func(c color.Color) bool) *Bitmap

func BitmapFromNRGBA

func BitmapFromNRGBA(img *image.NRGBA, fnc func(c color.NRGBA) bool) *Bitmap

func BitmapFromRGBA

func BitmapFromRGBA(img *image.RGBA, fnc func(c color.RGBA) bool) *Bitmap

func BitmapRead

func BitmapRead(r io.Reader, threshold float64) (*Bitmap, error)

func BitmapReadFile

func BitmapReadFile(path string, threshold float64) (*Bitmap, error)

func NewBitmap

func NewBitmap(w int, h int) *Bitmap

func (*Bitmap) Get

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

func (*Bitmap) Put

func (bm *Bitmap) Put(x int, y int, b bool)

type Config

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

func DefaultConfig

func DefaultConfig() *Config

type Curve

type Curve struct {
	N   int
	Tag []int
	C   [][3]DPoint
}

type DPoint

type DPoint struct {
	X float64
	Y float64
}

type Dim

type Dim struct {
	X float64
	D float64
}

type FileConfig

type FileConfig struct {
	Outfile     *byte
	Infiles     **byte
	Infilecount int
	Invert      bool
	Highpass    bool
	Lambda      float64
	Lowpass     bool
	Lambda1     float64
	Scale       int
	Linear      bool
	Bilevel     bool
	Level       float64
	Outext      *byte
}

type Greymap

type Greymap struct {
	W    int
	H    int
	Dy   int
	Base []Sample
	Map  []Sample
}

func NewGreymap

func NewGreymap(w int, h int) *Greymap

type Path

type Path struct {
	Area      int
	Sign      int
	Curve     Curve
	Next      *Path
	Childlist *Path
	Sibling   *Path
	Priv      *potrace_privpath_s
}

func Trace

func Trace(bm *Bitmap, conf *Config) (*Path, error)

Trace a bitmap.

type Point

type Point = image.Point

type Progress

type Progress struct {
	Callback func(progress float64, privdata unsafe.Pointer)
	Data     unsafe.Pointer
	Min      float64
	Max      float64
	Epsilon  float64
}

type RenderConf

type RenderConf struct {
	Backend *BackendInfo

	Debug        bool
	Width_d      Dim
	Height_d     Dim
	Rx           float64
	Ry           float64
	Sx           float64
	Sy           float64
	Stretch      float64
	Lmar_d       Dim
	Rmar_d       Dim
	Tmar_d       Dim
	Bmar_d       Dim
	Angle        float64
	Paperwidth   int
	Paperheight  int
	Tight        int
	Unit         float64
	Compress     bool
	Pslevel      int
	Color        int
	Fillcolor    int
	Gamma        float64
	Longcoding   int
	Outfile      *byte
	Infiles      **byte
	Infilecount  int
	Some_infiles int
	Blacklevel   float64
	Invert       int
	Opaque       bool
	Grouping     int
	Progress     int
}

func NewRenderConf

func NewRenderConf() *RenderConf

NewRenderConf creates a default render config.

type Sample

type Sample int16

type Word

type Word uint

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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