From ecd59d214a2be1163cd1260390e7b07c2f6f32c8 Mon Sep 17 00:00:00 2001 From: treeform Date: Sat, 7 May 2022 12:19:15 -0700 Subject: [PATCH] Better JPEG validation. --- .gitignore | 1 + tests/benchmark_jpeg.nim | 1 + tests/fuzz_jpeg.nim | 1 + tests/test_jpeg.nim | 1 + tests/validate_jpeg.nim | 61 ++++++++++++++++++++++++++++++++++++++-- 5 files changed, 63 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index c85394a..64c9d74 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ bindings/generated *.dSYM dump.txt tests/fileformats/jpeg/generated +tests/fileformats/jpeg/diff diff --git a/tests/benchmark_jpeg.nim b/tests/benchmark_jpeg.nim index 0558438..498e691 100644 --- a/tests/benchmark_jpeg.nim +++ b/tests/benchmark_jpeg.nim @@ -24,6 +24,7 @@ var files = @[ "tests/fileformats/jpeg/master/cat_4_2_2.jpg", "tests/fileformats/jpeg/master/cat_4_2_0.jpg", "tests/fileformats/jpeg/master/cat_4_1_1.jpg", + "tests/fileformats/jpeg/master/cat_4_2_0_progressive.jpg", "tests/fileformats/jpeg/master/cat_4_4_4_progressive.jpg", "tests/fileformats/jpeg/master/cat_restart_markers_5.jpg", "tests/fileformats/jpeg/master/cat_restart_markers_5_progressive.jpg", diff --git a/tests/fuzz_jpeg.nim b/tests/fuzz_jpeg.nim index 3f9d7a8..248ab56 100644 --- a/tests/fuzz_jpeg.nim +++ b/tests/fuzz_jpeg.nim @@ -26,6 +26,7 @@ var files = @[ "tests/fileformats/jpeg/master/cat_4_2_2.jpg", "tests/fileformats/jpeg/master/cat_4_2_0.jpg", "tests/fileformats/jpeg/master/cat_4_1_1.jpg", + "tests/fileformats/jpeg/master/cat_4_2_0_progressive.jpg", "tests/fileformats/jpeg/master/cat_4_4_4_progressive.jpg", "tests/fileformats/jpeg/master/cat_restart_markers_5.jpg", "tests/fileformats/jpeg/master/cat_restart_markers_5_progressive.jpg", diff --git a/tests/test_jpeg.nim b/tests/test_jpeg.nim index de65564..93b8e45 100644 --- a/tests/test_jpeg.nim +++ b/tests/test_jpeg.nim @@ -26,6 +26,7 @@ var files = @[ "tests/fileformats/jpeg/master/cat_4_2_2.jpg", "tests/fileformats/jpeg/master/cat_4_2_0.jpg", "tests/fileformats/jpeg/master/cat_4_1_1.jpg", + "tests/fileformats/jpeg/master/cat_4_2_0_progressive.jpg", "tests/fileformats/jpeg/master/cat_4_4_4_progressive.jpg", "tests/fileformats/jpeg/master/cat_restart_markers_5.jpg", "tests/fileformats/jpeg/master/cat_restart_markers_5_progressive.jpg", diff --git a/tests/validate_jpeg.nim b/tests/validate_jpeg.nim index b37d018..f3f5647 100644 --- a/tests/validate_jpeg.nim +++ b/tests/validate_jpeg.nim @@ -1,3 +1,60 @@ -import pixie/fileformats/jpg, pixie/fileformats/stb_image/stb_image +import pixie, strutils, os, strformat -let original = readFile("tests/fileformats/jpeg/jpeg420exif.jpg") +import pixie/fileformats/jpg + +var files = @[ + "tests/fileformats/jpeg/master/red.jpg", + "tests/fileformats/jpeg/master/green.jpg", + "tests/fileformats/jpeg/master/blue.jpg", + "tests/fileformats/jpeg/master/white.jpg", + "tests/fileformats/jpeg/master/black.jpg", + + "tests/fileformats/jpeg/master/8x8.jpg", + "tests/fileformats/jpeg/master/8x8_progressive.jpg", + + "tests/fileformats/jpeg/master/16x16.jpg", + "tests/fileformats/jpeg/master/16x16_progressive.jpg", + + "tests/fileformats/jpeg/master/quality_01.jpg", + "tests/fileformats/jpeg/master/quality_10.jpg", + "tests/fileformats/jpeg/master/quality_25.jpg", + "tests/fileformats/jpeg/master/quality_50.jpg", + "tests/fileformats/jpeg/master/quality_100.jpg", + + "tests/fileformats/jpeg/master/cat_4_4_4.jpg", + "tests/fileformats/jpeg/master/cat_4_4_4.jpg", + "tests/fileformats/jpeg/master/cat_4_2_2.jpg", + "tests/fileformats/jpeg/master/cat_4_2_0.jpg", + "tests/fileformats/jpeg/master/cat_4_1_1.jpg", + "tests/fileformats/jpeg/master/cat_4_2_0_progressive.jpg", + "tests/fileformats/jpeg/master/cat_4_4_4_progressive.jpg", + "tests/fileformats/jpeg/master/cat_restart_markers_5.jpg", + "tests/fileformats/jpeg/master/cat_restart_markers_5_progressive.jpg", + + "tests/fileformats/jpeg/master/mandrill.jpg", + "tests/fileformats/jpeg/master/exif_overrun.jpg", + "tests/fileformats/jpeg/master/grayscale_test.jpg", + "tests/fileformats/jpeg/master/progressive.jpg", + + "tests/fileformats/jpeg/master/testimg.jpg", + "tests/fileformats/jpeg/master/testimgp.jpg", + "tests/fileformats/jpeg/master/testorig.jpg", + "tests/fileformats/jpeg/master/testprog.jpg", +] + +for file in files: + var img = decodeJpg(readFile(file)) + + let genFile = file.replace("master", "generated").replace(".jpg", ".png") + let diffFile = file.replace("master", "diff").replace(".jpg", ".png") + + if execShellCmd(&"convert {file} {genFile}") != 0: + echo "fail" + + var img2 = readImage(genFile) + let (score, diff) = img2.diff(img) + diff.writeFile(diffFile) + + if score > 1: + echo "!!!!!!!!!!!!!! FAIL !!!!!!!!!!!!!" + echo &"{score:2.3f}% ... {file}"