raster

package
v0.20.1 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package raster provides scanline rasterization for 2D paths. This file implements AlphaRuns for RLE-encoded alpha (coverage) values. Based on tiny-skia's alpha_runs.rs (Android/Skia heritage).

Package raster provides scanline rasterization for 2D paths.

Package raster provides scanline rasterization for 2D paths. This file implements anti-aliased filling using 4x supersampling.

Package raster provides scanline rasterization for 2D paths. This file implements SuperBlitter for anti-aliased rendering via 4x supersampling. Based on tiny-skia's path_aa.rs (Android/Skia heritage).

Index

Constants

View Source
const SupersampleMask = SupersampleScale - 1

SupersampleMask is used to extract subpixel coordinates.

View Source
const SupersampleScale = 1 << SupersampleShift

SupersampleScale is the number of subpixels per pixel (4 for 2-bit shift).

View Source
const SupersampleShift = 2

SupersampleShift controls supersampling level: 2 means 4x (1 << 2 = 4).

Variables

This section is empty.

Functions

func CatchOverflow added in v0.19.0

func CatchOverflow(alpha uint16) uint8

CatchOverflow converts 0-256 to 0-255 safely. Input value 256 maps to 255 (handles overflow from accumulation).

Types

type AAPixmap added in v0.19.0

type AAPixmap interface {
	Pixmap
	// BlendPixelAlpha blends a color with the existing pixel using given alpha.
	// alpha is in range 0-255.
	BlendPixelAlpha(x, y int, c RGBA, alpha uint8)
}

AAPixmap extends Pixmap with alpha-blended pixel writing.

type AAPixmapAdapter added in v0.19.0

type AAPixmapAdapter struct {
	Pixmap Pixmap
}

AAPixmapAdapter wraps a basic Pixmap to provide BlendPixelAlpha for AA rendering. This allows non-AA pixmaps to work with the AA rasterizer.

func (*AAPixmapAdapter) BlendPixelAlpha added in v0.19.0

func (a *AAPixmapAdapter) BlendPixelAlpha(x, y int, c RGBA, alpha uint8)

BlendPixelAlpha blends a color with the existing pixel using given alpha. This is a simple implementation that uses alpha to modulate the color.

func (*AAPixmapAdapter) Height added in v0.19.0

func (a *AAPixmapAdapter) Height() int

Height returns the height of the pixmap.

func (*AAPixmapAdapter) SetPixel added in v0.19.0

func (a *AAPixmapAdapter) SetPixel(x, y int, c RGBA)

SetPixel sets a pixel color.

func (*AAPixmapAdapter) Width added in v0.19.0

func (a *AAPixmapAdapter) Width() int

Width returns the width of the pixmap.

type AAPixmapBatch added in v0.19.0

type AAPixmapBatch interface {
	AAPixmap
	// BlendSpanAlpha blends a solid color over a horizontal span with constant alpha.
	// Parameters:
	//   - x, y: starting pixel coordinates
	//   - count: number of pixels to blend
	//   - r, g, b, a: source color (premultiplied alpha, 0-255)
	//   - alpha: coverage alpha (0-255)
	BlendSpanAlpha(x, y, count int, r, g, b, a, alpha uint8)
}

AAPixmapBatch is an optional interface for pixmaps that support batch AA blending. Pixmaps implementing this interface can benefit from SIMD-optimized processing for runs of 16+ pixels with the same coverage alpha.

type ActiveEdge

type ActiveEdge struct {
	// contains filtered or unexported fields
}

ActiveEdge is an edge being processed by the rasterizer.

type ActiveEdgeTable

type ActiveEdgeTable struct {
	// contains filtered or unexported fields
}

ActiveEdgeTable represents edges active at a scanline.

func NewActiveEdgeTable

func NewActiveEdgeTable() *ActiveEdgeTable

NewActiveEdgeTable creates a new active edge table.

func (*ActiveEdgeTable) Add

func (aet *ActiveEdgeTable) Add(edge Edge)

Add adds an edge to the active edge table.

func (*ActiveEdgeTable) AddAtY added in v0.19.0

func (aet *ActiveEdgeTable) AddAtY(edge Edge, y float64)

AddAtY adds an edge to the active edge table with x computed for the given y.

func (*ActiveEdgeTable) Clear

func (aet *ActiveEdgeTable) Clear()

Clear clears all edges.

func (*ActiveEdgeTable) Edges

func (aet *ActiveEdgeTable) Edges() []ActiveEdge

Edges returns the active edges.

func (*ActiveEdgeTable) Remove

func (aet *ActiveEdgeTable) Remove(y float64)

Remove removes inactive edges for the given scanline.

func (*ActiveEdgeTable) Sort

func (aet *ActiveEdgeTable) Sort()

Sort sorts edges by x coordinate (insertion sort for small lists).

func (*ActiveEdgeTable) Update

func (aet *ActiveEdgeTable) Update()

Update updates x positions for the next scanline.

type AlphaRuns added in v0.19.0

type AlphaRuns struct {
	// contains filtered or unexported fields
}

