Transparent BG for loaded Image

Discussion in 'Java' started by Steffen Krause, Feb 27, 2004.

  1. 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.
     
    Steffen Krause, Feb 27, 2004
    #1
    1. Advertising

  2. Steffen Krause

    Tom N Guest

    "Steffen Krause" wrote:
    > 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?
     
    Tom N, Feb 27, 2004
    #2
    1. Advertising

  3. >
    > 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.


    --
    Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
     
    Steffen Krause, Feb 27, 2004
    #3
  4. Steffen Krause

    Adam Guest

    "Steffen Krause" <> wrote in message
    news:eek:pr30ui5cl7uaw89@53.47.1.4...
    > >
    > > 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


    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
     
    Adam, Feb 27, 2004
    #4
  5. Steffen Krause wrote:
    > 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
     
    Thomas Weidenfeller, Feb 27, 2004
    #5
  6. Steffen Krause

    ak Guest

    > 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.
     
    ak, Feb 27, 2004
    #6
  7. 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
    --
    Please reply in the newsgroup, not by email!
    Java programming tips: http://jiu.sourceforge.net/javatips.html
    Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
     
    Marco Schmidt, Feb 27, 2004
    #7

  8. > 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?
     
    Steffen Krause, Feb 27, 2004
    #8
  9. Steffen Krause

    Adam Guest

    "Steffen Krause" <> wrote in message
    news:eek:...
    >
    > > 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?

    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
     
    Adam, Feb 27, 2004
    #9

  10. > 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.
     
    Steffen Krause, Feb 27, 2004
    #10
  11. 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
    --
    Please reply in the newsgroup, not by email!
    Java programming tips: http://jiu.sourceforge.net/javatips.html
    Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
     
    Marco Schmidt, Feb 27, 2004
    #11
  12. Steffen Krause

    nos Guest

    "Thomas Weidenfeller" <> wrote in message
    news:c1n5ap$5mk$...
    > Steffen Krause wrote:
    > > 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.


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

    >
    > /Thomas
    >
     
    nos, Feb 27, 2004
    #12
  13. Steffen Krause

    Tom N Guest

    "Marco Schmidt" wrote
    > 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.


    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.

    > 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.
     
    Tom N, Feb 29, 2004
    #13
  14. > 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!
     
    Steffen Krause, Mar 1, 2004
    #14
  15. nos wrote:
    > 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
     
    Thomas Weidenfeller, Mar 1, 2004
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marcelo Birnbach [MS]
    Replies:
    0
    Views:
    3,364
    Marcelo Birnbach [MS]
    Jun 28, 2003
  2. =?Utf-8?B?Um9iZXJ0?=
    Replies:
    0
    Views:
    470
    =?Utf-8?B?Um9iZXJ0?=
    Apr 6, 2004
  3. =?Utf-8?B?TGluYQ==?=

    Place transparent image in crystal report

    =?Utf-8?B?TGluYQ==?=, Sep 22, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    4,981
    =?Utf-8?B?TGluYQ==?=
    Sep 22, 2004
  4. Oleg Ogurok
    Replies:
    0
    Views:
    792
    Oleg Ogurok
    Jan 27, 2005
  5. Udo Corban
    Replies:
    0
    Views:
    3,933
    Udo Corban
    Jan 23, 2004
Loading...

Share This Page