pack

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2025 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

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

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

Index

Constants

View Source
const (
	Version = "0.0.1"
	Repo    = "https://github.com/91xusir/spritepacker"
	Format  = "RGBA8888"
)
View Source
const (
	NoCompression      clv = 0 // No compression
	BestSpeed          clv = 1 // Best speed
	DefaultCompression clv = 2 // Default compression
	BestCompression    clv = 3 // Best compression
)

Variables

This section is empty.

Functions

func DecImg

func DecImg(r io.Reader) (image.Image, error)

DecImg Supported formats:

  • JPEG
  • PNG
  • TIFF
  • BMP
  • WEBP

func EncImg

func EncImg(w io.Writer, img image.Image, format ImageFormat, compressionLevel ...SetClv) error

EncImg encodes the image to the specified format and writes it to the writer. Parameters:

  • w: the writer to write the encoded image
  • img: the image to encode
  • format: the image format to encode
  • compressionLevel: the compression level to use, default is NoCompression

Returns:

  • error: an error if the format is unsupported or if encoding fails

Supported formats:

  • JPEG
  • PNG
  • TIFF
  • BMP

func GetFilesInDirectory added in v1.0.0

func GetFilesInDirectory(dirPath string) ([]string, error)

GetFilesInDirectory reads files in specified directory and returns file names slice

func GetLastFolderName

func GetLastFolderName(path string) string

GetLastFolderName 获取路径中的最后一个文件夹名称

func GetOpaqueBounds

func GetOpaqueBounds(img image.Image, tolerance uint8) image.Rectangle

GetOpaqueBounds returns the bounds of the opaque area of the image. The image is assumed to be in RGBA format.

func LoadImg

func LoadImg(pathName string) (image.Image, error)

func MaxInt

func MaxInt(a, b int) int

MaxInt returns the larger of a and b.

func MinInt

func MinInt(a, b int) int

MinInt returns the smaller of a and b.

func NaturalLess

func NaturalLess(a, b string) bool

func NaturalSort

func NaturalSort(items []string)

NaturalSort sorts the items in natural order.

func NextPowerOfTwo

func NextPowerOfTwo(n int) int

NextPowerOfTwo returns the next power of two of n.

func Parallel

func Parallel(start, stop int, fn func(<-chan int))

Parallel processes the data in separate goroutines.

func Rotate90

func Rotate90(img image.Image) *image.NRGBA

Rotate90 rotates the image 90 degrees counter-clockwise and returns the transformed image.

func Rotate180

func Rotate180(img image.Image) *image.NRGBA

Rotate180 rotates the image 180 degrees counter-clockwise and returns the transformed image.

func Rotate270

func Rotate270(img image.Image) *image.NRGBA

Rotate270 rotates the image 270 degrees counter-clockwise and returns the transformed image.

func SaveImg added in v1.0.0

func SaveImg(pathName string, img image.Image, format ImageFormat, compressionLevel ...SetClv) error

func SaveImgExt added in v1.0.0

func SaveImgExt(fileName string, img image.Image, compressionLevel ...SetClv) error

func UnpackSprites

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

Types

type Algorithm

type Algorithm int

Algorithm defines the packing algorithm used.

const (
	AlgoBasic Algorithm = iota
	AlgoSkyline
	AlgoMaxRects
	MaxAlgoIndex
)

type Atlas

type Atlas struct {
	Name    string   `json:"name"`
	Size    Size     `json:"size"`
	Sprites []Sprite `json:"sprites"`
}

type Bin

type Bin struct {
	Rect
	PackedRects []PackedRect
	UsedArea    int
	FillRate    float64
}

Bin represents a Bin with a width, height,and a list of rectangles have been packed.

func NewBin

func NewBin(w, h int, req []PackedRect, usedArea int, fillRate float64) Bin

NewBin creates a new bin with the given width, height, and list of rectangles to be packed.

func (Bin) String

func (b Bin) String() string

type FreeRect added in v1.0.0

type FreeRect struct {
	Rect
	X, Y int
}

type Heuristic

type Heuristic int

Heuristic defines the heuristic used by the MaxRects algorithm.

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

type ImageFormat

type ImageFormat int
const (
	JPEG ImageFormat = iota
	PNG
	TIFF
	BMP
	WEBP
)

type Meta

type Meta struct {
	Repo      string `json:"repo"`
	Format    string `json:"format"`
	Version   string `json:"version"`
	Timestamp string `json:"timestamp"`
}

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) 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) 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) PmAlpha added in v1.0.0

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

PmAlpha sets the premultiplied alpha.

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 PackedRect added in v1.0.0

type PackedRect struct {
	X int
	Y int
	Rect
	IsRotated bool
}

PackedRect represents a packed rectangle

func NewRectPacked added in v1.0.0

func NewRectPacked(x, y int, rect Rect) PackedRect

NewRectPacked creates a new PackedRect with the given ID, x, y, width, height, and rotation flag. If isRotated is true, the rectangle is rotated 90 degrees.

func (PackedRect) Rotated added in v1.0.0

func (r PackedRect) Rotated() PackedRect

func (PackedRect) String added in v1.0.0

func (r PackedRect) String() string

type PackedResult added in v1.0.0

type PackedResult struct {
	Bin           Bin
	UnpackedRects []Rect
}

PackedResult represents the result of packing rectangles into a bin.

func (PackedResult) String added in v1.0.0

func (r PackedResult) String() string

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 []Rect) PackedResult

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

func (*Packer) PackSprites

func (p *Packer) PackSprites(spritePaths []string) (*SpriteAtlas, []image.Image, error)

PackSprites packs the given sprite images

Parameters:

  • spritePaths: the paths of the sprite images

Returns:

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

type Rect

type Rect struct {
	W  int
	H  int
	Id int
}

Rect represents an immutable rectangle using value semantics.

func NewRect

func NewRect(w, h int) Rect

NewRect creates a new Rect value.

func NewRectById added in v1.0.0

func NewRectById(w, h int, id int) Rect

NewRectById creates a configured Rect value.

func (Rect) Area added in v1.0.0

func (r Rect) Area() int

Area returns the area of the rectangle.

func (Rect) Rotated

func (r Rect) Rotated() Rect

Rotated returns a new rotated rectangle.

func (Rect) String

func (r Rect) String() string

type Rectangle added in v1.0.0

type Rectangle struct {
	X int `json:"x"`
	Y int `json:"y"`
	W int `json:"w"`
	H int `json:"h"`
}

type SetClv

type SetClv func(*encodeImgOpt)

func WithCLV

func WithCLV(clv clv) SetClv

WithCLV sets the compression level for the image.

type Size

type Size struct {
	W int `json:"w"`
	H int `json:"h"`
}

type Sprite

type Sprite struct {
	Filepath    string    `json:"filepath"`
	Frame       Rectangle `json:"frame"`
	SrcRect     Size      `json:"srcRect"`
	TrimmedRect Rectangle `json:"trimmedRect,omitzero"`
	Rotated     bool      `json:"rotated"`
	Trimmed     bool      `json:"trimmed"`
}

type SpriteAtlas

type SpriteAtlas struct {
	Meta    Meta    `json:"meta"`
	Atlases []Atlas `json:"atlases"`
}

type UnpackOpts

type UnpackOpts func(*unpackedOpts)

func WithAtlasImgPath added in v1.0.0

func WithAtlasImgPath(atlasImgPath string) UnpackOpts

func WithOutputPath added in v1.0.0

func WithOutputPath(outputPath string) UnpackOpts

Jump to

Keyboard shortcuts

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