From 3e7cb37253e8da5e51f7266fb4f11cb9ac93424d Mon Sep 17 00:00:00 2001 From: kris Date: Tue, 12 Jan 2021 00:27:03 +0000 Subject: [PATCH] Oops, don't use python to cast to long --- dither.py | 7 +------ dither_apply.pyx | 11 +++++------ setup.py | 9 +++++++++ 3 files changed, 15 insertions(+), 12 deletions(-) create mode 100644 setup.py diff --git a/dither.py b/dither.py index 19ab178..f9bbc16 100644 --- a/dither.py +++ b/dither.py @@ -7,9 +7,6 @@ from typing import Tuple from PIL import Image import numpy as np -import pyximport; - -pyximport.install(language_level=3) import dither_apply @@ -438,8 +435,6 @@ def dither_image( image_4bit = np.empty( (image_rgb.shape[0], image_rgb.shape[1]), dtype=np.uint8) - # pattern = dither.PATTERN - for y in range(screen.Y_RES): print(y) output_pixel_4bit = np.uint8(0) @@ -456,7 +451,7 @@ def dither_image( image_4bit[y, x] = output_pixel_4bit image_rgb[y, x, :] = output_pixel_rgb quant_error = input_pixel_rgb - output_pixel_rgb - dither.apply(screen, image_rgb, x, y, quant_error) + dither_apply.apply(dither, screen, x, y, image_rgb, quant_error) return image_4bit, image_rgb diff --git a/dither_apply.pyx b/dither_apply.pyx index e4b953e..b45b03d 100644 --- a/dither_apply.pyx +++ b/dither_apply.pyx @@ -22,7 +22,7 @@ cdef void apply_one_line(float[:, :, ::1] pattern, int xl, int xr, float[:, ::1] for i in range(xr - xl): for j in range(3): error = pattern[0, i, 0] * quant_error[j] - image[xl+i, j] = clip(image[xl + i, j] + error, 0, 255) + image[xl + i, j] = clip(image[xl + i, j] + error, 0, 255) @cython.boundscheck(False) @@ -96,13 +96,12 @@ def dither_lookahead( for j in range(xxr - x): for k in range(3): lah_image_rgb[i, j, k] = image_rgb[y, x+j, k] - # lah_image_rgb[:, 0:xxr - x, :] = image_rgb[y, x:xxr, :] # Leave enough space at right of image so we can dither the last of our lookahead pixels. for j in range(xxr - x, lookahead + xr - xl): for k in range(3): lah_image_rgb[i, j, k] = 0 - cdef float[3] quant_error + cdef float[3] quant_error # Iterating by row then column is faster for some reason? for i in range(xxr - x): xl, xr = x_dither_bounds(pattern, dither_x_origin, x_res, i) @@ -129,9 +128,9 @@ def dither_lookahead( total_error = 0 for j in range(lookahead): # Clip lah_image_rgb into 0..255 range to prepare for computing colour distance - r = long(clip(lah_image_rgb[i, j, 0], 0, 255)) - g = long(clip(lah_image_rgb[i, j, 1], 0, 255)) - b = long(clip(lah_image_rgb[i, j, 2], 0, 255)) + r = clip(lah_image_rgb[i, j, 0], 0, 255) + g = clip(lah_image_rgb[i, j, 1], 0, 255) + b = clip(lah_image_rgb[i, j, 2], 0, 255) flat = (r << 16) + (g << 8) + b bit4 = options_4bit[i, j] diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..19d8b0f --- /dev/null +++ b/setup.py @@ -0,0 +1,9 @@ +from setuptools import setup +from Cython.Build import cythonize + +import Cython.Compiler.Options +Cython.Compiler.Options.annotate = True + +setup( + ext_modules=cythonize(["dither_apply.pyx"], annotate=True) +)