mosaic

package
v0.0.0-...-5f3dda6 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: MIT Imports: 9 Imported by: 0

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

View Source
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.

View Source
const JPEGQuality = 90

JPEGQuality is the quality setting used when encoding output mosaics.

Variables

View Source
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")
)
View Source
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

func CellSize(bounds image.Rectangle, gridSize int) (w, h int)

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

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

Decode reads a JPEG or PNG image from r. Data in any other format is reported as ErrUnsupportedFormat.

func EncodeJPEG

func EncodeJPEG(w io.Writer, img image.Image) error

EncodeJPEG writes img to w as a JPEG at JPEGQuality.

func Generate

func Generate(input image.Image, tiles []image.Image, gridSize int) (image.Image, error)

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

func SplitGrid(img image.Image, gridSize int) []image.Image

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

func SquaredDistance(a, b RGB) float64

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.

Types

type RGB

type RGB struct {
	R, G, B float64
}

RGB is an average color with channel values on the conventional 0-255 scale.

func AverageRGB

func AverageRGB(img image.Image) RGB

AverageRGB returns the mean color of every pixel in img. A zero-area image returns the zero RGB.

Jump to

Keyboard shortcuts

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