From d4df933e2841f5a2f4f23eecee26aebd4fc1f262 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Sat, 6 Apr 2002 03:41:18 +0000 Subject: [PATCH] added composeMaskedImage() to composite the alpha from one image and the RGB from a second. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1202 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../com/threerings/media/image/ImageUtil.java | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/java/com/threerings/media/image/ImageUtil.java b/src/java/com/threerings/media/image/ImageUtil.java index 04297047c..f32c4f238 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.7 2002/03/08 21:05:01 mdb Exp $ +// $Id: ImageUtil.java,v 1.8 2002/04/06 03:41:18 ray Exp $ package com.threerings.media.util; @@ -15,6 +15,8 @@ import java.awt.image.BufferedImage; import java.awt.image.ColorModel; import java.awt.image.DataBuffer; import java.awt.image.IndexColorModel; +import java.awt.image.Raster; +import java.awt.image.WritableRaster; import com.samskivert.util.StringUtil; import com.threerings.media.Log; @@ -189,6 +191,35 @@ public class ImageUtil return Color.HSBtoRGB(hsv[0], hsv[1], hsv[2]); } + /** + * Create an image using the alpha channel from the first Image + * and the RGB values from the second. + */ + public static BufferedImage composeMaskedImage (BufferedImage mask, + BufferedImage base) + { + int wid = base.getWidth(null); + int hei = base.getHeight(null); + + Raster maskdata = mask.getData(); + Raster basedata = base.getData(); + + WritableRaster target = basedata.createCompatibleWritableRaster( + wid, hei); + + // copy the alpha from the mask image + int[] adata = maskdata.getSamples(0, 0, wid, hei, 3, (int[]) null); + target.setSamples(0, 0, wid, hei, 3, adata); + + // copy the RGB from the base image + for (int ii=0; ii < 3; ii++) { + int[] cdata = basedata.getSamples(0, 0, wid, hei, ii, (int[]) null); + target.setSamples(0, 0, wid, hei, ii, cdata); + } + + return new BufferedImage(mask.getColorModel(), target, true, null); + } + /** * Converts floating point HSV values to a fixed point integer * representation.