diff --git a/src/MaterialEditor.Base/MaterialEditor.Base.projitems b/src/MaterialEditor.Base/MaterialEditor.Base.projitems index dc999a54..af34208d 100644 --- a/src/MaterialEditor.Base/MaterialEditor.Base.projitems +++ b/src/MaterialEditor.Base/MaterialEditor.Base.projitems @@ -35,5 +35,8 @@ Never + + PreserveNewest + \ No newline at end of file diff --git a/src/MaterialEditor.Base/PluginBase.cs b/src/MaterialEditor.Base/PluginBase.cs index 6fd4cd9f..943faf19 100644 --- a/src/MaterialEditor.Base/PluginBase.cs +++ b/src/MaterialEditor.Base/PluginBase.cs @@ -21,8 +21,6 @@ public partial class MaterialEditorPluginBase : BaseUnityPlugin public static new ManualLogSource Logger; public static MaterialEditorPluginBase Instance; - internal const string FileFilter = "Images (*.png;.jpg;.apng;.gif)|*.png;*.jpg;*.apng;*.gif|All files|*.*"; - /// /// Path where textures will be exported /// diff --git a/src/MaterialEditor.Base/Resources/libwebp.lib b/src/MaterialEditor.Base/Resources/libwebp.lib new file mode 100644 index 00000000..21ceb3aa Binary files /dev/null and b/src/MaterialEditor.Base/Resources/libwebp.lib differ diff --git a/src/MaterialEditor.Base/UI/UI.cs b/src/MaterialEditor.Base/UI/UI.cs index 8926d652..8821bd22 100644 --- a/src/MaterialEditor.Base/UI/UI.cs +++ b/src/MaterialEditor.Base/UI/UI.cs @@ -546,7 +546,12 @@ void PopulateListMaterial(Material mat, Projector projector = null) }; textureItem.TextureOnImport = () => { - OpenFileDialog.Show(OnFileAccept, "Open image", Application.dataPath, FileFilter); +#if !API + string fileFilter = KK_Plugins.ImageHelper.FileFilter; +#else + string fileFilter = "Images (*.png;.jpg)|*.png;*.jpg|All files|*.*"; +#endif + OpenFileDialog.Show(OnFileAccept, "Open image", Application.dataPath, fileFilter); void OnFileAccept(string[] strings) { diff --git a/src/MaterialEditor.Core/Core.MaterialEditor.cs b/src/MaterialEditor.Core/Core.MaterialEditor.cs index 7d736ab0..24e33ea1 100644 --- a/src/MaterialEditor.Core/Core.MaterialEditor.cs +++ b/src/MaterialEditor.Core/Core.MaterialEditor.cs @@ -138,6 +138,9 @@ public override void Awake() { base.Awake(); + //Load any image loading dependencies before any images are actually ever loaded + ImageHelper.LoadDependencies(typeof(MaterialEditorPlugin)); + #if KK || EC || KKS RimRemover = Config.Bind("Config", "Remove Rim Lighting", false, new ConfigDescription("Remove rim lighting for all characters clothes, hair, accessories, etc. Will save modified values to the card.\n\nUse with caution as it cannot be undone except by manually resetting all the changes.", null, new ConfigurationManagerAttributes { Order = 0, IsAdvanced = true })); #endif diff --git a/src/Shared.TextureContainer/Shared.ImageHelper.cs b/src/Shared.TextureContainer/Shared.ImageHelper.cs new file mode 100644 index 00000000..a1fdf308 --- /dev/null +++ b/src/Shared.TextureContainer/Shared.ImageHelper.cs @@ -0,0 +1,127 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using UnityEngine; + +namespace KK_Plugins +{ + //Shamelessly stolen from https://stackoverflow.com/a/1246008 + public static class ImageHelper + { + /// + /// File filter for all the supported images + /// + public const string FileFilter = "Images (*.png;.jpg;.apng;.gif;.webp)|*.png;*.jpg;*.apng;*.gif;*.webp|All files|*.*"; + + /// + /// Dict containing the dll and dll file for all external dependencies needed to load all supported file formats + /// + private static readonly Dictionary dllDependencies = new Dictionary + { + { "webp", "libwebp.lib" } + }; + + /// + /// Read the file signature from a byte array to deterimine its file format. + /// https://www.garykessler.net/library/file_sigs.html + /// + /// Byte array containing the image + /// ImageFormat + public static ImageFormat GetContentType(byte[] imageBytes) + { + foreach (var kvPair in imageFormatDecoders.OrderByDescending(x => x.Key.Length)) + if (kvPair.Key.Length <= imageBytes.Length && imageBytes.StartsWith(kvPair.Key)) + return kvPair.Value; + return ImageFormat.Unrecognized; + } + + public static Texture2D LoadTexture2DFromBytes(byte[] texBytes, TextureFormat format, bool mipmaps) + { + Texture2D tex = null; + var imageFormat = GetContentType(texBytes); + + //Only use magic numbers for custom supported image formats. Let LoadImage handle png/jpg/unknown + if (imageFormat == ImageFormat.WebP) + { + tex = WebP.Texture2DExt.CreateTexture2DFromWebP(texBytes, mipmaps, false, out var error); + if (error != WebP.Error.Success) tex = null; + } + + //Always fall back to default load method if all others were skipped/failed + if (tex == null) + { + //LoadImage automatically resizes the texture so the texture size doesn't matter here + tex = new Texture2D(2, 2, format, mipmaps); + tex.LoadImage(texBytes); + } + return tex; + } + + /// + /// Load all needed dependencies for loading all the supported file formats. + /// Needs to be to be run before any classes using these dependencies are loaded! + /// + internal static void LoadDependencies(Type pluginType) + { + foreach(var dependency in dllDependencies) + try + { + LoadDependency(dependency.Key, dependency.Value, pluginType); + } + catch + { + System.Console.WriteLine($"Failed to load {dependency.Value}"); + } + } + + private static bool StartsWith(this byte[] thisBytes, byte[] thatBytes) + { + for (int i = 0; i < thatBytes.Length; i += 1) + { + if (thisBytes[i] != thatBytes[i]) + { + return false; + } + } + return true; + } + + //Site that lists magic numbers for file formats https://www.garykessler.net/library/file_sigs.html + private static Dictionary imageFormatDecoders = new Dictionary() + { + { new byte[]{ 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }, ImageFormat.Png }, + { new byte[]{ 0xff, 0xd8 }, ImageFormat.Jpeg }, + { new byte[]{ 0x52, 0x49, 0x46, 0x46 }, ImageFormat.WebP }, + { new byte[]{ 0x00, 0x00, 0x00 }, ImageFormat.Avif }, + { new byte[]{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, ImageFormat.Unrecognized}, + }; + + public enum ImageFormat + { + Png, + Jpeg, + WebP, + Avif, + Unrecognized + } + + + [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)] + private static extern IntPtr LoadLibrary(string fileName); + [DllImport("__Internal", CharSet = CharSet.Ansi)] + private static extern void mono_dllmap_insert(IntPtr assembly, string dll, string func, string tdll, string tfunc); + + private static void LoadDependency(string dllName, string dllFileName, Type pluginType) + { + var assemblyPath = Path.GetDirectoryName(pluginType.Assembly.Location); + var nativeDllPath = Path.Combine(assemblyPath, dllFileName); + if (LoadLibrary(nativeDllPath) == IntPtr.Zero) + throw new IOException($"Failed to load {nativeDllPath}, verify that the file exists and is not corrupted."); + // Needed to let the non-standard extension to work with dllimport + mono_dllmap_insert(IntPtr.Zero, dllName, null, dllFileName, null); + } + } +} diff --git a/src/Shared.TextureContainer/Shared.TextureContainer.projitems b/src/Shared.TextureContainer/Shared.TextureContainer.projitems index 72b30d78..c5a532bd 100644 --- a/src/Shared.TextureContainer/Shared.TextureContainer.projitems +++ b/src/Shared.TextureContainer/Shared.TextureContainer.projitems @@ -9,7 +9,12 @@ Shared.TextureContainer + + + + + \ No newline at end of file diff --git a/src/Shared.TextureContainer/Shared.TextureContainerManager.cs b/src/Shared.TextureContainer/Shared.TextureContainerManager.cs index b05d9dae..adc21e88 100644 --- a/src/Shared.TextureContainer/Shared.TextureContainerManager.cs +++ b/src/Shared.TextureContainer/Shared.TextureContainerManager.cs @@ -151,13 +151,11 @@ public static void Release( Token holder ) private static Texture TextureFromBytes(byte[] texBytes, TextureFormat format, bool mipmaps) { if (texBytes == null || texBytes.Length == 0) return null; - - //LoadImage automatically resizes the texture so the texture size doesn't matter here - Texture2D tex = new Texture2D(2, 2, format, mipmaps); + Texture2D tex = null; try { - tex.LoadImage(texBytes); + tex = ImageHelper.LoadTexture2DFromBytes(texBytes, format, mipmaps); //Transfer to GPU memory and delete data in normal memory RenderTexture rt = new RenderTexture(tex.width, tex.height, 0); diff --git a/src/Shared.TextureContainer/WebP/Error.cs b/src/Shared.TextureContainer/WebP/Error.cs new file mode 100644 index 00000000..4f23acf3 --- /dev/null +++ b/src/Shared.TextureContainer/WebP/Error.cs @@ -0,0 +1,16 @@ +//https://github.com/octo-code/webp-unity3d +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace WebP +{ + public enum Error + { + Success = 0, + + InvalidHeader = 20, + DecodingError = 30, + } +} diff --git a/src/Shared.TextureContainer/WebP/Info.cs b/src/Shared.TextureContainer/WebP/Info.cs new file mode 100644 index 00000000..8e801934 --- /dev/null +++ b/src/Shared.TextureContainer/WebP/Info.cs @@ -0,0 +1,28 @@ +//https://github.com/octo-code/webp-unity3d +using System; +using System.Text; +using System.Collections.Generic; + +namespace WebP +{ + public class Info + { + public static string GetDecoderVersion() + { + uint v = (uint)WebP.Extern.NativeBindings.WebPGetDecoderVersion(); + var revision = v % 256; + var minor = (v >> 8) % 256; + var major = (v >> 16) % 256; + return major + "." + minor + "." + revision; + } + + public static string GetEncoderVersion() + { + uint v = (uint)WebP.Extern.NativeBindings.WebPGetEncoderVersion(); + var revision = v % 256; + var minor = (v >> 8) % 256; + var major = (v >> 16) % 256; + return major + "." + minor + "." + revision; + } + } +} diff --git a/src/Shared.TextureContainer/WebP/LICENSE b/src/Shared.TextureContainer/WebP/LICENSE new file mode 100644 index 00000000..e06d2081 --- /dev/null +++ b/src/Shared.TextureContainer/WebP/LICENSE @@ -0,0 +1,202 @@ +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/src/Shared.TextureContainer/WebP/NativeBindings.cs b/src/Shared.TextureContainer/WebP/NativeBindings.cs new file mode 100644 index 00000000..1a687cda --- /dev/null +++ b/src/Shared.TextureContainer/WebP/NativeBindings.cs @@ -0,0 +1,2825 @@ +//https://github.com/octo-code/webp-unity3d +using System; +using System.Text; +using System.Collections.Generic; +using System.Runtime.InteropServices; + +using UnityEngine; + +#pragma warning disable 1591 + +namespace WebP.Extern +{ + [StructLayoutAttribute(LayoutKind.Sequential)] + public struct WebPIDecoder { } + + public enum WEBP_CSP_MODE + { + + /// MODE_RGB -> 0 + MODE_RGB = 0, + + /// MODE_RGBA -> 1 + MODE_RGBA = 1, + + /// MODE_BGR -> 2 + MODE_BGR = 2, + + /// MODE_BGRA -> 3 + MODE_BGRA = 3, + + /// MODE_ARGB -> 4 + MODE_ARGB = 4, + + /// MODE_RGBA_4444 -> 5 + MODE_RGBA_4444 = 5, + + /// MODE_RGB_565 -> 6 + MODE_RGB_565 = 6, + + /// MODE_rgbA -> 7 + MODE_rgbA = 7, + + /// MODE_bgrA -> 8 + MODE_bgrA = 8, + + /// MODE_Argb -> 9 + MODE_Argb = 9, + + /// MODE_rgbA_4444 -> 10 + MODE_rgbA_4444 = 10, + + /// MODE_YUV -> 11 + MODE_YUV = 11, + + /// MODE_YUVA -> 12 + MODE_YUVA = 12, + + /// MODE_LAST -> 13 + MODE_LAST = 13, + } + + //------------------------------------------------------------------------------ + // WebPDecBuffer: Generic structure for describing the output sample buffer. + + [StructLayoutAttribute(LayoutKind.Sequential)] + public struct WebPRGBABuffer + { + + /// uint8_t* + public IntPtr rgba; + + /// int + public int stride; + + /// size_t->unsigned int + public UIntPtr size; + } + + [StructLayoutAttribute(LayoutKind.Sequential)] + public struct WebPYUVABuffer + { + + /// uint8_t* + public IntPtr y; + + /// uint8_t* + public IntPtr u; + + /// uint8_t* + public IntPtr v; + + /// uint8_t* + public IntPtr a; + + /// int + public int y_stride; + + /// int + public int u_stride; + + /// int + public int v_stride; + + /// int + public int a_stride; + + /// size_t->unsigned int + public UIntPtr y_size; + + /// size_t->unsigned int + public UIntPtr u_size; + + /// size_t->unsigned int + public UIntPtr v_size; + + /// size_t->unsigned int + public UIntPtr a_size; + } + + [StructLayoutAttribute(LayoutKind.Explicit)] + public struct Anonymous_690ed5ec_4c3d_40c6_9bd0_0747b5a28b54 + { + + /// WebPRGBABuffer->Anonymous_47cdec86_3c1a_4b39_ab93_76bc7499076a + [FieldOffsetAttribute(0)] + public WebPRGBABuffer RGBA; + + /// WebPYUVABuffer->Anonymous_70de6e8e_c3ae_4506_bef0_c17f17a7e678 + [FieldOffsetAttribute(0)] + public WebPYUVABuffer YUVA; + } + + [StructLayoutAttribute(LayoutKind.Sequential)] + public struct WebPDecBuffer + { + + /// WEBP_CSP_MODE->Anonymous_cb136f5b_1d5d_49a0_aca4_656a79e9d159 + public WEBP_CSP_MODE colorspace; + + /// int + public int width; + + /// int + public int height; + + /// int + public int is_external_memory; + + /// Anonymous_690ed5ec_4c3d_40c6_9bd0_0747b5a28b54 + public Anonymous_690ed5ec_4c3d_40c6_9bd0_0747b5a28b54 u; + + /// uint32_t[4] + [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = UnmanagedType.U4)] + public uint[] pad; + + /// uint8_t* + public IntPtr private_memory; + } + + + //------------------------------------------------------------------------------ + // Enumeration of the status codes + + public enum VP8StatusCode + { + + /// VP8_STATUS_OK -> 0 + VP8_STATUS_OK = 0, + + VP8_STATUS_OUT_OF_MEMORY, + + VP8_STATUS_INVALID_PARAM, + + VP8_STATUS_BITSTREAM_ERROR, + + VP8_STATUS_UNSUPPORTED_FEATURE, + + VP8_STATUS_SUSPENDED, + + VP8_STATUS_USER_ABORT, + + VP8_STATUS_NOT_ENOUGH_DATA, + } + + [StructLayoutAttribute(LayoutKind.Sequential)] + public struct WebPBitstreamFeatures + { + + /// + /// Width in pixels, as read from the bitstream + /// + public int width; + + /// + /// Height in pixels, as read from the bitstream. + /// + public int height; + + /// + /// // True if the bitstream contains an alpha channel. + /// + public int has_alpha; + + /// + /// Unused for now - should be 0 + /// + public int bitstream_version; + + /// + /// If true, incremental decoding is not reccomended + /// + public int no_incremental_decoding; + + /// + /// Unused, should be 0 for now + /// + public int rotate; + + /// + /// Unused, should be 0 for now + /// + public int uv_sampling; + + /// + /// Padding for later use + /// + [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 3, ArraySubType = UnmanagedType.U4)] + public uint[] pad; + } + + // Decoding options + [StructLayoutAttribute(LayoutKind.Sequential)] + public struct WebPDecoderOptions + { + public int bypass_filtering; // if true, skip the in-loop filtering + public int no_fancy_upsampling; // if true, use faster pointwise upsampler + public int use_cropping; // if true, cropping is applied _first_ + public int crop_left, crop_top; // top-left position for cropping. + // Will be snapped to even values. + public int crop_width, crop_height; // dimension of the cropping area + public int use_scaling; // if true, scaling is applied _afterward_ + public int scaled_width, scaled_height; // final resolution + public int use_threads; // if true, use multi-threaded decoding + public int dithering_strength; // dithering strength (0=Off, 100=full) + + // Unused for now: + public int force_rotation; // forced rotation (to be applied _last_) + public int no_enhancement; // if true, discard enhancement layer + /// uint32_t[5] + [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 5, ArraySubType = UnmanagedType.U4)] + public uint[] pad; + }; + + [StructLayoutAttribute(LayoutKind.Sequential)] + public struct WebPDecoderConfig + { + + /// WebPBitstreamFeatures->Anonymous_c6b01f0b_3e38_4731_b2d6_9c0e3bdb71aa + public WebPBitstreamFeatures input; + + /// WebPDecBuffer->Anonymous_5c438b36_7de6_498e_934a_d3613b37f5fc + public WebPDecBuffer output; + + /// WebPDecoderOptions->Anonymous_78066979_3e1e_4d74_aee5_f316b20e3385 + public WebPDecoderOptions options; + } + + + public enum WebPImageHint + { + + /// WEBP_HINT_DEFAULT -> 0 + WEBP_HINT_DEFAULT = 0, + + WEBP_HINT_PICTURE, + + WEBP_HINT_PHOTO, + + WEBP_HINT_GRAPH, + + WEBP_HINT_LAST, + } + + [StructLayoutAttribute(LayoutKind.Sequential)] + public struct WebPConfig + { + + /// int + public int lossless; + + /// float + public float quality; + + /// int + public int method; + + /// WebPImageHint->Anonymous_838f22f5_6f57_48a0_9ecb_8eec917009f9 + public WebPImageHint image_hint; + + /// int + public int target_size; + + /// float + public float target_PSNR; + + /// int + public int segments; + + /// int + public int sns_strength; + + /// int + public int filter_strength; + + /// int + public int filter_sharpness; + + /// int + public int filter_type; + + /// int + public int autofilter; + + /// int + public int alpha_compression; + + /// int + public int alpha_filtering; + + /// int + public int alpha_quality; + + /// int + public int pass; + + /// int + public int show_compressed; + + /// int + public int preprocessing; + + /// int + public int partitions; + + /// int + public int partition_limit; + + public int emulate_jpeg_size; + + public int thread_level; + + public int low_memory; + + /// uint32_t[5] + [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 5, ArraySubType = UnmanagedType.U4)] + public uint[] pad; + } + + public enum WebPPreset + { + + /// WEBP_PRESET_DEFAULT -> 0 + WEBP_PRESET_DEFAULT = 0, + + WEBP_PRESET_PICTURE, + + WEBP_PRESET_PHOTO, + + WEBP_PRESET_DRAWING, + + WEBP_PRESET_ICON, + + WEBP_PRESET_TEXT, + } + + + + + + + [StructLayoutAttribute(LayoutKind.Sequential)] + public struct WebPAuxStats + { + + /// int + public int coded_size; + + /// float[5] + [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 5, ArraySubType = UnmanagedType.R4)] + public float[] PSNR; + + /// int[3] + [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 3, ArraySubType = UnmanagedType.I4)] + public int[] block_count; + + /// int[2] + [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 2, ArraySubType = UnmanagedType.I4)] + public int[] header_bytes; + + /// int[12] + [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 12, ArraySubType = UnmanagedType.I4)] + public int[] residual_bytes; + + /// int[4] + [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = UnmanagedType.I4)] + public int[] segment_size; + + /// int[4] + [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = UnmanagedType.I4)] + public int[] segment_quant; + + /// int[4] + [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = UnmanagedType.I4)] + public int[] segment_level; + + /// int + public int alpha_data_size; + + /// int + public int layer_data_size; + + /// uint32_t->unsigned int + public uint lossless_features; + + /// int + public int histogram_bits; + + /// int + public int transform_bits; + + /// int + public int cache_bits; + + /// int + public int palette_size; + + /// int + public int lossless_size; + + /// uint32_t[4] + [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = UnmanagedType.U4)] + public uint[] pad; + } + + + /// Return Type: int + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///picture: WebPPicture* + public delegate int WebPWriterFunction([InAttribute()] IntPtr data, UIntPtr data_size, ref WebPPicture picture); + + [StructLayoutAttribute(LayoutKind.Sequential)] + public struct WebPMemoryWriter + { + + /// uint8_t* + public IntPtr mem; + + /// size_t->unsigned int + public UIntPtr size; + + /// size_t->unsigned int + public UIntPtr max_size; + + /// uint32_t[1] + [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 1, ArraySubType = UnmanagedType.U4)] + public uint[] pad; + } + + + + /// Return Type: int + ///percent: int + ///picture: WebPPicture* + public delegate int WebPProgressHook(int percent, ref WebPPicture picture); + + public enum WebPEncCSP + { + + /// WEBP_YUV420 -> 0 + WEBP_YUV420 = 0, + + /// WEBP_YUV422 -> 1 + WEBP_YUV422 = 1, + + /// WEBP_YUV444 -> 2 + WEBP_YUV444 = 2, + + /// WEBP_YUV400 -> 3 + WEBP_YUV400 = 3, + + /// WEBP_CSP_UV_MASK -> 3 + WEBP_CSP_UV_MASK = 3, + + /// WEBP_YUV420A -> 4 + WEBP_YUV420A = 4, + + /// WEBP_YUV422A -> 5 + WEBP_YUV422A = 5, + + /// WEBP_YUV444A -> 6 + WEBP_YUV444A = 6, + + /// WEBP_YUV400A -> 7 + WEBP_YUV400A = 7, + + /// WEBP_CSP_ALPHA_BIT -> 4 + WEBP_CSP_ALPHA_BIT = 4, + } + + + public enum WebPEncodingError + { + + /// VP8_ENC_OK -> 0 + VP8_ENC_OK = 0, + + VP8_ENC_ERROR_OUT_OF_MEMORY, + + VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY, + + VP8_ENC_ERROR_NULL_PARAMETER, + + VP8_ENC_ERROR_INVALID_CONFIGURATION, + + VP8_ENC_ERROR_BAD_DIMENSION, + + VP8_ENC_ERROR_PARTITION0_OVERFLOW, + + VP8_ENC_ERROR_PARTITION_OVERFLOW, + + VP8_ENC_ERROR_BAD_WRITE, + + VP8_ENC_ERROR_FILE_TOO_BIG, + + VP8_ENC_ERROR_USER_ABORT, + + VP8_ENC_ERROR_LAST, + } + + + + [StructLayoutAttribute(LayoutKind.Sequential)] + public struct WebPPicture + { + + /// int + public int use_argb; + + /// WebPEncCSP->Anonymous_84ce7065_fe91_48b4_93d8_1f0e84319dba + public WebPEncCSP colorspace; + + /// int + public int width; + + /// int + public int height; + + /// uint8_t* + public IntPtr y; + + /// uint8_t* + public IntPtr u; + + /// uint8_t* + public IntPtr v; + + /// int + public int y_stride; + + /// int + public int uv_stride; + + /// uint8_t* + public IntPtr a; + + /// int + public int a_stride; + + /// uint32_t[2] + [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 2, ArraySubType = UnmanagedType.U4)] + public uint[] pad1; + + /// uint32_t* + public IntPtr argb; + + /// int + public int argb_stride; + + /// uint32_t[3] + [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 3, ArraySubType = UnmanagedType.U4)] + public uint[] pad2; + + /// WebPWriterFunction + public WebPWriterFunction writer; + + /// void* + public IntPtr custom_ptr; + + /// int + public int extra_info_type; + + /// uint8_t* + public IntPtr extra_info; + + /// WebPAuxStats* + public IntPtr stats; + + /// WebPEncodingError->Anonymous_8b714d63_f91b_46af_b0d0_667c703ed356 + public WebPEncodingError error_code; + + /// WebPProgressHook + public WebPProgressHook progress_hook; + + /// void* + public IntPtr user_data; + + /// uint32_t[3] + [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 3, ArraySubType = UnmanagedType.U4)] + public uint[] pad3; + + /// uint8_t* + public IntPtr u0; + + /// uint8_t* + public IntPtr v0; + + /// int + public int uv0_stride; + + /// uint32_t[7] + [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 7, ArraySubType = UnmanagedType.U4)] + public uint[] pad4; + + /// void* + public IntPtr memory_; + + /// void* + public IntPtr memory_argb_; + + /// void*[2] + [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 2, ArraySubType = UnmanagedType.SysUInt)] + public IntPtr[] pad5; + } + + public class NativeBindings + { + + /// WEBP_DECODER_ABI_VERSION 0x0203 // MAJOR(8b) + MINOR(8b) + public const int WEBP_DECODER_ABI_VERSION = 515; + + /// WEBP_ENCODER_ABI_VERSION 0x0202 // MAJOR(8b) + MINOR(8b) + public const int WEBP_ENCODER_ABI_VERSION = 514; + + /// + /// The maximum length of any dimension of a WebP image is 16383 + /// + public const int WEBP_MAX_DIMENSION = 16383; + + #region NATIVE_WRAPPERS + + + /// Return Type: int + public static int WebPGetDecoderVersion() + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPGetDecoderVersion() : + External.WebPGetDecoderVersion(); + } + + public static void WebPSafeFree(IntPtr toDeallocate) + { + if (Application.platform == RuntimePlatform.IPhonePlayer) + { + Internal.WebPSafeFree(toDeallocate); + } + else + { + External.WebPSafeFree(toDeallocate); + } + } + + + /// + /// Retrieve basic header information: width, height. + /// This function will also validate the header and return 0 in + /// case of formatting error. + /// Pointers 'width' and 'height' can be passed NULL if deemed irrelevant. + /// + /// + /// + /// + /// + /// + public static int WebPGetInfo([InAttribute()] IntPtr data, UIntPtr data_size, ref int width, ref int height) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPGetInfo(data, data_size, ref width, ref height) : + External.WebPGetInfo(data, data_size, ref width, ref height); + } + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///width: int* + ///height: int* + public static IntPtr WebPDecodeRGBA([InAttribute()] IntPtr data, UIntPtr data_size, ref int width, ref int height) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPDecodeRGBA(data, data_size, ref width, ref height) : + External.WebPDecodeRGBA(data, data_size, ref width, ref height); + } + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///width: int* + ///height: int* + public static IntPtr WebPDecodeARGB([InAttribute()] IntPtr data, UIntPtr data_size, ref int width, ref int height) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPDecodeARGB(data, data_size, ref width, ref height) : + External.WebPDecodeARGB(data, data_size, ref width, ref height); + } + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///width: int* + ///height: int* + public static IntPtr WebPDecodeBGRA([InAttribute()] IntPtr data, UIntPtr data_size, ref int width, ref int height) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPDecodeBGRA(data, data_size, ref width, ref height) : + External.WebPDecodeBGRA(data, data_size, ref width, ref height); + } + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///width: int* + ///height: int* + public static IntPtr WebPDecodeRGB([InAttribute()] IntPtr data, UIntPtr data_size, ref int width, ref int height) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPDecodeRGB(data, data_size, ref width, ref height) : + External.WebPDecodeRGB(data, data_size, ref width, ref height); + } + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///width: int* + ///height: int* + public static IntPtr WebPDecodeBGR([InAttribute()] IntPtr data, UIntPtr data_size, ref int width, ref int height) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPDecodeBGR(data, data_size, ref width, ref height) : + External.WebPDecodeBGR(data, data_size, ref width, ref height); + } + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///width: int* + ///height: int* + ///u: uint8_t** + ///v: uint8_t** + ///stride: int* + ///uv_stride: int* + public static IntPtr WebPDecodeYUV([InAttribute()] IntPtr data, UIntPtr data_size, ref int width, ref int height, ref IntPtr u, ref IntPtr v, ref int stride, ref int uv_stride) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPDecodeYUV(data, data_size, ref width, ref height, ref u, ref v, ref stride, ref uv_stride) : + External.WebPDecodeYUV(data, data_size, ref width, ref height, ref u, ref v, ref stride, ref uv_stride); + } + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///output_buffer: uint8_t* + ///output_buffer_size: size_t->unsigned int + ///output_stride: int + public static IntPtr WebPDecodeRGBAInto([InAttribute()] IntPtr data, UIntPtr data_size, IntPtr output_buffer, UIntPtr output_buffer_size, int output_stride) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPDecodeRGBAInto(data, data_size, output_buffer, output_buffer_size, output_stride) : + External.WebPDecodeRGBAInto(data, data_size, output_buffer, output_buffer_size, output_stride); + } + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///output_buffer: uint8_t* + ///output_buffer_size: size_t->unsigned int + ///output_stride: int + public static IntPtr WebPDecodeARGBInto([InAttribute()] IntPtr data, UIntPtr data_size, IntPtr output_buffer, UIntPtr output_buffer_size, int output_stride) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPDecodeARGBInto(data, data_size, output_buffer, output_buffer_size, output_stride) : + External.WebPDecodeARGBInto(data, data_size, output_buffer, output_buffer_size, output_stride); + } + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///output_buffer: uint8_t* + ///output_buffer_size: size_t->unsigned int + ///output_stride: int + public static IntPtr WebPDecodeBGRAInto([InAttribute()] IntPtr data, UIntPtr data_size, IntPtr output_buffer, UIntPtr output_buffer_size, int output_stride) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPDecodeBGRAInto(data, data_size, output_buffer, output_buffer_size, output_stride) : + External.WebPDecodeBGRAInto(data, data_size, output_buffer, output_buffer_size, output_stride); + } + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///output_buffer: uint8_t* + ///output_buffer_size: size_t->unsigned int + ///output_stride: int + public static IntPtr WebPDecodeRGBInto([InAttribute()] IntPtr data, UIntPtr data_size, IntPtr output_buffer, UIntPtr output_buffer_size, int output_stride) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPDecodeRGBInto(data, data_size, output_buffer, output_buffer_size, output_stride) : + External.WebPDecodeRGBInto(data, data_size, output_buffer, output_buffer_size, output_stride); + } + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///output_buffer: uint8_t* + ///output_buffer_size: size_t->unsigned int + ///output_stride: int + public static IntPtr WebPDecodeBGRInto([InAttribute()] IntPtr data, UIntPtr data_size, IntPtr output_buffer, UIntPtr output_buffer_size, int output_stride) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPDecodeBGRInto(data, data_size, output_buffer, output_buffer_size, output_stride) : + External.WebPDecodeBGRInto(data, data_size, output_buffer, output_buffer_size, output_stride); + } + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///luma: uint8_t* + ///luma_size: size_t->unsigned int + ///luma_stride: int + ///u: uint8_t* + ///u_size: size_t->unsigned int + ///u_stride: int + ///v: uint8_t* + ///v_size: size_t->unsigned int + ///v_stride: int + public static IntPtr WebPDecodeYUVInto([InAttribute()] IntPtr data, UIntPtr data_size, IntPtr luma, UIntPtr luma_size, int luma_stride, IntPtr u, UIntPtr u_size, int u_stride, IntPtr v, UIntPtr v_size, int v_stride) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPDecodeYUVInto(data, data_size, luma, luma_size, luma_stride, u, u_size, u_stride, v, v_size, v_stride) : + External.WebPDecodeYUVInto(data, data_size, luma, luma_size, luma_stride, u, u_size, u_stride, v, v_size, v_stride); + } + + + /// Return Type: int + ///param0: WebPDecBuffer* + ///param1: int + public static int WebPInitDecBufferInternal(ref WebPDecBuffer param0, int param1) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPInitDecBufferInternal(ref param0, param1) : + External.WebPInitDecBufferInternal(ref param0, param1); + } + + + /// Return Type: void + ///buffer: WebPDecBuffer* + public static void WebPFreeDecBuffer(ref WebPDecBuffer buffer) + { + if (Application.platform == RuntimePlatform.IPhonePlayer) + { + Internal.WebPFreeDecBuffer(ref buffer); + } + else + { + External.WebPFreeDecBuffer(ref buffer); + } + } + + + /// Return Type: WebPIDecoder* + ///output_buffer: WebPDecBuffer* + public static IntPtr WebPINewDecoder(ref WebPDecBuffer output_buffer) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPINewDecoder(ref output_buffer) : + External.WebPINewDecoder(ref output_buffer); + } + + + /// Return Type: WebPIDecoder* + ///csp: WEBP_CSP_MODE->Anonymous_cb136f5b_1d5d_49a0_aca4_656a79e9d159 + ///output_buffer: uint8_t* + ///output_buffer_size: size_t->unsigned int + ///output_stride: int + public static IntPtr WebPINewRGB(WEBP_CSP_MODE csp, IntPtr output_buffer, UIntPtr output_buffer_size, int output_stride) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPINewRGB(csp, output_buffer, output_buffer_size, output_stride) : + External.WebPINewRGB(csp, output_buffer, output_buffer_size, output_stride); + } + + /// Return Type: WebPIDecoder* + ///luma: uint8_t* + ///luma_size: size_t->unsigned int + ///luma_stride: int + ///u: uint8_t* + ///u_size: size_t->unsigned int + ///u_stride: int + ///v: uint8_t* + ///v_size: size_t->unsigned int + ///v_stride: int + ///a: uint8_t* + ///a_size: size_t->unsigned int + ///a_stride: int + public static IntPtr WebPINewYUVA(IntPtr luma, UIntPtr luma_size, int luma_stride, IntPtr u, UIntPtr u_size, int u_stride, IntPtr v, UIntPtr v_size, int v_stride, IntPtr a, UIntPtr a_size, int a_stride) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPINewYUVA(luma, luma_size, luma_stride, u, u_size, u_stride, v, v_size, v_stride, a, a_size, a_stride) : + External.WebPINewYUVA(luma, luma_size, luma_stride, u, u_size, u_stride, v, v_size, v_stride, a, a_size, a_stride); + } + + + /// Return Type: WebPIDecoder* + ///luma: uint8_t* + ///luma_size: size_t->unsigned int + ///luma_stride: int + ///u: uint8_t* + ///u_size: size_t->unsigned int + ///u_stride: int + ///v: uint8_t* + ///v_size: size_t->unsigned int + ///v_stride: int + public static IntPtr WebPINewYUV(IntPtr luma, UIntPtr luma_size, int luma_stride, IntPtr u, UIntPtr u_size, int u_stride, IntPtr v, UIntPtr v_size, int v_stride) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPINewYUV(luma, luma_size, luma_stride, u, u_size, u_stride, v, v_size, v_stride) : + External.WebPINewYUV(luma, luma_size, luma_stride, u, u_size, u_stride, v, v_size, v_stride); + } + + /// Return Type: void + ///idec: WebPIDecoder* + public static void WebPIDelete(ref WebPIDecoder idec) + { + if (Application.platform == RuntimePlatform.IPhonePlayer) + { + Internal.WebPIDelete(ref idec); + } + else + { + External.WebPIDelete(ref idec); + } + } + + /// Return Type: VP8StatusCode->Anonymous_b244cc15_fbc7_4c41_8884_71fe4f515cd6 + ///idec: WebPIDecoder* + ///data: uint8_t* + ///data_size: size_t->unsigned int + public static VP8StatusCode WebPIAppend(ref WebPIDecoder idec, [InAttribute()] IntPtr data, UIntPtr data_size) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPIAppend(ref idec, data, data_size) : + External.WebPIAppend(ref idec, data, data_size); + } + + + /// Return Type: VP8StatusCode->Anonymous_b244cc15_fbc7_4c41_8884_71fe4f515cd6 + ///idec: WebPIDecoder* + ///data: uint8_t* + ///data_size: size_t->unsigned int + public static VP8StatusCode WebPIUpdate(ref WebPIDecoder idec, [InAttribute()] IntPtr data, UIntPtr data_size) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPIUpdate(ref idec, data, data_size) : + External.WebPIUpdate(ref idec, data, data_size); + } + + + /// Return Type: uint8_t* + ///idec: WebPIDecoder* + ///last_y: int* + ///width: int* + ///height: int* + ///stride: int* + public static IntPtr WebPIDecGetRGB(ref WebPIDecoder idec, ref int last_y, ref int width, ref int height, ref int stride) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPIDecGetRGB(ref idec, ref last_y, ref width, ref height, ref stride) : + External.WebPIDecGetRGB(ref idec, ref last_y, ref width, ref height, ref stride); + } + + + /// Return Type: uint8_t* + ///idec: WebPIDecoder* + ///last_y: int* + ///u: uint8_t** + ///v: uint8_t** + ///a: uint8_t** + ///width: int* + ///height: int* + ///stride: int* + ///uv_stride: int* + ///a_stride: int* + public static IntPtr WebPIDecGetYUVA(ref WebPIDecoder idec, ref int last_y, ref IntPtr u, ref IntPtr v, ref IntPtr a, ref int width, ref int height, ref int stride, ref int uv_stride, ref int a_stride) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPIDecGetYUVA(ref idec, ref last_y, ref u, ref v, ref a, ref width, ref height, ref stride, ref uv_stride, ref a_stride) : + External.WebPIDecGetYUVA(ref idec, ref last_y, ref u, ref v, ref a, ref width, ref height, ref stride, ref uv_stride, ref a_stride); + } + + + /// Return Type: WebPDecBuffer* + ///idec: WebPIDecoder* + ///left: int* + ///top: int* + ///width: int* + ///height: int* + public static IntPtr WebPIDecodedArea(ref WebPIDecoder idec, ref int left, ref int top, ref int width, ref int height) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPIDecodedArea(ref idec, ref left, ref top, ref width, ref height) : + External.WebPIDecodedArea(ref idec, ref left, ref top, ref width, ref height); + } + + + /// Return Type: VP8StatusCode->Anonymous_b244cc15_fbc7_4c41_8884_71fe4f515cd6 + ///param0: uint8_t* + ///param1: size_t->unsigned int + ///param2: WebPBitstreamFeatures* + ///param3: int + public static VP8StatusCode WebPGetFeaturesInternal([InAttribute()] IntPtr param0, UIntPtr param1, ref WebPBitstreamFeatures param2, int param3) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPGetFeaturesInternal(param0, param1, ref param2, param3) : + External.WebPGetFeaturesInternal(param0, param1, ref param2, param3); + } + + + /// Return Type: int + ///param0: WebPDecoderConfig* + ///param1: int + public static int WebPInitDecoderConfigInternal(ref WebPDecoderConfig param0, int param1) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPInitDecoderConfigInternal(ref param0, param1) : + External.WebPInitDecoderConfigInternal(ref param0, param1); + } + + + /// Return Type: WebPIDecoder* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///config: WebPDecoderConfig* + public static IntPtr WebPIDecode([InAttribute()] IntPtr data, UIntPtr data_size, ref WebPDecoderConfig config) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPIDecode(data, data_size, ref config) : + External.WebPIDecode(data, data_size, ref config); + } + + + /// Return Type: VP8StatusCode->Anonymous_b244cc15_fbc7_4c41_8884_71fe4f515cd6 + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///config: WebPDecoderConfig* + public static VP8StatusCode WebPDecode([InAttribute()] IntPtr data, UIntPtr data_size, ref WebPDecoderConfig config) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPDecode(data, data_size, ref config) : + External.WebPDecode(data, data_size, ref config); + } + + + /// Return Type: int + public static int WebPGetEncoderVersion() + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPGetEncoderVersion() : + External.WebPGetEncoderVersion(); + } + + + /// Return Type: size_t->unsigned int + ///rgb: uint8_t* + ///width: int + ///height: int + ///stride: int + ///quality_factor: float + ///output: uint8_t** + public static UIntPtr WebPEncodeRGB([InAttribute()] IntPtr rgb, int width, int height, int stride, float quality_factor, ref IntPtr output) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPEncodeRGB(rgb, width, height, stride, quality_factor, ref output) : + External.WebPEncodeRGB(rgb, width, height, stride, quality_factor, ref output); + } + + + /// Return Type: size_t->unsigned int + ///bgr: uint8_t* + ///width: int + ///height: int + ///stride: int + ///quality_factor: float + ///output: uint8_t** + public static UIntPtr WebPEncodeBGR([InAttribute()] IntPtr bgr, int width, int height, int stride, float quality_factor, ref IntPtr output) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPEncodeBGR(bgr, width, height, stride, quality_factor, ref output) : + External.WebPEncodeBGR(bgr, width, height, stride, quality_factor, ref output); + } + + + /// Return Type: size_t->unsigned int + ///rgba: uint8_t* + ///width: int + ///height: int + ///stride: int + ///quality_factor: float + ///output: uint8_t** + public static UIntPtr WebPEncodeRGBA([InAttribute()] IntPtr rgba, int width, int height, int stride, float quality_factor, ref IntPtr output) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPEncodeRGBA(rgba, width, height, stride, quality_factor, ref output) : + External.WebPEncodeRGBA(rgba, width, height, stride, quality_factor, ref output); + } + + + /// Return Type: size_t->unsigned int + ///bgra: uint8_t* + ///width: int + ///height: int + ///stride: int + ///quality_factor: float + ///output: uint8_t** + public static IntPtr WebPEncodeBGRA([InAttribute()] IntPtr bgra, int width, int height, int stride, float quality_factor, ref IntPtr output) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPEncodeBGRA(bgra, width, height, stride, quality_factor, ref output) : + External.WebPEncodeBGRA(bgra, width, height, stride, quality_factor, ref output); + } + + + /// Return Type: size_t->unsigned int + ///rgb: uint8_t* + ///width: int + ///height: int + ///stride: int + ///output: uint8_t** + public static UIntPtr WebPEncodeLosslessRGB([InAttribute()] IntPtr rgb, int width, int height, int stride, ref IntPtr output) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPEncodeLosslessRGB(rgb, width, height, stride, ref output) : + External.WebPEncodeLosslessRGB(rgb, width, height, stride, ref output); + } + + + /// Return Type: size_t->unsigned int + ///bgr: uint8_t* + ///width: int + ///height: int + ///stride: int + ///output: uint8_t** + public static UIntPtr WebPEncodeLosslessBGR([InAttribute()] IntPtr bgr, int width, int height, int stride, ref IntPtr output) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPEncodeLosslessBGR(bgr, width, height, stride, ref output) : + External.WebPEncodeLosslessBGR(bgr, width, height, stride, ref output); + } + + + /// Return Type: size_t->unsigned int + ///rgba: uint8_t* + ///width: int + ///height: int + ///stride: int + ///output: uint8_t** + public static UIntPtr WebPEncodeLosslessRGBA([InAttribute()] IntPtr rgba, int width, int height, int stride, ref IntPtr output) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPEncodeLosslessRGBA(rgba, width, height, stride, ref output) : + External.WebPEncodeLosslessRGBA(rgba, width, height, stride, ref output); + } + + + /// Return Type: size_t->unsigned int + ///bgra: uint8_t* + ///width: int + ///height: int + ///stride: int + ///output: uint8_t** + public static UIntPtr WebPEncodeLosslessBGRA([InAttribute()] IntPtr bgra, int width, int height, int stride, ref IntPtr output) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPEncodeLosslessBGRA(bgra, width, height, stride, ref output) : + External.WebPEncodeLosslessBGRA(bgra, width, height, stride, ref output); + } + + + /// Return Type: int + ///param0: WebPConfig* + ///param1: WebPPreset->Anonymous_017d4167_f53e_4b3d_b029_592ff5c3f80b + ///param2: float + ///param3: int + public static int WebPConfigInitInternal(ref WebPConfig param0, WebPPreset param1, float param2, int param3) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPConfigInitInternal(ref param0, param1, param2, param3) : + External.WebPConfigInitInternal(ref param0, param1, param2, param3); + } + + + /// Return Type: int + ///config: WebPConfig* + public static int WebPValidateConfig(ref WebPConfig config) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPValidateConfig(ref config) : + External.WebPValidateConfig(ref config); + } + + + /// Return Type: void + ///writer: WebPMemoryWriter* + public static void WebPMemoryWriterInit(ref WebPMemoryWriter writer) + { + if (Application.platform == RuntimePlatform.IPhonePlayer) + { + Internal.WebPMemoryWriterInit(ref writer); + } + else + { + External.WebPMemoryWriterInit(ref writer); + } + } + + + /// Return Type: int + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///picture: WebPPicture* + public static int WebPMemoryWrite([InAttribute()] IntPtr data, UIntPtr data_size, ref WebPPicture picture) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPMemoryWrite(data, data_size, ref picture) : + External.WebPMemoryWrite(data, data_size, ref picture); + } + + + /// Return Type: int + ///param0: WebPPicture* + ///param1: int + public static int WebPPictureInitInternal(ref WebPPicture param0, int param1) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPPictureInitInternal(ref param0, param1) : + External.WebPPictureInitInternal(ref param0, param1); + } + + + /// Return Type: int + ///picture: WebPPicture* + public static int WebPPictureAlloc(ref WebPPicture picture) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPPictureAlloc(ref picture) : + External.WebPPictureAlloc(ref picture); + } + + + /// Return Type: void + ///picture: WebPPicture* + public static void WebPPictureFree(ref WebPPicture picture) + { + if (Application.platform == RuntimePlatform.IPhonePlayer) + { + Internal.WebPPictureFree(ref picture); + } + else + { + External.WebPPictureFree(ref picture); + } + } + + + /// Return Type: int + ///src: WebPPicture* + ///dst: WebPPicture* + public static int WebPPictureCopy(ref WebPPicture src, ref WebPPicture dst) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPPictureCopy(ref src, ref dst) : + External.WebPPictureCopy(ref src, ref dst); + } + + /// Return Type: int + ///pic1: WebPPicture* + ///pic2: WebPPicture* + ///metric_type: int + ///result: float* result[5] + /// + + /// + /// Compute PSNR, SSIM or LSIM distortion metric between two pictures. + /// Result is in dB, stores in result[] in the Y/U/V/Alpha/All order. + /// Returns false in case of error (src and ref don't have same dimension, ...) + /// Warning: this function is rather CPU-intensive. + /// + /// + /// + /// 0 = PSNR, 1 = SSIM, 2 = LSIM + /// + /// + public static int WebPPictureDistortion(ref WebPPicture src, ref WebPPicture reference, int metric_type, ref float result) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPPictureDistortion(ref src, ref reference, metric_type, ref result) : + External.WebPPictureDistortion(ref src, ref reference, metric_type, ref result); + } + + /// Return Type: int + ///picture: WebPPicture* + ///left: int + ///top: int + ///width: int + ///height: int + public static int WebPPictureCrop(ref WebPPicture picture, int left, int top, int width, int height) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPPictureCrop(ref picture, left, top, width, height) : + External.WebPPictureCrop(ref picture, left, top, width, height); + } + + /// Return Type: int + ///src: WebPPicture* + ///left: int + ///top: int + ///width: int + ///height: int + ///dst: WebPPicture* + public static int WebPPictureView(ref WebPPicture src, int left, int top, int width, int height, ref WebPPicture dst) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPPictureView(ref src, left, top, width, height, ref dst) : + External.WebPPictureView(ref src, left, top, width, height, ref dst); + } + + /// Return Type: int + ///picture: WebPPicture* + public static int WebPPictureIsView(ref WebPPicture picture) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPPictureIsView(ref picture) : + External.WebPPictureIsView(ref picture); + } + + + /// Return Type: int + ///pic: WebPPicture* + ///width: int + ///height: int + public static int WebPPictureRescale(ref WebPPicture pic, int width, int height) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPPictureRescale(ref pic, width, height) : + External.WebPPictureRescale(ref pic, width, height); + } + + /// Return Type: int + ///picture: WebPPicture* + ///rgb: uint8_t* + ///rgb_stride: int + public static int WebPPictureImportRGB(ref WebPPicture picture, [InAttribute()] IntPtr rgb, int rgb_stride) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPPictureImportRGB(ref picture, rgb, rgb_stride) : + External.WebPPictureImportRGB(ref picture, rgb, rgb_stride); + } + + + /// Return Type: int + ///picture: WebPPicture* + ///rgba: uint8_t* + ///rgba_stride: int + public static int WebPPictureImportRGBA(ref WebPPicture picture, [InAttribute()] IntPtr rgba, int rgba_stride) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPPictureImportRGBA(ref picture, rgba, rgba_stride) : + External.WebPPictureImportRGBA(ref picture, rgba, rgba_stride); + } + + + /// Return Type: int + ///picture: WebPPicture* + ///rgbx: uint8_t* + ///rgbx_stride: int + public static int WebPPictureImportRGBX(ref WebPPicture picture, [InAttribute()] IntPtr rgbx, int rgbx_stride) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPPictureImportRGBX(ref picture, rgbx, rgbx_stride) : + External.WebPPictureImportRGBX(ref picture, rgbx, rgbx_stride); + } + + + /// Return Type: int + ///picture: WebPPicture* + ///bgr: uint8_t* + ///bgr_stride: int + public static int WebPPictureImportBGR(ref WebPPicture picture, [InAttribute()] IntPtr bgr, int bgr_stride) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPPictureImportBGR(ref picture, bgr, bgr_stride) : + External.WebPPictureImportBGR(ref picture, bgr, bgr_stride); + } + + + /// Return Type: int + ///picture: WebPPicture* + ///bgra: uint8_t* + ///bgra_stride: int + public static int WebPPictureImportBGRA(ref WebPPicture picture, [InAttribute()] IntPtr bgra, int bgra_stride) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPPictureImportBGRA(ref picture, bgra, bgra_stride) : + External.WebPPictureImportBGRA(ref picture, bgra, bgra_stride); + } + + + /// Return Type: int + ///picture: WebPPicture* + ///bgrx: uint8_t* + ///bgrx_stride: int + public static int WebPPictureImportBGRX(ref WebPPicture picture, [InAttribute()] IntPtr bgrx, int bgrx_stride) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPPictureImportBGRX(ref picture, bgrx, bgrx_stride) : + External.WebPPictureImportBGRX(ref picture, bgrx, bgrx_stride); + } + + + /// Return Type: int + ///picture: WebPPicture* + ///colorspace: WebPEncCSP->Anonymous_84ce7065_fe91_48b4_93d8_1f0e84319dba + public static int WebPPictureARGBToYUVA(ref WebPPicture picture, WebPEncCSP colorspace) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPPictureARGBToYUVA(ref picture, colorspace) : + External.WebPPictureARGBToYUVA(ref picture, colorspace); + } + + + /// Return Type: int + ///picture: WebPPicture* + public static int WebPPictureYUVAToARGB(ref WebPPicture picture) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPPictureYUVAToARGB(ref picture) : + External.WebPPictureYUVAToARGB(ref picture); + } + + + /// Return Type: void + ///picture: WebPPicture* + public static void WebPCleanupTransparentArea(ref WebPPicture picture) + { + if (Application.platform == RuntimePlatform.IPhonePlayer) + { + Internal.WebPCleanupTransparentArea(ref picture); + } + else + { + External.WebPCleanupTransparentArea(ref picture); + } + } + + + /// Return Type: int + ///picture: WebPPicture* + public static int WebPPictureHasTransparency(ref WebPPicture picture) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPPictureHasTransparency(ref picture) : + External.WebPPictureHasTransparency(ref picture); + } + + /// Return Type: int + ///config: WebPConfig* + ///picture: WebPPicture* + public static int WebPEncode(ref WebPConfig config, ref WebPPicture picture) + { + return (Application.platform == RuntimePlatform.IPhonePlayer) ? + Internal.WebPEncode(ref config, ref picture) : + External.WebPEncode(ref config, ref picture); + } + + #endregion + + #region INTERNAL_DLL_IMPORT + + class Internal + { + /// Return Type: int + [DllImportAttribute("__Internal", EntryPoint = "WebPGetDecoderVersion")] + public static extern int WebPGetDecoderVersion(); + + [DllImportAttribute("__Internal", EntryPoint = "WebPSafeFree")] + public static extern void WebPSafeFree(IntPtr toDeallocate); + + + /// + /// Retrieve basic header information: width, height. + /// This function will also validate the header and return 0 in + /// case of formatting error. + /// Pointers 'width' and 'height' can be passed NULL if deemed irrelevant. + /// + /// + /// + /// + /// + /// + [DllImportAttribute("__Internal", EntryPoint = "WebPGetInfo")] + public static extern int WebPGetInfo([InAttribute()] IntPtr data, UIntPtr data_size, ref int width, ref int height); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///width: int* + ///height: int* + [DllImportAttribute("__Internal", EntryPoint = "WebPDecodeRGBA")] + public static extern IntPtr WebPDecodeRGBA([InAttribute()] IntPtr data, UIntPtr data_size, ref int width, ref int height); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///width: int* + ///height: int* + [DllImportAttribute("__Internal", EntryPoint = "WebPDecodeARGB")] + public static extern IntPtr WebPDecodeARGB([InAttribute()] IntPtr data, UIntPtr data_size, ref int width, ref int height); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///width: int* + ///height: int* + [DllImportAttribute("__Internal", EntryPoint = "WebPDecodeBGRA")] + public static extern IntPtr WebPDecodeBGRA([InAttribute()] IntPtr data, UIntPtr data_size, ref int width, ref int height); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///width: int* + ///height: int* + [DllImportAttribute("__Internal", EntryPoint = "WebPDecodeRGB")] + public static extern IntPtr WebPDecodeRGB([InAttribute()] IntPtr data, UIntPtr data_size, ref int width, ref int height); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///width: int* + ///height: int* + [DllImportAttribute("__Internal", EntryPoint = "WebPDecodeBGR")] + public static extern IntPtr WebPDecodeBGR([InAttribute()] IntPtr data, UIntPtr data_size, ref int width, ref int height); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///width: int* + ///height: int* + ///u: uint8_t** + ///v: uint8_t** + ///stride: int* + ///uv_stride: int* + [DllImportAttribute("__Internal", EntryPoint = "WebPDecodeYUV")] + public static extern IntPtr WebPDecodeYUV([InAttribute()] IntPtr data, UIntPtr data_size, ref int width, ref int height, ref IntPtr u, ref IntPtr v, ref int stride, ref int uv_stride); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///output_buffer: uint8_t* + ///output_buffer_size: size_t->unsigned int + ///output_stride: int + [DllImportAttribute("__Internal", EntryPoint = "WebPDecodeRGBAInto")] + public static extern IntPtr WebPDecodeRGBAInto([InAttribute()] IntPtr data, UIntPtr data_size, IntPtr output_buffer, UIntPtr output_buffer_size, int output_stride); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///output_buffer: uint8_t* + ///output_buffer_size: size_t->unsigned int + ///output_stride: int + [DllImportAttribute("__Internal", EntryPoint = "WebPDecodeARGBInto")] + public static extern IntPtr WebPDecodeARGBInto([InAttribute()] IntPtr data, UIntPtr data_size, IntPtr output_buffer, UIntPtr output_buffer_size, int output_stride); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///output_buffer: uint8_t* + ///output_buffer_size: size_t->unsigned int + ///output_stride: int + [DllImportAttribute("__Internal", EntryPoint = "WebPDecodeBGRAInto")] + public static extern IntPtr WebPDecodeBGRAInto([InAttribute()] IntPtr data, UIntPtr data_size, IntPtr output_buffer, UIntPtr output_buffer_size, int output_stride); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///output_buffer: uint8_t* + ///output_buffer_size: size_t->unsigned int + ///output_stride: int + [DllImportAttribute("__Internal", EntryPoint = "WebPDecodeRGBInto")] + public static extern IntPtr WebPDecodeRGBInto([InAttribute()] IntPtr data, UIntPtr data_size, IntPtr output_buffer, UIntPtr output_buffer_size, int output_stride); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///output_buffer: uint8_t* + ///output_buffer_size: size_t->unsigned int + ///output_stride: int + [DllImportAttribute("__Internal", EntryPoint = "WebPDecodeBGRInto")] + public static extern IntPtr WebPDecodeBGRInto([InAttribute()] IntPtr data, UIntPtr data_size, IntPtr output_buffer, UIntPtr output_buffer_size, int output_stride); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///luma: uint8_t* + ///luma_size: size_t->unsigned int + ///luma_stride: int + ///u: uint8_t* + ///u_size: size_t->unsigned int + ///u_stride: int + ///v: uint8_t* + ///v_size: size_t->unsigned int + ///v_stride: int + [DllImportAttribute("__Internal", EntryPoint = "WebPDecodeYUVInto")] + public static extern IntPtr WebPDecodeYUVInto([InAttribute()] IntPtr data, UIntPtr data_size, IntPtr luma, UIntPtr luma_size, int luma_stride, IntPtr u, UIntPtr u_size, int u_stride, IntPtr v, UIntPtr v_size, int v_stride); + + + /// Return Type: int + ///param0: WebPDecBuffer* + ///param1: int + [DllImportAttribute("__Internal", EntryPoint = "WebPInitDecBufferInternal")] + public static extern int WebPInitDecBufferInternal(ref WebPDecBuffer param0, int param1); + + + /// Return Type: void + ///buffer: WebPDecBuffer* + [DllImportAttribute("__Internal", EntryPoint = "WebPFreeDecBuffer")] + public static extern void WebPFreeDecBuffer(ref WebPDecBuffer buffer); + + + /// Return Type: WebPIDecoder* + ///output_buffer: WebPDecBuffer* + [DllImportAttribute("__Internal", EntryPoint = "WebPINewDecoder")] + public static extern IntPtr WebPINewDecoder(ref WebPDecBuffer output_buffer); + + + /// Return Type: WebPIDecoder* + ///csp: WEBP_CSP_MODE->Anonymous_cb136f5b_1d5d_49a0_aca4_656a79e9d159 + ///output_buffer: uint8_t* + ///output_buffer_size: size_t->unsigned int + ///output_stride: int + [DllImportAttribute("__Internal", EntryPoint = "WebPINewRGB")] + public static extern IntPtr WebPINewRGB(WEBP_CSP_MODE csp, IntPtr output_buffer, UIntPtr output_buffer_size, int output_stride); + + + /// Return Type: WebPIDecoder* + ///luma: uint8_t* + ///luma_size: size_t->unsigned int + ///luma_stride: int + ///u: uint8_t* + ///u_size: size_t->unsigned int + ///u_stride: int + ///v: uint8_t* + ///v_size: size_t->unsigned int + ///v_stride: int + ///a: uint8_t* + ///a_size: size_t->unsigned int + ///a_stride: int + [DllImportAttribute("__Internal", EntryPoint = "WebPINewYUVA")] + public static extern IntPtr WebPINewYUVA(IntPtr luma, UIntPtr luma_size, int luma_stride, IntPtr u, UIntPtr u_size, int u_stride, IntPtr v, UIntPtr v_size, int v_stride, IntPtr a, UIntPtr a_size, int a_stride); + + + /// Return Type: WebPIDecoder* + ///luma: uint8_t* + ///luma_size: size_t->unsigned int + ///luma_stride: int + ///u: uint8_t* + ///u_size: size_t->unsigned int + ///u_stride: int + ///v: uint8_t* + ///v_size: size_t->unsigned int + ///v_stride: int + [DllImportAttribute("__Internal", EntryPoint = "WebPINewYUV")] + public static extern IntPtr WebPINewYUV(IntPtr luma, UIntPtr luma_size, int luma_stride, IntPtr u, UIntPtr u_size, int u_stride, IntPtr v, UIntPtr v_size, int v_stride); + + + /// Return Type: void + ///idec: WebPIDecoder* + [DllImportAttribute("__Internal", EntryPoint = "WebPIDelete")] + public static extern void WebPIDelete(ref WebPIDecoder idec); + + + /// Return Type: VP8StatusCode->Anonymous_b244cc15_fbc7_4c41_8884_71fe4f515cd6 + ///idec: WebPIDecoder* + ///data: uint8_t* + ///data_size: size_t->unsigned int + [DllImportAttribute("__Internal", EntryPoint = "WebPIAppend")] + public static extern VP8StatusCode WebPIAppend(ref WebPIDecoder idec, [InAttribute()] IntPtr data, UIntPtr data_size); + + + /// Return Type: VP8StatusCode->Anonymous_b244cc15_fbc7_4c41_8884_71fe4f515cd6 + ///idec: WebPIDecoder* + ///data: uint8_t* + ///data_size: size_t->unsigned int + [DllImportAttribute("__Internal", EntryPoint = "WebPIUpdate")] + public static extern VP8StatusCode WebPIUpdate(ref WebPIDecoder idec, [InAttribute()] IntPtr data, UIntPtr data_size); + + + /// Return Type: uint8_t* + ///idec: WebPIDecoder* + ///last_y: int* + ///width: int* + ///height: int* + ///stride: int* + [DllImportAttribute("__Internal", EntryPoint = "WebPIDecGetRGB")] + public static extern IntPtr WebPIDecGetRGB(ref WebPIDecoder idec, ref int last_y, ref int width, ref int height, ref int stride); + + + /// Return Type: uint8_t* + ///idec: WebPIDecoder* + ///last_y: int* + ///u: uint8_t** + ///v: uint8_t** + ///a: uint8_t** + ///width: int* + ///height: int* + ///stride: int* + ///uv_stride: int* + ///a_stride: int* + [DllImportAttribute("__Internal", EntryPoint = "WebPIDecGetYUVA")] + public static extern IntPtr WebPIDecGetYUVA(ref WebPIDecoder idec, ref int last_y, ref IntPtr u, ref IntPtr v, ref IntPtr a, ref int width, ref int height, ref int stride, ref int uv_stride, ref int a_stride); + + + /// Return Type: WebPDecBuffer* + ///idec: WebPIDecoder* + ///left: int* + ///top: int* + ///width: int* + ///height: int* + [DllImportAttribute("__Internal", EntryPoint = "WebPIDecodedArea")] + public static extern IntPtr WebPIDecodedArea(ref WebPIDecoder idec, ref int left, ref int top, ref int width, ref int height); + + + /// Return Type: VP8StatusCode->Anonymous_b244cc15_fbc7_4c41_8884_71fe4f515cd6 + ///param0: uint8_t* + ///param1: size_t->unsigned int + ///param2: WebPBitstreamFeatures* + ///param3: int + [DllImportAttribute("__Internal", EntryPoint = "WebPGetFeaturesInternal")] + public static extern VP8StatusCode WebPGetFeaturesInternal([InAttribute()] IntPtr param0, UIntPtr param1, ref WebPBitstreamFeatures param2, int param3); + + + /// Return Type: int + ///param0: WebPDecoderConfig* + ///param1: int + [DllImportAttribute("__Internal", EntryPoint = "WebPInitDecoderConfigInternal")] + public static extern int WebPInitDecoderConfigInternal(ref WebPDecoderConfig param0, int param1); + + + /// Return Type: WebPIDecoder* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///config: WebPDecoderConfig* + [DllImportAttribute("__Internal", EntryPoint = "WebPIDecode")] + public static extern IntPtr WebPIDecode([InAttribute()] IntPtr data, UIntPtr data_size, ref WebPDecoderConfig config); + + + /// Return Type: VP8StatusCode->Anonymous_b244cc15_fbc7_4c41_8884_71fe4f515cd6 + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///config: WebPDecoderConfig* + [DllImportAttribute("__Internal", EntryPoint = "WebPDecode")] + public static extern VP8StatusCode WebPDecode([InAttribute()] IntPtr data, UIntPtr data_size, ref WebPDecoderConfig config); + + + /// Return Type: int + [DllImportAttribute("__Internal", EntryPoint = "WebPGetEncoderVersion")] + public static extern int WebPGetEncoderVersion(); + + + /// Return Type: size_t->unsigned int + ///rgb: uint8_t* + ///width: int + ///height: int + ///stride: int + ///quality_factor: float + ///output: uint8_t** + [DllImportAttribute("__Internal", EntryPoint = "WebPEncodeRGB")] + public static extern UIntPtr WebPEncodeRGB([InAttribute()] IntPtr rgb, int width, int height, int stride, float quality_factor, ref IntPtr output); + + + /// Return Type: size_t->unsigned int + ///bgr: uint8_t* + ///width: int + ///height: int + ///stride: int + ///quality_factor: float + ///output: uint8_t** + [DllImportAttribute("__Internal", EntryPoint = "WebPEncodeBGR")] + public static extern UIntPtr WebPEncodeBGR([InAttribute()] IntPtr bgr, int width, int height, int stride, float quality_factor, ref IntPtr output); + + + /// Return Type: size_t->unsigned int + ///rgba: uint8_t* + ///width: int + ///height: int + ///stride: int + ///quality_factor: float + ///output: uint8_t** + [DllImportAttribute("__Internal", EntryPoint = "WebPEncodeRGBA")] + public static extern UIntPtr WebPEncodeRGBA([InAttribute()] IntPtr rgba, int width, int height, int stride, float quality_factor, ref IntPtr output); + + + /// Return Type: size_t->unsigned int + ///bgra: uint8_t* + ///width: int + ///height: int + ///stride: int + ///quality_factor: float + ///output: uint8_t** + [DllImportAttribute("__Internal", EntryPoint = "WebPEncodeBGRA")] + public static extern IntPtr WebPEncodeBGRA([InAttribute()] IntPtr bgra, int width, int height, int stride, float quality_factor, ref IntPtr output); + + + /// Return Type: size_t->unsigned int + ///rgb: uint8_t* + ///width: int + ///height: int + ///stride: int + ///output: uint8_t** + [DllImportAttribute("__Internal", EntryPoint = "WebPEncodeLosslessRGB")] + public static extern UIntPtr WebPEncodeLosslessRGB([InAttribute()] IntPtr rgb, int width, int height, int stride, ref IntPtr output); + + + /// Return Type: size_t->unsigned int + ///bgr: uint8_t* + ///width: int + ///height: int + ///stride: int + ///output: uint8_t** + [DllImportAttribute("__Internal", EntryPoint = "WebPEncodeLosslessBGR")] + public static extern UIntPtr WebPEncodeLosslessBGR([InAttribute()] IntPtr bgr, int width, int height, int stride, ref IntPtr output); + + + /// Return Type: size_t->unsigned int + ///rgba: uint8_t* + ///width: int + ///height: int + ///stride: int + ///output: uint8_t** + [DllImportAttribute("__Internal", EntryPoint = "WebPEncodeLosslessRGBA")] + public static extern UIntPtr WebPEncodeLosslessRGBA([InAttribute()] IntPtr rgba, int width, int height, int stride, ref IntPtr output); + + + /// Return Type: size_t->unsigned int + ///bgra: uint8_t* + ///width: int + ///height: int + ///stride: int + ///output: uint8_t** + [DllImportAttribute("__Internal", EntryPoint = "WebPEncodeLosslessBGRA")] + public static extern UIntPtr WebPEncodeLosslessBGRA([InAttribute()] IntPtr bgra, int width, int height, int stride, ref IntPtr output); + + + /// Return Type: int + ///param0: WebPConfig* + ///param1: WebPPreset->Anonymous_017d4167_f53e_4b3d_b029_592ff5c3f80b + ///param2: float + ///param3: int + [DllImportAttribute("__Internal", EntryPoint = "WebPConfigInitInternal")] + public static extern int WebPConfigInitInternal(ref WebPConfig param0, WebPPreset param1, float param2, int param3); + + + /// Return Type: int + ///config: WebPConfig* + [DllImportAttribute("__Internal", EntryPoint = "WebPValidateConfig")] + public static extern int WebPValidateConfig(ref WebPConfig config); + + + /// Return Type: void + ///writer: WebPMemoryWriter* + [DllImportAttribute("__Internal", EntryPoint = "WebPMemoryWriterInit")] + public static extern void WebPMemoryWriterInit(ref WebPMemoryWriter writer); + + + /// Return Type: int + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///picture: WebPPicture* + [DllImportAttribute("__Internal", EntryPoint = "WebPMemoryWrite")] + public static extern int WebPMemoryWrite([InAttribute()] IntPtr data, UIntPtr data_size, ref WebPPicture picture); + + + /// Return Type: int + ///param0: WebPPicture* + ///param1: int + [DllImportAttribute("__Internal", EntryPoint = "WebPPictureInitInternal")] + public static extern int WebPPictureInitInternal(ref WebPPicture param0, int param1); + + + /// Return Type: int + ///picture: WebPPicture* + [DllImportAttribute("__Internal", EntryPoint = "WebPPictureAlloc")] + public static extern int WebPPictureAlloc(ref WebPPicture picture); + + + /// Return Type: void + ///picture: WebPPicture* + [DllImportAttribute("__Internal", EntryPoint = "WebPPictureFree")] + public static extern void WebPPictureFree(ref WebPPicture picture); + + + /// Return Type: int + ///src: WebPPicture* + ///dst: WebPPicture* + [DllImportAttribute("__Internal", EntryPoint = "WebPPictureCopy")] + public static extern int WebPPictureCopy(ref WebPPicture src, ref WebPPicture dst); + + + /// Return Type: int + ///pic1: WebPPicture* + ///pic2: WebPPicture* + ///metric_type: int + ///result: float* result[5] + /// + + /// + /// Compute PSNR, SSIM or LSIM distortion metric between two pictures. + /// Result is in dB, stores in result[] in the Y/U/V/Alpha/All order. + /// Returns false in case of error (src and ref don't have same dimension, ...) + /// Warning: this function is rather CPU-intensive. + /// + /// + /// + /// 0 = PSNR, 1 = SSIM, 2 = LSIM + /// + /// + [DllImportAttribute("__Internal", EntryPoint = "WebPPictureDistortion")] + public static extern int WebPPictureDistortion(ref WebPPicture src, ref WebPPicture reference, int metric_type, ref float result); + + + + + /// Return Type: int + ///picture: WebPPicture* + ///left: int + ///top: int + ///width: int + ///height: int + [DllImportAttribute("__Internal", EntryPoint = "WebPPictureCrop")] + public static extern int WebPPictureCrop(ref WebPPicture picture, int left, int top, int width, int height); + + + /// Return Type: int + ///src: WebPPicture* + ///left: int + ///top: int + ///width: int + ///height: int + ///dst: WebPPicture* + [DllImportAttribute("__Internal", EntryPoint = "WebPPictureView")] + public static extern int WebPPictureView(ref WebPPicture src, int left, int top, int width, int height, ref WebPPicture dst); + + + /// Return Type: int + ///picture: WebPPicture* + [DllImportAttribute("__Internal", EntryPoint = "WebPPictureIsView")] + public static extern int WebPPictureIsView(ref WebPPicture picture); + + + /// Return Type: int + ///pic: WebPPicture* + ///width: int + ///height: int + [DllImportAttribute("__Internal", EntryPoint = "WebPPictureRescale")] + public static extern int WebPPictureRescale(ref WebPPicture pic, int width, int height); + + + /// Return Type: int + ///picture: WebPPicture* + ///rgb: uint8_t* + ///rgb_stride: int + [DllImportAttribute("__Internal", EntryPoint = "WebPPictureImportRGB")] + public static extern int WebPPictureImportRGB(ref WebPPicture picture, [InAttribute()] IntPtr rgb, int rgb_stride); + + + /// Return Type: int + ///picture: WebPPicture* + ///rgba: uint8_t* + ///rgba_stride: int + [DllImportAttribute("__Internal", EntryPoint = "WebPPictureImportRGBA")] + public static extern int WebPPictureImportRGBA(ref WebPPicture picture, [InAttribute()] IntPtr rgba, int rgba_stride); + + + /// Return Type: int + ///picture: WebPPicture* + ///rgbx: uint8_t* + ///rgbx_stride: int + [DllImportAttribute("__Internal", EntryPoint = "WebPPictureImportRGBX")] + public static extern int WebPPictureImportRGBX(ref WebPPicture picture, [InAttribute()] IntPtr rgbx, int rgbx_stride); + + + /// Return Type: int + ///picture: WebPPicture* + ///bgr: uint8_t* + ///bgr_stride: int + [DllImportAttribute("__Internal", EntryPoint = "WebPPictureImportBGR")] + public static extern int WebPPictureImportBGR(ref WebPPicture picture, [InAttribute()] IntPtr bgr, int bgr_stride); + + + /// Return Type: int + ///picture: WebPPicture* + ///bgra: uint8_t* + ///bgra_stride: int + [DllImportAttribute("__Internal", EntryPoint = "WebPPictureImportBGRA")] + public static extern int WebPPictureImportBGRA(ref WebPPicture picture, [InAttribute()] IntPtr bgra, int bgra_stride); + + + /// Return Type: int + ///picture: WebPPicture* + ///bgrx: uint8_t* + ///bgrx_stride: int + [DllImportAttribute("__Internal", EntryPoint = "WebPPictureImportBGRX")] + public static extern int WebPPictureImportBGRX(ref WebPPicture picture, [InAttribute()] IntPtr bgrx, int bgrx_stride); + + + /// Return Type: int + ///picture: WebPPicture* + ///colorspace: WebPEncCSP->Anonymous_84ce7065_fe91_48b4_93d8_1f0e84319dba + [DllImportAttribute("__Internal", EntryPoint = "WebPPictureARGBToYUVA")] + public static extern int WebPPictureARGBToYUVA(ref WebPPicture picture, WebPEncCSP colorspace); + + + /// Return Type: int + ///picture: WebPPicture* + [DllImportAttribute("__Internal", EntryPoint = "WebPPictureYUVAToARGB")] + public static extern int WebPPictureYUVAToARGB(ref WebPPicture picture); + + + /// Return Type: void + ///picture: WebPPicture* + [DllImportAttribute("__Internal", EntryPoint = "WebPCleanupTransparentArea")] + public static extern void WebPCleanupTransparentArea(ref WebPPicture picture); + + + /// Return Type: int + ///picture: WebPPicture* + [DllImportAttribute("__Internal", EntryPoint = "WebPPictureHasTransparency")] + public static extern int WebPPictureHasTransparency(ref WebPPicture picture); + + + /// Return Type: int + ///config: WebPConfig* + ///picture: WebPPicture* + [DllImportAttribute("__Internal", EntryPoint = "WebPEncode")] + public static extern int WebPEncode(ref WebPConfig config, ref WebPPicture picture); + } + + #endregion + + #region EXTERNAL_DLL_IMPORT + + class External + { + /// Return Type: int + [DllImportAttribute("webp", EntryPoint = "WebPGetDecoderVersion")] + public static extern int WebPGetDecoderVersion(); + + [DllImportAttribute("webp", EntryPoint = "WebPSafeFree")] + public static extern void WebPSafeFree(IntPtr toDeallocate); + + + /// + /// Retrieve basic header information: width, height. + /// This function will also validate the header and return 0 in + /// case of formatting error. + /// Pointers 'width' and 'height' can be passed NULL if deemed irrelevant. + /// + /// + /// + /// + /// + /// + [DllImportAttribute("webp", EntryPoint = "WebPGetInfo")] + public static extern int WebPGetInfo([InAttribute()] IntPtr data, UIntPtr data_size, ref int width, ref int height); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///width: int* + ///height: int* + [DllImportAttribute("webp", EntryPoint = "WebPDecodeRGBA")] + public static extern IntPtr WebPDecodeRGBA([InAttribute()] IntPtr data, UIntPtr data_size, ref int width, ref int height); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///width: int* + ///height: int* + [DllImportAttribute("webp", EntryPoint = "WebPDecodeARGB")] + public static extern IntPtr WebPDecodeARGB([InAttribute()] IntPtr data, UIntPtr data_size, ref int width, ref int height); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///width: int* + ///height: int* + [DllImportAttribute("webp", EntryPoint = "WebPDecodeBGRA")] + public static extern IntPtr WebPDecodeBGRA([InAttribute()] IntPtr data, UIntPtr data_size, ref int width, ref int height); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///width: int* + ///height: int* + [DllImportAttribute("webp", EntryPoint = "WebPDecodeRGB")] + public static extern IntPtr WebPDecodeRGB([InAttribute()] IntPtr data, UIntPtr data_size, ref int width, ref int height); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///width: int* + ///height: int* + [DllImportAttribute("webp", EntryPoint = "WebPDecodeBGR")] + public static extern IntPtr WebPDecodeBGR([InAttribute()] IntPtr data, UIntPtr data_size, ref int width, ref int height); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///width: int* + ///height: int* + ///u: uint8_t** + ///v: uint8_t** + ///stride: int* + ///uv_stride: int* + [DllImportAttribute("webp", EntryPoint = "WebPDecodeYUV")] + public static extern IntPtr WebPDecodeYUV([InAttribute()] IntPtr data, UIntPtr data_size, ref int width, ref int height, ref IntPtr u, ref IntPtr v, ref int stride, ref int uv_stride); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///output_buffer: uint8_t* + ///output_buffer_size: size_t->unsigned int + ///output_stride: int + [DllImportAttribute("webp", EntryPoint = "WebPDecodeRGBAInto")] + public static extern IntPtr WebPDecodeRGBAInto([InAttribute()] IntPtr data, UIntPtr data_size, IntPtr output_buffer, UIntPtr output_buffer_size, int output_stride); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///output_buffer: uint8_t* + ///output_buffer_size: size_t->unsigned int + ///output_stride: int + [DllImportAttribute("webp", EntryPoint = "WebPDecodeARGBInto")] + public static extern IntPtr WebPDecodeARGBInto([InAttribute()] IntPtr data, UIntPtr data_size, IntPtr output_buffer, UIntPtr output_buffer_size, int output_stride); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///output_buffer: uint8_t* + ///output_buffer_size: size_t->unsigned int + ///output_stride: int + [DllImportAttribute("webp", EntryPoint = "WebPDecodeBGRAInto")] + public static extern IntPtr WebPDecodeBGRAInto([InAttribute()] IntPtr data, UIntPtr data_size, IntPtr output_buffer, UIntPtr output_buffer_size, int output_stride); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///output_buffer: uint8_t* + ///output_buffer_size: size_t->unsigned int + ///output_stride: int + [DllImportAttribute("webp", EntryPoint = "WebPDecodeRGBInto")] + public static extern IntPtr WebPDecodeRGBInto([InAttribute()] IntPtr data, UIntPtr data_size, IntPtr output_buffer, UIntPtr output_buffer_size, int output_stride); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///output_buffer: uint8_t* + ///output_buffer_size: size_t->unsigned int + ///output_stride: int + [DllImportAttribute("webp", EntryPoint = "WebPDecodeBGRInto")] + public static extern IntPtr WebPDecodeBGRInto([InAttribute()] IntPtr data, UIntPtr data_size, IntPtr output_buffer, UIntPtr output_buffer_size, int output_stride); + + + /// Return Type: uint8_t* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///luma: uint8_t* + ///luma_size: size_t->unsigned int + ///luma_stride: int + ///u: uint8_t* + ///u_size: size_t->unsigned int + ///u_stride: int + ///v: uint8_t* + ///v_size: size_t->unsigned int + ///v_stride: int + [DllImportAttribute("webp", EntryPoint = "WebPDecodeYUVInto")] + public static extern IntPtr WebPDecodeYUVInto([InAttribute()] IntPtr data, UIntPtr data_size, IntPtr luma, UIntPtr luma_size, int luma_stride, IntPtr u, UIntPtr u_size, int u_stride, IntPtr v, UIntPtr v_size, int v_stride); + + + /// Return Type: int + ///param0: WebPDecBuffer* + ///param1: int + [DllImportAttribute("webp", EntryPoint = "WebPInitDecBufferInternal")] + public static extern int WebPInitDecBufferInternal(ref WebPDecBuffer param0, int param1); + + + /// Return Type: void + ///buffer: WebPDecBuffer* + [DllImportAttribute("webp", EntryPoint = "WebPFreeDecBuffer")] + public static extern void WebPFreeDecBuffer(ref WebPDecBuffer buffer); + + + /// Return Type: WebPIDecoder* + ///output_buffer: WebPDecBuffer* + [DllImportAttribute("webp", EntryPoint = "WebPINewDecoder")] + public static extern IntPtr WebPINewDecoder(ref WebPDecBuffer output_buffer); + + + /// Return Type: WebPIDecoder* + ///csp: WEBP_CSP_MODE->Anonymous_cb136f5b_1d5d_49a0_aca4_656a79e9d159 + ///output_buffer: uint8_t* + ///output_buffer_size: size_t->unsigned int + ///output_stride: int + [DllImportAttribute("webp", EntryPoint = "WebPINewRGB")] + public static extern IntPtr WebPINewRGB(WEBP_CSP_MODE csp, IntPtr output_buffer, UIntPtr output_buffer_size, int output_stride); + + + /// Return Type: WebPIDecoder* + ///luma: uint8_t* + ///luma_size: size_t->unsigned int + ///luma_stride: int + ///u: uint8_t* + ///u_size: size_t->unsigned int + ///u_stride: int + ///v: uint8_t* + ///v_size: size_t->unsigned int + ///v_stride: int + ///a: uint8_t* + ///a_size: size_t->unsigned int + ///a_stride: int + [DllImportAttribute("webp", EntryPoint = "WebPINewYUVA")] + public static extern IntPtr WebPINewYUVA(IntPtr luma, UIntPtr luma_size, int luma_stride, IntPtr u, UIntPtr u_size, int u_stride, IntPtr v, UIntPtr v_size, int v_stride, IntPtr a, UIntPtr a_size, int a_stride); + + + /// Return Type: WebPIDecoder* + ///luma: uint8_t* + ///luma_size: size_t->unsigned int + ///luma_stride: int + ///u: uint8_t* + ///u_size: size_t->unsigned int + ///u_stride: int + ///v: uint8_t* + ///v_size: size_t->unsigned int + ///v_stride: int + [DllImportAttribute("webp", EntryPoint = "WebPINewYUV")] + public static extern IntPtr WebPINewYUV(IntPtr luma, UIntPtr luma_size, int luma_stride, IntPtr u, UIntPtr u_size, int u_stride, IntPtr v, UIntPtr v_size, int v_stride); + + + /// Return Type: void + ///idec: WebPIDecoder* + [DllImportAttribute("webp", EntryPoint = "WebPIDelete")] + public static extern void WebPIDelete(ref WebPIDecoder idec); + + + /// Return Type: VP8StatusCode->Anonymous_b244cc15_fbc7_4c41_8884_71fe4f515cd6 + ///idec: WebPIDecoder* + ///data: uint8_t* + ///data_size: size_t->unsigned int + [DllImportAttribute("webp", EntryPoint = "WebPIAppend")] + public static extern VP8StatusCode WebPIAppend(ref WebPIDecoder idec, [InAttribute()] IntPtr data, UIntPtr data_size); + + + /// Return Type: VP8StatusCode->Anonymous_b244cc15_fbc7_4c41_8884_71fe4f515cd6 + ///idec: WebPIDecoder* + ///data: uint8_t* + ///data_size: size_t->unsigned int + [DllImportAttribute("webp", EntryPoint = "WebPIUpdate")] + public static extern VP8StatusCode WebPIUpdate(ref WebPIDecoder idec, [InAttribute()] IntPtr data, UIntPtr data_size); + + + /// Return Type: uint8_t* + ///idec: WebPIDecoder* + ///last_y: int* + ///width: int* + ///height: int* + ///stride: int* + [DllImportAttribute("webp", EntryPoint = "WebPIDecGetRGB")] + public static extern IntPtr WebPIDecGetRGB(ref WebPIDecoder idec, ref int last_y, ref int width, ref int height, ref int stride); + + + /// Return Type: uint8_t* + ///idec: WebPIDecoder* + ///last_y: int* + ///u: uint8_t** + ///v: uint8_t** + ///a: uint8_t** + ///width: int* + ///height: int* + ///stride: int* + ///uv_stride: int* + ///a_stride: int* + [DllImportAttribute("webp", EntryPoint = "WebPIDecGetYUVA")] + public static extern IntPtr WebPIDecGetYUVA(ref WebPIDecoder idec, ref int last_y, ref IntPtr u, ref IntPtr v, ref IntPtr a, ref int width, ref int height, ref int stride, ref int uv_stride, ref int a_stride); + + + /// Return Type: WebPDecBuffer* + ///idec: WebPIDecoder* + ///left: int* + ///top: int* + ///width: int* + ///height: int* + [DllImportAttribute("webp", EntryPoint = "WebPIDecodedArea")] + public static extern IntPtr WebPIDecodedArea(ref WebPIDecoder idec, ref int left, ref int top, ref int width, ref int height); + + + /// Return Type: VP8StatusCode->Anonymous_b244cc15_fbc7_4c41_8884_71fe4f515cd6 + ///param0: uint8_t* + ///param1: size_t->unsigned int + ///param2: WebPBitstreamFeatures* + ///param3: int + [DllImportAttribute("webp", EntryPoint = "WebPGetFeaturesInternal")] + public static extern VP8StatusCode WebPGetFeaturesInternal([InAttribute()] IntPtr param0, UIntPtr param1, ref WebPBitstreamFeatures param2, int param3); + + + /// Return Type: int + ///param0: WebPDecoderConfig* + ///param1: int + [DllImportAttribute("webp", EntryPoint = "WebPInitDecoderConfigInternal")] + public static extern int WebPInitDecoderConfigInternal(ref WebPDecoderConfig param0, int param1); + + + /// Return Type: WebPIDecoder* + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///config: WebPDecoderConfig* + [DllImportAttribute("webp", EntryPoint = "WebPIDecode")] + public static extern IntPtr WebPIDecode([InAttribute()] IntPtr data, UIntPtr data_size, ref WebPDecoderConfig config); + + + /// Return Type: VP8StatusCode->Anonymous_b244cc15_fbc7_4c41_8884_71fe4f515cd6 + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///config: WebPDecoderConfig* + [DllImportAttribute("webp", EntryPoint = "WebPDecode")] + public static extern VP8StatusCode WebPDecode([InAttribute()] IntPtr data, UIntPtr data_size, ref WebPDecoderConfig config); + + + /// Return Type: int + [DllImportAttribute("webp", EntryPoint = "WebPGetEncoderVersion")] + public static extern int WebPGetEncoderVersion(); + + + /// Return Type: size_t->unsigned int + ///rgb: uint8_t* + ///width: int + ///height: int + ///stride: int + ///quality_factor: float + ///output: uint8_t** + [DllImportAttribute("webp", EntryPoint = "WebPEncodeRGB")] + public static extern UIntPtr WebPEncodeRGB([InAttribute()] IntPtr rgb, int width, int height, int stride, float quality_factor, ref IntPtr output); + + + /// Return Type: size_t->unsigned int + ///bgr: uint8_t* + ///width: int + ///height: int + ///stride: int + ///quality_factor: float + ///output: uint8_t** + [DllImportAttribute("webp", EntryPoint = "WebPEncodeBGR")] + public static extern UIntPtr WebPEncodeBGR([InAttribute()] IntPtr bgr, int width, int height, int stride, float quality_factor, ref IntPtr output); + + + /// Return Type: size_t->unsigned int + ///rgba: uint8_t* + ///width: int + ///height: int + ///stride: int + ///quality_factor: float + ///output: uint8_t** + [DllImportAttribute("webp", EntryPoint = "WebPEncodeRGBA")] + public static extern UIntPtr WebPEncodeRGBA([InAttribute()] IntPtr rgba, int width, int height, int stride, float quality_factor, ref IntPtr output); + + + /// Return Type: size_t->unsigned int + ///bgra: uint8_t* + ///width: int + ///height: int + ///stride: int + ///quality_factor: float + ///output: uint8_t** + [DllImportAttribute("webp", EntryPoint = "WebPEncodeBGRA")] + public static extern IntPtr WebPEncodeBGRA([InAttribute()] IntPtr bgra, int width, int height, int stride, float quality_factor, ref IntPtr output); + + + /// Return Type: size_t->unsigned int + ///rgb: uint8_t* + ///width: int + ///height: int + ///stride: int + ///output: uint8_t** + [DllImportAttribute("webp", EntryPoint = "WebPEncodeLosslessRGB")] + public static extern UIntPtr WebPEncodeLosslessRGB([InAttribute()] IntPtr rgb, int width, int height, int stride, ref IntPtr output); + + + /// Return Type: size_t->unsigned int + ///bgr: uint8_t* + ///width: int + ///height: int + ///stride: int + ///output: uint8_t** + [DllImportAttribute("webp", EntryPoint = "WebPEncodeLosslessBGR")] + public static extern UIntPtr WebPEncodeLosslessBGR([InAttribute()] IntPtr bgr, int width, int height, int stride, ref IntPtr output); + + + /// Return Type: size_t->unsigned int + ///rgba: uint8_t* + ///width: int + ///height: int + ///stride: int + ///output: uint8_t** + [DllImportAttribute("webp", EntryPoint = "WebPEncodeLosslessRGBA")] + public static extern UIntPtr WebPEncodeLosslessRGBA([InAttribute()] IntPtr rgba, int width, int height, int stride, ref IntPtr output); + + + /// Return Type: size_t->unsigned int + ///bgra: uint8_t* + ///width: int + ///height: int + ///stride: int + ///output: uint8_t** + [DllImportAttribute("webp", EntryPoint = "WebPEncodeLosslessBGRA")] + public static extern UIntPtr WebPEncodeLosslessBGRA([InAttribute()] IntPtr bgra, int width, int height, int stride, ref IntPtr output); + + + /// Return Type: int + ///param0: WebPConfig* + ///param1: WebPPreset->Anonymous_017d4167_f53e_4b3d_b029_592ff5c3f80b + ///param2: float + ///param3: int + [DllImportAttribute("webp", EntryPoint = "WebPConfigInitInternal")] + public static extern int WebPConfigInitInternal(ref WebPConfig param0, WebPPreset param1, float param2, int param3); + + + /// Return Type: int + ///config: WebPConfig* + [DllImportAttribute("webp", EntryPoint = "WebPValidateConfig")] + public static extern int WebPValidateConfig(ref WebPConfig config); + + + /// Return Type: void + ///writer: WebPMemoryWriter* + [DllImportAttribute("webp", EntryPoint = "WebPMemoryWriterInit")] + public static extern void WebPMemoryWriterInit(ref WebPMemoryWriter writer); + + + /// Return Type: int + ///data: uint8_t* + ///data_size: size_t->unsigned int + ///picture: WebPPicture* + [DllImportAttribute("webp", EntryPoint = "WebPMemoryWrite")] + public static extern int WebPMemoryWrite([InAttribute()] IntPtr data, UIntPtr data_size, ref WebPPicture picture); + + + /// Return Type: int + ///param0: WebPPicture* + ///param1: int + [DllImportAttribute("webp", EntryPoint = "WebPPictureInitInternal")] + public static extern int WebPPictureInitInternal(ref WebPPicture param0, int param1); + + + /// Return Type: int + ///picture: WebPPicture* + [DllImportAttribute("webp", EntryPoint = "WebPPictureAlloc")] + public static extern int WebPPictureAlloc(ref WebPPicture picture); + + + /// Return Type: void + ///picture: WebPPicture* + [DllImportAttribute("webp", EntryPoint = "WebPPictureFree")] + public static extern void WebPPictureFree(ref WebPPicture picture); + + + /// Return Type: int + ///src: WebPPicture* + ///dst: WebPPicture* + [DllImportAttribute("webp", EntryPoint = "WebPPictureCopy")] + public static extern int WebPPictureCopy(ref WebPPicture src, ref WebPPicture dst); + + + /// Return Type: int + ///pic1: WebPPicture* + ///pic2: WebPPicture* + ///metric_type: int + ///result: float* result[5] + /// + + /// + /// Compute PSNR, SSIM or LSIM distortion metric between two pictures. + /// Result is in dB, stores in result[] in the Y/U/V/Alpha/All order. + /// Returns false in case of error (src and ref don't have same dimension, ...) + /// Warning: this function is rather CPU-intensive. + /// + /// + /// + /// 0 = PSNR, 1 = SSIM, 2 = LSIM + /// + /// + [DllImportAttribute("webp", EntryPoint = "WebPPictureDistortion")] + public static extern int WebPPictureDistortion(ref WebPPicture src, ref WebPPicture reference, int metric_type, ref float result); + + + + + /// Return Type: int + ///picture: WebPPicture* + ///left: int + ///top: int + ///width: int + ///height: int + [DllImportAttribute("webp", EntryPoint = "WebPPictureCrop")] + public static extern int WebPPictureCrop(ref WebPPicture picture, int left, int top, int width, int height); + + + /// Return Type: int + ///src: WebPPicture* + ///left: int + ///top: int + ///width: int + ///height: int + ///dst: WebPPicture* + [DllImportAttribute("webp", EntryPoint = "WebPPictureView")] + public static extern int WebPPictureView(ref WebPPicture src, int left, int top, int width, int height, ref WebPPicture dst); + + + /// Return Type: int + ///picture: WebPPicture* + [DllImportAttribute("webp", EntryPoint = "WebPPictureIsView")] + public static extern int WebPPictureIsView(ref WebPPicture picture); + + + /// Return Type: int + ///pic: WebPPicture* + ///width: int + ///height: int + [DllImportAttribute("webp", EntryPoint = "WebPPictureRescale")] + public static extern int WebPPictureRescale(ref WebPPicture pic, int width, int height); + + + /// Return Type: int + ///picture: WebPPicture* + ///rgb: uint8_t* + ///rgb_stride: int + [DllImportAttribute("webp", EntryPoint = "WebPPictureImportRGB")] + public static extern int WebPPictureImportRGB(ref WebPPicture picture, [InAttribute()] IntPtr rgb, int rgb_stride); + + + /// Return Type: int + ///picture: WebPPicture* + ///rgba: uint8_t* + ///rgba_stride: int + [DllImportAttribute("webp", EntryPoint = "WebPPictureImportRGBA")] + public static extern int WebPPictureImportRGBA(ref WebPPicture picture, [InAttribute()] IntPtr rgba, int rgba_stride); + + + /// Return Type: int + ///picture: WebPPicture* + ///rgbx: uint8_t* + ///rgbx_stride: int + [DllImportAttribute("webp", EntryPoint = "WebPPictureImportRGBX")] + public static extern int WebPPictureImportRGBX(ref WebPPicture picture, [InAttribute()] IntPtr rgbx, int rgbx_stride); + + + /// Return Type: int + ///picture: WebPPicture* + ///bgr: uint8_t* + ///bgr_stride: int + [DllImportAttribute("webp", EntryPoint = "WebPPictureImportBGR")] + public static extern int WebPPictureImportBGR(ref WebPPicture picture, [InAttribute()] IntPtr bgr, int bgr_stride); + + + /// Return Type: int + ///picture: WebPPicture* + ///bgra: uint8_t* + ///bgra_stride: int + [DllImportAttribute("webp", EntryPoint = "WebPPictureImportBGRA")] + public static extern int WebPPictureImportBGRA(ref WebPPicture picture, [InAttribute()] IntPtr bgra, int bgra_stride); + + + /// Return Type: int + ///picture: WebPPicture* + ///bgrx: uint8_t* + ///bgrx_stride: int + [DllImportAttribute("webp", EntryPoint = "WebPPictureImportBGRX")] + public static extern int WebPPictureImportBGRX(ref WebPPicture picture, [InAttribute()] IntPtr bgrx, int bgrx_stride); + + + /// Return Type: int + ///picture: WebPPicture* + ///colorspace: WebPEncCSP->Anonymous_84ce7065_fe91_48b4_93d8_1f0e84319dba + [DllImportAttribute("webp", EntryPoint = "WebPPictureARGBToYUVA")] + public static extern int WebPPictureARGBToYUVA(ref WebPPicture picture, WebPEncCSP colorspace); + + + /// Return Type: int + ///picture: WebPPicture* + [DllImportAttribute("webp", EntryPoint = "WebPPictureYUVAToARGB")] + public static extern int WebPPictureYUVAToARGB(ref WebPPicture picture); + + + /// Return Type: void + ///picture: WebPPicture* + [DllImportAttribute("webp", EntryPoint = "WebPCleanupTransparentArea")] + public static extern void WebPCleanupTransparentArea(ref WebPPicture picture); + + + /// Return Type: int + ///picture: WebPPicture* + [DllImportAttribute("webp", EntryPoint = "WebPPictureHasTransparency")] + public static extern int WebPPictureHasTransparency(ref WebPPicture picture); + + + /// Return Type: int + ///config: WebPConfig* + ///picture: WebPPicture* + [DllImportAttribute("webp", EntryPoint = "WebPEncode")] + public static extern int WebPEncode(ref WebPConfig config, ref WebPPicture picture); + } + + #endregion + + // Some useful macros: + + /// + /// Returns true if the specified mode uses a premultiplied alpha + /// + /// + /// + public static bool WebPIsPremultipliedMode(WEBP_CSP_MODE mode) + { + + return (mode == WEBP_CSP_MODE.MODE_rgbA || mode == WEBP_CSP_MODE.MODE_bgrA || mode == WEBP_CSP_MODE.MODE_Argb || + mode == WEBP_CSP_MODE.MODE_rgbA_4444); + + } + + /// + /// Returns true if the given mode is RGB(A) + /// + /// + /// + public static bool WebPIsRGBMode(WEBP_CSP_MODE mode) + { + + return (mode < WEBP_CSP_MODE.MODE_YUV); + + } + + + /// + /// Returns true if the given mode has an alpha channel + /// + /// + /// + public static bool WebPIsAlphaMode(WEBP_CSP_MODE mode) + { + + return (mode == WEBP_CSP_MODE.MODE_RGBA || mode == WEBP_CSP_MODE.MODE_BGRA || mode == WEBP_CSP_MODE.MODE_ARGB || + mode == WEBP_CSP_MODE.MODE_RGBA_4444 || mode == WEBP_CSP_MODE.MODE_YUVA || + WebPIsPremultipliedMode(mode)); + + } + + + + // + + /// + /// Retrieve features from the bitstream. The *features structure is filled + /// with information gathered from the bitstream. + /// Returns false in case of error or version mismatch. + /// In case of error, features->bitstream_status will reflect the error code. + /// + /// + /// + /// + /// + public static VP8StatusCode WebPGetFeatures(IntPtr data, UIntPtr data_size, ref WebPBitstreamFeatures features) + { + return NativeBindings.WebPGetFeaturesInternal(data, data_size, ref features, WEBP_DECODER_ABI_VERSION); + + } + /// + /// Initialize the configuration as empty. This function must always be + /// called first, unless WebPGetFeatures() is to be called. + /// Returns false in case of mismatched version. + /// + /// + /// + public static int WebPInitDecoderConfig(ref WebPDecoderConfig config) + { + + return WebPInitDecoderConfigInternal(ref config, WEBP_DECODER_ABI_VERSION); + + } + + + /// + /// Initialize the structure as empty. Must be called before any other use. Returns false in case of version mismatch + /// + /// + /// + public static int WebPInitDecBuffer(ref WebPDecBuffer buffer) + { + return WebPInitDecBufferInternal(ref buffer, WEBP_DECODER_ABI_VERSION); + } + + + + // // Deprecated alpha-less version of WebPIDecGetYUVA(): it will ignore the + + //// alpha information (if present). Kept for backward compatibility. + + //public IntPtr WebPIDecGetYUV(IntPtr decoder, int* last_y, uint8_t** u, uint8_t** v, + + // int* width, int* height, int* stride, int* uv_stride) { + + // return WebPIDecGetYUVA(idec, last_y, u, v, NULL, width, height, + + // stride, uv_stride, NULL); + + /// + /// Should always be called, to initialize a fresh WebPConfig structure before + /// modification. Returns false in case of version mismatch. WebPConfigInit() + /// must have succeeded before using the 'config' object. + /// Note that the default values are lossless=0 and quality=75. + /// + /// + /// + public static int WebPConfigInit(ref WebPConfig config) + { + return NativeBindings.WebPConfigInitInternal(ref config, WebPPreset.WEBP_PRESET_DEFAULT, 75.0f, WEBP_ENCODER_ABI_VERSION); + } + + /// + /// This function will initialize the configuration according to a predefined + /// set of parameters (referred to by 'preset') and a given quality factor. + /// This function can be called as a replacement to WebPConfigInit(). Will return false in case of error. + /// + /// + /// + /// + /// + public static int WebPConfigPreset(ref WebPConfig config, WebPPreset preset, float quality) + { + return NativeBindings.WebPConfigInitInternal(ref config, preset, quality, WEBP_ENCODER_ABI_VERSION); + } + + /// + /// Should always be called, to initialize the structure. Returns false in case + /// of version mismatch. WebPPictureInit() must have succeeded before using the + /// 'picture' object. + /// Note that, by default, use_argb is false and colorspace is WEBP_YUV420. + /// + /// + /// + public static int WebPPictureInit(ref WebPPicture picture) + { + + return NativeBindings.WebPPictureInitInternal(ref picture, WEBP_ENCODER_ABI_VERSION); + + } + } +} + +#pragma warning restore 1591 diff --git a/src/Shared.TextureContainer/WebP/Texture2DExt.cs b/src/Shared.TextureContainer/WebP/Texture2DExt.cs new file mode 100644 index 00000000..3e814fe9 --- /dev/null +++ b/src/Shared.TextureContainer/WebP/Texture2DExt.cs @@ -0,0 +1,280 @@ +//https://github.com/octo-code/webp-unity3d +using System; +using System.Text; +using System.Collections.Generic; +using System.Runtime.InteropServices; + +using UnityEngine; + +using WebP.Extern; + +namespace WebP +{ + /// + /// + /// + public static class Texture2DExt + { + public delegate void ScalingFunction(ref int width, ref int height); + + /// + /// Scaling funtion to scale image to specific width and height. + /// + /// The to size. + /// Width in pixels. + /// Height in pixels. + public static ScalingFunction ScaleToSize(int widthInPixels , int heightInPixels) + { + return delegate(ref int width, ref int height) { + width = widthInPixels; + height = heightInPixels; + }; + } + + /// + /// Scaling function scale both height and width by a specific amount. + /// + /// The by. + /// Scale. + public static ScalingFunction ScaleBy(float scale) + { + return delegate(ref int width, ref int height) { + width = (int)(width * scale); + height = (int)(height * scale); + }; + } + + /// + /// Scaling function to scale to a specifc width while retaining aspect. + /// + /// The to width. + /// Width in pixels. + public static ScalingFunction ScaleToWidth(int widthInPixels) + { + return delegate(ref int width, ref int height) { + height = (int)((height / (float)width) * widthInPixels); + width = widthInPixels; + }; + } + + /// + /// Scaling function to scale to a specific height while retaining aspect. + /// + /// The to height. + /// Height in pixels. + public static ScalingFunction ScaleToHeight(int heightInPixels) + { + return delegate(ref int width, ref int height) { + width = (int)((width / (float)height) * heightInPixels); + height = heightInPixels; + }; + } + + /// + /// Gets dimensions from a webp format block of data. + /// + /// L data. + /// L width. + /// L height. + public static unsafe void GetWebPDimensions(byte[] lData, out int lWidth, out int lHeight) + { + fixed (byte* lDataPtr = lData) + { + lWidth = 0; + lHeight = 0; + if (NativeBindings.WebPGetInfo((IntPtr)lDataPtr, (UIntPtr)lData.Length, ref lWidth, ref lHeight) == 0) + { + throw new Exception("Invalid WebP header detected"); + } + } + } + + /// + /// Loads an image from webp into a byte array in RGBA format. + /// + /// The RGBA from web p. + /// L data. + /// L width. + /// L height. + /// If set to true l mipmaps. + /// L error. + /// Scaling function. + public static unsafe byte[] LoadRGBAFromWebP(byte[] lData, ref int lWidth, ref int lHeight, bool lMipmaps, out Error lError, ScalingFunction scalingFunction = null) + { + lError = 0; + byte[] lRawData = null; + int lLength = lData.Length; + + fixed (byte* lDataPtr = lData) + { + // If we've been supplied a function to alter the width and height, use that now. + if (scalingFunction != null) + { + scalingFunction(ref lWidth, ref lHeight); + } + + // If mipmaps are requested we need to create 1/3 more memory for the mipmaps to be generated in. + int numBytesRequired = lWidth * lHeight * 4; + if (lMipmaps) + { + numBytesRequired = Mathf.CeilToInt((numBytesRequired * 4.0f) / 3.0f); + } + + lRawData = new byte[numBytesRequired]; + fixed (byte* lRawDataPtr = lRawData) + { + int lStride = 4 * lWidth; + + // As we have to reverse the y order of the data, we pass through a negative stride and + // pass through a pointer to the last line of the data. + byte* lTmpDataPtr = lRawDataPtr + (lHeight - 1) * lStride; + + WebPDecoderConfig config = new WebPDecoderConfig(); + + if (NativeBindings.WebPInitDecoderConfig(ref config) == 0) + { + throw new Exception("WebPInitDecoderConfig failed. Wrong version?"); + } + + // Set up decode options + config.options.use_threads = 1; + if (scalingFunction != null) + { + config.options.use_scaling = 1; + } + config.options.scaled_width = lWidth; + config.options.scaled_height = lHeight; + + // read the .webp input file information + VP8StatusCode result = NativeBindings.WebPGetFeatures((IntPtr)lDataPtr, (UIntPtr)lLength, ref config.input); + if (result != VP8StatusCode.VP8_STATUS_OK) + { + throw new Exception(string.Format("Failed WebPGetFeatures with error {0}.", result.ToString())); + } + + // specify the output format + config.output.colorspace = WEBP_CSP_MODE.MODE_RGBA; + config.output.u.RGBA.rgba = (IntPtr)lTmpDataPtr; + config.output.u.RGBA.stride = -lStride; + config.output.u.RGBA.size = (UIntPtr)(lHeight * lStride); + config.output.height = lHeight; + config.output.width = lWidth; + config.output.is_external_memory = 1; + + // Decode + result = NativeBindings.WebPDecode((IntPtr)lDataPtr, (UIntPtr)lLength, ref config); + if (result != VP8StatusCode.VP8_STATUS_OK) + { + throw new Exception(string.Format("Failed WebPDecode with error {0}.", result.ToString())); + } + } + lError = Error.Success; + } + return lRawData; + } + + /// + /// + /// + /// + /// + /// + public static unsafe Texture2D CreateTexture2DFromWebP(byte[] lData, bool lMipmaps, bool lLinear, out Error lError, ScalingFunction scalingFunction = null ) + { + lError = 0; + Texture2D lTexture2D = null; + int lWidth = 0, lHeight = 0; + + GetWebPDimensions(lData, out lWidth, out lHeight); + + byte[] lRawData = LoadRGBAFromWebP(lData, ref lWidth, ref lHeight, lMipmaps, out lError, scalingFunction); + + if (lError == Error.Success) + { + lTexture2D = new Texture2D(lWidth, lHeight, TextureFormat.RGBA32, lMipmaps, lLinear); + lTexture2D.LoadRawTextureData(lRawData); + lTexture2D.Apply(lMipmaps, true); + } + + return lTexture2D; + } + + /// + /// + /// + /// + /// + /// + public static unsafe void LoadWebP(this Texture2D lTexture2D, byte[] lData, out Error lError, ScalingFunction scalingFunction = null) + { + lError = 0; + bool lMipmaps = lTexture2D.mipmapCount != 1; + + int lWidth = 0, lHeight = 0; + + GetWebPDimensions(lData, out lWidth, out lHeight); + + byte[] lRawData = LoadRGBAFromWebP(lData, ref lWidth, ref lHeight, lMipmaps, out lError, scalingFunction); + + if (lError == Error.Success) + { + lTexture2D.LoadRawTextureData(lRawData); + lTexture2D.Apply(lMipmaps, true); + } + } + + /// + /// + /// + /// + /// + /// + public static unsafe byte[] EncodeToWebP(this Texture2D lTexture2D, float lQuality, out Error lError) + { + lError = 0; + + if (lQuality < -1) lQuality = -1; + if (lQuality > 100) lQuality = 100; + + Color32[] lRawColorData = lTexture2D.GetPixels32(); + int lWidth = lTexture2D.width; + int lHeight = lTexture2D.height; + + IntPtr lResult = IntPtr.Zero; + + GCHandle lPinnedArray = GCHandle.Alloc(lRawColorData, GCHandleType.Pinned); + IntPtr lRawDataPtr = lPinnedArray.AddrOfPinnedObject(); + + byte[] lOutputBuffer = null; + + try + { + int lLength; + + if (lQuality == -1) + { + lLength = (int)NativeBindings.WebPEncodeLosslessRGBA(lRawDataPtr, lWidth, lHeight, 4 * lWidth, ref lResult); + } + else + { + lLength = (int)NativeBindings.WebPEncodeRGBA(lRawDataPtr, lWidth, lHeight, 4 * lWidth, lQuality, ref lResult); + } + + if (lLength == 0) + { + throw new Exception("WebP encode failed!"); + } + + lOutputBuffer = new byte[lLength]; + Marshal.Copy(lResult, lOutputBuffer, 0, lLength); + } + finally + { + NativeBindings.WebPSafeFree(lResult); + lPinnedArray.Free(); + } + + return lOutputBuffer; + } + } +}