Documentation
¶
Overview ¶
Package pixelize resizes images and quantizes their colors to a palette.
Two layers:
- Palette[M] models an ordered set of color entries with arbitrary typed metadata M (name, ID, hex, whatever a domain needs).
- Apply takes an image and returns a Pattern[M]: the quantized image plus the palette index per pixel and a usage histogram.
Distance defaults to stdlib unweighted Euclidean (image/color.Palette.Index). Callers can plug in a custom DistanceFunc.
Index ¶
- Variables
- func EncodeAnimatedGIF(w io.Writer, frames []*image.RGBA, delaysCS []int, loopCount int) error
- func EncodeGIF(w io.Writer, frames []*image.RGBA, opts GIFOptions) error
- func EuclideanRGBA(a, b color.Color) float64
- func RunBatch(ctx context.Context, jobs []BatchJob, opts BatchOptions, ...) error
- func Watch(ctx context.Context, path string, onChange func() error) error
- func WriteBuildMap[M any](w io.Writer, p *Pattern[M]) error
- func WritePiecesCSV[M any](w io.Writer, p *Pattern[M]) error
- type ApplyOptions
- type BatchJob
- type BatchOptions
- type Bucket
- type DistanceFunc
- type Entry
- type EntryMeta
- type GIFOptions
- type Hexed
- type Identified
- type LoopMode
- type Named
- type Palette
- type Pattern
- type ResizeMode
- type StatsJSON
- type SwatchOptions
Constants ¶
This section is empty.
Variables ¶
var Version = "dev"
Version is set at build time via -ldflags.
Functions ¶
func EncodeAnimatedGIF ¶
EncodeAnimatedGIF writes frames as an animated GIF preserving per- frame delays and a loop count. Use this when source frames carry their own timing (e.g. converted from another animated GIF). DelaysCS values are centiseconds, matching gif.GIF.Delay.
func EncodeGIF ¶
EncodeGIF writes the frames as an animated GIF. Each frame is re-paletted against its own colors via image.NewPaletted so the caller does not need to pre-quantize. Output size matches the first frame's bounds; subsequent frames are clipped to that rectangle.
func EuclideanRGBA ¶
EuclideanRGBA is the stdlib-equivalent unweighted Euclidean distance in 16-bit RGBA space.
func RunBatch ¶
func RunBatch(ctx context.Context, jobs []BatchJob, opts BatchOptions, process func(context.Context, BatchJob) error) error
RunBatch executes process(job) for every job concurrently, with at most opts.Workers goroutines. Returns the first error if any worker fails; the rest are canceled.
func Watch ¶
Watch invokes onChange every time `path` is modified, until ctx is canceled. Coalesces bursts of events with a small debounce.
Watches the parent directory and filters for the basename so editors that swap files atomically (write-then-rename) still trigger.
func WriteBuildMap ¶
WriteBuildMap writes a per-pixel build map suitable for assembling physical mosaics. One line per pixel:
[x][y] = R:r, G:g, B:b -name
Works with any Pattern whose metadata implements Named (else the name field is left blank).
Types ¶
type ApplyOptions ¶
type ApplyOptions struct {
// Width and Height set the target dimensions. Zero means no resize.
// If only one is zero, it is derived from the other to preserve the
// aspect ratio.
Width, Height int
// Resize selects the resize algorithm. Default NearestNeighbor.
// Ignored when Width and Height are both zero.
Resize ResizeMode
// Dither enables Floyd-Steinberg dithering when quantizing.
Dither bool
// Distance overrides the nearest-color metric. nil means stdlib
// unweighted Euclidean (color.Palette.Index).
Distance DistanceFunc
}
ApplyOptions configures palette application.
type BatchJob ¶
BatchJob is one item in a batch run.
func CollectJobs ¶
func CollectJobs(inDir, outDir, outExt string, opts BatchOptions) ([]BatchJob, error)
CollectJobs walks inDir and returns jobs that pair each accepted input file with an output path derived as outDir/<basename>.<outExt>.
type BatchOptions ¶
type BatchOptions struct {
// Workers is the number of concurrent jobs. Default runtime.NumCPU.
// Zero or negative uses the default.
Workers int
// Extensions filters input files by extension (lowercase, with dot).
// Default: .png, .jpg, .jpeg, .gif, .webp, .jxl.
Extensions []string
}
BatchOptions controls Batch concurrency and file selection.
type Bucket ¶
type Bucket[M any] struct { Index int `json:"index"` Entry Entry[M] `json:"entry"` Count int `json:"count"` }
Bucket is one entry of a histogram.
type DistanceFunc ¶
DistanceFunc returns a distance metric between two colors. Smaller means more similar. Absolute scale does not matter, only relative ordering.
The default implementation is stdlib unweighted Euclidean (via color.Palette.Index). Callers that need perceptual accuracy can plug in CIEDE2000 over CIE Lab.
type Entry ¶
Entry is one palette color plus arbitrary typed metadata.
M is the metadata type chosen by the consumer: a string name, a rich struct (e.g. LegoColor with brick id), or struct{} when none is needed.
type EntryMeta ¶
type EntryMeta struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Hex string `json:"hex,omitempty"`
More map[string]string `json:"more,omitempty"`
}
EntryMeta is the metadata struct used by the CLI loaders. It is generic-friendly: any field can be empty.
type GIFOptions ¶
type GIFOptions struct {
// DelayMS is the per-frame delay in milliseconds. GIF resolution
// is 10ms, so values are floored to the nearest centisecond.
// Default 200ms.
DelayMS int
// Loop controls frame ordering.
Loop LoopMode
// LoopCount sets the GIF loop count (0 = infinite).
LoopCount int
}
GIFOptions configures EncodeGIF.
type Identified ¶
type Identified interface {
GetID() string
}
Identified is implemented by metadata types that carry an ID (legoid, brick-link sku, dmc thread number, ...).
type Named ¶
type Named interface {
GetName() string
}
Named is implemented by metadata types that carry a human-readable name. EntryMeta satisfies this.
type Palette ¶
Palette is an ordered set of entries. Indices into the slice are the canonical reference for results: Pattern.Indices stores them.
func LoadCSV ¶
LoadCSV reads a header-driven CSV palette. Recognized columns: hex, name, id, r, g, b. Order is free. Either "hex" or all of "r","g","b" must be present.
No comment syntax: "#" is reserved for hex literals like "#FF0000". Use HEX format if you need inline comments.
func LoadFile ¶
LoadFile dispatches to the right loader based on file extension. Supports .csv, .hex, .gpl, .json.
func LoadGPL ¶
LoadGPL reads a GIMP palette. Format:
GIMP Palette Name: ... Columns: N # R G B Name ...
func LoadHEX ¶
LoadHEX reads one #rrggbb (or rrggbb) per line. Comments start with # in column zero or after whitespace; a hex line may not start with # the same way, but we tell them apart by length: 6 or 7 chars => color.
func (Palette[M]) Apply ¶
func (p Palette[M]) Apply(ctx context.Context, img image.Image, opts ApplyOptions) (*Pattern[M], error)
Apply resizes the image (if requested) and quantizes it against p. Returns a Pattern indexed against p.
func (Palette[M]) ColorPalette ¶
ColorPalette converts to stdlib color.Palette for use with stdlib drawing routines (color.Palette.Index, image.NewPaletted, etc.).
type Pattern ¶
type Pattern[M any] struct { // Image is the quantized output. Every pixel matches an entry in Palette. Image *image.RGBA // Palette is the palette used to produce Image. Held for downstream // helpers (Histogram, Dominant, build-map writers). Palette Palette[M] // Indices[x][y] is the palette index assigned to pixel (x, y). Indices [][]int // Usage maps palette index to pixel count. Usage map[int]int }
Pattern is the result of Apply.
func (*Pattern[M]) Histogram ¶
Histogram returns palette indices sorted by usage count (descending).
func (*Pattern[M]) UniqueColors ¶
UniqueColors returns the number of palette entries actually used.
type ResizeMode ¶
type ResizeMode int
ResizeMode selects the resize algorithm used by Apply.
const ( // NearestNeighbor preserves blocky edges. Default for pixel art. NearestNeighbor ResizeMode = iota // BlockAverage averages every NxN source pixel block into one // destination pixel. Useful as a smoothing pre-pass before // quantizing photographic input. Only works when source dimensions // are integer multiples of the destination dimensions; otherwise // falls back to NearestNeighbor for the remainder. BlockAverage // BiLinear is golang.org/x/image/draw.BiLinear. Smooth. BiLinear // CatmullRom is golang.org/x/image/draw.CatmullRom. Sharpest of the // smooth resamplers. CatmullRom )
type StatsJSON ¶
type StatsJSON[M any] struct { Width int `json:"width"` Height int `json:"height"` TotalPixels int `json:"total_pixels"` UniqueColors int `json:"unique_colors"` PaletteSize int `json:"palette_size"` Histogram []Bucket[M] `json:"histogram"` }
StatsJSON is the machine-readable stats summary.
type SwatchOptions ¶
type SwatchOptions struct {
// CellSize is the side length of one color square in pixels.
// Default 32.
CellSize int
// Columns sets how many cells per row. Default 8.
// If 0 or larger than len(palette), all entries fit on one row.
Columns int
}
SwatchOptions configures palette swatch rendering.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
pixelize
command
Command pixelize resizes images and quantizes their colors to a palette.
|
Command pixelize resizes images and quantizes their colors to a palette. |
|
Package decode wraps image.Decode and registers extra formats (WebP, JXL) so the caller can hand off any supported file.
|
Package decode wraps image.Decode and registers extra formats (WebP, JXL) so the caller can hand off any supported file. |
|
Package palettes ships example palettes and resolves palette names against the user's config dir, then the embedded examples.
|
Package palettes ships example palettes and resolves palette names against the user's config dir, then the embedded examples. |
|
Package preview renders an image to the terminal using whichever graphics protocol the terminal supports.
|
Package preview renders an image to the terminal using whichever graphics protocol the terminal supports. |