Documentation ¶
Overview ¶
Package dithering provides a customizable image ditherer
Index ¶
- Variables
- type Dither
- type ErrorImage
- func (p *ErrorImage) At(x, y int) color.Color
- func (p *ErrorImage) Bounds() image.Rectangle
- func (p *ErrorImage) ColorModel() color.Model
- func (p *ErrorImage) PixOffset(x, y int) int
- func (p *ErrorImage) PixelErrorAt(x, y int) PixelError
- func (p *ErrorImage) Set(x, y int, c color.Color)
- func (p *ErrorImage) SetPixelError(x, y int, c PixelError)
- type PixelError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 NewDitherAnimation ¶
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 ¶
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 ¶
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) RGBA ¶
func (c PixelError) RGBA() (r, g, b, a uint32)
RGBA returns the errors for each canal in the image