Bug 35040 - Problems with PNG transparency in Image Class
Summary: Problems with PNG transparency in Image Class
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 2.1   Edit
Hardware: PC Windows XP
: P3 normal with 33 votes (vote)
Target Milestone: ---   Edit
Assignee: Steve Northover CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 41812 45087 66972 73170 83892 (view as bug list)
Depends on:
Blocks: 14905
  Show dependency tree
 
Reported: 2003-03-14 13:20 EST by Sebastian Bauer CLA
Modified: 2005-06-22 17:46 EDT (History)
24 users (show)

See Also:


Attachments
The png file with transparency (310 bytes, image/png)
2003-03-14 13:22 EST, Sebastian Bauer CLA
no flags Details
Example of PNG alpha trans rendering on menu and toolbar under WinXP (16.24 KB, image/png)
2004-08-09 05:55 EDT, rich boakes CLA
no flags Details
Screenshoot (48.08 KB, image/png)
2004-12-08 05:06 EST, Stephan Michels CLA
no flags Details
For future reference, snippet that runs all the widgets that have setImage. Notice the task tray item and the menu item. (4.15 KB, text/plain)
2005-04-14 14:17 EDT, Christophe Cornu CLA
no flags Details
Sample png icon with alpha information used by above snippet (to be copied in same directory as snippet) (638 bytes, image/png)
2005-04-14 14:19 EDT, Christophe Cornu CLA
no flags Details
gif image with transparency info to compare with alpha in above snippet (537 bytes, image/gif)
2005-04-14 14:20 EDT, Christophe Cornu CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Sebastian Bauer CLA 2003-03-14 13:20:04 EST
Image class ignores the transparency of the attached PNG file with 
transparency while the Windows Exlorer shows the transparency.
Comment 1 Sebastian Bauer CLA 2003-03-14 13:22:18 EST
Created attachment 4141 [details]
The png file with transparency

This is the attachment
Comment 2 Sebastian Bauer CLA 2003-03-16 16:55:40 EST
The attached PNG file is a indexed pallete based one and contains a TRNS chunk 
with index'ed alhpa values and I looked into the Image, ImageData and PNG 
loading class and they handle this case properly.

They must be a problem when bringing the imageData Object on the display. I 
use the image in a ToolItem.
Comment 3 Sebastian Bauer CLA 2003-03-16 18:05:42 EST
Ok, more infos:

I loaded the image like this:

  ImageData source = new ImageData(name);
  ImageData mask = source.getTransparencyMask();
  Image image = new Image(shell.getDisplay(), source, mask);

Because the png image does not contain a transparent pixel (== -1) but only 
alpha values source.getTransparencyMask() will return a mask with all bits set.

Changing the code to
  ImageData source = new ImageData(name);
  Image image = new Image(shell.getDisplay(), source);
(or
  Image image = new Image(shell.getDisplay,name);
)

...results in the same problem for PNG images (but GIF transparency still 
works). The png image object has the alphaData set but it seems to be ignored 
for ToolItems when the graphics is brought onto the display.

Here are some solutions to fix the problem:
1) If the PNG file looks like the previous attached (only has one entry in the 
TRNS chunk which is set to 0 meaning that it is full transparent) the PNG file 
gets automatically a transparentPixel to this color entry assigned.
2) Add proper alpha blending in ToolItem and maybe other classes which are 
missing this.
3) source.getTransparencyMask() returns a "better" mask if getTransparency() 
returns TRANSPARENCY_ALPHA. Maybe a threseshold should be specified (and 
defaults to 0x7f)

The best would be to implement all three points.
Comment 4 Silenio Quarti CLA 2003-03-17 11:10:42 EST
I don't think there is support on the Windows ToolBar for alpha
blended images. It only takes an icon (color and mask).

