Skip to main content

Problem of scale a transparent BufferedImage with IndexColorModel

2 replies [Last post]
java2x
Offline
Joined: 2006-12-07
Points: 0

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=4fdef921e0d1bf1b1...
*
* Sorry for my English
*
* @author Eastsun
* @date 2008-11-26
*/

The code as follow:

<br />
import java.awt.AlphaComposite;<br />
import java.awt.Graphics2D;<br />
import java.awt.RenderingHints;<br />
import java.awt.image.BufferedImage;<br />
import java.awt.image.IndexColorModel;<br />
import java.awt.image.WritableRaster;<br />
import java.io.File;<br />
import java.io.IOException;<br />
import javax.imageio.ImageIO;</p>
<p>public class ScaleTransparentImage {</p>
<p>    private static int width,  height;</p>
<p>    public static void main(String[] args) throws IOException {<br />
        BufferedImage image = ImageIO.read(new File("duke.png"));<br />
        double scale = 0.8;<br />
        width = (int) (image.getWidth() * scale);<br />
        height = (int) (image.getHeight() * scale);<br />
        System.out.println(image.getColorModel() instanceof IndexColorModel);<br />
        scaleA(image, "scaleA.png");<br />
        scaleB(image, "scaleB.png");<br />
        scaleC(image, "scaleC.png");<br />
    }</p>
<p>    public static void scaleA(BufferedImage src, String dstFile) throws IOException {<br />
        BufferedImage dst = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);<br />
        Graphics2D g2d = dst.createGraphics();<br />
        g2d.setComposite(AlphaComposite.Src);<br />
        g2d.drawImage(src, 0, 0, width, height, null);<br />
        g2d.dispose();<br />
        ImageIO.write(dst, "png", new File(dstFile));<br />
        int rgb = dst.getRGB(0, 0);<br />
        System.out.println("RGB :" + ((rgb >> 16) & 0xff) + "," + ((rgb >> 8) & 0xff) + "," + (rgb & 0xff));<br />
    }</p>
<p>    public static void scaleB(BufferedImage src, String dstFile) throws IOException {<br />
        IndexColorModel icm = (IndexColorModel) src.getColorModel();<br />
        WritableRaster wr = icm.createCompatibleWritableRaster(width, height);<br />
        BufferedImage dst = new BufferedImage(icm, wr, false, null);</p>
<p>        Graphics2D g2d = dst.createGraphics();<br />
        g2d.setComposite(AlphaComposite.Src);<br />
        g2d.drawImage(src, 0, 0, width, height, null);<br />
        g2d.dispose();<br />
        ImageIO.write(dst, "png", new File(dstFile));<br />
        int rgb = dst.getRGB(0, 0);<br />
        System.out.println("RGB :" + ((rgb >> 16) & 0xff) + "," + ((rgb >> 8) & 0xff) + "," + (rgb & 0xff));<br />
    }</p>
<p>    public static void scaleC(BufferedImage src, String dstFile) throws IOException {<br />
        IndexColorModel icm = (IndexColorModel) src.getColorModel();<br />
        WritableRaster wr = icm.createCompatibleWritableRaster(width, height);<br />
        BufferedImage dst = new BufferedImage(icm, wr, false, null);</p>
<p>        Graphics2D g2d = dst.createGraphics();<br />
        g2d.setComposite(AlphaComposite.Src);<br />
        //same effect with RenderingHints.KEY_INTERPOLATION<br />
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);<br />
        g2d.drawImage(src, 0, 0, width, height, null);<br />
        g2d.dispose();<br />
        ImageIO.write(dst, "png", new File(dstFile));<br />
        int rgb = dst.getRGB(0, 0);<br />
        System.out.println("RGB :" + ((rgb >> 16) & 0xff) + "," + ((rgb >> 8) & 0xff) + "," + (rgb & 0xff));<br />
    }</p>
<p>}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
java2x
Offline
Joined: 2006-12-07
Points: 0

souce code and image

java2x
Offline
Joined: 2006-12-07
Points: 0

Sorry for the duplicated post
My network si tooooo slow, I clicked the "Post Message" twice.