CoordinatesType resultCoordinatesType) { if (region.isSizeEmpty()) { return new Region(region); if (intersectedRegion.isSizeEmpty()) { return intersectedRegion;
if (region.isSizeEmpty()) { return new Region(region); if (region.isSizeEmpty()) { return region;
@Override public EyesWebDriverScreenshot getSubScreenshot(Region region, boolean throwIfClipped) { logger.verbose(String.format("getSubScreenshot([%s], %b)", region, throwIfClipped)); ArgumentGuard.notNull(region, "region"); // We calculate intersection based on as-is coordinates. Region asIsSubScreenshotRegion = getIntersectedRegion(region, CoordinatesType.SCREENSHOT_AS_IS); if (asIsSubScreenshotRegion.isSizeEmpty() || (throwIfClipped && !asIsSubScreenshotRegion.getSize().equals( region.getSize()))) { throw new OutOfBoundsException(String.format( "Region [%s] is out of screenshot bounds [%s]", region, frameWindow)); } BufferedImage subScreenshotImage = ImageUtils.getImagePart(image, asIsSubScreenshotRegion); EyesWebDriverScreenshot result = new EyesWebDriverScreenshot(logger, driver, subScreenshotImage, new Region(region.getLeft(), region.getTop(), subScreenshotImage.getWidth(), subScreenshotImage.getHeight())); logger.verbose("Done!"); return result; }
/** * Removes a given region from the image. * @param image The image to crop. * @param regionToCrop The region to crop from the image. * @return A new image without the cropped region. */ public static BufferedImage cropImage(BufferedImage image, Region regionToCrop) { Region imageRegion = new Region(0,0, image.getWidth(), image.getHeight()); imageRegion.intersect(regionToCrop); if (imageRegion.isSizeEmpty()){ logger.log("WARNING - requested cropped area results in zero-size image! Cropped not performed. Returning original image."); return image; } if (!imageRegion.equals(regionToCrop)){ logger.log("WARNING - requested cropped area overflows image boundaries."); } BufferedImage croppedImage = Scalr.crop(image, imageRegion.getLeft(), imageRegion.getTop(), imageRegion.getWidth(), imageRegion.getHeight()); return normalizeImageType(croppedImage); }
private Region getRegionInScreenshot(Region region, BufferedImage image, double pixelRatio) { if (region.isSizeEmpty()) { return region; } logger.verbose("Creating screenshot object..."); // We need the screenshot to be able to convert the region to screenshot coordinates. EyesScreenshot screenshot = screenshotFactory.makeScreenshot(image); logger.verbose("Getting region in screenshot..."); // Region regionInScreenshot = screenshot.convertRegionLocation(regionProvider.getRegion(), regionProvider.getCoordinatesType(), CoordinatesType.SCREENSHOT_AS_IS); Region regionInScreenshot = screenshot.getIntersectedRegion(region, CoordinatesType.SCREENSHOT_AS_IS); logger.verbose("Region in screenshot: " + regionInScreenshot); regionInScreenshot = regionInScreenshot.scale(pixelRatio); logger.verbose("Scaled region: " + regionInScreenshot); regionInScreenshot = regionPositionCompensation.compensateRegionPosition(regionInScreenshot, pixelRatio); // Handling a specific case where the region is actually larger than // the screenshot (e.g., when body width/height are set to 100%, and // an internal div is set to value which is larger than the viewport). regionInScreenshot.intersect(new Region(0, 0, image.getWidth(), image.getHeight())); logger.verbose("Region after intersect: " + regionInScreenshot); return regionInScreenshot; } }
/** * Removes a given region from the image. * @param logger The logger to use. * @param image The image to crop. * @param regionToCrop The region to crop from the image. * @return A new image without the cropped region. */ public static BufferedImage cropImage(Logger logger, BufferedImage image, Region regionToCrop) { Region imageRegion = new Region(0,0, image.getWidth(), image.getHeight()); imageRegion.intersect(regionToCrop); if (imageRegion.isSizeEmpty()){ logger.log("WARNING - requested cropped area results in zero-size image! Cropped not performed. Returning original image."); return image; } if (!imageRegion.equals(regionToCrop)){ logger.log("WARNING - requested cropped area overflows image boundaries."); } BufferedImage croppedImage = Scalr.crop(image, imageRegion.getLeft(), imageRegion.getTop(), imageRegion.getWidth(), imageRegion.getHeight()); return normalizeImageType(croppedImage); }
/** * Get sub screenshot. * @param region The region for which we should get the sub screenshot. * @param throwIfClipped Throw an EyesException if the region is not fully contained in the screenshot. * @return Sub screenshot. */ @Override public EyesScreenshot getSubScreenshot(Region region, boolean throwIfClipped) { ArgumentGuard.notNull(region, "region"); // We want to get the sub-screenshot in as-is coordinates type. Region subScreenshotRegion = getIntersectedRegion(region, CoordinatesType.SCREENSHOT_AS_IS); if (subScreenshotRegion.isSizeEmpty() || (throwIfClipped && !subScreenshotRegion.getSize().equals(region.getSize()))) { throw new OutOfBoundsException(String.format( "Region [%s] is out of screenshot bounds [%s]", region, bounds)); } BufferedImage subScreenshotImage = ImageUtils.getImagePart(image, subScreenshotRegion); // Notice that we need the bounds-relative coordinates as parameter // for new sub-screenshot. Region relativeSubScreenshotRegion = convertRegionLocation(subScreenshotRegion, CoordinatesType.SCREENSHOT_AS_IS, CoordinatesType.CONTEXT_RELATIVE); return new EyesImagesScreenshot(logger, subScreenshotImage, relativeSubScreenshotRegion.getLocation()); }
/** * Converts a region's location coordinates with the {@code from} * coordinates type to the {@code to} coordinates type. * @param region The region which location's coordinates needs to be converted. * @param from The current coordinates type for {@code region}. * @param to The target coordinates type for {@code region}. * @return A new region which is the transformation of {@code region} to * the {@code to} coordinates type. */ public Region convertRegionLocation(Region region, CoordinatesType from, CoordinatesType to) { ArgumentGuard.notNull(region, "region"); if (region.isSizeEmpty()) { return new Region(region); } ArgumentGuard.notNull(from, "from"); ArgumentGuard.notNull(to, "to"); Location originalLocation = region.getLocation(); logger.verbose("original location: " + originalLocation); Location updatedLocation = convertLocation(originalLocation, from, to); logger.verbose("updated location: " + updatedLocation); return new Region(updatedLocation, region.getSize()); } }
/** * Gets the elements region in the screenshot. * @param element The element which region we want to intersect. * @return The intersected region, in {@code SCREENSHOT_AS_IS} coordinates * type. */ public Region getIntersectedRegion(WebElement element) { ArgumentGuard.notNull(element, "element"); Point pl = element.getLocation(); Dimension ds = element.getSize(); Region elementRegion = new Region(pl.getX(), pl.getY(), ds.getWidth(), ds.getHeight()); // Since the element coordinates are in context relative elementRegion = getIntersectedRegion(elementRegion, CoordinatesType.CONTEXT_RELATIVE); if (!elementRegion.isSizeEmpty()) { elementRegion = convertRegionLocation(elementRegion, CoordinatesType.CONTEXT_RELATIVE, CoordinatesType.SCREENSHOT_AS_IS); } return elementRegion; } }
/** * Converts a region's location coordinates with the {@code from} * coordinates type to the {@code to} coordinates type. * @param region The region which location's coordinates needs to be converted. * @param from The current coordinates type for {@code region}. * @param to The target coordinates type for {@code region}. * @return A new region which is the transformation of {@code region} to * the {@code to} coordinates type. */ public Region convertRegionLocation(Region region, CoordinatesType from, CoordinatesType to) { ArgumentGuard.notNull(region, "region"); if (region.isSizeEmpty()) { return new Region(region); } ArgumentGuard.notNull(from, "from"); ArgumentGuard.notNull(to, "to"); Location originalLocation = region.getLocation(); logger.verbose("original location: " + originalLocation); Location updatedLocation = convertLocation(originalLocation, from, to); logger.verbose("updated location: " + updatedLocation); return new Region(updatedLocation, region.getSize()); } }
if (!regionInScreenshot.isSizeEmpty()) { image = ImageUtils.getImagePart(image, regionInScreenshot); saveDebugScreenshotPart(image, region, "cropped"); if (!regionInScreenshot.isSizeEmpty()) { logger.verbose("cropping..."); partImage = ImageUtils.getImagePart(partImage, regionInScreenshot);
while (parent != null && !region.isSizeEmpty()) { Point parentLocation = parent.getLocation(); Dimension parentSize = parent.getSize();
if (!effectiveViewport.isSizeEmpty()) { regionToCheck.intersect(effectiveViewport);