pack

package
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package pack provides texture packing utilities, including support for rotation, transparent pixel trimming, automatic resizing and so on.

Supported image formats include PNG, JPEG, TIFF, BMP, and WEBP.

Index

Constants

View Source
const (
	Repo   = "https://github.com/91xusir/spritepacker"
	Format = "RGBA8888"
)

Variables

View Source
var (
	Version = "dev"
)

Functions

func UnpackAtlas added in v1.1.4

func UnpackAtlas(atlasInfo *model.AtlasInfo, opts unpackedOpts) error

func UnpackSprites

func UnpackSprites(infoPath string, fn ...UnpackOpts) error

Types

type Algorithm

type Algorithm int

Algorithm defines the packing algorithm used.

const (
	AlgoBasic Algorithm = iota
	AlgoSkyline
	AlgoMaxRects
	MaxAlgoIndex
)

type Heuristic

type Heuristic int

Heuristic defines the heuristic used by the MaxRects algorithm.

const (
	BestShortSideFit Heuristic = iota
	BestLongSideFit
	BestAreaFit
	BottomLeftFit
	ContactPointFit
	MaxHeuristicsIndex
)

type Options

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

func NewOptions

func NewOptions() *Options

func (*Options) Algorithm

func (b *Options) Algorithm(algo Algorithm) *Options

Algorithm sets the packing algorithm of the atlas. If the algorithm is not valid, it will be set to AlgoBasic.

func (*Options) AllowRotate

func (b *Options) AllowRotate(enable bool) *Options

AllowRotate allow sprites to Rotate to optimizeAtlasSize

func (*Options) AutoSize

func (b *Options) AutoSize(enable bool) *Options

AutoSize atlas size will be automatically adjusted.

func (*Options) Default added in v1.1.6

func (b *Options) Default() *Options

func (*Options) Heuristic

func (b *Options) Heuristic(heuristic Heuristic) *Options

Heuristic sets the heuristic of the atlas. If the heuristic is not valid, it will be set to BestShortSideFit. It is valid only when the algorithm is AlgoMaxRects.

func (*Options) ImgExt added in v1.1.4

func (b *Options) ImgExt(ext string) *Options

func (*Options) MaxSize

func (b *Options) MaxSize(w, h int) *Options

MaxSize sets the maximum size of the atlas. If the width or height is less than or equal to 0, it will be set to 512.

func (*Options) Name

func (b *Options) Name(name string) *Options

Name sets the name of the atlas. If the name is empty, the default name is "atlas".

func (*Options) Padding

func (b *Options) Padding(padding int) *Options

Padding sets the padding of the atlas. If the padding is less than 0, it will be set to 0.

func (*Options) PowerOfTwo

func (b *Options) PowerOfTwo(enable bool) *Options

PowerOfTwo sets the power of two of the atlas. The atlas pixels are fixed to a power of 2.

func (*Options) SameDetect

func (b *Options) SameDetect(enable bool) *Options

SameDetect sets the same detection of the atlas.

func (*Options) Sort

func (b *Options) Sort(enable bool) *Options

Sort sets the sorting of the atlas. default method is sort by area.

func (*Options) Tolerance

func (b *Options) Tolerance(tolerance int) *Options

Tolerance sets the tolerance of trimming transparency pixels.

func (*Options) Trim

func (b *Options) Trim(enable bool) *Options

Trim transparent pixels will be trimmed from the image.

func (*Options) Validate

func (b *Options) Validate() (*Options, error)

Validate validates the options. If the options are invalid, it will return an error.

type Packer

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

func NewPacker

func NewPacker(option *Options) *Packer

func (*Packer) PackRect

func (p *Packer) PackRect(reqRects []model.Rect) []model.Bin

PackRect packs the given rectangles into a bin and returns the result.

func (*Packer) PackSprites

func (p *Packer) PackSprites(input string) (*model.AtlasInfo, []image.Image, error)

PackSprites packs the given sprite images

Parameters:

  • spritePaths: the paths of the sprite images

Returns:

  • *AtlasInfo: the sprite atlas info
  • []image.Image: the atlas images
  • error

Example:

spriteAtlas, atlasImages, err := packer.PackSprites("./input")

type UnpackOpts

type UnpackOpts func(*unpackedOpts)

func WithImgInput added in v1.1.5

func WithImgInput(atlasImgPath string) UnpackOpts

func WithOutput

func WithOutput(outputPath string) UnpackOpts

Jump to

Keyboard shortcuts

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