Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Question on adding transparency with JAI

Thread view: 
forelight - 28 Jun 2006 05:08 GMT
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.
Andrey Kuznetsov - 28 Jun 2006 08:04 GMT
> 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
[quoted text clipped - 13 lines]
> 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[i] = (rgb[i] & 0xFFFFFF) | _alpha;
   }
   bi.setRGB(0,0,w, h, rgb, 0, w);
}

Andrey

Signature

http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

Oliver Wong - 29 Jun 2006 22:22 GMT
>> 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
[quoted text clipped - 39 lines]
>    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
Andrey Kuznetsov - 30 Jun 2006 14:19 GMT
>    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

Signature

http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.