Skip to content

Bug in BMP padding calculation when pixelsPerModule is odd #697

Closed

Description

@KrisVandermotten

BitmapByteQRCode.GetGraphic uses the following formula to calculate the number of padding bytes per row:

var paddingLen = sideLength % 4;

That's not correct. It happens to work when sideLength is even, i.e. when pixelsPerModule is even, but it fails when sideLength is odd.

The correct formula would be (4 - (sideLength % 4)) % 4, or the more efficient equivalent -sideLength & 3.

Activity

  1. KrisVandermotten commented on Jul 12, 2026

    @KrisVandermotten
    ContributorAuthor

    Hmmm, it looks like the code may have been correct after all, albeit in a non-obvious way.

    Indeed, sideLength is the number of pixels, not the number of bytes in a pixel row. That needs to be multiplied by 3 to obtain the number of bytes. That multiplication by 3 saves the day.

    sideLength % 4 (sideLength % 4) * 3 ((sideLength % 4) * 3) % 4
    0 0 0
    1 3 3
    2 6 2
    3 9 1

    Sorry about that.

  2. Shane32 commented on Jul 24, 2026

    @Shane32
    Owner

    No problem and thanks for looking into it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions