Solved

ICO files & transparency with PNG images

Posted on 2004-10-10
22
477 Views
Last Modified: 2008-02-01
This is a problem I have been having for a long time:

I have a beautiful round logo for my application ;)
Because it is round, I have to use images that support transparency such as GIF or PNG.
GIF is not an option because it doesn't have an alpha channel, therefore forcing me to create a "hard border" logo or blend it to some color.
I've created an ICO file, that supports various formats, including 32bit with alpha channel.

As I found out that Java, to preserve platform independency, does not support ICO files (or so I heard), I converted the icon to a PNG image (also with alpha channel)

However, Java just discards the alpha channel and "blends" it with white

The platform for which the application is being developed is Windows so I searched for an ICO reader for Java and found:
http://www.acproductions.de/commercial/aclibico/index.html

Has anyone used this library and can give me some directions?
If not, is there a way I can preserve the PNG alpha channel?

And by the way, can I set a specific Image for windows to use in the "ALT-TAB menu"?
I hate it when it stretches my image...

Thanks in advance
0
Comment
Question by:b_loco
  • 8
  • 6
  • 3
  • +2
22 Comments
 
LVL 2

Author Comment

by:b_loco
Comment Utility
Here is the code I'm using to import the icon:

toolbarOpen.setIcon(new ImageIcon(ClassLoader.getSystemResource("open.png")));

I've googled this matter for a long time, but couldn't get an objective answer
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>GIF is not an option because it doesn't have an alpha channel

You can save a the image to a gif where the alpha channel is used to support the transparency
0
 
LVL 9

Assisted Solution

by:DrWarezz
DrWarezz earned 25 total points
Comment Utility
The only thing I think I can help you with there is:
>"And by the way, can I set a specific Image for windows to use in the "ALT-TAB menu"?"

So, you want to set an Icon for your Java app, right?

See here: http://www.javaalmanac.com/egs/java.awt/frame_FrameIcon.html

Code:
    // Set icon
    Image icon = Toolkit.getDefaultToolkit().getImage("icon.gif");
    frame.setIconImage(icon);

Hope that helps :)
[r.D]
0
 
LVL 2

Author Comment

by:b_loco
Comment Utility
>>You can save a the image to a gif where the alpha channel is used to support the transparency

But GIF only supports "total" transparency unlike PNG which supports a degree of transparency
(hope you can understand me)

DrWarezz:

I know how to set the icon for the application. What I wanted to know was if it is possible to set an icon for the taskbar (16x16) and a different one for the "alt-tab" menu (48x48 or 64x64)

but that is secondary, the most important thing I need to solve is getting Java to read an image with "PNG like" transparency
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
AFAIK, what you want to do is show a round image. You can do that in Java both with gif and png
0
 
LVL 2

Author Comment

by:b_loco
Comment Utility
yes I can do it

with GIF, I force my image to have a "hard border", or set matte to a certain color and it will blend it with that color. But with the first option the icon looks very crappy, and with the second option, it will only look good if the background color is the same as the color i used for matte and because the icon is used in the window title (blue background) and taskbar (grey background), it won't look well in one of them...

with PNG I was supposed to overcome this problem, because PNG has an alpha channel, with which I can tell that the pixels around the circle are, for example, of the color blue but only filled by 50%, therefore blending it with whatever background I put it in. It's like an anti-alias feature of png...

But Java is ignoring the alpha channel's information, simply blending it with white

Is it a bug or simply an unsupported feature?
Should I load the png in a different way for it to work?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 25 total points
Comment Utility
>>with GIF, I force my image to have a "hard border",

There's no need for that. Simply make the area outside the round bit transparent
0
 
LVL 2

Author Comment

by:b_loco
Comment Utility
Yes ;)

That's what I meant by "hard border", no anti-alias, therefore poor results
I cannot smooth the borders of the circle. The border pixels of the circle have to have 100% of alpha in order to be a GIF

Take Windows XP icons for instance, most of them have a "drop shadow", and no matter what background color you set on the desktop, they always blend in nicely, because of the alpha channel that windows xp icons have

