Coder Social home page Coder Social logo

josepineiro / webp-wrapper Goto Github PK

View Code? Open in Web Editor NEW
213.0 16.0 48.0 5.99 MB

Wrapper for libwebp in C#. The most complete wapper in pure managed C#. Exposes Simple Decoding API, Simple Encoding API, Advanced Encoding API (with stadistis of compresion), Get version library and WebPGetFeatures (info of any WebP file). In the future I´ll update for expose Advanced Decoding API. The wapper are in safe managed code in one class. No need external dll except libwebp.dll (included). The wapper work in 32 and 64 bit system.

License: MIT License

C# 100.00%
webp webp-wrapper

webp-wrapper's Introduction

WebP-wrapper

Wrapper for libwebp in C#. The most complete wrapper in pure managed C#.

Exposes Simple Decoding and Encoding API, Advanced Decoding and Encoding API (with statistics of compression), Get version library and WebPGetFeatures (info of any WebP file). Exposed get PSNR, SSIM or LSIM distortion metrics.

The wrapper is in safe managed code in one class. No need for external dll except libwebp_x86.dll(included v0.4.4) and libwebp_x64.dll (included v1.2.1). The wrapper works in 32, 64 bit or ANY (auto swith to the appropriate library).

The code is commented and includes simple examples for using the wrapper.

Decompress Functions:

Load WebP image for WebP file

using (WebP webp = new WebP())
  Bitmap bmp = webp.Load("test.webp");

Decode WebP filename to bitmap and load in PictureBox container

byte[] rawWebP = File.ReadAllBytes("test.webp");
using (WebP webp = new WebP())
  this.pictureBox.Image = webp.Decode(rawWebP);

Advanced decode WebP filename to bitmap and load in PictureBox container

byte[] rawWebP = File.ReadAllBytes("test.webp");
WebPDecoderOptions decoderOptions = new WebPDecoderOptions();
decoderOptions.use_threads = 1;     //Use multhreading
decoderOptions.flip = 1;   			//Flip the image
using (WebP webp = new WebP())
  this.pictureBox.Image = webp.Decode(rawWebP, decoderOptions);

Get thumbnail with 200x150 pixels in fast/low quality mode

using (WebP webp = new WebP())
	this.pictureBox.Image = webp.GetThumbnailFast(rawWebP, 200, 150);

Get thumbnail with 200x150 pixels in slow/high quality mode

using (WebP webp = new WebP())
	this.pictureBox.Image = webp.GetThumbnailQuality(rawWebP, 200, 150);

Compress Functions:

Save bitmap to WebP file

Bitmap bmp = new Bitmap("test.jpg");
using (WebP webp = new WebP())
  webp.Save(bmp, 80, "test.webp");

Encode to memory buffer in lossy mode with quality 75 and save to file

byte[] rawWebP = File.ReadAllBytes("test.jpg");
using (WebP webp = new WebP())
  rawWebP = webp.EncodeLossy(bmp, 75);
File.WriteAllBytes("test.webp", rawWebP); 

Encode to memory buffer in lossy mode with quality 75 and speed 9. Save to file

byte[] rawWebP = File.ReadAllBytes("test.jpg");
using (WebP webp = new WebP())
  rawWebP = webp.EncodeLossy(bmp, 75, 9);
File.WriteAllBytes("test.webp", rawWebP); 

Encode to memory buffer in lossy mode with quality 75, speed 9 and get information. Save to file

byte[] rawWebP = File.ReadAllBytes("test.jpg");
using (WebP webp = new WebP())
  rawWebP = webp.EncodeLossy(bmp, 75, 9, true);
File.WriteAllBytes("test.webp", rawWebP); 

Encode to memory buffer in lossless mode and save to file

byte[] rawWebP = File.ReadAllBytes("test.jpg");
using (WebP webp = new WebP())
  rawWebP = webp.EncodeLossless(bmp);
File.WriteAllBytes("test.webp", rawWebP); 

Encode to memory buffer in lossless mode with speed 9 and save to file

byte[] rawWebP = File.ReadAllBytes("test.jpg");
using (WebP webp = new WebP())
  rawWebP = webp.EncodeLossless(bmp, 9);
File.WriteAllBytes("test.webp", rawWebP); 

Encode to memory buffer in near lossless mode with quality 40 and speed 9 and save to file

byte[] rawWebP = File.ReadAllBytes("test.jpg");
using (WebP webp = new WebP())
  rawWebP = webp.EncodeNearLossless(bmp, 40, 9);
