How to remove transparency from an image Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 362
Joined: Fri Dec 09, 2016 7:28 am
Contact:

How to remove transparency from an image

Post by cutecode » Sat Nov 21, 2020 3:19 pm

I have an wxImge which must be drawn on wxDC not using transparency

Is this right code?

Code: Select all

         image.Rescale(x0, y0, wxIMAGE_QUALITY_NEAREST);
	wxBitmap bmp(image);
	
	bool useMask = false
	pdc->DrawBitmap(bmp, x, y, useMask);
The problem is, I convert this to wxPdfDocument and my service reponds

Code: Select all

ImageTransparency is set, but shouldn't be
thank you
wx 3.1.4 win/mac/linux

regards,
Alexander Saprykin
https://v2.dental-soft.ru

User avatar
doublemax
Moderator
Moderator
Posts: 15275
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to remove transparency from an image

Post by doublemax » Sat Nov 21, 2020 3:52 pm

Regarding the actual drawing operation that should work, but it's possible that wxPdfDocument still embeds the image data with alpha. Try calling wxImage::ClearAlpha() https://docs.wxwidgets.org/trunk/classw ... d9065628b1
Use the source, Luke!

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2668
Joined: Sun Jan 03, 2010 5:45 pm

Re: How to remove transparency from an image

Post by PB » Sat Nov 21, 2020 4:02 pm

I would also make sure the wxBitmap is created with 24-bit instead of 32-bit depth . Not sure what the default wxBITMAP_SCREEN_DEPTH translates to on all platforms.

User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 362
Joined: Fri Dec 09, 2016 7:28 am
Contact:

Re: How to remove transparency from an image

Post by cutecode » Sat Nov 21, 2020 4:40 pm

if I call ClearAlpha() an assertion is called stating that this image does not have Alpha.

Setting 24-bit depth did the trick, and this error does not reported.

But my image has black background instead of white

How to make it white?
If I call

Code: Select all

  image.Replace(image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue(), 255, 255, 255);
The background is white, but image is not shown as expected
my code

Code: Select all

        if(image.HasAlpha())
              image.ClearAlpha();

        image.Replace(image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue(), 255, 255, 255);

	wxBitmap bmp(image, 24);
	pdc->DrawBitmap(bmp, x + dx, y + dy, false);

wx 3.1.4 win/mac/linux

regards,
Alexander Saprykin
https://v2.dental-soft.ru

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2668
Joined: Sun Jan 03, 2010 5:45 pm

Re: How to remove transparency from an image

Post by PB » Sat Nov 21, 2020 4:51 pm

So is your image transparent or not?

If so, did you call its ConvertAlphaToMask()?

If not, calling those GetMask...() may not be useful.

Also, not to nitpick but wxImage does not have a "background". It just has pixels without any notion what is "background" and what is not. This is actually what transparency may be used for, to NOT draw certain (fully transparent) pixels over the background, i.e., things drawn under the image.

User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 362
Joined: Fri Dec 09, 2016 7:28 am
Contact:

Re: How to remove transparency from an image

Post by cutecode » Sat Nov 21, 2020 5:08 pm

I don't know whether it is tranaparent or not
My service just responed that

Code: Select all

ImageTransparency is set, but shouldn't be
I just want to write a code for any image without transparancy

I'm not an mage-alpha expert
wx 3.1.4 win/mac/linux

regards,
Alexander Saprykin
https://v2.dental-soft.ru

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2668
Joined: Sun Jan 03, 2010 5:45 pm

Re: How to remove transparency from an image

Post by PB » Sat Nov 21, 2020 5:53 pm

You do not have to be an expert to tell if the image is transparent or not.

And only you can tell if your image is. However, if the image did not have alpha, then it is not transparent.

You already told us the reason for the error was using 32-bit wxBitmap (where the fourth byte is used for alpha) and the problem now was how to draw the image "background" (see my response to that in my previous post)?

User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 362
Joined: Fri Dec 09, 2016 7:28 am
Contact:

Re: How to remove transparency from an image

Post by cutecode » Sat Nov 21, 2020 7:25 pm

thank you ConvertAlphaToMask(); did help
wx 3.1.4 win/mac/linux

regards,
Alexander Saprykin
https://v2.dental-soft.ru

utelle
Moderator
Moderator
Posts: 993
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: How to remove transparency from an image

Post by utelle » Sat Nov 21, 2020 10:36 pm

cutecode wrote:
Sat Nov 21, 2020 7:25 pm
thank you ConvertAlphaToMask(); did help
The method wxDC::DrawBitmap allows to disable the mask. And that is also respected by wxPdfDC. However, a wxBitmap can have both, a mask and an alpha channel. Therefore wxPdfDC uses transparency, if the bitmap has an alpha channel.

If the underlying wxPdfDocument instance of a wxPdfDC is in PDF/A-1b mode, it would be possible to apply method ConvertAlphaToMask automatically to get rid of the alpha channel (which is forbidden for PDF/A-1b conformance). Most likely, I will add this to wxPdfDC. Nevertheless, it will be necessary to visually check the resulting PDF to make sure that the appearance of the document is as intended.

Post Reply