コード例 #1
0
        public void DecodeTest()
        {
            MockEncoder encoder = new MockEncoder();

            encoder.values.Add(42);
            encoder.values.Add(43);
            encoder.values.Add(256);
            LZW lzw = new LZW(encoder);

            Assert.AreEqual(42, lzw.Decode());
            Assert.AreEqual(43, lzw.Decode());
            Assert.AreEqual(42, lzw.Decode());
            Assert.AreEqual(43, lzw.Decode());
        }
コード例 #2
0
        private byte[] DecodePicture(ref int index, uint length)
        {
            byte bits = _bytes[index++];

            byte[] img = new byte[length - 5];
            Array.Copy(_bytes, index, img, 0, (int)(length - 5));
            index += (int)(length - 5);
            return(RLE.Decode(LZW.Decode(img)));
        }
コード例 #3
0
        public void EncodeDecodeTest()
        {
            MockEncoder encoder = new MockEncoder();
            LZW         lzw     = new LZW(encoder);

            foreach (byte character in "Lorem ipsum dolor sit amet consectetur adepiscig nullam")
            {
                lzw.Encode(character);
            }
            lzw.EncoderFinalize();
            LZW lzwDecoder = new LZW(encoder);

            String output = "";

            foreach (byte character in "Lorem ipsum dolor sit amet consectetur adepiscig nullam")
            {
                byte actual = lzwDecoder.Decode();
                output += (char)actual;
                Assert.AreEqual(character, actual);
            }
        }
コード例 #4
0
 private void Decode(object sender, RoutedEventArgs e)
 {
     resultDecode.Text = LZW.Decode(alphabet, encodeList);
 }
コード例 #5
0
        public GifFile(byte[] buffer)
        {
            // Check for valid GIF-file header
            if (Encoding.UTF8.GetString(buffer, 0, 6) != "GIF87a" && Encoding.UTF8.GetString(buffer, 0, 6) != "GIF89a")
            {
                return;
            }

            int width  = BitConverter.ToUInt16(buffer, 6);
            int height = BitConverter.ToUInt16(buffer, 8);

            // GCT Descriptor
            bool hasGct           = ((buffer[10] >> 7) & 1) == 1;
            byte colourResolution = (byte)(((buffer[10] >> 4) & 7) + 1);
            bool sorted           = ((buffer[10] >> 3) * 1) == 1;
            int  colourCount      = (int)Math.Pow(2, (buffer[10] & 7) + 1);
            byte backgroundIndex  = buffer[11];
            byte aspectRatio      = buffer[12];

            if (aspectRatio != 0)
            {
                return;                               // Can not handle this file
            }
            if (!hasGct)
            {
                return;                      // Can not handle this file
            }
            //if (colourResolution != 8) return; // Can not handle this file

            int index = 13;

            Colour[] palette = new Colour[256];
            for (int i = 0; i < colourCount; i++)
            {
                byte r = buffer[index++], g = buffer[index++], b = buffer[index++];
                palette[i] = new Colour(r, g, b);
            }

            while (index < buffer.Length)
            {
                switch (buffer[index])
                {
                case 0x2C:
                    // Image descriptor
                    if (buffer[index++] != 0x2C)
                    {
                        return;                                                      // Unexpected byte
                    }
                    if (BitConverter.ToUInt16(buffer, index) != 0)
                    {
                        return;                                                                        // Can not handle this file
                    }
                    if (BitConverter.ToUInt16(buffer, index + 2) != 0)
                    {
                        return;                                                                            // Can not handle this file
                    }
                    if (BitConverter.ToUInt16(buffer, index + 4) != width)
                    {
                        return;                                                                                // Can not handle this file
                    }
                    if (BitConverter.ToUInt16(buffer, index + 6) != height)
                    {
                        return;                                                                                 // Can not handle this file
                    }
                    index += 8;
                    if (buffer[index++] != 0)
                    {
                        return;                                                   // Can not handle this file
                    }
                    break;

                case 0x21:
                    // Graphics Control Extension/Comment Extension Block
                    index++;
                    switch (buffer[index++])
                    {
                    case 0xF9:
                        // Graphics Control Extension
                    {
                        int size = buffer[index++];
                        if (size == 4)
                        {
                            byte   packed    = buffer[index++];
                            ushort delayTime = BitConverter.ToUInt16(buffer, index);
                            index += 2;
                            byte transparentColour = buffer[index++];
                            if ((packed & 1) == 1)
                            {
                                palette[transparentColour] = Colour.Transparent;
                            }
                            index++;
                        }
                        else
                        {
                            index += size + 1;
                        }
                    }
                    break;

                    case 0xFE:
                        // Comment Extension Block
                    {
                        int size = buffer[index++];
                        index += size + 1;
                    }
                    break;

                    default:
                        // Unexpected byte
                        return;
                    }
                    break;

                case 0x3B:
                    // Trailer (end of file)
                    index = buffer.Length;
                    break;

                default:
                    int         minCode = buffer[index++];
                    List <byte> lzwData = new List <byte>();
                    foreach (byte[] data in InputBlock(buffer, index))
                    {
                        index += (data.Length + 2);
                        lzwData.AddRange(data);
                    }

                    try
                    {
                        byte[] pixels = LZW.Decode(lzwData.ToArray(), true, false, minCode, 12);
                        _pixels  = new Bytemap(width, height).FromByteArray(pixels);
                        _palette = palette;
                    }
                    catch
                    {
                        RuntimeHandler.Runtime.Log("Could not decode GIF file.");
                    }
                    return;
                }
            }
        }