Skip to main content

ImageIO and transparent gif

3 replies [Last post]
besherman
Offline
Joined: 2007-01-11
Points: 0

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?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Alan P. Sexton

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.Sexton@cs.bham.ac.uk

On 6/23/07, jai-imageio@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
>
>
[att1.html]

Rosati Claudio

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:
claudio.rosati@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.Sexton@cs.bham.ac.uk
>
>
> On 6/23/07, jai-imageio@javadesktop.org > imageio@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.

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.
----------------------------------------------------------------------
[att1.html]

Brian Burkhalter

Are you using JAI Image I/O Tools or the Java SE GIF writer (available in Java
6)?

On Sat, 23 Jun 2007, jai-imageio@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
>
>

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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