Another issue with wxImage scaling and MSVC 2022 compiler optimization (Issue #23164)

36 views
Skip to first unread message

PB

unread,
Jan 21, 2023, 3:16:49 PM1/21/23
to wx-...@googlegroups.com, Subscribed

On wxForum, there is a report about what looks like another MSVC 2022 compiler optimization bug manifesting with wxImage scaling.

This time it appears to be in wxImage::ShrinkBy(), (called from wxImage::Scale() when certain conditions are met), where in the Release build the scaled image is monochrome. I tested 64-bit shared build of the current master with MSVS 2022 v17.4.3 (using CMake).

Scaled image in debug build
wximage-shrinkby-debug

Scaled image in release build
wximage-shrinkby-release
Also as can be seen in the above picture, even the color used for image labels is different in the release build? Moreover, the sample menubar is also black (not in Debug build, Windows is in the dark mode). I tried to build the drawing sample and it seems broken too, the the top of the canvas background is black and so is the menubar. I did not look into this any further, having ran out of time for today.

How to reproduce:

  1. Use the following patch to the minimal sample.
diff --git a/samples/image/canvas.cpp b/samples/image/canvas.cpp
index 25139741f2..1dcb5e5e00 100644
--- a/samples/image/canvas.cpp
+++ b/samples/image/canvas.cpp
@@ -113,7 +113,7 @@ MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
     my_toucan_flipped_both = wxBitmap(image.Mirror(true).Mirror(false));
     my_toucan_grey = wxBitmap(image.ConvertToGreyscale());
     my_toucan_head = wxBitmap(image.GetSubImage(wxRect(40, 7, 80, 60)));
-    my_toucan_scaled_normal = wxBitmap(image.Scale(110,90,wxIMAGE_QUALITY_NORMAL));
+    my_toucan_scaled_normal = wxBitmap(image.ShrinkBy(2,2));
     my_toucan_scaled_high = wxBitmap(image.Scale(110,90,wxIMAGE_QUALITY_HIGH));
     my_toucan_blur = wxBitmap(image.Blur(10));
  1. Build the sample in the Release configuration.
  2. Run the sample and scroll down so that the toucan picture "scaled with normal quality" can be seen.

I did not try any other compilers.


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23164@github.com>

Maarten

unread,
Jan 21, 2023, 4:57:20 PM1/21/23
to wx-...@googlegroups.com, Subscribed

Seems that alpha calculation is optimized away. Moving it inside the if fixes it for me:

diff --git "a/src/common/image.cpp" "b/src/common/image.cpp"
index 6360f5a447..fc0f391381 100644
--- "a/src/common/image.cpp"
+++ "b/src/common/image.cpp"
@@ -390,11 +390,11 @@ wxImage wxImage::ShrinkBy( int xFactor , int yFactor ) const
                     unsigned char red = pixel[0] ;
                     unsigned char green = pixel[1] ;
                     unsigned char blue = pixel[2] ;
-                    unsigned char alpha = 255  ;
-                    if ( source_alpha )
-                        alpha = *(source_alpha + y_offset + x * xFactor + x1) ;
                     if ( !hasMask || red != maskRed || green != maskGreen || blue != maskBlue )
                     {
+                        unsigned char alpha = 255;
+                        if (source_alpha)
+                            alpha = *(source_alpha + y_offset + x * xFactor + x1);
                         if ( alpha > 0 )
                         {
                             avgRed += red ;


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23164/1399340257@github.com>

PB

unread,
Jan 22, 2023, 8:24:16 AM1/22/23
to wx-...@googlegroups.com, Subscribed

I can confirm that after using Maarten's patch the issue is gone. I have also upgraded MSVC from 17.4.3 to 17.4.4 but without the patch, the issue was still there.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23164/1399483268@github.com>

VZ

unread,
Jan 28, 2023, 7:39:48 PM1/28/23
to wx-...@googlegroups.com, Subscribed

I don't see any drawback in doing this, so I'm going to apply this soon, thanks for finding and fixing this, respectively!


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23164/1407523431@github.com>

VZ

unread,
Jan 29, 2023, 2:36:09 PM1/29/23
to wx-...@googlegroups.com, Subscribed

Closed #23164 as completed via e51bc73.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issue/23164/issue_event/8382500327@github.com>

VZ

unread,
Jan 29, 2023, 2:51:03 PM1/29/23
to wx-...@googlegroups.com, Subscribed

Oh, and I'll cherry-pick this into 3.2 too soon.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23164/1407753060@github.com>

Reply all
Reply to author
Forward
0 new messages