Webp in c#

1,210 views
Skip to first unread message

diosmede...@gmail.com

unread,
May 13, 2014, 5:30:32 PM5/13/14
to webp-d...@webmproject.org
I´m interesting in use WEBP in c#, but i don´t found a dll.

When compiled libwebp to dinamic link, can not importd the functions in c#

Thanks

James Zern

unread,
May 13, 2014, 11:36:26 PM5/13/14
to webp-d...@webmproject.org, diosmede...@gmail.com
Hi,


On Tuesday, May 13, 2014 2:30:32 PM UTC-7, diosmede...@gmail.com wrote:
I´m interesting in use WEBP in c#, but i don´t found a dll.


You can build libwebp.dll using Makefile.vc:
> nmake /f Makefile.vc CFG=release-dynamic

This will set WEBP_EXTERN properly to export the API functions. 
 
When compiled libwebp to dinamic link, can not importd the functions in c#


Note if you use the simple API you should call WebPFree() to free any buffers returned. 

[DllImport("libwebp.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int WebPEncodeBGR(IntPtr rgb, int width, int height, int stride, float quality_factor, out IntPtr output);

[DllImport("libwebp.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int WebPFree(IntPtr p);
 
Thanks

diosmede...@gmail.com

unread,
May 16, 2014, 5:31:29 PM5/16/14
to webp-d...@webmproject.org, diosmede...@gmail.com

Thanks for your answer. This help me a lot.

I was compiled the last library (libwebp-0.4.0.tar.gz) in a dll, referenced API functions and make a simple test.

I can compress the image in lossy and lossless mode. Work very well.
The compressed WebP can be open with Crome.

I can decompress the image. In one image (good.jpg) work but in another (bad.jpg) don´t work.

The code for descompress function:

        void D2WebP(string fileName, out Bitmap bmp)
        {
            int intReturn;
            int imgWidth = 0;
            int imgHeight = 0;
            IntPtr outputBuffer = IntPtr.Zero;
            int outputBufferSize;
            bmp = null;

            //Read webP file
            byte[] arrWebP = File.ReadAllBytes(fileName);

            //Get image width and height
            GCHandle pinnedWebP = GCHandle.Alloc(arrWebP, GCHandleType.Pinned);
            IntPtr ptrData = pinnedWebP.AddrOfPinnedObject();
            UInt32 dataSize = (uint)arrWebP.Length;
            intReturn = WebPGetInfo(ptrData, dataSize, ref imgWidth, ref imgHeight);
            if (intReturn != 1) return;

            //Allocate memory for uncompress image
            outputBufferSize = imgWidth * imgHeight * Image.GetPixelFormatSize(PixelFormat.Format24bppRgb) / 8;
            outputBuffer = Marshal.AllocHGlobal(outputBufferSize);

            //Uncompress the image
            outputBuffer = WebPDecodeBGR(ptrData, dataSize, ref imgWidth, ref imgHeight);

            //Create a BitmapData and Lock all pixels to be written
            bmp = new Bitmap(imgWidth, imgHeight, PixelFormat.Format24bppRgb);
            BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);

            //Wtite image to bitmap
            CopyMemory(bmpData.Scan0, outputBuffer, (uint)outputBufferSize);

            //Unlock the pixels
            bmp.UnlockBits(bmpData);

            //Free memory
            pinnedWebP.Free();
            Marshal.FreeHGlobal(outputBuffer);
        }

Append to this message:
sources.zip - The code, compiled dll, the working image and the not working image.
compiled.zip - The compiles exe in 32bits for .NET 3.5, compiled dll, the working image and the not working image.

Are bad the lib? Are bad my code?
Can you help me?

Thanks in advance.
sources.zip
compiled.zip

James Zern

unread,
May 19, 2014, 11:59:16 PM5/19/14
to webp-d...@webmproject.org, diosmede...@gmail.com
Hi,


On Friday, May 16, 2014 2:31:29 PM UTC-7, diosmede...@gmail.com wrote:

Thanks for your answer. This help me a lot.


Sorry for the delayed response, I see 2 issues on my side.
 
I was compiled the last library (libwebp-0.4.0.tar.gz) in a dll, referenced API functions and make a simple test.

I can compress the image in lossy and lossless mode. Work very well.
The compressed WebP can be open with Crome.

I can decompress the image. In one image (good.jpg) work but in another (bad.jpg) don´t work.

I see corruption (bad blocks) in the lossless encode of good.jpg using the latest code, but it sounds like this is OK for you, so I'll deal with that separately.
bad.jpg has a width of 482, which is not divisible by 4, this is a bitmap requirement, so writing out the rectangle as is will show stride problems. The solution will be to write out each row and pad the remaining (2 bytes in this case) with zeroes. Have a look at WriteBMP() in dwebp.c to get an idea.
Reply all
Reply to author
Forward
0 new messages