diff --git a/dither.pyx b/dither.pyx index fbe687c..ecf93e7 100644 --- a/dither.pyx +++ b/dither.pyx @@ -4,7 +4,7 @@ cimport cython import functools import numpy as np -# cimport numpy as np +cimport numpy as np # from cython.parallel import prange from cython.view cimport array as cvarray from libc.stdlib cimport malloc, free @@ -57,20 +57,20 @@ cdef int dither_bounds_yb(Dither *dither, int y_res, int y) nogil: @cython.boundscheck(False) @cython.wraparound(False) @functools.lru_cache(None) -def lookahead_options(screen, lookahead, last_pixel_nbit, x): - options_nbit = np.empty((2 ** lookahead, lookahead), dtype=np.uint8) - options_rgb = np.empty((2 ** lookahead, lookahead, 3), dtype=np.float32) +def lookahead_options(screen, lookahead, last_pixel_4bit, x): + options_4bit = np.empty([2 ** lookahead, lookahead], dtype=np.uint8) + options_rgb = np.empty([2 ** lookahead, lookahead, 3], dtype=np.float) for i in range(2 ** lookahead): output_pixel_nbit = last_pixel_nbit for j in range(lookahead): xx = x + j - palette_choices_nbit, palette_choices_rgb = \ - screen.pixel_palette_options(output_pixel_nbit, xx) - output_pixel_nbit = palette_choices_nbit[(i & (1 << j)) >> j] - output_pixel_rgb = np.array( - palette_choices_rgb[(i & (1 << j)) >> j]) - options_nbit[i, j] = output_pixel_nbit - options_rgb[i, j, :] = output_pixel_rgb + palette_choices_4bit, palette_choices_rgb = \ + screen.pixel_palette_options(output_pixel_4bit, xx) + # output_pixel_4bit = palette_choices_4bit[(i & (1 << j)) >> j] + # output_pixel_rgb = np.array( + # palette_choices_rgb[(i & (1 << j)) >> j]) + options_4bit[i, j] = palette_choices_4bit[(i & (1 << j)) >> j] # output_pixel_4bit + options_rgb[i, j, :] = palette_choices_rgb[(i & (1 << j)) >> j] #output_pixel_rgb return options_nbit, options_rgb @@ -78,8 +78,8 @@ def lookahead_options(screen, lookahead, last_pixel_nbit, x): @cython.boundscheck(False) @cython.wraparound(False) cdef int dither_lookahead(Dither* dither, - Image* image_rgb, int x, int y, unsigned char[:, ::1] options_4bit, - float[:, :, ::1] options_rgb, int lookahead, unsigned char[:, ::1] distances, int x_res): + Image* image_rgb, int x, int y, np.ndarray[np.uint8_t, ndim=2] options_4bit, + np.ndarray[np.float_t, ndim=3] options_rgb, int lookahead, unsigned char[:, ::1] distances, int x_res): cdef int xl = dither_bounds_xl(dither, x) cdef int xr = dither_bounds_xr(dither, x_res, x) @@ -103,11 +103,10 @@ cdef int dither_lookahead(Dither* dither, lah_image_rgb[i * lah_shape1 * lah_shape2 + j * lah_shape2 + k] = 0 cdef float[3] quant_error - # Iterating by row then column is faster for some reason? - for i in range(xxr - x): - xl = dither_bounds_xl(dither, i) - xr = dither_bounds_xr(dither, x_res - x, i) - for j in range(2 ** lookahead): + for i in range(2 ** lookahead): + for j in range(xxr - x): + xl = dither_bounds_xl(dither, j) + xr = dither_bounds_xr(dither, x_res - x, j) # Don't update the input at position x (since we've already chosen # fixed outputs), but do propagate quantization errors to positions >x # so we can compensate for how good/bad these choices were @@ -116,8 +115,8 @@ cdef int dither_lookahead(Dither* dither, # quantization error from having made these choices, in order to compute # the total error for k in range(3): - quant_error[k] = lah_image_rgb[j * lah_shape1 * lah_shape2 + i * lah_shape2 + k] - options_rgb[j, i, k] - apply_one_line(dither, xl, xr, i, &lah_image_rgb[j * lah_shape1 * lah_shape2], lah_shape2, quant_error) + quant_error[k] = lah_image_rgb[i * lah_shape1 * lah_shape2 + j * lah_shape2 + k] - options_rgb[i, j, k] + apply_one_line(dither, xl, xr, j, &lah_image_rgb[j * lah_shape1 * lah_shape2], lah_shape2, quant_error) cdef unsigned char bit4 cdef int best diff --git a/setup.py b/setup.py index 1090d0b..ac9b113 100644 --- a/setup.py +++ b/setup.py @@ -1,9 +1,16 @@ -from setuptools import setup +from setuptools import setup, Extension from Cython.Build import cythonize +import numpy import Cython.Compiler.Options Cython.Compiler.Options.annotate = True +extensions = [ + Extension("dither", ["dither.pyx"], include_dirs=[numpy.get_include()]) +] + setup( - ext_modules=cythonize(["dither.pyx"], annotate=True) -) + name='dither', + ext_modules=cythonize(extensions), + script_args = ["build_ext", "--inplace"] +) \ No newline at end of file