예제 #1
0
        public void Write(EasyBuffer source, long byteCount)
        {
            Util.CheckOffsetAndCount(source.Size, 0, byteCount);
            while (byteCount > 0)
            {
                // Share bytes from the head segment of 'source' with the deflater.
                Segment head      = source.Head;
                int     toDeflate = (int)Math.Min(byteCount, head.Limit - head.Pos);
                deflater.SetInput(head.Data, head.Pos, toDeflate);
                deflater.Flush();

                // Deflate those bytes into sink.
                Deflate();

                // Mark those bytes as read.
                source.Size -= toDeflate;
                head.Pos    += toDeflate;
                if (head.Pos == head.Limit)
                {
                    source.Head = head.Pop();
                    SegmentPool.Recycle(head);
                }

                byteCount -= toDeflate;
            }
        }
예제 #2
0
        private static byte[] Compress(byte[] data)
        {
            Deflater mDeflater = new Deflater();

            mDeflater.SetInput(data);
            mDeflater.Finish();
            byte[] compressBytes       = new byte[512];
            int    compressBytesLength = mDeflater.Deflate(compressBytes);

            mDeflater.Flush();

            return(compressBytes.Take(compressBytesLength).ToArray());
        }
        /// <summary>
        /// Flushes the stream by calling flush() on the deflater and then
        /// on the underlying stream.  This ensures that all bytes are
        /// flushed.
        /// </summary>
        public override void Flush()
        {
            if (m_PossibleBytesPending)
            {
                def.SetInput(new byte[0]);

                def.Flush();
                Deflate();

                m_PossibleBytesPending = false;
            }

            baseOutputStream.Flush();
        }
        public static string StringToCompressedBase64(string text)
        {
            Deflater     deflater     = new Deflater(1, true);
            MemoryStream memoryStream = new MemoryStream();

            byte[] numArray = new byte[256];
            deflater.SetInput(new UnicodeEncoding().GetBytes(text));
            deflater.Flush();
            int count;

            do
            {
                count = deflater.Deflate(numArray, 0, numArray.Length);
                memoryStream.Write(numArray, 0, count);
            }while (count > 0);
            return(Convert.ToBase64String(memoryStream.ToArray()));
        }
예제 #5
0
        /// <summary>
        ///     Called when the stream needs to compress the outgoing data.
        /// </summary>
        /// <param name="data">The data to be compressed as a byte array.</param>
        /// <returns>A byte array containiong the compressed data.</returns>
        public byte[] Deflate(byte[] data)
        {
            int ret;

            _deflate.SetInput(data);
            _deflate.Flush();

            var ms = new MemoryStream();

            do
            {
                var buf = new byte[4096];
                ret = _deflate.Deflate(buf);
                if (ret > 0)
                {
                    ms.Write(buf, 0, ret);
                }
            } while (ret > 0);

            return(ms.ToArray());
        }
            public async Task Execute(IReadableChannel input, IWritableChannel output)
            {
                while (true)
                {
                    var inputBuffer = await input;

                    if (inputBuffer.IsEmpty && input.Completion.IsCompleted)
                    {
                        break;
                    }

                    var writerBuffer = output.Alloc(2048);
                    var span         = inputBuffer.FirstSpan;

                    _deflater.SetInput(span.BufferPtr, span.Length);

                    while (!_deflater.NeedsInput())
                    {
                        int written = _deflater.ReadDeflateOutput(writerBuffer.Memory.BufferPtr, writerBuffer.Memory.Length);
                        writerBuffer.CommitBytes(written);
                    }

                    var consumed = span.Length - _deflater.AvailableInput;

                    inputBuffer = inputBuffer.Slice(0, consumed);

                    inputBuffer.Consumed();

                    await writerBuffer.FlushAsync();
                }

                bool flushed;

                do
                {
                    // Need to do more stuff here
                    var writerBuffer = output.Alloc(2048);

                    int compressedBytes;
                    flushed = _deflater.Flush(writerBuffer.Memory.BufferPtr, writerBuffer.Memory.Length, out compressedBytes);
                    writerBuffer.CommitBytes(compressedBytes);

                    await writerBuffer.FlushAsync();
                }while (flushed);

                bool finished;

                do
                {
                    // Need to do more stuff here
                    var writerBuffer = output.Alloc(2048);

                    int compressedBytes;
                    finished = _deflater.Finish(writerBuffer.Memory.BufferPtr, writerBuffer.Memory.Length, out compressedBytes);
                    writerBuffer.CommitBytes(compressedBytes);

                    await writerBuffer.FlushAsync();
                }while (!finished);

                input.CompleteReading();

                output.CompleteWriting();

                _deflater.Dispose();
            }
