Exemplo n.º 1
0
        /// <inheritdoc/>
        protected override void Dispose(bool disposing)
        {
            if (this.isDisposed)
            {
                return;
            }

            if (disposing)
            {
                // dispose managed resources
                this.deflateStream.Dispose();

                // Add the crc
                uint crc = (uint)this.adler32.Value;
                this.rawStream.WriteByte((byte)((crc >> 24) & 0xFF));
                this.rawStream.WriteByte((byte)((crc >> 16) & 0xFF));
                this.rawStream.WriteByte((byte)((crc >> 8) & 0xFF));
                this.rawStream.WriteByte((byte)(crc & 0xFF));
            }

            this.deflateStream = null;

            base.Dispose(disposing);
            this.isDisposed = true;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ZlibDeflateStream"/> class.
        /// </summary>
        /// <param name="memoryAllocator">The memory allocator to use for buffer allocations.</param>
        /// <param name="stream">The stream to compress.</param>
        /// <param name="level">The compression level.</param>
        public ZlibDeflateStream(MemoryAllocator memoryAllocator, Stream stream, PngCompressionLevel level)
        {
            int compressionLevel = (int)level;

            this.rawStream = stream;

            // Write the zlib header : http://tools.ietf.org/html/rfc1950
            // CMF(Compression Method and flags)
            // This byte is divided into a 4 - bit compression method and a
            // 4-bit information field depending on the compression method.
            // bits 0 to 3  CM Compression method
            // bits 4 to 7  CINFO Compression info
            //
            //   0   1
            // +---+---+
            // |CMF|FLG|
            // +---+---+
            const int Cmf = 0x78;
            int       flg = 218;

            // http://stackoverflow.com/a/2331025/277304
            if (compressionLevel >= 5 && compressionLevel <= 6)
            {
                flg = 156;
            }
            else if (compressionLevel >= 3 && compressionLevel <= 4)
            {
                flg = 94;
            }
            else if (compressionLevel <= 2)
            {
                flg = 1;
            }

            // Just in case
            flg -= ((Cmf * 256) + flg) % 31;

            if (flg < 0)
            {
                flg += 31;
            }

            this.rawStream.WriteByte(Cmf);
            this.rawStream.WriteByte((byte)flg);

            this.deflateStream = new DeflaterOutputStream(memoryAllocator, this.rawStream, compressionLevel);
        }