Skip to content

Commit

Permalink
feat: Make the diffPath optional (#112)
Browse files Browse the repository at this point in the history
* Make the diffPath optional

* Update README

* Fix running TIFF tests

The incorrect equals were used: (==) where (=) was intended.

* Introduce diffWithoutOutput
  • Loading branch information
serpent7776 authored Nov 22, 2024
1 parent c58c505 commit 3bb5f75
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <IMG1 path> <IMG2 path> <DIFF output path>
odiff <IMG1 path> <IMG2 path> [DIFF output path]
```

### Node.js
Expand Down
2 changes: 1 addition & 1 deletion bin/Main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions bin/ODiffBin.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
31 changes: 24 additions & 7 deletions src/Diff.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions test/Test_Core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 4 additions & 0 deletions test/Test_IO_BMP.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions test/Test_IO_JPG.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 4 additions & 0 deletions test/Test_IO_PNG.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion test/Test_IO_TIFF.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"

0 comments on commit 3bb5f75

Please sign in to comment.