public void Add(bool[] initial, int value, int bitCount, bool[] expected) { var target = new BitList(); target.Add(initial); target.Add(value, bitCount); CollectionAssert.AreEquivalent(expected, target); }
private static BitList VersionInfoBitList(int version) { BitList result = new BitList(); result.Add(version, s_LengthDataBits); result.Add(BCHCalculator.CalculateBCH(version, s_VersionBCHPoly), s_LengthECBits); if(result.Count != (s_LengthECBits + s_LengthDataBits)) throw new Exception("Version Info creation error. Result is not 18 bits"); return result; }
static void Main(string[] args) { BitList<int> list = new BitList<int>(); list.Add(1, 0); list.Add(2, 0); list.Add(3, 0); list.Add(4, 1); list.Add(5, 3); list.PrintList(); }
/// <summary> /// /// </summary> /// <param name="characterCount"></param> /// <returns></returns> internal BitList GetCharCountIndicator(int characterCount, int version) { BitList characterCountBits = new BitList(); int bitCount = GetBitCountInCharCountIndicator(version); characterCountBits.Add(characterCount, bitCount); return characterCountBits; }
private void TestOneCase(IEnumerable<bool> dataCodewords, VersionDetail vc, IEnumerable<bool> expected) { BitList dcList = new BitList(); dcList.Add(dataCodewords); IEnumerable<bool> actualResult = ECGenerator.FillECCodewords(dcList, vc); BitVectorTestExtensions.CompareIEnumerable(actualResult, expected, "string"); }
private void TestOneData(IEnumerable<bool> data, int numTotalByte, IEnumerable<bool> expected) { BitList tData = new BitList(); tData.Add(data); tData.TerminateBites(tData.Count, numTotalByte); IEnumerable actualResult = tData; CollectionAssert.AreEquivalent(expected.ToList(), actualResult); }
/// <summary> /// Toes the bit list. /// </summary> /// <param name="bArray">The b array.</param> /// <returns></returns> /// <remarks></remarks> internal static BitList ToBitList(byte[] bArray) { int bLength = bArray.Length; var result = new BitList(); for (int bIndex = 0; bIndex < bLength; bIndex++) { result.Add(bArray[bIndex], 8); } return result; }
private void Test_One_Case(int version, TriStateMatrix expected, IEnumerable<bool> codewords) { BitList dcList = new BitList(); dcList.Add(codewords); TriStateMatrix target = new TriStateMatrix(expected.Width); PositioninngPatternBuilder.EmbedBasicPatterns(version, target); target.TryEmbedCodewords(dcList); expected.AssertEquals(target); }
public void Test003() { var bs = new BitList(); var ret = new StringBuilder(); for (var i = 0; i < 12; i++) { var item = (i % 2) != 0; bs.Add(item); ret.Insert(0, item ? "1" : "0"); } Assert.AreEqual(ret.ToString(), string.Join("", bs.Reverse().Select(a => a ? "1" : "0"))); }
public void Test005() { var bs = new BitList(); for (var i = 0; i < 8000000; i++) { bs.Add(i % 2 != 0); // Index# Even=false, Odd=true } Assert.AreEqual(false, bs[0]); Assert.AreEqual(false, bs[1000000]); Assert.AreEqual(true, bs[4999999]); Assert.AreEqual(true, bs[7999999]); }
private void Test_One_Case(int version, TriStateMatrix expected, IEnumerable <bool> codewords) { BitList dcList = new BitList(); dcList.Add(codewords); TriStateMatrix target = new TriStateMatrix(expected.Width); PositioninngPatternBuilder.EmbedBasicPatterns(version, target); target.TryEmbedCodewords(dcList); expected.AssertEquals(target); }
internal override BitList GetDataBits(string content) { BitList dataBits = new BitList(); int contentLength = content.Length; for (int i = 0; i < contentLength; i += 2) { int groupLength = Math.Min(2, contentLength-i); int value = GetAlphaNumValue(content, i, groupLength); int bitCount = GetBitCountByGroupLength(groupLength); dataBits.Add(value, bitCount); } return dataBits; }
/// <summary> /// Encodes the specified content. /// </summary> /// <param name="content">The content.</param> /// <param name="ecLevel">The ec level.</param> /// <returns></returns> /// <remarks></remarks> internal static EncodationStruct Encode(string content, ErrorCorrectionLevel ecLevel) { RecognitionStruct recognitionResult = InputRecognise.Recognise(content); EncoderBase encoderBase = CreateEncoder(recognitionResult.Mode, recognitionResult.EncodingName); BitList encodeContent = encoderBase.GetDataBits(content); int encodeContentLength = encodeContent.Count; VersionControlStruct vcStruct = VersionControl.InitialSetup(encodeContentLength, recognitionResult.Mode, ecLevel, recognitionResult.EncodingName); var dataCodewords = new BitList(); //Eci header if (vcStruct.isContainECI && vcStruct.ECIHeader != null) dataCodewords.Add(vcStruct.ECIHeader); //Header dataCodewords.Add(encoderBase.GetModeIndicator()); int numLetter = recognitionResult.Mode == Mode.EightBitByte ? encodeContentLength >> 3 : content.Length; dataCodewords.Add(encoderBase.GetCharCountIndicator(numLetter, vcStruct.VersionDetail.Version)); //Data dataCodewords.Add(encodeContent); //Terminator Padding dataCodewords.TerminateBites(dataCodewords.Count, vcStruct.VersionDetail.NumDataBytes); int dataCodewordsCount = dataCodewords.Count; if ((dataCodewordsCount & 0x7) != 0) throw new ArgumentException("data codewords is not byte sized."); else if (dataCodewordsCount >> 3 != vcStruct.VersionDetail.NumDataBytes) { throw new ArgumentException("datacodewords num of bytes not equal to NumDataBytes for current version"); } var encStruct = new EncodationStruct(vcStruct); encStruct.Mode = recognitionResult.Mode; encStruct.DataCodewords = dataCodewords; return encStruct; }
internal override BitList GetDataBits(string content) { BitList dataBits = new BitList(); int contentLength = content.Length; for (int i = 0; i < contentLength; i += 2) { int groupLength = Math.Min(2, contentLength - i); int value = GetAlphaNumValue(content, i, groupLength); int bitCount = GetBitCountByGroupLength(groupLength); dataBits.Add(value, bitCount); } return(dataBits); }
public void Test009() { var bs1 = new BitList { true, false, true }; var bs2 = new BitArray(new[] { true, false, true, true, }); bs1.Add(bs2); Assert.AreEqual("1101101", string.Join("", bs1.Reverse().Select(a => a ? "1" : "0"))); }
internal static EncodationStruct Encode(IEnumerable<byte> content, ErrorCorrectionLevel eclevel) { EncoderBase encoderBase = CreateEncoder(Mode.EightBitByte, QRCodeConstantVariable.DefaultEncoding); BitList encodeContent = new BitList(content); int encodeContentLength = encodeContent.Count; VersionControlStruct vcStruct = VersionControl.InitialSetup(encodeContentLength, Mode.EightBitByte, eclevel, QRCodeConstantVariable.DefaultEncoding); BitList dataCodewords = new BitList(); //Eci header if (vcStruct.isContainECI && vcStruct.ECIHeader != null) dataCodewords.Add(vcStruct.ECIHeader); //Header dataCodewords.Add(encoderBase.GetModeIndicator()); int numLetter = encodeContentLength >> 3; dataCodewords.Add(encoderBase.GetCharCountIndicator(numLetter, vcStruct.VersionDetail.Version)); //Data dataCodewords.Add(encodeContent); //Terminator Padding dataCodewords.TerminateBites(dataCodewords.Count, vcStruct.VersionDetail.NumDataBytes); int dataCodewordsCount = dataCodewords.Count; if ((dataCodewordsCount & 0x7) != 0) throw new ArgumentException("data codewords is not byte sized."); else if (dataCodewordsCount >> 3 != vcStruct.VersionDetail.NumDataBytes) { throw new ArgumentException("datacodewords num of bytes not equal to NumDataBytes for current version"); } EncodationStruct encStruct = new EncodationStruct(vcStruct); encStruct.Mode = Mode.EightBitByte; encStruct.DataCodewords = dataCodewords; return encStruct; }
internal BitList GetDataBitsByByteArray(byte[] encodeContent, string encodingName) { BitList dataBits = new BitList(); //Current plan for UTF8 support is put Byte order Mark in front of content byte. //Also include ECI header before encoding header. Which will be add with encoding header. if (encodingName == "utf-8") { byte[] utf8BOM = QRCodeConstantVariable.UTF8ByteOrderMark; int utf8BOMLength = utf8BOM.Length; for (int index = 0; index < utf8BOMLength; index++) { dataBits.Add(utf8BOM[index], EIGHT_BIT_BYTE_BITCOUNT); } } int encodeContentLength = encodeContent.Length; for (int index = 0; index < encodeContentLength; index++) { dataBits.Add(encodeContent[index], EIGHT_BIT_BYTE_BITCOUNT); } return(dataBits); }
public void Test001() { var bs = new BitList(); Assert.AreEqual(0x00, bs.GetByte(0)); bs.Add(true); Assert.AreEqual(0x01, bs.GetByte(0)); bs.Add(true); Assert.AreEqual(0x03, bs.GetByte(0)); bs.Add(true); Assert.AreEqual(0x07, bs.GetByte(0)); bs.Add(true); Assert.AreEqual(0x0f, bs.GetByte(0)); bs.Add(true); Assert.AreEqual(0x1f, bs.GetByte(0)); bs.Add(true); Assert.AreEqual(0x3f, bs.GetByte(0)); bs.Add(true); Assert.AreEqual(0x7f, bs.GetByte(0)); bs.Add(true); Assert.AreEqual(0xff, bs.GetByte(0)); bs.Add(true); Assert.AreEqual(0xff, bs.GetByte(0)); Assert.AreEqual(0x01, bs.GetByte(1)); Assert.AreEqual(9, bs.Count); bs.Clear(); Assert.AreEqual(0, bs.Count); foreach (var _ in bs) { Assert.Fail(); } foreach (var _ in bs.ToBytes()) { Assert.Fail(); } }
internal BitList GetDataBitsByByteArray(byte[] encodeContent, int contentLength) { BitList dataBits = new BitList(); int bytesLength = encodeContent.Length; if(bytesLength == contentLength*2) { for(int i = 0; i < bytesLength; i += 2) { int encoded = ConvertShiftJIS(encodeContent[i], encodeContent[i+1]); dataBits.Add(encoded, KANJI_BITCOUNT); } } else throw new ArgumentOutOfRangeException("Each char must be two byte length"); return dataBits; }
internal BitList GetDataBitsByByteArray(byte[] encodeContent, int contentLength) { BitList dataBits = new BitList(); int bytesLength = encodeContent.Length; if (bytesLength == contentLength * 2) { for (int i = 0; i < bytesLength; i += 2) { int encoded = ConvertShiftJIS(encodeContent[i], encodeContent[i + 1]); dataBits.Add(encoded, KANJI_BITCOUNT); } } else { throw new ArgumentOutOfRangeException("Each char must be two byte length"); } return(dataBits); }
public void PerformanceTest() { Random randomizer = new Random(); BitVector dataCodewordsV = GenerateDataCodewords(s_vcInfo.NumDataBytes, randomizer); BitList dataCodewordsL = new BitList(); dataCodewordsL.Add(dataCodewordsV); Stopwatch sw = new Stopwatch(); int timesofTest = 1000; string[] timeElapsed = new string[2]; sw.Start(); for(int i = 0; i < timesofTest; i++) { ECGenerator.FillECCodewords(dataCodewordsL, s_vcInfo); } sw.Stop(); timeElapsed[0] = sw.ElapsedMilliseconds.ToString(); sw.Reset(); sw.Start(); for(int i = 0; i < timesofTest; i++) { BitVector finalBits = new BitVector(); EncoderInternal.interleaveWithECBytes(dataCodewordsV, s_vcInfo.NumTotalBytes, s_vcInfo.NumDataBytes, s_vcInfo.NumECBlocks, finalBits); } sw.Stop(); timeElapsed[1] = sw.ElapsedMilliseconds.ToString(); Assert.Pass("ErrorCorrection performance {0} Tests~ QrCode.Net: {1} ZXing: {2}", timesofTest, timeElapsed[0], timeElapsed[1]); }
public void Test002() { var bs = new BitList(); foreach (var _ in bs.ToByteArray()) { Assert.Fail(); } foreach (var _ in Collection.Seq(32)) { bs.Add(true); } for (var i = 0; i < 32; i++) { Assert.AreEqual(true, bs.Check(i)); } Assert.AreEqual(0b11111111_11111111_11111111_11111111u, BitConverter.ToUInt32(bs.ToByteArray(), 0)); bs[10] = false; Assert.AreEqual(0b11111111_11111111_11111011_11111111u, BitConverter.ToUInt32(bs.ToByteArray(), 0)); bs[20] = false; Assert.AreEqual(0b11111111_11101111_11111011_11111111u, BitConverter.ToUInt32(bs.ToByteArray(), 0)); bs[30] = false; Assert.AreEqual(0b10111111_11101111_11111011_11111111u, BitConverter.ToUInt32(bs.ToByteArray(), 0)); bs[31] = false; Assert.AreEqual(0b00111111_11101111_11111011_11111111u, BitConverter.ToUInt32(bs.ToByteArray(), 0)); bs.Set(0, false); Assert.AreEqual(0b00111111_11101111_11111011_11111110u, BitConverter.ToUInt32(bs.ToByteArray(), 0)); bs[10] = true; Assert.AreEqual(0b00111111_11101111_11111111_11111110u, BitConverter.ToUInt32(bs.ToByteArray(), 0)); bs[20] = true; Assert.AreEqual(0b00111111_11111111_11111111_11111110u, BitConverter.ToUInt32(bs.ToByteArray(), 0)); bs[30] = true; Assert.AreEqual(0b01111111_11111111_11111111_11111110u, BitConverter.ToUInt32(bs.ToByteArray(), 0)); bs[31] = true; Assert.AreEqual(0b11111111_11111111_11111111_11111110u, BitConverter.ToUInt32(bs.ToByteArray(), 0)); bs.Set(0, true); Assert.AreEqual(0b11111111_11111111_11111111_11111111u, BitConverter.ToUInt32(bs.ToByteArray(), 0)); }
private static BitList GetFormatInfoBits(ErrorCorrectionLevel errorlevel, Pattern pattern) { int formatInfo = (int)pattern.MaskPatternType; //Pattern bits length = 3 formatInfo |= GetErrorCorrectionIndicatorBits(errorlevel) << 3; int bchCode = BCHCalculator.CalculateBCH(formatInfo, s_FormatInfoPoly); //bchCode length = 10 formatInfo = (formatInfo << 10) | bchCode; //xor maskPattern formatInfo ^= s_FormatInfoMaskPattern; BitList resultBits = new BitList(); resultBits.Add(formatInfo, 15); if(resultBits.Count != 15) throw new Exception("FormatInfoBits length is not 15"); else return resultBits; }
public void PerformanceTest() { Random randomizer = new Random(); BitVector dataCodewordsV = GenerateDataCodewords(s_vcInfo.NumDataBytes, randomizer); BitList dataCodewordsL = new BitList(); dataCodewordsL.Add(dataCodewordsV); Stopwatch sw = new Stopwatch(); int timesofTest = 1000; string[] timeElapsed = new string[2]; sw.Start(); for (int i = 0; i < timesofTest; i++) { ECGenerator.FillECCodewords(dataCodewordsL, s_vcInfo); } sw.Stop(); timeElapsed[0] = sw.ElapsedMilliseconds.ToString(); sw.Reset(); sw.Start(); for (int i = 0; i < timesofTest; i++) { BitVector finalBits = new BitVector(); EncoderInternal.interleaveWithECBytes(dataCodewordsV, s_vcInfo.NumTotalBytes, s_vcInfo.NumDataBytes, s_vcInfo.NumECBlocks, finalBits); } sw.Stop(); timeElapsed[1] = sw.ElapsedMilliseconds.ToString(); Assert.Pass("ErrorCorrection performance {0} Tests~ QrCode.Net: {1} ZXing: {2}", timesofTest, timeElapsed[0], timeElapsed[1]); }
public void CopyToShouldWork() { const int ByteCount = 1927; const int BoolCount = ByteCount * 8; const int ArrayOffset = 3871; byte[] someStuff = CryptographicRandomGenerator.GetBuffer(ByteCount); var bl = new BitList(); foreach (byte val in someStuff) { for (int j = 0; j < 8; ++j) { bl.Add((val & (1 << j)) > 0); } } bool[] vals = new bool[BoolCount + ArrayOffset]; ((IList <bool>)bl).CopyTo(vals, ArrayOffset); Assert.Equal(bl, vals.Skip(ArrayOffset)); }
public void ToBitArrayShouldWork() { const int ByteCount = 1927; byte[] someStuff = CryptographicRandomGenerator.GetBuffer(ByteCount); var bl = new BitList(); foreach (byte val in someStuff) { for (int j = 0; j < 8; ++j) { bl.Add((val & (1 << j)) > 0); } } var ba = bl.ToBitArray(); Assert.Equal(bl.Count, ba.Length); for (int i = 0; i < bl.Count; ++i) { Assert.Equal(bl[i], ba[i]); } }
internal BitList GetDataBitsByByteArray(byte[] encodeContent, string encodingName) { BitList dataBits = new BitList(); //Current plan for UTF8 support is put Byte order Mark in front of content byte. //Also include ECI header before encoding header. Which will be add with encoding header. if(encodingName == "utf-8") { byte[] utf8BOM = QRCodeConstantVariable.UTF8ByteOrderMark; int utf8BOMLength = utf8BOM.Length; for(int index = 0; index < utf8BOMLength; index++) { dataBits.Add(utf8BOM[index], EIGHT_BIT_BYTE_BITCOUNT); } } int encodeContentLength = encodeContent.Length; for(int index = 0; index < encodeContentLength; index++) { dataBits.Add(encodeContent[index], EIGHT_BIT_BYTE_BITCOUNT); } return dataBits; }
/// <summary> /// Generate error correction blocks. Then out put with codewords BitList /// ISO/IEC 18004/2006 P45, 46. Chapter 6.6 Constructing final message codewords sequence. /// </summary> /// <param name="dataCodewords">Datacodewords from DataEncodation.DataEncode</param> /// <param name="numTotalBytes">Total number of bytes</param> /// <param name="numDataBytes">Number of data bytes</param> /// <param name="numECBlocks">Number of Error Correction blocks</param> /// <returns>codewords BitList contain datacodewords and ECCodewords</returns> internal static BitList FillECCodewords(BitList dataCodewords, VersionDetail vd) { List <byte> dataCodewordsByte = dataCodewords.List; int ecBlockGroup2 = vd.ECBlockGroup2; int ecBlockGroup1 = vd.ECBlockGroup1; int numDataBytesGroup1 = vd.NumDataBytesGroup1; int numDataBytesGroup2 = vd.NumDataBytesGroup2; int ecBytesPerBlock = vd.NumECBytesPerBlock; int dataBytesOffset = 0; byte[][] dByteJArray = new byte[vd.NumECBlocks][]; byte[][] ecByteJArray = new byte[vd.NumECBlocks][]; GaloisField256 gf256 = GaloisField256.QRCodeGaloisField; GeneratorPolynomial generator = new GeneratorPolynomial(gf256); for (int blockID = 0; blockID < vd.NumECBlocks; blockID++) { if (blockID < ecBlockGroup1) { dByteJArray[blockID] = new byte[numDataBytesGroup1]; for (int index = 0; index < numDataBytesGroup1; index++) { dByteJArray[blockID][index] = dataCodewordsByte[dataBytesOffset + index]; } dataBytesOffset += numDataBytesGroup1; } else { dByteJArray[blockID] = new byte[numDataBytesGroup2]; for (int index = 0; index < numDataBytesGroup2; index++) { dByteJArray[blockID][index] = dataCodewordsByte[dataBytesOffset + index]; } dataBytesOffset += numDataBytesGroup2; } ecByteJArray[blockID] = ReedSolomonEncoder.Encode(dByteJArray[blockID], ecBytesPerBlock, generator); } if (vd.NumDataBytes != dataBytesOffset) { throw new ArgumentException("Data bytes does not match offset"); } BitList codewords = new BitList(); int maxDataLength = ecBlockGroup1 == vd.NumECBlocks ? numDataBytesGroup1 : numDataBytesGroup2; for (int dataID = 0; dataID < maxDataLength; dataID++) { for (int blockID = 0; blockID < vd.NumECBlocks; blockID++) { if (!(dataID == numDataBytesGroup1 && blockID < ecBlockGroup1)) { codewords.Add((int)dByteJArray[blockID][dataID], 8); } } } for (int ECID = 0; ECID < ecBytesPerBlock; ECID++) { for (int blockID = 0; blockID < vd.NumECBlocks; blockID++) { codewords.Add((int)ecByteJArray[blockID][ECID], 8); } } if (vd.NumTotalBytes != codewords.Count >> 3) { throw new ArgumentException(string.Format("total bytes: {0}, actual bits: {1}", vd.NumTotalBytes, codewords.Count)); } return(codewords); }
/// <remarks>ISO/IEC 18004:2006 Chapter 6.4.2 Page 24.</remarks> internal BitList GetECIHeader(string encodingName) { int eciValue = GetECIValueByName(encodingName); BitList dataBits = new BitList(); dataBits.Add(ECIMode, ECIIndicatorNumBits); int eciAssignmentByte = NumOfCodewords(eciValue); //Number of bits = Num codewords indicator + codeword value = Number of codewords * 8 //Chapter 6.4.2.1 ECI Designator ISOIEC 18004:2006 Page 24 int eciAssignmentBits = 0; switch (eciAssignmentByte) { case 1: //Indicator = 0. Page 24. Chapter 6.4.2.1 dataBits.Add((int)ECICodewordsLength.one, 1); eciAssignmentBits = eciAssignmentByte * 8 - 1; break; case 2: //Indicator = 10. Page 24. Chapter 6.4.2.1 dataBits.Add((int)ECICodewordsLength.two, 2); eciAssignmentBits = eciAssignmentByte * 8 - 2; break; case 3: //Indicator = 110. Page 24. Chapter 6.4.2.1 dataBits.Add((int)ECICodewordsLength.three, 3); eciAssignmentBits = eciAssignmentByte * 8 - 3; break; default: throw new InvalidOperationException("Assignment Codewords should be either 1, 2 or 3"); } dataBits.Add(eciValue, eciAssignmentBits); return dataBits; }
private void TestBitListWith(int size, int seed) { BitList blist = new BitList(); List <bool> list = new List <bool>(); Random r = new Random(seed); for (int i = 0; i < size; i++) { bool b = r.NextDouble() < 0.5; blist.Add(b); list.Add(b); AssertListEquals(list, blist); } for (int i = size - 1; i >= 0; i -= 3) { blist.RemoveAt(i); list.RemoveAt(i); AssertListEquals(list, blist); if (i % 4 == 0) { bool b = r.NextDouble() < 0.5; int index = r.Next(100); blist.Insert(index, b); list.Insert(index, b); AssertListEquals(list, blist); } } for (int i = 0; i < blist.Count; i++) { bool b = r.NextDouble() < 0.5; int index = r.Next(blist.Count); blist[index] = b; list[index] = b; AssertListEquals(list, blist); } for (int i = 0; i < size; i++) { bool b = r.NextDouble() < 0.5; blist.Add(b); list.Add(b); AssertListEquals(list, blist); } bool[] arrayA = new bool[list.Count + 2]; bool[] arrayB = new bool[blist.Count + 2]; Assert.AreEqual(arrayA.Length, arrayB.Length); list.CopyTo(arrayA, 2); blist.CopyTo(arrayB, 2); for (int i = 0; i < arrayA.Length; i++) { Assert.AreEqual(arrayA[i], arrayB[i]); } for (int i = list.Count - 1; i >= 0; i--) { blist.RemoveAt(i); list.RemoveAt(i); AssertListEquals(list, blist); } }
/// <summary> /// Returns bit representation of <see cref="Mode"/> value. /// </summary> /// <returns></returns> /// <remarks>See Chapter 8.4 Data encodation, Table 2 — Mode indicators</remarks> internal BitList GetModeIndicator() { BitList modeIndicatorBits = new BitList(); modeIndicatorBits.Add((int)this.Mode, 4); return modeIndicatorBits; }
public void TestBitList() { BitList list = new BitList(); for (int i = 0; i < 1025; i++) { list.Add(true); } Assert.AreEqual(0, list.IndexOf(true)); Assert.AreEqual(-1, list.IndexOf(false)); list.Add(false); Assert.AreEqual(0, list.IndexOf(true)); Assert.AreEqual(1025, list.IndexOf(false)); Assert.IsTrue(list.Remove(false)); Assert.AreEqual(0, list.IndexOf(true)); Assert.AreEqual(-1, list.IndexOf(false)); try { var x = new bool[1024]; list.CopyTo(x, 0); Assert.Fail(); } catch (InvalidOperationException) { } try { var x = new bool[1026]; list.CopyTo(x, 3); Assert.Fail(); } catch (InvalidOperationException) { } var array = new bool[1025]; list.CopyTo(array, 0); int index = 0; foreach (bool value in array) { Assert.AreEqual(list[index++], value); } Assert.AreEqual(1025, list.Count); for (int i = 0; i < 1025; i++) { list.RemoveAt(list.Count / 2); } Assert.AreEqual(0, list.Count); if (list.Remove(false)) { Assert.Fail(); } if (list.Remove(true)) { Assert.Fail(); } try { list.RemoveAt(0); Assert.Fail(); } catch (IndexOutOfRangeException) { } try { list[0] = false; Assert.Fail(); } catch (IndexOutOfRangeException) { } try { var a = list[0]; Assert.Fail(); } catch (IndexOutOfRangeException) { } list.Insert(0, true); if (!list.Remove(true)) { Assert.Fail(); } try { list.Insert(1, true); Assert.Fail(); } catch (IndexOutOfRangeException) { } try { list.CopyTo(null, 0); Assert.Fail(); } catch (NullReferenceException) { } array = new bool[0]; list.CopyTo(array, 0); try { list.CopyTo(array, -1); Assert.Fail(); } catch (IndexOutOfRangeException) { } TestBitListWith(128, 1234); TestBitListWith(1024, 54321); TestBitListWith(256, 11); TestBitListWith(2048, 242); TestBitListWith(1111, 42); BitList blist = new BitList(); blist.Add(false); blist.Add(false); Assert.IsTrue(blist.Contains(false)); Assert.IsFalse(blist.Contains(true)); blist.Add(true); Assert.IsTrue(blist.Contains(false)); Assert.IsTrue(blist.Contains(true)); blist.Clear(); Assert.AreEqual(0, blist.Count); blist.Add(true); blist.Add(true); Assert.IsFalse(blist.Contains(false)); Assert.IsTrue(blist.Contains(true)); blist.Add(false); Assert.IsTrue(blist.Contains(false)); Assert.IsTrue(blist.Contains(true)); blist.Clear(); Assert.AreEqual(0, blist.Count); for (int i = 0; i < 128; i++) { blist.Add(false); } Assert.IsTrue(blist.Contains(false)); Assert.IsFalse(blist.Contains(true)); blist.Add(true); Assert.IsTrue(blist.Contains(false)); Assert.IsTrue(blist.Contains(true)); Assert.AreEqual(129, blist.Count); blist.Remove(true); Assert.AreEqual(128, blist.Count); Assert.IsTrue(blist.Contains(false)); Assert.IsFalse(blist.Contains(true)); blist.Clear(); Assert.AreEqual(0, blist.Count); for (int i = 0; i < 128; i++) { blist.Add(true); } Assert.IsFalse(blist.Contains(false)); Assert.IsTrue(blist.Contains(true)); blist.Add(false); Assert.IsTrue(blist.Contains(false)); Assert.IsTrue(blist.Contains(true)); Assert.AreEqual(129, blist.Count); blist.Remove(false); Assert.AreEqual(128, blist.Count); Assert.IsFalse(blist.Contains(false)); Assert.IsTrue(blist.Contains(true)); }
private static void TerminatorPadding(this BitList mainList, int numBits) { mainList.Add(QRCodeConstantVariable.TerminatorNPaddingBit, numBits); }
public INPUT_LOW() : base() { BitList.Add(0); }
public INPUT_HIGH() : base() { BitList.Add(1); }
public override void Send() { base.Send(); BitList.Add(1); }
/// <summary> /// Convert an image in 2BPP planar (Gameboy) into binary data. /// </summary> /// <param name="image">Image source to convert.</param> /// <param name="parameters">Parameters for the image.</param> /// <returns>Returns image converted in binary data.</returns> public MemoryStream ConvertBack(SKBitmap image, IImagePattern parameters) { if (image == null || parameters == null) { return(null); } var stream = new MemoryStream(); int tileHeight = parameters.TilePattern.Height; int tileWidth = parameters.TilePattern.Width; int numberOfRowsOfTiles = parameters.Height / tileHeight; int numberOfColsOfTiles = parameters.Width / tileWidth; var pixels = image.Pixels; int numberOfTiles = numberOfRowsOfTiles * numberOfColsOfTiles; int numberTotalOfBit = numberOfTiles * parameters.TilePattern.Size * 8; var imageBits = new BitList(); for (var row = 0; row < numberOfRowsOfTiles; row++) { for (var column = 0; column < numberOfColsOfTiles; column++) { var tile = new BitList(); for (var height = 0; height < tileHeight; height++) { for (var width = 0; width < tileWidth; width++) { var color = pixels[(height * parameters.Width) + width + (column * tileWidth) + (row * tileHeight * parameters.Width)]; int bit; if (parameters.Palette.Count > 0) { bit = parameters.Palette.IndexOf(color); } else { bit = this.defaultPalette.IndexOf(color); } var bitString = System.Convert.ToString(bit, 2).PadLeft(2, '0'); var bits = bitString.SplitByLength(1); foreach (var b in bits) { tile.Add(b == "1" ? true : false); } } } if (tile.Count != parameters.TilePattern.Size * 8) { throw new JHacksonException(LocalizationManager.GetMessage("image.tile.incorrectNumberOfBits", row, column, imageBits.Count, numberTotalOfBit)); } imageBits.AddBitList(tile.RearrangeBitsWith2PlanesBack(parameters.TilePattern)); } } if (imageBits.Count != numberTotalOfBit) { throw new JHacksonException(LocalizationManager.GetMessage("image.incorrectBitsImageSize", imageBits.Count, numberTotalOfBit)); } var bytes = imageBits.ToBytes(); stream.Write(bytes, 0, bytes.Length); return(stream); }
/// <summary> /// Generate error correction blocks. Then out put with codewords BitList /// ISO/IEC 18004/2006 P45, 46. Chapter 6.6 Constructing final message codewords sequence. /// </summary> /// <param name="dataCodewords">Datacodewords from DataEncodation.DataEncode</param> /// <param name="vd">The vd.</param> /// <returns>codewords BitList contain datacodewords and ECCodewords</returns> /// <remarks></remarks> internal static BitList FillECCodewords(BitList dataCodewords, VersionDetail vd) { List<byte> dataCodewordsByte = dataCodewords.List; int ecBlockGroup2 = vd.ECBlockGroup2; int ecBlockGroup1 = vd.ECBlockGroup1; int numDataBytesGroup1 = vd.NumDataBytesGroup1; int numDataBytesGroup2 = vd.NumDataBytesGroup2; int ecBytesPerBlock = vd.NumECBytesPerBlock; int dataBytesOffset = 0; var dByteJArray = new byte[vd.NumECBlocks][]; var ecByteJArray = new byte[vd.NumECBlocks][]; GaloisField256 gf256 = GaloisField256.QRCodeGaloisField; var generator = new GeneratorPolynomial(gf256); for (int blockID = 0; blockID < vd.NumECBlocks; blockID++) { if (blockID < ecBlockGroup1) { dByteJArray[blockID] = new byte[numDataBytesGroup1]; for (int index = 0; index < numDataBytesGroup1; index++) { dByteJArray[blockID][index] = dataCodewordsByte[dataBytesOffset + index]; } dataBytesOffset += numDataBytesGroup1; } else { dByteJArray[blockID] = new byte[numDataBytesGroup2]; for (int index = 0; index < numDataBytesGroup2; index++) { dByteJArray[blockID][index] = dataCodewordsByte[dataBytesOffset + index]; } dataBytesOffset += numDataBytesGroup2; } ecByteJArray[blockID] = ReedSolomonEncoder.Encode(dByteJArray[blockID], ecBytesPerBlock, generator); } if (vd.NumDataBytes != dataBytesOffset) throw new ArgumentException("Data bytes does not match offset"); var codewords = new BitList(); int maxDataLength = ecBlockGroup1 == vd.NumECBlocks ? numDataBytesGroup1 : numDataBytesGroup2; for (int dataID = 0; dataID < maxDataLength; dataID++) { for (int blockID = 0; blockID < vd.NumECBlocks; blockID++) { if (!(dataID == numDataBytesGroup1 && blockID < ecBlockGroup1)) codewords.Add(dByteJArray[blockID][dataID], 8); } } for (int ECID = 0; ECID < ecBytesPerBlock; ECID++) { for (int blockID = 0; blockID < vd.NumECBlocks; blockID++) { codewords.Add(ecByteJArray[blockID][ECID], 8); } } if (vd.NumTotalBytes != codewords.Count >> 3) throw new ArgumentException(string.Format("total bytes: {0}, actual bits: {1}", vd.NumTotalBytes, codewords.Count)); return codewords; }
public void Insert_0_count_is_1() { BitList target = new BitList(); target.Add(true); Assert.AreEqual(1, target.Count); }
public override void Add(bool item) { lock (_syncRoot) _list.Add(item); }