Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane32 committed May 7, 2024
1 parent eb7a0ad commit 8a177f0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 42 deletions.
32 changes: 1 addition & 31 deletions QRCoder/Extensions/BitArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,13 @@ internal static class BitArrayExtensions
/// <param name="destinationOffset">The zero-based index in the destination <see cref="BitArray"/> at which storing begins.</param>
/// <param name="count">The number of elements to copy.</param>
/// <returns>The index in the destination <see cref="BitArray"/> immediately following the last copied element.</returns>
public static int CopyTo(this BitArray source, int sourceOffset, BitArray destination, int destinationOffset, int count)
public static int CopyTo(this BitArray source, BitArray destination, int sourceOffset, int destinationOffset, int count)
{
for (int i = 0; i < count; i++)
{
destination[destinationOffset + i] = source[sourceOffset + i];
}
return destinationOffset + count;
}

/// <summary>
/// Copies a specified number of elements from one <see cref="BitArray"/> to another starting at the specified offsets,
/// and reverses every set of 8 bits during the copy operation.
/// </summary>
/// <param name="source">The source <see cref="BitArray"/> from which elements will be copied.</param>
/// <param name="sourceOffset">The zero-based index in the source <see cref="BitArray"/> at which copying begins.</param>
/// <param name="destination">The destination <see cref="BitArray"/> to which elements will be copied.</param>
/// <param name="destinationOffset">The zero-based index in the destination <see cref="BitArray"/> at which storing begins.</param>
/// <param name="count">The number of elements to copy. Must be a multiple of 8.</param>
/// <returns>The index in the destination <see cref="BitArray"/> immediately following the last copied element.</returns>
public static int CopyToRev8(this BitArray source, int sourceOffset, BitArray destination, int destinationOffset, int count)
{
if (count % 8 != 0)
{
throw new ArgumentException("Count must be a multiple of 8.", nameof(count));
}

for (int i = 0; i < count; i += 8)
{
// Reverse the current set of 8 bits
for (int j = 0; j < 8; j++)
{
destination[destinationOffset + i + j] = source[sourceOffset + i + (7 - j)];
}
}

return destinationOffset + count;
}

}
}
17 changes: 6 additions & 11 deletions QRCoder/QRCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ private static QRCodeData GenerateQrCode(BitArray bitArray, ECCLevel eccLevel, i

//Calculate error correction words
var codeWordWithECC = new List<CodewordBlock>(eccInfo.BlocksInGroup1 + eccInfo.BlocksInGroup2);
AddCodeWordBlocks(1, eccInfo.BlocksInGroup1, eccInfo.CodewordsInGroup1, bitArray, 0, bitArray.Length);
AddCodeWordBlocks(1, eccInfo.BlocksInGroup1, eccInfo.CodewordsInGroup1, 0, bitArray.Length);
int offset = eccInfo.BlocksInGroup1 * eccInfo.CodewordsInGroup1 * 8;
AddCodeWordBlocks(2, eccInfo.BlocksInGroup2, eccInfo.CodewordsInGroup2, bitArray, offset, bitArray.Length - offset);
AddCodeWordBlocks(2, eccInfo.BlocksInGroup2, eccInfo.CodewordsInGroup2, offset, bitArray.Length - offset);


//Calculate interleaved code word lengths
Expand All @@ -253,7 +253,7 @@ private static QRCodeData GenerateQrCode(BitArray bitArray, ECCLevel eccLevel, i
foreach (var codeBlock in codeWordWithECC)
{
if (codeBlock.CodeWordsLength / 8 > i)
pos = bitArray.CopyTo(i * 8 + codeBlock.CodeWordsOffset, interleavedData, pos, 8);
pos = bitArray.CopyTo(interleavedData, i * 8 + codeBlock.CodeWordsOffset, pos, 8);
}
}
for (var i = 0; i < eccInfo.ECCPerBlock; i++)
Expand Down Expand Up @@ -287,18 +287,13 @@ private static QRCodeData GenerateQrCode(BitArray bitArray, ECCLevel eccLevel, i
ModulePlacer.AddQuietZone(qr);
return qr;

void AddCodeWordBlocks(int blockNum, int blocksInGroup, int codewordsInGroup, BitArray bitArray2, int offset2, int count)
void AddCodeWordBlocks(int blockNum, int blocksInGroup, int codewordsInGroup, int offset2, int count)
{
var groupLength = codewordsInGroup * 8;
for (var i = 0; i < blocksInGroup; i++)
{
var eccWordList = CalculateECCWords(bitArray2, offset2, groupLength, eccInfo);
codeWordWithECC.Add(new CodewordBlock(
//bitArray2,
offset2,
groupLength,
eccWordList)
);
var eccWordList = CalculateECCWords(bitArray, offset2, groupLength, eccInfo);
codeWordWithECC.Add(new CodewordBlock(offset2, groupLength, eccWordList));
offset2 += groupLength;
}
}
Expand Down

0 comments on commit 8a177f0

Please sign in to comment.