Skip to content

Commit

Permalink
tests/gentle_compare.py: added pixmaps_rms().
Browse files Browse the repository at this point in the history
Does simple RMS comparison of two bitmaps.
  • Loading branch information
julian-smith-artifex-com committed Oct 1, 2024
1 parent e5efec4 commit 0b8b0ba
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tests/gentle_compare.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import math

import pymupdf


Expand All @@ -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.
<a> and <b> 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

0 comments on commit 0b8b0ba

Please sign in to comment.