halfgone

package module
v0.0.0-...-482157b Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2017 License: MIT Imports: 7 Imported by: 14

README

halfgone

This repository contains implementations of digital halftoning - also called dithering - algorithms written in Go. The implementations are restricted to black and white rendering and are based on the image library from Go's standard library.

The implementations are quite fast but are not optimized for production where you would typically want to use bit shifting when possible. I moved the common code for error-diffusion dithering into a separate functions because it's always the same underlying algorithm, whether it be Floyd-Steinberg dithering or Stucki dithering. I did the same for ordered dithering. In production you would probably want to choose a particular dithering algorithm and avoid using generic code which makes it harder to write optimized code.

If you are interested in digital halftoning, this web page is, in my opinion, a fantastic introduction. I've also written a blog post which goes through some of the implementations.

Original image

img := LoadImage("images/penguin.jpg")

original

Grayscale

gray := ImageToGray(img)

grayscale

Inverted grayscale

InvertGray(gray)

reversed_grayscale

Threshold dithering

halfgone.ThresholdDitherer{Threshold: 127}.Apply(gray)

threshold_dithering

Random threshold dithering

halfgone.RandomThresholdDitherer{MaxThreshold: 255, RNG: rng}.Apply(gray)

random_threshold_dithering

Importance sampling

halfgone.ImportanceSampling{N: 4000, Threshold: 100, RNG: rng}.Apply(gray)

importance_sampling

Bosch and Herman’s grid-based dithering

halfgone.GridDitherer{K: 5, Alpha: 3, Beta: 8, RNG: rng}.Apply(gray)

grid_dithering

Ordered dithering

Order-2 ordered dithering
halfgone.Order2OrderedDitherer{}.Apply(gray)

order_2_ordered_dithering

Order-3 ordered dithering
halfgone.Order3OrderedDitherer{}.Apply(gray)

order_3_ordered_dithering

Order-4 ordered dithering
halfgone.Order4OrderedDitherer{}.Apply(gray)

order_4_ordered_dithering

Order-8 ordered dithering
halfgone.Order8OrderedDitherer{}.Apply(gray)

order_8_ordered_dithering

Error-diffusion dithering

Floyd-Steinberg dithering
halfgone.FloydSteinbergDitherer{}.apply(gray)

floyd_steinberg_dithering

Jarvis-Judice-Ninke dithering
halfgone.JarvisJudiceNinkeDitherer{}.Apply(gray)

jarvis_judice_ninke_dithering

Stucki dithering
halfgone.StuckiDitherer{}.Apply(gray)

stucki_dithering

Atkinson dithering
halfgone.AtkinsonDitherer{}.Apply(gray)

atkinson_dithering

Burkes dithering
halfgone.BurkesDitherer{}.Apply(gray)

burkes_dithering

Sierra dithering
halfgone.SierraDitherer{}.Apply(gray)

seria_dithering

Two-row Sierra dithering
halfgone.TwoRowSierraDitherer{}.Apply(gray)

two_row_seria_dithering

Sierra Lite dithering
halfgone.SierraLiteDitherer{}.Apply(gray)

seria_lite_dithering

License

The MIT License (MIT). Please see the license file for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ImageToGray

func ImageToGray(img image.Image) *image.Gray

ImageToGray converts an image.Image into an image.Gray.

func InvertGray

func InvertGray(gray *image.Gray) *image.Gray

InvertGray inverses the scale of a grayscale image by substracting each intensity to 255. Thus 0 (black) becomes 255 (white) and vice versa.

func LoadImage

func LoadImage(path string) (image.Image, error)

LoadImage reads and loads an image from a file path.

func SaveImagePNG

func SaveImagePNG(img image.Image, path string) error

SaveImagePNG save an image to a PNG file.

Types

type AtkinsonDitherer

type AtkinsonDitherer struct{}

AtkinsonDitherer implements Atkinson dithering.

func (AtkinsonDitherer) Apply

func (ad AtkinsonDitherer) Apply(gray *image.Gray) *image.Gray

Apply Atkinson dithering.

type BurkesDitherer

type BurkesDitherer struct{}

BurkesDitherer implements Burkes dithering.

func (BurkesDitherer) Apply

func (ad BurkesDitherer) Apply(gray *image.Gray) *image.Gray

Apply Burkes dithering.

type DiffusionCell

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

A DiffusionCell indicates a relative position and a diffusion intensity used for error diffusion.

type DiffusionMask

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

A DiffusionMask contains a slice of DiffusionCells and a normalization divisor.

type Ditherer

type Ditherer interface {
	Apply(gray *image.Gray) *image.Gray
}

A Ditherer convert a grayscale image with intensities going from 0 (black) to 255 (white) into a black and white image.

type FloydSteinbergDitherer

type FloydSteinbergDitherer struct{}

FloydSteinbergDitherer implements Floyd-Steingberg dithering.

