stackblur

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2025 License: MIT Imports: 3 Imported by: 24

README

stackblur-go

Go Reference build

Go port of Mario Klingemann's Stackblur algorithm.

Stackblur is a compromise between Gaussian blur and Box blur, but it creates much better looking blurs than Box blur and it is ~7x faster than Gaussian blur.

Comparing to the Javascript implementation the Go version is at least 50% faster (depending on the image size and blur radius), applied on the same image with the same bluring radius.

Benchmark
Radius Javascript Go
20 ~15ms ~7.4ms

Installation

$ go install github.com/esimov/stackblur-go/cmd/stackblur@latest
CLI example

The provided CLI example supports the following flags:

$ stackblur --help

Usage of stackblur:
  -gif
    	Output Gif
  -in string
    	Source
  -out string
    	Destination
  -radius int
    	Radius (default 20)

The command below will generate the blurred version of the source image.

$ stackblur -in image/sample.png -out image/output.png -radius 10

The cli command supports a -gif flag, which if set as true it visualize the bluring process by outputting the result into a gif file.

API

The usage of the API is very simple: it exposes a single public Process function which requires a destination and a source image together with a blur radius. The blured image will be encoded into the destination image.

stackblur.Process(dst, src, blurRadius)

Results

Original image Blurred image

License

This project is under the MIT License. See the LICENSE file for the full license text.

Documentation

Overview

stackblur-go is a Go port of the Stackblur algorithm.

Stackblur is a compromise between Gaussian blur and Box blur, but it creates much better looking blurs than Box blur and it is ~7x faster than Gaussian blur.

The usage of the API is very simple: it exposes a single public `Process` function which requires a destination and a source image together with a blur radius. The blured image will be encoded into the destination image.

func Process(dst, src image.Image, radius uint32) error

Below is a very simple example of how you can use this package.

package main

import (
	"image"
	"image/jpeg"
	"log"
	"os"

	"github.com/esimov/stackblur-go"
)

func main() {
	var radius uint32 = 5
	f, err := os.Open("sample.png")
	if err != nil {
		log.Fatalf("could not open source file: %v", err)
	}
	defer f.Close()

	src, _, err := image.Decode(f)
	if err != nil {
		log.Fatalf("could not decode source file: %v", err)
	}

	dst := image.NewNRGBA(src.Bounds())
	err = stackblur.Process(dst, src, radius)
	if err != nil {
		log.Fatal(err)
	}

	output, err := os.OpenFile("output.jpg", os.O_CREATE|os.O_RDWR, 0755)
	if err != nil {
		log.Fatalf("could not open destination file: %v", err)
	}
	defer output.Close()

	if err = jpeg.Encode(output, dst, &jpeg.Options{Quality: 100}); err != nil {
		log.Fatalf("could not encode destination image: %v", err)
	}
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Process

func Process(dst, src image.Image, radius uint32) error

Process takes the source image and returns it's blurred version by applying the blur radius defined as parameter.

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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