spriteframes

package
v0.4.0-rc.3 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package spriteframes is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.

Package spriteframes turns a run of raster frames from a renderer, a video model or a capture into atlas-ready frames: chroma key with despill, loop seam selection, a shared anchor and nearest-neighbor scaling. Every function is pure and deterministic and returns new images, leaving its input untouched.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Anchor

func Anchor(frames []*image.NRGBA, mode AnchorMode) ([]*image.NRGBA, error)

Anchor crops each frame to its non-transparent bounding box and places it in a cell as large as the largest box, at the same anchor in every frame, so a subject that drifts inside its canvas stays put. The cell's pivot is (0.5, 1) for AnchorBottomCenter and (0.5, 0.5) for AnchorCenter. A fully transparent frame is refused.

func ChromaKey

func ChromaKey(frames []*image.NRGBA, opts KeyOptions) ([]*image.NRGBA, error)

ChromaKey returns copies of frames with the key colour made transparent. Each pixel is keyed on its own, so key-coloured holes enclosed by the subject (between an arm and the body, say) are cleared as well as the background around it.

Example

ChromaKey copies frames with the key colour made transparent; each pixel is keyed on its own, so holes enclosed by the subject are cleared too.

package main

import (
	"fmt"
	"image"
	"image/color"

	"github.com/nsgo-dev/game/pkg/tooling/spriteframes"
)

func main() {
	green := color.NRGBA{G: 255, A: 255}
	frame := image.NewNRGBA(image.Rect(0, 0, 2, 2))
	for y := 0; y < 2; y++ {
		for x := 0; x < 2; x++ {
			frame.SetNRGBA(x, y, green)
		}
	}
	out, err := spriteframes.ChromaKey([]*image.NRGBA{frame}, spriteframes.KeyOptions{Color: green, Tolerance: 40})
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(len(out), out[0].NRGBAAt(0, 0).A)
}
Output:
1 0

func LoopSeam

func LoopSeam(frames []*image.NRGBA, opts SeamOptions) (start, end int, loop []*image.NRGBA, err error)

LoopSeam finds the start and end frames that are most alike, so playing frames[start:end] in a loop jumps from end-1 back to start as smoothly as the source moved from end-1 to end. It compares the frames as alpha-weighted luminance thumbnails at most 96 pixels on a side. The end frame itself is dropped, which removes a final frame that duplicates the first. Ties keep the longer loop, then the earlier start. All frames must be the same size.

func ResizeNearest

func ResizeNearest(frames []*image.NRGBA, width, height int) ([]*image.NRGBA, error)

ResizeNearest scales each frame to width by height with nearest-neighbor sampling, which keeps hard pixel edges when reducing a render to a pixel-art grid.

Types

type AnchorMode

type AnchorMode string

AnchorMode is where Anchor places each frame's subject in the shared cell.

const (
	// AnchorBottomCenter puts the bottom edge of each subject on the cell's bottom edge, centered horizontally: feet stay planted.
	AnchorBottomCenter AnchorMode = "bottom-center"
	// AnchorCenter centers each subject in the cell.
	AnchorCenter AnchorMode = "center"
)

type KeyOptions

type KeyOptions struct {
	// Color is the background colour to remove; its alpha is ignored.
	Color color.NRGBA
	// Tolerance is the RGB Euclidean distance from Color at or below which a pixel becomes fully transparent.
	Tolerance uint8
	// Softness is the width of the linear alpha ramp above Tolerance, so pixels between Tolerance and Tolerance+Softness keep part of their alpha.
	Softness uint8
	// Despill is the strength, from 0 (off) to 1, with which the key's dominant channel is pulled down to the larger of the other two channels on edge pixels: pixels left partly transparent and opaque pixels next to a removed one.
	Despill float64
}

KeyOptions controls ChromaKey.

type SeamOptions

type SeamOptions struct {
	// MinFrames is the fewest frames the loop keeps (2 when below 2).
	MinFrames int
	// MaxFrames is the most frames the loop keeps (no limit when 0).
	MaxFrames int
}

SeamOptions bounds the loop LoopSeam picks.

Jump to

Keyboard shortcuts

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