func (FloydSteinbergDitherer) Apply

func (fsd FloydSteinbergDitherer) Apply(gray *image.Gray) *image.Gray

Apply Floyd-Steinberg dithering.

type GridDitherer

type GridDitherer struct {
	K     int     // Size in pixels of a side of a cell
	Alpha float64 // Minimum desired number of points in a cell
	Beta  float64 // Maximum desired number of points in a cell
	RNG   *rand.Rand
}

GridDitherer implements Bosch and Herman's grid-based method.

func (GridDitherer) Apply

func (gd GridDitherer) Apply(gray *image.Gray) *image.Gray

Apply random grid dithering.

type ImportanceSampling

type ImportanceSampling struct {
	N         int   // Number of points to sample
	Threshold uint8 // Threshold after which intensities are ignored (the threshold is not ignored)
	RNG       *rand.Rand
}

ImportanceSampling implements importance sampling.

func (ImportanceSampling) Apply

func (is ImportanceSampling) Apply(gray *image.Gray) *image.Gray

Apply importance sampling.

type JarvisJudiceNinkeDitherer

type JarvisJudiceNinkeDitherer struct{}

JarvisJudiceNinkeDitherer implements Jarvis-Judice-Ninke dithering.

func (JarvisJudiceNinkeDitherer) Apply

func (jjnd JarvisJudiceNinkeDitherer) Apply(gray *image.Gray) *image.Gray

Apply Jarvis-Judice-Ninke dithering.

type Order2OrderedDitherer

type Order2OrderedDitherer struct{}

Order2OrderedDitherer implements order-2 ordered dithering.

func (Order2OrderedDitherer) Apply

func (o2od Order2OrderedDitherer) Apply(gray *image.Gray) *image.Gray

Apply order-2 ordered dithering dithering.

type Order3OrderedDitherer

type Order3OrderedDitherer struct{}

Order3OrderedDitherer implements order-3 ordered dithering.

func (Order3OrderedDitherer) Apply

func (o3od Order3OrderedDitherer) Apply(gray *image.Gray) *image.Gray

Apply order-3 ordered dithering dithering.

type Order4OrderedDitherer

type Order4OrderedDitherer struct{}

Order4OrderedDitherer implements order-4 ordered dithering.

func (Order4OrderedDitherer) Apply

func (o4od Order4OrderedDitherer) Apply(gray *image.Gray) *image.Gray

Apply order-4 ordered dithering dithering.

type Order8OrderedDitherer

type Order8OrderedDitherer struct{}

Order8OrderedDitherer implements order-8 ordered dithering.

func (Order8OrderedDitherer) Apply

func (o8od Order8OrderedDitherer) Apply(gray *image.Gray) *image.Gray

Apply order-8 ordered dithering dithering.

type Pattern

type Pattern [][]uint8

A Pattern is a matrix of threshold values used for ordered dithering.

type RandomThresholdDitherer

type RandomThresholdDitherer struct {
	MaxThreshold int
	RNG          *rand.Rand
}

RandomThresholdDitherer works the same way as ThresholdDitherer except that the threshold is randomly sampled for each pixel. This way some pixels are white when they would have been actually black.

func (RandomThresholdDitherer) Apply

func (rtd RandomThresholdDitherer) Apply(gray *image.Gray) *image.Gray

Apply random threshold dithering.

type SierraDitherer

type SierraDitherer struct{}

SierraDitherer implements Sierra dithering.

func (SierraDitherer) Apply

func (sd SierraDitherer) Apply(gray *image.Gray) *image.Gray

Apply Sierra dithering.

type SierraLiteDitherer

type SierraLiteDitherer struct{}

SierraLiteDitherer implements Sierra Lite dithering.

func (SierraLiteDitherer) Apply

func (sd SierraLiteDitherer) Apply(gray *image.Gray) *image.Gray

Apply Sierra Lite dithering.

type StuckiDitherer

type StuckiDitherer struct{}

StuckiDitherer implements Stucki dithering.

func (StuckiDitherer) Apply

func (sd StuckiDitherer) Apply(gray *image.Gray) *image.Gray

Apply Stucki dithering.

type ThresholdDitherer

type ThresholdDitherer struct {
	Threshold uint8
}

ThresholdDitherer converts each pixel in a grayscale image to black or white depending on the intensity of the pixel. If a pixel's intensity is above the given threshold then the pixel becomes white, else it becomes black.

func (ThresholdDitherer) Apply

func (td ThresholdDitherer) Apply(gray *image.Gray) *image.Gray

Apply threshold dithering.

type TwoRowSierraDitherer

type TwoRowSierraDitherer struct{}

TwoRowSierraDitherer implements Two-row Sierra dithering.

func (TwoRowSierraDitherer) Apply

func (sd TwoRowSierraDitherer) Apply(gray *image.Gray) *image.Gray

Apply Two-row Sierra dithering.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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