Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Fading images with java.awt.image.RGBImageFilter

13 views
Skip to first unread message

Leon Brocard

unread,
Jul 2, 1996, 3:00:00 AM7/2/96
to

Hey everyone,

What I want to do is fade an image to white. However, I am having
very little luck fading an image at all. I realize I should be using
an RGBImageFilter, possibly with canFilterIndexColorModel enabled.

Can somebody point me in the right direction?

Many thanks, Leon.

--
Leon Brocard.....................................Perl/Java programmer
le...@intelifest.co.uk..................................for Intelifest

John Zukowski

unread,
Jul 10, 1996, 3:00:00 AM7/10/96
to Leon Brocard

Leon Brocard wrote:
>
> Hey everyone,
>
> What I want to do is fade an image to white. However, I am having
> very little luck fading an image at all. I realize I should be using
> an RGBImageFilter, possibly with canFilterIndexColorModel enabled.
>
> Can somebody point me in the right direction?
>

Subclass RGBImageFilter. In constructor, set canFilterIndexColorModel to
true since filter is not dependant on x and y positions.
Create your own filterRGB that generates multiple frames by converging
each color to 255. Instead of fading to white, you might wish to play
with the transparency. This allows whatever is behind the image to
appear through.

If you are not comfortable with the multiple frames part, you would just
have to call the filter multiple times... with the appropriate delay
between calls.

--
John Zukowski
j...@ultranet.com / j.zuk...@ieee.org
Java Consultant / Author / Speaker

Mark Wutka

unread,
Jul 10, 1996, 3:00:00 AM7/10/96
to

Leon Brocard (le...@intelifest.co.uk) wrote:
: Hey everyone,

: What I want to do is fade an image to white. However, I am having
: very little luck fading an image at all. I realize I should be using
: an RGBImageFilter, possibly with canFilterIndexColorModel enabled.

I have tried fading two different ways. One is by modifying the transparency
of each color. That works okay, but it is a lot smoother to fade to a
pre-set background color. Obviously that won't work if you want to see what's
below.

Here is the filter that modifies the alpha component. Originally, this
filter did not have the filterIndexColorModel method you see here. I
put it in so it would work with transparent GIFs. If there is a transparent
pixel, it automatically sets its alpha value to 0. You control the amount
of fade by calling setTransValue with a number between 0 and 255, 255 is
completely visible, 0 is completely transparent.

import java.awt.image.*;

public class TransFilter extends RGBImageFilter {
private int transValue;

public TransFilter(int tV)
{
canFilterIndexColorModel = true;
transValue = tV;
}

public void setTransValue(int newValue)
{
transValue = newValue;
}

public int getTransValue()
{
return transValue;
}

public int filterRGB(int x, int y, int rgb)
{
return ((rgb & 0x00ffffff) | (transValue << 24));
}

public IndexColorModel filterIndexColorModel(IndexColorModel icm)
{
int mapsize = icm.getMapSize();
byte r[] = new byte[mapsize];
byte g[] = new byte[mapsize];
byte b[] = new byte[mapsize];
byte a[] = new byte[mapsize];
icm.getReds(r);
icm.getGreens(g);
icm.getBlues(b);
icm.getAlphas(a);
int trans = icm.getTransparentPixel();
boolean needalpha = false;
for (int i = 0; i < mapsize; i++) {
int rgb = filterRGB(-1, -1, icm.getRGB(i));
a[i] = (byte) (rgb >> 24);
if (a[i] != ((byte)0xff) && i != trans) {
needalpha = true;
}
r[i] = (byte) (rgb >> 16);
g[i] = (byte) (rgb >> 8);
b[i] = (byte) (rgb >> 0);
}
if (needalpha) {
if (trans >= 0) {
a[trans] = 0;
}
return new IndexColorModel(icm.getPixelSize(), mapsize,
r, g, b, a);
} else {
return new IndexColorModel(icm.getPixelSize(), mapsize,
r, g, b, trans);
}
}
}

Here is the fader that fades an image to a background color. Again,
setTransValue changes the amount of transparency:

import java.awt.*;
import java.awt.image.*;

public class TransFilter extends RGBImageFilter {
private int transValue;
private Color backgroundRGB;

public TransFilter(int tV, Color backgroundRGB)
{
canFilterIndexColorModel = true;
transValue = tV;
this.backgroundRGB = backgroundRGB;
}

public void setTransValue(int newValue)
{
transValue = newValue;
}

public int getTransValue()
{
return transValue;
}

public int transComponent(int bg, int fg)
{
return bg + (fg - bg) * transValue / 255;
}

public int filterRGB(int x, int y, int rgb)
{
Color c = new Color(rgb);

Color newColor = new Color(
transComponent(backgroundRGB.getRed(), c.getRed()),
transComponent(backgroundRGB.getGreen(), c.getGreen()),
transComponent(backgroundRGB.getBlue(), c.getBlue()));

return newColor.getRGB();
}
}
--
_________________________________________________________
Mark Wutka wu...@netcom.com
*** No animals were harmed during the production of this .signature ***

Hiang-Swee Chiang

