Skip to content

Commit

Permalink
fix: Wrong pixel comparison output when layout is different(#111)
Browse files Browse the repository at this point in the history
* Fix comparison when layout differs

In case `layoutDifference` is `true` `compColor` is assigned incorrect
pixel value read from `base` instead of `comp`.

* Test checking comparison of images with different layouts

* Update test_different_sized_aa_images expects
  • Loading branch information
serpent7776 authored Oct 2, 2024
1 parent 93dd425 commit 99e5e2a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Diff.ml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ module MakeDiff (IO1 : ImageIO.ImageIO) (IO2 : ImageIO.ImageIO) = struct
if alpha <> Int32.zero then countDifference !x !y)
else
let compColor =
if layoutDifference then IO1.readRawPixel ~x:!x ~y:!y base
if layoutDifference then IO2.readRawPixel ~x:!x ~y:!y comp
else IO2.readRawPixelAtOffset offset comp
in

Expand Down
19 changes: 17 additions & 2 deletions test/Test_Core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ let test_different_sized_aa_images () =
let _, diffPixels, diffPercentage, _ =
PNG_Diff.compare img1 img2 ~outputDiffMask:true ~antialiasing:true ()
in
check int "diffPixels" 399 diffPixels;
check (float 0.01) "diffPercentage" 0.99 diffPercentage
check int "diffPixels" 417 diffPixels;
check (float 0.01) "diffPercentage" 1.0425 diffPercentage

let test_threshold () =
let img1 = Png.IO.loadImage "test-images/png/orange.png" in
Expand Down Expand Up @@ -73,6 +73,16 @@ let test_blend_semi_transparent_color () =
test_blend 0. 128. 255. 51. 204. 229.6 255. 0.2;
test_blend 0. 128. 255. 128. 127. 191.25 255. 0.5

let test_different_layouts () =
Sys.getcwd () |> print_endline;
let img1 = Png.IO.loadImage "test-images/png/white4x4.png" in
let img2 = Png.IO.loadImage "test-images/png/purple8x8.png" in
let _, diffPixels, diffPercentage, _ =
PNG_Diff.compare img1 img2 ~outputDiffMask:false ~antialiasing:false ()
in
check int "diffPixels" 16 diffPixels;
check (float 0.001) "diffPercentage" 100.0 diffPercentage

let () =
run "CORE"
[
Expand All @@ -98,4 +108,9 @@ let () =
test_case "blend semi-transparent colors" `Quick
test_blend_semi_transparent_color;
] );
( "layoutDifference",
[
test_case "diff images with different layouts" `Quick
test_different_layouts;
] );
]
Binary file added test/test-images/png/purple8x8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/test-images/png/white4x4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 99e5e2a

Please sign in to comment.