ii-pix/dither_pattern.py

179 lines
4.1 KiB
Python
Raw Normal View History

2021-01-15 22:18:25 +00:00
import numpy as np
class DitherPattern:
PATTERN = None
ORIGIN = None
class FloydSteinbergDither(DitherPattern):
"""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)),
dtype=np.float32).reshape(2, 3, 1) / np.float(16)
# XXX X_ORIGIN since ORIGIN[0] == 0
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)),
dtype=np.float32).reshape(2, 6, 1) / np.float(16)
# XXX X_ORIGIN since ORIGIN[0] == 0
ORIGIN = (0, 2)
2021-01-15 22:18:25 +00:00
class BuckelsDither(DitherPattern):
"""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)),
dtype=np.float32).reshape(3, 4, 1) / np.float32(8)
ORIGIN = (0, 1)
class JarvisDither(DitherPattern):
"""Jarvis dithering."""
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)),
dtype=np.float32).reshape(3, 5, 1) / np.float32(48)
ORIGIN = (0, 2)
2021-01-21 23:17:55 +00:00
class NoDither(DitherPattern):
"""Floyd-Steinberg dither."""
# 0 * 7
# 3 5 1
PATTERN = np.array(((0, 0), (0, 0)),
dtype=np.float32).reshape(2, 2, 1) / np.float(16)
# XXX X_ORIGIN since ORIGIN[0] == 0
ORIGIN = (0, 1)
class TestDither(DitherPattern):
"""Jarvis dithering."""
# 0 0 X 7 5
# 3 5 7 5 3
# 1 3 5 3 1
PATTERN = np.array((
(0, 0, 0, 31, 29, 27, 25),
(3, 5, 7, 5, 3, 1, 0),
(1, 3, 5, 3, 1, 0, 0)), dtype=np.float32).reshape(3, 7, 1)
PATTERN /= np.sum(PATTERN)
ORIGIN = (0, 2)
class xTestDither(DitherPattern):
"""Jarvis dithering."""
# 0 0 X 7 5
# 3 5 7 5 3
# 1 3 5 3 1
PATTERN = np.array((
(0, 0, 15, 15, 15, 15),
(3, 3, 5, 5, 1, 1)), dtype=np.float32).reshape(2, 6, 1)
PATTERN /= np.sum(PATTERN)
ORIGIN = (0, 1)
class TestDither(DitherPattern):
"""Jarvis dithering."""
# 0 0 X 7 5
# 3 5 7 5 3
# 1 3 5 3 1
PATTERN = np.array((
(0, 0, 0, 7, 7, 7, 7),
(3, 5, 7, 5, 3, 1, 0),
(1, 3, 5, 3, 1, 0, 0)), dtype=np.float32).reshape(3, 7, 1)
PATTERN /= np.sum(PATTERN)
ORIGIN = (0, 2)
# !!
class TestDither(DitherPattern):
"""Jarvis dithering."""
# 0 0 X 7 5
# 3 5 7 5 3
# 1 3 5 3 1
PATTERN = np.array((
(0, 0, 0, 9, 7, 5, 3),
(3, 5, 7, 5, 3, 1, 0),
(1, 3, 5, 3, 1, 0, 0)), dtype=np.float32).reshape(3, 7, 1)
PATTERN /= np.sum(PATTERN)
ORIGIN = (0, 2)
# !!!
class TestDither(DitherPattern):
"""Jarvis dithering."""
# 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),
(1, 3, 5, 3, 1, 0, 0)), dtype=np.float32).reshape(3, 7, 1)
PATTERN /= np.sum(PATTERN)
ORIGIN = (0, 2)
class xTestDither(DitherPattern):
"""Jarvis dithering."""
# 0 0 X 7 5
# 3 5 7 5 3
# 1 3 5 3 1
PATTERN = np.array((
(0, 0, 0, 9, 9,9,9),
(3, 5, 7, 5, 3, 1, 0),
(1, 3, 5, 3, 1, 0, 0)), dtype=np.float32).reshape(3, 7, 1)
PATTERN /= np.sum(PATTERN)
ORIGIN = (0, 2)
class xTestDither(DitherPattern):
"""Jarvis dithering."""
# 0 0 X 7 5
# 3 5 7 5 3
# 1 3 5 3 1
PATTERN = np.array((
(0, 0, 0, 15,13,11,9),
(3, 5, 7, 5, 3, 1, 0),
(1, 3, 5, 3, 1, 0, 0)), dtype=np.float32).reshape(3, 7, 1)
PATTERN /= np.sum(PATTERN)
ORIGIN = (0, 2)
class xTestDither(DitherPattern):
"""Jarvis dithering."""
# 0 0 X 7 5
# 3 5 7 5 3
# 1 3 5 3 1
PATTERN = np.array((
(0, 0, 15,),
(3, 5, 1,)), dtype=np.float32).reshape(2, 3, 1)
PATTERN /= np.sum(PATTERN)
ORIGIN = (0, 1)
PATTERNS = {
'floyd': FloydSteinbergDither,
2021-01-21 23:17:55 +00:00
'floyd2': FloydSteinbergDither2,
'floyd-steinberg': FloydSteinbergDither,
'buckels': BuckelsDither,
2021-01-21 23:17:55 +00:00
'jarvis': JarvisDither,
'test': TestDither,
'none': NoDither
}
2021-01-21 23:17:55 +00:00
DEFAULT_PATTERN = 'jarvis'