static BufferedImage cutOutRegionFromImage(IImageRegion region) { if (region == null) null; var fullImage = toBufferedImage(region.image()); var r = region.bounds(); var clipped = clipBufferedImage(fullImage, r); int[] pixels = pixelsOfBufferedImage(clipped); int w = r.w, h = r.h, i = 0; for y to h: for x to w: { if (!region.contains(r.x+x, r.y+y)) pixels[i] = 0; ++i; } ret bufferedImageWithAlpha(w, h, pixels); }