Skip to content

Commit

Permalink
add internally packaged Images class so that distance code in one place
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmoten committed May 14, 2017
1 parent 10146e0 commit fc5aff9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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="
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@

import static org.junit.Assert.assertTrue;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.junit.Test;
import org.scilab.forge.jlatexmath.internal.util.Images;

public class ExamplesTest {

Expand Down Expand Up @@ -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;
}
}
}

0 comments on commit fc5aff9

Please sign in to comment.