#!/usr/bin/env python3 """Generate independent PDFium/Poppler comparison evidence. Requires the built pdf-worker-evidence target, Pillow and Poppler utils. The result is comparison evidence; it is not a human-approved golden image. """ import argparse import hashlib import json import os from pathlib import Path import shutil import subprocess from PIL import Image, ImageChops, ImageDraw root = Path(__file__).resolve().parent.parent parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--build-dir', type=Path, default=root / os.environ.get('DOCVIEW_BUILD_DIR', 'build')) parser.add_argument('--output', type=Path, default=root / 'tests/results/pdf') args = parser.parse_args() build = args.build_dir.resolve() out = args.output.resolve() out.mkdir(parents=True, exist_ok=True) helper = build / 'pdf-worker-evidence' if not helper.is_file(): raise SystemExit('Build the pdf-worker-evidence target before comparison.') fixture = root / 'tests/fixtures/pdf/navigation.pdf' subprocess.run([str(helper), str(fixture), str(out / 'worker')], check=True) shutil.copyfile(out / 'worker/page-1.png', out / 'pdfium.png') poppler = '/usr/bin/pdftoppm' if Path('/usr/bin/pdftoppm').exists() else shutil.which('pdftoppm') if not poppler: raise SystemExit('Poppler pdftoppm is required for the independent comparison.') subprocess.run([poppler, '-cropbox', '-f', '1', '-singlefile', '-r', '144', '-png', str(fixture), str(out / 'poppler')], check=True) a = Image.open(out / 'pdfium.png').convert('RGB') b = Image.open(out / 'poppler.png').convert('RGB') if a.size != b.size: raise SystemExit('Renderer dimensions disagree.') diff = ImageChops.difference(a, b) diff.save(out / 'difference.png') pixels = list(diff.getdata()); count = len(pixels) metrics = {'transport': 'production sandboxed PDF worker and FontBroker', 'worker_sha256': hashlib.sha256((build / 'docview-pdf-worker').read_bytes()).hexdigest(), 'broker_sha256': hashlib.sha256((build / 'libdocview_font_broker.a').read_bytes()).hexdigest(), 'fixture_sha256': hashlib.sha256(fixture.read_bytes()).hexdigest(), 'size': list(a.size), 'pixels': count, 'equal_pixels': sum(max(p) == 0 for p in pixels), 'pixels_max_delta_gt_16': sum(max(p) > 16 for p in pixels), 'mean_absolute_channel_error': sum(sum(p) for p in pixels) / (count * 3), 'reference_status': 'independent Poppler comparison, not human-approved golden image'} (out / 'comparison.json').write_text(json.dumps(metrics, indent=2) + '\n') canvas = Image.new('RGB', (1200, 430), '#eeeeee'); draw = ImageDraw.Draw(canvas) for x, label, image in [(0, 'PDFium 155.0.8057.0', a), (400, 'Poppler independent renderer', b), (800, 'Absolute RGB difference', diff)]: draw.text((x + 10, 9), label, fill='black'); canvas.paste(image, (x, 30)) canvas.save(out / 'comparison.png') print(json.dumps(metrics, indent=2))