Esempio n. 1
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");
        }