wxImage and grayscale BMP files

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
vanarieleyen
Knows some wx things
Knows some wx things
Posts: 47
Joined: Thu Aug 29, 2019 3:55 am
Location: China, Shenzhen

wxImage and grayscale BMP files

Post by vanarieleyen »

I have a problem with BMP files that are in grayscale format.

Where are the pixels in grayscale stored? When I try to access them using for instance getRed() I don't receive the expected result.

When however the BMP file is in 24 bit RGB format and the content is grayscale (each channel has the same value) then I get the correct values.
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxImage and grayscale BMP files

Post by doublemax »

How do you load the bitmap and how do you access the pixel value?

wxImage is always be 24bit RGB, so all 3 values should be the same even when loading an 8 Bit bitmap. (Which probably has a palette that contains the grayscale values).
Use the source, Luke!
vanarieleyen
Knows some wx things
Knows some wx things
Posts: 47
Joined: Thu Aug 29, 2019 3:55 am
Location: China, Shenzhen

Re: wxImage and grayscale BMP files

Post by vanarieleyen »

I use the folowing code to load the bmp file:

Code: Select all

image.LoadFile(filePath, wxBITMAP_TYPE_BMP);
This works fine, when the bmp file is 24 bit (3x8 bit channels) I can read the rgb values, but when the bmp file is in grayscale format it fails.

I use the following to retrieve the pixel values:

Code: Select all

image.GetRed(x,y);
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxImage and grayscale BMP files

Post by doublemax »

Can you upload the image anywhere? I can't reproduce this with any bmp i created.

BTW: What platform and wxWidgets version are you using?
Use the source, Luke!
vanarieleyen
Knows some wx things
Knows some wx things
Posts: 47
Joined: Thu Aug 29, 2019 3:55 am
Location: China, Shenzhen

Re: wxImage and grayscale BMP files

Post by vanarieleyen »

I use widgets vs. 2.8 with windows 8
Attachments
bol.bmp.zip
(206.98 KiB) Downloaded 57 times
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxImage and grayscale BMP files

Post by doublemax »

I see it now. It seems that the BMP uses a header that wxWidgets doesn't support properly.

https://trac.wxwidgets.org/ticket/18634
Use the source, Luke!
vanarieleyen
Knows some wx things
Knows some wx things
Posts: 47
Joined: Thu Aug 29, 2019 3:55 am
Location: China, Shenzhen

Re: wxImage and grayscale BMP files

Post by vanarieleyen »

You guys are Amazing!! :D

I have checked the bug track and saw that it is already fixed. I have immediately applied the fix and compiled it again.

It is now working without any issues.

Thank you for the quick fix!
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxImage and grayscale BMP files

Post by doublemax »

Just be warned that i'm not 100% sure if the patch i posted doesn't have any unwanted side-effects under certain conditions. Make sure to follow the ticket so that you know when an "official" fix is committed.
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxImage and grayscale BMP files

Post by ONEEYEMAN »

Hi,
Just to add - 2.8 is old.
You need to upgrade wx to at least 3.0 (stable) or 3.1.3 (latest) release.

You will get many more fixes and better library.

Thank you.
vanarieleyen
Knows some wx things
Knows some wx things
Posts: 47
Joined: Thu Aug 29, 2019 3:55 am
Location: China, Shenzhen

Re: wxImage and grayscale BMP files

Post by vanarieleyen »

Yes, I understand that it is not an official fix but until now I didn't notice any problems, but of course will follow the ticket.

And no, I made a mistake - I am currently using 3.1.2. Yesterday however I have also tried 3.1.3 but received an error related to wxClientDC. This made me switch back to 3.1.2 - I will wait a while until 3.1.3 is more mature.

Thanks for all the efforts.
Arie
Post Reply