Question on adding transparency with JAI

F

forelight

Hello,

This is probably an offputtingly trivial question to someone who isn't
as new to JAI as I am. I have a servlet that, depending on some
condition,
will send to the browser either an unaltered image as it exists on disk
in the form of a JPEG file, or the same imaged dimmed. The "dimming"
consists of little more than adding an alpha channel to the jpeg image
and
setting it to some value in the range between 0 and 255 for each pixel.
It is
performed on the fly using a Graphics2D manipulation and is sent to the
response output stream directly from memory.

The problem with this is that it doesn't work on Unix, because
Graphics2D
depends on a running X server, which I don't want to intoduce into the
picture. I understand that JAI doesn't have this dependency on X and
would like to change my program to use JAI.

Would anyone be so kind as to give me some pointers where I could
find a recyclable snipped of code or some relevant discussion? I've
googled around for this, but haven't found anything direcly usable.
I've also read most of the JAI user guide and it seems a bit too
general
and vague to me, admittedly not a graphics prefessional.

Thank you very much in advance,

-Igor.
 
A

Andrey Kuznetsov

This is probably an offputtingly trivial question to someone who isn't
as new to JAI as I am. I have a servlet that, depending on some
condition,
will send to the browser either an unaltered image as it exists on disk
in the form of a JPEG file, or the same imaged dimmed. The "dimming"
consists of little more than adding an alpha channel to the jpeg image
and
setting it to some value in the range between 0 and 255 for each pixel.
It is
performed on the fly using a Graphics2D manipulation and is sent to the
response output stream directly from memory.

The problem with this is that it doesn't work on Unix, because
Graphics2D
depends on a running X server, which I don't want to intoduce into the
picture. I understand that JAI doesn't have this dependency on X and
would like to change my program to use JAI.

you don't need JAI for this.

There are a few ways to manipulate alpha.

With Java 1.x you can use RGBImageFilter.

With Java 2 and BufferedImage you can use getRGB() to get pixel values,
then change alpha and write it back with setRGB().
Note that BufferedImage must support alpha (TYPE_INT_ARGB for example)

something like

//alpha from 0 to 255
void setAlpha(BufferedImage bi, int alpha) {
int w = bi.getWidth();
int h = bi.getHeight();
int [] rgb = new int[w * h];
bi.getRGB(0, 0, w, h, rgb, 0, w);

int _alpha = (alpha & 0xFF) << 24;
for(int i = 0; i < rgb.length; i++) {
rgb = (rgb & 0xFFFFFF) | _alpha;
}
bi.setRGB(0,0,w, h, rgb, 0, w);
}

Andrey
 
O

Oliver Wong

Andrey Kuznetsov said:
This is probably an offputtingly trivial question to someone who isn't
as new to JAI as I am. I have a servlet that, depending on some
condition,
will send to the browser either an unaltered image as it exists on disk
in the form of a JPEG file, or the same imaged dimmed. The "dimming"
consists of little more than adding an alpha channel to the jpeg image
and
setting it to some value in the range between 0 and 255 for each pixel.
It is
performed on the fly using a Graphics2D manipulation and is sent to the
response output stream directly from memory.

The problem with this is that it doesn't work on Unix, because
Graphics2D
depends on a running X server, which I don't want to intoduce into the
picture. I understand that JAI doesn't have this dependency on X and
would like to change my program to use JAI.

you don't need JAI for this.

There are a few ways to manipulate alpha.

With Java 1.x you can use RGBImageFilter.

With Java 2 and BufferedImage you can use getRGB() to get pixel values,
then change alpha and write it back with setRGB().
Note that BufferedImage must support alpha (TYPE_INT_ARGB for example)

something like

//alpha from 0 to 255
void setAlpha(BufferedImage bi, int alpha) {
int w = bi.getWidth();
int h = bi.getHeight();
int [] rgb = new int[w * h];
bi.getRGB(0, 0, w, h, rgb, 0, w);

int _alpha = (alpha & 0xFF) << 24;
for(int i = 0; i < rgb.length; i++) {
rgb = (rgb & 0xFFFFFF) | _alpha;
}
bi.setRGB(0,0,w, h, rgb, 0, w);
}


The JPG file format doesn't support an alpha channel. If the intent is
just to dim the picture, I'd get the RGB values, and then multiply them by a
constant (less than 1) and write them back. For example, divide every value
in half.

- Oliver
 
A

Andrey Kuznetsov

The JPG file format doesn't support an alpha channel. If the intent is
just to dim the picture, I'd get the RGB values, and then multiply them by
a constant (less than 1) and write them back. For example, divide every
value in half.

I just told how to change alpha.
That jpeg does not support alpha channes is clear.
I should read things with more attention ;-)

Andrey
 

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

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top