AlphaRuns stores run-length-encoded alpha (supersampling coverage) values. Sparseness allows independent composition of several paths into the same buffer.

func NewAlphaRuns added in v0.19.0

func NewAlphaRuns(width int) *AlphaRuns

NewAlphaRuns creates a new AlphaRuns buffer for the given width.

func (*AlphaRuns) Add added in v0.19.0

func (ar *AlphaRuns) Add(x int, startAlpha uint8, middleCount int, stopAlpha uint8, maxValue uint8, offsetX int) int

Add inserts a run into the buffer. Parameters:

  • x: starting x coordinate
  • startAlpha: alpha for first pixel (if non-zero)
  • middleCount: number of full-coverage pixels
  • stopAlpha: alpha for last pixel (if non-zero)
  • maxValue: maximum alpha value for middle pixels
  • offsetX: hint for where to start searching in runs array

Returns the new offsetX value for the next call on the same scanline.

func (*AlphaRuns) Alpha added in v0.19.0

func (ar *AlphaRuns) Alpha() []uint8

Alpha returns the alpha slice.

func (*AlphaRuns) IsEmpty added in v0.19.0

func (ar *AlphaRuns) IsEmpty() bool

IsEmpty returns true if the scanline contains only a single run of alpha 0.

func (*AlphaRuns) Reset added in v0.19.0

func (ar *AlphaRuns) Reset(width int)

Reset reinitializes the buffer for a new scanline.

func (*AlphaRuns) Runs added in v0.19.0

func (ar *AlphaRuns) Runs() []uint16

Runs returns the runs slice.

type Blitter added in v0.19.0

type Blitter interface {
	// BlitH blits a horizontal span at supersampled coordinates.
	BlitH(x, y uint32, width int)
}

Blitter is an interface for receiving horizontal spans.

type Edge

type Edge struct {
	// contains filtered or unexported fields
}

Edge represents a line segment for scanline rasterization.

func NewEdge

func NewEdge(p0, p1 Point) Edge

NewEdge creates a new edge from two points.

func (*Edge) XAtY

func (e *Edge) XAtY(y float64) float64

XAtY calculates the x coordinate at the given y coordinate.

type FillRule

type FillRule int

FillRule specifies how to determine which areas are inside a path.

const (
	// FillRuleNonZero uses the non-zero winding rule.
	FillRuleNonZero FillRule = iota
	// FillRuleEvenOdd uses the even-odd rule.
	FillRuleEvenOdd
)

type Pixmap

type Pixmap interface {
	Width() int
	Height() int
	SetPixel(x, y int, c RGBA)
}

Pixmap is an interface for writing pixels (avoids import cycle).

type Point

type Point struct {
	X, Y float64
}

Point represents a 2D point (internal copy to avoid import cycle).

type RGBA

type RGBA struct {
	R, G, B, A float64
}

RGBA represents a color (internal copy to avoid import cycle).

type Rasterizer

type Rasterizer struct {
	// contains filtered or unexported fields
}

Rasterizer performs scanline rasterization.

func NewRasterizer

func NewRasterizer(width, height int) *Rasterizer

NewRasterizer creates a new rasterizer for the given dimensions.

func (*Rasterizer) Fill

func (r *Rasterizer) Fill(pixmap Pixmap, points []Point, fillRule FillRule, color RGBA)

Fill rasterizes a filled path onto a pixmap.

func (*Rasterizer) FillAA added in v0.19.0

func (r *Rasterizer) FillAA(pixmap AAPixmap, points []Point, fillRule FillRule, color RGBA)

FillAA rasterizes a filled path with anti-aliasing onto a pixmap. Uses 4x supersampling for smooth edges.

func (*Rasterizer) Stroke

func (r *Rasterizer) Stroke(pixmap Pixmap, points []Point, lineWidth float64, color RGBA)

Stroke rasterizes a stroked path.

type SpanFiller added in v0.5.0

type SpanFiller interface {
	FillSpan(x1, x2, y int, c RGBA)
}

SpanFiller is an optional interface that pixmaps can implement for optimized span filling.

type SuperBlitter added in v0.19.0

type SuperBlitter struct {
	// contains filtered or unexported fields
}

SuperBlitter accumulates supersampled coverage and blits AA pixels.

func NewSuperBlitter added in v0.19.0

func NewSuperBlitter(
	pixmap AAPixmap,
	color RGBA,
	boundsLeft, boundsTop, boundsRight, boundsBottom int,
	clipLeft, clipTop, clipRight, clipBottom int,
) *SuperBlitter

NewSuperBlitter creates a new SuperBlitter for AA rendering. bounds defines the pixel-space bounding box of the path. clipLeft, clipTop, clipRight, clipBottom define the clipping region.

func (*SuperBlitter) BlitH added in v0.19.0

func (sb *SuperBlitter) BlitH(x, y uint32, width int)

BlitH implements Blitter interface for receiving supersampled spans.

func (*SuperBlitter) Flush added in v0.19.0

func (sb *SuperBlitter) Flush()

Flush writes the accumulated coverage to the pixmap.

Jump to

Keyboard shortcuts

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