Skip to content

Commit

Permalink
meta: Added epsilon to image compare.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Sep 30, 2024
1 parent 58d4b05 commit 4e05fd1
Showing 1 changed file with 94 additions and 26 deletions.
120 changes: 94 additions & 26 deletions meta/plugins/reftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,61 @@ def buildPaperMuncher(args: model.TargetArgs) -> builder.ProductScope:
return builder.build(scope, component)[0]


def compareImages(
lhs: bytes,
rhs: bytes,
lowEpsilon: float = 0.05,
highEpsilon: float = 0.1,
strict=False,
) -> bool:
if strict:
return lhs == rhs

if len(lhs) != len(rhs):
return False

errorSum = 0
for i in range(len(lhs)):
diff = abs(lhs[i] - rhs[i]) / 255
if diff > highEpsilon:
print(f"Image rejected with diff = {diff}")
return False
errorSum += diff > lowEpsilon

if errorSum > len(lhs) // 100:
print(f"Image reject with errorSum = {errorSum}")
return False

return True


class RefTestArgs(model.TargetArgs):
glob: str = cli.arg("g", "glob")
fast: str = cli.arg(None, "fast", "Proceed to the next test as soon as an error occurs.")
fast: str = cli.arg(
None, "fast", "Proceed to the next test as soon as an error occurs."
)


@cli.command(None, "reftests", "Manage the reftests")
def _(args: RefTestArgs):
paperMuncher = buildPaperMuncher(args)

test_folder = Path(__file__).parent.parent.parent / 'tests'
test_tmp_folder = test_folder / '_local'
test_folder = Path(__file__).parent.parent.parent / "tests"
test_tmp_folder = test_folder / "_local"
test_tmp_folder.mkdir(parents=True, exist_ok=True)
# for temp in test_tmp_folder.glob('*.*'):
# temp.unlink()

temp_file = test_tmp_folder / 'reftest.xhtml'
temp_file = test_tmp_folder / "reftest.xhtml"

def update_temp_file(container, rendering):
# write xhtml into the temporary file
xhtml = re.sub(r"<slot\s*/>", rendering, container) if container else rendering
with temp_file.open("w") as f:
f.write(f"<!DOCTYPE html>\n{textwrap.dedent(xhtml)}")

REG_INFO = re.compile(r"""(\w+)=['"]([^'"]+)['"]""")

def getInfo(txt):
return {prop: value for prop, value in REG_INFO.findall(txt)}

Expand All @@ -61,19 +93,23 @@ def getInfo(txt):
container = search and search.group(1)

expected_xhtml = None
expected_image = None
expected_image: bytes | None = None
expected_image_url = test_tmp_folder / f"{temp_file_name}.expected.bmp"
if props.get('id'):
if props.get("id"):
ref_image = file.parent / f"{props.get('id')}.bmp"
if ref_image.exists():
with ref_image.open('rb') as f:
expected_image = f.read()
with expected_image_url.open("wb") as f:
f.write(expected_image)
with ref_image.open("rb") as imageReader:
expected_image = imageReader.read()

with expected_image_url.open("wb") as imageWriter:
imageWriter.write(expected_image)

expected_image_url = ref_image

num = 0
for tag, info, rendering in re.findall(r"""<(rendering|error)([^>]*)>([\w\W]+?)</(?:rendering|error)>""", test):
for tag, info, rendering in re.findall(
r"""<(rendering|error)([^>]*)>([\w\W]+?)</(?:rendering|error)>""", test
):
num += 1
renderingProps = getInfo(info)
if "skip" in renderingProps:
Expand All @@ -89,46 +125,78 @@ def getInfo(txt):
paperMuncher.popen("render", "-sdlpo", img_path, temp_file)
else:
size = props.get("size", "200")
paperMuncher.popen("render", "--width", size, "--height", size, "-sdlpo", img_path, temp_file)

