Skip to content

Commit 2e9d24a

Browse files
committed
Merge from master
2 parents 26a87e6 + 8259416 commit 2e9d24a

File tree

8 files changed

+203
-113
lines changed

8 files changed

+203
-113
lines changed

QRCoder/QRCodeData.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@ public QRCodeData(int version)
1717
{
1818
this.Version = version;
1919
var size = ModulesPerSideFromVersion(version);
20-
this.ModuleMatrix = new List<BitArray>();
20+
this.ModuleMatrix = new List<BitArray>(size);
21+
for (var i = 0; i < size; i++)
22+
this.ModuleMatrix.Add(new BitArray(size));
23+
}
24+
25+
public QRCodeData(int version, bool addPadding)
26+
{
27+
this.Version = version;
28+
var size = ModulesPerSideFromVersion(version) + (addPadding ? 8 : 0);
29+
this.ModuleMatrix = new List<BitArray>(size);
2130
for (var i = 0; i < size; i++)
2231
this.ModuleMatrix.Add(new BitArray(size));
2332
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using System;
2+
using System.Collections;
3+
using System.Threading;
4+
5+
namespace QRCoder
6+
{
7+
public partial class QRCodeGenerator
8+
{
9+
private static partial class ModulePlacer
10+
{
11+
/// <summary>
12+
/// Struct that represents blocked modules using rectangles.
13+
/// </summary>
14+
public struct BlockedModules : IDisposable
15+
{
16+
private readonly BitArray[] _blockedModules;
17+
18+
private static BitArray[] _staticBlockedModules;
19+
20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="BlockedModules"/> struct with a specified capacity.
22+
/// </summary>
23+
/// <param name="capacity">The initial capacity of the blocked modules list.</param>
24+
public BlockedModules(int size)
25+
{
26+
_blockedModules = Interlocked.Exchange(ref _staticBlockedModules, null);
27+
if (_blockedModules != null && _blockedModules.Length >= size)
28+
{
29+
for (int i = 0; i < size; i++)
30+
_blockedModules[i].SetAll(false);
31+
}
32+
else
33+
{
34+
_blockedModules = new BitArray[size];
35+
for (int i = 0; i < size; i++)
36+
_blockedModules[i] = new BitArray(size);
37+
}
38+
}
39+
40+
/// <summary>
41+
/// Adds a blocked module at the specified coordinates.
42+
/// </summary>
43+
/// <param name="x">The x-coordinate of the module.</param>
44+
/// <param name="y">The y-coordinate of the module.</param>
45+
public void Add(int x, int y)
46+
{
47+
_blockedModules[y][x] = true;
48+
}
49+
50+
/// <summary>
51+
/// Adds a blocked module defined by the specified rectangle.
52+
/// </summary>
53+
/// <param name="rect">The rectangle that defines the blocked module.</param>
54+
public void Add(Rectangle rect)
55+
{
56+
for (int y = rect.Y; y < rect.Y + rect.Height; y++)
57+
{
58+
for (int x = rect.X; x < rect.X + rect.Width; x++)
59+
{
60+
_blockedModules[y][x] = true;
61+
}
62+
}
63+
}
64+
65+
/// <summary>
66+
/// Checks if the specified coordinates are blocked.
67+
/// </summary>
68+
/// <param name="x">The x-coordinate to check.</param>
69+
/// <param name="y">The y-coordinate to check.</param>
70+
/// <returns><c>true</c> if the coordinates are blocked; otherwise, <c>false</c>.</returns>
71+
public bool IsBlocked(int x, int y)
72+
{
73+
return _blockedModules[y][x];
74+
}
75+
76+
/// <summary>
77+
/// Checks if the specified rectangle is blocked.
78+
/// </summary>
79+
/// <param name="r1">The rectangle to check.</param>
80+
/// <returns><c>true</c> if the rectangle is blocked; otherwise, <c>false</c>.</returns>
81+
public bool IsBlocked(Rectangle r1)
82+
{
83+
for (int y = r1.Y; y < r1.Y + r1.Height; y++)
84+
{
85+
for (int x = r1.X; x < r1.X + r1.Width; x++)
86+
{
87+
if (_blockedModules[y][x])
88+
return true;
89+
}
90+
}
91+
return false;
92+
}
93+
94+
public void Dispose()
95+
{
96+
Interlocked.CompareExchange(ref _staticBlockedModules, _blockedModules, null);
97+
}
98+
}
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)