Beispiel #1
0
        /// <summary>
        /// Internal convert method to convert byte array to compressed byte array
        /// </summary>
        /// <param name="data_in">Uncompressed data array</param>
        /// <param name="type">Format type, DEFLATE, GZIP, ZLIB</param>
        /// <param name="options">Compression options</param>
        /// <returns>Compressed data array</returns>
        public static byte[] compress(byte[] data_in, ZopfliFormat type, ZopfliOptions options)
        {
            IntPtr  result      = IntPtr.Zero;
            UIntPtr result_size = UIntPtr.Zero;

            try
            {
                // Get image data length
                UIntPtr data_size = (UIntPtr)data_in.Length;

                // Compress the data via native methods
                if (Environment.Is64BitProcess)
                {
                    ZopfliCompressor64.ZopfliCompress(ref options, type, data_in, data_in.Length, ref result, ref result_size);
                }
                else
                {
                    ZopfliCompressor32.ZopfliCompress(ref options, type, data_in, data_in.Length, ref result, ref result_size);
                }

                // Copy data back to managed memory and return
                return(NativeUtilities.GetDataFromUnmanagedMemory(result, (int)result_size));
            }
            catch
            {
                throw;
            }
            finally
            {
                // Free unmanaged memory
                Marshal.FreeHGlobal(result);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Internal convert method to convert byte array to compressed byte array
        /// </summary>
        /// <param name="data_in">Uncompressed data array</param>
        /// <param name="options">Zopfli PNG compression options</param>
        /// <returns></returns>
        public static byte[] compress(byte[] data_in, ZopfliPNGOptions options)
        {
            IntPtr result = IntPtr.Zero;

            int error = 0;

            try
            {
                // Get image data length
                UIntPtr data_size = (UIntPtr)data_in.Length;

                // Compress the data via native methods
                if (Environment.Is64BitProcess)
                {
                    error = ZopfliPNGCompressor64.ZopfliPNGExternalOptimize(data_in, data_in.Length, ref result);
                }
                else
                {
                    error = ZopfliPNGCompressor32.ZopfliPNGExternalOptimize(data_in, data_in.Length, ref result);
                }

                // Copy data back to managed memory and return
                return(NativeUtilities.GetDataFromUnmanagedMemory(result, error));
            }
            catch
            {
                if (result == IntPtr.Zero && error > 0)
                {
                    throw new ZopfliPNGException(error);
                }

                throw;
            }
            finally
            {
                // Free unmanaged memory
                Marshal.FreeHGlobal(result);
            }
        }