ii-pix/dither.pyx

264 lines
11 KiB
Cython
Raw Normal View History

2021-01-10 22:12:14 +00:00
# cython: infer_types=True
cimport cython
2021-01-15 22:58:01 +00:00
import functools
2021-01-10 22:12:14 +00:00
import numpy as np
# from cython.parallel import prange
2021-01-12 10:00:56 +00:00
from cython.view cimport array as cvarray
2021-01-12 10:21:31 +00:00
from libc.stdlib cimport malloc, free
2021-01-10 22:12:14 +00:00
2021-01-11 23:04:47 +00:00
@cython.boundscheck(False)
@cython.wraparound(False)
2021-01-10 22:12:14 +00:00
cdef float clip(float a, float min_value, float max_value) nogil:
return min(max(a, min_value), max_value)
2021-01-21 23:17:55 +00:00
#@cython.boundscheck(False)
2021-01-11 23:04:47 +00:00
@cython.wraparound(False)
2021-01-21 23:17:55 +00:00
cdef void apply_one_line(float[:, :, ::1] pattern, int xl, int xr, int x, int x_origin, float[] image, int image_shape1, float[] quant_error):
2021-01-10 22:12:14 +00:00
cdef int i, j
2021-01-11 23:04:47 +00:00
cdef float error
2021-01-10 22:12:14 +00:00
2021-01-21 23:17:55 +00:00
for i in range(xl, xr):
2021-01-10 22:12:14 +00:00
for j in range(3):
2021-01-21 23:17:55 +00:00
# print("aol: x=%d, applying pattern pos %d to pos %d" % (x, i-x+1, i))
error = pattern[0, i - x + x_origin, 0] * quant_error[j]
image[i * image_shape1 + j] = clip(image[i * image_shape1 + j] + error, 0, 255)
2021-01-10 22:12:14 +00:00
2021-01-21 23:17:55 +00:00
#@cython.boundscheck(False)
2021-01-11 23:41:04 +00:00
@cython.wraparound(False)
2021-01-12 10:00:56 +00:00
cdef apply(dither, screen, int x, int y, float [:, :, ::1]image, float[] quant_error):
2021-01-11 23:04:47 +00:00
cdef int i, j, k
2021-01-10 22:12:14 +00:00
2021-01-11 23:41:04 +00:00
# XXX only need 2 dimensions now
cdef float[:, :, ::1] pattern = dither.PATTERN
2021-01-12 22:22:37 +00:00
cdef int yt = dither_bounds_yt(dither.ORIGIN[0], y)
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)
2021-01-21 23:17:55 +00:00
# print("X %d %d %d" % (xl, x, xr))
# print("Y %d %d %d" % (yt, y, yb))
2021-01-11 23:04:47 +00:00
cdef float error
2021-01-10 22:12:14 +00:00
# We could avoid clipping here, i.e. allow RGB values to extend beyond
# 0..255 to capture a larger range of residual error. This is faster
# but seems to reduce image quality.
2021-01-21 23:17:55 +00:00
for i in range(yt, yb):
for j in range(xl, xr):
# XXX partially compute error here
2021-01-11 23:04:47 +00:00
for k in range(3):
2021-01-21 23:17:55 +00:00
# XXX unroll/malloc pattern
error = pattern[i - y, j - x + dither.ORIGIN[1], 0] * quant_error[k]
#print("Pattern %f " % pattern[i - y, j - x + dither.ORIGIN[1], 0])
#print("Apply error %f" % quant_error[k])
#print(error)
#print("(%d, %d) -> (%d, %d, %d): %f --> %f (%f, %f)" % (y, x, i, j, k, image[i,j,k], error, pattern[i - y, j - x + dither.ORIGIN[1], 0], quant_error[k]))
image[i, j, k] = clip(image[i, j, k] + error, 0, 255)
# print("%d %d %d" % (i,j,k))
2021-01-11 23:04:47 +00:00
@cython.boundscheck(False)
@cython.wraparound(False)
2021-01-12 22:22:37 +00:00
cdef int dither_bounds_xl(int x_origin, int x):
2021-01-11 20:56:26 +00:00
cdef int el = max(x_origin - x, 0)
cdef int xl = x - x_origin + el
2021-01-21 23:17:55 +00:00
# print("xl: origin=%d x=%d el=%d, xl=%d" % (x_origin, x, el, xl))
2021-01-12 22:22:37 +00:00
return xl
2021-01-11 23:41:04 +00:00
@cython.boundscheck(False)
@cython.wraparound(False)
2021-01-12 22:22:37 +00:00
cdef int dither_bounds_xr(float [:, :, ::1] pattern, int x_origin, int x_res, int x):
2021-01-21 23:17:55 +00:00
cdef int er = min(pattern.shape[1], x_res - x)
2021-01-12 22:22:37 +00:00
cdef int xr = x - x_origin + er
2021-01-21 23:17:55 +00:00
# print("xr: shape=%d origin=%d res=%d x=%d er=%d, xr=%d" % (pattern.shape[1], x_origin, x_res, x, er, xr))
2021-01-12 22:22:37 +00:00
return xr
2021-01-11 23:41:04 +00:00
2021-01-12 22:22:37 +00:00
@cython.boundscheck(False)
@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
2021-01-11 23:41:04 +00:00
2021-01-12 22:22:37 +00:00
return yt
2021-01-12 22:22:37 +00:00
@cython.boundscheck(False)
@cython.wraparound(False)
cdef int dither_bounds_yb(float [:, :, ::1] pattern, int y_origin, int y_res, int y):
2021-01-21 23:17:55 +00:00
cdef int eb = min(pattern.shape[0], y_res - y)
2021-01-12 22:22:37 +00:00
cdef int yb = y - y_origin + eb
return yb
2021-01-21 23:17:55 +00:00
# @cython.boundscheck(False)
2021-01-11 23:04:47 +00:00
@cython.wraparound(False)
def dither_lookahead(
2021-01-15 22:58:01 +00:00
screen, float[:,:,::1] image_rgb, dither, int x, int y, unsigned char[:, ::1] options_4bit,
float[:, :, ::1] options_rgb, int lookahead):
2021-01-11 20:56:26 +00:00
cdef float[:, :, ::1] pattern = dither.PATTERN
cdef int x_res = screen.X_RES
cdef int dither_x_origin = dither.ORIGIN[1]
2021-01-12 22:22:37 +00:00
cdef int xl = dither_bounds_xl(dither_x_origin, 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
2021-01-21 23:17:55 +00:00
cdef int xxr = min(max(x + lookahead, xr), x_res) # XXX
# print("xxr=%d, x=%d, xr=%d, x_res=%d" % (xxr, x, xr, x_res))
2021-01-11 20:43:28 +00:00
cdef int i, j, k, l
2021-01-12 10:21:31 +00:00
cdef int lah_shape0 = 2 ** lookahead
cdef int lah_shape1 = lookahead + xr - xl
cdef int lah_shape2 = 3
cdef float *lah_image_rgb = <float *> malloc(lah_shape0 * lah_shape1 * lah_shape2 * sizeof(float))
2021-01-11 23:41:04 +00:00
for i in range(2**lookahead):
# Copies of input pixels so we can dither in bulk
for j in range(xxr - x):
for k in range(3):
2021-01-12 10:21:31 +00:00
lah_image_rgb[i * lah_shape1 * lah_shape2 + j * lah_shape2 + k] = image_rgb[y, x+j, k]
2021-01-11 23:41:04 +00:00
# Leave enough space at right of image so we can dither the last of our lookahead pixels.
2021-01-21 23:17:55 +00:00
for j in range(xxr - x, lookahead + xr - xl): # XXX
2021-01-11 23:41:04 +00:00
for k in range(3):
2021-01-12 10:21:31 +00:00
lah_image_rgb[i * lah_shape1 * lah_shape2 + j * lah_shape2 + k] = 0
2021-01-11 23:41:04 +00:00
2021-01-12 00:27:03 +00:00
cdef float[3] quant_error
2021-01-11 23:41:04 +00:00
# Iterating by row then column is faster for some reason?
for i in range(xxr - x):
2021-01-12 22:22:37 +00:00
xl = dither_bounds_xl(dither_x_origin, i)
2021-01-21 23:17:55 +00:00
xr = dither_bounds_xr(pattern, dither_x_origin, x_res - x, i)# XXX right-hand bounds?
# print("aol: %d %d (%d) %d" % (xl, i, i+x, xr))
for j in range(2 ** lookahead):
2021-01-11 23:41:04 +00:00
# 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
2021-01-11 20:43:28 +00:00
2021-01-11 23:41:04 +00:00
# options_rgb choices are fixed, but we can still distribute
# quantization error from having made these choices, in order to compute
# the total error
for k in range(3):
2021-01-21 23:17:55 +00:00
#print("j=%d, i=%d, k=%d, lah=%f, option=%f" % (j, i, k, lah_image_rgb[j * lah_shape1 * lah_shape2 + i * lah_shape2 + k] , options_rgb[j,i,k]))
2021-01-12 10:21:31 +00:00
quant_error[k] = lah_image_rgb[j * lah_shape1 * lah_shape2 + i * lah_shape2 + k] - options_rgb[j, i, k]
2021-01-21 23:17:55 +00:00
#print("qe=%f" % (quant_error[k]))
apply_one_line(pattern, xl, xr, i, dither_x_origin, &lah_image_rgb[j * lah_shape1 * lah_shape2], lah_shape2, quant_error)
2021-01-15 22:58:01 +00:00
cdef unsigned char bit4
2021-01-11 21:35:13 +00:00
cdef int best
cdef int best_error = 2**31-1
cdef int total_error
2021-01-15 22:58:01 +00:00
cdef long flat, dist
2021-01-11 22:19:41 +00:00
cdef long r, g, b
2021-01-15 22:18:25 +00:00
cdef (unsigned char)[:, ::1] distances = screen.palette.distances
2021-01-11 21:35:13 +00:00
for i in range(2**lookahead):
total_error = 0
for j in range(lookahead):
2021-01-11 22:19:41 +00:00
# Clip lah_image_rgb into 0..255 range to prepare for computing colour distance
2021-01-12 10:21:31 +00:00
r = <long>clip(lah_image_rgb[i * lah_shape1 * lah_shape2 + j * lah_shape2 + 0], 0, 255)
g = <long>clip(lah_image_rgb[i * lah_shape1 * lah_shape2 + j * lah_shape2 + 1], 0, 255)
b = <long>clip(lah_image_rgb[i * lah_shape1 * lah_shape2 + j * lah_shape2 + 2], 0, 255)
2021-01-11 22:19:41 +00:00
flat = (r << 16) + (g << 8) + b
2021-01-21 23:17:55 +00:00
# print("%f, r=%d, g=%d, b=%d, flat=%d" % (lah_image_rgb[i * lah_shape1 * lah_shape2 + j * lah_shape2 + 2], r,g,b,flat))
2021-01-11 22:19:41 +00:00
bit4 = options_4bit[i, j]
dist = distances[flat, bit4]
2021-01-21 23:17:55 +00:00
total_error += <long>dist ** 2 # * (j+1)
#if total_error >= best_error:
# break
#print("total_error %d %d" % (i, total_error))
2021-01-11 21:35:13 +00:00
if total_error < best_error:
best_error = total_error
best = i
2021-01-21 23:17:55 +00:00
#print("best=%d" % best)
2021-01-12 10:21:31 +00:00
free(lah_image_rgb)
2021-01-11 23:41:04 +00:00
return options_4bit[best, 0], options_rgb[best, 0, :]
2021-01-12 10:00:56 +00:00
@functools.lru_cache(None)
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.float32)
for i in range(2 ** lookahead):
output_pixel_4bit = last_pixel_4bit
for j in range(lookahead):
xx = x + j
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] = output_pixel_4bit
2021-01-12 22:22:37 +00:00
options_rgb[i, j, :] = output_pixel_rgb
2021-01-12 10:00:56 +00:00
return options_4bit, options_rgb
2021-01-15 22:58:01 +00:00
@cython.boundscheck(False)
@cython.wraparound(False)
def find_nearest_colour(screen, float[::1] pixel_rgb, unsigned char[::1] options_4bit, unsigned char[:, ::1] options_rgb):
cdef int best, dist
cdef unsigned char bit4
cdef int best_dist = 2**8
cdef long flat
cdef (unsigned char)[:, ::1] distances = screen.palette.distances
for i in range(options_4bit.shape[0]):
flat = (<long>pixel_rgb[0] << 16) + (<long>pixel_rgb[1] << 8) + <long>pixel_rgb[2]
bit4 = options_4bit[i]
dist = distances[flat, bit4]
if dist < best_dist:
best_dist = dist
best = i
return options_4bit[best], options_rgb[best, :]
2021-01-12 10:00:56 +00:00
@cython.boundscheck(False)
@cython.wraparound(False)
def dither_image(
2021-01-15 22:18:25 +00:00
screen, float[:, :, ::1] image_rgb, dither, int lookahead):
2021-01-12 22:01:49 +00:00
cdef (unsigned char)[:, ::1] image_4bit = np.empty(
2021-01-12 10:00:56 +00:00
(image_rgb.shape[0], image_rgb.shape[1]), dtype=np.uint8)
cdef int yres = screen.Y_RES
cdef int xres = screen.X_RES
cdef int y, x, i
cdef float[3] quant_error
cdef (unsigned char)[:, ::1] options_4bit
cdef float[:, :, ::1] options_rgb
2021-01-12 22:01:49 +00:00
cdef unsigned char output_pixel_4bit
cdef float[::1] input_pixel_rgb
2021-01-12 10:00:56 +00:00
for y in range(yres):
2021-01-21 23:17:55 +00:00
#print(y)
2021-01-12 22:01:49 +00:00
output_pixel_4bit = 0
2021-01-12 10:00:56 +00:00
for x in range(xres):
input_pixel_rgb = image_rgb[y, x, :]
2021-01-21 23:17:55 +00:00
#for i in range(3):
#print("Input %f" % input_pixel_rgb[i])
2021-01-15 22:58:01 +00:00
if lookahead:
palette_choices_4bit, palette_choices_rgb = lookahead_options(
screen, lookahead, output_pixel_4bit, x % 4)
output_pixel_4bit, output_pixel_rgb = \
dither_lookahead(
screen, image_rgb, dither, x, y, palette_choices_4bit,
palette_choices_rgb, lookahead)
else:
palette_choices_4bit, palette_choices_rgb = screen.pixel_palette_options(output_pixel_4bit, x)
output_pixel_4bit, output_pixel_rgb = \
find_nearest_colour(screen, input_pixel_rgb, palette_choices_4bit, palette_choices_rgb)
2021-01-12 10:00:56 +00:00
for i in range(3):
quant_error[i] = input_pixel_rgb[i] - output_pixel_rgb[i]
2021-01-21 23:17:55 +00:00
#print("Input2 %f" % input_pixel_rgb[i])
#print("Output %f" % output_pixel_rgb[i])
#print("QE %f" % quant_error[i])
# XXX dither channels independently
2021-01-12 10:00:56 +00:00
image_4bit[y, x] = output_pixel_4bit
apply(dither, screen, x, y, image_rgb, quant_error)
2021-01-21 23:17:55 +00:00
for i in range(3):
# print(output_pixel_rgb[i])
image_rgb[y, x, i] = output_pixel_rgb[i]
2021-01-12 10:00:56 +00:00
2021-01-12 22:22:37 +00:00
return image_4bit, np.array(image_rgb)