You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by no...@apache.org on 2011/07/25 11:56:50 UTC

svn commit: r1150611 - /pivot/trunk/wtk/src/org/apache/pivot/wtk/effects/GrayscaleDecorator.java

Author: noelgrandin
Date: Mon Jul 25 09:56:49 2011
New Revision: 1150611

URL: http://svn.apache.org/viewvc?rev=1150611&view=rev
Log:
PIVOT-723 Better GrayscaleDecorator

Modified:
    pivot/trunk/wtk/src/org/apache/pivot/wtk/effects/GrayscaleDecorator.java

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/effects/GrayscaleDecorator.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/effects/GrayscaleDecorator.java?rev=1150611&r1=1150610&r2=1150611&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/effects/GrayscaleDecorator.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/effects/GrayscaleDecorator.java Mon Jul 25 09:56:49 2011
@@ -17,13 +17,17 @@
 package org.apache.pivot.wtk.effects;
 
 import java.awt.Graphics2D;
+import java.awt.Transparency;
+import java.awt.color.ColorSpace;
 import java.awt.geom.AffineTransform;
 import java.awt.image.BufferedImage;
+import java.awt.image.ComponentColorModel;
+import java.awt.image.DataBuffer;
+import java.awt.image.WritableRaster;
 
 import org.apache.pivot.wtk.Bounds;
 import org.apache.pivot.wtk.Component;
 
-
 /**
  * Decorator that applies a grayscale conversion to a component.
  */
@@ -40,10 +44,21 @@ public class GrayscaleDecorator implemen
         int width = component.getWidth();
         int height = component.getHeight();
 
-        if (bufferedImage == null
-            || bufferedImage.getWidth() < width
+        /* To convert to gray, we create a BufferedImage in the grayscale color
+         * space into which the decorated component draws, and we output the
+         * resulting image. The naive way to create the buffer is new
+         * BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); but that
+         * doesn't respect transparency. Hence the following more complicated
+         * method.
+         */
+
+        if (bufferedImage == null || bufferedImage.getWidth() < width
             || bufferedImage.getHeight() < height) {
-            bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
+            ColorSpace gsColorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
+            ComponentColorModel ccm = new ComponentColorModel(gsColorSpace, true, false,
+                Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
+            WritableRaster raster = ccm.createCompatibleWritableRaster(width, height);
+            bufferedImage = new BufferedImage(ccm, raster, ccm.isAlphaPremultiplied(), null);
         }
 
         bufferedImageGraphics = bufferedImage.createGraphics();