Skip to content

Avoid temporary color arrays in PDF and bitmap renderers - #713

Merged
Shane32 merged 2 commits into
masterfrom
codex/span-color-parsing
Nov 2, 2026
Merged

Shane32 merged 2 commits into
masterfrom
codex/span-color-parsing

Conversation

@Shane32

@Shane32 Shane32 commented Nov 2, 2026 •

Copy link
Copy Markdown
Owner

Summary

PDF and bitmap rendering allocate temporary byte arrays when parsing hex colors. Parse directly into stack-allocated RGB buffers on HAS_SPAN targets, and use a private span-based bitmap rendering core so default black-and-white rendering also avoids these color arrays.

Compile HexColorToByteArray only under !HAS_SPAN. Preserve bitmap parsing and validation of extra color bytes, PDF's exact RGB length requirement, and the existing public API.

Test plan

  • Built all seven library target frameworks with dotnet build QRCoder/QRCoder.csproj --no-restore.
  • Passed 37 selected PDF, bitmap, and string-helper tests on each of net462, netcoreapp2.1, netcoreapp3.1, net5.0-windows, and net6.0-windows (185 passing cases).
  • Added byte-for-byte output comparisons and parsing cases for optional prefixes, mixed case, odd trailing characters, extra bitmap color bytes, and invalid colors.
  • git diff --check passed.

Summary by CodeRabbit

  • Bug Fixes
    • Bitmap and PDF QR code rendering now handles hexadecimal and RGB color inputs consistently, including default colors and hexadecimal values with optional # prefixes.
    • Invalid color formats and incorrect RGB lengths are rejected with appropriate errors. Bitmap rendering also accepts extra trailing hexadecimal color bytes.
  • Tests
    • Added coverage for color equivalence, default colors, accepted input variations, and invalid color values across bitmap and PDF rendering.

@coderabbitai

coderabbitai Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5bf6df24-8424-4432-acae-9f3a5ce765e4
Commits

Reviewing files that changed from the base of the PR and between 1707903 and 6e4e32f.

Files selected for processing: 2
  • QRCoder/BitmapByteQRCode.cs
  • QRCoder/PdfByteQRCode.cs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Walkthrough

The bitmap and PDF QR renderers use stack-allocated RGB buffers and buffer-based hex parsing when span support is available. Tests cover color equivalence, default bitmap colors, and invalid color inputs.

Changes: Renderer color parsing

Layer / File(s) Summary
Hex color parsing
QRCoder/Extensions/StringExtensions.cs
Span-enabled builds parse hex colors into a supplied buffer and validate the input byte count. Other builds retain the allocating parser.
Bitmap renderer integration
QRCoder/BitmapByteQRCode.cs, QRCoderTests/BitmapByteQRCodeRendererTests.cs
The bitmap renderer routes color overloads through a shared core method. Span-enabled builds parse colors into stack-allocated buffers. Tests compare equivalent colors, verify default colors, and check invalid input.
PDF renderer integration
QRCoder/PdfByteQRCode.cs, QRCoderTests/PdfByteQRCodeRendererTests.cs
Span-enabled builds parse PDF colors into stack-allocated buffers. Other builds retain array-based parsing and length validation. Tests cover equivalent colors, incorrect lengths, and invalid digits.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Possibly related PRs

  • Shane32/QRCoder#712: Updates PdfByteQRCode.GetGraphic allocation behavior and RGB color validation, which overlap with the PDF renderer changes here.

Suggested reviewers: krisvandermotten, gfoidl

Merge Risk: Minimal · up to 6e4e3

No actionable merge-blocking risk was established for this change.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: removing temporary color arrays from the PDF and bitmap renderers.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 4 functions across 5 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@Shane32
Shane32 requested a review from gfoidl November 2, 2026 01:35
Comment thread QRCoder/BitmapByteQRCode.cs Outdated
Comment on lines +61 to +62
Span<byte> darkColor = stackalloc byte[3];
Span<byte> lightColor = stackalloc byte[3];

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Span<byte> darkColor = stackalloc byte[3];
Span<byte> lightColor = stackalloc byte[3];
Span<byte> scratch = stackalloc byte[8];
Span<byte> darkColor = sratch.Slice(0, 3);
Span<byte> lightColor = scratch.Slice(3, 3);

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Comment thread QRCoder/PdfByteQRCode.cs Outdated
Comment on lines +52 to +53
Span<byte> darkColor = stackalloc byte[3];
Span<byte> lightColor = stackalloc byte[3];

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Comment on lines +70 to +71
var offset = colorString.StartsWith('#') ? 1 : 0;
var byteCount = (colorString.Length - offset) / 2;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var and typed variables are used quite inconsistent in the code.
Can we use the strong typed declaration? It's easier to read w/o IDE then.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do a follow-up PR to clean this up across the codebase

@Shane32
Shane32 requested a review from gfoidl November 2, 2026 13:21
@Shane32
Shane32 merged commit bd9d1ae into master Nov 2, 2026
8 checks passed
@Shane32
Shane32 deleted the codex/span-color-parsing branch November 2, 2026 14:20
This was referenced Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants