Optimize more

This commit is contained in:
kris 2021-01-11 20:56:26 +00:00
parent a40b496a38
commit b601360669
1 changed files with 17 additions and 24 deletions

View File

@ -40,39 +40,34 @@ def apply(pattern, int el, int er, int xl, int xr, int et, int eb, int yt, int y
image[yt:yb, xl:xr, :] + error[et:eb, el:er, :], 0, 255)
def x_dither_bounds(dither, screen, int x):
cdef int el = max(dither.ORIGIN[1] - x, 0)
cdef int er = min(dither.PATTERN.shape[1], screen.X_RES - 1 - x)
cdef x_dither_bounds(float [:, :, ::1] pattern, int x_origin, int x_res, int x):
cdef int el = max(x_origin - x, 0)
cdef int er = min(pattern.shape[1], x_res - 1 - x)
cdef int xl = x - dither.ORIGIN[1] + el
cdef int xr = x - dither.ORIGIN[1] + er
cdef int xl = x - x_origin + el
cdef int xr = x - x_origin + er
return el, er, xl, xr
def y_dither_bounds(dither, screen, int y):
cdef int et = max(dither.ORIGIN[0] - y, 0)
cdef int eb = min(dither.PATTERN.shape[0], screen.Y_RES - 1 - y)
cdef int yt = y - dither.ORIGIN[0] + et
cdef int yb = y - dither.ORIGIN[0] + eb
return et, eb, yt, yb
def dither_lookahead(
screen, float[:,:,::1] image_rgb, dither, differ, int x, int y, char[:, ::1] options_4bit,
float[:, :, ::1] options_rgb, int lookahead):
el, er, xl, xr = x_dither_bounds(dither, screen, x)
cdef float[:, :, ::1] pattern = dither.PATTERN
cdef int x_res = screen.X_RES
cdef int dither_x_origin = dither.ORIGIN[1]
cdef int el, er, xl, xr
el, er, xl, xr = x_dither_bounds(pattern, dither_x_origin, x_res, x)
# X coord value of larger of dither bounding box or lookahead horizon
xxr = min(max(x + lookahead, xr), screen.X_RES)
cdef int xxr = min(max(x + lookahead, xr), x_res)
# copies of input pixels so we can dither in bulk
# Leave enough space so we can dither the last of our lookahead pixels
cdef float[:, :, ::1] lah_image_rgb = np.zeros(
(2 ** lookahead, lookahead + xr - xl, 3), dtype=np.float32)
# X
# XXX opt
lah_image_rgb[:, 0:xxr - x, :] = image_rgb[y, x:xxr, :]
cdef float[:, ::] output_pixels
@ -80,7 +75,6 @@ def dither_lookahead(
cdef int i, j, k, l
cdef int[:, :, ::1] pattern = dither.PATTERN
for i in range(xxr - x):
# options_rgb choices are fixed, but we can still distribute
# quantization error from having made these choices, in order to compute
@ -92,15 +86,14 @@ def dither_lookahead(
# 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
el, er, xl, xr = x_dither_bounds(dither, screen, i)
el, er, xl, xr = x_dither_bounds(pattern, dither_x_origin, x_res, i)
for j in range(2 ** lookahead):
apply_one_line(pattern, el, er, xl, xr, 0, lah_image_rgb[j, :, :], &quant_error[j])
free(quant_error)
# XXX opt
error = differ.distance(np.clip(
lah_image_rgb[:, 0:lookahead, :], 0, 255), options_4bit)
total_error = np.sum(np.power(error, 2), axis=1)
best = np.argmin(total_error)
cdef long[:, ::1] error = differ.distance(np.clip(lah_image_rgb[:, 0:lookahead, :], 0, 255), options_4bit)
cdef long[::1] total_error = np.sum(np.power(error, 2), axis=1)
cdef int best = np.argmin(total_error)
return options_4bit[best, 0], options_rgb[best, 0, :]