Skip to content

Commit

Permalink
Refactor BlockedModules
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane32 committed May 19, 2024
1 parent a95e5fa commit 419d5da
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 96 deletions.
11 changes: 10 additions & 1 deletion QRCoder/QRCodeData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ public QRCodeData(int version)
{
this.Version = version;
var size = ModulesPerSideFromVersion(version);
this.ModuleMatrix = new List<BitArray>();
this.ModuleMatrix = new List<BitArray>(size);
for (var i = 0; i < size; i++)
this.ModuleMatrix.Add(new BitArray(size));
}

public QRCodeData(int version, bool addPadding)
{
this.Version = version;
var size = ModulesPerSideFromVersion(version) + (addPadding ? 8 : 0);
this.ModuleMatrix = new List<BitArray>(size);
for (var i = 0; i < size; i++)
this.ModuleMatrix.Add(new BitArray(size));
}
Expand Down
74 changes: 74 additions & 0 deletions QRCoder/QRCodeGenerator.ModulePlacer.BlockedModules.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Collections.Generic;

namespace QRCoder
{
public partial class QRCodeGenerator
{
private static partial class ModulePlacer
{
/// <summary>
/// Struct that represents blocked modules using rectangles.
/// </summary>
public struct BlockedModules
{
private readonly List<Rectangle> _blockedModules;

/// <summary>
/// Initializes a new instance of the <see cref="BlockedModules"/> struct with a specified capacity.
/// </summary>
/// <param name="capacity">The initial capacity of the blocked modules list.</param>
public BlockedModules(int capacity)
{
_blockedModules = new List<Rectangle>(capacity);
}

/// <summary>
/// Adds a blocked module at the specified coordinates.
/// </summary>
/// <param name="x">The x-coordinate of the module.</param>
/// <param name="y">The y-coordinate of the module.</param>
public void Add(int x, int y)
{
_blockedModules.Add(new Rectangle(x, y, 1, 1));
}

/// <summary>
/// Adds a blocked module defined by the specified rectangle.
/// </summary>
/// <param name="rect">The rectangle that defines the blocked module.</param>
public void Add(Rectangle rect)
{
_blockedModules.Add(rect);
}

/// <summary>
/// Checks if the specified coordinates are blocked.
/// </summary>
/// <param name="x">The x-coordinate to check.</param>
/// <param name="y">The y-coordinate to check.</param>
/// <returns><c>true</c> if the coordinates are blocked; otherwise, <c>false</c>.</returns>
public bool IsBlocked(int x, int y)
{
return IsBlocked(new Rectangle(x, y, 1, 1));
}

/// <summary>
/// Checks if the specified rectangle is blocked.
/// </summary>
/// <param name="r1">The rectangle to check.</param>
/// <returns><c>true</c> if the rectangle is blocked; otherwise, <c>false</c>.</returns>
public bool IsBlocked(Rectangle r1)
{
// Iterate through the list of blocked modules to check for any intersection.
foreach (var r2 in _blockedModules)
{
// Check if any part of the rectangles overlap.
if (r2.X < r1.X + r1.Width && r1.X < r2.X + r2.Width && r2.Y < r1.Y + r1.Height && r1.Y < r2.Y + r2.Height)
return true;
}
return false;
}
}
}
}
}
140 changes: 53 additions & 87 deletions QRCoder/QRCodeGenerator.ModulePlacer.cs

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions QRCoder/QRCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,27 +319,26 @@ BitArray InterleaveData()
// Place the modules on the QR code matrix
QRCodeData PlaceModules()
{
var qr = new QRCodeData(version);
var blockedModules = new List<Rectangle>(17);
var qr = new QRCodeData(version, true);
var size = qr.ModuleMatrix.Count - 8;
var blockedModules = new ModulePlacer.BlockedModules(17);
ModulePlacer.PlaceFinderPatterns(qr, blockedModules);
ModulePlacer.ReserveSeperatorAreas(qr.ModuleMatrix.Count, blockedModules);
ModulePlacer.ReserveSeperatorAreas(size, blockedModules);
ModulePlacer.PlaceAlignmentPatterns(qr, alignmentPatternTable[version].PatternPositions, blockedModules);
ModulePlacer.PlaceTimingPatterns(qr, blockedModules);
ModulePlacer.PlaceDarkModule(qr, version, blockedModules);
ModulePlacer.ReserveVersionAreas(qr.ModuleMatrix.Count, version, blockedModules);
ModulePlacer.ReserveVersionAreas(size, version, blockedModules);
ModulePlacer.PlaceDataWords(qr, interleavedData, blockedModules);
var maskVersion = ModulePlacer.MaskCode(qr, version, blockedModules, eccLevel);
var formatStr = GetFormatString(eccLevel, maskVersion);

ModulePlacer.PlaceFormat(qr, formatStr);
ModulePlacer.PlaceFormat(qr, formatStr, true);
if (version >= 7)
{
var versionString = GetVersionString(version);
ModulePlacer.PlaceVersion(qr, versionString);
ModulePlacer.PlaceVersion(qr, versionString, true);
}

ModulePlacer.AddQuietZone(qr);

return qr;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,7 @@ namespace QRCoder
{
public QRCodeData(int version) { }
public QRCodeData(byte[] rawData, QRCoder.QRCodeData.Compression compressMode) { }
public QRCodeData(int version, bool addPadding) { }
public QRCodeData(string pathToRawData, QRCoder.QRCodeData.Compression compressMode) { }
public System.Collections.Generic.List<System.Collections.BitArray> ModuleMatrix { get; set; }
public int Version { get; }
Expand Down
1 change: 1 addition & 0 deletions QRCoderApiTests/net60-windows/QRCoder.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,7 @@ namespace QRCoder
{
public QRCodeData(int version) { }
public QRCodeData(byte[] rawData, QRCoder.QRCodeData.Compression compressMode) { }
public QRCodeData(int version, bool addPadding) { }
public QRCodeData(string pathToRawData, QRCoder.QRCodeData.Compression compressMode) { }
public System.Collections.Generic.List<System.Collections.BitArray> ModuleMatrix { get; set; }
public int Version { get; }
Expand Down
1 change: 1 addition & 0 deletions QRCoderApiTests/net60/QRCoder.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@ namespace QRCoder
{
public QRCodeData(int version) { }
public QRCodeData(byte[] rawData, QRCoder.QRCodeData.Compression compressMode) { }
public QRCodeData(int version, bool addPadding) { }
public QRCodeData(string pathToRawData, QRCoder.QRCodeData.Compression compressMode) { }
public System.Collections.Generic.List<System.Collections.BitArray> ModuleMatrix { get; set; }
public int Version { get; }
Expand Down
1 change: 1 addition & 0 deletions QRCoderApiTests/netstandard13/QRCoder.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,7 @@ namespace QRCoder
{
public QRCodeData(int version) { }
public QRCodeData(byte[] rawData, QRCoder.QRCodeData.Compression compressMode) { }
public QRCodeData(int version, bool addPadding) { }
public QRCodeData(string pathToRawData, QRCoder.QRCodeData.Compression compressMode) { }
public System.Collections.Generic.List<System.Collections.BitArray> ModuleMatrix { get; set; }
public int Version { get; }
Expand Down

0 comments on commit 419d5da

Please sign in to comment.