Mmap uncompressed distance array instead of bunzipping first. Since

we don't end up reading most of the elements again, this is faster.
This commit is contained in:
kris 2021-01-12 20:44:39 +00:00
parent 45ceb9987f
commit ebd9f94d98

View File

@ -135,17 +135,16 @@ class CIE2000Distance(ColourDistance):
"""CIE2000 delta-E distance.""" """CIE2000 delta-E distance."""
def __init__(self): def __init__(self):
with bz2.open("nearest.pickle.bz2", "rb") as f: self._distances = np.memmap("distances.npy", mode="r+",
self._distances = pickle.load(f) dtype=np.uint8, shape=(16777216, 16))
assert self._distances.dtype == np.uint8 #
# @staticmethod
@staticmethod # def _flatten_rgb(rgb):
def _flatten_rgb(rgb): # return (rgb[..., 0] << 16) + (rgb[..., 1] << 8) + (rgb[..., 2])
return (rgb[..., 0] << 16) + (rgb[..., 1] << 8) + (rgb[..., 2]) #
# def distance(self, rgb: np.ndarray, bit4: np.ndarray) -> np.ndarray:
def distance(self, rgb: np.ndarray, bit4: np.ndarray) -> np.ndarray: # rgb24 = self._flatten_rgb(rgb)
rgb24 = self._flatten_rgb(rgb) # return self._distances[rgb24, bit4].astype(np.int)
return self._distances[rgb24, bit4].astype(np.int)
class Screen: class Screen:
@ -342,10 +341,12 @@ def open_image(screen: Screen, filename: str) -> np.ndarray:
# Convert to linear RGB before rescaling so that colour interpolation is # Convert to linear RGB before rescaling so that colour interpolation is
# in linear space # in linear space
# XXX opt?
linear = srgb_to_linear(np.array(im, dtype=np.float32)) linear = srgb_to_linear(np.array(im, dtype=np.float32))
rescaled = Image.fromarray( rescaled = Image.fromarray(
linear.astype(np.uint8)).resize( linear.astype(np.uint8)).resize(
(screen.X_RES, screen.Y_RES), Image.LANCZOS) (screen.X_RES, screen.Y_RES), Image.LANCZOS)
# XXX work with malloc'ed array?
return np.array(rescaled, dtype=np.float32) return np.array(rescaled, dtype=np.float32)