mirror of
https://github.com/kwsch/PKHeX
synced 2024-11-26 14:00:21 +00:00
Misc cleanup
This commit is contained in:
parent
1e932e14e9
commit
b97e967304
3 changed files with 183 additions and 227 deletions
|
@ -102,14 +102,13 @@ namespace PKHeX.Core
|
|||
{
|
||||
var moves = MoveEgg.GetEggMoves(pk, Species, pk.AltForm, Version);
|
||||
if (moves.Length == 0)
|
||||
moves = MoveLevelUp.GetEncounterMoves(pk, Level, Version);
|
||||
else if (moves.Length < 4 && pk.Format >= 6)
|
||||
{
|
||||
// Sprinkle in some default level up moves
|
||||
var lvl = Legal.GetBaseEggMoves(pk, Species, Version, Level);
|
||||
moves = lvl.Concat(moves).ToArray();
|
||||
}
|
||||
return moves;
|
||||
return MoveLevelUp.GetEncounterMoves(pk, Level, Version);
|
||||
if (moves.Length >= 4 || pk.Format < 6)
|
||||
return moves;
|
||||
|
||||
// Sprinkle in some default level up moves
|
||||
var lvl = Legal.GetBaseEggMoves(pk, Species, Version, Level);
|
||||
return lvl.Concat(moves).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -541,9 +541,9 @@ namespace PKHeX.Core
|
|||
public static uint GetWurmpleEC(int evoVal)
|
||||
{
|
||||
uint EC;
|
||||
while (true)
|
||||
if (evoVal == GetWurmpleEvoVal(EC = Util.Rand32()))
|
||||
return EC;
|
||||
do EC = Util.Rand32();
|
||||
while (evoVal != GetWurmpleEvoVal(EC));
|
||||
return EC;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -21,37 +21,35 @@ namespace QRCoder
|
|||
private List<Antilog> galoisField;
|
||||
private Dictionary<char, int> alphanumEncDict;
|
||||
|
||||
|
||||
public QRCodeGenerator()
|
||||
{
|
||||
this.CreateAntilogTable();
|
||||
this.CreateAlphanumEncDict();
|
||||
this.CreateCapacityTable();
|
||||
this.CreateCapacityECCTable();
|
||||
this.CreateAlignmentPatternTable();
|
||||
CreateAntilogTable();
|
||||
CreateAlphanumEncDict();
|
||||
CreateCapacityTable();
|
||||
CreateCapacityECCTable();
|
||||
CreateAlignmentPatternTable();
|
||||
}
|
||||
|
||||
public QRCodeData CreateQRCode(byte[] data)
|
||||
{
|
||||
return CreateQRCode(new string(data.Select(c => (char) c).ToArray()), ECCLevel.Q);
|
||||
return CreateQRCode(string.Concat(data.Select(c => (char) c)), ECCLevel.Q);
|
||||
}
|
||||
|
||||
public QRCodeData CreateQRCode(string plainText, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false)
|
||||
{
|
||||
var encoding = this.GetEncodingFromPlaintext(plainText);
|
||||
var codedText = this.PlainTextToBinary(plainText, encoding, utf8BOM, forceUtf8);
|
||||
var dataInputLength = this.GetDataLength(encoding, plainText, codedText, forceUtf8);
|
||||
var version = this.GetVersion(dataInputLength, encoding, eccLevel);
|
||||
var encoding = GetEncodingFromPlaintext(plainText);
|
||||
var codedText = PlainTextToBinary(plainText, encoding, utf8BOM, forceUtf8);
|
||||
var dataInputLength = GetDataLength(encoding, plainText, codedText, forceUtf8);
|
||||
var version = GetVersion(dataInputLength, encoding, eccLevel);
|
||||
|
||||
var modeIndicator = DecToBin((int)encoding, 4);
|
||||
var countIndicator = DecToBin(dataInputLength, this.GetCountIndicatorLength(version, encoding));
|
||||
var countIndicator = DecToBin(dataInputLength, GetCountIndicatorLength(version, encoding));
|
||||
var bitString = modeIndicator + countIndicator;
|
||||
|
||||
|
||||
bitString += codedText;
|
||||
|
||||
//Fill up data code word
|
||||
var eccInfo = this.capacityECCTable.Single(x => x.Version == version && x.ErrorCorrectionLevel.Equals(eccLevel));
|
||||
var eccInfo = capacityECCTable.Single(x => x.Version == version && x.ErrorCorrectionLevel.Equals(eccLevel));
|
||||
var dataLength = eccInfo.TotalDataCodewords * 8;
|
||||
var lengthDiff = dataLength - bitString.Length;
|
||||
if (lengthDiff > 0)
|
||||
|
@ -68,10 +66,10 @@ namespace QRCoder
|
|||
for (var i = 0; i < eccInfo.BlocksInGroup1; i++)
|
||||
{
|
||||
var bitStr = bitString.Substring(i * eccInfo.CodewordsInGroup1 * 8, eccInfo.CodewordsInGroup1 * 8);
|
||||
var bitBlockList = this.BinaryStringToBitBlockList(bitStr);
|
||||
var bitBlockListDec = this.BinaryStringListToDecList(bitBlockList);
|
||||
var eccWordList = this.CalculateECCWords(bitStr, eccInfo);
|
||||
var eccWordListDec = this.BinaryStringListToDecList(eccWordList);
|
||||
var bitBlockList = BinaryStringToBitBlockList(bitStr);
|
||||
var bitBlockListDec = BinaryStringListToDecList(bitBlockList);
|
||||
var eccWordList = CalculateECCWords(bitStr, eccInfo);
|
||||
var eccWordListDec = BinaryStringListToDecList(eccWordList);
|
||||
codeWordWithECC.Add(
|
||||
new CodewordBlock(1,
|
||||
i + 1,
|
||||
|
@ -86,10 +84,10 @@ namespace QRCoder
|
|||
for (var i = 0; i < eccInfo.BlocksInGroup2; i++)
|
||||
{
|
||||
var bitStr = bitString.Substring(i * eccInfo.CodewordsInGroup2 * 8, eccInfo.CodewordsInGroup2 * 8);
|
||||
var bitBlockList = this.BinaryStringToBitBlockList(bitStr);
|
||||
var bitBlockListDec = this.BinaryStringListToDecList(bitBlockList);
|
||||
var eccWordList = this.CalculateECCWords(bitStr, eccInfo);
|
||||
var eccWordListDec = this.BinaryStringListToDecList(eccWordList);
|
||||
var bitBlockList = BinaryStringToBitBlockList(bitStr);
|
||||
var bitBlockListDec = BinaryStringListToDecList(bitBlockList);
|
||||
var eccWordList = CalculateECCWords(bitStr, eccInfo);
|
||||
var eccWordListDec = BinaryStringListToDecList(eccWordList);
|
||||
codeWordWithECC.Add(new CodewordBlock(2,
|
||||
i + 1,
|
||||
bitStr,
|
||||
|
@ -100,7 +98,6 @@ namespace QRCoder
|
|||
);
|
||||
}
|
||||
|
||||
|
||||
//Interleave code words
|
||||
var interleavedWordsSb = new StringBuilder();
|
||||
for (var i = 0; i < Math.Max(eccInfo.CodewordsInGroup1, eccInfo.CodewordsInGroup2); i++)
|
||||
|
@ -110,23 +107,21 @@ namespace QRCoder
|
|||
interleavedWordsSb.Append(codeBlock.CodeWords[i]);
|
||||
}
|
||||
|
||||
|
||||
for (var i = 0; i < eccInfo.ECCPerBlock; i++)
|
||||
{
|
||||
foreach (var codeBlock in codeWordWithECC)
|
||||
if (codeBlock.ECCWords.Count > i)
|
||||
interleavedWordsSb.Append(codeBlock.ECCWords[i]);
|
||||
}
|
||||
interleavedWordsSb.Append(new string('0', this.remainderBits[version - 1]));
|
||||
interleavedWordsSb.Append(new string('0', remainderBits[version - 1]));
|
||||
var interleavedData = interleavedWordsSb.ToString();
|
||||
|
||||
|
||||
//Place interleaved data on module matrix
|
||||
var qr = new QRCodeData(version);
|
||||
var blockedModules = new List<Rectangle>();
|
||||
ModulePlacer.PlaceFinderPatterns(ref qr, ref blockedModules);
|
||||
ModulePlacer.ReserveSeperatorAreas(qr.ModuleMatrix.Count, ref blockedModules);
|
||||
ModulePlacer.PlaceAlignmentPatterns(ref qr, this.alignmentPatternTable.Where(x => x.Version == version).Select(x => x.PatternPositions).First(), ref blockedModules);
|
||||
ModulePlacer.PlaceAlignmentPatterns(ref qr, alignmentPatternTable.Where(x => x.Version == version).Select(x => x.PatternPositions).First(), ref blockedModules);
|
||||
ModulePlacer.PlaceTimingPatterns(ref qr, ref blockedModules);
|
||||
ModulePlacer.PlaceDarkModule(ref qr, version, ref blockedModules);
|
||||
ModulePlacer.ReserveVersionAreas(qr.ModuleMatrix.Count, version, ref blockedModules);
|
||||
|
@ -141,7 +136,6 @@ namespace QRCoder
|
|||
ModulePlacer.PlaceVersion(ref qr, versionString);
|
||||
}
|
||||
|
||||
|
||||
ModulePlacer.AddQuietZone(ref qr);
|
||||
return qr;
|
||||
}
|
||||
|
@ -149,7 +143,7 @@ namespace QRCoder
|
|||
private static string GetFormatString(ECCLevel level, int maskVersion)
|
||||
{
|
||||
var generator = "10100110111";
|
||||
var fStrMask = "101010000010010";
|
||||
const string fStrMask = "101010000010010";
|
||||
|
||||
var fStr = (level == ECCLevel.L) ? "01" : (level == ECCLevel.M) ? "00" : (level == ECCLevel.Q) ? "11" : "10";
|
||||
fStr += DecToBin(maskVersion, 3);
|
||||
|
@ -253,7 +247,6 @@ namespace QRCoder
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public static int MaskCode(ref QRCodeData qrCode, int version, ref List<Rectangle> blockedModules, ECCLevel eccLevel)
|
||||
{
|
||||
var patternName = string.Empty;
|
||||
|
@ -274,15 +267,14 @@ namespace QRCoder
|
|||
{
|
||||
qrTemp.ModuleMatrix[y][x] = qrCode.ModuleMatrix[y][x];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var formatStr = GetFormatString(eccLevel, Convert.ToInt32(pattern.Name.Substring(7, 1))-1);
|
||||
ModulePlacer.PlaceFormat(ref qrTemp, formatStr);
|
||||
PlaceFormat(ref qrTemp, formatStr);
|
||||
if (version >= 7)
|
||||
{
|
||||
var versionString = GetVersionString(version);
|
||||
ModulePlacer.PlaceVersion(ref qrTemp, versionString);
|
||||
PlaceVersion(ref qrTemp, versionString);
|
||||
}
|
||||
|
||||
for (var x = 0; x < size; x++)
|
||||
|
@ -302,15 +294,11 @@ namespace QRCoder
|
|||
patternName = pattern.Name;
|
||||
patternScore = score;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
var patterMethod = methods.First(x => x.Name == patternName);
|
||||
|
||||
|
||||
for (var x = 0; x < size; x++)
|
||||
{
|
||||
for (var y = 0; y < size; y++)
|
||||
|
@ -324,17 +312,15 @@ namespace QRCoder
|
|||
return Convert.ToInt32(patterMethod.Name.Substring(patterMethod.Name.Length - 1, 1)) - 1;
|
||||
}
|
||||
|
||||
|
||||
public static void PlaceDataWords(ref QRCodeData qrCode, string data, ref List<Rectangle> blockedModules)
|
||||
{
|
||||
var size = qrCode.ModuleMatrix.Count;
|
||||
var up = true;
|
||||
var datawords = new Queue<bool>();
|
||||
for (int i = 0; i< data.Length; i++)
|
||||
{
|
||||
datawords.Enqueue(data[i] != '0');
|
||||
}
|
||||
for (var x = size - 1; x >= 0; x = x - 2)
|
||||
foreach (char c in data)
|
||||
datawords.Enqueue(c != '0');
|
||||
|
||||
for (var x = size - 1; x >= 0; x -= 2)
|
||||
{
|
||||
if (x == 6)
|
||||
x = 5;
|
||||
|
@ -404,7 +390,7 @@ namespace QRCoder
|
|||
var size = qrCode.ModuleMatrix.Count;
|
||||
int[] locations = { 0, 0, size - 7, 0, 0, size - 7 };
|
||||
|
||||
for (var i = 0; i < 6; i = i + 2)
|
||||
for (var i = 0; i < 6; i += 2)
|
||||
{
|
||||
for (var x = 0; x < 7; x++)
|
||||
{
|
||||
|
@ -488,10 +474,9 @@ namespace QRCoder
|
|||
{
|
||||
public static int Score(ref QRCodeData qrCode)
|
||||
{
|
||||
int score1 = 0,
|
||||
score2 = 0,
|
||||
score3 = 0,
|
||||
score4 = 0;
|
||||
int score1 = 0;
|
||||
int score2 = 0;
|
||||
int score3 = 0;
|
||||
var size = qrCode.ModuleMatrix.Count;
|
||||
|
||||
//Penalty 1
|
||||
|
@ -513,7 +498,6 @@ namespace QRCoder
|
|||
score1++;
|
||||
lastValRow = qrCode.ModuleMatrix[y][x];
|
||||
|
||||
|
||||
if (qrCode.ModuleMatrix[x][y] == lastValColumn)
|
||||
modInColumn++;
|
||||
else
|
||||
|
@ -526,7 +510,6 @@ namespace QRCoder
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
//Penalty 2
|
||||
for (var y = 0; y < size - 1; y++)
|
||||
{
|
||||
|
@ -608,7 +591,7 @@ namespace QRCoder
|
|||
var percent = (blackModules / (qrCode.ModuleMatrix.Count * qrCode.ModuleMatrix.Count)) * 100;
|
||||
var prevMultipleOf5 = Math.Abs((int) Math.Floor(percent/5)*5 - 50)/5;
|
||||
var nextMultipleOf5 = Math.Abs((int)Math.Floor(percent / 5) * 5 -45)/5;
|
||||
score4 = Math.Min(prevMultipleOf5, nextMultipleOf5)*10;
|
||||
var score4 = Math.Min(prevMultipleOf5, nextMultipleOf5)*10;
|
||||
|
||||
return score1 + score2 + score3 + score4;
|
||||
}
|
||||
|
@ -627,8 +610,8 @@ namespace QRCoder
|
|||
private List<string> CalculateECCWords(string bitString, ECCInfo eccInfo)
|
||||
{
|
||||
var eccWords = eccInfo.ECCPerBlock;
|
||||
var messagePolynom = this.CalculateMessagePolynom(bitString);
|
||||
var generatorPolynom = this.CalculateGeneratorPolynom(eccWords);
|
||||
var messagePolynom = CalculateMessagePolynom(bitString);
|
||||
var generatorPolynom = CalculateGeneratorPolynom(eccWords);
|
||||
|
||||
for (var i = 0; i < messagePolynom.PolyItems.Count; i++)
|
||||
messagePolynom.PolyItems[i] = new PolynomItem(messagePolynom.PolyItems[i].Coefficient,
|
||||
|
@ -648,9 +631,9 @@ namespace QRCoder
|
|||
}
|
||||
else
|
||||
{
|
||||
var resPoly = this.MultiplyGeneratorPolynomByLeadterm(generatorPolynom, this.ConvertToAlphaNotation(leadTermSource).PolyItems[0], i);
|
||||
resPoly = this.ConvertToDecNotation(resPoly);
|
||||
resPoly = this.XORPolynoms(leadTermSource, resPoly);
|
||||
var resPoly = MultiplyGeneratorPolynomByLeadterm(generatorPolynom, ConvertToAlphaNotation(leadTermSource).PolyItems[0], i);
|
||||
resPoly = ConvertToDecNotation(resPoly);
|
||||
resPoly = XORPolynoms(leadTermSource, resPoly);
|
||||
leadTermSource = resPoly;
|
||||
}
|
||||
}
|
||||
|
@ -673,13 +656,13 @@ namespace QRCoder
|
|||
{
|
||||
var newPoly = new Polynom();
|
||||
for (var i = 0; i < poly.PolyItems.Count; i++)
|
||||
newPoly.PolyItems.Add(new PolynomItem(this.GetIntValFromAlphaExp(poly.PolyItems[i].Coefficient), poly.PolyItems[i].Exponent));
|
||||
newPoly.PolyItems.Add(new PolynomItem(GetIntValFromAlphaExp(poly.PolyItems[i].Coefficient), poly.PolyItems[i].Exponent));
|
||||
return newPoly;
|
||||
}
|
||||
|
||||
private int GetVersion(int length, EncodingMode encMode, ECCLevel eccLevel)
|
||||
{
|
||||
var version = this.capacityTable.Where(
|
||||
var version = capacityTable.Where(
|
||||
x => x.Details.Count(
|
||||
y => (y.ErrorCorrectionLevel == eccLevel
|
||||
&& y.CapacityDict[encMode] >= Convert.ToInt32(length)
|
||||
|
@ -698,39 +681,27 @@ namespace QRCoder
|
|||
{
|
||||
if (StringAll(plainText, "0123456789"))
|
||||
return EncodingMode.Numeric;
|
||||
else if (StringAll(plainText, new string(this.alphanumEncTable)))
|
||||
if (StringAll(plainText, new string(alphanumEncTable)))
|
||||
return EncodingMode.Alphanumeric;
|
||||
else
|
||||
return EncodingMode.Byte;
|
||||
return EncodingMode.Byte;
|
||||
}
|
||||
|
||||
private bool StringAll(string input, string charGroupIn)
|
||||
private static bool StringAll(string input, string charGroupIn)
|
||||
{
|
||||
|
||||
bool res = true;
|
||||
foreach (var c in input)
|
||||
{
|
||||
if (!charGroupIn.Contains(c.ToString()))
|
||||
{
|
||||
res = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
return input.All(c => charGroupIn.Contains(c.ToString()));
|
||||
}
|
||||
|
||||
private Polynom CalculateMessagePolynom(string bitString)
|
||||
private static Polynom CalculateMessagePolynom(string bitString)
|
||||
{
|
||||
var messagePol = new Polynom();
|
||||
for (var i = bitString.Length / 8 - 1; i >= 0; i--)
|
||||
{
|
||||
messagePol.PolyItems.Add(new PolynomItem(this.BinToDec(bitString.Substring(0, 8)), i));
|
||||
messagePol.PolyItems.Add(new PolynomItem(BinToDec(bitString.Substring(0, 8)), i));
|
||||
bitString = bitString.Remove(0, 8);
|
||||
}
|
||||
return messagePol;
|
||||
}
|
||||
|
||||
|
||||
private Polynom CalculateGeneratorPolynom(int numEccWords)
|
||||
{
|
||||
var generatorPolynom = new Polynom();
|
||||
|
@ -746,34 +717,28 @@ namespace QRCoder
|
|||
new PolynomItem(i,0)
|
||||
});
|
||||
|
||||
generatorPolynom = this.MultiplyAlphaPolynoms(generatorPolynom, multiplierPolynom);
|
||||
generatorPolynom = MultiplyAlphaPolynoms(generatorPolynom, multiplierPolynom);
|
||||
}
|
||||
|
||||
return generatorPolynom;
|
||||
}
|
||||
|
||||
private List<string> BinaryStringToBitBlockList(string bitString)
|
||||
private static List<string> BinaryStringToBitBlockList(string bitString)
|
||||
{
|
||||
return new List<char>(bitString.ToCharArray()).Select((x, i) => new { Index = i, Value = x })
|
||||
.GroupBy(x => x.Index / 8)
|
||||
.Select(x => String.Join("", x.Select(v => v.Value.ToString()).ToArray()))
|
||||
.Select(x => string.Concat(x.Select(v => v.Value.ToString()).ToArray()))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private List<int> BinaryStringListToDecList(List<string> binaryStringList)
|
||||
private static List<int> BinaryStringListToDecList(List<string> binaryStringList)
|
||||
{
|
||||
return binaryStringList.Select(binaryString => this.BinToDec(binaryString)).ToList();
|
||||
return binaryStringList.Select(BinToDec).ToList();
|
||||
}
|
||||
|
||||
private int BinToDec(string binStr)
|
||||
{
|
||||
return Convert.ToInt32(binStr, 2);
|
||||
}
|
||||
private static int BinToDec(string binStr) => Convert.ToInt32(binStr, 2);
|
||||
|
||||
private static string DecToBin(int decNum)
|
||||
{
|
||||
return Convert.ToString(decNum, 2);
|
||||
}
|
||||
private static string DecToBin(int decNum) => Convert.ToString(decNum, 2);
|
||||
|
||||
private static string DecToBin(int decNum, int padLeftUpTo)
|
||||
{
|
||||
|
@ -818,12 +783,12 @@ namespace QRCoder
|
|||
|
||||
private int GetDataLength(EncodingMode encoding, string plainText, string codedText, bool forceUtf8)
|
||||
{
|
||||
return forceUtf8 || this.IsUtf8(encoding, plainText) ? (codedText.Length / 8) : plainText.Length;
|
||||
return forceUtf8 || IsUtf8(encoding, plainText) ? (codedText.Length / 8) : plainText.Length;
|
||||
}
|
||||
|
||||
private bool IsUtf8(EncodingMode encoding, string plainText)
|
||||
{
|
||||
return encoding == EncodingMode.Byte && !this.IsValidISO(plainText);
|
||||
return encoding == EncodingMode.Byte && !IsValidISO(plainText);
|
||||
}
|
||||
|
||||
private bool IsValidISO(string input)
|
||||
|
@ -831,19 +796,18 @@ namespace QRCoder
|
|||
var bytes = Encoding.GetEncoding("ISO-8859-1").GetBytes(input);
|
||||
//var result = Encoding.GetEncoding("ISO-8859-1").GetString(bytes);
|
||||
var result = Encoding.GetEncoding("ISO-8859-1").GetString(bytes,0,bytes.Length);
|
||||
return String.Equals(input, result);
|
||||
return string.Equals(input, result);
|
||||
}
|
||||
|
||||
private string PlainTextToBinary(string plainText, EncodingMode encMode, bool utf8BOM, bool forceUtf8)
|
||||
{
|
||||
if (encMode.Equals(EncodingMode.Numeric))
|
||||
return this.PlainTextToBinaryNumeric(plainText);
|
||||
else if (encMode.Equals(EncodingMode.Alphanumeric))
|
||||
return this.PlainTextToBinaryAlphanumeric(plainText);
|
||||
else if (encMode.Equals(EncodingMode.Byte))
|
||||
return this.PlainTextToBinaryByte(plainText, utf8BOM, forceUtf8);
|
||||
else
|
||||
return string.Empty;
|
||||
return PlainTextToBinaryNumeric(plainText);
|
||||
if (encMode.Equals(EncodingMode.Alphanumeric))
|
||||
return PlainTextToBinaryAlphanumeric(plainText);
|
||||
if (encMode.Equals(EncodingMode.Byte))
|
||||
return PlainTextToBinaryByte(plainText, utf8BOM, forceUtf8);
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
private string PlainTextToBinaryNumeric(string plainText)
|
||||
|
@ -854,7 +818,6 @@ namespace QRCoder
|
|||
var dec = Convert.ToInt32(plainText.Substring(0, 3));
|
||||
codeText += DecToBin(dec, 10);
|
||||
plainText = plainText.Substring(3);
|
||||
|
||||
}
|
||||
if (plainText.Length == 2)
|
||||
{
|
||||
|
@ -875,14 +838,13 @@ namespace QRCoder
|
|||
while (plainText.Length >= 2)
|
||||
{
|
||||
var token = plainText.Substring(0, 2);
|
||||
var dec = this.alphanumEncDict[token[0]] * 45 + this.alphanumEncDict[token[1]];
|
||||
var dec = alphanumEncDict[token[0]] * 45 + alphanumEncDict[token[1]];
|
||||
codeText += DecToBin(dec, 11);
|
||||
plainText = plainText.Substring(2);
|
||||
|
||||
}
|
||||
if (plainText.Length > 0)
|
||||
{
|
||||
codeText += DecToBin(this.alphanumEncDict[plainText[0]], 6);
|
||||
codeText += DecToBin(alphanumEncDict[plainText[0]], 6);
|
||||
}
|
||||
return codeText;
|
||||
}
|
||||
|
@ -892,7 +854,7 @@ namespace QRCoder
|
|||
byte[] codeBytes;
|
||||
var codeText = string.Empty;
|
||||
|
||||
if (this.IsValidISO(plainText) && !forceUtf8)
|
||||
if (IsValidISO(plainText) && !forceUtf8)
|
||||
codeBytes = Encoding.GetEncoding("ISO-8859-1").GetBytes(plainText);
|
||||
else
|
||||
codeBytes = utf8BOM ? Encoding.UTF8.GetPreamble().Concat(Encoding.UTF8.GetBytes(plainText)).ToArray() : Encoding.UTF8.GetBytes(plainText);
|
||||
|
@ -903,7 +865,6 @@ namespace QRCoder
|
|||
return codeText;
|
||||
}
|
||||
|
||||
|
||||
private Polynom XORPolynoms(Polynom messagePolynom, Polynom resPolynom)
|
||||
{
|
||||
var resultPolynom = new Polynom();
|
||||
|
@ -934,7 +895,6 @@ namespace QRCoder
|
|||
return resultPolynom;
|
||||
}
|
||||
|
||||
|
||||
private Polynom MultiplyGeneratorPolynomByLeadterm(Polynom genPolynom, PolynomItem leadTerm, int lowerExponentBy)
|
||||
{
|
||||
var resultPolynom = new Polynom();
|
||||
|
@ -950,7 +910,6 @@ namespace QRCoder
|
|||
return resultPolynom;
|
||||
}
|
||||
|
||||
|
||||
private Polynom MultiplyAlphaPolynoms(Polynom polynomBase, Polynom polynomMultiplier)
|
||||
{
|
||||
var resultPolynom = new Polynom();
|
||||
|
@ -972,8 +931,8 @@ namespace QRCoder
|
|||
foreach (var exponent in toGlue)
|
||||
{
|
||||
var coefficient = resultPolynom.PolyItems.Where(x => x.Exponent == exponent).Aggregate(0, (current, polynomOld)
|
||||
=> current ^ this.GetIntValFromAlphaExp(polynomOld.Coefficient));
|
||||
var polynomFixed = new PolynomItem(this.GetAlphaExpFromIntVal(coefficient), exponent);
|
||||
=> current ^ GetIntValFromAlphaExp(polynomOld.Coefficient));
|
||||
var polynomFixed = new PolynomItem(GetAlphaExpFromIntVal(coefficient), exponent);
|
||||
gluedPolynoms.Add(polynomFixed);
|
||||
}
|
||||
resultPolynom.PolyItems.RemoveAll(x => toGlue.Contains(x.Exponent));
|
||||
|
@ -984,47 +943,45 @@ namespace QRCoder
|
|||
|
||||
private int GetIntValFromAlphaExp(int exp)
|
||||
{
|
||||
return this.galoisField.Where(alog => alog.ExponentAlpha == exp).Select(alog => alog.IntegerValue).First();
|
||||
return galoisField.First(alog => alog.ExponentAlpha == exp).IntegerValue;
|
||||
}
|
||||
|
||||
private int GetAlphaExpFromIntVal(int intVal)
|
||||
{
|
||||
return this.galoisField.Where(alog => alog.IntegerValue == intVal).Select(alog => alog.ExponentAlpha).First();
|
||||
return galoisField.First(alog => alog.IntegerValue == intVal).ExponentAlpha;
|
||||
}
|
||||
|
||||
private static int ShrinkAlphaExp(int alphaExp)
|
||||
{
|
||||
// ReSharper disable once PossibleLossOfFraction
|
||||
return (int)((alphaExp % 256) + Math.Floor((double)(alphaExp / 256)));
|
||||
return (byte)alphaExp + alphaExp >> 8;
|
||||
}
|
||||
|
||||
private void CreateAlphanumEncDict()
|
||||
{
|
||||
this.alphanumEncDict = new Dictionary<char, int>();
|
||||
//this.alphanumEncTable.ToList().Select((x, i) => new { Chr = x, Index = i }).ToList().ForEach(x => this.alphanumEncDict.Add(x.Chr, x.Index));
|
||||
var resList = this.alphanumEncTable.ToList().Select((x, i) => new { Chr = x, Index = i }).ToList();
|
||||
alphanumEncDict = new Dictionary<char, int>();
|
||||
var resList = alphanumEncTable.ToList().Select((x, i) => new { Chr = x, Index = i }).ToList();
|
||||
foreach (var res in resList)
|
||||
{
|
||||
this.alphanumEncDict.Add(res.Chr, res.Index);
|
||||
alphanumEncDict.Add(res.Chr, res.Index);
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateAlignmentPatternTable()
|
||||
{
|
||||
this.alignmentPatternTable = new List<AlignmentPattern>();
|
||||
alignmentPatternTable = new List<AlignmentPattern>();
|
||||
|
||||
for (var i = 0; i < (7 * 40); i = i + 7)
|
||||
for (var i = 0; i < (7 * 40); i += 7)
|
||||
{
|
||||
var points = new List<Point>();
|
||||
for (var x = 0; x < 7; x++)
|
||||
{
|
||||
if (this.alignmentPatternBaseValues[i + x] != 0)
|
||||
if (alignmentPatternBaseValues[i + x] != 0)
|
||||
{
|
||||
for (var y = 0; y < 7; y++)
|
||||
{
|
||||
if (this.alignmentPatternBaseValues[i + y] != 0)
|
||||
if (alignmentPatternBaseValues[i + y] != 0)
|
||||
{
|
||||
var p = new Point(this.alignmentPatternBaseValues[i + x] - 2, this.alignmentPatternBaseValues[i + y] - 2);
|
||||
var p = new Point(alignmentPatternBaseValues[i + x] - 2, alignmentPatternBaseValues[i + y] - 2);
|
||||
if (!points.Contains(p))
|
||||
points.Add(p);
|
||||
}
|
||||
|
@ -1032,7 +989,7 @@ namespace QRCoder
|
|||
}
|
||||
}
|
||||
|
||||
this.alignmentPatternTable.Add(new AlignmentPattern()
|
||||
alignmentPatternTable.Add(new AlignmentPattern()
|
||||
{
|
||||
Version = (i + 7) / 7,
|
||||
PatternPositions = points
|
||||
|
@ -1041,56 +998,55 @@ namespace QRCoder
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
private void CreateCapacityECCTable()
|
||||
{
|
||||
this.capacityECCTable = new List<ECCInfo>();
|
||||
for (var i = 0; i < (4 * 6 * 40); i = i + (4 * 6))
|
||||
capacityECCTable = new List<ECCInfo>();
|
||||
for (var i = 0; i < (4 * 6 * 40); i += (4 * 6))
|
||||
{
|
||||
this.capacityECCTable.AddRange(
|
||||
capacityECCTable.AddRange(
|
||||
new[]
|
||||
{
|
||||
new ECCInfo(
|
||||
(i+24) / 24,
|
||||
ECCLevel.L,
|
||||
this.capacityECCBaseValues[i],
|
||||
this.capacityECCBaseValues[i+1],
|
||||
this.capacityECCBaseValues[i+2],
|
||||
this.capacityECCBaseValues[i+3],
|
||||
this.capacityECCBaseValues[i+4],
|
||||
this.capacityECCBaseValues[i+5]),
|
||||
capacityECCBaseValues[i],
|
||||
capacityECCBaseValues[i+1],
|
||||
capacityECCBaseValues[i+2],
|
||||
capacityECCBaseValues[i+3],
|
||||
capacityECCBaseValues[i+4],
|
||||
capacityECCBaseValues[i+5]),
|
||||
new ECCInfo
|
||||
(
|
||||
version: (i + 24) / 24,
|
||||
errorCorrectionLevel: ECCLevel.M,
|
||||
totalDataCodewords: this.capacityECCBaseValues[i+6],
|
||||
eccPerBlock: this.capacityECCBaseValues[i+7],
|
||||
blocksInGroup1: this.capacityECCBaseValues[i+8],
|
||||
codewordsInGroup1: this.capacityECCBaseValues[i+9],
|
||||
blocksInGroup2: this.capacityECCBaseValues[i+10],
|
||||
codewordsInGroup2: this.capacityECCBaseValues[i+11]
|
||||
totalDataCodewords: capacityECCBaseValues[i+6],
|
||||
eccPerBlock: capacityECCBaseValues[i+7],
|
||||
blocksInGroup1: capacityECCBaseValues[i+8],
|
||||
codewordsInGroup1: capacityECCBaseValues[i+9],
|
||||
blocksInGroup2: capacityECCBaseValues[i+10],
|
||||
codewordsInGroup2: capacityECCBaseValues[i+11]
|
||||
),
|
||||
new ECCInfo
|
||||
(
|
||||
version: (i + 24) / 24,
|
||||
errorCorrectionLevel: ECCLevel.Q,
|
||||
totalDataCodewords: this.capacityECCBaseValues[i+12],
|
||||
eccPerBlock: this.capacityECCBaseValues[i+13],
|
||||
blocksInGroup1: this.capacityECCBaseValues[i+14],
|
||||
codewordsInGroup1: this.capacityECCBaseValues[i+15],
|
||||
blocksInGroup2: this.capacityECCBaseValues[i+16],
|
||||
codewordsInGroup2: this.capacityECCBaseValues[i+17]
|
||||
totalDataCodewords: capacityECCBaseValues[i+12],
|
||||
eccPerBlock: capacityECCBaseValues[i+13],
|
||||
blocksInGroup1: capacityECCBaseValues[i+14],
|
||||
codewordsInGroup1: capacityECCBaseValues[i+15],
|
||||
blocksInGroup2: capacityECCBaseValues[i+16],
|
||||
codewordsInGroup2: capacityECCBaseValues[i+17]
|
||||
),
|
||||
new ECCInfo
|
||||
(
|
||||
version: (i + 24) / 24,
|
||||
errorCorrectionLevel: ECCLevel.H,
|
||||
totalDataCodewords: this.capacityECCBaseValues[i+18],
|
||||
eccPerBlock: this.capacityECCBaseValues[i+19],
|
||||
blocksInGroup1: this.capacityECCBaseValues[i+20],
|
||||
codewordsInGroup1: this.capacityECCBaseValues[i+21],
|
||||
blocksInGroup2: this.capacityECCBaseValues[i+22],
|
||||
codewordsInGroup2: this.capacityECCBaseValues[i+23]
|
||||
totalDataCodewords: capacityECCBaseValues[i+18],
|
||||
eccPerBlock: capacityECCBaseValues[i+19],
|
||||
blocksInGroup1: capacityECCBaseValues[i+20],
|
||||
codewordsInGroup1: capacityECCBaseValues[i+21],
|
||||
blocksInGroup2: capacityECCBaseValues[i+22],
|
||||
codewordsInGroup2: capacityECCBaseValues[i+23]
|
||||
)
|
||||
});
|
||||
}
|
||||
|
@ -1098,10 +1054,10 @@ namespace QRCoder
|
|||
|
||||
private void CreateCapacityTable()
|
||||
{
|
||||
this.capacityTable = new List<VersionInfo>();
|
||||
for (var i = 0; i < (16 * 40); i = i + 16)
|
||||
capacityTable = new List<VersionInfo>();
|
||||
for (var i = 0; i < (16 * 40); i += 16)
|
||||
{
|
||||
this.capacityTable.Add(new VersionInfo(
|
||||
capacityTable.Add(new VersionInfo(
|
||||
|
||||
(i + 16) / 16,
|
||||
new List<VersionInfoDetails>
|
||||
|
@ -1109,37 +1065,37 @@ namespace QRCoder
|
|||
new VersionInfoDetails(
|
||||
ECCLevel.L,
|
||||
new Dictionary<EncodingMode,int>(){
|
||||
{ EncodingMode.Numeric, this.capacityBaseValues[i] },
|
||||
{ EncodingMode.Alphanumeric, this.capacityBaseValues[i+1] },
|
||||
{ EncodingMode.Byte, this.capacityBaseValues[i+2] },
|
||||
{ EncodingMode.Kanji, this.capacityBaseValues[i+3] },
|
||||
{ EncodingMode.Numeric, capacityBaseValues[i] },
|
||||
{ EncodingMode.Alphanumeric, capacityBaseValues[i+1] },
|
||||
{ EncodingMode.Byte, capacityBaseValues[i+2] },
|
||||
{ EncodingMode.Kanji, capacityBaseValues[i+3] },
|
||||
}
|
||||
),
|
||||
new VersionInfoDetails(
|
||||
ECCLevel.M,
|
||||
new Dictionary<EncodingMode,int>(){
|
||||
{ EncodingMode.Numeric, this.capacityBaseValues[i+4] },
|
||||
{ EncodingMode.Alphanumeric, this.capacityBaseValues[i+5] },
|
||||
{ EncodingMode.Byte, this.capacityBaseValues[i+6] },
|
||||
{ EncodingMode.Kanji, this.capacityBaseValues[i+7] },
|
||||
{ EncodingMode.Numeric, capacityBaseValues[i+4] },
|
||||
{ EncodingMode.Alphanumeric, capacityBaseValues[i+5] },
|
||||
{ EncodingMode.Byte, capacityBaseValues[i+6] },
|
||||
{ EncodingMode.Kanji, capacityBaseValues[i+7] },
|
||||
}
|
||||
),
|
||||
new VersionInfoDetails(
|
||||
ECCLevel.Q,
|
||||
new Dictionary<EncodingMode,int>(){
|
||||
{ EncodingMode.Numeric, this.capacityBaseValues[i+8] },
|
||||
{ EncodingMode.Alphanumeric, this.capacityBaseValues[i+9] },
|
||||
{ EncodingMode.Byte, this.capacityBaseValues[i+10] },
|
||||
{ EncodingMode.Kanji, this.capacityBaseValues[i+11] },
|
||||
{ EncodingMode.Numeric, capacityBaseValues[i+8] },
|
||||
{ EncodingMode.Alphanumeric, capacityBaseValues[i+9] },
|
||||
{ EncodingMode.Byte, capacityBaseValues[i+10] },
|
||||
{ EncodingMode.Kanji, capacityBaseValues[i+11] },
|
||||
}
|
||||
),
|
||||
new VersionInfoDetails(
|
||||
ECCLevel.H,
|
||||
new Dictionary<EncodingMode,int>(){
|
||||
{ EncodingMode.Numeric, this.capacityBaseValues[i+12] },
|
||||
{ EncodingMode.Alphanumeric, this.capacityBaseValues[i+13] },
|
||||
{ EncodingMode.Byte, this.capacityBaseValues[i+14] },
|
||||
{ EncodingMode.Kanji, this.capacityBaseValues[i+15] },
|
||||
{ EncodingMode.Numeric, capacityBaseValues[i+12] },
|
||||
{ EncodingMode.Alphanumeric, capacityBaseValues[i+13] },
|
||||
{ EncodingMode.Byte, capacityBaseValues[i+14] },
|
||||
{ EncodingMode.Kanji, capacityBaseValues[i+15] },
|
||||
}
|
||||
)
|
||||
}
|
||||
|
@ -1149,7 +1105,7 @@ namespace QRCoder
|
|||
|
||||
private void CreateAntilogTable()
|
||||
{
|
||||
this.galoisField = new List<Antilog>();
|
||||
galoisField = new List<Antilog>();
|
||||
|
||||
for (var i = 0; i < 256; i++)
|
||||
{
|
||||
|
@ -1157,13 +1113,13 @@ namespace QRCoder
|
|||
|
||||
if (i > 7)
|
||||
{
|
||||
gfItem = this.galoisField[i - 1].IntegerValue * 2;
|
||||
gfItem = galoisField[i - 1].IntegerValue * 2;
|
||||
}
|
||||
if (gfItem > 255)
|
||||
{
|
||||
gfItem = gfItem ^ 285;
|
||||
gfItem ^= 285;
|
||||
}
|
||||
this.galoisField.Add(new Antilog(i, gfItem));
|
||||
galoisField.Add(new Antilog(i, gfItem));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1195,13 +1151,13 @@ namespace QRCoder
|
|||
public CodewordBlock(int groupNumber, int blockNumber, string bitString, List<string> codeWords,
|
||||
List<string> eccWords, List<int> codeWordsInt, List<int> eccWordsInt)
|
||||
{
|
||||
this.GroupNumber = groupNumber;
|
||||
this.BlockNumber = blockNumber;
|
||||
this.BitString = bitString;
|
||||
this.CodeWords = codeWords;
|
||||
this.ECCWords = eccWords;
|
||||
this.CodeWordsInt = codeWordsInt;
|
||||
this.ECCWordsInt = eccWordsInt;
|
||||
GroupNumber = groupNumber;
|
||||
BlockNumber = blockNumber;
|
||||
BitString = bitString;
|
||||
CodeWords = codeWords;
|
||||
ECCWords = eccWords;
|
||||
CodeWordsInt = codeWordsInt;
|
||||
ECCWordsInt = eccWordsInt;
|
||||
}
|
||||
|
||||
public int GroupNumber { get; }
|
||||
|
@ -1218,14 +1174,14 @@ namespace QRCoder
|
|||
public ECCInfo(int version, ECCLevel errorCorrectionLevel, int totalDataCodewords, int eccPerBlock, int blocksInGroup1,
|
||||
int codewordsInGroup1, int blocksInGroup2, int codewordsInGroup2)
|
||||
{
|
||||
this.Version = version;
|
||||
this.ErrorCorrectionLevel = errorCorrectionLevel;
|
||||
this.TotalDataCodewords = totalDataCodewords;
|
||||
this.ECCPerBlock = eccPerBlock;
|
||||
this.BlocksInGroup1 = blocksInGroup1;
|
||||
this.CodewordsInGroup1 = codewordsInGroup1;
|
||||
this.BlocksInGroup2 = blocksInGroup2;
|
||||
this.CodewordsInGroup2 = codewordsInGroup2;
|
||||
Version = version;
|
||||
ErrorCorrectionLevel = errorCorrectionLevel;
|
||||
TotalDataCodewords = totalDataCodewords;
|
||||
ECCPerBlock = eccPerBlock;
|
||||
BlocksInGroup1 = blocksInGroup1;
|
||||
CodewordsInGroup1 = codewordsInGroup1;
|
||||
BlocksInGroup2 = blocksInGroup2;
|
||||
CodewordsInGroup2 = codewordsInGroup2;
|
||||
}
|
||||
public int Version { get; }
|
||||
public ECCLevel ErrorCorrectionLevel { get; }
|
||||
|
@ -1241,8 +1197,8 @@ namespace QRCoder
|
|||
{
|
||||
public VersionInfo(int version, List<VersionInfoDetails> versionInfoDetails)
|
||||
{
|
||||
this.Version = version;
|
||||
this.Details = versionInfoDetails;
|
||||
Version = version;
|
||||
Details = versionInfoDetails;
|
||||
}
|
||||
public int Version { get; }
|
||||
public List<VersionInfoDetails> Details { get; }
|
||||
|
@ -1252,8 +1208,8 @@ namespace QRCoder
|
|||
{
|
||||
public VersionInfoDetails(ECCLevel errorCorrectionLevel, Dictionary<EncodingMode, int> capacityDict)
|
||||
{
|
||||
this.ErrorCorrectionLevel = errorCorrectionLevel;
|
||||
this.CapacityDict = capacityDict;
|
||||
ErrorCorrectionLevel = errorCorrectionLevel;
|
||||
CapacityDict = capacityDict;
|
||||
}
|
||||
|
||||
public ECCLevel ErrorCorrectionLevel { get; }
|
||||
|
@ -1264,8 +1220,8 @@ namespace QRCoder
|
|||
{
|
||||
public Antilog(int exponentAlpha, int integerValue)
|
||||
{
|
||||
this.ExponentAlpha = exponentAlpha;
|
||||
this.IntegerValue = integerValue;
|
||||
ExponentAlpha = exponentAlpha;
|
||||
IntegerValue = integerValue;
|
||||
}
|
||||
public int ExponentAlpha { get; }
|
||||
public int IntegerValue { get; }
|
||||
|
@ -1275,8 +1231,8 @@ namespace QRCoder
|
|||
{
|
||||
public PolynomItem(int coefficient, int exponent)
|
||||
{
|
||||
this.Coefficient = coefficient;
|
||||
this.Exponent = exponent;
|
||||
Coefficient = coefficient;
|
||||
Exponent = exponent;
|
||||
}
|
||||
|
||||
public int Coefficient { get; }
|
||||
|
@ -1287,7 +1243,7 @@ namespace QRCoder
|
|||
{
|
||||
public Polynom()
|
||||
{
|
||||
this.PolyItems = new List<PolynomItem>();
|
||||
PolyItems = new List<PolynomItem>();
|
||||
}
|
||||
|
||||
public List<PolynomItem> PolyItems { get; set; }
|
||||
|
@ -1296,12 +1252,12 @@ namespace QRCoder
|
|||
{
|
||||
var sb = new StringBuilder();
|
||||
//this.PolyItems.ForEach(x => sb.Append("a^" + x.Coefficient + "*x^" + x.Exponent + " + "));
|
||||
foreach (var polyItem in this.PolyItems)
|
||||
foreach (var polyItem in PolyItems)
|
||||
{
|
||||
sb.Append("a^").Append(polyItem.Coefficient).Append("*x^").Append(polyItem.Exponent).Append(" + ");
|
||||
}
|
||||
|
||||
return sb.ToString().TrimEnd(new[] { ' ', '+' });
|
||||
return sb.ToString().TrimEnd(' ', '+');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1309,10 +1265,11 @@ namespace QRCoder
|
|||
{
|
||||
public int X { get; }
|
||||
public int Y { get; }
|
||||
|
||||
public Point(int x, int y)
|
||||
{
|
||||
this.X = x;
|
||||
this.Y = y;
|
||||
X = x;
|
||||
Y = y;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1325,25 +1282,25 @@ namespace QRCoder
|
|||
|
||||
public Rectangle(int x, int y, int w, int h)
|
||||
{
|
||||
this.X = x;
|
||||
this.Y = y;
|
||||
this.Width = w;
|
||||
this.Height = h;
|
||||
X = x;
|
||||
Y = y;
|
||||
Width = w;
|
||||
Height = h;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
this.alignmentPatternBaseValues = null;
|
||||
this.alignmentPatternTable = null;
|
||||
this.alphanumEncDict = null;
|
||||
this.alphanumEncTable = null;
|
||||
this.capacityBaseValues = null;
|
||||
this.capacityECCBaseValues = null;
|
||||
this.capacityECCTable = null;
|
||||
this.capacityTable = null;
|
||||
this.galoisField = null;
|
||||
this.remainderBits = null;
|
||||
alignmentPatternBaseValues = null;
|
||||
alignmentPatternTable = null;
|
||||
alphanumEncDict = null;
|
||||
alphanumEncTable = null;
|
||||
capacityBaseValues = null;
|
||||
capacityECCBaseValues = null;
|
||||
capacityECCTable = null;
|
||||
capacityTable = null;
|
||||
galoisField = null;
|
||||
remainderBits = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue