Transparent BG for loaded Image

S

Steffen Krause

Hi all,

I'm pretty new to imaging with java so please excuse if I'm asking a dump
question. My problem is:

I load an image from a BMP file using JAI 1.1.2. So far so good, now I have
it as BufferedImage. Next I want to draw it on top of another image but
with it's background beeing transparent. Just to let you know exactly what
I mean, the background image is a photo and the other pic is a blue filled
circle with a magenta background. Now the blue circle should be drawn over
the photo but of course without the magenta showing. Can anybody help me??

Thanks,
Steffen.
 
T

Tom N

Steffen Krause said:
I'm pretty new to imaging with java so please excuse if I'm asking a dump
question. My problem is:

I load an image from a BMP file using JAI 1.1.2. So far so good, now I have
it as BufferedImage. Next I want to draw it on top of another image but
with it's background beeing transparent. Just to let you know exactly what
I mean, the background image is a photo and the other pic is a blue filled
circle with a magenta background. Now the blue circle should be drawn over
the photo but of course without the magenta showing. Can anybody help me??

The obvious answer is to convert the BMP to a GIF with a transparent
background (GIF supports transparency). Then it all just works, without any
difficulty. GIF is of course not Windows specific so that's another
advantage :)

Does it have to be a BMP file?
 
S

Steffen Krause

Does it have to be a BMP file?

Unfortunately yes if I wanna stay compatible to some other programms. But
does the image stay a BMP even when I loaded it as BufferedImage? I mean,
isn't there a way when I draw the BMP on top of the photo to filter out
the background? Somewhere I read that I have to use one of the
BufferedImageOp to do a transformation.
 
A

Adam

Steffen Krause said:
Unfortunately yes if I wanna stay compatible to some other programms. But
does the image stay a BMP even when I loaded it as BufferedImage? I mean,
isn't there a way when I draw the BMP on top of the photo to filter out
the background? Somewhere I read that I have to use one of the
BufferedImageOp to do a transformation

You need to know the background color of your bitmap
that you want to be transparent.
Then loop through all pixels of your BufferedImage
and replace all pixels of background color
with transparent (new Color(0,0,0,0)).

Adam
 
T

Thomas Weidenfeller

Steffen said:
I load an image from a BMP file using JAI 1.1.2. So far so good, now I
have it as BufferedImage. Next I want to draw it on top of another image
but with it's background beeing transparent.

Then that part of the image which should be transparent needs to have
its alpha component set to transparent (0.0). Or (an old trick), you
reverse the upper and lower image: You set all of the underlying image
to transparent, and actually paint the underlying image over the top image.
Just to let you know
exactly what I mean, the background image is a photo and the other pic
is a blue filled circle with a magenta background.

Magenta? Well, what company could that be? :))
Now the blue circle
should be drawn over the photo but of course without the magenta
showing. Can anybody help me??

Once you have the alpha of the background set correctly, create an
AlphaComposite, e.g. with SRC_OVER or DST_OVER (depends which image you
paint over which). Get the Graphics2D of one of the BufferedImages, and
set the composite on it. Then paint the other image over the first one,
using the Graphics2D object.

/Thomas
 
A

ak

I load an image from a BMP file using JAI 1.1.2. So far so good, now I
have
it as BufferedImage. Next I want to draw it on top of another image but
with it's background beeing transparent. Just to let you know exactly what
I mean, the background image is a photo and the other pic is a blue filled
circle with a magenta background. Now the blue circle should be drawn over
the photo but of course without the magenta showing. Can anybody help me??

hmm, if it is just blue circle - you could just draw blue circle to Graphics
of your BufferedImage.

____________

http://reader.imagero.com the best java image reader.
 
M

Marco Schmidt

Tom N:
The obvious answer is to convert the BMP to a GIF with a transparent
background (GIF supports transparency).
Then it all just works, without any difficulty.

No, that may or may not work. GIF can store only images with up to 256
colors, so a truecolor BMP file may lose quality when converted.

PNG may be the format of choice - supported since Java 1.3, can
include different types of transparency information.

BMP does not support transparency, as far as I know.

Regards,
Marco
 
S

Steffen Krause

You need to know the background color of your bitmap
that you want to be transparent.
Then loop through all pixels of your BufferedImage
and replace all pixels of background color
with transparent (new Color(0,0,0,0)).

Thanks Adam, I'll try that. I can do that with that ColorConvertOp, right?
 
A

Adam

Steffen Krause said:
Thanks Adam, I'll try that. I can do that with that ColorConvertOp,
right?

I do not know, I have never used ColorConvertOp.
My first guess was to use BufferedImage.getRGB/setRGB,
that will work correctly for sure, but I'm not sure
about performance though.

Adam
 
S

Steffen Krause

I do not know, I have never used ColorConvertOp.
My first guess was to use BufferedImage.getRGB/setRGB,
that will work correctly for sure, but I'm not sure
about performance though.

Adam

From what I understand exactly what you suggest is done by that
ColorConvertOp. Those Ops are actually pretty cool, you put a BufferedImage
in, tell the Op what to do and a new BufferedImage comes out. I just
havened figured out, how to initialize them right. But I'm sure I'll find
it out.
 
M

Marco Schmidt

Adam:
I do not know, I have never used ColorConvertOp.
My first guess was to use BufferedImage.getRGB/setRGB,
that will work correctly for sure, but I'm not sure
about performance though.

Performance is not so good, but it can be increased using the versions
of the methods that work on int arrays instead of single int values.

Regards,
Marco
 
N

nos

Thomas Weidenfeller said:
Then that part of the image which should be transparent needs to have
its alpha component set to transparent (0.0). Or (an old trick), you
reverse the upper and lower image: You set all of the underlying image
to transparent, and actually paint the underlying image over the top image.

Magenta? Well, what company could that be? :))


Once you have the alpha of the background set correctly, create an
AlphaComposite, e.g. with SRC_OVER or DST_OVER (depends which image you
paint over which). Get the Graphics2D of one of the BufferedImages, and
set the composite on it. Then paint the other image over the first one,
using the Graphics2D object.

does this work if the two images are different sizes? can i
specify the origin for the upper left corner of the smaller one?
 
T

Tom N

Tom N:


No, that may or may not work. GIF can store only images with up to 256
colors, so a truecolor BMP file may lose quality when converted.

In the described problem, there are only two colours in the image that
requires a transparent background. Hence GIF would work in the described
problem.
 
S

Steffen Krause

In the described problem, there are only two colours in the image that
requires a transparent background. Hence GIF would work in the described
problem.

That's true but due to the specification both images are BMP-Files and I
can not change anything about it!
 
T

Thomas Weidenfeller

nos said:
does this work if the two images are different sizes? can i
specify the origin for the upper left corner of the smaller one?

Yes, see the api documentation of Graphics[2D].drawImage().

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top