2021-05-20 05:51:43 +02:00
|
|
|
import multiprocessing
|
2021-05-19 18:25:13 +02:00
|
|
|
from PIL import Image # type: ignore
|
2021-05-23 22:37:18 +02:00
|
|
|
from typing import Optional, Tuple
|
2021-05-19 18:25:13 +02:00
|
|
|
|
2021-08-09 21:09:00 +02:00
|
|
|
from ..types import Color, Matrix, Point, AAMode
|
2021-08-05 19:32:00 +02:00
|
|
|
from .perspective import perspective_calculate
|
2021-05-19 18:25:13 +02:00
|
|
|
|
|
|
|
cdef extern struct floatcolor_t:
|
2021-08-11 01:36:18 +02:00
|
|
|
double r;
|
|
|
|
double g;
|
|
|
|
double b;
|
|
|
|
double a;
|
2021-05-19 18:25:13 +02:00
|
|
|
|
|
|
|
cdef extern struct matrix_t:
|
2021-08-11 01:36:18 +02:00
|
|
|
double a11;
|
|
|
|
double a12;
|
|
|
|
double a13;
|
|
|
|
double a21;
|
|
|
|
double a22;
|
|
|
|
double a23;
|
|
|
|
double a31;
|
|
|
|
double a32;
|
|
|
|
double a33;
|
|
|
|
double a41;
|
|
|
|
double a42;
|
|
|
|
double a43;
|
2021-05-19 18:25:13 +02:00
|
|
|
|
2021-08-05 19:32:00 +02:00
|
|
|
cdef extern int composite_fast(
|
|
|
|
unsigned char *imgbytes,
|
|
|
|
unsigned char *maskbytes,
|
2021-05-19 18:25:13 +02:00
|
|
|
unsigned int imgwidth,
|
|
|
|
unsigned int imgheight,
|
|
|
|
unsigned int minx,
|
|
|
|
unsigned int maxx,
|
|
|
|
unsigned int miny,
|
|
|
|
unsigned int maxy,
|
2021-05-22 03:31:42 +02:00
|
|
|
floatcolor_t add_color,
|
2021-05-19 18:25:13 +02:00
|
|
|
floatcolor_t mult_color,
|
2021-08-11 01:36:18 +02:00
|
|
|
double xscale,
|
|
|
|
double yscale,
|
2021-05-19 18:25:13 +02:00
|
|
|
matrix_t inverse,
|
2021-08-05 19:32:00 +02:00
|
|
|
int use_perspective,
|
2021-08-03 19:04:20 +02:00
|
|
|
int blendfunc,
|
|
|
|
unsigned char *texbytes,
|
|
|
|
unsigned int texwidth,
|
|
|
|
unsigned int texheight,
|
|
|
|
unsigned int threads,
|
2021-08-09 21:09:00 +02:00
|
|
|
unsigned int aa_mode
|
2021-08-03 19:04:20 +02:00
|
|
|
)
|
|
|
|
|
2021-05-19 18:25:13 +02:00
|
|
|
def affine_composite(
|
|
|
|
img: Image.Image,
|
2021-05-22 03:31:42 +02:00
|
|
|
add_color: Color,
|
2021-05-19 18:25:13 +02:00
|
|
|
mult_color: Color,
|
|
|
|
transform: Matrix,
|
2021-05-23 22:37:18 +02:00
|
|
|
mask: Optional[Image.Image],
|
2021-05-19 18:25:13 +02:00
|
|
|
blendfunc: int,
|
|
|
|
texture: Image.Image,
|
|
|
|
single_threaded: bool = False,
|
2021-08-09 21:09:00 +02:00
|
|
|
aa_mode: int = AAMode.SSAA_OR_BILINEAR,
|
2021-05-19 18:25:13 +02:00
|
|
|
) -> Image.Image:
|
2021-09-20 20:22:56 +02:00
|
|
|
if blendfunc not in {0, 1, 2, 3, 8, 9, 13, 70, 256, 257}:
|
|
|
|
print(f"WARNING: Unsupported blend {blendfunc}")
|
|
|
|
return img
|
|
|
|
|
2021-05-19 18:25:13 +02:00
|
|
|
# Calculate the inverse so we can map canvas space back to texture space.
|
|
|
|
try:
|
|
|
|
inverse = transform.inverse()
|
|
|
|
except ZeroDivisionError:
|
|
|
|
# If this happens, that means one of the scaling factors was zero, making
|
|
|
|
# this object invisible. We can ignore this since the object should not
|
|
|
|
# be drawn.
|
|
|
|
return img
|
|
|
|
|
|
|
|
# These are calculated properties and caching them outside of the loop
|
|
|
|
# speeds things up a bit.
|
|
|
|
imgwidth = img.width
|
|
|
|
imgheight = img.height
|
|
|
|
texwidth = texture.width
|
|
|
|
texheight = texture.height
|
|
|
|
|
|
|
|
# Calculate the maximum range of update this texture can possibly reside in.
|
2021-05-21 23:32:02 +02:00
|
|
|
pix1 = transform.multiply_point(Point.identity())
|
|
|
|
pix2 = transform.multiply_point(Point.identity().add(Point(texwidth, 0)))
|
|
|
|
pix3 = transform.multiply_point(Point.identity().add(Point(0, texheight)))
|
|
|
|
pix4 = transform.multiply_point(Point.identity().add(Point(texwidth, texheight)))
|
2021-05-19 18:25:13 +02:00
|
|
|
|
|
|
|
# Map this to the rectangle we need to sweep in the rendering image.
|
|
|
|
minx = max(int(min(pix1.x, pix2.x, pix3.x, pix4.x)), 0)
|
|
|
|
maxx = min(int(max(pix1.x, pix2.x, pix3.x, pix4.x)) + 1, imgwidth)
|
|
|
|
miny = max(int(min(pix1.y, pix2.y, pix3.y, pix4.y)), 0)
|
|
|
|
maxy = min(int(max(pix1.y, pix2.y, pix3.y, pix4.y)) + 1, imgheight)
|
|
|
|
|
2021-05-20 05:51:43 +02:00
|
|
|
if maxx <= minx or maxy <= miny:
|
2021-05-19 18:25:13 +02:00
|
|
|
# This image is entirely off the screen!
|
|
|
|
return img
|
|
|
|
|
|
|
|
# Grab the raw image data.
|
|
|
|
imgbytes = img.tobytes('raw', 'RGBA')
|
|
|
|
texbytes = texture.tobytes('raw', 'RGBA')
|
|
|
|
|
2021-05-23 22:37:18 +02:00
|
|
|
# Grab the mask data.
|
|
|
|
if mask is not None:
|
|
|
|
alpha = mask.split()[-1]
|
|
|
|
maskdata = alpha.tobytes('raw', 'L')
|
|
|
|
else:
|
|
|
|
maskdata = None
|
|
|
|
cdef unsigned char *maskbytes = NULL
|
|
|
|
if maskdata is not None:
|
|
|
|
maskbytes = maskdata
|
|
|
|
|
2021-05-19 18:25:13 +02:00
|
|
|
# Convert classes to C structs.
|
2021-05-22 03:31:42 +02:00
|
|
|
cdef floatcolor_t c_addcolor = floatcolor_t(r=add_color.r, g=add_color.g, b=add_color.b, a=add_color.a)
|
2021-05-19 18:25:13 +02:00
|
|
|
cdef floatcolor_t c_multcolor = floatcolor_t(r=mult_color.r, g=mult_color.g, b=mult_color.b, a=mult_color.a)
|
2021-07-06 23:58:32 +02:00
|
|
|
cdef matrix_t c_inverse = matrix_t(
|
|
|
|
a11=inverse.a11, a12=inverse.a12, a13=inverse.a13,
|
|
|
|
a21=inverse.a21, a22=inverse.a22, a23=inverse.a23,
|
|
|
|
a31=inverse.a31, a32=inverse.a32, a33=inverse.a33,
|
|
|
|
a41=inverse.a41, a42=inverse.a42, a43=inverse.a43,
|
|
|
|
)
|
2021-05-20 05:51:43 +02:00
|
|
|
cdef unsigned int threads = 1 if single_threaded else multiprocessing.cpu_count()
|
2021-05-19 18:25:13 +02:00
|
|
|
|
|
|
|
# Call the C++ function.
|
2021-08-05 19:32:00 +02:00
|
|
|
errors = composite_fast(
|
2021-05-19 18:25:13 +02:00
|
|
|
imgbytes,
|
2021-05-23 22:37:18 +02:00
|
|
|
maskbytes,
|
2021-05-19 18:25:13 +02:00
|
|
|
imgwidth,
|
|
|
|
imgheight,
|
|
|
|
minx,
|
|
|
|
maxx,
|
|
|
|
miny,
|
|
|
|
maxy,
|
|
|
|
c_addcolor,
|
|
|
|
c_multcolor,
|
2021-08-05 19:32:00 +02:00
|
|
|
transform.xscale,
|
|
|
|
transform.yscale,
|
2021-05-19 18:25:13 +02:00
|
|
|
c_inverse,
|
2021-08-05 19:32:00 +02:00
|
|
|
0,
|
2021-05-19 18:25:13 +02:00
|
|
|
blendfunc,
|
|
|
|
texbytes,
|
|
|
|
texwidth,
|
|
|
|
texheight,
|
2021-05-20 05:51:43 +02:00
|
|
|
threads,
|
2021-08-09 21:09:00 +02:00
|
|
|
aa_mode,
|
2021-05-19 18:25:13 +02:00
|
|
|
)
|
|
|
|
if errors != 0:
|
|
|
|
raise Exception("Error raised in C++!")
|
|
|
|
|
2021-05-31 20:13:43 +02:00
|
|
|
# We blitted in-place, return that. There seems to be a reference bug in Cython
|
|
|
|
# when called from compiled mypyc code, so if we don't assign to a local variable
|
|
|
|
# first this function appears to return None.
|
|
|
|
img = Image.frombytes('RGBA', (imgwidth, imgheight), imgbytes)
|
|
|
|
return img
|
2021-08-03 19:04:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
def perspective_composite(
|
|
|
|
img: Image.Image,
|
|
|
|
add_color: Color,
|
|
|
|
mult_color: Color,
|
|
|
|
transform: Matrix,
|
|
|
|
camera: Point,
|
|
|
|
focal_length: float,
|
|
|
|
mask: Optional[Image.Image],
|
|
|
|
blendfunc: int,
|
|
|
|
texture: Image.Image,
|
|
|
|
single_threaded: bool = False,
|
2021-08-09 21:09:00 +02:00
|
|
|
aa_mode: int = AAMode.SSAA_ONLY,
|
2021-08-03 19:04:20 +02:00
|
|
|
) -> Image.Image:
|
2021-09-20 20:22:56 +02:00
|
|
|
if blendfunc not in {0, 1, 2, 3, 8, 9, 13, 70, 256, 257}:
|
2021-08-03 19:04:20 +02:00
|
|
|
print(f"WARNING: Unsupported blend {blendfunc}")
|
|
|
|
return img
|
|
|
|
|
|
|
|
# These are calculated properties and caching them outside of the loop
|
|
|
|
# speeds things up a bit.
|
|
|
|
imgwidth = img.width
|
|
|
|
imgheight = img.height
|
|
|
|
texwidth = texture.width
|
|
|
|
texheight = texture.height
|
|
|
|
|
2021-08-05 19:32:00 +02:00
|
|
|
# Get the perspective-correct inverse matrix for looking up texture coordinates.
|
|
|
|
inverse_matrix, minx, miny, maxx, maxy = perspective_calculate(imgwidth, imgheight, texwidth, texheight, transform, camera, focal_length)
|
|
|
|
if inverse_matrix is None:
|
|
|
|
# This texture is entirely off of the screen.
|
|
|
|
return img
|
|
|
|
|
2021-08-03 19:04:20 +02:00
|
|
|
# Grab the raw image data.
|
|
|
|
imgbytes = img.tobytes('raw', 'RGBA')
|
|
|
|
texbytes = texture.tobytes('raw', 'RGBA')
|
|
|
|
|
|
|
|
# Grab the mask data.
|
|
|
|
if mask is not None:
|
|
|
|
alpha = mask.split()[-1]
|
|
|
|
maskdata = alpha.tobytes('raw', 'L')
|
|
|
|
else:
|
|
|
|
maskdata = None
|
|
|
|
cdef unsigned char *maskbytes = NULL
|
|
|
|
if maskdata is not None:
|
|
|
|
maskbytes = maskdata
|
|
|
|
|
|
|
|
# Convert classes to C structs.
|
|
|
|
cdef floatcolor_t c_addcolor = floatcolor_t(r=add_color.r, g=add_color.g, b=add_color.b, a=add_color.a)
|
|
|
|
cdef floatcolor_t c_multcolor = floatcolor_t(r=mult_color.r, g=mult_color.g, b=mult_color.b, a=mult_color.a)
|
2021-08-05 19:32:00 +02:00
|
|
|
cdef matrix_t c_inverse = matrix_t(
|
|
|
|
a11=inverse_matrix.a11, a12=inverse_matrix.a12, a13=inverse_matrix.a13,
|
|
|
|
a21=inverse_matrix.a21, a22=inverse_matrix.a22, a23=inverse_matrix.a23,
|
|
|
|
a31=inverse_matrix.a31, a32=inverse_matrix.a32, a33=inverse_matrix.a33,
|
|
|
|
a41=inverse_matrix.a41, a42=inverse_matrix.a42, a43=inverse_matrix.a43,
|
2021-08-03 19:04:20 +02:00
|
|
|
)
|
|
|
|
cdef unsigned int threads = 1 if single_threaded else multiprocessing.cpu_count()
|
|
|
|
|
|
|
|
# Call the C++ function.
|
2021-08-05 19:32:00 +02:00
|
|
|
errors = composite_fast(
|
2021-08-03 19:04:20 +02:00
|
|
|
imgbytes,
|
|
|
|
maskbytes,
|
|
|
|
imgwidth,
|
|
|
|
imgheight,
|
2021-08-05 19:32:00 +02:00
|
|
|
minx,
|
|
|
|
maxx,
|
|
|
|
miny,
|
|
|
|
maxy,
|
2021-08-03 19:04:20 +02:00
|
|
|
c_addcolor,
|
|
|
|
c_multcolor,
|
2021-08-05 19:32:00 +02:00
|
|
|
transform.xscale,
|
|
|
|
transform.yscale,
|
|
|
|
c_inverse,
|
|
|
|
1,
|
2021-08-03 19:04:20 +02:00
|
|
|
blendfunc,
|
|
|
|
texbytes,
|
|
|
|
texwidth,
|
|
|
|
texheight,
|
|
|
|
threads,
|
2021-08-09 21:09:00 +02:00
|
|
|
aa_mode,
|
2021-08-03 19:04:20 +02:00
|
|
|
)
|
|
|
|
if errors != 0:
|
|
|
|
raise Exception("Error raised in C++!")
|
|
|
|
|
|
|
|
# We blitted in-place, return that. There seems to be a reference bug in Cython
|
|
|
|
# when called from compiled mypyc code, so if we don't assign to a local variable
|
|
|
|
# first this function appears to return None.
|
|
|
|
img = Image.frombytes('RGBA', (imgwidth, imgheight), imgbytes)
|
|
|
|
return img
|