File.WriteAllBytes("test.webp", rawWebP); 

Another Functions:

Get version of libwebp.dll

using (WebP webp = new WebP())
  string version = "libwebp.dll v" + webp.GetVersion();

Get info from WebP file

byte[] rawWebp = File.ReadAllBytes(pathFileName);
using (WebP webp = new WebP())
  webp.GetInfo(rawWebp, out width, out height, out has_alpha, out has_animation, out format);
MessageBox.Show("Width: " + width + "\n" +
                "Height: " + height + "\n" +
                "Has alpha: " + has_alpha + "\n" +
                "Is animation: " + has_animation + "\n" +
                "Format: " + format);

Get PSNR, SSIM or LSIM distortion metric between two pictures

int metric = 0;  //0 = PSNR, 1= SSIM, 2=LSIM
Bitmap bmp1 = Bitmap.FromFile("image1.png");
Bitmap bmp2 = Bitmap.FromFile("image2.png");
using (WebP webp = new WebP())
	result = webp.GetPictureDistortion(source, reference, metric);
	                    MessageBox.Show("Red: " + result[0] + "dB.\nGreen: " + result[1] + "dB.\nBlue: " + result[2] + "dB.\nAlpha: " + result[3] + "dB.\nAll: " + result[4] + "dB.", "PSNR");

MessageBox.Show("Red: " + result[0] + dB\n" +
                "Green: " + result[1] + "dB\n" +
                "Blue: " + result[2] + "dB\n" +
                "Alpha: " + result[3] + "dB\n" +
                "All: " + result[4] + "dB\n");

Without their help this wapper would not have been possible.

webp-wrapper's People

Contributors

adamtreineke avatar davidrutten avatar josepineiro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

webp-wrapper's Issues

Presets settings don't work

Hi.

I am still discovering your useful GUI tool, I could encode many .png files to .webp files easily. Thanks!

But I noticed that the Preset Settings drop-down menu does nothing. It doesn't change slidebars (sharpness, compression method, deblocking filter strength).

Can't load saved WEBP file

I am getting this error when trying to load a saved WEBP file:

Exception thrown: 'System.Exception' in quick-picture-viewer.exe
Failed WebPGetFeatures with error VP8_STATUS_BITSTREAM_ERROR
In WebP.Decode

This code I am using to open the JPG image:

Bitmap bmp = new Bitmap(path);

This code I am using to save the WEBP image:

using (WebP webp = new WebP()) webp.Save(bmp, path);

This code I am using to load the WEBP image:

byte[] rawWebP = File.ReadAllBytes(path);
using (WebP webp = new WebP())
{
	WebPDecoderOptions decoderOptions = new WebPDecoderOptions();
	decoderOptions.use_threads = 1;
	decoderOptions.alpha_dithering_strength = 100;
	Bitmap bmp = webp.Decode(rawWebP, decoderOptions); // error here
}

This image that I am trying to save and load: image.zip

P.S.
Looks like Windows Explorer is rendering the thumbnail of the saved WEBP file correctly so I think there are no issues with the saving function:

image

Webp decode to Stream

WebP webp = new WebP();

string pathFileName = openFileDialog.FileName;
//decode to byte[]
byte[] rawWebp = File.ReadAllBytes(pathFileName);
webp.GetInfo(rawWebp, out width, out height, out has_alpha, out has_animation, out format);

//is possible ?
MemoryStream ms = new MemoryStream(rawWebp);

wrapper code for decoding animated webp

Hello,

I'd like to contribute wrapper code for the WebPAnimDecoder API to enable decoding of Animated WebP files.

For that to work it needs the WebP DLL libwebpdemux.dll that depends on libwebp.dll. But sticking to the current way the auto switch is done and naming the file libwebpdemux_x64.dll doesn't work, because it expects to find the dependency with its original file name.
One solution is to move the files to subfolders and specify them in the DllImport attributes:

[DllImport(@"x64\libwebp.dll"]
[DllImport(@"x64\libwebpdemux.dll"]

It works and also the dependencies can be loaded. But maybe not the best solution.

The second solution uses the fact that DLLs aren't loaded into the process again if they have already been loaded before. So we can just preload the DLLs from the correct subfolder:

string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), IntPtr.Size == 4 ? "x86" : "x64");
LoadLibrary(Path.Combine(path, "libwebp.dll"));
LoadLibrary(Path.Combine(path, "libwebpdemux.dll"));

