Documentation
¶
Overview ¶
Package mosaic implements photo-mosaic generation: splitting a source image into a grid, matching each cell to the nearest-colored tile image, and compositing the matched tiles into a single output image.
Index ¶
- Constants
- Variables
- func CellSize(bounds image.Rectangle, gridSize int) (w, h int)
- func Decode(r io.Reader) (image.Image, error)
- func EncodeJPEG(w io.Writer, img image.Image) error
- func Generate(input image.Image, tiles []image.Image, gridSize int) (image.Image, error)
- func SplitGrid(img image.Image, gridSize int) []image.Image
- func SquaredDistance(a, b RGB) float64
- type RGB
Constants ¶
const ( MinGridSize = 2 MaxGridSize = 300 DefaultGridSize = 50 )
Grid size bounds. The lower bound keeps the result recognizably a mosaic; the upper bound keeps memory and matching work sane.
const JPEGQuality = 90
JPEGQuality is the quality setting used when encoding output mosaics.
Variables ¶
var ( // ErrNoTiles means no usable tile images were supplied. ErrNoTiles = errors.New("no tile images provided") // ErrGridSizeOutOfRange means gridSize fell outside [MinGridSize, MaxGridSize]. ErrGridSizeOutOfRange = fmt.Errorf("grid size must be between %d and %d", MinGridSize, MaxGridSize) // ErrImageTooSmall means the input image cannot be divided into the // requested grid without producing zero-sized cells. ErrImageTooSmall = errors.New("input image is too small for the requested grid size") )
var ErrUnsupportedFormat = errors.New("unsupported image format: only JPEG and PNG are supported")
ErrUnsupportedFormat means the data was not JPEG or PNG.
Functions ¶
func CellSize ¶
CellSize returns the pixel dimensions of a single grid cell for the given image bounds and grid size. Integer division means a source whose dimensions are not divisible by gridSize leaves a few edge pixels unused, which keeps every cell exactly the same size.
func Decode ¶
Decode reads a JPEG or PNG image from r. Data in any other format is reported as ErrUnsupportedFormat.
func EncodeJPEG ¶
EncodeJPEG writes img to w as a JPEG at JPEGQuality.
func Generate ¶
Generate builds a photo mosaic of input out of the supplied tile images.
The input is divided into a gridSize x gridSize grid. Each cell is matched to whichever tile has the closest average color, and that tile — stretched to the cell's dimensions — is drawn into the output. Tiles may be reused freely.
The output is cellWidth*gridSize x cellHeight*gridSize, which can be slightly smaller than the input when its dimensions are not divisible by gridSize.
func SplitGrid ¶
SplitGrid divides img into a gridSize x gridSize grid of equally sized cells, returned in row-major order: index i is row i/gridSize, column i%gridSize.
It returns nil if gridSize is not positive, or if the grid is so fine that cells would have zero width or height.
func SquaredDistance ¶
SquaredDistance returns the squared Euclidean distance between two colors in RGB space. Squared distance is enough for nearest-color comparisons and avoids a square root per candidate.