Skip to content
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,5 @@ reise_bestand_ws_war/overlays

# Gradle
build/
*.pdf
*.pdf
output/
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@ private boolean writeTo(final PDFMergerUtility mergerUtility) {
swapToDisk();
Utilities.shutdownAndAwaitTermination(swapExecutor, "Swap");
try {
LOG.trace("Merging...");
LOG.info("Merging...");
Instant start = Instant.now();
for (Path path : FileUtils.getPaths(getTempDir(), "partial_*")) {
mergerUtility.addSource(path.toFile());
}
mergerUtility.mergeDocuments(Utilities.getMemorySettings(environment.getMergeCacheSize()));
Instant end = Instant.now();

LOG.trace("Merging took: {}ms", Duration.between(start, end).toMillis());

} catch (IOException e) {
throw new RuntimeException(e);
} finally {
Expand Down Expand Up @@ -115,11 +117,11 @@ private synchronized void swapToDisk() {
if (!images.isEmpty()) {
swapped = true;
getExecutor(environment).execute(() -> {
LOG.trace("Swapping {} pages to disk", images.size());
LOG.info("Swapping {} pages to disk", images.size());
Instant start = Instant.now();

final int minPageIndex = images.keySet().iterator().next();
LOG.trace("minPageIndex: {}", minPageIndex);
LOG.info("minPageIndex: {}", minPageIndex);
try (PDDocument document = new PDDocument(Utilities.getMemorySettings(environment.getSwapCacheSize()))) {
document.setResourceCache(new ResourceCacheWithLimitedImages(environment));
addImagesToDocument(document, images);
Expand All @@ -129,7 +131,7 @@ private synchronized void swapToDisk() {
throw new RuntimeException(e);
}
Instant end = Instant.now();
LOG.trace("Swapping took: {}ms", Duration.between(start, end).toMillis());
LOG.info("Swapping took: {}ms", Duration.between(start, end).toMillis());
});
}
}
Expand Down
65 changes: 50 additions & 15 deletions src/main/java/de/redsix/pdfcompare/CompareResultImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package de.redsix.pdfcompare;

import de.redsix.pdfcompare.env.Environment;
import x.team.tool.MergeImages;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
Expand Down Expand Up @@ -122,21 +124,25 @@ protected boolean keepImages() {
}

@Override
public synchronized void addPage(final PageDiffCalculator diffCalculator, final int pageIndex,
final ImageWithDimension expectedImage, final ImageWithDimension actualImage, final ImageWithDimension diffImage) {
Objects.requireNonNull(expectedImage, "expectedImage is null");
Objects.requireNonNull(actualImage, "actualImage is null");
Objects.requireNonNull(diffImage, "diffImage is null");
this.hasDifferenceInExclusion |= diffCalculator.differencesFoundInExclusion();
diffPercentages.put(pageIndex, diffCalculator.getDifferenceInPercent());
if (diffCalculator.differencesFound()) {
isEqual = false;
diffAreas.add(diffCalculator.getDiffArea());
diffImages.put(pageIndex, diffImage);
pages++;
} else if (environment.addEqualPagesToResult()) {
diffImages.put(pageIndex, diffImage);
pages++;
public synchronized void addPage(final PageDiffCalculator diffCalculator, final int pageIndex,final ImageWithDimension expectedImage, final ImageWithDimension actualImage, final ImageWithDimension diffImage) {
if(this.environment.getEnableHorizontalCompareOutput()) {
this.addPageWithHorizontalCompare(diffCalculator, pageIndex, expectedImage, actualImage, diffImage);
}else {
Objects.requireNonNull(expectedImage, "expectedImage is null");
Objects.requireNonNull(actualImage, "actualImage is null");
Objects.requireNonNull(diffImage, "diffImage is null");

this.hasDifferenceInExclusion |= diffCalculator.differencesFoundInExclusion();
if (diffCalculator.differencesFound()) {
isEqual = false;
diffAreas.add(diffCalculator.getDiffArea());
diffImages.put(pageIndex, diffImage);
pages++;
} else if (environment.addEqualPagesToResult()) {
diffImages.put(pageIndex, diffImage);
pages++;
}

}
}

Expand Down Expand Up @@ -211,4 +217,33 @@ public void actualOnly() {
public void setEnvironment(Environment environment) {
this.environment = environment;
}

@Override
public void addPageWithHorizontalCompare(PageDiffCalculator diffCalculator, int pageIndex,
ImageWithDimension expectedImage, ImageWithDimension actualImage, ImageWithDimension diffImage) {

Objects.requireNonNull(expectedImage, "expectedImage is null");
Objects.requireNonNull(actualImage, "actualImage is null");
Objects.requireNonNull(diffImage, "diffImage is null");
this.hasDifferenceInExclusion |= diffCalculator.differencesFoundInExclusion();

//Creto una nuova immagine con a sinistra l'atteso e a destra l'attuale con segnate le differenze
MergeImages merge=new MergeImages();
ImageWithDimension mergedImage=null;
if(pageIndex==0) {
mergedImage=merge.mergeOnLeft(expectedImage, diffImage,PdfComparator.headerLeft,PdfComparator.headerRight);
}else {
mergedImage=merge.mergeOnLeft(expectedImage, diffImage,null,null);
}

if (diffCalculator.differencesFound()) {
isEqual = false;
diffAreas.add(diffCalculator.getDiffArea());
diffImages.put(pageIndex, mergedImage);
pages++;
} else if (environment.addEqualPagesToResult()) {
diffImages.put(pageIndex, mergedImage);
pages++;
}
}
}
89 changes: 86 additions & 3 deletions src/main/java/de/redsix/pdfcompare/DiffImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,29 @@

import static de.redsix.pdfcompare.PdfComparator.MARKER_WIDTH;


import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import de.redsix.pdfcompare.env.Environment;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;


public class DiffImage {

private static final Logger LOG = LoggerFactory.getLogger(DiffImage.class);
Expand Down Expand Up @@ -49,6 +64,9 @@ public int getPage() {
}

public void diffImages() {

System.out.println("TEST JVM 1");

BufferedImage expectBuffImage = this.expectedImage.bufferedImage;
BufferedImage actualBuffImage = this.actualImage.bufferedImage;
expectedBuffer = expectBuffImage.getRaster().getDataBuffer();
Expand All @@ -75,8 +93,9 @@ public void diffImages() {
final int actualLineOffset = y * actualImageWidth;
final int resultLineOffset = y * resultImageWidth;
for (int x = 0; x < resultImageWidth; x++) {
expectedElement = getExpectedElement(x, y, expectedLineOffset);
expectedElement = getExpectedElement(x, y, expectedLineOffset);
actualElement = getActualElement(x, y, actualLineOffset);
//this.salvaSuFileImmagine(actualBuffer, expectedBuffer);
int element = getElement(expectedElement, actualElement);
if (pageExclusions.contains(x, y)) {
element = ImageTools.fadeExclusion(element);
Expand All @@ -87,24 +106,88 @@ public void diffImages() {
if (expectedElement != actualElement) {
extendDiffArea(x, y);
diffCalculator.diffFound();

LOG.trace("Difference found on page: {} at x: {}, y: {}", page + 1, x, y);
mark(resultBuffer, x, y, resultImageWidth);

}
}
resultBuffer.setElem(x + resultLineOffset, element);
}
}
if (diffCalculator.differencesFound()) {
diffCalculator.addDiffArea(new PageArea(page + 1, diffAreaX1, diffAreaY1, diffAreaX2, diffAreaY2));
LOG.info("Differences found at { page: {}, x1: {}, y1: {}, x2: {}, y2: {} }", page + 1, diffAreaX1, diffAreaY1, diffAreaX2,
LOG.debug("Differences found at { page: {}, x1: {}, y1: {}, x2: {}, y2: {} }", page + 1, diffAreaX1, diffAreaY1, diffAreaX2,
diffAreaY2);
}
final float maxWidth = Math.max(expectedImage.width, actualImage.width);
final float maxHeight = Math.max(expectedImage.height, actualImage.height);
compareResult.addPage(diffCalculator, page, expectedImage, actualImage, new ImageWithDimension(resultImage, maxWidth, maxHeight));
}

private void extendDiffArea(final int x, final int y) {
private void salvaSuFileImmagine(DataBuffer actualBuffer2, DataBuffer expectedBuffer2) {
System.out.println("********************* DATA BUFFER ACTUAL ******************************");
String actualBuffer="";
System.out.println("Inizio a leggere actualbuffer...");
int value=0;
for(int i=0;i<actualBuffer2.getSize();i++) {
value=actualBuffer2.getElem(i);

actualBuffer=actualBuffer.concat(String.valueOf(value));
if(i%100==0) {
//System.out.println(i+""+value);
}
}
this.writeTextFile("", "ACTUAL.txt",actualBuffer);
System.out.println("Ho creato il file:"+new File("ACTUAL.txt").getAbsolutePath());

System.out.println("********************* DATA BUFFER EXPECTED ******************************");
String expectedBuffer="";
System.out.println("Inizio a leggere actualbuffer...");
value=0;
for(int i=0;i<expectedBuffer2.getSize();i++) {
value=expectedBuffer2.getElem(i);
expectedBuffer=expectedBuffer.concat(String.valueOf(value));
if(i%100==0) {
//System.out.println(i+""+value);
}
}
this.writeTextFile("", "EXPECTED.txt",expectedBuffer);
System.out.println("Ho creato il file:"+new File("EXPECTED.txt").getAbsolutePath());
}


public String writeTextFile(String pathOutput, String fileNameOutput, String text) {
BufferedWriter writer = null;
File fileOutput = null;
try {
File dir = new File(pathOutput);
if (!dir.exists()) {
dir.mkdirs();
}
fileOutput = new File(dir + File.separator + fileNameOutput);
writer = new BufferedWriter(new FileWriter(fileOutput.getAbsoluteFile()));
writer.write(text);
System.out.println("Created file: " + fileOutput.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
System.out.println("STACKTRACE"+e.getMessage());
return ("ERROR_WRITE_FILE");
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("STACKTRACE"+e.getMessage());
}
}
return ("DONE");
}


private void extendDiffArea(final int x, final int y) {
if (!diffCalculator.differencesFound()) {
diffAreaX1 = x;
diffAreaY1 = y;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/de/redsix/pdfcompare/ImageWithDimension.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ public ImageWithDimension(final BufferedImage bufferedImage, final float width,
this.width = width;
this.height = height;
}

public BufferedImage getBufferedImage() {
return bufferedImage;
}
}
7 changes: 6 additions & 1 deletion src/main/java/de/redsix/pdfcompare/PdfComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,14 @@ public class PdfComparator<T extends CompareResultImpl> {
private final T compareResult;
private String expectedPassword = "";
private String actualPassword = "";
private boolean withIgnoreCalled = false;

private boolean withIgnoreCalled = false;
public static String headerLeft="";
public static String headerRight="";

private final ConcurrentLinkedQueue<Throwable> exceptionFromOtherThread = new ConcurrentLinkedQueue<>();


/**
* Compare two PDFs, that are given as base64 encoded strings.
*
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/de/redsix/pdfcompare/ResultCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ public interface ResultCollector {
void addPage(PageDiffCalculator diffCalculator, int pageIndex,
ImageWithDimension expectedImage, ImageWithDimension actualImage, ImageWithDimension diffImage);

void addPageWithHorizontalCompare(PageDiffCalculator diffCalculator, int pageIndex,
ImageWithDimension expectedImage, ImageWithDimension actualImage, ImageWithDimension diffImage);


void noPagesFound();

default void done() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public Path getTempDirectory() {

@Override
public int getNrOfImagesToCache() {
return config.getInt("imageCacheSizeCount");
return(0);
//return config.getInt("imageCacheSizeCount");

}

@Override
Expand Down Expand Up @@ -137,4 +139,9 @@ public boolean failOnMissingIgnoreFile() {
private int getMB(final String path) {
return config.getInt(path) * 1024 * 1024;
}

@Override
public boolean getEnableHorizontalCompareOutput() {
return false;
}
}
2 changes: 2 additions & 0 deletions src/main/java/de/redsix/pdfcompare/env/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ public interface Environment {
boolean addEqualPagesToResult();

boolean failOnMissingIgnoreFile();

boolean getEnableHorizontalCompareOutput();
}
14 changes: 14 additions & 0 deletions src/main/java/de/redsix/pdfcompare/env/SimpleEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class SimpleEnvironment implements Environment {
private Integer dpi;
private Boolean addEqualPagesToResult;
private Boolean failOnMissingIgnoreFile;
private Boolean enableHorizontalCompareOutput=false;

public SimpleEnvironment() {
this(DefaultEnvironment.create());
Expand Down Expand Up @@ -186,4 +187,17 @@ public SimpleEnvironment setFailOnMissingIgnoreFile(final boolean b) {
this.failOnMissingIgnoreFile = b;
return this;
}

public SimpleEnvironment setEnableHorizontalCompareOutput(Boolean enableHorizontalCompareOutput) {
this.enableHorizontalCompareOutput = enableHorizontalCompareOutput;
return this;
}

public boolean getEnableHorizontalCompareOutput() {
return enableHorizontalCompareOutput;
}




}
33 changes: 33 additions & 0 deletions src/main/java/x/team/poc/POC.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package x.team.poc;

import java.awt.Color;

import de.redsix.pdfcompare.CompareResult;
import de.redsix.pdfcompare.PdfComparator;
import de.redsix.pdfcompare.env.SimpleEnvironment;

public class POC {

public static void main(String[] args) {
try {

PdfComparator compare = new PdfComparator("input/expected.pdf", "input/actual.pdf")
.withEnvironment(new SimpleEnvironment().setActualColor(Color.red)
.setExpectedColor(Color.white).setAddEqualPagesToResult(true)
.setEnableHorizontalCompareOutput(true));

CompareResult result = compare.compare();
result .writeTo("output/diffOutput");
if (result.isNotEqual()) {
System.out.println("Ho trovato differenza!");
}
if (result.isEqual()) {
System.out.println("Non ho trovato nessuna differenza!");
}
result.getDifferences();
} catch (Exception e) {
e.printStackTrace();
}
}

}
Loading