diff --git a/jlatexmath-example-export/src/test/java/org/scilab/forge/jlatexmath/examples/export/ExamplesTest.java b/jlatexmath-example-export/src/test/java/org/scilab/forge/jlatexmath/examples/export/ExamplesTest.java index 4525ad13..09f806fe 100644 --- a/jlatexmath-example-export/src/test/java/org/scilab/forge/jlatexmath/examples/export/ExamplesTest.java +++ b/jlatexmath-example-export/src/test/java/org/scilab/forge/jlatexmath/examples/export/ExamplesTest.java @@ -46,7 +46,6 @@ import static org.junit.Assert.assertTrue; -import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; @@ -64,6 +63,7 @@ import org.junit.Ignore; import org.junit.Test; import org.junit.runners.MethodSorters; +import org.scilab.forge.jlatexmath.internal.util.Images; @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class ExamplesTest { @@ -157,11 +157,11 @@ private static void check(String filename) { System.out.println("checking image " + filename); BufferedImage a = ImageIO.read(new File("src/test/resources/expected/" + filename)); BufferedImage b = ImageIO.read(new File("target/" + filename)); - double distance = distance(a, b); + double distance = Images.distance(a, b); System.out.println("distance=" + distance); // TODO establish a reasonable threshold after running the tests on // different platforms (windows, osx, linux, others?) - double THRESHOLD = 40; + final double THRESHOLD = Images.DISTANCE_THRESHOLD; assertTrue("actual and expected images for " + filename + " are different sizes!", distance >= 0); assertTrue("distance is above threshold, images are probably significantly different, distance=" @@ -172,33 +172,4 @@ private static void check(String filename) { } } - private static double distance(BufferedImage imgA, BufferedImage imgB) { - // The images must be the same size. - if (imgA.getWidth() == imgB.getWidth() && imgA.getHeight() == imgB.getHeight()) { - int width = imgA.getWidth(); - int height = imgA.getHeight(); - - double mse = 0; - // Loop over every pixel. - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - Color ca = new Color(imgA.getRGB(x, y)); - Color cb = new Color(imgB.getRGB(x, y)); - double variance = sqr(ca.getRed() - cb.getRed()) // - + sqr(ca.getBlue() - cb.getBlue()) // - + sqr(ca.getGreen() - cb.getGreen()) // - + sqr(ca.getAlpha() - cb.getAlpha()); - mse += variance; - } - } - return Math.sqrt(mse / height / width); - } else { - return -1; - } - } - - private static double sqr(double x) { - return x * x; - } - } diff --git a/jlatexmath/src/main/java/org/scilab/forge/jlatexmath/internal/util/Images.java b/jlatexmath/src/main/java/org/scilab/forge/jlatexmath/internal/util/Images.java new file mode 100644 index 00000000..2786c381 --- /dev/null +++ b/jlatexmath/src/main/java/org/scilab/forge/jlatexmath/internal/util/Images.java @@ -0,0 +1,44 @@ +package org.scilab.forge.jlatexmath.internal.util; + +import java.awt.Color; +import java.awt.image.BufferedImage; + +public final class Images { + + private Images() { + //prevent instantiation + } + + public static double DISTANCE_THRESHOLD=40; + + public static double distance(BufferedImage imgA, BufferedImage imgB) { + // The images must be the same size. + if (imgA.getWidth() == imgB.getWidth() && imgA.getHeight() == imgB.getHeight()) { + int width = imgA.getWidth(); + int height = imgA.getHeight(); + + double mse = 0; + // Loop over every pixel. + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + Color ca = new Color(imgA.getRGB(x, y)); + Color cb = new Color(imgB.getRGB(x, y)); + double variance = sqr(ca.getRed() - cb.getRed()) // + + sqr(ca.getBlue() - cb.getBlue()) // + + sqr(ca.getGreen() - cb.getGreen()) // + + sqr(ca.getAlpha() - cb.getAlpha()); + mse += variance; + } + } + return Math.sqrt(mse / height / width); + } else { + return -1; + } + } + + private static double sqr(double x) { + return x * x; + } + + +} diff --git a/jlatexmath/src/test/java/org/scilab/forge/jlatexmath/examples/basic/ExamplesTest.java b/jlatexmath/src/test/java/org/scilab/forge/jlatexmath/examples/basic/ExamplesTest.java index 5e716848..2c704b5e 100644 --- a/jlatexmath/src/test/java/org/scilab/forge/jlatexmath/examples/basic/ExamplesTest.java +++ b/jlatexmath/src/test/java/org/scilab/forge/jlatexmath/examples/basic/ExamplesTest.java @@ -46,7 +46,6 @@ import static org.junit.Assert.assertTrue; -import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; @@ -54,6 +53,7 @@ import javax.imageio.ImageIO; import org.junit.Test; +import org.scilab.forge.jlatexmath.internal.util.Images; public class ExamplesTest { @@ -98,46 +98,20 @@ private static void check(String filename) { System.out.println("checking image " + filename); BufferedImage a = ImageIO.read(new File("src/test/resources/expected/" + filename)); BufferedImage b = ImageIO.read(new File("target/" + filename)); - double distance = distance(a, b); + double distance = Images.distance(a, b); System.out.println("distance=" + distance); // TODO establish a reasonable threshold after running the tests on // different platforms (windows, osx, linux, others?) and different // jdks - double THRESHOLD = 40; + double THRESHOLD = Images.DISTANCE_THRESHOLD; assertTrue("actual and expected images for " + filename + " are different sizes!", distance >= 0); - assertTrue("distance is above threshold, images are probably significantly different, distance=" + distance, + assertTrue( + "distance=" + distance + " is above threshold=" + THRESHOLD + + ", images are probably significantly different, distance=" + distance, distance <= THRESHOLD); } catch (IOException e) { throw new RuntimeException(e); } } - private static double distance(BufferedImage imgA, BufferedImage imgB) { - // The images must be the same size. - if (imgA.getWidth() == imgB.getWidth() && imgA.getHeight() == imgB.getHeight()) { - int width = imgA.getWidth(); - int height = imgA.getHeight(); - - double mse = 0; - // Loop over every pixel. - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - Color ca = new Color(imgA.getRGB(x, y)); - Color cb = new Color(imgB.getRGB(x, y)); - double variance = sqr(ca.getRed() - cb.getRed()) // - + sqr(ca.getBlue() - cb.getBlue()) // - + sqr(ca.getGreen() - cb.getGreen()) // - + sqr(ca.getAlpha() - cb.getAlpha()); - mse += variance; - } - } - return Math.sqrt(mse / height / width); - } else { - return -1; - } - } - - private static double sqr(double x) { - return x * x; - } -} +} \ No newline at end of file