pack

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2025 License: MIT Imports: 24 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 GetLastFolderName

func GetLastFolderName(path string) string

GetLastFolderName

Parameters:

  • path: the path

Returns:

  • string: the last folder name

Example:

folderName := GetLastFolderName("./sprites/atlas/a.png")
// folderName = "atlas"

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 ListFilePaths

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

ListFilePaths

Parameters:

  • dirPath: the directory path

Returns:

  • []string: the file paths
  • error: the error

Example:

filePaths, err := ListFilePaths("./sprites")

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 SafeCreate

func SafeCreate(outputPath string) (*os.File, error)

func SaveImgAutoExt

func SaveImgAutoExt(pathNoneExt string, img image.Image, format ImageFormat, compressionLevel ...SetClv) error

func SaveImgByExt

func SaveImgByExt(outputPath 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 {
	Size
	PackedRects []Rect
	UsedArea    int
}

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

func NewBin

func NewBin(w, h int, req []Rect) Bin

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

func (Bin) FillRate

func (b Bin) FillRate() float64

func (Bin) String

func (b Bin) String() string

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

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

func (*Packer) PackSprites

func (p *Packer) PackSprites(input 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

Example:

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

type Point

type Point struct {
	X int `json:"x"`
	Y int `json:"y"`
}

func NewPoint

func NewPoint(x, y int) Point

type Rect

type Rect struct {
	Point
	Size
	Id        int  `json:"-"`
	IsRotated bool `json:"rotated,omitempty"`
}

Rect represents an immutable rectangle using value semantics.

func NewRect

func NewRect(x, y, w, h, id int) Rect

NewRect creates a new Rect value.

func NewRectByPosAndSize

func NewRectByPosAndSize(x, y, w, h int) Rect

func NewRectBySize

func NewRectBySize(w, h int) Rect

func NewRectBySizeAndId

func NewRectBySizeAndId(w, h, id int) Rect

func (Rect) Clone

func (r Rect) Clone() Rect

func (Rect) CloneWithPos

func (r Rect) CloneWithPos(x, y int) Rect

func (Rect) CloneWithSize

func (r Rect) CloneWithSize(w, h int) Rect

func (Rect) Rotated

func (r Rect) Rotated() Rect

func (Rect) String

func (r Rect) String() string

func (Rect) ToImageRect

func (r Rect) ToImageRect() image.Rectangle

type SameDetectInfo

type SameDetectInfo struct {
	DupeToBaseName  map[string]string
	BaseToDupesName map[string][]string
}

func FindDuplicateFiles

func FindDuplicateFiles(filePaths []string) ([]string, SameDetectInfo, error)

FindDuplicateFiles finds duplicate files in the given file paths.

Parameters:

  • filePaths: the paths of the files to be checked

Returns:

  • []string: the paths of the unique files
  • map[string]string: the map of duplicate file names and their corresponding base names
  • map[string][]string: the map of base file names to their duplicate file names
  • error

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"`
}

Size represents a size with width and height. The width and height must be positive.

func NewSize

func NewSize(w, h int) Size

func (Size) Area

func (s Size) Area() int

func (Size) Clone

func (s Size) Clone() Size

func (Size) PowerOfTwo

func (s Size) PowerOfTwo() Size

func (Size) Rotated

func (s Size) Rotated() Size

type Sprite

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

func (Sprite) Clone

func (s Sprite) Clone() Sprite

type SpriteAtlas

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

type UnpackOpts

type UnpackOpts func(*unpackedOpts)

func WithImg

func WithImg(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