Skip to content

Commit 77a8bb9

Browse files
committed
Ordered dither adjust positive and negative
1 parent e4fc33e commit 77a8bb9

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

src/dither/ordered.nim

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ proc orderedDither*[P](
1717
## * https://bisqwit.iki.fi/story/howto/dither/jy/
1818

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

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

2425
for y in 0..<input.height:
2526
for x in 0..<input.width:
26-
let factor = thresholds.threshold(x, y)
27+
let factor = thresholds.threshold(x, y) - halfMax
2728
let actualPixel = input.getPixel(x, y)
28-
let adjustdPixel = actualPixel + (factor * threshold div maxThresholdMap)
29+
let delta = factor * threshold div maxThresholdMap
30+
let adjustdPixel = actualPixel + delta
2931
let final = palette.nearestColor(adjustdPixel)
3032
output.setPixel(x, y, final)

tests/t_ordered.nim

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ suite "Ordered dithering":
1515
img.orderedDither(output, BlackAndWhiteIntPalette, Bayer2x2)
1616

1717
check(output == [
18-
[ 0, 0, 0, 255, 0, 255, 255, 255, 255, 255],
19-
[ 0, 0, 255, 0, 255, 255, 255, 255, 255, 255],
20-
[ 0, 0, 0, 255, 0, 255, 255, 255, 255, 255],
21-
[ 0, 0, 255, 0, 255, 255, 255, 255, 255, 255]
18+
[ 0, 0, 0, 0, 0, 0, 0, 255, 255, 255],
19+
[ 0, 0, 0, 0, 0, 0, 255, 255, 255, 255],
20+
[ 0, 0, 0, 0, 0, 0, 0, 255, 255, 255],
21+
[ 0, 0, 0, 0, 0, 0, 255, 255, 255, 255]
2222
])
2323

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

3838
check(output == [
39-
[ 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
40-
[ 0, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
41-
[ 0, 0, 0, 0, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
42-
[ 0, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
43-
[ 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
44-
[ 0, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
39+
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255],
40+
[ 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
41+
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255],
42+
[ 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255],
43+
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255],
44+
[ 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
4545
])
4646

4747

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

5757
check(output == [
58-
[ 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 255, 255],
59-
[ 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255],
60-
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255],
61-
[ 0, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255],
62-
[ 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 255, 255],
63-
[ 0, 0, 255, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255],
64-
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255],
65-
[ 0, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255],
66-
[ 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 255, 255],
67-
[ 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255],
58+
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255],
59+
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 0],
60+
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255],
61+
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0],
62+
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255],
63+
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 0],
64+
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255],
65+
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0],
66+
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255],
67+
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 0],
6868
])

0 commit comments

Comments
 (0)