java version "1.6.0_10" Java(TM) SE Runtime Environment (build 1.6.0_10-b33) Java HotSpot(TM) Client VM (build 11.0-b15, mixed mode, sharing)
OS: Windows XP sp3 /** * test for scaling tranparent image * When draw a transparent Image to a BuffferedImage with IndexColorModel, the transparent color would loss if we change * the default setting of RenderingHints (KEY_INTERPOLATION or KEY_RENDERING). * This issue also appeared when use the Image's getScaledInstance method if the image is a transparent BufferedImage with * IndexColorModel * I'm not sure if this issue relate to the BUG: * http://bugs.sun.com/bugdatabase/view_bug.do;jsessionid=4fdef921e0d1bf1b187722c623502?bug_id=4950176 * * Sorry for my English * * @author Eastsun * @date 2008-11-26 */ The code as follow: [code] import java.awt.AlphaComposite; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.awt.image.IndexColorModel; import java.awt.image.WritableRaster; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ScaleTransparentImage { private static int width, height; public static void main(String[] args) throws IOException { BufferedImage image = ImageIO.read(new File("duke.png")); double scale = 0.8; width = (int) (image.getWidth() * scale); height = (int) (image.getHeight() * scale); System.out.println(image.getColorModel() instanceof IndexColorModel); scaleA(image, "scaleA.png"); scaleB(image, "scaleB.png"); scaleC(image, "scaleC.png"); } public static void scaleA(BufferedImage src, String dstFile) throws IOException { BufferedImage dst = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = dst.createGraphics(); g2d.setComposite(AlphaComposite.Src); g2d.drawImage(src, 0, 0, width, height, null); g2d.dispose(); ImageIO.write(dst, "png", new File(dstFile)); int rgb = dst.getRGB(0, 0); System.out.println("RGB :" + ((rgb >> 16) & 0xff) + "," + ((rgb >> 8) & 0xff) + "," + (rgb & 0xff)); } public static void scaleB(BufferedImage src, String dstFile) throws IOException { IndexColorModel icm = (IndexColorModel) src.getColorModel(); WritableRaster wr = icm.createCompatibleWritableRaster(width, height); BufferedImage dst = new BufferedImage(icm, wr, false, null); Graphics2D g2d = dst.createGraphics(); g2d.setComposite(AlphaComposite.Src); g2d.drawImage(src, 0, 0, width, height, null); g2d.dispose(); ImageIO.write(dst, "png", new File(dstFile)); int rgb = dst.getRGB(0, 0); System.out.println("RGB :" + ((rgb >> 16) & 0xff) + "," + ((rgb >> 8) & 0xff) + "," + (rgb & 0xff)); } public static void scaleC(BufferedImage src, String dstFile) throws IOException { IndexColorModel icm = (IndexColorModel) src.getColorModel(); WritableRaster wr = icm.createCompatibleWritableRaster(width, height); BufferedImage dst = new BufferedImage(icm, wr, false, null); Graphics2D g2d = dst.createGraphics(); g2d.setComposite(AlphaComposite.Src); //same effect with RenderingHints.KEY_INTERPOLATION g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2d.drawImage(src, 0, 0, width, height, null); g2d.dispose(); ImageIO.write(dst, "png", new File(dstFile)); int rgb = dst.getRGB(0, 0); System.out.println("RGB :" + ((rgb >> 16) & 0xff) + "," + ((rgb >> 8) & 0xff) + "," + (rgb & 0xff)); } } [/code] [Message sent by forum member 'java2x' (java2x)] http://forums.java.net/jive/thread.jspa?messageID=318973 =========================================================================== To unsubscribe, send email to [EMAIL PROTECTED] and include in the body of the message "signoff JAVA2D-INTEREST". For general help, send email to [EMAIL PROTECTED] and include in the body of the message "help".
