/// <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; }
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 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 Maze(int width, int height, BitList northWalls, BitList westWalls, long startKey, long endKey) { Width = width; Height = height; _northWalls = northWalls; _westWalls = westWalls; Start = new Cell(this, startKey); End = new Cell(this, endKey); }
/// <summary> /// Create a copy of another maze. /// </summary> /// <param name="other">The other maze</param> public Maze(Maze other) { Width = other.Width; Height = other.Height; Start = new Cell(this, other.Start.Key); End = new Cell(this, other.End.Key); _northWalls = new BitList(other._northWalls); _westWalls = new BitList(other._westWalls); }
/// <summary> /// Create a 2D maze with all the walls set. /// </summary> /// <param name="width">The width of the maze</param> /// <param name="height">The height of the maze</param> public Maze(int width, int height) { Width = width; Height = height; var total = width * height; _northWalls = new BitList(total, true); _westWalls = new BitList(total, true); }
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); }
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; }
/// <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; }
public void Append8BitBytes() { // 0x61, 0x62, 0x63 BitList bits = new BitList(); QREncoderMatrix.Append8BitBytes("abc", bits); Assert.AreEqual("01100001 01100010 01100011", bits.ToString()); // Empty. bits = new BitList(); QREncoderMatrix.Append8BitBytes("", bits); Assert.AreEqual("", bits.ToString()); }
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(); }
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; }
public void Test008() { var bs1 = new BitList { true, false, true }; var bs3 = BitList.Join(bs1, new[] { true, false, true }); Assert.AreEqual("101101", string.Join("", bs3.Reverse().Select(a => a ? "1" : "0"))); }
public override void Send() { if (BitList.Count == PreviousList.Count) { int answer = (BitList[0] == 1) ? 0 : 1; foreach (Node next in NextList) { next.Recieve(answer); } BitList.Clear(); } }
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]); }
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 Test022() { var bs = new BitList { true, false, false, true, true, false, false, true, true, }; bs.AddPad(); Assert.AreEqual(16, bs.Count); Assert.AreEqual(0b0000_0001_1001_1001, BitConverter.ToUInt16(bs.ToByteArray())); }
internal static void TryEmbedCodewords(this TriStateMatrix tsMatrix, BitList codewords) { int sWidth = tsMatrix.Width; int codewordsSize = codewords.Count; int bitIndex = 0; int directionUp = -1; int x = sWidth - 1; int y = sWidth - 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) { continue; } else { bool bit; if(bitIndex < codewordsSize) { bit = codewords[bitIndex]; bitIndex++; } else bit = false; tsMatrix[xPos, y, MatrixStatus.Data] = bit; } } y = NextY(y, directionUp); } directionUp = ChangeDirection(directionUp); y = NextY(y, directionUp); x -= 2; } if(bitIndex != codewordsSize) throw new Exception(string.Format("Not all bits from codewords consumed by matrix: {0} / {1}", bitIndex, codewordsSize)); }
public static IBarcodeIntCS Encode(string content, bool includeChecksum = true, bool gs1ModeEnabled = false) { if (content == null) { throw new ArgumentNullException(nameof(content)); } if (gs1ModeEnabled) { content = Gs1Encoder.Encode(content, Constants.FNC1); } char[] contentChars = content.ToCharArray(); if (contentChars.Length <= 0 || contentChars.Length > 80) { throw new ArgumentException($"Content length should be between 1 and 80 but got {contentChars.Length}", nameof(content)); } BitList?idxList = GetCodeIndexList(contentChars); if (!idxList.HasValue) { throw new InvalidOperationException($"{content} could not be encoded"); } var result = new BitList(); var sum = 0; var i = 0; foreach (var idx in idxList.Value.GetBytes()) { if (i == 0) { sum = idx; } else { sum += i * idx; } result.AddBit(Constants.EncodingTable[idx]); i++; } sum = sum % 103; if (includeChecksum) { result.AddBit(Constants.EncodingTable[sum]); } result.AddBit(Constants.EncodingTable[Constants.StopSymbol]); return(new Base1DCodeIntCS(result, BarcodeType.Code128, content, sum, Constants.Margin)); }
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); }
private static BitMatrix ProcessEncodationResult(EncodationStruct encodeStruct, ErrorCorrectionLevel errorLevel) { BitList codewords = ECGenerator.FillECCodewords(encodeStruct.DataCodewords, encodeStruct.VersionDetail); TriStateMatrix triMatrix = new(encodeStruct.VersionDetail.MatrixWidth); PositioningPatternBuilder.EmbedBasicPatterns(encodeStruct.VersionDetail.Version, triMatrix); triMatrix.EmbedVersionInformation(encodeStruct.VersionDetail.Version); triMatrix.EmbedFormatInformation(errorLevel, new Pattern0()); triMatrix.TryEmbedCodewords(codewords); return(triMatrix.GetLowestPenaltyMatrix(errorLevel)); }
private static BitList VersionInfoBitList(int version) { BitList result = new BitList { { version, S_LengthDataBits }, { 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); }
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(string content, ErrorCorrectionLevel ecLevel) { RecognitionStruct recognitionResult = InputRecognise.Recognise(content); EncoderBase encoderBase = CreateEncoder(recognitionResult.EncodingName); BitList encodeContent = encoderBase.GetDataBits(content); int encodeContentLength = encodeContent.Count; VersionControlStruct vcStruct = VersionControl.InitialSetup(encodeContentLength, ecLevel, recognitionResult.EncodingName); BitList dataCodewords = new BitList(); // Eci header if (vcStruct.IsContainECI && !(vcStruct.ECIHeader is 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($"{nameof(dataCodewords)} is not byte sized."); } else if (dataCodewordsCount >> 3 != vcStruct.VersionDetail.NumDataBytes) { throw new ArgumentException($"{nameof(dataCodewords)} num of bytes not equal to {nameof(vcStruct.VersionDetail.NumDataBytes)} for current version"); } var encStruct = new EncodationStruct(vcStruct) { DataCodewords = dataCodewords }; return(encStruct); }
//protected virtual IEqualityComparer<T> OriginalSubKeyComparer => (subKeyComparer as SubKeyMaskComparer<T>).SubKeyComparer; protected override bool AddSubKeyPosition(ISubKeyMask <T> subKeyMask) { if (subKeyPositions.TryGetValue(subKeyMask.SubKey, out IBitList positionMask)) { positionMask.Set(subKeyMask.Position, true); return(false); } else { positionMask = new BitList(subKeyMask.Position); positionMask.Set(subKeyMask.Position, true); subKeyPositions.Add(subKeyMask.SubKey, positionMask); return(true); } }
public void BuildMatrix() { const int WIDTH = 21, HEIGHT = 21; // From http://www.swetake.com/qr/qr7.html int[] ints = { 32, 65, 205, 69, 41, 220, 46, 128, 236, 42, 159, 74, 221, 244, 169, 239, 150, 138, 70, 237, 85, 224, 96, 74, 219, 61 }; BitList bits = new BitList(); foreach (int i in ints) { bits.AppendBits(i, 8); } QRMatrix matrix = new QRMatrix(WIDTH, HEIGHT); matrix.FormMatrix(bits, QRCorrectionLevel.H, QRVersion.GetVersionByNumber(1), 3); string expected = "1 1 1 1 1 1 1 0 0 1 1 0 0 0 1 1 1 1 1 1 1\r\n" + "1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\r\n" + "1 0 1 1 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 0 1\r\n" + "1 0 1 1 1 0 1 0 0 1 1 0 0 0 1 0 1 1 1 0 1\r\n" + "1 0 1 1 1 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 1\r\n" + "1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 0 0 0 0 0 1\r\n" + "1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\r\n" + "0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0\r\n" + "0 0 1 1 0 0 1 1 1 0 0 1 1 1 1 0 1 0 0 0 0\r\n" + "1 0 1 0 1 0 0 0 0 0 1 1 1 0 0 1 0 1 1 1 0\r\n" + "1 1 1 1 0 1 1 0 1 0 1 1 1 0 0 1 1 1 0 1 0\r\n" + "1 0 1 0 1 1 0 1 1 1 0 0 1 1 1 0 0 1 0 1 0\r\n" + "0 0 1 0 0 1 1 1 0 0 0 0 0 0 1 0 1 1 1 1 1\r\n" + "0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 1 0 1 1\r\n" + "1 1 1 1 1 1 1 0 1 1 1 1 0 0 0 0 1 0 1 1 0\r\n" + "1 0 0 0 0 0 1 0 0 0 0 1 0 1 1 1 0 0 0 0 0\r\n" + "1 0 1 1 1 0 1 0 0 1 0 0 1 1 0 0 1 0 0 1 1\r\n" + "1 0 1 1 1 0 1 0 1 1 0 1 0 0 0 0 0 1 1 1 0\r\n" + "1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 0 0\r\n" + "1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0\r\n" + "1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 0 1 0 0 1 0"; Assert.AreEqual(expected, matrix.ToString()); }
public static QREncoderMatrix Encode(string content, QRCorrectionLevel correctionLevel) { string encoding = DEFAULT_ENCODING; QRMode mode = chooseMode(content, encoding); BitList header = new BitList(); header.AppendBits(mode.ModeSignature, 4); BitList data = new BitList(); AppendBytes(content, mode, data); int provisionalBitsNeeded = header.Size + mode.GetVersionCharacterCount(QRVersion.GetVersionByNumber(1)) + data.Size; QRVersion provisionalVersion = chooseVersion(provisionalBitsNeeded, correctionLevel); int bitsNeeded = header.Size + mode.GetVersionCharacterCount(provisionalVersion) + data.Size; QRVersion version = chooseVersion(bitsNeeded, correctionLevel); BitList headerNData = new BitList(); headerNData.AppendBitList(header); int numLetters = mode == QRMode.BYTE ? data.ByteSize : content.Length; AppendLengthInfo(numLetters, version, mode, headerNData); headerNData.AppendBitList(data); QRVersion.CorrectionBlockSet correctionBlockSet = version.GetBlockSetByLevel(correctionLevel); int dataBytesQty = version.TotalCodewords - correctionBlockSet.TotalCodewords; WriteTerminationSection(dataBytesQty, headerNData); BitList finalBits = MixWithCorrectionBytes(headerNData, version.TotalCodewords, dataBytesQty, correctionBlockSet.TotalQty); int dimension = version.Dimension; QREncoderMatrix matrix = new QREncoderMatrix(dimension, content, correctionLevel, mode, version); int maskPattern = chooseMaskPattern(finalBits, correctionLevel, version, matrix); matrix.MaskPattern = maskPattern; matrix.FormMatrix(finalBits, correctionLevel, version, maskPattern); return(matrix); }
/// <summary> /// Creates a DICOM overlay from a GDI+ Bitmap. /// </summary> /// <param name="ds">Dataset</param> /// <param name="bitmap">Bitmap</param> /// <param name="mask">Color mask for overlay</param> /// <returns>DICOM overlay</returns> public static DicomOverlayData FromBitmap(DicomDataset ds, Bitmap bitmap, Color mask) { ushort group = 0x6000; while (ds.Contains(new DicomTag(group, DicomTag.OverlayBitPosition.Element))) { group += 2; } var overlay = new DicomOverlayData(ds, group); overlay.Type = DicomOverlayType.Graphics; overlay.Rows = bitmap.Height; overlay.Columns = bitmap.Width; overlay.OriginX = 1; overlay.OriginY = 1; overlay.BitsAllocated = 1; overlay.BitPosition = 1; var count = overlay.Rows * overlay.Columns / 8; if ((overlay.Rows * overlay.Columns) % 8 > 0) { count++; } var array = new BitList(); array.Capacity = overlay.Rows * overlay.Columns; int p = 0; for (int y = 0; y < bitmap.Height; y++) { for (int x = 0; x < bitmap.Width; x++, p++) { if (bitmap.GetPixel(x, y).ToArgb() == mask.ToArgb()) { array[p] = true; } } } overlay.Data = EvenLengthBuffer.Create( new MemoryByteBuffer(array.Array)); return(overlay); }
public void AddAndRemoveShouldBeConsistent() { var bl = new BitList { false, false }; Assert.False(bl[0]); Assert.Equal(2, bl.Count); Assert.False(bl.Remove(true)); Assert.Equal(2, bl.Count); Assert.True(bl.Remove(false)); Assert.Equal(1, bl.Count); }
internal static EncodationStruct Encode(string content, ErrorCorrectionLevel ecLevel) { RecognitionStruct recognitionResult = InputRecognise.Recognise(content); EncoderBase encoderBase = CreateEncoder(recognitionResult.EncodingName); BitList encodeContent = encoderBase.GetDataBits(content); int encodeContentLength = encodeContent.Count; VersionControlStruct vcStruct = VersionControl.InitialSetup(encodeContentLength, ecLevel, recognitionResult.EncodingName); BitList dataCodewords = new(); // Eci header if (vcStruct.IsContainECI && vcStruct.ECIHeader is { })
public void ReadNumericTest_BigValue() { // Centésima potência de 2 string numericText = "1267650600228229401496703205376"; var expected = new BitList(); for (int i = 0; i < 100; ++i) { expected.Add(0); } expected.Add(1); var actual = BitList.ReadNumeric(numericText); Assert.AreEqual(expected, actual); }
public static IEnumerable<long> GetPrimesWithErastosthenesUpTill(long max) { long topNumber = max; BitList numbers = new BitList(topNumber, 255); for (int i = 2; i < topNumber; i++) if (numbers.GetBit(i)) { for (int j = i * 2; j < topNumber; j += i) numbers.SetBit(j, false); } for (int i = 1; i < topNumber; i++) if (numbers.GetBit(i)) { yield return i; } }
public static void Append8BitBytes(String content, BitList bits) { byte[] bytes; try { bytes = Encoding.GetEncoding(DEFAULT_ENCODING).GetBytes(content); } catch (Exception) { throw new AzosException(StringConsts.CODE_LOGIC_ERROR + typeof(QREncoderMatrix).Name + ".append8BitBytes: Encoding.GetEncoding(DEFAULT_BYTE_MODE_ENCODING).GetBytes(content)"); } foreach (byte b in bytes) { bits.AppendBits(b, 8); } }
public void AppendOther() { const int SIZE0 = 13, SIZE1 = 33; BitList bits0 = new BitList(); for (int i = 0; i < SIZE0; i++) bits0.AppendBit(true); BitList bits1 = new BitList(); for (int i = 0; i < SIZE1; i++) bits1.AppendBit(true); bits0.AppendBitList(bits1); Assert.AreEqual( SIZE0 + SIZE1, bits0.Size); }
public void Encode_ShouldKeepBitCountLow() { // Found on an airline boarding pass. Several stretches of Binary shift are // necessary to keep the bit-count so low. // Arrange const string content = "09 UAG ^160MEUCIQC0sYS/HpKxnBELR1uB85R20OoqqwFGa0q2uEiYgh6utAIgLl1aBVM4EOTQtMQQYH9M2Z3Dp4qnA/fwWuQ+M8L3V8U="; byte[] data = content.Select(x => (byte)x).ToArray(); // Act BitList result = HighLevelEncoding.Encode(data); // Assert result.Length.Should().Be(823); }
public static IBarcodeIntCS Encode(string content, bool includeChecksum, bool fullAsciiMode) { if (content == null) { throw new ArgumentNullException(nameof(content)); } if (fullAsciiMode) { content = Prepare(content); } else if (content.Contains("*")) { throw new InvalidOperationException("Invalid data! Try full ASCII mode"); } char checksum = GetChecksum(content); var data = new StringBuilder("*"); data.Append(content); if (includeChecksum) { data.Append(checksum); } data.Append("*"); var result = new BitList(); var i = 0; foreach (char r in data.ToString()) { if (i++ != 0) { result.AddBit(false); } if (!Constants.EncodingTable.TryGetValue(r, out (int value, bool[] data)info)) { throw new InvalidOperationException("Invalid data! Try full ASCII mode"); } result.AddBit(info.data); } return(new Base1DCodeIntCS(result, BarcodeType.Code39, content, checksum, Constants.Margin)); }
public override void Send() { if (BitList.Count == PreviousList.Count) { int answer = BitList[0] + BitList[1]; if (answer == 2) { answer = 1; } foreach (Node next in NextList) { next.Recieve(answer); } BitList.Clear(); } }
public static QREncoderMatrix Encode(string content, QRCorrectionLevel correctionLevel) { string encoding = DEFAULT_ENCODING; QRMode mode = chooseMode(content, encoding); BitList header = new BitList(); header.AppendBits(mode.ModeSignature, 4); BitList data = new BitList(); AppendBytes(content, mode, data); int provisionalBitsNeeded = header.Size + mode.GetVersionCharacterCount(QRVersion.GetVersionByNumber(1)) + data.Size; QRVersion provisionalVersion = chooseVersion(provisionalBitsNeeded, correctionLevel); int bitsNeeded = header.Size + mode.GetVersionCharacterCount(provisionalVersion) + data.Size; QRVersion version = chooseVersion(bitsNeeded, correctionLevel); BitList headerNData = new BitList(); headerNData.AppendBitList(header); int numLetters = mode == QRMode.BYTE ? data.ByteSize : content.Length; AppendLengthInfo(numLetters, version, mode, headerNData); headerNData.AppendBitList(data); QRVersion.CorrectionBlockSet correctionBlockSet = version.GetBlockSetByLevel(correctionLevel); int dataBytesQty = version.TotalCodewords - correctionBlockSet.TotalCodewords; WriteTerminationSection(dataBytesQty, headerNData); BitList finalBits = MixWithCorrectionBytes(headerNData, version.TotalCodewords, dataBytesQty, correctionBlockSet.TotalQty); int dimension = version.Dimension; QREncoderMatrix matrix = new QREncoderMatrix( dimension, content, correctionLevel, mode, version); int maskPattern = chooseMaskPattern(finalBits, correctionLevel, version, matrix); matrix.MaskPattern = maskPattern; matrix.FormMatrix(finalBits, correctionLevel, version, maskPattern); return matrix; }
internal static EncodationStruct Encode(IEnumerable <byte> content, ErrorCorrectionLevel eclevel) { EncoderBase encoderBase = CreateEncoder(QRCodeConstantVariable.DefaultEncoding); BitList encodeContent = new BitList(content); int encodeContentLength = encodeContent.Count; VersionControlStruct vcStruct = VersionControl.InitialSetup(encodeContentLength, eclevel, QRCodeConstantVariable.DefaultEncoding); BitList dataCodewords = new BitList(); //Eci header if (vcStruct.IsContainECI && !(vcStruct.ECIHeader is 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"); } var encStruct = new EncodationStruct(vcStruct) { DataCodewords = dataCodewords }; return(encStruct); }
public void AppendBit() { BitList bits = new BitList(); Assert.AreEqual(0, bits.Size); bits.AppendBit(true); Assert.AreEqual(1, bits.Size); Assert.AreEqual(true, bits[0]); bits.AppendBit(false); bits.AppendBit(true); Assert.AreEqual(3, bits.Size); Assert.AreEqual(false, bits[1]); Assert.AreEqual(true, bits[2]); }
/// <summary> /// Create a truth table of a given length /// e.g. if length == 2 the output would be as following /// | a | b | /// | 0 | 0 | /// | 0 | 1 | /// | 1 | 0 | /// | 1 | 1 | /// </summary> /// <param name="length"></param> /// <returns></returns> private IEnumerable <BitList> GenerateCombinations(int length) { var output = new List <BitList>(); var numberOfCombinations = (int)Math.Pow(2, length); for (var bitList = new BitList(); bitList < numberOfCombinations; bitList++) { if (bitList.Count != length) { var paddingNeeded = length - bitList.Count; bitList.AddRange(paddingNeeded.BitListOfLength()); } output.Add(bitList); } return(output); }
public void Dispose() { MarketWindowValue = null; _1ItemlistLabelValue = null; CloseButtonValue = null; BuyButtonValue = null; ItemlistGroupValue = null; ItemListValue = null; ItemGroupValue = null; ItemnameLabelValue = null; ItempriceLabelValue = null; ItemimagePictureValue = null; DescriptionGroupValue = null; DescriptionTextareaValue = null; DescriptionLabelValue = null; PriceLabelValue = null; NameLabelValue = null; }
private static void PadeCodewords(this BitList mainList, int numOfPadeCodewords) { if (numOfPadeCodewords < 0) { throw new ArgumentException("Num of pade codewords less than Zero"); } for (int numOfP = 1; numOfP <= numOfPadeCodewords; numOfP++) { if (numOfP % 2 == 1) { mainList.Add(QRCodeConstantVariable.PadeCodewordsOdd, NumBitsForByte); } else { mainList.Add(QRCodeConstantVariable.PadeCodewordsEven, NumBitsForByte); } } }
private static int chooseMaskPattern(BitList bits, QRCorrectionLevel correctionLevel, QRVersion version, QRMatrix matrix) { int minPenalty = Int32.MaxValue; // Assume the lowest possible penalty int bestMaskPattern = -1; // Calculate all mask paterns to find the pattern with minimum possible penalty for (int maskPattern = 0; maskPattern < MASK_PATTERNS_QTY; maskPattern++) { matrix.FormMatrix(bits, correctionLevel, version, maskPattern); int penalty = matrix.GetMaskPenalty(); if (penalty < minPenalty) { minPenalty = penalty; bestMaskPattern = maskPattern; } } return(bestMaskPattern); }
/// <summary> /// Determine which version to use /// </summary> /// <param name="dataBitsLength">Number of bits for encoded content</param> /// <param name="mode">The mode.</param> /// <param name="level">The level.</param> /// <param name="encodingName">Encoding name for EightBitByte</param> /// <returns>VersionDetail and ECI</returns> /// <remarks></remarks> internal static VersionControlStruct InitialSetup(int dataBitsLength, Mode mode, ErrorCorrectionLevel level, string encodingName) { int totalDataBits = dataBitsLength; bool containECI = false; var eciHeader = new BitList(); //Check ECI header if (mode == Mode.EightBitByte) { if (encodingName != DEFAULT_ENCODING && encodingName != QRCodeConstantVariable.UTF8Encoding) { var eciSet = new ECISet(ECISet.AppendOption.NameToValue); int eciValue = eciSet.GetECIValueByName(encodingName); totalDataBits += ECISet.NumOfECIHeaderBits(eciValue); eciHeader = eciSet.GetECIHeader(encodingName); containECI = true; } } //Determine which version group it belong to int searchGroup = DynamicSearchIndicator(totalDataBits, level, mode); int[] charCountIndicator = CharCountIndicatorTable.GetCharCountIndicatorSet(mode); totalDataBits += (NUM_BITS_MODE_INDICATOR + charCountIndicator[searchGroup]); int lowerSearchBoundary = searchGroup == 0 ? 1 : (VERSION_GROUP[searchGroup - 1] + 1); int higherSearchBoundary = VERSION_GROUP[searchGroup]; //Binary search to find proper version int versionNum = BinarySearch(totalDataBits, level, lowerSearchBoundary, higherSearchBoundary); VersionControlStruct vcStruct = FillVCStruct(versionNum, level, encodingName); vcStruct.isContainECI = containECI; vcStruct.ECIHeader = eciHeader; return(vcStruct); }
/// <summary> /// Determine which version to use /// </summary> /// <param name="dataBitsLength">Number of bits for encoded content</param> /// <param name="mode">The mode.</param> /// <param name="level">The level.</param> /// <param name="encodingName">Encoding name for EightBitByte</param> /// <returns>VersionDetail and ECI</returns> /// <remarks></remarks> internal static VersionControlStruct InitialSetup(int dataBitsLength, Mode mode, ErrorCorrectionLevel level, string encodingName) { int totalDataBits = dataBitsLength; bool containECI = false; var eciHeader = new BitList(); //Check ECI header if (mode == Mode.EightBitByte) { if (encodingName != DEFAULT_ENCODING && encodingName != QRCodeConstantVariable.UTF8Encoding) { var eciSet = new ECISet(ECISet.AppendOption.NameToValue); int eciValue = eciSet.GetECIValueByName(encodingName); totalDataBits += ECISet.NumOfECIHeaderBits(eciValue); eciHeader = eciSet.GetECIHeader(encodingName); containECI = true; } } //Determine which version group it belong to int searchGroup = DynamicSearchIndicator(totalDataBits, level, mode); int[] charCountIndicator = CharCountIndicatorTable.GetCharCountIndicatorSet(mode); totalDataBits += (NUM_BITS_MODE_INDICATOR + charCountIndicator[searchGroup]); int lowerSearchBoundary = searchGroup == 0 ? 1 : (VERSION_GROUP[searchGroup - 1] + 1); int higherSearchBoundary = VERSION_GROUP[searchGroup]; //Binary search to find proper version int versionNum = BinarySearch(totalDataBits, level, lowerSearchBoundary, higherSearchBoundary); VersionControlStruct vcStruct = FillVCStruct(versionNum, level, encodingName); vcStruct.isContainECI = containECI; vcStruct.ECIHeader = eciHeader; return vcStruct; }
/// <summary> /// Gets the data bits by byte array. /// </summary> /// <param name="encodeContent">Content of the encode.</param> /// <param name="contentLength">Length of the content.</param> /// <returns></returns> /// <remarks></remarks> internal BitList GetDataBitsByByteArray(byte[] encodeContent, int contentLength) { var 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 BuildMatrix() { const int WIDTH = 21, HEIGHT = 21; // From http://www.swetake.com/qr/qr7.html int[] ints = { 32, 65, 205, 69, 41, 220, 46, 128, 236, 42, 159, 74, 221, 244, 169, 239, 150, 138, 70, 237, 85, 224, 96, 74, 219 , 61}; BitList bits = new BitList(); foreach (int i in ints) bits.AppendBits(i, 8); QRMatrix matrix = new QRMatrix(WIDTH, HEIGHT); matrix.FormMatrix(bits, QRCorrectionLevel.H, QRVersion.GetVersionByNumber(1), 3); string expected = "1 1 1 1 1 1 1 0 0 1 1 0 0 0 1 1 1 1 1 1 1\r\n" + "1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\r\n" + "1 0 1 1 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 0 1\r\n" + "1 0 1 1 1 0 1 0 0 1 1 0 0 0 1 0 1 1 1 0 1\r\n" + "1 0 1 1 1 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 1\r\n" + "1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 0 0 0 0 0 1\r\n" + "1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\r\n" + "0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0\r\n" + "0 0 1 1 0 0 1 1 1 0 0 1 1 1 1 0 1 0 0 0 0\r\n" + "1 0 1 0 1 0 0 0 0 0 1 1 1 0 0 1 0 1 1 1 0\r\n" + "1 1 1 1 0 1 1 0 1 0 1 1 1 0 0 1 1 1 0 1 0\r\n" + "1 0 1 0 1 1 0 1 1 1 0 0 1 1 1 0 0 1 0 1 0\r\n" + "0 0 1 0 0 1 1 1 0 0 0 0 0 0 1 0 1 1 1 1 1\r\n" + "0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 1 0 1 1\r\n" + "1 1 1 1 1 1 1 0 1 1 1 1 0 0 0 0 1 0 1 1 0\r\n" + "1 0 0 0 0 0 1 0 0 0 0 1 0 1 1 1 0 0 0 0 0\r\n" + "1 0 1 1 1 0 1 0 0 1 0 0 1 1 0 0 1 0 0 1 1\r\n" + "1 0 1 1 1 0 1 0 1 1 0 1 0 0 0 0 0 1 1 1 0\r\n" + "1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 0 0\r\n" + "1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0\r\n" + "1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 0 1 0 0 1 0"; Assert.AreEqual( expected, matrix.ToString()); }
public static void AppendBytes(String content, QRMode mode, BitList bits) { if (mode.Equals(QRMode.NUMERIC)) { AppendNumericBytes(content, bits); } else if (mode.Equals(QRMode.ALPHANUMERIC)) { AppendAlphanumericBytes(content, bits); } else if (mode.Equals(QRMode.BYTE)) { Append8BitBytes(content, bits); } else { throw new AzosException(StringConsts.ARGUMENT_ERROR + typeof(QREncoderMatrix).Name + ".appendBytes(mode=NUMERIC|ALPHANUMERIC|BYTE)"); } }
public void AppendAlphanumericBytes() { // A = 10 = 0xa = 001010 in 6 bits BitList bits = new BitList(); QREncoderMatrix.AppendAlphanumericBytes("A", bits); Assert.AreEqual("001010" , bits.ToString()); // AB = 10 * 45 + 11 = 461 = 0x1cd = 00111001101 in 11 bits bits = new BitList(); QREncoderMatrix.AppendAlphanumericBytes("AB", bits); Assert.AreEqual("00111001 101", bits.ToString()); // ABC = "AB" + "C" = 00111001101 + 001100 bits = new BitList(); QREncoderMatrix.AppendAlphanumericBytes("ABC", bits); Assert.AreEqual("00111001 10100110 0" , bits.ToString()); // Empty. bits = new BitList(); QREncoderMatrix.AppendAlphanumericBytes("", bits); Assert.AreEqual("" , bits.ToString()); }
public void AppendBytes() { // Should use appendNumericBytes. // 1 = 01 = 0001 in 4 bits. BitList bits = new BitList(); QREncoderMatrix.AppendBytes("1", QRMode.NUMERIC, bits); Assert.AreEqual("0001" , bits.ToString()); // Should use appendAlphanumericBytes. // A = 10 = 0xa = 001010 in 6 bits bits = new BitList(); QREncoderMatrix.AppendBytes("A", QRMode.ALPHANUMERIC, bits); Assert.AreEqual("001010" , bits.ToString()); bits = new BitList(); QREncoderMatrix.AppendBytes("abc", QRMode.BYTE, bits); Assert.AreEqual("01100001 01100010 01100011", bits.ToString()); // Anything can be encoded in QRCode.MODE_8BIT_BYTE. QREncoderMatrix.AppendBytes("\0", QRMode.BYTE, bits); }
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]); }
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; }
/// <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 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; }
public void PerformanceTest() { Stopwatch sw = new Stopwatch(); int timesofTest = 1000; string[] timeElapsed = new string[2]; sw.Start(); for(int i = 0; i < timesofTest; i++) { BitList list = new BitList(); list.TerminateBites(0, 400); } sw.Stop(); timeElapsed[0] = sw.ElapsedMilliseconds.ToString(); sw.Reset(); sw.Start(); for(int i = 0; i < timesofTest; i++) { BitVector headerAndDataBits = new BitVector(); //headerAndDataBits.Append(1, 1); EncoderInternal.terminateBits(400, headerAndDataBits); } sw.Stop(); timeElapsed[1] = sw.ElapsedMilliseconds.ToString(); Assert.Pass("Terminator performance {0} Tests~ QrCode.Net: {1} ZXing: {2}", timesofTest, timeElapsed[0], timeElapsed[1]); }
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> /// 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; }