blurhash

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2020 License: GPL-3.0 Imports: 6 Imported by: 14

README

go-blurhash Build Status Go Report Card codecov GoDoc

go-blurhash is a pure Go implementation of the BlurHash algorithm, which is used by Mastodon an other Fediverse software to implement a swift way of preloading placeholder images as well as hiding sensitive media. Read more about it here.

tl;dr: BlurHash is a compact representation of a placeholder for an image.

This library allows generating the BlurHash of a given image, as well as reconstructing a blurred version with specified dimensions from a given BlurHash.

This library is based on the following reference implementations:

BlurHash is written by Dag Ågren / Wolt.

Before After
Image alt text "LFE.@D9F01_2%L%MIVD*9Goe-;WB"
Hash "LFE.@D9F01_2%L%MIVD*9Goe-;WB" alt text

Installation

From source
go get -u github.com/buckket/go-blurhash

Usage

go-blurhash exports three functions:

func blurhash.Encode(xComponents, yComponents int, rgba image.Image) (string, error)
func blurhash.Decode(hash string, width, height, punch int) (image.Image, error)
func blurhash.Components(hash string) (xComponents, yComponents int, err error)

Here’s a simple demonstration. Check pkg.go.dev for the full documentation.

package main

import (
	"fmt"
	"github.com/buckket/go-blurhash"
	"image/png"
	"os"
)

func main() {
	// Generate the BlurHash for a given image
	imageFile, _ := os.Open("test.png")
	loadedImage, err := png.Decode(imageFile)
	str, _ := blurhash.Encode(4, 3, loadedImage)
	if err != nil {
		// Handle errors
	}
	fmt.Printf("Hash: %s\n", str)

	// Generate an image for a given BlurHash
	// Width will be 300px and Height will be 500px
	// Punch specifies the contrasts and defaults to 1
	img, err := blurhash.Decode(str, 300, 500, 1)
	if err != nil {
		// Handle errors
	}
	f, _ := os.Create("test_blur.png")
	_ = png.Encode(f, img)
	
	// Get the x and y components used for encoding a given BlurHash
	x, y, err := blurhash.Components("LFE.@D9F01_2%L%MIVD*9Goe-;WB")
	if err != nil {
		// Handle errors
	}
	fmt.Printf("xComponents: %d, yComponents: %d", x, y)
}

Limitations

  • Presumably a bit slower than the C implementation (TODO: Benchmarks)

Notes

  • As mentioned here, it’s best to generate very small images (~32x32px) via BlurHash and scale them up to the desired dimensions afterwards for optimal performance.
  • Since #2 we diverted from the reference implementation by memorizing sRGBtoLinear values, thus increasing encoding speed at the cost of higher memory usage.
  • Starting with v1.1.0 the signature of blurhash.Encode() has changed slightly (see #3).

License

GNU GPLv3+

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Components

func Components(hash string) (xComponents, yComponents int, err error)

Components decodes and returns the number of x and y components in the given BlurHash.

Example
x, y, err := blurhash.Components("LFE.@D9F01_2%L%MIVD*9Goe-;WB")
if err != nil {
	// Handle errors
}
fmt.Printf("xComponents: %d, yComponents: %d", x, y)
Output:

xComponents: 4, yComponents: 3

func Decode

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

Decode generates an image of the given BlurHash with a size of width and height. Punch is a multiplier that adjusts the contrast of the resulting image.

Example
img, err := blurhash.Decode("LFE.@D9F01_2%L%MIVD*9Goe-;WB", 204, 204, 1)
if err != nil {
	// Handling errors
}
f, _ := os.Create("test_blur.png")
_ = png.Encode(f, img)
Output:

func Encode

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

Encode calculates the Blurhash for an image using the given x and y component counts. The x and y components have to be between 1 and 9 respectively. The image must be of image.Image type.

Example
imageFile, _ := os.Open("test.png")
loadedImage, _ := png.Decode(imageFile)
str, err := blurhash.Encode(4, 3, loadedImage)
if err != nil {
	// Handle errors
}
fmt.Printf("Hash: %s", str)
Output:

Hash: LFE.@D9F01_2%L%MIVD*9Goe-;WB

Types

type EncodingError

type EncodingError string

An EncodingError represents an error that occurred during the encoding of the given value. This most likely means that your input image is invalid and can not be processed.

func (EncodingError) Error

func (e EncodingError) Error() string

type InvalidHashError

type InvalidHashError string

An InvalidHashError occurs when the given hash is either too short or the length does not match its size flag.

func (InvalidHashError) Error

func (e InvalidHashError) Error() string

type InvalidParameterError

type InvalidParameterError struct {
	Value     int
	Parameter string
}

An InvalidParameterError occurs when an invalid argument is passed to either the Decode or Encode function.

func (InvalidParameterError) Error

func (e InvalidParameterError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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