Exemple #1
0
        private void DecodeZPlaneStrip(ZPlaneStripData strip)
        {
            _bitStreamManager = new BitStreamManager(strip.ImageData);
            bool finishDecode = false;

            while (!finishDecode)
            {
                byte count = _bitStreamManager.ReadByte();
                if (count > 0)
                {
                    if (BinaryHelper.CheckBitState(count, 7))
                    {                                                   // write the same byte count times
                        count = BinaryHelper.GetBitsFromByte(count, 7); // &= 0x7F;
                        byte b = _bitStreamManager.ReadByte();

                        for (int i = 0; i < count; i++)
                        {
                            if (!CheckEndOfGraphics())
                            {
                                DrawLine(b);
                            }
                        }
                    }
                    else
                    {  // write count bytes as is from the input
                        for (int i = 0; i < count; i++)
                        {
                            if (!CheckEndOfGraphics())
                            {
                                DrawLine(_bitStreamManager.ReadByte());
                            }
                        }
                    }
                }
                //else
                //{
                //    Debugger.Break();
                //}

                if (CheckEndOfGraphics() || _bitStreamManager.EndOfStream)
                {
                    finishDecode = true;
                }
            }
        }
Exemple #2
0
        private ZPlaneStripData EncodeZPlaneStrip()
        {
            var strip     = new ZPlaneStripData();
            var bitStream = new BitStreamManager();

            var linesInformation = new List <LineInformation>();

            _currentLine = 0;
            var currentLineInformation = new LineInformation();

            byte lastLine = GetNextLine();

            if (lastLine == PeekNextLine())
            {
                currentLineInformation.RepeatSameLine = true;
            }

            currentLineInformation.Lines.Add(lastLine);

            //Primeira passagem - Cadastra tudo alternado
            while (_currentLine < (_height))
            {
                byte currentLine = GetNextLine();

                if (currentLine != lastLine)
                {
                    if (currentLineInformation.RepeatSameLine)
                    {
                        //Mudou de REPETIR para NÃO REPETIR
                        linesInformation.Add(currentLineInformation);

                        currentLineInformation = new LineInformation();
                        currentLineInformation.RepeatSameLine = false;
                    }
                }
                else
                {
                    if (!currentLineInformation.RepeatSameLine)
                    {
                        //Mudou de NÃO REPETIR para REPETIR
                        linesInformation.Add(currentLineInformation);

                        currentLineInformation = new LineInformation();
                        currentLineInformation.RepeatSameLine = true;
                    }
                }

                currentLineInformation.Lines.Add(currentLine);

                lastLine = currentLine;
            }
            linesInformation.Add(currentLineInformation);

            //Segunda passagem - verifica a lista de itens não repetidos para ver se o ultimo elemento é um elemento da lista repetida seguinte.
            for (int i = 1; i < linesInformation.Count; i++)
            {
                var previous          = linesInformation[i - 1];
                var previousLastIndex = previous.Lines.Count - 1;
                var current           = linesInformation[i - 1];

                if (!previous.RepeatSameLine && current.RepeatSameLine && previous.Lines[previousLastIndex] == current.Lines[0])
                {
                    //move o item da lista anterior para a lista atual
                    current.Lines.Insert(0, previous.Lines[previousLastIndex]);

                    previous.Lines.RemoveAt(previousLastIndex);
                }
            }

            //Terceira passagem - procura por listas que eventualmente ficaram vazias, e as remove.
            //É importante percorrer a lista ao contrários, caso contrário a lista sera lida errada, além de dar IndexOutOfBounds no final.
            for (int i = linesInformation.Count - 1; i >= 0; i--)
            {
                if (linesInformation[i].Lines.Count == 0)
                {
                    linesInformation.RemoveAt(i);
                }
            }

            //Quarta passagem, divide as listas com mais de 127 itens em varias listas, pois 127 é o limite máximo de 7 bits.
            var finalLinesInformation = new List <LineInformation>();

            foreach (LineInformation lineInformation in linesInformation)
            {
                if (lineInformation.Lines.Count > 127)
                {
                    while (lineInformation.Lines.Count > 127)
                    {
                        var dividedLineInformation = new LineInformation();
                        dividedLineInformation.RepeatSameLine = lineInformation.RepeatSameLine;
                        dividedLineInformation.Lines.AddRange(lineInformation.Lines.Take(127).ToArray());

                        finalLinesInformation.Add(dividedLineInformation);

                        lineInformation.Lines = lineInformation.Lines.Skip(127).ToList();
                    }
                }

                finalLinesInformation.Add(lineInformation);
            }

            //Quinta passagem, grava o BitStream.
            foreach (LineInformation lineInformation in finalLinesInformation)
            {
                if (lineInformation.RepeatSameLine)
                {
                    byte repeatByte = BinaryHelper.Compose2Bytes(1, (byte)lineInformation.Lines.Count, 7);
                    bitStream.AddByte(repeatByte);
                    bitStream.AddByte(lineInformation.Lines[0]);
                }
                else
                {
                    //Verifica se o different line não tinha apenas 1 linha antes de gravar a informação, isso pode acontecer
                    //em situações onde a linha 1 e 2 são A e 3 e 4 são B, por exemplo.
                    byte repeatByte = BinaryHelper.Compose2Bytes(0, (byte)lineInformation.Lines.Count, 7);
                    bitStream.AddByte(repeatByte);
                    foreach (byte differentLine in lineInformation.Lines)
                    {
                        bitStream.AddByte(differentLine);
                    }
                }
            }

            strip.ImageData = bitStream.ToByteArray();
            return(strip);
        }