unread,
Jul 11, 1996, 3:00:00 AM7/11/96
to

Hello everyone,

I am trying to display video with Java (I don't mind if it cannot do 25 frames
per second, 1 frames per second is so good for me, in other words, I just want
to push it to any limit that Java will go).

I wrote the following test code:

public void run() {
int width = 88;
int height = 64;
int foreColor = (255<<24)|(255<<16)|(255<<8)|0;
int backColor = (255<<24)|(0<<16)|(0<<8)|255;
// alpha, red, green, blue
int pixels[] = new int[width * height];
int linepos = 0;
while (!mStopThread) {
int index = 0;
int color = backColor;
for (int y = 0; y < height; y++) {
if (y == linepos)
color = foreColor;
else if (y == linepos + 4)
color = backColor;
// else do nothing
for (int x = 0; x < width; x++)
pixels[index++] = color;
}
System.gc();
mImage = createImage(new
MemoryImageSource(width,height,pixels,0,width));
mCanvas.getGraphics().drawImage(mImage,0,0,this);
if (++linepos > height - 4 - 1) linepos = 0;
}
mThread = null;
} // run

The code runs really slow because of two things:
1) Without System.gc(), it run a lot faster but my computer very soon run out
of memory because all the cpu time is used up to display the images, and all
the MemoryImageSource that is "new" are not garbage collected.
2) drawImage is slow anyway.

However, I am wondering if I have used this combination of createImage(),
MemoryImageSource() wrongly? Is there a better way of doing it so that I
don't have to "new" an object for every frame and hence would not need garbage
collection which slow things down and would not run out of memory resources?
Is there a way to "recycle" so that MemoryImageSource return the previous
instance instead of a new one and createImage reuse the previous instance
also?

Any alternative way of doing this sort of high speed display is very much
appreciated.

Regards,
Swee


Jay Cox

unread,
Jul 12, 1996, 3:00:00 AM7/12/96
to

Hiang-Swee Chiang wrote:
>
> Hello everyone,
>
> I am trying to display video with Java (I don't mind if it cannot do 25 frames
> per second, 1 frames per second is so good for me, in other words, I just want
> to push it to any limit that Java will go).
>

Create your image once, then call the Image.flush() method every time
you have finish a new frame.
The code would look something like this:

...
mImage = createImage(new
MemoryImageSource(width,height,pixels,0,width));
while (!done)
{
//compute next frame;
mImage.flush();
mCanvas.getGraphics().drawImage(mImage,0,0,this);
}

Hope this helps.

Jay Cox

Wayne Rasband

unread,
Jul 12, 1996, 3:00:00 AM7/12/96
to

In article <4s34rn$b...@lyra.csx.cam.ac.uk>, hs...@cus.cam.ac.uk (Hiang-Swee
Chiang) wrote:

> Hello everyone,
>
> I am trying to display video with Java (I don't mind if it cannot do 25 frames
> per second, 1 frames per second is so good for me, in other words, I just want
> to push it to any limit that Java will go).

Create an ImageProducer class that continuously feeds frames to an image. I
have an applet at:

http://rsb.info.nih.gov/nih-image/java/benchmarks/plasma.html

that uses this technique. It can generate and display 16 192x192 frames per
second on a PowerMac 7500/100.

-wayne

Jim Graham

unread,
Jul 14, 1996, 3:00:00 AM7/14/96
to

wu...@netcom.com (Mark Wutka) writes:
> Here is the filter that modifies the alpha component. Originally, this
> filter did not have the filterIndexColorModel method you see here. I
> put it in so it would work with transparent GIFs. If there is a transparent
> pixel, it automatically sets its alpha value to 0. You control the amount
> of fade by calling setTransValue with a number between 0 and 255, 255 is
> completely visible, 0 is completely transparent.

You shouldn't have to do that if you rewrite your filterRGB method:

> public int filterRGB(int x, int y, int rgb)
> {
> return ((rgb & 0x00ffffff) | (transValue << 24));
> }

Try this instead:

public int filterRGB(int x, int y, int rgb) {
return ((rgb & 0x00ffffff) |

((rgb >>> 24) * transvalue / 255));
}

In other words, rather than "setting" the alpha value to the desired
transparency level, multiply the two (wrt the 0 - 255 scale) so that
the effects of the original transparency and your new relative transparency
are cumulative.

If you write things that way then the standard "filterIndexColorModel"
method should do the right thing.

...jim

Jim Graham

unread,
Jul 14, 1996, 3:00:00 AM7/14/96
to

fl...@bendenweyr.eng.sun.com (Jim Graham) writes:
> > public int filterRGB(int x, int y, int rgb)
> > {
> > return ((rgb & 0x00ffffff) | (transValue << 24));
> > }
>
> Try this instead:
>
> public int filterRGB(int x, int y, int rgb) {
> return ((rgb & 0x00ffffff) |
> ((rgb >>> 24) * transvalue / 255));
> }

Eeek. That should be:

public int filterRGB(int x, int y, int rgb) {
return ((rgb & 0x00ffffff) |

(((rgb >>> 24) * transValue / 255) << 24));
}

...jim

0 new messages