tidy
This commit is contained in:
@@ -2,59 +2,59 @@ namespace Gma.QrCodeNet.Encoding.EncodingRegion;
|
||||
|
||||
internal static class BCHCalculator
|
||||
{
|
||||
/// <summary>
|
||||
/// Calculate int length by search for Most significant bit
|
||||
/// </summary>
|
||||
/// <param name="num">Input Number</param>
|
||||
/// <returns>Most significant bit</returns>
|
||||
internal static int PosMSB(int num) => num == 0 ? 0 : BinarySearchPos(num, 0, 32) + 1;
|
||||
/// <summary>
|
||||
/// Calculate int length by search for Most significant bit
|
||||
/// </summary>
|
||||
/// <param name="num">Input Number</param>
|
||||
/// <returns>Most significant bit</returns>
|
||||
internal static int PosMSB(int num) => num == 0 ? 0 : BinarySearchPos(num, 0, 32) + 1;
|
||||
|
||||
/// <summary>
|
||||
/// Search for right side bit of Most significant bit
|
||||
/// </summary>
|
||||
/// <param name="num">Input number</param>
|
||||
/// <param name="lowBoundary">Lower boundary. At start should be 0</param>
|
||||
/// <param name="highBoundary">Higher boundary. At start should be 32</param>
|
||||
/// <returns>Most significant bit - 1</returns>
|
||||
private static int BinarySearchPos(int num, int lowBoundary, int highBoundary)
|
||||
{
|
||||
int mid = (lowBoundary + highBoundary) / 2;
|
||||
int shiftResult = num >> mid;
|
||||
if (shiftResult == 1)
|
||||
{
|
||||
return mid;
|
||||
}
|
||||
else if (shiftResult < 1)
|
||||
{
|
||||
return BinarySearchPos(num, lowBoundary, mid);
|
||||
}
|
||||
else
|
||||
{
|
||||
return BinarySearchPos(num, mid, highBoundary);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Search for right side bit of Most significant bit
|
||||
/// </summary>
|
||||
/// <param name="num">Input number</param>
|
||||
/// <param name="lowBoundary">Lower boundary. At start should be 0</param>
|
||||
/// <param name="highBoundary">Higher boundary. At start should be 32</param>
|
||||
/// <returns>Most significant bit - 1</returns>
|
||||
private static int BinarySearchPos(int num, int lowBoundary, int highBoundary)
|
||||
{
|
||||
int mid = (lowBoundary + highBoundary) / 2;
|
||||
int shiftResult = num >> mid;
|
||||
if (shiftResult == 1)
|
||||
{
|
||||
return mid;
|
||||
}
|
||||
else if (shiftResult < 1)
|
||||
{
|
||||
return BinarySearchPos(num, lowBoundary, mid);
|
||||
}
|
||||
else
|
||||
{
|
||||
return BinarySearchPos(num, mid, highBoundary);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// With input number and polynomial number. Method will calculate BCH value and return
|
||||
/// </summary>
|
||||
/// <param name="num">Input number</param>
|
||||
/// <param name="poly">Polynomial number</param>
|
||||
/// <returns>BCH value</returns>
|
||||
internal static int CalculateBCH(int num, int poly)
|
||||
{
|
||||
int polyMSB = PosMSB(poly);
|
||||
/// <summary>
|
||||
/// With input number and polynomial number. Method will calculate BCH value and return
|
||||
/// </summary>
|
||||
/// <param name="num">Input number</param>
|
||||
/// <param name="poly">Polynomial number</param>
|
||||
/// <returns>BCH value</returns>
|
||||
internal static int CalculateBCH(int num, int poly)
|
||||
{
|
||||
int polyMSB = PosMSB(poly);
|
||||
|
||||
// num's length will be old length + new length - 1.
|
||||
// Once divide poly number. BCH number will be one length short than Poly number's length.
|
||||
num <<= (polyMSB - 1);
|
||||
int numMSB = PosMSB(num);
|
||||
while (PosMSB(num) >= polyMSB)
|
||||
{
|
||||
// left shift Poly number to same level as num. Then xor.
|
||||
// Remove most significant bits of num.
|
||||
num ^= poly << (numMSB - polyMSB);
|
||||
numMSB = PosMSB(num);
|
||||
}
|
||||
return num;
|
||||
}
|
||||
}
|
||||
// num's length will be old length + new length - 1.
|
||||
// Once divide poly number. BCH number will be one length short than Poly number's length.
|
||||
num <<= (polyMSB - 1);
|
||||
int numMSB = PosMSB(num);
|
||||
while (PosMSB(num) >= polyMSB)
|
||||
{
|
||||
// left shift Poly number to same level as num. Then xor.
|
||||
// Remove most significant bits of num.
|
||||
num ^= poly << (numMSB - polyMSB);
|
||||
numMSB = PosMSB(num);
|
||||
}
|
||||
return num;
|
||||
}
|
||||
}
|
||||
@@ -1,72 +1,70 @@
|
||||
using System;
|
||||
|
||||
namespace Gma.QrCodeNet.Encoding.EncodingRegion;
|
||||
|
||||
/// <remarks>ISO/IEC 18004:2000 Chapter 8.7.3 Page 46</remarks>
|
||||
internal static class Codeword
|
||||
{
|
||||
internal static void TryEmbedCodewords(this TriStateMatrix tsMatrix, BitList codewords)
|
||||
{
|
||||
int sWidth = tsMatrix.Width;
|
||||
int codewordsSize = codewords.Count;
|
||||
internal static void TryEmbedCodewords(this TriStateMatrix tsMatrix, BitList codewords)
|
||||
{
|
||||
int sWidth = tsMatrix.Width;
|
||||
int codewordsSize = codewords.Count;
|
||||
|
||||
int bitIndex = 0;
|
||||
int directionUp = -1;
|
||||
int bitIndex = 0;
|
||||
int directionUp = -1;
|
||||
|
||||
int x = sWidth - 1;
|
||||
int y = sWidth - 1;
|
||||
int x = sWidth - 1;
|
||||
int y = sWidth - 1;
|
||||
|
||||
while (x > 0)
|
||||
{
|
||||
// Skip vertical timing pattern
|
||||
if (x == 6)
|
||||
{
|
||||
x -= 1;
|
||||
}
|
||||
while (x > 0)
|
||||
{
|
||||
// Skip vertical timing pattern
|
||||
if (x == 6)
|
||||
{
|
||||
x -= 1;
|
||||
}
|
||||
|
||||
while (y >= 0 && y < sWidth)
|
||||
{
|
||||
for (int xOffset = 0; xOffset < 2; xOffset++)
|
||||
{
|
||||
int xPos = x - xOffset;
|
||||
if (tsMatrix.MStatus(xPos, y) == MatrixStatus.None)
|
||||
{
|
||||
bool bit;
|
||||
if (bitIndex < codewordsSize)
|
||||
{
|
||||
bit = codewords[bitIndex];
|
||||
bitIndex++;
|
||||
}
|
||||
else
|
||||
{
|
||||
bit = false;
|
||||
}
|
||||
while (y >= 0 && y < sWidth)
|
||||
{
|
||||
for (int xOffset = 0; xOffset < 2; xOffset++)
|
||||
{
|
||||
int xPos = x - xOffset;
|
||||
if (tsMatrix.MStatus(xPos, y) == MatrixStatus.None)
|
||||
{
|
||||
bool bit;
|
||||
if (bitIndex < codewordsSize)
|
||||
{
|
||||
bit = codewords[bitIndex];
|
||||
bitIndex++;
|
||||
}
|
||||
else
|
||||
{
|
||||
bit = false;
|
||||
}
|
||||
|
||||
tsMatrix[xPos, y, MatrixStatus.Data] = bit;
|
||||
}
|
||||
}
|
||||
tsMatrix[xPos, y, MatrixStatus.Data] = bit;
|
||||
}
|
||||
}
|
||||
|
||||
y = NextY(y, directionUp);
|
||||
}
|
||||
y = NextY(y, directionUp);
|
||||
}
|
||||
|
||||
directionUp = ChangeDirection(directionUp);
|
||||
y = NextY(y, directionUp);
|
||||
x -= 2;
|
||||
}
|
||||
directionUp = ChangeDirection(directionUp);
|
||||
y = NextY(y, directionUp);
|
||||
x -= 2;
|
||||
}
|
||||
|
||||
if (bitIndex != codewordsSize)
|
||||
{
|
||||
throw new Exception($"Not all bits from {nameof(codewords)} consumed by matrix: {bitIndex} / {codewordsSize}.");
|
||||
}
|
||||
}
|
||||
if (bitIndex != codewordsSize)
|
||||
{
|
||||
throw new Exception($"Not all bits from {nameof(codewords)} consumed by matrix: {bitIndex} / {codewordsSize}.");
|
||||
}
|
||||
}
|
||||
|
||||
internal static int NextY(int y, int directionUp)
|
||||
{
|
||||
return y + directionUp;
|
||||
}
|
||||
internal static int NextY(int y, int directionUp)
|
||||
{
|
||||
return y + directionUp;
|
||||
}
|
||||
|
||||
internal static int ChangeDirection(int directionUp)
|
||||
{
|
||||
return -directionUp;
|
||||
}
|
||||
}
|
||||
internal static int ChangeDirection(int directionUp)
|
||||
{
|
||||
return -directionUp;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using Gma.QrCodeNet.Encoding.Masking;
|
||||
|
||||
namespace Gma.QrCodeNet.Encoding.EncodingRegion;
|
||||
@@ -10,105 +9,105 @@ namespace Gma.QrCodeNet.Encoding.EncodingRegion;
|
||||
/// <remarks>ISO/IEC 18004:2000 Chapter 8.9 Page 53</remarks>
|
||||
internal static class FormatInformation
|
||||
{
|
||||
/// <summary>
|
||||
/// From Appendix C in JISX0510:2004 (p.65).
|
||||
/// </summary>
|
||||
private const int FormatInfoPoly = 0x537;
|
||||
/// <summary>
|
||||
/// From Appendix C in JISX0510:2004 (p.65).
|
||||
/// </summary>
|
||||
private const int FormatInfoPoly = 0x537;
|
||||
|
||||
/// <summary>
|
||||
/// From Appendix C in JISX0510:2004 (p.65).
|
||||
/// </summary>
|
||||
private const int FormatInfoMaskPattern = 0x5412;
|
||||
/// <summary>
|
||||
/// From Appendix C in JISX0510:2004 (p.65).
|
||||
/// </summary>
|
||||
private const int FormatInfoMaskPattern = 0x5412;
|
||||
|
||||
/// <summary>
|
||||
/// Embed format information to tristatematrix.
|
||||
/// Process combination of create info bits, BCH error correction bits calculation, embed towards matrix.
|
||||
/// </summary>
|
||||
/// <remarks>ISO/IEC 18004:2000 Chapter 8.9 Page 53</remarks>
|
||||
internal static void EmbedFormatInformation(this TriStateMatrix triMatrix, ErrorCorrectionLevel errorLevel, Pattern pattern)
|
||||
{
|
||||
BitList formatInfo = GetFormatInfoBits(errorLevel, pattern);
|
||||
int width = triMatrix.Width;
|
||||
for (int index = 0; index < 15; index++)
|
||||
{
|
||||
MatrixPoint point = PointForInfo1(index);
|
||||
bool bit = formatInfo[index];
|
||||
triMatrix[point.X, point.Y, MatrixStatus.NoMask] = bit;
|
||||
/// <summary>
|
||||
/// Embed format information to tristatematrix.
|
||||
/// Process combination of create info bits, BCH error correction bits calculation, embed towards matrix.
|
||||
/// </summary>
|
||||
/// <remarks>ISO/IEC 18004:2000 Chapter 8.9 Page 53</remarks>
|
||||
internal static void EmbedFormatInformation(this TriStateMatrix triMatrix, ErrorCorrectionLevel errorLevel, Pattern pattern)
|
||||
{
|
||||
BitList formatInfo = GetFormatInfoBits(errorLevel, pattern);
|
||||
int width = triMatrix.Width;
|
||||
for (int index = 0; index < 15; index++)
|
||||
{
|
||||
MatrixPoint point = PointForInfo1(index);
|
||||
bool bit = formatInfo[index];
|
||||
triMatrix[point.X, point.Y, MatrixStatus.NoMask] = bit;
|
||||
|
||||
if (index < 7)
|
||||
{
|
||||
triMatrix[8, width - 1 - index, MatrixStatus.NoMask] = bit;
|
||||
}
|
||||
else
|
||||
{
|
||||
triMatrix[width - 8 + (index - 7), 8, MatrixStatus.NoMask] = bit;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (index < 7)
|
||||
{
|
||||
triMatrix[8, width - 1 - index, MatrixStatus.NoMask] = bit;
|
||||
}
|
||||
else
|
||||
{
|
||||
triMatrix[width - 8 + (index - 7), 8, MatrixStatus.NoMask] = bit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static MatrixPoint PointForInfo1(int bitsIndex)
|
||||
{
|
||||
if (bitsIndex <= 7)
|
||||
{
|
||||
return bitsIndex >= 6
|
||||
? new MatrixPoint(bitsIndex + 1, 8)
|
||||
: new MatrixPoint(bitsIndex, 8);
|
||||
}
|
||||
else
|
||||
{
|
||||
return bitsIndex == 8
|
||||
? new MatrixPoint(8, 8 - (bitsIndex - 7))
|
||||
: new MatrixPoint(8, 8 - (bitsIndex - 7) - 1);
|
||||
}
|
||||
}
|
||||
private static MatrixPoint PointForInfo1(int bitsIndex)
|
||||
{
|
||||
if (bitsIndex <= 7)
|
||||
{
|
||||
return bitsIndex >= 6
|
||||
? new MatrixPoint(bitsIndex + 1, 8)
|
||||
: new MatrixPoint(bitsIndex, 8);
|
||||
}
|
||||
else
|
||||
{
|
||||
return bitsIndex == 8
|
||||
? new MatrixPoint(8, 8 - (bitsIndex - 7))
|
||||
: new MatrixPoint(8, 8 - (bitsIndex - 7) - 1);
|
||||
}
|
||||
}
|
||||
|
||||
private static BitList GetFormatInfoBits(ErrorCorrectionLevel errorLevel, Pattern pattern)
|
||||
{
|
||||
int formatInfo = (int)pattern.MaskPatternType;
|
||||
private static BitList GetFormatInfoBits(ErrorCorrectionLevel errorLevel, Pattern pattern)
|
||||
{
|
||||
int formatInfo = (int)pattern.MaskPatternType;
|
||||
|
||||
// Pattern bits length = 3
|
||||
formatInfo |= GetErrorCorrectionIndicatorBits(errorLevel) << 3;
|
||||
// Pattern bits length = 3
|
||||
formatInfo |= GetErrorCorrectionIndicatorBits(errorLevel) << 3;
|
||||
|
||||
int bchCode = BCHCalculator.CalculateBCH(formatInfo, FormatInfoPoly);
|
||||
int bchCode = BCHCalculator.CalculateBCH(formatInfo, FormatInfoPoly);
|
||||
|
||||
// bchCode length = 10
|
||||
formatInfo = (formatInfo << 10) | bchCode;
|
||||
// bchCode length = 10
|
||||
formatInfo = (formatInfo << 10) | bchCode;
|
||||
|
||||
// xor maskPattern
|
||||
formatInfo ^= FormatInfoMaskPattern;
|
||||
// xor maskPattern
|
||||
formatInfo ^= FormatInfoMaskPattern;
|
||||
|
||||
BitList resultBits = new()
|
||||
{
|
||||
{ formatInfo, 15 }
|
||||
};
|
||||
BitList resultBits = new()
|
||||
{
|
||||
{ formatInfo, 15 }
|
||||
};
|
||||
|
||||
if (resultBits.Count != 15)
|
||||
{
|
||||
throw new Exception("FormatInfoBits length is not 15");
|
||||
}
|
||||
else
|
||||
{
|
||||
return resultBits;
|
||||
}
|
||||
}
|
||||
if (resultBits.Count != 15)
|
||||
{
|
||||
throw new Exception("FormatInfoBits length is not 15");
|
||||
}
|
||||
else
|
||||
{
|
||||
return resultBits;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// According Table 25 — Error correction level indicators
|
||||
/// Using these bits as enum values would destroy their order which currently corresponds to error correction strength.
|
||||
/// </summary>
|
||||
internal static int GetErrorCorrectionIndicatorBits(ErrorCorrectionLevel errorLevel)
|
||||
{
|
||||
// L 01
|
||||
// M 00
|
||||
// Q 11
|
||||
// H 10
|
||||
return errorLevel switch
|
||||
{
|
||||
ErrorCorrectionLevel.H => 0x02,
|
||||
ErrorCorrectionLevel.L => 0x01,
|
||||
ErrorCorrectionLevel.M => 0x00,
|
||||
ErrorCorrectionLevel.Q => 0x03,
|
||||
_ => throw new ArgumentException($"Unsupported error correction level [{errorLevel}]", nameof(errorLevel))
|
||||
};
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// According Table 25 — Error correction level indicators
|
||||
/// Using these bits as enum values would destroy their order which currently corresponds to error correction strength.
|
||||
/// </summary>
|
||||
internal static int GetErrorCorrectionIndicatorBits(ErrorCorrectionLevel errorLevel)
|
||||
{
|
||||
// L 01
|
||||
// M 00
|
||||
// Q 11
|
||||
// H 10
|
||||
return errorLevel switch
|
||||
{
|
||||
ErrorCorrectionLevel.H => 0x02,
|
||||
ErrorCorrectionLevel.L => 0x01,
|
||||
ErrorCorrectionLevel.M => 0x00,
|
||||
ErrorCorrectionLevel.Q => 0x03,
|
||||
_ => throw new ArgumentException($"Unsupported error correction level [{errorLevel}]", nameof(errorLevel))
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
using System;
|
||||
|
||||
namespace Gma.QrCodeNet.Encoding.EncodingRegion;
|
||||
|
||||
/// <summary>
|
||||
@@ -8,63 +6,63 @@ namespace Gma.QrCodeNet.Encoding.EncodingRegion;
|
||||
/// <remarks>ISO/IEC 18004:2000 Chapter 8.10 Page 54</remarks>
|
||||
internal static class VersionInformation
|
||||
{
|
||||
private const int VIRectangleHeight = 3;
|
||||
private const int VIRectangleWidth = 6;
|
||||
private const int VIRectangleHeight = 3;
|
||||
private const int VIRectangleWidth = 6;
|
||||
|
||||
private const int LengthDataBits = 6;
|
||||
private const int LengthECBits = 12;
|
||||
private const int VersionBCHPoly = 0x1f25;
|
||||
private const int LengthDataBits = 6;
|
||||
private const int LengthECBits = 12;
|
||||
private const int VersionBCHPoly = 0x1f25;
|
||||
|
||||
/// <summary>
|
||||
/// Embed version information to Matrix
|
||||
/// Only for version greater than or equal to 7
|
||||
/// </summary>
|
||||
internal static void EmbedVersionInformation(this TriStateMatrix tsMatrix, int version)
|
||||
{
|
||||
if (version < 7)
|
||||
{
|
||||
return;
|
||||
}
|
||||
/// <summary>
|
||||
/// Embed version information to Matrix
|
||||
/// Only for version greater than or equal to 7
|
||||
/// </summary>
|
||||
internal static void EmbedVersionInformation(this TriStateMatrix tsMatrix, int version)
|
||||
{
|
||||
if (version < 7)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
BitList versionInfo = VersionInfoBitList(version);
|
||||
BitList versionInfo = VersionInfoBitList(version);
|
||||
|
||||
int matrixWidth = tsMatrix.Width;
|
||||
int matrixWidth = tsMatrix.Width;
|
||||
|
||||
// 1 cell between version info and position stencil
|
||||
int shiftLength = QRCodeConstantVariable.PositionStencilWidth + VIRectangleHeight + 1;
|
||||
// 1 cell between version info and position stencil
|
||||
int shiftLength = QRCodeConstantVariable.PositionStencilWidth + VIRectangleHeight + 1;
|
||||
|
||||
// Reverse order input
|
||||
int viIndex = LengthDataBits + LengthECBits - 1;
|
||||
// Reverse order input
|
||||
int viIndex = LengthDataBits + LengthECBits - 1;
|
||||
|
||||
for (int viWidth = 0; viWidth < VIRectangleWidth; viWidth++)
|
||||
{
|
||||
for (int viHeight = 0; viHeight < VIRectangleHeight; viHeight++)
|
||||
{
|
||||
bool bit = versionInfo[viIndex];
|
||||
viIndex--;
|
||||
for (int viWidth = 0; viWidth < VIRectangleWidth; viWidth++)
|
||||
{
|
||||
for (int viHeight = 0; viHeight < VIRectangleHeight; viHeight++)
|
||||
{
|
||||
bool bit = versionInfo[viIndex];
|
||||
viIndex--;
|
||||
|
||||
// Bottom left
|
||||
tsMatrix[viWidth, (matrixWidth - shiftLength + viHeight), MatrixStatus.NoMask] = bit;
|
||||
// Bottom left
|
||||
tsMatrix[viWidth, (matrixWidth - shiftLength + viHeight), MatrixStatus.NoMask] = bit;
|
||||
|
||||
// Top right
|
||||
tsMatrix[(matrixWidth - shiftLength + viHeight), viWidth, MatrixStatus.NoMask] = bit;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Top right
|
||||
tsMatrix[(matrixWidth - shiftLength + viHeight), viWidth, MatrixStatus.NoMask] = bit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static BitList VersionInfoBitList(int version)
|
||||
{
|
||||
BitList result = new()
|
||||
{
|
||||
{ version, LengthDataBits },
|
||||
{ BCHCalculator.CalculateBCH(version, VersionBCHPoly), LengthECBits }
|
||||
};
|
||||
private static BitList VersionInfoBitList(int version)
|
||||
{
|
||||
BitList result = new()
|
||||
{
|
||||
{ version, LengthDataBits },
|
||||
{ BCHCalculator.CalculateBCH(version, VersionBCHPoly), LengthECBits }
|
||||
};
|
||||
|
||||
if (result.Count != (LengthECBits + LengthDataBits))
|
||||
{
|
||||
throw new Exception("Version Info creation error. Result is not 18 bits");
|
||||
}
|
||||
if (result.Count != (LengthECBits + LengthDataBits))
|
||||
{
|
||||
throw new Exception("Version Info creation error. Result is not 18 bits");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user