Would that be okay for you?

So first I'd like to create a PR to rename and move the DLLs to the respective subfolders.

Besides, are there any special reasons why the 32- and 64-bit version of libwebp.dll aren't used in the same version (number)?

add refrence

not able to add in refrence c# error "Please make sure that the file is accessible and that it is a valid assembly or COM component."

License

This repo mentions the MIT license but the header in WebPWrapper.cs mentions:

/// Wrapper for WebP format in C#. (GPL) Jose M. Piñeiro

So under which license is this code?

WebP-wrapper

I tried the example attached to the WebP-wrapper project, (Webp-wrapper-master) using Visual Studio; well it works.

I decide to create a new project to check, I add WebpWrapper.cs to the new project.

problem: I can't add libwebp_x86.dll and libwebp_x64.dll to the references (Visual Studio reports * can't add a reference to libwebp_x86.dll verify that the file is accessible and that the assembly or COM component is valid

It also occurs if to the example attached to the project I delete the dlls from References and try to add them again.

I need help figuring out how to add the DLLs to References.

Suggestion : Change licence to LGPL

Hi,
I just have a suggestion/polite request:
Is it possible to change current licence from GPL 3 to LGPL 3 ?

GPL 3 is no-go for a lot of use-cases (especially for other open-source projects).

Thanks.

Parameter is not valid at webp.Decode(byte[], WebPDecoderOptions)

Hello.
I get an error when I try to use the decoder options.

Code:

image

Output:

Exception thrown: 'System.ArgumentException' in System.Drawing.dll
Exception thrown: 'System.Exception' in quick-picture-viewer.exe
System.Exception: Parameter is not valid.
In WebP.Decode
at quick_picture_viewer.WebP.Decode(Byte[] rawWebP, WebPDecoderOptions options) in D:\Projects\DotNet\quick-picture-viewer\quick-picture-viewer\WebPWrapper.cs:line 177
at quick_picture_viewer.MainForm.openFile(String path) in D:\Projects\DotNet\quick-picture-viewer\quick-picture-viewer\MainForm.cs:line 207

Where do I put the DLL files in an ASP.NET application

When I try to simply load a webp file into a bitmap, I get the error:

Unable to load DLL 'libwebp_x86.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
In WebP.GetInfo
In WebP.Load

I am using ASP.NET version 4.8 and so far I have placed DLL files in /bin folder. I also tried /bin/Release and / (root), but without success.

Where is BGRA methods

I have many PNG files with transparent background and i need to convert them to WebP files.
I download the source code then run release version and select a file and out put was good but when i open the solution and run in debug mode, PNG background changed to green. i decompile the release version and i find the problem ... there was no BGRA method in UnsafeNativeMethods class!!!

type_mismatch of return_value from DLL

/// <returns>output_buffer if function succeeds; NULL otherwise</returns>

/// <returns>output_buffer if function succeeds; NULL otherwise</returns>

I think the following:

public static int WebPDecodeBGRInto(IntPtr data, int data_size, IntPtr output_buffer, int output_buffer_size, int output_stride)

must be
public static IntPtr WebPDecodeBGRInto(IntPtr data, int data_size, IntPtr output_buffer, int output_buffer_size, int output_stride)
and
public static int WebPDecodeBGRAInto(IntPtr data, int data_size, IntPtr output_buffer, int output_buffer_size, int output_stride)

must be
public static IntPtr WebPDecodeBGRAInto(IntPtr data, int data_size, IntPtr output_buffer, int output_buffer_size, int output_stride)

Some other lines related to this also need to be changed.

On Windows x86, int and IntPtr are same size.
But on Windows x64, they are different size.

Question for using in ASP.NET Web Application

I have an ASP.NET application using .NET Core 3.1 and I'm wondering where I should put the .dll files. There is a bin/Release folder in Project.WebApi so, should I add them there?

System.NullReferenceException in UnsafeNativeMethods.OnCallback

UnsafeNativeMethods.OnCallback = new UnsafeNativeMethods.WebPMemoryWrite(MyWriter); is causing an exception:

System.NullReferenceException Object reference not set to an instance of an object. at UnsafeNativeMethods.WebPEncode_x64(WebPConfig& config, WebPPicture& picture) at WebP.AdvancedEncode(Bitmap bmp, WebPConfig config, Boolean info)

I can only seem to reproduce this on a server; running in development mode works fine.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.