From a526e3b23b674a5cbeef5f4040d9683c460c176c Mon Sep 17 00:00:00 2001 From: "Jorj X. McKie" Date: Thu, 5 Sep 2024 15:32:23 -0400 Subject: [PATCH] Address 3848 --- src/__init__.py | 9 ++++++--- src/utils.py | 3 +++ tests/test_pixmap.py | 10 ++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/__init__.py b/src/__init__.py index 2ee9bd6ff..6fb8356a5 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -10021,11 +10021,14 @@ def color_count(self, colors=0, clip=None): def color_topusage(self, clip=None): """Return most frequent color and its usage ratio.""" + if clip is not None: + clip = IRect(self.irect) & clip + if clip.is_empty: + raise ValueError("clip must not be empty") allpixels = 0 cnt = 0 - if clip is not None and self.irect in Rect(clip): - clip = self.irect - for pixel, count in self.color_count(colors=True,clip=clip).items(): + + for pixel, count in self.color_count(colors=True, clip=clip).items(): allpixels += count if count > cnt: cnt = count diff --git a/src/utils.py b/src/utils.py index 670403d92..aa8d44853 100644 --- a/src/utils.py +++ b/src/utils.py @@ -881,6 +881,9 @@ def get_pixmap( if colorspace.n not in (1, 3, 4): raise ValueError("unsupported colorspace") + if clip is not None and pymupdf.IRect(clip).is_empty: + raise ValueError("clip must not be empty") + dl = page.get_displaylist(annots=annots) pix = dl.get_pixmap(matrix=matrix, colorspace=colorspace, alpha=alpha, clip=clip) dl = None diff --git a/tests/test_pixmap.py b/tests/test_pixmap.py index c456f05fc..7231d81b6 100644 --- a/tests/test_pixmap.py +++ b/tests/test_pixmap.py @@ -264,6 +264,16 @@ def test_3177(): pixmap = pymupdf.Pixmap(path) pixmap2 = pymupdf.Pixmap(None, pixmap) +def test_3848(): + path = os.path.abspath(f"{__file__}/../../tests/resources/img-transparent.png") + pix = pymupdf.Pixmap(path) + error = False + try: + top = pix.color_topusage(clip=(0, 0, 0, 0)) + except ValueError: + error = True + assert error + def test_3493(): '''