Пример #1
0
        public static byte[] Encode(byte[] src)
        {
            /* quick & dirty format80 encoder -- only uses raw copy operator, terminated with a zero-run. */
            /* this does not produce good compression, but it's valid format80 */

            var ctx = new FastByteReader(src);
            var ms  = new MemoryStream();

            do
            {
                var len = Math.Min(ctx.Remaining(), 0x3F);
                ms.WriteByte((byte)(0x80 | len));
                while (len-- > 0)
                {
                    ms.WriteByte(ctx.ReadByte());
                }
            }while (!ctx.Done());

            ms.WriteByte(0x80);                 // terminator -- 0-length run.

            return(ms.ToArray());
        }
Пример #2
0
        public static byte[] Encode(byte[] src)
        {
            /* quick & dirty format80 encoder -- only uses raw copy operator, terminated with a zero-run. */
            /* this does not produce good compression, but it's valid format80 */

            var ctx = new FastByteReader(src);
            var ms = new MemoryStream();

            do
            {
                var len = Math.Min(ctx.Remaining(), 0x3F);
                ms.WriteByte((byte)(0x80 | len));
                while (len-- > 0)
                    ms.WriteByte(ctx.ReadByte());
            }
            while (!ctx.Done());

            ms.WriteByte(0x80);	// terminator -- 0-length run.

            return ms.ToArray();
        }
Пример #3
0
        public static int DecodeInto(byte[] src, byte[] dest)
        {
            var ctx = new FastByteReader(src);
            var destIndex = 0;

            while (true)
            {
                var i = ctx.ReadByte();
                if ((i & 0x80) == 0)
                {
                    // case 2
                    var secondByte = ctx.ReadByte();
                    var count = ((i & 0x70) >> 4) + 3;
                    var rpos = ((i & 0xf) << 8) + secondByte;

                    ReplicatePrevious(dest, destIndex, destIndex - rpos, count);
                    destIndex += count;
                }
                else if ((i & 0x40) == 0)
                {
                    // case 1
                    var count = i & 0x3F;
                    if (count == 0)
                        return destIndex;

                    ctx.CopyTo(dest, destIndex, count);
                    destIndex += count;
                }
                else
                {
                    var count3 = i & 0x3F;
                    if (count3 == 0x3E)
                    {
                        // case 4
                        var count = ctx.ReadWord();
                        var color = ctx.ReadByte();

                        for (var end = destIndex + count; destIndex < end; destIndex++)
                            dest[destIndex] = color;
                    }
                    else if (count3 == 0x3F)
                    {
                        // case 5
                        var count = ctx.ReadWord();
                        var srcIndex = ctx.ReadWord();
                        if (srcIndex >= destIndex)
                            throw new NotImplementedException("srcIndex >= destIndex {0} {1}".F(srcIndex, destIndex));

                        for (var end = destIndex + count; destIndex < end; destIndex++)
                            dest[destIndex] = dest[srcIndex++];
                    }
                    else
                    {
                        // case 3
                        var count = count3 + 3;
                        var srcIndex = ctx.ReadWord();
                        if (srcIndex >= destIndex)
                            throw new NotImplementedException("srcIndex >= destIndex {0} {1}".F(srcIndex, destIndex));

                        for (var end = destIndex + count; destIndex < end; destIndex++)
                            dest[destIndex] = dest[srcIndex++];
                    }
                }
            }
        }
