IgnoringClearPixelsImageComparator extends ImageComparator<ComparisonParameters> { @Override protected List<Point> compare(BufferedImage image1, BufferedImage image2, int offsetX, int offsetY) { LOG.trace("[Compare] image1[w: {}, h: {}], image2[w: {}, h: {}], offset: ({}, {})", image1.getWidth(), image1.getHeight(), image2.getWidth(), image2.getHeight(), offsetX, offsetY); int width = Math.min(image1.getWidth(), image2.getWidth()); int height = Math.min(image1.getHeight(), image2.getHeight()); int[] rgb1 = ImageUtils.getRGB(image1, width, height); int[] rgb2 = ImageUtils.getRGB(image2, width, height); List<Point> diffPoints = new ArrayList<Point>(); for (int i = 0, length = rgb1.length; i < length; i++) { if (isClear(rgb1[i]) || isClear(rgb2[i])) { LOG.trace("[Compare] is clear. #{} or #{}", Integer.toHexString(rgb1[i]), Integer.toHexString(rgb2[i])); continue; } if (rgb1[i] != rgb2[i]) { int x = (i % width) + offsetX; int y = (i / width) + offsetY; Point diffPoint = new Point(x, y); diffPoints.add(diffPoint); LOG.trace("[Compare] Diff found ({}, {}). #{} <=> #{}", diffPoint.x, diffPoint.y, Integer.toHexString(rgb1[i]), Integer.toHexString(rgb2[i])); } } if (!diffPoints.isEmpty()) { LOG.debug("[Compare] {} diff found.", diffPoints.size()); } return diffPoints; } IgnoringClearPixelsImageComparator();   }
@Test public void testCompare_illegalArgument_image1_null() throws Exception { BufferedImage image = ImageIO.read(getClass().getResource("hifive_logo.png")); Rectangle rectangle = new Rectangle(0, 0, image.getWidth(), image.getHeight()); expectedException.expect(TestRuntimeException.class); new IgnoringClearPixelsImageComparator().compare(null, null, image, rectangle); }
@Test public void testCompare_illegalArgument_image2_null() throws Exception { BufferedImage image = ImageIO.read(getClass().getResource("hifive_logo.png")); Rectangle rectangle = new Rectangle(0, 0, image.getWidth(), image.getHeight()); expectedException.expect(TestRuntimeException.class); new IgnoringClearPixelsImageComparator().compare(image, rectangle, null, null); }
@Test public void testCompare() throws Exception { BufferedImage image = ImageIO.read(getClass().getResource("hifive_logo.png")); Rectangle rectangle = new Rectangle(0, 0, image.getWidth(), image.getHeight()); ImageComparedResult result = new IgnoringClearPixelsImageComparator().compare(image, rectangle, image, rectangle); assertThat(result.isSucceeded(), is(true)); }
@Test public void testCompare_different_image() throws Exception { BufferedImage image1 = ImageIO.read(getClass().getResource("hifive_logo.png")); Rectangle rectangle1 = new Rectangle(0, 0, image1.getWidth(), image1.getHeight()); BufferedImage image2 = ImageIO.read(getClass().getResource("hifive_logo_part.png")); Rectangle rectangle2 = new Rectangle(0, 0, image2.getWidth(), image2.getHeight()); ImageComparedResult result = new IgnoringClearPixelsImageComparator().compare(image1, rectangle1, image2, rectangle2); assertThat(result.isFailed(), is(true)); }
@Test public void testCompare_different_1px() throws Exception { BufferedImage image1 = ImageIO.read(getClass().getResource("hifive_logo.png")); BufferedImage image2 = ImageIO.read(getClass().getResource("hifive_logo.png")); Random random = new Random(); int x = random.nextInt(image2.getWidth()); int y = random.nextInt(image2.getHeight()); image2.setRGB(x, y, image2.getRGB(x, y) - 1); Rectangle rectangle = new Rectangle(0, 0, image1.getWidth(), image2.getHeight()); DiffPoints result = (DiffPoints) new IgnoringClearPixelsImageComparator().compare(image1, rectangle, image2, rectangle); assertThat(result.isFailed(), is(true)); assertThat(result.getDiffPoints().size(), is(1)); assertThat(result.getSizeDiffPoints().isEmpty(), is(true)); assertThat(result.getDiffPoints().get(0), is(new Point(x, y))); }
@Test public void testCompare_different_but_clear_1px() throws Exception { BufferedImage image1 = ImageIO.read(getClass().getResource("hifive_logo.png")); BufferedImage image2 = ImageIO.read(getClass().getResource("hifive_logo.png")); Random random = new Random(); int x = random.nextInt(image2.getWidth()); int y = random.nextInt(image2.getHeight()); image2.setRGB(x, y, (image2.getRGB(x, y) - 1) & 0x00FFFFFF); Rectangle rectangle = new Rectangle(0, 0, image1.getWidth(), image2.getHeight()); ImageComparedResult result = new IgnoringClearPixelsImageComparator().compare(image1, rectangle, image2, rectangle); assertThat(result.isSucceeded(), is(true)); assertThat(result.isFailed(), is(false)); }
@Test public void testCompare_different_but_clear_0xFE_1px() throws Exception { BufferedImage image1 = ImageIO.read(getClass().getResource("hifive_logo.png")); BufferedImage image2 = ImageIO.read(getClass().getResource("hifive_logo.png")); Random random = new Random(); int x = random.nextInt(image2.getWidth()); int y = random.nextInt(image2.getHeight()); image2.setRGB(x, y, (image2.getRGB(x, y) - 1) & 0xFEFFFFFF); Rectangle rectangle = new Rectangle(0, 0, image1.getWidth(), image2.getHeight()); ImageComparedResult result = new IgnoringClearPixelsImageComparator().compare(image1, rectangle, image2, rectangle); assertThat(result.isSucceeded(), is(true)); assertThat(result.isFailed(), is(false)); }
@Test public void testCompare_same_clear_1px() throws Exception { BufferedImage image1 = ImageIO.read(getClass().getResource("hifive_logo.png")); BufferedImage image2 = ImageIO.read(getClass().getResource("hifive_logo.png")); Random random = new Random(); int x = random.nextInt(image2.getWidth()); int y = random.nextInt(image2.getHeight()); image2.setRGB(x, y, (image2.getRGB(x, y)) & 0xFEFFFFFF); Rectangle rectangle = new Rectangle(0, 0, image1.getWidth(), image2.getHeight()); ImageComparedResult result = new IgnoringClearPixelsImageComparator().compare(image1, rectangle, image2, rectangle); assertThat(result.isSucceeded(), is(true)); assertThat(result.isFailed(), is(false)); }
@Test public void testCompare_different_multipxs_all_clear() throws Exception { BufferedImage image1 = ImageIO.read(getClass().getResource("hifive_logo.png")); BufferedImage image2 = ImageIO.read(getClass().getResource("hifive_logo.png")); Random random = new Random(); int x = random.nextInt(image2.getWidth()); int y = random.nextInt(image2.getHeight()); image2.setRGB(x, y, (image2.getRGB(x, y) - 1) & 0xFEFFFFFF); int x2 = (x + 1) % image2.getWidth(); int y2 = (y + 1) % image2.getHeight(); image2.setRGB(x2, y2, (image2.getRGB(x2, y2) - 1) & 0xFEFFFFFF); Rectangle rectangle = new Rectangle(0, 0, image1.getWidth(), image2.getHeight()); ImageComparedResult result = new IgnoringClearPixelsImageComparator().compare(image1, rectangle, image2, rectangle); assertThat(result.isSucceeded(), is(true)); assertThat(result.isFailed(), is(false)); }
@Test public void testCompare_different_multipxs_some_clear() throws Exception { BufferedImage image1 = ImageIO.read(getClass().getResource("hifive_logo.png")); BufferedImage image2 = ImageIO.read(getClass().getResource("hifive_logo.png")); Random random = new Random(); int x = random.nextInt(image2.getWidth()); int y = random.nextInt(image2.getHeight()); image2.setRGB(x, y, (image2.getRGB(x, y) - 1)); int x2 = (x + 1) % image2.getWidth(); int y2 = (y + 1) % image2.getHeight(); image2.setRGB(x2, y2, (image2.getRGB(x2, y2) - 1) & 0xFEFFFFFF); Rectangle rectangle = new Rectangle(0, 0, image1.getWidth(), image2.getHeight()); DiffPoints result = (DiffPoints) new IgnoringClearPixelsImageComparator().compare(image1, rectangle, image2, rectangle); assertThat(result.isFailed(), is(true)); assertThat(result.getDiffPoints().size(), is(1)); assertThat(result.getSizeDiffPoints().isEmpty(), is(true)); assertThat(result.getDiffPoints().get(0), is(new Point(x, y))); }
@Test public void testCompare_different_area() throws Exception { BufferedImage image = ImageIO.read(getClass().getResource("hifive_logo.png")); Rectangle rectangle1 = new Rectangle(0, 0, image.getWidth(), image.getHeight()); Rectangle rectangle2 = new Rectangle(0, 0, image.getWidth() - 1, image.getHeight() - 1); DiffPoints result = (DiffPoints) new IgnoringClearPixelsImageComparator().compare(image, rectangle1, image, rectangle2); assertThat(result.isFailed(), is(true)); assertThat(result.getDiffPoints().isEmpty(), is(true)); assertThat(result.getSizeDiffPoints().isEmpty(), is(false)); Comparator<Point> comparator = new Comparator<Point>() { @Override public int compare(Point o1, Point o2) { if (o1.x != o2.x) return Integer.compare(o1.x, o2.x); if (o1.y != o2.y) return Integer.compare(o1.y, o2.y); return 0; } }; Set<Point> expectedDiffPoints = new TreeSet<Point>(comparator); for (int x = 0; x < image.getWidth(); x++) { expectedDiffPoints.add(new Point(x, image.getHeight())); } for (int y = 0; y < image.getHeight(); y++) { expectedDiffPoints.add(new Point(image.getWidth(), y)); } Set<Point> diffPoints = new TreeSet<Point>(comparator); diffPoints.addAll(result.getSizeDiffPoints()); assertThat(diffPoints, is(expectedDiffPoints)); }