1.1 --- a/openide.util/src/org/openide/util/IconManager.java Mon Dec 11 14:29:15 2006 +0000
1.2 +++ b/openide.util/src/org/openide/util/IconManager.java Mon Dec 11 14:30:45 2006 +0000
1.3 @@ -23,6 +23,7 @@
1.4 import java.awt.Image;
1.5 import java.awt.MediaTracker;
1.6 import java.awt.Toolkit;
1.7 +import java.awt.Transparency;
1.8 import java.awt.image.BufferedImage;
1.9 import java.awt.image.ImageObserver;
1.10 import java.lang.ref.SoftReference;
1.11 @@ -336,10 +337,12 @@
1.12
1.13 int w = Math.max(image1.getWidth(null), x + image2.getWidth(null));
1.14 int h = Math.max(image1.getHeight(null), y + image2.getHeight(null));
1.15 + boolean bitmask = (image1 instanceof Transparency) && ((Transparency)image1).getTransparency() != Transparency.TRANSLUCENT
1.16 + && (image2 instanceof Transparency) && ((Transparency)image2).getTransparency() != Transparency.TRANSLUCENT;
1.17
1.18 java.awt.image.ColorModel model = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment()
1.19 .getDefaultScreenDevice().getDefaultConfiguration()
1.20 - .getColorModel(java.awt.Transparency.BITMASK);
1.21 + .getColorModel(bitmask? Transparency.BITMASK: Transparency.TRANSLUCENT);
1.22 java.awt.image.BufferedImage buffImage = new java.awt.image.BufferedImage(
1.23 model, model.createCompatibleWritableRaster(w, h), model.isAlphaPremultiplied(), null
1.24 );
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/openide.util/test/unit/src/org/openide/util/IconManagerTest.java Mon Dec 11 14:30:45 2006 +0000
2.3 @@ -0,0 +1,94 @@
2.4 +/*
2.5 + * The contents of this file are subject to the terms of the Common Development
2.6 + * and Distribution License (the License). You may not use this file except in
2.7 + * compliance with the License.
2.8 + *
2.9 + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
2.10 + * or http://www.netbeans.org/cddl.txt.
2.11 + *
2.12 + * When distributing Covered Code, include this CDDL Header Notice in each file
2.13 + * and include the License file at http://www.netbeans.org/cddl.txt.
2.14 + * If applicable, add the following below the CDDL Header, with the fields
2.15 + * enclosed by brackets [] replaced by your own identifying information:
2.16 + * "Portions Copyrighted [year] [name of copyright owner]"
2.17 + *
2.18 + * The Original Software is NetBeans. The Initial Developer of the Original
2.19 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
2.20 + * Microsystems, Inc. All Rights Reserved.
2.21 + */
2.22 +package org.openide.util;
2.23 +
2.24 +import java.awt.Color;
2.25 +import java.awt.Image;
2.26 +import java.awt.Transparency;
2.27 +import java.awt.image.BufferedImage;
2.28 +import junit.framework.*;
2.29 +import java.lang.ref.*;
2.30 +import java.util.*;
2.31 +
2.32 +/**
2.33 + *
2.34 + * @author Radim Kubacki
2.35 + */
2.36 +public class IconManagerTest extends TestCase {
2.37 +
2.38 + public IconManagerTest (String testName) {
2.39 + super (testName);
2.40 + }
2.41 +
2.42 + public void testMergeImages() throws Exception {
2.43 + // test if merged image preserves alpha (#90862)
2.44 + BufferedImage img1 = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
2.45 +// System.out.println("img1 transparency "+img1.getTransparency());
2.46 + java.awt.Graphics2D g = img1.createGraphics();
2.47 + Color c = new Color(255, 255, 255, 128);
2.48 + g.setColor(c);
2.49 + g.fillRect(0, 0, 16, 16);
2.50 + g.dispose();
2.51 +
2.52 + BufferedImage img2 = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
2.53 +// System.out.println("img2 transparency "+img2.getTransparency());
2.54 + g = img2.createGraphics();
2.55 + c = new Color(255, 255, 255);
2.56 + g.setColor(c);
2.57 + g.fillRect(0, 0, 2, 2);
2.58 + g.dispose();
2.59 +
2.60 + Image mergedImg = IconManager.mergeImages(img1, img2, 0, 0);
2.61 + if (!(mergedImg instanceof BufferedImage)) {
2.62 + fail("It is assumed that mergeImages returns BufferedImage. Need to update test");
2.63 + }
2.64 +
2.65 + BufferedImage merged = (BufferedImage)mergedImg;
2.66 +// System.out.println("pixels " + Integer.toHexString(merged.getRGB(10, 10)) +", "+ Integer.toHexString(merged.getRGB(0, 0)));
2.67 + assertNotSame("transparency has to be kept for pixel <1,1>", merged.getRGB(10, 10), merged.getRGB(0, 0));
2.68 + }
2.69 +
2.70 + public void testMergeBitmaskImages() throws Exception {
2.71 + // test if two bitmask images are merged to bitmask again to avoid use of alpha channel
2.72 + BufferedImage img1 = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
2.73 +// System.out.println("img1 transparency "+img1.getTransparency());
2.74 + java.awt.Graphics2D g = img1.createGraphics();
2.75 + g.setColor(Color.BLUE);
2.76 + g.fillRect(0, 0, 16, 16);
2.77 + g.dispose();
2.78 +
2.79 + BufferedImage img2 = new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB);
2.80 +// System.out.println("img2 transparency "+img2.getTransparency());
2.81 + g = img2.createGraphics();
2.82 + g.setColor(Color.RED);
2.83 + g.fillRect(0, 0, 2, 2);
2.84 + g.dispose();
2.85 +
2.86 + Image mergedImg = IconManager.mergeImages(img1, img2, 0, 0);
2.87 + if (!(mergedImg instanceof BufferedImage)) {
2.88 + fail("It is assumed that mergeImages returns BufferedImage. Need to update test");
2.89 + }
2.90 +
2.91 + BufferedImage merged = (BufferedImage)mergedImg;
2.92 + assertEquals("Should create bitmask image", Transparency.BITMASK, merged.getTransparency());
2.93 + assertEquals(Color.RED, new Color(merged.getRGB(1, 1)));
2.94 + assertEquals(Color.BLUE, new Color(merged.getRGB(10, 10)));
2.95 + }
2.96 +
2.97 +}