Re: Making color image with transparency into black & white w/transparency



gswarthout@xxxxxxxxx wrote:
I need to turn a series of icon images into black & white equivalents
programattically in a paint method.

I'm starting with roughly circular images which are colored within and
everything outside of the circle is transparent. The transparency
displays properly when the images are used in their normal form.

On the fly, however, I want to recolor select images into gray-scale
reflections of their original form, which I do with the following
code:

<Code>
BufferedImage grayImage = new BufferedImage(usableWidth, usableHeight,
BufferedImage.TYPE_BYTE_GRAY);
Graphics2D grayGraphics = grayImage.createGraphics();
grayGraphics.drawImage(originalImage, 0, 0, usableWidth, usableHeight,
this);
</Code>

This produces the effect I want with one problem, the previously
transparent corners of the image are now black. I then draw this gray-
scale image into a new image which allows for transparency thusly:

<Code>
BufferedImage bufferedImage = new BufferedImage(usableWidth,
usableHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bufferedImage.createGraphics();
g2d.drawImage(grayImage, 0, 0, usableWidth, usableHeight, this);
</Code>

From here I have been unable to find a way to change the corners back
to being transparent (ignoring that it would turn all other instances
of black transparent as well). I've tried XORing black with white
and, sure enough, this turns black into white and vice versa, but not
transparent. I've tried it with new (Color 255, 255, 255, 0) which
would be a fully transparent white and it still doesn't work.

How can I accomplish this feat with either A) the way I started or, B)
a better way that I haven't explored?

Any help would be appreciated.

Greg


Greg:

Try this and see if it works for you. You might have to play with the transparency. If you want send me one of your images with the transparent pixels and I'll try it.

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

public class test {
public static void main (String[] args) throws Exception {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();

BufferedImage src = ImageIO.read(new File("saturn.jpg"));
BufferedImage dest =
new BufferedImage(src.getWidth(),src.getHeight(),
BufferedImage.TYPE_BYTE_GRAY);

ColorConvertOp op = new ColorConvertOp(
src.getColorModel().getColorSpace(),
dest.getColorModel().getColorSpace(),
null);

op.filter(src,dest);

ImageIO.write(dest,"JPG",new File("bw.jpg"));
}
}

--

Knute Johnson
email s/nospam/knute/
.



Relevant Pages

  • Re: Transparent bitmaps
    ... because code to draw images is there already (e.g. ... the .NET Compact Framework itself. ... > color based on index, if any, is to be transparent in the the image when ...
    (microsoft.public.dotnet.framework.compactframework)
  • Making color image with transparency into black & white w/transparency
    ... I need to turn a series of icon images into black & white equivalents ... everything outside of the circle is transparent. ... BufferedImage grayImage = new BufferedImage(usableWidth, usableHeight, ... BufferedImage bufferedImage = new BufferedImage(usableWidth, ...
    (comp.lang.java.gui)
  • Re: Transparent GIF/PNG link not passing clicks to element behind it
    ... The glass analogy is interesting, ... "Transparency for PNG images is respected during text selection. ... through the transparent image. ... Elementen op een pagina mogen geen enkel gedeelte van de advertentie ...
    (comp.infosystems.www.authoring.html)
  • Re: Making color image with transparency into black & white w/transparency
    ... everything outside of the circle is transparent. ... displays properly when the images are used in their normal form. ... BufferedImage grayImage = new BufferedImage(usableWidth, usableHeight, ... Graphics2D grayGraphics = grayImage.createGraphics; ...
    (comp.lang.java.gui)
  • Re: PowerPoint Playback Problems
    ... I play the presentation, some look like EPS images without a clipping ... I think you'd have better luck using .gif files with a transparent ... Using the smaller .gif files should make the presentation much quicker, ...
    (microsoft.public.mac.office.powerpoint)