/// <summary> /// Internal convert method to get a Bitmap from a WebP image file /// </summary> /// <param name="path">The path to the WebP image file</param> /// <param name="type">The color type you want to convert to</param> /// <param name="format">The PixelFormat the Bitmap should use</param> /// <returns></returns> private Bitmap decode(string path, decodeType type, PixelFormat format) { int width = 0; int height = 0; byte[] data = decode(path, type, format, out width, out height); return(Utilities.ConvertDataToBitmap(data, width, height, format)); }
/// <summary> /// Internal convert method to get a byte array from a WebP image file /// </summary> /// <param name="path">The path to the WebP image file</param> /// <param name="type">The color type you want to convert to</param> /// <param name="format">The PixelFormat you want to use</param> /// <param name="imgWidth">Returns the width of the WebP image</param> /// <param name="imgHeight">Returns the height of the WebP image</param> /// <returns></returns> private byte[] decode(string path, decodeType type, PixelFormat format, out int imgWidth, out int imgHeight) { int width = 0; int height = 0; IntPtr data = IntPtr.Zero; IntPtr output_buffer = IntPtr.Zero; IntPtr result = IntPtr.Zero; try { // Load data byte[] managedData = Utilities.CopyFileToManagedArray(path); // Copy data to unmanaged memory data = Utilities.CopyDataToUnmanagedMemory(managedData); // Get image width and height int ret = Native.WebPDecoder.WebPGetInfo(data, (uint)managedData.Length, ref width, ref height); // Get image data lenght UInt32 data_size = (UInt32)managedData.Length; // Calculate bitmap size for decoded WebP image int output_buffer_size = Utilities.CalculateBitmapSize(width, height, format); // Allocate unmanaged memory to decoded WebP image output_buffer = Marshal.AllocHGlobal(output_buffer_size); // Calculate distance between scanlines int output_stride = (width * Image.GetPixelFormatSize(format)) / 8; // Convert image switch (type) { case decodeType.RGB: result = Native.WebPDecoder.WebPDecodeRGBInto(data, data_size, output_buffer, output_buffer_size, output_stride); break; case decodeType.RGBA: result = Native.WebPDecoder.WebPDecodeRGBAInto(data, data_size, output_buffer, output_buffer_size, output_stride); break; case decodeType.BGR: result = Native.WebPDecoder.WebPDecodeBGRInto(data, data_size, output_buffer, output_buffer_size, output_stride); break; case decodeType.BGRA: result = Native.WebPDecoder.WebPDecodeBGRAInto(data, data_size, output_buffer, output_buffer_size, output_stride); break; } // Set out values imgWidth = width; imgHeight = height; // Copy data back to managed memory and return return(Utilities.GetDataFromUnmanagedMemory(result, output_buffer_size)); } catch { throw; } finally { // Free unmanaged memory Marshal.FreeHGlobal(data); Marshal.FreeHGlobal(output_buffer); } }
/// <summary> /// Internal convert method to get a byte array from a WebP image file /// </summary> /// <param name="path">The path to the WebP image file</param> /// <param name="type">The color type you want to convert to</param> /// <param name="format">The PixelFormat you want to use</param> /// <param name="imgWidth">Returns the width of the WebP image</param> /// <param name="imgHeight">Returns the height of the WebP image</param> /// <returns></returns> private byte[] decode(string path, decodeType type, PixelFormat format, out int imgWidth, out int imgHeight) { int width = 0; int height = 0; IntPtr data = IntPtr.Zero; IntPtr output_buffer = IntPtr.Zero; IntPtr result = IntPtr.Zero; try { // Load data byte[] managedData = Utilities.CopyFileToManagedArray(path); // Copy data to unmanaged memory data = Utilities.CopyDataToUnmanagedMemory(managedData); // Get image width and height int ret = Native.WebPDecoder.WebPGetInfo(data, (uint)managedData.Length, ref width, ref height); // Get image data lenght UInt32 data_size = (UInt32)managedData.Length; // Calculate bitmap size for decoded WebP image int output_buffer_size = Utilities.CalculateBitmapSize(width, height, format); // Allocate unmanaged memory to decoded WebP image output_buffer = Marshal.AllocHGlobal(output_buffer_size); // Calculate distance between scanlines int output_stride = (width * Image.GetPixelFormatSize(format)) / 8; // Convert image switch (type) { case decodeType.RGB: result = Native.WebPDecoder.WebPDecodeRGBInto(data, data_size, output_buffer, output_buffer_size, output_stride); break; case decodeType.RGBA: result = Native.WebPDecoder.WebPDecodeRGBAInto(data, data_size, output_buffer, output_buffer_size, output_stride); break; case decodeType.BGR: result = Native.WebPDecoder.WebPDecodeBGRInto(data, data_size, output_buffer, output_buffer_size, output_stride); break; case decodeType.BGRA: result = Native.WebPDecoder.WebPDecodeBGRAInto(data, data_size, output_buffer, output_buffer_size, output_stride); break; } // Set out values imgWidth = width; imgHeight = height; // Copy data back to managed memory and return return Utilities.GetDataFromUnmanagedMemory(result, output_buffer_size); } catch { throw; } finally { // Free unmanaged memory Marshal.FreeHGlobal(data); Marshal.FreeHGlobal(output_buffer); } }
/// <summary> /// Internal convert method to get a Bitmap from a WebP image file /// </summary> /// <param name="path">The path to the WebP image file</param> /// <param name="type">The color type you want to convert to</param> /// <param name="format">The PixelFormat the Bitmap should use</param> /// <returns></returns> private Bitmap decode(string path, decodeType type, PixelFormat format) { int width = 0; int height = 0; byte[] data = decode(path, type, format, out width, out height); return Utilities.ConvertDataToBitmap(data, width, height, format); }