Sign in to follow this  

LibJpegTurbo, PInvoking C#

Recommended Posts

Is anyone familiar to using LibJpegTurbo with C/C++?
If so, are you able to make a PInvoke for use with C#?

As i myself am totally crap at C/C++, i can´t even get LibJpegTurbo to work in it at all, so i do all my work in C#.
But as would really need to get that Jpeg to work, as the inbuilt .Net jpeg Encoder/Decoder isn´t something to kill for.

I don´t think much need´s to be done, as the work is already there, C# can just import the C code more or less i guess.

Well, any thought about this is appreciated as well.


Share this post

Link to post
Share on other sites

The thing i am asking is, I don´t understand C++, and i wondered if someone could help me PInvoke it to call the compress/decompress features.
I can´t even get it working with C++ alone, it´s just to different from C# even if they look alike.

Share this post

Link to post
Share on other sites

Do you really need that specific library?


Is there some specific functionality that you need that the .net framework's image library does not offer?  


Looking over the code it looks like the only extraordinary claim the library offers is that it has been tuned for specific architectures. For most images of the type used in games you are talking about a performance difference of microseconds.  


Are those microseconds really this important?  That is up to you to decide.



Also, note that few games use jpeg format for images. Most games use image formats that do not require decoding and can be passed directly to the video card, such as dds format images.



That said...


You will need to use "unsafe" c# code, work with byte array buffers, and C-style file objects. You will need to ensure your project and permissions are set to allow unsafe code.


Go download PInvoker from, drop the header file into the tool, extract the few structures you need (JSAMPARRAY, jpeg_decompress_struct, and a few others), look up signatures for the few functions you need (jpeg_start_decompress, jpeg_read_scanlines, jpeg_finish_decompress, and jpeg_destroy_decompress) and use them. I don't see anything particularly difficult about it, other than the fact that you need to make C# placeholders of the structures.

Share this post

Link to post
Share on other sites

The only thing i really need is something that beats the .Net Jpeg, cause it isn´t that great in terms of quality/size/speed. Especially Quality.

Unsafe code is not a problem. The other part however is, my skills doesn´t really allow for it.

I don´t want to (or more like Can) write much code, just to set it to encode an image.
I can understand the settings like (RGB,YUV etc) Quality, and all that stuff.

But not the way it works (as i tried to use it in C/C++ and couldn´t get it to work. And well i don´t even get anything to really work in C++).

But what i got was that, it needs to create a compress, decompress etc, and then destroy them as the end.

I am used to more, simple stuff, like.

Jpegencoder.Encode (Byte[]);

well, something towards that.

But as said, i am not limited to just wanting to get JpegTurbo, but i want something that´s is fast with imagecompression, in terms of encoding and decoding (and the only thing i can think of, is Jpeg, as there seems to be a standstill when it comes to new image formats).

Share this post

Link to post
Share on other sites

Yes i have. It´s Okay, but Libjpeg is likely much better. But sadly, there is no wrapper available.



But well if someone can/has Libjpeg-Turbo working in C#, i would gladly like to know how. Or any other Libjpeg for that matter.

Edited by ZeroWalker

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this