merge() function tool and coverage improvement

This commit is contained in:
dutcu 2024-06-19 14:18:43 +02:00
parent 99dd55324d
commit 812925ed49
649 changed files with 146588 additions and 146522 deletions

View File

@ -18,7 +18,10 @@ exclude_also =
[run]
omit =
Tests/32bit_segfault_check.py
Tests/bench_cffi_access.py
Tests/check_*.py
Tests/createfontdatachunk.py
# Tests/32bit_segfault_check.py
# Tests/bench_cffi_access.py
# Tests/check_*.py
# Tests/createfontdatachunk.py
Tests/*
src/*

View File

@ -1087,3 +1087,6 @@ class TestRegistry:
def test_encode_registry_fail(self) -> None:
with pytest.raises(OSError):
Image._getencoder("RGB", "DoesNotExist", ("args",), extra=("extra",))

25
Tests/test_image_merge.py Normal file
View File

@ -0,0 +1,25 @@
import pytest
from PIL import Image
def test_merge_wrong_number_of_bands():
R = Image.new('L', (100, 100), color=255)
G = Image.new('L', (100, 100), color=128)
with pytest.raises(ValueError, match="wrong number of bands"):
Image.merge('RGB', [R, G])
def test_merge_mode_mismatch():
R = Image.new('L', (100, 100), color=255)
G = Image.new('L', (100, 100), color=128)
B = Image.new('1', (100, 100)) # Incorrect mode
with pytest.raises(ValueError, match="mode mismatch"):
Image.merge('RGB', [R, G, B])
def test_merge_size_mismatch():
R = Image.new('L', (100, 100), color=255)
G = Image.new('L', (200, 100), color=128) # Different size
B = Image.new('L', (100, 100), color=0)
with pytest.raises(ValueError, match="size mismatch"):
Image.merge('RGB', [R, G, B])

22
coveragetool.py Normal file
View File

@ -0,0 +1,22 @@
from PIL import Image
from PIL.Image import branches
# Define a function to calculate and print branch coverage
def calculate_branch_coverage():
num_branches = len(branches)
branch_covered = {key: value for key, value in branches.items() if value is True}
sum_branches = len(branch_covered)
coverage = (sum_branches/num_branches) * 100
print(f"Branches covered: {sum_branches}")
print(f"Total branches: {num_branches}")
print("\nBRANCH COVERAGE:", coverage, "%\n")
R = Image.new('L', (100, 100), color=255)
G = Image.new('L', (100, 100), color=128)
B = Image.new('L', (100, 100), color=0)
merged_image = Image.merge('RGB', (R, G, B))
merged_image.save('merged_image.png')
calculate_branch_coverage()

BIN
merged_image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 289 B

View File

@ -75,6 +75,14 @@ class DecompressionBombWarning(RuntimeWarning):
class DecompressionBombError(Exception):
pass
branches = {
"1": False,
"2": False,
"3": False,
"4": False,
"5": False,
}
# Limit to around a quarter gigabyte for a 24-bit (3 bpp) image
MAX_IMAGE_PIXELS: int | None = int(1024 * 1024 * 1024 // 4 // 3)
@ -3521,16 +3529,21 @@ def merge(mode: str, bands: Sequence[Image]) -> Image:
"""
if getmodebands(mode) != len(bands) or "*" in mode:
branches["1"] = True
msg = "wrong number of bands"
raise ValueError(msg)
for band in bands[1:]:
branches["2"] = True
if band.mode != getmodetype(mode):
branches["3"] = True
msg = "mode mismatch"
raise ValueError(msg)
if band.size != bands[0].size:
branches["4"] = True
msg = "size mismatch"
raise ValueError(msg)
for band in bands:
branches["5"] = True
band.load()
return bands[0]._new(core.merge(mode, *[b.im for b in bands]))