|
| 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