merging 2 images

I have a background image and a foreground image.

I want to overlay the foreground image on top of the background image so that I can see parts of the background below.  I have drawn the foreground image with black where I want it to be transparent.  I need some code to show me how to merge the 2 images.

Thanks,
Mike.
LVL 3
MDarlingAsked:
Who is Participating?
 
imladrisConnect With a Mentor Commented:
One solution would be to process the foreground image as it is loaded, turning all the black pixels to transparent ones. To do this write an imagefilter something like:

class TransFilter extends RGBImageFilter
{     TransFilter()
     {     canFilterIndexColorModel=true;
          return;
     }

        // if rgb is black (red, green and blue components
        // all off) return a transparent pixel
        // (alpha component (top byte) off)

     public int filterRGB(int x,int y,int rgb)
     {     int i;

                return((rgb&0xffffff)==0?0:rgb);
     }
}


You use an imagefilter with something like:

Image buf=getImage(name);  // get the image
ImageFilter flt=new TransFilter();
ImageProducer p=new FilteredImageSource(buf.getSource(),flt);
Image pi=window.createImage(p);


Out of curiosity, what software are you using to load png images? I have done png's in the past, but the libraries I found all turned out to have substantive problems, though that was some years ago.
0
 
s_lavieCommented:
Can you send the code of what you did, so we'll speak the same "language"?
0
 
MDarlingAuthor Commented:
I haven't got any code yet.  Just need to load an image, load another, then overlay one on top of the other with the top one being transparent wherever it is black.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
s_lavieCommented:
Can the background image be a transparent gif?
0
 
MDarlingAuthor Commented:
No the background image is totally opaque.
The foreground image is partially transparent.

All my images are png format, they are also in high colour format, which is beyound GIF capabilities.
0
 
MDarlingAuthor Commented:
Hi imladris

Im just using loadImage from the Java SDK.  Seems to work ok.  

I'll check out your code and get back to you.

Thanks,
Mike.
0
 
MDarlingAuthor Commented:
Thanks - just the job!

Regards,
Mike.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.