[java] alpha blending - a solution
Hi everyone,
I posted some source a while back to do 50% blending. It was actually designed to be used in c++ and be fast.
I have noticed that the question of how to do alpha blending in java has surfaced a few times.
If you have any questions, either email me (tali @ nms.com.au)
or icq on 57237889.
set the IPERCENT and WPERCENT values.
here is a working algorithm:
[source]
// alpha blending example
// copyright (c) 2000 T.Streit, all rights reserved
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
public class alphademo extends Applet
{
Image Source1;
Image Source2;
Image Result;
// Initialize the applet
public void init()
{
Source1 = getImage(getCodeBase(), "first_image.gif");
Source2 = getImage(getCodeBase(), "second_image.gif");
// hard coded for simplicity. can use Image.getWidth() etc...
int alphaWidth = 50;
int alphaHeight = 50;
int[] imgBuf1 = new int[alphaWidth * alphaHeight];
int[] imgBuf2 = new int[alphaWidth * alphaHeight];
int[] imgResult = new int[alphaWidth * alphaHeight];
// Grab the pixels so we can manipulate them
PixelGrabber imgGrabber = new PixelGrabber(image.getSource(), 0, 0, alphaWidth, alphaHeight, imgBuf1, 0, alphaWidth);
PixelGrabber imgWGrabber = new PixelGrabber(time.getSource(), 0, 0, alphaWidth, alphaHeight, imgBuf1, 0, alphaWidth);
try
{
imgGrabber.grabPixels();
imgWGrabber.grabPixels();
}
catch(InterruptedException e)
{}
// - START ALPHA BLENDING -
int img1Pixel;
int img2Pixel;
// hard coded
int rmask = 0x00ff0000;
int gmask = 0x0000ff00;
int bmask = 0x000000ff;
int rpix, gpix, bpix;
int WPERCENT;
int IPERCENT;
// set these values to control the level of alpha blending
WPERCENT = 72;
// IPERCENT should be 100-WPERCENT...
IPERCENT = 100-WPERCENT;
for(int y = 0; y < alphaHeight; y++)
for(int x = 0; x < alphaWidth; x++)
{
img1Pixel = imgBuf1[y*alphaWidth+x];
img2Pixel = imgBuf2[y*alphaHeight+x];
rpix = (imgWPixel&rmask) * WPERCENT / 200; // water color %
rpix += (imgPixel&rmask) * IPERCENT / 200; // image %
gpix = (imgWPixel&gmask) * WPERCENT / 200; // water color %
gpix += (imgPixel&gmask) * IPERCENT / 200; // image %
bpix = (imgWPixel&bmask) * WPERCENT / 200; // water color %
bpix += (imgPixel&bmask) * IPERCENT / 200; // image %
imgFinal[y*128+x] = 0xff000000 | (rpix&rmask) | (gpix&gmask) | (bpix&bmask);
}
Result = createImage(new MemoryImageSource(alphaWidth, alphaHeight, imgFinal, 0, alphaWidth));
}
public void paint(Graphics g)
{
g.drawImage(Result, 0, 0, this);
}
}
Hey Thanks for the alpha blending source. I think I know just where I can put this to good use. I''ve had on old robotech/macross game that I''d put on ice, until I could get some working code. I think this will revive it for good.
War doesn't determine who is right, war determines who is left.
War doesn't determine who is right, war determines who is left.
I don''t mind if you post the code in the FAQ, but!!!
* I realized I posted a slightly older version of the code, some of the variable names are wrong *
change:
imgWPixel to img1Pixel
imgPixel to img2Pixel
imgFinal to imgResult
(sorry about that)
* I realized I posted a slightly older version of the code, some of the variable names are wrong *
change:
imgWPixel to img1Pixel
imgPixel to img2Pixel
imgFinal to imgResult
(sorry about that)
Hey zeneic, do you have additive blending code?
Hardcore Until The End.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement