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

Add difference percentages to CompareResult #108

Merged
merged 3 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions src/main/java/de/redsix/pdfcompare/CompareResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.OutputStream;
import java.util.Collection;
import java.util.Map;

public interface CompareResult {

Expand Down Expand Up @@ -99,4 +100,11 @@ public interface CompareResult {
* @return collection of page numbers, that have a difference
*/
Collection<Integer> getPagesWithDifferences();

/**
* Gives a map of the difference percentages per page.
*
* @return difference percentages mapped to the page index.
*/
Map<Integer, Double> getPageDiffsInPercent();
}
16 changes: 13 additions & 3 deletions src/main/java/de/redsix/pdfcompare/CompareResultImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class CompareResultImpl implements ResultCollector, CompareResult {
private boolean expectedOnly;
private boolean actualOnly;
private final Collection<PageArea> diffAreas = new ArrayList<>();
private final Map<Integer, Double> diffPercentages = new TreeMap<>();
private int pages = 0;

@Override
Expand Down Expand Up @@ -131,10 +132,14 @@ public synchronized void addPage(final PageDiffCalculator diffCalculator, final
isEqual = false;
diffAreas.add(diffCalculator.getDiffArea());
diffImages.put(pageIndex, diffImage);
diffPercentages.put(pageIndex, diffCalculator.getDifferenceInPercent());
pages++;
} else if (environment.addEqualPagesToResult()) {
diffImages.put(pageIndex, diffImage);
pages++;
} else {
diffPercentages.put(pageIndex, 0.0);
LennartKoot marked this conversation as resolved.
Show resolved Hide resolved
if (environment.addEqualPagesToResult()) {
diffImages.put(pageIndex, diffImage);
pages++;
}
}
}

Expand Down Expand Up @@ -193,6 +198,11 @@ public Collection<Integer> getPagesWithDifferences() {
return diffAreas.stream().map(a -> a.page).collect(Collectors.toList());
}

@Override
public Map<Integer, Double> getPageDiffsInPercent() {
return diffPercentages;
}

public void expectedOnly() {
this.expectedOnly = true;
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/de/redsix/pdfcompare/PageDiffCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public boolean differencesFoundInExclusion() {
return diffsFoundInExclusion > 0;
}

public double getDifferenceInPercent() {
if (totalPixels == 0) {
return diffsFound > 0 ? 100.0 : 0.0;
} else {
return (double)diffsFound / (double)totalPixels * 100.0;
}
}

@Override
public boolean equals(final Object o) {
if (this == o) {
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/de/redsix/pdfcompare/CompareResultImplTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package de.redsix.pdfcompare;

import static org.hamcrest.collection.IsMapContaining.hasEntry;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import de.redsix.pdfcompare.env.SimpleEnvironment;
import org.junit.jupiter.api.Test;

import java.awt.image.BufferedImage;
import java.util.Map;

class CompareResultImplTest {

Expand Down Expand Up @@ -35,4 +37,22 @@ public void addEqualPagesAreNotStored() {
assertThat(compareResult.getNumberOfPages(), is(1));
assertThat(compareResult.diffImages.size(), is(1));
}

@Test
public void mapsDiffPercentagesCorrectly() {
CompareResultImpl compareResult = new CompareResultImpl();
compareResult.setEnvironment(new SimpleEnvironment());
ImageWithDimension image = new ImageWithDimension(new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_BINARY), 0.0f, 0.0f);

PageDiffCalculator pageWithDiff = new PageDiffCalculator(5, 0);
pageWithDiff.diffFound();
PageDiffCalculator pageWithoutDiff = new PageDiffCalculator(10, 0);

compareResult.addPage(pageWithDiff, 1, image, image, image);
compareResult.addPage(pageWithoutDiff, 2, image, image, image);

Map<Integer, Double> result = compareResult.getPageDiffsInPercent();
assertThat(result, hasEntry(1, 20.0));
assertThat(result, hasEntry(2, 0.0));
}
}
28 changes: 28 additions & 0 deletions src/test/java/de/redsix/pdfcompare/PageDiffCalculatorTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.redsix.pdfcompare;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -62,4 +63,31 @@ public void diffAbovePercentageAsFractionIsReported() {
diffCalculator.diffFound();
assertTrue(diffCalculator.differencesFound());
}

@Test
public void zeroTotalPixelsWithDiffFoundReportsHundredPercentDifference() {
final PageDiffCalculator diffCalculator = new PageDiffCalculator(0, 0);
diffCalculator.diffFound();
assertEquals(100.0, diffCalculator.getDifferenceInPercent());
}

@Test
public void zeroTotalPixelsWithNoDiffFoundReportsZeroPercentDifference() {
final PageDiffCalculator diffCalculator = new PageDiffCalculator(0, 0);
assertEquals(0.0, diffCalculator.getDifferenceInPercent());
}

@Test
public void diffsFoundReportsCorrectPercentage() {
final PageDiffCalculator diffCalculator = new PageDiffCalculator(10, 0);
diffCalculator.diffFound();
diffCalculator.diffFound();
assertEquals(20.0, diffCalculator.getDifferenceInPercent());
}

@Test
public void noDiffsFoundReportsCorrectPercentage() {
final PageDiffCalculator diffCalculator = new PageDiffCalculator(6, 0);
assertEquals(0.0, diffCalculator.getDifferenceInPercent());
}
}