mirror of
https://github.com/KrisKennaway/ii-pix.git
synced 2024-11-19 08:30:48 +00:00
Checkpoint - I broke something
This commit is contained in:
parent
a686ef76c3
commit
f4cc7dac40
39
dither.pyx
39
dither.pyx
@ -4,7 +4,7 @@
|
|||||||
cimport cython
|
cimport cython
|
||||||
import functools
|
import functools
|
||||||
import numpy as np
|
import numpy as np
|
||||||
# cimport numpy as np
|
cimport numpy as np
|
||||||
# from cython.parallel import prange
|
# from cython.parallel import prange
|
||||||
from cython.view cimport array as cvarray
|
from cython.view cimport array as cvarray
|
||||||
from libc.stdlib cimport malloc, free
|
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.boundscheck(False)
|
||||||
@cython.wraparound(False)
|
@cython.wraparound(False)
|
||||||
@functools.lru_cache(None)
|
@functools.lru_cache(None)
|
||||||
def lookahead_options(screen, lookahead, last_pixel_nbit, x):
|
def lookahead_options(screen, lookahead, last_pixel_4bit, x):
|
||||||
options_nbit = np.empty((2 ** lookahead, lookahead), dtype=np.uint8)
|
options_4bit = np.empty([2 ** lookahead, lookahead], dtype=np.uint8)
|
||||||
options_rgb = np.empty((2 ** lookahead, lookahead, 3), dtype=np.float32)
|
options_rgb = np.empty([2 ** lookahead, lookahead, 3], dtype=np.float)
|
||||||
for i in range(2 ** lookahead):
|
for i in range(2 ** lookahead):
|
||||||
output_pixel_nbit = last_pixel_nbit
|
output_pixel_nbit = last_pixel_nbit
|
||||||
for j in range(lookahead):
|
for j in range(lookahead):
|
||||||
xx = x + j
|
xx = x + j
|
||||||
palette_choices_nbit, palette_choices_rgb = \
|
palette_choices_4bit, palette_choices_rgb = \
|
||||||
screen.pixel_palette_options(output_pixel_nbit, xx)
|
screen.pixel_palette_options(output_pixel_4bit, xx)
|
||||||
output_pixel_nbit = palette_choices_nbit[(i & (1 << j)) >> j]
|
# output_pixel_4bit = palette_choices_4bit[(i & (1 << j)) >> j]
|
||||||
output_pixel_rgb = np.array(
|
# output_pixel_rgb = np.array(
|
||||||
palette_choices_rgb[(i & (1 << j)) >> j])
|
# palette_choices_rgb[(i & (1 << j)) >> j])
|
||||||
options_nbit[i, j] = output_pixel_nbit
|
options_4bit[i, j] = palette_choices_4bit[(i & (1 << j)) >> j] # output_pixel_4bit
|
||||||
options_rgb[i, j, :] = output_pixel_rgb
|
options_rgb[i, j, :] = palette_choices_rgb[(i & (1 << j)) >> j] #output_pixel_rgb
|
||||||
|
|
||||||
return options_nbit, options_rgb
|
return options_nbit, options_rgb
|
||||||
|
|
||||||
@ -78,8 +78,8 @@ def lookahead_options(screen, lookahead, last_pixel_nbit, x):
|
|||||||
@cython.boundscheck(False)
|
@cython.boundscheck(False)
|
||||||
@cython.wraparound(False)
|
@cython.wraparound(False)
|
||||||
cdef int dither_lookahead(Dither* dither,
|
cdef int dither_lookahead(Dither* dither,
|
||||||
Image* image_rgb, int x, int y, unsigned char[:, ::1] options_4bit,
|
Image* image_rgb, int x, int y, np.ndarray[np.uint8_t, ndim=2] options_4bit,
|
||||||
float[:, :, ::1] options_rgb, int lookahead, unsigned char[:, ::1] distances, int x_res):
|
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 xl = dither_bounds_xl(dither, x)
|
||||||
cdef int xr = dither_bounds_xr(dither, x_res, 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
|
lah_image_rgb[i * lah_shape1 * lah_shape2 + j * lah_shape2 + 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(2 ** lookahead):
|
||||||
for i in range(xxr - x):
|
for j in range(xxr - x):
|
||||||
xl = dither_bounds_xl(dither, i)
|
xl = dither_bounds_xl(dither, j)
|
||||||
xr = dither_bounds_xr(dither, x_res - x, i)
|
xr = dither_bounds_xr(dither, x_res - x, j)
|
||||||
for j in range(2 ** lookahead):
|
|
||||||
# Don't update the input at position x (since we've already chosen
|
# Don't update the input at position x (since we've already chosen
|
||||||
# fixed outputs), but do propagate quantization errors to positions >x
|
# fixed outputs), but do propagate quantization errors to positions >x
|
||||||
# so we can compensate for how good/bad these choices were
|
# 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
|
# quantization error from having made these choices, in order to compute
|
||||||
# the total error
|
# the total error
|
||||||
for k in range(3):
|
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]
|
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, i, &lah_image_rgb[j * lah_shape1 * lah_shape2], lah_shape2, quant_error)
|
apply_one_line(dither, xl, xr, j, &lah_image_rgb[j * lah_shape1 * lah_shape2], lah_shape2, quant_error)
|
||||||
|
|
||||||
cdef unsigned char bit4
|
cdef unsigned char bit4
|
||||||
cdef int best
|
cdef int best
|
||||||
|
13
setup.py
13
setup.py
@ -1,9 +1,16 @@
|
|||||||
from setuptools import setup
|
from setuptools import setup, Extension
|
||||||
from Cython.Build import cythonize
|
from Cython.Build import cythonize
|
||||||
|
import numpy
|
||||||
|
|
||||||
import Cython.Compiler.Options
|
import Cython.Compiler.Options
|
||||||
Cython.Compiler.Options.annotate = True
|
Cython.Compiler.Options.annotate = True
|
||||||
|
|
||||||
|
extensions = [
|
||||||
|
Extension("dither", ["dither.pyx"], include_dirs=[numpy.get_include()])
|
||||||
|
]
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
ext_modules=cythonize(["dither.pyx"], annotate=True)
|
name='dither',
|
||||||
)
|
ext_modules=cythonize(extensions),
|
||||||
|
script_args = ["build_ext", "--inplace"]
|
||||||
|
)
|
Loading…
Reference in New Issue
Block a user