2021-01-25 23:16:46 +00:00
|
|
|
"""Error diffusion dither patterns."""
|
|
|
|
|
2021-01-15 22:18:25 +00:00
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
class DitherPattern:
|
|
|
|
PATTERN = None
|
|
|
|
ORIGIN = None
|
|
|
|
|
|
|
|
|
2021-01-25 22:43:03 +00:00
|
|
|
class NoDither(DitherPattern):
|
|
|
|
"""No dithering."""
|
|
|
|
PATTERN = np.array(((0, 0), (0, 0)),
|
2021-03-15 16:22:55 +00:00
|
|
|
dtype=np.float32).reshape(2, 2) / np.float(16)
|
2021-01-25 22:43:03 +00:00
|
|
|
ORIGIN = (0, 1)
|
|
|
|
|
|
|
|
|
2021-01-15 22:18:25 +00:00
|
|
|
class FloydSteinbergDither(DitherPattern):
|
2021-01-15 22:25:06 +00:00
|
|
|
"""Floyd-Steinberg dither."""
|
2021-01-15 22:18:25 +00:00
|
|
|
# 0 * 7
|
|
|
|
# 3 5 1
|
|
|
|
PATTERN = np.array(((0, 0, 7), (3, 5, 1)),
|
2021-03-15 16:22:55 +00:00
|
|
|
dtype=np.float32).reshape(2, 3) / np.float(16)
|
2021-01-15 22:18:25 +00:00
|
|
|
ORIGIN = (0, 1)
|
|
|
|
|
|
|
|
|
2021-01-21 23:17:55 +00:00
|
|
|
class FloydSteinbergDither2(DitherPattern):
|
|
|
|
"""Floyd-Steinberg dither."""
|
|
|
|
# 0 * 7
|
|
|
|
# 3 5 1
|
|
|
|
PATTERN = np.array(
|
|
|
|
((0, 0, 0, 0, 0, 7),
|
|
|
|
(3, 5, 1, 0, 0, 0)),
|
2021-03-15 16:22:55 +00:00
|
|
|
dtype=np.float32).reshape(2, 6) / np.float(16)
|
2021-01-21 23:17:55 +00:00
|
|
|
ORIGIN = (0, 2)
|
|
|
|
|
|
|
|
|
2021-01-15 22:18:25 +00:00
|
|
|
class BuckelsDither(DitherPattern):
|
2021-01-15 22:25:06 +00:00
|
|
|
"""Default dither from bmp2dhr."""
|
2021-01-15 22:18:25 +00:00
|
|
|
# 0 * 2 1
|
|
|
|
# 1 2 1 0
|
|
|
|
# 0 1 0 0
|
|
|
|
PATTERN = np.array(((0, 0, 2, 1), (1, 2, 1, 0), (0, 1, 0, 0)),
|
2021-03-15 16:22:55 +00:00
|
|
|
dtype=np.float32).reshape(3, 4) / np.float32(8)
|
2021-01-15 22:18:25 +00:00
|
|
|
ORIGIN = (0, 1)
|
|
|
|
|
|
|
|
|
|
|
|
class JarvisDither(DitherPattern):
|
2021-01-25 22:43:03 +00:00
|
|
|
"""Jarvis-Judice-Ninke dithering."""
|
2021-01-15 22:25:06 +00:00
|
|
|
|
2021-01-15 22:18:25 +00:00
|
|
|
# 0 0 X 7 5
|
|
|
|
# 3 5 7 5 3
|
|
|
|
# 1 3 5 3 1
|
|
|
|
PATTERN = np.array(((0, 0, 0, 7, 5), (3, 5, 7, 5, 3), (1, 3, 5, 3, 1)),
|
2021-03-15 16:22:55 +00:00
|
|
|
dtype=np.float32).reshape(3, 5) / np.float32(48)
|
2021-01-15 22:18:25 +00:00
|
|
|
ORIGIN = (0, 2)
|
|
|
|
|
2021-01-15 22:25:06 +00:00
|
|
|
|
2021-01-25 22:43:03 +00:00
|
|
|
class JarvisModifiedDither(DitherPattern):
|
|
|
|
"""Jarvis dithering, modified to diffuse errors to 4 forward x positions.
|
2021-01-21 23:17:55 +00:00
|
|
|
|
2021-01-25 22:43:03 +00:00
|
|
|
This works well for double hi-res dithering, since the "best" colour
|
|
|
|
match to a given pixel may only be accessible up to 4 x-positions further
|
|
|
|
on. Standard Jarvis dithering only propagates errors for 2 x-positions
|
|
|
|
in the forward direction, which means that errors may have diffused away
|
|
|
|
before we get to the pixel that can best take advantage of it.
|
|
|
|
"""
|
2021-01-21 23:17:55 +00:00
|
|
|
|
|
|
|
# 0 0 X 7 5
|
|
|
|
# 3 5 7 5 3
|
|
|
|
# 1 3 5 3 1
|
|
|
|
PATTERN = np.array((
|
|
|
|
(0, 0, 0, 15, 11, 7, 3),
|
|
|
|
(3, 5, 7, 5, 3, 1, 0),
|
2021-03-15 16:22:55 +00:00
|
|
|
(1, 3, 5, 3, 1, 0, 0)), dtype=np.float32).reshape(3, 7)
|
2021-01-21 23:17:55 +00:00
|
|
|
PATTERN /= np.sum(PATTERN)
|
|
|
|
ORIGIN = (0, 2)
|
|
|
|
|
|
|
|
|
2021-01-15 22:25:06 +00:00
|
|
|
PATTERNS = {
|
|
|
|
'floyd': FloydSteinbergDither,
|
2021-01-21 23:17:55 +00:00
|
|
|
'floyd2': FloydSteinbergDither2,
|
2021-01-15 22:25:06 +00:00
|
|
|
'floyd-steinberg': FloydSteinbergDither,
|
|
|
|
'buckels': BuckelsDither,
|
2021-01-21 23:17:55 +00:00
|
|
|
'jarvis': JarvisDither,
|
2021-01-25 22:43:03 +00:00
|
|
|
'jarvis-mod': JarvisModifiedDither,
|
2021-01-21 23:17:55 +00:00
|
|
|
'none': NoDither
|
2021-01-15 22:25:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 22:43:03 +00:00
|
|
|
DEFAULT_PATTERN = 'jarvis-mod'
|