blurhash

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2026 License: MIT Imports: 8 Imported by: 15

README

go-blurhash Go Reference GitHub tag license Go Report Card codecov

A pure Go implementation of Blurhash. The API is stable, however the hashing function in either direction may not be.

Blurhash Demo

Blurhash is an algorithm written by Dag Ågren for Wolt (woltapp/blurhash) that encodes an image into a short (~20-30 byte) ASCII string. When you decode the string back into an image, you get a gradient of colors that represent the original image. This can be useful for scenarios where you want an image placeholder before loading, or even to censor the contents of an image a la Mastodon.

Notable features

  • Pure Go with no dependencies
  • High performance (as of v1.2)
  • Reusable Encoder/Decoder APIs for zero-allocation batch processing

Contributing

Issues, feature requests or improvements welcome!

Licence

This project is licensed under the MIT License.

Documentation

Overview

Package blurhash provides encoding and decoding of Blurhash image placeholders.

Blurhash is an algorithm that encodes an image into a short ASCII string representing a gradient of colors. When decoded, this string produces a blurred placeholder that approximates the original image's colors and structure.

For simple one-off operations, use the package-level Encode and Decode functions. For batch processing with reduced allocations, use the reusable Encoder and Decoder types.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidComponents is returned when components passed to Encode are invalid.
	ErrInvalidComponents = errors.New("blurhash: must have between 1 and 9 components")
	// ErrInvalidHash is returned when the library encounters a hash it can't recognise.
	ErrInvalidHash = errors.New("blurhash: invalid hash")
	// ErrInvalidDimensions is returned when width or height is invalid.
	ErrInvalidDimensions = errors.New("blurhash: width and height must be positive")
)

Functions

func Components

func Components(hash string) (x, y int, err error)

Components returns the X and Y components of a blurhash.

func Decode

func Decode(hash string, width, height int, punch int) (image.Image, error)

Decode returns an NRGBA image of the given hash with the given size.

func DecodeDraw

func DecodeDraw(dst draw.Image, hash string, punch float64) error

DecodeDraw decodes the given hash into the given image.

func Encode

func Encode(xComponents, yComponents int, img image.Image) (string, error)

Encode returns the blurhash for the given image.

Types

type Decoder added in v1.2.0

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder is a reusable blurhash decoder that minimizes allocations by reusing internal buffers across decode operations.

A Decoder is safe for sequential use but not for concurrent use. For concurrent workloads, use a sync.Pool of Decoders.

The zero value is ready to use.

func NewDecoder added in v1.2.0

func NewDecoder() *Decoder

NewDecoder creates a new reusable Decoder. Buffers are allocated lazily on first use and grown as needed.

func (*Decoder) Decode added in v1.2.0

func (d *Decoder) Decode(hash string, width, height, punch int) (image.Image, error)

Decode decodes a blurhash to a new NRGBA image. Internal buffers are reused across calls when possible.

func (*Decoder) DecodeDraw added in v1.2.0

func (d *Decoder) DecodeDraw(dst draw.Image, hash string, punch float64) error

DecodeDraw decodes a blurhash into an existing image. Internal buffers are reused across calls when possible.

type Encoder added in v1.2.0

type Encoder struct {
	// contains filtered or unexported fields
}

Encoder is a reusable blurhash encoder that minimizes allocations by reusing internal buffers across encode operations.

An Encoder is safe for sequential use but not for concurrent use. For concurrent workloads, use a sync.Pool of Encoders.

The zero value is ready to use.

func NewEncoder added in v1.2.0

func NewEncoder() *Encoder

NewEncoder creates a new reusable Encoder. Buffers are allocated lazily on first use and grown as needed.

func (*Encoder) Encode added in v1.2.0

func (e *Encoder) Encode(xComponents, yComponents int, img image.Image) (string, error)

Encode returns the blurhash for the given image. Internal buffers are reused across calls when possible.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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