Skip to content

Commit 3bb5f75

Browse files
authored
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
1 parent c58c505 commit 3bb5f75

File tree

9 files changed

+50
-13
lines changed

9 files changed

+50
-13
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ ODiff is a blazing fast native image comparison tool. Check [benchmarks](#benchm
4848

4949
### Basic comparison
5050

51-
Run the simple comparison. Image paths can be one of supported formats, diff output can only be `.png`.
51+
Run the simple comparison. Image paths can be one of supported formats, diff output is optional and can only be `.png`.
5252

5353
```
54-
odiff <IMG1 path> <IMG2 path> <DIFF output path>
54+
odiff <IMG1 path> <IMG2 path> [DIFF output path]
5555
```
5656

5757
### Node.js

bin/Main.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ let main img1Path img2Path diffPath threshold outputDiffMask failOnLayoutChange
6060
->
6161
{ exitCode = 0; diff = Some diffOutput }
6262
| Pixel (diffOutput, diffCount, diffPercentage, _) ->
63-
IO1.saveImage diffOutput diffPath;
63+
diffPath |> Option.iter (IO1.saveImage diffOutput);
6464
{ exitCode = 22; diff = Some diffOutput }
6565
in
6666
IO1.freeImage img1;

bin/ODiffBin.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ open Term
33
open Arg
44

55
let diffPath =
6-
value & pos 2 string ""
7-
& info [] ~docv:"DIFF" ~doc:"Diff output path (.png only)"
6+
value & pos 2 (some string) None
7+
& info [] ~docv:"DIFF" ~doc:"Optional Diff output path (.png only)"
88

99
let base =
1010
value & pos 0 file "" & info [] ~docv:"BASE" ~doc:"Path to base image"

src/Diff.ml

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,24 @@ module MakeDiff (IO1 : ImageIO.ImageIO) (IO2 : ImageIO.ImageIO) = struct
2929

3030
let compare (base : IO1.t ImageIO.img) (comp : IO2.t ImageIO.img)
3131
?(antialiasing = false) ?(outputDiffMask = false) ?(diffLines = false)
32-
?diffPixel ?(threshold = 0.1) ?ignoreRegions () =
32+
?diffPixel ?(threshold = 0.1) ?ignoreRegions ?(captureDiff = true) () =
3333
let maxDelta = maxYIQPossibleDelta *. (threshold ** 2.) in
3434
let diffPixel = match diffPixel with Some x -> x | None -> redPixel in
3535
let diffOutput =
36-
match outputDiffMask with
37-
| true -> IO1.makeSameAsLayout base
38-
| false -> base
36+
match captureDiff with
37+
| true ->
38+
Some
39+
(match outputDiffMask with
40+
| true -> IO1.makeSameAsLayout base
41+
| false -> base)
42+
| false -> None
3943
in
4044

4145
let diffCount = ref 0 in
4246
let diffLinesStack = Stack.create () in
4347
let countDifference x y =
4448
incr diffCount;
45-
IO1.setImgColor ~x ~y diffPixel diffOutput;
49+
diffOutput |> Option.iter (IO1.setImgColor ~x ~y diffPixel);
4650

4751
if
4852
diffLines
@@ -115,10 +119,23 @@ module MakeDiff (IO1 : ImageIO.ImageIO) (IO2 : ImageIO.ImageIO) = struct
115119
&& (base.width <> comp.width || base.height <> comp.height)
116120
then Layout
117121
else
118-
let diffResult =
122+
let diffOutput, diffCount, diffPercentage, diffLinesStack =
119123
compare base comp ~threshold ~diffPixel ~outputDiffMask ~antialiasing
120-
~diffLines ?ignoreRegions ()
124+
~diffLines ?ignoreRegions ~captureDiff:true ()
121125
in
126+
Pixel (Option.get diffOutput, diffCount, diffPercentage, diffLinesStack)
122127

128+
let diffWithoutOutput (base : IO1.t ImageIO.img) (comp : IO2.t ImageIO.img)
129+
?(threshold = 0.1) ?(failOnLayoutChange = true) ?(antialiasing = false)
130+
?(diffLines = false) ?ignoreRegions () =
131+
if
132+
failOnLayoutChange = true
133+
&& (base.width <> comp.width || base.height <> comp.height)
134+
then Layout
135+
else
136+
let diffResult =
137+
compare base comp ~threshold ~outputDiffMask:false ~antialiasing
138+
~diffLines ?ignoreRegions ~captureDiff:false ()
139+
in
123140
Pixel diffResult
124141
end

test/Test_Core.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,14 @@ let test_diff_color () =
4848
~diffPixel:(Int32.of_int 4278255360 (*int32 representation of #00ff00*))
4949
()
5050
in
51+
check bool "diffOutput" (Option.is_some diffOutput) true;
52+
let diffOutput = Option.get diffOutput in
5153
let originalDiff = Png.IO.loadImage "test-images/png/orange_diff_green.png" in
5254
let diffMaskOfDiff, diffOfDiffPixels, diffOfDiffPercentage, _ =
5355
PNG_Diff.compare originalDiff diffOutput ()
5456
in
57+
check bool "diffMaskOfDiff" (Option.is_some diffMaskOfDiff) true;
58+
let diffMaskOfDiff = Option.get diffMaskOfDiff in
5559
if diffOfDiffPixels > 0 then (
5660
Png.IO.saveImage diffOutput "test-images/png/diff-output-green.png";
5761
Png.IO.saveImage diffMaskOfDiff "test-images/png/diff-of-diff-green.png");

test/Test_IO_BMP.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ let test_creates_correct_diff_output_image () =
3434
let img1 = load_image "test-images/bmp/clouds.bmp" in
3535
let img2 = load_image "test-images/bmp/clouds-2.bmp" in
3636
let diffOutput, _, _, _ = Diff.compare img1 img2 () in
37+
check bool "diffOutput" (Option.is_some diffOutput) true;
38+
let diffOutput = Option.get diffOutput in
3739
let originalDiff = load_png_image "test-images/bmp/clouds-diff.png" in
3840
let diffMaskOfDiff, diffOfDiffPixels, diffOfDiffPercentage, _ = Output_Diff.compare originalDiff diffOutput () in
41+
check bool "diffMaskOfDiff" (Option.is_some diffMaskOfDiff) true;
42+
let diffMaskOfDiff = Option.get diffMaskOfDiff in
3943
if diffOfDiffPixels > 0 then (
4044
Bmp.IO.saveImage diffOutput "test-images/bmp/_diff-output.png";
4145
Png.IO.saveImage diffMaskOfDiff "test-images/bmp/_diff-of-diff.png"

test/Test_IO_JPG.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@ let test_creates_correct_diff_output_image () =
4343
let img1 = load_image "test-images/jpg/tiger.jpg" in
4444
let img2 = load_image "test-images/jpg/tiger-2.jpg" in
4545
let diffOutput, _, _, _ = Diff.compare img1 img2 () in
46+
check bool "diffOutput" (Option.is_some diffOutput) true;
47+
let diffOutput = Option.get diffOutput in
4648
let originalDiff = load_png_image "test-images/jpg/tiger-diff.png" in
4749
let diffMaskOfDiff, diffOfDiffPixels, diffOfDiffPercentage, _ =
4850
Output_Diff.compare originalDiff diffOutput ()
4951
in
52+
check bool "diffMaskOfDiff" (Option.is_some diffMaskOfDiff) true;
53+
let diffMaskOfDiff = Option.get diffMaskOfDiff in
5054
if diffOfDiffPixels > 0 then (
5155
Jpg.IO.saveImage diffOutput "test-images/jpg/_diff-output.png";
5256
Png.IO.saveImage diffMaskOfDiff "test-images/jpg/_diff-of-diff.png");

test/Test_IO_PNG.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,14 @@ let () =
4040
let img1 = load_image "test-images/png/orange.png" in
4141
let img2 = load_image "test-images/png/orange_changed.png" in
4242
let diffOutput, _, _, _ = Diff.compare img1 img2 () in
43+
check bool "diffOutput" (Option.is_some diffOutput) true;
44+
let diffOutput = Option.get diffOutput in
4345
let originalDiff = load_image "test-images/png/orange_diff.png" in
4446
let diffMaskOfDiff, diffOfDiffPixels, diffOfDiffPercentage, _ =
4547
Diff.compare originalDiff diffOutput ()
4648
in
49+
check bool "diffMaskOfDiff" (Option.is_some diffMaskOfDiff) true;
50+
let diffMaskOfDiff = Option.get diffMaskOfDiff in
4751
if diffOfDiffPixels > 0 then (
4852
Png.IO.saveImage diffOutput "test-images/png/diff-output.png";
4953
Png.IO.saveImage diffMaskOfDiff

test/Test_IO_TIFF.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,16 @@ let run_tiff_tests () =
4949
let img1 = load_tiff_image "test-images/tiff/laptops.tiff" in
5050
let img2 = load_tiff_image "test-images/tiff/laptops-2.tiff" in
5151
let diffOutput, _, _, _ = Diff.compare img1 img2 () in
52+
check bool "diffOutput" (Option.is_some diffOutput) true;
53+
let diffOutput = Option.get diffOutput in
5254
let originalDiff =
5355
load_png_image "test-images/tiff/laptops-diff.png"
5456
in
5557
let diffMaskOfDiff, diffOfDiffPixels, diffOfDiffPercentage, _ =
5658
Output_Diff.compare originalDiff diffOutput ()
5759
in
60+
check bool "diffMaskOfDiff" (Option.is_some diffMaskOfDiff) true;
61+
let diffMaskOfDiff = Option.get diffMaskOfDiff in
5862
if diffOfDiffPixels > 0 then (
5963
Tiff.IO.saveImage diffOutput "test-images/tiff/_diff-output.png";
6064
Png.IO.saveImage diffMaskOfDiff
@@ -66,5 +70,5 @@ let run_tiff_tests () =
6670
]
6771

6872
let () =
69-
if Sys.os_type == "Unix" then run_tiff_tests ()
73+
if Sys.os_type = "Unix" then run_tiff_tests ()
7074
else print_endline "Skipping TIFF tests on Windows systems"

0 commit comments

Comments
 (0)