From 0b8b0ba8cc993c8a3f53a25182b7a5a4be103005 Mon Sep 17 00:00:00 2001 From: Julian Smith Date: Tue, 1 Oct 2024 12:09:43 +0100 Subject: [PATCH] tests/gentle_compare.py: added pixmaps_rms(). Does simple RMS comparison of two bitmaps. --- tests/gentle_compare.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/gentle_compare.py b/tests/gentle_compare.py index 805f48797..b94010762 100644 --- a/tests/gentle_compare.py +++ b/tests/gentle_compare.py @@ -1,3 +1,5 @@ +import math + import pymupdf @@ -24,3 +26,29 @@ def gentle_compare(w0, w1): print(f"word {i}: rectangle mismatch {delta}") return False return True + + +def pixmaps_rms(a, b, out_prefix=''): + ''' + Returns RMS diff of raw bytes of two pixmaps. We assert that the pixmaps + are the same size. + and can each be a pymupdf.Pixmap or path of a bitmap file. + ''' + if isinstance(a, str): + print(f'{out_prefix}pixmaps_rms(): reading pixmap from {a=}.') + a = pymupdf.Pixmap(a) + if isinstance(b, str): + print(f'{out_prefix}pixmaps_rms(): reading pixmap from {b=}.') + b = pymupdf.Pixmap(b) + assert a.irect == b.irect, f'Differing rects: {a.irect=} {b.irect=}.' + a_mv = a.samples_mv + b_mv = b.samples_mv + assert len(a_mv) == len(b_mv) + e = 0 + for i, (a_byte, b_byte) in enumerate(zip(a_mv, b_mv)): + if i % 100000 == 0: + print(f'{out_prefix}compare_pixmaps(): {i=} {e=} {a_byte=} {b_byte=}.') + e += (a_byte - b_byte) ** 2 + rms = math.sqrt(e / len(a_mv)) + print(f'{out_prefix}compare_pixmaps(): {e=} {rms=}.') + return rms