Opened 7 years ago

Closed 6 years ago

#4087 closed defect (duplicate)

png transparency fails for fully transparent image

Reported by: lvdlinden Owned by:
Priority: high Milestone:
Component: GUI-all Version:
Keywords: wxImage png transparency Cc: lvdlinden, exsudat@…
Blocked By: Blocking:
Patch: no

Description

A fully transparent image is rendered as black (opaque).

When a single pixel is changed, the image is rendered correctly.

I've added a screenshot. The black square should be transparent.

I'm using wxGtk 2.8.0 on linux 2.6.20 (kubuntu)

Attachments (4)

test.png download (2.3 KB) - added by lvdlinden 7 years ago.
screenshot, black square should be transparent
png-test.cpp download (961 bytes) - added by lvdlinden 7 years ago.
minimal test application
fully_transparent.png download (200 bytes) - added by lvdlinden 7 years ago.
fully transparent png image (32x32 pixels)
almost_transparent.png download (210 bytes) - added by lvdlinden 7 years ago.
almost fully transparent png image (32x32 pixels)

Download all attachments as: .zip

Change History (12)

Changed 7 years ago by lvdlinden

screenshot, black square should be transparent

Changed 7 years ago by lvdlinden

minimal test application

comment:1 Changed 7 years ago by lvdlinden

File Added: png-test.cpp

Changed 7 years ago by lvdlinden

fully transparent png image (32x32 pixels)

comment:2 Changed 7 years ago by lvdlinden

File Added: fully_transparent.png

Changed 7 years ago by lvdlinden

almost fully transparent png image (32x32 pixels)

comment:3 Changed 7 years ago by lvdlinden

File Added: almost_transparent.png

comment:4 Changed 6 years ago by wojdyr

  • Keywords wxImage png transparency added

comment:5 Changed 6 years ago by wojdyr

  • Component set to GUI-all

comment:6 Changed 6 years ago by moonkid

  • Cc exsudat@… added
  • Priority changed from normal to high

comment:7 Changed 6 years ago by dickhamill

  • Status changed from new to infoneeded_new

Reproduced this issue under Windows and Linux. Can't reproduce under Mac.

Here's what's happening:

When your png file is loaded (common/imagpng.cpp), the CheckTransparency function is used to determine if your image will require an alpha channel (Transparency_Alpha), or if you can get away with just using a mask (Transparency_Mask).

If your image only has 0x00 and 0xFF for alpha values, an entire alpha channel isn't needed, because the draw function can use a simple mask to draw. So, in this case, it creates your image without an alpha channel, and rewrites the image data to turn any pixels with alpha=0x00 into the transparent color. It chooses a transparent color by scanning the image data and choosing the first unused RGB value it can find. It then sets the attribute on the bitmap to specify that it has a mask, and specifies the transparent color.

When you call
dc.DrawBitmap( bitmap1_, 4, 4 );
there's a default parameter of "false" for the fourth parameter, which is "useMask". Because your image only contains 0x00 for the alpha channel, it falls under the special case mentioned above - which is that it's going to try to draw it with a mask instead of a full alpha channel.
And, since the "useMask" defaults to false, it draws the image without the mask it created.

So, although not obvious, the solution is easy. Change your DrawBitmap line to this:
dc.DrawBitmap( bitmap1_, 4, 4, true );
and you'll force the bitmap to be drawn with transparency.

This fixed the issue under Windows and Linux.

comment:8 Changed 6 years ago by wojdyr

  • Resolution set to duplicate
  • Status changed from infoneeded_new to closed

dickhamill: thanks for this analysis.
The same problem was reported before as #3019, so I'm closing this ticket.

Note: See TracTickets for help on using tickets.