From 61887e9ee1a108c26cdd4b2d67fc4095d62b6e9d Mon Sep 17 00:00:00 2001 From: jaseg Date: Fri, 4 Jun 2021 23:28:36 +0200 Subject: Add & fix vectorizer tests --- svg-flatten/src/test/svg_tests.py | 60 ++++++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 11 deletions(-) (limited to 'svg-flatten/src/test') diff --git a/svg-flatten/src/test/svg_tests.py b/svg-flatten/src/test/svg_tests.py index c457ed4..48ee8fd 100644 --- a/svg-flatten/src/test/svg_tests.py +++ b/svg-flatten/src/test/svg_tests.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import tempfile +import shutil import unittest from pathlib import Path import subprocess @@ -9,7 +10,7 @@ import os from PIL import Image import numpy as np -def run_svg_flatten(input_file, output_file, **kwargs): +def run_svg_flatten(input_file, output_file, *args, **kwargs): if 'SVG_FLATTEN' in os.environ: svg_flatten = os.environ.get('SVG_FLATTEN') elif (Path(__file__) / '../../build/svg-flatten').is_file(): @@ -19,9 +20,15 @@ def run_svg_flatten(input_file, output_file, **kwargs): else: svg_flatten = 'svg-flatten' - args = [ svg_flatten, - *(arg for (key, value) in kwargs.items() for arg in (f'--{key.replace("_", "-")}', value)), - str(input_file), str(output_file) ] + args = [ svg_flatten ] + for key, value in kwargs.items(): + key = '--' + key.replace("_", "-") + args.append(key) + + if type(value) is not bool: + args.append(value) + args.append(str(input_file)) + args.append(str(output_file)) try: proc = subprocess.run(args, capture_output=True, check=True) @@ -49,6 +56,11 @@ class SVGRoundTripTests(unittest.TestCase): # Both are expected and OK. 'stroke_dashes_comparison': 0.03, 'stroke_dashes': 0.05, + # The vectorizer tests produce output with lots of edges, which leads to a large amount of aliasing artifacts. + 'vectorizer_simple': 0.05, + 'vectorizer_clip': 0.05, + 'vectorizer_xform': 0.05, + 'vectorizer_xform_clip': 0.05, } # Force use of rsvg-convert instead of resvg for these test cases @@ -60,9 +72,21 @@ class SVGRoundTripTests(unittest.TestCase): 'pattern_stroke_dashed' } - def compare_images(self, reference, output, test_name, mean, rsvg_workaround=False): - ref = np.array(Image.open(reference)) - out = np.array(Image.open(output)) + def compare_images(self, reference, output, test_name, mean, vectorizer_test=False, rsvg_workaround=False): + ref, out = Image.open(reference), Image.open(output) + + if vectorizer_test: + target_size = (100, 100) + ref.thumbnail(target_size, Image.ANTIALIAS) + out.thumbnail(target_size, Image.ANTIALIAS) + ref, out = np.array(ref), np.array(out) + + else: + ref, out = np.array(ref), np.array(out) + + ref, out = ref.astype(float).mean(axis=2), out.astype(float).mean(axis=2) + + if rsvg_workaround: # For some stupid reason, rsvg-convert does not actually output black as in "black" pixels when asked to. # Instead, it outputs #010101. We fix this in post here. @@ -86,9 +110,24 @@ class SVGRoundTripTests(unittest.TestCase): tempfile.NamedTemporaryFile(suffix='.png') as tmp_out_png,\ tempfile.NamedTemporaryFile(suffix='.png') as tmp_in_png: - run_svg_flatten(test_in_svg, tmp_out_svg.name, format='svg') - use_rsvg = test_in_svg.stem in SVGRoundTripTests.rsvg_override + vectorizer_test = test_in_svg.stem.startswith('vectorizer') + contours_test = test_in_svg.stem.startswith('contours') + + if not vectorizer_test: + run_svg_flatten(test_in_svg, tmp_out_svg.name, format='svg') + + else: + run_svg_flatten(test_in_svg, tmp_out_svg.name, format='svg', + svg_white_is_gerber_dark=True, + clear_color='black', dark_color='white') + + if contours_test: + run_svg_flatten(test_in_svg, tmp_out_svg.name, + clear_color='black', dark_color='white', + svg_white_is_gerber_dark=True, + format='svg', + vectorizer='binary-contours') if not use_rsvg: # default! subprocess.run(['resvg', tmp_out_svg.name, tmp_out_png.name], check=True, stdout=subprocess.DEVNULL) @@ -101,10 +140,9 @@ class SVGRoundTripTests(unittest.TestCase): try: self.compare_images(tmp_in_png, tmp_out_png, test_in_svg.stem, SVGRoundTripTests.test_mean_overrides.get(test_in_svg.stem, SVGRoundTripTests.test_mean_default), - rsvg_workaround=use_rsvg) + vectorizer_test, rsvg_workaround=use_rsvg) except AssertionError as e: - import shutil shutil.copyfile(tmp_in_png.name, f'/tmp/gerbolyze-fail-{test_in_svg.stem}-in.png') shutil.copyfile(tmp_out_png.name, f'/tmp/gerbolyze-fail-{test_in_svg.stem}-out.png') foo = list(e.args) -- cgit