On the other hand, I believe Window XP supports icons with
alpha transparency. I need to investigate this more.
Comment 5 Dimas Moreira Junior CLA 2003-07-31 00:57:06 EDT
I'm having the same problem in the GTK port. I tested versions 2.1.1 and 3.0.
Comment 6 Christophe Cornu CLA 2003-09-29 09:54:54 EDT
*** Bug 41812 has been marked as a duplicate of this bug. ***
Comment 7 Grant Gayed CLA 2003-10-21 16:35:38 EDT
*** Bug 45087 has been marked as a duplicate of this bug. ***
Comment 8 Robert Jenks CLA 2003-12-12 16:32:52 EST
One thing I found is that imageData.getTransparencyMask() is documented as 
returning null if the image has no transparency or if it is not an icon, but 
the Win32 implementation always returns a mask.  If the imagedata does not 
have a transparentPixel or a transparency mask it simply returns a mask with 
everything opaque.  It never takes the alphaMask into account.
Comment 9 Robert Jenks CLA 2003-12-15 19:38:18 EST
Here is a related Win32 article about how to use 32bit (RGBA) images in WinXP 
toolbars:

http://www.codeguru.com/toolbar/iebars.html
Comment 10 Grant Gayed CLA 2004-01-20 09:35:15 EST
*** Bug 50163 has been marked as a duplicate of this bug. ***
Comment 11 Daniel Spiewak CLA 2004-04-12 16:26:57 EDT
I don't know about the rest of you, but this bug is increadably frustrating and
crippling for me.  I'm writing a standalone app using SWT right now, and being
forced to use GIFs isn't helping one bit.  I don't care how it's done, but I
sure hope it's done soon.  Perhaps you could hack the ImageData so that the
platform is tricked into thinking it's another type of image.  Manually tune the
alpha transparency into the background.
Comment 12 Sébastien Moran CLA 2004-04-13 03:25:05 EDT
I totally agree with you Daniel, GIF sux, all my icons are .PNGs and converting
them to gif make them so ugly... 
Please fix it :) 
Comment 13 Billy Biggs CLA 2004-06-15 13:19:29 EDT
*** Bug 66972 has been marked as a duplicate of this bug. ***
Comment 14 rich boakes CLA 2004-08-09 05:55:42 EDT
Created attachment 13810 [details]
Example of PNG alpha trans rendering on menu and toolbar under WinXP

An example of inconsistent behaviour for PNG alpha transparrency

In the attachment.  The toolbar icons display a black background (this is not
expected), whereas the menu icons are properly rendered.  

Also note that the view icons are also alpha-trans PNG's, and these appear to
render perfectly on both light and dark backgrounds.
Comment 15 Stefan Matthias Aust CLA 2004-08-29 12:41:20 EDT
As everybody else here, I've an PNG image with alpha transparency which isn't
drawn correctly on Windows XP.  The image has a palette, therefore 8 bit color
depth, and transparancy values for all palette entries (tRNS chunk).  I stepped
through the PNG loader and it seems that the image is correctly loaded.  Its
image data says

 alpha = -1
 depth = 8
 transparentPixel = -1
 type = -1
 alphaData = a byte[] for all pixels
 palette.isDirect = false

if rendered directly onto a GC, it works. Using it for MenuItems works, too.  
However, if used for ToolItems, Buttons or Labels, I get ugly artifacts. The
alphaData is ignored, there's a white or black background instead.  Using a
CLabel works (because that uses GC#drawImage()).  That method eventually uses
drawBitmapAlpha() which does the everything "by hand" (quite noticable for
larger bitmaps - it's awful slow - can't Windows draw alpha images? Why isn't
AlphaBlend() used?)

Otherwise, ImageList objects are used to store the images for Windows. According
to MSDN, 32 bit alpha channel images are supported for Windows XP. So why
doesn't it work as expected? I looked into the codeguru article mentioned above
and it seems, that it is enough to provide a 32bit image and a null mask. 
However, it seems that the Windows bitmap resource created from the Image object
has only 8 or 24 bits but not 32 as it should have.  The alpha values aren't
taken into account.

I tried to make SWT create a 32 bit alpha channel Windows bitmap but failed. 
I'd guess that the OS.BitBlt calls in Image#init() and ImageList#copyBitmap will
kill the alpha channel.

The code attached creates a 32 bit bitmap, but I can't tell whether with or
without alpha values...

private Image convertImage(Image image) {
 ImageData id = image.getImageData();
 if (id.depth == 8) {
  ImageData id2 = new ImageData(id.width, id.height, 32, 
   new PaletteData(0xFF00, 0xFF0000, 0xFF000000), id.scanlinePad, 
   new byte[id.width * id.height * 4]);
  int i = 0;
  for (int y = 0; y < id.height; y++) {
   for (int x = 0; x < id.width; x++) {
    RGB rgb = id.palette.colors[id.getPixel(x, y)];
    id2.data[i++] = (byte) rgb.blue;
    id2.data[i++] = (byte) rgb.green;
    id2.data[i++] = (byte) rgb.red;
    id2.data[i++] = (byte) id.getAlpha(x, y);
   }
  }
  image = new Image(Display.getCurrent(), id2);
 }
 return image;	
}

Please fix the bug :)
Comment 16 Grant Gayed CLA 2004-09-17 12:48:11 EDT
*** Bug 73170 has been marked as a duplicate of this bug. ***
Comment 17 Jay Batson CLA 2004-09-29 00:56:56 EDT
This is one of those "This just has to work for SWT to be credible" type of bugs.  Nobody can afford to 
use GIF in toolbars now; application users expect higher quality icons, and GIF sucks.