With most distributions of linux, we have the same nice behaviour because most icons are PNGs which also have an alpha channel
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
Comment Utility
> But Java is ignoring the alpha channel's information, simply blending it with white
If you are trying to use it as a frame icon, i think it's Windows' fault.
What you see in your windows bar and on the ALT+TAB splash is handeled by Windows and has nothing to do with java.
I have played a little with alpha channel on PNG files and realized that even IE6.0 does not support it... one more reason to switch to a good browser.
If switching to a good OS is not an option, i guess you need to say bye to the alpha channel :-(
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>That's what I meant by "hard border", no anti-alias, therefore poor results

OK. I'm with you now and reading on ...
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 30

Expert Comment

by:GrandSchtroumpf
Comment Utility
i guess that internally, the JVM transforms the png into an ico file and passes that to Windows...
i don't think the ico image format supports alpha channel (i might be wrong, i'm not an ico expert).
if that's the case, i don't think java can do anything better than blending with an arbitrary color, and white seems to be a good choice.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
I've just made a png a showed it in java 1.4. It certainly appears to be anti-aliased, as it was at its creation
0
 
LVL 2

Author Comment

by:b_loco
Comment Utility
GrandSchtroumpf:
>>i don't think the ico image format supports alpha channel (i might be wrong, i'm not an ico expert).
It does. It only works on XP though, but as it still has the other formats it just picks up the best one without an alpha channel

 CEHJ:
>> I've just made a png a showed it in java 1.4. It certainly appears to be anti-aliased, as it was at its creation

Did it have transparency?
If so, did you import it to Java in any fancy way or just how I did it?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>Did it have transparency?

Yes, the background is transparent - it's simply a circle image.

>>If so, did you import it to Java in any fancy way or just how I did it?

Nothing fancy at all
0
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 25 total points
Comment Utility
Hi b_loco,

Transparency in icons is supported by windows. Yourproblem comes from the java png library which doesn't read the alpha channel if the image is truecolor. You need to convert it to a 256 colors indexed image, with alpha component in the palette.
0
 
LVL 30

Accepted Solution

by:
GrandSchtroumpf earned 50 total points
Comment Utility
CEHJ and Webstorm,
just to make things clear, b_loco does not want to use simple transparency (completely transparent pixels).
but he wants to use opacity(e.g. white at 20%, will produce pink if the background is red, light blue if the background is blue...).
you can create such png images with the GIMP 2, but when you convert from RGB to indexed, you lose the opacity settings.
0
 
LVL 2

Author Comment

by:b_loco
Comment Utility
Exacty what GrandSchtroumpf said.

I can't tell you anything more really, except my expectations:

I came here hoping that there was a different API I could use that dealt with PNGs the right way, or any class I didn't know of.

I'll leave the question around a few more days... you never know

I'm willing to try workarounds
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
These are the pixel values of my image where they are > 0:


40ed1f25,40ed1f25,20ed1f25,70ed1f25,70ed1f25,20ed1f25,60ed1f25,60ed1f25,60ed1f25,60ed1f25,
50ed1f25,50ed1f25,10ed1f25,10ed1f25,50ed1f25,50ed1f25,10ed1f25,10ed1f25,10ed1f25,10ed1f25,
10ed1f25,10ed1f25,6fed1f25,6fed1f25,30ed1f25,30ed1f25,7fed1f25,7fed1f25,20ed1f25,20ed1f25,
20ed1f25,20ed1f25,20ed1f25,20ed1f25,6fed1f25,6fed1f25,30ed1f25,30ed1f25,3fed1f25,3fed1f25,
6fed1f25,6fed1f25,6fed1f25,6fed1f25,3fed1f25,3fed1f25,2fed1f25,2fed1f25,6fed1f25,6fed1f25,
1fed1f25,1fed1f25,1fed1f25,1fed1f25,1fed1f25,1fed1f25,7fed1f25,7fed1f25,2fed1f25,2fed1f25,
6fed1f25,6fed1f25,fed1f25,fed1f25,fed1f25,fed1f25,fed1f25,fed1f25,4fed1f25,4fed1f25,
fed1f25,fed1f25,4fed1f25,4fed1f25,5fed1f25,5fed1f25,5fed1f25,5fed1f25,1fed1f25,6fed1f25,
6fed1f25,1fed1f25,3fed1f25,7fed1f25,7fed1f25,3fed1f25,
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Well - the printout of my png's pixels in Java demonstrates the alpha channel
0
 
LVL 13

Expert Comment

by:Webstorm
Comment Utility
I suggest to split the points : DrWarezz, CEHJ, GrandSchtroumpf, Webstorm
0
 
LVL 13

Expert Comment

by:Webstorm
Comment Utility
:-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now