Skip to content

Commit

Permalink
Introduce diffWithoutOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
serpent7776 committed Oct 19, 2024
1 parent 2b5131e commit e672f01
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 7 deletions.
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
4 changes: 4 additions & 0 deletions 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 Down

0 comments on commit e672f01

Please sign in to comment.