Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,22 @@ def test_effect_spread_i16(self, mode: str) -> None:
im2 = im.effect_spread(2)
assert set(im2.get_flattened_data()) <= set(im.get_flattened_data())

@pytest.mark.parametrize("mode", ("I;16", "I;16L", "I;16B", "I;16N"))
def test_blend_i16(self, mode: str) -> None:
im1 = Image.new(mode, (2, 2), 10000)
im2 = Image.new(mode, (2, 2), 20000)

# Shortcuts
assert Image.blend(im1, im2, 0.0).getpixel((0, 0)) == 10000
assert Image.blend(im1, im2, 1.0).getpixel((0, 0)) == 20000

# Interpolation
assert Image.blend(im1, im2, 0.5).getpixel((0, 0)) == 15000

# Extrapolation with clipping
assert Image.blend(im1, im2, -2.0).getpixel((0, 0)) == 0
assert Image.blend(im1, im2, 6.0).getpixel((0, 0)) == 65535

def test_check_size(self) -> None:
# Checking that the _check_size function throws value errors when we want it to
with pytest.raises(ValueError):
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_imagechops.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from collections.abc import Callable

BLACK = (0, 0, 0)
BROWN = (127, 64, 0)
BROWN = (128, 64, 0)
CYAN = (0, 255, 255)
DARK_GREEN = (0, 128, 0)
GREEN = (0, 255, 0)
Expand Down
100 changes: 67 additions & 33 deletions src/libImaging/Blend.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ ImagingBlend(Imaging imIn1, Imaging imIn2, float alpha) {
int x, y;

/* Check arguments */
if (!imIn1 || !imIn2 || imIn1->type != IMAGING_TYPE_UINT8 || imIn1->palette ||
imIn1->mode == IMAGING_MODE_1 || imIn2->palette ||
imIn2->mode == IMAGING_MODE_1) {
if (!imIn1 || !imIn2 ||
(imIn1->type != IMAGING_TYPE_UINT8 && imIn1->type != IMAGING_TYPE_I16) ||
imIn1->palette || imIn1->mode == IMAGING_MODE_1) {
return ImagingError_ModeError();
}

if (imIn1->type != imIn2->type || imIn1->bands != imIn2->bands ||
imIn1->xsize != imIn2->xsize || imIn1->ysize != imIn2->ysize) {
if (imIn1->mode != imIn2->mode || imIn1->xsize != imIn2->xsize ||
imIn1->ysize != imIn2->ysize) {
return ImagingError_Mismatch();
}

Expand All @@ -52,37 +52,71 @@ ImagingBlend(Imaging imIn1, Imaging imIn2, float alpha) {
return NULL;
}

// We know these won't change in the loops below,
// so the compiler can optimize better if we pull them out.
int ysize = imIn1->ysize;
int linesize = imIn1->linesize;

if (alpha >= 0 && alpha <= 1.0) {
/* Interpolate between bands */
for (y = 0; y < ysize; y++) {
// restrict safe: imIn1 and imIn2 are read-only; imOut is a new allocation
UINT8 *restrict in1 = (UINT8 *)imIn1->image[y];
UINT8 *restrict in2 = (UINT8 *)imIn2->image[y];
UINT8 *restrict out = (UINT8 *)imOut->image[y];
for (x = 0; x < linesize; x++) {
out[x] = (UINT8)((int)in1[x] + alpha * ((int)in2[x] - (int)in1[x]));
if (imIn1->type == IMAGING_TYPE_UINT8) {
int linesize = imIn1->linesize;
if (alpha >= 0 && alpha <= 1.0) {
/* Interpolate between bands */
for (y = 0; y < ysize; y++) {
// restrict safe: imIn1 and imIn2 are read-only; imOut is a new
// allocation
UINT8 *restrict in1 = (UINT8 *)imIn1->image[y];
UINT8 *restrict in2 = (UINT8 *)imIn2->image[y];
UINT8 *restrict out = (UINT8 *)imOut->image[y];
for (x = 0; x < linesize; x++) {
out[x] = (UINT8)roundf(
(int)in1[x] + alpha * ((int)in2[x] - (int)in1[x])
);
}
}
} else {
/* Extrapolation; must make sure to clip resulting values */
for (y = 0; y < ysize; y++) {
// restrict safe: imIn1 and imIn2 are read-only; imOut is a new
// allocation
UINT8 *restrict in1 = (UINT8 *)imIn1->image[y];
UINT8 *restrict in2 = (UINT8 *)imIn2->image[y];
UINT8 *restrict out = (UINT8 *)imOut->image[y];
for (x = 0; x < linesize; x++) {
out[x] = (UINT8)CLIP8(
roundf((int)in1[x] + alpha * ((int)in2[x] - (int)in1[x]))
);
}
}
}
} else {
/* Extrapolation; must make sure to clip resulting values */
for (y = 0; y < ysize; y++) {
// restrict safe: imIn1 and imIn2 are read-only; imOut is a new allocation
UINT8 *restrict in1 = (UINT8 *)imIn1->image[y];
UINT8 *restrict in2 = (UINT8 *)imIn2->image[y];
UINT8 *restrict out = (UINT8 *)imOut->image[y];
for (x = 0; x < linesize; x++) {
float temp = (float)((int)in1[x] + alpha * ((int)in2[x] - (int)in1[x]));
if (temp <= 0.0) {
out[x] = 0;
} else if (temp >= 255.0) {
out[x] = 255;
} else {
out[x] = (UINT8)temp;
} else if (imIn1->type == IMAGING_TYPE_I16) {
int xsize = imIn1->xsize;
int bigendian = isImageI16BigEndian(imIn1);

if (alpha >= 0 && alpha <= 1.0) {
for (y = 0; y < ysize; y++) {
UINT8 *restrict in1 = (UINT8 *)imIn1->image[y];
UINT8 *restrict in2 = (UINT8 *)imIn2->image[y];
UINT8 *restrict out = (UINT8 *)imOut->image[y];
for (x = 0; x < xsize; x++) {
float v1 = (int)in1[x * 2 + (bigendian ? 1 : 0)] +
((int)in1[x * 2 + (bigendian ? 0 : 1)] << 8);
float v2 = (int)in2[x * 2 + (bigendian ? 1 : 0)] +
((int)in2[x * 2 + (bigendian ? 0 : 1)] << 8);
UINT16 v = roundf(v1 + alpha * (v2 - v1));
out[x * 2 + (bigendian ? 1 : 0)] = (UINT8)(v & 0xff);
out[x * 2 + (bigendian ? 0 : 1)] = (UINT8)(v >> 8);
}
}
} else {
/* Extrapolation; must make sure to clip resulting values */
for (y = 0; y < ysize; y++) {
UINT8 *restrict in1 = (UINT8 *)imIn1->image[y];
UINT8 *restrict in2 = (UINT8 *)imIn2->image[y];
UINT8 *restrict out = (UINT8 *)imOut->image[y];
for (x = 0; x < xsize; x++) {
float v1 = (int)in1[x * 2 + (bigendian ? 1 : 0)] +
((int)in1[x * 2 + (bigendian ? 0 : 1)] << 8);
float v2 = (int)in2[x * 2 + (bigendian ? 1 : 0)] +
((int)in2[x * 2 + (bigendian ? 0 : 1)] << 8);
UINT16 v = CLIP16(roundf(v1 + alpha * (v2 - v1)));
out[x * 2 + (bigendian ? 1 : 0)] = (UINT8)(v & 0xff);
out[x * 2 + (bigendian ? 0 : 1)] = (UINT8)(v >> 8);
}
}
}
Expand Down
Loading