Пример #1
0
// NewFromImage uses the given image as the destination for all calls to Add.
// It is assumed to be empty at the beginning so all the available space will be
// used for sub-images.
func NewFromImage(atlas draw.Image) *Atlas {
	packer := binpacker.New(atlas.Bounds().Dx(), atlas.Bounds().Dy())
	return &Atlas{
		Image:  atlas,
		packer: packer,
	}
}
Пример #2
0
func pack(paths []string) error {
	packer := binpacker.New(*binSize, *binSize)
	bin := image.NewRGBA(image.Rect(0, 0, *binSize, *binSize))
	boundsTable := make(map[string]binpacker.Rect)

	for _, path := range paths {
		file, err := os.Open(path)
		if err != nil {
			return err
		}
		defer file.Close()

		img, _, err := image.Decode(file)
		if err != nil {
			return err
		}

		rect, err := packer.Insert(img.Bounds().Dx(), img.Bounds().Dy())
		if err != nil {
			return err
		}

		draw.Draw(bin, bounds(rect), img, img.Bounds().Min, draw.Src)
		boundsTable[id(path)] = rect
	}

	if err := saveTable(boundsTable, *tableFile); err != nil {
		return err
	}

	return saveImage(bin, *outputImageFile)
}
Пример #3
0
// New creates a new square image atlas of the given size. The bounds will have
// (0,0) as the minimum and the image is of type NRGBA.
func New(size int) *Atlas {
	packer := binpacker.New(size, size)
	return &Atlas{
		Image:  image.NewNRGBA(image.Rect(0, 0, size, size)),
		packer: packer,
	}
}