Skip to content

Commit

Permalink
Error diffusion strength
Browse files Browse the repository at this point in the history
  • Loading branch information
makew0rld committed Feb 13, 2021
1 parent 9601f1d commit 02b2882
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Added `ErrorDiffusionStrength` to set the strength of error diffusion dithering (#4)


## [1.0.0] - 2021-02-11
Initial release.
6 changes: 6 additions & 0 deletions dither_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ func TestSerpentine(t *testing.T) {
ditherAndCompareImage(gradient, "edm_floyd-steinberg_serpentine.png", d, t)
}

func TestErrorDiffusionStrength(t *testing.T) {
d := NewDitherer(blackWhite)
d.Matrix = ErrorDiffusionStrength(FloydSteinberg, 0.5)
ditherAndCompareImage(gradient, "edm_floyd-steinberg_strength_02.png", d, t)
}

func TestErrorDiffusionColor(t *testing.T) {
d := NewDitherer(redGreenBlack)

Expand Down
26 changes: 26 additions & 0 deletions error_diffusers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,32 @@ func (e ErrorDiffusionMatrix) Offset(x, y, curPx int) (int, int) {
return x - curPx, y
}

// ErrorDiffusionStrength modifies an existing error diffusion matrix so that it will
// be applied with the specified strength.
//
// strength is usually a value from 0 to 1.0, where 1.0 means 100% strength, and will
// not modify the matrix at all. It is inversely proportional to contrast - reducing the
// strength increases the contrast. It can be useful at values like 0.8 for reducing
// noise in the dithered image.
//
// See the documentation for Bayer for more details.
func ErrorDiffusionStrength(edm ErrorDiffusionMatrix, strength float32) ErrorDiffusionMatrix {
if strength == 1 {
return edm
}

dy := len(edm)
dx := len(edm[0])
edm2 := make(ErrorDiffusionMatrix, dy)
for y := 0; y < dy; y++ {
edm2[y] = make([]float32, dx)
for x := 0; x < dx; x++ {
edm2[y][x] = edm[y][x] * strength
}
}
return edm2
}

var Simple2D = ErrorDiffusionMatrix{
{0, 0.5},
{0.5, 0},
Expand Down
Binary file added images/output/edm_floyd-steinberg_strength_02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion pixelmappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func convThresholdToAddition(scale float32, value uint, max uint) float32 {
// lighter ones just get quantized.
//
// You might also want to reduce the strength to reduce noise in the image, as dithering
// doesn't produce smooth colored areas.
// doesn't produce smooth colored areas. Usually a value around 0.8 is good for this.
//
// You can also make strength negative. If you know already that your image is dark, and so
// you don't want it to be made bright, then this is a better approach then shrinking the
Expand Down

0 comments on commit 02b2882

Please sign in to comment.