Skip to content

Commit

Permalink
Ordered dither adjust positive and negative
Browse files Browse the repository at this point in the history
  • Loading branch information
Nycto committed Feb 3, 2024
1 parent e4fc33e commit 77a8bb9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
6 changes: 4 additions & 2 deletions src/dither/ordered.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ proc orderedDither*[P](
## * https://bisqwit.iki.fi/story/howto/dither/jy/

let maxThresholdMap = thresholds.maxThreshold
let halfMax = (maxThresholdMap div 2) + 1

# The likely distance between colors in the palette
let threshold = palette.approxMaxColorDistance

for y in 0..<input.height:
for x in 0..<input.width:
let factor = thresholds.threshold(x, y)
let factor = thresholds.threshold(x, y) - halfMax
let actualPixel = input.getPixel(x, y)
let adjustdPixel = actualPixel + (factor * threshold div maxThresholdMap)
let delta = factor * threshold div maxThresholdMap
let adjustdPixel = actualPixel + delta
let final = palette.nearestColor(adjustdPixel)
output.setPixel(x, y, final)
40 changes: 20 additions & 20 deletions tests/t_ordered.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ suite "Ordered dithering":
img.orderedDither(output, BlackAndWhiteIntPalette, Bayer2x2)

check(output == [
[ 0, 0, 0, 255, 0, 255, 255, 255, 255, 255],
[ 0, 0, 255, 0, 255, 255, 255, 255, 255, 255],
[ 0, 0, 0, 255, 0, 255, 255, 255, 255, 255],
[ 0, 0, 255, 0, 255, 255, 255, 255, 255, 255]
[ 0, 0, 0, 0, 0, 0, 0, 255, 255, 255],
[ 0, 0, 0, 0, 0, 0, 255, 255, 255, 255],
[ 0, 0, 0, 0, 0, 0, 0, 255, 255, 255],
[ 0, 0, 0, 0, 0, 0, 255, 255, 255, 255]
])

test "Can perform 4x4 dithering":
Expand All @@ -36,12 +36,12 @@ suite "Ordered dithering":
img.orderedDither(output, BlackAndWhiteIntPalette, Bayer4x4)

check(output == [
[ 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[ 0, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[ 0, 0, 0, 0, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[ 0, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[ 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[ 0, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255],
[ 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255],
[ 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255],
[ 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
])


Expand All @@ -55,14 +55,14 @@ suite "Ordered dithering":
img.orderedDither(output, BlackAndWhiteIntPalette, Bayer8x8)

check(output == [
[ 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 255, 255],
[ 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255],
[ 0, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255],
[ 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 255, 255],
[ 0, 0, 255, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255],
[ 0, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255],
[ 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 255, 255],
[ 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 0],
])

0 comments on commit 77a8bb9

Please sign in to comment.