Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: "index out of bounds" on antialiasing check #38

Merged
merged 1 commit into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions src/Antialiasing.re
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
open ImageIO;

module MakeAntialiasing = (IO1: ImageIO.ImageIO, IO2: ImageIO.ImageIO) => {
let hasManySiblingsWithSameColor = (~x, ~y, ~width, ~height, ~readColor) => {
let x0 = max(x - 1, 0);
let y0 = max(y - 1, 0);
let hasManySiblingsWithSameColor = (~x, ~y, ~width, ~height, ~readColor) =>
if (x <= width - 1 && y <= height - 1) {
let x0 = max(x - 1, 0);
let y0 = max(y - 1, 0);

let x1 = min(x + 1, width - 1);
let y1 = min(y + 1, height - 1);
let x1 = min(x + 1, width - 1);
let y1 = min(y + 1, height - 1);

let zeroes = x == x0 || x == x1 || y == y0 || y == y1 ? ref(1) : ref(0);
let zeroes =
x == x0 || x == x1 || y == y0 || y == y1 ? ref(1) : ref(0);

let baseColor = readColor(~x, ~y);
let baseColor = readColor(~x, ~y);

// go through 8 adjacent pixels
for (adj_y in y0 to y1) {
for (adj_x in x0 to x1) {
/* This is the current pixel or we already have our result, do nothing */
if (x == adj_x && y == adj_y || zeroes^ >= 3) {
();
} else {
let adjacentColor = readColor(~x=adj_x, ~y=adj_y);
if (Helpers.isSameColor(baseColor, adjacentColor)) {
zeroes := zeroes^ + 1;
// go through 8 adjacent pixels
for (adj_y in y0 to y1) {
for (adj_x in x0 to x1) {
/* This is the current pixel or we already have our result, do nothing */
if (x == adj_x && y == adj_y || zeroes^ >= 3) {
();
} else {
let adjacentColor = readColor(~x=adj_x, ~y=adj_y);
if (Helpers.isSameColor(baseColor, adjacentColor)) {
zeroes := zeroes^ + 1;
};
};
};
};
};

zeroes^ >= 3;
};
zeroes^ >= 3;
} else {
false;
};

let detect = (~x, ~y, ~baseImg, ~compImg) => {
let x0 = max(x - 1, 0);
Expand Down
21 changes: 21 additions & 0 deletions test/PngTests.re
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,25 @@ describe("Png comparing", ({test, _}) => {
expect.int(diffPixels).toBe(diffPixelsMask);
expect.float(diffPercentage).toBeCloseTo(diffPercentageMask);
});

test("tests diffrent sized AA images", ({expect, _}) => {
let img1 =
PureC_IO_Bigarray.IO.loadImage("test/test-images/antialiasing-on.png");
let img2 =
PureC_IO_Bigarray.IO.loadImage(
"test/test-images/antialiasing-off-small.png",
);

let (_, diffPixels, diffPercentage) =
AADiff.compare(
img1,
img2,
~outputDiffMask=true,
~antialiasing=true,
(),
);

expect.int(diffPixels).toBe(417);
expect.float(diffPercentage).toBeCloseTo(1.04);
});
});
Binary file added test/test-images/antialiasing-off-small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.