Skip to content

Commit

Permalink
Update test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
mara004 committed Jan 15, 2025
1 parent 8b90ea2 commit 55d4048
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
1 change: 1 addition & 0 deletions src/pypdfium2/_cli/extract_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,4 @@ def main(args):
except pdfium.PdfiumError:
traceback.print_exc()
image.close()
logger.debug() # newline
2 changes: 1 addition & 1 deletion src/pypdfium2/_helpers/pageobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def get_bitmap(self, render=False, scale_to_original=True):

bitmap = PdfBitmap.from_raw(raw_bitmap)
if render and scale_to_original:
logger.debug(f"Extracted size: {bitmap.width}, {bitmap.height}\n")
logger.debug(f"Extracted size: {bitmap.width}, {bitmap.height}")

return bitmap

Expand Down
18 changes: 9 additions & 9 deletions tests/test_pageobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,10 @@ def test_replace_image_with_jpeg():


@pytest.mark.parametrize(
"render", [False, True]
"render, scale_to_original",
[(False, None), (True, False), (True, True)]
)
def test_image_get_bitmap(render):
def test_image_get_bitmap(render, scale_to_original):

pdf = pdfium.PdfDocument(TestFiles.images)
page = pdf[0]
Expand All @@ -210,25 +211,24 @@ def test_image_get_bitmap(render):
assert metadata.marked_content_id == 1
assert metadata.bits_per_pixel == 1

bitmap = image.get_bitmap(render=render)
bitmap = image.get_bitmap(render=render, scale_to_original=scale_to_original)
assert isinstance(bitmap, pdfium.PdfBitmap)

if render:
assert bitmap.format == pdfium_c.FPDFBitmap_BGRA
assert bitmap.n_channels == 4
# Somewhere between pdfium 6462 and 6899, size/stride expectation changed here
assert bitmap.width == 217
assert bitmap.height == 91
assert bitmap.stride == 868
if scale_to_original:
assert (bitmap.width, bitmap.height, bitmap.stride) == (115, 49, 460)
else:
assert (bitmap.width, bitmap.height, bitmap.stride) == (217, 91, 868)
assert bitmap.rev_byteorder is False
output_path = OutputDir / "extract_rendered.png"
else:
# NOTE fails with pdfium >= 1e1e173 (6015), < b5bc2e9 (6029), which returns RGB
assert bitmap.format == pdfium_c.FPDFBitmap_Gray
assert bitmap.n_channels == 1
assert bitmap.width == 115
assert bitmap.height == 48
assert bitmap.stride == 116
assert (bitmap.width, bitmap.height, bitmap.stride) == (115, 48, 116)
assert bitmap.rev_byteorder is False
output_path = OutputDir / "extract.png"

Expand Down
38 changes: 19 additions & 19 deletions tests/test_textpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,22 @@ def test_get_text_bounded_defaults_with_rotation():
assert len(text) == 438


@pytest.mark.parametrize("explicit_close", [False, True])
def test_autoclose_with_remove_obj(caplog, explicit_close):

pdf = pdfium.PdfDocument(TestFiles.text)
page = pdf[0]
textobj = next( page.get_objects(filter=[pdfium_c.FPDF_PAGEOBJ_TEXT]) )
assert len(page._kids) == 0
textpage = page.get_textpage()
assert len(page._kids) == 1

if explicit_close:
textpage.close()
with caplog.at_level(logging.WARNING):
page.remove_obj(textobj)

if explicit_close:
assert not caplog.text
else:
assert f"Removing text pageobbject implicitly closes affected textpage {textpage}." in caplog.text
# @pytest.mark.parametrize("explicit_close", [False, True])
# def test_autoclose_with_remove_obj(caplog, explicit_close):
#
# pdf = pdfium.PdfDocument(TestFiles.text)
# page = pdf[0]
# textobj = next( page.get_objects(filter=[pdfium_c.FPDF_PAGEOBJ_TEXT]) )
# assert len(page._kids) == 0
# textpage = page.get_textpage()
# assert len(page._kids) == 1
#
# if explicit_close:
# textpage.close()
# with caplog.at_level(logging.WARNING):
# page.remove_obj(textobj)
#
# if explicit_close:
# assert not caplog.text
# else:
# assert f"Removing text pageobbject implicitly closes affected textpage {textpage}." in caplog.text

0 comments on commit 55d4048

Please sign in to comment.