srecord noeq CutOutRegionFromImage(IImageRegion region) { settable double alphaNonContained = 0.5; // optional settable BufferedImage fullImage; // grow bounds by this many pixels to include surroundings settable int margin; // optional settable Rect rect; BufferedImage get() { if (region == null) null; fullImage if null = toBufferedImage(region.image()); if (fullImage == null) null; rect = region.bounds(); rect = intersectRects(growRect(rect, margin), imageRect(fullImage)); var clipped = clipBufferedImage(fullImage, rect); int[] pixels = pixelsOfBufferedImage(clipped); int w = rect.w, h = rect.h, i = 0; for y to h: for x to w: { if (!region.contains(rect.x+x, rect.y+y)) pixels[i] = withAlpha(alphaNonContained, pixels[i]); ++i; } ret bufferedImageWithAlpha(w, h, pixels); } }