Search code examples
.netjpegfreeimageprogressivelossless

Make an image progressive and lossless with .NET


I need a DLL, or .NET code without an exe file, to make a progressive lossless jpeg (.jpg, not JPEG 2000 or JPEG-LS that are not supported in several browsers) because I'm using a shared hosting where I can't execute .exe files. I'm using freeImage which is supposed to do it but it doesn't produce a lossless jpeg (I have obtained a progressive one).

This is my freeImage code:

image = FreeImage.Load(FREE_IMAGE_FORMAT.FIF_JPEG, inputPath, FREE_IMAGE_LOAD_FLAGS.JPEG_ACCURATE)
FreeImage.Save(FREE_IMAGE_FORMAT.FIF_JPEG, image, outputPath, FREE_IMAGE_SAVE_FLAGS.JPEG_OPTIMIZE Or FREE_IMAGE_SAVE_FLAGS.JPEG_PROGRESSIVE)

It's supposed that the FREE_IMAGE_SAVE_FLAGS.JPEG_OPTIMIZE flag makes the image lossless, but it doesn't work. Am I doing something wrong?

With an exe file, I could solve the problem using jpegtran. To get a lossless progressive jpeg, you only need to execute a shell command from your .NET app or Web.

If you execute this command:

jpegtran -copy none -optimize -progressive inputPath.jpg outputPath.jpg

You'll get a progressive lossless jpeg file

I have used it and it works, but I need to do it without an exe file.


Solution

  • Check out LibJpeg.Net: http://bitmiracle.com/libjpeg/ It is basically the IJG library in C#. Since it's quite a low-level lib (it has to be to fiddle with DCT coefficients) the best way would be get the jpegtrans source, look how they do it there and do the same in LibJpeg.Net (functions have same names, ...)

    EDIT1: you need to use the "Classic" API from LibJpeg.Net. There you need a decopmpressor and compressor, but instead of decoding and encoding the images, you want to pass the DCT coefficients. Look for jpeg_write_coefficients and jpeg_read_coefficients