예제 #7
0
 /// <summary>
 /// Flushes the stream by calling <see cref="Flush">Flush</see> on the deflater and then
 /// on the underlying stream.  This ensures that all bytes are flushed.
 /// </summary>
 public override void Flush()
 {
     Deflater.Flush();
     Deflate();
     BaseOutputStream.Flush();
 }
 /// <summary>
 /// Flushes the stream by calling flush() on the deflater and then
 /// on the underlying stream.  This ensures that all bytes are
 /// flushed.
 /// </summary>
 public override void Flush()
 {
     def.Flush();
     Deflate();
     baseOutputStream.Flush();
 }
        static PNG_Chunk[] ProcessChunks(PNG_Chunk[] chunks)
        {
            List <PNG_Chunk> result = new List <PNG_Chunk>();

            PNG_IHDR header = new PNG_IHDR();

            using (MemoryStream rawData = new MemoryStream())
            {
                foreach (PNG_Chunk chunk in chunks)
                {
                    if (chunk.Name.Equals(PNG_CgBIChunk))
                    {
                        continue;
                    }

                    if (chunk.Name.Equals(PNG_HeaderChunk))
                    {
                        header = PNG_IHDR.FromBytes(chunk.Data);
                    }

                    if (chunk.Name.Equals(PNG_DataChunk))
                    {
                        rawData.Write(chunk.Data, 0, chunk.Data.Length);
                        continue;
                    }

                    if (chunk.Name.Equals(PNG_EndChunk))
                    {
                        const int dataChunkSize = 16 * 1024;

                        rawData.Position = 0;
                        using (MemoryStream buffer = new MemoryStream())
                        {
                            using (InflaterInputStream inflate = new InflaterInputStream(rawData, new Inflater(true)))
                            {
                                inflate.IsStreamOwner = false;
                                IPATools.Utilities.Utils.CopyStream(inflate, buffer);
                                //inflate.CopyTo(buffer);
                            }

                            buffer.Position = 0;
                            rawData.SetLength(0);
                            IPATools.Utilities.Utils.CopyStream(buffer, rawData);
                            //buffer.CopyTo(rawData);
                        }

                        {
                            byte[] imageData = rawData.GetBuffer();

                            int scanlineSize = header.Width * 4 + 1;
                            int scanlinePos  = 0;

                            int bpp = 4;

                            for (int y = 0; y < header.Height; ++y, scanlinePos += scanlineSize)
                            {
                                PNG_Filter filter = (PNG_Filter)imageData[scanlinePos];

                                switch (filter)
                                {
                                case PNG_Filter.Sub:
                                    for (int x = 1 + bpp; x < scanlineSize; ++x)
                                    {
                                        imageData[scanlinePos + x] = (byte)((int)imageData[scanlinePos + x] + (int)imageData[scanlinePos + x - bpp]);
                                    }
                                    break;

                                case PNG_Filter.Up:
                                    if (y > 0)
                                    {
                                        for (int x = 1; x < scanlineSize; ++x)
                                        {
                                            imageData[scanlinePos + x] = (byte)((int)imageData[scanlinePos + x] + (int)imageData[scanlinePos + x - bpp - scanlineSize]);
                                        }
                                    }
                                    break;

                                case PNG_Filter.Average:
                                    if (y > 0)
                                    {
                                        for (int x = 1 + bpp; x < scanlineSize; ++x)
                                        {
                                            imageData[scanlinePos + x] = (byte)
                                                                         (((int)imageData[scanlinePos + x - bpp] +
                                                                           (int)imageData[scanlinePos + x - bpp - scanlineSize]) >> 1);
                                        }
                                    }
                                    break;

                                case PNG_Filter.Paeth:
                                    if (y > 0)
                                    {
                                        for (int x = 1 + bpp; x < scanlineSize; ++x)
                                        {
                                            imageData[scanlinePos + x] =
                                                (byte)FilterPaethPredictor(
                                                    imageData[scanlinePos + x - bpp],
                                                    imageData[scanlinePos + x - bpp - scanlineSize],
                                                    imageData[scanlinePos + x - scanlineSize]);
                                        }
                                    }
                                    break;
                                }

                                for (int x = 0; x < header.Width; ++x)
                                {
                                    byte r = imageData[scanlinePos + x * 4 + 1];
                                    byte g = imageData[scanlinePos + x * 4 + 2];
                                    byte b = imageData[scanlinePos + x * 4 + 3];
                                    byte a = imageData[scanlinePos + x * 4 + 4];

                                    if (a > 0)
                                    {
                                        r = (byte)(255 * r / a);
                                        g = (byte)(255 * g / a);
                                        b = (byte)(255 * b / a);
                                    }

                                    imageData[scanlinePos + x * 4 + 1] = b;
                                    imageData[scanlinePos + x * 4 + 2] = g;
                                    imageData[scanlinePos + x * 4 + 3] = r;
                                    imageData[scanlinePos + x * 4 + 4] = a;
                                }

                                imageData[scanlinePos] = (byte)PNG_Filter.None;

                                //imageData[scanlinePos] = (byte)PNG_Filter.Sub;
                                //for (int x = 1 + bpp; x < scanlineSize; ++x)
                                //{
                                //    byte c = imageData[scanlinePos + x];
                                //    byte l = imageData[scanlinePos + x - bpp];
                                //    imageData[scanlinePos + x] = (byte)(l - c);
                                //}
                            }
                        }

                        Deflater deflater = new Deflater(9);

                        Crc32 crc32 = new Crc32();

                        rawData.Position = 0;
                        long dataLeft = rawData.Length;
                        while (dataLeft > 0)
                        {
                            byte[] chunkData = new byte[dataChunkSize];

                            int dataInChunk = rawData.Read(chunkData, 0, dataChunkSize);

                            dataLeft -= dataInChunk;

                            byte[] deflatedChunkData = new byte[dataChunkSize];
                            deflater.SetInput(chunkData, 0, dataInChunk);
                            if (dataLeft > 0)
                            {
                                deflater.Flush();
                            }
                            else
                            {
                                deflater.Finish();
                            }
                            int deflatedBytes = deflater.Deflate(deflatedChunkData, 0, dataInChunk);

                            PNG_Chunk dataChunk = new PNG_Chunk();
                            dataChunk.Name = PNG_DataChunk;
                            dataChunk.Data = new byte[deflatedBytes];

                            for (int i = 0; i < deflatedBytes; ++i)
                            {
                                dataChunk.Data[i] = deflatedChunkData[i];
                            }

                            crc32.Reset();
                            crc32.Update(ASCIIEncoding.ASCII.GetBytes(dataChunk.Name));
                            crc32.Update(dataChunk.Data);
                            dataChunk.CRC = (uint)crc32.Value;

                            result.Add(dataChunk);
                        }
                    }

                    result.Add(chunk);
                }
            }

            return(result.ToArray());
        }
