Exemple #1
0
        //precision [0..255]
        public string ParseCorrectBitmap(Bitmap bitmap)
        {
            var drawingR = HexMathHelper.GetDrawingR(_r, _distanceBetweenHexes);
            var width    = bitmap.Width;
            var height   = bitmap.Height;

            var centerPoint             = new PointF(width / 2f, height / 2f);
            var centerHexagonDataString = ParseHexagon(bitmap, centerPoint, drawingR);

            var metaData = BinaryHelper.ToBoolArray(Reverse(centerHexagonDataString));

            var useLargeAlphabet = metaData[0];

            _activeAlphabet = metaData[0] ? _largeAlphabet : _alphabet;

            var imageActiveLayer = 0;

            for (int i = metaData.Length - 1; i >= 1; i--)
            {
                imageActiveLayer += (metaData[i] ? 1 : 0) * (int)Math.Pow(2, metaData.Length - i - 1);
            }

            var sumHexagons = HexMathHelper.GetLayerSumHexagonsCount(imageActiveLayer) - 1;

            var binaryStringBuilder = new StringBuilder(sumHexagons * 6);

            for (int index = sumHexagons - 1; index >= 0; index--)
            {
                var position     = index; //индекс символа в текущем слое
                var currentLayer = 1;     //вычисляем слой буквы

                HexMathHelper.GetItemLayer(ref position, ref currentLayer);

                var hexesInSide = HexMathHelper.GetHexagonsInSide(currentLayer);

                var hexagonPoint = centerPoint;
                HexMathHelper.TranlateToTopRightCorner(ref hexagonPoint, drawingR, currentLayer);

                HexMathHelper.TranslatePointAroundHexagon(ref hexagonPoint, position, hexesInSide, drawingR);

                var hexagonDataString = ParseHexagon(bitmap, hexagonPoint, drawingR);
                binaryStringBuilder.Append(hexagonDataString);
            }

            var binaryString = Reverse(binaryStringBuilder.ToString());

            return(FromBinaryString(binaryString, useLargeAlphabet));
        }
Exemple #2
0
        public Bitmap GenerateBitmap(string content)
        {
            if (content.Length == 0)
            {
                throw new IndexOutOfRangeException("Content Length 0");
            }

            //если хотя бы 1 символ в обычном алфавите отсутствует
            var useLargeAlphabet = content.Any(c => !_alphabet.Contains(c));

            //длина бинарного символа (кол-во бит на символ)
            var binarySymbolLength = GetAlphabetBinaryDigits(useLargeAlphabet);

            var sb = new StringBuilder(content.Length * binarySymbolLength);

            _activeAlphabet = useLargeAlphabet ? _largeAlphabet : _alphabet;
            foreach (var t in content)
            {
                sb.Append(BinaryHelper.ToBinaryString(Array.IndexOf(_activeAlphabet, t), binarySymbolLength));
            }

            var binaryString = sb.ToString();

            if (binaryString.Length > HexMathHelper.GetLayerSumHexagonsCount(32) * 6)
            {
                throw new IndexOutOfRangeException("Message is too long");
            }

            var binaryPieces = BinaryHelper.DivideBy6(binaryString);

            var maxUsedIndex = binaryPieces.Count - 1;
            var maxUsedLayer = 0;

            HexMathHelper.GetItemLayer(ref maxUsedIndex, ref maxUsedLayer);

            var drawingR = HexMathHelper.GetDrawingR(_r, _distanceBetweenHexes);
            var width    = HexMathHelper.GetWidth(maxUsedLayer, _r, _distanceBetweenHexes) + 1;
            var height   = HexMathHelper.GetHeight(maxUsedLayer, _r, _distanceBetweenHexes) + 1;

            var bitmap = new Bitmap((int)width, (int)height);

            var graphics = Graphics.FromImage(bitmap);

            graphics.FillRectangle(Brushes.White, 0, 0, width, height);


            var maxUsedLayerBinaryString = Convert.ToString(maxUsedLayer, 2);

            maxUsedLayerBinaryString = maxUsedLayerBinaryString.PadLeft(5, '0');
            var maxUserLayerBoolArray = BinaryHelper.ToBoolArray(maxUsedLayerBinaryString);

            var centerPoint = new PointF(width / 2f, height / 2f);

            var metaData = new bool[6];

            metaData[0] = useLargeAlphabet;
            for (int i = 0; i < maxUserLayerBoolArray.Length; i++)
            {
                metaData[i + 1] = maxUserLayerBoolArray[i];
            }

            //graphics.DrawLine(Pens.DarkSlateGray, centerX, 0, centerX, height);
            //graphics.DrawLine(Pens.DarkSlateGray, 0, centerY, width, centerY);

            DrawHexagon(centerPoint, _r, graphics, metaData);

            for (var i = 0; i < HexMathHelper.GetLayerSumHexagonsCount(maxUsedLayer) - 1; i++)
            {
                bool[] binaryPiece;
                if (i < binaryPieces.Count)
                {
                    binaryPiece = BinaryHelper.ToBoolArray(binaryPieces[i]);
                }
                else
                {
                    binaryPiece = new[] { false, false, false, false, false, false };
                }

                var position     = i; //индекс символа в текущем слое
                var currentLayer = 1; //вычисляем слой буквы

                HexMathHelper.GetItemLayer(ref position, ref currentLayer);

                var hexesInSide = HexMathHelper.GetHexagonsInSide(currentLayer);

                var hexagonPoint = centerPoint;
                HexMathHelper.TranlateToTopRightCorner(ref hexagonPoint, drawingR, currentLayer);

                HexMathHelper.TranslatePointAroundHexagon(ref hexagonPoint, position, hexesInSide, drawingR);

                //рисуем гексагон
                DrawHexagon(hexagonPoint, _r, graphics, binaryPiece);
            }

            graphics.Dispose();
            return(bitmap);
        }