dithering

package module
v0.0.0-...-30cad01 Latest Latest
Warning

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

Go to latest
Published: May 5, 2019 License: MIT Imports: 4 Imported by: 1

README

Dithering

GoDoc Go Report Card GitHub license

Image dithering in go

This go library provides a general purpose dithering algorithm implementation.

The color palette and the error diffusion matrix are customizable.

Install

In order to use this module run:

go get github.com/brouxco/dithering 

Note: this may not be necessary if you use Go 1.11 or later: see Go Modules

In your code don't forget the import:

import "github.com/brouxco/dithering"
License

MIT

Documentation

Overview

Package dithering provides a customizable image ditherer

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// FloydSteinberg is the Floyd Steinberg matrix
	FloydSteinberg = [][]float32{{0, 0, 7.0 / 16.0}, {3.0 / 16.0, 5.0 / 16.0, 1.0 / 16.0}}
	// JarvisJudiceNinke is the JarvisJudiceNinke matrix
	JarvisJudiceNinke = [][]float32{{0, 0, 0, 7.0 / 48.0, 5.0 / 48.0}, {3.0 / 48.0, 5.0 / 48.0, 7.0 / 48.0, 5.0 / 48.0, 3.0 / 48.0}, {1.0 / 48.0, 3.0 / 48.0, 5.0 / 48.0, 3.0 / 48.0, 1.0 / 48.0}}
	// Stucki is the Stucki matrix
	Stucki = [][]float32{{0, 0, 0, 8.0 / 42.0, 4.0 / 42.0}, {2.0 / 42.0, 4.0 / 42.0, 8.0 / 42.0, 4.0 / 42.0, 2.0 / 42.0}, {1.0 / 42.0, 2.0 / 42.0, 4.0 / 42.0, 2.0 / 42.0, 1.0 / 42.0}}
	// Atkinson is the Atkinson matrix
	Atkinson = [][]float32{{0, 0, 1.0 / 8.0, 1.0 / 8.0}, {1.0 / 8.0, 1.0 / 8.0, 1.0 / 8.0, 0}, {0, 1.0 / 8.0, 0, 0}}
	// Burkes is the Burkes matrix
	Burkes = [][]float32{{0, 0, 0, 8.0 / 32.0, 4.0 / 32.0}, {2.0 / 32.0, 4.0 / 32.0, 8.0 / 32.0, 4.0 / 32.0, 2.0 / 32.0}}
	// Sierra is the Sierra matrix
	Sierra = [][]float32{{0, 0, 0, 5.0 / 32.0, 3.0 / 32.0}, {2.0 / 32.0, 4.0 / 32.0, 5.0 / 32.0, 4.0 / 32.0, 2.0 / 32.0}, {0, 2.0 / 32.0, 3.0 / 32.0, 2.0 / 32.0, 0}}
	// TwoRowSierra is a variant of the Sierrra matrix
	TwoRowSierra = [][]float32{{0, 0, 0, 4.0 / 16.0, 3.0 / 16.0}, {1.0 / 32.0, 2.0 / 32.0, 3.0 / 32.0, 2.0 / 32.0, 1.0 / 32.0}}
	// SierraLite is a variant of the Sierra matrix
	SierraLite = [][]float32{{0, 0, 2.0 / 4.0}, {1.0 / 4.0, 1.0 / 4.0, 0}}
)

Functions

This section is empty.

Types

type Dither

type Dither struct {
	// Matrix is the error diffusion matrix
	Matrix [][]float32
	// contains filtered or unexported fields
}

Dither represent dithering algorithm implementation

func NewDither

func NewDither(matrix [][]float32) Dither

NewDither prepares a dithering algorithm

func NewDitherAnimation

func NewDitherAnimation(matrix [][]float32, nbFrames int) Dither

NewDitherAnimation prepares a dithering algorithm and animation

you can retrieve every generated frames thanks to RetrieveFrame Note: frames are shared using an unbuffered channel

func (Dither) Draw

func (dit Dither) Draw(dst draw.Image, rect image.Rectangle, src image.Image, sp image.Point)

Draw applies an error diffusion algorithm to the src image

Example
reader, err := os.Open("lenna.png")
if err != nil {
	log.Fatal(err)
}
defer reader.Close()

src, _, err := image.Decode(reader)
if err != nil {
	log.Fatal(err)
}

dst := image.NewPaletted(src.Bounds(), color.Palette{color.Black, color.White})

floydSteinberg := NewDither(FloydSteinberg)
floydSteinberg.Draw(dst, dst.Bounds(), src, image.ZP)

file, err := os.Create("result.png")
if err != nil {
	log.Fatal(err)
}
defer file.Close()

if err = png.Encode(file, dst); err != nil {
	log.Fatal(err)
}
Output:

func (Dither) RetrieveFrame

func (dit Dither) RetrieveFrame() draw.Image

RetrieveFrame returns the next available frame

type ErrorImage

type ErrorImage struct {
	// Pix holds the image's pixels, in R, G, B, A order. The pixel at
	// (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*4].
	Pix []float32
	// Stride is the Pix stride between vertically adjacent pixels.
	Stride int
	// Rect is the image's bounds.
	Rect image.Rectangle
	// Min & Max values in the image
	Min, Max PixelError
}

ErrorImage is an in-memory image whose At method returns dithering.PixelError values

func NewErrorImage

func NewErrorImage(r image.Rectangle) *ErrorImage

NewErrorImage returns a new ErrorImage image with the given width and height

func (*ErrorImage) At

func (p *ErrorImage) At(x, y int) color.Color

At returns the color of the pixel at (x, y)

func (*ErrorImage) Bounds

func (p *ErrorImage) Bounds() image.Rectangle

Bounds returns the domain for which At can return non-zero color

func (*ErrorImage) ColorModel

func (p *ErrorImage) ColorModel() color.Model

ColorModel returns the ErrorImage color model

func (*ErrorImage) PixOffset

func (p *ErrorImage) PixOffset(x, y int) int

PixOffset returns the index of the first element of Pix that corresponds to the pixel at (x, y).

func (*ErrorImage) PixelErrorAt

func (p *ErrorImage) PixelErrorAt(x, y int) PixelError

PixelErrorAt returns the pixel error at (x, y)

func (*ErrorImage) Set

func (p *ErrorImage) Set(x, y int, c color.Color)

Set sets the error of the pixel at (x, y)

func (*ErrorImage) SetPixelError

func (p *ErrorImage) SetPixelError(x, y int, c PixelError)

SetPixelError sets the error of the pixel at (x, y)

type PixelError

type PixelError struct {
	// TODO(brouxco): the alpha value does not make a lot of sense in a PixelError
	R, G, B, A float32
}

PixelError represents the error for each canal in the image when dithering an image Errors are floats because they are the result of a division

func (PixelError) Add

func (c PixelError) Add(c2 PixelError) PixelError

Add adds two PixelError

func (PixelError) Mul

func (c PixelError) Mul(v float32) PixelError

Mul multiplies two PixelError

func (PixelError) RGBA

func (c PixelError) RGBA() (r, g, b, a uint32)

RGBA returns the errors for each canal in the image

Jump to

Keyboard shortcuts

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