I'm a small ISV self-funding the development of my product, and my (contract) coders can jump in and 
help fix this bug -- IF they can get somebody to help jump-start them.  They know SWT pretty well 
from the API-user point of view, but don't know SWT implementation internals. I'd rather not have to 
pay them to become SWT-gods.  So if somebody can hand us a basic map, compass, and tell us the 
foxholes to watch out for, we can go try to chase this bug down. 

Silenio -- any chance for a few minutes of your time with my guys to jump-start them?
Comment 18 David J. Orme CLA 2004-10-22 10:39:59 EDT
Last month in comment 17, Jay Baston offered the use of his developers to help
to fix this bug.  Is anything happening on this front?
Comment 19 Christophe Cornu CLA 2004-11-19 15:21:55 EST
checking in fix
Comment 20 Jay Batson CLA 2004-11-19 15:27:17 EST
Thanks a ton, Christophe!
Comment 21 Christophe Cornu CLA 2004-11-19 15:30:21 EST
v>2004119
fixed with Silenio: toolbar now supports images with alpha on Windows and GTK

Comment 22 Christophe Cornu CLA 2004-11-19 15:33:40 EST
On Windows full alpha is supported with XP and the manifest (comctrl 6.0). For 
other Windows platforms, a threshold (127) is used to generate a transparent 
mask.
Comment 23 Andrée Proulx CLA 2004-11-19 17:20:32 EST
awesome thanks!
Comment 24 Sébastien Moran CLA 2004-11-24 16:14:52 EST
Thanks a lot guys !!!!!
Comment 25 Steve Northover CLA 2004-11-24 16:37:18 EST
Thanks to Chrix and SSQ!
Comment 26 Stephan Michels CLA 2004-12-08 05:03:10 EST
First, a big thank you that it almost works :-) Nevertheless it seems that
transparent png images does work correct in menus or shell icons (GTK). I don't
know if is a bug of the menu implementations or the image class itself.
Comment 27 Stephan Michels CLA 2004-12-08 05:06:48 EST
Created attachment 16451 [details]
Screenshoot

A screenshoot where I used the same image for the first toolbar action and the
first menu action.

Ps. I used a CVS snapshot.
Comment 28 Jens Rossbach CLA 2005-03-29 15:08:28 EST
Is this now still a problem? If not, in wich SWT version is it fixed? I tried
with 3.1M5a and it still seems to be existing...
Comment 29 Christophe Cornu CLA 2005-04-14 14:17:38 EDT
Created attachment 19920 [details]
For future reference, snippet that runs all the widgets that have setImage. Notice the task tray item and the menu item.
Comment 30 Christophe Cornu CLA 2005-04-14 14:19:41 EDT
Created attachment 19921 [details]
Sample png icon with alpha information used by above snippet (to be copied in same directory as snippet)
Comment 31 Christophe Cornu CLA 2005-04-14 14:20:25 EDT
Created attachment 19922 [details]
gif image with transparency info to compare with alpha in above snippet
Comment 32 Stephan Michels CLA 2005-04-14 15:43:15 EDT
Thank you, I tested your code against the CVS HEAD of SWT on GTK/x86. The
transparency of the PNG image within the Button, Label and the Shell icon
doesn't work. The GIF image seems to work in any case.

