Ejemplo n.º 1
0
 private static byte[] Compress(byte[] zlib)
 {
     // Create a GZIP stream with decompression mode.
     // ... Then create a buffer and write into while reading from the GZIP stream.
     using (MemoryStream memory = new MemoryStream())
     {
         var def = new Deflater();
         def.setInput(gzip);
         def.finish();
         var decompressStream = new MemoryStream();
         int i      = 0;
         var buffer = new byte[1024 * 1024];
         while ((i = def.deflate(buffer)) > 0)
         {
             decompressStream.Write(buffer, 0, i);
         }
         return(decompressStream.ToArray());
     }
     //using (GZipStream stream = new GZipStream(memory, CompressionMode.Compress))
     //{
     //    stream.Write(gzip, 0, gzip.Length);
     //    stream.Flush();
     //    stream.Close();
     //    return memory.ToArray();
     //}
 }
Ejemplo n.º 2
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: @Override public void writeEnd() throws java.io.IOException
            public override void writeEnd()
            {
                // See https://en.wikipedia.org/wiki/Portable_Network_Graphics
                // for detailed information about the PNG file format

                sbyte[] fileHeader = new sbyte[8];
                fileHeader[0] = unchecked ((sbyte)0x89);
                fileHeader[1] = (sbyte)'P';
                fileHeader[2] = (sbyte)'N';
                fileHeader[3] = (sbyte)'G';
                fileHeader[4] = (sbyte)'\r';
                fileHeader[5] = (sbyte)'\n';
                fileHeader[6] = (sbyte)0x1A;
                fileHeader[7] = (sbyte)'\n';

                sbyte[] ihdr = new sbyte[13 + 8 + 4];
                storeBigEndianInt(ihdr, 0, 13);
                storeChunkType(ihdr, 4, 'I', 'H', 'D', 'R');
                storeBigEndianInt(ihdr, 8, width);
                storeBigEndianInt(ihdr, 12, height);
                ihdr[16] = 8;                 // bit depth
                ihdr[17] = 6;                 // color type: red, green, blue, alpha
                ihdr[18] = 0;                 // compression method: deflate/inflate
                ihdr[19] = 0;                 // filter method: none
                ihdr[20] = 0;                 // interlace method: no interlace
                storeCRC(ihdr, 21);

                Deflater deflater = new Deflater();

                deflater.Input = buffer;
                deflater.finish();
                sbyte[] data       = new sbyte[buffer.Length];
                int     dataLength = deflater.deflate(data);

                sbyte[] idat = new sbyte[8 + dataLength + 4];
                storeBigEndianInt(idat, 0, dataLength);
                storeChunkType(idat, 4, 'I', 'D', 'A', 'T');
                Array.Copy(data, 0, idat, 8, dataLength);
                storeCRC(idat, 8 + dataLength);

                sbyte[] iend = new sbyte[12];
                storeBigEndianInt(iend, 0, 0);
                storeChunkType(iend, 4, 'I', 'E', 'N', 'D');
                storeCRC(iend, 8);

                System.IO.Stream outStream = new System.IO.FileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                outStream.Write(fileHeader, 0, fileHeader.Length);
                outStream.Write(ihdr, 0, ihdr.Length);
                outStream.Write(idat, 0, idat.Length);
                outStream.Write(iend, 0, iend.Length);
                outStream.Close();
            }
Ejemplo n.º 3
0
        public Packet51MapChunk(int i, int j, int k, int l, int i1, int j1, World world)
        {
            isChunkDataPacket = true;
            xPosition         = i;
            yPosition         = j;
            zPosition         = k;
            xSize             = l;
            ySize             = i1;
            zSize             = j1;
            byte[] abyte0   = world.getChunkData(i, j, k, l, i1, j1);
            var    deflater = new Deflater(1);

            try
            {
                deflater.setInput(abyte0);
                deflater.finish();
                chunk     = new byte[(l * i1 * j1 * 5) / 2];
                chunkSize = deflater.deflate(chunk);
            }
            finally
            {
                deflater.end();
            }
        }
Ejemplo n.º 4
0
        private static int compress(int tp, byte[] inBytes, int inSize, byte[] outBytes)
        {
            if (inSize == 0)
            {
                return(0);
            }

            int t1 = 0, t2 = (inSize + 1) / 2;
            int inPtr = 0, ret;

            byte[] tmp = new byte[inSize];

            // zip and rle treat the data first, in the same way so I'm not
            // repeating the code
            if ((tp == ZIP_COMPRESSION) || (tp == RLE_COMPRESSION))
            {
                // reorder the pixel data ~ straight from ImfZipCompressor.cpp :)
                while (true)
                {
                    if (inPtr < inSize)
                    {
                        tmp[t1++] = inBytes[inPtr++];
                    }
                    else
                    {
                        break;
                    }

                    if (inPtr < inSize)
                    {
                        tmp[t2++] = inBytes[inPtr++];
                    }
                    else
                    {
                        break;
                    }
                }

                // Predictor ~ straight from ImfZipCompressor.cpp :)
                t1 = 1;
                int p = tmp[t1 - 1];
                while (t1 < inSize)
                {
                    int d = (int)tmp[t1] - p + (128 + 256);
                    p       = (int)tmp[t1];
                    tmp[t1] = (byte)d;
                    t1++;
                }
            }

            // We'll just jump from here to the wanted compress/decompress stuff if
            // need be
            switch (tp)
            {
            case ZIP_COMPRESSION:
                Deflater def = new Deflater(Deflater.DEFAULT_COMPRESSION, false);
                def.setInput(tmp, 0, inSize);
                def.finish();
                ret = def.deflate(outBytes);
                return(ret);

            case RLE_COMPRESSION:
                return(rleCompress(tmp, inSize, outBytes));

            default:
                return(-1);
            }
        }
Ejemplo n.º 5
0
        private static int compress(int tp, byte[] inBytes, int inSize, byte[] outBytes)
        {
            if (inSize == 0)
                return 0;

            int t1 = 0, t2 = (inSize + 1) / 2;
            int inPtr = 0, ret;
            byte[] tmp = new byte[inSize];

            // zip and rle treat the data first, in the same way so I'm not
            // repeating the code
            if ((tp == ZIP_COMPRESSION) || (tp == RLE_COMPRESSION))
            {
                // reorder the pixel data ~ straight from ImfZipCompressor.cpp :)
                while (true)
                {
                    if (inPtr < inSize)
                        tmp[t1++] = inBytes[inPtr++];
                    else
                        break;

                    if (inPtr < inSize)
                        tmp[t2++] = inBytes[inPtr++];
                    else
                        break;
                }

                // Predictor ~ straight from ImfZipCompressor.cpp :)
                t1 = 1;
                int p = tmp[t1 - 1];
                while (t1 < inSize)
                {
                    int d = (int)tmp[t1] - p + (128 + 256);
                    p = (int)tmp[t1];
                    tmp[t1] = (byte)d;
                    t1++;
                }
            }

            // We'll just jump from here to the wanted compress/decompress stuff if
            // need be
            switch (tp)
            {
                case ZIP_COMPRESSION:
                    Deflater def = new Deflater(Deflater.DEFAULT_COMPRESSION, false);
                    def.setInput(tmp, 0, inSize);
                    def.finish();
                    ret = def.deflate(outBytes);
                    return ret;
                case RLE_COMPRESSION:
                    return rleCompress(tmp, inSize, outBytes);
                default:
                    return -1;
            }
        }