/** * 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); }
/** * Get an offset region. * @param dx The X axis offset. * @param dy The Y axis offset. * @return A region with an offset location. */ public Region offset(int dx, int dy) { return new Region(getLocation().offset(dx, dy), getSize(), getCoordinatesType()); }
/** * @see #getSubRegions(RectangleSize, boolean) . * * {@code isFixedSize} defaults to {@code false}. */ public Iterable<Region> getSubRegions(RectangleSize subRegionSize) { return getSubRegions(subRegionSize, false); }
protected void makeEmpty() { left = EMPTY.getLeft(); top = EMPTY.getTop(); width = EMPTY.getWidth(); height = EMPTY.getHeight(); this.coordinatesType = EMPTY.getCoordinatesType(); }
@Override public Region getRegion() { return new Region(targetRegion.getLocation(), targetRegion.getSize(), CoordinatesType.CONTEXT_RELATIVE); } }, name, false, checkSettings);
if (!regionInScreenshot.isSizeEmpty()) { image = ImageUtils.getImagePart(image, regionInScreenshot); saveDebugScreenshotPart(image, region, "cropped"); if (fullArea == null || fullArea.isEmpty()) { RectangleSize entireSize; try { fullArea = new Region(Location.ZERO, entireSize); Iterable<Region> imageParts = fullArea.getSubRegions(partImageSize); fullArea.getWidth(), fullArea.getHeight(), image.getType()); Location originPosition = positionProvider.setPosition(partRegion.getLocation()); Location targetPosition = originPosition.offset(-fullArea.getLeft(), -fullArea.getTop()); logger.verbose(String.format("Origin Position is set to %s", originPosition)); logger.verbose(String.format("Target Position is %s", targetPosition)); if (!regionInScreenshot.isSizeEmpty()) { logger.verbose("cropping..."); partImage = ImageUtils.getImagePart(partImage, regionInScreenshot); logger.verbose("Extracted entire size: " + fullArea.getSize()); logger.verbose("Actual stitched size: " + actualImageWidth + "x" + actualImageHeight); logger.verbose("Trimming unnecessary margins..."); stitchedImage = ImageUtils.getImagePart(stitchedImage, new Region(0, 0,
/** * Replaces this region with the intersection of itself and {@code other} * @param other The region with which to intersect. */ public void intersect(Region other) { // If there's no intersection set this as the Empty region. if (!isIntersected(other)) { makeEmpty(); return; } // The regions intersect. So let's first find the left & top values int otherLeft = other.getLeft(); int otherTop = other.getTop(); int intersectionLeft = (left >= otherLeft) ? left : otherLeft; int intersectionTop = (top >= otherTop) ? top : otherTop; // Now the width and height of the intersect int right = left + width; int otherRight = otherLeft + other.getWidth(); int intersectionRight = (right <= otherRight) ? right : otherRight; int intersectionWidth = intersectionRight - intersectionLeft; int bottom = top + height; int otherBottom = otherTop + other.getHeight(); int intersectionBottom = (bottom <= otherBottom) ? bottom : otherBottom; int intersectionHeight = intersectionBottom - intersectionTop; left = intersectionLeft; top = intersectionTop; width = intersectionWidth; height = intersectionHeight; }
/** * 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. */ protected Region convertRegionLocation(Region region, CoordinatesType from, CoordinatesType to) { ArgumentGuard.notNull(region, "region"); if (region.isEmpty()) { return new Region(region); } ArgumentGuard.notNull(from, "from"); ArgumentGuard.notNull(to, "to"); Location updatedLocation = convertLocation(region.getLocation(), from, to); return new Region(updatedLocation, region.getSize()); } }
if (region.isSizeEmpty()) { return new Region(region); region.getCoordinatesType(), CoordinatesType.CONTEXT_RELATIVE); intersectedRegion.intersect(bounds); if (region.isSizeEmpty()) { return region; intersectedRegion.setLocation( convertLocation(intersectedRegion.getLocation(), CoordinatesType.CONTEXT_RELATIVE, resultCoordinatesType));
/** * 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()); } }
public Region expandToContain(Region region) { int left = Math.min(this.left, region.left); int top = Math.min(this.top, region.top); int thisRight = this.left + this.width; int otherRight = region.left + region.width; int maxRight = Math.max(thisRight, otherRight); int width = maxRight - left; int thisBottom = this.top + this.height; int otherBottom = region.top + region.height; int maxBottom = Math.max(thisBottom, otherBottom); int height = maxBottom - top; return new Region(left, top, width, height); }
ArgumentGuard.notNull(originalCoordinatesType, "coordinatesType"); if (region.isEmpty()) { return new Region(region); originalCoordinatesType, CoordinatesType.CONTEXT_RELATIVE); intersectedRegion.intersect(bounds); if (region.isEmpty()) { return region; intersectedRegion.setLocation( convertLocation(intersectedRegion.getLocation(), CoordinatesType.CONTEXT_RELATIVE, resultCoordinatesType));
this.frameWindow = new Region(frameLocationInScreenshot, frameSize); this.frameWindow.intersect(new Region(0, 0, image.getWidth(), image.getHeight())); if (this.frameWindow.getWidth() <= 0 || this.frameWindow.getHeight() <= 0) { throw new EyesException("Got empty frame window for screenshot!"); regionWindow = new Region(0, 0, 0, 0); // FIXME: 18/03/2018 Region workaround
@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; }
CoordinatesType resultCoordinatesType) { if (region.isSizeEmpty()) { return new Region(region); CoordinatesType originalCoordinatesType = region.getCoordinatesType(); intersectedRegion.intersect(frameWindow); break; intersectedRegion.intersect(new Region(0, 0, image.getWidth(), image.getHeight())); break; if (intersectedRegion.isSizeEmpty()) { return intersectedRegion;
subRegions.add(new Region(containerRegion)); return subRegions; int right = containerRegion.left + containerRegion.width - 1; CoordinatesType currentType = containerRegion.getCoordinatesType(); subRegions.add(new Region(currentLeft, currentTop, subRegionWidth, subRegionHeight, currentType));
cursorInScreenshot.offset(control.getLocation()); try { cursorInScreenshot = lastScreenshot.getLocationInScreenshot( if (!controlScreenshotIntersect.isEmpty()) { Location l = controlScreenshotIntersect.getLocation(); cursorInScreenshot.offset(-l.getX(), -l.getY());
Point location = element.getLocation(); Dimension size = element.getSize(); Region region = new Region(location.getX(), location.getY(), size.getWidth(), size.getHeight()); WebElement parent; while (parent != null && !region.isSizeEmpty()) { Point parentLocation = parent.getLocation(); Dimension parentSize = parent.getSize(); Region parentRegion = new Region(parentLocation.getX(), parentLocation.getY(), parentSize.getWidth(), parentSize.getHeight()); region.intersect(parentRegion); try { parent = parent.findElement(By.xpath("..")); return region.getSize();