From d595a2fab867ce29a44e551e3599b3f4fed00aee Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Wed, 8 Jan 2003 04:28:13 +0000 Subject: [PATCH] Added a version of composeMaskedImage() that takes a Shape object to use as the mask. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2101 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../com/threerings/media/image/ImageUtil.java | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/java/com/threerings/media/image/ImageUtil.java b/src/java/com/threerings/media/image/ImageUtil.java index 53f3ac34c..bfb30d2da 100644 --- a/src/java/com/threerings/media/image/ImageUtil.java +++ b/src/java/com/threerings/media/image/ImageUtil.java @@ -1,5 +1,5 @@ // -// $Id: ImageUtil.java,v 1.20 2003/01/08 04:09:02 mdb Exp $ +// $Id: ImageUtil.java,v 1.21 2003/01/08 04:28:13 ray Exp $ package com.threerings.media.image; @@ -449,6 +449,43 @@ public class ImageUtil } } + /** + * Create a new image using the supplied shape as a mask from which to + * cut out pixels from the supplied image. Pixels inside the shape + * will be added to the final image, pixels outside the shape will be + * clear. + */ + public static BufferedImage composeMaskedImage ( + Shape mask, BufferedImage base) + { + int wid = base.getWidth(); + int hei = base.getHeight(); + + // alternate method for composition: + // 1. create WriteableRaster with base data + // 2. test each pixel with mask.contains() and set the alpha + // channel to fully-alpha if false + // 3. create buffered image from raster + // (I didn't use this method because it depends on the colormodel + // of the source image, and was booching when the souce image was + // a cut-up from a tileset, and it seems like it would take + // longer than the method we are using. + // But it's something to consider) + + // composite them by rendering them with an alpha rule + BufferedImage target = createImage(wid, hei); + Graphics2D g2 = target.createGraphics(); + try { + g2.setColor(Color.BLACK); // whatever, really + g2.fill(mask); + g2.setComposite(AlphaComposite.SrcIn); + g2.drawImage(base, 0, 0, null); + } finally { + g2.dispose(); + } + return target; + } + /** * Returns true if the supplied image contains a non-transparent pixel * at the specified coordinates, false otherwise.