Bug? BitmapCreator.DrawPath2 - ArrayIndexOutOfBoundsException in DrawWuLine

knutf

Active Member
Licensed User
Longtime User
Calling DrawPath2 with Filled=True on certain polygon paths throws ArrayIndexOutOfBoundsException from inside DrawWuLine. The bug is deterministic — the attached minimal project reproduces it 100%


Logs showing error message:
bc.mWidth:30, bc.mHeight:25
path x extremes: 7.29707 - 20.795
path y extremes: 9.325 - 23.8353
Error occurred on line: 55 (B4XMainPage)
java.lang.ArrayIndexOutOfBoundsException: Index 12 out of bounds for length 12
    at b4j.example.bitmapcreator._drawwuline(bitmapcreator.java:3613)
    at b4j.example.bitmapcreator._drawfullpath(bitmapcreator.java:2305)
    at b4j.example.bitmapcreator._drawpath2(bitmapcreator.java:2450)
    at b4j.example.b4xmainpage._button1_click(b4xmainpage.java:131)

Minimal repro code (full project attached):

Minimal repro code ()full project attached:
    Dim bc As BitmapCreator
    bc.Initialize(30, 25)
    Dim Path As BCPath
    Path.Initialize(19.24428,14.80650)
    Path.LineTo(18.92485,10.29233)
    Path.LineTo(14.41068,9.88168)
    Path.LineTo(8.92918,9.32500)
    Path.LineTo(7.29707,14.80650)
    Path.LineTo(7.38147,21.83570)
    Path.LineTo(14.41068,23.83530)
    Path.LineTo(20.79500,21.19083)
    Path.LineTo(19.24428,14.80650)
    Dim brush As BCBrush = bc.CreateBrushFromColor(xui.Color_Black)
    bc.DrawPath2(Path, brush, True, 0)

The polygon is a small octagon (~13×15 px) with all vertices safely inside the BC bounds. I'm pretty sure the error also will happen for other polygons as well. In the app I'm developing, polygons are randomly fabricated and the ArrayIndexOutOfBoundsException happened quite often. BitmapCreator is version 4.73

I have done following investigations:
-Substitutting BitmapCreator library with BitmapCreator.bas and BCPath.bas found in BitmapCreator Source.zip in this post.
-Logs shows the error occurs on line: 871 in BitmapCreator.bas
-In line 864 of BitmapCreator the CheckBounds is false, so it is no checking for bounds
-CheckBounds originates from the sub ShouldCheckBounds in BitmapCreator

I suggests to change the margins in BitmapCreator.ShouldCheckBounds from one pixel to two pixels like this:
Suggested change to the ShouldCheckBounds sub:
Private Sub ShouldCheckBounds(PointsX() As Float, PointsY() As Float) As Boolean
    For i = 0 To PointsY.Length - 1
        If PointsX(i) < 2 Or PointsX(i) >= mWidth - 2 Or PointsY(i) < 2 Or PointsY(i) >= mHeight - 2 Then
            Return True
        End If
    Next
    Return False
End Sub
 

Attachments

  • BitmapCreatorError.zip
    70.7 KB · Views: 10

Erel

B4X founder
Staff member
Licensed User
Longtime User
Fixed for the next update. Thanks for the accurate report.

The updated B4A and B4J libraries are attached (v4.74).

Note that non-integer coordinate points are rounded internally in BitmapCreator, so it is better to work with whole numbers.
 

Attachments

  • jBitmapCreator.zip
    45.7 KB · Views: 4
  • BitmapCreator.zip
    46.5 KB · Views: 4
Top