Skip to content

Commit e672f01

Browse files
committed
Introduce diffWithoutOutput
1 parent 2b5131e commit e672f01

File tree

6 files changed

+44
-7
lines changed

6 files changed

+44
-7
lines changed

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: 4 additions & 0 deletions
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

0 commit comments

Comments
 (0)