with img_path.open("rb") as f:
output_image = f.read()
paperMuncher.popen(
"render",
"--width",
size,
"--height",
size,
"-sdlpo",
img_path,
temp_file,
)

with img_path.open("rb") as imageFile:
output_image: bytes = imageFile.read()

# the first template is the expected value
if not expected_xhtml:
expected_xhtml = rendering
if not expected_image:
expected_image = output_image
with (test_tmp_folder / f"{temp_file_name}.expected.bmp").open("wb") as f:
f.write(expected_image)
with (test_tmp_folder / f"{temp_file_name}.expected.bmp").open(
"wb"
) as imageWriter:
imageWriter.write(expected_image)
continue

# check if the rendering is different
if (expected_image == output_image) == (tag == "rendering"):
assert expected_image is not None
assert output_image is not None
if compareImages(expected_image, output_image) == (tag == "rendering"):
img_path.unlink()
print(f"{vt100.GREEN}Passed{vt100.RESET}")
else:
# generate temporary file for debugging
paperMuncher.popen("print", "-sdlpo", test_tmp_folder / f"{temp_file_name}-{num}.pdf", temp_file)
paperMuncher.popen(
"print",
"-sdlpo",
test_tmp_folder / f"{temp_file_name}-{num}.pdf",
temp_file,
)

help = renderingProps.get("help")
if tag == "error":
print(f"{vt100.RED}Failed {props.get('name')!r} (The result should be different){vt100.RESET}")
print(f"{vt100.WHITE}{expected_xhtml[1:].rstrip()}{vt100.RESET}")
print(
f"{vt100.RED}Failed {props.get('name')!r} (The result should be different){vt100.RESET}"
)
print(
f"{vt100.WHITE}{expected_xhtml[1:].rstrip()}{vt100.RESET}"
)
print(f"{vt100.WHITE}{expected_image_url}{vt100.RESET}")
print(f"{vt100.BLUE}{rendering[1:].rstrip()}{vt100.RESET}")
print(f"{vt100.BLUE}{test_tmp_folder / f'{temp_file_name}-{num}.pdf'}{vt100.RESET}")
print(f"{vt100.BLUE}{test_tmp_folder / f'{temp_file_name}-{num}.bmp'}{vt100.RESET}")
print(
f"{vt100.BLUE}{test_tmp_folder / f'{temp_file_name}-{num}.pdf'}{vt100.RESET}"
)
print(
f"{vt100.BLUE}{test_tmp_folder / f'{temp_file_name}-{num}.bmp'}{vt100.RESET}"
)
if help:
print(f"{vt100.BLUE}{help}{vt100.RESET}")
else:
print(f"{vt100.RED}Failed {props.get('name')!r}{vt100.RESET}")
if expected_xhtml != rendering:
print(f"{vt100.WHITE}{expected_xhtml[1:].rstrip()}{vt100.RESET}")
print(
f"{vt100.WHITE}{expected_xhtml[1:].rstrip()}{vt100.RESET}"
)
print(f"{vt100.WHITE}{expected_image_url}{vt100.RESET}")
print(f"{vt100.BLUE}{rendering[1:].rstrip()}{vt100.RESET}")
print(f"{vt100.BLUE}{test_tmp_folder / f'{temp_file_name}-{num}.pdf'}{vt100.RESET}")
print(f"{vt100.BLUE}{test_tmp_folder / f'{temp_file_name}-{num}.bmp'}{vt100.RESET}")
print(
f"{vt100.BLUE}{test_tmp_folder / f'{temp_file_name}-{num}.pdf'}{vt100.RESET}"
)
print(
f"{vt100.BLUE}{test_tmp_folder / f'{temp_file_name}-{num}.bmp'}{vt100.RESET}"
)
if help:
print(f"{vt100.BLUE}{help}{vt100.RESET}")

Expand Down

0 comments on commit 4e05fd1

Please sign in to comment.