atom feed4 messages in net.java.dev.jai-imageio.interestRe: [JAI-IMAGEIO] ImageIO and transpa...
FromSent OnAttachments
jai-...@javadesktop.orgJun 23, 2007 4:56 am 
Brian BurkhalterJun 23, 2007 9:21 am 
Alan P. SextonJun 25, 2007 9:30 am 
Rosati ClaudioJun 25, 2007 11:11 pm 
Subject:Re: [JAI-IMAGEIO] ImageIO and transparent gif
From:Rosati Claudio (clau@acsys.it)
Date:Jun 25, 2007 11:11:35 pm
List:net.java.dev.jai-imageio.interest

HI,

my preferred way is the following:

BufferedImage bi = new BufferedImage(width, height, bufferedImage.TYPE_INT_ARGB); Graphics2D g2d = bi.createGraphics();

...

g2d.setComposite(AlphaComposite.Clear); g2d.fillRect(0, 0, width, height); g2d.setComposite(AlphaComposite.SrcOver);

// Now draw over the fully transparent image

...

Claudio

+----------------------------------------------------------------------- -+ | |\ | _/_/_/_/_/ _/_/_/_/ Claudio Rosati | \ | _/ _/ _/ Advanced ComputerSystems ACS S.p.A. | \ | _/ _/ _/ via Della Bufalotta 378 +---+ | _/ _/_/_/_/ I-00139 Roma (RM) | | _/ _/ _/ Italy | | _/ _/ _/ Phone: +39 06 8709 0516 | | _/_/_/_/_/ _/ _/ E-mail: clau@acsys.it | | | +----------------------------------------------------------------------- -----+

ATTENZIONE: le informazioni contenute in questo messaggio sono da considerarsi confidenziali ed il loro utilizzo e' riservato unicamente al destinatario sopra indicato. Chi dovesse ricevere questo messaggio per errore e' tenuto ad informare il mittente ed a rimuoverlo definitivamente da ogni supporto elettronico o cartaceo.

WARNING: This message contains confidential and/or proprietary information which may be subject to privilege or immunity and which is intended for use of its addressee only. Should you receive this message in error, you are kindly requested to inform the sender and to definitively remove it from any paper or electronic format.

On Jun 25, 2007, at 6:31 PM, Alan P. Sexton wrote:

The code you provide assumes that a new BufferedImage is, by default, transparent. I don't see that specified in the API.

Aside from that, some time ago I was trying to do something similar, but using fillRect to fill the image with transparent white first to set the alpha and then drawing over it in opaque black. It only partially worked: the transparency worked when I drew the BufferedImage to the screen but when I saved the image to a tiff file (using the Java IO tools) and opened it in a number of different image viewer programs, the whole image displayed as black. I never got to the bottom of why exactly it was going wrong. However, changing the fillRect to a nested loop to explicitly set the pixel values seemed to fix it. The working code, with the broken code commented out, is shown below:

BufferedImage bi = new BufferedImage(width, height, bufferedImage.TYPE_INT_ARGB); Graphics2D g = bi.createGraphics(); Color transparentWhite = new Color( Color.WHITE.getRed(), Color.WHITE.getGreen(), Color.WHITE.getBlue(), 0); int tw = transparentWhite.getRGB() ; // Following code does not work correctly: I suspect fillRect does not set the RGB value if the // transparency is 0: while this might seem like a reasonable optimisation, it means that // it is not doing what it is supposed to do and one is left with incorrect color values // in the image so cannot do things like stripping the alpha channel to get the correct // non-alpha image. // g.setColor(transparentWhite); // g.fillRect(0, 0, maxX - minX, maxY - minY); for (int x = 0 ; x < bi.getWidth() ; x++) for (int y = 0 ; y < bi.getHeight() ; y++) bi.setRGB(x, y, tw) ; g.setColor(Color.BLACK); // draw in opaque black g.dispose();

-- Alan P. Sexton Email: A.P.@cs.bham.ac.uk

On 6/23/07, jai-@javadesktop.org <jai- imag@javadesktop.org> wrote:

I am having a bit of trouble with creating transparent gifs. This gives me a transparent png with a black rectangle in the corner, but the gif goes all black:

BufferedImage img = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = img.createGraphics(); g2.setColor(Color.BLACK); g2.fillRect(0, 0, 10, 10); g2.dispose(); ImageIO.write(img, "png", new File("c:/test.png")); ImageIO.write(img, "gif", new File("c:/test.gif"));

Could someone give me a friendly pointer as to what I've missed? [Message sent by forum member 'besherman' (besherman)]

http://forums.java.net/jive/thread.jspa?messageID=223645

---------------------------------------------------------------------

To unsubscribe, e-mail: interest-unsubscribe@jai- imageio.dev.java.net For additional commands, e-mail: interest-help@jai- imageio.dev.java.net

---------------------------------------------------------------------- ATTENZIONE: le informazioni contenute in questo messaggio sono da considerarsi
confidenziali ed il loro utilizzo e' riservato unicamente al destinatario sopra
indicato. Chi dovesse ricevere questo messaggio per errore e' tenuto ad
informare il mittente ed a rimuoverlo definitivamente da ogni supporto
elettronico o cartaceo.

----------------------------------------------------------------------