The background of the tray icon looks a bit odd, but I think its more a 
problem of GTK.
Comment 33 Michael Seele CLA 2005-04-19 05:14:12 EDT
when do you support complete png support? in 3.1 final?
imho it is the most important feature i've been missing in swt!!!
Comment 34 Steve Northover CLA 2005-04-19 08:50:48 EDT
It may be that we run out of time for 3.1.  We have fixed many places 
already.  Is there a particular widget that you are using that is still not 
fixed?  Which platform?
Comment 35 Michael Seele CLA 2005-04-19 08:53:31 EDT
i want to use buttons with png images! 
looks very adventurous in 3.1M6!!! ;-)
Comment 36 Steve Northover CLA 2005-04-19 09:07:10 EDT
It works right now but you need to run with the XP manifest.
Comment 37 Andrée Proulx CLA 2005-04-19 15:17:24 EDT
When can we start replacing all GIF images in Eclipse to PNGs? :)

Especially wizard graphics - I would like to start with the wizard banner 
graphics because on some platform the background is not white. We would make 
immediate use of alpha channel to resolve this outstanding issue (since Eclipse 
1.0).

To upgrade the icons in eclipse from GIFs to PNGs, we need to assure that a one 
to one replacement on each GIF image with a PNG will work in every possible 
contexts (toolbar, menu, context menu, tab, local toolbar, tree, button, table, 
editor, dialog, title bar, wizard, etc, etc...) and platforms.

I am very interested in upgrading all the GIFs in eclipse to PNGs format, 
because this will address many dithering and readability issues with GIFs. But 
as well it will have a significant quality impact to the overall look and feel.
Comment 38 Kim Peter CLA 2005-04-19 15:27:17 EDT
+1 for PNG support for all UI graphics. See also related bug in support of PNG 
for wizards: https://bugs.eclipse.org/bugs/show_bug.cgi?id=14905
Comment 39 Thomas Schindl CLA 2005-04-20 02:47:07 EDT
(In reply to comment #36)
> It works right now but you need to run with the XP manifest.

Steve, can you enlight my brain and teach me what does that mean, "run with XP
manifest"?

Comment 40 Steve Northover CLA 2005-04-20 08:21:45 EDT
You need to copy the file javaw.exe.manifest from the SWT distribution into 
the directory where you run javaw.exe in order to get the XP look and feel.  
Most XP native applications embed the contents of this file into their 
executable but we can't modify the bits of javaw.exe.  Thanks Microsoft!
Comment 41 Felipe Heidrich CLA 2005-04-29 14:59:50 EDT
*** Bug 83892 has been marked as a duplicate of this bug. ***
Comment 42 Felipe Heidrich CLA 2005-04-29 17:13:05 EDT
fixed in head > 20050429
fixed on winxp (common control 6), gtk and motif.
Comment 43 Stephan Michels CLA 2005-04-30 03:52:01 EDT
Hey, great work! But I have a last minor problem with the PNG image as 
a shell icon on GTK, which doesn't work. The GIF image works pretty well.

BTW, I can't reopen the bug.
Comment 44 Billy Biggs CLA 2005-04-30 11:38:44 EDT
Thanks!  I opened bug 93349 with a patch for the Shell decorations case on
Linux-GTK.
Comment 45 Michael Seele CLA 2005-05-02 01:36:48 EDT
are the common controls 6 the winXP controls? 
and can i only use these controls with adding a manifiest file into the
javaw.exe path?
Comment 46 Steve Northover CLA 2005-05-02 08:58:53 EDT
>BTW, I can't reopen the bug.

Not sure why.  However, please open specific problem reports to capture places 
where we might have bugs, rather than reopening this one.

>and can i only use these controls with adding a manifiest file into
>the javaw.exe path?

Yes, you'll need to the manifest for this to work right now on XP.
Comment 47 Kanstantsin Chernysh CLA 2005-06-22 17:10:01 EDT
What about Windows 2000 and Windows 98. Whill this solution with .manifest work?
Comment 48 Steve Northover CLA 2005-06-22 17:46:58 EDT
The XP .manifest is not recognized on those platforms however, when we 
encounter an image with alpha, we construct a mask for the image.