// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
//
//
// Provides access to unmanaged native methods.
//
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Imaging.Formats
{
using System;
using System.Runtime.InteropServices;
///
/// Provides access to unmanaged native methods.
///
internal static class NativeMethods
{
///
/// Whether the process is running in 64bit mode. Used for calling the correct dllimport method.
/// Clunky I know but I couldn't get dynamic methods to work.
///
private static readonly bool Is64Bit = Environment.Is64BitProcess;
#region WebP
///
/// Validate the WebP image header and retrieve the image height and width. Pointers *width and *height can be passed NULL if deemed irrelevant
///
///
/// Pointer to WebP image data
///
///
/// This is the size of the memory block pointed to by data containing the image data
///
///
/// The width range is limited currently from 1 to 16383
///
///
/// The height range is limited currently from 1 to 16383
///
///
/// 1 if success, otherwise error code returned in the case of (a) formatting error(s).
///
public static int WebPGetInfo(IntPtr data, uint dataSize, out int width, out int height)
{
return Is64Bit ? WebPGetInfo64(data, dataSize, out width, out height)
: WebPGetInfo86(data, dataSize, out width, out height);
}
///
/// Decode WEBP image pointed to by *data and returns BGR samples into a pre-allocated buffer
///
///
/// Pointer to WebP image data
///
///
/// This is the size of the memory block pointed to by data containing the image data
///
///
/// Pointer to decoded WebP image
///
///
/// Size of allocated buffer
///
///
/// Specifies the distance between scan-lines
///
///
/// output_buffer if function succeeds; NULL otherwise
///
public static IntPtr WebPDecodeBGRAInto(IntPtr data, uint dataSize, IntPtr outputBuffer, int outputBufferSize, int outputStride)
{
return Is64Bit ? WebPDecodeBGRAInto64(data, dataSize, outputBuffer, outputBufferSize, outputStride)
: WebPDecodeBGRAInto86(data, dataSize, outputBuffer, outputBufferSize, outputStride);
}
///
/// Lossy encoding images pointed to by *data in WebP format
///
///
/// Pointer to RGB image data
///
///
/// The width range is limited currently from 1 to 16383
///
///
/// The height range is limited currently from 1 to 16383
///
///
/// The stride.
///
///
/// Ranges from 0 (lower quality) to 100 (highest quality). Controls the loss and quality during compression
///
///
/// output_buffer with WebP image
///
///
/// Size of WebP Image
///
public static int WebPEncodeBGRA(IntPtr rgb, int width, int height, int stride, float qualityFactor, out IntPtr output)
{
return Is64Bit ? WebPEncodeBGRA64(rgb, width, height, stride, qualityFactor, out output)
: WebPEncodeBGRA86(rgb, width, height, stride, qualityFactor, out output);
}
///
/// Frees the unmanaged memory.
///
///
/// The pointer.
///
///
/// 1 if success, otherwise error code returned in the case of (a) error(s).
///
public static int WebPFree(IntPtr pointer)
{
return Is64Bit ? WebPFree64(pointer) : WebPFree86(pointer);
}
///
/// Validate the WebP image header and retrieve the image height and width. Pointers *width and *height can be passed NULL if deemed irrelevant
///
///
/// Pointer to WebP image data
///
///
/// This is the size of the memory block pointed to by data containing the image data
///
///
/// The width range is limited currently from 1 to 16383
///
///
/// The height range is limited currently from 1 to 16383
///
///
/// 1 if success, otherwise error code returned in the case of (a) formatting error(s).
///
[DllImport("x86\\imageprocessor.libwebp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "WebPGetInfo")]
private static extern int WebPGetInfo86(IntPtr data, uint dataSize, out int width, out int height);
///
/// Validate the WebP image header and retrieve the image height and width. Pointers *width and *height can be passed NULL if deemed irrelevant
///
///
/// Pointer to WebP image data
///
///
/// This is the size of the memory block pointed to by data containing the image data
///
///
/// The width range is limited currently from 1 to 16383
///
///
/// The height range is limited currently from 1 to 16383
///
///
/// 1 if success, otherwise error code returned in the case of (a) formatting error(s).
///
[DllImport("x64\\imageprocessor.libwebp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "WebPGetInfo")]
private static extern int WebPGetInfo64(IntPtr data, uint dataSize, out int width, out int height);
///
/// Decode WEBP image pointed to by *data and returns BGR samples into a pre-allocated buffer
///
///
/// Pointer to WebP image data
///
///
/// This is the size of the memory block pointed to by data containing the image data
///
///
/// Pointer to decoded WebP image
///
///
/// Size of allocated buffer
///
///
/// Specifies the distance between scan-lines
///
///
/// output_buffer if function succeeds; NULL otherwise
///
[DllImport("x86\\imageprocessor.libwebp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "WebPDecodeBGRAInto")]
private static extern IntPtr WebPDecodeBGRAInto86(IntPtr data, uint dataSize, IntPtr outputBuffer, int outputBufferSize, int outputStride);
///
/// Decode WEBP image pointed to by *data and returns BGR samples into a pre-allocated buffer
///
///
/// Pointer to WebP image data
///
///
/// This is the size of the memory block pointed to by data containing the image data
///
///
/// Pointer to decoded WebP image
///
///
/// Size of allocated buffer
///
///
/// Specifies the distance between scan-lines
///
///
/// output_buffer if function succeeds; NULL otherwise
///
[DllImport("x64\\imageprocessor.libwebp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "WebPDecodeBGRAInto")]
private static extern IntPtr WebPDecodeBGRAInto64(IntPtr data, uint dataSize, IntPtr outputBuffer, int outputBufferSize, int outputStride);
///
/// Lossy encoding images pointed to by *data in WebP format
///
///
/// Pointer to RGB image data
///
///
/// The width range is limited currently from 1 to 16383
///
///
/// The height range is limited currently from 1 to 16383
///
///
/// The stride.
///
///
/// Ranges from 0 (lower quality) to 100 (highest quality). Controls the loss and quality during compression
///
///
/// output_buffer with WebP image
///
///
/// Size of WebP Image
///
[DllImport("x86\\imageprocessor.libwebp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "WebPEncodeBGRA")]
private static extern int WebPEncodeBGRA86(IntPtr rgb, int width, int height, int stride, float qualityFactor, out IntPtr output);
///
/// Lossy encoding images pointed to by *data in WebP format
///
///
/// Pointer to RGB image data
///
///
/// The width range is limited currently from 1 to 16383
///
///
/// The height range is limited currently from 1 to 16383
///
///
/// The stride.
///
///
/// Ranges from 0 (lower quality) to 100 (highest quality). Controls the loss and quality during compression
///
///
/// output_buffer with WebP image
///
///
/// Size of WebP Image
///
[DllImport("x64\\imageprocessor.libwebp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "WebPEncodeBGRA")]
private static extern int WebPEncodeBGRA64(IntPtr rgb, int width, int height, int stride, float qualityFactor, out IntPtr output);
///
/// Frees the unmanaged memory.
///
///
/// The pointer.
///
///
/// 1 if success, otherwise error code returned in the case of (a) error(s).
///
[DllImport("x86\\imageprocessor.libwebp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "WebPFree")]
private static extern int WebPFree86(IntPtr pointer);
///
/// Frees the unmanaged memory.
///
///
/// The pointer.
///
///
/// 1 if success, otherwise error code returned in the case of (a) error(s).
///
[DllImport("x64\\imageprocessor.libwebp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "WebPFree")]
private static extern int WebPFree64(IntPtr pointer);
#endregion
}
}