예제 #10
0
            public async Task Execute(IReadableChannel input, IWritableChannel output)
            {
                while (true)
                {
                    var inputBuffer = await input.ReadAsync();

                    if (inputBuffer.IsEmpty && input.Reading.IsCompleted)
                    {
                        break;
                    }

                    var writerBuffer = output.Alloc(2048);
                    var memory       = inputBuffer.First;

                    unsafe
                    {
                        _deflater.SetInput((IntPtr)memory.UnsafePointer, memory.Length);
                    }

                    while (!_deflater.NeedsInput())
                    {
                        unsafe
                        {
                            int written = _deflater.ReadDeflateOutput((IntPtr)writerBuffer.Memory.UnsafePointer, writerBuffer.Memory.Length);
                            writerBuffer.Advance(written);
                        }
                    }

                    var consumed = memory.Length - _deflater.AvailableInput;

                    inputBuffer = inputBuffer.Slice(0, consumed);

                    input.Advance(inputBuffer.End);

                    await writerBuffer.FlushAsync();
                }

                bool flushed;

                do
                {
                    // Need to do more stuff here
                    var writerBuffer = output.Alloc(2048);
                    var memory       = writerBuffer.Memory;

                    unsafe
                    {
                        int compressedBytes;
                        flushed = _deflater.Flush((IntPtr)memory.UnsafePointer, memory.Length, out compressedBytes);
                        writerBuffer.Advance(compressedBytes);
                    }

                    await writerBuffer.FlushAsync();
                }while (flushed);

                bool finished;

                do
                {
                    // Need to do more stuff here
                    var writerBuffer = output.Alloc(2048);
                    var memory       = writerBuffer.Memory;

                    unsafe
                    {
                        int compressedBytes;
                        finished = _deflater.Finish((IntPtr)memory.UnsafePointer, memory.Length, out compressedBytes);
                        writerBuffer.Advance(compressedBytes);
                    }

                    await writerBuffer.FlushAsync();
                }while (!finished);

                input.Complete();

                output.Complete();

                _deflater.Dispose();
            }