Esempio n. 1
0
        private static void AddBytes(ref List <byte> result, int count, byte val)
        {
            if (count == 1)
            {
                BitWise.SetBit(ref val, 7);
                result.Add(val);
            }
            else
            {
                result.Add(val);
                if (count <= 0x7F)
                {
                    //Always keep first bit as 0
                    result.Add((byte)count);
                }
                else
                {
                    if (count > 0x7FFF)
                    {
                        throw new ArithmeticException("Длина не может превышать 32767");
                    }

                    byte[] _bytes = BitConverter.GetBytes((UInt16)count);
                    BitWise.SetBit(ref _bytes[1], 7);
                    result.Add(_bytes[1]);
                    result.Add(_bytes[0]);
                }
            }
        }
Esempio n. 2
0
        private static byte CreateByte(List <bool> controlBits, int index)
        {
            byte b = 0;

            for (int i = 0; i < 7; i++)
            {
                if (index * 7 + i < controlBits.Count)
                {
                    BitWise.SSetBit(ref b, i, controlBits[index * 7 + i] ? 1 : 0);
                }
            }
            return(b);
        }
Esempio n. 3
0
        public static void Decode(List <byte> bytes, int offset, int count,
                                  Action <bool> bitProceed)
        {
            for (int i = 0; i < count;)
            {
                byte leadB = bytes[offset + i];

                if (BitWise.Bit(leadB, 7) == 1)
                {
                    for (int b = 0; b < 7; b++)
                    {
                        bitProceed(BitWise.Bit(leadB, b) == 1);
                    }
                    i++;
                }
                else
                {
                    byte indexLB = bytes[offset + i + 1];
                    if (BitWise.Bit(indexLB, 7) == 0)
                    {
                        for (int cnt = 0; cnt < indexLB; cnt++)
                        {
                            for (int b = 0; b < 7; b++)
                            {
                                bitProceed(BitWise.Bit(leadB, b) == 1);
                            }
                        }
                        i += 2;
                    }
                    else
                    {
                        byte indexHB = bytes[offset + i + 2];
                        BitWise.ClearBit(ref indexLB, 7);
                        var l = BitConverter.ToUInt16(new byte[] { indexLB, indexHB }, 0);

                        for (int cnt = 0; cnt < l; cnt++)
                        {
                            for (int b = 0; b < 7; b++)
                            {
                                bitProceed(BitWise.Bit(leadB, b) == 1);
                            }
                        }

                        i += 3;
                    }
                }
            }
        }
Esempio n. 4
0
        public override void PostProceed(ref List <byte> result)
        {
            List <byte> copy = new List <byte>();

            copy.AddRange(result);
            int bitCount = 0;

            result = Program.CreateSequence(p =>
            {
                byte b = 0;
                for (int bit = 0; bit < 8; bit++)
                {
                    if (bitCount < copy.Count)
                    {
                        BitWise.SSetBit(ref b, bit, copy[bitCount++]);
                    }
                }
                return(b);
            }, (int)Math.Ceiling(result.Count / 8f)).ToList();
        }
Esempio n. 5
0
        public static List <byte> Encode(List <byte> bytes, int offset, int count)
        {
            List <byte> result = new List <byte>();

            List <bool> controlBits = new List <bool>();

            for (int i = 0; i < count; i++)
            {
                for (int b = 0; b < 8; b++)
                {
                    controlBits.Add(BitWise.Bit(bytes[i + offset], b) == 1);
                }
            }

            var newByteCount = (int)Math.Ceiling(controlBits.Count / 7f);

            List <byte> controlBytes = Program.CreateSequence(p => CreateByte(controlBits, p),
                                                              newByteCount).ToList();

            byte last = controlBytes[0];
            int  cnt  = 0;

            for (int i = 0; i < newByteCount; i++)
            {
                if (last == controlBytes[i])
                {
                    cnt++;
                }
                else
                {
                    AddBytes(ref result, cnt, last);
                    last = controlBytes[i];
                    cnt  = 1;
                }
            }
            AddBytes(ref result, cnt, last);
            return(result);
        }
Esempio n. 6
0
        static public void ToBitmap(List <byte> bytes)
        {
            ColorMode colorMode = (ColorMode)bytes[0];
            ushort    W         = BitConverter.ToUInt16(bytes.ToArray(), 1);
            ushort    H         = BitConverter.ToUInt16(bytes.ToArray(), 3);

            BitmapColor[,] colors = new BitmapColor[W, H];
            BitmapColor baseColor = BitmapColor.CreateColor(colorMode, Color.Empty);
            int         counter   = 0;


            if (colorMode == ColorMode.GrayScale_encoded)
            {
                List <bool> bits = new List <bool>();
                Encoder.Decode(bytes, 5, bytes.Count - 5, p => bits.Add(p));
                var decodeResult = CreateSequence(p =>
                {
                    byte b = 0;
                    for (int i = 0; i < 8; i++)
                    {
                        if (p * 8 + i < bits.Count)
                        {
                            BitWise.SSetBit(ref b, i, bits[p * 8 + i] ? 1 : 0);
                        }
                    }
                    return(b);
                }, (int)Math.Ceiling(bits.Count / 8f)).ToList();

                bytes = new List <byte>();
                bytes.AddRange(new byte[5] {
                    0, 0, 0, 0, 0
                });
                bytes.AddRange(decodeResult);
            }
            else if (colorMode == ColorMode.Binary_encoded)
            {
                List <bool> controlBits = new List <bool>();
                Encoder.Decode(bytes, 5, bytes.Count - 5, p => controlBits.Add(p));
                bytes = new List <byte>();
                bytes.AddRange(new byte[5] {
                    0, 0, 0, 0, 0
                });
                bytes.AddRange(controlBits.Select(p => (byte)(p ? 1 : 0)));
            }
            else if (colorMode == ColorMode.Binary)
            {
                List <bool> controlBits = new List <bool>();

                for (int i = 5; i < bytes.Count; i++)
                {
                    for (int b = 0; b < 8; b++)
                    {
                        controlBits.Add(BitWise.Bit(bytes[i], b) == 1);
                    }
                }

                bytes = new List <byte>();
                bytes.AddRange(new byte[5] {
                    0, 0, 0, 0, 0
                });
                bytes.AddRange(controlBits.Select(p => (byte)(p ? 1 : 0)));
            }

            for (int x = 0; x < W; x++)
            {
                for (int y = 0; y < H; y++)
                {
                    colors[x, y] = BitmapColor.CreateColor(colorMode, Color.Empty);
                    colors[x, y].FromBytes(bytes.Skip(baseColor.BytesPerColor * counter + 5).Take(baseColor.BytesPerColor).ToArray());
                    counter++;
                }
            }

            Bitmap bmp = new Bitmap(W, H);

            for (int x = 0; x < W; x++)
            {
                for (int y = 0; y < H; y++)
                {
                    bmp.SetPixel(x, y, colors[x, y].ToRGB());
                }
            }

            bmp.Save("result_out3.png");
        }