ImageUtils { public static boolean isContained(BufferedImage entireImage, BufferedImage partImage) { double[][] integralImage = calcIntegralImage(entireImage); double sumContent = 0; Raster r = partImage.getRaster(); int[] dArray = new int[r.getNumDataElements()]; for (int x = 0; x < r.getWidth(); x++) { for (int y = 0; y < r.getHeight(); y++) { sumContent += r.getPixel(x, y, dArray)[0]; } } int contentWidth = partImage.getWidth(); int contentHeight = partImage.getHeight(); double p0; double p1; double p2; double p3; double sumContainer; final int yMax = entireImage.getHeight() - partImage.getHeight() + 1; final int xMax = entireImage.getWidth() - partImage.getWidth() + 1; for (int y = 0; y < yMax; y++) { for (int x = 0; x < xMax; x++) { p0 = integralImage[y + contentHeight - 1][x + contentWidth - 1]; p1 = (x == 0) ? 0 : integralImage[y + contentHeight - 1][x - 1]; p2 = (y == 0) ? 0 : integralImage[y - 1][x + contentWidth - 1]; p3 = (x == 0 || y == 0) ? 0 : integralImage[y - 1][x - 1]; sumContainer = p0 - p1 - p2 + p3; if (Double.compare(sumContainer, sumContent) == 0) { BufferedImage window = entireImage.getSubimage(x, y, contentWidth, contentHeight); if (imageEquals(window, partImage)) { return true; } } } } return false; } private  ImageUtils(); static ImageComparedResult compare(BufferedImage image1, Rectangle imageArea1, BufferedImage image2,
			Rectangle imageArea2, CompareOption[] options); static boolean isContained(BufferedImage entireImage, BufferedImage partImage); static double[][] calcIntegralImage(BufferedImage source); static boolean imageEquals(BufferedImage image1, BufferedImage image2); static BufferedImage getMaskedImage(BufferedImage image, List<Rectangle> maskAreas); static BufferedImage getMarkedImage(BufferedImage image, DiffPoints diffPoints); static BufferedImage getDiffImage(BufferedImage leftImage, BufferedImage rightImage, DiffPoints diffPoints); static BufferedImage trim(BufferedImage image, int trimTop, int trimLeft, int trimBottom, int trimRight); static BufferedImage verticalMerge(List<BufferedImage> images); static BufferedImage merge(List<List<BufferedImage>> images); static int[] getRGB(BufferedImage image, int width, int height); static int[] toARGB(int pixel); static int toPixel(int[] argb); static int toPixel(int r, int g, int b); static int toPixel(int a, int r, int g, int b); static int detectBackgroundColor(BufferedImage b); static boolean getObjectRectangle(BufferedImage image, Rectangle rectangle); static double norm(int pixel1, int pixel2); static double[] countSubpixel(BufferedImage bimage); static void removeRedundantRectangles(List<Rectangle> rectangles, int xLimit, int yLimit); static void removeOverlappingRectangles(List<Rectangle> rectangles); static void reshapeRect(Rectangle rectangle, int xLimit, int yLimit); static Rectangle getTightDiffArea(Rectangle rectangle, int xLimit, int yLimit); static List<Rectangle> convertObjectGroupsToAreas(List<ObjectGroup> objectGroups); static List<ObjectGroup> convertDiffPointsToObjectGroups(DiffPoints DP, int group_distance); static BufferedImage getSubImage(BufferedImage image, Rectangle rectangle); static Offset findDominantOffset(BufferedImage expectedImage, BufferedImage actualImage, double diffThreshold); static BufferedImage getDominantImage(BufferedImage expectedImage, BufferedImage actualImage, Offset offset); static int getSizeRelationType(int expectedWidth, int expectedHeight, int actualWidth, int actualHeight);  }
@Test public void testIsContained_contained() throws Exception { BufferedImage entireImage = ImageIO.read(getClass().getResource("hifive_logo.png")); BufferedImage partImage = ImageIO.read(getClass().getResource("hifive_logo_part.png")); assertThat(ImageUtils.isContained(entireImage, partImage), is(true)); }
@Test public void testIsContained_NOT_contained() throws Exception { BufferedImage entireImage = ImageIO.read(getClass().getResource("hifive_logo.png")); BufferedImage partImage = ImageIO.read(getClass().getResource("hifive_logo_not_part.png")); assertThat(ImageUtils.isContained(entireImage, partImage), is(false)); }