mirror of
https://github.com/KrisKennaway/ii-pix.git
synced 2024-06-03 05:29:32 +00:00
Tidy a bit
This commit is contained in:
parent
348752dc35
commit
0109675bf2
|
@ -32,9 +32,11 @@ cdef apply(dither, screen, int x, int y, float [:, :, ::1]image, float[] quant_e
|
||||||
|
|
||||||
# XXX only need 2 dimensions now
|
# XXX only need 2 dimensions now
|
||||||
cdef float[:, :, ::1] pattern = dither.PATTERN
|
cdef float[:, :, ::1] pattern = dither.PATTERN
|
||||||
cdef int yt, yb, xl, xr
|
|
||||||
yt, yb = y_dither_bounds(pattern, dither.ORIGIN[0], screen.Y_RES, y)
|
cdef int yt = dither_bounds_yt(dither.ORIGIN[0], y)
|
||||||
xl, xr = x_dither_bounds(pattern, dither.ORIGIN[1], screen.X_RES, x)
|
cdef int yb = dither_bounds_yb(pattern, dither.ORIGIN[0], screen.Y_RES, y)
|
||||||
|
cdef int xl = dither_bounds_xl(dither.ORIGIN[1], x)
|
||||||
|
cdef int xr = dither_bounds_xr(pattern, dither.ORIGIN[1], screen.X_RES, x)
|
||||||
|
|
||||||
cdef float error
|
cdef float error
|
||||||
# We could avoid clipping here, i.e. allow RGB values to extend beyond
|
# We could avoid clipping here, i.e. allow RGB values to extend beyond
|
||||||
|
@ -49,28 +51,32 @@ cdef apply(dither, screen, int x, int y, float [:, :, ::1]image, float[] quant_e
|
||||||
|
|
||||||
@cython.boundscheck(False)
|
@cython.boundscheck(False)
|
||||||
@cython.wraparound(False)
|
@cython.wraparound(False)
|
||||||
cdef x_dither_bounds(float [:, :, ::1] pattern, int x_origin, int x_res, int x):
|
cdef int dither_bounds_xl(int x_origin, int x):
|
||||||
cdef int el = max(x_origin - x, 0)
|
cdef int el = max(x_origin - x, 0)
|
||||||
cdef int er = min(pattern.shape[1], x_res - 1 - x)
|
|
||||||
|
|
||||||
cdef int xl = x - x_origin + el
|
cdef int xl = x - x_origin + el
|
||||||
cdef int xr = x - x_origin + er
|
return xl
|
||||||
|
|
||||||
return xl, xr
|
|
||||||
|
|
||||||
|
|
||||||
@cython.boundscheck(False)
|
@cython.boundscheck(False)
|
||||||
@cython.wraparound(False)
|
@cython.wraparound(False)
|
||||||
cdef y_dither_bounds(float [:, :, ::1] pattern, int y_origin, int y_res, int y):
|
cdef int dither_bounds_xr(float [:, :, ::1] pattern, int x_origin, int x_res, int x):
|
||||||
pshape = pattern.shape
|
cdef int er = min(pattern.shape[1], x_res - 1 - x)
|
||||||
et = max(y_origin - y, 0)
|
cdef int xr = x - x_origin + er
|
||||||
eb = min(pshape[0], y_res - 1 - y)
|
return xr
|
||||||
|
|
||||||
yt = y - y_origin + et
|
@cython.boundscheck(False)
|
||||||
yb = y - y_origin + eb
|
@cython.wraparound(False)
|
||||||
|
cdef int dither_bounds_yt(int y_origin, int y):
|
||||||
|
cdef int et = max(y_origin - y, 0)
|
||||||
|
cdef int yt = y - y_origin + et
|
||||||
|
|
||||||
|
return yt
|
||||||
|
|
||||||
return yt, yb
|
@cython.boundscheck(False)
|
||||||
|
@cython.wraparound(False)
|
||||||
|
cdef int dither_bounds_yb(float [:, :, ::1] pattern, int y_origin, int y_res, int y):
|
||||||
|
cdef int eb = min(pattern.shape[0], y_res - 1 - y)
|
||||||
|
cdef int yb = y - y_origin + eb
|
||||||
|
return yb
|
||||||
|
|
||||||
@cython.boundscheck(False)
|
@cython.boundscheck(False)
|
||||||
@cython.wraparound(False)
|
@cython.wraparound(False)
|
||||||
|
@ -81,16 +87,14 @@ def dither_lookahead(
|
||||||
cdef int x_res = screen.X_RES
|
cdef int x_res = screen.X_RES
|
||||||
cdef int dither_x_origin = dither.ORIGIN[1]
|
cdef int dither_x_origin = dither.ORIGIN[1]
|
||||||
|
|
||||||
cdef int xl, xr
|
cdef int xl = dither_bounds_xl(dither_x_origin, x)
|
||||||
xl, xr = x_dither_bounds(pattern, dither_x_origin, x_res, x)
|
cdef int xr = dither_bounds_xr(pattern, dither_x_origin, x_res, x)
|
||||||
|
|
||||||
# X coord value of larger of dither bounding box or lookahead horizon
|
# X coord value of larger of dither bounding box or lookahead horizon
|
||||||
cdef int xxr = min(max(x + lookahead, xr), x_res)
|
cdef int xxr = min(max(x + lookahead, xr), x_res)
|
||||||
|
|
||||||
cdef int i, j, k, l
|
cdef int i, j, k, l
|
||||||
|
|
||||||
# XXX malloc
|
|
||||||
#cdef float [:, :, ::1] lah_image_rgb = cvarray((2 ** lookahead, lookahead + xr - xl, 3), itemsize=sizeof(float), format="f")
|
|
||||||
cdef int lah_shape0 = 2 ** lookahead
|
cdef int lah_shape0 = 2 ** lookahead
|
||||||
cdef int lah_shape1 = lookahead + xr - xl
|
cdef int lah_shape1 = lookahead + xr - xl
|
||||||
cdef int lah_shape2 = 3
|
cdef int lah_shape2 = 3
|
||||||
|
@ -108,7 +112,8 @@ def dither_lookahead(
|
||||||
cdef float[3] quant_error
|
cdef float[3] quant_error
|
||||||
# Iterating by row then column is faster for some reason?
|
# Iterating by row then column is faster for some reason?
|
||||||
for i in range(xxr - x):
|
for i in range(xxr - x):
|
||||||
xl, xr = x_dither_bounds(pattern, dither_x_origin, x_res, i)
|
xl = dither_bounds_xl(dither_x_origin, i)
|
||||||
|
xr = dither_bounds_xr(pattern, dither_x_origin, x_res, i)
|
||||||
for j in range(2 ** lookahead):
|
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
|
||||||
|
@ -164,9 +169,8 @@ def lookahead_options(screen, lookahead, last_pixel_4bit, x):
|
||||||
output_pixel_4bit = palette_choices_4bit[(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])
|
||||||
# XXX copy
|
|
||||||
options_4bit[i, j] = output_pixel_4bit
|
options_4bit[i, j] = output_pixel_4bit
|
||||||
options_rgb[i, j, :] = np.copy(output_pixel_rgb)
|
options_rgb[i, j, :] = output_pixel_rgb
|
||||||
|
|
||||||
return options_4bit, options_rgb
|
return options_4bit, options_rgb
|
||||||
|
|
||||||
|
@ -206,4 +210,4 @@ def dither_image(
|
||||||
image_4bit[y, x] = output_pixel_4bit
|
image_4bit[y, x] = output_pixel_4bit
|
||||||
apply(dither, screen, x, y, image_rgb, quant_error)
|
apply(dither, screen, x, y, image_rgb, quant_error)
|
||||||
|
|
||||||
return image_4bit, np.array(image_rgb)
|
return image_4bit, np.array(image_rgb)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user