Debian Codegraph @

sejda-sambox/src/test/java/org/sejda/impl/sambox/util/PixelCompareUtils.java

raw
1 /*
2 * Copyright 2021 by Eduard Weissmann ([email protected]).
3 *
4 * This file is part of the Sejda source code
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 package org.sejda.impl.sambox.util;
20
21 import static org.sejda.core.service.BaseTaskTest.customInput;
22
23 import java.awt.image.BufferedImage;
24 import java.io.File;
25 import java.io.IOException;
26
27 import javax.imageio.ImageIO;
28
29 import org.junit.Assert;
30 import org.sejda.core.support.io.IOUtils;
31 import org.sejda.impl.sambox.component.DefaultPdfSourceOpener;
32 import org.sejda.impl.sambox.component.PDDocumentHandler;
33 import org.sejda.model.exception.TaskIOException;
34 import org.sejda.sambox.pdmodel.PDDocument;
35 import org.sejda.sambox.rendering.PDFRenderer;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import net.coobird.thumbnailator.Thumbnails;
40 import net.coobird.thumbnailator.resizers.configurations.ScalingMode;
41
42 public class PixelCompareUtils {
43
44 private static final Logger LOG = LoggerFactory.getLogger(PixelCompareUtils.class);
45
46 public static void assertSimilar(PDDocumentHandler doc1, String doc2ResourcePath) {
47 assertSimilar(doc1.getUnderlyingPDDocument(), doc2ResourcePath);
48 }
49
50 public static void assertSimilar(PDDocument doc1, String doc2ResourcePath) {
51 try {
52 PDDocument doc2 = customInput(doc2ResourcePath).open(new DefaultPdfSourceOpener())
53 .getUnderlyingPDDocument();
54 assertSimilar(doc1, doc2, Integer.MAX_VALUE);
55 } catch (TaskIOException e) {
56 throw new RuntimeException(e);
57 }
58 }
59
60 public static void assertSimilar(PDDocument doc1, PDDocument doc2) {
61 assertSimilar(doc1, doc2, Integer.MAX_VALUE);
62 }
63
64 public static void assertSimilar(PDDocument doc1, PDDocument doc2, int maxNumberOfPages) {
65 try {
66 if (doc1.getNumberOfPages() != doc2.getNumberOfPages()) {
67 Assert.fail("Documents have different number of pages:" + doc1.getNumberOfPages() + ", "
68 + doc2.getNumberOfPages());
69 }
70
71 int numOfPages = Math.min(doc1.getNumberOfPages(), maxNumberOfPages);
72
73 for (int i = 0; i < numOfPages; i++) {
74 LOG.info("Comparing page " + (i + 1));
75
76 BufferedImage p1 = takeScreenshotOf(doc1, i);
77 BufferedImage p2 = takeScreenshotOf(doc2, i);
78 double similary = pixelSimilarityOf(p1, p2);
79
80 if (similary < 99.5d) {
81 File folder = IOUtils.createTemporaryFolder();
82 File f1 = new File(folder, "p" + (i + 1) + "_1.png");
83 File f2 = new File(folder, "p" + (i + 1) + "_2.png");
84 ImageIO.write(p1, "png", f1);
85 ImageIO.write(p2, "png", f2);
86 LOG.error("Troubleshoot:\n" + f1 + "\n" + f2);
87
88 Assert.fail("Page " + (i + 1) + " differs, similarity is: " + pixelSimilarityOf(p1, p2));
89 }
90 }
91 } catch (Exception e) {
92 throw new RuntimeException(e);
93 }
94 }
95
96 public static BufferedImage takeScreenshotOf(PDDocument doc, int pageIndex) throws IOException {
97 PDFRenderer pdfRenderer = new PDFRenderer(doc);
98 return pdfRenderer.renderImageWithDPI(pageIndex, 150);
99 }
100
101 private static BufferedImage resizeTo(BufferedImage image, int width) throws IOException {
102 return Thumbnails.of(image).scalingMode(ScalingMode.PROGRESSIVE_BILINEAR).width(width)
103 .asBufferedImage();
104 }
105
106 private static double pixelSimilarityOf(BufferedImage src1, BufferedImage src2) throws IOException {
107 BufferedImage img1 = resizeTo(src1, 1024);
108 BufferedImage img2 = resizeTo(src2, 1024);
109
110 double difference = 0d;
111 for (int x = 0; x < img1.getWidth(); x++) {
112 for (int y = 0; y < img1.getHeight(); y++) {
113 int rgbA = img1.getRGB(x, y);
114 int rgbB = img2.getRGB(x, y);
115 int redA = (rgbA >> 16) & 0xff;
116 int greenA = (rgbA >> 8) & 0xff;
117 int blueA = (rgbA) & 0xff;
118 int redB = (rgbB >> 16) & 0xff;
119 int greenB = (rgbB >> 8) & 0xff;
120 int blueB = (rgbB) & 0xff;
121 difference += Math.abs(redA - redB);
122 difference += Math.abs(greenA - greenB);
123 difference += Math.abs(blueA - blueB);
124 }
125 }
126
127 // Total number of red pixels = width * height
128 // Total number of blue pixels = width * height
129 // Total number of green pixels = width * height
130 // So total number of pixels = width * height * 3
131 double totalPixels = img1.getWidth() * img1.getHeight() * 3d;
132
133 // Normalizing the value of different pixels
134 // for accuracy(average pixels per color
135 // component)
136 double avgDifferentPixels = difference / totalPixels;
137
138 // There are 255 values of pixels in total
139 double percentage = (avgDifferentPixels / 255) * 100d;
140
141 return 100 - percentage;
142 }
143 }