Phoenix icon indicating copy to clipboard operation
Phoenix copied to clipboard

AssertionError in FloatCanvas ScaledBitmap zooming

Open komoto48g opened this issue 7 months ago • 3 comments

Operating system: Windows 11 wxPython version & source: 4.2.3 (pypi) Python version & source: 3.12

Description of the problem: A wxAssertionError (C++ assertion failure) occurs when zooming in or out on a ScaledBitmap in FloatCanvas beyond certain limits -- either too far in or out.

For zooming up:

Traceback (most recent call last):
  File "C:\Python312\Lib\site-packages\wx\lib\floatcanvas\FloatCanvas.py", line 452, in WheelEvent
    self.GUIMode.OnWheel(event)
  File "C:\Python312\Lib\site-packages\wx\lib\floatcanvas\GUIMode.py", line 132, in OnWheel
    self.Canvas.Zoom(1.1, point, centerCoords = "Pixel", keepPointInPlace=True)
  File "C:\Python312\Lib\site-packages\wx\lib\floatcanvas\FloatCanvas.py", line 758, in Zoom
    self.MoveImage(-delta, 'World')
  File "C:\Python312\Lib\site-packages\wx\lib\floatcanvas\FloatCanvas.py", line 718, in MoveImage
    self.Draw()
  File "C:\Python312\Lib\site-packages\wx\lib\floatcanvas\FloatCanvas.py", line 625, in Draw
    self._DrawObjects(dc, self._DrawList, ScreenDC, self.ViewPortBB, HTdc)
  File "C:\Python312\Lib\site-packages\wx\lib\floatcanvas\FloatCanvas.py", line 985, in _DrawObjects
    Object._Draw(dc, WorldToPixel, ScaleWorldToPixel, HTdc)
  File "C:\Python312\Lib\site-packages\wx\lib\floatcanvas\FCObjects.py", line 2157, in _Draw
    Img = self.Image.Scale(int(W), int(H))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
wx._core.wxAssertionError: C++ assertion ""data"" failed at ..\..\src\common\image.cpp(535) in wxImage::ResampleNearest(): unable to create image

For zooming down:

Traceback (most recent call last):
  File "C:\Python312\Lib\site-packages\wx\lib\floatcanvas\FloatCanvas.py", line 452, in WheelEvent
    self.GUIMode.OnWheel(event)
  File "C:\Python312\Lib\site-packages\wx\lib\floatcanvas\GUIMode.py", line 130, in OnWheel
    self.Canvas.Zoom(0.9, point, centerCoords = "Pixel", keepPointInPlace=True)
  File "C:\Python312\Lib\site-packages\wx\lib\floatcanvas\FloatCanvas.py", line 758, in Zoom
    self.MoveImage(-delta, 'World')
  File "C:\Python312\Lib\site-packages\wx\lib\floatcanvas\FloatCanvas.py", line 718, in MoveImage
    self.Draw()
  File "C:\Python312\Lib\site-packages\wx\lib\floatcanvas\FloatCanvas.py", line 625, in Draw
    self._DrawObjects(dc, self._DrawList, ScreenDC, self.ViewPortBB, HTdc)
  File "C:\Python312\Lib\site-packages\wx\lib\floatcanvas\FloatCanvas.py", line 985, in _DrawObjects
    Object._Draw(dc, WorldToPixel, ScaleWorldToPixel, HTdc)
  File "C:\Python312\Lib\site-packages\wx\lib\floatcanvas\FCObjects.py", line 2157, in _Draw
    Img = self.Image.Scale(int(W), int(H))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
wx._core.wxAssertionError: C++ assertion ""(width > 0) && (height > 0)"" failed at ..\..\src\common\image.cpp(450) in wxImage::Scale(): invalid new image size

To confrime this issue:

  1. Open the wxPython demo and launch the FloatCanvas demo window.
  2. From the menu, select Tests > Bitmap Test.
  3. In the toolbar, choose either Pan or Zoom.
  4. Scroll the mouse wheel up or down many times.

komoto48g avatar May 17 '25 07:05 komoto48g

@komoto48g was this fixed by your recent PR or does this problem still exist?

swt2c avatar Jun 03 '25 15:06 swt2c

This problem still exists. I think there must be a certain limit on zooming to fix it.

komoto48g avatar Jun 06 '25 18:06 komoto48g

The quick and dirty workaround to this would be to catch and silence the AssertionError(s) and do nothing if they happen - I.e., no zoom in either direction.

This is of course suboptimal, in particular for the case of zooming in, as rescaling an image so much is going to eat up large amounts of RAM.

Best solution would be to apply the scaling to the wx.DC/wx.GraphicsContext and not the image, but that may require reworking FloatCanvas more deeply - I don’t know anything about FloatCanvas so I have no idea whether this would be feasible or not.

infinity77 avatar Jun 06 '25 19:06 infinity77