Пример #4
0
        public static int DecodeInto(byte[] src, byte[] dest, int srcOffset = 0, bool reverse = false)
        {
            var ctx       = new FastByteReader(src, srcOffset);
            var destIndex = 0;

            while (true)
            {
                var i = ctx.ReadByte();
                if ((i & 0x80) == 0)
                {
                    // case 2
                    var secondByte = ctx.ReadByte();
                    var count      = ((i & 0x70) >> 4) + 3;
                    var rpos       = ((i & 0xf) << 8) + secondByte;

                    if (destIndex + count > dest.Length)
                    {
                        return(destIndex);
                    }

                    ReplicatePrevious(dest, destIndex, destIndex - rpos, count);
                    destIndex += count;
                }
                else if ((i & 0x40) == 0)
                {
                    // case 1
                    var count = i & 0x3F;
                    if (count == 0)
                    {
                        return(destIndex);
                    }

                    ctx.CopyTo(dest, destIndex, count);
                    destIndex += count;
                }
                else
                {
                    var count3 = i & 0x3F;
                    if (count3 == 0x3E)
                    {
                        // case 4
                        var count = ctx.ReadWord();
                        var color = ctx.ReadByte();

                        for (var end = destIndex + count; destIndex < end; destIndex++)
                        {
                            dest[destIndex] = color;
                        }
                    }
                    else if (count3 == 0x3F)
                    {
                        // case 5
                        var count    = ctx.ReadWord();
                        var srcIndex = reverse ? destIndex - ctx.ReadWord() : ctx.ReadWord();
                        if (srcIndex >= destIndex)
                        {
                            throw new NotImplementedException("srcIndex >= destIndex {0} {1}".F(srcIndex, destIndex));
                        }

                        for (var end = destIndex + count; destIndex < end; destIndex++)
                        {
                            dest[destIndex] = dest[srcIndex++];
                        }
                    }
                    else
                    {
                        // case 3
                        var count    = count3 + 3;
                        var srcIndex = reverse ? destIndex - ctx.ReadWord() : ctx.ReadWord();
                        if (srcIndex >= destIndex)
                        {
                            throw new NotImplementedException("srcIndex >= destIndex {0} {1}".F(srcIndex, destIndex));
                        }

                        for (var end = destIndex + count; destIndex < end; destIndex++)
                        {
                            dest[destIndex] = dest[srcIndex++];
                        }
                    }
                }
            }
        }
Пример #5
0
        public static int DecodeInto(byte[] src, byte[] dest)
        {
            var ctx       = new FastByteReader(src);
            int destIndex = 0;

            while (true)
            {
                byte i = ctx.ReadByte();
                if ((i & 0x80) == 0)
                {
                    // case 2
                    byte secondByte = ctx.ReadByte();
                    int  count      = ((i & 0x70) >> 4) + 3;
                    int  rpos       = ((i & 0xf) << 8) + secondByte;

                    ReplicatePrevious(dest, destIndex, destIndex - rpos, count);
                    destIndex += count;
                }
                else if ((i & 0x40) == 0)
                {
                    // case 1
                    int count = i & 0x3F;
                    if (count == 0)
                    {
                        return(destIndex);
                    }

                    ctx.CopyTo(dest, destIndex, count);
                    destIndex += count;
                }
                else
                {
                    int count3 = i & 0x3F;
                    if (count3 == 0x3E)
                    {
                        // case 4
                        int  count = ctx.ReadWord();
                        byte color = ctx.ReadByte();

                        for (int end = destIndex + count; destIndex < end; destIndex++)
                        {
                            dest[destIndex] = color;
                        }
                    }
                    else if (count3 == 0x3F)
                    {
                        // case 5
                        int count    = ctx.ReadWord();
                        int srcIndex = ctx.ReadWord();
                        if (srcIndex >= destIndex)
                        {
                            throw new NotImplementedException(string.Format("srcIndex >= destIndex  {0}  {1}", srcIndex, destIndex));
                        }

                        for (int end = destIndex + count; destIndex < end; destIndex++)
                        {
                            dest[destIndex] = dest[srcIndex++];
                        }
                    }
                    else
                    {
                        // case 3
                        int count    = count3 + 3;
                        int srcIndex = ctx.ReadWord();
                        if (srcIndex >= destIndex)
                        {
                            throw new NotImplementedException(string.Format("srcIndex >= destIndex  {0}  {1}", srcIndex, destIndex));
                        }

                        for (int end = destIndex + count; destIndex < end; destIndex++)
                        {
                            dest[destIndex] = dest[srcIndex++];
                        }
                    }
                }
            }
        }
Пример #6
0
        public static int EncodeInto(byte[] src, byte[] dest)
        {
            /* quick & dirty format80 encoder -- only uses raw copy operator, terminated with a zero-run. */
            /* this does not produce good compression, but it's valid format80 */

            var destIndex = 0;
            var ctx = new FastByteReader(src);

            do
            {
                var len = Math.Min(ctx.Remaining(), 0x3F);
                dest[destIndex++] = (byte)(0x80 | len);
                if (len > 0)
                    ctx.CopyTo(dest, destIndex, len);
            }
            while (!ctx.Done());

            return destIndex;
        }