From 3bb5f7597749b140f0b87b3691d60ef9870e81f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Le=C5=9Bniak?= Date: Fri, 22 Nov 2024 09:26:18 +0100 Subject: [PATCH] feat: Make the diffPath optional (#112) * Make the diffPath optional * Update README * Fix running TIFF tests The incorrect equals were used: (==) where (=) was intended. * Introduce diffWithoutOutput --- README.md | 4 ++-- bin/Main.ml | 2 +- bin/ODiffBin.ml | 4 ++-- src/Diff.ml | 31 ++++++++++++++++++++++++------- test/Test_Core.ml | 4 ++++ test/Test_IO_BMP.ml | 4 ++++ test/Test_IO_JPG.ml | 4 ++++ test/Test_IO_PNG.ml | 4 ++++ test/Test_IO_TIFF.ml | 6 +++++- 9 files changed, 50 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 3b4b9e26..46863e69 100644 --- a/README.md +++ b/README.md @@ -48,10 +48,10 @@ ODiff is a blazing fast native image comparison tool. Check [benchmarks](#benchm ### Basic comparison -Run the simple comparison. Image paths can be one of supported formats, diff output can only be `.png`. +Run the simple comparison. Image paths can be one of supported formats, diff output is optional and can only be `.png`. ``` -odiff +odiff [DIFF output path] ``` ### Node.js diff --git a/bin/Main.ml b/bin/Main.ml index b1896d88..afe820f4 100644 --- a/bin/Main.ml +++ b/bin/Main.ml @@ -60,7 +60,7 @@ let main img1Path img2Path diffPath threshold outputDiffMask failOnLayoutChange -> { exitCode = 0; diff = Some diffOutput } | Pixel (diffOutput, diffCount, diffPercentage, _) -> - IO1.saveImage diffOutput diffPath; + diffPath |> Option.iter (IO1.saveImage diffOutput); { exitCode = 22; diff = Some diffOutput } in IO1.freeImage img1; diff --git a/bin/ODiffBin.ml b/bin/ODiffBin.ml index c364346c..7c18bfcd 100644 --- a/bin/ODiffBin.ml +++ b/bin/ODiffBin.ml @@ -3,8 +3,8 @@ open Term open Arg let diffPath = - value & pos 2 string "" - & info [] ~docv:"DIFF" ~doc:"Diff output path (.png only)" + value & pos 2 (some string) None + & info [] ~docv:"DIFF" ~doc:"Optional Diff output path (.png only)" let base = value & pos 0 file "" & info [] ~docv:"BASE" ~doc:"Path to base image" diff --git a/src/Diff.ml b/src/Diff.ml index dc3d63f6..09dbba2f 100644 --- a/src/Diff.ml +++ b/src/Diff.ml @@ -29,20 +29,24 @@ module MakeDiff (IO1 : ImageIO.ImageIO) (IO2 : ImageIO.ImageIO) = struct let compare (base : IO1.t ImageIO.img) (comp : IO2.t ImageIO.img) ?(antialiasing = false) ?(outputDiffMask = false) ?(diffLines = false) - ?diffPixel ?(threshold = 0.1) ?ignoreRegions () = + ?diffPixel ?(threshold = 0.1) ?ignoreRegions ?(captureDiff = true) () = let maxDelta = maxYIQPossibleDelta *. (threshold ** 2.) in let diffPixel = match diffPixel with Some x -> x | None -> redPixel in let diffOutput = - match outputDiffMask with - | true -> IO1.makeSameAsLayout base - | false -> base + match captureDiff with + | true -> + Some + (match outputDiffMask with + | true -> IO1.makeSameAsLayout base + | false -> base) + | false -> None in let diffCount = ref 0 in let diffLinesStack = Stack.create () in let countDifference x y = incr diffCount; - IO1.setImgColor ~x ~y diffPixel diffOutput; + diffOutput |> Option.iter (IO1.setImgColor ~x ~y diffPixel); if diffLines @@ -115,10 +119,23 @@ module MakeDiff (IO1 : ImageIO.ImageIO) (IO2 : ImageIO.ImageIO) = struct && (base.width <> comp.width || base.height <> comp.height) then Layout else - let diffResult = + let diffOutput, diffCount, diffPercentage, diffLinesStack = compare base comp ~threshold ~diffPixel ~outputDiffMask ~antialiasing - ~diffLines ?ignoreRegions () + ~diffLines ?ignoreRegions ~captureDiff:true () in + Pixel (Option.get diffOutput, diffCount, diffPercentage, diffLinesStack) + let diffWithoutOutput (base : IO1.t ImageIO.img) (comp : IO2.t ImageIO.img) + ?(threshold = 0.1) ?(failOnLayoutChange = true) ?(antialiasing = false) + ?(diffLines = false) ?ignoreRegions () = + if + failOnLayoutChange = true + && (base.width <> comp.width || base.height <> comp.height) + then Layout + else + let diffResult = + compare base comp ~threshold ~outputDiffMask:false ~antialiasing + ~diffLines ?ignoreRegions ~captureDiff:false () + in Pixel diffResult end diff --git a/test/Test_Core.ml b/test/Test_Core.ml index d4a1a0ae..4399f540 100644 --- a/test/Test_Core.ml +++ b/test/Test_Core.ml @@ -48,10 +48,14 @@ let test_diff_color () = ~diffPixel:(Int32.of_int 4278255360 (*int32 representation of #00ff00*)) () in + check bool "diffOutput" (Option.is_some diffOutput) true; + let diffOutput = Option.get diffOutput in let originalDiff = Png.IO.loadImage "test-images/png/orange_diff_green.png" in let diffMaskOfDiff, diffOfDiffPixels, diffOfDiffPercentage, _ = PNG_Diff.compare originalDiff diffOutput () in + check bool "diffMaskOfDiff" (Option.is_some diffMaskOfDiff) true; + let diffMaskOfDiff = Option.get diffMaskOfDiff in if diffOfDiffPixels > 0 then ( Png.IO.saveImage diffOutput "test-images/png/diff-output-green.png"; Png.IO.saveImage diffMaskOfDiff "test-images/png/diff-of-diff-green.png"); diff --git a/test/Test_IO_BMP.ml b/test/Test_IO_BMP.ml index 4d6f716b..c8c87e79 100644 --- a/test/Test_IO_BMP.ml +++ b/test/Test_IO_BMP.ml @@ -34,8 +34,12 @@ let test_creates_correct_diff_output_image () = let img1 = load_image "test-images/bmp/clouds.bmp" in let img2 = load_image "test-images/bmp/clouds-2.bmp" in let diffOutput, _, _, _ = Diff.compare img1 img2 () in + check bool "diffOutput" (Option.is_some diffOutput) true; + let diffOutput = Option.get diffOutput in let originalDiff = load_png_image "test-images/bmp/clouds-diff.png" in let diffMaskOfDiff, diffOfDiffPixels, diffOfDiffPercentage, _ = Output_Diff.compare originalDiff diffOutput () in + check bool "diffMaskOfDiff" (Option.is_some diffMaskOfDiff) true; + let diffMaskOfDiff = Option.get diffMaskOfDiff in if diffOfDiffPixels > 0 then ( Bmp.IO.saveImage diffOutput "test-images/bmp/_diff-output.png"; Png.IO.saveImage diffMaskOfDiff "test-images/bmp/_diff-of-diff.png" diff --git a/test/Test_IO_JPG.ml b/test/Test_IO_JPG.ml index e2fc6dc5..00fd8946 100644 --- a/test/Test_IO_JPG.ml +++ b/test/Test_IO_JPG.ml @@ -43,10 +43,14 @@ let test_creates_correct_diff_output_image () = let img1 = load_image "test-images/jpg/tiger.jpg" in let img2 = load_image "test-images/jpg/tiger-2.jpg" in let diffOutput, _, _, _ = Diff.compare img1 img2 () in + check bool "diffOutput" (Option.is_some diffOutput) true; + let diffOutput = Option.get diffOutput in let originalDiff = load_png_image "test-images/jpg/tiger-diff.png" in let diffMaskOfDiff, diffOfDiffPixels, diffOfDiffPercentage, _ = Output_Diff.compare originalDiff diffOutput () in + check bool "diffMaskOfDiff" (Option.is_some diffMaskOfDiff) true; + let diffMaskOfDiff = Option.get diffMaskOfDiff in if diffOfDiffPixels > 0 then ( Jpg.IO.saveImage diffOutput "test-images/jpg/_diff-output.png"; Png.IO.saveImage diffMaskOfDiff "test-images/jpg/_diff-of-diff.png"); diff --git a/test/Test_IO_PNG.ml b/test/Test_IO_PNG.ml index 496321cd..208603d0 100644 --- a/test/Test_IO_PNG.ml +++ b/test/Test_IO_PNG.ml @@ -40,10 +40,14 @@ let () = let img1 = load_image "test-images/png/orange.png" in let img2 = load_image "test-images/png/orange_changed.png" in let diffOutput, _, _, _ = Diff.compare img1 img2 () in + check bool "diffOutput" (Option.is_some diffOutput) true; + let diffOutput = Option.get diffOutput in let originalDiff = load_image "test-images/png/orange_diff.png" in let diffMaskOfDiff, diffOfDiffPixels, diffOfDiffPercentage, _ = Diff.compare originalDiff diffOutput () in + check bool "diffMaskOfDiff" (Option.is_some diffMaskOfDiff) true; + let diffMaskOfDiff = Option.get diffMaskOfDiff in if diffOfDiffPixels > 0 then ( Png.IO.saveImage diffOutput "test-images/png/diff-output.png"; Png.IO.saveImage diffMaskOfDiff diff --git a/test/Test_IO_TIFF.ml b/test/Test_IO_TIFF.ml index 498d24f2..d2845387 100644 --- a/test/Test_IO_TIFF.ml +++ b/test/Test_IO_TIFF.ml @@ -49,12 +49,16 @@ let run_tiff_tests () = let img1 = load_tiff_image "test-images/tiff/laptops.tiff" in let img2 = load_tiff_image "test-images/tiff/laptops-2.tiff" in let diffOutput, _, _, _ = Diff.compare img1 img2 () in + check bool "diffOutput" (Option.is_some diffOutput) true; + let diffOutput = Option.get diffOutput in let originalDiff = load_png_image "test-images/tiff/laptops-diff.png" in let diffMaskOfDiff, diffOfDiffPixels, diffOfDiffPercentage, _ = Output_Diff.compare originalDiff diffOutput () in + check bool "diffMaskOfDiff" (Option.is_some diffMaskOfDiff) true; + let diffMaskOfDiff = Option.get diffMaskOfDiff in if diffOfDiffPixels > 0 then ( Tiff.IO.saveImage diffOutput "test-images/tiff/_diff-output.png"; Png.IO.saveImage diffMaskOfDiff @@ -66,5 +70,5 @@ let run_tiff_tests () = ] let () = - if Sys.os_type == "Unix" then run_tiff_tests () + if Sys.os_type = "Unix" then run_tiff_tests () else print_endline "Skipping TIFF tests on Windows systems"